blob: dbc6e6e7e31b13bb7a3ab82c6af75bb338c28a80 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
Mathias Agopian65ab4712010-07-14 17:59:35 -07002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
Glenn Kasten153b9fe2013-07-15 11:23:36 -070022#include "Configuration.h"
Glenn Kastenda6ef132013-01-10 12:31:01 -080023#include <dirent.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070024#include <math.h>
25#include <signal.h>
26#include <sys/time.h>
27#include <sys/resource.h>
28
Gloria Wang9ee159b2011-02-24 14:51:45 -080029#include <binder/IPCThreadState.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070030#include <binder/IServiceManager.h>
31#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070032#include <utils/Trace.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070033#include <binder/Parcel.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/String16.h>
35#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070036#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070037
Dima Zavinfce7a472011-04-19 22:30:36 -070038#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070039#include <cutils/properties.h>
40
Dima Zavin64760242011-05-11 14:15:23 -070041#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070042#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070043
44#include "AudioMixer.h"
45#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080046#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070047
Andy Hung6770c6f2015-04-07 13:43:36 -070048#include <media/AudioResamplerPublic.h>
49
Mathias Agopian65ab4712010-07-14 17:59:35 -070050#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070051#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070052#include <audio_effects/effect_ns.h>
53#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070054
Glenn Kasten3b21c502011-12-15 09:52:39 -080055#include <audio_utils/primitives.h>
56
Eric Laurentfeb0db62011-07-22 09:04:31 -070057#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080058
Glenn Kasten9e58b552013-01-18 15:09:48 -080059#include <media/IMediaLogService.h>
60
Glenn Kastenda6ef132013-01-10 12:31:01 -080061#include <media/nbaio/Pipe.h>
62#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070063#include <media/AudioParameter.h>
Wei Jia3f273d12015-11-24 09:06:49 -080064#include <mediautils/BatteryNotifier.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070065#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080066
Mathias Agopian65ab4712010-07-14 17:59:35 -070067// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070068
John Grossman1c345192012-03-27 14:00:17 -070069// Note: the following macro is used for extremely verbose logging message. In
70// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
71// 0; but one side effect of this is to turn all LOGV's as well. Some messages
72// are so verbose that we want to suppress them even when we have ALOG_ASSERT
73// turned on. Do not uncomment the #def below unless you really know what you
74// are doing and want to see all of the extremely verbose messages.
75//#define VERY_VERY_VERBOSE_LOGGING
76#ifdef VERY_VERY_VERBOSE_LOGGING
77#define ALOGVV ALOGV
78#else
79#define ALOGVV(a...) do { } while(0)
80#endif
Eric Laurentde070132010-07-13 04:45:46 -070081
Mathias Agopian65ab4712010-07-14 17:59:35 -070082namespace android {
83
Glenn Kastenec1d6b52011-12-12 09:04:45 -080084static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
85static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Eric Laurent021cf962014-05-13 10:18:14 -070086static const char kClientLockedString[] = "Client lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070087
Glenn Kasten58912562012-04-03 10:45:00 -070088
John Grossman4ff14ba2012-02-08 16:37:41 -080089nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080090
Eric Laurent81784c32012-11-19 14:55:58 -080091uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070092
Glenn Kasten46909e72013-02-26 09:20:22 -080093#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080094bool AudioFlinger::mTeeSinkInputEnabled = false;
95bool AudioFlinger::mTeeSinkOutputEnabled = false;
96bool AudioFlinger::mTeeSinkTrackEnabled = false;
97
98size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
99size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
100size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800101#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800102
Eric Laurent813e2a72013-08-31 12:59:48 -0700103// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
104// we define a minimum time during which a global effect is considered enabled.
105static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
106
Mathias Agopian65ab4712010-07-14 17:59:35 -0700107// ----------------------------------------------------------------------------
108
Marco Nelissenb2208842014-02-07 14:00:50 -0800109const char *formatToString(audio_format_t format) {
Phil Burkfdb3c072016-02-09 10:47:02 -0800110 switch (audio_get_main_format(format)) {
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530111 case AUDIO_FORMAT_PCM:
112 switch (format) {
113 case AUDIO_FORMAT_PCM_16_BIT: return "pcm16";
114 case AUDIO_FORMAT_PCM_8_BIT: return "pcm8";
115 case AUDIO_FORMAT_PCM_32_BIT: return "pcm32";
116 case AUDIO_FORMAT_PCM_8_24_BIT: return "pcm8.24";
117 case AUDIO_FORMAT_PCM_FLOAT: return "pcmfloat";
118 case AUDIO_FORMAT_PCM_24_BIT_PACKED: return "pcm24";
119 default:
120 break;
121 }
122 break;
Marco Nelissenb2208842014-02-07 14:00:50 -0800123 case AUDIO_FORMAT_MP3: return "mp3";
124 case AUDIO_FORMAT_AMR_NB: return "amr-nb";
125 case AUDIO_FORMAT_AMR_WB: return "amr-wb";
126 case AUDIO_FORMAT_AAC: return "aac";
127 case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
128 case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
129 case AUDIO_FORMAT_VORBIS: return "vorbis";
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530130 case AUDIO_FORMAT_OPUS: return "opus";
131 case AUDIO_FORMAT_AC3: return "ac-3";
132 case AUDIO_FORMAT_E_AC3: return "e-ac-3";
Phil Burkfdb3c072016-02-09 10:47:02 -0800133 case AUDIO_FORMAT_IEC61937: return "iec61937";
Marco Nelissenb2208842014-02-07 14:00:50 -0800134 default:
135 break;
136 }
137 return "unknown";
138}
139
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700140static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700141{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700142 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700143 int rc;
144
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700145 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
146 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
147 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
148 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700149 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700150 }
151 rc = audio_hw_device_open(mod, dev);
152 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
153 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
154 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700155 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700156 }
Eric Laurent5806b352014-05-22 12:23:26 -0700157 if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700158 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
159 rc = BAD_VALUE;
160 goto out;
161 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700162 return 0;
163
164out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700165 *dev = NULL;
166 return rc;
167}
168
Mathias Agopian65ab4712010-07-14 17:59:35 -0700169// ----------------------------------------------------------------------------
170
171AudioFlinger::AudioFlinger()
172 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800173 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800174 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700175 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800176 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800177 mMasterMute(false),
178 mNextUniqueId(1),
179 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700180 mBtNrecIsOff(false),
181 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700182 mIsDeviceTypeKnown(false),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700183 mGlobalEffectEnableTime(0),
Eric Laurent72e3f392015-05-20 14:43:50 -0700184 mSystemReady(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700185{
Glenn Kasten949a9262013-04-16 12:35:20 -0700186 getpid_cached = getpid();
Andy Hungce717682015-12-22 13:40:32 -0800187 // disable media.log until the service is reenabled, see b/26306954
188 const bool doLog = false; // property_get_bool("ro.test_harness", false);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800189 if (doLog) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800190 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters",
191 MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800192 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700193
Wei Jia3f273d12015-11-24 09:06:49 -0800194 // reset battery stats.
195 // if the audio service has crashed, battery stats could be left
196 // in bad state, reset the state upon service start.
197 BatteryNotifier::getInstance().noteResetAudio();
198
Glenn Kasten46909e72013-02-26 09:20:22 -0800199#ifdef TEE_SINK
Glenn Kasten9a003992016-02-23 15:24:34 -0800200 char value[PROPERTY_VALUE_MAX];
Glenn Kastenda6ef132013-01-10 12:31:01 -0800201 (void) property_get("ro.debuggable", value, "0");
202 int debuggable = atoi(value);
203 int teeEnabled = 0;
204 if (debuggable) {
205 (void) property_get("af.tee", value, "0");
206 teeEnabled = atoi(value);
207 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800208 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700209 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800210 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700211 }
212 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800213 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700214 }
215 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800216 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700217 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800218#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700219}
220
221void AudioFlinger::onFirstRef()
222{
Dima Zavin799a70e2011-04-18 16:57:27 -0700223 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700224
Eric Laurent93575202011-01-18 18:39:02 -0800225 Mutex::Autolock _l(mLock);
226
Dima Zavin799a70e2011-04-18 16:57:27 -0700227 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800228 char val_str[PROPERTY_VALUE_MAX] = { 0 };
229 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
230 uint32_t int_val;
231 if (1 == sscanf(val_str, "%u", &int_val)) {
232 mStandbyTimeInNsecs = milliseconds(int_val);
233 ALOGI("Using %u mSec as standby time.", int_val);
234 } else {
235 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
236 ALOGI("Using default %u mSec as standby time.",
237 (uint32_t)(mStandbyTimeInNsecs / 1000000));
238 }
239 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700240
Eric Laurent1c333e22014-05-20 10:48:17 -0700241 mPatchPanel = new PatchPanel(this);
242
Eric Laurenta4c5a552012-03-29 10:12:40 -0700243 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244}
245
246AudioFlinger::~AudioFlinger()
247{
248 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700249 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700250 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700251 }
252 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700253 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700254 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700255 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700256
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800257 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
258 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700259 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
260 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700261 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700262
263 // Tell media.log service about any old writers that still need to be unregistered
Andy Hungce717682015-12-22 13:40:32 -0800264 if (mLogMemoryDealer != 0) {
265 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
266 if (binder != 0) {
267 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
268 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
269 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
270 mUnregisteredWriters.pop();
271 mediaLogService->unregisterWriter(iMemory);
272 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700273 }
274 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700275}
276
Eric Laurenta4c5a552012-03-29 10:12:40 -0700277static const char * const audio_interfaces[] = {
278 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
279 AUDIO_HARDWARE_MODULE_ID_A2DP,
280 AUDIO_HARDWARE_MODULE_ID_USB,
281};
282#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
283
Phil Burk062e67a2015-02-11 13:40:50 -0800284AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700285 audio_module_handle_t module,
286 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700287{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700288 // if module is 0, the request comes from an old policy manager and we should load
289 // well known modules
290 if (module == 0) {
291 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
292 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
293 loadHwModule_l(audio_interfaces[i]);
294 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700295 // then try to find a module supporting the requested device.
296 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
297 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
298 audio_hw_device_t *dev = audioHwDevice->hwDevice();
299 if ((dev->get_supported_devices != NULL) &&
300 (dev->get_supported_devices(dev) & devices) == devices)
301 return audioHwDevice;
302 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700303 } else {
304 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700305 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
306 if (audioHwDevice != NULL) {
307 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700308 }
309 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700310
Dima Zavin799a70e2011-04-18 16:57:27 -0700311 return NULL;
312}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313
Glenn Kasten0f11b512014-01-31 16:18:54 -0800314void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700315{
316 const size_t SIZE = 256;
317 char buffer[SIZE];
318 String8 result;
319
320 result.append("Clients:\n");
321 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800322 sp<Client> client = mClients.valueAt(i).promote();
323 if (client != 0) {
324 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
325 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700326 }
327 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700328
Eric Laurentd1b28d42013-09-18 18:47:13 -0700329 result.append("Notification Clients:\n");
330 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
331 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
332 result.append(buffer);
333 }
334
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700335 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800336 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700337 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
338 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800339 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700340 result.append(buffer);
341 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700342 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700343}
344
345
Glenn Kasten0f11b512014-01-31 16:18:54 -0800346void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700347{
348 const size_t SIZE = 256;
349 char buffer[SIZE];
350 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800351 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700352
John Grossman4ff14ba2012-02-08 16:37:41 -0800353 snprintf(buffer, SIZE, "Hardware status: %d\n"
354 "Standby Time mSec: %u\n",
355 hardwareStatus,
356 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700357 result.append(buffer);
358 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700359}
360
Glenn Kasten0f11b512014-01-31 16:18:54 -0800361void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362{
363 const size_t SIZE = 256;
364 char buffer[SIZE];
365 String8 result;
366 snprintf(buffer, SIZE, "Permission Denial: "
367 "can't dump AudioFlinger from pid=%d, uid=%d\n",
368 IPCThreadState::self()->getCallingPid(),
369 IPCThreadState::self()->getCallingUid());
370 result.append(buffer);
371 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700372}
373
Eric Laurent81784c32012-11-19 14:55:58 -0800374bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700375{
376 bool locked = false;
377 for (int i = 0; i < kDumpLockRetries; ++i) {
378 if (mutex.tryLock() == NO_ERROR) {
379 locked = true;
380 break;
381 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800382 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700383 }
384 return locked;
385}
386
387status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
388{
Glenn Kasten44deb052012-02-05 18:09:08 -0800389 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700390 dumpPermissionDenial(fd, args);
391 } else {
392 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800393 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700394 if (!hardwareLocked) {
395 String8 result(kHardwareLockedString);
396 write(fd, result.string(), result.size());
397 } else {
398 mHardwareLock.unlock();
399 }
400
Eric Laurent81784c32012-11-19 14:55:58 -0800401 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700402
403 // failed to lock - AudioFlinger is probably deadlocked
404 if (!locked) {
405 String8 result(kDeadlockedString);
406 write(fd, result.string(), result.size());
407 }
408
Eric Laurent021cf962014-05-13 10:18:14 -0700409 bool clientLocked = dumpTryLock(mClientLock);
410 if (!clientLocked) {
411 String8 result(kClientLockedString);
412 write(fd, result.string(), result.size());
413 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700414
415 EffectDumpEffects(fd);
416
Mathias Agopian65ab4712010-07-14 17:59:35 -0700417 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700418 if (clientLocked) {
419 mClientLock.unlock();
420 }
421
Mathias Agopian65ab4712010-07-14 17:59:35 -0700422 dumpInternals(fd, args);
423
424 // dump playback threads
425 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
426 mPlaybackThreads.valueAt(i)->dump(fd, args);
427 }
428
429 // dump record threads
430 for (size_t i = 0; i < mRecordThreads.size(); i++) {
431 mRecordThreads.valueAt(i)->dump(fd, args);
432 }
433
Eric Laurentaaa44472014-09-12 17:41:50 -0700434 // dump orphan effect chains
435 if (mOrphanEffectChains.size() != 0) {
436 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
437 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
438 mOrphanEffectChains.valueAt(i)->dump(fd, args);
439 }
440 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700441 // dump all hardware devs
442 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700443 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700444 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700445 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700446
Glenn Kasten46909e72013-02-26 09:20:22 -0800447#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700448 // dump the serially shared record tee sink
449 if (mRecordTeeSource != 0) {
450 dumpTee(fd, mRecordTeeSource);
451 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800452#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700453
Glenn Kastend65d73c2012-06-22 17:21:07 -0700454 if (locked) {
455 mLock.unlock();
456 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800457
458 // append a copy of media.log here by forwarding fd to it, but don't attempt
459 // to lookup the service if it's not running, as it will block for a second
460 if (mLogMemoryDealer != 0) {
461 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
462 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700463 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800464 Vector<String16> args;
465 binder->dump(fd, args);
466 }
467 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700468 }
469 return NO_ERROR;
470}
471
Eric Laurent021cf962014-05-13 10:18:14 -0700472sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800473{
Eric Laurent021cf962014-05-13 10:18:14 -0700474 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800475 // If pid is already in the mClients wp<> map, then use that entry
476 // (for which promote() is always != 0), otherwise create a new entry and Client.
477 sp<Client> client = mClients.valueFor(pid).promote();
478 if (client == 0) {
479 client = new Client(this, pid);
480 mClients.add(pid, client);
481 }
482
483 return client;
484}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700485
Glenn Kasten9e58b552013-01-18 15:09:48 -0800486sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
487{
Glenn Kasten481fb672013-09-30 14:39:28 -0700488 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800489 if (mLogMemoryDealer == 0) {
490 return new NBLog::Writer();
491 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800492 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700493 // Similarly if we can't contact the media.log service, also return a dummy writer
494 if (binder == 0) {
495 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800496 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700497 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
498 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
499 // If allocation fails, consult the vector of previously unregistered writers
500 // and garbage-collect one or more them until an allocation succeeds
501 if (shared == 0) {
502 Mutex::Autolock _l(mUnregisteredWritersLock);
503 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
504 {
505 // Pick the oldest stale writer to garbage-collect
506 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
507 mUnregisteredWriters.removeAt(0);
508 mediaLogService->unregisterWriter(iMemory);
509 // Now the media.log remote reference to IMemory is gone. When our last local
510 // reference to IMemory also drops to zero at end of this block,
511 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
512 }
513 // Re-attempt the allocation
514 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
515 if (shared != 0) {
516 goto success;
517 }
518 }
519 // Even after garbage-collecting all old writers, there is still not enough memory,
520 // so return a dummy writer
521 return new NBLog::Writer();
522 }
523success:
524 mediaLogService->registerWriter(shared, size, name);
525 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800526}
527
528void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
529{
Glenn Kasten685ef092013-02-04 08:15:34 -0800530 if (writer == 0) {
531 return;
532 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800533 sp<IMemory> iMemory(writer->getIMemory());
534 if (iMemory == 0) {
535 return;
536 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700537 // Rather than removing the writer immediately, append it to a queue of old writers to
538 // be garbage-collected later. This allows us to continue to view old logs for a while.
539 Mutex::Autolock _l(mUnregisteredWritersLock);
540 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800541}
542
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543// IAudioFlinger interface
544
545
546sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800547 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700548 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800549 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700550 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800551 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800552 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800554 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800555 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700556 int *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800557 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700558 status_t *status)
559{
560 sp<PlaybackThread::Track> track;
561 sp<TrackHandle> trackHandle;
562 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700563 status_t lStatus;
564 int lSessionId;
565
Glenn Kasten263709e2012-01-06 08:40:01 -0800566 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
567 // but if someone uses binder directly they could bypass that and cause us to crash
568 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000569 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700570 lStatus = BAD_VALUE;
571 goto Exit;
572 }
573
Glenn Kasten53b5d092014-02-05 10:00:23 -0800574 // further sample rate checks are performed by createTrack_l() depending on the thread type
575 if (sampleRate == 0) {
576 ALOGE("createTrack() invalid sample rate %u", sampleRate);
577 lStatus = BAD_VALUE;
578 goto Exit;
579 }
580
581 // further channel mask checks are performed by createTrack_l() depending on the thread type
582 if (!audio_is_output_channel(channelMask)) {
583 ALOGE("createTrack() invalid channel mask %#x", channelMask);
584 lStatus = BAD_VALUE;
585 goto Exit;
586 }
587
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700588 // further format checks are performed by createTrack_l() depending on the thread type
589 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800590 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700591 lStatus = BAD_VALUE;
592 goto Exit;
593 }
594
Glenn Kasten663c2242013-09-24 11:52:37 -0700595 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
596 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
597 lStatus = BAD_VALUE;
598 goto Exit;
599 }
600
Mathias Agopian65ab4712010-07-14 17:59:35 -0700601 {
602 Mutex::Autolock _l(mLock);
603 PlaybackThread *thread = checkPlaybackThread_l(output);
604 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800605 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700606 lStatus = BAD_VALUE;
607 goto Exit;
608 }
609
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800610 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -0700611 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700612
Glenn Kastene848bd92014-03-13 15:00:32 -0700613 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700614 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700615 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700616 // check if an effect chain with the same session ID is present on another
617 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700618 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700619 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
620 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700621 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700622 if (sessions & PlaybackThread::EFFECT_SESSION) {
623 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700624 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700625 }
Eric Laurentde070132010-07-13 04:45:46 -0700626 }
627 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700629 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700630 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700631 if (sessionId != NULL) {
632 *sessionId = lSessionId;
633 }
634 }
Steve Block3856b092011-10-20 11:56:00 +0100635 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700636
637 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800638 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800639 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700640 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700641
642 // move effect chain to this output thread if an effect on same session was waiting
643 // for a track to be created
644 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700645 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700646 Mutex::Autolock _dl(thread->mLock);
647 Mutex::Autolock _sl(effectThread->mLock);
648 moveEffectChain_l(lSessionId, effectThread, thread, true);
649 }
Eric Laurenta011e352012-03-29 15:51:43 -0700650
651 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700652 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700653 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
654 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700655 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700656 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700657 } else {
658 mPendingSyncEvents[i]->cancel();
659 }
Eric Laurenta011e352012-03-29 15:51:43 -0700660 mPendingSyncEvents.removeAt(i);
661 i--;
662 }
663 }
664 }
Glenn Kastene198c362013-08-13 09:13:36 -0700665
Eric Laurentfa90e842014-10-17 18:12:31 -0700666 setAudioHwSyncForSession_l(thread, (audio_session_t)lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700667 }
Glenn Kastene198c362013-08-13 09:13:36 -0700668
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700669 if (lStatus != NO_ERROR) {
670 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700671 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700672 // Don't hold mClientLock when releasing the reference on the track as the
673 // destructor will acquire it.
674 {
675 Mutex::Autolock _cl(mClientLock);
676 client.clear();
677 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700679 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700680 }
681
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700682 // return handle to client
683 trackHandle = new TrackHandle(track);
684
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700686 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700687 return trackHandle;
688}
689
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800690uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700691{
692 Mutex::Autolock _l(mLock);
693 PlaybackThread *thread = checkPlaybackThread_l(output);
694 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000695 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700696 return 0;
697 }
698 return thread->sampleRate();
699}
700
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800701audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700702{
703 Mutex::Autolock _l(mLock);
704 PlaybackThread *thread = checkPlaybackThread_l(output);
705 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000706 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800707 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700708 }
709 return thread->format();
710}
711
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800712size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700713{
714 Mutex::Autolock _l(mLock);
715 PlaybackThread *thread = checkPlaybackThread_l(output);
716 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000717 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700718 return 0;
719 }
Glenn Kasten58912562012-04-03 10:45:00 -0700720 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
721 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700722 return thread->frameCount();
723}
724
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800725uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700726{
727 Mutex::Autolock _l(mLock);
728 PlaybackThread *thread = checkPlaybackThread_l(output);
729 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800730 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700731 return 0;
732 }
733 return thread->latency();
734}
735
736status_t AudioFlinger::setMasterVolume(float value)
737{
Eric Laurenta1884f92011-08-23 08:25:03 -0700738 status_t ret = initCheck();
739 if (ret != NO_ERROR) {
740 return ret;
741 }
742
Mathias Agopian65ab4712010-07-14 17:59:35 -0700743 // check calling permissions
744 if (!settingsAllowed()) {
745 return PERMISSION_DENIED;
746 }
747
Eric Laurenta4c5a552012-03-29 10:12:40 -0700748 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700749 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700750
John Grossmanee578c02012-07-23 17:05:46 -0700751 // Set master volume in the HALs which support it.
752 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
753 AutoMutex lock(mHardwareLock);
754 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800755
John Grossmanee578c02012-07-23 17:05:46 -0700756 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
757 if (dev->canSetMasterVolume()) {
758 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800759 }
John Grossmanee578c02012-07-23 17:05:46 -0700760 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700761 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700762
John Grossmanee578c02012-07-23 17:05:46 -0700763 // Now set the master volume in each playback thread. Playback threads
764 // assigned to HALs which do not have master volume support will apply
765 // master volume during the mix operation. Threads with HALs which do
766 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700767 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
768 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
769 continue;
770 }
John Grossmanee578c02012-07-23 17:05:46 -0700771 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700772 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700773
774 return NO_ERROR;
775}
776
Glenn Kastenf78aee72012-01-04 11:00:47 -0800777status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700778{
Eric Laurenta1884f92011-08-23 08:25:03 -0700779 status_t ret = initCheck();
780 if (ret != NO_ERROR) {
781 return ret;
782 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700783
784 // check calling permissions
785 if (!settingsAllowed()) {
786 return PERMISSION_DENIED;
787 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800788 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000789 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700790 return BAD_VALUE;
791 }
792
793 { // scope for the lock
794 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700795 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700796 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700797 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700798 mHardwareStatus = AUDIO_HW_IDLE;
799 }
800
801 if (NO_ERROR == ret) {
802 Mutex::Autolock _l(mLock);
803 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800804 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700805 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700806 }
807
808 return ret;
809}
810
811status_t AudioFlinger::setMicMute(bool state)
812{
Eric Laurenta1884f92011-08-23 08:25:03 -0700813 status_t ret = initCheck();
814 if (ret != NO_ERROR) {
815 return ret;
816 }
817
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818 // check calling permissions
819 if (!settingsAllowed()) {
820 return PERMISSION_DENIED;
821 }
822
823 AutoMutex lock(mHardwareLock);
824 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700825 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
826 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
827 status_t result = dev->set_mic_mute(dev, state);
828 if (result != NO_ERROR) {
829 ret = result;
830 }
831 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700832 mHardwareStatus = AUDIO_HW_IDLE;
833 return ret;
834}
835
836bool AudioFlinger::getMicMute() const
837{
Eric Laurenta1884f92011-08-23 08:25:03 -0700838 status_t ret = initCheck();
839 if (ret != NO_ERROR) {
840 return false;
841 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800842 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700843 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800844 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700845 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800846 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
847 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
848 status_t result = dev->get_mic_mute(dev, &state);
849 if (result == NO_ERROR) {
850 mute = mute && state;
851 }
852 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700853 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800854
855 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700856}
857
858status_t AudioFlinger::setMasterMute(bool muted)
859{
John Grossmand8f178d2012-07-20 14:51:35 -0700860 status_t ret = initCheck();
861 if (ret != NO_ERROR) {
862 return ret;
863 }
864
Mathias Agopian65ab4712010-07-14 17:59:35 -0700865 // check calling permissions
866 if (!settingsAllowed()) {
867 return PERMISSION_DENIED;
868 }
869
John Grossmanee578c02012-07-23 17:05:46 -0700870 Mutex::Autolock _l(mLock);
871 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700872
John Grossmanee578c02012-07-23 17:05:46 -0700873 // Set master mute in the HALs which support it.
874 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
875 AutoMutex lock(mHardwareLock);
876 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700877
John Grossmanee578c02012-07-23 17:05:46 -0700878 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
879 if (dev->canSetMasterMute()) {
880 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700881 }
John Grossmanee578c02012-07-23 17:05:46 -0700882 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700883 }
884
John Grossmanee578c02012-07-23 17:05:46 -0700885 // Now set the master mute in each playback thread. Playback threads
886 // assigned to HALs which do not have master mute support will apply master
887 // mute during the mix operation. Threads with HALs which do support master
888 // mute will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700889 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
890 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
891 continue;
892 }
John Grossmanee578c02012-07-23 17:05:46 -0700893 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700894 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700895
896 return NO_ERROR;
897}
898
899float AudioFlinger::masterVolume() const
900{
Glenn Kasten98067102011-12-13 11:47:54 -0800901 Mutex::Autolock _l(mLock);
902 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700903}
904
905bool AudioFlinger::masterMute() const
906{
Glenn Kasten98067102011-12-13 11:47:54 -0800907 Mutex::Autolock _l(mLock);
908 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700909}
910
John Grossman4ff14ba2012-02-08 16:37:41 -0800911float AudioFlinger::masterVolume_l() const
912{
John Grossman4ff14ba2012-02-08 16:37:41 -0800913 return mMasterVolume;
914}
915
John Grossmand8f178d2012-07-20 14:51:35 -0700916bool AudioFlinger::masterMute_l() const
917{
John Grossmanee578c02012-07-23 17:05:46 -0700918 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700919}
920
Eric Laurent223fd5c2014-11-11 13:43:36 -0800921status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
922{
923 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
924 ALOGW("setStreamVolume() invalid stream %d", stream);
925 return BAD_VALUE;
926 }
927 pid_t caller = IPCThreadState::self()->getCallingPid();
928 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
929 ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
930 return PERMISSION_DENIED;
931 }
932
933 return NO_ERROR;
934}
935
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800936status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
937 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700938{
939 // check calling permissions
940 if (!settingsAllowed()) {
941 return PERMISSION_DENIED;
942 }
943
Eric Laurent223fd5c2014-11-11 13:43:36 -0800944 status_t status = checkStreamType(stream);
945 if (status != NO_ERROR) {
946 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800948 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700949
950 AutoMutex lock(mLock);
951 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700952 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700953 thread = checkPlaybackThread_l(output);
954 if (thread == NULL) {
955 return BAD_VALUE;
956 }
957 }
958
959 mStreamTypes[stream].volume = value;
960
961 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800962 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700963 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700964 }
965 } else {
966 thread->setStreamVolume(stream, value);
967 }
968
969 return NO_ERROR;
970}
971
Glenn Kastenfff6d712012-01-12 16:38:12 -0800972status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700973{
974 // check calling permissions
975 if (!settingsAllowed()) {
976 return PERMISSION_DENIED;
977 }
978
Eric Laurent223fd5c2014-11-11 13:43:36 -0800979 status_t status = checkStreamType(stream);
980 if (status != NO_ERROR) {
981 return status;
982 }
983 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
984
985 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000986 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700987 return BAD_VALUE;
988 }
989
Eric Laurent93575202011-01-18 18:39:02 -0800990 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700991 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700992 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700993 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700994
995 return NO_ERROR;
996}
997
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800998float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700999{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001000 status_t status = checkStreamType(stream);
1001 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001002 return 0.0f;
1003 }
1004
1005 AutoMutex lock(mLock);
1006 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -07001007 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001008 PlaybackThread *thread = checkPlaybackThread_l(output);
1009 if (thread == NULL) {
1010 return 0.0f;
1011 }
1012 volume = thread->streamVolume(stream);
1013 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001014 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001015 }
1016
1017 return volume;
1018}
1019
Glenn Kastenfff6d712012-01-12 16:38:12 -08001020bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001021{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001022 status_t status = checkStreamType(stream);
1023 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001024 return true;
1025 }
1026
Glenn Kasten6637baa2012-01-09 09:40:36 -08001027 AutoMutex lock(mLock);
1028 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001029}
1030
Eric Laurent054d9d32015-04-24 08:48:48 -07001031
1032void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1033{
1034 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1035 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1036 }
1037}
1038
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001039status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001040{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001041 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1042 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001043
Mathias Agopian65ab4712010-07-14 17:59:35 -07001044 // check calling permissions
1045 if (!settingsAllowed()) {
1046 return PERMISSION_DENIED;
1047 }
1048
Glenn Kasten142f5192014-03-25 17:44:59 -07001049 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1050 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001051 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -07001052 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001053 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001054 AutoMutex lock(mHardwareLock);
1055 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1056 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1057 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1058 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1059 final_result = result ?: final_result;
1060 }
1061 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001062 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001063 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1064 AudioParameter param = AudioParameter(keyValuePairs);
1065 String8 value;
1066 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001067 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1068 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001069 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1070 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001071 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001072 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1073 // collect all of the thread's session IDs
1074 KeyedVector<int, bool> ids = thread->sessionIds();
1075 // suspend effects associated with those session IDs
1076 for (size_t j = 0; j < ids.size(); ++j) {
1077 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001078 thread->setEffectSuspended(FX_IID_AEC,
1079 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001080 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001081 thread->setEffectSuspended(FX_IID_NS,
1082 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001083 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001084 }
1085 }
Eric Laurentbee53372011-08-29 12:42:48 -07001086 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001087 }
1088 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001089 String8 screenState;
1090 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1091 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001092 if (isOff != (AudioFlinger::mScreenState & 1)) {
1093 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001094 }
1095 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001096 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001097 }
1098
1099 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1100 // and the thread is exited once the lock is released
1101 sp<ThreadBase> thread;
1102 {
1103 Mutex::Autolock _l(mLock);
1104 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001105 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001106 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001107 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001108 // indicate output device change to all input threads for pre processing
1109 AudioParameter param = AudioParameter(keyValuePairs);
1110 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001111 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1112 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001113 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001114 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001115 }
1116 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001117 if (thread != 0) {
1118 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001119 }
1120 return BAD_VALUE;
1121}
1122
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001123String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001124{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001125 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1126 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001127
Eric Laurenta4c5a552012-03-29 10:12:40 -07001128 Mutex::Autolock _l(mLock);
1129
Glenn Kasten142f5192014-03-25 17:44:59 -07001130 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001131 String8 out_s8;
1132
Dima Zavin799a70e2011-04-18 16:57:27 -07001133 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001134 char *s;
1135 {
1136 AutoMutex lock(mHardwareLock);
1137 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001138 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001139 s = dev->get_parameters(dev, keys.string());
1140 mHardwareStatus = AUDIO_HW_IDLE;
1141 }
John Grossmanef7740b2012-02-09 11:28:36 -08001142 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001143 free(s);
1144 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001145 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001146 }
1147
Mathias Agopian65ab4712010-07-14 17:59:35 -07001148 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1149 if (playbackThread != NULL) {
1150 return playbackThread->getParameters(keys);
1151 }
1152 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1153 if (recordThread != NULL) {
1154 return recordThread->getParameters(keys);
1155 }
1156 return String8("");
1157}
1158
Glenn Kastendd8104c2012-07-02 12:42:44 -07001159size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1160 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001161{
Eric Laurenta1884f92011-08-23 08:25:03 -07001162 status_t ret = initCheck();
1163 if (ret != NO_ERROR) {
1164 return 0;
1165 }
Eric Laurentcdf924a2016-02-04 15:57:56 -08001166 if ((sampleRate == 0) ||
Phil Burkfdb3c072016-02-09 10:47:02 -08001167 !audio_is_valid_format(format) || !audio_has_proportional_frames(format) ||
Eric Laurentcdf924a2016-02-04 15:57:56 -08001168 !audio_is_input_channel(channelMask)) {
Andy Hung6770c6f2015-04-07 13:43:36 -07001169 return 0;
1170 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001171
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001172 AutoMutex lock(mHardwareLock);
1173 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001174 audio_config_t config, proposed;
1175 memset(&proposed, 0, sizeof(proposed));
1176 proposed.sample_rate = sampleRate;
1177 proposed.channel_mask = channelMask;
1178 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001179
John Grossmanee578c02012-07-23 17:05:46 -07001180 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001181 size_t frames;
1182 for (;;) {
1183 // Note: config is currently a const parameter for get_input_buffer_size()
1184 // but we use a copy from proposed in case config changes from the call.
1185 config = proposed;
1186 frames = dev->get_input_buffer_size(dev, &config);
1187 if (frames != 0) {
1188 break; // hal success, config is the result
1189 }
1190 // change one parameter of the configuration each iteration to a more "common" value
1191 // to see if the device will support it.
1192 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1193 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1194 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1195 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1196 } else {
1197 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1198 "format %#x, channelMask 0x%X",
1199 sampleRate, format, channelMask);
1200 break; // retries failed, break out of loop with frames == 0.
1201 }
1202 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001203 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001204 if (frames > 0 && config.sample_rate != sampleRate) {
1205 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1206 }
1207 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001208}
1209
Glenn Kasten5f972c02014-01-13 09:59:31 -08001210uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001212 Mutex::Autolock _l(mLock);
1213
1214 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1215 if (recordThread != NULL) {
1216 return recordThread->getInputFramesLost();
1217 }
1218 return 0;
1219}
1220
1221status_t AudioFlinger::setVoiceVolume(float value)
1222{
Eric Laurenta1884f92011-08-23 08:25:03 -07001223 status_t ret = initCheck();
1224 if (ret != NO_ERROR) {
1225 return ret;
1226 }
1227
Mathias Agopian65ab4712010-07-14 17:59:35 -07001228 // check calling permissions
1229 if (!settingsAllowed()) {
1230 return PERMISSION_DENIED;
1231 }
1232
1233 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001234 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001235 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001236 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001237 mHardwareStatus = AUDIO_HW_IDLE;
1238
1239 return ret;
1240}
1241
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001242status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001243 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001244{
1245 status_t status;
1246
1247 Mutex::Autolock _l(mLock);
1248
1249 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1250 if (playbackThread != NULL) {
1251 return playbackThread->getRenderPosition(halFrames, dspFrames);
1252 }
1253
1254 return BAD_VALUE;
1255}
1256
1257void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1258{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001259 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001260 if (client == 0) {
1261 return;
1262 }
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001263 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001264 {
1265 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001266 if (mNotificationClients.indexOfKey(pid) < 0) {
1267 sp<NotificationClient> notificationClient = new NotificationClient(this,
1268 client,
1269 pid);
1270 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001271
Eric Laurent021cf962014-05-13 10:18:14 -07001272 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001273
Marco Nelissen06b46062014-11-14 07:58:25 -08001274 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001275 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001276 }
1277 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001278
Eric Laurent021cf962014-05-13 10:18:14 -07001279 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001280 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001281 // the config change is always sent from playback or record threads to avoid deadlock
1282 // with AudioSystem::gLock
1283 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1284 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1285 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001286
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001287 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1288 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001289 }
1290}
1291
1292void AudioFlinger::removeNotificationClient(pid_t pid)
1293{
1294 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001295 {
1296 Mutex::Autolock _cl(mClientLock);
1297 mNotificationClients.removeItem(pid);
1298 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001299
Steve Block3856b092011-10-20 11:56:00 +01001300 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001301 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001302 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001303 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001304 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001305 ALOGV(" pid %d @ %d", ref->mPid, i);
1306 if (ref->mPid == pid) {
1307 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001308 mAudioSessionRefs.removeAt(i);
1309 delete ref;
1310 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001311 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001312 } else {
1313 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001314 }
1315 }
1316 if (removed) {
1317 purgeStaleEffects_l();
1318 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001319}
1320
Eric Laurent73e26b62015-04-27 16:55:58 -07001321void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001322 const sp<AudioIoDescriptor>& ioDesc,
1323 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001324{
Eric Laurent021cf962014-05-13 10:18:14 -07001325 Mutex::Autolock _l(mClientLock);
1326 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001327 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001328 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1329 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1330 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001331 }
1332}
1333
Eric Laurent021cf962014-05-13 10:18:14 -07001334// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001335void AudioFlinger::removeClient_l(pid_t pid)
1336{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001337 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001338 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001339 mClients.removeItem(pid);
1340}
1341
Eric Laurent717e1282012-06-29 16:36:52 -07001342// getEffectThread_l() must be called with AudioFlinger::mLock held
1343sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1344{
1345 sp<PlaybackThread> thread;
1346
1347 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1348 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1349 ALOG_ASSERT(thread == 0);
1350 thread = mPlaybackThreads.valueAt(i);
1351 }
1352 }
1353
1354 return thread;
1355}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001356
Mathias Agopian65ab4712010-07-14 17:59:35 -07001357
Mathias Agopian65ab4712010-07-14 17:59:35 -07001358
1359// ----------------------------------------------------------------------------
1360
1361AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1362 : RefBase(),
1363 mAudioFlinger(audioFlinger),
Glenn Kastend79072e2016-01-06 08:41:20 -08001364 mPid(pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001365{
Eric Laurentda73b6c2015-08-20 16:18:53 -07001366 size_t heapSize = kClientSharedHeapSizeBytes;
1367 // Increase heap size on non low ram devices to limit risk of reconnection failure for
1368 // invalidated tracks
1369 if (!audioFlinger->isLowRamDevice()) {
1370 heapSize *= kClientSharedHeapSizeMultiplier;
1371 }
1372 mMemoryDealer = new MemoryDealer(heapSize, "AudioFlinger::Client");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001373}
1374
Eric Laurent021cf962014-05-13 10:18:14 -07001375// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001376AudioFlinger::Client::~Client()
1377{
1378 mAudioFlinger->removeClient_l(mPid);
1379}
1380
Glenn Kasten435dbe62012-01-30 10:15:48 -08001381sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001382{
1383 return mMemoryDealer;
1384}
1385
1386// ----------------------------------------------------------------------------
1387
1388AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1389 const sp<IAudioFlingerClient>& client,
1390 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001391 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001392{
1393}
1394
1395AudioFlinger::NotificationClient::~NotificationClient()
1396{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001397}
1398
Glenn Kasten0f11b512014-01-31 16:18:54 -08001399void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001400{
1401 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001402 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001403}
1404
Mathias Agopian65ab4712010-07-14 17:59:35 -07001405
1406// ----------------------------------------------------------------------------
1407
Jeff Brown893a5642013-08-16 20:19:26 -07001408static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1409 return audio_is_remote_submix_device(inDevice);
1410}
1411
Mathias Agopian65ab4712010-07-14 17:59:35 -07001412sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001413 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001414 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001415 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001416 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001417 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001418 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001419 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001420 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001421 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001422 int *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001423 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001424 sp<IMemory>& cblk,
1425 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001426 status_t *status)
1427{
1428 sp<RecordThread::RecordTrack> recordTrack;
1429 sp<RecordHandle> recordHandle;
1430 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001431 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001432 int lSessionId;
1433
Glenn Kastend776ac62014-05-07 09:16:09 -07001434 cblk.clear();
1435 buffers.clear();
1436
Marco Nelissendcb346b2015-09-09 10:47:29 -07001437 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
1438 if (!isTrustedCallingUid(callingUid)) {
Andy Hung879db192016-01-05 16:00:34 -08001439 ALOGW_IF((uid_t)clientUid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -07001440 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, clientUid);
1441 clientUid = callingUid;
1442 }
1443
Mathias Agopian65ab4712010-07-14 17:59:35 -07001444 // check calling permissions
Marco Nelissendcb346b2015-09-09 10:47:29 -07001445 if (!recordingAllowed(opPackageName, tid, clientUid)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001446 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001447 lStatus = PERMISSION_DENIED;
1448 goto Exit;
1449 }
1450
Glenn Kasten53b5d092014-02-05 10:00:23 -08001451 // further sample rate checks are performed by createRecordTrack_l()
1452 if (sampleRate == 0) {
1453 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1454 lStatus = BAD_VALUE;
1455 goto Exit;
1456 }
1457
Andy Hung6770c6f2015-04-07 13:43:36 -07001458 // we don't yet support anything other than linear PCM
1459 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001460 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001461 lStatus = BAD_VALUE;
1462 goto Exit;
1463 }
1464
Glenn Kasten53b5d092014-02-05 10:00:23 -08001465 // further channel mask checks are performed by createRecordTrack_l()
1466 if (!audio_is_input_channel(channelMask)) {
1467 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1468 lStatus = BAD_VALUE;
1469 goto Exit;
1470 }
1471
Glenn Kasten05997e22014-03-13 15:08:33 -07001472 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001473 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001474 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001475 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001476 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001477 lStatus = BAD_VALUE;
1478 goto Exit;
1479 }
1480
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001481 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001482 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001483
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001484 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001485 lSessionId = *sessionId;
1486 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001487 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001488 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001489 if (sessionId != NULL) {
1490 *sessionId = lSessionId;
1491 }
1492 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001493 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001494
Glenn Kasten1879fff2012-07-11 15:36:59 -07001495 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001496 frameCount, lSessionId, notificationFrames,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001497 clientUid, flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001498 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001499
1500 if (lStatus == NO_ERROR) {
1501 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1502 // session and move it to this thread.
1503 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)lSessionId);
1504 if (chain != 0) {
1505 Mutex::Autolock _l(thread->mLock);
1506 thread->addEffectChain_l(chain);
1507 }
1508 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001509 }
Glenn Kastene198c362013-08-13 09:13:36 -07001510
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001511 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001512 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001513 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001514 // Don't hold mClientLock when releasing the reference on the track as the
1515 // destructor will acquire it.
1516 {
1517 Mutex::Autolock _cl(mClientLock);
1518 client.clear();
1519 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001520 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001521 goto Exit;
1522 }
1523
Glenn Kastend776ac62014-05-07 09:16:09 -07001524 cblk = recordTrack->getCblk();
1525 buffers = recordTrack->getBuffers();
1526
Glenn Kasten2fc14732013-08-05 14:58:14 -07001527 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001528 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001529
1530Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001531 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001532 return recordHandle;
1533}
1534
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001535
1536
Mathias Agopian65ab4712010-07-14 17:59:35 -07001537// ----------------------------------------------------------------------------
1538
Eric Laurenta4c5a552012-03-29 10:12:40 -07001539audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1540{
Eric Laurent44622db2014-08-01 19:00:33 -07001541 if (name == NULL) {
1542 return 0;
1543 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001544 if (!settingsAllowed()) {
1545 return 0;
1546 }
1547 Mutex::Autolock _l(mLock);
1548 return loadHwModule_l(name);
1549}
1550
1551// loadHwModule_l() must be called with AudioFlinger::mLock held
1552audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1553{
1554 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1555 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1556 ALOGW("loadHwModule() module %s already loaded", name);
1557 return mAudioHwDevs.keyAt(i);
1558 }
1559 }
1560
Eric Laurenta4c5a552012-03-29 10:12:40 -07001561 audio_hw_device_t *dev;
1562
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001563 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001564 if (rc) {
1565 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1566 return 0;
1567 }
1568
1569 mHardwareStatus = AUDIO_HW_INIT;
1570 rc = dev->init_check(dev);
1571 mHardwareStatus = AUDIO_HW_IDLE;
1572 if (rc) {
1573 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1574 return 0;
1575 }
1576
John Grossmanee578c02012-07-23 17:05:46 -07001577 // Check and cache this HAL's level of support for master mute and master
1578 // volume. If this is the first HAL opened, and it supports the get
1579 // methods, use the initial values provided by the HAL as the current
1580 // master mute and volume settings.
1581
1582 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1583 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001584 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001585
1586 if (0 == mAudioHwDevs.size()) {
1587 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1588 if (NULL != dev->get_master_volume) {
1589 float mv;
1590 if (OK == dev->get_master_volume(dev, &mv)) {
1591 mMasterVolume = mv;
1592 }
1593 }
1594
1595 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1596 if (NULL != dev->get_master_mute) {
1597 bool mm;
1598 if (OK == dev->get_master_mute(dev, &mm)) {
1599 mMasterMute = mm;
1600 }
1601 }
1602 }
1603
Eric Laurenta4c5a552012-03-29 10:12:40 -07001604 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001605 if ((NULL != dev->set_master_volume) &&
1606 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1607 flags = static_cast<AudioHwDevice::Flags>(flags |
1608 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1609 }
1610
1611 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1612 if ((NULL != dev->set_master_mute) &&
1613 (OK == dev->set_master_mute(dev, mMasterMute))) {
1614 flags = static_cast<AudioHwDevice::Flags>(flags |
1615 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1616 }
1617
Eric Laurenta4c5a552012-03-29 10:12:40 -07001618 mHardwareStatus = AUDIO_HW_IDLE;
1619 }
1620
1621 audio_module_handle_t handle = nextUniqueId();
Eric Laurent83b88082014-06-20 18:31:16 -07001622 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001623
1624 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001625 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001626
1627 return handle;
1628
1629}
1630
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001631// ----------------------------------------------------------------------------
1632
Glenn Kasten3b16c762012-11-14 08:44:39 -08001633uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001634{
1635 Mutex::Autolock _l(mLock);
1636 PlaybackThread *thread = primaryPlaybackThread_l();
1637 return thread != NULL ? thread->sampleRate() : 0;
1638}
1639
Glenn Kastene33054e2012-11-14 12:54:39 -08001640size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001641{
1642 Mutex::Autolock _l(mLock);
1643 PlaybackThread *thread = primaryPlaybackThread_l();
1644 return thread != NULL ? thread->frameCountHAL() : 0;
1645}
1646
1647// ----------------------------------------------------------------------------
1648
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001649status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1650{
1651 uid_t uid = IPCThreadState::self()->getCallingUid();
1652 if (uid != AID_SYSTEM) {
1653 return PERMISSION_DENIED;
1654 }
1655 Mutex::Autolock _l(mLock);
1656 if (mIsDeviceTypeKnown) {
1657 return INVALID_OPERATION;
1658 }
1659 mIsLowRamDevice = isLowRamDevice;
1660 mIsDeviceTypeKnown = true;
1661 return NO_ERROR;
1662}
1663
Eric Laurent93c3d412014-08-01 14:48:35 -07001664audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1665{
1666 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001667
1668 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1669 if (index >= 0) {
1670 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1671 mHwAvSyncIds.valueAt(index), sessionId);
1672 return mHwAvSyncIds.valueAt(index);
1673 }
1674
1675 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1676 if (dev == NULL) {
1677 return AUDIO_HW_SYNC_INVALID;
1678 }
1679 char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1680 AudioParameter param = AudioParameter(String8(reply));
1681 free(reply);
1682
1683 int value;
1684 if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1685 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1686 return AUDIO_HW_SYNC_INVALID;
1687 }
1688
1689 // allow only one session for a given HW A/V sync ID.
1690 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1691 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1692 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1693 value, mHwAvSyncIds.keyAt(i));
1694 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001695 break;
1696 }
1697 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001698
1699 mHwAvSyncIds.add(sessionId, value);
1700
1701 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1702 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1703 uint32_t sessions = thread->hasAudioSession(sessionId);
1704 if (sessions & PlaybackThread::TRACK_SESSION) {
1705 AudioParameter param = AudioParameter();
1706 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1707 thread->setParameters(param.toString());
1708 break;
1709 }
1710 }
1711
1712 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1713 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001714}
1715
Eric Laurent72e3f392015-05-20 14:43:50 -07001716status_t AudioFlinger::systemReady()
1717{
1718 Mutex::Autolock _l(mLock);
1719 ALOGI("%s", __FUNCTION__);
1720 if (mSystemReady) {
1721 ALOGW("%s called twice", __FUNCTION__);
1722 return NO_ERROR;
1723 }
1724 mSystemReady = true;
1725 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1726 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1727 thread->systemReady();
1728 }
1729 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1730 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1731 thread->systemReady();
1732 }
1733 return NO_ERROR;
1734}
1735
Eric Laurentfa90e842014-10-17 18:12:31 -07001736// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1737void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1738{
1739 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1740 if (index >= 0) {
1741 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1742 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1743 AudioParameter param = AudioParameter();
1744 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1745 thread->setParameters(param.toString());
1746 }
1747}
1748
1749
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001750// ----------------------------------------------------------------------------
1751
Eric Laurent83b88082014-06-20 18:31:16 -07001752
1753sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001754 audio_io_handle_t *output,
1755 audio_config_t *config,
1756 audio_devices_t devices,
1757 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001758 audio_output_flags_t flags)
1759{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001760 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001761 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001762 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001763 }
1764
1765 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentcf2c0212014-07-25 16:20:43 -07001766 if (*output == AUDIO_IO_HANDLE_NONE) {
1767 *output = nextUniqueId();
1768 }
Eric Laurent83b88082014-06-20 18:31:16 -07001769
1770 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1771
Eric Laurent83b88082014-06-20 18:31:16 -07001772 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001773 // This if statement allows overriding the audio policy settings
1774 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1775 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1776 // Check only for Normal Mixing mode
1777 if (kEnableExtendedPrecision) {
1778 // Specify format (uncomment one below to choose)
1779 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1780 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1781 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1782 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001783 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001784 }
1785 if (kEnableExtendedChannels) {
1786 // Specify channel mask (uncomment one below to choose)
1787 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1788 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1789 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1790 }
Eric Laurent83b88082014-06-20 18:31:16 -07001791 }
1792
Phil Burk062e67a2015-02-11 13:40:50 -08001793 AudioStreamOut *outputStream = NULL;
1794 status_t status = outHwDev->openOutputStream(
1795 &outputStream,
1796 *output,
1797 devices,
1798 flags,
1799 config,
1800 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001801
1802 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001803
Phil Burk062e67a2015-02-11 13:40:50 -08001804 if (status == NO_ERROR) {
Eric Laurent83b88082014-06-20 18:31:16 -07001805
1806 PlaybackThread *thread;
1807 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001808 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001809 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001810 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1811 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001812 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001813 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001814 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001815 } else {
Eric Laurent72e3f392015-05-20 14:43:50 -07001816 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001817 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001818 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001819 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001820 return thread;
1821 }
1822
1823 return 0;
1824}
1825
Eric Laurentcf2c0212014-07-25 16:20:43 -07001826status_t AudioFlinger::openOutput(audio_module_handle_t module,
1827 audio_io_handle_t *output,
1828 audio_config_t *config,
1829 audio_devices_t *devices,
1830 const String8& address,
1831 uint32_t *latencyMs,
1832 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001833{
Phil Burk062e67a2015-02-11 13:40:50 -08001834 ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001835 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001836 (devices != NULL) ? *devices : 0,
1837 config->sample_rate,
1838 config->format,
1839 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001840 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001841
Eric Laurentcf2c0212014-07-25 16:20:43 -07001842 if (*devices == AUDIO_DEVICE_NONE) {
1843 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001844 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001845
Mathias Agopian65ab4712010-07-14 17:59:35 -07001846 Mutex::Autolock _l(mLock);
1847
Eric Laurentcf2c0212014-07-25 16:20:43 -07001848 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001849 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001850 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001851
1852 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001853 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001854
1855 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001856 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001857 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001858 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001859
1860 AutoMutex lock(mHardwareLock);
1861 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001862 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001863 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001864 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001865 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001866 }
1867
Eric Laurentcf2c0212014-07-25 16:20:43 -07001868 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001869}
1870
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001871audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1872 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001873{
1874 Mutex::Autolock _l(mLock);
1875 MixerThread *thread1 = checkMixerThread_l(output1);
1876 MixerThread *thread2 = checkMixerThread_l(output2);
1877
1878 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001879 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1880 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001881 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001882 }
1883
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001884 audio_io_handle_t id = nextUniqueId();
Eric Laurent72e3f392015-05-20 14:43:50 -07001885 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001886 thread->addOutputTrack(thread2);
1887 mPlaybackThreads.add(id, thread);
1888 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001889 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001890 return id;
1891}
1892
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001893status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001894{
Glenn Kastend96c5722012-04-25 13:44:49 -07001895 return closeOutput_nonvirtual(output);
1896}
1897
1898status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1899{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001900 // keep strong reference on the playback thread so that
1901 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001902 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001903 {
1904 Mutex::Autolock _l(mLock);
1905 thread = checkPlaybackThread_l(output);
1906 if (thread == NULL) {
1907 return BAD_VALUE;
1908 }
1909
Steve Block3856b092011-10-20 11:56:00 +01001910 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001911
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001912 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001913 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentf6870ae2015-05-08 10:50:03 -07001914 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001915 DuplicatingThread *dupThread =
1916 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001917 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001918 }
1919 }
1920 }
1921
1922
1923 mPlaybackThreads.removeItem(output);
1924 // save all effects to the default thread
1925 if (mPlaybackThreads.size()) {
1926 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1927 if (dstThread != NULL) {
1928 // audioflinger lock is held here so the acquisition order of thread locks does not
1929 // matter
1930 Mutex::Autolock _dl(dstThread->mLock);
1931 Mutex::Autolock _sl(thread->mLock);
1932 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1933 for (size_t i = 0; i < effectChains.size(); i ++) {
1934 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001935 }
1936 }
1937 }
Eric Laurent73e26b62015-04-27 16:55:58 -07001938 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
1939 ioDesc->mIoHandle = output;
1940 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001941 }
1942 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001943 // The thread entity (active unit of execution) is no longer running here,
1944 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001945
Eric Laurentf6870ae2015-05-08 10:50:03 -07001946 if (!thread->isDuplicating()) {
Eric Laurent83b88082014-06-20 18:31:16 -07001947 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001948 }
Eric Laurent83b88082014-06-20 18:31:16 -07001949
Mathias Agopian65ab4712010-07-14 17:59:35 -07001950 return NO_ERROR;
1951}
1952
Eric Laurent83b88082014-06-20 18:31:16 -07001953void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
1954{
1955 AudioStreamOut *out = thread->clearOutput();
1956 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1957 // from now on thread->mOutput is NULL
1958 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
1959 delete out;
1960}
1961
1962void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
1963{
1964 mPlaybackThreads.removeItem(thread->mId);
1965 thread->exit();
1966 closeOutputFinish(thread);
1967}
1968
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001969status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001970{
1971 Mutex::Autolock _l(mLock);
1972 PlaybackThread *thread = checkPlaybackThread_l(output);
1973
1974 if (thread == NULL) {
1975 return BAD_VALUE;
1976 }
1977
Steve Block3856b092011-10-20 11:56:00 +01001978 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001979 thread->suspend();
1980
1981 return NO_ERROR;
1982}
1983
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001984status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001985{
1986 Mutex::Autolock _l(mLock);
1987 PlaybackThread *thread = checkPlaybackThread_l(output);
1988
1989 if (thread == NULL) {
1990 return BAD_VALUE;
1991 }
1992
Steve Block3856b092011-10-20 11:56:00 +01001993 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001994
1995 thread->restore();
1996
1997 return NO_ERROR;
1998}
1999
Eric Laurentcf2c0212014-07-25 16:20:43 -07002000status_t AudioFlinger::openInput(audio_module_handle_t module,
2001 audio_io_handle_t *input,
2002 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002003 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002004 const String8& address,
2005 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002006 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002007{
Eric Laurent83b88082014-06-20 18:31:16 -07002008 Mutex::Autolock _l(mLock);
2009
Glenn Kastene7d66712015-03-05 13:46:52 -08002010 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002011 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002012 }
2013
Glenn Kastene7d66712015-03-05 13:46:52 -08002014 sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002015
2016 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002017 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002018 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002019 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002020 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002021 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002022}
Dima Zavin799a70e2011-04-18 16:57:27 -07002023
Eric Laurent83b88082014-06-20 18:31:16 -07002024sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002025 audio_io_handle_t *input,
2026 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002027 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002028 const String8& address,
2029 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002030 audio_input_flags_t flags)
2031{
Glenn Kastene7d66712015-03-05 13:46:52 -08002032 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002033 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002034 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002035 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002036 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002037
Eric Laurentcf2c0212014-07-25 16:20:43 -07002038 if (*input == AUDIO_IO_HANDLE_NONE) {
2039 *input = nextUniqueId();
2040 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002041
Eric Laurentcf2c0212014-07-25 16:20:43 -07002042 audio_config_t halconfig = *config;
2043 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07002044 audio_stream_in_t *inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002045 status_t status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002046 &inStream, flags, address.string(), source);
2047 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002048 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Dima Zavin799a70e2011-04-18 16:57:27 -07002049 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002050 halconfig.sample_rate,
2051 halconfig.format,
2052 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002053 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002054 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002055
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002056 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002057 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002058 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002059 audio_is_linear_pcm(config->format) &&
2060 audio_is_linear_pcm(halconfig.format) &&
2061 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
Eric Laurentcf2c0212014-07-25 16:20:43 -07002062 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
2063 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002064 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002065 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002066 inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002067 status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002068 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07002069 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002070 }
2071
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002072 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002073
Glenn Kasten46909e72013-02-26 09:20:22 -08002074#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002075 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2076 // or (re-)create if current Pipe is idle and does not match the new format
2077 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002078 enum {
2079 TEE_SINK_NO, // don't copy input
2080 TEE_SINK_NEW, // copy input using a new pipe
2081 TEE_SINK_OLD, // copy input using an existing pipe
2082 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002083 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2084 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002085 if (!mTeeSinkInputEnabled) {
2086 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002087 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002088 kind = TEE_SINK_NO;
2089 } else if (mRecordTeeSink == 0) {
2090 kind = TEE_SINK_NEW;
2091 } else if (mRecordTeeSink->getStrongCount() != 1) {
2092 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002093 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002094 kind = TEE_SINK_OLD;
2095 } else {
2096 kind = TEE_SINK_NEW;
2097 }
2098 switch (kind) {
2099 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002100 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002101 size_t numCounterOffers = 0;
2102 const NBAIO_Format offers[1] = {format};
2103 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2104 ALOG_ASSERT(index == 0);
2105 PipeReader *pipeReader = new PipeReader(*pipe);
2106 numCounterOffers = 0;
2107 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2108 ALOG_ASSERT(index == 0);
2109 mRecordTeeSink = pipe;
2110 mRecordTeeSource = pipeReader;
2111 teeSink = pipe;
2112 }
2113 break;
2114 case TEE_SINK_OLD:
2115 teeSink = mRecordTeeSink;
2116 break;
2117 case TEE_SINK_NO:
2118 default:
2119 break;
2120 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002121#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002122
Eric Laurentcf2c0212014-07-25 16:20:43 -07002123 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002124
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002125 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002126 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002127 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002128 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002129 inputStream,
2130 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002131 primaryOutputDevice_l(),
Eric Laurent72e3f392015-05-20 14:43:50 -07002132 devices,
2133 mSystemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08002134#ifdef TEE_SINK
2135 , teeSink
2136#endif
2137 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002138 mRecordThreads.add(*input, thread);
2139 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002140 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002141 }
2142
Eric Laurentcf2c0212014-07-25 16:20:43 -07002143 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002144 return 0;
2145}
2146
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002147status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002148{
Glenn Kastend96c5722012-04-25 13:44:49 -07002149 return closeInput_nonvirtual(input);
2150}
2151
2152status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2153{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002154 // keep strong reference on the record thread so that
2155 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002156 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002157 {
2158 Mutex::Autolock _l(mLock);
2159 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002160 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002161 return BAD_VALUE;
2162 }
2163
Steve Block3856b092011-10-20 11:56:00 +01002164 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002165
2166 // If we still have effect chains, it means that a client still holds a handle
2167 // on at least one effect. We must either move the chain to an existing thread with the
2168 // same session ID or put it aside in case a new record thread is opened for a
2169 // new capture on the same session
2170 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002171 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002172 Mutex::Autolock _sl(thread->mLock);
2173 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002174 // Note: maximum one chain per record thread
2175 if (effectChains.size() != 0) {
2176 chain = effectChains[0];
2177 }
2178 }
2179 if (chain != 0) {
2180 // first check if a record thread is already opened with a client on the same session.
2181 // This should only happen in case of overlap between one thread tear down and the
2182 // creation of its replacement
2183 size_t i;
2184 for (i = 0; i < mRecordThreads.size(); i++) {
2185 sp<RecordThread> t = mRecordThreads.valueAt(i);
2186 if (t == thread) {
2187 continue;
2188 }
2189 if (t->hasAudioSession(chain->sessionId()) != 0) {
2190 Mutex::Autolock _l(t->mLock);
2191 ALOGV("closeInput() found thread %d for effect session %d",
2192 t->id(), chain->sessionId());
2193 t->addEffectChain_l(chain);
2194 break;
2195 }
2196 }
2197 // put the chain aside if we could not find a record thread with the same session id.
2198 if (i == mRecordThreads.size()) {
2199 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002200 }
2201 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002202 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2203 ioDesc->mIoHandle = input;
2204 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002205 mRecordThreads.removeItem(input);
2206 }
Eric Laurent83b88082014-06-20 18:31:16 -07002207 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2208 // we have a different lock for notification client
2209 closeInputFinish(thread);
2210 return NO_ERROR;
2211}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002212
Eric Laurent83b88082014-06-20 18:31:16 -07002213void AudioFlinger::closeInputFinish(sp<RecordThread> thread)
2214{
2215 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002216 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002217 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002218 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002219 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002220 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002221}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002222
Eric Laurent83b88082014-06-20 18:31:16 -07002223void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
2224{
2225 mRecordThreads.removeItem(thread->mId);
2226 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002227}
2228
Glenn Kastend2304db2014-02-03 07:40:31 -08002229status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002230{
2231 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002232 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002233
2234 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2235 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002236 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002237 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002238
2239 return NO_ERROR;
2240}
2241
2242
Eric Laurentde3f8392014-07-27 18:38:22 -07002243audio_unique_id_t AudioFlinger::newAudioUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002244{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002245 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002246}
2247
Marco Nelissend457c972014-02-11 08:47:07 -08002248void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002249{
2250 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002251 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002252 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2253 if (pid != -1 && (caller == getpid_cached)) {
2254 caller = pid;
2255 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002256
Eric Laurent021cf962014-05-13 10:18:14 -07002257 {
2258 Mutex::Autolock _cl(mClientLock);
2259 // Ignore requests received from processes not known as notification client. The request
2260 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2261 // called from a different pid leaving a stale session reference. Also we don't know how
2262 // to clear this reference if the client process dies.
2263 if (mNotificationClients.indexOfKey(caller) < 0) {
2264 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2265 return;
2266 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002267 }
2268
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002269 size_t num = mAudioSessionRefs.size();
2270 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002271 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002272 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2273 ref->mCnt++;
2274 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002275 return;
2276 }
2277 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002278 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2279 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002280}
2281
Marco Nelissend457c972014-02-11 08:47:07 -08002282void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002283{
2284 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002285 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002286 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2287 if (pid != -1 && (caller == getpid_cached)) {
2288 caller = pid;
2289 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002290 size_t num = mAudioSessionRefs.size();
2291 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002292 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002293 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2294 ref->mCnt--;
2295 ALOGV(" decremented refcount to %d", ref->mCnt);
2296 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002297 mAudioSessionRefs.removeAt(i);
2298 delete ref;
2299 purgeStaleEffects_l();
2300 }
2301 return;
2302 }
2303 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002304 // If the caller is mediaserver it is likely that the session being released was acquired
2305 // on behalf of a process not in notification clients and we ignore the warning.
2306 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002307}
2308
2309void AudioFlinger::purgeStaleEffects_l() {
2310
Steve Block3856b092011-10-20 11:56:00 +01002311 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002312
2313 Vector< sp<EffectChain> > chains;
2314
2315 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2316 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2317 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2318 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002319 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2320 chains.push(ec);
2321 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002322 }
2323 }
2324 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2325 sp<RecordThread> t = mRecordThreads.valueAt(i);
2326 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2327 sp<EffectChain> ec = t->mEffectChains[j];
2328 chains.push(ec);
2329 }
2330 }
2331
2332 for (size_t i = 0; i < chains.size(); i++) {
2333 sp<EffectChain> ec = chains[i];
2334 int sessionid = ec->sessionId();
2335 sp<ThreadBase> t = ec->mThread.promote();
2336 if (t == 0) {
2337 continue;
2338 }
2339 size_t numsessionrefs = mAudioSessionRefs.size();
2340 bool found = false;
2341 for (size_t k = 0; k < numsessionrefs; k++) {
2342 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002343 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002344 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002345 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002346 found = true;
2347 break;
2348 }
2349 }
2350 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002351 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002352 // remove all effects from the chain
2353 while (ec->mEffects.size()) {
2354 sp<EffectModule> effect = ec->mEffects[0];
2355 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002356 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002357 if (effect->purgeHandles()) {
2358 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002359 }
2360 AudioSystem::unregisterEffect(effect->id());
2361 }
2362 }
2363 }
2364 return;
2365}
2366
Mathias Agopian65ab4712010-07-14 17:59:35 -07002367// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002368AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002369{
Glenn Kastena1117922012-01-26 10:53:32 -08002370 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002371}
2372
2373// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002374AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002375{
2376 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002377 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002378}
2379
2380// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002381AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002382{
Glenn Kastena1117922012-01-26 10:53:32 -08002383 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002384}
2385
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002386uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002387{
Glenn Kastenbcefec32014-01-17 12:09:05 -08002388 return (uint32_t) android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002389}
2390
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002391AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002392{
2393 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2394 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002395 if(thread->isDuplicating()) {
2396 continue;
2397 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002398 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002399 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002400 return thread;
2401 }
2402 }
2403 return NULL;
2404}
2405
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002406audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002407{
2408 PlaybackThread *thread = primaryPlaybackThread_l();
2409
2410 if (thread == NULL) {
2411 return 0;
2412 }
2413
Eric Laurentf1c04f92012-08-28 14:26:53 -07002414 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002415}
2416
Eric Laurenta011e352012-03-29 15:51:43 -07002417sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2418 int triggerSession,
2419 int listenerSession,
2420 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002421 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002422{
2423 Mutex::Autolock _l(mLock);
2424
2425 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2426 status_t playStatus = NAME_NOT_FOUND;
2427 status_t recStatus = NAME_NOT_FOUND;
2428 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2429 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2430 if (playStatus == NO_ERROR) {
2431 return event;
2432 }
2433 }
2434 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2435 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2436 if (recStatus == NO_ERROR) {
2437 return event;
2438 }
2439 }
2440 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2441 mPendingSyncEvents.add(event);
2442 } else {
2443 ALOGV("createSyncEvent() invalid event %d", event->type());
2444 event.clear();
2445 }
2446 return event;
2447}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002448
Mathias Agopian65ab4712010-07-14 17:59:35 -07002449// ----------------------------------------------------------------------------
2450// Effect management
2451// ----------------------------------------------------------------------------
2452
2453
Glenn Kastenf587ba52012-01-26 16:25:10 -08002454status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002455{
2456 Mutex::Autolock _l(mLock);
2457 return EffectQueryNumberEffects(numEffects);
2458}
2459
Glenn Kastenf587ba52012-01-26 16:25:10 -08002460status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002461{
2462 Mutex::Autolock _l(mLock);
2463 return EffectQueryEffect(index, descriptor);
2464}
2465
Glenn Kasten5e92a782012-01-30 07:40:52 -08002466status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002467 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002468{
2469 Mutex::Autolock _l(mLock);
2470 return EffectGetDescriptor(pUuid, descriptor);
2471}
2472
2473
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002474sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002475 effect_descriptor_t *pDesc,
2476 const sp<IEffectClient>& effectClient,
2477 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002478 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002479 int sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002480 const String16& opPackageName,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002481 status_t *status,
2482 int *id,
2483 int *enabled)
2484{
2485 status_t lStatus = NO_ERROR;
2486 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002487 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002488
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002489 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002490 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002491 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002492
2493 if (pDesc == NULL) {
2494 lStatus = BAD_VALUE;
2495 goto Exit;
2496 }
2497
Eric Laurent84e9a102010-09-23 16:10:16 -07002498 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002499 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002500 lStatus = PERMISSION_DENIED;
2501 goto Exit;
2502 }
2503
Dima Zavinfce7a472011-04-19 22:30:36 -07002504 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002505 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002506 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002507 lStatus = PERMISSION_DENIED;
2508 goto Exit;
2509 }
2510
Mathias Agopian65ab4712010-07-14 17:59:35 -07002511 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002512 if (!EffectIsNullUuid(&pDesc->uuid)) {
2513 // if uuid is specified, request effect descriptor
2514 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2515 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002516 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002517 goto Exit;
2518 }
2519 } else {
2520 // if uuid is not specified, look for an available implementation
2521 // of the required type in effect factory
2522 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002523 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002524 lStatus = BAD_VALUE;
2525 goto Exit;
2526 }
2527 uint32_t numEffects = 0;
2528 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002529 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002530 bool found = false;
2531
2532 lStatus = EffectQueryNumberEffects(&numEffects);
2533 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002534 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002535 goto Exit;
2536 }
2537 for (uint32_t i = 0; i < numEffects; i++) {
2538 lStatus = EffectQueryEffect(i, &desc);
2539 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002540 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002541 continue;
2542 }
2543 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2544 // If matching type found save effect descriptor. If the session is
2545 // 0 and the effect is not auxiliary, continue enumeration in case
2546 // an auxiliary version of this effect type is available
2547 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002548 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002549 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002550 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2551 break;
2552 }
2553 }
2554 }
2555 if (!found) {
2556 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002557 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002558 goto Exit;
2559 }
2560 // For same effect type, chose auxiliary version over insert version if
2561 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002562 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002563 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002564 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002565 }
2566 }
2567
2568 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002569 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002570 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2571 lStatus = INVALID_OPERATION;
2572 goto Exit;
2573 }
2574
Eric Laurent59255e42011-07-27 19:49:51 -07002575 // check recording permission for visualizer
2576 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Marco Nelissendcb346b2015-09-09 10:47:29 -07002577 !recordingAllowed(opPackageName, pid, IPCThreadState::self()->getCallingUid())) {
Eric Laurent59255e42011-07-27 19:49:51 -07002578 lStatus = PERMISSION_DENIED;
2579 goto Exit;
2580 }
2581
Mathias Agopian65ab4712010-07-14 17:59:35 -07002582 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002583 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002584 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002585 // if the output returned by getOutputForEffect() is removed before we lock the
2586 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2587 // and we will exit safely
2588 io = AudioSystem::getOutputForEffect(&desc);
2589 ALOGV("createEffect got output %d", io);
2590 }
2591
2592 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002593
2594 // If output is not specified try to find a matching audio session ID in one of the
2595 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002596 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2597 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002598 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002599 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002600 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2601 // output must be specified by AudioPolicyManager when using session
2602 // AUDIO_SESSION_OUTPUT_STAGE
2603 lStatus = BAD_VALUE;
2604 goto Exit;
2605 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002606 // look for the thread where the specified audio session is present
2607 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2608 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2609 io = mPlaybackThreads.keyAt(i);
2610 break;
2611 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002612 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002613 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002614 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2615 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2616 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002617 break;
2618 }
2619 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002620 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002621 // If no output thread contains the requested session ID, default to
2622 // first output. The effect chain will be moved to the correct output
2623 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002624 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002625 io = mPlaybackThreads.keyAt(0);
2626 }
Steve Block3856b092011-10-20 11:56:00 +01002627 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002628 }
2629 ThreadBase *thread = checkRecordThread_l(io);
2630 if (thread == NULL) {
2631 thread = checkPlaybackThread_l(io);
2632 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002633 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002634 lStatus = BAD_VALUE;
2635 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002636 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002637 } else {
2638 // Check if one effect chain was awaiting for an effect to be created on this
2639 // session and used it instead of creating a new one.
2640 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)sessionId);
2641 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002642 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002643 thread->addEffectChain_l(chain);
2644 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002645 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002646
Eric Laurent021cf962014-05-13 10:18:14 -07002647 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002648
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002649 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002650 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2651 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002652 if (handle != 0 && id != NULL) {
2653 *id = handle->id();
2654 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002655 if (handle == 0) {
2656 // remove local strong reference to Client with mClientLock held
2657 Mutex::Autolock _cl(mClientLock);
2658 client.clear();
2659 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002660 }
2661
2662Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002663 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002664 return handle;
2665}
2666
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002667status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2668 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002669{
Steve Block3856b092011-10-20 11:56:00 +01002670 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002671 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002672 Mutex::Autolock _l(mLock);
2673 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002674 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002675 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002676 }
Eric Laurentde070132010-07-13 04:45:46 -07002677 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2678 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002679 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002680 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002681 }
Eric Laurentde070132010-07-13 04:45:46 -07002682 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2683 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002684 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002685 return BAD_VALUE;
2686 }
2687
2688 Mutex::Autolock _dl(dstThread->mLock);
2689 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002690 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002691}
2692
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002693// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002694status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002695 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002696 AudioFlinger::PlaybackThread *dstThread,
2697 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002698{
Steve Block3856b092011-10-20 11:56:00 +01002699 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002700 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002701
Eric Laurent59255e42011-07-27 19:49:51 -07002702 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002703 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002704 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002705 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002706 return INVALID_OPERATION;
2707 }
2708
Andy Hung9a592762014-07-21 21:56:01 -07002709 // Check whether the destination thread has a channel count of FCC_2, which is
2710 // currently required for (most) effects. Prevent moving the effect chain here rather
2711 // than disabling the addEffect_l() call in dstThread below.
Eric Laurentf6870ae2015-05-08 10:50:03 -07002712 if ((dstThread->type() == ThreadBase::MIXER || dstThread->isDuplicating()) &&
Eric Laurentb10352f2014-11-03 16:25:39 -08002713 dstThread->mChannelCount != FCC_2) {
Andy Hung9a592762014-07-21 21:56:01 -07002714 ALOGW("moveEffectChain_l() effect chain failed because"
2715 " destination thread %p channel count(%u) != %u",
2716 dstThread, dstThread->mChannelCount, FCC_2);
2717 return INVALID_OPERATION;
2718 }
2719
Eric Laurent39e94f82010-07-28 01:32:47 -07002720 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002721 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002722 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002723 // removed.
2724 srcThread->removeEffectChain_l(chain);
2725
2726 // transfer all effects one by one so that new effect chain is created on new thread with
2727 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002728 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002729 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002730 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002731 Vector< sp<EffectModule> > removed;
2732 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002733 while (effect != 0) {
2734 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002735 removed.add(effect);
2736 status = dstThread->addEffect_l(effect);
2737 if (status != NO_ERROR) {
2738 break;
2739 }
Eric Laurentec35a142011-10-05 17:42:25 -07002740 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2741 if (effect->state() == EffectModule::ACTIVE ||
2742 effect->state() == EffectModule::STOPPING) {
2743 effect->start();
2744 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002745 // if the move request is not received from audio policy manager, the effect must be
2746 // re-registered with the new strategy and output
2747 if (dstChain == 0) {
2748 dstChain = effect->chain().promote();
2749 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002750 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002751 status = NO_INIT;
2752 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002753 }
2754 strategy = dstChain->strategy();
2755 }
2756 if (reRegister) {
2757 AudioSystem::unregisterEffect(effect->id());
2758 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002759 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002760 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002761 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002762 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002763 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002764 }
Eric Laurentde070132010-07-13 04:45:46 -07002765 effect = chain->getEffectFromId_l(0);
2766 }
2767
Eric Laurent5baf2af2013-09-12 17:37:00 -07002768 if (status != NO_ERROR) {
2769 for (size_t i = 0; i < removed.size(); i++) {
2770 srcThread->addEffect_l(removed[i]);
2771 if (dstChain != 0 && reRegister) {
2772 AudioSystem::unregisterEffect(removed[i]->id());
2773 AudioSystem::registerEffect(&removed[i]->desc(),
2774 srcThread->id(),
2775 strategy,
2776 sessionId,
2777 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002778 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002779 }
2780 }
2781 }
2782
2783 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002784}
2785
Eric Laurent5baf2af2013-09-12 17:37:00 -07002786bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002787{
2788 if (mGlobalEffectEnableTime != 0 &&
2789 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2790 return true;
2791 }
2792
2793 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2794 sp<EffectChain> ec =
2795 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002796 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002797 return true;
2798 }
2799 }
2800 return false;
2801}
2802
Eric Laurent5baf2af2013-09-12 17:37:00 -07002803void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002804{
2805 Mutex::Autolock _l(mLock);
2806
2807 mGlobalEffectEnableTime = systemTime();
2808
2809 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2810 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2811 if (t->mType == ThreadBase::OFFLOAD) {
2812 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2813 }
2814 }
2815
2816}
2817
Eric Laurentaaa44472014-09-12 17:41:50 -07002818status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2819{
2820 audio_session_t session = (audio_session_t)chain->sessionId();
2821 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2822 ALOGV("putOrphanEffectChain_l session %d index %d", session, index);
2823 if (index >= 0) {
2824 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2825 return ALREADY_EXISTS;
2826 }
2827 mOrphanEffectChains.add(session, chain);
2828 return NO_ERROR;
2829}
2830
2831sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2832{
2833 sp<EffectChain> chain;
2834 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2835 ALOGV("getOrphanEffectChain_l session %d index %d", session, index);
2836 if (index >= 0) {
2837 chain = mOrphanEffectChains.valueAt(index);
2838 mOrphanEffectChains.removeItemsAt(index);
2839 }
2840 return chain;
2841}
2842
2843bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2844{
2845 Mutex::Autolock _l(mLock);
2846 audio_session_t session = (audio_session_t)effect->sessionId();
2847 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2848 ALOGV("updateOrphanEffectChains session %d index %d", session, index);
2849 if (index >= 0) {
2850 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2851 if (chain->removeEffect_l(effect) == 0) {
2852 ALOGV("updateOrphanEffectChains removing effect chain at index %d", index);
2853 mOrphanEffectChains.removeItemsAt(index);
2854 }
2855 return true;
2856 }
2857 return false;
2858}
2859
2860
Glenn Kastenda6ef132013-01-10 12:31:01 -08002861struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002862#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
2863 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002864};
2865
2866int comparEntry(const void *p1, const void *p2)
2867{
Glenn Kasten2f55e762015-03-05 16:05:08 -08002868 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002869}
2870
Glenn Kasten46909e72013-02-26 09:20:22 -08002871#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002872void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002873{
Eric Laurent81784c32012-11-19 14:55:58 -08002874 NBAIO_Source *teeSource = source.get();
2875 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002876 // .wav rotation
2877 // There is a benign race condition if 2 threads call this simultaneously.
2878 // They would both traverse the directory, but the result would simply be
2879 // failures at unlink() which are ignored. It's also unlikely since
2880 // normally dumpsys is only done by bugreport or from the command line.
2881 char teePath[32+256];
Glenn Kasten9a003992016-02-23 15:24:34 -08002882 strcpy(teePath, "/data/misc/audioserver");
Glenn Kastenda6ef132013-01-10 12:31:01 -08002883 size_t teePathLen = strlen(teePath);
2884 DIR *dir = opendir(teePath);
2885 teePath[teePathLen++] = '/';
2886 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002887#define TEE_MAX_SORT 20 // number of entries to sort
2888#define TEE_MAX_KEEP 10 // number of entries to keep
2889 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002890 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08002891 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002892 struct dirent de;
2893 struct dirent *result = NULL;
2894 int rc = readdir_r(dir, &de, &result);
2895 if (rc != 0) {
2896 ALOGW("readdir_r failed %d", rc);
2897 break;
2898 }
2899 if (result == NULL) {
2900 break;
2901 }
2902 if (result != &de) {
2903 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2904 break;
2905 }
2906 // ignore non .wav file entries
2907 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002908 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08002909 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2910 continue;
2911 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08002912 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002913 }
2914 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002915 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002916 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002917 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
2918 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002919 (void) unlink(teePath);
2920 }
2921 }
2922 } else {
2923 if (fd >= 0) {
Glenn Kasten9a003992016-02-23 15:24:34 -08002924 dprintf(fd, "unable to rotate tees in %.*s: %s\n", teePathLen, teePath,
2925 strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002926 }
2927 }
Eric Laurent81784c32012-11-19 14:55:58 -08002928 char teeTime[16];
2929 struct timeval tv;
2930 gettimeofday(&tv, NULL);
2931 struct tm tm;
2932 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002933 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2934 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2935 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2936 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002937 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07002938 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08002939 char wavHeader[44];
2940 memcpy(wavHeader,
2941 "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",
2942 sizeof(wavHeader));
2943 NBAIO_Format format = teeSource->format();
2944 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002945 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07002946 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002947 wavHeader[22] = channelCount; // number of channels
2948 wavHeader[24] = sampleRate; // sample rate
2949 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07002950 wavHeader[32] = frameSize; // block alignment
2951 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08002952 write(teeFd, wavHeader, sizeof(wavHeader));
2953 size_t total = 0;
2954 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07002955#define TEE_SINK_READ 1024 // frames per I/O operation
2956 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002957 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08002958 size_t count = TEE_SINK_READ;
Glenn Kastend79072e2016-01-06 08:41:20 -08002959 ssize_t actual = teeSource->read(buffer, count);
Eric Laurent81784c32012-11-19 14:55:58 -08002960 bool wasFirstRead = firstRead;
2961 firstRead = false;
2962 if (actual <= 0) {
2963 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2964 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002965 }
Eric Laurent81784c32012-11-19 14:55:58 -08002966 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002967 }
Eric Laurent81784c32012-11-19 14:55:58 -08002968 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07002969 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002970 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002971 }
Glenn Kasten329f6512014-08-28 16:23:16 -07002972 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08002973 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002974 uint32_t temp = 44 + total * frameSize - 8;
2975 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002976 write(teeFd, &temp, sizeof(temp));
2977 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002978 temp = total * frameSize;
2979 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002980 write(teeFd, &temp, sizeof(temp));
2981 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002982 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002983 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002984 }
Eric Laurent59255e42011-07-27 19:49:51 -07002985 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002986 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002987 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002988 }
Eric Laurent59255e42011-07-27 19:49:51 -07002989 }
2990 }
2991}
Glenn Kasten46909e72013-02-26 09:20:22 -08002992#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002993
Mathias Agopian65ab4712010-07-14 17:59:35 -07002994// ----------------------------------------------------------------------------
2995
2996status_t AudioFlinger::onTransact(
2997 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2998{
2999 return BnAudioFlinger::onTransact(code, data, reply, flags);
3000}
3001
Glenn Kasten63238ef2015-03-02 15:50:29 -08003002} // namespace android