blob: 6f342716ea2cb94a57e7b5a34477dafb226fc376 [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) {
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530110 switch (format & AUDIO_FORMAT_MAIN_MASK) {
111 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";
Marco Nelissenb2208842014-02-07 14:00:50 -0800133 default:
134 break;
135 }
136 return "unknown";
137}
138
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700139static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700140{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700141 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700142 int rc;
143
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700144 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
145 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
146 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
147 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700148 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700149 }
150 rc = audio_hw_device_open(mod, dev);
151 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
152 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
153 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700154 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700155 }
Eric Laurent5806b352014-05-22 12:23:26 -0700156 if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700157 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
158 rc = BAD_VALUE;
159 goto out;
160 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700161 return 0;
162
163out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700164 *dev = NULL;
165 return rc;
166}
167
Mathias Agopian65ab4712010-07-14 17:59:35 -0700168// ----------------------------------------------------------------------------
169
170AudioFlinger::AudioFlinger()
171 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800172 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800173 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700174 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800175 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800176 mMasterMute(false),
177 mNextUniqueId(1),
178 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700179 mBtNrecIsOff(false),
180 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700181 mIsDeviceTypeKnown(false),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700182 mGlobalEffectEnableTime(0),
Eric Laurent72e3f392015-05-20 14:43:50 -0700183 mSystemReady(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700184{
Glenn Kasten949a9262013-04-16 12:35:20 -0700185 getpid_cached = getpid();
Andy Hungce717682015-12-22 13:40:32 -0800186 // disable media.log until the service is reenabled, see b/26306954
187 const bool doLog = false; // 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 Kastenda6ef132013-01-10 12:31:01 -0800199 (void) property_get("ro.debuggable", value, "0");
200 int debuggable = atoi(value);
201 int teeEnabled = 0;
202 if (debuggable) {
203 (void) property_get("af.tee", value, "0");
204 teeEnabled = atoi(value);
205 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800206 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700207 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800208 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700209 }
210 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800211 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700212 }
213 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800214 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700215 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800216#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700217}
218
219void AudioFlinger::onFirstRef()
220{
Dima Zavin799a70e2011-04-18 16:57:27 -0700221 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700222
Eric Laurent93575202011-01-18 18:39:02 -0800223 Mutex::Autolock _l(mLock);
224
Dima Zavin799a70e2011-04-18 16:57:27 -0700225 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800226 char val_str[PROPERTY_VALUE_MAX] = { 0 };
227 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
228 uint32_t int_val;
229 if (1 == sscanf(val_str, "%u", &int_val)) {
230 mStandbyTimeInNsecs = milliseconds(int_val);
231 ALOGI("Using %u mSec as standby time.", int_val);
232 } else {
233 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
234 ALOGI("Using default %u mSec as standby time.",
235 (uint32_t)(mStandbyTimeInNsecs / 1000000));
236 }
237 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700238
Eric Laurent1c333e22014-05-20 10:48:17 -0700239 mPatchPanel = new PatchPanel(this);
240
Eric Laurenta4c5a552012-03-29 10:12:40 -0700241 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242}
243
244AudioFlinger::~AudioFlinger()
245{
246 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700247 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700248 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700249 }
250 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700251 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700252 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700253 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700254
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800255 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
256 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700257 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
258 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700259 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700260
261 // Tell media.log service about any old writers that still need to be unregistered
Andy Hungce717682015-12-22 13:40:32 -0800262 if (mLogMemoryDealer != 0) {
263 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
264 if (binder != 0) {
265 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
266 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
267 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
268 mUnregisteredWriters.pop();
269 mediaLogService->unregisterWriter(iMemory);
270 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700271 }
272 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700273}
274
Eric Laurenta4c5a552012-03-29 10:12:40 -0700275static const char * const audio_interfaces[] = {
276 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
277 AUDIO_HARDWARE_MODULE_ID_A2DP,
278 AUDIO_HARDWARE_MODULE_ID_USB,
279};
280#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
281
Phil Burk062e67a2015-02-11 13:40:50 -0800282AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700283 audio_module_handle_t module,
284 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700285{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700286 // if module is 0, the request comes from an old policy manager and we should load
287 // well known modules
288 if (module == 0) {
289 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
290 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
291 loadHwModule_l(audio_interfaces[i]);
292 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700293 // then try to find a module supporting the requested device.
294 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
295 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
296 audio_hw_device_t *dev = audioHwDevice->hwDevice();
297 if ((dev->get_supported_devices != NULL) &&
298 (dev->get_supported_devices(dev) & devices) == devices)
299 return audioHwDevice;
300 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700301 } else {
302 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700303 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
304 if (audioHwDevice != NULL) {
305 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700306 }
307 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700308
Dima Zavin799a70e2011-04-18 16:57:27 -0700309 return NULL;
310}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700311
Glenn Kasten0f11b512014-01-31 16:18:54 -0800312void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313{
314 const size_t SIZE = 256;
315 char buffer[SIZE];
316 String8 result;
317
318 result.append("Clients:\n");
319 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800320 sp<Client> client = mClients.valueAt(i).promote();
321 if (client != 0) {
322 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
323 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324 }
325 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700326
Eric Laurentd1b28d42013-09-18 18:47:13 -0700327 result.append("Notification Clients:\n");
328 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
329 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
330 result.append(buffer);
331 }
332
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700333 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800334 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700335 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
336 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800337 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700338 result.append(buffer);
339 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700340 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700341}
342
343
Glenn Kasten0f11b512014-01-31 16:18:54 -0800344void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345{
346 const size_t SIZE = 256;
347 char buffer[SIZE];
348 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800349 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700350
John Grossman4ff14ba2012-02-08 16:37:41 -0800351 snprintf(buffer, SIZE, "Hardware status: %d\n"
352 "Standby Time mSec: %u\n",
353 hardwareStatus,
354 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700355 result.append(buffer);
356 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700357}
358
Glenn Kasten0f11b512014-01-31 16:18:54 -0800359void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360{
361 const size_t SIZE = 256;
362 char buffer[SIZE];
363 String8 result;
364 snprintf(buffer, SIZE, "Permission Denial: "
365 "can't dump AudioFlinger from pid=%d, uid=%d\n",
366 IPCThreadState::self()->getCallingPid(),
367 IPCThreadState::self()->getCallingUid());
368 result.append(buffer);
369 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370}
371
Eric Laurent81784c32012-11-19 14:55:58 -0800372bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373{
374 bool locked = false;
375 for (int i = 0; i < kDumpLockRetries; ++i) {
376 if (mutex.tryLock() == NO_ERROR) {
377 locked = true;
378 break;
379 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800380 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700381 }
382 return locked;
383}
384
385status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
386{
Glenn Kasten44deb052012-02-05 18:09:08 -0800387 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388 dumpPermissionDenial(fd, args);
389 } else {
390 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800391 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392 if (!hardwareLocked) {
393 String8 result(kHardwareLockedString);
394 write(fd, result.string(), result.size());
395 } else {
396 mHardwareLock.unlock();
397 }
398
Eric Laurent81784c32012-11-19 14:55:58 -0800399 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400
401 // failed to lock - AudioFlinger is probably deadlocked
402 if (!locked) {
403 String8 result(kDeadlockedString);
404 write(fd, result.string(), result.size());
405 }
406
Eric Laurent021cf962014-05-13 10:18:14 -0700407 bool clientLocked = dumpTryLock(mClientLock);
408 if (!clientLocked) {
409 String8 result(kClientLockedString);
410 write(fd, result.string(), result.size());
411 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700412
413 EffectDumpEffects(fd);
414
Mathias Agopian65ab4712010-07-14 17:59:35 -0700415 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700416 if (clientLocked) {
417 mClientLock.unlock();
418 }
419
Mathias Agopian65ab4712010-07-14 17:59:35 -0700420 dumpInternals(fd, args);
421
422 // dump playback threads
423 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
424 mPlaybackThreads.valueAt(i)->dump(fd, args);
425 }
426
427 // dump record threads
428 for (size_t i = 0; i < mRecordThreads.size(); i++) {
429 mRecordThreads.valueAt(i)->dump(fd, args);
430 }
431
Eric Laurentaaa44472014-09-12 17:41:50 -0700432 // dump orphan effect chains
433 if (mOrphanEffectChains.size() != 0) {
434 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
435 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
436 mOrphanEffectChains.valueAt(i)->dump(fd, args);
437 }
438 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700439 // dump all hardware devs
440 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700441 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700442 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700443 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700444
Glenn Kasten46909e72013-02-26 09:20:22 -0800445#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700446 // dump the serially shared record tee sink
447 if (mRecordTeeSource != 0) {
448 dumpTee(fd, mRecordTeeSource);
449 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800450#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700451
Glenn Kastend65d73c2012-06-22 17:21:07 -0700452 if (locked) {
453 mLock.unlock();
454 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800455
456 // append a copy of media.log here by forwarding fd to it, but don't attempt
457 // to lookup the service if it's not running, as it will block for a second
458 if (mLogMemoryDealer != 0) {
459 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
460 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700461 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800462 Vector<String16> args;
463 binder->dump(fd, args);
464 }
465 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700466 }
467 return NO_ERROR;
468}
469
Eric Laurent021cf962014-05-13 10:18:14 -0700470sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800471{
Eric Laurent021cf962014-05-13 10:18:14 -0700472 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800473 // If pid is already in the mClients wp<> map, then use that entry
474 // (for which promote() is always != 0), otherwise create a new entry and Client.
475 sp<Client> client = mClients.valueFor(pid).promote();
476 if (client == 0) {
477 client = new Client(this, pid);
478 mClients.add(pid, client);
479 }
480
481 return client;
482}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700483
Glenn Kasten9e58b552013-01-18 15:09:48 -0800484sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
485{
Glenn Kasten481fb672013-09-30 14:39:28 -0700486 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800487 if (mLogMemoryDealer == 0) {
488 return new NBLog::Writer();
489 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800490 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700491 // Similarly if we can't contact the media.log service, also return a dummy writer
492 if (binder == 0) {
493 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800494 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700495 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
496 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
497 // If allocation fails, consult the vector of previously unregistered writers
498 // and garbage-collect one or more them until an allocation succeeds
499 if (shared == 0) {
500 Mutex::Autolock _l(mUnregisteredWritersLock);
501 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
502 {
503 // Pick the oldest stale writer to garbage-collect
504 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
505 mUnregisteredWriters.removeAt(0);
506 mediaLogService->unregisterWriter(iMemory);
507 // Now the media.log remote reference to IMemory is gone. When our last local
508 // reference to IMemory also drops to zero at end of this block,
509 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
510 }
511 // Re-attempt the allocation
512 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
513 if (shared != 0) {
514 goto success;
515 }
516 }
517 // Even after garbage-collecting all old writers, there is still not enough memory,
518 // so return a dummy writer
519 return new NBLog::Writer();
520 }
521success:
522 mediaLogService->registerWriter(shared, size, name);
523 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800524}
525
526void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
527{
Glenn Kasten685ef092013-02-04 08:15:34 -0800528 if (writer == 0) {
529 return;
530 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800531 sp<IMemory> iMemory(writer->getIMemory());
532 if (iMemory == 0) {
533 return;
534 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700535 // Rather than removing the writer immediately, append it to a queue of old writers to
536 // be garbage-collected later. This allows us to continue to view old logs for a while.
537 Mutex::Autolock _l(mUnregisteredWritersLock);
538 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800539}
540
Mathias Agopian65ab4712010-07-14 17:59:35 -0700541// IAudioFlinger interface
542
543
544sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800545 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700546 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800547 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700548 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800549 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800550 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700551 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800552 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800553 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700554 int *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800555 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700556 status_t *status)
557{
558 sp<PlaybackThread::Track> track;
559 sp<TrackHandle> trackHandle;
560 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700561 status_t lStatus;
562 int lSessionId;
563
Glenn Kasten263709e2012-01-06 08:40:01 -0800564 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
565 // but if someone uses binder directly they could bypass that and cause us to crash
566 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000567 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700568 lStatus = BAD_VALUE;
569 goto Exit;
570 }
571
Glenn Kasten53b5d092014-02-05 10:00:23 -0800572 // further sample rate checks are performed by createTrack_l() depending on the thread type
573 if (sampleRate == 0) {
574 ALOGE("createTrack() invalid sample rate %u", sampleRate);
575 lStatus = BAD_VALUE;
576 goto Exit;
577 }
578
579 // further channel mask checks are performed by createTrack_l() depending on the thread type
580 if (!audio_is_output_channel(channelMask)) {
581 ALOGE("createTrack() invalid channel mask %#x", channelMask);
582 lStatus = BAD_VALUE;
583 goto Exit;
584 }
585
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700586 // further format checks are performed by createTrack_l() depending on the thread type
587 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800588 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700589 lStatus = BAD_VALUE;
590 goto Exit;
591 }
592
Glenn Kasten663c2242013-09-24 11:52:37 -0700593 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
594 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
595 lStatus = BAD_VALUE;
596 goto Exit;
597 }
598
Mathias Agopian65ab4712010-07-14 17:59:35 -0700599 {
600 Mutex::Autolock _l(mLock);
601 PlaybackThread *thread = checkPlaybackThread_l(output);
602 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800603 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700604 lStatus = BAD_VALUE;
605 goto Exit;
606 }
607
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800608 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -0700609 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700610
Glenn Kastene848bd92014-03-13 15:00:32 -0700611 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700612 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700613 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700614 // check if an effect chain with the same session ID is present on another
615 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700616 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700617 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
618 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700619 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700620 if (sessions & PlaybackThread::EFFECT_SESSION) {
621 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700622 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700623 }
Eric Laurentde070132010-07-13 04:45:46 -0700624 }
625 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700626 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700627 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700628 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700629 if (sessionId != NULL) {
630 *sessionId = lSessionId;
631 }
632 }
Steve Block3856b092011-10-20 11:56:00 +0100633 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634
635 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800636 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800637 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700638 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700639
640 // move effect chain to this output thread if an effect on same session was waiting
641 // for a track to be created
642 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700643 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700644 Mutex::Autolock _dl(thread->mLock);
645 Mutex::Autolock _sl(effectThread->mLock);
646 moveEffectChain_l(lSessionId, effectThread, thread, true);
647 }
Eric Laurenta011e352012-03-29 15:51:43 -0700648
649 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700650 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700651 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
652 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700653 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700654 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700655 } else {
656 mPendingSyncEvents[i]->cancel();
657 }
Eric Laurenta011e352012-03-29 15:51:43 -0700658 mPendingSyncEvents.removeAt(i);
659 i--;
660 }
661 }
662 }
Glenn Kastene198c362013-08-13 09:13:36 -0700663
Eric Laurentfa90e842014-10-17 18:12:31 -0700664 setAudioHwSyncForSession_l(thread, (audio_session_t)lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700665 }
Glenn Kastene198c362013-08-13 09:13:36 -0700666
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700667 if (lStatus != NO_ERROR) {
668 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700669 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700670 // Don't hold mClientLock when releasing the reference on the track as the
671 // destructor will acquire it.
672 {
673 Mutex::Autolock _cl(mClientLock);
674 client.clear();
675 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700676 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700677 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678 }
679
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700680 // return handle to client
681 trackHandle = new TrackHandle(track);
682
Mathias Agopian65ab4712010-07-14 17:59:35 -0700683Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700684 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 return trackHandle;
686}
687
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800688uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700689{
690 Mutex::Autolock _l(mLock);
691 PlaybackThread *thread = checkPlaybackThread_l(output);
692 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000693 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694 return 0;
695 }
696 return thread->sampleRate();
697}
698
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800699audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700700{
701 Mutex::Autolock _l(mLock);
702 PlaybackThread *thread = checkPlaybackThread_l(output);
703 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000704 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800705 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700706 }
707 return thread->format();
708}
709
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800710size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700711{
712 Mutex::Autolock _l(mLock);
713 PlaybackThread *thread = checkPlaybackThread_l(output);
714 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000715 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700716 return 0;
717 }
Glenn Kasten58912562012-04-03 10:45:00 -0700718 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
719 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720 return thread->frameCount();
721}
722
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800723uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700724{
725 Mutex::Autolock _l(mLock);
726 PlaybackThread *thread = checkPlaybackThread_l(output);
727 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800728 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700729 return 0;
730 }
731 return thread->latency();
732}
733
734status_t AudioFlinger::setMasterVolume(float value)
735{
Eric Laurenta1884f92011-08-23 08:25:03 -0700736 status_t ret = initCheck();
737 if (ret != NO_ERROR) {
738 return ret;
739 }
740
Mathias Agopian65ab4712010-07-14 17:59:35 -0700741 // check calling permissions
742 if (!settingsAllowed()) {
743 return PERMISSION_DENIED;
744 }
745
Eric Laurenta4c5a552012-03-29 10:12:40 -0700746 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700747 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700748
John Grossmanee578c02012-07-23 17:05:46 -0700749 // Set master volume in the HALs which support it.
750 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
751 AutoMutex lock(mHardwareLock);
752 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800753
John Grossmanee578c02012-07-23 17:05:46 -0700754 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
755 if (dev->canSetMasterVolume()) {
756 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800757 }
John Grossmanee578c02012-07-23 17:05:46 -0700758 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700759 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700760
John Grossmanee578c02012-07-23 17:05:46 -0700761 // Now set the master volume in each playback thread. Playback threads
762 // assigned to HALs which do not have master volume support will apply
763 // master volume during the mix operation. Threads with HALs which do
764 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700765 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
766 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
767 continue;
768 }
John Grossmanee578c02012-07-23 17:05:46 -0700769 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700770 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700771
772 return NO_ERROR;
773}
774
Glenn Kastenf78aee72012-01-04 11:00:47 -0800775status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700776{
Eric Laurenta1884f92011-08-23 08:25:03 -0700777 status_t ret = initCheck();
778 if (ret != NO_ERROR) {
779 return ret;
780 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700781
782 // check calling permissions
783 if (!settingsAllowed()) {
784 return PERMISSION_DENIED;
785 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800786 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000787 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700788 return BAD_VALUE;
789 }
790
791 { // scope for the lock
792 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700793 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700795 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700796 mHardwareStatus = AUDIO_HW_IDLE;
797 }
798
799 if (NO_ERROR == ret) {
800 Mutex::Autolock _l(mLock);
801 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800802 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700803 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700804 }
805
806 return ret;
807}
808
809status_t AudioFlinger::setMicMute(bool state)
810{
Eric Laurenta1884f92011-08-23 08:25:03 -0700811 status_t ret = initCheck();
812 if (ret != NO_ERROR) {
813 return ret;
814 }
815
Mathias Agopian65ab4712010-07-14 17:59:35 -0700816 // check calling permissions
817 if (!settingsAllowed()) {
818 return PERMISSION_DENIED;
819 }
820
821 AutoMutex lock(mHardwareLock);
822 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700823 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
824 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
825 status_t result = dev->set_mic_mute(dev, state);
826 if (result != NO_ERROR) {
827 ret = result;
828 }
829 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700830 mHardwareStatus = AUDIO_HW_IDLE;
831 return ret;
832}
833
834bool AudioFlinger::getMicMute() const
835{
Eric Laurenta1884f92011-08-23 08:25:03 -0700836 status_t ret = initCheck();
837 if (ret != NO_ERROR) {
838 return false;
839 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800840 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700841 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800842 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700843 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800844 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
845 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
846 status_t result = dev->get_mic_mute(dev, &state);
847 if (result == NO_ERROR) {
848 mute = mute && state;
849 }
850 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700851 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800852
853 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700854}
855
856status_t AudioFlinger::setMasterMute(bool muted)
857{
John Grossmand8f178d2012-07-20 14:51:35 -0700858 status_t ret = initCheck();
859 if (ret != NO_ERROR) {
860 return ret;
861 }
862
Mathias Agopian65ab4712010-07-14 17:59:35 -0700863 // check calling permissions
864 if (!settingsAllowed()) {
865 return PERMISSION_DENIED;
866 }
867
John Grossmanee578c02012-07-23 17:05:46 -0700868 Mutex::Autolock _l(mLock);
869 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700870
John Grossmanee578c02012-07-23 17:05:46 -0700871 // Set master mute in the HALs which support it.
872 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
873 AutoMutex lock(mHardwareLock);
874 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700875
John Grossmanee578c02012-07-23 17:05:46 -0700876 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
877 if (dev->canSetMasterMute()) {
878 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700879 }
John Grossmanee578c02012-07-23 17:05:46 -0700880 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700881 }
882
John Grossmanee578c02012-07-23 17:05:46 -0700883 // Now set the master mute in each playback thread. Playback threads
884 // assigned to HALs which do not have master mute support will apply master
885 // mute during the mix operation. Threads with HALs which do support master
886 // mute will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700887 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
888 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
889 continue;
890 }
John Grossmanee578c02012-07-23 17:05:46 -0700891 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700892 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700893
894 return NO_ERROR;
895}
896
897float AudioFlinger::masterVolume() const
898{
Glenn Kasten98067102011-12-13 11:47:54 -0800899 Mutex::Autolock _l(mLock);
900 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700901}
902
903bool AudioFlinger::masterMute() const
904{
Glenn Kasten98067102011-12-13 11:47:54 -0800905 Mutex::Autolock _l(mLock);
906 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700907}
908
John Grossman4ff14ba2012-02-08 16:37:41 -0800909float AudioFlinger::masterVolume_l() const
910{
John Grossman4ff14ba2012-02-08 16:37:41 -0800911 return mMasterVolume;
912}
913
John Grossmand8f178d2012-07-20 14:51:35 -0700914bool AudioFlinger::masterMute_l() const
915{
John Grossmanee578c02012-07-23 17:05:46 -0700916 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700917}
918
Eric Laurent223fd5c2014-11-11 13:43:36 -0800919status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
920{
921 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
922 ALOGW("setStreamVolume() invalid stream %d", stream);
923 return BAD_VALUE;
924 }
925 pid_t caller = IPCThreadState::self()->getCallingPid();
926 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
927 ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
928 return PERMISSION_DENIED;
929 }
930
931 return NO_ERROR;
932}
933
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800934status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
935 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700936{
937 // check calling permissions
938 if (!settingsAllowed()) {
939 return PERMISSION_DENIED;
940 }
941
Eric Laurent223fd5c2014-11-11 13:43:36 -0800942 status_t status = checkStreamType(stream);
943 if (status != NO_ERROR) {
944 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700945 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800946 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947
948 AutoMutex lock(mLock);
949 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700950 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700951 thread = checkPlaybackThread_l(output);
952 if (thread == NULL) {
953 return BAD_VALUE;
954 }
955 }
956
957 mStreamTypes[stream].volume = value;
958
959 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800960 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700961 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700962 }
963 } else {
964 thread->setStreamVolume(stream, value);
965 }
966
967 return NO_ERROR;
968}
969
Glenn Kastenfff6d712012-01-12 16:38:12 -0800970status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971{
972 // check calling permissions
973 if (!settingsAllowed()) {
974 return PERMISSION_DENIED;
975 }
976
Eric Laurent223fd5c2014-11-11 13:43:36 -0800977 status_t status = checkStreamType(stream);
978 if (status != NO_ERROR) {
979 return status;
980 }
981 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
982
983 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000984 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700985 return BAD_VALUE;
986 }
987
Eric Laurent93575202011-01-18 18:39:02 -0800988 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700989 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700990 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700991 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700992
993 return NO_ERROR;
994}
995
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800996float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700997{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800998 status_t status = checkStreamType(stream);
999 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001000 return 0.0f;
1001 }
1002
1003 AutoMutex lock(mLock);
1004 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -07001005 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001006 PlaybackThread *thread = checkPlaybackThread_l(output);
1007 if (thread == NULL) {
1008 return 0.0f;
1009 }
1010 volume = thread->streamVolume(stream);
1011 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001012 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001013 }
1014
1015 return volume;
1016}
1017
Glenn Kastenfff6d712012-01-12 16:38:12 -08001018bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001019{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001020 status_t status = checkStreamType(stream);
1021 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001022 return true;
1023 }
1024
Glenn Kasten6637baa2012-01-09 09:40:36 -08001025 AutoMutex lock(mLock);
1026 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001027}
1028
Eric Laurent054d9d32015-04-24 08:48:48 -07001029
1030void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1031{
1032 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1033 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1034 }
1035}
1036
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001037status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001038{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001039 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1040 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001041
Mathias Agopian65ab4712010-07-14 17:59:35 -07001042 // check calling permissions
1043 if (!settingsAllowed()) {
1044 return PERMISSION_DENIED;
1045 }
1046
Glenn Kasten142f5192014-03-25 17:44:59 -07001047 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1048 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001049 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -07001050 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001051 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001052 AutoMutex lock(mHardwareLock);
1053 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1054 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1055 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1056 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1057 final_result = result ?: final_result;
1058 }
1059 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001060 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001061 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1062 AudioParameter param = AudioParameter(keyValuePairs);
1063 String8 value;
1064 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001065 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1066 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001067 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1068 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001069 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001070 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1071 // collect all of the thread's session IDs
1072 KeyedVector<int, bool> ids = thread->sessionIds();
1073 // suspend effects associated with those session IDs
1074 for (size_t j = 0; j < ids.size(); ++j) {
1075 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001076 thread->setEffectSuspended(FX_IID_AEC,
1077 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001078 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001079 thread->setEffectSuspended(FX_IID_NS,
1080 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001081 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001082 }
1083 }
Eric Laurentbee53372011-08-29 12:42:48 -07001084 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001085 }
1086 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001087 String8 screenState;
1088 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1089 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001090 if (isOff != (AudioFlinger::mScreenState & 1)) {
1091 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001092 }
1093 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001094 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001095 }
1096
1097 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1098 // and the thread is exited once the lock is released
1099 sp<ThreadBase> thread;
1100 {
1101 Mutex::Autolock _l(mLock);
1102 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001103 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001104 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001105 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001106 // indicate output device change to all input threads for pre processing
1107 AudioParameter param = AudioParameter(keyValuePairs);
1108 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001109 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1110 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001111 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001112 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001113 }
1114 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001115 if (thread != 0) {
1116 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001117 }
1118 return BAD_VALUE;
1119}
1120
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001121String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001122{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001123 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1124 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001125
Eric Laurenta4c5a552012-03-29 10:12:40 -07001126 Mutex::Autolock _l(mLock);
1127
Glenn Kasten142f5192014-03-25 17:44:59 -07001128 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001129 String8 out_s8;
1130
Dima Zavin799a70e2011-04-18 16:57:27 -07001131 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001132 char *s;
1133 {
1134 AutoMutex lock(mHardwareLock);
1135 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001136 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001137 s = dev->get_parameters(dev, keys.string());
1138 mHardwareStatus = AUDIO_HW_IDLE;
1139 }
John Grossmanef7740b2012-02-09 11:28:36 -08001140 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001141 free(s);
1142 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001143 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001144 }
1145
Mathias Agopian65ab4712010-07-14 17:59:35 -07001146 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1147 if (playbackThread != NULL) {
1148 return playbackThread->getParameters(keys);
1149 }
1150 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1151 if (recordThread != NULL) {
1152 return recordThread->getParameters(keys);
1153 }
1154 return String8("");
1155}
1156
Glenn Kastendd8104c2012-07-02 12:42:44 -07001157size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1158 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001159{
Eric Laurenta1884f92011-08-23 08:25:03 -07001160 status_t ret = initCheck();
1161 if (ret != NO_ERROR) {
1162 return 0;
1163 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001164 if ((sampleRate == 0) ||
1165 !audio_is_valid_format(format) || !audio_is_linear_pcm(format) ||
1166 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001167 return 0;
1168 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001169
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001170 AutoMutex lock(mHardwareLock);
1171 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001172 audio_config_t config, proposed;
1173 memset(&proposed, 0, sizeof(proposed));
1174 proposed.sample_rate = sampleRate;
1175 proposed.channel_mask = channelMask;
1176 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001177
John Grossmanee578c02012-07-23 17:05:46 -07001178 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001179 size_t frames;
1180 for (;;) {
1181 // Note: config is currently a const parameter for get_input_buffer_size()
1182 // but we use a copy from proposed in case config changes from the call.
1183 config = proposed;
1184 frames = dev->get_input_buffer_size(dev, &config);
1185 if (frames != 0) {
1186 break; // hal success, config is the result
1187 }
1188 // change one parameter of the configuration each iteration to a more "common" value
1189 // to see if the device will support it.
1190 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1191 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1192 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1193 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1194 } else {
1195 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1196 "format %#x, channelMask 0x%X",
1197 sampleRate, format, channelMask);
1198 break; // retries failed, break out of loop with frames == 0.
1199 }
1200 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001201 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001202 if (frames > 0 && config.sample_rate != sampleRate) {
1203 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1204 }
1205 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001206}
1207
Glenn Kasten5f972c02014-01-13 09:59:31 -08001208uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001209{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001210 Mutex::Autolock _l(mLock);
1211
1212 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1213 if (recordThread != NULL) {
1214 return recordThread->getInputFramesLost();
1215 }
1216 return 0;
1217}
1218
1219status_t AudioFlinger::setVoiceVolume(float value)
1220{
Eric Laurenta1884f92011-08-23 08:25:03 -07001221 status_t ret = initCheck();
1222 if (ret != NO_ERROR) {
1223 return ret;
1224 }
1225
Mathias Agopian65ab4712010-07-14 17:59:35 -07001226 // check calling permissions
1227 if (!settingsAllowed()) {
1228 return PERMISSION_DENIED;
1229 }
1230
1231 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001232 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001233 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001234 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001235 mHardwareStatus = AUDIO_HW_IDLE;
1236
1237 return ret;
1238}
1239
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001240status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001241 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001242{
1243 status_t status;
1244
1245 Mutex::Autolock _l(mLock);
1246
1247 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1248 if (playbackThread != NULL) {
1249 return playbackThread->getRenderPosition(halFrames, dspFrames);
1250 }
1251
1252 return BAD_VALUE;
1253}
1254
1255void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1256{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001257 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001258 if (client == 0) {
1259 return;
1260 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001261 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001262 {
1263 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001264 if (mNotificationClients.indexOfKey(pid) < 0) {
1265 sp<NotificationClient> notificationClient = new NotificationClient(this,
1266 client,
1267 pid);
1268 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001269
Eric Laurent021cf962014-05-13 10:18:14 -07001270 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001271
Marco Nelissen06b46062014-11-14 07:58:25 -08001272 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001273 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001274 }
1275 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001276
Eric Laurent021cf962014-05-13 10:18:14 -07001277 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001278 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001279 // the config change is always sent from playback or record threads to avoid deadlock
1280 // with AudioSystem::gLock
1281 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1282 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1283 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001284
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001285 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1286 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001287 }
1288}
1289
1290void AudioFlinger::removeNotificationClient(pid_t pid)
1291{
1292 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001293 {
1294 Mutex::Autolock _cl(mClientLock);
1295 mNotificationClients.removeItem(pid);
1296 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001297
Steve Block3856b092011-10-20 11:56:00 +01001298 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001299 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001300 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001301 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001302 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001303 ALOGV(" pid %d @ %d", ref->mPid, i);
1304 if (ref->mPid == pid) {
1305 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001306 mAudioSessionRefs.removeAt(i);
1307 delete ref;
1308 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001309 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001310 } else {
1311 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001312 }
1313 }
1314 if (removed) {
1315 purgeStaleEffects_l();
1316 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001317}
1318
Eric Laurent73e26b62015-04-27 16:55:58 -07001319void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001320 const sp<AudioIoDescriptor>& ioDesc,
1321 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001322{
Eric Laurent021cf962014-05-13 10:18:14 -07001323 Mutex::Autolock _l(mClientLock);
1324 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001325 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001326 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1327 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1328 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001329 }
1330}
1331
Eric Laurent021cf962014-05-13 10:18:14 -07001332// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001333void AudioFlinger::removeClient_l(pid_t pid)
1334{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001335 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001336 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001337 mClients.removeItem(pid);
1338}
1339
Eric Laurent717e1282012-06-29 16:36:52 -07001340// getEffectThread_l() must be called with AudioFlinger::mLock held
1341sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1342{
1343 sp<PlaybackThread> thread;
1344
1345 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1346 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1347 ALOG_ASSERT(thread == 0);
1348 thread = mPlaybackThreads.valueAt(i);
1349 }
1350 }
1351
1352 return thread;
1353}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001354
Mathias Agopian65ab4712010-07-14 17:59:35 -07001355
Mathias Agopian65ab4712010-07-14 17:59:35 -07001356
1357// ----------------------------------------------------------------------------
1358
1359AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1360 : RefBase(),
1361 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001362 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001363{
Eric Laurentda73b6c2015-08-20 16:18:53 -07001364 size_t heapSize = kClientSharedHeapSizeBytes;
1365 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1366 // invalidated tracks
1367 if (!audioFlinger->isLowRamDevice()) {
1368 heapSize *= kClientSharedHeapSizeMultiplier;
1369 }
1370 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001371}
1372
Eric Laurent021cf962014-05-13 10:18:14 -07001373// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001374AudioFlinger::Client::~Client()
1375{
1376 mAudioFlinger->removeClient_l(mPid);
1377}
1378
Glenn Kasten435dbe62012-01-30 10:15:48 -08001379sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001380{
1381 return mMemoryDealer;
1382}
1383
1384// ----------------------------------------------------------------------------
1385
1386AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1387 const sp<IAudioFlingerClient>& client,
1388 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001389 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001390{
1391}
1392
1393AudioFlinger::NotificationClient::~NotificationClient()
1394{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001395}
1396
Glenn Kasten0f11b512014-01-31 16:18:54 -08001397void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001398{
1399 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001400 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001401}
1402
Mathias Agopian65ab4712010-07-14 17:59:35 -07001403
1404// ----------------------------------------------------------------------------
1405
Jeff Brown893a5642013-08-16 20:19:26 -07001406static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1407 return audio_is_remote_submix_device(inDevice);
1408}
1409
Mathias Agopian65ab4712010-07-14 17:59:35 -07001410sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001411 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001412 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001413 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001414 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001415 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001416 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001417 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001418 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001419 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001420 int *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001421 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001422 sp<IMemory>& cblk,
1423 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001424 status_t *status)
1425{
1426 sp<RecordThread::RecordTrack> recordTrack;
1427 sp<RecordHandle> recordHandle;
1428 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001429 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001430 int lSessionId;
1431
Glenn Kastend776ac62014-05-07 09:16:09 -07001432 cblk.clear();
1433 buffers.clear();
1434
Marco Nelissendcb346b2015-09-09 10:47:29 -07001435 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1436 if (!isTrustedCallingUid(callingUid)) {
Andy Hung879db192016-01-05 16:00:34 -08001437 ALOGW_IF((uid_t)clientUid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -07001438 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1439 clientUid = callingUid;
1440 }
1441
Mathias Agopian65ab4712010-07-14 17:59:35 -07001442 // check calling permissions
Marco Nelissendcb346b2015-09-09 10:47:29 -07001443 if (!recordingAllowed(opPackageName, tid, clientUid)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001444 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001445 lStatus = PERMISSION_DENIED;
1446 goto Exit;
1447 }
1448
Glenn Kasten53b5d092014-02-05 10:00:23 -08001449 // further sample rate checks are performed by createRecordTrack_l()
1450 if (sampleRate == 0) {
1451 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1452 lStatus = BAD_VALUE;
1453 goto Exit;
1454 }
1455
Andy Hung6770c6f2015-04-07 13:43:36 -07001456 // we don't yet support anything other than linear PCM
1457 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001458 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001459 lStatus = BAD_VALUE;
1460 goto Exit;
1461 }
1462
Glenn Kasten53b5d092014-02-05 10:00:23 -08001463 // further channel mask checks are performed by createRecordTrack_l()
1464 if (!audio_is_input_channel(channelMask)) {
1465 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1466 lStatus = BAD_VALUE;
1467 goto Exit;
1468 }
1469
Glenn Kasten05997e22014-03-13 15:08:33 -07001470 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001471 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001472 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001473 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001474 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001475 lStatus = BAD_VALUE;
1476 goto Exit;
1477 }
1478
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001479 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001480 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001481
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001482 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001483 lSessionId = *sessionId;
1484 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001485 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001486 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001487 if (sessionId != NULL) {
1488 *sessionId = lSessionId;
1489 }
1490 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001491 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001492
Glenn Kasten1879fff2012-07-11 15:36:59 -07001493 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001494 frameCount, lSessionId, notificationFrames,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001495 clientUid, flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001496 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001497
1498 if (lStatus == NO_ERROR) {
1499 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1500 // session and move it to this thread.
1501 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)lSessionId);
1502 if (chain != 0) {
1503 Mutex::Autolock _l(thread->mLock);
1504 thread->addEffectChain_l(chain);
1505 }
1506 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001507 }
Glenn Kastene198c362013-08-13 09:13:36 -07001508
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001509 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001510 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001511 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001512 // Don't hold mClientLock when releasing the reference on the track as the
1513 // destructor will acquire it.
1514 {
1515 Mutex::Autolock _cl(mClientLock);
1516 client.clear();
1517 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001518 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001519 goto Exit;
1520 }
1521
Glenn Kastend776ac62014-05-07 09:16:09 -07001522 cblk = recordTrack->getCblk();
1523 buffers = recordTrack->getBuffers();
1524
Glenn Kasten2fc14732013-08-05 14:58:14 -07001525 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001526 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001527
1528Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001529 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001530 return recordHandle;
1531}
1532
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001533
1534
Mathias Agopian65ab4712010-07-14 17:59:35 -07001535// ----------------------------------------------------------------------------
1536
Eric Laurenta4c5a552012-03-29 10:12:40 -07001537audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1538{
Eric Laurent44622db2014-08-01 19:00:33 -07001539 if (name == NULL) {
1540 return 0;
1541 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001542 if (!settingsAllowed()) {
1543 return 0;
1544 }
1545 Mutex::Autolock _l(mLock);
1546 return loadHwModule_l(name);
1547}
1548
1549// loadHwModule_l() must be called with AudioFlinger::mLock held
1550audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1551{
1552 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1553 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1554 ALOGW("loadHwModule() module %s already loaded", name);
1555 return mAudioHwDevs.keyAt(i);
1556 }
1557 }
1558
Eric Laurenta4c5a552012-03-29 10:12:40 -07001559 audio_hw_device_t *dev;
1560
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001561 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001562 if (rc) {
1563 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1564 return 0;
1565 }
1566
1567 mHardwareStatus = AUDIO_HW_INIT;
1568 rc = dev->init_check(dev);
1569 mHardwareStatus = AUDIO_HW_IDLE;
1570 if (rc) {
1571 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1572 return 0;
1573 }
1574
John Grossmanee578c02012-07-23 17:05:46 -07001575 // Check and cache this HAL's level of support for master mute and master
1576 // volume. If this is the first HAL opened, and it supports the get
1577 // methods, use the initial values provided by the HAL as the current
1578 // master mute and volume settings.
1579
1580 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1581 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001582 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001583
1584 if (0 == mAudioHwDevs.size()) {
1585 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1586 if (NULL != dev->get_master_volume) {
1587 float mv;
1588 if (OK == dev->get_master_volume(dev, &mv)) {
1589 mMasterVolume = mv;
1590 }
1591 }
1592
1593 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1594 if (NULL != dev->get_master_mute) {
1595 bool mm;
1596 if (OK == dev->get_master_mute(dev, &mm)) {
1597 mMasterMute = mm;
1598 }
1599 }
1600 }
1601
Eric Laurenta4c5a552012-03-29 10:12:40 -07001602 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001603 if ((NULL != dev->set_master_volume) &&
1604 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1605 flags = static_cast<AudioHwDevice::Flags>(flags |
1606 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1607 }
1608
1609 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1610 if ((NULL != dev->set_master_mute) &&
1611 (OK == dev->set_master_mute(dev, mMasterMute))) {
1612 flags = static_cast<AudioHwDevice::Flags>(flags |
1613 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1614 }
1615
Eric Laurenta4c5a552012-03-29 10:12:40 -07001616 mHardwareStatus = AUDIO_HW_IDLE;
1617 }
1618
1619 audio_module_handle_t handle = nextUniqueId();
Eric Laurent83b88082014-06-20 18:31:16 -07001620 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001621
1622 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001623 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001624
1625 return handle;
1626
1627}
1628
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001629// ----------------------------------------------------------------------------
1630
Glenn Kasten3b16c762012-11-14 08:44:39 -08001631uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001632{
1633 Mutex::Autolock _l(mLock);
1634 PlaybackThread *thread = primaryPlaybackThread_l();
1635 return thread != NULL ? thread->sampleRate() : 0;
1636}
1637
Glenn Kastene33054e2012-11-14 12:54:39 -08001638size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001639{
1640 Mutex::Autolock _l(mLock);
1641 PlaybackThread *thread = primaryPlaybackThread_l();
1642 return thread != NULL ? thread->frameCountHAL() : 0;
1643}
1644
1645// ----------------------------------------------------------------------------
1646
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001647status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1648{
1649 uid_t uid = IPCThreadState::self()->getCallingUid();
1650 if (uid != AID_SYSTEM) {
1651 return PERMISSION_DENIED;
1652 }
1653 Mutex::Autolock _l(mLock);
1654 if (mIsDeviceTypeKnown) {
1655 return INVALID_OPERATION;
1656 }
1657 mIsLowRamDevice = isLowRamDevice;
1658 mIsDeviceTypeKnown = true;
1659 return NO_ERROR;
1660}
1661
Eric Laurent93c3d412014-08-01 14:48:35 -07001662audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1663{
1664 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001665
1666 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1667 if (index >= 0) {
1668 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1669 mHwAvSyncIds.valueAt(index), sessionId);
1670 return mHwAvSyncIds.valueAt(index);
1671 }
1672
1673 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1674 if (dev == NULL) {
1675 return AUDIO_HW_SYNC_INVALID;
1676 }
1677 char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1678 AudioParameter param = AudioParameter(String8(reply));
1679 free(reply);
1680
1681 int value;
1682 if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1683 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1684 return AUDIO_HW_SYNC_INVALID;
1685 }
1686
1687 // allow only one session for a given HW A/V sync ID.
1688 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1689 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1690 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1691 value, mHwAvSyncIds.keyAt(i));
1692 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001693 break;
1694 }
1695 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001696
1697 mHwAvSyncIds.add(sessionId, value);
1698
1699 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1700 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1701 uint32_t sessions = thread->hasAudioSession(sessionId);
1702 if (sessions & PlaybackThread::TRACK_SESSION) {
1703 AudioParameter param = AudioParameter();
1704 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1705 thread->setParameters(param.toString());
1706 break;
1707 }
1708 }
1709
1710 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1711 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001712}
1713
Eric Laurent72e3f392015-05-20 14:43:50 -07001714status_t AudioFlinger::systemReady()
1715{
1716 Mutex::Autolock _l(mLock);
1717 ALOGI("%s", __FUNCTION__);
1718 if (mSystemReady) {
1719 ALOGW("%s called twice", __FUNCTION__);
1720 return NO_ERROR;
1721 }
1722 mSystemReady = true;
1723 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1724 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1725 thread->systemReady();
1726 }
1727 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1728 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1729 thread->systemReady();
1730 }
1731 return NO_ERROR;
1732}
1733
Eric Laurentfa90e842014-10-17 18:12:31 -07001734// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1735void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1736{
1737 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1738 if (index >= 0) {
1739 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1740 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1741 AudioParameter param = AudioParameter();
1742 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1743 thread->setParameters(param.toString());
1744 }
1745}
1746
1747
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001748// ----------------------------------------------------------------------------
1749
Eric Laurent83b88082014-06-20 18:31:16 -07001750
1751sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001752 audio_io_handle_t *output,
1753 audio_config_t *config,
1754 audio_devices_t devices,
1755 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001756 audio_output_flags_t flags)
1757{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001758 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001759 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001760 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001761 }
1762
1763 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentcf2c0212014-07-25 16:20:43 -07001764 if (*output == AUDIO_IO_HANDLE_NONE) {
1765 *output = nextUniqueId();
1766 }
Eric Laurent83b88082014-06-20 18:31:16 -07001767
1768 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1769
Eric Laurent83b88082014-06-20 18:31:16 -07001770 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001771 // This if statement allows overriding the audio policy settings
1772 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1773 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1774 // Check only for Normal Mixing mode
1775 if (kEnableExtendedPrecision) {
1776 // Specify format (uncomment one below to choose)
1777 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1778 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1779 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1780 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001781 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001782 }
1783 if (kEnableExtendedChannels) {
1784 // Specify channel mask (uncomment one below to choose)
1785 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1786 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1787 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1788 }
Eric Laurent83b88082014-06-20 18:31:16 -07001789 }
1790
Phil Burk062e67a2015-02-11 13:40:50 -08001791 AudioStreamOut *outputStream = NULL;
1792 status_t status = outHwDev->openOutputStream(
1793 &outputStream,
1794 *output,
1795 devices,
1796 flags,
1797 config,
1798 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001799
1800 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001801
Phil Burk062e67a2015-02-11 13:40:50 -08001802 if (status == NO_ERROR) {
Eric Laurent83b88082014-06-20 18:31:16 -07001803
1804 PlaybackThread *thread;
1805 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001806 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001807 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001808 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1809 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001810 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001811 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001812 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001813 } else {
Eric Laurent72e3f392015-05-20 14:43:50 -07001814 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001815 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001816 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001817 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001818 return thread;
1819 }
1820
1821 return 0;
1822}
1823
Eric Laurentcf2c0212014-07-25 16:20:43 -07001824status_t AudioFlinger::openOutput(audio_module_handle_t module,
1825 audio_io_handle_t *output,
1826 audio_config_t *config,
1827 audio_devices_t *devices,
1828 const String8& address,
1829 uint32_t *latencyMs,
1830 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001831{
Phil Burk062e67a2015-02-11 13:40:50 -08001832 ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001833 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001834 (devices != NULL) ? *devices : 0,
1835 config->sample_rate,
1836 config->format,
1837 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001838 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001839
Eric Laurentcf2c0212014-07-25 16:20:43 -07001840 if (*devices == AUDIO_DEVICE_NONE) {
1841 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001842 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001843
Mathias Agopian65ab4712010-07-14 17:59:35 -07001844 Mutex::Autolock _l(mLock);
1845
Eric Laurentcf2c0212014-07-25 16:20:43 -07001846 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001847 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001848 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001849
1850 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001851 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001852
1853 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001854 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001855 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001856 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001857
1858 AutoMutex lock(mHardwareLock);
1859 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001860 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001861 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001862 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001863 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001864 }
1865
Eric Laurentcf2c0212014-07-25 16:20:43 -07001866 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001867}
1868
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001869audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1870 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001871{
1872 Mutex::Autolock _l(mLock);
1873 MixerThread *thread1 = checkMixerThread_l(output1);
1874 MixerThread *thread2 = checkMixerThread_l(output2);
1875
1876 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001877 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1878 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001879 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001880 }
1881
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001882 audio_io_handle_t id = nextUniqueId();
Eric Laurent72e3f392015-05-20 14:43:50 -07001883 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001884 thread->addOutputTrack(thread2);
1885 mPlaybackThreads.add(id, thread);
1886 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001887 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001888 return id;
1889}
1890
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001891status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001892{
Glenn Kastend96c5722012-04-25 13:44:49 -07001893 return closeOutput_nonvirtual(output);
1894}
1895
1896status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1897{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001898 // keep strong reference on the playback thread so that
1899 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001900 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001901 {
1902 Mutex::Autolock _l(mLock);
1903 thread = checkPlaybackThread_l(output);
1904 if (thread == NULL) {
1905 return BAD_VALUE;
1906 }
1907
Steve Block3856b092011-10-20 11:56:00 +01001908 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001909
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001910 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001911 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentf6870ae2015-05-08 10:50:03 -07001912 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001913 DuplicatingThread *dupThread =
1914 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001915 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001916 }
1917 }
1918 }
1919
1920
1921 mPlaybackThreads.removeItem(output);
1922 // save all effects to the default thread
1923 if (mPlaybackThreads.size()) {
1924 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1925 if (dstThread != NULL) {
1926 // audioflinger lock is held here so the acquisition order of thread locks does not
1927 // matter
1928 Mutex::Autolock _dl(dstThread->mLock);
1929 Mutex::Autolock _sl(thread->mLock);
1930 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1931 for (size_t i = 0; i < effectChains.size(); i ++) {
1932 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001933 }
1934 }
1935 }
Eric Laurent73e26b62015-04-27 16:55:58 -07001936 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
1937 ioDesc->mIoHandle = output;
1938 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001939 }
1940 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001941 // The thread entity (active unit of execution) is no longer running here,
1942 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001943
Eric Laurentf6870ae2015-05-08 10:50:03 -07001944 if (!thread->isDuplicating()) {
Eric Laurent83b88082014-06-20 18:31:16 -07001945 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001946 }
Eric Laurent83b88082014-06-20 18:31:16 -07001947
Mathias Agopian65ab4712010-07-14 17:59:35 -07001948 return NO_ERROR;
1949}
1950
Eric Laurent83b88082014-06-20 18:31:16 -07001951void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
1952{
1953 AudioStreamOut *out = thread->clearOutput();
1954 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1955 // from now on thread->mOutput is NULL
1956 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
1957 delete out;
1958}
1959
1960void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
1961{
1962 mPlaybackThreads.removeItem(thread->mId);
1963 thread->exit();
1964 closeOutputFinish(thread);
1965}
1966
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001967status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001968{
1969 Mutex::Autolock _l(mLock);
1970 PlaybackThread *thread = checkPlaybackThread_l(output);
1971
1972 if (thread == NULL) {
1973 return BAD_VALUE;
1974 }
1975
Steve Block3856b092011-10-20 11:56:00 +01001976 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001977 thread->suspend();
1978
1979 return NO_ERROR;
1980}
1981
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001982status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001983{
1984 Mutex::Autolock _l(mLock);
1985 PlaybackThread *thread = checkPlaybackThread_l(output);
1986
1987 if (thread == NULL) {
1988 return BAD_VALUE;
1989 }
1990
Steve Block3856b092011-10-20 11:56:00 +01001991 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001992
1993 thread->restore();
1994
1995 return NO_ERROR;
1996}
1997
Eric Laurentcf2c0212014-07-25 16:20:43 -07001998status_t AudioFlinger::openInput(audio_module_handle_t module,
1999 audio_io_handle_t *input,
2000 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002001 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002002 const String8& address,
2003 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002004 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002005{
Eric Laurent83b88082014-06-20 18:31:16 -07002006 Mutex::Autolock _l(mLock);
2007
Glenn Kastene7d66712015-03-05 13:46:52 -08002008 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002009 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002010 }
2011
Glenn Kastene7d66712015-03-05 13:46:52 -08002012 sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002013
2014 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002015 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002016 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002017 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002018 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002019 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002020}
Dima Zavin799a70e2011-04-18 16:57:27 -07002021
Eric Laurent83b88082014-06-20 18:31:16 -07002022sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002023 audio_io_handle_t *input,
2024 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002025 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002026 const String8& address,
2027 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002028 audio_input_flags_t flags)
2029{
Glenn Kastene7d66712015-03-05 13:46:52 -08002030 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002031 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002032 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002033 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002034 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002035
Eric Laurentcf2c0212014-07-25 16:20:43 -07002036 if (*input == AUDIO_IO_HANDLE_NONE) {
2037 *input = nextUniqueId();
2038 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002039
Eric Laurentcf2c0212014-07-25 16:20:43 -07002040 audio_config_t halconfig = *config;
2041 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07002042 audio_stream_in_t *inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002043 status_t status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002044 &inStream, flags, address.string(), source);
2045 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002046 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Dima Zavin799a70e2011-04-18 16:57:27 -07002047 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002048 halconfig.sample_rate,
2049 halconfig.format,
2050 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002051 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002052 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002053
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002054 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002055 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002056 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002057 audio_is_linear_pcm(config->format) &&
2058 audio_is_linear_pcm(halconfig.format) &&
2059 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
Eric Laurentcf2c0212014-07-25 16:20:43 -07002060 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
2061 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002062 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002063 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002064 inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002065 status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002066 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07002067 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002068 }
2069
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002070 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002071
Glenn Kasten46909e72013-02-26 09:20:22 -08002072#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002073 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2074 // or (re-)create if current Pipe is idle and does not match the new format
2075 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002076 enum {
2077 TEE_SINK_NO, // don't copy input
2078 TEE_SINK_NEW, // copy input using a new pipe
2079 TEE_SINK_OLD, // copy input using an existing pipe
2080 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002081 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2082 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002083 if (!mTeeSinkInputEnabled) {
2084 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002085 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002086 kind = TEE_SINK_NO;
2087 } else if (mRecordTeeSink == 0) {
2088 kind = TEE_SINK_NEW;
2089 } else if (mRecordTeeSink->getStrongCount() != 1) {
2090 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002091 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002092 kind = TEE_SINK_OLD;
2093 } else {
2094 kind = TEE_SINK_NEW;
2095 }
2096 switch (kind) {
2097 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002098 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002099 size_t numCounterOffers = 0;
2100 const NBAIO_Format offers[1] = {format};
2101 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2102 ALOG_ASSERT(index == 0);
2103 PipeReader *pipeReader = new PipeReader(*pipe);
2104 numCounterOffers = 0;
2105 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2106 ALOG_ASSERT(index == 0);
2107 mRecordTeeSink = pipe;
2108 mRecordTeeSource = pipeReader;
2109 teeSink = pipe;
2110 }
2111 break;
2112 case TEE_SINK_OLD:
2113 teeSink = mRecordTeeSink;
2114 break;
2115 case TEE_SINK_NO:
2116 default:
2117 break;
2118 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002119#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002120
Eric Laurentcf2c0212014-07-25 16:20:43 -07002121 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002122
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002123 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002124 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002125 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002126 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002127 inputStream,
2128 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002129 primaryOutputDevice_l(),
Eric Laurent72e3f392015-05-20 14:43:50 -07002130 devices,
2131 mSystemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08002132#ifdef TEE_SINK
2133 , teeSink
2134#endif
2135 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002136 mRecordThreads.add(*input, thread);
2137 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002138 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002139 }
2140
Eric Laurentcf2c0212014-07-25 16:20:43 -07002141 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002142 return 0;
2143}
2144
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002145status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002146{
Glenn Kastend96c5722012-04-25 13:44:49 -07002147 return closeInput_nonvirtual(input);
2148}
2149
2150status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2151{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002152 // keep strong reference on the record thread so that
2153 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002154 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002155 {
2156 Mutex::Autolock _l(mLock);
2157 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002158 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002159 return BAD_VALUE;
2160 }
2161
Steve Block3856b092011-10-20 11:56:00 +01002162 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002163
2164 // If we still have effect chains, it means that a client still holds a handle
2165 // on at least one effect. We must either move the chain to an existing thread with the
2166 // same session ID or put it aside in case a new record thread is opened for a
2167 // new capture on the same session
2168 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002169 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002170 Mutex::Autolock _sl(thread->mLock);
2171 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002172 // Note: maximum one chain per record thread
2173 if (effectChains.size() != 0) {
2174 chain = effectChains[0];
2175 }
2176 }
2177 if (chain != 0) {
2178 // first check if a record thread is already opened with a client on the same session.
2179 // This should only happen in case of overlap between one thread tear down and the
2180 // creation of its replacement
2181 size_t i;
2182 for (i = 0; i < mRecordThreads.size(); i++) {
2183 sp<RecordThread> t = mRecordThreads.valueAt(i);
2184 if (t == thread) {
2185 continue;
2186 }
2187 if (t->hasAudioSession(chain->sessionId()) != 0) {
2188 Mutex::Autolock _l(t->mLock);
2189 ALOGV("closeInput() found thread %d for effect session %d",
2190 t->id(), chain->sessionId());
2191 t->addEffectChain_l(chain);
2192 break;
2193 }
2194 }
2195 // put the chain aside if we could not find a record thread with the same session id.
2196 if (i == mRecordThreads.size()) {
2197 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002198 }
2199 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002200 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2201 ioDesc->mIoHandle = input;
2202 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002203 mRecordThreads.removeItem(input);
2204 }
Eric Laurent83b88082014-06-20 18:31:16 -07002205 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2206 // we have a different lock for notification client
2207 closeInputFinish(thread);
2208 return NO_ERROR;
2209}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002210
Eric Laurent83b88082014-06-20 18:31:16 -07002211void AudioFlinger::closeInputFinish(sp<RecordThread> thread)
2212{
2213 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002214 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002215 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002216 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002217 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002218 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002219}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002220
Eric Laurent83b88082014-06-20 18:31:16 -07002221void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
2222{
2223 mRecordThreads.removeItem(thread->mId);
2224 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002225}
2226
Glenn Kastend2304db2014-02-03 07:40:31 -08002227status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002228{
2229 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002230 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002231
2232 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2233 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002234 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002235 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002236
2237 return NO_ERROR;
2238}
2239
2240
Eric Laurentde3f8392014-07-27 18:38:22 -07002241audio_unique_id_t AudioFlinger::newAudioUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002242{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002243 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002244}
2245
Marco Nelissend457c972014-02-11 08:47:07 -08002246void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002247{
2248 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002249 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002250 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2251 if (pid != -1 && (caller == getpid_cached)) {
2252 caller = pid;
2253 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002254
Eric Laurent021cf962014-05-13 10:18:14 -07002255 {
2256 Mutex::Autolock _cl(mClientLock);
2257 // Ignore requests received from processes not known as notification client. The request
2258 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2259 // called from a different pid leaving a stale session reference. Also we don't know how
2260 // to clear this reference if the client process dies.
2261 if (mNotificationClients.indexOfKey(caller) < 0) {
2262 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2263 return;
2264 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002265 }
2266
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002267 size_t num = mAudioSessionRefs.size();
2268 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002269 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002270 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2271 ref->mCnt++;
2272 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002273 return;
2274 }
2275 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002276 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2277 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002278}
2279
Marco Nelissend457c972014-02-11 08:47:07 -08002280void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002281{
2282 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002283 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002284 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2285 if (pid != -1 && (caller == getpid_cached)) {
2286 caller = pid;
2287 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002288 size_t num = mAudioSessionRefs.size();
2289 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002290 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002291 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2292 ref->mCnt--;
2293 ALOGV(" decremented refcount to %d", ref->mCnt);
2294 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002295 mAudioSessionRefs.removeAt(i);
2296 delete ref;
2297 purgeStaleEffects_l();
2298 }
2299 return;
2300 }
2301 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002302 // If the caller is mediaserver it is likely that the session being released was acquired
2303 // on behalf of a process not in notification clients and we ignore the warning.
2304 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002305}
2306
2307void AudioFlinger::purgeStaleEffects_l() {
2308
Steve Block3856b092011-10-20 11:56:00 +01002309 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002310
2311 Vector< sp<EffectChain> > chains;
2312
2313 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2314 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2315 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2316 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002317 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2318 chains.push(ec);
2319 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002320 }
2321 }
2322 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2323 sp<RecordThread> t = mRecordThreads.valueAt(i);
2324 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2325 sp<EffectChain> ec = t->mEffectChains[j];
2326 chains.push(ec);
2327 }
2328 }
2329
2330 for (size_t i = 0; i < chains.size(); i++) {
2331 sp<EffectChain> ec = chains[i];
2332 int sessionid = ec->sessionId();
2333 sp<ThreadBase> t = ec->mThread.promote();
2334 if (t == 0) {
2335 continue;
2336 }
2337 size_t numsessionrefs = mAudioSessionRefs.size();
2338 bool found = false;
2339 for (size_t k = 0; k < numsessionrefs; k++) {
2340 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002341 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002342 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002343 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002344 found = true;
2345 break;
2346 }
2347 }
2348 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002349 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002350 // remove all effects from the chain
2351 while (ec->mEffects.size()) {
2352 sp<EffectModule> effect = ec->mEffects[0];
2353 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002354 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002355 if (effect->purgeHandles()) {
2356 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002357 }
2358 AudioSystem::unregisterEffect(effect->id());
2359 }
2360 }
2361 }
2362 return;
2363}
2364
Mathias Agopian65ab4712010-07-14 17:59:35 -07002365// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002366AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002367{
Glenn Kastena1117922012-01-26 10:53:32 -08002368 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002369}
2370
2371// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002372AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002373{
2374 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002375 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002376}
2377
2378// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002379AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002380{
Glenn Kastena1117922012-01-26 10:53:32 -08002381 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002382}
2383
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002384uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002385{
Glenn Kastenbcefec32014-01-17 12:09:05 -08002386 return (uint32_t) android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002387}
2388
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002389AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002390{
2391 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2392 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002393 if(thread->isDuplicating()) {
2394 continue;
2395 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002396 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002397 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002398 return thread;
2399 }
2400 }
2401 return NULL;
2402}
2403
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002404audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002405{
2406 PlaybackThread *thread = primaryPlaybackThread_l();
2407
2408 if (thread == NULL) {
2409 return 0;
2410 }
2411
Eric Laurentf1c04f92012-08-28 14:26:53 -07002412 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002413}
2414
Eric Laurenta011e352012-03-29 15:51:43 -07002415sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2416 int triggerSession,
2417 int listenerSession,
2418 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002419 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002420{
2421 Mutex::Autolock _l(mLock);
2422
2423 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2424 status_t playStatus = NAME_NOT_FOUND;
2425 status_t recStatus = NAME_NOT_FOUND;
2426 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2427 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2428 if (playStatus == NO_ERROR) {
2429 return event;
2430 }
2431 }
2432 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2433 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2434 if (recStatus == NO_ERROR) {
2435 return event;
2436 }
2437 }
2438 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2439 mPendingSyncEvents.add(event);
2440 } else {
2441 ALOGV("createSyncEvent() invalid event %d", event->type());
2442 event.clear();
2443 }
2444 return event;
2445}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002446
Mathias Agopian65ab4712010-07-14 17:59:35 -07002447// ----------------------------------------------------------------------------
2448// Effect management
2449// ----------------------------------------------------------------------------
2450
2451
Glenn Kastenf587ba52012-01-26 16:25:10 -08002452status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002453{
2454 Mutex::Autolock _l(mLock);
2455 return EffectQueryNumberEffects(numEffects);
2456}
2457
Glenn Kastenf587ba52012-01-26 16:25:10 -08002458status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002459{
2460 Mutex::Autolock _l(mLock);
2461 return EffectQueryEffect(index, descriptor);
2462}
2463
Glenn Kasten5e92a782012-01-30 07:40:52 -08002464status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002465 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002466{
2467 Mutex::Autolock _l(mLock);
2468 return EffectGetDescriptor(pUuid, descriptor);
2469}
2470
2471
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002472sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002473 effect_descriptor_t *pDesc,
2474 const sp<IEffectClient>& effectClient,
2475 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002476 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002477 int sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002478 const String16& opPackageName,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002479 status_t *status,
2480 int *id,
2481 int *enabled)
2482{
2483 status_t lStatus = NO_ERROR;
2484 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002485 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002486
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002487 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002488 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002489 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002490
2491 if (pDesc == NULL) {
2492 lStatus = BAD_VALUE;
2493 goto Exit;
2494 }
2495
Eric Laurent84e9a102010-09-23 16:10:16 -07002496 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002497 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002498 lStatus = PERMISSION_DENIED;
2499 goto Exit;
2500 }
2501
Dima Zavinfce7a472011-04-19 22:30:36 -07002502 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002503 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002504 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002505 lStatus = PERMISSION_DENIED;
2506 goto Exit;
2507 }
2508
Mathias Agopian65ab4712010-07-14 17:59:35 -07002509 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002510 if (!EffectIsNullUuid(&pDesc->uuid)) {
2511 // if uuid is specified, request effect descriptor
2512 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2513 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002514 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002515 goto Exit;
2516 }
2517 } else {
2518 // if uuid is not specified, look for an available implementation
2519 // of the required type in effect factory
2520 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002521 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002522 lStatus = BAD_VALUE;
2523 goto Exit;
2524 }
2525 uint32_t numEffects = 0;
2526 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002527 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002528 bool found = false;
2529
2530 lStatus = EffectQueryNumberEffects(&numEffects);
2531 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002532 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002533 goto Exit;
2534 }
2535 for (uint32_t i = 0; i < numEffects; i++) {
2536 lStatus = EffectQueryEffect(i, &desc);
2537 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002538 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002539 continue;
2540 }
2541 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2542 // If matching type found save effect descriptor. If the session is
2543 // 0 and the effect is not auxiliary, continue enumeration in case
2544 // an auxiliary version of this effect type is available
2545 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002546 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002547 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002548 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2549 break;
2550 }
2551 }
2552 }
2553 if (!found) {
2554 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002555 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002556 goto Exit;
2557 }
2558 // For same effect type, chose auxiliary version over insert version if
2559 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002560 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002561 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002562 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002563 }
2564 }
2565
2566 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002567 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002568 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2569 lStatus = INVALID_OPERATION;
2570 goto Exit;
2571 }
2572
Eric Laurent59255e42011-07-27 19:49:51 -07002573 // check recording permission for visualizer
2574 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Marco Nelissendcb346b2015-09-09 10:47:29 -07002575 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07002576 lStatus = PERMISSION_DENIED;
2577 goto Exit;
2578 }
2579
Mathias Agopian65ab4712010-07-14 17:59:35 -07002580 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002581 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002582 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002583 // if the output returned by getOutputForEffect() is removed before we lock the
2584 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2585 // and we will exit safely
2586 io = AudioSystem::getOutputForEffect(&desc);
2587 ALOGV("createEffect got output %d", io);
2588 }
2589
2590 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002591
2592 // If output is not specified try to find a matching audio session ID in one of the
2593 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002594 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2595 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002596 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002597 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002598 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2599 // output must be specified by AudioPolicyManager when using session
2600 // AUDIO_SESSION_OUTPUT_STAGE
2601 lStatus = BAD_VALUE;
2602 goto Exit;
2603 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002604 // look for the thread where the specified audio session is present
2605 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2606 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2607 io = mPlaybackThreads.keyAt(i);
2608 break;
2609 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002610 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002611 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002612 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2613 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2614 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002615 break;
2616 }
2617 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002618 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002619 // If no output thread contains the requested session ID, default to
2620 // first output. The effect chain will be moved to the correct output
2621 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002622 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002623 io = mPlaybackThreads.keyAt(0);
2624 }
Steve Block3856b092011-10-20 11:56:00 +01002625 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002626 }
2627 ThreadBase *thread = checkRecordThread_l(io);
2628 if (thread == NULL) {
2629 thread = checkPlaybackThread_l(io);
2630 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002631 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002632 lStatus = BAD_VALUE;
2633 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002634 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002635 } else {
2636 // Check if one effect chain was awaiting for an effect to be created on this
2637 // session and used it instead of creating a new one.
2638 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)sessionId);
2639 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002640 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002641 thread->addEffectChain_l(chain);
2642 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002643 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002644
Eric Laurent021cf962014-05-13 10:18:14 -07002645 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002646
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002647 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002648 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2649 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002650 if (handle != 0 && id != NULL) {
2651 *id = handle->id();
2652 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002653 if (handle == 0) {
2654 // remove local strong reference to Client with mClientLock held
2655 Mutex::Autolock _cl(mClientLock);
2656 client.clear();
2657 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002658 }
2659
2660Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002661 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002662 return handle;
2663}
2664
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002665status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2666 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002667{
Steve Block3856b092011-10-20 11:56:00 +01002668 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002669 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002670 Mutex::Autolock _l(mLock);
2671 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002672 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002673 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002674 }
Eric Laurentde070132010-07-13 04:45:46 -07002675 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2676 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002677 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002678 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002679 }
Eric Laurentde070132010-07-13 04:45:46 -07002680 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2681 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002682 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002683 return BAD_VALUE;
2684 }
2685
2686 Mutex::Autolock _dl(dstThread->mLock);
2687 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002688 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002689}
2690
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002691// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002692status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002693 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002694 AudioFlinger::PlaybackThread *dstThread,
2695 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002696{
Steve Block3856b092011-10-20 11:56:00 +01002697 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002698 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002699
Eric Laurent59255e42011-07-27 19:49:51 -07002700 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002701 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002702 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002703 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002704 return INVALID_OPERATION;
2705 }
2706
Andy Hung9a592762014-07-21 21:56:01 -07002707 // Check whether the destination thread has a channel count of FCC_2, which is
2708 // currently required for (most) effects. Prevent moving the effect chain here rather
2709 // than disabling the addEffect_l() call in dstThread below.
Eric Laurentf6870ae2015-05-08 10:50:03 -07002710 if ((dstThread->type() == ThreadBase::MIXER || dstThread->isDuplicating()) &&
Eric Laurentb10352f2014-11-03 16:25:39 -08002711 dstThread->mChannelCount != FCC_2) {
Andy Hung9a592762014-07-21 21:56:01 -07002712 ALOGW("moveEffectChain_l() effect chain failed because"
2713 " destination thread %p channel count(%u) != %u",
2714 dstThread, dstThread->mChannelCount, FCC_2);
2715 return INVALID_OPERATION;
2716 }
2717
Eric Laurent39e94f82010-07-28 01:32:47 -07002718 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002719 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002720 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002721 // removed.
2722 srcThread->removeEffectChain_l(chain);
2723
2724 // transfer all effects one by one so that new effect chain is created on new thread with
2725 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002726 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002727 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002728 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002729 Vector< sp<EffectModule> > removed;
2730 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002731 while (effect != 0) {
2732 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002733 removed.add(effect);
2734 status = dstThread->addEffect_l(effect);
2735 if (status != NO_ERROR) {
2736 break;
2737 }
Eric Laurentec35a142011-10-05 17:42:25 -07002738 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2739 if (effect->state() == EffectModule::ACTIVE ||
2740 effect->state() == EffectModule::STOPPING) {
2741 effect->start();
2742 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002743 // if the move request is not received from audio policy manager, the effect must be
2744 // re-registered with the new strategy and output
2745 if (dstChain == 0) {
2746 dstChain = effect->chain().promote();
2747 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002748 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002749 status = NO_INIT;
2750 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002751 }
2752 strategy = dstChain->strategy();
2753 }
2754 if (reRegister) {
2755 AudioSystem::unregisterEffect(effect->id());
2756 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002757 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002758 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002759 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002760 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002761 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002762 }
Eric Laurentde070132010-07-13 04:45:46 -07002763 effect = chain->getEffectFromId_l(0);
2764 }
2765
Eric Laurent5baf2af2013-09-12 17:37:00 -07002766 if (status != NO_ERROR) {
2767 for (size_t i = 0; i < removed.size(); i++) {
2768 srcThread->addEffect_l(removed[i]);
2769 if (dstChain != 0 && reRegister) {
2770 AudioSystem::unregisterEffect(removed[i]->id());
2771 AudioSystem::registerEffect(&removed[i]->desc(),
2772 srcThread->id(),
2773 strategy,
2774 sessionId,
2775 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002776 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002777 }
2778 }
2779 }
2780
2781 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002782}
2783
Eric Laurent5baf2af2013-09-12 17:37:00 -07002784bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002785{
2786 if (mGlobalEffectEnableTime != 0 &&
2787 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2788 return true;
2789 }
2790
2791 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2792 sp<EffectChain> ec =
2793 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002794 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002795 return true;
2796 }
2797 }
2798 return false;
2799}
2800
Eric Laurent5baf2af2013-09-12 17:37:00 -07002801void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002802{
2803 Mutex::Autolock _l(mLock);
2804
2805 mGlobalEffectEnableTime = systemTime();
2806
2807 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2808 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2809 if (t->mType == ThreadBase::OFFLOAD) {
2810 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2811 }
2812 }
2813
2814}
2815
Eric Laurentaaa44472014-09-12 17:41:50 -07002816status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2817{
2818 audio_session_t session = (audio_session_t)chain->sessionId();
2819 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2820 ALOGV("putOrphanEffectChain_l session %d index %d", session, index);
2821 if (index >= 0) {
2822 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2823 return ALREADY_EXISTS;
2824 }
2825 mOrphanEffectChains.add(session, chain);
2826 return NO_ERROR;
2827}
2828
2829sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2830{
2831 sp<EffectChain> chain;
2832 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2833 ALOGV("getOrphanEffectChain_l session %d index %d", session, index);
2834 if (index >= 0) {
2835 chain = mOrphanEffectChains.valueAt(index);
2836 mOrphanEffectChains.removeItemsAt(index);
2837 }
2838 return chain;
2839}
2840
2841bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2842{
2843 Mutex::Autolock _l(mLock);
2844 audio_session_t session = (audio_session_t)effect->sessionId();
2845 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2846 ALOGV("updateOrphanEffectChains session %d index %d", session, index);
2847 if (index >= 0) {
2848 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2849 if (chain->removeEffect_l(effect) == 0) {
2850 ALOGV("updateOrphanEffectChains removing effect chain at index %d", index);
2851 mOrphanEffectChains.removeItemsAt(index);
2852 }
2853 return true;
2854 }
2855 return false;
2856}
2857
2858
Glenn Kastenda6ef132013-01-10 12:31:01 -08002859struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002860#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
2861 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002862};
2863
2864int comparEntry(const void *p1, const void *p2)
2865{
Glenn Kasten2f55e762015-03-05 16:05:08 -08002866 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002867}
2868
Glenn Kasten46909e72013-02-26 09:20:22 -08002869#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002870void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002871{
Eric Laurent81784c32012-11-19 14:55:58 -08002872 NBAIO_Source *teeSource = source.get();
2873 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002874 // .wav rotation
2875 // There is a benign race condition if 2 threads call this simultaneously.
2876 // They would both traverse the directory, but the result would simply be
2877 // failures at unlink() which are ignored. It's also unlikely since
2878 // normally dumpsys is only done by bugreport or from the command line.
2879 char teePath[32+256];
2880 strcpy(teePath, "/data/misc/media");
2881 size_t teePathLen = strlen(teePath);
2882 DIR *dir = opendir(teePath);
2883 teePath[teePathLen++] = '/';
2884 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002885#define TEE_MAX_SORT 20 // number of entries to sort
2886#define TEE_MAX_KEEP 10 // number of entries to keep
2887 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002888 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08002889 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002890 struct dirent de;
2891 struct dirent *result = NULL;
2892 int rc = readdir_r(dir, &de, &result);
2893 if (rc != 0) {
2894 ALOGW("readdir_r failed %d", rc);
2895 break;
2896 }
2897 if (result == NULL) {
2898 break;
2899 }
2900 if (result != &de) {
2901 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2902 break;
2903 }
2904 // ignore non .wav file entries
2905 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002906 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08002907 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2908 continue;
2909 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08002910 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002911 }
2912 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002913 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002914 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002915 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
2916 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002917 (void) unlink(teePath);
2918 }
2919 }
2920 } else {
2921 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002922 dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002923 }
2924 }
Eric Laurent81784c32012-11-19 14:55:58 -08002925 char teeTime[16];
2926 struct timeval tv;
2927 gettimeofday(&tv, NULL);
2928 struct tm tm;
2929 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002930 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2931 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2932 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2933 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002934 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07002935 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08002936 char wavHeader[44];
2937 memcpy(wavHeader,
2938 "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",
2939 sizeof(wavHeader));
2940 NBAIO_Format format = teeSource->format();
2941 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002942 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07002943 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002944 wavHeader[22] = channelCount; // number of channels
2945 wavHeader[24] = sampleRate; // sample rate
2946 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07002947 wavHeader[32] = frameSize; // block alignment
2948 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08002949 write(teeFd, wavHeader, sizeof(wavHeader));
2950 size_t total = 0;
2951 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07002952#define TEE_SINK_READ 1024 // frames per I/O operation
2953 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002954 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08002955 size_t count = TEE_SINK_READ;
Glenn Kastend79072e2016-01-06 08:41:20 -08002956 ssize_t actual = teeSource->read(buffer, count);
Eric Laurent81784c32012-11-19 14:55:58 -08002957 bool wasFirstRead = firstRead;
2958 firstRead = false;
2959 if (actual <= 0) {
2960 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2961 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002962 }
Eric Laurent81784c32012-11-19 14:55:58 -08002963 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002964 }
Eric Laurent81784c32012-11-19 14:55:58 -08002965 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07002966 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002967 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002968 }
Glenn Kasten329f6512014-08-28 16:23:16 -07002969 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08002970 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002971 uint32_t temp = 44 + total * frameSize - 8;
2972 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002973 write(teeFd, &temp, sizeof(temp));
2974 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002975 temp = total * frameSize;
2976 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002977 write(teeFd, &temp, sizeof(temp));
2978 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002979 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002980 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002981 }
Eric Laurent59255e42011-07-27 19:49:51 -07002982 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002983 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002984 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002985 }
Eric Laurent59255e42011-07-27 19:49:51 -07002986 }
2987 }
2988}
Glenn Kasten46909e72013-02-26 09:20:22 -08002989#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002990
Mathias Agopian65ab4712010-07-14 17:59:35 -07002991// ----------------------------------------------------------------------------
2992
2993status_t AudioFlinger::onTransact(
2994 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2995{
2996 return BnAudioFlinger::onTransact(code, data, reply, flags);
2997}
2998
Glenn Kasten63238ef2015-03-02 15:50:29 -08002999} // namespace android