blob: f4bd1c471a484e4758c76d929fde8dc39e628e99 [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 }
Andy Hung6770c6f2015-04-07 13:43:36 -07001164 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
1165 return 0;
1166 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001167
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001168 AutoMutex lock(mHardwareLock);
1169 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001170 audio_config_t config, proposed;
1171 memset(&proposed, 0, sizeof(proposed));
1172 proposed.sample_rate = sampleRate;
1173 proposed.channel_mask = channelMask;
1174 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001175
John Grossmanee578c02012-07-23 17:05:46 -07001176 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001177 size_t frames;
1178 for (;;) {
1179 // Note: config is currently a const parameter for get_input_buffer_size()
1180 // but we use a copy from proposed in case config changes from the call.
1181 config = proposed;
1182 frames = dev->get_input_buffer_size(dev, &config);
1183 if (frames != 0) {
1184 break; // hal success, config is the result
1185 }
1186 // change one parameter of the configuration each iteration to a more "common" value
1187 // to see if the device will support it.
1188 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1189 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1190 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1191 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1192 } else {
1193 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1194 "format %#x, channelMask 0x%X",
1195 sampleRate, format, channelMask);
1196 break; // retries failed, break out of loop with frames == 0.
1197 }
1198 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001199 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001200 if (frames > 0 && config.sample_rate != sampleRate) {
1201 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1202 }
1203 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001204}
1205
Glenn Kasten5f972c02014-01-13 09:59:31 -08001206uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001207{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001208 Mutex::Autolock _l(mLock);
1209
1210 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1211 if (recordThread != NULL) {
1212 return recordThread->getInputFramesLost();
1213 }
1214 return 0;
1215}
1216
1217status_t AudioFlinger::setVoiceVolume(float value)
1218{
Eric Laurenta1884f92011-08-23 08:25:03 -07001219 status_t ret = initCheck();
1220 if (ret != NO_ERROR) {
1221 return ret;
1222 }
1223
Mathias Agopian65ab4712010-07-14 17:59:35 -07001224 // check calling permissions
1225 if (!settingsAllowed()) {
1226 return PERMISSION_DENIED;
1227 }
1228
1229 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001230 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001231 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001232 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001233 mHardwareStatus = AUDIO_HW_IDLE;
1234
1235 return ret;
1236}
1237
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001238status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001239 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001240{
1241 status_t status;
1242
1243 Mutex::Autolock _l(mLock);
1244
1245 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1246 if (playbackThread != NULL) {
1247 return playbackThread->getRenderPosition(halFrames, dspFrames);
1248 }
1249
1250 return BAD_VALUE;
1251}
1252
1253void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1254{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001255 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001256 if (client == 0) {
1257 return;
1258 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001259 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001260 {
1261 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001262 if (mNotificationClients.indexOfKey(pid) < 0) {
1263 sp<NotificationClient> notificationClient = new NotificationClient(this,
1264 client,
1265 pid);
1266 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001267
Eric Laurent021cf962014-05-13 10:18:14 -07001268 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001269
Marco Nelissen06b46062014-11-14 07:58:25 -08001270 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001271 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001272 }
1273 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001274
Eric Laurent021cf962014-05-13 10:18:14 -07001275 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001276 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001277 // the config change is always sent from playback or record threads to avoid deadlock
1278 // with AudioSystem::gLock
1279 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1280 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1281 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001282
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001283 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1284 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001285 }
1286}
1287
1288void AudioFlinger::removeNotificationClient(pid_t pid)
1289{
1290 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001291 {
1292 Mutex::Autolock _cl(mClientLock);
1293 mNotificationClients.removeItem(pid);
1294 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001295
Steve Block3856b092011-10-20 11:56:00 +01001296 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001297 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001298 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001299 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001300 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001301 ALOGV(" pid %d @ %d", ref->mPid, i);
1302 if (ref->mPid == pid) {
1303 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001304 mAudioSessionRefs.removeAt(i);
1305 delete ref;
1306 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001307 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001308 } else {
1309 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001310 }
1311 }
1312 if (removed) {
1313 purgeStaleEffects_l();
1314 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001315}
1316
Eric Laurent73e26b62015-04-27 16:55:58 -07001317void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001318 const sp<AudioIoDescriptor>& ioDesc,
1319 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001320{
Eric Laurent021cf962014-05-13 10:18:14 -07001321 Mutex::Autolock _l(mClientLock);
1322 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001323 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001324 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1325 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1326 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001327 }
1328}
1329
Eric Laurent021cf962014-05-13 10:18:14 -07001330// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001331void AudioFlinger::removeClient_l(pid_t pid)
1332{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001333 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001334 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001335 mClients.removeItem(pid);
1336}
1337
Eric Laurent717e1282012-06-29 16:36:52 -07001338// getEffectThread_l() must be called with AudioFlinger::mLock held
1339sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1340{
1341 sp<PlaybackThread> thread;
1342
1343 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1344 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1345 ALOG_ASSERT(thread == 0);
1346 thread = mPlaybackThreads.valueAt(i);
1347 }
1348 }
1349
1350 return thread;
1351}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001352
Mathias Agopian65ab4712010-07-14 17:59:35 -07001353
Mathias Agopian65ab4712010-07-14 17:59:35 -07001354
1355// ----------------------------------------------------------------------------
1356
1357AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1358 : RefBase(),
1359 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001360 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001361{
Eric Laurentda73b6c2015-08-20 16:18:53 -07001362 size_t heapSize = kClientSharedHeapSizeBytes;
1363 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1364 // invalidated tracks
1365 if (!audioFlinger->isLowRamDevice()) {
1366 heapSize *= kClientSharedHeapSizeMultiplier;
1367 }
1368 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001369}
1370
Eric Laurent021cf962014-05-13 10:18:14 -07001371// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001372AudioFlinger::Client::~Client()
1373{
1374 mAudioFlinger->removeClient_l(mPid);
1375}
1376
Glenn Kasten435dbe62012-01-30 10:15:48 -08001377sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001378{
1379 return mMemoryDealer;
1380}
1381
1382// ----------------------------------------------------------------------------
1383
1384AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1385 const sp<IAudioFlingerClient>& client,
1386 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001387 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001388{
1389}
1390
1391AudioFlinger::NotificationClient::~NotificationClient()
1392{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001393}
1394
Glenn Kasten0f11b512014-01-31 16:18:54 -08001395void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001396{
1397 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001398 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001399}
1400
Mathias Agopian65ab4712010-07-14 17:59:35 -07001401
1402// ----------------------------------------------------------------------------
1403
Jeff Brown893a5642013-08-16 20:19:26 -07001404static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1405 return audio_is_remote_submix_device(inDevice);
1406}
1407
Mathias Agopian65ab4712010-07-14 17:59:35 -07001408sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001409 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001410 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001411 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001412 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001413 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001414 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001415 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001416 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001417 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001418 int *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001419 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001420 sp<IMemory>& cblk,
1421 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001422 status_t *status)
1423{
1424 sp<RecordThread::RecordTrack> recordTrack;
1425 sp<RecordHandle> recordHandle;
1426 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001427 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001428 int lSessionId;
1429
Glenn Kastend776ac62014-05-07 09:16:09 -07001430 cblk.clear();
1431 buffers.clear();
1432
Marco Nelissendcb346b2015-09-09 10:47:29 -07001433 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1434 if (!isTrustedCallingUid(callingUid)) {
Andy Hung879db192016-01-05 16:00:34 -08001435 ALOGW_IF((uid_t)clientUid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -07001436 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1437 clientUid = callingUid;
1438 }
1439
Mathias Agopian65ab4712010-07-14 17:59:35 -07001440 // check calling permissions
Marco Nelissendcb346b2015-09-09 10:47:29 -07001441 if (!recordingAllowed(opPackageName, tid, clientUid)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001442 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001443 lStatus = PERMISSION_DENIED;
1444 goto Exit;
1445 }
1446
Glenn Kasten53b5d092014-02-05 10:00:23 -08001447 // further sample rate checks are performed by createRecordTrack_l()
1448 if (sampleRate == 0) {
1449 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1450 lStatus = BAD_VALUE;
1451 goto Exit;
1452 }
1453
Andy Hung6770c6f2015-04-07 13:43:36 -07001454 // we don't yet support anything other than linear PCM
1455 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001456 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001457 lStatus = BAD_VALUE;
1458 goto Exit;
1459 }
1460
Glenn Kasten53b5d092014-02-05 10:00:23 -08001461 // further channel mask checks are performed by createRecordTrack_l()
1462 if (!audio_is_input_channel(channelMask)) {
1463 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1464 lStatus = BAD_VALUE;
1465 goto Exit;
1466 }
1467
Glenn Kasten05997e22014-03-13 15:08:33 -07001468 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001469 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001470 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001471 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001472 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001473 lStatus = BAD_VALUE;
1474 goto Exit;
1475 }
1476
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001477 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001478 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001479
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001480 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001481 lSessionId = *sessionId;
1482 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001483 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001484 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001485 if (sessionId != NULL) {
1486 *sessionId = lSessionId;
1487 }
1488 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001489 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001490
Glenn Kasten1879fff2012-07-11 15:36:59 -07001491 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001492 frameCount, lSessionId, notificationFrames,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001493 clientUid, flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001494 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001495
1496 if (lStatus == NO_ERROR) {
1497 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1498 // session and move it to this thread.
1499 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)lSessionId);
1500 if (chain != 0) {
1501 Mutex::Autolock _l(thread->mLock);
1502 thread->addEffectChain_l(chain);
1503 }
1504 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001505 }
Glenn Kastene198c362013-08-13 09:13:36 -07001506
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001507 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001508 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001509 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001510 // Don't hold mClientLock when releasing the reference on the track as the
1511 // destructor will acquire it.
1512 {
1513 Mutex::Autolock _cl(mClientLock);
1514 client.clear();
1515 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001516 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001517 goto Exit;
1518 }
1519
Glenn Kastend776ac62014-05-07 09:16:09 -07001520 cblk = recordTrack->getCblk();
1521 buffers = recordTrack->getBuffers();
1522
Glenn Kasten2fc14732013-08-05 14:58:14 -07001523 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001524 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001525
1526Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001527 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001528 return recordHandle;
1529}
1530
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001531
1532
Mathias Agopian65ab4712010-07-14 17:59:35 -07001533// ----------------------------------------------------------------------------
1534
Eric Laurenta4c5a552012-03-29 10:12:40 -07001535audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1536{
Eric Laurent44622db2014-08-01 19:00:33 -07001537 if (name == NULL) {
1538 return 0;
1539 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001540 if (!settingsAllowed()) {
1541 return 0;
1542 }
1543 Mutex::Autolock _l(mLock);
1544 return loadHwModule_l(name);
1545}
1546
1547// loadHwModule_l() must be called with AudioFlinger::mLock held
1548audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1549{
1550 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1551 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1552 ALOGW("loadHwModule() module %s already loaded", name);
1553 return mAudioHwDevs.keyAt(i);
1554 }
1555 }
1556
Eric Laurenta4c5a552012-03-29 10:12:40 -07001557 audio_hw_device_t *dev;
1558
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001559 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001560 if (rc) {
1561 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1562 return 0;
1563 }
1564
1565 mHardwareStatus = AUDIO_HW_INIT;
1566 rc = dev->init_check(dev);
1567 mHardwareStatus = AUDIO_HW_IDLE;
1568 if (rc) {
1569 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1570 return 0;
1571 }
1572
John Grossmanee578c02012-07-23 17:05:46 -07001573 // Check and cache this HAL's level of support for master mute and master
1574 // volume. If this is the first HAL opened, and it supports the get
1575 // methods, use the initial values provided by the HAL as the current
1576 // master mute and volume settings.
1577
1578 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1579 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001580 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001581
1582 if (0 == mAudioHwDevs.size()) {
1583 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1584 if (NULL != dev->get_master_volume) {
1585 float mv;
1586 if (OK == dev->get_master_volume(dev, &mv)) {
1587 mMasterVolume = mv;
1588 }
1589 }
1590
1591 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1592 if (NULL != dev->get_master_mute) {
1593 bool mm;
1594 if (OK == dev->get_master_mute(dev, &mm)) {
1595 mMasterMute = mm;
1596 }
1597 }
1598 }
1599
Eric Laurenta4c5a552012-03-29 10:12:40 -07001600 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001601 if ((NULL != dev->set_master_volume) &&
1602 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1603 flags = static_cast<AudioHwDevice::Flags>(flags |
1604 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1605 }
1606
1607 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1608 if ((NULL != dev->set_master_mute) &&
1609 (OK == dev->set_master_mute(dev, mMasterMute))) {
1610 flags = static_cast<AudioHwDevice::Flags>(flags |
1611 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1612 }
1613
Eric Laurenta4c5a552012-03-29 10:12:40 -07001614 mHardwareStatus = AUDIO_HW_IDLE;
1615 }
1616
1617 audio_module_handle_t handle = nextUniqueId();
Eric Laurent83b88082014-06-20 18:31:16 -07001618 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001619
1620 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001621 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001622
1623 return handle;
1624
1625}
1626
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001627// ----------------------------------------------------------------------------
1628
Glenn Kasten3b16c762012-11-14 08:44:39 -08001629uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001630{
1631 Mutex::Autolock _l(mLock);
1632 PlaybackThread *thread = primaryPlaybackThread_l();
1633 return thread != NULL ? thread->sampleRate() : 0;
1634}
1635
Glenn Kastene33054e2012-11-14 12:54:39 -08001636size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001637{
1638 Mutex::Autolock _l(mLock);
1639 PlaybackThread *thread = primaryPlaybackThread_l();
1640 return thread != NULL ? thread->frameCountHAL() : 0;
1641}
1642
1643// ----------------------------------------------------------------------------
1644
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001645status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1646{
1647 uid_t uid = IPCThreadState::self()->getCallingUid();
1648 if (uid != AID_SYSTEM) {
1649 return PERMISSION_DENIED;
1650 }
1651 Mutex::Autolock _l(mLock);
1652 if (mIsDeviceTypeKnown) {
1653 return INVALID_OPERATION;
1654 }
1655 mIsLowRamDevice = isLowRamDevice;
1656 mIsDeviceTypeKnown = true;
1657 return NO_ERROR;
1658}
1659
Eric Laurent93c3d412014-08-01 14:48:35 -07001660audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1661{
1662 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001663
1664 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1665 if (index >= 0) {
1666 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1667 mHwAvSyncIds.valueAt(index), sessionId);
1668 return mHwAvSyncIds.valueAt(index);
1669 }
1670
1671 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1672 if (dev == NULL) {
1673 return AUDIO_HW_SYNC_INVALID;
1674 }
1675 char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1676 AudioParameter param = AudioParameter(String8(reply));
1677 free(reply);
1678
1679 int value;
1680 if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1681 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1682 return AUDIO_HW_SYNC_INVALID;
1683 }
1684
1685 // allow only one session for a given HW A/V sync ID.
1686 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1687 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1688 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1689 value, mHwAvSyncIds.keyAt(i));
1690 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001691 break;
1692 }
1693 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001694
1695 mHwAvSyncIds.add(sessionId, value);
1696
1697 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1698 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1699 uint32_t sessions = thread->hasAudioSession(sessionId);
1700 if (sessions & PlaybackThread::TRACK_SESSION) {
1701 AudioParameter param = AudioParameter();
1702 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1703 thread->setParameters(param.toString());
1704 break;
1705 }
1706 }
1707
1708 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1709 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001710}
1711
Eric Laurent72e3f392015-05-20 14:43:50 -07001712status_t AudioFlinger::systemReady()
1713{
1714 Mutex::Autolock _l(mLock);
1715 ALOGI("%s", __FUNCTION__);
1716 if (mSystemReady) {
1717 ALOGW("%s called twice", __FUNCTION__);
1718 return NO_ERROR;
1719 }
1720 mSystemReady = true;
1721 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1722 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1723 thread->systemReady();
1724 }
1725 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1726 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1727 thread->systemReady();
1728 }
1729 return NO_ERROR;
1730}
1731
Eric Laurentfa90e842014-10-17 18:12:31 -07001732// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1733void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1734{
1735 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1736 if (index >= 0) {
1737 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1738 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1739 AudioParameter param = AudioParameter();
1740 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1741 thread->setParameters(param.toString());
1742 }
1743}
1744
1745
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001746// ----------------------------------------------------------------------------
1747
Eric Laurent83b88082014-06-20 18:31:16 -07001748
1749sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001750 audio_io_handle_t *output,
1751 audio_config_t *config,
1752 audio_devices_t devices,
1753 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001754 audio_output_flags_t flags)
1755{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001756 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001757 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001758 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001759 }
1760
1761 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentcf2c0212014-07-25 16:20:43 -07001762 if (*output == AUDIO_IO_HANDLE_NONE) {
1763 *output = nextUniqueId();
1764 }
Eric Laurent83b88082014-06-20 18:31:16 -07001765
1766 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1767
Eric Laurent83b88082014-06-20 18:31:16 -07001768 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001769 // This if statement allows overriding the audio policy settings
1770 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1771 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1772 // Check only for Normal Mixing mode
1773 if (kEnableExtendedPrecision) {
1774 // Specify format (uncomment one below to choose)
1775 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1776 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1777 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1778 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001779 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001780 }
1781 if (kEnableExtendedChannels) {
1782 // Specify channel mask (uncomment one below to choose)
1783 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1784 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1785 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1786 }
Eric Laurent83b88082014-06-20 18:31:16 -07001787 }
1788
Phil Burk062e67a2015-02-11 13:40:50 -08001789 AudioStreamOut *outputStream = NULL;
1790 status_t status = outHwDev->openOutputStream(
1791 &outputStream,
1792 *output,
1793 devices,
1794 flags,
1795 config,
1796 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001797
1798 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001799
Phil Burk062e67a2015-02-11 13:40:50 -08001800 if (status == NO_ERROR) {
Eric Laurent83b88082014-06-20 18:31:16 -07001801
1802 PlaybackThread *thread;
1803 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001804 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001805 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001806 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1807 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001808 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001809 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001810 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001811 } else {
Eric Laurent72e3f392015-05-20 14:43:50 -07001812 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001813 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001814 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001815 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001816 return thread;
1817 }
1818
1819 return 0;
1820}
1821
Eric Laurentcf2c0212014-07-25 16:20:43 -07001822status_t AudioFlinger::openOutput(audio_module_handle_t module,
1823 audio_io_handle_t *output,
1824 audio_config_t *config,
1825 audio_devices_t *devices,
1826 const String8& address,
1827 uint32_t *latencyMs,
1828 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001829{
Phil Burk062e67a2015-02-11 13:40:50 -08001830 ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001831 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001832 (devices != NULL) ? *devices : 0,
1833 config->sample_rate,
1834 config->format,
1835 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001836 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001837
Eric Laurentcf2c0212014-07-25 16:20:43 -07001838 if (*devices == AUDIO_DEVICE_NONE) {
1839 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001840 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001841
Mathias Agopian65ab4712010-07-14 17:59:35 -07001842 Mutex::Autolock _l(mLock);
1843
Eric Laurentcf2c0212014-07-25 16:20:43 -07001844 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001845 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001846 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001847
1848 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001849 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001850
1851 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001852 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001853 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001854 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001855
1856 AutoMutex lock(mHardwareLock);
1857 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001858 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001859 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001860 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001861 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001862 }
1863
Eric Laurentcf2c0212014-07-25 16:20:43 -07001864 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001865}
1866
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001867audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1868 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001869{
1870 Mutex::Autolock _l(mLock);
1871 MixerThread *thread1 = checkMixerThread_l(output1);
1872 MixerThread *thread2 = checkMixerThread_l(output2);
1873
1874 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001875 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1876 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001877 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001878 }
1879
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001880 audio_io_handle_t id = nextUniqueId();
Eric Laurent72e3f392015-05-20 14:43:50 -07001881 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001882 thread->addOutputTrack(thread2);
1883 mPlaybackThreads.add(id, thread);
1884 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001885 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001886 return id;
1887}
1888
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001889status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001890{
Glenn Kastend96c5722012-04-25 13:44:49 -07001891 return closeOutput_nonvirtual(output);
1892}
1893
1894status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1895{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001896 // keep strong reference on the playback thread so that
1897 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001898 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001899 {
1900 Mutex::Autolock _l(mLock);
1901 thread = checkPlaybackThread_l(output);
1902 if (thread == NULL) {
1903 return BAD_VALUE;
1904 }
1905
Steve Block3856b092011-10-20 11:56:00 +01001906 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001907
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001908 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001909 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentf6870ae2015-05-08 10:50:03 -07001910 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001911 DuplicatingThread *dupThread =
1912 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001913 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001914 }
1915 }
1916 }
1917
1918
1919 mPlaybackThreads.removeItem(output);
1920 // save all effects to the default thread
1921 if (mPlaybackThreads.size()) {
1922 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1923 if (dstThread != NULL) {
1924 // audioflinger lock is held here so the acquisition order of thread locks does not
1925 // matter
1926 Mutex::Autolock _dl(dstThread->mLock);
1927 Mutex::Autolock _sl(thread->mLock);
1928 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1929 for (size_t i = 0; i < effectChains.size(); i ++) {
1930 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001931 }
1932 }
1933 }
Eric Laurent73e26b62015-04-27 16:55:58 -07001934 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
1935 ioDesc->mIoHandle = output;
1936 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001937 }
1938 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001939 // The thread entity (active unit of execution) is no longer running here,
1940 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001941
Eric Laurentf6870ae2015-05-08 10:50:03 -07001942 if (!thread->isDuplicating()) {
Eric Laurent83b88082014-06-20 18:31:16 -07001943 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001944 }
Eric Laurent83b88082014-06-20 18:31:16 -07001945
Mathias Agopian65ab4712010-07-14 17:59:35 -07001946 return NO_ERROR;
1947}
1948
Eric Laurent83b88082014-06-20 18:31:16 -07001949void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
1950{
1951 AudioStreamOut *out = thread->clearOutput();
1952 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1953 // from now on thread->mOutput is NULL
1954 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
1955 delete out;
1956}
1957
1958void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
1959{
1960 mPlaybackThreads.removeItem(thread->mId);
1961 thread->exit();
1962 closeOutputFinish(thread);
1963}
1964
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001965status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001966{
1967 Mutex::Autolock _l(mLock);
1968 PlaybackThread *thread = checkPlaybackThread_l(output);
1969
1970 if (thread == NULL) {
1971 return BAD_VALUE;
1972 }
1973
Steve Block3856b092011-10-20 11:56:00 +01001974 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001975 thread->suspend();
1976
1977 return NO_ERROR;
1978}
1979
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001980status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001981{
1982 Mutex::Autolock _l(mLock);
1983 PlaybackThread *thread = checkPlaybackThread_l(output);
1984
1985 if (thread == NULL) {
1986 return BAD_VALUE;
1987 }
1988
Steve Block3856b092011-10-20 11:56:00 +01001989 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001990
1991 thread->restore();
1992
1993 return NO_ERROR;
1994}
1995
Eric Laurentcf2c0212014-07-25 16:20:43 -07001996status_t AudioFlinger::openInput(audio_module_handle_t module,
1997 audio_io_handle_t *input,
1998 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08001999 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002000 const String8& address,
2001 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002002 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002003{
Eric Laurent83b88082014-06-20 18:31:16 -07002004 Mutex::Autolock _l(mLock);
2005
Glenn Kastene7d66712015-03-05 13:46:52 -08002006 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002007 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002008 }
2009
Glenn Kastene7d66712015-03-05 13:46:52 -08002010 sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002011
2012 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002013 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002014 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002015 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002016 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002017 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002018}
Dima Zavin799a70e2011-04-18 16:57:27 -07002019
Eric Laurent83b88082014-06-20 18:31:16 -07002020sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002021 audio_io_handle_t *input,
2022 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002023 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002024 const String8& address,
2025 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002026 audio_input_flags_t flags)
2027{
Glenn Kastene7d66712015-03-05 13:46:52 -08002028 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002029 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002030 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002031 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002032 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002033
Eric Laurentcf2c0212014-07-25 16:20:43 -07002034 if (*input == AUDIO_IO_HANDLE_NONE) {
2035 *input = nextUniqueId();
2036 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002037
Eric Laurentcf2c0212014-07-25 16:20:43 -07002038 audio_config_t halconfig = *config;
2039 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07002040 audio_stream_in_t *inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002041 status_t status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002042 &inStream, flags, address.string(), source);
2043 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002044 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Dima Zavin799a70e2011-04-18 16:57:27 -07002045 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002046 halconfig.sample_rate,
2047 halconfig.format,
2048 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002049 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002050 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002051
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002052 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002053 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002054 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002055 audio_is_linear_pcm(config->format) &&
2056 audio_is_linear_pcm(halconfig.format) &&
2057 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
Eric Laurentcf2c0212014-07-25 16:20:43 -07002058 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
2059 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002060 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002061 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002062 inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002063 status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002064 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07002065 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002066 }
2067
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002068 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002069
Glenn Kasten46909e72013-02-26 09:20:22 -08002070#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002071 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2072 // or (re-)create if current Pipe is idle and does not match the new format
2073 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002074 enum {
2075 TEE_SINK_NO, // don't copy input
2076 TEE_SINK_NEW, // copy input using a new pipe
2077 TEE_SINK_OLD, // copy input using an existing pipe
2078 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002079 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2080 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002081 if (!mTeeSinkInputEnabled) {
2082 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002083 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002084 kind = TEE_SINK_NO;
2085 } else if (mRecordTeeSink == 0) {
2086 kind = TEE_SINK_NEW;
2087 } else if (mRecordTeeSink->getStrongCount() != 1) {
2088 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002089 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002090 kind = TEE_SINK_OLD;
2091 } else {
2092 kind = TEE_SINK_NEW;
2093 }
2094 switch (kind) {
2095 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002096 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002097 size_t numCounterOffers = 0;
2098 const NBAIO_Format offers[1] = {format};
2099 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2100 ALOG_ASSERT(index == 0);
2101 PipeReader *pipeReader = new PipeReader(*pipe);
2102 numCounterOffers = 0;
2103 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2104 ALOG_ASSERT(index == 0);
2105 mRecordTeeSink = pipe;
2106 mRecordTeeSource = pipeReader;
2107 teeSink = pipe;
2108 }
2109 break;
2110 case TEE_SINK_OLD:
2111 teeSink = mRecordTeeSink;
2112 break;
2113 case TEE_SINK_NO:
2114 default:
2115 break;
2116 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002117#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002118
Eric Laurentcf2c0212014-07-25 16:20:43 -07002119 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002120
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002121 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002122 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002123 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002124 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002125 inputStream,
2126 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002127 primaryOutputDevice_l(),
Eric Laurent72e3f392015-05-20 14:43:50 -07002128 devices,
2129 mSystemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08002130#ifdef TEE_SINK
2131 , teeSink
2132#endif
2133 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002134 mRecordThreads.add(*input, thread);
2135 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002136 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002137 }
2138
Eric Laurentcf2c0212014-07-25 16:20:43 -07002139 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002140 return 0;
2141}
2142
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002143status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002144{
Glenn Kastend96c5722012-04-25 13:44:49 -07002145 return closeInput_nonvirtual(input);
2146}
2147
2148status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2149{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002150 // keep strong reference on the record thread so that
2151 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002152 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002153 {
2154 Mutex::Autolock _l(mLock);
2155 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002156 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002157 return BAD_VALUE;
2158 }
2159
Steve Block3856b092011-10-20 11:56:00 +01002160 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002161
2162 // If we still have effect chains, it means that a client still holds a handle
2163 // on at least one effect. We must either move the chain to an existing thread with the
2164 // same session ID or put it aside in case a new record thread is opened for a
2165 // new capture on the same session
2166 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002167 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002168 Mutex::Autolock _sl(thread->mLock);
2169 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002170 // Note: maximum one chain per record thread
2171 if (effectChains.size() != 0) {
2172 chain = effectChains[0];
2173 }
2174 }
2175 if (chain != 0) {
2176 // first check if a record thread is already opened with a client on the same session.
2177 // This should only happen in case of overlap between one thread tear down and the
2178 // creation of its replacement
2179 size_t i;
2180 for (i = 0; i < mRecordThreads.size(); i++) {
2181 sp<RecordThread> t = mRecordThreads.valueAt(i);
2182 if (t == thread) {
2183 continue;
2184 }
2185 if (t->hasAudioSession(chain->sessionId()) != 0) {
2186 Mutex::Autolock _l(t->mLock);
2187 ALOGV("closeInput() found thread %d for effect session %d",
2188 t->id(), chain->sessionId());
2189 t->addEffectChain_l(chain);
2190 break;
2191 }
2192 }
2193 // put the chain aside if we could not find a record thread with the same session id.
2194 if (i == mRecordThreads.size()) {
2195 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002196 }
2197 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002198 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2199 ioDesc->mIoHandle = input;
2200 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002201 mRecordThreads.removeItem(input);
2202 }
Eric Laurent83b88082014-06-20 18:31:16 -07002203 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2204 // we have a different lock for notification client
2205 closeInputFinish(thread);
2206 return NO_ERROR;
2207}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002208
Eric Laurent83b88082014-06-20 18:31:16 -07002209void AudioFlinger::closeInputFinish(sp<RecordThread> thread)
2210{
2211 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002212 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002213 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002214 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002215 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002216 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002217}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002218
Eric Laurent83b88082014-06-20 18:31:16 -07002219void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
2220{
2221 mRecordThreads.removeItem(thread->mId);
2222 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002223}
2224
Glenn Kastend2304db2014-02-03 07:40:31 -08002225status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002226{
2227 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002228 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002229
2230 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2231 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002232 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002233 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002234
2235 return NO_ERROR;
2236}
2237
2238
Eric Laurentde3f8392014-07-27 18:38:22 -07002239audio_unique_id_t AudioFlinger::newAudioUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002240{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002241 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002242}
2243
Marco Nelissend457c972014-02-11 08:47:07 -08002244void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002245{
2246 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002247 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002248 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2249 if (pid != -1 && (caller == getpid_cached)) {
2250 caller = pid;
2251 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002252
Eric Laurent021cf962014-05-13 10:18:14 -07002253 {
2254 Mutex::Autolock _cl(mClientLock);
2255 // Ignore requests received from processes not known as notification client. The request
2256 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2257 // called from a different pid leaving a stale session reference. Also we don't know how
2258 // to clear this reference if the client process dies.
2259 if (mNotificationClients.indexOfKey(caller) < 0) {
2260 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2261 return;
2262 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002263 }
2264
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002265 size_t num = mAudioSessionRefs.size();
2266 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002267 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002268 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2269 ref->mCnt++;
2270 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002271 return;
2272 }
2273 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002274 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2275 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002276}
2277
Marco Nelissend457c972014-02-11 08:47:07 -08002278void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002279{
2280 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002281 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002282 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2283 if (pid != -1 && (caller == getpid_cached)) {
2284 caller = pid;
2285 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002286 size_t num = mAudioSessionRefs.size();
2287 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002288 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002289 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2290 ref->mCnt--;
2291 ALOGV(" decremented refcount to %d", ref->mCnt);
2292 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002293 mAudioSessionRefs.removeAt(i);
2294 delete ref;
2295 purgeStaleEffects_l();
2296 }
2297 return;
2298 }
2299 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002300 // If the caller is mediaserver it is likely that the session being released was acquired
2301 // on behalf of a process not in notification clients and we ignore the warning.
2302 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002303}
2304
2305void AudioFlinger::purgeStaleEffects_l() {
2306
Steve Block3856b092011-10-20 11:56:00 +01002307 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002308
2309 Vector< sp<EffectChain> > chains;
2310
2311 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2312 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2313 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2314 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002315 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2316 chains.push(ec);
2317 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002318 }
2319 }
2320 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2321 sp<RecordThread> t = mRecordThreads.valueAt(i);
2322 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2323 sp<EffectChain> ec = t->mEffectChains[j];
2324 chains.push(ec);
2325 }
2326 }
2327
2328 for (size_t i = 0; i < chains.size(); i++) {
2329 sp<EffectChain> ec = chains[i];
2330 int sessionid = ec->sessionId();
2331 sp<ThreadBase> t = ec->mThread.promote();
2332 if (t == 0) {
2333 continue;
2334 }
2335 size_t numsessionrefs = mAudioSessionRefs.size();
2336 bool found = false;
2337 for (size_t k = 0; k < numsessionrefs; k++) {
2338 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002339 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002340 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002341 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002342 found = true;
2343 break;
2344 }
2345 }
2346 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002347 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002348 // remove all effects from the chain
2349 while (ec->mEffects.size()) {
2350 sp<EffectModule> effect = ec->mEffects[0];
2351 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002352 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002353 if (effect->purgeHandles()) {
2354 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002355 }
2356 AudioSystem::unregisterEffect(effect->id());
2357 }
2358 }
2359 }
2360 return;
2361}
2362
Mathias Agopian65ab4712010-07-14 17:59:35 -07002363// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002364AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002365{
Glenn Kastena1117922012-01-26 10:53:32 -08002366 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002367}
2368
2369// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002370AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002371{
2372 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002373 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002374}
2375
2376// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002377AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002378{
Glenn Kastena1117922012-01-26 10:53:32 -08002379 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002380}
2381
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002382uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002383{
Glenn Kastenbcefec32014-01-17 12:09:05 -08002384 return (uint32_t) android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002385}
2386
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002387AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002388{
2389 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2390 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002391 if(thread->isDuplicating()) {
2392 continue;
2393 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002394 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002395 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002396 return thread;
2397 }
2398 }
2399 return NULL;
2400}
2401
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002402audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002403{
2404 PlaybackThread *thread = primaryPlaybackThread_l();
2405
2406 if (thread == NULL) {
2407 return 0;
2408 }
2409
Eric Laurentf1c04f92012-08-28 14:26:53 -07002410 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002411}
2412
Eric Laurenta011e352012-03-29 15:51:43 -07002413sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2414 int triggerSession,
2415 int listenerSession,
2416 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002417 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002418{
2419 Mutex::Autolock _l(mLock);
2420
2421 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2422 status_t playStatus = NAME_NOT_FOUND;
2423 status_t recStatus = NAME_NOT_FOUND;
2424 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2425 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2426 if (playStatus == NO_ERROR) {
2427 return event;
2428 }
2429 }
2430 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2431 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2432 if (recStatus == NO_ERROR) {
2433 return event;
2434 }
2435 }
2436 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2437 mPendingSyncEvents.add(event);
2438 } else {
2439 ALOGV("createSyncEvent() invalid event %d", event->type());
2440 event.clear();
2441 }
2442 return event;
2443}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002444
Mathias Agopian65ab4712010-07-14 17:59:35 -07002445// ----------------------------------------------------------------------------
2446// Effect management
2447// ----------------------------------------------------------------------------
2448
2449
Glenn Kastenf587ba52012-01-26 16:25:10 -08002450status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002451{
2452 Mutex::Autolock _l(mLock);
2453 return EffectQueryNumberEffects(numEffects);
2454}
2455
Glenn Kastenf587ba52012-01-26 16:25:10 -08002456status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002457{
2458 Mutex::Autolock _l(mLock);
2459 return EffectQueryEffect(index, descriptor);
2460}
2461
Glenn Kasten5e92a782012-01-30 07:40:52 -08002462status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002463 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002464{
2465 Mutex::Autolock _l(mLock);
2466 return EffectGetDescriptor(pUuid, descriptor);
2467}
2468
2469
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002470sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002471 effect_descriptor_t *pDesc,
2472 const sp<IEffectClient>& effectClient,
2473 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002474 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002475 int sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002476 const String16& opPackageName,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002477 status_t *status,
2478 int *id,
2479 int *enabled)
2480{
2481 status_t lStatus = NO_ERROR;
2482 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002483 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002484
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002485 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002486 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002487 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002488
2489 if (pDesc == NULL) {
2490 lStatus = BAD_VALUE;
2491 goto Exit;
2492 }
2493
Eric Laurent84e9a102010-09-23 16:10:16 -07002494 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002495 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002496 lStatus = PERMISSION_DENIED;
2497 goto Exit;
2498 }
2499
Dima Zavinfce7a472011-04-19 22:30:36 -07002500 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002501 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002502 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002503 lStatus = PERMISSION_DENIED;
2504 goto Exit;
2505 }
2506
Mathias Agopian65ab4712010-07-14 17:59:35 -07002507 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002508 if (!EffectIsNullUuid(&pDesc->uuid)) {
2509 // if uuid is specified, request effect descriptor
2510 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2511 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002512 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002513 goto Exit;
2514 }
2515 } else {
2516 // if uuid is not specified, look for an available implementation
2517 // of the required type in effect factory
2518 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002519 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002520 lStatus = BAD_VALUE;
2521 goto Exit;
2522 }
2523 uint32_t numEffects = 0;
2524 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002525 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002526 bool found = false;
2527
2528 lStatus = EffectQueryNumberEffects(&numEffects);
2529 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002530 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002531 goto Exit;
2532 }
2533 for (uint32_t i = 0; i < numEffects; i++) {
2534 lStatus = EffectQueryEffect(i, &desc);
2535 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002536 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002537 continue;
2538 }
2539 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2540 // If matching type found save effect descriptor. If the session is
2541 // 0 and the effect is not auxiliary, continue enumeration in case
2542 // an auxiliary version of this effect type is available
2543 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002544 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002545 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002546 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2547 break;
2548 }
2549 }
2550 }
2551 if (!found) {
2552 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002553 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002554 goto Exit;
2555 }
2556 // For same effect type, chose auxiliary version over insert version if
2557 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002558 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002559 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002560 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002561 }
2562 }
2563
2564 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002565 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002566 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2567 lStatus = INVALID_OPERATION;
2568 goto Exit;
2569 }
2570
Eric Laurent59255e42011-07-27 19:49:51 -07002571 // check recording permission for visualizer
2572 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Marco Nelissendcb346b2015-09-09 10:47:29 -07002573 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07002574 lStatus = PERMISSION_DENIED;
2575 goto Exit;
2576 }
2577
Mathias Agopian65ab4712010-07-14 17:59:35 -07002578 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002579 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002580 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002581 // if the output returned by getOutputForEffect() is removed before we lock the
2582 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2583 // and we will exit safely
2584 io = AudioSystem::getOutputForEffect(&desc);
2585 ALOGV("createEffect got output %d", io);
2586 }
2587
2588 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002589
2590 // If output is not specified try to find a matching audio session ID in one of the
2591 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002592 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2593 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002594 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002595 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002596 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2597 // output must be specified by AudioPolicyManager when using session
2598 // AUDIO_SESSION_OUTPUT_STAGE
2599 lStatus = BAD_VALUE;
2600 goto Exit;
2601 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002602 // look for the thread where the specified audio session is present
2603 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2604 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2605 io = mPlaybackThreads.keyAt(i);
2606 break;
2607 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002608 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002609 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002610 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2611 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2612 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002613 break;
2614 }
2615 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002616 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002617 // If no output thread contains the requested session ID, default to
2618 // first output. The effect chain will be moved to the correct output
2619 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002620 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002621 io = mPlaybackThreads.keyAt(0);
2622 }
Steve Block3856b092011-10-20 11:56:00 +01002623 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002624 }
2625 ThreadBase *thread = checkRecordThread_l(io);
2626 if (thread == NULL) {
2627 thread = checkPlaybackThread_l(io);
2628 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002629 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002630 lStatus = BAD_VALUE;
2631 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002632 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002633 } else {
2634 // Check if one effect chain was awaiting for an effect to be created on this
2635 // session and used it instead of creating a new one.
2636 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)sessionId);
2637 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002638 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002639 thread->addEffectChain_l(chain);
2640 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002641 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002642
Eric Laurent021cf962014-05-13 10:18:14 -07002643 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002644
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002645 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002646 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2647 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002648 if (handle != 0 && id != NULL) {
2649 *id = handle->id();
2650 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002651 if (handle == 0) {
2652 // remove local strong reference to Client with mClientLock held
2653 Mutex::Autolock _cl(mClientLock);
2654 client.clear();
2655 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002656 }
2657
2658Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002659 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002660 return handle;
2661}
2662
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002663status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2664 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002665{
Steve Block3856b092011-10-20 11:56:00 +01002666 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002667 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002668 Mutex::Autolock _l(mLock);
2669 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002670 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002671 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002672 }
Eric Laurentde070132010-07-13 04:45:46 -07002673 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2674 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002675 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002676 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002677 }
Eric Laurentde070132010-07-13 04:45:46 -07002678 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2679 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002680 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002681 return BAD_VALUE;
2682 }
2683
2684 Mutex::Autolock _dl(dstThread->mLock);
2685 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002686 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002687}
2688
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002689// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002690status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002691 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002692 AudioFlinger::PlaybackThread *dstThread,
2693 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002694{
Steve Block3856b092011-10-20 11:56:00 +01002695 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002696 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002697
Eric Laurent59255e42011-07-27 19:49:51 -07002698 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002699 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002700 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002701 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002702 return INVALID_OPERATION;
2703 }
2704
Andy Hung9a592762014-07-21 21:56:01 -07002705 // Check whether the destination thread has a channel count of FCC_2, which is
2706 // currently required for (most) effects. Prevent moving the effect chain here rather
2707 // than disabling the addEffect_l() call in dstThread below.
Eric Laurentf6870ae2015-05-08 10:50:03 -07002708 if ((dstThread->type() == ThreadBase::MIXER || dstThread->isDuplicating()) &&
Eric Laurentb10352f2014-11-03 16:25:39 -08002709 dstThread->mChannelCount != FCC_2) {
Andy Hung9a592762014-07-21 21:56:01 -07002710 ALOGW("moveEffectChain_l() effect chain failed because"
2711 " destination thread %p channel count(%u) != %u",
2712 dstThread, dstThread->mChannelCount, FCC_2);
2713 return INVALID_OPERATION;
2714 }
2715
Eric Laurent39e94f82010-07-28 01:32:47 -07002716 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002717 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002718 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002719 // removed.
2720 srcThread->removeEffectChain_l(chain);
2721
2722 // transfer all effects one by one so that new effect chain is created on new thread with
2723 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002724 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002725 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002726 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002727 Vector< sp<EffectModule> > removed;
2728 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002729 while (effect != 0) {
2730 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002731 removed.add(effect);
2732 status = dstThread->addEffect_l(effect);
2733 if (status != NO_ERROR) {
2734 break;
2735 }
Eric Laurentec35a142011-10-05 17:42:25 -07002736 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2737 if (effect->state() == EffectModule::ACTIVE ||
2738 effect->state() == EffectModule::STOPPING) {
2739 effect->start();
2740 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002741 // if the move request is not received from audio policy manager, the effect must be
2742 // re-registered with the new strategy and output
2743 if (dstChain == 0) {
2744 dstChain = effect->chain().promote();
2745 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002746 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002747 status = NO_INIT;
2748 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002749 }
2750 strategy = dstChain->strategy();
2751 }
2752 if (reRegister) {
2753 AudioSystem::unregisterEffect(effect->id());
2754 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002755 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002756 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002757 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002758 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002759 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002760 }
Eric Laurentde070132010-07-13 04:45:46 -07002761 effect = chain->getEffectFromId_l(0);
2762 }
2763
Eric Laurent5baf2af2013-09-12 17:37:00 -07002764 if (status != NO_ERROR) {
2765 for (size_t i = 0; i < removed.size(); i++) {
2766 srcThread->addEffect_l(removed[i]);
2767 if (dstChain != 0 && reRegister) {
2768 AudioSystem::unregisterEffect(removed[i]->id());
2769 AudioSystem::registerEffect(&removed[i]->desc(),
2770 srcThread->id(),
2771 strategy,
2772 sessionId,
2773 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002774 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002775 }
2776 }
2777 }
2778
2779 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002780}
2781
Eric Laurent5baf2af2013-09-12 17:37:00 -07002782bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002783{
2784 if (mGlobalEffectEnableTime != 0 &&
2785 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2786 return true;
2787 }
2788
2789 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2790 sp<EffectChain> ec =
2791 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002792 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002793 return true;
2794 }
2795 }
2796 return false;
2797}
2798
Eric Laurent5baf2af2013-09-12 17:37:00 -07002799void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002800{
2801 Mutex::Autolock _l(mLock);
2802
2803 mGlobalEffectEnableTime = systemTime();
2804
2805 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2806 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2807 if (t->mType == ThreadBase::OFFLOAD) {
2808 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2809 }
2810 }
2811
2812}
2813
Eric Laurentaaa44472014-09-12 17:41:50 -07002814status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2815{
2816 audio_session_t session = (audio_session_t)chain->sessionId();
2817 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2818 ALOGV("putOrphanEffectChain_l session %d index %d", session, index);
2819 if (index >= 0) {
2820 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2821 return ALREADY_EXISTS;
2822 }
2823 mOrphanEffectChains.add(session, chain);
2824 return NO_ERROR;
2825}
2826
2827sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2828{
2829 sp<EffectChain> chain;
2830 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2831 ALOGV("getOrphanEffectChain_l session %d index %d", session, index);
2832 if (index >= 0) {
2833 chain = mOrphanEffectChains.valueAt(index);
2834 mOrphanEffectChains.removeItemsAt(index);
2835 }
2836 return chain;
2837}
2838
2839bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2840{
2841 Mutex::Autolock _l(mLock);
2842 audio_session_t session = (audio_session_t)effect->sessionId();
2843 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2844 ALOGV("updateOrphanEffectChains session %d index %d", session, index);
2845 if (index >= 0) {
2846 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2847 if (chain->removeEffect_l(effect) == 0) {
2848 ALOGV("updateOrphanEffectChains removing effect chain at index %d", index);
2849 mOrphanEffectChains.removeItemsAt(index);
2850 }
2851 return true;
2852 }
2853 return false;
2854}
2855
2856
Glenn Kastenda6ef132013-01-10 12:31:01 -08002857struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002858#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
2859 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002860};
2861
2862int comparEntry(const void *p1, const void *p2)
2863{
Glenn Kasten2f55e762015-03-05 16:05:08 -08002864 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002865}
2866
Glenn Kasten46909e72013-02-26 09:20:22 -08002867#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002868void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002869{
Eric Laurent81784c32012-11-19 14:55:58 -08002870 NBAIO_Source *teeSource = source.get();
2871 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002872 // .wav rotation
2873 // There is a benign race condition if 2 threads call this simultaneously.
2874 // They would both traverse the directory, but the result would simply be
2875 // failures at unlink() which are ignored. It's also unlikely since
2876 // normally dumpsys is only done by bugreport or from the command line.
2877 char teePath[32+256];
2878 strcpy(teePath, "/data/misc/media");
2879 size_t teePathLen = strlen(teePath);
2880 DIR *dir = opendir(teePath);
2881 teePath[teePathLen++] = '/';
2882 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002883#define TEE_MAX_SORT 20 // number of entries to sort
2884#define TEE_MAX_KEEP 10 // number of entries to keep
2885 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002886 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08002887 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002888 struct dirent de;
2889 struct dirent *result = NULL;
2890 int rc = readdir_r(dir, &de, &result);
2891 if (rc != 0) {
2892 ALOGW("readdir_r failed %d", rc);
2893 break;
2894 }
2895 if (result == NULL) {
2896 break;
2897 }
2898 if (result != &de) {
2899 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2900 break;
2901 }
2902 // ignore non .wav file entries
2903 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002904 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08002905 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2906 continue;
2907 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08002908 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002909 }
2910 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002911 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002912 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002913 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
2914 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002915 (void) unlink(teePath);
2916 }
2917 }
2918 } else {
2919 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002920 dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002921 }
2922 }
Eric Laurent81784c32012-11-19 14:55:58 -08002923 char teeTime[16];
2924 struct timeval tv;
2925 gettimeofday(&tv, NULL);
2926 struct tm tm;
2927 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002928 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2929 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2930 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2931 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002932 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07002933 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08002934 char wavHeader[44];
2935 memcpy(wavHeader,
2936 "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",
2937 sizeof(wavHeader));
2938 NBAIO_Format format = teeSource->format();
2939 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002940 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07002941 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002942 wavHeader[22] = channelCount; // number of channels
2943 wavHeader[24] = sampleRate; // sample rate
2944 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07002945 wavHeader[32] = frameSize; // block alignment
2946 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08002947 write(teeFd, wavHeader, sizeof(wavHeader));
2948 size_t total = 0;
2949 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07002950#define TEE_SINK_READ 1024 // frames per I/O operation
2951 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002952 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08002953 size_t count = TEE_SINK_READ;
Glenn Kastend79072e2016-01-06 08:41:20 -08002954 ssize_t actual = teeSource->read(buffer, count);
Eric Laurent81784c32012-11-19 14:55:58 -08002955 bool wasFirstRead = firstRead;
2956 firstRead = false;
2957 if (actual <= 0) {
2958 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2959 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002960 }
Eric Laurent81784c32012-11-19 14:55:58 -08002961 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002962 }
Eric Laurent81784c32012-11-19 14:55:58 -08002963 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07002964 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002965 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002966 }
Glenn Kasten329f6512014-08-28 16:23:16 -07002967 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08002968 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002969 uint32_t temp = 44 + total * frameSize - 8;
2970 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002971 write(teeFd, &temp, sizeof(temp));
2972 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002973 temp = total * frameSize;
2974 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002975 write(teeFd, &temp, sizeof(temp));
2976 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002977 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002978 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002979 }
Eric Laurent59255e42011-07-27 19:49:51 -07002980 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002981 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002982 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002983 }
Eric Laurent59255e42011-07-27 19:49:51 -07002984 }
2985 }
2986}
Glenn Kasten46909e72013-02-26 09:20:22 -08002987#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002988
Mathias Agopian65ab4712010-07-14 17:59:35 -07002989// ----------------------------------------------------------------------------
2990
2991status_t AudioFlinger::onTransact(
2992 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2993{
2994 return BnAudioFlinger::onTransact(code, data, reply, flags);
2995}
2996
Glenn Kasten63238ef2015-03-02 15:50:29 -08002997} // namespace android