blob: 9db313e3d8c8e6b1f2390dd42d8418b9c51f7c15 [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
Mathias Agopian65ab4712010-07-14 17:59:35 -070048#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070049#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070050#include <audio_effects/effect_ns.h>
51#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Glenn Kasten3b21c502011-12-15 09:52:39 -080053#include <audio_utils/primitives.h>
54
Eric Laurentfeb0db62011-07-22 09:04:31 -070055#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080056
John Grossman4ff14ba2012-02-08 16:37:41 -080057#include <common_time/cc_helper.h>
Glenn Kasten58912562012-04-03 10:45:00 -070058
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>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070064#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080065
Mathias Agopian65ab4712010-07-14 17:59:35 -070066// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070067
John Grossman1c345192012-03-27 14:00:17 -070068// Note: the following macro is used for extremely verbose logging message. In
69// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
70// 0; but one side effect of this is to turn all LOGV's as well. Some messages
71// are so verbose that we want to suppress them even when we have ALOG_ASSERT
72// turned on. Do not uncomment the #def below unless you really know what you
73// are doing and want to see all of the extremely verbose messages.
74//#define VERY_VERY_VERBOSE_LOGGING
75#ifdef VERY_VERY_VERBOSE_LOGGING
76#define ALOGVV ALOGV
77#else
78#define ALOGVV(a...) do { } while(0)
79#endif
Eric Laurentde070132010-07-13 04:45:46 -070080
Mathias Agopian65ab4712010-07-14 17:59:35 -070081namespace android {
82
Glenn Kastenec1d6b52011-12-12 09:04:45 -080083static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
84static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Eric Laurent021cf962014-05-13 10:18:14 -070085static const char kClientLockedString[] = "Client lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070086
Glenn Kasten58912562012-04-03 10:45:00 -070087
John Grossman4ff14ba2012-02-08 16:37:41 -080088nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080089
Eric Laurent81784c32012-11-19 14:55:58 -080090uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070091
Glenn Kasten46909e72013-02-26 09:20:22 -080092#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080093bool AudioFlinger::mTeeSinkInputEnabled = false;
94bool AudioFlinger::mTeeSinkOutputEnabled = false;
95bool AudioFlinger::mTeeSinkTrackEnabled = false;
96
97size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
98size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
99size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800100#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800101
Eric Laurent813e2a72013-08-31 12:59:48 -0700102// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
103// we define a minimum time during which a global effect is considered enabled.
104static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
105
Mathias Agopian65ab4712010-07-14 17:59:35 -0700106// ----------------------------------------------------------------------------
107
Marco Nelissenb2208842014-02-07 14:00:50 -0800108const char *formatToString(audio_format_t format) {
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530109 switch (format & AUDIO_FORMAT_MAIN_MASK) {
110 case AUDIO_FORMAT_PCM:
111 switch (format) {
112 case AUDIO_FORMAT_PCM_16_BIT: return "pcm16";
113 case AUDIO_FORMAT_PCM_8_BIT: return "pcm8";
114 case AUDIO_FORMAT_PCM_32_BIT: return "pcm32";
115 case AUDIO_FORMAT_PCM_8_24_BIT: return "pcm8.24";
116 case AUDIO_FORMAT_PCM_FLOAT: return "pcmfloat";
117 case AUDIO_FORMAT_PCM_24_BIT_PACKED: return "pcm24";
118 default:
119 break;
120 }
121 break;
Marco Nelissenb2208842014-02-07 14:00:50 -0800122 case AUDIO_FORMAT_MP3: return "mp3";
123 case AUDIO_FORMAT_AMR_NB: return "amr-nb";
124 case AUDIO_FORMAT_AMR_WB: return "amr-wb";
125 case AUDIO_FORMAT_AAC: return "aac";
126 case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
127 case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
128 case AUDIO_FORMAT_VORBIS: return "vorbis";
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530129 case AUDIO_FORMAT_OPUS: return "opus";
130 case AUDIO_FORMAT_AC3: return "ac-3";
131 case AUDIO_FORMAT_E_AC3: return "e-ac-3";
Marco Nelissenb2208842014-02-07 14:00:50 -0800132 default:
133 break;
134 }
135 return "unknown";
136}
137
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700138static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700139{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700140 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700141 int rc;
142
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700143 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
144 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
145 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
146 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700147 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700148 }
149 rc = audio_hw_device_open(mod, dev);
150 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
151 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
152 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700153 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700154 }
Eric Laurent5806b352014-05-22 12:23:26 -0700155 if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700156 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
157 rc = BAD_VALUE;
158 goto out;
159 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700160 return 0;
161
162out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700163 *dev = NULL;
164 return rc;
165}
166
Mathias Agopian65ab4712010-07-14 17:59:35 -0700167// ----------------------------------------------------------------------------
168
169AudioFlinger::AudioFlinger()
170 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800171 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800172 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700173 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800174 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800175 mMasterMute(false),
176 mNextUniqueId(1),
177 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700178 mBtNrecIsOff(false),
179 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700180 mIsDeviceTypeKnown(false),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700181 mGlobalEffectEnableTime(0),
182 mPrimaryOutputSampleRate(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700183{
Glenn Kasten949a9262013-04-16 12:35:20 -0700184 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800185 char value[PROPERTY_VALUE_MAX];
186 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
187 if (doLog) {
Glenn Kastencf515ab2014-03-14 16:01:58 -0700188 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters", MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800189 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700190
Glenn Kasten46909e72013-02-26 09:20:22 -0800191#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800192 (void) property_get("ro.debuggable", value, "0");
193 int debuggable = atoi(value);
194 int teeEnabled = 0;
195 if (debuggable) {
196 (void) property_get("af.tee", value, "0");
197 teeEnabled = atoi(value);
198 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800199 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700200 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800201 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700202 }
203 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800204 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700205 }
206 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800207 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700208 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800209#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700210}
211
212void AudioFlinger::onFirstRef()
213{
Dima Zavin799a70e2011-04-18 16:57:27 -0700214 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700215
Eric Laurent93575202011-01-18 18:39:02 -0800216 Mutex::Autolock _l(mLock);
217
Dima Zavin799a70e2011-04-18 16:57:27 -0700218 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800219 char val_str[PROPERTY_VALUE_MAX] = { 0 };
220 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
221 uint32_t int_val;
222 if (1 == sscanf(val_str, "%u", &int_val)) {
223 mStandbyTimeInNsecs = milliseconds(int_val);
224 ALOGI("Using %u mSec as standby time.", int_val);
225 } else {
226 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
227 ALOGI("Using default %u mSec as standby time.",
228 (uint32_t)(mStandbyTimeInNsecs / 1000000));
229 }
230 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700231
Eric Laurent1c333e22014-05-20 10:48:17 -0700232 mPatchPanel = new PatchPanel(this);
233
Eric Laurenta4c5a552012-03-29 10:12:40 -0700234 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700235}
236
237AudioFlinger::~AudioFlinger()
238{
239 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700240 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700241 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242 }
243 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700244 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700245 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700246 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700247
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800248 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
249 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700250 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
251 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700252 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700253
254 // Tell media.log service about any old writers that still need to be unregistered
255 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
256 if (binder != 0) {
257 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
258 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
259 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
260 mUnregisteredWriters.pop();
261 mediaLogService->unregisterWriter(iMemory);
262 }
263 }
264
Mathias Agopian65ab4712010-07-14 17:59:35 -0700265}
266
Eric Laurenta4c5a552012-03-29 10:12:40 -0700267static const char * const audio_interfaces[] = {
268 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
269 AUDIO_HARDWARE_MODULE_ID_A2DP,
270 AUDIO_HARDWARE_MODULE_ID_USB,
271};
272#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
273
John Grossmanee578c02012-07-23 17:05:46 -0700274AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
275 audio_module_handle_t module,
276 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700277{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700278 // if module is 0, the request comes from an old policy manager and we should load
279 // well known modules
280 if (module == 0) {
281 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
282 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
283 loadHwModule_l(audio_interfaces[i]);
284 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700285 // then try to find a module supporting the requested device.
286 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
287 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
288 audio_hw_device_t *dev = audioHwDevice->hwDevice();
289 if ((dev->get_supported_devices != NULL) &&
290 (dev->get_supported_devices(dev) & devices) == devices)
291 return audioHwDevice;
292 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700293 } else {
294 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700295 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
296 if (audioHwDevice != NULL) {
297 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700298 }
299 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700300
Dima Zavin799a70e2011-04-18 16:57:27 -0700301 return NULL;
302}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700303
Glenn Kasten0f11b512014-01-31 16:18:54 -0800304void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305{
306 const size_t SIZE = 256;
307 char buffer[SIZE];
308 String8 result;
309
310 result.append("Clients:\n");
311 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800312 sp<Client> client = mClients.valueAt(i).promote();
313 if (client != 0) {
314 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
315 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316 }
317 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700318
Eric Laurentd1b28d42013-09-18 18:47:13 -0700319 result.append("Notification Clients:\n");
320 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
321 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
322 result.append(buffer);
323 }
324
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700325 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800326 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700327 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
328 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800329 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700330 result.append(buffer);
331 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700332 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700333}
334
335
Glenn Kasten0f11b512014-01-31 16:18:54 -0800336void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700337{
338 const size_t SIZE = 256;
339 char buffer[SIZE];
340 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800341 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700342
John Grossman4ff14ba2012-02-08 16:37:41 -0800343 snprintf(buffer, SIZE, "Hardware status: %d\n"
344 "Standby Time mSec: %u\n",
345 hardwareStatus,
346 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700347 result.append(buffer);
348 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349}
350
Glenn Kasten0f11b512014-01-31 16:18:54 -0800351void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700352{
353 const size_t SIZE = 256;
354 char buffer[SIZE];
355 String8 result;
356 snprintf(buffer, SIZE, "Permission Denial: "
357 "can't dump AudioFlinger from pid=%d, uid=%d\n",
358 IPCThreadState::self()->getCallingPid(),
359 IPCThreadState::self()->getCallingUid());
360 result.append(buffer);
361 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362}
363
Eric Laurent81784c32012-11-19 14:55:58 -0800364bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365{
366 bool locked = false;
367 for (int i = 0; i < kDumpLockRetries; ++i) {
368 if (mutex.tryLock() == NO_ERROR) {
369 locked = true;
370 break;
371 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800372 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373 }
374 return locked;
375}
376
377status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
378{
Glenn Kasten44deb052012-02-05 18:09:08 -0800379 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700380 dumpPermissionDenial(fd, args);
381 } else {
382 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800383 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384 if (!hardwareLocked) {
385 String8 result(kHardwareLockedString);
386 write(fd, result.string(), result.size());
387 } else {
388 mHardwareLock.unlock();
389 }
390
Eric Laurent81784c32012-11-19 14:55:58 -0800391 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392
393 // failed to lock - AudioFlinger is probably deadlocked
394 if (!locked) {
395 String8 result(kDeadlockedString);
396 write(fd, result.string(), result.size());
397 }
398
Eric Laurent021cf962014-05-13 10:18:14 -0700399 bool clientLocked = dumpTryLock(mClientLock);
400 if (!clientLocked) {
401 String8 result(kClientLockedString);
402 write(fd, result.string(), result.size());
403 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700404
405 EffectDumpEffects(fd);
406
Mathias Agopian65ab4712010-07-14 17:59:35 -0700407 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700408 if (clientLocked) {
409 mClientLock.unlock();
410 }
411
Mathias Agopian65ab4712010-07-14 17:59:35 -0700412 dumpInternals(fd, args);
413
414 // dump playback threads
415 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
416 mPlaybackThreads.valueAt(i)->dump(fd, args);
417 }
418
419 // dump record threads
420 for (size_t i = 0; i < mRecordThreads.size(); i++) {
421 mRecordThreads.valueAt(i)->dump(fd, args);
422 }
423
Eric Laurentaaa44472014-09-12 17:41:50 -0700424 // dump orphan effect chains
425 if (mOrphanEffectChains.size() != 0) {
426 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
427 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
428 mOrphanEffectChains.valueAt(i)->dump(fd, args);
429 }
430 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700431 // dump all hardware devs
432 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700433 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700434 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700436
Glenn Kasten46909e72013-02-26 09:20:22 -0800437#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700438 // dump the serially shared record tee sink
439 if (mRecordTeeSource != 0) {
440 dumpTee(fd, mRecordTeeSource);
441 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800442#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700443
Glenn Kastend65d73c2012-06-22 17:21:07 -0700444 if (locked) {
445 mLock.unlock();
446 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800447
448 // append a copy of media.log here by forwarding fd to it, but don't attempt
449 // to lookup the service if it's not running, as it will block for a second
450 if (mLogMemoryDealer != 0) {
451 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
452 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700453 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800454 Vector<String16> args;
455 binder->dump(fd, args);
456 }
457 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700458 }
459 return NO_ERROR;
460}
461
Eric Laurent021cf962014-05-13 10:18:14 -0700462sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800463{
Eric Laurent021cf962014-05-13 10:18:14 -0700464 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800465 // If pid is already in the mClients wp<> map, then use that entry
466 // (for which promote() is always != 0), otherwise create a new entry and Client.
467 sp<Client> client = mClients.valueFor(pid).promote();
468 if (client == 0) {
469 client = new Client(this, pid);
470 mClients.add(pid, client);
471 }
472
473 return client;
474}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700475
Glenn Kasten9e58b552013-01-18 15:09:48 -0800476sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
477{
Glenn Kasten481fb672013-09-30 14:39:28 -0700478 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800479 if (mLogMemoryDealer == 0) {
480 return new NBLog::Writer();
481 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800482 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700483 // Similarly if we can't contact the media.log service, also return a dummy writer
484 if (binder == 0) {
485 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800486 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700487 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
488 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
489 // If allocation fails, consult the vector of previously unregistered writers
490 // and garbage-collect one or more them until an allocation succeeds
491 if (shared == 0) {
492 Mutex::Autolock _l(mUnregisteredWritersLock);
493 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
494 {
495 // Pick the oldest stale writer to garbage-collect
496 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
497 mUnregisteredWriters.removeAt(0);
498 mediaLogService->unregisterWriter(iMemory);
499 // Now the media.log remote reference to IMemory is gone. When our last local
500 // reference to IMemory also drops to zero at end of this block,
501 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
502 }
503 // Re-attempt the allocation
504 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
505 if (shared != 0) {
506 goto success;
507 }
508 }
509 // Even after garbage-collecting all old writers, there is still not enough memory,
510 // so return a dummy writer
511 return new NBLog::Writer();
512 }
513success:
514 mediaLogService->registerWriter(shared, size, name);
515 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800516}
517
518void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
519{
Glenn Kasten685ef092013-02-04 08:15:34 -0800520 if (writer == 0) {
521 return;
522 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800523 sp<IMemory> iMemory(writer->getIMemory());
524 if (iMemory == 0) {
525 return;
526 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700527 // Rather than removing the writer immediately, append it to a queue of old writers to
528 // be garbage-collected later. This allows us to continue to view old logs for a while.
529 Mutex::Autolock _l(mUnregisteredWritersLock);
530 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800531}
532
Mathias Agopian65ab4712010-07-14 17:59:35 -0700533// IAudioFlinger interface
534
535
536sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800537 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700538 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800539 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700540 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800541 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800542 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800544 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800545 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700546 int *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800547 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700548 status_t *status)
549{
550 sp<PlaybackThread::Track> track;
551 sp<TrackHandle> trackHandle;
552 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553 status_t lStatus;
554 int lSessionId;
555
Glenn Kasten263709e2012-01-06 08:40:01 -0800556 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
557 // but if someone uses binder directly they could bypass that and cause us to crash
558 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000559 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700560 lStatus = BAD_VALUE;
561 goto Exit;
562 }
563
Glenn Kasten53b5d092014-02-05 10:00:23 -0800564 // further sample rate checks are performed by createTrack_l() depending on the thread type
565 if (sampleRate == 0) {
566 ALOGE("createTrack() invalid sample rate %u", sampleRate);
567 lStatus = BAD_VALUE;
568 goto Exit;
569 }
570
571 // further channel mask checks are performed by createTrack_l() depending on the thread type
572 if (!audio_is_output_channel(channelMask)) {
573 ALOGE("createTrack() invalid channel mask %#x", channelMask);
574 lStatus = BAD_VALUE;
575 goto Exit;
576 }
577
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700578 // further format checks are performed by createTrack_l() depending on the thread type
579 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800580 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700581 lStatus = BAD_VALUE;
582 goto Exit;
583 }
584
Glenn Kasten663c2242013-09-24 11:52:37 -0700585 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
586 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
587 lStatus = BAD_VALUE;
588 goto Exit;
589 }
590
Mathias Agopian65ab4712010-07-14 17:59:35 -0700591 {
592 Mutex::Autolock _l(mLock);
593 PlaybackThread *thread = checkPlaybackThread_l(output);
594 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800595 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700596 lStatus = BAD_VALUE;
597 goto Exit;
598 }
599
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800600 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -0700601 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700602
Glenn Kastene848bd92014-03-13 15:00:32 -0700603 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700604 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700605 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700606 // check if an effect chain with the same session ID is present on another
607 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700608 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700609 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
610 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700611 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700612 if (sessions & PlaybackThread::EFFECT_SESSION) {
613 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700614 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700615 }
Eric Laurentde070132010-07-13 04:45:46 -0700616 }
617 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700618 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700619 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700620 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700621 if (sessionId != NULL) {
622 *sessionId = lSessionId;
623 }
624 }
Steve Block3856b092011-10-20 11:56:00 +0100625 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700626
627 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800628 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800629 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700630 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700631
632 // move effect chain to this output thread if an effect on same session was waiting
633 // for a track to be created
634 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700635 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700636 Mutex::Autolock _dl(thread->mLock);
637 Mutex::Autolock _sl(effectThread->mLock);
638 moveEffectChain_l(lSessionId, effectThread, thread, true);
639 }
Eric Laurenta011e352012-03-29 15:51:43 -0700640
641 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700642 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700643 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
644 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700645 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700646 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700647 } else {
648 mPendingSyncEvents[i]->cancel();
649 }
Eric Laurenta011e352012-03-29 15:51:43 -0700650 mPendingSyncEvents.removeAt(i);
651 i--;
652 }
653 }
654 }
Glenn Kastene198c362013-08-13 09:13:36 -0700655
Eric Laurentfa90e842014-10-17 18:12:31 -0700656 setAudioHwSyncForSession_l(thread, (audio_session_t)lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657 }
Glenn Kastene198c362013-08-13 09:13:36 -0700658
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700659 if (lStatus != NO_ERROR) {
660 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700661 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700662 // Don't hold mClientLock when releasing the reference on the track as the
663 // destructor will acquire it.
664 {
665 Mutex::Autolock _cl(mClientLock);
666 client.clear();
667 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700668 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700669 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700670 }
671
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700672 // return handle to client
673 trackHandle = new TrackHandle(track);
674
Mathias Agopian65ab4712010-07-14 17:59:35 -0700675Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700676 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700677 return trackHandle;
678}
679
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800680uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700681{
682 Mutex::Autolock _l(mLock);
683 PlaybackThread *thread = checkPlaybackThread_l(output);
684 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000685 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700686 return 0;
687 }
688 return thread->sampleRate();
689}
690
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800691audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700692{
693 Mutex::Autolock _l(mLock);
694 PlaybackThread *thread = checkPlaybackThread_l(output);
695 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000696 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800697 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700698 }
699 return thread->format();
700}
701
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800702size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700703{
704 Mutex::Autolock _l(mLock);
705 PlaybackThread *thread = checkPlaybackThread_l(output);
706 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000707 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700708 return 0;
709 }
Glenn Kasten58912562012-04-03 10:45:00 -0700710 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
711 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712 return thread->frameCount();
713}
714
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800715uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700716{
717 Mutex::Autolock _l(mLock);
718 PlaybackThread *thread = checkPlaybackThread_l(output);
719 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800720 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700721 return 0;
722 }
723 return thread->latency();
724}
725
726status_t AudioFlinger::setMasterVolume(float value)
727{
Eric Laurenta1884f92011-08-23 08:25:03 -0700728 status_t ret = initCheck();
729 if (ret != NO_ERROR) {
730 return ret;
731 }
732
Mathias Agopian65ab4712010-07-14 17:59:35 -0700733 // check calling permissions
734 if (!settingsAllowed()) {
735 return PERMISSION_DENIED;
736 }
737
Eric Laurenta4c5a552012-03-29 10:12:40 -0700738 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700739 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700740
John Grossmanee578c02012-07-23 17:05:46 -0700741 // Set master volume in the HALs which support it.
742 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
743 AutoMutex lock(mHardwareLock);
744 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800745
John Grossmanee578c02012-07-23 17:05:46 -0700746 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
747 if (dev->canSetMasterVolume()) {
748 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800749 }
John Grossmanee578c02012-07-23 17:05:46 -0700750 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700751 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700752
John Grossmanee578c02012-07-23 17:05:46 -0700753 // Now set the master volume in each playback thread. Playback threads
754 // assigned to HALs which do not have master volume support will apply
755 // master volume during the mix operation. Threads with HALs which do
756 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800757 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700758 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700759
760 return NO_ERROR;
761}
762
Glenn Kastenf78aee72012-01-04 11:00:47 -0800763status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700764{
Eric Laurenta1884f92011-08-23 08:25:03 -0700765 status_t ret = initCheck();
766 if (ret != NO_ERROR) {
767 return ret;
768 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700769
770 // check calling permissions
771 if (!settingsAllowed()) {
772 return PERMISSION_DENIED;
773 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800774 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000775 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700776 return BAD_VALUE;
777 }
778
779 { // scope for the lock
780 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700781 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700782 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700783 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700784 mHardwareStatus = AUDIO_HW_IDLE;
785 }
786
787 if (NO_ERROR == ret) {
788 Mutex::Autolock _l(mLock);
789 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800790 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700791 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700792 }
793
794 return ret;
795}
796
797status_t AudioFlinger::setMicMute(bool state)
798{
Eric Laurenta1884f92011-08-23 08:25:03 -0700799 status_t ret = initCheck();
800 if (ret != NO_ERROR) {
801 return ret;
802 }
803
Mathias Agopian65ab4712010-07-14 17:59:35 -0700804 // check calling permissions
805 if (!settingsAllowed()) {
806 return PERMISSION_DENIED;
807 }
808
809 AutoMutex lock(mHardwareLock);
810 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700811 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
812 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
813 status_t result = dev->set_mic_mute(dev, state);
814 if (result != NO_ERROR) {
815 ret = result;
816 }
817 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818 mHardwareStatus = AUDIO_HW_IDLE;
819 return ret;
820}
821
822bool AudioFlinger::getMicMute() const
823{
Eric Laurenta1884f92011-08-23 08:25:03 -0700824 status_t ret = initCheck();
825 if (ret != NO_ERROR) {
826 return false;
827 }
828
Dima Zavinfce7a472011-04-19 22:30:36 -0700829 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800830 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700831 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700832 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700833 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700834 mHardwareStatus = AUDIO_HW_IDLE;
835 return state;
836}
837
838status_t AudioFlinger::setMasterMute(bool muted)
839{
John Grossmand8f178d2012-07-20 14:51:35 -0700840 status_t ret = initCheck();
841 if (ret != NO_ERROR) {
842 return ret;
843 }
844
Mathias Agopian65ab4712010-07-14 17:59:35 -0700845 // check calling permissions
846 if (!settingsAllowed()) {
847 return PERMISSION_DENIED;
848 }
849
John Grossmanee578c02012-07-23 17:05:46 -0700850 Mutex::Autolock _l(mLock);
851 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700852
John Grossmanee578c02012-07-23 17:05:46 -0700853 // Set master mute in the HALs which support it.
854 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
855 AutoMutex lock(mHardwareLock);
856 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700857
John Grossmanee578c02012-07-23 17:05:46 -0700858 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
859 if (dev->canSetMasterMute()) {
860 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700861 }
John Grossmanee578c02012-07-23 17:05:46 -0700862 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700863 }
864
John Grossmanee578c02012-07-23 17:05:46 -0700865 // Now set the master mute in each playback thread. Playback threads
866 // assigned to HALs which do not have master mute support will apply master
867 // mute during the mix operation. Threads with HALs which do support master
868 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800869 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700870 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700871
872 return NO_ERROR;
873}
874
875float AudioFlinger::masterVolume() const
876{
Glenn Kasten98067102011-12-13 11:47:54 -0800877 Mutex::Autolock _l(mLock);
878 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700879}
880
881bool AudioFlinger::masterMute() const
882{
Glenn Kasten98067102011-12-13 11:47:54 -0800883 Mutex::Autolock _l(mLock);
884 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700885}
886
John Grossman4ff14ba2012-02-08 16:37:41 -0800887float AudioFlinger::masterVolume_l() const
888{
John Grossman4ff14ba2012-02-08 16:37:41 -0800889 return mMasterVolume;
890}
891
John Grossmand8f178d2012-07-20 14:51:35 -0700892bool AudioFlinger::masterMute_l() const
893{
John Grossmanee578c02012-07-23 17:05:46 -0700894 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700895}
896
Eric Laurent223fd5c2014-11-11 13:43:36 -0800897status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
898{
899 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
900 ALOGW("setStreamVolume() invalid stream %d", stream);
901 return BAD_VALUE;
902 }
903 pid_t caller = IPCThreadState::self()->getCallingPid();
904 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
905 ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
906 return PERMISSION_DENIED;
907 }
908
909 return NO_ERROR;
910}
911
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800912status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
913 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700914{
915 // check calling permissions
916 if (!settingsAllowed()) {
917 return PERMISSION_DENIED;
918 }
919
Eric Laurent223fd5c2014-11-11 13:43:36 -0800920 status_t status = checkStreamType(stream);
921 if (status != NO_ERROR) {
922 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700923 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800924 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700925
926 AutoMutex lock(mLock);
927 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700928 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700929 thread = checkPlaybackThread_l(output);
930 if (thread == NULL) {
931 return BAD_VALUE;
932 }
933 }
934
935 mStreamTypes[stream].volume = value;
936
937 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800938 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700939 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940 }
941 } else {
942 thread->setStreamVolume(stream, value);
943 }
944
945 return NO_ERROR;
946}
947
Glenn Kastenfff6d712012-01-12 16:38:12 -0800948status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700949{
950 // check calling permissions
951 if (!settingsAllowed()) {
952 return PERMISSION_DENIED;
953 }
954
Eric Laurent223fd5c2014-11-11 13:43:36 -0800955 status_t status = checkStreamType(stream);
956 if (status != NO_ERROR) {
957 return status;
958 }
959 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
960
961 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000962 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700963 return BAD_VALUE;
964 }
965
Eric Laurent93575202011-01-18 18:39:02 -0800966 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700967 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700968 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700969 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700970
971 return NO_ERROR;
972}
973
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800974float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700975{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800976 status_t status = checkStreamType(stream);
977 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700978 return 0.0f;
979 }
980
981 AutoMutex lock(mLock);
982 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -0700983 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700984 PlaybackThread *thread = checkPlaybackThread_l(output);
985 if (thread == NULL) {
986 return 0.0f;
987 }
988 volume = thread->streamVolume(stream);
989 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800990 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700991 }
992
993 return volume;
994}
995
Glenn Kastenfff6d712012-01-12 16:38:12 -0800996bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700997{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800998 status_t status = checkStreamType(stream);
999 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001000 return true;
1001 }
1002
Glenn Kasten6637baa2012-01-09 09:40:36 -08001003 AutoMutex lock(mLock);
1004 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001005}
1006
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001007status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001008{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001009 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1010 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001011
Mathias Agopian65ab4712010-07-14 17:59:35 -07001012 // check calling permissions
1013 if (!settingsAllowed()) {
1014 return PERMISSION_DENIED;
1015 }
1016
Glenn Kasten142f5192014-03-25 17:44:59 -07001017 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1018 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001019 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -07001020 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001021 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001022 AutoMutex lock(mHardwareLock);
1023 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1024 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1025 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1026 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1027 final_result = result ?: final_result;
1028 }
1029 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001030 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001031 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1032 AudioParameter param = AudioParameter(keyValuePairs);
1033 String8 value;
1034 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001035 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1036 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001037 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1038 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001039 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001040 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1041 // collect all of the thread's session IDs
1042 KeyedVector<int, bool> ids = thread->sessionIds();
1043 // suspend effects associated with those session IDs
1044 for (size_t j = 0; j < ids.size(); ++j) {
1045 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001046 thread->setEffectSuspended(FX_IID_AEC,
1047 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001048 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001049 thread->setEffectSuspended(FX_IID_NS,
1050 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001051 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001052 }
1053 }
Eric Laurentbee53372011-08-29 12:42:48 -07001054 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001055 }
1056 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001057 String8 screenState;
1058 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1059 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001060 if (isOff != (AudioFlinger::mScreenState & 1)) {
1061 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001062 }
1063 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001064 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001065 }
1066
1067 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1068 // and the thread is exited once the lock is released
1069 sp<ThreadBase> thread;
1070 {
1071 Mutex::Autolock _l(mLock);
1072 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001073 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001074 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001075 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001076 // indicate output device change to all input threads for pre processing
1077 AudioParameter param = AudioParameter(keyValuePairs);
1078 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001079 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1080 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001081 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1082 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1083 }
1084 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001085 }
1086 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001087 if (thread != 0) {
1088 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001089 }
1090 return BAD_VALUE;
1091}
1092
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001093String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001094{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001095 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1096 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001097
Eric Laurenta4c5a552012-03-29 10:12:40 -07001098 Mutex::Autolock _l(mLock);
1099
Glenn Kasten142f5192014-03-25 17:44:59 -07001100 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001101 String8 out_s8;
1102
Dima Zavin799a70e2011-04-18 16:57:27 -07001103 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001104 char *s;
1105 {
1106 AutoMutex lock(mHardwareLock);
1107 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001108 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001109 s = dev->get_parameters(dev, keys.string());
1110 mHardwareStatus = AUDIO_HW_IDLE;
1111 }
John Grossmanef7740b2012-02-09 11:28:36 -08001112 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001113 free(s);
1114 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001115 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001116 }
1117
Mathias Agopian65ab4712010-07-14 17:59:35 -07001118 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1119 if (playbackThread != NULL) {
1120 return playbackThread->getParameters(keys);
1121 }
1122 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1123 if (recordThread != NULL) {
1124 return recordThread->getParameters(keys);
1125 }
1126 return String8("");
1127}
1128
Glenn Kastendd8104c2012-07-02 12:42:44 -07001129size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1130 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001131{
Eric Laurenta1884f92011-08-23 08:25:03 -07001132 status_t ret = initCheck();
1133 if (ret != NO_ERROR) {
1134 return 0;
1135 }
1136
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001137 AutoMutex lock(mHardwareLock);
1138 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001139 audio_config_t config;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001140 memset(&config, 0, sizeof(config));
1141 config.sample_rate = sampleRate;
1142 config.channel_mask = channelMask;
1143 config.format = format;
1144
John Grossmanee578c02012-07-23 17:05:46 -07001145 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1146 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001147 mHardwareStatus = AUDIO_HW_IDLE;
1148 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001149}
1150
Glenn Kasten5f972c02014-01-13 09:59:31 -08001151uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001152{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001153 Mutex::Autolock _l(mLock);
1154
1155 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1156 if (recordThread != NULL) {
1157 return recordThread->getInputFramesLost();
1158 }
1159 return 0;
1160}
1161
1162status_t AudioFlinger::setVoiceVolume(float value)
1163{
Eric Laurenta1884f92011-08-23 08:25:03 -07001164 status_t ret = initCheck();
1165 if (ret != NO_ERROR) {
1166 return ret;
1167 }
1168
Mathias Agopian65ab4712010-07-14 17:59:35 -07001169 // check calling permissions
1170 if (!settingsAllowed()) {
1171 return PERMISSION_DENIED;
1172 }
1173
1174 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001175 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001176 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001177 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001178 mHardwareStatus = AUDIO_HW_IDLE;
1179
1180 return ret;
1181}
1182
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001183status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001184 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001185{
1186 status_t status;
1187
1188 Mutex::Autolock _l(mLock);
1189
1190 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1191 if (playbackThread != NULL) {
1192 return playbackThread->getRenderPosition(halFrames, dspFrames);
1193 }
1194
1195 return BAD_VALUE;
1196}
1197
1198void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1199{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001200 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001201 if (client == 0) {
1202 return;
1203 }
Eric Laurent021cf962014-05-13 10:18:14 -07001204 bool clientAdded = false;
1205 {
1206 Mutex::Autolock _cl(mClientLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001207
Eric Laurent021cf962014-05-13 10:18:14 -07001208 pid_t pid = IPCThreadState::self()->getCallingPid();
1209 if (mNotificationClients.indexOfKey(pid) < 0) {
1210 sp<NotificationClient> notificationClient = new NotificationClient(this,
1211 client,
1212 pid);
1213 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001214
Eric Laurent021cf962014-05-13 10:18:14 -07001215 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001216
Marco Nelissen06b46062014-11-14 07:58:25 -08001217 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001218 binder->linkToDeath(notificationClient);
1219 clientAdded = true;
1220 }
1221 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001222
Eric Laurent021cf962014-05-13 10:18:14 -07001223 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001224 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent021cf962014-05-13 10:18:14 -07001225 if (clientAdded) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001226 // the config change is always sent from playback or record threads to avoid deadlock
1227 // with AudioSystem::gLock
1228 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001229 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001230 }
1231
1232 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001233 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001234 }
1235 }
1236}
1237
1238void AudioFlinger::removeNotificationClient(pid_t pid)
1239{
1240 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001241 {
1242 Mutex::Autolock _cl(mClientLock);
1243 mNotificationClients.removeItem(pid);
1244 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001245
Steve Block3856b092011-10-20 11:56:00 +01001246 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001247 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001248 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001249 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001250 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001251 ALOGV(" pid %d @ %d", ref->mPid, i);
1252 if (ref->mPid == pid) {
1253 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001254 mAudioSessionRefs.removeAt(i);
1255 delete ref;
1256 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001257 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001258 } else {
1259 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001260 }
1261 }
1262 if (removed) {
1263 purgeStaleEffects_l();
1264 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001265}
1266
Eric Laurent021cf962014-05-13 10:18:14 -07001267void AudioFlinger::audioConfigChanged(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001268{
Eric Laurent021cf962014-05-13 10:18:14 -07001269 Mutex::Autolock _l(mClientLock);
1270 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001271 for (size_t i = 0; i < size; i++) {
Eric Laurent021cf962014-05-13 10:18:14 -07001272 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event,
Eric Laurent10351942014-05-08 18:49:52 -07001273 ioHandle,
1274 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001275 }
1276}
1277
Eric Laurent021cf962014-05-13 10:18:14 -07001278// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001279void AudioFlinger::removeClient_l(pid_t pid)
1280{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001281 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001282 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001283 mClients.removeItem(pid);
1284}
1285
Eric Laurent717e1282012-06-29 16:36:52 -07001286// getEffectThread_l() must be called with AudioFlinger::mLock held
1287sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1288{
1289 sp<PlaybackThread> thread;
1290
1291 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1292 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1293 ALOG_ASSERT(thread == 0);
1294 thread = mPlaybackThreads.valueAt(i);
1295 }
1296 }
1297
1298 return thread;
1299}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001300
Mathias Agopian65ab4712010-07-14 17:59:35 -07001301
Mathias Agopian65ab4712010-07-14 17:59:35 -07001302
1303// ----------------------------------------------------------------------------
1304
1305AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1306 : RefBase(),
1307 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001308 // FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below
Mathias Agopian65ab4712010-07-14 17:59:35 -07001309 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001310 mPid(pid),
1311 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001312{
1313 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1314}
1315
Eric Laurent021cf962014-05-13 10:18:14 -07001316// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001317AudioFlinger::Client::~Client()
1318{
1319 mAudioFlinger->removeClient_l(mPid);
1320}
1321
Glenn Kasten435dbe62012-01-30 10:15:48 -08001322sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001323{
1324 return mMemoryDealer;
1325}
1326
John Grossman4ff14ba2012-02-08 16:37:41 -08001327// Reserve one of the limited slots for a timed audio track associated
1328// with this client
1329bool AudioFlinger::Client::reserveTimedTrack()
1330{
1331 const int kMaxTimedTracksPerClient = 4;
1332
1333 Mutex::Autolock _l(mTimedTrackLock);
1334
1335 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1336 ALOGW("can not create timed track - pid %d has exceeded the limit",
1337 mPid);
1338 return false;
1339 }
1340
1341 mTimedTrackCount++;
1342 return true;
1343}
1344
1345// Release a slot for a timed audio track
1346void AudioFlinger::Client::releaseTimedTrack()
1347{
1348 Mutex::Autolock _l(mTimedTrackLock);
1349 mTimedTrackCount--;
1350}
1351
Mathias Agopian65ab4712010-07-14 17:59:35 -07001352// ----------------------------------------------------------------------------
1353
1354AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1355 const sp<IAudioFlingerClient>& client,
1356 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001357 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001358{
1359}
1360
1361AudioFlinger::NotificationClient::~NotificationClient()
1362{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001363}
1364
Glenn Kasten0f11b512014-01-31 16:18:54 -08001365void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001366{
1367 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001368 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001369}
1370
Mathias Agopian65ab4712010-07-14 17:59:35 -07001371
1372// ----------------------------------------------------------------------------
1373
Jeff Brown893a5642013-08-16 20:19:26 -07001374static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1375 return audio_is_remote_submix_device(inDevice);
1376}
1377
Mathias Agopian65ab4712010-07-14 17:59:35 -07001378sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001379 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001380 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001381 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001382 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001383 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001384 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001385 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001386 int *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001387 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001388 sp<IMemory>& cblk,
1389 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001390 status_t *status)
1391{
1392 sp<RecordThread::RecordTrack> recordTrack;
1393 sp<RecordHandle> recordHandle;
1394 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001395 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001396 int lSessionId;
1397
Glenn Kastend776ac62014-05-07 09:16:09 -07001398 cblk.clear();
1399 buffers.clear();
1400
Mathias Agopian65ab4712010-07-14 17:59:35 -07001401 // check calling permissions
1402 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001403 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001404 lStatus = PERMISSION_DENIED;
1405 goto Exit;
1406 }
1407
Glenn Kasten53b5d092014-02-05 10:00:23 -08001408 // further sample rate checks are performed by createRecordTrack_l()
1409 if (sampleRate == 0) {
1410 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1411 lStatus = BAD_VALUE;
1412 goto Exit;
1413 }
1414
Glenn Kasten0e0e8462014-03-13 15:02:40 -07001415 // we don't yet support anything other than 16-bit PCM
1416 if (!(audio_is_valid_format(format) &&
1417 audio_is_linear_pcm(format) && format == AUDIO_FORMAT_PCM_16_BIT)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001418 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001419 lStatus = BAD_VALUE;
1420 goto Exit;
1421 }
1422
Glenn Kasten53b5d092014-02-05 10:00:23 -08001423 // further channel mask checks are performed by createRecordTrack_l()
1424 if (!audio_is_input_channel(channelMask)) {
1425 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1426 lStatus = BAD_VALUE;
1427 goto Exit;
1428 }
1429
Glenn Kasten05997e22014-03-13 15:08:33 -07001430 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001431 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001432 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001433 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001434 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001435 lStatus = BAD_VALUE;
1436 goto Exit;
1437 }
1438
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001439 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001440 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001441
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001442 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001443 lSessionId = *sessionId;
1444 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001445 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001446 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001447 if (sessionId != NULL) {
1448 *sessionId = lSessionId;
1449 }
1450 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001451 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001452
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001453 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001454 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001455 frameCount, lSessionId, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001456 IPCThreadState::self()->getCallingUid(),
1457 flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001458 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001459
1460 if (lStatus == NO_ERROR) {
1461 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1462 // session and move it to this thread.
1463 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)lSessionId);
1464 if (chain != 0) {
1465 Mutex::Autolock _l(thread->mLock);
1466 thread->addEffectChain_l(chain);
1467 }
1468 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001469 }
Glenn Kastene198c362013-08-13 09:13:36 -07001470
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001471 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001472 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001473 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001474 // Don't hold mClientLock when releasing the reference on the track as the
1475 // destructor will acquire it.
1476 {
1477 Mutex::Autolock _cl(mClientLock);
1478 client.clear();
1479 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001480 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001481 goto Exit;
1482 }
1483
Glenn Kastend776ac62014-05-07 09:16:09 -07001484 cblk = recordTrack->getCblk();
1485 buffers = recordTrack->getBuffers();
1486
Glenn Kasten2fc14732013-08-05 14:58:14 -07001487 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001488 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001489
1490Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001491 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001492 return recordHandle;
1493}
1494
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001495
1496
Mathias Agopian65ab4712010-07-14 17:59:35 -07001497// ----------------------------------------------------------------------------
1498
Eric Laurenta4c5a552012-03-29 10:12:40 -07001499audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1500{
Eric Laurent44622db2014-08-01 19:00:33 -07001501 if (name == NULL) {
1502 return 0;
1503 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001504 if (!settingsAllowed()) {
1505 return 0;
1506 }
1507 Mutex::Autolock _l(mLock);
1508 return loadHwModule_l(name);
1509}
1510
1511// loadHwModule_l() must be called with AudioFlinger::mLock held
1512audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1513{
1514 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1515 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1516 ALOGW("loadHwModule() module %s already loaded", name);
1517 return mAudioHwDevs.keyAt(i);
1518 }
1519 }
1520
Eric Laurenta4c5a552012-03-29 10:12:40 -07001521 audio_hw_device_t *dev;
1522
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001523 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001524 if (rc) {
1525 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1526 return 0;
1527 }
1528
1529 mHardwareStatus = AUDIO_HW_INIT;
1530 rc = dev->init_check(dev);
1531 mHardwareStatus = AUDIO_HW_IDLE;
1532 if (rc) {
1533 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1534 return 0;
1535 }
1536
John Grossmanee578c02012-07-23 17:05:46 -07001537 // Check and cache this HAL's level of support for master mute and master
1538 // volume. If this is the first HAL opened, and it supports the get
1539 // methods, use the initial values provided by the HAL as the current
1540 // master mute and volume settings.
1541
1542 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1543 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001544 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001545
1546 if (0 == mAudioHwDevs.size()) {
1547 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1548 if (NULL != dev->get_master_volume) {
1549 float mv;
1550 if (OK == dev->get_master_volume(dev, &mv)) {
1551 mMasterVolume = mv;
1552 }
1553 }
1554
1555 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1556 if (NULL != dev->get_master_mute) {
1557 bool mm;
1558 if (OK == dev->get_master_mute(dev, &mm)) {
1559 mMasterMute = mm;
1560 }
1561 }
1562 }
1563
Eric Laurenta4c5a552012-03-29 10:12:40 -07001564 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001565 if ((NULL != dev->set_master_volume) &&
1566 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1567 flags = static_cast<AudioHwDevice::Flags>(flags |
1568 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1569 }
1570
1571 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1572 if ((NULL != dev->set_master_mute) &&
1573 (OK == dev->set_master_mute(dev, mMasterMute))) {
1574 flags = static_cast<AudioHwDevice::Flags>(flags |
1575 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1576 }
1577
Eric Laurenta4c5a552012-03-29 10:12:40 -07001578 mHardwareStatus = AUDIO_HW_IDLE;
1579 }
1580
1581 audio_module_handle_t handle = nextUniqueId();
Eric Laurent83b88082014-06-20 18:31:16 -07001582 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001583
1584 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001585 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001586
1587 return handle;
1588
1589}
1590
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001591// ----------------------------------------------------------------------------
1592
Glenn Kasten3b16c762012-11-14 08:44:39 -08001593uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001594{
1595 Mutex::Autolock _l(mLock);
1596 PlaybackThread *thread = primaryPlaybackThread_l();
1597 return thread != NULL ? thread->sampleRate() : 0;
1598}
1599
Glenn Kastene33054e2012-11-14 12:54:39 -08001600size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001601{
1602 Mutex::Autolock _l(mLock);
1603 PlaybackThread *thread = primaryPlaybackThread_l();
1604 return thread != NULL ? thread->frameCountHAL() : 0;
1605}
1606
1607// ----------------------------------------------------------------------------
1608
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001609status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1610{
1611 uid_t uid = IPCThreadState::self()->getCallingUid();
1612 if (uid != AID_SYSTEM) {
1613 return PERMISSION_DENIED;
1614 }
1615 Mutex::Autolock _l(mLock);
1616 if (mIsDeviceTypeKnown) {
1617 return INVALID_OPERATION;
1618 }
1619 mIsLowRamDevice = isLowRamDevice;
1620 mIsDeviceTypeKnown = true;
1621 return NO_ERROR;
1622}
1623
Eric Laurent93c3d412014-08-01 14:48:35 -07001624audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1625{
1626 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001627
1628 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1629 if (index >= 0) {
1630 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1631 mHwAvSyncIds.valueAt(index), sessionId);
1632 return mHwAvSyncIds.valueAt(index);
1633 }
1634
1635 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1636 if (dev == NULL) {
1637 return AUDIO_HW_SYNC_INVALID;
1638 }
1639 char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1640 AudioParameter param = AudioParameter(String8(reply));
1641 free(reply);
1642
1643 int value;
1644 if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1645 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1646 return AUDIO_HW_SYNC_INVALID;
1647 }
1648
1649 // allow only one session for a given HW A/V sync ID.
1650 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1651 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1652 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1653 value, mHwAvSyncIds.keyAt(i));
1654 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001655 break;
1656 }
1657 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001658
1659 mHwAvSyncIds.add(sessionId, value);
1660
1661 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1662 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1663 uint32_t sessions = thread->hasAudioSession(sessionId);
1664 if (sessions & PlaybackThread::TRACK_SESSION) {
1665 AudioParameter param = AudioParameter();
1666 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1667 thread->setParameters(param.toString());
1668 break;
1669 }
1670 }
1671
1672 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1673 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001674}
1675
Eric Laurentfa90e842014-10-17 18:12:31 -07001676// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1677void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1678{
1679 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1680 if (index >= 0) {
1681 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1682 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1683 AudioParameter param = AudioParameter();
1684 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1685 thread->setParameters(param.toString());
1686 }
1687}
1688
1689
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001690// ----------------------------------------------------------------------------
1691
Eric Laurent83b88082014-06-20 18:31:16 -07001692
1693sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001694 audio_io_handle_t *output,
1695 audio_config_t *config,
1696 audio_devices_t devices,
1697 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001698 audio_output_flags_t flags)
1699{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001700 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001701 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001702 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001703 }
1704
1705 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentcf2c0212014-07-25 16:20:43 -07001706 if (*output == AUDIO_IO_HANDLE_NONE) {
1707 *output = nextUniqueId();
1708 }
Eric Laurent83b88082014-06-20 18:31:16 -07001709
1710 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1711
1712 audio_stream_out_t *outStream = NULL;
1713
1714 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001715 // This if statement allows overriding the audio policy settings
1716 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1717 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1718 // Check only for Normal Mixing mode
1719 if (kEnableExtendedPrecision) {
1720 // Specify format (uncomment one below to choose)
1721 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1722 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1723 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1724 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001725 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001726 }
1727 if (kEnableExtendedChannels) {
1728 // Specify channel mask (uncomment one below to choose)
1729 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1730 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1731 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1732 }
Eric Laurent83b88082014-06-20 18:31:16 -07001733 }
1734
1735 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001736 *output,
1737 devices,
1738 flags,
1739 config,
1740 &outStream,
1741 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001742
1743 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001744 ALOGV("openOutput_l() openOutputStream returned output %p, sampleRate %d, Format %#x, "
Andy Hung9a592762014-07-21 21:56:01 -07001745 "channelMask %#x, status %d",
Eric Laurent83b88082014-06-20 18:31:16 -07001746 outStream,
1747 config->sample_rate,
1748 config->format,
1749 config->channel_mask,
1750 status);
1751
1752 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001753 AudioStreamOut *outputStream = new AudioStreamOut(outHwDev, outStream, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001754
1755 PlaybackThread *thread;
1756 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001757 thread = new OffloadThread(this, outputStream, *output, devices);
1758 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001759 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1760 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001761 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001762 thread = new DirectOutputThread(this, outputStream, *output, devices);
1763 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001764 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001765 thread = new MixerThread(this, outputStream, *output, devices);
1766 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001767 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001768 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001769 return thread;
1770 }
1771
1772 return 0;
1773}
1774
Eric Laurentcf2c0212014-07-25 16:20:43 -07001775status_t AudioFlinger::openOutput(audio_module_handle_t module,
1776 audio_io_handle_t *output,
1777 audio_config_t *config,
1778 audio_devices_t *devices,
1779 const String8& address,
1780 uint32_t *latencyMs,
1781 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001782{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001783 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001784 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001785 (devices != NULL) ? *devices : 0,
1786 config->sample_rate,
1787 config->format,
1788 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001789 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001790
Eric Laurentcf2c0212014-07-25 16:20:43 -07001791 if (*devices == AUDIO_DEVICE_NONE) {
1792 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001793 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001794
Mathias Agopian65ab4712010-07-14 17:59:35 -07001795 Mutex::Autolock _l(mLock);
1796
Eric Laurentcf2c0212014-07-25 16:20:43 -07001797 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001798 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001799 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001800
1801 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001802 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001803
1804 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001805 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001806 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001807 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001808
1809 AutoMutex lock(mHardwareLock);
1810 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001811 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001812 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten4ea00a22014-06-02 08:29:22 -07001813
Eric Laurentcf2c0212014-07-25 16:20:43 -07001814 mPrimaryOutputSampleRate = config->sample_rate;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001815 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001816 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001817 }
1818
Eric Laurentcf2c0212014-07-25 16:20:43 -07001819 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001820}
1821
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001822audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1823 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001824{
1825 Mutex::Autolock _l(mLock);
1826 MixerThread *thread1 = checkMixerThread_l(output1);
1827 MixerThread *thread2 = checkMixerThread_l(output2);
1828
1829 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001830 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1831 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001832 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001833 }
1834
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001835 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001836 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1837 thread->addOutputTrack(thread2);
1838 mPlaybackThreads.add(id, thread);
1839 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001840 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001841 return id;
1842}
1843
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001844status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001845{
Glenn Kastend96c5722012-04-25 13:44:49 -07001846 return closeOutput_nonvirtual(output);
1847}
1848
1849status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1850{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001851 // keep strong reference on the playback thread so that
1852 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001853 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001854 {
1855 Mutex::Autolock _l(mLock);
1856 thread = checkPlaybackThread_l(output);
1857 if (thread == NULL) {
1858 return BAD_VALUE;
1859 }
1860
Steve Block3856b092011-10-20 11:56:00 +01001861 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001862
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001863 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001864 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001865 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001866 DuplicatingThread *dupThread =
1867 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001868 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001869
1870 }
1871 }
1872 }
1873
1874
1875 mPlaybackThreads.removeItem(output);
1876 // save all effects to the default thread
1877 if (mPlaybackThreads.size()) {
1878 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1879 if (dstThread != NULL) {
1880 // audioflinger lock is held here so the acquisition order of thread locks does not
1881 // matter
1882 Mutex::Autolock _dl(dstThread->mLock);
1883 Mutex::Autolock _sl(thread->mLock);
1884 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1885 for (size_t i = 0; i < effectChains.size(); i ++) {
1886 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001887 }
1888 }
1889 }
Eric Laurent021cf962014-05-13 10:18:14 -07001890 audioConfigChanged(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001891 }
1892 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001893 // The thread entity (active unit of execution) is no longer running here,
1894 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001895
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001896 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurent83b88082014-06-20 18:31:16 -07001897 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001898 }
Eric Laurent83b88082014-06-20 18:31:16 -07001899
Mathias Agopian65ab4712010-07-14 17:59:35 -07001900 return NO_ERROR;
1901}
1902
Eric Laurent83b88082014-06-20 18:31:16 -07001903void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
1904{
1905 AudioStreamOut *out = thread->clearOutput();
1906 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1907 // from now on thread->mOutput is NULL
1908 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
1909 delete out;
1910}
1911
1912void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
1913{
1914 mPlaybackThreads.removeItem(thread->mId);
1915 thread->exit();
1916 closeOutputFinish(thread);
1917}
1918
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001919status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001920{
1921 Mutex::Autolock _l(mLock);
1922 PlaybackThread *thread = checkPlaybackThread_l(output);
1923
1924 if (thread == NULL) {
1925 return BAD_VALUE;
1926 }
1927
Steve Block3856b092011-10-20 11:56:00 +01001928 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001929 thread->suspend();
1930
1931 return NO_ERROR;
1932}
1933
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001934status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001935{
1936 Mutex::Autolock _l(mLock);
1937 PlaybackThread *thread = checkPlaybackThread_l(output);
1938
1939 if (thread == NULL) {
1940 return BAD_VALUE;
1941 }
1942
Steve Block3856b092011-10-20 11:56:00 +01001943 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001944
1945 thread->restore();
1946
1947 return NO_ERROR;
1948}
1949
Eric Laurentcf2c0212014-07-25 16:20:43 -07001950status_t AudioFlinger::openInput(audio_module_handle_t module,
1951 audio_io_handle_t *input,
1952 audio_config_t *config,
1953 audio_devices_t *device,
1954 const String8& address,
1955 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07001956 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001957{
Eric Laurent83b88082014-06-20 18:31:16 -07001958 Mutex::Autolock _l(mLock);
1959
Eric Laurentcf2c0212014-07-25 16:20:43 -07001960 if (*device == AUDIO_DEVICE_NONE) {
1961 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07001962 }
1963
Eric Laurentcf2c0212014-07-25 16:20:43 -07001964 sp<RecordThread> thread = openInput_l(module, input, config, *device, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001965
1966 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07001967 // notify client processes of the new input creation
1968 thread->audioConfigChanged(AudioSystem::INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001969 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001970 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001971 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07001972}
Dima Zavin799a70e2011-04-18 16:57:27 -07001973
Eric Laurent83b88082014-06-20 18:31:16 -07001974sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001975 audio_io_handle_t *input,
1976 audio_config_t *config,
Eric Laurent83b88082014-06-20 18:31:16 -07001977 audio_devices_t device,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001978 const String8& address,
1979 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07001980 audio_input_flags_t flags)
1981{
Eric Laurent83b88082014-06-20 18:31:16 -07001982 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, device);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001983 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001984 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07001985 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001986 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001987
Eric Laurentcf2c0212014-07-25 16:20:43 -07001988 if (*input == AUDIO_IO_HANDLE_NONE) {
1989 *input = nextUniqueId();
1990 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001991
Eric Laurentcf2c0212014-07-25 16:20:43 -07001992 audio_config_t halconfig = *config;
1993 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07001994 audio_stream_in_t *inStream = NULL;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001995 status_t status = inHwHal->open_input_stream(inHwHal, *input, device, &halconfig,
1996 &inStream, flags, address.string(), source);
1997 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001998 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Dima Zavin799a70e2011-04-18 16:57:27 -07001999 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002000 halconfig.sample_rate,
2001 halconfig.format,
2002 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002003 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002004 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002005
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002006 // If the input could not be opened with the requested parameters and we can handle the
2007 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
2008 // resample the input and do mono to stereo or stereo to mono conversions on 16 bit PCM inputs.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002009 if (status == BAD_VALUE &&
Eric Laurentcf2c0212014-07-25 16:20:43 -07002010 config->format == halconfig.format && halconfig.format == AUDIO_FORMAT_PCM_16_BIT &&
2011 (halconfig.sample_rate <= 2 * config->sample_rate) &&
2012 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
2013 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002014 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002015 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002016 inStream = NULL;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002017 status = inHwHal->open_input_stream(inHwHal, *input, device, &halconfig,
2018 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07002019 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002020 }
2021
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002022 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002023
Glenn Kasten46909e72013-02-26 09:20:22 -08002024#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002025 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2026 // or (re-)create if current Pipe is idle and does not match the new format
2027 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002028 enum {
2029 TEE_SINK_NO, // don't copy input
2030 TEE_SINK_NEW, // copy input using a new pipe
2031 TEE_SINK_OLD, // copy input using an existing pipe
2032 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002033 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2034 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002035 if (!mTeeSinkInputEnabled) {
2036 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002037 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002038 kind = TEE_SINK_NO;
2039 } else if (mRecordTeeSink == 0) {
2040 kind = TEE_SINK_NEW;
2041 } else if (mRecordTeeSink->getStrongCount() != 1) {
2042 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002043 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002044 kind = TEE_SINK_OLD;
2045 } else {
2046 kind = TEE_SINK_NEW;
2047 }
2048 switch (kind) {
2049 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002050 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002051 size_t numCounterOffers = 0;
2052 const NBAIO_Format offers[1] = {format};
2053 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2054 ALOG_ASSERT(index == 0);
2055 PipeReader *pipeReader = new PipeReader(*pipe);
2056 numCounterOffers = 0;
2057 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2058 ALOG_ASSERT(index == 0);
2059 mRecordTeeSink = pipe;
2060 mRecordTeeSource = pipeReader;
2061 teeSink = pipe;
2062 }
2063 break;
2064 case TEE_SINK_OLD:
2065 teeSink = mRecordTeeSink;
2066 break;
2067 case TEE_SINK_NO:
2068 default:
2069 break;
2070 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002071#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002072
Eric Laurentcf2c0212014-07-25 16:20:43 -07002073 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002074
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002075 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002076 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002077 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002078 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002079 inputStream,
2080 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002081 primaryOutputDevice_l(),
Eric Laurent83b88082014-06-20 18:31:16 -07002082 device
Glenn Kasten46909e72013-02-26 09:20:22 -08002083#ifdef TEE_SINK
2084 , teeSink
2085#endif
2086 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002087 mRecordThreads.add(*input, thread);
2088 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002089 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002090 }
2091
Eric Laurentcf2c0212014-07-25 16:20:43 -07002092 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002093 return 0;
2094}
2095
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002096status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002097{
Glenn Kastend96c5722012-04-25 13:44:49 -07002098 return closeInput_nonvirtual(input);
2099}
2100
2101status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2102{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002103 // keep strong reference on the record thread so that
2104 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002105 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002106 {
2107 Mutex::Autolock _l(mLock);
2108 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002109 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002110 return BAD_VALUE;
2111 }
2112
Steve Block3856b092011-10-20 11:56:00 +01002113 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002114
2115 // If we still have effect chains, it means that a client still holds a handle
2116 // on at least one effect. We must either move the chain to an existing thread with the
2117 // same session ID or put it aside in case a new record thread is opened for a
2118 // new capture on the same session
2119 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002120 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002121 Mutex::Autolock _sl(thread->mLock);
2122 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002123 // Note: maximum one chain per record thread
2124 if (effectChains.size() != 0) {
2125 chain = effectChains[0];
2126 }
2127 }
2128 if (chain != 0) {
2129 // first check if a record thread is already opened with a client on the same session.
2130 // This should only happen in case of overlap between one thread tear down and the
2131 // creation of its replacement
2132 size_t i;
2133 for (i = 0; i < mRecordThreads.size(); i++) {
2134 sp<RecordThread> t = mRecordThreads.valueAt(i);
2135 if (t == thread) {
2136 continue;
2137 }
2138 if (t->hasAudioSession(chain->sessionId()) != 0) {
2139 Mutex::Autolock _l(t->mLock);
2140 ALOGV("closeInput() found thread %d for effect session %d",
2141 t->id(), chain->sessionId());
2142 t->addEffectChain_l(chain);
2143 break;
2144 }
2145 }
2146 // put the chain aside if we could not find a record thread with the same session id.
2147 if (i == mRecordThreads.size()) {
2148 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002149 }
2150 }
Eric Laurent021cf962014-05-13 10:18:14 -07002151 audioConfigChanged(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002152 mRecordThreads.removeItem(input);
2153 }
Eric Laurent83b88082014-06-20 18:31:16 -07002154 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2155 // we have a different lock for notification client
2156 closeInputFinish(thread);
2157 return NO_ERROR;
2158}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002159
Eric Laurent83b88082014-06-20 18:31:16 -07002160void AudioFlinger::closeInputFinish(sp<RecordThread> thread)
2161{
2162 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002163 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002164 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002165 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002166 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002167 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002168}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002169
Eric Laurent83b88082014-06-20 18:31:16 -07002170void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
2171{
2172 mRecordThreads.removeItem(thread->mId);
2173 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002174}
2175
Glenn Kastend2304db2014-02-03 07:40:31 -08002176status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002177{
2178 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002179 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002180
2181 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2182 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002183 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002184 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002185
2186 return NO_ERROR;
2187}
2188
2189
Eric Laurentde3f8392014-07-27 18:38:22 -07002190audio_unique_id_t AudioFlinger::newAudioUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002191{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002192 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002193}
2194
Marco Nelissend457c972014-02-11 08:47:07 -08002195void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002196{
2197 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002198 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002199 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2200 if (pid != -1 && (caller == getpid_cached)) {
2201 caller = pid;
2202 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002203
Eric Laurent021cf962014-05-13 10:18:14 -07002204 {
2205 Mutex::Autolock _cl(mClientLock);
2206 // Ignore requests received from processes not known as notification client. The request
2207 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2208 // called from a different pid leaving a stale session reference. Also we don't know how
2209 // to clear this reference if the client process dies.
2210 if (mNotificationClients.indexOfKey(caller) < 0) {
2211 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2212 return;
2213 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002214 }
2215
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002216 size_t num = mAudioSessionRefs.size();
2217 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002218 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002219 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2220 ref->mCnt++;
2221 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002222 return;
2223 }
2224 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002225 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2226 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002227}
2228
Marco Nelissend457c972014-02-11 08:47:07 -08002229void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002230{
2231 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002232 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002233 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2234 if (pid != -1 && (caller == getpid_cached)) {
2235 caller = pid;
2236 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002237 size_t num = mAudioSessionRefs.size();
2238 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002239 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002240 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2241 ref->mCnt--;
2242 ALOGV(" decremented refcount to %d", ref->mCnt);
2243 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002244 mAudioSessionRefs.removeAt(i);
2245 delete ref;
2246 purgeStaleEffects_l();
2247 }
2248 return;
2249 }
2250 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002251 // If the caller is mediaserver it is likely that the session being released was acquired
2252 // on behalf of a process not in notification clients and we ignore the warning.
2253 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002254}
2255
2256void AudioFlinger::purgeStaleEffects_l() {
2257
Steve Block3856b092011-10-20 11:56:00 +01002258 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002259
2260 Vector< sp<EffectChain> > chains;
2261
2262 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2263 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2264 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2265 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002266 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2267 chains.push(ec);
2268 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002269 }
2270 }
2271 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2272 sp<RecordThread> t = mRecordThreads.valueAt(i);
2273 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2274 sp<EffectChain> ec = t->mEffectChains[j];
2275 chains.push(ec);
2276 }
2277 }
2278
2279 for (size_t i = 0; i < chains.size(); i++) {
2280 sp<EffectChain> ec = chains[i];
2281 int sessionid = ec->sessionId();
2282 sp<ThreadBase> t = ec->mThread.promote();
2283 if (t == 0) {
2284 continue;
2285 }
2286 size_t numsessionrefs = mAudioSessionRefs.size();
2287 bool found = false;
2288 for (size_t k = 0; k < numsessionrefs; k++) {
2289 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002290 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002291 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002292 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002293 found = true;
2294 break;
2295 }
2296 }
2297 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002298 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002299 // remove all effects from the chain
2300 while (ec->mEffects.size()) {
2301 sp<EffectModule> effect = ec->mEffects[0];
2302 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002303 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002304 if (effect->purgeHandles()) {
2305 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002306 }
2307 AudioSystem::unregisterEffect(effect->id());
2308 }
2309 }
2310 }
2311 return;
2312}
2313
Mathias Agopian65ab4712010-07-14 17:59:35 -07002314// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002315AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002316{
Glenn Kastena1117922012-01-26 10:53:32 -08002317 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002318}
2319
2320// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002321AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002322{
2323 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002324 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002325}
2326
2327// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002328AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002329{
Glenn Kastena1117922012-01-26 10:53:32 -08002330 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002331}
2332
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002333uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002334{
Glenn Kastenbcefec32014-01-17 12:09:05 -08002335 return (uint32_t) android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002336}
2337
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002338AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002339{
2340 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2341 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002342 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002343 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002344 return thread;
2345 }
2346 }
2347 return NULL;
2348}
2349
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002350audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002351{
2352 PlaybackThread *thread = primaryPlaybackThread_l();
2353
2354 if (thread == NULL) {
2355 return 0;
2356 }
2357
Eric Laurentf1c04f92012-08-28 14:26:53 -07002358 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002359}
2360
Eric Laurenta011e352012-03-29 15:51:43 -07002361sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2362 int triggerSession,
2363 int listenerSession,
2364 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002365 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002366{
2367 Mutex::Autolock _l(mLock);
2368
2369 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2370 status_t playStatus = NAME_NOT_FOUND;
2371 status_t recStatus = NAME_NOT_FOUND;
2372 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2373 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2374 if (playStatus == NO_ERROR) {
2375 return event;
2376 }
2377 }
2378 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2379 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2380 if (recStatus == NO_ERROR) {
2381 return event;
2382 }
2383 }
2384 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2385 mPendingSyncEvents.add(event);
2386 } else {
2387 ALOGV("createSyncEvent() invalid event %d", event->type());
2388 event.clear();
2389 }
2390 return event;
2391}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002392
Mathias Agopian65ab4712010-07-14 17:59:35 -07002393// ----------------------------------------------------------------------------
2394// Effect management
2395// ----------------------------------------------------------------------------
2396
2397
Glenn Kastenf587ba52012-01-26 16:25:10 -08002398status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002399{
2400 Mutex::Autolock _l(mLock);
2401 return EffectQueryNumberEffects(numEffects);
2402}
2403
Glenn Kastenf587ba52012-01-26 16:25:10 -08002404status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002405{
2406 Mutex::Autolock _l(mLock);
2407 return EffectQueryEffect(index, descriptor);
2408}
2409
Glenn Kasten5e92a782012-01-30 07:40:52 -08002410status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002411 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002412{
2413 Mutex::Autolock _l(mLock);
2414 return EffectGetDescriptor(pUuid, descriptor);
2415}
2416
2417
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002418sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002419 effect_descriptor_t *pDesc,
2420 const sp<IEffectClient>& effectClient,
2421 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002422 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002423 int sessionId,
2424 status_t *status,
2425 int *id,
2426 int *enabled)
2427{
2428 status_t lStatus = NO_ERROR;
2429 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002430 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002431
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002432 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002433 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002434 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002435
2436 if (pDesc == NULL) {
2437 lStatus = BAD_VALUE;
2438 goto Exit;
2439 }
2440
Eric Laurent84e9a102010-09-23 16:10:16 -07002441 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002442 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002443 lStatus = PERMISSION_DENIED;
2444 goto Exit;
2445 }
2446
Dima Zavinfce7a472011-04-19 22:30:36 -07002447 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002448 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002449 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002450 lStatus = PERMISSION_DENIED;
2451 goto Exit;
2452 }
2453
Mathias Agopian65ab4712010-07-14 17:59:35 -07002454 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002455 if (!EffectIsNullUuid(&pDesc->uuid)) {
2456 // if uuid is specified, request effect descriptor
2457 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2458 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002459 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002460 goto Exit;
2461 }
2462 } else {
2463 // if uuid is not specified, look for an available implementation
2464 // of the required type in effect factory
2465 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002466 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002467 lStatus = BAD_VALUE;
2468 goto Exit;
2469 }
2470 uint32_t numEffects = 0;
2471 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002472 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002473 bool found = false;
2474
2475 lStatus = EffectQueryNumberEffects(&numEffects);
2476 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002477 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002478 goto Exit;
2479 }
2480 for (uint32_t i = 0; i < numEffects; i++) {
2481 lStatus = EffectQueryEffect(i, &desc);
2482 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002483 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002484 continue;
2485 }
2486 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2487 // If matching type found save effect descriptor. If the session is
2488 // 0 and the effect is not auxiliary, continue enumeration in case
2489 // an auxiliary version of this effect type is available
2490 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002491 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002492 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002493 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2494 break;
2495 }
2496 }
2497 }
2498 if (!found) {
2499 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002500 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002501 goto Exit;
2502 }
2503 // For same effect type, chose auxiliary version over insert version if
2504 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002505 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002506 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002507 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002508 }
2509 }
2510
2511 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002512 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002513 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2514 lStatus = INVALID_OPERATION;
2515 goto Exit;
2516 }
2517
Eric Laurent59255e42011-07-27 19:49:51 -07002518 // check recording permission for visualizer
2519 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2520 !recordingAllowed()) {
2521 lStatus = PERMISSION_DENIED;
2522 goto Exit;
2523 }
2524
Mathias Agopian65ab4712010-07-14 17:59:35 -07002525 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002526 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002527 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002528 // if the output returned by getOutputForEffect() is removed before we lock the
2529 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2530 // and we will exit safely
2531 io = AudioSystem::getOutputForEffect(&desc);
2532 ALOGV("createEffect got output %d", io);
2533 }
2534
2535 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002536
2537 // If output is not specified try to find a matching audio session ID in one of the
2538 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002539 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2540 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002541 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002542 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002543 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2544 // output must be specified by AudioPolicyManager when using session
2545 // AUDIO_SESSION_OUTPUT_STAGE
2546 lStatus = BAD_VALUE;
2547 goto Exit;
2548 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002549 // look for the thread where the specified audio session is present
2550 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2551 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2552 io = mPlaybackThreads.keyAt(i);
2553 break;
2554 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002555 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002556 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002557 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2558 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2559 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002560 break;
2561 }
2562 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002563 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002564 // If no output thread contains the requested session ID, default to
2565 // first output. The effect chain will be moved to the correct output
2566 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002567 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002568 io = mPlaybackThreads.keyAt(0);
2569 }
Steve Block3856b092011-10-20 11:56:00 +01002570 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002571 }
2572 ThreadBase *thread = checkRecordThread_l(io);
2573 if (thread == NULL) {
2574 thread = checkPlaybackThread_l(io);
2575 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002576 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002577 lStatus = BAD_VALUE;
2578 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002579 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002580 } else {
2581 // Check if one effect chain was awaiting for an effect to be created on this
2582 // session and used it instead of creating a new one.
2583 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)sessionId);
2584 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002585 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002586 thread->addEffectChain_l(chain);
2587 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002588 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002589
Eric Laurent021cf962014-05-13 10:18:14 -07002590 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002591
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002592 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002593 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2594 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002595 if (handle != 0 && id != NULL) {
2596 *id = handle->id();
2597 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002598 if (handle == 0) {
2599 // remove local strong reference to Client with mClientLock held
2600 Mutex::Autolock _cl(mClientLock);
2601 client.clear();
2602 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002603 }
2604
2605Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002606 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002607 return handle;
2608}
2609
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002610status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2611 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002612{
Steve Block3856b092011-10-20 11:56:00 +01002613 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002614 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002615 Mutex::Autolock _l(mLock);
2616 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002617 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002618 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002619 }
Eric Laurentde070132010-07-13 04:45:46 -07002620 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2621 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002622 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002623 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002624 }
Eric Laurentde070132010-07-13 04:45:46 -07002625 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2626 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002627 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002628 return BAD_VALUE;
2629 }
2630
2631 Mutex::Autolock _dl(dstThread->mLock);
2632 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002633 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002634}
2635
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002636// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002637status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002638 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002639 AudioFlinger::PlaybackThread *dstThread,
2640 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002641{
Steve Block3856b092011-10-20 11:56:00 +01002642 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002643 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002644
Eric Laurent59255e42011-07-27 19:49:51 -07002645 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002646 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002647 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002648 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002649 return INVALID_OPERATION;
2650 }
2651
Andy Hung9a592762014-07-21 21:56:01 -07002652 // Check whether the destination thread has a channel count of FCC_2, which is
2653 // currently required for (most) effects. Prevent moving the effect chain here rather
2654 // than disabling the addEffect_l() call in dstThread below.
Eric Laurentb10352f2014-11-03 16:25:39 -08002655 if ((dstThread->type() == ThreadBase::MIXER || dstThread->type() == ThreadBase::DUPLICATING) &&
2656 dstThread->mChannelCount != FCC_2) {
Andy Hung9a592762014-07-21 21:56:01 -07002657 ALOGW("moveEffectChain_l() effect chain failed because"
2658 " destination thread %p channel count(%u) != %u",
2659 dstThread, dstThread->mChannelCount, FCC_2);
2660 return INVALID_OPERATION;
2661 }
2662
Eric Laurent39e94f82010-07-28 01:32:47 -07002663 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002664 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002665 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002666 // removed.
2667 srcThread->removeEffectChain_l(chain);
2668
2669 // transfer all effects one by one so that new effect chain is created on new thread with
2670 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002671 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002672 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002673 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002674 Vector< sp<EffectModule> > removed;
2675 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002676 while (effect != 0) {
2677 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002678 removed.add(effect);
2679 status = dstThread->addEffect_l(effect);
2680 if (status != NO_ERROR) {
2681 break;
2682 }
Eric Laurentec35a142011-10-05 17:42:25 -07002683 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2684 if (effect->state() == EffectModule::ACTIVE ||
2685 effect->state() == EffectModule::STOPPING) {
2686 effect->start();
2687 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002688 // if the move request is not received from audio policy manager, the effect must be
2689 // re-registered with the new strategy and output
2690 if (dstChain == 0) {
2691 dstChain = effect->chain().promote();
2692 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002693 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002694 status = NO_INIT;
2695 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002696 }
2697 strategy = dstChain->strategy();
2698 }
2699 if (reRegister) {
2700 AudioSystem::unregisterEffect(effect->id());
2701 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002702 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002703 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002704 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002705 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002706 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002707 }
Eric Laurentde070132010-07-13 04:45:46 -07002708 effect = chain->getEffectFromId_l(0);
2709 }
2710
Eric Laurent5baf2af2013-09-12 17:37:00 -07002711 if (status != NO_ERROR) {
2712 for (size_t i = 0; i < removed.size(); i++) {
2713 srcThread->addEffect_l(removed[i]);
2714 if (dstChain != 0 && reRegister) {
2715 AudioSystem::unregisterEffect(removed[i]->id());
2716 AudioSystem::registerEffect(&removed[i]->desc(),
2717 srcThread->id(),
2718 strategy,
2719 sessionId,
2720 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002721 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002722 }
2723 }
2724 }
2725
2726 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002727}
2728
Eric Laurent5baf2af2013-09-12 17:37:00 -07002729bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002730{
2731 if (mGlobalEffectEnableTime != 0 &&
2732 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2733 return true;
2734 }
2735
2736 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2737 sp<EffectChain> ec =
2738 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002739 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002740 return true;
2741 }
2742 }
2743 return false;
2744}
2745
Eric Laurent5baf2af2013-09-12 17:37:00 -07002746void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002747{
2748 Mutex::Autolock _l(mLock);
2749
2750 mGlobalEffectEnableTime = systemTime();
2751
2752 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2753 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2754 if (t->mType == ThreadBase::OFFLOAD) {
2755 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2756 }
2757 }
2758
2759}
2760
Eric Laurentaaa44472014-09-12 17:41:50 -07002761status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2762{
2763 audio_session_t session = (audio_session_t)chain->sessionId();
2764 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2765 ALOGV("putOrphanEffectChain_l session %d index %d", session, index);
2766 if (index >= 0) {
2767 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2768 return ALREADY_EXISTS;
2769 }
2770 mOrphanEffectChains.add(session, chain);
2771 return NO_ERROR;
2772}
2773
2774sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2775{
2776 sp<EffectChain> chain;
2777 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2778 ALOGV("getOrphanEffectChain_l session %d index %d", session, index);
2779 if (index >= 0) {
2780 chain = mOrphanEffectChains.valueAt(index);
2781 mOrphanEffectChains.removeItemsAt(index);
2782 }
2783 return chain;
2784}
2785
2786bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2787{
2788 Mutex::Autolock _l(mLock);
2789 audio_session_t session = (audio_session_t)effect->sessionId();
2790 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2791 ALOGV("updateOrphanEffectChains session %d index %d", session, index);
2792 if (index >= 0) {
2793 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2794 if (chain->removeEffect_l(effect) == 0) {
2795 ALOGV("updateOrphanEffectChains removing effect chain at index %d", index);
2796 mOrphanEffectChains.removeItemsAt(index);
2797 }
2798 return true;
2799 }
2800 return false;
2801}
2802
2803
Glenn Kastenda6ef132013-01-10 12:31:01 -08002804struct Entry {
2805#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2806 char mName[MAX_NAME];
2807};
2808
2809int comparEntry(const void *p1, const void *p2)
2810{
2811 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2812}
2813
Glenn Kasten46909e72013-02-26 09:20:22 -08002814#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002815void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002816{
Eric Laurent81784c32012-11-19 14:55:58 -08002817 NBAIO_Source *teeSource = source.get();
2818 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002819 // .wav rotation
2820 // There is a benign race condition if 2 threads call this simultaneously.
2821 // They would both traverse the directory, but the result would simply be
2822 // failures at unlink() which are ignored. It's also unlikely since
2823 // normally dumpsys is only done by bugreport or from the command line.
2824 char teePath[32+256];
2825 strcpy(teePath, "/data/misc/media");
2826 size_t teePathLen = strlen(teePath);
2827 DIR *dir = opendir(teePath);
2828 teePath[teePathLen++] = '/';
2829 if (dir != NULL) {
2830#define MAX_SORT 20 // number of entries to sort
2831#define MAX_KEEP 10 // number of entries to keep
2832 struct Entry entries[MAX_SORT];
2833 size_t entryCount = 0;
2834 while (entryCount < MAX_SORT) {
2835 struct dirent de;
2836 struct dirent *result = NULL;
2837 int rc = readdir_r(dir, &de, &result);
2838 if (rc != 0) {
2839 ALOGW("readdir_r failed %d", rc);
2840 break;
2841 }
2842 if (result == NULL) {
2843 break;
2844 }
2845 if (result != &de) {
2846 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2847 break;
2848 }
2849 // ignore non .wav file entries
2850 size_t nameLen = strlen(de.d_name);
2851 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2852 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2853 continue;
2854 }
2855 strcpy(entries[entryCount++].mName, de.d_name);
2856 }
2857 (void) closedir(dir);
2858 if (entryCount > MAX_KEEP) {
2859 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2860 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2861 strcpy(&teePath[teePathLen], entries[i].mName);
2862 (void) unlink(teePath);
2863 }
2864 }
2865 } else {
2866 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002867 dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002868 }
2869 }
Eric Laurent81784c32012-11-19 14:55:58 -08002870 char teeTime[16];
2871 struct timeval tv;
2872 gettimeofday(&tv, NULL);
2873 struct tm tm;
2874 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002875 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2876 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2877 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2878 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002879 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07002880 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08002881 char wavHeader[44];
2882 memcpy(wavHeader,
2883 "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",
2884 sizeof(wavHeader));
2885 NBAIO_Format format = teeSource->format();
2886 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002887 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07002888 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002889 wavHeader[22] = channelCount; // number of channels
2890 wavHeader[24] = sampleRate; // sample rate
2891 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07002892 wavHeader[32] = frameSize; // block alignment
2893 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08002894 write(teeFd, wavHeader, sizeof(wavHeader));
2895 size_t total = 0;
2896 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07002897#define TEE_SINK_READ 1024 // frames per I/O operation
2898 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002899 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08002900 size_t count = TEE_SINK_READ;
2901 ssize_t actual = teeSource->read(buffer, count,
2902 AudioBufferProvider::kInvalidPTS);
2903 bool wasFirstRead = firstRead;
2904 firstRead = false;
2905 if (actual <= 0) {
2906 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2907 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002908 }
Eric Laurent81784c32012-11-19 14:55:58 -08002909 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002910 }
Eric Laurent81784c32012-11-19 14:55:58 -08002911 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07002912 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002913 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002914 }
Glenn Kasten329f6512014-08-28 16:23:16 -07002915 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08002916 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002917 uint32_t temp = 44 + total * frameSize - 8;
2918 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002919 write(teeFd, &temp, sizeof(temp));
2920 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002921 temp = total * frameSize;
2922 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002923 write(teeFd, &temp, sizeof(temp));
2924 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002925 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002926 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002927 }
Eric Laurent59255e42011-07-27 19:49:51 -07002928 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002929 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002930 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002931 }
Eric Laurent59255e42011-07-27 19:49:51 -07002932 }
2933 }
2934}
Glenn Kasten46909e72013-02-26 09:20:22 -08002935#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002936
Mathias Agopian65ab4712010-07-14 17:59:35 -07002937// ----------------------------------------------------------------------------
2938
2939status_t AudioFlinger::onTransact(
2940 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2941{
2942 return BnAudioFlinger::onTransact(code, data, reply, flags);
2943}
2944
Mathias Agopian65ab4712010-07-14 17:59:35 -07002945}; // namespace android