blob: 115b60c74f19d1be5fc563d560d6b2f86d6a1c4b [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
Mathias Agopian65ab4712010-07-14 17:59:35 -07002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
Glenn Kasten153b9fe2013-07-15 11:23:36 -070022#include "Configuration.h"
Glenn Kastenda6ef132013-01-10 12:31:01 -080023#include <dirent.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070024#include <math.h>
25#include <signal.h>
26#include <sys/time.h>
27#include <sys/resource.h>
28
Gloria Wang9ee159b2011-02-24 14:51:45 -080029#include <binder/IPCThreadState.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070030#include <binder/IServiceManager.h>
31#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070032#include <utils/Trace.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070033#include <binder/Parcel.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/String16.h>
35#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070036#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070037
Dima Zavinfce7a472011-04-19 22:30:36 -070038#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070039#include <cutils/properties.h>
40
Dima Zavin64760242011-05-11 14:15:23 -070041#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070042#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070043
44#include "AudioMixer.h"
45#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080046#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070047
Mathias Agopian65ab4712010-07-14 17:59:35 -070048#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070049#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070050#include <audio_effects/effect_ns.h>
51#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Glenn Kasten3b21c502011-12-15 09:52:39 -080053#include <audio_utils/primitives.h>
54
Eric Laurentfeb0db62011-07-22 09:04:31 -070055#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080056
John Grossman4ff14ba2012-02-08 16:37:41 -080057#include <common_time/cc_helper.h>
Glenn Kasten58912562012-04-03 10:45:00 -070058
Glenn Kasten9e58b552013-01-18 15:09:48 -080059#include <media/IMediaLogService.h>
60
Glenn Kastenda6ef132013-01-10 12:31:01 -080061#include <media/nbaio/Pipe.h>
62#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070063#include <media/AudioParameter.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070064#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080065
Mathias Agopian65ab4712010-07-14 17:59:35 -070066// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070067
John Grossman1c345192012-03-27 14:00:17 -070068// Note: the following macro is used for extremely verbose logging message. In
69// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
70// 0; but one side effect of this is to turn all LOGV's as well. Some messages
71// are so verbose that we want to suppress them even when we have ALOG_ASSERT
72// turned on. Do not uncomment the #def below unless you really know what you
73// are doing and want to see all of the extremely verbose messages.
74//#define VERY_VERY_VERBOSE_LOGGING
75#ifdef VERY_VERY_VERBOSE_LOGGING
76#define ALOGVV ALOGV
77#else
78#define ALOGVV(a...) do { } while(0)
79#endif
Eric Laurentde070132010-07-13 04:45:46 -070080
Mathias Agopian65ab4712010-07-14 17:59:35 -070081namespace android {
82
Glenn Kastenec1d6b52011-12-12 09:04:45 -080083static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
84static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Eric Laurent021cf962014-05-13 10:18:14 -070085static const char kClientLockedString[] = "Client lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070086
Glenn Kasten58912562012-04-03 10:45:00 -070087
John Grossman4ff14ba2012-02-08 16:37:41 -080088nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080089
Eric Laurent81784c32012-11-19 14:55:58 -080090uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070091
Glenn Kasten46909e72013-02-26 09:20:22 -080092#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080093bool AudioFlinger::mTeeSinkInputEnabled = false;
94bool AudioFlinger::mTeeSinkOutputEnabled = false;
95bool AudioFlinger::mTeeSinkTrackEnabled = false;
96
97size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
98size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
99size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800100#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800101
Eric Laurent813e2a72013-08-31 12:59:48 -0700102// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
103// we define a minimum time during which a global effect is considered enabled.
104static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
105
Mathias Agopian65ab4712010-07-14 17:59:35 -0700106// ----------------------------------------------------------------------------
107
Marco Nelissenb2208842014-02-07 14:00:50 -0800108const char *formatToString(audio_format_t format) {
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530109 switch (format & AUDIO_FORMAT_MAIN_MASK) {
110 case AUDIO_FORMAT_PCM:
111 switch (format) {
112 case AUDIO_FORMAT_PCM_16_BIT: return "pcm16";
113 case AUDIO_FORMAT_PCM_8_BIT: return "pcm8";
114 case AUDIO_FORMAT_PCM_32_BIT: return "pcm32";
115 case AUDIO_FORMAT_PCM_8_24_BIT: return "pcm8.24";
116 case AUDIO_FORMAT_PCM_FLOAT: return "pcmfloat";
117 case AUDIO_FORMAT_PCM_24_BIT_PACKED: return "pcm24";
118 default:
119 break;
120 }
121 break;
Marco Nelissenb2208842014-02-07 14:00:50 -0800122 case AUDIO_FORMAT_MP3: return "mp3";
123 case AUDIO_FORMAT_AMR_NB: return "amr-nb";
124 case AUDIO_FORMAT_AMR_WB: return "amr-wb";
125 case AUDIO_FORMAT_AAC: return "aac";
126 case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
127 case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
128 case AUDIO_FORMAT_VORBIS: return "vorbis";
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530129 case AUDIO_FORMAT_OPUS: return "opus";
130 case AUDIO_FORMAT_AC3: return "ac-3";
131 case AUDIO_FORMAT_E_AC3: return "e-ac-3";
Marco Nelissenb2208842014-02-07 14:00:50 -0800132 default:
133 break;
134 }
135 return "unknown";
136}
137
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700138static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700139{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700140 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700141 int rc;
142
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700143 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
144 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
145 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
146 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700147 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700148 }
149 rc = audio_hw_device_open(mod, dev);
150 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
151 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
152 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700153 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700154 }
Eric Laurent5806b352014-05-22 12:23:26 -0700155 if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700156 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
157 rc = BAD_VALUE;
158 goto out;
159 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700160 return 0;
161
162out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700163 *dev = NULL;
164 return rc;
165}
166
Mathias Agopian65ab4712010-07-14 17:59:35 -0700167// ----------------------------------------------------------------------------
168
169AudioFlinger::AudioFlinger()
170 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800171 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800172 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700173 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800174 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800175 mMasterMute(false),
176 mNextUniqueId(1),
177 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700178 mBtNrecIsOff(false),
179 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700180 mIsDeviceTypeKnown(false),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700181 mGlobalEffectEnableTime(0),
182 mPrimaryOutputSampleRate(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700183{
Glenn Kasten949a9262013-04-16 12:35:20 -0700184 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800185 char value[PROPERTY_VALUE_MAX];
186 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
187 if (doLog) {
Glenn Kastencf515ab2014-03-14 16:01:58 -0700188 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters", MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800189 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700190
Glenn Kasten46909e72013-02-26 09:20:22 -0800191#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800192 (void) property_get("ro.debuggable", value, "0");
193 int debuggable = atoi(value);
194 int teeEnabled = 0;
195 if (debuggable) {
196 (void) property_get("af.tee", value, "0");
197 teeEnabled = atoi(value);
198 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800199 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700200 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800201 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700202 }
203 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800204 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700205 }
206 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800207 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700208 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800209#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700210}
211
212void AudioFlinger::onFirstRef()
213{
Dima Zavin799a70e2011-04-18 16:57:27 -0700214 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700215
Eric Laurent93575202011-01-18 18:39:02 -0800216 Mutex::Autolock _l(mLock);
217
Dima Zavin799a70e2011-04-18 16:57:27 -0700218 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800219 char val_str[PROPERTY_VALUE_MAX] = { 0 };
220 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
221 uint32_t int_val;
222 if (1 == sscanf(val_str, "%u", &int_val)) {
223 mStandbyTimeInNsecs = milliseconds(int_val);
224 ALOGI("Using %u mSec as standby time.", int_val);
225 } else {
226 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
227 ALOGI("Using default %u mSec as standby time.",
228 (uint32_t)(mStandbyTimeInNsecs / 1000000));
229 }
230 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700231
Eric Laurent1c333e22014-05-20 10:48:17 -0700232 mPatchPanel = new PatchPanel(this);
233
Eric Laurenta4c5a552012-03-29 10:12:40 -0700234 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700235}
236
237AudioFlinger::~AudioFlinger()
238{
239 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700240 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700241 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242 }
243 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700244 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700245 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700246 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700247
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800248 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
249 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700250 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
251 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700252 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700253
254 // Tell media.log service about any old writers that still need to be unregistered
255 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
256 if (binder != 0) {
257 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
258 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
259 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
260 mUnregisteredWriters.pop();
261 mediaLogService->unregisterWriter(iMemory);
262 }
263 }
264
Mathias Agopian65ab4712010-07-14 17:59:35 -0700265}
266
Eric Laurenta4c5a552012-03-29 10:12:40 -0700267static const char * const audio_interfaces[] = {
268 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
269 AUDIO_HARDWARE_MODULE_ID_A2DP,
270 AUDIO_HARDWARE_MODULE_ID_USB,
271};
272#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
273
John Grossmanee578c02012-07-23 17:05:46 -0700274AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
275 audio_module_handle_t module,
276 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700277{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700278 // if module is 0, the request comes from an old policy manager and we should load
279 // well known modules
280 if (module == 0) {
281 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
282 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
283 loadHwModule_l(audio_interfaces[i]);
284 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700285 // then try to find a module supporting the requested device.
286 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
287 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
288 audio_hw_device_t *dev = audioHwDevice->hwDevice();
289 if ((dev->get_supported_devices != NULL) &&
290 (dev->get_supported_devices(dev) & devices) == devices)
291 return audioHwDevice;
292 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700293 } else {
294 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700295 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
296 if (audioHwDevice != NULL) {
297 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700298 }
299 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700300
Dima Zavin799a70e2011-04-18 16:57:27 -0700301 return NULL;
302}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700303
Glenn Kasten0f11b512014-01-31 16:18:54 -0800304void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305{
306 const size_t SIZE = 256;
307 char buffer[SIZE];
308 String8 result;
309
310 result.append("Clients:\n");
311 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800312 sp<Client> client = mClients.valueAt(i).promote();
313 if (client != 0) {
314 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
315 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316 }
317 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700318
Eric Laurentd1b28d42013-09-18 18:47:13 -0700319 result.append("Notification Clients:\n");
320 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
321 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
322 result.append(buffer);
323 }
324
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700325 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800326 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700327 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
328 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800329 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700330 result.append(buffer);
331 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700332 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700333}
334
335
Glenn Kasten0f11b512014-01-31 16:18:54 -0800336void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700337{
338 const size_t SIZE = 256;
339 char buffer[SIZE];
340 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800341 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700342
John Grossman4ff14ba2012-02-08 16:37:41 -0800343 snprintf(buffer, SIZE, "Hardware status: %d\n"
344 "Standby Time mSec: %u\n",
345 hardwareStatus,
346 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700347 result.append(buffer);
348 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349}
350
Glenn Kasten0f11b512014-01-31 16:18:54 -0800351void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700352{
353 const size_t SIZE = 256;
354 char buffer[SIZE];
355 String8 result;
356 snprintf(buffer, SIZE, "Permission Denial: "
357 "can't dump AudioFlinger from pid=%d, uid=%d\n",
358 IPCThreadState::self()->getCallingPid(),
359 IPCThreadState::self()->getCallingUid());
360 result.append(buffer);
361 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362}
363
Eric Laurent81784c32012-11-19 14:55:58 -0800364bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365{
366 bool locked = false;
367 for (int i = 0; i < kDumpLockRetries; ++i) {
368 if (mutex.tryLock() == NO_ERROR) {
369 locked = true;
370 break;
371 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800372 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373 }
374 return locked;
375}
376
377status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
378{
Glenn Kasten44deb052012-02-05 18:09:08 -0800379 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700380 dumpPermissionDenial(fd, args);
381 } else {
382 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800383 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384 if (!hardwareLocked) {
385 String8 result(kHardwareLockedString);
386 write(fd, result.string(), result.size());
387 } else {
388 mHardwareLock.unlock();
389 }
390
Eric Laurent81784c32012-11-19 14:55:58 -0800391 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392
393 // failed to lock - AudioFlinger is probably deadlocked
394 if (!locked) {
395 String8 result(kDeadlockedString);
396 write(fd, result.string(), result.size());
397 }
398
Eric Laurent021cf962014-05-13 10:18:14 -0700399 bool clientLocked = dumpTryLock(mClientLock);
400 if (!clientLocked) {
401 String8 result(kClientLockedString);
402 write(fd, result.string(), result.size());
403 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700404
405 EffectDumpEffects(fd);
406
Mathias Agopian65ab4712010-07-14 17:59:35 -0700407 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700408 if (clientLocked) {
409 mClientLock.unlock();
410 }
411
Mathias Agopian65ab4712010-07-14 17:59:35 -0700412 dumpInternals(fd, args);
413
414 // dump playback threads
415 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
416 mPlaybackThreads.valueAt(i)->dump(fd, args);
417 }
418
419 // dump record threads
420 for (size_t i = 0; i < mRecordThreads.size(); i++) {
421 mRecordThreads.valueAt(i)->dump(fd, args);
422 }
423
Eric Laurentaaa44472014-09-12 17:41:50 -0700424 // dump orphan effect chains
425 if (mOrphanEffectChains.size() != 0) {
426 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
427 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
428 mOrphanEffectChains.valueAt(i)->dump(fd, args);
429 }
430 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700431 // dump all hardware devs
432 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700433 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700434 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700436
Glenn Kasten46909e72013-02-26 09:20:22 -0800437#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700438 // dump the serially shared record tee sink
439 if (mRecordTeeSource != 0) {
440 dumpTee(fd, mRecordTeeSource);
441 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800442#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700443
Glenn Kastend65d73c2012-06-22 17:21:07 -0700444 if (locked) {
445 mLock.unlock();
446 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800447
448 // append a copy of media.log here by forwarding fd to it, but don't attempt
449 // to lookup the service if it's not running, as it will block for a second
450 if (mLogMemoryDealer != 0) {
451 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
452 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700453 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800454 Vector<String16> args;
455 binder->dump(fd, args);
456 }
457 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700458 }
459 return NO_ERROR;
460}
461
Eric Laurent021cf962014-05-13 10:18:14 -0700462sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800463{
Eric Laurent021cf962014-05-13 10:18:14 -0700464 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800465 // If pid is already in the mClients wp<> map, then use that entry
466 // (for which promote() is always != 0), otherwise create a new entry and Client.
467 sp<Client> client = mClients.valueFor(pid).promote();
468 if (client == 0) {
469 client = new Client(this, pid);
470 mClients.add(pid, client);
471 }
472
473 return client;
474}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700475
Glenn Kasten9e58b552013-01-18 15:09:48 -0800476sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
477{
Glenn Kasten481fb672013-09-30 14:39:28 -0700478 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800479 if (mLogMemoryDealer == 0) {
480 return new NBLog::Writer();
481 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800482 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700483 // Similarly if we can't contact the media.log service, also return a dummy writer
484 if (binder == 0) {
485 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800486 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700487 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
488 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
489 // If allocation fails, consult the vector of previously unregistered writers
490 // and garbage-collect one or more them until an allocation succeeds
491 if (shared == 0) {
492 Mutex::Autolock _l(mUnregisteredWritersLock);
493 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
494 {
495 // Pick the oldest stale writer to garbage-collect
496 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
497 mUnregisteredWriters.removeAt(0);
498 mediaLogService->unregisterWriter(iMemory);
499 // Now the media.log remote reference to IMemory is gone. When our last local
500 // reference to IMemory also drops to zero at end of this block,
501 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
502 }
503 // Re-attempt the allocation
504 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
505 if (shared != 0) {
506 goto success;
507 }
508 }
509 // Even after garbage-collecting all old writers, there is still not enough memory,
510 // so return a dummy writer
511 return new NBLog::Writer();
512 }
513success:
514 mediaLogService->registerWriter(shared, size, name);
515 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800516}
517
518void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
519{
Glenn Kasten685ef092013-02-04 08:15:34 -0800520 if (writer == 0) {
521 return;
522 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800523 sp<IMemory> iMemory(writer->getIMemory());
524 if (iMemory == 0) {
525 return;
526 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700527 // Rather than removing the writer immediately, append it to a queue of old writers to
528 // be garbage-collected later. This allows us to continue to view old logs for a while.
529 Mutex::Autolock _l(mUnregisteredWritersLock);
530 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800531}
532
Mathias Agopian65ab4712010-07-14 17:59:35 -0700533// IAudioFlinger interface
534
535
536sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800537 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700538 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800539 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700540 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800541 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800542 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800544 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800545 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700546 int *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800547 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700548 status_t *status)
549{
550 sp<PlaybackThread::Track> track;
551 sp<TrackHandle> trackHandle;
552 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553 status_t lStatus;
554 int lSessionId;
555
Glenn Kasten263709e2012-01-06 08:40:01 -0800556 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
557 // but if someone uses binder directly they could bypass that and cause us to crash
558 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000559 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700560 lStatus = BAD_VALUE;
561 goto Exit;
562 }
563
Glenn Kasten53b5d092014-02-05 10:00:23 -0800564 // further sample rate checks are performed by createTrack_l() depending on the thread type
565 if (sampleRate == 0) {
566 ALOGE("createTrack() invalid sample rate %u", sampleRate);
567 lStatus = BAD_VALUE;
568 goto Exit;
569 }
570
571 // further channel mask checks are performed by createTrack_l() depending on the thread type
572 if (!audio_is_output_channel(channelMask)) {
573 ALOGE("createTrack() invalid channel mask %#x", channelMask);
574 lStatus = BAD_VALUE;
575 goto Exit;
576 }
577
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700578 // further format checks are performed by createTrack_l() depending on the thread type
579 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800580 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700581 lStatus = BAD_VALUE;
582 goto Exit;
583 }
584
Glenn Kasten663c2242013-09-24 11:52:37 -0700585 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
586 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
587 lStatus = BAD_VALUE;
588 goto Exit;
589 }
590
Mathias Agopian65ab4712010-07-14 17:59:35 -0700591 {
592 Mutex::Autolock _l(mLock);
593 PlaybackThread *thread = checkPlaybackThread_l(output);
594 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800595 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700596 lStatus = BAD_VALUE;
597 goto Exit;
598 }
599
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800600 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -0700601 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700602
Glenn Kastene848bd92014-03-13 15:00:32 -0700603 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700604 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700605 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700606 // check if an effect chain with the same session ID is present on another
607 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700608 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700609 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
610 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700611 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700612 if (sessions & PlaybackThread::EFFECT_SESSION) {
613 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700614 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700615 }
Eric Laurentde070132010-07-13 04:45:46 -0700616 }
617 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700618 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700619 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700620 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700621 if (sessionId != NULL) {
622 *sessionId = lSessionId;
623 }
624 }
Steve Block3856b092011-10-20 11:56:00 +0100625 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700626
627 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800628 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800629 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700630 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700631
632 // move effect chain to this output thread if an effect on same session was waiting
633 // for a track to be created
634 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700635 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700636 Mutex::Autolock _dl(thread->mLock);
637 Mutex::Autolock _sl(effectThread->mLock);
638 moveEffectChain_l(lSessionId, effectThread, thread, true);
639 }
Eric Laurenta011e352012-03-29 15:51:43 -0700640
641 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700642 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700643 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
644 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700645 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700646 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700647 } else {
648 mPendingSyncEvents[i]->cancel();
649 }
Eric Laurenta011e352012-03-29 15:51:43 -0700650 mPendingSyncEvents.removeAt(i);
651 i--;
652 }
653 }
654 }
Glenn Kastene198c362013-08-13 09:13:36 -0700655
Eric Laurentfa90e842014-10-17 18:12:31 -0700656 setAudioHwSyncForSession_l(thread, (audio_session_t)lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657 }
Glenn Kastene198c362013-08-13 09:13:36 -0700658
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700659 if (lStatus != NO_ERROR) {
660 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700661 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700662 // Don't hold mClientLock when releasing the reference on the track as the
663 // destructor will acquire it.
664 {
665 Mutex::Autolock _cl(mClientLock);
666 client.clear();
667 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700668 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700669 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700670 }
671
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700672 // return handle to client
673 trackHandle = new TrackHandle(track);
674
Mathias Agopian65ab4712010-07-14 17:59:35 -0700675Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700676 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700677 return trackHandle;
678}
679
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800680uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700681{
682 Mutex::Autolock _l(mLock);
683 PlaybackThread *thread = checkPlaybackThread_l(output);
684 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000685 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700686 return 0;
687 }
688 return thread->sampleRate();
689}
690
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800691audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700692{
693 Mutex::Autolock _l(mLock);
694 PlaybackThread *thread = checkPlaybackThread_l(output);
695 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000696 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800697 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700698 }
699 return thread->format();
700}
701
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800702size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700703{
704 Mutex::Autolock _l(mLock);
705 PlaybackThread *thread = checkPlaybackThread_l(output);
706 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000707 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700708 return 0;
709 }
Glenn Kasten58912562012-04-03 10:45:00 -0700710 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
711 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712 return thread->frameCount();
713}
714
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800715uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700716{
717 Mutex::Autolock _l(mLock);
718 PlaybackThread *thread = checkPlaybackThread_l(output);
719 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800720 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700721 return 0;
722 }
723 return thread->latency();
724}
725
726status_t AudioFlinger::setMasterVolume(float value)
727{
Eric Laurenta1884f92011-08-23 08:25:03 -0700728 status_t ret = initCheck();
729 if (ret != NO_ERROR) {
730 return ret;
731 }
732
Mathias Agopian65ab4712010-07-14 17:59:35 -0700733 // check calling permissions
734 if (!settingsAllowed()) {
735 return PERMISSION_DENIED;
736 }
737
Eric Laurenta4c5a552012-03-29 10:12:40 -0700738 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700739 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700740
John Grossmanee578c02012-07-23 17:05:46 -0700741 // Set master volume in the HALs which support it.
742 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
743 AutoMutex lock(mHardwareLock);
744 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800745
John Grossmanee578c02012-07-23 17:05:46 -0700746 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
747 if (dev->canSetMasterVolume()) {
748 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800749 }
John Grossmanee578c02012-07-23 17:05:46 -0700750 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700751 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700752
John Grossmanee578c02012-07-23 17:05:46 -0700753 // Now set the master volume in each playback thread. Playback threads
754 // assigned to HALs which do not have master volume support will apply
755 // master volume during the mix operation. Threads with HALs which do
756 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800757 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700758 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700759
760 return NO_ERROR;
761}
762
Glenn Kastenf78aee72012-01-04 11:00:47 -0800763status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700764{
Eric Laurenta1884f92011-08-23 08:25:03 -0700765 status_t ret = initCheck();
766 if (ret != NO_ERROR) {
767 return ret;
768 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700769
770 // check calling permissions
771 if (!settingsAllowed()) {
772 return PERMISSION_DENIED;
773 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800774 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000775 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700776 return BAD_VALUE;
777 }
778
779 { // scope for the lock
780 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700781 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700782 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700783 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700784 mHardwareStatus = AUDIO_HW_IDLE;
785 }
786
787 if (NO_ERROR == ret) {
788 Mutex::Autolock _l(mLock);
789 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800790 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700791 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700792 }
793
794 return ret;
795}
796
797status_t AudioFlinger::setMicMute(bool state)
798{
Eric Laurenta1884f92011-08-23 08:25:03 -0700799 status_t ret = initCheck();
800 if (ret != NO_ERROR) {
801 return ret;
802 }
803
Mathias Agopian65ab4712010-07-14 17:59:35 -0700804 // check calling permissions
805 if (!settingsAllowed()) {
806 return PERMISSION_DENIED;
807 }
808
809 AutoMutex lock(mHardwareLock);
810 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700811 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
812 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
813 status_t result = dev->set_mic_mute(dev, state);
814 if (result != NO_ERROR) {
815 ret = result;
816 }
817 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818 mHardwareStatus = AUDIO_HW_IDLE;
819 return ret;
820}
821
822bool AudioFlinger::getMicMute() const
823{
Eric Laurenta1884f92011-08-23 08:25:03 -0700824 status_t ret = initCheck();
825 if (ret != NO_ERROR) {
826 return false;
827 }
828
Dima Zavinfce7a472011-04-19 22:30:36 -0700829 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800830 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700831 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700832 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700833 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700834 mHardwareStatus = AUDIO_HW_IDLE;
835 return state;
836}
837
838status_t AudioFlinger::setMasterMute(bool muted)
839{
John Grossmand8f178d2012-07-20 14:51:35 -0700840 status_t ret = initCheck();
841 if (ret != NO_ERROR) {
842 return ret;
843 }
844
Mathias Agopian65ab4712010-07-14 17:59:35 -0700845 // check calling permissions
846 if (!settingsAllowed()) {
847 return PERMISSION_DENIED;
848 }
849
John Grossmanee578c02012-07-23 17:05:46 -0700850 Mutex::Autolock _l(mLock);
851 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700852
John Grossmanee578c02012-07-23 17:05:46 -0700853 // Set master mute in the HALs which support it.
854 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
855 AutoMutex lock(mHardwareLock);
856 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700857
John Grossmanee578c02012-07-23 17:05:46 -0700858 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
859 if (dev->canSetMasterMute()) {
860 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700861 }
John Grossmanee578c02012-07-23 17:05:46 -0700862 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700863 }
864
John Grossmanee578c02012-07-23 17:05:46 -0700865 // Now set the master mute in each playback thread. Playback threads
866 // assigned to HALs which do not have master mute support will apply master
867 // mute during the mix operation. Threads with HALs which do support master
868 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800869 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700870 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700871
872 return NO_ERROR;
873}
874
875float AudioFlinger::masterVolume() const
876{
Glenn Kasten98067102011-12-13 11:47:54 -0800877 Mutex::Autolock _l(mLock);
878 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700879}
880
881bool AudioFlinger::masterMute() const
882{
Glenn Kasten98067102011-12-13 11:47:54 -0800883 Mutex::Autolock _l(mLock);
884 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700885}
886
John Grossman4ff14ba2012-02-08 16:37:41 -0800887float AudioFlinger::masterVolume_l() const
888{
John Grossman4ff14ba2012-02-08 16:37:41 -0800889 return mMasterVolume;
890}
891
John Grossmand8f178d2012-07-20 14:51:35 -0700892bool AudioFlinger::masterMute_l() const
893{
John Grossmanee578c02012-07-23 17:05:46 -0700894 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700895}
896
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800897status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
898 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700899{
900 // check calling permissions
901 if (!settingsAllowed()) {
902 return PERMISSION_DENIED;
903 }
904
Glenn Kasten263709e2012-01-06 08:40:01 -0800905 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000906 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700907 return BAD_VALUE;
908 }
909
910 AutoMutex lock(mLock);
911 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700912 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700913 thread = checkPlaybackThread_l(output);
914 if (thread == NULL) {
915 return BAD_VALUE;
916 }
917 }
918
919 mStreamTypes[stream].volume = value;
920
921 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800922 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700923 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700924 }
925 } else {
926 thread->setStreamVolume(stream, value);
927 }
928
929 return NO_ERROR;
930}
931
Glenn Kastenfff6d712012-01-12 16:38:12 -0800932status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700933{
934 // check calling permissions
935 if (!settingsAllowed()) {
936 return PERMISSION_DENIED;
937 }
938
Glenn Kasten263709e2012-01-06 08:40:01 -0800939 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700940 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000941 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700942 return BAD_VALUE;
943 }
944
Eric Laurent93575202011-01-18 18:39:02 -0800945 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700947 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700948 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700949
950 return NO_ERROR;
951}
952
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800953float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700954{
Glenn Kasten263709e2012-01-06 08:40:01 -0800955 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700956 return 0.0f;
957 }
958
959 AutoMutex lock(mLock);
960 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -0700961 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700962 PlaybackThread *thread = checkPlaybackThread_l(output);
963 if (thread == NULL) {
964 return 0.0f;
965 }
966 volume = thread->streamVolume(stream);
967 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800968 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700969 }
970
971 return volume;
972}
973
Glenn Kastenfff6d712012-01-12 16:38:12 -0800974bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700975{
Glenn Kasten263709e2012-01-06 08:40:01 -0800976 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700977 return true;
978 }
979
Glenn Kasten6637baa2012-01-09 09:40:36 -0800980 AutoMutex lock(mLock);
981 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700982}
983
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800984status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700985{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700986 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
987 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800988
Mathias Agopian65ab4712010-07-14 17:59:35 -0700989 // check calling permissions
990 if (!settingsAllowed()) {
991 return PERMISSION_DENIED;
992 }
993
Glenn Kasten142f5192014-03-25 17:44:59 -0700994 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
995 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700996 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700997 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800998 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700999 AutoMutex lock(mHardwareLock);
1000 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1001 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1002 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1003 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1004 final_result = result ?: final_result;
1005 }
1006 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001007 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001008 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1009 AudioParameter param = AudioParameter(keyValuePairs);
1010 String8 value;
1011 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001012 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1013 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001014 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1015 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001016 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001017 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1018 // collect all of the thread's session IDs
1019 KeyedVector<int, bool> ids = thread->sessionIds();
1020 // suspend effects associated with those session IDs
1021 for (size_t j = 0; j < ids.size(); ++j) {
1022 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001023 thread->setEffectSuspended(FX_IID_AEC,
1024 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001025 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001026 thread->setEffectSuspended(FX_IID_NS,
1027 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001028 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001029 }
1030 }
Eric Laurentbee53372011-08-29 12:42:48 -07001031 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001032 }
1033 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001034 String8 screenState;
1035 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1036 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001037 if (isOff != (AudioFlinger::mScreenState & 1)) {
1038 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001039 }
1040 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001041 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001042 }
1043
1044 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1045 // and the thread is exited once the lock is released
1046 sp<ThreadBase> thread;
1047 {
1048 Mutex::Autolock _l(mLock);
1049 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001050 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001051 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001052 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001053 // indicate output device change to all input threads for pre processing
1054 AudioParameter param = AudioParameter(keyValuePairs);
1055 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001056 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1057 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001058 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1059 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1060 }
1061 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062 }
1063 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001064 if (thread != 0) {
1065 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001066 }
1067 return BAD_VALUE;
1068}
1069
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001070String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001071{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001072 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1073 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001074
Eric Laurenta4c5a552012-03-29 10:12:40 -07001075 Mutex::Autolock _l(mLock);
1076
Glenn Kasten142f5192014-03-25 17:44:59 -07001077 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001078 String8 out_s8;
1079
Dima Zavin799a70e2011-04-18 16:57:27 -07001080 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001081 char *s;
1082 {
1083 AutoMutex lock(mHardwareLock);
1084 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001085 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001086 s = dev->get_parameters(dev, keys.string());
1087 mHardwareStatus = AUDIO_HW_IDLE;
1088 }
John Grossmanef7740b2012-02-09 11:28:36 -08001089 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001090 free(s);
1091 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001092 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001093 }
1094
Mathias Agopian65ab4712010-07-14 17:59:35 -07001095 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1096 if (playbackThread != NULL) {
1097 return playbackThread->getParameters(keys);
1098 }
1099 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1100 if (recordThread != NULL) {
1101 return recordThread->getParameters(keys);
1102 }
1103 return String8("");
1104}
1105
Glenn Kastendd8104c2012-07-02 12:42:44 -07001106size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1107 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001108{
Eric Laurenta1884f92011-08-23 08:25:03 -07001109 status_t ret = initCheck();
1110 if (ret != NO_ERROR) {
1111 return 0;
1112 }
1113
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001114 AutoMutex lock(mHardwareLock);
1115 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001116 audio_config_t config;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001117 memset(&config, 0, sizeof(config));
1118 config.sample_rate = sampleRate;
1119 config.channel_mask = channelMask;
1120 config.format = format;
1121
John Grossmanee578c02012-07-23 17:05:46 -07001122 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1123 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001124 mHardwareStatus = AUDIO_HW_IDLE;
1125 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001126}
1127
Glenn Kasten5f972c02014-01-13 09:59:31 -08001128uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001129{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001130 Mutex::Autolock _l(mLock);
1131
1132 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1133 if (recordThread != NULL) {
1134 return recordThread->getInputFramesLost();
1135 }
1136 return 0;
1137}
1138
1139status_t AudioFlinger::setVoiceVolume(float value)
1140{
Eric Laurenta1884f92011-08-23 08:25:03 -07001141 status_t ret = initCheck();
1142 if (ret != NO_ERROR) {
1143 return ret;
1144 }
1145
Mathias Agopian65ab4712010-07-14 17:59:35 -07001146 // check calling permissions
1147 if (!settingsAllowed()) {
1148 return PERMISSION_DENIED;
1149 }
1150
1151 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001152 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001153 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001154 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001155 mHardwareStatus = AUDIO_HW_IDLE;
1156
1157 return ret;
1158}
1159
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001160status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001161 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001162{
1163 status_t status;
1164
1165 Mutex::Autolock _l(mLock);
1166
1167 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1168 if (playbackThread != NULL) {
1169 return playbackThread->getRenderPosition(halFrames, dspFrames);
1170 }
1171
1172 return BAD_VALUE;
1173}
1174
1175void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1176{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001177 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001178 if (client == 0) {
1179 return;
1180 }
Eric Laurent021cf962014-05-13 10:18:14 -07001181 bool clientAdded = false;
1182 {
1183 Mutex::Autolock _cl(mClientLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001184
Eric Laurent021cf962014-05-13 10:18:14 -07001185 pid_t pid = IPCThreadState::self()->getCallingPid();
1186 if (mNotificationClients.indexOfKey(pid) < 0) {
1187 sp<NotificationClient> notificationClient = new NotificationClient(this,
1188 client,
1189 pid);
1190 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001191
Eric Laurent021cf962014-05-13 10:18:14 -07001192 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001193
Eric Laurent021cf962014-05-13 10:18:14 -07001194 sp<IBinder> binder = client->asBinder();
1195 binder->linkToDeath(notificationClient);
1196 clientAdded = true;
1197 }
1198 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001199
Eric Laurent021cf962014-05-13 10:18:14 -07001200 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001201 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent021cf962014-05-13 10:18:14 -07001202 if (clientAdded) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001203 // the config change is always sent from playback or record threads to avoid deadlock
1204 // with AudioSystem::gLock
1205 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001206 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001207 }
1208
1209 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001210 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211 }
1212 }
1213}
1214
1215void AudioFlinger::removeNotificationClient(pid_t pid)
1216{
1217 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001218 {
1219 Mutex::Autolock _cl(mClientLock);
1220 mNotificationClients.removeItem(pid);
1221 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001222
Steve Block3856b092011-10-20 11:56:00 +01001223 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001224 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001225 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001226 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001227 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001228 ALOGV(" pid %d @ %d", ref->mPid, i);
1229 if (ref->mPid == pid) {
1230 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001231 mAudioSessionRefs.removeAt(i);
1232 delete ref;
1233 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001234 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001235 } else {
1236 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001237 }
1238 }
1239 if (removed) {
1240 purgeStaleEffects_l();
1241 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001242}
1243
Eric Laurent021cf962014-05-13 10:18:14 -07001244void AudioFlinger::audioConfigChanged(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001245{
Eric Laurent021cf962014-05-13 10:18:14 -07001246 Mutex::Autolock _l(mClientLock);
1247 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001248 for (size_t i = 0; i < size; i++) {
Eric Laurent021cf962014-05-13 10:18:14 -07001249 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event,
Eric Laurent10351942014-05-08 18:49:52 -07001250 ioHandle,
1251 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001252 }
1253}
1254
Eric Laurent021cf962014-05-13 10:18:14 -07001255// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001256void AudioFlinger::removeClient_l(pid_t pid)
1257{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001258 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001259 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001260 mClients.removeItem(pid);
1261}
1262
Eric Laurent717e1282012-06-29 16:36:52 -07001263// getEffectThread_l() must be called with AudioFlinger::mLock held
1264sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1265{
1266 sp<PlaybackThread> thread;
1267
1268 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1269 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1270 ALOG_ASSERT(thread == 0);
1271 thread = mPlaybackThreads.valueAt(i);
1272 }
1273 }
1274
1275 return thread;
1276}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001277
Mathias Agopian65ab4712010-07-14 17:59:35 -07001278
Mathias Agopian65ab4712010-07-14 17:59:35 -07001279
1280// ----------------------------------------------------------------------------
1281
1282AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1283 : RefBase(),
1284 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001285 // 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 -07001286 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001287 mPid(pid),
1288 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001289{
1290 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1291}
1292
Eric Laurent021cf962014-05-13 10:18:14 -07001293// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001294AudioFlinger::Client::~Client()
1295{
1296 mAudioFlinger->removeClient_l(mPid);
1297}
1298
Glenn Kasten435dbe62012-01-30 10:15:48 -08001299sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001300{
1301 return mMemoryDealer;
1302}
1303
John Grossman4ff14ba2012-02-08 16:37:41 -08001304// Reserve one of the limited slots for a timed audio track associated
1305// with this client
1306bool AudioFlinger::Client::reserveTimedTrack()
1307{
1308 const int kMaxTimedTracksPerClient = 4;
1309
1310 Mutex::Autolock _l(mTimedTrackLock);
1311
1312 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1313 ALOGW("can not create timed track - pid %d has exceeded the limit",
1314 mPid);
1315 return false;
1316 }
1317
1318 mTimedTrackCount++;
1319 return true;
1320}
1321
1322// Release a slot for a timed audio track
1323void AudioFlinger::Client::releaseTimedTrack()
1324{
1325 Mutex::Autolock _l(mTimedTrackLock);
1326 mTimedTrackCount--;
1327}
1328
Mathias Agopian65ab4712010-07-14 17:59:35 -07001329// ----------------------------------------------------------------------------
1330
1331AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1332 const sp<IAudioFlingerClient>& client,
1333 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001334 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001335{
1336}
1337
1338AudioFlinger::NotificationClient::~NotificationClient()
1339{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001340}
1341
Glenn Kasten0f11b512014-01-31 16:18:54 -08001342void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001343{
1344 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001345 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001346}
1347
Mathias Agopian65ab4712010-07-14 17:59:35 -07001348
1349// ----------------------------------------------------------------------------
1350
Jeff Brown893a5642013-08-16 20:19:26 -07001351static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1352 return audio_is_remote_submix_device(inDevice);
1353}
1354
Mathias Agopian65ab4712010-07-14 17:59:35 -07001355sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001356 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001357 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001358 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001359 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001360 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001361 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001362 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001363 int *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001364 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001365 sp<IMemory>& cblk,
1366 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001367 status_t *status)
1368{
1369 sp<RecordThread::RecordTrack> recordTrack;
1370 sp<RecordHandle> recordHandle;
1371 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001372 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001373 int lSessionId;
1374
Glenn Kastend776ac62014-05-07 09:16:09 -07001375 cblk.clear();
1376 buffers.clear();
1377
Mathias Agopian65ab4712010-07-14 17:59:35 -07001378 // check calling permissions
1379 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001380 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001381 lStatus = PERMISSION_DENIED;
1382 goto Exit;
1383 }
1384
Glenn Kasten53b5d092014-02-05 10:00:23 -08001385 // further sample rate checks are performed by createRecordTrack_l()
1386 if (sampleRate == 0) {
1387 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1388 lStatus = BAD_VALUE;
1389 goto Exit;
1390 }
1391
Glenn Kasten0e0e8462014-03-13 15:02:40 -07001392 // we don't yet support anything other than 16-bit PCM
1393 if (!(audio_is_valid_format(format) &&
1394 audio_is_linear_pcm(format) && format == AUDIO_FORMAT_PCM_16_BIT)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001395 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001396 lStatus = BAD_VALUE;
1397 goto Exit;
1398 }
1399
Glenn Kasten53b5d092014-02-05 10:00:23 -08001400 // further channel mask checks are performed by createRecordTrack_l()
1401 if (!audio_is_input_channel(channelMask)) {
1402 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1403 lStatus = BAD_VALUE;
1404 goto Exit;
1405 }
1406
Glenn Kasten05997e22014-03-13 15:08:33 -07001407 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001408 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001409 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001410 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001411 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001412 lStatus = BAD_VALUE;
1413 goto Exit;
1414 }
1415
Jeff Brown893a5642013-08-16 20:19:26 -07001416 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1417 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001418 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001419 lStatus = PERMISSION_DENIED;
1420 goto Exit;
1421 }
1422
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001423 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001424 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001425
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001426 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001427 lSessionId = *sessionId;
1428 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001429 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001430 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001431 if (sessionId != NULL) {
1432 *sessionId = lSessionId;
1433 }
1434 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001435 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001436
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001437 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001438 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001439 frameCount, lSessionId, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001440 IPCThreadState::self()->getCallingUid(),
1441 flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001442 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001443
1444 if (lStatus == NO_ERROR) {
1445 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1446 // session and move it to this thread.
1447 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)lSessionId);
1448 if (chain != 0) {
1449 Mutex::Autolock _l(thread->mLock);
1450 thread->addEffectChain_l(chain);
1451 }
1452 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001453 }
Glenn Kastene198c362013-08-13 09:13:36 -07001454
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001455 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001456 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001457 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001458 // Don't hold mClientLock when releasing the reference on the track as the
1459 // destructor will acquire it.
1460 {
1461 Mutex::Autolock _cl(mClientLock);
1462 client.clear();
1463 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001464 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001465 goto Exit;
1466 }
1467
Glenn Kastend776ac62014-05-07 09:16:09 -07001468 cblk = recordTrack->getCblk();
1469 buffers = recordTrack->getBuffers();
1470
Glenn Kasten2fc14732013-08-05 14:58:14 -07001471 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001472 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001473
1474Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001475 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001476 return recordHandle;
1477}
1478
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001479
1480
Mathias Agopian65ab4712010-07-14 17:59:35 -07001481// ----------------------------------------------------------------------------
1482
Eric Laurenta4c5a552012-03-29 10:12:40 -07001483audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1484{
Eric Laurent44622db2014-08-01 19:00:33 -07001485 if (name == NULL) {
1486 return 0;
1487 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001488 if (!settingsAllowed()) {
1489 return 0;
1490 }
1491 Mutex::Autolock _l(mLock);
1492 return loadHwModule_l(name);
1493}
1494
1495// loadHwModule_l() must be called with AudioFlinger::mLock held
1496audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1497{
1498 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1499 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1500 ALOGW("loadHwModule() module %s already loaded", name);
1501 return mAudioHwDevs.keyAt(i);
1502 }
1503 }
1504
Eric Laurenta4c5a552012-03-29 10:12:40 -07001505 audio_hw_device_t *dev;
1506
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001507 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001508 if (rc) {
1509 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1510 return 0;
1511 }
1512
1513 mHardwareStatus = AUDIO_HW_INIT;
1514 rc = dev->init_check(dev);
1515 mHardwareStatus = AUDIO_HW_IDLE;
1516 if (rc) {
1517 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1518 return 0;
1519 }
1520
John Grossmanee578c02012-07-23 17:05:46 -07001521 // Check and cache this HAL's level of support for master mute and master
1522 // volume. If this is the first HAL opened, and it supports the get
1523 // methods, use the initial values provided by the HAL as the current
1524 // master mute and volume settings.
1525
1526 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1527 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001528 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001529
1530 if (0 == mAudioHwDevs.size()) {
1531 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1532 if (NULL != dev->get_master_volume) {
1533 float mv;
1534 if (OK == dev->get_master_volume(dev, &mv)) {
1535 mMasterVolume = mv;
1536 }
1537 }
1538
1539 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1540 if (NULL != dev->get_master_mute) {
1541 bool mm;
1542 if (OK == dev->get_master_mute(dev, &mm)) {
1543 mMasterMute = mm;
1544 }
1545 }
1546 }
1547
Eric Laurenta4c5a552012-03-29 10:12:40 -07001548 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001549 if ((NULL != dev->set_master_volume) &&
1550 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1551 flags = static_cast<AudioHwDevice::Flags>(flags |
1552 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1553 }
1554
1555 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1556 if ((NULL != dev->set_master_mute) &&
1557 (OK == dev->set_master_mute(dev, mMasterMute))) {
1558 flags = static_cast<AudioHwDevice::Flags>(flags |
1559 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1560 }
1561
Eric Laurenta4c5a552012-03-29 10:12:40 -07001562 mHardwareStatus = AUDIO_HW_IDLE;
1563 }
1564
1565 audio_module_handle_t handle = nextUniqueId();
Eric Laurent83b88082014-06-20 18:31:16 -07001566 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001567
1568 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001569 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001570
1571 return handle;
1572
1573}
1574
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001575// ----------------------------------------------------------------------------
1576
Glenn Kasten3b16c762012-11-14 08:44:39 -08001577uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001578{
1579 Mutex::Autolock _l(mLock);
1580 PlaybackThread *thread = primaryPlaybackThread_l();
1581 return thread != NULL ? thread->sampleRate() : 0;
1582}
1583
Glenn Kastene33054e2012-11-14 12:54:39 -08001584size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001585{
1586 Mutex::Autolock _l(mLock);
1587 PlaybackThread *thread = primaryPlaybackThread_l();
1588 return thread != NULL ? thread->frameCountHAL() : 0;
1589}
1590
1591// ----------------------------------------------------------------------------
1592
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001593status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1594{
1595 uid_t uid = IPCThreadState::self()->getCallingUid();
1596 if (uid != AID_SYSTEM) {
1597 return PERMISSION_DENIED;
1598 }
1599 Mutex::Autolock _l(mLock);
1600 if (mIsDeviceTypeKnown) {
1601 return INVALID_OPERATION;
1602 }
1603 mIsLowRamDevice = isLowRamDevice;
1604 mIsDeviceTypeKnown = true;
1605 return NO_ERROR;
1606}
1607
Eric Laurent93c3d412014-08-01 14:48:35 -07001608audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1609{
1610 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001611
1612 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1613 if (index >= 0) {
1614 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1615 mHwAvSyncIds.valueAt(index), sessionId);
1616 return mHwAvSyncIds.valueAt(index);
1617 }
1618
1619 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1620 if (dev == NULL) {
1621 return AUDIO_HW_SYNC_INVALID;
1622 }
1623 char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1624 AudioParameter param = AudioParameter(String8(reply));
1625 free(reply);
1626
1627 int value;
1628 if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1629 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1630 return AUDIO_HW_SYNC_INVALID;
1631 }
1632
1633 // allow only one session for a given HW A/V sync ID.
1634 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1635 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1636 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1637 value, mHwAvSyncIds.keyAt(i));
1638 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001639 break;
1640 }
1641 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001642
1643 mHwAvSyncIds.add(sessionId, value);
1644
1645 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1646 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1647 uint32_t sessions = thread->hasAudioSession(sessionId);
1648 if (sessions & PlaybackThread::TRACK_SESSION) {
1649 AudioParameter param = AudioParameter();
1650 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1651 thread->setParameters(param.toString());
1652 break;
1653 }
1654 }
1655
1656 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1657 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001658}
1659
Eric Laurentfa90e842014-10-17 18:12:31 -07001660// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1661void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1662{
1663 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1664 if (index >= 0) {
1665 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1666 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1667 AudioParameter param = AudioParameter();
1668 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1669 thread->setParameters(param.toString());
1670 }
1671}
1672
1673
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001674// ----------------------------------------------------------------------------
1675
Eric Laurent83b88082014-06-20 18:31:16 -07001676
1677sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001678 audio_io_handle_t *output,
1679 audio_config_t *config,
1680 audio_devices_t devices,
1681 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001682 audio_output_flags_t flags)
1683{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001684 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001685 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001686 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001687 }
1688
1689 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentcf2c0212014-07-25 16:20:43 -07001690 if (*output == AUDIO_IO_HANDLE_NONE) {
1691 *output = nextUniqueId();
1692 }
Eric Laurent83b88082014-06-20 18:31:16 -07001693
1694 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1695
1696 audio_stream_out_t *outStream = NULL;
1697
1698 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001699 // This if statement allows overriding the audio policy settings
1700 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1701 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1702 // Check only for Normal Mixing mode
1703 if (kEnableExtendedPrecision) {
1704 // Specify format (uncomment one below to choose)
1705 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1706 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1707 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1708 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001709 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001710 }
1711 if (kEnableExtendedChannels) {
1712 // Specify channel mask (uncomment one below to choose)
1713 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1714 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1715 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1716 }
Eric Laurent83b88082014-06-20 18:31:16 -07001717 }
1718
1719 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001720 *output,
1721 devices,
1722 flags,
1723 config,
1724 &outStream,
1725 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001726
1727 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001728 ALOGV("openOutput_l() openOutputStream returned output %p, sampleRate %d, Format %#x, "
Andy Hung9a592762014-07-21 21:56:01 -07001729 "channelMask %#x, status %d",
Eric Laurent83b88082014-06-20 18:31:16 -07001730 outStream,
1731 config->sample_rate,
1732 config->format,
1733 config->channel_mask,
1734 status);
1735
1736 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001737 AudioStreamOut *outputStream = new AudioStreamOut(outHwDev, outStream, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001738
1739 PlaybackThread *thread;
1740 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001741 thread = new OffloadThread(this, outputStream, *output, devices);
1742 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001743 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1744 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001745 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001746 thread = new DirectOutputThread(this, outputStream, *output, devices);
1747 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001748 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001749 thread = new MixerThread(this, outputStream, *output, devices);
1750 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001751 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001752 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001753 return thread;
1754 }
1755
1756 return 0;
1757}
1758
Eric Laurentcf2c0212014-07-25 16:20:43 -07001759status_t AudioFlinger::openOutput(audio_module_handle_t module,
1760 audio_io_handle_t *output,
1761 audio_config_t *config,
1762 audio_devices_t *devices,
1763 const String8& address,
1764 uint32_t *latencyMs,
1765 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001766{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001767 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001768 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001769 (devices != NULL) ? *devices : 0,
1770 config->sample_rate,
1771 config->format,
1772 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001773 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001774
Eric Laurentcf2c0212014-07-25 16:20:43 -07001775 if (*devices == AUDIO_DEVICE_NONE) {
1776 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001777 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001778
Mathias Agopian65ab4712010-07-14 17:59:35 -07001779 Mutex::Autolock _l(mLock);
1780
Eric Laurentcf2c0212014-07-25 16:20:43 -07001781 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001782 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001783 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001784
1785 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001786 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001787
1788 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001789 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001790 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001791 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001792
1793 AutoMutex lock(mHardwareLock);
1794 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001795 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001796 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten4ea00a22014-06-02 08:29:22 -07001797
Eric Laurentcf2c0212014-07-25 16:20:43 -07001798 mPrimaryOutputSampleRate = config->sample_rate;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001799 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001800 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001801 }
1802
Eric Laurentcf2c0212014-07-25 16:20:43 -07001803 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001804}
1805
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001806audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1807 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001808{
1809 Mutex::Autolock _l(mLock);
1810 MixerThread *thread1 = checkMixerThread_l(output1);
1811 MixerThread *thread2 = checkMixerThread_l(output2);
1812
1813 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001814 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1815 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001816 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001817 }
1818
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001819 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001820 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1821 thread->addOutputTrack(thread2);
1822 mPlaybackThreads.add(id, thread);
1823 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001824 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001825 return id;
1826}
1827
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001828status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001829{
Glenn Kastend96c5722012-04-25 13:44:49 -07001830 return closeOutput_nonvirtual(output);
1831}
1832
1833status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1834{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001835 // keep strong reference on the playback thread so that
1836 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001837 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001838 {
1839 Mutex::Autolock _l(mLock);
1840 thread = checkPlaybackThread_l(output);
1841 if (thread == NULL) {
1842 return BAD_VALUE;
1843 }
1844
Steve Block3856b092011-10-20 11:56:00 +01001845 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001846
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001847 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001848 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001849 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001850 DuplicatingThread *dupThread =
1851 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001852 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001853
1854 }
1855 }
1856 }
1857
1858
1859 mPlaybackThreads.removeItem(output);
1860 // save all effects to the default thread
1861 if (mPlaybackThreads.size()) {
1862 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1863 if (dstThread != NULL) {
1864 // audioflinger lock is held here so the acquisition order of thread locks does not
1865 // matter
1866 Mutex::Autolock _dl(dstThread->mLock);
1867 Mutex::Autolock _sl(thread->mLock);
1868 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1869 for (size_t i = 0; i < effectChains.size(); i ++) {
1870 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001871 }
1872 }
1873 }
Eric Laurent021cf962014-05-13 10:18:14 -07001874 audioConfigChanged(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001875 }
1876 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001877 // The thread entity (active unit of execution) is no longer running here,
1878 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001879
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001880 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurent83b88082014-06-20 18:31:16 -07001881 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001882 }
Eric Laurent83b88082014-06-20 18:31:16 -07001883
Mathias Agopian65ab4712010-07-14 17:59:35 -07001884 return NO_ERROR;
1885}
1886
Eric Laurent83b88082014-06-20 18:31:16 -07001887void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
1888{
1889 AudioStreamOut *out = thread->clearOutput();
1890 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1891 // from now on thread->mOutput is NULL
1892 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
1893 delete out;
1894}
1895
1896void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
1897{
1898 mPlaybackThreads.removeItem(thread->mId);
1899 thread->exit();
1900 closeOutputFinish(thread);
1901}
1902
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001903status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001904{
1905 Mutex::Autolock _l(mLock);
1906 PlaybackThread *thread = checkPlaybackThread_l(output);
1907
1908 if (thread == NULL) {
1909 return BAD_VALUE;
1910 }
1911
Steve Block3856b092011-10-20 11:56:00 +01001912 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001913 thread->suspend();
1914
1915 return NO_ERROR;
1916}
1917
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001918status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001919{
1920 Mutex::Autolock _l(mLock);
1921 PlaybackThread *thread = checkPlaybackThread_l(output);
1922
1923 if (thread == NULL) {
1924 return BAD_VALUE;
1925 }
1926
Steve Block3856b092011-10-20 11:56:00 +01001927 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001928
1929 thread->restore();
1930
1931 return NO_ERROR;
1932}
1933
Eric Laurentcf2c0212014-07-25 16:20:43 -07001934status_t AudioFlinger::openInput(audio_module_handle_t module,
1935 audio_io_handle_t *input,
1936 audio_config_t *config,
1937 audio_devices_t *device,
1938 const String8& address,
1939 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07001940 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001941{
Eric Laurent83b88082014-06-20 18:31:16 -07001942 Mutex::Autolock _l(mLock);
1943
Eric Laurentcf2c0212014-07-25 16:20:43 -07001944 if (*device == AUDIO_DEVICE_NONE) {
1945 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07001946 }
1947
Eric Laurentcf2c0212014-07-25 16:20:43 -07001948 sp<RecordThread> thread = openInput_l(module, input, config, *device, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001949
1950 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07001951 // notify client processes of the new input creation
1952 thread->audioConfigChanged(AudioSystem::INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001953 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001954 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001955 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07001956}
Dima Zavin799a70e2011-04-18 16:57:27 -07001957
Eric Laurent83b88082014-06-20 18:31:16 -07001958sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001959 audio_io_handle_t *input,
1960 audio_config_t *config,
Eric Laurent83b88082014-06-20 18:31:16 -07001961 audio_devices_t device,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001962 const String8& address,
1963 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07001964 audio_input_flags_t flags)
1965{
Eric Laurent83b88082014-06-20 18:31:16 -07001966 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, device);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001967 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001968 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07001969 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001970 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001971
Eric Laurentcf2c0212014-07-25 16:20:43 -07001972 if (*input == AUDIO_IO_HANDLE_NONE) {
1973 *input = nextUniqueId();
1974 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001975
Eric Laurentcf2c0212014-07-25 16:20:43 -07001976 audio_config_t halconfig = *config;
1977 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07001978 audio_stream_in_t *inStream = NULL;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001979 status_t status = inHwHal->open_input_stream(inHwHal, *input, device, &halconfig,
1980 &inStream, flags, address.string(), source);
1981 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001982 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Dima Zavin799a70e2011-04-18 16:57:27 -07001983 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001984 halconfig.sample_rate,
1985 halconfig.format,
1986 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07001987 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001988 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001989
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001990 // If the input could not be opened with the requested parameters and we can handle the
1991 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1992 // 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 -07001993 if (status == BAD_VALUE &&
Eric Laurentcf2c0212014-07-25 16:20:43 -07001994 config->format == halconfig.format && halconfig.format == AUDIO_FORMAT_PCM_16_BIT &&
1995 (halconfig.sample_rate <= 2 * config->sample_rate) &&
1996 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
1997 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07001998 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07001999 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002000 inStream = NULL;
Eric Laurentcf2c0212014-07-25 16:20:43 -07002001 status = inHwHal->open_input_stream(inHwHal, *input, device, &halconfig,
2002 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07002003 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002004 }
2005
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002006 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002007
Glenn Kasten46909e72013-02-26 09:20:22 -08002008#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002009 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2010 // or (re-)create if current Pipe is idle and does not match the new format
2011 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002012 enum {
2013 TEE_SINK_NO, // don't copy input
2014 TEE_SINK_NEW, // copy input using a new pipe
2015 TEE_SINK_OLD, // copy input using an existing pipe
2016 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002017 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2018 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002019 if (!mTeeSinkInputEnabled) {
2020 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002021 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002022 kind = TEE_SINK_NO;
2023 } else if (mRecordTeeSink == 0) {
2024 kind = TEE_SINK_NEW;
2025 } else if (mRecordTeeSink->getStrongCount() != 1) {
2026 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002027 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002028 kind = TEE_SINK_OLD;
2029 } else {
2030 kind = TEE_SINK_NEW;
2031 }
2032 switch (kind) {
2033 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002034 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002035 size_t numCounterOffers = 0;
2036 const NBAIO_Format offers[1] = {format};
2037 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2038 ALOG_ASSERT(index == 0);
2039 PipeReader *pipeReader = new PipeReader(*pipe);
2040 numCounterOffers = 0;
2041 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2042 ALOG_ASSERT(index == 0);
2043 mRecordTeeSink = pipe;
2044 mRecordTeeSource = pipeReader;
2045 teeSink = pipe;
2046 }
2047 break;
2048 case TEE_SINK_OLD:
2049 teeSink = mRecordTeeSink;
2050 break;
2051 case TEE_SINK_NO:
2052 default:
2053 break;
2054 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002055#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002056
Eric Laurentcf2c0212014-07-25 16:20:43 -07002057 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002058
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002059 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002060 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002061 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002062 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002063 inputStream,
2064 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002065 primaryOutputDevice_l(),
Eric Laurent83b88082014-06-20 18:31:16 -07002066 device
Glenn Kasten46909e72013-02-26 09:20:22 -08002067#ifdef TEE_SINK
2068 , teeSink
2069#endif
2070 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002071 mRecordThreads.add(*input, thread);
2072 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002073 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002074 }
2075
Eric Laurentcf2c0212014-07-25 16:20:43 -07002076 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002077 return 0;
2078}
2079
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002080status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002081{
Glenn Kastend96c5722012-04-25 13:44:49 -07002082 return closeInput_nonvirtual(input);
2083}
2084
2085status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2086{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002087 // keep strong reference on the record thread so that
2088 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002089 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002090 {
2091 Mutex::Autolock _l(mLock);
2092 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002093 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002094 return BAD_VALUE;
2095 }
2096
Steve Block3856b092011-10-20 11:56:00 +01002097 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002098
2099 // If we still have effect chains, it means that a client still holds a handle
2100 // on at least one effect. We must either move the chain to an existing thread with the
2101 // same session ID or put it aside in case a new record thread is opened for a
2102 // new capture on the same session
2103 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002104 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002105 Mutex::Autolock _sl(thread->mLock);
2106 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002107 // Note: maximum one chain per record thread
2108 if (effectChains.size() != 0) {
2109 chain = effectChains[0];
2110 }
2111 }
2112 if (chain != 0) {
2113 // first check if a record thread is already opened with a client on the same session.
2114 // This should only happen in case of overlap between one thread tear down and the
2115 // creation of its replacement
2116 size_t i;
2117 for (i = 0; i < mRecordThreads.size(); i++) {
2118 sp<RecordThread> t = mRecordThreads.valueAt(i);
2119 if (t == thread) {
2120 continue;
2121 }
2122 if (t->hasAudioSession(chain->sessionId()) != 0) {
2123 Mutex::Autolock _l(t->mLock);
2124 ALOGV("closeInput() found thread %d for effect session %d",
2125 t->id(), chain->sessionId());
2126 t->addEffectChain_l(chain);
2127 break;
2128 }
2129 }
2130 // put the chain aside if we could not find a record thread with the same session id.
2131 if (i == mRecordThreads.size()) {
2132 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002133 }
2134 }
Eric Laurent021cf962014-05-13 10:18:14 -07002135 audioConfigChanged(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002136 mRecordThreads.removeItem(input);
2137 }
Eric Laurent83b88082014-06-20 18:31:16 -07002138 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2139 // we have a different lock for notification client
2140 closeInputFinish(thread);
2141 return NO_ERROR;
2142}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002143
Eric Laurent83b88082014-06-20 18:31:16 -07002144void AudioFlinger::closeInputFinish(sp<RecordThread> thread)
2145{
2146 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002147 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002148 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002149 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002150 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002151 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002152}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002153
Eric Laurent83b88082014-06-20 18:31:16 -07002154void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
2155{
2156 mRecordThreads.removeItem(thread->mId);
2157 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002158}
2159
Glenn Kastend2304db2014-02-03 07:40:31 -08002160status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002161{
2162 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002163 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002164
2165 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2166 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002167 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002168 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002169
2170 return NO_ERROR;
2171}
2172
2173
Eric Laurentde3f8392014-07-27 18:38:22 -07002174audio_unique_id_t AudioFlinger::newAudioUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002175{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002176 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002177}
2178
Marco Nelissend457c972014-02-11 08:47:07 -08002179void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002180{
2181 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002182 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002183 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2184 if (pid != -1 && (caller == getpid_cached)) {
2185 caller = pid;
2186 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002187
Eric Laurent021cf962014-05-13 10:18:14 -07002188 {
2189 Mutex::Autolock _cl(mClientLock);
2190 // Ignore requests received from processes not known as notification client. The request
2191 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2192 // called from a different pid leaving a stale session reference. Also we don't know how
2193 // to clear this reference if the client process dies.
2194 if (mNotificationClients.indexOfKey(caller) < 0) {
2195 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2196 return;
2197 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002198 }
2199
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002200 size_t num = mAudioSessionRefs.size();
2201 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002202 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002203 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2204 ref->mCnt++;
2205 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002206 return;
2207 }
2208 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002209 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2210 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002211}
2212
Marco Nelissend457c972014-02-11 08:47:07 -08002213void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002214{
2215 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002216 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002217 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2218 if (pid != -1 && (caller == getpid_cached)) {
2219 caller = pid;
2220 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002221 size_t num = mAudioSessionRefs.size();
2222 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002223 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002224 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2225 ref->mCnt--;
2226 ALOGV(" decremented refcount to %d", ref->mCnt);
2227 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002228 mAudioSessionRefs.removeAt(i);
2229 delete ref;
2230 purgeStaleEffects_l();
2231 }
2232 return;
2233 }
2234 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002235 // If the caller is mediaserver it is likely that the session being released was acquired
2236 // on behalf of a process not in notification clients and we ignore the warning.
2237 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002238}
2239
2240void AudioFlinger::purgeStaleEffects_l() {
2241
Steve Block3856b092011-10-20 11:56:00 +01002242 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002243
2244 Vector< sp<EffectChain> > chains;
2245
2246 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2247 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2248 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2249 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002250 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2251 chains.push(ec);
2252 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002253 }
2254 }
2255 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2256 sp<RecordThread> t = mRecordThreads.valueAt(i);
2257 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2258 sp<EffectChain> ec = t->mEffectChains[j];
2259 chains.push(ec);
2260 }
2261 }
2262
2263 for (size_t i = 0; i < chains.size(); i++) {
2264 sp<EffectChain> ec = chains[i];
2265 int sessionid = ec->sessionId();
2266 sp<ThreadBase> t = ec->mThread.promote();
2267 if (t == 0) {
2268 continue;
2269 }
2270 size_t numsessionrefs = mAudioSessionRefs.size();
2271 bool found = false;
2272 for (size_t k = 0; k < numsessionrefs; k++) {
2273 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002274 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002275 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002276 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002277 found = true;
2278 break;
2279 }
2280 }
2281 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002282 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002283 // remove all effects from the chain
2284 while (ec->mEffects.size()) {
2285 sp<EffectModule> effect = ec->mEffects[0];
2286 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002287 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002288 if (effect->purgeHandles()) {
2289 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002290 }
2291 AudioSystem::unregisterEffect(effect->id());
2292 }
2293 }
2294 }
2295 return;
2296}
2297
Mathias Agopian65ab4712010-07-14 17:59:35 -07002298// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002299AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002300{
Glenn Kastena1117922012-01-26 10:53:32 -08002301 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002302}
2303
2304// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002305AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002306{
2307 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002308 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002309}
2310
2311// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002312AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002313{
Glenn Kastena1117922012-01-26 10:53:32 -08002314 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002315}
2316
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002317uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002318{
Glenn Kastenbcefec32014-01-17 12:09:05 -08002319 return (uint32_t) android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002320}
2321
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002322AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002323{
2324 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2325 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002326 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002327 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002328 return thread;
2329 }
2330 }
2331 return NULL;
2332}
2333
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002334audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002335{
2336 PlaybackThread *thread = primaryPlaybackThread_l();
2337
2338 if (thread == NULL) {
2339 return 0;
2340 }
2341
Eric Laurentf1c04f92012-08-28 14:26:53 -07002342 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002343}
2344
Eric Laurenta011e352012-03-29 15:51:43 -07002345sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2346 int triggerSession,
2347 int listenerSession,
2348 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002349 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002350{
2351 Mutex::Autolock _l(mLock);
2352
2353 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2354 status_t playStatus = NAME_NOT_FOUND;
2355 status_t recStatus = NAME_NOT_FOUND;
2356 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2357 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2358 if (playStatus == NO_ERROR) {
2359 return event;
2360 }
2361 }
2362 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2363 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2364 if (recStatus == NO_ERROR) {
2365 return event;
2366 }
2367 }
2368 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2369 mPendingSyncEvents.add(event);
2370 } else {
2371 ALOGV("createSyncEvent() invalid event %d", event->type());
2372 event.clear();
2373 }
2374 return event;
2375}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002376
Mathias Agopian65ab4712010-07-14 17:59:35 -07002377// ----------------------------------------------------------------------------
2378// Effect management
2379// ----------------------------------------------------------------------------
2380
2381
Glenn Kastenf587ba52012-01-26 16:25:10 -08002382status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002383{
2384 Mutex::Autolock _l(mLock);
2385 return EffectQueryNumberEffects(numEffects);
2386}
2387
Glenn Kastenf587ba52012-01-26 16:25:10 -08002388status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002389{
2390 Mutex::Autolock _l(mLock);
2391 return EffectQueryEffect(index, descriptor);
2392}
2393
Glenn Kasten5e92a782012-01-30 07:40:52 -08002394status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002395 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002396{
2397 Mutex::Autolock _l(mLock);
2398 return EffectGetDescriptor(pUuid, descriptor);
2399}
2400
2401
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002402sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002403 effect_descriptor_t *pDesc,
2404 const sp<IEffectClient>& effectClient,
2405 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002406 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002407 int sessionId,
2408 status_t *status,
2409 int *id,
2410 int *enabled)
2411{
2412 status_t lStatus = NO_ERROR;
2413 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002414 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002415
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002416 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002417 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002418 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002419
2420 if (pDesc == NULL) {
2421 lStatus = BAD_VALUE;
2422 goto Exit;
2423 }
2424
Eric Laurent84e9a102010-09-23 16:10:16 -07002425 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002426 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002427 lStatus = PERMISSION_DENIED;
2428 goto Exit;
2429 }
2430
Dima Zavinfce7a472011-04-19 22:30:36 -07002431 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002432 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002433 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002434 lStatus = PERMISSION_DENIED;
2435 goto Exit;
2436 }
2437
Mathias Agopian65ab4712010-07-14 17:59:35 -07002438 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002439 if (!EffectIsNullUuid(&pDesc->uuid)) {
2440 // if uuid is specified, request effect descriptor
2441 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2442 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002443 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002444 goto Exit;
2445 }
2446 } else {
2447 // if uuid is not specified, look for an available implementation
2448 // of the required type in effect factory
2449 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002450 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002451 lStatus = BAD_VALUE;
2452 goto Exit;
2453 }
2454 uint32_t numEffects = 0;
2455 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002456 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002457 bool found = false;
2458
2459 lStatus = EffectQueryNumberEffects(&numEffects);
2460 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002461 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002462 goto Exit;
2463 }
2464 for (uint32_t i = 0; i < numEffects; i++) {
2465 lStatus = EffectQueryEffect(i, &desc);
2466 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002467 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002468 continue;
2469 }
2470 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2471 // If matching type found save effect descriptor. If the session is
2472 // 0 and the effect is not auxiliary, continue enumeration in case
2473 // an auxiliary version of this effect type is available
2474 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002475 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002476 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002477 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2478 break;
2479 }
2480 }
2481 }
2482 if (!found) {
2483 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002484 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002485 goto Exit;
2486 }
2487 // For same effect type, chose auxiliary version over insert version if
2488 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002489 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002490 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002491 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002492 }
2493 }
2494
2495 // Do not allow auxiliary effects on a session different from 0 (output mix)
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 lStatus = INVALID_OPERATION;
2499 goto Exit;
2500 }
2501
Eric Laurent59255e42011-07-27 19:49:51 -07002502 // check recording permission for visualizer
2503 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2504 !recordingAllowed()) {
2505 lStatus = PERMISSION_DENIED;
2506 goto Exit;
2507 }
2508
Mathias Agopian65ab4712010-07-14 17:59:35 -07002509 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002510 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002511 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002512 // if the output returned by getOutputForEffect() is removed before we lock the
2513 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2514 // and we will exit safely
2515 io = AudioSystem::getOutputForEffect(&desc);
2516 ALOGV("createEffect got output %d", io);
2517 }
2518
2519 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002520
2521 // If output is not specified try to find a matching audio session ID in one of the
2522 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002523 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2524 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002525 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002526 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002527 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2528 // output must be specified by AudioPolicyManager when using session
2529 // AUDIO_SESSION_OUTPUT_STAGE
2530 lStatus = BAD_VALUE;
2531 goto Exit;
2532 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002533 // look for the thread where the specified audio session is present
2534 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2535 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2536 io = mPlaybackThreads.keyAt(i);
2537 break;
2538 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002539 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002540 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002541 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2542 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2543 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002544 break;
2545 }
2546 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002547 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002548 // If no output thread contains the requested session ID, default to
2549 // first output. The effect chain will be moved to the correct output
2550 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002551 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002552 io = mPlaybackThreads.keyAt(0);
2553 }
Steve Block3856b092011-10-20 11:56:00 +01002554 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002555 }
2556 ThreadBase *thread = checkRecordThread_l(io);
2557 if (thread == NULL) {
2558 thread = checkPlaybackThread_l(io);
2559 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002560 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002561 lStatus = BAD_VALUE;
2562 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002563 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002564 } else {
2565 // Check if one effect chain was awaiting for an effect to be created on this
2566 // session and used it instead of creating a new one.
2567 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)sessionId);
2568 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002569 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002570 thread->addEffectChain_l(chain);
2571 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002572 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002573
Eric Laurent021cf962014-05-13 10:18:14 -07002574 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002575
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002576 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002577 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2578 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002579 if (handle != 0 && id != NULL) {
2580 *id = handle->id();
2581 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002582 if (handle == 0) {
2583 // remove local strong reference to Client with mClientLock held
2584 Mutex::Autolock _cl(mClientLock);
2585 client.clear();
2586 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002587 }
2588
2589Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002590 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002591 return handle;
2592}
2593
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002594status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2595 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002596{
Steve Block3856b092011-10-20 11:56:00 +01002597 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002598 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002599 Mutex::Autolock _l(mLock);
2600 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002601 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002602 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002603 }
Eric Laurentde070132010-07-13 04:45:46 -07002604 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2605 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002606 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002607 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002608 }
Eric Laurentde070132010-07-13 04:45:46 -07002609 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2610 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002611 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002612 return BAD_VALUE;
2613 }
2614
2615 Mutex::Autolock _dl(dstThread->mLock);
2616 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002617 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002618}
2619
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002620// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002621status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002622 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002623 AudioFlinger::PlaybackThread *dstThread,
2624 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002625{
Steve Block3856b092011-10-20 11:56:00 +01002626 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002627 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002628
Eric Laurent59255e42011-07-27 19:49:51 -07002629 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002630 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002631 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002632 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002633 return INVALID_OPERATION;
2634 }
2635
Andy Hung9a592762014-07-21 21:56:01 -07002636 // Check whether the destination thread has a channel count of FCC_2, which is
2637 // currently required for (most) effects. Prevent moving the effect chain here rather
2638 // than disabling the addEffect_l() call in dstThread below.
Eric Laurentb10352f2014-11-03 16:25:39 -08002639 if ((dstThread->type() == ThreadBase::MIXER || dstThread->type() == ThreadBase::DUPLICATING) &&
2640 dstThread->mChannelCount != FCC_2) {
Andy Hung9a592762014-07-21 21:56:01 -07002641 ALOGW("moveEffectChain_l() effect chain failed because"
2642 " destination thread %p channel count(%u) != %u",
2643 dstThread, dstThread->mChannelCount, FCC_2);
2644 return INVALID_OPERATION;
2645 }
2646
Eric Laurent39e94f82010-07-28 01:32:47 -07002647 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002648 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002649 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002650 // removed.
2651 srcThread->removeEffectChain_l(chain);
2652
2653 // transfer all effects one by one so that new effect chain is created on new thread with
2654 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002655 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002656 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002657 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002658 Vector< sp<EffectModule> > removed;
2659 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002660 while (effect != 0) {
2661 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002662 removed.add(effect);
2663 status = dstThread->addEffect_l(effect);
2664 if (status != NO_ERROR) {
2665 break;
2666 }
Eric Laurentec35a142011-10-05 17:42:25 -07002667 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2668 if (effect->state() == EffectModule::ACTIVE ||
2669 effect->state() == EffectModule::STOPPING) {
2670 effect->start();
2671 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002672 // if the move request is not received from audio policy manager, the effect must be
2673 // re-registered with the new strategy and output
2674 if (dstChain == 0) {
2675 dstChain = effect->chain().promote();
2676 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002677 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002678 status = NO_INIT;
2679 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002680 }
2681 strategy = dstChain->strategy();
2682 }
2683 if (reRegister) {
2684 AudioSystem::unregisterEffect(effect->id());
2685 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002686 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002687 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002688 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002689 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002690 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002691 }
Eric Laurentde070132010-07-13 04:45:46 -07002692 effect = chain->getEffectFromId_l(0);
2693 }
2694
Eric Laurent5baf2af2013-09-12 17:37:00 -07002695 if (status != NO_ERROR) {
2696 for (size_t i = 0; i < removed.size(); i++) {
2697 srcThread->addEffect_l(removed[i]);
2698 if (dstChain != 0 && reRegister) {
2699 AudioSystem::unregisterEffect(removed[i]->id());
2700 AudioSystem::registerEffect(&removed[i]->desc(),
2701 srcThread->id(),
2702 strategy,
2703 sessionId,
2704 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002705 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002706 }
2707 }
2708 }
2709
2710 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002711}
2712
Eric Laurent5baf2af2013-09-12 17:37:00 -07002713bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002714{
2715 if (mGlobalEffectEnableTime != 0 &&
2716 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2717 return true;
2718 }
2719
2720 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2721 sp<EffectChain> ec =
2722 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002723 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002724 return true;
2725 }
2726 }
2727 return false;
2728}
2729
Eric Laurent5baf2af2013-09-12 17:37:00 -07002730void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002731{
2732 Mutex::Autolock _l(mLock);
2733
2734 mGlobalEffectEnableTime = systemTime();
2735
2736 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2737 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2738 if (t->mType == ThreadBase::OFFLOAD) {
2739 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2740 }
2741 }
2742
2743}
2744
Eric Laurentaaa44472014-09-12 17:41:50 -07002745status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2746{
2747 audio_session_t session = (audio_session_t)chain->sessionId();
2748 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2749 ALOGV("putOrphanEffectChain_l session %d index %d", session, index);
2750 if (index >= 0) {
2751 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2752 return ALREADY_EXISTS;
2753 }
2754 mOrphanEffectChains.add(session, chain);
2755 return NO_ERROR;
2756}
2757
2758sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2759{
2760 sp<EffectChain> chain;
2761 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2762 ALOGV("getOrphanEffectChain_l session %d index %d", session, index);
2763 if (index >= 0) {
2764 chain = mOrphanEffectChains.valueAt(index);
2765 mOrphanEffectChains.removeItemsAt(index);
2766 }
2767 return chain;
2768}
2769
2770bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2771{
2772 Mutex::Autolock _l(mLock);
2773 audio_session_t session = (audio_session_t)effect->sessionId();
2774 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2775 ALOGV("updateOrphanEffectChains session %d index %d", session, index);
2776 if (index >= 0) {
2777 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2778 if (chain->removeEffect_l(effect) == 0) {
2779 ALOGV("updateOrphanEffectChains removing effect chain at index %d", index);
2780 mOrphanEffectChains.removeItemsAt(index);
2781 }
2782 return true;
2783 }
2784 return false;
2785}
2786
2787
Glenn Kastenda6ef132013-01-10 12:31:01 -08002788struct Entry {
2789#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2790 char mName[MAX_NAME];
2791};
2792
2793int comparEntry(const void *p1, const void *p2)
2794{
2795 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2796}
2797
Glenn Kasten46909e72013-02-26 09:20:22 -08002798#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002799void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002800{
Eric Laurent81784c32012-11-19 14:55:58 -08002801 NBAIO_Source *teeSource = source.get();
2802 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002803 // .wav rotation
2804 // There is a benign race condition if 2 threads call this simultaneously.
2805 // They would both traverse the directory, but the result would simply be
2806 // failures at unlink() which are ignored. It's also unlikely since
2807 // normally dumpsys is only done by bugreport or from the command line.
2808 char teePath[32+256];
2809 strcpy(teePath, "/data/misc/media");
2810 size_t teePathLen = strlen(teePath);
2811 DIR *dir = opendir(teePath);
2812 teePath[teePathLen++] = '/';
2813 if (dir != NULL) {
2814#define MAX_SORT 20 // number of entries to sort
2815#define MAX_KEEP 10 // number of entries to keep
2816 struct Entry entries[MAX_SORT];
2817 size_t entryCount = 0;
2818 while (entryCount < MAX_SORT) {
2819 struct dirent de;
2820 struct dirent *result = NULL;
2821 int rc = readdir_r(dir, &de, &result);
2822 if (rc != 0) {
2823 ALOGW("readdir_r failed %d", rc);
2824 break;
2825 }
2826 if (result == NULL) {
2827 break;
2828 }
2829 if (result != &de) {
2830 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2831 break;
2832 }
2833 // ignore non .wav file entries
2834 size_t nameLen = strlen(de.d_name);
2835 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2836 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2837 continue;
2838 }
2839 strcpy(entries[entryCount++].mName, de.d_name);
2840 }
2841 (void) closedir(dir);
2842 if (entryCount > MAX_KEEP) {
2843 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2844 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2845 strcpy(&teePath[teePathLen], entries[i].mName);
2846 (void) unlink(teePath);
2847 }
2848 }
2849 } else {
2850 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002851 dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002852 }
2853 }
Eric Laurent81784c32012-11-19 14:55:58 -08002854 char teeTime[16];
2855 struct timeval tv;
2856 gettimeofday(&tv, NULL);
2857 struct tm tm;
2858 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002859 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2860 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2861 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2862 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002863 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07002864 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08002865 char wavHeader[44];
2866 memcpy(wavHeader,
2867 "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",
2868 sizeof(wavHeader));
2869 NBAIO_Format format = teeSource->format();
2870 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002871 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07002872 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002873 wavHeader[22] = channelCount; // number of channels
2874 wavHeader[24] = sampleRate; // sample rate
2875 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07002876 wavHeader[32] = frameSize; // block alignment
2877 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08002878 write(teeFd, wavHeader, sizeof(wavHeader));
2879 size_t total = 0;
2880 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07002881#define TEE_SINK_READ 1024 // frames per I/O operation
2882 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002883 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08002884 size_t count = TEE_SINK_READ;
2885 ssize_t actual = teeSource->read(buffer, count,
2886 AudioBufferProvider::kInvalidPTS);
2887 bool wasFirstRead = firstRead;
2888 firstRead = false;
2889 if (actual <= 0) {
2890 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2891 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002892 }
Eric Laurent81784c32012-11-19 14:55:58 -08002893 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002894 }
Eric Laurent81784c32012-11-19 14:55:58 -08002895 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07002896 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002897 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002898 }
Glenn Kasten329f6512014-08-28 16:23:16 -07002899 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08002900 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002901 uint32_t temp = 44 + total * frameSize - 8;
2902 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002903 write(teeFd, &temp, sizeof(temp));
2904 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002905 temp = total * frameSize;
2906 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002907 write(teeFd, &temp, sizeof(temp));
2908 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002909 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002910 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002911 }
Eric Laurent59255e42011-07-27 19:49:51 -07002912 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002913 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002914 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002915 }
Eric Laurent59255e42011-07-27 19:49:51 -07002916 }
2917 }
2918}
Glenn Kasten46909e72013-02-26 09:20:22 -08002919#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002920
Mathias Agopian65ab4712010-07-14 17:59:35 -07002921// ----------------------------------------------------------------------------
2922
2923status_t AudioFlinger::onTransact(
2924 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2925{
2926 return BnAudioFlinger::onTransact(code, data, reply, flags);
2927}
2928
Mathias Agopian65ab4712010-07-14 17:59:35 -07002929}; // namespace android