blob: 145715def8487b3e7b6d91bcc78822be83996b54 [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{
Eric Laurent93575202011-01-18 18:39:02 -0800222 Mutex::Autolock _l(mLock);
223
Dima Zavin799a70e2011-04-18 16:57:27 -0700224 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800225 char val_str[PROPERTY_VALUE_MAX] = { 0 };
226 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
227 uint32_t int_val;
228 if (1 == sscanf(val_str, "%u", &int_val)) {
229 mStandbyTimeInNsecs = milliseconds(int_val);
230 ALOGI("Using %u mSec as standby time.", int_val);
231 } else {
232 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
233 ALOGI("Using default %u mSec as standby time.",
234 (uint32_t)(mStandbyTimeInNsecs / 1000000));
235 }
236 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700237
Eric Laurent1c333e22014-05-20 10:48:17 -0700238 mPatchPanel = new PatchPanel(this);
239
Eric Laurenta4c5a552012-03-29 10:12:40 -0700240 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700241}
242
243AudioFlinger::~AudioFlinger()
244{
245 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700246 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700247 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700248 }
249 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700250 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700251 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700252 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700253
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800254 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
255 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700256 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
257 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700258 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700259
260 // Tell media.log service about any old writers that still need to be unregistered
Andy Hungce717682015-12-22 13:40:32 -0800261 if (mLogMemoryDealer != 0) {
262 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
263 if (binder != 0) {
264 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
265 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
266 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
267 mUnregisteredWriters.pop();
268 mediaLogService->unregisterWriter(iMemory);
269 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700270 }
271 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700272}
273
Eric Laurenta4c5a552012-03-29 10:12:40 -0700274static const char * const audio_interfaces[] = {
275 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
276 AUDIO_HARDWARE_MODULE_ID_A2DP,
277 AUDIO_HARDWARE_MODULE_ID_USB,
278};
279#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
280
Phil Burk062e67a2015-02-11 13:40:50 -0800281AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700282 audio_module_handle_t module,
283 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700284{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700285 // if module is 0, the request comes from an old policy manager and we should load
286 // well known modules
287 if (module == 0) {
288 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
289 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
290 loadHwModule_l(audio_interfaces[i]);
291 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700292 // then try to find a module supporting the requested device.
293 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
294 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
295 audio_hw_device_t *dev = audioHwDevice->hwDevice();
296 if ((dev->get_supported_devices != NULL) &&
297 (dev->get_supported_devices(dev) & devices) == devices)
298 return audioHwDevice;
299 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700300 } else {
301 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700302 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
303 if (audioHwDevice != NULL) {
304 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700305 }
306 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700307
Dima Zavin799a70e2011-04-18 16:57:27 -0700308 return NULL;
309}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310
Glenn Kasten0f11b512014-01-31 16:18:54 -0800311void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700312{
313 const size_t SIZE = 256;
314 char buffer[SIZE];
315 String8 result;
316
317 result.append("Clients:\n");
318 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800319 sp<Client> client = mClients.valueAt(i).promote();
320 if (client != 0) {
321 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
322 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323 }
324 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700325
Eric Laurentd1b28d42013-09-18 18:47:13 -0700326 result.append("Notification Clients:\n");
327 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
328 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
329 result.append(buffer);
330 }
331
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700332 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800333 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700334 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
335 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800336 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700337 result.append(buffer);
338 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700339 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700340}
341
342
Glenn Kasten0f11b512014-01-31 16:18:54 -0800343void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700344{
345 const size_t SIZE = 256;
346 char buffer[SIZE];
347 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800348 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349
John Grossman4ff14ba2012-02-08 16:37:41 -0800350 snprintf(buffer, SIZE, "Hardware status: %d\n"
351 "Standby Time mSec: %u\n",
352 hardwareStatus,
353 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700354 result.append(buffer);
355 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700356}
357
Glenn Kasten0f11b512014-01-31 16:18:54 -0800358void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700359{
360 const size_t SIZE = 256;
361 char buffer[SIZE];
362 String8 result;
363 snprintf(buffer, SIZE, "Permission Denial: "
364 "can't dump AudioFlinger from pid=%d, uid=%d\n",
365 IPCThreadState::self()->getCallingPid(),
366 IPCThreadState::self()->getCallingUid());
367 result.append(buffer);
368 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700369}
370
Eric Laurent81784c32012-11-19 14:55:58 -0800371bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700372{
373 bool locked = false;
374 for (int i = 0; i < kDumpLockRetries; ++i) {
375 if (mutex.tryLock() == NO_ERROR) {
376 locked = true;
377 break;
378 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800379 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700380 }
381 return locked;
382}
383
384status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
385{
Glenn Kasten44deb052012-02-05 18:09:08 -0800386 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700387 dumpPermissionDenial(fd, args);
388 } else {
389 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800390 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700391 if (!hardwareLocked) {
392 String8 result(kHardwareLockedString);
393 write(fd, result.string(), result.size());
394 } else {
395 mHardwareLock.unlock();
396 }
397
Eric Laurent81784c32012-11-19 14:55:58 -0800398 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700399
400 // failed to lock - AudioFlinger is probably deadlocked
401 if (!locked) {
402 String8 result(kDeadlockedString);
403 write(fd, result.string(), result.size());
404 }
405
Eric Laurent021cf962014-05-13 10:18:14 -0700406 bool clientLocked = dumpTryLock(mClientLock);
407 if (!clientLocked) {
408 String8 result(kClientLockedString);
409 write(fd, result.string(), result.size());
410 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700411
412 EffectDumpEffects(fd);
413
Mathias Agopian65ab4712010-07-14 17:59:35 -0700414 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700415 if (clientLocked) {
416 mClientLock.unlock();
417 }
418
Mathias Agopian65ab4712010-07-14 17:59:35 -0700419 dumpInternals(fd, args);
420
421 // dump playback threads
422 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
423 mPlaybackThreads.valueAt(i)->dump(fd, args);
424 }
425
426 // dump record threads
427 for (size_t i = 0; i < mRecordThreads.size(); i++) {
428 mRecordThreads.valueAt(i)->dump(fd, args);
429 }
430
Eric Laurentaaa44472014-09-12 17:41:50 -0700431 // dump orphan effect chains
432 if (mOrphanEffectChains.size() != 0) {
433 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
434 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
435 mOrphanEffectChains.valueAt(i)->dump(fd, args);
436 }
437 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700438 // dump all hardware devs
439 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700440 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700441 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700442 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700443
Glenn Kasten46909e72013-02-26 09:20:22 -0800444#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700445 // dump the serially shared record tee sink
446 if (mRecordTeeSource != 0) {
447 dumpTee(fd, mRecordTeeSource);
448 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800449#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700450
Glenn Kastend65d73c2012-06-22 17:21:07 -0700451 if (locked) {
452 mLock.unlock();
453 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800454
455 // append a copy of media.log here by forwarding fd to it, but don't attempt
456 // to lookup the service if it's not running, as it will block for a second
457 if (mLogMemoryDealer != 0) {
458 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
459 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700460 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800461 Vector<String16> args;
462 binder->dump(fd, args);
463 }
464 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700465 }
466 return NO_ERROR;
467}
468
Eric Laurent021cf962014-05-13 10:18:14 -0700469sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800470{
Eric Laurent021cf962014-05-13 10:18:14 -0700471 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800472 // If pid is already in the mClients wp<> map, then use that entry
473 // (for which promote() is always != 0), otherwise create a new entry and Client.
474 sp<Client> client = mClients.valueFor(pid).promote();
475 if (client == 0) {
476 client = new Client(this, pid);
477 mClients.add(pid, client);
478 }
479
480 return client;
481}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700482
Glenn Kasten9e58b552013-01-18 15:09:48 -0800483sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
484{
Glenn Kasten481fb672013-09-30 14:39:28 -0700485 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800486 if (mLogMemoryDealer == 0) {
487 return new NBLog::Writer();
488 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800489 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700490 // Similarly if we can't contact the media.log service, also return a dummy writer
491 if (binder == 0) {
492 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800493 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700494 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
495 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
496 // If allocation fails, consult the vector of previously unregistered writers
497 // and garbage-collect one or more them until an allocation succeeds
498 if (shared == 0) {
499 Mutex::Autolock _l(mUnregisteredWritersLock);
500 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
501 {
502 // Pick the oldest stale writer to garbage-collect
503 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
504 mUnregisteredWriters.removeAt(0);
505 mediaLogService->unregisterWriter(iMemory);
506 // Now the media.log remote reference to IMemory is gone. When our last local
507 // reference to IMemory also drops to zero at end of this block,
508 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
509 }
510 // Re-attempt the allocation
511 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
512 if (shared != 0) {
513 goto success;
514 }
515 }
516 // Even after garbage-collecting all old writers, there is still not enough memory,
517 // so return a dummy writer
518 return new NBLog::Writer();
519 }
520success:
521 mediaLogService->registerWriter(shared, size, name);
522 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800523}
524
525void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
526{
Glenn Kasten685ef092013-02-04 08:15:34 -0800527 if (writer == 0) {
528 return;
529 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800530 sp<IMemory> iMemory(writer->getIMemory());
531 if (iMemory == 0) {
532 return;
533 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700534 // Rather than removing the writer immediately, append it to a queue of old writers to
535 // be garbage-collected later. This allows us to continue to view old logs for a while.
536 Mutex::Autolock _l(mUnregisteredWritersLock);
537 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800538}
539
Mathias Agopian65ab4712010-07-14 17:59:35 -0700540// IAudioFlinger interface
541
542
543sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800544 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700545 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800546 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700547 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800548 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800549 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800551 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800552 pid_t tid,
Glenn Kastend848eb42016-03-08 13:42:11 -0800553 audio_session_t *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800554 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700555 status_t *status)
556{
557 sp<PlaybackThread::Track> track;
558 sp<TrackHandle> trackHandle;
559 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700560 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -0800561 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700562
Glenn Kasten263709e2012-01-06 08:40:01 -0800563 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
564 // but if someone uses binder directly they could bypass that and cause us to crash
565 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000566 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700567 lStatus = BAD_VALUE;
568 goto Exit;
569 }
570
Glenn Kasten53b5d092014-02-05 10:00:23 -0800571 // further sample rate checks are performed by createTrack_l() depending on the thread type
572 if (sampleRate == 0) {
573 ALOGE("createTrack() invalid sample rate %u", sampleRate);
574 lStatus = BAD_VALUE;
575 goto Exit;
576 }
577
578 // further channel mask checks are performed by createTrack_l() depending on the thread type
579 if (!audio_is_output_channel(channelMask)) {
580 ALOGE("createTrack() invalid channel mask %#x", channelMask);
581 lStatus = BAD_VALUE;
582 goto Exit;
583 }
584
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700585 // further format checks are performed by createTrack_l() depending on the thread type
586 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800587 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700588 lStatus = BAD_VALUE;
589 goto Exit;
590 }
591
Glenn Kasten663c2242013-09-24 11:52:37 -0700592 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
593 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
594 lStatus = BAD_VALUE;
595 goto Exit;
596 }
597
Mathias Agopian65ab4712010-07-14 17:59:35 -0700598 {
599 Mutex::Autolock _l(mLock);
600 PlaybackThread *thread = checkPlaybackThread_l(output);
601 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800602 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700603 lStatus = BAD_VALUE;
604 goto Exit;
605 }
606
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800607 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -0700608 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700609
Glenn Kastene848bd92014-03-13 15:00:32 -0700610 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700611 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -0800612 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
613 ALOGE("createTrack() invalid session ID %d", *sessionId);
614 lStatus = BAD_VALUE;
615 goto Exit;
616 }
Glenn Kasten570f6332014-03-13 15:01:06 -0700617 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700618 // check if an effect chain with the same session ID is present on another
619 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700620 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700621 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
622 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700623 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700624 if (sessions & PlaybackThread::EFFECT_SESSION) {
625 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700626 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700627 }
Eric Laurentde070132010-07-13 04:45:46 -0700628 }
629 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700630 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700631 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -0800632 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633 if (sessionId != NULL) {
634 *sessionId = lSessionId;
635 }
636 }
Steve Block3856b092011-10-20 11:56:00 +0100637 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700638
639 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800640 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800641 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700642 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700643
644 // move effect chain to this output thread if an effect on same session was waiting
645 // for a track to be created
646 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700647 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700648 Mutex::Autolock _dl(thread->mLock);
649 Mutex::Autolock _sl(effectThread->mLock);
650 moveEffectChain_l(lSessionId, effectThread, thread, true);
651 }
Eric Laurenta011e352012-03-29 15:51:43 -0700652
653 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700654 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700655 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
656 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700657 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700658 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700659 } else {
660 mPendingSyncEvents[i]->cancel();
661 }
Eric Laurenta011e352012-03-29 15:51:43 -0700662 mPendingSyncEvents.removeAt(i);
663 i--;
664 }
665 }
666 }
Glenn Kastene198c362013-08-13 09:13:36 -0700667
Glenn Kastend848eb42016-03-08 13:42:11 -0800668 setAudioHwSyncForSession_l(thread, lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700669 }
Glenn Kastene198c362013-08-13 09:13:36 -0700670
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700671 if (lStatus != NO_ERROR) {
672 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700673 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700674 // Don't hold mClientLock when releasing the reference on the track as the
675 // destructor will acquire it.
676 {
677 Mutex::Autolock _cl(mClientLock);
678 client.clear();
679 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700680 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700681 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700682 }
683
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700684 // return handle to client
685 trackHandle = new TrackHandle(track);
686
Mathias Agopian65ab4712010-07-14 17:59:35 -0700687Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700688 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700689 return trackHandle;
690}
691
Glenn Kasten2c073da2016-02-26 09:14:08 -0800692uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700693{
694 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800695 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700696 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800697 ALOGW("sampleRate() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700698 return 0;
699 }
700 return thread->sampleRate();
701}
702
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800703audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704{
705 Mutex::Autolock _l(mLock);
706 PlaybackThread *thread = checkPlaybackThread_l(output);
707 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000708 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800709 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700710 }
711 return thread->format();
712}
713
Glenn Kasten2c073da2016-02-26 09:14:08 -0800714size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715{
716 Mutex::Autolock _l(mLock);
Glenn Kasten2c073da2016-02-26 09:14:08 -0800717 ThreadBase *thread = checkThread_l(ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700718 if (thread == NULL) {
Glenn Kasten2c073da2016-02-26 09:14:08 -0800719 ALOGW("frameCount() unknown thread %d", ioHandle);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720 return 0;
721 }
Glenn Kasten58912562012-04-03 10:45:00 -0700722 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
723 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700724 return thread->frameCount();
725}
726
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800727uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700728{
729 Mutex::Autolock _l(mLock);
730 PlaybackThread *thread = checkPlaybackThread_l(output);
731 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800732 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700733 return 0;
734 }
735 return thread->latency();
736}
737
738status_t AudioFlinger::setMasterVolume(float value)
739{
Eric Laurenta1884f92011-08-23 08:25:03 -0700740 status_t ret = initCheck();
741 if (ret != NO_ERROR) {
742 return ret;
743 }
744
Mathias Agopian65ab4712010-07-14 17:59:35 -0700745 // check calling permissions
746 if (!settingsAllowed()) {
747 return PERMISSION_DENIED;
748 }
749
Eric Laurenta4c5a552012-03-29 10:12:40 -0700750 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700751 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700752
John Grossmanee578c02012-07-23 17:05:46 -0700753 // Set master volume in the HALs which support it.
754 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
755 AutoMutex lock(mHardwareLock);
756 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800757
John Grossmanee578c02012-07-23 17:05:46 -0700758 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
759 if (dev->canSetMasterVolume()) {
760 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800761 }
John Grossmanee578c02012-07-23 17:05:46 -0700762 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700763 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700764
John Grossmanee578c02012-07-23 17:05:46 -0700765 // Now set the master volume in each playback thread. Playback threads
766 // assigned to HALs which do not have master volume support will apply
767 // master volume during the mix operation. Threads with HALs which do
768 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700769 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
770 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
771 continue;
772 }
John Grossmanee578c02012-07-23 17:05:46 -0700773 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700774 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700775
776 return NO_ERROR;
777}
778
Glenn Kastenf78aee72012-01-04 11:00:47 -0800779status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700780{
Eric Laurenta1884f92011-08-23 08:25:03 -0700781 status_t ret = initCheck();
782 if (ret != NO_ERROR) {
783 return ret;
784 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700785
786 // check calling permissions
787 if (!settingsAllowed()) {
788 return PERMISSION_DENIED;
789 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800790 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000791 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700792 return BAD_VALUE;
793 }
794
795 { // scope for the lock
796 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700797 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700798 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700799 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700800 mHardwareStatus = AUDIO_HW_IDLE;
801 }
802
803 if (NO_ERROR == ret) {
804 Mutex::Autolock _l(mLock);
805 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800806 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700807 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700808 }
809
810 return ret;
811}
812
813status_t AudioFlinger::setMicMute(bool state)
814{
Eric Laurenta1884f92011-08-23 08:25:03 -0700815 status_t ret = initCheck();
816 if (ret != NO_ERROR) {
817 return ret;
818 }
819
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820 // check calling permissions
821 if (!settingsAllowed()) {
822 return PERMISSION_DENIED;
823 }
824
825 AutoMutex lock(mHardwareLock);
826 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700827 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
828 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
829 status_t result = dev->set_mic_mute(dev, state);
830 if (result != NO_ERROR) {
831 ret = result;
832 }
833 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700834 mHardwareStatus = AUDIO_HW_IDLE;
835 return ret;
836}
837
838bool AudioFlinger::getMicMute() const
839{
Eric Laurenta1884f92011-08-23 08:25:03 -0700840 status_t ret = initCheck();
841 if (ret != NO_ERROR) {
842 return false;
843 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800844 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700845 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800846 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700847 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800848 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
849 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
850 status_t result = dev->get_mic_mute(dev, &state);
851 if (result == NO_ERROR) {
852 mute = mute && state;
853 }
854 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700855 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800856
857 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700858}
859
860status_t AudioFlinger::setMasterMute(bool muted)
861{
John Grossmand8f178d2012-07-20 14:51:35 -0700862 status_t ret = initCheck();
863 if (ret != NO_ERROR) {
864 return ret;
865 }
866
Mathias Agopian65ab4712010-07-14 17:59:35 -0700867 // check calling permissions
868 if (!settingsAllowed()) {
869 return PERMISSION_DENIED;
870 }
871
John Grossmanee578c02012-07-23 17:05:46 -0700872 Mutex::Autolock _l(mLock);
873 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700874
John Grossmanee578c02012-07-23 17:05:46 -0700875 // Set master mute in the HALs which support it.
876 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
877 AutoMutex lock(mHardwareLock);
878 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700879
John Grossmanee578c02012-07-23 17:05:46 -0700880 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
881 if (dev->canSetMasterMute()) {
882 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700883 }
John Grossmanee578c02012-07-23 17:05:46 -0700884 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700885 }
886
John Grossmanee578c02012-07-23 17:05:46 -0700887 // Now set the master mute in each playback thread. Playback threads
888 // assigned to HALs which do not have master mute support will apply master
889 // mute during the mix operation. Threads with HALs which do support master
890 // mute will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700891 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
892 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
893 continue;
894 }
John Grossmanee578c02012-07-23 17:05:46 -0700895 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700896 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700897
898 return NO_ERROR;
899}
900
901float AudioFlinger::masterVolume() const
902{
Glenn Kasten98067102011-12-13 11:47:54 -0800903 Mutex::Autolock _l(mLock);
904 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700905}
906
907bool AudioFlinger::masterMute() const
908{
Glenn Kasten98067102011-12-13 11:47:54 -0800909 Mutex::Autolock _l(mLock);
910 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911}
912
John Grossman4ff14ba2012-02-08 16:37:41 -0800913float AudioFlinger::masterVolume_l() const
914{
John Grossman4ff14ba2012-02-08 16:37:41 -0800915 return mMasterVolume;
916}
917
John Grossmand8f178d2012-07-20 14:51:35 -0700918bool AudioFlinger::masterMute_l() const
919{
John Grossmanee578c02012-07-23 17:05:46 -0700920 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700921}
922
Eric Laurent223fd5c2014-11-11 13:43:36 -0800923status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
924{
925 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
926 ALOGW("setStreamVolume() invalid stream %d", stream);
927 return BAD_VALUE;
928 }
929 pid_t caller = IPCThreadState::self()->getCallingPid();
930 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
931 ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
932 return PERMISSION_DENIED;
933 }
934
935 return NO_ERROR;
936}
937
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800938status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
939 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940{
941 // check calling permissions
942 if (!settingsAllowed()) {
943 return PERMISSION_DENIED;
944 }
945
Eric Laurent223fd5c2014-11-11 13:43:36 -0800946 status_t status = checkStreamType(stream);
947 if (status != NO_ERROR) {
948 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700949 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800950 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700951
952 AutoMutex lock(mLock);
953 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700954 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700955 thread = checkPlaybackThread_l(output);
956 if (thread == NULL) {
957 return BAD_VALUE;
958 }
959 }
960
961 mStreamTypes[stream].volume = value;
962
963 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800964 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700965 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700966 }
967 } else {
968 thread->setStreamVolume(stream, value);
969 }
970
971 return NO_ERROR;
972}
973
Glenn Kastenfff6d712012-01-12 16:38:12 -0800974status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700975{
976 // check calling permissions
977 if (!settingsAllowed()) {
978 return PERMISSION_DENIED;
979 }
980
Eric Laurent223fd5c2014-11-11 13:43:36 -0800981 status_t status = checkStreamType(stream);
982 if (status != NO_ERROR) {
983 return status;
984 }
985 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
986
987 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000988 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700989 return BAD_VALUE;
990 }
991
Eric Laurent93575202011-01-18 18:39:02 -0800992 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700993 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700994 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700995 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700996
997 return NO_ERROR;
998}
999
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001000float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001001{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001002 status_t status = checkStreamType(stream);
1003 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001004 return 0.0f;
1005 }
1006
1007 AutoMutex lock(mLock);
1008 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -07001009 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001010 PlaybackThread *thread = checkPlaybackThread_l(output);
1011 if (thread == NULL) {
1012 return 0.0f;
1013 }
1014 volume = thread->streamVolume(stream);
1015 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001016 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001017 }
1018
1019 return volume;
1020}
1021
Glenn Kastenfff6d712012-01-12 16:38:12 -08001022bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001023{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001024 status_t status = checkStreamType(stream);
1025 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001026 return true;
1027 }
1028
Glenn Kasten6637baa2012-01-09 09:40:36 -08001029 AutoMutex lock(mLock);
1030 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001031}
1032
Eric Laurent054d9d32015-04-24 08:48:48 -07001033
1034void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1035{
1036 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1037 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1038 }
1039}
1040
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001041status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001042{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001043 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1044 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001045
Mathias Agopian65ab4712010-07-14 17:59:35 -07001046 // check calling permissions
1047 if (!settingsAllowed()) {
1048 return PERMISSION_DENIED;
1049 }
1050
Glenn Kasten142f5192014-03-25 17:44:59 -07001051 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1052 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001053 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -07001054 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001055 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001056 AutoMutex lock(mHardwareLock);
1057 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1058 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1059 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1060 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1061 final_result = result ?: final_result;
1062 }
1063 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001064 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001065 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1066 AudioParameter param = AudioParameter(keyValuePairs);
1067 String8 value;
1068 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001069 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1070 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001071 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1072 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001073 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001074 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1075 // collect all of the thread's session IDs
Glenn Kastend848eb42016-03-08 13:42:11 -08001076 KeyedVector<audio_session_t, bool> ids = thread->sessionIds();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001077 // suspend effects associated with those session IDs
1078 for (size_t j = 0; j < ids.size(); ++j) {
Glenn Kastend848eb42016-03-08 13:42:11 -08001079 audio_session_t sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001080 thread->setEffectSuspended(FX_IID_AEC,
1081 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001082 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001083 thread->setEffectSuspended(FX_IID_NS,
1084 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001085 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001086 }
1087 }
Eric Laurentbee53372011-08-29 12:42:48 -07001088 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001089 }
1090 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001091 String8 screenState;
1092 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1093 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001094 if (isOff != (AudioFlinger::mScreenState & 1)) {
1095 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001096 }
1097 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001098 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001099 }
1100
1101 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1102 // and the thread is exited once the lock is released
1103 sp<ThreadBase> thread;
1104 {
1105 Mutex::Autolock _l(mLock);
1106 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001107 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001108 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001109 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001110 // indicate output device change to all input threads for pre processing
1111 AudioParameter param = AudioParameter(keyValuePairs);
1112 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001113 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1114 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001115 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001116 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001117 }
1118 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001119 if (thread != 0) {
1120 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001121 }
1122 return BAD_VALUE;
1123}
1124
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001125String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001126{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001127 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1128 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001129
Eric Laurenta4c5a552012-03-29 10:12:40 -07001130 Mutex::Autolock _l(mLock);
1131
Glenn Kasten142f5192014-03-25 17:44:59 -07001132 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001133 String8 out_s8;
1134
Dima Zavin799a70e2011-04-18 16:57:27 -07001135 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001136 char *s;
1137 {
1138 AutoMutex lock(mHardwareLock);
1139 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001140 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001141 s = dev->get_parameters(dev, keys.string());
1142 mHardwareStatus = AUDIO_HW_IDLE;
1143 }
John Grossmanef7740b2012-02-09 11:28:36 -08001144 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001145 free(s);
1146 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001147 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001148 }
1149
Mathias Agopian65ab4712010-07-14 17:59:35 -07001150 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1151 if (playbackThread != NULL) {
1152 return playbackThread->getParameters(keys);
1153 }
1154 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1155 if (recordThread != NULL) {
1156 return recordThread->getParameters(keys);
1157 }
1158 return String8("");
1159}
1160
Glenn Kastendd8104c2012-07-02 12:42:44 -07001161size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1162 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001163{
Eric Laurenta1884f92011-08-23 08:25:03 -07001164 status_t ret = initCheck();
1165 if (ret != NO_ERROR) {
1166 return 0;
1167 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001168 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001169 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001170 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001171 return 0;
1172 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001173
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001174 AutoMutex lock(mHardwareLock);
1175 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001176 audio_config_t config, proposed;
1177 memset(&proposed, 0, sizeof(proposed));
1178 proposed.sample_rate = sampleRate;
1179 proposed.channel_mask = channelMask;
1180 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001181
John Grossmanee578c02012-07-23 17:05:46 -07001182 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001183 size_t frames;
1184 for (;;) {
1185 // Note: config is currently a const parameter for get_input_buffer_size()
1186 // but we use a copy from proposed in case config changes from the call.
1187 config = proposed;
1188 frames = dev->get_input_buffer_size(dev, &config);
1189 if (frames != 0) {
1190 break; // hal success, config is the result
1191 }
1192 // change one parameter of the configuration each iteration to a more "common" value
1193 // to see if the device will support it.
1194 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1195 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1196 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1197 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1198 } else {
1199 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1200 "format %#x, channelMask 0x%X",
1201 sampleRate, format, channelMask);
1202 break; // retries failed, break out of loop with frames == 0.
1203 }
1204 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001205 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001206 if (frames > 0 && config.sample_rate != sampleRate) {
1207 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1208 }
1209 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001210}
1211
Glenn Kasten5f972c02014-01-13 09:59:31 -08001212uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001213{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001214 Mutex::Autolock _l(mLock);
1215
1216 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1217 if (recordThread != NULL) {
1218 return recordThread->getInputFramesLost();
1219 }
1220 return 0;
1221}
1222
1223status_t AudioFlinger::setVoiceVolume(float value)
1224{
Eric Laurenta1884f92011-08-23 08:25:03 -07001225 status_t ret = initCheck();
1226 if (ret != NO_ERROR) {
1227 return ret;
1228 }
1229
Mathias Agopian65ab4712010-07-14 17:59:35 -07001230 // check calling permissions
1231 if (!settingsAllowed()) {
1232 return PERMISSION_DENIED;
1233 }
1234
1235 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001236 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001237 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001238 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001239 mHardwareStatus = AUDIO_HW_IDLE;
1240
1241 return ret;
1242}
1243
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001244status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001245 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001246{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001247 Mutex::Autolock _l(mLock);
1248
1249 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1250 if (playbackThread != NULL) {
1251 return playbackThread->getRenderPosition(halFrames, dspFrames);
1252 }
1253
1254 return BAD_VALUE;
1255}
1256
1257void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1258{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001259 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001260 if (client == 0) {
1261 return;
1262 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001263 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001264 {
1265 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001266 if (mNotificationClients.indexOfKey(pid) < 0) {
1267 sp<NotificationClient> notificationClient = new NotificationClient(this,
1268 client,
1269 pid);
1270 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001271
Eric Laurent021cf962014-05-13 10:18:14 -07001272 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001273
Marco Nelissen06b46062014-11-14 07:58:25 -08001274 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001275 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001276 }
1277 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001278
Eric Laurent021cf962014-05-13 10:18:14 -07001279 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001280 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001281 // the config change is always sent from playback or record threads to avoid deadlock
1282 // with AudioSystem::gLock
1283 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1284 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1285 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001286
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001287 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1288 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001289 }
1290}
1291
1292void AudioFlinger::removeNotificationClient(pid_t pid)
1293{
1294 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001295 {
1296 Mutex::Autolock _cl(mClientLock);
1297 mNotificationClients.removeItem(pid);
1298 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001299
Steve Block3856b092011-10-20 11:56:00 +01001300 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001301 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001302 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001303 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001304 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001305 ALOGV(" pid %d @ %zu", ref->mPid, i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001306 if (ref->mPid == pid) {
1307 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001308 mAudioSessionRefs.removeAt(i);
1309 delete ref;
1310 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001311 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001312 } else {
1313 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001314 }
1315 }
1316 if (removed) {
1317 purgeStaleEffects_l();
1318 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001319}
1320
Eric Laurent73e26b62015-04-27 16:55:58 -07001321void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001322 const sp<AudioIoDescriptor>& ioDesc,
1323 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001324{
Eric Laurent021cf962014-05-13 10:18:14 -07001325 Mutex::Autolock _l(mClientLock);
1326 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001327 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001328 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1329 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1330 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001331 }
1332}
1333
Eric Laurent021cf962014-05-13 10:18:14 -07001334// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001335void AudioFlinger::removeClient_l(pid_t pid)
1336{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001337 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001338 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001339 mClients.removeItem(pid);
1340}
1341
Eric Laurent717e1282012-06-29 16:36:52 -07001342// getEffectThread_l() must be called with AudioFlinger::mLock held
Glenn Kastend848eb42016-03-08 13:42:11 -08001343sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(audio_session_t sessionId,
1344 int EffectId)
Eric Laurent717e1282012-06-29 16:36:52 -07001345{
1346 sp<PlaybackThread> thread;
1347
1348 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1349 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1350 ALOG_ASSERT(thread == 0);
1351 thread = mPlaybackThreads.valueAt(i);
1352 }
1353 }
1354
1355 return thread;
1356}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001357
Mathias Agopian65ab4712010-07-14 17:59:35 -07001358
Mathias Agopian65ab4712010-07-14 17:59:35 -07001359
1360// ----------------------------------------------------------------------------
1361
1362AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1363 : RefBase(),
1364 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001365 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001366{
Sumit Bhattacharyacd64e0e2016-02-11 01:37:20 +05301367 size_t heapSize = property_get_int32("ro.af.client_heap_size_kbyte", 0);
1368 heapSize *= 1024;
1369 if (!heapSize) {
1370 heapSize = kClientSharedHeapSizeBytes;
1371 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1372 // invalidated tracks
1373 if (!audioFlinger->isLowRamDevice()) {
1374 heapSize *= kClientSharedHeapSizeMultiplier;
1375 }
Eric Laurentda73b6c2015-08-20 16:18:53 -07001376 }
1377 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001378}
1379
Eric Laurent021cf962014-05-13 10:18:14 -07001380// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001381AudioFlinger::Client::~Client()
1382{
1383 mAudioFlinger->removeClient_l(mPid);
1384}
1385
Glenn Kasten435dbe62012-01-30 10:15:48 -08001386sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001387{
1388 return mMemoryDealer;
1389}
1390
1391// ----------------------------------------------------------------------------
1392
1393AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1394 const sp<IAudioFlingerClient>& client,
1395 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001396 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001397{
1398}
1399
1400AudioFlinger::NotificationClient::~NotificationClient()
1401{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001402}
1403
Glenn Kasten0f11b512014-01-31 16:18:54 -08001404void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001405{
1406 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001407 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001408}
1409
Mathias Agopian65ab4712010-07-14 17:59:35 -07001410
1411// ----------------------------------------------------------------------------
1412
1413sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001414 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001415 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001416 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001417 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001418 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001419 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001420 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001421 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001422 int clientUid,
Glenn Kastend848eb42016-03-08 13:42:11 -08001423 audio_session_t *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001424 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001425 sp<IMemory>& cblk,
1426 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001427 status_t *status)
1428{
1429 sp<RecordThread::RecordTrack> recordTrack;
1430 sp<RecordHandle> recordHandle;
1431 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001432 status_t lStatus;
Glenn Kastend848eb42016-03-08 13:42:11 -08001433 audio_session_t lSessionId;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001434
Glenn Kastend776ac62014-05-07 09:16:09 -07001435 cblk.clear();
1436 buffers.clear();
1437
Marco Nelissendcb346b2015-09-09 10:47:29 -07001438 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1439 if (!isTrustedCallingUid(callingUid)) {
Andy Hung879db192016-01-05 16:00:34 -08001440 ALOGW_IF((uid_t)clientUid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -07001441 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1442 clientUid = callingUid;
1443 }
1444
Mathias Agopian65ab4712010-07-14 17:59:35 -07001445 // check calling permissions
Marco Nelissendcb346b2015-09-09 10:47:29 -07001446 if (!recordingAllowed(opPackageName, tid, clientUid)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001447 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001448 lStatus = PERMISSION_DENIED;
1449 goto Exit;
1450 }
1451
Glenn Kasten53b5d092014-02-05 10:00:23 -08001452 // further sample rate checks are performed by createRecordTrack_l()
1453 if (sampleRate == 0) {
1454 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1455 lStatus = BAD_VALUE;
1456 goto Exit;
1457 }
1458
Andy Hung6770c6f2015-04-07 13:43:36 -07001459 // we don't yet support anything other than linear PCM
1460 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001461 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001462 lStatus = BAD_VALUE;
1463 goto Exit;
1464 }
1465
Glenn Kasten53b5d092014-02-05 10:00:23 -08001466 // further channel mask checks are performed by createRecordTrack_l()
1467 if (!audio_is_input_channel(channelMask)) {
1468 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1469 lStatus = BAD_VALUE;
1470 goto Exit;
1471 }
1472
Glenn Kasten05997e22014-03-13 15:08:33 -07001473 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001474 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001475 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001476 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001477 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001478 lStatus = BAD_VALUE;
1479 goto Exit;
1480 }
1481
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001482 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001483 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001484
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001485 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001486 if (audio_unique_id_get_use(*sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) {
1487 lStatus = BAD_VALUE;
1488 goto Exit;
1489 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001490 lSessionId = *sessionId;
1491 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001492 // if no audio session id is provided, create one here
Glenn Kastend848eb42016-03-08 13:42:11 -08001493 lSessionId = (audio_session_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_SESSION);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001494 if (sessionId != NULL) {
1495 *sessionId = lSessionId;
1496 }
1497 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001498 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001499
Glenn Kasten1879fff2012-07-11 15:36:59 -07001500 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001501 frameCount, lSessionId, notificationFrames,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001502 clientUid, flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001503 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001504
1505 if (lStatus == NO_ERROR) {
1506 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1507 // session and move it to this thread.
Glenn Kastend848eb42016-03-08 13:42:11 -08001508 sp<EffectChain> chain = getOrphanEffectChain_l(lSessionId);
Eric Laurent1b928682014-10-02 19:41:47 -07001509 if (chain != 0) {
1510 Mutex::Autolock _l(thread->mLock);
1511 thread->addEffectChain_l(chain);
1512 }
1513 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001514 }
Glenn Kastene198c362013-08-13 09:13:36 -07001515
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001516 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001517 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001518 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001519 // Don't hold mClientLock when releasing the reference on the track as the
1520 // destructor will acquire it.
1521 {
1522 Mutex::Autolock _cl(mClientLock);
1523 client.clear();
1524 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001525 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001526 goto Exit;
1527 }
1528
Glenn Kastend776ac62014-05-07 09:16:09 -07001529 cblk = recordTrack->getCblk();
1530 buffers = recordTrack->getBuffers();
1531
Glenn Kasten2fc14732013-08-05 14:58:14 -07001532 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001533 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001534
1535Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001536 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001537 return recordHandle;
1538}
1539
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001540
1541
Mathias Agopian65ab4712010-07-14 17:59:35 -07001542// ----------------------------------------------------------------------------
1543
Eric Laurenta4c5a552012-03-29 10:12:40 -07001544audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1545{
Eric Laurent44622db2014-08-01 19:00:33 -07001546 if (name == NULL) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001547 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurent44622db2014-08-01 19:00:33 -07001548 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001549 if (!settingsAllowed()) {
Glenn Kastena13cde92016-03-28 15:26:02 -07001550 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001551 }
1552 Mutex::Autolock _l(mLock);
1553 return loadHwModule_l(name);
1554}
1555
1556// loadHwModule_l() must be called with AudioFlinger::mLock held
1557audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1558{
1559 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1560 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1561 ALOGW("loadHwModule() module %s already loaded", name);
1562 return mAudioHwDevs.keyAt(i);
1563 }
1564 }
1565
Eric Laurenta4c5a552012-03-29 10:12:40 -07001566 audio_hw_device_t *dev;
1567
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001568 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001569 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001570 ALOGE("loadHwModule() error %d loading module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001571 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001572 }
1573
1574 mHardwareStatus = AUDIO_HW_INIT;
1575 rc = dev->init_check(dev);
1576 mHardwareStatus = AUDIO_HW_IDLE;
1577 if (rc) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001578 ALOGE("loadHwModule() init check error %d for module %s", rc, name);
Glenn Kastena13cde92016-03-28 15:26:02 -07001579 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001580 }
1581
John Grossmanee578c02012-07-23 17:05:46 -07001582 // Check and cache this HAL's level of support for master mute and master
1583 // volume. If this is the first HAL opened, and it supports the get
1584 // methods, use the initial values provided by the HAL as the current
1585 // master mute and volume settings.
1586
1587 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1588 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001589 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001590
1591 if (0 == mAudioHwDevs.size()) {
1592 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1593 if (NULL != dev->get_master_volume) {
1594 float mv;
1595 if (OK == dev->get_master_volume(dev, &mv)) {
1596 mMasterVolume = mv;
1597 }
1598 }
1599
1600 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1601 if (NULL != dev->get_master_mute) {
1602 bool mm;
1603 if (OK == dev->get_master_mute(dev, &mm)) {
1604 mMasterMute = mm;
1605 }
1606 }
1607 }
1608
Eric Laurenta4c5a552012-03-29 10:12:40 -07001609 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001610 if ((NULL != dev->set_master_volume) &&
1611 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1612 flags = static_cast<AudioHwDevice::Flags>(flags |
1613 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1614 }
1615
1616 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1617 if ((NULL != dev->set_master_mute) &&
1618 (OK == dev->set_master_mute(dev, mMasterMute))) {
1619 flags = static_cast<AudioHwDevice::Flags>(flags |
1620 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1621 }
1622
Eric Laurenta4c5a552012-03-29 10:12:40 -07001623 mHardwareStatus = AUDIO_HW_IDLE;
1624 }
1625
Glenn Kastena13cde92016-03-28 15:26:02 -07001626 audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE);
Eric Laurent83b88082014-06-20 18:31:16 -07001627 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001628
1629 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001630 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001631
1632 return handle;
1633
1634}
1635
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001636// ----------------------------------------------------------------------------
1637
Glenn Kasten3b16c762012-11-14 08:44:39 -08001638uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001639{
1640 Mutex::Autolock _l(mLock);
1641 PlaybackThread *thread = primaryPlaybackThread_l();
1642 return thread != NULL ? thread->sampleRate() : 0;
1643}
1644
Glenn Kastene33054e2012-11-14 12:54:39 -08001645size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001646{
1647 Mutex::Autolock _l(mLock);
1648 PlaybackThread *thread = primaryPlaybackThread_l();
1649 return thread != NULL ? thread->frameCountHAL() : 0;
1650}
1651
1652// ----------------------------------------------------------------------------
1653
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001654status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1655{
1656 uid_t uid = IPCThreadState::self()->getCallingUid();
1657 if (uid != AID_SYSTEM) {
1658 return PERMISSION_DENIED;
1659 }
1660 Mutex::Autolock _l(mLock);
1661 if (mIsDeviceTypeKnown) {
1662 return INVALID_OPERATION;
1663 }
1664 mIsLowRamDevice = isLowRamDevice;
1665 mIsDeviceTypeKnown = true;
1666 return NO_ERROR;
1667}
1668
Eric Laurent93c3d412014-08-01 14:48:35 -07001669audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1670{
1671 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001672
1673 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1674 if (index >= 0) {
1675 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1676 mHwAvSyncIds.valueAt(index), sessionId);
1677 return mHwAvSyncIds.valueAt(index);
1678 }
1679
1680 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1681 if (dev == NULL) {
1682 return AUDIO_HW_SYNC_INVALID;
1683 }
1684 char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1685 AudioParameter param = AudioParameter(String8(reply));
1686 free(reply);
1687
1688 int value;
1689 if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1690 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1691 return AUDIO_HW_SYNC_INVALID;
1692 }
1693
1694 // allow only one session for a given HW A/V sync ID.
1695 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1696 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1697 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1698 value, mHwAvSyncIds.keyAt(i));
1699 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001700 break;
1701 }
1702 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001703
1704 mHwAvSyncIds.add(sessionId, value);
1705
1706 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1707 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1708 uint32_t sessions = thread->hasAudioSession(sessionId);
1709 if (sessions & PlaybackThread::TRACK_SESSION) {
1710 AudioParameter param = AudioParameter();
1711 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1712 thread->setParameters(param.toString());
1713 break;
1714 }
1715 }
1716
1717 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1718 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001719}
1720
Eric Laurent72e3f392015-05-20 14:43:50 -07001721status_t AudioFlinger::systemReady()
1722{
1723 Mutex::Autolock _l(mLock);
1724 ALOGI("%s", __FUNCTION__);
1725 if (mSystemReady) {
1726 ALOGW("%s called twice", __FUNCTION__);
1727 return NO_ERROR;
1728 }
1729 mSystemReady = true;
1730 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1731 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1732 thread->systemReady();
1733 }
1734 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1735 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1736 thread->systemReady();
1737 }
1738 return NO_ERROR;
1739}
1740
Eric Laurentfa90e842014-10-17 18:12:31 -07001741// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1742void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1743{
1744 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1745 if (index >= 0) {
1746 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1747 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1748 AudioParameter param = AudioParameter();
1749 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1750 thread->setParameters(param.toString());
1751 }
1752}
1753
1754
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001755// ----------------------------------------------------------------------------
1756
Eric Laurent83b88082014-06-20 18:31:16 -07001757
1758sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001759 audio_io_handle_t *output,
1760 audio_config_t *config,
1761 audio_devices_t devices,
1762 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001763 audio_output_flags_t flags)
1764{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001765 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001766 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001767 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001768 }
1769
Eric Laurentcf2c0212014-07-25 16:20:43 -07001770 if (*output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08001771 *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
1772 } else {
1773 // Audio Policy does not currently request a specific output handle.
1774 // If this is ever needed, see openInput_l() for example code.
1775 ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output);
1776 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001777 }
Eric Laurent83b88082014-06-20 18:31:16 -07001778
1779 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1780
Eric Laurent83b88082014-06-20 18:31:16 -07001781 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001782 // This if statement allows overriding the audio policy settings
1783 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1784 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1785 // Check only for Normal Mixing mode
1786 if (kEnableExtendedPrecision) {
1787 // Specify format (uncomment one below to choose)
1788 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1789 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1790 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1791 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001792 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001793 }
1794 if (kEnableExtendedChannels) {
1795 // Specify channel mask (uncomment one below to choose)
1796 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1797 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1798 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1799 }
Eric Laurent83b88082014-06-20 18:31:16 -07001800 }
1801
Phil Burk062e67a2015-02-11 13:40:50 -08001802 AudioStreamOut *outputStream = NULL;
1803 status_t status = outHwDev->openOutputStream(
1804 &outputStream,
1805 *output,
1806 devices,
1807 flags,
1808 config,
1809 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001810
1811 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001812
Phil Burk062e67a2015-02-11 13:40:50 -08001813 if (status == NO_ERROR) {
Eric Laurent83b88082014-06-20 18:31:16 -07001814
1815 PlaybackThread *thread;
1816 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurent51716182016-02-29 18:00:56 -08001817 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady,
1818 config->offload_info.bit_rate);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001819 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001820 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1821 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001822 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001823 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001824 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001825 } else {
Eric Laurent72e3f392015-05-20 14:43:50 -07001826 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001827 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001828 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001829 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001830 return thread;
1831 }
1832
1833 return 0;
1834}
1835
Eric Laurentcf2c0212014-07-25 16:20:43 -07001836status_t AudioFlinger::openOutput(audio_module_handle_t module,
1837 audio_io_handle_t *output,
1838 audio_config_t *config,
1839 audio_devices_t *devices,
1840 const String8& address,
1841 uint32_t *latencyMs,
1842 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001843{
Phil Burk062e67a2015-02-11 13:40:50 -08001844 ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001845 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001846 (devices != NULL) ? *devices : 0,
1847 config->sample_rate,
1848 config->format,
1849 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001850 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001851
Eric Laurentcf2c0212014-07-25 16:20:43 -07001852 if (*devices == AUDIO_DEVICE_NONE) {
1853 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001854 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001855
Mathias Agopian65ab4712010-07-14 17:59:35 -07001856 Mutex::Autolock _l(mLock);
1857
Eric Laurentcf2c0212014-07-25 16:20:43 -07001858 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001859 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001860 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001861
1862 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001863 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001864
1865 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001866 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001867 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001868 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001869
1870 AutoMutex lock(mHardwareLock);
1871 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001872 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001873 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001874 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001875 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001876 }
1877
Eric Laurentcf2c0212014-07-25 16:20:43 -07001878 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001879}
1880
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001881audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1882 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001883{
1884 Mutex::Autolock _l(mLock);
1885 MixerThread *thread1 = checkMixerThread_l(output1);
1886 MixerThread *thread2 = checkMixerThread_l(output2);
1887
1888 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001889 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1890 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001891 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001892 }
1893
Glenn Kasteneeecb982016-02-26 10:44:04 -08001894 audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT);
Eric Laurent72e3f392015-05-20 14:43:50 -07001895 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001896 thread->addOutputTrack(thread2);
1897 mPlaybackThreads.add(id, thread);
1898 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001899 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001900 return id;
1901}
1902
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001903status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001904{
Glenn Kastend96c5722012-04-25 13:44:49 -07001905 return closeOutput_nonvirtual(output);
1906}
1907
1908status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1909{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001910 // keep strong reference on the playback thread so that
1911 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001912 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001913 {
1914 Mutex::Autolock _l(mLock);
1915 thread = checkPlaybackThread_l(output);
1916 if (thread == NULL) {
1917 return BAD_VALUE;
1918 }
1919
Steve Block3856b092011-10-20 11:56:00 +01001920 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001921
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001922 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001923 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentf6870ae2015-05-08 10:50:03 -07001924 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001925 DuplicatingThread *dupThread =
1926 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001927 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001928 }
1929 }
1930 }
1931
1932
1933 mPlaybackThreads.removeItem(output);
1934 // save all effects to the default thread
1935 if (mPlaybackThreads.size()) {
1936 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1937 if (dstThread != NULL) {
1938 // audioflinger lock is held here so the acquisition order of thread locks does not
1939 // matter
1940 Mutex::Autolock _dl(dstThread->mLock);
1941 Mutex::Autolock _sl(thread->mLock);
1942 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1943 for (size_t i = 0; i < effectChains.size(); i ++) {
1944 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001945 }
1946 }
1947 }
Eric Laurent73e26b62015-04-27 16:55:58 -07001948 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
1949 ioDesc->mIoHandle = output;
1950 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001951 }
1952 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001953 // The thread entity (active unit of execution) is no longer running here,
1954 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001955
Eric Laurentf6870ae2015-05-08 10:50:03 -07001956 if (!thread->isDuplicating()) {
Eric Laurent83b88082014-06-20 18:31:16 -07001957 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001958 }
Eric Laurent83b88082014-06-20 18:31:16 -07001959
Mathias Agopian65ab4712010-07-14 17:59:35 -07001960 return NO_ERROR;
1961}
1962
Eric Laurent83b88082014-06-20 18:31:16 -07001963void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
1964{
1965 AudioStreamOut *out = thread->clearOutput();
1966 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1967 // from now on thread->mOutput is NULL
1968 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
1969 delete out;
1970}
1971
1972void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
1973{
1974 mPlaybackThreads.removeItem(thread->mId);
1975 thread->exit();
1976 closeOutputFinish(thread);
1977}
1978
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001979status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001980{
1981 Mutex::Autolock _l(mLock);
1982 PlaybackThread *thread = checkPlaybackThread_l(output);
1983
1984 if (thread == NULL) {
1985 return BAD_VALUE;
1986 }
1987
Steve Block3856b092011-10-20 11:56:00 +01001988 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001989 thread->suspend();
1990
1991 return NO_ERROR;
1992}
1993
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001994status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001995{
1996 Mutex::Autolock _l(mLock);
1997 PlaybackThread *thread = checkPlaybackThread_l(output);
1998
1999 if (thread == NULL) {
2000 return BAD_VALUE;
2001 }
2002
Steve Block3856b092011-10-20 11:56:00 +01002003 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002004
2005 thread->restore();
2006
2007 return NO_ERROR;
2008}
2009
Eric Laurentcf2c0212014-07-25 16:20:43 -07002010status_t AudioFlinger::openInput(audio_module_handle_t module,
2011 audio_io_handle_t *input,
2012 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002013 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002014 const String8& address,
2015 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002016 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002017{
Eric Laurent83b88082014-06-20 18:31:16 -07002018 Mutex::Autolock _l(mLock);
2019
Glenn Kastene7d66712015-03-05 13:46:52 -08002020 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002021 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002022 }
2023
Glenn Kastene7d66712015-03-05 13:46:52 -08002024 sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002025
2026 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002027 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002028 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002029 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002030 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002031 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002032}
Dima Zavin799a70e2011-04-18 16:57:27 -07002033
Eric Laurent83b88082014-06-20 18:31:16 -07002034sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002035 audio_io_handle_t *input,
2036 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002037 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002038 const String8& address,
2039 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002040 audio_input_flags_t flags)
2041{
Glenn Kastene7d66712015-03-05 13:46:52 -08002042 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002043 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002044 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002045 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002046 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002047
Glenn Kasteneeecb982016-02-26 10:44:04 -08002048 // Audio Policy can request a specific handle for hardware hotword.
2049 // The goal here is not to re-open an already opened input.
2050 // It is to use a pre-assigned I/O handle.
Eric Laurentcf2c0212014-07-25 16:20:43 -07002051 if (*input == AUDIO_IO_HANDLE_NONE) {
Glenn Kasteneeecb982016-02-26 10:44:04 -08002052 *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT);
2053 } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) {
2054 ALOGE("openInput_l() requested input handle %d is invalid", *input);
2055 return 0;
2056 } else if (mRecordThreads.indexOfKey(*input) >= 0) {
2057 // This should not happen in a transient state with current design.
2058 ALOGE("openInput_l() requested input handle %d is already assigned", *input);
2059 return 0;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002060 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002061
Eric Laurentcf2c0212014-07-25 16:20:43 -07002062 audio_config_t halconfig = *config;
2063 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07002064 audio_stream_in_t *inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002065 status_t status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002066 &inStream, flags, address.string(), source);
2067 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002068 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Dima Zavin799a70e2011-04-18 16:57:27 -07002069 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002070 halconfig.sample_rate,
2071 halconfig.format,
2072 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002073 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002074 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002075
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002076 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002077 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002078 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002079 audio_is_linear_pcm(config->format) &&
2080 audio_is_linear_pcm(halconfig.format) &&
2081 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
Eric Laurentcf2c0212014-07-25 16:20:43 -07002082 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
2083 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002084 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002085 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002086 inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002087 status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002088 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07002089 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002090 }
2091
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002092 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002093
Glenn Kasten46909e72013-02-26 09:20:22 -08002094#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002095 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2096 // or (re-)create if current Pipe is idle and does not match the new format
2097 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002098 enum {
2099 TEE_SINK_NO, // don't copy input
2100 TEE_SINK_NEW, // copy input using a new pipe
2101 TEE_SINK_OLD, // copy input using an existing pipe
2102 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002103 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2104 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002105 if (!mTeeSinkInputEnabled) {
2106 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002107 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002108 kind = TEE_SINK_NO;
2109 } else if (mRecordTeeSink == 0) {
2110 kind = TEE_SINK_NEW;
2111 } else if (mRecordTeeSink->getStrongCount() != 1) {
2112 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002113 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002114 kind = TEE_SINK_OLD;
2115 } else {
2116 kind = TEE_SINK_NEW;
2117 }
2118 switch (kind) {
2119 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002120 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002121 size_t numCounterOffers = 0;
2122 const NBAIO_Format offers[1] = {format};
2123 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2124 ALOG_ASSERT(index == 0);
2125 PipeReader *pipeReader = new PipeReader(*pipe);
2126 numCounterOffers = 0;
2127 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2128 ALOG_ASSERT(index == 0);
2129 mRecordTeeSink = pipe;
2130 mRecordTeeSource = pipeReader;
2131 teeSink = pipe;
2132 }
2133 break;
2134 case TEE_SINK_OLD:
2135 teeSink = mRecordTeeSink;
2136 break;
2137 case TEE_SINK_NO:
2138 default:
2139 break;
2140 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002141#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002142
Eric Laurentcf2c0212014-07-25 16:20:43 -07002143 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002144
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002145 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002146 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002147 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002148 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002149 inputStream,
2150 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002151 primaryOutputDevice_l(),
Eric Laurent72e3f392015-05-20 14:43:50 -07002152 devices,
2153 mSystemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08002154#ifdef TEE_SINK
2155 , teeSink
2156#endif
2157 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002158 mRecordThreads.add(*input, thread);
2159 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002160 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002161 }
2162
Eric Laurentcf2c0212014-07-25 16:20:43 -07002163 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002164 return 0;
2165}
2166
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002167status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002168{
Glenn Kastend96c5722012-04-25 13:44:49 -07002169 return closeInput_nonvirtual(input);
2170}
2171
2172status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2173{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002174 // keep strong reference on the record thread so that
2175 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002176 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002177 {
2178 Mutex::Autolock _l(mLock);
2179 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002180 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002181 return BAD_VALUE;
2182 }
2183
Steve Block3856b092011-10-20 11:56:00 +01002184 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002185
2186 // If we still have effect chains, it means that a client still holds a handle
2187 // on at least one effect. We must either move the chain to an existing thread with the
2188 // same session ID or put it aside in case a new record thread is opened for a
2189 // new capture on the same session
2190 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002191 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002192 Mutex::Autolock _sl(thread->mLock);
2193 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002194 // Note: maximum one chain per record thread
2195 if (effectChains.size() != 0) {
2196 chain = effectChains[0];
2197 }
2198 }
2199 if (chain != 0) {
2200 // first check if a record thread is already opened with a client on the same session.
2201 // This should only happen in case of overlap between one thread tear down and the
2202 // creation of its replacement
2203 size_t i;
2204 for (i = 0; i < mRecordThreads.size(); i++) {
2205 sp<RecordThread> t = mRecordThreads.valueAt(i);
2206 if (t == thread) {
2207 continue;
2208 }
2209 if (t->hasAudioSession(chain->sessionId()) != 0) {
2210 Mutex::Autolock _l(t->mLock);
2211 ALOGV("closeInput() found thread %d for effect session %d",
2212 t->id(), chain->sessionId());
2213 t->addEffectChain_l(chain);
2214 break;
2215 }
2216 }
2217 // put the chain aside if we could not find a record thread with the same session id.
2218 if (i == mRecordThreads.size()) {
2219 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002220 }
2221 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002222 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2223 ioDesc->mIoHandle = input;
2224 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002225 mRecordThreads.removeItem(input);
2226 }
Eric Laurent83b88082014-06-20 18:31:16 -07002227 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2228 // we have a different lock for notification client
2229 closeInputFinish(thread);
2230 return NO_ERROR;
2231}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002232
Eric Laurent83b88082014-06-20 18:31:16 -07002233void AudioFlinger::closeInputFinish(sp<RecordThread> thread)
2234{
2235 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002236 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002237 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002238 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002239 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002240 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002241}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002242
Eric Laurent83b88082014-06-20 18:31:16 -07002243void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
2244{
2245 mRecordThreads.removeItem(thread->mId);
2246 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002247}
2248
Glenn Kastend2304db2014-02-03 07:40:31 -08002249status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002250{
2251 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002252 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002253
2254 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2255 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002256 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002257 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002258
2259 return NO_ERROR;
2260}
2261
2262
Glenn Kasteneeecb982016-02-26 10:44:04 -08002263audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002264{
Glenn Kasten9d003132016-04-06 14:38:09 -07002265 // This is a binder API, so a malicious client could pass in a bad parameter.
2266 // Check for that before calling the internal API nextUniqueId().
2267 if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) {
2268 ALOGE("newAudioUniqueId invalid use %d", use);
2269 return AUDIO_UNIQUE_ID_ALLOCATE;
2270 }
Glenn Kasteneeecb982016-02-26 10:44:04 -08002271 return nextUniqueId(use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002272}
2273
Glenn Kastend848eb42016-03-08 13:42:11 -08002274void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002275{
2276 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002277 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002278 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2279 if (pid != -1 && (caller == getpid_cached)) {
2280 caller = pid;
2281 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002282
Eric Laurent021cf962014-05-13 10:18:14 -07002283 {
2284 Mutex::Autolock _cl(mClientLock);
2285 // Ignore requests received from processes not known as notification client. The request
2286 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2287 // called from a different pid leaving a stale session reference. Also we don't know how
2288 // to clear this reference if the client process dies.
2289 if (mNotificationClients.indexOfKey(caller) < 0) {
2290 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2291 return;
2292 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002293 }
2294
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002295 size_t num = mAudioSessionRefs.size();
2296 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002297 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002298 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2299 ref->mCnt++;
2300 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002301 return;
2302 }
2303 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002304 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2305 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002306}
2307
Glenn Kastend848eb42016-03-08 13:42:11 -08002308void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002309{
2310 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002311 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002312 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2313 if (pid != -1 && (caller == getpid_cached)) {
2314 caller = pid;
2315 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002316 size_t num = mAudioSessionRefs.size();
2317 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002318 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002319 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2320 ref->mCnt--;
2321 ALOGV(" decremented refcount to %d", ref->mCnt);
2322 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002323 mAudioSessionRefs.removeAt(i);
2324 delete ref;
2325 purgeStaleEffects_l();
2326 }
2327 return;
2328 }
2329 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002330 // If the caller is mediaserver it is likely that the session being released was acquired
2331 // on behalf of a process not in notification clients and we ignore the warning.
2332 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002333}
2334
2335void AudioFlinger::purgeStaleEffects_l() {
2336
Steve Block3856b092011-10-20 11:56:00 +01002337 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002338
2339 Vector< sp<EffectChain> > chains;
2340
2341 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2342 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2343 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2344 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002345 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2346 chains.push(ec);
2347 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002348 }
2349 }
2350 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2351 sp<RecordThread> t = mRecordThreads.valueAt(i);
2352 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2353 sp<EffectChain> ec = t->mEffectChains[j];
2354 chains.push(ec);
2355 }
2356 }
2357
2358 for (size_t i = 0; i < chains.size(); i++) {
2359 sp<EffectChain> ec = chains[i];
2360 int sessionid = ec->sessionId();
2361 sp<ThreadBase> t = ec->mThread.promote();
2362 if (t == 0) {
2363 continue;
2364 }
2365 size_t numsessionrefs = mAudioSessionRefs.size();
2366 bool found = false;
2367 for (size_t k = 0; k < numsessionrefs; k++) {
2368 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002369 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002370 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002371 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002372 found = true;
2373 break;
2374 }
2375 }
2376 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002377 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002378 // remove all effects from the chain
2379 while (ec->mEffects.size()) {
2380 sp<EffectModule> effect = ec->mEffects[0];
2381 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002382 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002383 if (effect->purgeHandles()) {
2384 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002385 }
2386 AudioSystem::unregisterEffect(effect->id());
2387 }
2388 }
2389 }
2390 return;
2391}
2392
Glenn Kasteneeecb982016-02-26 10:44:04 -08002393// checkThread_l() must be called with AudioFlinger::mLock held
2394AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
2395{
2396 ThreadBase *thread = NULL;
2397 switch (audio_unique_id_get_use(ioHandle)) {
2398 case AUDIO_UNIQUE_ID_USE_OUTPUT:
2399 thread = checkPlaybackThread_l(ioHandle);
2400 break;
2401 case AUDIO_UNIQUE_ID_USE_INPUT:
2402 thread = checkRecordThread_l(ioHandle);
2403 break;
2404 default:
2405 break;
2406 }
2407 return thread;
2408}
2409
Mathias Agopian65ab4712010-07-14 17:59:35 -07002410// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002411AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002412{
Glenn Kastena1117922012-01-26 10:53:32 -08002413 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002414}
2415
2416// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002417AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002418{
2419 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002420 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002421}
2422
2423// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002424AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002425{
Glenn Kastena1117922012-01-26 10:53:32 -08002426 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002427}
2428
Glenn Kasteneeecb982016-02-26 10:44:04 -08002429audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002430{
Glenn Kasteneeecb982016-02-26 10:44:04 -08002431 int32_t base = android_atomic_add(AUDIO_UNIQUE_ID_USE_MAX, &mNextUniqueId);
2432 // We have no way of recovering from wraparound
2433 LOG_ALWAYS_FATAL_IF(base == 0, "unique ID overflow");
Glenn Kasten9d003132016-04-06 14:38:09 -07002434 // This is the internal API, so it is OK to assert on bad parameter.
Glenn Kasteneeecb982016-02-26 10:44:04 -08002435 LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX);
2436 ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED);
2437 return (audio_unique_id_t) (base | use);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002438}
2439
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002440AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002441{
2442 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2443 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002444 if(thread->isDuplicating()) {
2445 continue;
2446 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002447 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002448 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002449 return thread;
2450 }
2451 }
2452 return NULL;
2453}
2454
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002455audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002456{
2457 PlaybackThread *thread = primaryPlaybackThread_l();
2458
2459 if (thread == NULL) {
2460 return 0;
2461 }
2462
Eric Laurentf1c04f92012-08-28 14:26:53 -07002463 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002464}
2465
Eric Laurenta011e352012-03-29 15:51:43 -07002466sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
Glenn Kastend848eb42016-03-08 13:42:11 -08002467 audio_session_t triggerSession,
2468 audio_session_t listenerSession,
Eric Laurenta011e352012-03-29 15:51:43 -07002469 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002470 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002471{
2472 Mutex::Autolock _l(mLock);
2473
2474 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2475 status_t playStatus = NAME_NOT_FOUND;
2476 status_t recStatus = NAME_NOT_FOUND;
2477 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2478 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2479 if (playStatus == NO_ERROR) {
2480 return event;
2481 }
2482 }
2483 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2484 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2485 if (recStatus == NO_ERROR) {
2486 return event;
2487 }
2488 }
2489 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2490 mPendingSyncEvents.add(event);
2491 } else {
2492 ALOGV("createSyncEvent() invalid event %d", event->type());
2493 event.clear();
2494 }
2495 return event;
2496}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002497
Mathias Agopian65ab4712010-07-14 17:59:35 -07002498// ----------------------------------------------------------------------------
2499// Effect management
2500// ----------------------------------------------------------------------------
2501
2502
Glenn Kastenf587ba52012-01-26 16:25:10 -08002503status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002504{
2505 Mutex::Autolock _l(mLock);
2506 return EffectQueryNumberEffects(numEffects);
2507}
2508
Glenn Kastenf587ba52012-01-26 16:25:10 -08002509status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002510{
2511 Mutex::Autolock _l(mLock);
2512 return EffectQueryEffect(index, descriptor);
2513}
2514
Glenn Kasten5e92a782012-01-30 07:40:52 -08002515status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002516 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002517{
2518 Mutex::Autolock _l(mLock);
2519 return EffectGetDescriptor(pUuid, descriptor);
2520}
2521
2522
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002523sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002524 effect_descriptor_t *pDesc,
2525 const sp<IEffectClient>& effectClient,
2526 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002527 audio_io_handle_t io,
Glenn Kastend848eb42016-03-08 13:42:11 -08002528 audio_session_t sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002529 const String16& opPackageName,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002530 status_t *status,
2531 int *id,
2532 int *enabled)
2533{
2534 status_t lStatus = NO_ERROR;
2535 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002536 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002537
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002538 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002539 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002540 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002541
2542 if (pDesc == NULL) {
2543 lStatus = BAD_VALUE;
2544 goto Exit;
2545 }
2546
Eric Laurent84e9a102010-09-23 16:10:16 -07002547 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002548 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002549 lStatus = PERMISSION_DENIED;
2550 goto Exit;
2551 }
2552
Dima Zavinfce7a472011-04-19 22:30:36 -07002553 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002554 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002555 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002556 lStatus = PERMISSION_DENIED;
2557 goto Exit;
2558 }
2559
Mathias Agopian65ab4712010-07-14 17:59:35 -07002560 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002561 if (!EffectIsNullUuid(&pDesc->uuid)) {
2562 // if uuid is specified, request effect descriptor
2563 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2564 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002565 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002566 goto Exit;
2567 }
2568 } else {
2569 // if uuid is not specified, look for an available implementation
2570 // of the required type in effect factory
2571 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002572 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002573 lStatus = BAD_VALUE;
2574 goto Exit;
2575 }
2576 uint32_t numEffects = 0;
2577 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002578 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002579 bool found = false;
2580
2581 lStatus = EffectQueryNumberEffects(&numEffects);
2582 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002583 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002584 goto Exit;
2585 }
2586 for (uint32_t i = 0; i < numEffects; i++) {
2587 lStatus = EffectQueryEffect(i, &desc);
2588 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002589 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002590 continue;
2591 }
2592 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2593 // If matching type found save effect descriptor. If the session is
2594 // 0 and the effect is not auxiliary, continue enumeration in case
2595 // an auxiliary version of this effect type is available
2596 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002597 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002598 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002599 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2600 break;
2601 }
2602 }
2603 }
2604 if (!found) {
2605 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002606 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002607 goto Exit;
2608 }
2609 // For same effect type, chose auxiliary version over insert version if
2610 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002611 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002612 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002613 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002614 }
2615 }
2616
2617 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002618 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002619 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2620 lStatus = INVALID_OPERATION;
2621 goto Exit;
2622 }
2623
Eric Laurent59255e42011-07-27 19:49:51 -07002624 // check recording permission for visualizer
2625 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Marco Nelissendcb346b2015-09-09 10:47:29 -07002626 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07002627 lStatus = PERMISSION_DENIED;
2628 goto Exit;
2629 }
2630
Mathias Agopian65ab4712010-07-14 17:59:35 -07002631 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002632 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002633 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002634 // if the output returned by getOutputForEffect() is removed before we lock the
2635 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2636 // and we will exit safely
2637 io = AudioSystem::getOutputForEffect(&desc);
2638 ALOGV("createEffect got output %d", io);
2639 }
2640
2641 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002642
2643 // If output is not specified try to find a matching audio session ID in one of the
2644 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002645 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2646 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002647 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002648 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002649 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2650 // output must be specified by AudioPolicyManager when using session
2651 // AUDIO_SESSION_OUTPUT_STAGE
2652 lStatus = BAD_VALUE;
2653 goto Exit;
2654 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002655 // look for the thread where the specified audio session is present
2656 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2657 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2658 io = mPlaybackThreads.keyAt(i);
2659 break;
2660 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002661 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002662 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002663 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2664 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2665 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002666 break;
2667 }
2668 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002669 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002670 // If no output thread contains the requested session ID, default to
2671 // first output. The effect chain will be moved to the correct output
2672 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002673 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002674 io = mPlaybackThreads.keyAt(0);
2675 }
Steve Block3856b092011-10-20 11:56:00 +01002676 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002677 }
2678 ThreadBase *thread = checkRecordThread_l(io);
2679 if (thread == NULL) {
2680 thread = checkPlaybackThread_l(io);
2681 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002682 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002683 lStatus = BAD_VALUE;
2684 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002685 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002686 } else {
2687 // Check if one effect chain was awaiting for an effect to be created on this
2688 // session and used it instead of creating a new one.
Glenn Kastend848eb42016-03-08 13:42:11 -08002689 sp<EffectChain> chain = getOrphanEffectChain_l(sessionId);
Eric Laurentaaa44472014-09-12 17:41:50 -07002690 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002691 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002692 thread->addEffectChain_l(chain);
2693 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002694 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002695
Eric Laurent021cf962014-05-13 10:18:14 -07002696 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002697
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002698 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002699 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2700 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002701 if (handle != 0 && id != NULL) {
2702 *id = handle->id();
2703 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002704 if (handle == 0) {
2705 // remove local strong reference to Client with mClientLock held
2706 Mutex::Autolock _cl(mClientLock);
2707 client.clear();
2708 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002709 }
2710
2711Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002712 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002713 return handle;
2714}
2715
Glenn Kastend848eb42016-03-08 13:42:11 -08002716status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002717 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002718{
Steve Block3856b092011-10-20 11:56:00 +01002719 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002720 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002721 Mutex::Autolock _l(mLock);
2722 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002723 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002724 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002725 }
Eric Laurentde070132010-07-13 04:45:46 -07002726 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2727 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002728 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002729 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002730 }
Eric Laurentde070132010-07-13 04:45:46 -07002731 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2732 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002733 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002734 return BAD_VALUE;
2735 }
2736
2737 Mutex::Autolock _dl(dstThread->mLock);
2738 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002739 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002740}
2741
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002742// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Glenn Kastend848eb42016-03-08 13:42:11 -08002743status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002744 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002745 AudioFlinger::PlaybackThread *dstThread,
2746 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002747{
Steve Block3856b092011-10-20 11:56:00 +01002748 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002749 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002750
Eric Laurent59255e42011-07-27 19:49:51 -07002751 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002752 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002753 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002754 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002755 return INVALID_OPERATION;
2756 }
2757
Andy Hung9a592762014-07-21 21:56:01 -07002758 // Check whether the destination thread has a channel count of FCC_2, which is
2759 // currently required for (most) effects. Prevent moving the effect chain here rather
2760 // than disabling the addEffect_l() call in dstThread below.
Eric Laurentf6870ae2015-05-08 10:50:03 -07002761 if ((dstThread->type() == ThreadBase::MIXER || dstThread->isDuplicating()) &&
Eric Laurentb10352f2014-11-03 16:25:39 -08002762 dstThread->mChannelCount != FCC_2) {
Andy Hung9a592762014-07-21 21:56:01 -07002763 ALOGW("moveEffectChain_l() effect chain failed because"
2764 " destination thread %p channel count(%u) != %u",
2765 dstThread, dstThread->mChannelCount, FCC_2);
2766 return INVALID_OPERATION;
2767 }
2768
Eric Laurent39e94f82010-07-28 01:32:47 -07002769 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002770 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002771 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002772 // removed.
2773 srcThread->removeEffectChain_l(chain);
2774
2775 // transfer all effects one by one so that new effect chain is created on new thread with
2776 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002777 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002778 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002779 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002780 Vector< sp<EffectModule> > removed;
2781 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002782 while (effect != 0) {
2783 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002784 removed.add(effect);
2785 status = dstThread->addEffect_l(effect);
2786 if (status != NO_ERROR) {
2787 break;
2788 }
Eric Laurentec35a142011-10-05 17:42:25 -07002789 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2790 if (effect->state() == EffectModule::ACTIVE ||
2791 effect->state() == EffectModule::STOPPING) {
2792 effect->start();
2793 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002794 // if the move request is not received from audio policy manager, the effect must be
2795 // re-registered with the new strategy and output
2796 if (dstChain == 0) {
2797 dstChain = effect->chain().promote();
2798 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002799 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002800 status = NO_INIT;
2801 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002802 }
2803 strategy = dstChain->strategy();
2804 }
2805 if (reRegister) {
2806 AudioSystem::unregisterEffect(effect->id());
2807 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002808 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002809 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002810 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002811 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002812 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002813 }
Eric Laurentde070132010-07-13 04:45:46 -07002814 effect = chain->getEffectFromId_l(0);
2815 }
2816
Eric Laurent5baf2af2013-09-12 17:37:00 -07002817 if (status != NO_ERROR) {
2818 for (size_t i = 0; i < removed.size(); i++) {
2819 srcThread->addEffect_l(removed[i]);
2820 if (dstChain != 0 && reRegister) {
2821 AudioSystem::unregisterEffect(removed[i]->id());
2822 AudioSystem::registerEffect(&removed[i]->desc(),
2823 srcThread->id(),
2824 strategy,
2825 sessionId,
2826 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002827 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002828 }
2829 }
2830 }
2831
2832 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002833}
2834
Eric Laurent5baf2af2013-09-12 17:37:00 -07002835bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002836{
2837 if (mGlobalEffectEnableTime != 0 &&
2838 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2839 return true;
2840 }
2841
2842 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2843 sp<EffectChain> ec =
2844 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002845 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002846 return true;
2847 }
2848 }
2849 return false;
2850}
2851
Eric Laurent5baf2af2013-09-12 17:37:00 -07002852void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002853{
2854 Mutex::Autolock _l(mLock);
2855
2856 mGlobalEffectEnableTime = systemTime();
2857
2858 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2859 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2860 if (t->mType == ThreadBase::OFFLOAD) {
2861 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2862 }
2863 }
2864
2865}
2866
Eric Laurentaaa44472014-09-12 17:41:50 -07002867status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2868{
Glenn Kastend848eb42016-03-08 13:42:11 -08002869 audio_session_t session = chain->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002870 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002871 ALOGV("putOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002872 if (index >= 0) {
2873 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2874 return ALREADY_EXISTS;
2875 }
2876 mOrphanEffectChains.add(session, chain);
2877 return NO_ERROR;
2878}
2879
2880sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2881{
2882 sp<EffectChain> chain;
2883 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002884 ALOGV("getOrphanEffectChain_l session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002885 if (index >= 0) {
2886 chain = mOrphanEffectChains.valueAt(index);
2887 mOrphanEffectChains.removeItemsAt(index);
2888 }
2889 return chain;
2890}
2891
2892bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2893{
2894 Mutex::Autolock _l(mLock);
Glenn Kastend848eb42016-03-08 13:42:11 -08002895 audio_session_t session = effect->sessionId();
Eric Laurentaaa44472014-09-12 17:41:50 -07002896 ssize_t index = mOrphanEffectChains.indexOfKey(session);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002897 ALOGV("updateOrphanEffectChains session %d index %zd", session, index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002898 if (index >= 0) {
2899 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2900 if (chain->removeEffect_l(effect) == 0) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002901 ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index);
Eric Laurentaaa44472014-09-12 17:41:50 -07002902 mOrphanEffectChains.removeItemsAt(index);
2903 }
2904 return true;
2905 }
2906 return false;
2907}
2908
2909
Glenn Kastenda6ef132013-01-10 12:31:01 -08002910struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002911#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
2912 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002913};
2914
2915int comparEntry(const void *p1, const void *p2)
2916{
Glenn Kasten2f55e762015-03-05 16:05:08 -08002917 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002918}
2919
Glenn Kasten46909e72013-02-26 09:20:22 -08002920#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002921void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002922{
Eric Laurent81784c32012-11-19 14:55:58 -08002923 NBAIO_Source *teeSource = source.get();
2924 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002925 // .wav rotation
2926 // There is a benign race condition if 2 threads call this simultaneously.
2927 // They would both traverse the directory, but the result would simply be
2928 // failures at unlink() which are ignored. It's also unlikely since
2929 // normally dumpsys is only done by bugreport or from the command line.
2930 char teePath[32+256];
Glenn Kasten9a003992016-02-23 15:24:34 -08002931 strcpy(teePath, "/data/misc/audioserver");
Glenn Kastenda6ef132013-01-10 12:31:01 -08002932 size_t teePathLen = strlen(teePath);
2933 DIR *dir = opendir(teePath);
2934 teePath[teePathLen++] = '/';
2935 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002936#define TEE_MAX_SORT 20 // number of entries to sort
2937#define TEE_MAX_KEEP 10 // number of entries to keep
2938 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002939 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08002940 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002941 struct dirent de;
2942 struct dirent *result = NULL;
2943 int rc = readdir_r(dir, &de, &result);
2944 if (rc != 0) {
2945 ALOGW("readdir_r failed %d", rc);
2946 break;
2947 }
2948 if (result == NULL) {
2949 break;
2950 }
2951 if (result != &de) {
2952 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2953 break;
2954 }
2955 // ignore non .wav file entries
2956 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002957 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08002958 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2959 continue;
2960 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08002961 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002962 }
2963 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002964 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002965 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002966 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
2967 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002968 (void) unlink(teePath);
2969 }
2970 }
2971 } else {
2972 if (fd >= 0) {
Glenn Kasten9a003992016-02-23 15:24:34 -08002973 dprintf(fd, "unable to rotate tees in %.*s: %s\n", teePathLen, teePath,
2974 strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002975 }
2976 }
Eric Laurent81784c32012-11-19 14:55:58 -08002977 char teeTime[16];
2978 struct timeval tv;
2979 gettimeofday(&tv, NULL);
2980 struct tm tm;
2981 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002982 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2983 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2984 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2985 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002986 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07002987 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08002988 char wavHeader[44];
2989 memcpy(wavHeader,
2990 "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",
2991 sizeof(wavHeader));
2992 NBAIO_Format format = teeSource->format();
2993 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002994 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07002995 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002996 wavHeader[22] = channelCount; // number of channels
2997 wavHeader[24] = sampleRate; // sample rate
2998 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07002999 wavHeader[32] = frameSize; // block alignment
3000 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08003001 write(teeFd, wavHeader, sizeof(wavHeader));
3002 size_t total = 0;
3003 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07003004#define TEE_SINK_READ 1024 // frames per I/O operation
3005 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003006 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08003007 size_t count = TEE_SINK_READ;
Glenn Kastend79072e2016-01-06 08:41:20 -08003008 ssize_t actual = teeSource->read(buffer, count);
Eric Laurent81784c32012-11-19 14:55:58 -08003009 bool wasFirstRead = firstRead;
3010 firstRead = false;
3011 if (actual <= 0) {
3012 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
3013 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07003014 }
Eric Laurent81784c32012-11-19 14:55:58 -08003015 break;
Eric Laurent59255e42011-07-27 19:49:51 -07003016 }
Eric Laurent81784c32012-11-19 14:55:58 -08003017 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07003018 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08003019 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07003020 }
Glenn Kasten329f6512014-08-28 16:23:16 -07003021 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08003022 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003023 uint32_t temp = 44 + total * frameSize - 8;
3024 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003025 write(teeFd, &temp, sizeof(temp));
3026 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07003027 temp = total * frameSize;
3028 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08003029 write(teeFd, &temp, sizeof(temp));
3030 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003031 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003032 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08003033 }
Eric Laurent59255e42011-07-27 19:49:51 -07003034 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08003035 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07003036 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003037 }
Eric Laurent59255e42011-07-27 19:49:51 -07003038 }
3039 }
3040}
Glenn Kasten46909e72013-02-26 09:20:22 -08003041#endif
Eric Laurent59255e42011-07-27 19:49:51 -07003042
Mathias Agopian65ab4712010-07-14 17:59:35 -07003043// ----------------------------------------------------------------------------
3044
3045status_t AudioFlinger::onTransact(
3046 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3047{
3048 return BnAudioFlinger::onTransact(code, data, reply, flags);
3049}
3050
Glenn Kasten63238ef2015-03-02 15:50:29 -08003051} // namespace android