blob: 8acfc07ff5599b1cec472244048225bdafb189f4 [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 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700404 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700405 if (clientLocked) {
406 mClientLock.unlock();
407 }
408
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409 dumpInternals(fd, args);
410
411 // dump playback threads
412 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
413 mPlaybackThreads.valueAt(i)->dump(fd, args);
414 }
415
416 // dump record threads
417 for (size_t i = 0; i < mRecordThreads.size(); i++) {
418 mRecordThreads.valueAt(i)->dump(fd, args);
419 }
420
Eric Laurentaaa44472014-09-12 17:41:50 -0700421 // dump orphan effect chains
422 if (mOrphanEffectChains.size() != 0) {
423 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
424 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
425 mOrphanEffectChains.valueAt(i)->dump(fd, args);
426 }
427 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700428 // dump all hardware devs
429 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700430 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700431 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700432 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700433
Glenn Kasten46909e72013-02-26 09:20:22 -0800434#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700435 // dump the serially shared record tee sink
436 if (mRecordTeeSource != 0) {
437 dumpTee(fd, mRecordTeeSource);
438 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800439#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700440
Glenn Kastend65d73c2012-06-22 17:21:07 -0700441 if (locked) {
442 mLock.unlock();
443 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800444
445 // append a copy of media.log here by forwarding fd to it, but don't attempt
446 // to lookup the service if it's not running, as it will block for a second
447 if (mLogMemoryDealer != 0) {
448 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
449 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700450 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800451 Vector<String16> args;
452 binder->dump(fd, args);
453 }
454 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700455 }
456 return NO_ERROR;
457}
458
Eric Laurent021cf962014-05-13 10:18:14 -0700459sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800460{
Eric Laurent021cf962014-05-13 10:18:14 -0700461 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800462 // If pid is already in the mClients wp<> map, then use that entry
463 // (for which promote() is always != 0), otherwise create a new entry and Client.
464 sp<Client> client = mClients.valueFor(pid).promote();
465 if (client == 0) {
466 client = new Client(this, pid);
467 mClients.add(pid, client);
468 }
469
470 return client;
471}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700472
Glenn Kasten9e58b552013-01-18 15:09:48 -0800473sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
474{
Glenn Kasten481fb672013-09-30 14:39:28 -0700475 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800476 if (mLogMemoryDealer == 0) {
477 return new NBLog::Writer();
478 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800479 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700480 // Similarly if we can't contact the media.log service, also return a dummy writer
481 if (binder == 0) {
482 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800483 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700484 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
485 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
486 // If allocation fails, consult the vector of previously unregistered writers
487 // and garbage-collect one or more them until an allocation succeeds
488 if (shared == 0) {
489 Mutex::Autolock _l(mUnregisteredWritersLock);
490 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
491 {
492 // Pick the oldest stale writer to garbage-collect
493 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
494 mUnregisteredWriters.removeAt(0);
495 mediaLogService->unregisterWriter(iMemory);
496 // Now the media.log remote reference to IMemory is gone. When our last local
497 // reference to IMemory also drops to zero at end of this block,
498 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
499 }
500 // Re-attempt the allocation
501 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
502 if (shared != 0) {
503 goto success;
504 }
505 }
506 // Even after garbage-collecting all old writers, there is still not enough memory,
507 // so return a dummy writer
508 return new NBLog::Writer();
509 }
510success:
511 mediaLogService->registerWriter(shared, size, name);
512 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800513}
514
515void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
516{
Glenn Kasten685ef092013-02-04 08:15:34 -0800517 if (writer == 0) {
518 return;
519 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800520 sp<IMemory> iMemory(writer->getIMemory());
521 if (iMemory == 0) {
522 return;
523 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700524 // Rather than removing the writer immediately, append it to a queue of old writers to
525 // be garbage-collected later. This allows us to continue to view old logs for a while.
526 Mutex::Autolock _l(mUnregisteredWritersLock);
527 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800528}
529
Mathias Agopian65ab4712010-07-14 17:59:35 -0700530// IAudioFlinger interface
531
532
533sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800534 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700535 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800536 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700537 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800538 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800539 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700540 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800541 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800542 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543 int *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800544 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700545 status_t *status)
546{
547 sp<PlaybackThread::Track> track;
548 sp<TrackHandle> trackHandle;
549 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550 status_t lStatus;
551 int lSessionId;
552
Glenn Kasten263709e2012-01-06 08:40:01 -0800553 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
554 // but if someone uses binder directly they could bypass that and cause us to crash
555 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000556 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700557 lStatus = BAD_VALUE;
558 goto Exit;
559 }
560
Glenn Kasten53b5d092014-02-05 10:00:23 -0800561 // further sample rate checks are performed by createTrack_l() depending on the thread type
562 if (sampleRate == 0) {
563 ALOGE("createTrack() invalid sample rate %u", sampleRate);
564 lStatus = BAD_VALUE;
565 goto Exit;
566 }
567
568 // further channel mask checks are performed by createTrack_l() depending on the thread type
569 if (!audio_is_output_channel(channelMask)) {
570 ALOGE("createTrack() invalid channel mask %#x", channelMask);
571 lStatus = BAD_VALUE;
572 goto Exit;
573 }
574
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700575 // further format checks are performed by createTrack_l() depending on the thread type
576 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800577 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700578 lStatus = BAD_VALUE;
579 goto Exit;
580 }
581
Glenn Kasten663c2242013-09-24 11:52:37 -0700582 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
583 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
584 lStatus = BAD_VALUE;
585 goto Exit;
586 }
587
Mathias Agopian65ab4712010-07-14 17:59:35 -0700588 {
589 Mutex::Autolock _l(mLock);
590 PlaybackThread *thread = checkPlaybackThread_l(output);
591 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800592 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593 lStatus = BAD_VALUE;
594 goto Exit;
595 }
596
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800597 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -0700598 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700599
Glenn Kastene848bd92014-03-13 15:00:32 -0700600 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700601 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700602 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700603 // check if an effect chain with the same session ID is present on another
604 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700605 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700606 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
607 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700608 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700609 if (sessions & PlaybackThread::EFFECT_SESSION) {
610 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700611 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700612 }
Eric Laurentde070132010-07-13 04:45:46 -0700613 }
614 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700615 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700616 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700617 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700618 if (sessionId != NULL) {
619 *sessionId = lSessionId;
620 }
621 }
Steve Block3856b092011-10-20 11:56:00 +0100622 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700623
624 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800625 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800626 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700627 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700628
629 // move effect chain to this output thread if an effect on same session was waiting
630 // for a track to be created
631 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700632 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700633 Mutex::Autolock _dl(thread->mLock);
634 Mutex::Autolock _sl(effectThread->mLock);
635 moveEffectChain_l(lSessionId, effectThread, thread, true);
636 }
Eric Laurenta011e352012-03-29 15:51:43 -0700637
638 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700639 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700640 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
641 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700642 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700643 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700644 } else {
645 mPendingSyncEvents[i]->cancel();
646 }
Eric Laurenta011e352012-03-29 15:51:43 -0700647 mPendingSyncEvents.removeAt(i);
648 i--;
649 }
650 }
651 }
Glenn Kastene198c362013-08-13 09:13:36 -0700652
Eric Laurentfa90e842014-10-17 18:12:31 -0700653 setAudioHwSyncForSession_l(thread, (audio_session_t)lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700654 }
Glenn Kastene198c362013-08-13 09:13:36 -0700655
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700656 if (lStatus != NO_ERROR) {
657 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700658 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700659 // Don't hold mClientLock when releasing the reference on the track as the
660 // destructor will acquire it.
661 {
662 Mutex::Autolock _cl(mClientLock);
663 client.clear();
664 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700665 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700666 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700667 }
668
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700669 // return handle to client
670 trackHandle = new TrackHandle(track);
671
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700673 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700674 return trackHandle;
675}
676
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800677uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678{
679 Mutex::Autolock _l(mLock);
680 PlaybackThread *thread = checkPlaybackThread_l(output);
681 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000682 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700683 return 0;
684 }
685 return thread->sampleRate();
686}
687
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800688audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700689{
690 Mutex::Autolock _l(mLock);
691 PlaybackThread *thread = checkPlaybackThread_l(output);
692 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000693 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800694 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700695 }
696 return thread->format();
697}
698
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800699size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700700{
701 Mutex::Autolock _l(mLock);
702 PlaybackThread *thread = checkPlaybackThread_l(output);
703 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000704 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700705 return 0;
706 }
Glenn Kasten58912562012-04-03 10:45:00 -0700707 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
708 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700709 return thread->frameCount();
710}
711
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800712uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700713{
714 Mutex::Autolock _l(mLock);
715 PlaybackThread *thread = checkPlaybackThread_l(output);
716 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800717 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700718 return 0;
719 }
720 return thread->latency();
721}
722
723status_t AudioFlinger::setMasterVolume(float value)
724{
Eric Laurenta1884f92011-08-23 08:25:03 -0700725 status_t ret = initCheck();
726 if (ret != NO_ERROR) {
727 return ret;
728 }
729
Mathias Agopian65ab4712010-07-14 17:59:35 -0700730 // check calling permissions
731 if (!settingsAllowed()) {
732 return PERMISSION_DENIED;
733 }
734
Eric Laurenta4c5a552012-03-29 10:12:40 -0700735 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700736 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700737
John Grossmanee578c02012-07-23 17:05:46 -0700738 // Set master volume in the HALs which support it.
739 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
740 AutoMutex lock(mHardwareLock);
741 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800742
John Grossmanee578c02012-07-23 17:05:46 -0700743 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
744 if (dev->canSetMasterVolume()) {
745 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800746 }
John Grossmanee578c02012-07-23 17:05:46 -0700747 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700748 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700749
John Grossmanee578c02012-07-23 17:05:46 -0700750 // Now set the master volume in each playback thread. Playback threads
751 // assigned to HALs which do not have master volume support will apply
752 // master volume during the mix operation. Threads with HALs which do
753 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800754 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700755 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700756
757 return NO_ERROR;
758}
759
Glenn Kastenf78aee72012-01-04 11:00:47 -0800760status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700761{
Eric Laurenta1884f92011-08-23 08:25:03 -0700762 status_t ret = initCheck();
763 if (ret != NO_ERROR) {
764 return ret;
765 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700766
767 // check calling permissions
768 if (!settingsAllowed()) {
769 return PERMISSION_DENIED;
770 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800771 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000772 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700773 return BAD_VALUE;
774 }
775
776 { // scope for the lock
777 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700778 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700779 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700780 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700781 mHardwareStatus = AUDIO_HW_IDLE;
782 }
783
784 if (NO_ERROR == ret) {
785 Mutex::Autolock _l(mLock);
786 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800787 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700788 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700789 }
790
791 return ret;
792}
793
794status_t AudioFlinger::setMicMute(bool state)
795{
Eric Laurenta1884f92011-08-23 08:25:03 -0700796 status_t ret = initCheck();
797 if (ret != NO_ERROR) {
798 return ret;
799 }
800
Mathias Agopian65ab4712010-07-14 17:59:35 -0700801 // check calling permissions
802 if (!settingsAllowed()) {
803 return PERMISSION_DENIED;
804 }
805
806 AutoMutex lock(mHardwareLock);
807 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700808 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
809 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
810 status_t result = dev->set_mic_mute(dev, state);
811 if (result != NO_ERROR) {
812 ret = result;
813 }
814 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700815 mHardwareStatus = AUDIO_HW_IDLE;
816 return ret;
817}
818
819bool AudioFlinger::getMicMute() const
820{
Eric Laurenta1884f92011-08-23 08:25:03 -0700821 status_t ret = initCheck();
822 if (ret != NO_ERROR) {
823 return false;
824 }
825
Dima Zavinfce7a472011-04-19 22:30:36 -0700826 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800827 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700828 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700829 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700830 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700831 mHardwareStatus = AUDIO_HW_IDLE;
832 return state;
833}
834
835status_t AudioFlinger::setMasterMute(bool muted)
836{
John Grossmand8f178d2012-07-20 14:51:35 -0700837 status_t ret = initCheck();
838 if (ret != NO_ERROR) {
839 return ret;
840 }
841
Mathias Agopian65ab4712010-07-14 17:59:35 -0700842 // check calling permissions
843 if (!settingsAllowed()) {
844 return PERMISSION_DENIED;
845 }
846
John Grossmanee578c02012-07-23 17:05:46 -0700847 Mutex::Autolock _l(mLock);
848 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700849
John Grossmanee578c02012-07-23 17:05:46 -0700850 // Set master mute in the HALs which support it.
851 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
852 AutoMutex lock(mHardwareLock);
853 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700854
John Grossmanee578c02012-07-23 17:05:46 -0700855 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
856 if (dev->canSetMasterMute()) {
857 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700858 }
John Grossmanee578c02012-07-23 17:05:46 -0700859 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700860 }
861
John Grossmanee578c02012-07-23 17:05:46 -0700862 // Now set the master mute in each playback thread. Playback threads
863 // assigned to HALs which do not have master mute support will apply master
864 // mute during the mix operation. Threads with HALs which do support master
865 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800866 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700867 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700868
869 return NO_ERROR;
870}
871
872float AudioFlinger::masterVolume() const
873{
Glenn Kasten98067102011-12-13 11:47:54 -0800874 Mutex::Autolock _l(mLock);
875 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700876}
877
878bool AudioFlinger::masterMute() const
879{
Glenn Kasten98067102011-12-13 11:47:54 -0800880 Mutex::Autolock _l(mLock);
881 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700882}
883
John Grossman4ff14ba2012-02-08 16:37:41 -0800884float AudioFlinger::masterVolume_l() const
885{
John Grossman4ff14ba2012-02-08 16:37:41 -0800886 return mMasterVolume;
887}
888
John Grossmand8f178d2012-07-20 14:51:35 -0700889bool AudioFlinger::masterMute_l() const
890{
John Grossmanee578c02012-07-23 17:05:46 -0700891 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700892}
893
Eric Laurent223fd5c2014-11-11 13:43:36 -0800894status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
895{
896 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
897 ALOGW("setStreamVolume() invalid stream %d", stream);
898 return BAD_VALUE;
899 }
900 pid_t caller = IPCThreadState::self()->getCallingPid();
901 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
902 ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
903 return PERMISSION_DENIED;
904 }
905
906 return NO_ERROR;
907}
908
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800909status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
910 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911{
912 // check calling permissions
913 if (!settingsAllowed()) {
914 return PERMISSION_DENIED;
915 }
916
Eric Laurent223fd5c2014-11-11 13:43:36 -0800917 status_t status = checkStreamType(stream);
918 if (status != NO_ERROR) {
919 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700920 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800921 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700922
923 AutoMutex lock(mLock);
924 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700925 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700926 thread = checkPlaybackThread_l(output);
927 if (thread == NULL) {
928 return BAD_VALUE;
929 }
930 }
931
932 mStreamTypes[stream].volume = value;
933
934 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800935 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700936 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700937 }
938 } else {
939 thread->setStreamVolume(stream, value);
940 }
941
942 return NO_ERROR;
943}
944
Glenn Kastenfff6d712012-01-12 16:38:12 -0800945status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946{
947 // check calling permissions
948 if (!settingsAllowed()) {
949 return PERMISSION_DENIED;
950 }
951
Eric Laurent223fd5c2014-11-11 13:43:36 -0800952 status_t status = checkStreamType(stream);
953 if (status != NO_ERROR) {
954 return status;
955 }
956 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
957
958 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000959 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960 return BAD_VALUE;
961 }
962
Eric Laurent93575202011-01-18 18:39:02 -0800963 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700964 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700965 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700966 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700967
968 return NO_ERROR;
969}
970
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800971float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700972{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800973 status_t status = checkStreamType(stream);
974 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700975 return 0.0f;
976 }
977
978 AutoMutex lock(mLock);
979 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -0700980 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700981 PlaybackThread *thread = checkPlaybackThread_l(output);
982 if (thread == NULL) {
983 return 0.0f;
984 }
985 volume = thread->streamVolume(stream);
986 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800987 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700988 }
989
990 return volume;
991}
992
Glenn Kastenfff6d712012-01-12 16:38:12 -0800993bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700994{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800995 status_t status = checkStreamType(stream);
996 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700997 return true;
998 }
999
Glenn Kasten6637baa2012-01-09 09:40:36 -08001000 AutoMutex lock(mLock);
1001 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001002}
1003
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001004status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001005{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001006 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1007 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001008
Mathias Agopian65ab4712010-07-14 17:59:35 -07001009 // check calling permissions
1010 if (!settingsAllowed()) {
1011 return PERMISSION_DENIED;
1012 }
1013
Glenn Kasten142f5192014-03-25 17:44:59 -07001014 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1015 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001016 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -07001017 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001018 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001019 AutoMutex lock(mHardwareLock);
1020 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1021 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1022 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1023 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1024 final_result = result ?: final_result;
1025 }
1026 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001027 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001028 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1029 AudioParameter param = AudioParameter(keyValuePairs);
1030 String8 value;
1031 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001032 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1033 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001034 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1035 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001036 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001037 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1038 // collect all of the thread's session IDs
1039 KeyedVector<int, bool> ids = thread->sessionIds();
1040 // suspend effects associated with those session IDs
1041 for (size_t j = 0; j < ids.size(); ++j) {
1042 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001043 thread->setEffectSuspended(FX_IID_AEC,
1044 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001045 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001046 thread->setEffectSuspended(FX_IID_NS,
1047 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001048 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001049 }
1050 }
Eric Laurentbee53372011-08-29 12:42:48 -07001051 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001052 }
1053 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001054 String8 screenState;
1055 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1056 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001057 if (isOff != (AudioFlinger::mScreenState & 1)) {
1058 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001059 }
1060 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001061 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062 }
1063
1064 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1065 // and the thread is exited once the lock is released
1066 sp<ThreadBase> thread;
1067 {
1068 Mutex::Autolock _l(mLock);
1069 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001070 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001071 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001072 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001073 // indicate output device change to all input threads for pre processing
1074 AudioParameter param = AudioParameter(keyValuePairs);
1075 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001076 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1077 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001078 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1079 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1080 }
1081 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001082 }
1083 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001084 if (thread != 0) {
1085 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001086 }
1087 return BAD_VALUE;
1088}
1089
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001090String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001091{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001092 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1093 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001094
Eric Laurenta4c5a552012-03-29 10:12:40 -07001095 Mutex::Autolock _l(mLock);
1096
Glenn Kasten142f5192014-03-25 17:44:59 -07001097 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001098 String8 out_s8;
1099
Dima Zavin799a70e2011-04-18 16:57:27 -07001100 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001101 char *s;
1102 {
1103 AutoMutex lock(mHardwareLock);
1104 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001105 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001106 s = dev->get_parameters(dev, keys.string());
1107 mHardwareStatus = AUDIO_HW_IDLE;
1108 }
John Grossmanef7740b2012-02-09 11:28:36 -08001109 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001110 free(s);
1111 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001112 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001113 }
1114
Mathias Agopian65ab4712010-07-14 17:59:35 -07001115 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1116 if (playbackThread != NULL) {
1117 return playbackThread->getParameters(keys);
1118 }
1119 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1120 if (recordThread != NULL) {
1121 return recordThread->getParameters(keys);
1122 }
1123 return String8("");
1124}
1125
Glenn Kastendd8104c2012-07-02 12:42:44 -07001126size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1127 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001128{
Eric Laurenta1884f92011-08-23 08:25:03 -07001129 status_t ret = initCheck();
1130 if (ret != NO_ERROR) {
1131 return 0;
1132 }
1133
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001134 AutoMutex lock(mHardwareLock);
1135 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001136 audio_config_t config;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001137 memset(&config, 0, sizeof(config));
1138 config.sample_rate = sampleRate;
1139 config.channel_mask = channelMask;
1140 config.format = format;
1141
John Grossmanee578c02012-07-23 17:05:46 -07001142 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1143 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001144 mHardwareStatus = AUDIO_HW_IDLE;
1145 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001146}
1147
Glenn Kasten5f972c02014-01-13 09:59:31 -08001148uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001149{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001150 Mutex::Autolock _l(mLock);
1151
1152 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1153 if (recordThread != NULL) {
1154 return recordThread->getInputFramesLost();
1155 }
1156 return 0;
1157}
1158
1159status_t AudioFlinger::setVoiceVolume(float value)
1160{
Eric Laurenta1884f92011-08-23 08:25:03 -07001161 status_t ret = initCheck();
1162 if (ret != NO_ERROR) {
1163 return ret;
1164 }
1165
Mathias Agopian65ab4712010-07-14 17:59:35 -07001166 // check calling permissions
1167 if (!settingsAllowed()) {
1168 return PERMISSION_DENIED;
1169 }
1170
1171 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001172 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001173 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001174 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001175 mHardwareStatus = AUDIO_HW_IDLE;
1176
1177 return ret;
1178}
1179
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001180status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001181 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001182{
1183 status_t status;
1184
1185 Mutex::Autolock _l(mLock);
1186
1187 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1188 if (playbackThread != NULL) {
1189 return playbackThread->getRenderPosition(halFrames, dspFrames);
1190 }
1191
1192 return BAD_VALUE;
1193}
1194
1195void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1196{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001197 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001198 if (client == 0) {
1199 return;
1200 }
Eric Laurent021cf962014-05-13 10:18:14 -07001201 bool clientAdded = false;
1202 {
1203 Mutex::Autolock _cl(mClientLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001204
Eric Laurent021cf962014-05-13 10:18:14 -07001205 pid_t pid = IPCThreadState::self()->getCallingPid();
1206 if (mNotificationClients.indexOfKey(pid) < 0) {
1207 sp<NotificationClient> notificationClient = new NotificationClient(this,
1208 client,
1209 pid);
1210 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211
Eric Laurent021cf962014-05-13 10:18:14 -07001212 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001213
Eric Laurent021cf962014-05-13 10:18:14 -07001214 sp<IBinder> binder = client->asBinder();
1215 binder->linkToDeath(notificationClient);
1216 clientAdded = true;
1217 }
1218 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001219
Eric Laurent021cf962014-05-13 10:18:14 -07001220 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001221 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent021cf962014-05-13 10:18:14 -07001222 if (clientAdded) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001223 // the config change is always sent from playback or record threads to avoid deadlock
1224 // with AudioSystem::gLock
1225 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001226 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001227 }
1228
1229 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001230 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001231 }
1232 }
1233}
1234
1235void AudioFlinger::removeNotificationClient(pid_t pid)
1236{
1237 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001238 {
1239 Mutex::Autolock _cl(mClientLock);
1240 mNotificationClients.removeItem(pid);
1241 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001242
Steve Block3856b092011-10-20 11:56:00 +01001243 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001244 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001245 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001246 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001247 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001248 ALOGV(" pid %d @ %d", ref->mPid, i);
1249 if (ref->mPid == pid) {
1250 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001251 mAudioSessionRefs.removeAt(i);
1252 delete ref;
1253 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001254 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001255 } else {
1256 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001257 }
1258 }
1259 if (removed) {
1260 purgeStaleEffects_l();
1261 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001262}
1263
Eric Laurent021cf962014-05-13 10:18:14 -07001264void AudioFlinger::audioConfigChanged(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001265{
Eric Laurent021cf962014-05-13 10:18:14 -07001266 Mutex::Autolock _l(mClientLock);
1267 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001268 for (size_t i = 0; i < size; i++) {
Eric Laurent021cf962014-05-13 10:18:14 -07001269 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event,
Eric Laurent10351942014-05-08 18:49:52 -07001270 ioHandle,
1271 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001272 }
1273}
1274
Eric Laurent021cf962014-05-13 10:18:14 -07001275// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001276void AudioFlinger::removeClient_l(pid_t pid)
1277{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001278 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001279 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001280 mClients.removeItem(pid);
1281}
1282
Eric Laurent717e1282012-06-29 16:36:52 -07001283// getEffectThread_l() must be called with AudioFlinger::mLock held
1284sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1285{
1286 sp<PlaybackThread> thread;
1287
1288 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1289 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1290 ALOG_ASSERT(thread == 0);
1291 thread = mPlaybackThreads.valueAt(i);
1292 }
1293 }
1294
1295 return thread;
1296}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001297
Mathias Agopian65ab4712010-07-14 17:59:35 -07001298
Mathias Agopian65ab4712010-07-14 17:59:35 -07001299
1300// ----------------------------------------------------------------------------
1301
1302AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1303 : RefBase(),
1304 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001305 // 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 -07001306 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001307 mPid(pid),
1308 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001309{
1310 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1311}
1312
Eric Laurent021cf962014-05-13 10:18:14 -07001313// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001314AudioFlinger::Client::~Client()
1315{
1316 mAudioFlinger->removeClient_l(mPid);
1317}
1318
Glenn Kasten435dbe62012-01-30 10:15:48 -08001319sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001320{
1321 return mMemoryDealer;
1322}
1323
John Grossman4ff14ba2012-02-08 16:37:41 -08001324// Reserve one of the limited slots for a timed audio track associated
1325// with this client
1326bool AudioFlinger::Client::reserveTimedTrack()
1327{
1328 const int kMaxTimedTracksPerClient = 4;
1329
1330 Mutex::Autolock _l(mTimedTrackLock);
1331
1332 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1333 ALOGW("can not create timed track - pid %d has exceeded the limit",
1334 mPid);
1335 return false;
1336 }
1337
1338 mTimedTrackCount++;
1339 return true;
1340}
1341
1342// Release a slot for a timed audio track
1343void AudioFlinger::Client::releaseTimedTrack()
1344{
1345 Mutex::Autolock _l(mTimedTrackLock);
1346 mTimedTrackCount--;
1347}
1348
Mathias Agopian65ab4712010-07-14 17:59:35 -07001349// ----------------------------------------------------------------------------
1350
1351AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1352 const sp<IAudioFlingerClient>& client,
1353 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001354 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001355{
1356}
1357
1358AudioFlinger::NotificationClient::~NotificationClient()
1359{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001360}
1361
Glenn Kasten0f11b512014-01-31 16:18:54 -08001362void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001363{
1364 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001365 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001366}
1367
Mathias Agopian65ab4712010-07-14 17:59:35 -07001368
1369// ----------------------------------------------------------------------------
1370
Jeff Brown893a5642013-08-16 20:19:26 -07001371static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1372 return audio_is_remote_submix_device(inDevice);
1373}
1374
Mathias Agopian65ab4712010-07-14 17:59:35 -07001375sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001376 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001377 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001378 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001379 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001380 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001381 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001382 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001383 int *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001384 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001385 sp<IMemory>& cblk,
1386 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001387 status_t *status)
1388{
1389 sp<RecordThread::RecordTrack> recordTrack;
1390 sp<RecordHandle> recordHandle;
1391 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001392 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001393 int lSessionId;
1394
Glenn Kastend776ac62014-05-07 09:16:09 -07001395 cblk.clear();
1396 buffers.clear();
1397
Mathias Agopian65ab4712010-07-14 17:59:35 -07001398 // check calling permissions
1399 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001400 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001401 lStatus = PERMISSION_DENIED;
1402 goto Exit;
1403 }
1404
Glenn Kasten53b5d092014-02-05 10:00:23 -08001405 // further sample rate checks are performed by createRecordTrack_l()
1406 if (sampleRate == 0) {
1407 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1408 lStatus = BAD_VALUE;
1409 goto Exit;
1410 }
1411
Glenn Kasten0e0e8462014-03-13 15:02:40 -07001412 // we don't yet support anything other than 16-bit PCM
1413 if (!(audio_is_valid_format(format) &&
1414 audio_is_linear_pcm(format) && format == AUDIO_FORMAT_PCM_16_BIT)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001415 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001416 lStatus = BAD_VALUE;
1417 goto Exit;
1418 }
1419
Glenn Kasten53b5d092014-02-05 10:00:23 -08001420 // further channel mask checks are performed by createRecordTrack_l()
1421 if (!audio_is_input_channel(channelMask)) {
1422 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1423 lStatus = BAD_VALUE;
1424 goto Exit;
1425 }
1426
Glenn Kasten05997e22014-03-13 15:08:33 -07001427 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001428 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001429 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001430 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001431 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001432 lStatus = BAD_VALUE;
1433 goto Exit;
1434 }
1435
Jeff Brown893a5642013-08-16 20:19:26 -07001436 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1437 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001438 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001439 lStatus = PERMISSION_DENIED;
1440 goto Exit;
1441 }
1442
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001443 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001444 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001445
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001446 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001447 lSessionId = *sessionId;
1448 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001449 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001450 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001451 if (sessionId != NULL) {
1452 *sessionId = lSessionId;
1453 }
1454 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001455 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001456
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001457 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001458 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001459 frameCount, lSessionId, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001460 IPCThreadState::self()->getCallingUid(),
1461 flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001462 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001463
1464 if (lStatus == NO_ERROR) {
1465 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1466 // session and move it to this thread.
1467 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)lSessionId);
1468 if (chain != 0) {
1469 Mutex::Autolock _l(thread->mLock);
1470 thread->addEffectChain_l(chain);
1471 }
1472 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001473 }
Glenn Kastene198c362013-08-13 09:13:36 -07001474
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001475 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001476 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001477 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001478 // Don't hold mClientLock when releasing the reference on the track as the
1479 // destructor will acquire it.
1480 {
1481 Mutex::Autolock _cl(mClientLock);
1482 client.clear();
1483 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001484 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001485 goto Exit;
1486 }
1487
Glenn Kastend776ac62014-05-07 09:16:09 -07001488 cblk = recordTrack->getCblk();
1489 buffers = recordTrack->getBuffers();
1490
Glenn Kasten2fc14732013-08-05 14:58:14 -07001491 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001492 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001493
1494Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001495 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001496 return recordHandle;
1497}
1498
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001499
1500
Mathias Agopian65ab4712010-07-14 17:59:35 -07001501// ----------------------------------------------------------------------------
1502
Eric Laurenta4c5a552012-03-29 10:12:40 -07001503audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1504{
Eric Laurent44622db2014-08-01 19:00:33 -07001505 if (name == NULL) {
1506 return 0;
1507 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001508 if (!settingsAllowed()) {
1509 return 0;
1510 }
1511 Mutex::Autolock _l(mLock);
1512 return loadHwModule_l(name);
1513}
1514
1515// loadHwModule_l() must be called with AudioFlinger::mLock held
1516audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1517{
1518 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1519 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1520 ALOGW("loadHwModule() module %s already loaded", name);
1521 return mAudioHwDevs.keyAt(i);
1522 }
1523 }
1524
Eric Laurenta4c5a552012-03-29 10:12:40 -07001525 audio_hw_device_t *dev;
1526
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001527 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001528 if (rc) {
1529 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1530 return 0;
1531 }
1532
1533 mHardwareStatus = AUDIO_HW_INIT;
1534 rc = dev->init_check(dev);
1535 mHardwareStatus = AUDIO_HW_IDLE;
1536 if (rc) {
1537 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1538 return 0;
1539 }
1540
John Grossmanee578c02012-07-23 17:05:46 -07001541 // Check and cache this HAL's level of support for master mute and master
1542 // volume. If this is the first HAL opened, and it supports the get
1543 // methods, use the initial values provided by the HAL as the current
1544 // master mute and volume settings.
1545
1546 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1547 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001548 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001549
1550 if (0 == mAudioHwDevs.size()) {
1551 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1552 if (NULL != dev->get_master_volume) {
1553 float mv;
1554 if (OK == dev->get_master_volume(dev, &mv)) {
1555 mMasterVolume = mv;
1556 }
1557 }
1558
1559 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1560 if (NULL != dev->get_master_mute) {
1561 bool mm;
1562 if (OK == dev->get_master_mute(dev, &mm)) {
1563 mMasterMute = mm;
1564 }
1565 }
1566 }
1567
Eric Laurenta4c5a552012-03-29 10:12:40 -07001568 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001569 if ((NULL != dev->set_master_volume) &&
1570 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1571 flags = static_cast<AudioHwDevice::Flags>(flags |
1572 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1573 }
1574
1575 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1576 if ((NULL != dev->set_master_mute) &&
1577 (OK == dev->set_master_mute(dev, mMasterMute))) {
1578 flags = static_cast<AudioHwDevice::Flags>(flags |
1579 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1580 }
1581
Eric Laurenta4c5a552012-03-29 10:12:40 -07001582 mHardwareStatus = AUDIO_HW_IDLE;
1583 }
1584
1585 audio_module_handle_t handle = nextUniqueId();
Eric Laurent83b88082014-06-20 18:31:16 -07001586 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001587
1588 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001589 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001590
1591 return handle;
1592
1593}
1594
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001595// ----------------------------------------------------------------------------
1596
Glenn Kasten3b16c762012-11-14 08:44:39 -08001597uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001598{
1599 Mutex::Autolock _l(mLock);
1600 PlaybackThread *thread = primaryPlaybackThread_l();
1601 return thread != NULL ? thread->sampleRate() : 0;
1602}
1603
Glenn Kastene33054e2012-11-14 12:54:39 -08001604size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001605{
1606 Mutex::Autolock _l(mLock);
1607 PlaybackThread *thread = primaryPlaybackThread_l();
1608 return thread != NULL ? thread->frameCountHAL() : 0;
1609}
1610
1611// ----------------------------------------------------------------------------
1612
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001613status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1614{
1615 uid_t uid = IPCThreadState::self()->getCallingUid();
1616 if (uid != AID_SYSTEM) {
1617 return PERMISSION_DENIED;
1618 }
1619 Mutex::Autolock _l(mLock);
1620 if (mIsDeviceTypeKnown) {
1621 return INVALID_OPERATION;
1622 }
1623 mIsLowRamDevice = isLowRamDevice;
1624 mIsDeviceTypeKnown = true;
1625 return NO_ERROR;
1626}
1627
Eric Laurent93c3d412014-08-01 14:48:35 -07001628audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1629{
1630 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001631
1632 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1633 if (index >= 0) {
1634 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1635 mHwAvSyncIds.valueAt(index), sessionId);
1636 return mHwAvSyncIds.valueAt(index);
1637 }
1638
1639 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1640 if (dev == NULL) {
1641 return AUDIO_HW_SYNC_INVALID;
1642 }
1643 char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1644 AudioParameter param = AudioParameter(String8(reply));
1645 free(reply);
1646
1647 int value;
1648 if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1649 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1650 return AUDIO_HW_SYNC_INVALID;
1651 }
1652
1653 // allow only one session for a given HW A/V sync ID.
1654 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1655 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1656 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1657 value, mHwAvSyncIds.keyAt(i));
1658 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001659 break;
1660 }
1661 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001662
1663 mHwAvSyncIds.add(sessionId, value);
1664
1665 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1666 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1667 uint32_t sessions = thread->hasAudioSession(sessionId);
1668 if (sessions & PlaybackThread::TRACK_SESSION) {
1669 AudioParameter param = AudioParameter();
1670 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1671 thread->setParameters(param.toString());
1672 break;
1673 }
1674 }
1675
1676 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1677 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001678}
1679
Eric Laurentfa90e842014-10-17 18:12:31 -07001680// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1681void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1682{
1683 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1684 if (index >= 0) {
1685 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1686 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1687 AudioParameter param = AudioParameter();
1688 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1689 thread->setParameters(param.toString());
1690 }
1691}
1692
1693
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001694// ----------------------------------------------------------------------------
1695
Eric Laurent83b88082014-06-20 18:31:16 -07001696
1697sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001698 audio_io_handle_t *output,
1699 audio_config_t *config,
1700 audio_devices_t devices,
1701 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001702 audio_output_flags_t flags)
1703{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001704 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001705 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001706 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001707 }
1708
1709 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentcf2c0212014-07-25 16:20:43 -07001710 if (*output == AUDIO_IO_HANDLE_NONE) {
1711 *output = nextUniqueId();
1712 }
Eric Laurent83b88082014-06-20 18:31:16 -07001713
1714 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1715
1716 audio_stream_out_t *outStream = NULL;
1717
1718 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001719 // This if statement allows overriding the audio policy settings
1720 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1721 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1722 // Check only for Normal Mixing mode
1723 if (kEnableExtendedPrecision) {
1724 // Specify format (uncomment one below to choose)
1725 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1726 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1727 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1728 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001729 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001730 }
1731 if (kEnableExtendedChannels) {
1732 // Specify channel mask (uncomment one below to choose)
1733 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1734 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1735 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1736 }
Eric Laurent83b88082014-06-20 18:31:16 -07001737 }
1738
1739 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001740 *output,
1741 devices,
1742 flags,
1743 config,
1744 &outStream,
1745 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001746
1747 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001748 ALOGV("openOutput_l() openOutputStream returned output %p, sampleRate %d, Format %#x, "
Andy Hung9a592762014-07-21 21:56:01 -07001749 "channelMask %#x, status %d",
Eric Laurent83b88082014-06-20 18:31:16 -07001750 outStream,
1751 config->sample_rate,
1752 config->format,
1753 config->channel_mask,
1754 status);
1755
1756 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001757 AudioStreamOut *outputStream = new AudioStreamOut(outHwDev, outStream, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001758
1759 PlaybackThread *thread;
1760 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001761 thread = new OffloadThread(this, outputStream, *output, devices);
1762 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001763 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1764 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001765 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001766 thread = new DirectOutputThread(this, outputStream, *output, devices);
1767 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001768 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001769 thread = new MixerThread(this, outputStream, *output, devices);
1770 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001771 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001772 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001773 return thread;
1774 }
1775
1776 return 0;
1777}
1778
Eric Laurentcf2c0212014-07-25 16:20:43 -07001779status_t AudioFlinger::openOutput(audio_module_handle_t module,
1780 audio_io_handle_t *output,
1781 audio_config_t *config,
1782 audio_devices_t *devices,
1783 const String8& address,
1784 uint32_t *latencyMs,
1785 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001786{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001787 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001788 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001789 (devices != NULL) ? *devices : 0,
1790 config->sample_rate,
1791 config->format,
1792 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001793 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001794
Eric Laurentcf2c0212014-07-25 16:20:43 -07001795 if (*devices == AUDIO_DEVICE_NONE) {
1796 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001797 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001798
Mathias Agopian65ab4712010-07-14 17:59:35 -07001799 Mutex::Autolock _l(mLock);
1800
Eric Laurentcf2c0212014-07-25 16:20:43 -07001801 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001802 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001803 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001804
1805 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001806 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001807
1808 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001809 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001810 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001811 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001812
1813 AutoMutex lock(mHardwareLock);
1814 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001815 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001816 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten4ea00a22014-06-02 08:29:22 -07001817
Eric Laurentcf2c0212014-07-25 16:20:43 -07001818 mPrimaryOutputSampleRate = config->sample_rate;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001819 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001820 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001821 }
1822
Eric Laurentcf2c0212014-07-25 16:20:43 -07001823 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001824}
1825
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001826audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1827 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001828{
1829 Mutex::Autolock _l(mLock);
1830 MixerThread *thread1 = checkMixerThread_l(output1);
1831 MixerThread *thread2 = checkMixerThread_l(output2);
1832
1833 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001834 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1835 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001836 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001837 }
1838
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001839 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001840 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1841 thread->addOutputTrack(thread2);
1842 mPlaybackThreads.add(id, thread);
1843 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001844 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001845 return id;
1846}
1847
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001848status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001849{
Glenn Kastend96c5722012-04-25 13:44:49 -07001850 return closeOutput_nonvirtual(output);
1851}
1852
1853status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1854{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001855 // keep strong reference on the playback thread so that
1856 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001857 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001858 {
1859 Mutex::Autolock _l(mLock);
1860 thread = checkPlaybackThread_l(output);
1861 if (thread == NULL) {
1862 return BAD_VALUE;
1863 }
1864
Steve Block3856b092011-10-20 11:56:00 +01001865 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001866
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001867 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001868 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001869 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001870 DuplicatingThread *dupThread =
1871 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001872 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001873
1874 }
1875 }
1876 }
1877
1878
1879 mPlaybackThreads.removeItem(output);
1880 // save all effects to the default thread
1881 if (mPlaybackThreads.size()) {
1882 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1883 if (dstThread != NULL) {
1884 // audioflinger lock is held here so the acquisition order of thread locks does not
1885 // matter
1886 Mutex::Autolock _dl(dstThread->mLock);
1887 Mutex::Autolock _sl(thread->mLock);
1888 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1889 for (size_t i = 0; i < effectChains.size(); i ++) {
1890 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001891 }
1892 }
1893 }
Eric Laurent021cf962014-05-13 10:18:14 -07001894 audioConfigChanged(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001895 }
1896 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001897 // The thread entity (active unit of execution) is no longer running here,
1898 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001899
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001900 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurent83b88082014-06-20 18:31:16 -07001901 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001902 }
Eric Laurent83b88082014-06-20 18:31:16 -07001903
Mathias Agopian65ab4712010-07-14 17:59:35 -07001904 return NO_ERROR;
1905}
1906
Eric Laurent83b88082014-06-20 18:31:16 -07001907void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
1908{
1909 AudioStreamOut *out = thread->clearOutput();
1910 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1911 // from now on thread->mOutput is NULL
1912 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
1913 delete out;
1914}
1915
1916void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
1917{
1918 mPlaybackThreads.removeItem(thread->mId);
1919 thread->exit();
1920 closeOutputFinish(thread);
1921}
1922
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001923status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001924{
1925 Mutex::Autolock _l(mLock);
1926 PlaybackThread *thread = checkPlaybackThread_l(output);
1927
1928 if (thread == NULL) {
1929 return BAD_VALUE;
1930 }
1931
Steve Block3856b092011-10-20 11:56:00 +01001932 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001933 thread->suspend();
1934
1935 return NO_ERROR;
1936}
1937
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001938status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001939{
1940 Mutex::Autolock _l(mLock);
1941 PlaybackThread *thread = checkPlaybackThread_l(output);
1942
1943 if (thread == NULL) {
1944 return BAD_VALUE;
1945 }
1946
Steve Block3856b092011-10-20 11:56:00 +01001947 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001948
1949 thread->restore();
1950
1951 return NO_ERROR;
1952}
1953
Eric Laurentcf2c0212014-07-25 16:20:43 -07001954status_t AudioFlinger::openInput(audio_module_handle_t module,
1955 audio_io_handle_t *input,
1956 audio_config_t *config,
1957 audio_devices_t *device,
1958 const String8& address,
1959 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07001960 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001961{
Eric Laurent83b88082014-06-20 18:31:16 -07001962 Mutex::Autolock _l(mLock);
1963
Eric Laurentcf2c0212014-07-25 16:20:43 -07001964 if (*device == AUDIO_DEVICE_NONE) {
1965 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07001966 }
1967
Eric Laurentcf2c0212014-07-25 16:20:43 -07001968 sp<RecordThread> thread = openInput_l(module, input, config, *device, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001969
1970 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07001971 // notify client processes of the new input creation
1972 thread->audioConfigChanged(AudioSystem::INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001973 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001974 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001975 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07001976}
Dima Zavin799a70e2011-04-18 16:57:27 -07001977
Eric Laurent83b88082014-06-20 18:31:16 -07001978sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001979 audio_io_handle_t *input,
1980 audio_config_t *config,
Eric Laurent83b88082014-06-20 18:31:16 -07001981 audio_devices_t device,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001982 const String8& address,
1983 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07001984 audio_input_flags_t flags)
1985{
Eric Laurent83b88082014-06-20 18:31:16 -07001986 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, device);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001987 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001988 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07001989 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001990 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001991
Eric Laurentcf2c0212014-07-25 16:20:43 -07001992 if (*input == AUDIO_IO_HANDLE_NONE) {
1993 *input = nextUniqueId();
1994 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001995
Eric Laurentcf2c0212014-07-25 16:20:43 -07001996 audio_config_t halconfig = *config;
1997 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07001998 audio_stream_in_t *inStream = NULL;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001999 status_t status = inHwHal->open_input_stream(inHwHal, *input, device, &halconfig,
2000 &inStream, flags, address.string(), source);
2001 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002002 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Dima Zavin799a70e2011-04-18 16:57:27 -07002003 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002004 halconfig.sample_rate,
2005 halconfig.format,
2006 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002007 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002008 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002009
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002010 // If the input could not be opened with the requested parameters and we can handle the
2011 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
2012 // 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 -07002013 if (status == BAD_VALUE &&
Eric Laurentcf2c0212014-07-25 16:20:43 -07002014 config->format == halconfig.format && halconfig.format == AUDIO_FORMAT_PCM_16_BIT &&
2015 (halconfig.sample_rate <= 2 * config->sample_rate) &&
2016 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
2017 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002018 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002019 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002020 inStream = NULL;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002021 status = inHwHal->open_input_stream(inHwHal, *input, device, &halconfig,
2022 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07002023 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002024 }
2025
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002026 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002027
Glenn Kasten46909e72013-02-26 09:20:22 -08002028#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002029 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2030 // or (re-)create if current Pipe is idle and does not match the new format
2031 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002032 enum {
2033 TEE_SINK_NO, // don't copy input
2034 TEE_SINK_NEW, // copy input using a new pipe
2035 TEE_SINK_OLD, // copy input using an existing pipe
2036 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002037 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2038 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002039 if (!mTeeSinkInputEnabled) {
2040 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002041 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002042 kind = TEE_SINK_NO;
2043 } else if (mRecordTeeSink == 0) {
2044 kind = TEE_SINK_NEW;
2045 } else if (mRecordTeeSink->getStrongCount() != 1) {
2046 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002047 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002048 kind = TEE_SINK_OLD;
2049 } else {
2050 kind = TEE_SINK_NEW;
2051 }
2052 switch (kind) {
2053 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002054 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002055 size_t numCounterOffers = 0;
2056 const NBAIO_Format offers[1] = {format};
2057 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2058 ALOG_ASSERT(index == 0);
2059 PipeReader *pipeReader = new PipeReader(*pipe);
2060 numCounterOffers = 0;
2061 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2062 ALOG_ASSERT(index == 0);
2063 mRecordTeeSink = pipe;
2064 mRecordTeeSource = pipeReader;
2065 teeSink = pipe;
2066 }
2067 break;
2068 case TEE_SINK_OLD:
2069 teeSink = mRecordTeeSink;
2070 break;
2071 case TEE_SINK_NO:
2072 default:
2073 break;
2074 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002075#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002076
Eric Laurentcf2c0212014-07-25 16:20:43 -07002077 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002078
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002079 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002080 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002081 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002082 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002083 inputStream,
2084 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002085 primaryOutputDevice_l(),
Eric Laurent83b88082014-06-20 18:31:16 -07002086 device
Glenn Kasten46909e72013-02-26 09:20:22 -08002087#ifdef TEE_SINK
2088 , teeSink
2089#endif
2090 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002091 mRecordThreads.add(*input, thread);
2092 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002093 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002094 }
2095
Eric Laurentcf2c0212014-07-25 16:20:43 -07002096 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002097 return 0;
2098}
2099
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002100status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002101{
Glenn Kastend96c5722012-04-25 13:44:49 -07002102 return closeInput_nonvirtual(input);
2103}
2104
2105status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2106{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002107 // keep strong reference on the record thread so that
2108 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002109 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002110 {
2111 Mutex::Autolock _l(mLock);
2112 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002113 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002114 return BAD_VALUE;
2115 }
2116
Steve Block3856b092011-10-20 11:56:00 +01002117 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002118
2119 // If we still have effect chains, it means that a client still holds a handle
2120 // on at least one effect. We must either move the chain to an existing thread with the
2121 // same session ID or put it aside in case a new record thread is opened for a
2122 // new capture on the same session
2123 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002124 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002125 Mutex::Autolock _sl(thread->mLock);
2126 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002127 // Note: maximum one chain per record thread
2128 if (effectChains.size() != 0) {
2129 chain = effectChains[0];
2130 }
2131 }
2132 if (chain != 0) {
2133 // first check if a record thread is already opened with a client on the same session.
2134 // This should only happen in case of overlap between one thread tear down and the
2135 // creation of its replacement
2136 size_t i;
2137 for (i = 0; i < mRecordThreads.size(); i++) {
2138 sp<RecordThread> t = mRecordThreads.valueAt(i);
2139 if (t == thread) {
2140 continue;
2141 }
2142 if (t->hasAudioSession(chain->sessionId()) != 0) {
2143 Mutex::Autolock _l(t->mLock);
2144 ALOGV("closeInput() found thread %d for effect session %d",
2145 t->id(), chain->sessionId());
2146 t->addEffectChain_l(chain);
2147 break;
2148 }
2149 }
2150 // put the chain aside if we could not find a record thread with the same session id.
2151 if (i == mRecordThreads.size()) {
2152 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002153 }
2154 }
Eric Laurent021cf962014-05-13 10:18:14 -07002155 audioConfigChanged(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002156 mRecordThreads.removeItem(input);
2157 }
Eric Laurent83b88082014-06-20 18:31:16 -07002158 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2159 // we have a different lock for notification client
2160 closeInputFinish(thread);
2161 return NO_ERROR;
2162}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002163
Eric Laurent83b88082014-06-20 18:31:16 -07002164void AudioFlinger::closeInputFinish(sp<RecordThread> thread)
2165{
2166 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002167 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002168 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002169 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002170 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002171 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002172}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002173
Eric Laurent83b88082014-06-20 18:31:16 -07002174void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
2175{
2176 mRecordThreads.removeItem(thread->mId);
2177 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002178}
2179
Glenn Kastend2304db2014-02-03 07:40:31 -08002180status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002181{
2182 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002183 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002184
2185 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2186 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002187 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002188 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002189
2190 return NO_ERROR;
2191}
2192
2193
Eric Laurentde3f8392014-07-27 18:38:22 -07002194audio_unique_id_t AudioFlinger::newAudioUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002195{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002196 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002197}
2198
Marco Nelissend457c972014-02-11 08:47:07 -08002199void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002200{
2201 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002202 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002203 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2204 if (pid != -1 && (caller == getpid_cached)) {
2205 caller = pid;
2206 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002207
Eric Laurent021cf962014-05-13 10:18:14 -07002208 {
2209 Mutex::Autolock _cl(mClientLock);
2210 // Ignore requests received from processes not known as notification client. The request
2211 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2212 // called from a different pid leaving a stale session reference. Also we don't know how
2213 // to clear this reference if the client process dies.
2214 if (mNotificationClients.indexOfKey(caller) < 0) {
2215 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2216 return;
2217 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002218 }
2219
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002220 size_t num = mAudioSessionRefs.size();
2221 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002222 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002223 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2224 ref->mCnt++;
2225 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002226 return;
2227 }
2228 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002229 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2230 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002231}
2232
Marco Nelissend457c972014-02-11 08:47:07 -08002233void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002234{
2235 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002236 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002237 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2238 if (pid != -1 && (caller == getpid_cached)) {
2239 caller = pid;
2240 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002241 size_t num = mAudioSessionRefs.size();
2242 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002243 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002244 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2245 ref->mCnt--;
2246 ALOGV(" decremented refcount to %d", ref->mCnt);
2247 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002248 mAudioSessionRefs.removeAt(i);
2249 delete ref;
2250 purgeStaleEffects_l();
2251 }
2252 return;
2253 }
2254 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002255 // If the caller is mediaserver it is likely that the session being released was acquired
2256 // on behalf of a process not in notification clients and we ignore the warning.
2257 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002258}
2259
2260void AudioFlinger::purgeStaleEffects_l() {
2261
Steve Block3856b092011-10-20 11:56:00 +01002262 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002263
2264 Vector< sp<EffectChain> > chains;
2265
2266 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2267 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2268 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2269 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002270 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2271 chains.push(ec);
2272 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002273 }
2274 }
2275 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2276 sp<RecordThread> t = mRecordThreads.valueAt(i);
2277 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2278 sp<EffectChain> ec = t->mEffectChains[j];
2279 chains.push(ec);
2280 }
2281 }
2282
2283 for (size_t i = 0; i < chains.size(); i++) {
2284 sp<EffectChain> ec = chains[i];
2285 int sessionid = ec->sessionId();
2286 sp<ThreadBase> t = ec->mThread.promote();
2287 if (t == 0) {
2288 continue;
2289 }
2290 size_t numsessionrefs = mAudioSessionRefs.size();
2291 bool found = false;
2292 for (size_t k = 0; k < numsessionrefs; k++) {
2293 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002294 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002295 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002296 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002297 found = true;
2298 break;
2299 }
2300 }
2301 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002302 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002303 // remove all effects from the chain
2304 while (ec->mEffects.size()) {
2305 sp<EffectModule> effect = ec->mEffects[0];
2306 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002307 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002308 if (effect->purgeHandles()) {
2309 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002310 }
2311 AudioSystem::unregisterEffect(effect->id());
2312 }
2313 }
2314 }
2315 return;
2316}
2317
Mathias Agopian65ab4712010-07-14 17:59:35 -07002318// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002319AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002320{
Glenn Kastena1117922012-01-26 10:53:32 -08002321 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002322}
2323
2324// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002325AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002326{
2327 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002328 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002329}
2330
2331// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002332AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002333{
Glenn Kastena1117922012-01-26 10:53:32 -08002334 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002335}
2336
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002337uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002338{
Glenn Kastenbcefec32014-01-17 12:09:05 -08002339 return (uint32_t) android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002340}
2341
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002342AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002343{
2344 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2345 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002346 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002347 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002348 return thread;
2349 }
2350 }
2351 return NULL;
2352}
2353
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002354audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002355{
2356 PlaybackThread *thread = primaryPlaybackThread_l();
2357
2358 if (thread == NULL) {
2359 return 0;
2360 }
2361
Eric Laurentf1c04f92012-08-28 14:26:53 -07002362 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002363}
2364
Eric Laurenta011e352012-03-29 15:51:43 -07002365sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2366 int triggerSession,
2367 int listenerSession,
2368 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002369 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002370{
2371 Mutex::Autolock _l(mLock);
2372
2373 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2374 status_t playStatus = NAME_NOT_FOUND;
2375 status_t recStatus = NAME_NOT_FOUND;
2376 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2377 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2378 if (playStatus == NO_ERROR) {
2379 return event;
2380 }
2381 }
2382 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2383 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2384 if (recStatus == NO_ERROR) {
2385 return event;
2386 }
2387 }
2388 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2389 mPendingSyncEvents.add(event);
2390 } else {
2391 ALOGV("createSyncEvent() invalid event %d", event->type());
2392 event.clear();
2393 }
2394 return event;
2395}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002396
Mathias Agopian65ab4712010-07-14 17:59:35 -07002397// ----------------------------------------------------------------------------
2398// Effect management
2399// ----------------------------------------------------------------------------
2400
2401
Glenn Kastenf587ba52012-01-26 16:25:10 -08002402status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002403{
2404 Mutex::Autolock _l(mLock);
2405 return EffectQueryNumberEffects(numEffects);
2406}
2407
Glenn Kastenf587ba52012-01-26 16:25:10 -08002408status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002409{
2410 Mutex::Autolock _l(mLock);
2411 return EffectQueryEffect(index, descriptor);
2412}
2413
Glenn Kasten5e92a782012-01-30 07:40:52 -08002414status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002415 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002416{
2417 Mutex::Autolock _l(mLock);
2418 return EffectGetDescriptor(pUuid, descriptor);
2419}
2420
2421
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002422sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002423 effect_descriptor_t *pDesc,
2424 const sp<IEffectClient>& effectClient,
2425 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002426 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002427 int sessionId,
2428 status_t *status,
2429 int *id,
2430 int *enabled)
2431{
2432 status_t lStatus = NO_ERROR;
2433 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002434 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002435
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002436 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002437 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002438 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002439
2440 if (pDesc == NULL) {
2441 lStatus = BAD_VALUE;
2442 goto Exit;
2443 }
2444
Eric Laurent84e9a102010-09-23 16:10:16 -07002445 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002446 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002447 lStatus = PERMISSION_DENIED;
2448 goto Exit;
2449 }
2450
Dima Zavinfce7a472011-04-19 22:30:36 -07002451 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002452 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002453 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002454 lStatus = PERMISSION_DENIED;
2455 goto Exit;
2456 }
2457
Mathias Agopian65ab4712010-07-14 17:59:35 -07002458 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002459 if (!EffectIsNullUuid(&pDesc->uuid)) {
2460 // if uuid is specified, request effect descriptor
2461 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2462 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002463 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002464 goto Exit;
2465 }
2466 } else {
2467 // if uuid is not specified, look for an available implementation
2468 // of the required type in effect factory
2469 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002470 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002471 lStatus = BAD_VALUE;
2472 goto Exit;
2473 }
2474 uint32_t numEffects = 0;
2475 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002476 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002477 bool found = false;
2478
2479 lStatus = EffectQueryNumberEffects(&numEffects);
2480 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002481 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002482 goto Exit;
2483 }
2484 for (uint32_t i = 0; i < numEffects; i++) {
2485 lStatus = EffectQueryEffect(i, &desc);
2486 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002487 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002488 continue;
2489 }
2490 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2491 // If matching type found save effect descriptor. If the session is
2492 // 0 and the effect is not auxiliary, continue enumeration in case
2493 // an auxiliary version of this effect type is available
2494 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002495 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002496 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002497 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2498 break;
2499 }
2500 }
2501 }
2502 if (!found) {
2503 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002504 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002505 goto Exit;
2506 }
2507 // For same effect type, chose auxiliary version over insert version if
2508 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002509 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002510 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002511 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002512 }
2513 }
2514
2515 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002516 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002517 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2518 lStatus = INVALID_OPERATION;
2519 goto Exit;
2520 }
2521
Eric Laurent59255e42011-07-27 19:49:51 -07002522 // check recording permission for visualizer
2523 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2524 !recordingAllowed()) {
2525 lStatus = PERMISSION_DENIED;
2526 goto Exit;
2527 }
2528
Mathias Agopian65ab4712010-07-14 17:59:35 -07002529 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002530 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002531 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002532 // if the output returned by getOutputForEffect() is removed before we lock the
2533 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2534 // and we will exit safely
2535 io = AudioSystem::getOutputForEffect(&desc);
2536 ALOGV("createEffect got output %d", io);
2537 }
2538
2539 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002540
2541 // If output is not specified try to find a matching audio session ID in one of the
2542 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002543 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2544 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002545 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002546 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002547 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2548 // output must be specified by AudioPolicyManager when using session
2549 // AUDIO_SESSION_OUTPUT_STAGE
2550 lStatus = BAD_VALUE;
2551 goto Exit;
2552 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002553 // look for the thread where the specified audio session is present
2554 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2555 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2556 io = mPlaybackThreads.keyAt(i);
2557 break;
2558 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002559 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002560 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002561 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2562 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2563 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002564 break;
2565 }
2566 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002567 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002568 // If no output thread contains the requested session ID, default to
2569 // first output. The effect chain will be moved to the correct output
2570 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002571 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002572 io = mPlaybackThreads.keyAt(0);
2573 }
Steve Block3856b092011-10-20 11:56:00 +01002574 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002575 }
2576 ThreadBase *thread = checkRecordThread_l(io);
2577 if (thread == NULL) {
2578 thread = checkPlaybackThread_l(io);
2579 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002580 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002581 lStatus = BAD_VALUE;
2582 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002583 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002584 } else {
2585 // Check if one effect chain was awaiting for an effect to be created on this
2586 // session and used it instead of creating a new one.
2587 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)sessionId);
2588 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002589 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002590 thread->addEffectChain_l(chain);
2591 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002592 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002593
Eric Laurent021cf962014-05-13 10:18:14 -07002594 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002595
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002596 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002597 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2598 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002599 if (handle != 0 && id != NULL) {
2600 *id = handle->id();
2601 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002602 if (handle == 0) {
2603 // remove local strong reference to Client with mClientLock held
2604 Mutex::Autolock _cl(mClientLock);
2605 client.clear();
2606 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002607 }
2608
2609Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002610 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002611 return handle;
2612}
2613
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002614status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2615 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002616{
Steve Block3856b092011-10-20 11:56:00 +01002617 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002618 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002619 Mutex::Autolock _l(mLock);
2620 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002621 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002622 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002623 }
Eric Laurentde070132010-07-13 04:45:46 -07002624 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2625 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002626 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002627 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002628 }
Eric Laurentde070132010-07-13 04:45:46 -07002629 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2630 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002631 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002632 return BAD_VALUE;
2633 }
2634
2635 Mutex::Autolock _dl(dstThread->mLock);
2636 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002637 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002638}
2639
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002640// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002641status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002642 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002643 AudioFlinger::PlaybackThread *dstThread,
2644 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002645{
Steve Block3856b092011-10-20 11:56:00 +01002646 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002647 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002648
Eric Laurent59255e42011-07-27 19:49:51 -07002649 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002650 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002651 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002652 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002653 return INVALID_OPERATION;
2654 }
2655
Andy Hung9a592762014-07-21 21:56:01 -07002656 // Check whether the destination thread has a channel count of FCC_2, which is
2657 // currently required for (most) effects. Prevent moving the effect chain here rather
2658 // than disabling the addEffect_l() call in dstThread below.
Eric Laurentb10352f2014-11-03 16:25:39 -08002659 if ((dstThread->type() == ThreadBase::MIXER || dstThread->type() == ThreadBase::DUPLICATING) &&
2660 dstThread->mChannelCount != FCC_2) {
Andy Hung9a592762014-07-21 21:56:01 -07002661 ALOGW("moveEffectChain_l() effect chain failed because"
2662 " destination thread %p channel count(%u) != %u",
2663 dstThread, dstThread->mChannelCount, FCC_2);
2664 return INVALID_OPERATION;
2665 }
2666
Eric Laurent39e94f82010-07-28 01:32:47 -07002667 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002668 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002669 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002670 // removed.
2671 srcThread->removeEffectChain_l(chain);
2672
2673 // transfer all effects one by one so that new effect chain is created on new thread with
2674 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002675 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002676 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002677 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002678 Vector< sp<EffectModule> > removed;
2679 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002680 while (effect != 0) {
2681 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002682 removed.add(effect);
2683 status = dstThread->addEffect_l(effect);
2684 if (status != NO_ERROR) {
2685 break;
2686 }
Eric Laurentec35a142011-10-05 17:42:25 -07002687 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2688 if (effect->state() == EffectModule::ACTIVE ||
2689 effect->state() == EffectModule::STOPPING) {
2690 effect->start();
2691 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002692 // if the move request is not received from audio policy manager, the effect must be
2693 // re-registered with the new strategy and output
2694 if (dstChain == 0) {
2695 dstChain = effect->chain().promote();
2696 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002697 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002698 status = NO_INIT;
2699 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002700 }
2701 strategy = dstChain->strategy();
2702 }
2703 if (reRegister) {
2704 AudioSystem::unregisterEffect(effect->id());
2705 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002706 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002707 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002708 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002709 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002710 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002711 }
Eric Laurentde070132010-07-13 04:45:46 -07002712 effect = chain->getEffectFromId_l(0);
2713 }
2714
Eric Laurent5baf2af2013-09-12 17:37:00 -07002715 if (status != NO_ERROR) {
2716 for (size_t i = 0; i < removed.size(); i++) {
2717 srcThread->addEffect_l(removed[i]);
2718 if (dstChain != 0 && reRegister) {
2719 AudioSystem::unregisterEffect(removed[i]->id());
2720 AudioSystem::registerEffect(&removed[i]->desc(),
2721 srcThread->id(),
2722 strategy,
2723 sessionId,
2724 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002725 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002726 }
2727 }
2728 }
2729
2730 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002731}
2732
Eric Laurent5baf2af2013-09-12 17:37:00 -07002733bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002734{
2735 if (mGlobalEffectEnableTime != 0 &&
2736 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2737 return true;
2738 }
2739
2740 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2741 sp<EffectChain> ec =
2742 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002743 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002744 return true;
2745 }
2746 }
2747 return false;
2748}
2749
Eric Laurent5baf2af2013-09-12 17:37:00 -07002750void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002751{
2752 Mutex::Autolock _l(mLock);
2753
2754 mGlobalEffectEnableTime = systemTime();
2755
2756 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2757 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2758 if (t->mType == ThreadBase::OFFLOAD) {
2759 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2760 }
2761 }
2762
2763}
2764
Eric Laurentaaa44472014-09-12 17:41:50 -07002765status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2766{
2767 audio_session_t session = (audio_session_t)chain->sessionId();
2768 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2769 ALOGV("putOrphanEffectChain_l session %d index %d", session, index);
2770 if (index >= 0) {
2771 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2772 return ALREADY_EXISTS;
2773 }
2774 mOrphanEffectChains.add(session, chain);
2775 return NO_ERROR;
2776}
2777
2778sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2779{
2780 sp<EffectChain> chain;
2781 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2782 ALOGV("getOrphanEffectChain_l session %d index %d", session, index);
2783 if (index >= 0) {
2784 chain = mOrphanEffectChains.valueAt(index);
2785 mOrphanEffectChains.removeItemsAt(index);
2786 }
2787 return chain;
2788}
2789
2790bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2791{
2792 Mutex::Autolock _l(mLock);
2793 audio_session_t session = (audio_session_t)effect->sessionId();
2794 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2795 ALOGV("updateOrphanEffectChains session %d index %d", session, index);
2796 if (index >= 0) {
2797 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2798 if (chain->removeEffect_l(effect) == 0) {
2799 ALOGV("updateOrphanEffectChains removing effect chain at index %d", index);
2800 mOrphanEffectChains.removeItemsAt(index);
2801 }
2802 return true;
2803 }
2804 return false;
2805}
2806
2807
Glenn Kastenda6ef132013-01-10 12:31:01 -08002808struct Entry {
2809#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2810 char mName[MAX_NAME];
2811};
2812
2813int comparEntry(const void *p1, const void *p2)
2814{
2815 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2816}
2817
Glenn Kasten46909e72013-02-26 09:20:22 -08002818#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002819void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002820{
Eric Laurent81784c32012-11-19 14:55:58 -08002821 NBAIO_Source *teeSource = source.get();
2822 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002823 // .wav rotation
2824 // There is a benign race condition if 2 threads call this simultaneously.
2825 // They would both traverse the directory, but the result would simply be
2826 // failures at unlink() which are ignored. It's also unlikely since
2827 // normally dumpsys is only done by bugreport or from the command line.
2828 char teePath[32+256];
2829 strcpy(teePath, "/data/misc/media");
2830 size_t teePathLen = strlen(teePath);
2831 DIR *dir = opendir(teePath);
2832 teePath[teePathLen++] = '/';
2833 if (dir != NULL) {
2834#define MAX_SORT 20 // number of entries to sort
2835#define MAX_KEEP 10 // number of entries to keep
2836 struct Entry entries[MAX_SORT];
2837 size_t entryCount = 0;
2838 while (entryCount < MAX_SORT) {
2839 struct dirent de;
2840 struct dirent *result = NULL;
2841 int rc = readdir_r(dir, &de, &result);
2842 if (rc != 0) {
2843 ALOGW("readdir_r failed %d", rc);
2844 break;
2845 }
2846 if (result == NULL) {
2847 break;
2848 }
2849 if (result != &de) {
2850 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2851 break;
2852 }
2853 // ignore non .wav file entries
2854 size_t nameLen = strlen(de.d_name);
2855 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2856 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2857 continue;
2858 }
2859 strcpy(entries[entryCount++].mName, de.d_name);
2860 }
2861 (void) closedir(dir);
2862 if (entryCount > MAX_KEEP) {
2863 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2864 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2865 strcpy(&teePath[teePathLen], entries[i].mName);
2866 (void) unlink(teePath);
2867 }
2868 }
2869 } else {
2870 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002871 dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002872 }
2873 }
Eric Laurent81784c32012-11-19 14:55:58 -08002874 char teeTime[16];
2875 struct timeval tv;
2876 gettimeofday(&tv, NULL);
2877 struct tm tm;
2878 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002879 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2880 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2881 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2882 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002883 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07002884 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08002885 char wavHeader[44];
2886 memcpy(wavHeader,
2887 "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",
2888 sizeof(wavHeader));
2889 NBAIO_Format format = teeSource->format();
2890 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002891 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07002892 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002893 wavHeader[22] = channelCount; // number of channels
2894 wavHeader[24] = sampleRate; // sample rate
2895 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07002896 wavHeader[32] = frameSize; // block alignment
2897 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08002898 write(teeFd, wavHeader, sizeof(wavHeader));
2899 size_t total = 0;
2900 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07002901#define TEE_SINK_READ 1024 // frames per I/O operation
2902 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002903 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08002904 size_t count = TEE_SINK_READ;
2905 ssize_t actual = teeSource->read(buffer, count,
2906 AudioBufferProvider::kInvalidPTS);
2907 bool wasFirstRead = firstRead;
2908 firstRead = false;
2909 if (actual <= 0) {
2910 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2911 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002912 }
Eric Laurent81784c32012-11-19 14:55:58 -08002913 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002914 }
Eric Laurent81784c32012-11-19 14:55:58 -08002915 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07002916 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002917 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002918 }
Glenn Kasten329f6512014-08-28 16:23:16 -07002919 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08002920 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002921 uint32_t temp = 44 + total * frameSize - 8;
2922 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002923 write(teeFd, &temp, sizeof(temp));
2924 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002925 temp = total * frameSize;
2926 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002927 write(teeFd, &temp, sizeof(temp));
2928 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002929 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002930 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002931 }
Eric Laurent59255e42011-07-27 19:49:51 -07002932 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002933 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002934 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002935 }
Eric Laurent59255e42011-07-27 19:49:51 -07002936 }
2937 }
2938}
Glenn Kasten46909e72013-02-26 09:20:22 -08002939#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002940
Mathias Agopian65ab4712010-07-14 17:59:35 -07002941// ----------------------------------------------------------------------------
2942
2943status_t AudioFlinger::onTransact(
2944 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2945{
2946 return BnAudioFlinger::onTransact(code, data, reply, flags);
2947}
2948
Mathias Agopian65ab4712010-07-14 17:59:35 -07002949}; // namespace android