blob: e48af205a9a79ef28fe899161bd33ca87f224675 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
Mathias Agopian65ab4712010-07-14 17:59:35 -07002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
Glenn Kasten153b9fe2013-07-15 11:23:36 -070022#include "Configuration.h"
Glenn Kastenda6ef132013-01-10 12:31:01 -080023#include <dirent.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070024#include <math.h>
25#include <signal.h>
26#include <sys/time.h>
27#include <sys/resource.h>
28
Gloria Wang9ee159b2011-02-24 14:51:45 -080029#include <binder/IPCThreadState.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070030#include <binder/IServiceManager.h>
31#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070032#include <utils/Trace.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070033#include <binder/Parcel.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/String16.h>
35#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070036#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070037
Dima Zavinfce7a472011-04-19 22:30:36 -070038#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070039#include <cutils/properties.h>
40
Dima Zavin64760242011-05-11 14:15:23 -070041#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070042#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070043
44#include "AudioMixer.h"
45#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080046#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070047
Mathias Agopian65ab4712010-07-14 17:59:35 -070048#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070049#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070050#include <audio_effects/effect_ns.h>
51#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Glenn Kasten3b21c502011-12-15 09:52:39 -080053#include <audio_utils/primitives.h>
54
Eric Laurentfeb0db62011-07-22 09:04:31 -070055#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080056
John Grossman4ff14ba2012-02-08 16:37:41 -080057#include <common_time/cc_helper.h>
Glenn Kasten58912562012-04-03 10:45:00 -070058
Glenn Kasten9e58b552013-01-18 15:09:48 -080059#include <media/IMediaLogService.h>
60
Glenn Kastenda6ef132013-01-10 12:31:01 -080061#include <media/nbaio/Pipe.h>
62#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070063#include <media/AudioParameter.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070064#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080065
Mathias Agopian65ab4712010-07-14 17:59:35 -070066// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070067
John Grossman1c345192012-03-27 14:00:17 -070068// Note: the following macro is used for extremely verbose logging message. In
69// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
70// 0; but one side effect of this is to turn all LOGV's as well. Some messages
71// are so verbose that we want to suppress them even when we have ALOG_ASSERT
72// turned on. Do not uncomment the #def below unless you really know what you
73// are doing and want to see all of the extremely verbose messages.
74//#define VERY_VERY_VERBOSE_LOGGING
75#ifdef VERY_VERY_VERBOSE_LOGGING
76#define ALOGVV ALOGV
77#else
78#define ALOGVV(a...) do { } while(0)
79#endif
Eric Laurentde070132010-07-13 04:45:46 -070080
Mathias Agopian65ab4712010-07-14 17:59:35 -070081namespace android {
82
Glenn Kastenec1d6b52011-12-12 09:04:45 -080083static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
84static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Eric Laurent021cf962014-05-13 10:18:14 -070085static const char kClientLockedString[] = "Client lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070086
Glenn Kasten58912562012-04-03 10:45:00 -070087
John Grossman4ff14ba2012-02-08 16:37:41 -080088nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080089
Eric Laurent81784c32012-11-19 14:55:58 -080090uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070091
Glenn Kasten46909e72013-02-26 09:20:22 -080092#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080093bool AudioFlinger::mTeeSinkInputEnabled = false;
94bool AudioFlinger::mTeeSinkOutputEnabled = false;
95bool AudioFlinger::mTeeSinkTrackEnabled = false;
96
97size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
98size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
99size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800100#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800101
Eric Laurent813e2a72013-08-31 12:59:48 -0700102// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
103// we define a minimum time during which a global effect is considered enabled.
104static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
105
Mathias Agopian65ab4712010-07-14 17:59:35 -0700106// ----------------------------------------------------------------------------
107
Marco Nelissenb2208842014-02-07 14:00:50 -0800108const char *formatToString(audio_format_t format) {
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530109 switch (format & AUDIO_FORMAT_MAIN_MASK) {
110 case AUDIO_FORMAT_PCM:
111 switch (format) {
112 case AUDIO_FORMAT_PCM_16_BIT: return "pcm16";
113 case AUDIO_FORMAT_PCM_8_BIT: return "pcm8";
114 case AUDIO_FORMAT_PCM_32_BIT: return "pcm32";
115 case AUDIO_FORMAT_PCM_8_24_BIT: return "pcm8.24";
116 case AUDIO_FORMAT_PCM_FLOAT: return "pcmfloat";
117 case AUDIO_FORMAT_PCM_24_BIT_PACKED: return "pcm24";
118 default:
119 break;
120 }
121 break;
Marco Nelissenb2208842014-02-07 14:00:50 -0800122 case AUDIO_FORMAT_MP3: return "mp3";
123 case AUDIO_FORMAT_AMR_NB: return "amr-nb";
124 case AUDIO_FORMAT_AMR_WB: return "amr-wb";
125 case AUDIO_FORMAT_AAC: return "aac";
126 case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
127 case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
128 case AUDIO_FORMAT_VORBIS: return "vorbis";
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530129 case AUDIO_FORMAT_OPUS: return "opus";
130 case AUDIO_FORMAT_AC3: return "ac-3";
131 case AUDIO_FORMAT_E_AC3: return "e-ac-3";
Marco Nelissenb2208842014-02-07 14:00:50 -0800132 default:
133 break;
134 }
135 return "unknown";
136}
137
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700138static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700139{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700140 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700141 int rc;
142
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700143 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
144 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
145 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
146 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700147 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700148 }
149 rc = audio_hw_device_open(mod, dev);
150 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
151 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
152 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700153 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700154 }
Eric Laurent5806b352014-05-22 12:23:26 -0700155 if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700156 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
157 rc = BAD_VALUE;
158 goto out;
159 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700160 return 0;
161
162out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700163 *dev = NULL;
164 return rc;
165}
166
Mathias Agopian65ab4712010-07-14 17:59:35 -0700167// ----------------------------------------------------------------------------
168
169AudioFlinger::AudioFlinger()
170 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800171 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800172 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700173 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800174 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800175 mMasterMute(false),
176 mNextUniqueId(1),
177 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700178 mBtNrecIsOff(false),
179 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700180 mIsDeviceTypeKnown(false),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700181 mGlobalEffectEnableTime(0),
182 mPrimaryOutputSampleRate(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700183{
Glenn Kasten949a9262013-04-16 12:35:20 -0700184 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800185 char value[PROPERTY_VALUE_MAX];
186 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
187 if (doLog) {
Glenn Kastencf515ab2014-03-14 16:01:58 -0700188 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters", MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800189 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700190
Glenn Kasten46909e72013-02-26 09:20:22 -0800191#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800192 (void) property_get("ro.debuggable", value, "0");
193 int debuggable = atoi(value);
194 int teeEnabled = 0;
195 if (debuggable) {
196 (void) property_get("af.tee", value, "0");
197 teeEnabled = atoi(value);
198 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800199 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700200 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800201 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700202 }
203 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800204 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700205 }
206 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800207 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700208 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800209#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700210}
211
212void AudioFlinger::onFirstRef()
213{
Dima Zavin799a70e2011-04-18 16:57:27 -0700214 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700215
Eric Laurent93575202011-01-18 18:39:02 -0800216 Mutex::Autolock _l(mLock);
217
Dima Zavin799a70e2011-04-18 16:57:27 -0700218 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800219 char val_str[PROPERTY_VALUE_MAX] = { 0 };
220 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
221 uint32_t int_val;
222 if (1 == sscanf(val_str, "%u", &int_val)) {
223 mStandbyTimeInNsecs = milliseconds(int_val);
224 ALOGI("Using %u mSec as standby time.", int_val);
225 } else {
226 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
227 ALOGI("Using default %u mSec as standby time.",
228 (uint32_t)(mStandbyTimeInNsecs / 1000000));
229 }
230 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700231
Eric Laurent1c333e22014-05-20 10:48:17 -0700232 mPatchPanel = new PatchPanel(this);
233
Eric Laurenta4c5a552012-03-29 10:12:40 -0700234 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700235}
236
237AudioFlinger::~AudioFlinger()
238{
239 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700240 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700241 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242 }
243 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700244 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700245 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700246 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700247
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800248 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
249 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700250 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
251 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700252 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700253
254 // Tell media.log service about any old writers that still need to be unregistered
255 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
256 if (binder != 0) {
257 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
258 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
259 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
260 mUnregisteredWriters.pop();
261 mediaLogService->unregisterWriter(iMemory);
262 }
263 }
264
Mathias Agopian65ab4712010-07-14 17:59:35 -0700265}
266
Eric Laurenta4c5a552012-03-29 10:12:40 -0700267static const char * const audio_interfaces[] = {
268 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
269 AUDIO_HARDWARE_MODULE_ID_A2DP,
270 AUDIO_HARDWARE_MODULE_ID_USB,
271};
272#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
273
John Grossmanee578c02012-07-23 17:05:46 -0700274AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
275 audio_module_handle_t module,
276 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700277{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700278 // if module is 0, the request comes from an old policy manager and we should load
279 // well known modules
280 if (module == 0) {
281 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
282 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
283 loadHwModule_l(audio_interfaces[i]);
284 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700285 // then try to find a module supporting the requested device.
286 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
287 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
288 audio_hw_device_t *dev = audioHwDevice->hwDevice();
289 if ((dev->get_supported_devices != NULL) &&
290 (dev->get_supported_devices(dev) & devices) == devices)
291 return audioHwDevice;
292 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700293 } else {
294 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700295 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
296 if (audioHwDevice != NULL) {
297 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700298 }
299 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700300
Dima Zavin799a70e2011-04-18 16:57:27 -0700301 return NULL;
302}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700303
Glenn Kasten0f11b512014-01-31 16:18:54 -0800304void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305{
306 const size_t SIZE = 256;
307 char buffer[SIZE];
308 String8 result;
309
310 result.append("Clients:\n");
311 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800312 sp<Client> client = mClients.valueAt(i).promote();
313 if (client != 0) {
314 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
315 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316 }
317 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700318
Eric Laurentd1b28d42013-09-18 18:47:13 -0700319 result.append("Notification Clients:\n");
320 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
321 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
322 result.append(buffer);
323 }
324
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700325 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800326 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700327 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
328 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800329 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700330 result.append(buffer);
331 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700332 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700333}
334
335
Glenn Kasten0f11b512014-01-31 16:18:54 -0800336void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700337{
338 const size_t SIZE = 256;
339 char buffer[SIZE];
340 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800341 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700342
John Grossman4ff14ba2012-02-08 16:37:41 -0800343 snprintf(buffer, SIZE, "Hardware status: %d\n"
344 "Standby Time mSec: %u\n",
345 hardwareStatus,
346 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700347 result.append(buffer);
348 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349}
350
Glenn Kasten0f11b512014-01-31 16:18:54 -0800351void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700352{
353 const size_t SIZE = 256;
354 char buffer[SIZE];
355 String8 result;
356 snprintf(buffer, SIZE, "Permission Denial: "
357 "can't dump AudioFlinger from pid=%d, uid=%d\n",
358 IPCThreadState::self()->getCallingPid(),
359 IPCThreadState::self()->getCallingUid());
360 result.append(buffer);
361 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362}
363
Eric Laurent81784c32012-11-19 14:55:58 -0800364bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365{
366 bool locked = false;
367 for (int i = 0; i < kDumpLockRetries; ++i) {
368 if (mutex.tryLock() == NO_ERROR) {
369 locked = true;
370 break;
371 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800372 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373 }
374 return locked;
375}
376
377status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
378{
Glenn Kasten44deb052012-02-05 18:09:08 -0800379 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700380 dumpPermissionDenial(fd, args);
381 } else {
382 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800383 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384 if (!hardwareLocked) {
385 String8 result(kHardwareLockedString);
386 write(fd, result.string(), result.size());
387 } else {
388 mHardwareLock.unlock();
389 }
390
Eric Laurent81784c32012-11-19 14:55:58 -0800391 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392
393 // failed to lock - AudioFlinger is probably deadlocked
394 if (!locked) {
395 String8 result(kDeadlockedString);
396 write(fd, result.string(), result.size());
397 }
398
Eric Laurent021cf962014-05-13 10:18:14 -0700399 bool clientLocked = dumpTryLock(mClientLock);
400 if (!clientLocked) {
401 String8 result(kClientLockedString);
402 write(fd, result.string(), result.size());
403 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700404 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700405 if (clientLocked) {
406 mClientLock.unlock();
407 }
408
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409 dumpInternals(fd, args);
410
411 // dump playback threads
412 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
413 mPlaybackThreads.valueAt(i)->dump(fd, args);
414 }
415
416 // dump record threads
417 for (size_t i = 0; i < mRecordThreads.size(); i++) {
418 mRecordThreads.valueAt(i)->dump(fd, args);
419 }
420
Eric Laurentaaa44472014-09-12 17:41:50 -0700421 // dump orphan effect chains
422 if (mOrphanEffectChains.size() != 0) {
423 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
424 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
425 mOrphanEffectChains.valueAt(i)->dump(fd, args);
426 }
427 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700428 // dump all hardware devs
429 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700430 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700431 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700432 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700433
Glenn Kasten46909e72013-02-26 09:20:22 -0800434#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700435 // dump the serially shared record tee sink
436 if (mRecordTeeSource != 0) {
437 dumpTee(fd, mRecordTeeSource);
438 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800439#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700440
Glenn Kastend65d73c2012-06-22 17:21:07 -0700441 if (locked) {
442 mLock.unlock();
443 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800444
445 // append a copy of media.log here by forwarding fd to it, but don't attempt
446 // to lookup the service if it's not running, as it will block for a second
447 if (mLogMemoryDealer != 0) {
448 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
449 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700450 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800451 Vector<String16> args;
452 binder->dump(fd, args);
453 }
454 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700455 }
456 return NO_ERROR;
457}
458
Eric Laurent021cf962014-05-13 10:18:14 -0700459sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800460{
Eric Laurent021cf962014-05-13 10:18:14 -0700461 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800462 // If pid is already in the mClients wp<> map, then use that entry
463 // (for which promote() is always != 0), otherwise create a new entry and Client.
464 sp<Client> client = mClients.valueFor(pid).promote();
465 if (client == 0) {
466 client = new Client(this, pid);
467 mClients.add(pid, client);
468 }
469
470 return client;
471}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700472
Glenn Kasten9e58b552013-01-18 15:09:48 -0800473sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
474{
Glenn Kasten481fb672013-09-30 14:39:28 -0700475 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800476 if (mLogMemoryDealer == 0) {
477 return new NBLog::Writer();
478 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800479 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700480 // Similarly if we can't contact the media.log service, also return a dummy writer
481 if (binder == 0) {
482 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800483 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700484 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
485 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
486 // If allocation fails, consult the vector of previously unregistered writers
487 // and garbage-collect one or more them until an allocation succeeds
488 if (shared == 0) {
489 Mutex::Autolock _l(mUnregisteredWritersLock);
490 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
491 {
492 // Pick the oldest stale writer to garbage-collect
493 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
494 mUnregisteredWriters.removeAt(0);
495 mediaLogService->unregisterWriter(iMemory);
496 // Now the media.log remote reference to IMemory is gone. When our last local
497 // reference to IMemory also drops to zero at end of this block,
498 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
499 }
500 // Re-attempt the allocation
501 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
502 if (shared != 0) {
503 goto success;
504 }
505 }
506 // Even after garbage-collecting all old writers, there is still not enough memory,
507 // so return a dummy writer
508 return new NBLog::Writer();
509 }
510success:
511 mediaLogService->registerWriter(shared, size, name);
512 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800513}
514
515void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
516{
Glenn Kasten685ef092013-02-04 08:15:34 -0800517 if (writer == 0) {
518 return;
519 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800520 sp<IMemory> iMemory(writer->getIMemory());
521 if (iMemory == 0) {
522 return;
523 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700524 // Rather than removing the writer immediately, append it to a queue of old writers to
525 // be garbage-collected later. This allows us to continue to view old logs for a while.
526 Mutex::Autolock _l(mUnregisteredWritersLock);
527 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800528}
529
Mathias Agopian65ab4712010-07-14 17:59:35 -0700530// IAudioFlinger interface
531
532
533sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800534 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700535 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800536 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700537 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800538 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800539 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700540 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800541 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800542 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543 int *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800544 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700545 status_t *status)
546{
547 sp<PlaybackThread::Track> track;
548 sp<TrackHandle> trackHandle;
549 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550 status_t lStatus;
551 int lSessionId;
552
Glenn Kasten263709e2012-01-06 08:40:01 -0800553 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
554 // but if someone uses binder directly they could bypass that and cause us to crash
555 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000556 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700557 lStatus = BAD_VALUE;
558 goto Exit;
559 }
560
Glenn Kasten53b5d092014-02-05 10:00:23 -0800561 // further sample rate checks are performed by createTrack_l() depending on the thread type
562 if (sampleRate == 0) {
563 ALOGE("createTrack() invalid sample rate %u", sampleRate);
564 lStatus = BAD_VALUE;
565 goto Exit;
566 }
567
568 // further channel mask checks are performed by createTrack_l() depending on the thread type
569 if (!audio_is_output_channel(channelMask)) {
570 ALOGE("createTrack() invalid channel mask %#x", channelMask);
571 lStatus = BAD_VALUE;
572 goto Exit;
573 }
574
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700575 // further format checks are performed by createTrack_l() depending on the thread type
576 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800577 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700578 lStatus = BAD_VALUE;
579 goto Exit;
580 }
581
Glenn Kasten663c2242013-09-24 11:52:37 -0700582 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
583 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
584 lStatus = BAD_VALUE;
585 goto Exit;
586 }
587
Mathias Agopian65ab4712010-07-14 17:59:35 -0700588 {
589 Mutex::Autolock _l(mLock);
590 PlaybackThread *thread = checkPlaybackThread_l(output);
591 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800592 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593 lStatus = BAD_VALUE;
594 goto Exit;
595 }
596
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800597 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -0700598 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700599
Glenn Kastene848bd92014-03-13 15:00:32 -0700600 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700601 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700602 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700603 // check if an effect chain with the same session ID is present on another
604 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700605 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700606 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
607 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700608 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700609 if (sessions & PlaybackThread::EFFECT_SESSION) {
610 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700611 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700612 }
Eric Laurentde070132010-07-13 04:45:46 -0700613 }
614 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700615 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700616 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700617 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700618 if (sessionId != NULL) {
619 *sessionId = lSessionId;
620 }
621 }
Steve Block3856b092011-10-20 11:56:00 +0100622 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700623
624 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800625 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800626 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700627 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700628
629 // move effect chain to this output thread if an effect on same session was waiting
630 // for a track to be created
631 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700632 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700633 Mutex::Autolock _dl(thread->mLock);
634 Mutex::Autolock _sl(effectThread->mLock);
635 moveEffectChain_l(lSessionId, effectThread, thread, true);
636 }
Eric Laurenta011e352012-03-29 15:51:43 -0700637
638 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700639 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700640 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
641 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700642 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700643 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700644 } else {
645 mPendingSyncEvents[i]->cancel();
646 }
Eric Laurenta011e352012-03-29 15:51:43 -0700647 mPendingSyncEvents.removeAt(i);
648 i--;
649 }
650 }
651 }
Glenn Kastene198c362013-08-13 09:13:36 -0700652
Mathias Agopian65ab4712010-07-14 17:59:35 -0700653 }
Glenn Kastene198c362013-08-13 09:13:36 -0700654
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700655 if (lStatus != NO_ERROR) {
656 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700657 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700658 // Don't hold mClientLock when releasing the reference on the track as the
659 // destructor will acquire it.
660 {
661 Mutex::Autolock _cl(mClientLock);
662 client.clear();
663 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700664 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700665 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666 }
667
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700668 // return handle to client
669 trackHandle = new TrackHandle(track);
670
Mathias Agopian65ab4712010-07-14 17:59:35 -0700671Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700672 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700673 return trackHandle;
674}
675
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800676uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700677{
678 Mutex::Autolock _l(mLock);
679 PlaybackThread *thread = checkPlaybackThread_l(output);
680 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000681 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700682 return 0;
683 }
684 return thread->sampleRate();
685}
686
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800687audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700688{
689 Mutex::Autolock _l(mLock);
690 PlaybackThread *thread = checkPlaybackThread_l(output);
691 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000692 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800693 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694 }
695 return thread->format();
696}
697
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800698size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699{
700 Mutex::Autolock _l(mLock);
701 PlaybackThread *thread = checkPlaybackThread_l(output);
702 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000703 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704 return 0;
705 }
Glenn Kasten58912562012-04-03 10:45:00 -0700706 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
707 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700708 return thread->frameCount();
709}
710
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800711uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712{
713 Mutex::Autolock _l(mLock);
714 PlaybackThread *thread = checkPlaybackThread_l(output);
715 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800716 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700717 return 0;
718 }
719 return thread->latency();
720}
721
722status_t AudioFlinger::setMasterVolume(float value)
723{
Eric Laurenta1884f92011-08-23 08:25:03 -0700724 status_t ret = initCheck();
725 if (ret != NO_ERROR) {
726 return ret;
727 }
728
Mathias Agopian65ab4712010-07-14 17:59:35 -0700729 // check calling permissions
730 if (!settingsAllowed()) {
731 return PERMISSION_DENIED;
732 }
733
Eric Laurenta4c5a552012-03-29 10:12:40 -0700734 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700735 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700736
John Grossmanee578c02012-07-23 17:05:46 -0700737 // Set master volume in the HALs which support it.
738 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
739 AutoMutex lock(mHardwareLock);
740 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800741
John Grossmanee578c02012-07-23 17:05:46 -0700742 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
743 if (dev->canSetMasterVolume()) {
744 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800745 }
John Grossmanee578c02012-07-23 17:05:46 -0700746 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700747 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700748
John Grossmanee578c02012-07-23 17:05:46 -0700749 // Now set the master volume in each playback thread. Playback threads
750 // assigned to HALs which do not have master volume support will apply
751 // master volume during the mix operation. Threads with HALs which do
752 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800753 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700754 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700755
756 return NO_ERROR;
757}
758
Glenn Kastenf78aee72012-01-04 11:00:47 -0800759status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700760{
Eric Laurenta1884f92011-08-23 08:25:03 -0700761 status_t ret = initCheck();
762 if (ret != NO_ERROR) {
763 return ret;
764 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700765
766 // check calling permissions
767 if (!settingsAllowed()) {
768 return PERMISSION_DENIED;
769 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800770 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000771 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700772 return BAD_VALUE;
773 }
774
775 { // scope for the lock
776 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700777 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700778 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700779 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700780 mHardwareStatus = AUDIO_HW_IDLE;
781 }
782
783 if (NO_ERROR == ret) {
784 Mutex::Autolock _l(mLock);
785 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800786 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700787 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700788 }
789
790 return ret;
791}
792
793status_t AudioFlinger::setMicMute(bool state)
794{
Eric Laurenta1884f92011-08-23 08:25:03 -0700795 status_t ret = initCheck();
796 if (ret != NO_ERROR) {
797 return ret;
798 }
799
Mathias Agopian65ab4712010-07-14 17:59:35 -0700800 // check calling permissions
801 if (!settingsAllowed()) {
802 return PERMISSION_DENIED;
803 }
804
805 AutoMutex lock(mHardwareLock);
806 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700807 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
808 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
809 status_t result = dev->set_mic_mute(dev, state);
810 if (result != NO_ERROR) {
811 ret = result;
812 }
813 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814 mHardwareStatus = AUDIO_HW_IDLE;
815 return ret;
816}
817
818bool AudioFlinger::getMicMute() const
819{
Eric Laurenta1884f92011-08-23 08:25:03 -0700820 status_t ret = initCheck();
821 if (ret != NO_ERROR) {
822 return false;
823 }
824
Dima Zavinfce7a472011-04-19 22:30:36 -0700825 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800826 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700827 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700828 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700829 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700830 mHardwareStatus = AUDIO_HW_IDLE;
831 return state;
832}
833
834status_t AudioFlinger::setMasterMute(bool muted)
835{
John Grossmand8f178d2012-07-20 14:51:35 -0700836 status_t ret = initCheck();
837 if (ret != NO_ERROR) {
838 return ret;
839 }
840
Mathias Agopian65ab4712010-07-14 17:59:35 -0700841 // check calling permissions
842 if (!settingsAllowed()) {
843 return PERMISSION_DENIED;
844 }
845
John Grossmanee578c02012-07-23 17:05:46 -0700846 Mutex::Autolock _l(mLock);
847 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700848
John Grossmanee578c02012-07-23 17:05:46 -0700849 // Set master mute in the HALs which support it.
850 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
851 AutoMutex lock(mHardwareLock);
852 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700853
John Grossmanee578c02012-07-23 17:05:46 -0700854 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
855 if (dev->canSetMasterMute()) {
856 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700857 }
John Grossmanee578c02012-07-23 17:05:46 -0700858 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700859 }
860
John Grossmanee578c02012-07-23 17:05:46 -0700861 // Now set the master mute in each playback thread. Playback threads
862 // assigned to HALs which do not have master mute support will apply master
863 // mute during the mix operation. Threads with HALs which do support master
864 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800865 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700866 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700867
868 return NO_ERROR;
869}
870
871float AudioFlinger::masterVolume() const
872{
Glenn Kasten98067102011-12-13 11:47:54 -0800873 Mutex::Autolock _l(mLock);
874 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700875}
876
877bool AudioFlinger::masterMute() const
878{
Glenn Kasten98067102011-12-13 11:47:54 -0800879 Mutex::Autolock _l(mLock);
880 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700881}
882
John Grossman4ff14ba2012-02-08 16:37:41 -0800883float AudioFlinger::masterVolume_l() const
884{
John Grossman4ff14ba2012-02-08 16:37:41 -0800885 return mMasterVolume;
886}
887
John Grossmand8f178d2012-07-20 14:51:35 -0700888bool AudioFlinger::masterMute_l() const
889{
John Grossmanee578c02012-07-23 17:05:46 -0700890 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700891}
892
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800893status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
894 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700895{
896 // check calling permissions
897 if (!settingsAllowed()) {
898 return PERMISSION_DENIED;
899 }
900
Glenn Kasten263709e2012-01-06 08:40:01 -0800901 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000902 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700903 return BAD_VALUE;
904 }
905
906 AutoMutex lock(mLock);
907 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700908 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700909 thread = checkPlaybackThread_l(output);
910 if (thread == NULL) {
911 return BAD_VALUE;
912 }
913 }
914
915 mStreamTypes[stream].volume = value;
916
917 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800918 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700919 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700920 }
921 } else {
922 thread->setStreamVolume(stream, value);
923 }
924
925 return NO_ERROR;
926}
927
Glenn Kastenfff6d712012-01-12 16:38:12 -0800928status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700929{
930 // check calling permissions
931 if (!settingsAllowed()) {
932 return PERMISSION_DENIED;
933 }
934
Glenn Kasten263709e2012-01-06 08:40:01 -0800935 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700936 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000937 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700938 return BAD_VALUE;
939 }
940
Eric Laurent93575202011-01-18 18:39:02 -0800941 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700942 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700943 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700944 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700945
946 return NO_ERROR;
947}
948
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800949float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700950{
Glenn Kasten263709e2012-01-06 08:40:01 -0800951 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700952 return 0.0f;
953 }
954
955 AutoMutex lock(mLock);
956 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -0700957 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700958 PlaybackThread *thread = checkPlaybackThread_l(output);
959 if (thread == NULL) {
960 return 0.0f;
961 }
962 volume = thread->streamVolume(stream);
963 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800964 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700965 }
966
967 return volume;
968}
969
Glenn Kastenfff6d712012-01-12 16:38:12 -0800970bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971{
Glenn Kasten263709e2012-01-06 08:40:01 -0800972 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700973 return true;
974 }
975
Glenn Kasten6637baa2012-01-09 09:40:36 -0800976 AutoMutex lock(mLock);
977 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700978}
979
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800980status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700981{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700982 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
983 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800984
Mathias Agopian65ab4712010-07-14 17:59:35 -0700985 // check calling permissions
986 if (!settingsAllowed()) {
987 return PERMISSION_DENIED;
988 }
989
Glenn Kasten142f5192014-03-25 17:44:59 -0700990 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
991 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700992 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700993 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800994 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700995 AutoMutex lock(mHardwareLock);
996 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
997 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
998 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
999 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1000 final_result = result ?: final_result;
1001 }
1002 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001003 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001004 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1005 AudioParameter param = AudioParameter(keyValuePairs);
1006 String8 value;
1007 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001008 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1009 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001010 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1011 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001012 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001013 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1014 // collect all of the thread's session IDs
1015 KeyedVector<int, bool> ids = thread->sessionIds();
1016 // suspend effects associated with those session IDs
1017 for (size_t j = 0; j < ids.size(); ++j) {
1018 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001019 thread->setEffectSuspended(FX_IID_AEC,
1020 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001021 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001022 thread->setEffectSuspended(FX_IID_NS,
1023 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001024 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001025 }
1026 }
Eric Laurentbee53372011-08-29 12:42:48 -07001027 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001028 }
1029 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001030 String8 screenState;
1031 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1032 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001033 if (isOff != (AudioFlinger::mScreenState & 1)) {
1034 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001035 }
1036 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001037 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001038 }
1039
1040 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1041 // and the thread is exited once the lock is released
1042 sp<ThreadBase> thread;
1043 {
1044 Mutex::Autolock _l(mLock);
1045 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001046 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001047 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001048 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001049 // indicate output device change to all input threads for pre processing
1050 AudioParameter param = AudioParameter(keyValuePairs);
1051 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001052 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1053 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001054 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1055 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1056 }
1057 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001058 }
1059 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001060 if (thread != 0) {
1061 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062 }
1063 return BAD_VALUE;
1064}
1065
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001066String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001067{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001068 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1069 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001070
Eric Laurenta4c5a552012-03-29 10:12:40 -07001071 Mutex::Autolock _l(mLock);
1072
Glenn Kasten142f5192014-03-25 17:44:59 -07001073 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001074 String8 out_s8;
1075
Dima Zavin799a70e2011-04-18 16:57:27 -07001076 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001077 char *s;
1078 {
1079 AutoMutex lock(mHardwareLock);
1080 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001081 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001082 s = dev->get_parameters(dev, keys.string());
1083 mHardwareStatus = AUDIO_HW_IDLE;
1084 }
John Grossmanef7740b2012-02-09 11:28:36 -08001085 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001086 free(s);
1087 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001088 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001089 }
1090
Mathias Agopian65ab4712010-07-14 17:59:35 -07001091 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1092 if (playbackThread != NULL) {
1093 return playbackThread->getParameters(keys);
1094 }
1095 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1096 if (recordThread != NULL) {
1097 return recordThread->getParameters(keys);
1098 }
1099 return String8("");
1100}
1101
Glenn Kastendd8104c2012-07-02 12:42:44 -07001102size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1103 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001104{
Eric Laurenta1884f92011-08-23 08:25:03 -07001105 status_t ret = initCheck();
1106 if (ret != NO_ERROR) {
1107 return 0;
1108 }
1109
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001110 AutoMutex lock(mHardwareLock);
1111 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001112 audio_config_t config;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001113 memset(&config, 0, sizeof(config));
1114 config.sample_rate = sampleRate;
1115 config.channel_mask = channelMask;
1116 config.format = format;
1117
John Grossmanee578c02012-07-23 17:05:46 -07001118 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1119 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001120 mHardwareStatus = AUDIO_HW_IDLE;
1121 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001122}
1123
Glenn Kasten5f972c02014-01-13 09:59:31 -08001124uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001125{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001126 Mutex::Autolock _l(mLock);
1127
1128 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1129 if (recordThread != NULL) {
1130 return recordThread->getInputFramesLost();
1131 }
1132 return 0;
1133}
1134
1135status_t AudioFlinger::setVoiceVolume(float value)
1136{
Eric Laurenta1884f92011-08-23 08:25:03 -07001137 status_t ret = initCheck();
1138 if (ret != NO_ERROR) {
1139 return ret;
1140 }
1141
Mathias Agopian65ab4712010-07-14 17:59:35 -07001142 // check calling permissions
1143 if (!settingsAllowed()) {
1144 return PERMISSION_DENIED;
1145 }
1146
1147 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001148 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001149 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001150 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001151 mHardwareStatus = AUDIO_HW_IDLE;
1152
1153 return ret;
1154}
1155
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001156status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001157 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001158{
1159 status_t status;
1160
1161 Mutex::Autolock _l(mLock);
1162
1163 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1164 if (playbackThread != NULL) {
1165 return playbackThread->getRenderPosition(halFrames, dspFrames);
1166 }
1167
1168 return BAD_VALUE;
1169}
1170
1171void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1172{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001173 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001174 if (client == 0) {
1175 return;
1176 }
Eric Laurent021cf962014-05-13 10:18:14 -07001177 bool clientAdded = false;
1178 {
1179 Mutex::Autolock _cl(mClientLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001180
Eric Laurent021cf962014-05-13 10:18:14 -07001181 pid_t pid = IPCThreadState::self()->getCallingPid();
1182 if (mNotificationClients.indexOfKey(pid) < 0) {
1183 sp<NotificationClient> notificationClient = new NotificationClient(this,
1184 client,
1185 pid);
1186 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001187
Eric Laurent021cf962014-05-13 10:18:14 -07001188 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001189
Eric Laurent021cf962014-05-13 10:18:14 -07001190 sp<IBinder> binder = client->asBinder();
1191 binder->linkToDeath(notificationClient);
1192 clientAdded = true;
1193 }
1194 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001195
Eric Laurent021cf962014-05-13 10:18:14 -07001196 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001197 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent021cf962014-05-13 10:18:14 -07001198 if (clientAdded) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001199 // the config change is always sent from playback or record threads to avoid deadlock
1200 // with AudioSystem::gLock
1201 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001202 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001203 }
1204
1205 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001206 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001207 }
1208 }
1209}
1210
1211void AudioFlinger::removeNotificationClient(pid_t pid)
1212{
1213 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001214 {
1215 Mutex::Autolock _cl(mClientLock);
1216 mNotificationClients.removeItem(pid);
1217 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001218
Steve Block3856b092011-10-20 11:56:00 +01001219 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001220 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001221 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001222 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001223 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001224 ALOGV(" pid %d @ %d", ref->mPid, i);
1225 if (ref->mPid == pid) {
1226 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001227 mAudioSessionRefs.removeAt(i);
1228 delete ref;
1229 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001230 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001231 } else {
1232 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001233 }
1234 }
1235 if (removed) {
1236 purgeStaleEffects_l();
1237 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001238}
1239
Eric Laurent021cf962014-05-13 10:18:14 -07001240void AudioFlinger::audioConfigChanged(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001241{
Eric Laurent021cf962014-05-13 10:18:14 -07001242 Mutex::Autolock _l(mClientLock);
1243 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001244 for (size_t i = 0; i < size; i++) {
Eric Laurent021cf962014-05-13 10:18:14 -07001245 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event,
Eric Laurent10351942014-05-08 18:49:52 -07001246 ioHandle,
1247 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001248 }
1249}
1250
Eric Laurent021cf962014-05-13 10:18:14 -07001251// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001252void AudioFlinger::removeClient_l(pid_t pid)
1253{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001254 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001255 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001256 mClients.removeItem(pid);
1257}
1258
Eric Laurent717e1282012-06-29 16:36:52 -07001259// getEffectThread_l() must be called with AudioFlinger::mLock held
1260sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1261{
1262 sp<PlaybackThread> thread;
1263
1264 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1265 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1266 ALOG_ASSERT(thread == 0);
1267 thread = mPlaybackThreads.valueAt(i);
1268 }
1269 }
1270
1271 return thread;
1272}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001273
Mathias Agopian65ab4712010-07-14 17:59:35 -07001274
Mathias Agopian65ab4712010-07-14 17:59:35 -07001275
1276// ----------------------------------------------------------------------------
1277
1278AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1279 : RefBase(),
1280 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001281 // 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 -07001282 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001283 mPid(pid),
1284 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001285{
1286 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1287}
1288
Eric Laurent021cf962014-05-13 10:18:14 -07001289// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001290AudioFlinger::Client::~Client()
1291{
1292 mAudioFlinger->removeClient_l(mPid);
1293}
1294
Glenn Kasten435dbe62012-01-30 10:15:48 -08001295sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001296{
1297 return mMemoryDealer;
1298}
1299
John Grossman4ff14ba2012-02-08 16:37:41 -08001300// Reserve one of the limited slots for a timed audio track associated
1301// with this client
1302bool AudioFlinger::Client::reserveTimedTrack()
1303{
1304 const int kMaxTimedTracksPerClient = 4;
1305
1306 Mutex::Autolock _l(mTimedTrackLock);
1307
1308 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1309 ALOGW("can not create timed track - pid %d has exceeded the limit",
1310 mPid);
1311 return false;
1312 }
1313
1314 mTimedTrackCount++;
1315 return true;
1316}
1317
1318// Release a slot for a timed audio track
1319void AudioFlinger::Client::releaseTimedTrack()
1320{
1321 Mutex::Autolock _l(mTimedTrackLock);
1322 mTimedTrackCount--;
1323}
1324
Mathias Agopian65ab4712010-07-14 17:59:35 -07001325// ----------------------------------------------------------------------------
1326
1327AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1328 const sp<IAudioFlingerClient>& client,
1329 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001330 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001331{
1332}
1333
1334AudioFlinger::NotificationClient::~NotificationClient()
1335{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001336}
1337
Glenn Kasten0f11b512014-01-31 16:18:54 -08001338void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001339{
1340 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001341 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001342}
1343
Mathias Agopian65ab4712010-07-14 17:59:35 -07001344
1345// ----------------------------------------------------------------------------
1346
Jeff Brown893a5642013-08-16 20:19:26 -07001347static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1348 return audio_is_remote_submix_device(inDevice);
1349}
1350
Mathias Agopian65ab4712010-07-14 17:59:35 -07001351sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001352 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001353 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001354 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001355 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001356 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001357 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001358 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001359 int *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001360 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001361 sp<IMemory>& cblk,
1362 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001363 status_t *status)
1364{
1365 sp<RecordThread::RecordTrack> recordTrack;
1366 sp<RecordHandle> recordHandle;
1367 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001368 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001369 int lSessionId;
1370
Glenn Kastend776ac62014-05-07 09:16:09 -07001371 cblk.clear();
1372 buffers.clear();
1373
Mathias Agopian65ab4712010-07-14 17:59:35 -07001374 // check calling permissions
1375 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001376 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001377 lStatus = PERMISSION_DENIED;
1378 goto Exit;
1379 }
1380
Glenn Kasten53b5d092014-02-05 10:00:23 -08001381 // further sample rate checks are performed by createRecordTrack_l()
1382 if (sampleRate == 0) {
1383 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1384 lStatus = BAD_VALUE;
1385 goto Exit;
1386 }
1387
Glenn Kasten0e0e8462014-03-13 15:02:40 -07001388 // we don't yet support anything other than 16-bit PCM
1389 if (!(audio_is_valid_format(format) &&
1390 audio_is_linear_pcm(format) && format == AUDIO_FORMAT_PCM_16_BIT)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001391 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001392 lStatus = BAD_VALUE;
1393 goto Exit;
1394 }
1395
Glenn Kasten53b5d092014-02-05 10:00:23 -08001396 // further channel mask checks are performed by createRecordTrack_l()
1397 if (!audio_is_input_channel(channelMask)) {
1398 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1399 lStatus = BAD_VALUE;
1400 goto Exit;
1401 }
1402
Glenn Kasten05997e22014-03-13 15:08:33 -07001403 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001404 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001405 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001406 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001407 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001408 lStatus = BAD_VALUE;
1409 goto Exit;
1410 }
1411
Jeff Brown893a5642013-08-16 20:19:26 -07001412 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1413 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001414 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001415 lStatus = PERMISSION_DENIED;
1416 goto Exit;
1417 }
1418
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001419 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001420 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001421
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001422 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001423 lSessionId = *sessionId;
1424 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001425 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001426 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001427 if (sessionId != NULL) {
1428 *sessionId = lSessionId;
1429 }
1430 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001431 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001432
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001433 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001434 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001435 frameCount, lSessionId, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001436 IPCThreadState::self()->getCallingUid(),
1437 flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001438 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001439
1440 if (lStatus == NO_ERROR) {
1441 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1442 // session and move it to this thread.
1443 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)lSessionId);
1444 if (chain != 0) {
1445 Mutex::Autolock _l(thread->mLock);
1446 thread->addEffectChain_l(chain);
1447 }
1448 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001449 }
Glenn Kastene198c362013-08-13 09:13:36 -07001450
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001451 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001452 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001453 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001454 // Don't hold mClientLock when releasing the reference on the track as the
1455 // destructor will acquire it.
1456 {
1457 Mutex::Autolock _cl(mClientLock);
1458 client.clear();
1459 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001460 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001461 goto Exit;
1462 }
1463
Glenn Kastend776ac62014-05-07 09:16:09 -07001464 cblk = recordTrack->getCblk();
1465 buffers = recordTrack->getBuffers();
1466
Glenn Kasten2fc14732013-08-05 14:58:14 -07001467 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001468 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001469
1470Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001471 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001472 return recordHandle;
1473}
1474
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001475
1476
Mathias Agopian65ab4712010-07-14 17:59:35 -07001477// ----------------------------------------------------------------------------
1478
Eric Laurenta4c5a552012-03-29 10:12:40 -07001479audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1480{
Eric Laurent44622db2014-08-01 19:00:33 -07001481 if (name == NULL) {
1482 return 0;
1483 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001484 if (!settingsAllowed()) {
1485 return 0;
1486 }
1487 Mutex::Autolock _l(mLock);
1488 return loadHwModule_l(name);
1489}
1490
1491// loadHwModule_l() must be called with AudioFlinger::mLock held
1492audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1493{
1494 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1495 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1496 ALOGW("loadHwModule() module %s already loaded", name);
1497 return mAudioHwDevs.keyAt(i);
1498 }
1499 }
1500
Eric Laurenta4c5a552012-03-29 10:12:40 -07001501 audio_hw_device_t *dev;
1502
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001503 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001504 if (rc) {
1505 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1506 return 0;
1507 }
1508
1509 mHardwareStatus = AUDIO_HW_INIT;
1510 rc = dev->init_check(dev);
1511 mHardwareStatus = AUDIO_HW_IDLE;
1512 if (rc) {
1513 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1514 return 0;
1515 }
1516
John Grossmanee578c02012-07-23 17:05:46 -07001517 // Check and cache this HAL's level of support for master mute and master
1518 // volume. If this is the first HAL opened, and it supports the get
1519 // methods, use the initial values provided by the HAL as the current
1520 // master mute and volume settings.
1521
1522 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1523 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001524 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001525
1526 if (0 == mAudioHwDevs.size()) {
1527 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1528 if (NULL != dev->get_master_volume) {
1529 float mv;
1530 if (OK == dev->get_master_volume(dev, &mv)) {
1531 mMasterVolume = mv;
1532 }
1533 }
1534
1535 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1536 if (NULL != dev->get_master_mute) {
1537 bool mm;
1538 if (OK == dev->get_master_mute(dev, &mm)) {
1539 mMasterMute = mm;
1540 }
1541 }
1542 }
1543
Eric Laurenta4c5a552012-03-29 10:12:40 -07001544 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001545 if ((NULL != dev->set_master_volume) &&
1546 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1547 flags = static_cast<AudioHwDevice::Flags>(flags |
1548 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1549 }
1550
1551 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1552 if ((NULL != dev->set_master_mute) &&
1553 (OK == dev->set_master_mute(dev, mMasterMute))) {
1554 flags = static_cast<AudioHwDevice::Flags>(flags |
1555 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1556 }
1557
Eric Laurenta4c5a552012-03-29 10:12:40 -07001558 mHardwareStatus = AUDIO_HW_IDLE;
1559 }
1560
1561 audio_module_handle_t handle = nextUniqueId();
Eric Laurent83b88082014-06-20 18:31:16 -07001562 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001563
1564 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001565 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001566
1567 return handle;
1568
1569}
1570
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001571// ----------------------------------------------------------------------------
1572
Glenn Kasten3b16c762012-11-14 08:44:39 -08001573uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001574{
1575 Mutex::Autolock _l(mLock);
1576 PlaybackThread *thread = primaryPlaybackThread_l();
1577 return thread != NULL ? thread->sampleRate() : 0;
1578}
1579
Glenn Kastene33054e2012-11-14 12:54:39 -08001580size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001581{
1582 Mutex::Autolock _l(mLock);
1583 PlaybackThread *thread = primaryPlaybackThread_l();
1584 return thread != NULL ? thread->frameCountHAL() : 0;
1585}
1586
1587// ----------------------------------------------------------------------------
1588
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001589status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1590{
1591 uid_t uid = IPCThreadState::self()->getCallingUid();
1592 if (uid != AID_SYSTEM) {
1593 return PERMISSION_DENIED;
1594 }
1595 Mutex::Autolock _l(mLock);
1596 if (mIsDeviceTypeKnown) {
1597 return INVALID_OPERATION;
1598 }
1599 mIsLowRamDevice = isLowRamDevice;
1600 mIsDeviceTypeKnown = true;
1601 return NO_ERROR;
1602}
1603
Eric Laurent93c3d412014-08-01 14:48:35 -07001604audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1605{
1606 Mutex::Autolock _l(mLock);
1607 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1608 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1609 if ((thread->hasAudioSession(sessionId) & ThreadBase::TRACK_SESSION) != 0) {
1610 // A session can only be on one thread, so exit after first match
1611 String8 reply = thread->getParameters(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC));
1612 AudioParameter param = AudioParameter(reply);
1613 int value;
1614 if (param.getInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value) == NO_ERROR) {
1615 return value;
1616 }
1617 break;
1618 }
1619 }
1620 return AUDIO_HW_SYNC_INVALID;
1621}
1622
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001623// ----------------------------------------------------------------------------
1624
Eric Laurent83b88082014-06-20 18:31:16 -07001625
1626sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001627 audio_io_handle_t *output,
1628 audio_config_t *config,
1629 audio_devices_t devices,
1630 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001631 audio_output_flags_t flags)
1632{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001633 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001634 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001635 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001636 }
1637
1638 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentcf2c0212014-07-25 16:20:43 -07001639 if (*output == AUDIO_IO_HANDLE_NONE) {
1640 *output = nextUniqueId();
1641 }
Eric Laurent83b88082014-06-20 18:31:16 -07001642
1643 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1644
1645 audio_stream_out_t *outStream = NULL;
1646
1647 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001648 // This if statement allows overriding the audio policy settings
1649 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1650 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1651 // Check only for Normal Mixing mode
1652 if (kEnableExtendedPrecision) {
1653 // Specify format (uncomment one below to choose)
1654 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1655 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1656 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1657 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001658 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001659 }
1660 if (kEnableExtendedChannels) {
1661 // Specify channel mask (uncomment one below to choose)
1662 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1663 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1664 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1665 }
Eric Laurent83b88082014-06-20 18:31:16 -07001666 }
1667
1668 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001669 *output,
1670 devices,
1671 flags,
1672 config,
1673 &outStream,
1674 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001675
1676 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001677 ALOGV("openOutput_l() openOutputStream returned output %p, sampleRate %d, Format %#x, "
Andy Hung9a592762014-07-21 21:56:01 -07001678 "channelMask %#x, status %d",
Eric Laurent83b88082014-06-20 18:31:16 -07001679 outStream,
1680 config->sample_rate,
1681 config->format,
1682 config->channel_mask,
1683 status);
1684
1685 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001686 AudioStreamOut *outputStream = new AudioStreamOut(outHwDev, outStream, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001687
1688 PlaybackThread *thread;
1689 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001690 thread = new OffloadThread(this, outputStream, *output, devices);
1691 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001692 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1693 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001694 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001695 thread = new DirectOutputThread(this, outputStream, *output, devices);
1696 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001697 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001698 thread = new MixerThread(this, outputStream, *output, devices);
1699 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001700 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001701 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001702 return thread;
1703 }
1704
1705 return 0;
1706}
1707
Eric Laurentcf2c0212014-07-25 16:20:43 -07001708status_t AudioFlinger::openOutput(audio_module_handle_t module,
1709 audio_io_handle_t *output,
1710 audio_config_t *config,
1711 audio_devices_t *devices,
1712 const String8& address,
1713 uint32_t *latencyMs,
1714 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001715{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001716 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001717 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001718 (devices != NULL) ? *devices : 0,
1719 config->sample_rate,
1720 config->format,
1721 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001722 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001723
Eric Laurentcf2c0212014-07-25 16:20:43 -07001724 if (*devices == AUDIO_DEVICE_NONE) {
1725 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001726 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001727
Mathias Agopian65ab4712010-07-14 17:59:35 -07001728 Mutex::Autolock _l(mLock);
1729
Eric Laurentcf2c0212014-07-25 16:20:43 -07001730 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001731 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001732 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001733
1734 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001735 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001736
1737 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001738 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001739 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001740 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001741
1742 AutoMutex lock(mHardwareLock);
1743 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001744 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001745 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten4ea00a22014-06-02 08:29:22 -07001746
Eric Laurentcf2c0212014-07-25 16:20:43 -07001747 mPrimaryOutputSampleRate = config->sample_rate;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001748 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001749 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001750 }
1751
Eric Laurentcf2c0212014-07-25 16:20:43 -07001752 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001753}
1754
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001755audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1756 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001757{
1758 Mutex::Autolock _l(mLock);
1759 MixerThread *thread1 = checkMixerThread_l(output1);
1760 MixerThread *thread2 = checkMixerThread_l(output2);
1761
1762 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001763 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1764 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001765 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001766 }
1767
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001768 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001769 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1770 thread->addOutputTrack(thread2);
1771 mPlaybackThreads.add(id, thread);
1772 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001773 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001774 return id;
1775}
1776
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001777status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001778{
Glenn Kastend96c5722012-04-25 13:44:49 -07001779 return closeOutput_nonvirtual(output);
1780}
1781
1782status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1783{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001784 // keep strong reference on the playback thread so that
1785 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001786 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001787 {
1788 Mutex::Autolock _l(mLock);
1789 thread = checkPlaybackThread_l(output);
1790 if (thread == NULL) {
1791 return BAD_VALUE;
1792 }
1793
Steve Block3856b092011-10-20 11:56:00 +01001794 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001795
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001796 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001797 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001798 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001799 DuplicatingThread *dupThread =
1800 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001801 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001802
1803 }
1804 }
1805 }
1806
1807
1808 mPlaybackThreads.removeItem(output);
1809 // save all effects to the default thread
1810 if (mPlaybackThreads.size()) {
1811 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1812 if (dstThread != NULL) {
1813 // audioflinger lock is held here so the acquisition order of thread locks does not
1814 // matter
1815 Mutex::Autolock _dl(dstThread->mLock);
1816 Mutex::Autolock _sl(thread->mLock);
1817 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1818 for (size_t i = 0; i < effectChains.size(); i ++) {
1819 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001820 }
1821 }
1822 }
Eric Laurent021cf962014-05-13 10:18:14 -07001823 audioConfigChanged(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001824 }
1825 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001826 // The thread entity (active unit of execution) is no longer running here,
1827 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001828
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001829 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurent83b88082014-06-20 18:31:16 -07001830 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001831 }
Eric Laurent83b88082014-06-20 18:31:16 -07001832
Mathias Agopian65ab4712010-07-14 17:59:35 -07001833 return NO_ERROR;
1834}
1835
Eric Laurent83b88082014-06-20 18:31:16 -07001836void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
1837{
1838 AudioStreamOut *out = thread->clearOutput();
1839 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1840 // from now on thread->mOutput is NULL
1841 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
1842 delete out;
1843}
1844
1845void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
1846{
1847 mPlaybackThreads.removeItem(thread->mId);
1848 thread->exit();
1849 closeOutputFinish(thread);
1850}
1851
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001852status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001853{
1854 Mutex::Autolock _l(mLock);
1855 PlaybackThread *thread = checkPlaybackThread_l(output);
1856
1857 if (thread == NULL) {
1858 return BAD_VALUE;
1859 }
1860
Steve Block3856b092011-10-20 11:56:00 +01001861 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001862 thread->suspend();
1863
1864 return NO_ERROR;
1865}
1866
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001867status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001868{
1869 Mutex::Autolock _l(mLock);
1870 PlaybackThread *thread = checkPlaybackThread_l(output);
1871
1872 if (thread == NULL) {
1873 return BAD_VALUE;
1874 }
1875
Steve Block3856b092011-10-20 11:56:00 +01001876 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001877
1878 thread->restore();
1879
1880 return NO_ERROR;
1881}
1882
Eric Laurentcf2c0212014-07-25 16:20:43 -07001883status_t AudioFlinger::openInput(audio_module_handle_t module,
1884 audio_io_handle_t *input,
1885 audio_config_t *config,
1886 audio_devices_t *device,
1887 const String8& address,
1888 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07001889 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001890{
Eric Laurent83b88082014-06-20 18:31:16 -07001891 Mutex::Autolock _l(mLock);
1892
Eric Laurentcf2c0212014-07-25 16:20:43 -07001893 if (*device == AUDIO_DEVICE_NONE) {
1894 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07001895 }
1896
Eric Laurentcf2c0212014-07-25 16:20:43 -07001897 sp<RecordThread> thread = openInput_l(module, input, config, *device, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001898
1899 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07001900 // notify client processes of the new input creation
1901 thread->audioConfigChanged(AudioSystem::INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001902 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001903 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001904 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07001905}
Dima Zavin799a70e2011-04-18 16:57:27 -07001906
Eric Laurent83b88082014-06-20 18:31:16 -07001907sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001908 audio_io_handle_t *input,
1909 audio_config_t *config,
Eric Laurent83b88082014-06-20 18:31:16 -07001910 audio_devices_t device,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001911 const String8& address,
1912 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07001913 audio_input_flags_t flags)
1914{
Eric Laurent83b88082014-06-20 18:31:16 -07001915 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, device);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001916 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001917 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07001918 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001919 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001920
Eric Laurentcf2c0212014-07-25 16:20:43 -07001921 if (*input == AUDIO_IO_HANDLE_NONE) {
1922 *input = nextUniqueId();
1923 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001924
Eric Laurentcf2c0212014-07-25 16:20:43 -07001925 audio_config_t halconfig = *config;
1926 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07001927 audio_stream_in_t *inStream = NULL;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001928 status_t status = inHwHal->open_input_stream(inHwHal, *input, device, &halconfig,
1929 &inStream, flags, address.string(), source);
1930 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
1931 ", Format %#x, Channels %x, flags %#x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001932 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001933 halconfig.sample_rate,
1934 halconfig.format,
1935 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07001936 flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001937 status);
1938
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001939 // If the input could not be opened with the requested parameters and we can handle the
1940 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1941 // 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 -07001942 if (status == BAD_VALUE &&
Eric Laurentcf2c0212014-07-25 16:20:43 -07001943 config->format == halconfig.format && halconfig.format == AUDIO_FORMAT_PCM_16_BIT &&
1944 (halconfig.sample_rate <= 2 * config->sample_rate) &&
1945 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
1946 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07001947 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07001948 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001949 inStream = NULL;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001950 status = inHwHal->open_input_stream(inHwHal, *input, device, &halconfig,
1951 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07001952 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07001953 }
1954
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001955 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001956
Glenn Kasten46909e72013-02-26 09:20:22 -08001957#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001958 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1959 // or (re-)create if current Pipe is idle and does not match the new format
1960 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001961 enum {
1962 TEE_SINK_NO, // don't copy input
1963 TEE_SINK_NEW, // copy input using a new pipe
1964 TEE_SINK_OLD, // copy input using an existing pipe
1965 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07001966 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
1967 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08001968 if (!mTeeSinkInputEnabled) {
1969 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08001970 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001971 kind = TEE_SINK_NO;
1972 } else if (mRecordTeeSink == 0) {
1973 kind = TEE_SINK_NEW;
1974 } else if (mRecordTeeSink->getStrongCount() != 1) {
1975 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08001976 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001977 kind = TEE_SINK_OLD;
1978 } else {
1979 kind = TEE_SINK_NEW;
1980 }
1981 switch (kind) {
1982 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001983 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001984 size_t numCounterOffers = 0;
1985 const NBAIO_Format offers[1] = {format};
1986 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1987 ALOG_ASSERT(index == 0);
1988 PipeReader *pipeReader = new PipeReader(*pipe);
1989 numCounterOffers = 0;
1990 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1991 ALOG_ASSERT(index == 0);
1992 mRecordTeeSink = pipe;
1993 mRecordTeeSource = pipeReader;
1994 teeSink = pipe;
1995 }
1996 break;
1997 case TEE_SINK_OLD:
1998 teeSink = mRecordTeeSink;
1999 break;
2000 case TEE_SINK_NO:
2001 default:
2002 break;
2003 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002004#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002005
Eric Laurentcf2c0212014-07-25 16:20:43 -07002006 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002007
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002008 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002009 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002010 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002011 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002012 inputStream,
2013 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002014 primaryOutputDevice_l(),
Eric Laurent83b88082014-06-20 18:31:16 -07002015 device
Glenn Kasten46909e72013-02-26 09:20:22 -08002016#ifdef TEE_SINK
2017 , teeSink
2018#endif
2019 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002020 mRecordThreads.add(*input, thread);
2021 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002022 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002023 }
2024
Eric Laurentcf2c0212014-07-25 16:20:43 -07002025 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002026 return 0;
2027}
2028
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002029status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002030{
Glenn Kastend96c5722012-04-25 13:44:49 -07002031 return closeInput_nonvirtual(input);
2032}
2033
2034status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2035{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002036 // keep strong reference on the record thread so that
2037 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002038 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002039 {
2040 Mutex::Autolock _l(mLock);
2041 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002042 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002043 return BAD_VALUE;
2044 }
2045
Steve Block3856b092011-10-20 11:56:00 +01002046 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002047
2048 // If we still have effect chains, it means that a client still holds a handle
2049 // on at least one effect. We must either move the chain to an existing thread with the
2050 // same session ID or put it aside in case a new record thread is opened for a
2051 // new capture on the same session
2052 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002053 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002054 Mutex::Autolock _sl(thread->mLock);
2055 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002056 // Note: maximum one chain per record thread
2057 if (effectChains.size() != 0) {
2058 chain = effectChains[0];
2059 }
2060 }
2061 if (chain != 0) {
2062 // first check if a record thread is already opened with a client on the same session.
2063 // This should only happen in case of overlap between one thread tear down and the
2064 // creation of its replacement
2065 size_t i;
2066 for (i = 0; i < mRecordThreads.size(); i++) {
2067 sp<RecordThread> t = mRecordThreads.valueAt(i);
2068 if (t == thread) {
2069 continue;
2070 }
2071 if (t->hasAudioSession(chain->sessionId()) != 0) {
2072 Mutex::Autolock _l(t->mLock);
2073 ALOGV("closeInput() found thread %d for effect session %d",
2074 t->id(), chain->sessionId());
2075 t->addEffectChain_l(chain);
2076 break;
2077 }
2078 }
2079 // put the chain aside if we could not find a record thread with the same session id.
2080 if (i == mRecordThreads.size()) {
2081 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002082 }
2083 }
Eric Laurent021cf962014-05-13 10:18:14 -07002084 audioConfigChanged(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002085 mRecordThreads.removeItem(input);
2086 }
Eric Laurent83b88082014-06-20 18:31:16 -07002087 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2088 // we have a different lock for notification client
2089 closeInputFinish(thread);
2090 return NO_ERROR;
2091}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002092
Eric Laurent83b88082014-06-20 18:31:16 -07002093void AudioFlinger::closeInputFinish(sp<RecordThread> thread)
2094{
2095 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002096 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002097 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002098 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002099 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002100 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002101}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002102
Eric Laurent83b88082014-06-20 18:31:16 -07002103void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
2104{
2105 mRecordThreads.removeItem(thread->mId);
2106 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002107}
2108
Glenn Kastend2304db2014-02-03 07:40:31 -08002109status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002110{
2111 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002112 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002113
2114 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2115 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002116 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002117 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002118
2119 return NO_ERROR;
2120}
2121
2122
Eric Laurentde3f8392014-07-27 18:38:22 -07002123audio_unique_id_t AudioFlinger::newAudioUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002124{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002125 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002126}
2127
Marco Nelissend457c972014-02-11 08:47:07 -08002128void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002129{
2130 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002131 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002132 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2133 if (pid != -1 && (caller == getpid_cached)) {
2134 caller = pid;
2135 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002136
Eric Laurent021cf962014-05-13 10:18:14 -07002137 {
2138 Mutex::Autolock _cl(mClientLock);
2139 // Ignore requests received from processes not known as notification client. The request
2140 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2141 // called from a different pid leaving a stale session reference. Also we don't know how
2142 // to clear this reference if the client process dies.
2143 if (mNotificationClients.indexOfKey(caller) < 0) {
2144 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2145 return;
2146 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002147 }
2148
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002149 size_t num = mAudioSessionRefs.size();
2150 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002151 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002152 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2153 ref->mCnt++;
2154 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002155 return;
2156 }
2157 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002158 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2159 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002160}
2161
Marco Nelissend457c972014-02-11 08:47:07 -08002162void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002163{
2164 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002165 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002166 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2167 if (pid != -1 && (caller == getpid_cached)) {
2168 caller = pid;
2169 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002170 size_t num = mAudioSessionRefs.size();
2171 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002172 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002173 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2174 ref->mCnt--;
2175 ALOGV(" decremented refcount to %d", ref->mCnt);
2176 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002177 mAudioSessionRefs.removeAt(i);
2178 delete ref;
2179 purgeStaleEffects_l();
2180 }
2181 return;
2182 }
2183 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002184 // If the caller is mediaserver it is likely that the session being released was acquired
2185 // on behalf of a process not in notification clients and we ignore the warning.
2186 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002187}
2188
2189void AudioFlinger::purgeStaleEffects_l() {
2190
Steve Block3856b092011-10-20 11:56:00 +01002191 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002192
2193 Vector< sp<EffectChain> > chains;
2194
2195 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2196 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2197 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2198 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002199 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2200 chains.push(ec);
2201 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002202 }
2203 }
2204 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2205 sp<RecordThread> t = mRecordThreads.valueAt(i);
2206 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2207 sp<EffectChain> ec = t->mEffectChains[j];
2208 chains.push(ec);
2209 }
2210 }
2211
2212 for (size_t i = 0; i < chains.size(); i++) {
2213 sp<EffectChain> ec = chains[i];
2214 int sessionid = ec->sessionId();
2215 sp<ThreadBase> t = ec->mThread.promote();
2216 if (t == 0) {
2217 continue;
2218 }
2219 size_t numsessionrefs = mAudioSessionRefs.size();
2220 bool found = false;
2221 for (size_t k = 0; k < numsessionrefs; k++) {
2222 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002223 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002224 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002225 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002226 found = true;
2227 break;
2228 }
2229 }
2230 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002231 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002232 // remove all effects from the chain
2233 while (ec->mEffects.size()) {
2234 sp<EffectModule> effect = ec->mEffects[0];
2235 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002236 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002237 if (effect->purgeHandles()) {
2238 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002239 }
2240 AudioSystem::unregisterEffect(effect->id());
2241 }
2242 }
2243 }
2244 return;
2245}
2246
Mathias Agopian65ab4712010-07-14 17:59:35 -07002247// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002248AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002249{
Glenn Kastena1117922012-01-26 10:53:32 -08002250 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002251}
2252
2253// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002254AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002255{
2256 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002257 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002258}
2259
2260// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002261AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002262{
Glenn Kastena1117922012-01-26 10:53:32 -08002263 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002264}
2265
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002266uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002267{
Glenn Kastenbcefec32014-01-17 12:09:05 -08002268 return (uint32_t) android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002269}
2270
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002271AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002272{
2273 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2274 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002275 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002276 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002277 return thread;
2278 }
2279 }
2280 return NULL;
2281}
2282
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002283audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002284{
2285 PlaybackThread *thread = primaryPlaybackThread_l();
2286
2287 if (thread == NULL) {
2288 return 0;
2289 }
2290
Eric Laurentf1c04f92012-08-28 14:26:53 -07002291 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002292}
2293
Eric Laurenta011e352012-03-29 15:51:43 -07002294sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2295 int triggerSession,
2296 int listenerSession,
2297 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002298 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002299{
2300 Mutex::Autolock _l(mLock);
2301
2302 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2303 status_t playStatus = NAME_NOT_FOUND;
2304 status_t recStatus = NAME_NOT_FOUND;
2305 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2306 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2307 if (playStatus == NO_ERROR) {
2308 return event;
2309 }
2310 }
2311 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2312 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2313 if (recStatus == NO_ERROR) {
2314 return event;
2315 }
2316 }
2317 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2318 mPendingSyncEvents.add(event);
2319 } else {
2320 ALOGV("createSyncEvent() invalid event %d", event->type());
2321 event.clear();
2322 }
2323 return event;
2324}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002325
Mathias Agopian65ab4712010-07-14 17:59:35 -07002326// ----------------------------------------------------------------------------
2327// Effect management
2328// ----------------------------------------------------------------------------
2329
2330
Glenn Kastenf587ba52012-01-26 16:25:10 -08002331status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002332{
2333 Mutex::Autolock _l(mLock);
2334 return EffectQueryNumberEffects(numEffects);
2335}
2336
Glenn Kastenf587ba52012-01-26 16:25:10 -08002337status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002338{
2339 Mutex::Autolock _l(mLock);
2340 return EffectQueryEffect(index, descriptor);
2341}
2342
Glenn Kasten5e92a782012-01-30 07:40:52 -08002343status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002344 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002345{
2346 Mutex::Autolock _l(mLock);
2347 return EffectGetDescriptor(pUuid, descriptor);
2348}
2349
2350
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002351sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002352 effect_descriptor_t *pDesc,
2353 const sp<IEffectClient>& effectClient,
2354 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002355 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002356 int sessionId,
2357 status_t *status,
2358 int *id,
2359 int *enabled)
2360{
2361 status_t lStatus = NO_ERROR;
2362 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002363 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002364
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002365 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002366 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002367 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002368
2369 if (pDesc == NULL) {
2370 lStatus = BAD_VALUE;
2371 goto Exit;
2372 }
2373
Eric Laurent84e9a102010-09-23 16:10:16 -07002374 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002375 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002376 lStatus = PERMISSION_DENIED;
2377 goto Exit;
2378 }
2379
Dima Zavinfce7a472011-04-19 22:30:36 -07002380 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002381 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002382 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002383 lStatus = PERMISSION_DENIED;
2384 goto Exit;
2385 }
2386
Mathias Agopian65ab4712010-07-14 17:59:35 -07002387 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002388 if (!EffectIsNullUuid(&pDesc->uuid)) {
2389 // if uuid is specified, request effect descriptor
2390 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2391 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002392 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002393 goto Exit;
2394 }
2395 } else {
2396 // if uuid is not specified, look for an available implementation
2397 // of the required type in effect factory
2398 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002399 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002400 lStatus = BAD_VALUE;
2401 goto Exit;
2402 }
2403 uint32_t numEffects = 0;
2404 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002405 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002406 bool found = false;
2407
2408 lStatus = EffectQueryNumberEffects(&numEffects);
2409 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002410 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002411 goto Exit;
2412 }
2413 for (uint32_t i = 0; i < numEffects; i++) {
2414 lStatus = EffectQueryEffect(i, &desc);
2415 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002416 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002417 continue;
2418 }
2419 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2420 // If matching type found save effect descriptor. If the session is
2421 // 0 and the effect is not auxiliary, continue enumeration in case
2422 // an auxiliary version of this effect type is available
2423 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002424 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002425 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002426 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2427 break;
2428 }
2429 }
2430 }
2431 if (!found) {
2432 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002433 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002434 goto Exit;
2435 }
2436 // For same effect type, chose auxiliary version over insert version if
2437 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002438 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002439 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002440 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002441 }
2442 }
2443
2444 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002445 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002446 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2447 lStatus = INVALID_OPERATION;
2448 goto Exit;
2449 }
2450
Eric Laurent59255e42011-07-27 19:49:51 -07002451 // check recording permission for visualizer
2452 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2453 !recordingAllowed()) {
2454 lStatus = PERMISSION_DENIED;
2455 goto Exit;
2456 }
2457
Mathias Agopian65ab4712010-07-14 17:59:35 -07002458 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002459 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002460 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002461 // if the output returned by getOutputForEffect() is removed before we lock the
2462 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2463 // and we will exit safely
2464 io = AudioSystem::getOutputForEffect(&desc);
2465 ALOGV("createEffect got output %d", io);
2466 }
2467
2468 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002469
2470 // If output is not specified try to find a matching audio session ID in one of the
2471 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002472 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2473 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002474 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002475 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002476 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2477 // output must be specified by AudioPolicyManager when using session
2478 // AUDIO_SESSION_OUTPUT_STAGE
2479 lStatus = BAD_VALUE;
2480 goto Exit;
2481 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002482 // look for the thread where the specified audio session is present
2483 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2484 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2485 io = mPlaybackThreads.keyAt(i);
2486 break;
2487 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002488 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002489 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002490 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2491 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2492 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002493 break;
2494 }
2495 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002496 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002497 // If no output thread contains the requested session ID, default to
2498 // first output. The effect chain will be moved to the correct output
2499 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002500 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002501 io = mPlaybackThreads.keyAt(0);
2502 }
Steve Block3856b092011-10-20 11:56:00 +01002503 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002504 }
2505 ThreadBase *thread = checkRecordThread_l(io);
2506 if (thread == NULL) {
2507 thread = checkPlaybackThread_l(io);
2508 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002509 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002510 lStatus = BAD_VALUE;
2511 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002512 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002513 } else {
2514 // Check if one effect chain was awaiting for an effect to be created on this
2515 // session and used it instead of creating a new one.
2516 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)sessionId);
2517 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002518 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002519 thread->addEffectChain_l(chain);
2520 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002521 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002522
Eric Laurent021cf962014-05-13 10:18:14 -07002523 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002524
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002525 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002526 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2527 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002528 if (handle != 0 && id != NULL) {
2529 *id = handle->id();
2530 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002531 if (handle == 0) {
2532 // remove local strong reference to Client with mClientLock held
2533 Mutex::Autolock _cl(mClientLock);
2534 client.clear();
2535 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002536 }
2537
2538Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002539 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002540 return handle;
2541}
2542
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002543status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2544 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002545{
Steve Block3856b092011-10-20 11:56:00 +01002546 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002547 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002548 Mutex::Autolock _l(mLock);
2549 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002550 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002551 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002552 }
Eric Laurentde070132010-07-13 04:45:46 -07002553 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2554 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002555 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002556 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002557 }
Eric Laurentde070132010-07-13 04:45:46 -07002558 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2559 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002560 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002561 return BAD_VALUE;
2562 }
2563
2564 Mutex::Autolock _dl(dstThread->mLock);
2565 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002566 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002567}
2568
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002569// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002570status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002571 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002572 AudioFlinger::PlaybackThread *dstThread,
2573 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002574{
Steve Block3856b092011-10-20 11:56:00 +01002575 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002576 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002577
Eric Laurent59255e42011-07-27 19:49:51 -07002578 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002579 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002580 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002581 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002582 return INVALID_OPERATION;
2583 }
2584
Andy Hung9a592762014-07-21 21:56:01 -07002585 // Check whether the destination thread has a channel count of FCC_2, which is
2586 // currently required for (most) effects. Prevent moving the effect chain here rather
2587 // than disabling the addEffect_l() call in dstThread below.
2588 if (dstThread->mChannelCount != FCC_2) {
2589 ALOGW("moveEffectChain_l() effect chain failed because"
2590 " destination thread %p channel count(%u) != %u",
2591 dstThread, dstThread->mChannelCount, FCC_2);
2592 return INVALID_OPERATION;
2593 }
2594
Eric Laurent39e94f82010-07-28 01:32:47 -07002595 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002596 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002597 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002598 // removed.
2599 srcThread->removeEffectChain_l(chain);
2600
2601 // transfer all effects one by one so that new effect chain is created on new thread with
2602 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002603 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002604 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002605 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002606 Vector< sp<EffectModule> > removed;
2607 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002608 while (effect != 0) {
2609 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002610 removed.add(effect);
2611 status = dstThread->addEffect_l(effect);
2612 if (status != NO_ERROR) {
2613 break;
2614 }
Eric Laurentec35a142011-10-05 17:42:25 -07002615 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2616 if (effect->state() == EffectModule::ACTIVE ||
2617 effect->state() == EffectModule::STOPPING) {
2618 effect->start();
2619 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002620 // if the move request is not received from audio policy manager, the effect must be
2621 // re-registered with the new strategy and output
2622 if (dstChain == 0) {
2623 dstChain = effect->chain().promote();
2624 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002625 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002626 status = NO_INIT;
2627 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002628 }
2629 strategy = dstChain->strategy();
2630 }
2631 if (reRegister) {
2632 AudioSystem::unregisterEffect(effect->id());
2633 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002634 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002635 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002636 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002637 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002638 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002639 }
Eric Laurentde070132010-07-13 04:45:46 -07002640 effect = chain->getEffectFromId_l(0);
2641 }
2642
Eric Laurent5baf2af2013-09-12 17:37:00 -07002643 if (status != NO_ERROR) {
2644 for (size_t i = 0; i < removed.size(); i++) {
2645 srcThread->addEffect_l(removed[i]);
2646 if (dstChain != 0 && reRegister) {
2647 AudioSystem::unregisterEffect(removed[i]->id());
2648 AudioSystem::registerEffect(&removed[i]->desc(),
2649 srcThread->id(),
2650 strategy,
2651 sessionId,
2652 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002653 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002654 }
2655 }
2656 }
2657
2658 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002659}
2660
Eric Laurent5baf2af2013-09-12 17:37:00 -07002661bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002662{
2663 if (mGlobalEffectEnableTime != 0 &&
2664 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2665 return true;
2666 }
2667
2668 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2669 sp<EffectChain> ec =
2670 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002671 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002672 return true;
2673 }
2674 }
2675 return false;
2676}
2677
Eric Laurent5baf2af2013-09-12 17:37:00 -07002678void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002679{
2680 Mutex::Autolock _l(mLock);
2681
2682 mGlobalEffectEnableTime = systemTime();
2683
2684 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2685 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2686 if (t->mType == ThreadBase::OFFLOAD) {
2687 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2688 }
2689 }
2690
2691}
2692
Eric Laurentaaa44472014-09-12 17:41:50 -07002693status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2694{
2695 audio_session_t session = (audio_session_t)chain->sessionId();
2696 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2697 ALOGV("putOrphanEffectChain_l session %d index %d", session, index);
2698 if (index >= 0) {
2699 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2700 return ALREADY_EXISTS;
2701 }
2702 mOrphanEffectChains.add(session, chain);
2703 return NO_ERROR;
2704}
2705
2706sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2707{
2708 sp<EffectChain> chain;
2709 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2710 ALOGV("getOrphanEffectChain_l session %d index %d", session, index);
2711 if (index >= 0) {
2712 chain = mOrphanEffectChains.valueAt(index);
2713 mOrphanEffectChains.removeItemsAt(index);
2714 }
2715 return chain;
2716}
2717
2718bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2719{
2720 Mutex::Autolock _l(mLock);
2721 audio_session_t session = (audio_session_t)effect->sessionId();
2722 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2723 ALOGV("updateOrphanEffectChains session %d index %d", session, index);
2724 if (index >= 0) {
2725 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2726 if (chain->removeEffect_l(effect) == 0) {
2727 ALOGV("updateOrphanEffectChains removing effect chain at index %d", index);
2728 mOrphanEffectChains.removeItemsAt(index);
2729 }
2730 return true;
2731 }
2732 return false;
2733}
2734
2735
Glenn Kastenda6ef132013-01-10 12:31:01 -08002736struct Entry {
2737#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2738 char mName[MAX_NAME];
2739};
2740
2741int comparEntry(const void *p1, const void *p2)
2742{
2743 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2744}
2745
Glenn Kasten46909e72013-02-26 09:20:22 -08002746#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002747void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002748{
Eric Laurent81784c32012-11-19 14:55:58 -08002749 NBAIO_Source *teeSource = source.get();
2750 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002751 // .wav rotation
2752 // There is a benign race condition if 2 threads call this simultaneously.
2753 // They would both traverse the directory, but the result would simply be
2754 // failures at unlink() which are ignored. It's also unlikely since
2755 // normally dumpsys is only done by bugreport or from the command line.
2756 char teePath[32+256];
2757 strcpy(teePath, "/data/misc/media");
2758 size_t teePathLen = strlen(teePath);
2759 DIR *dir = opendir(teePath);
2760 teePath[teePathLen++] = '/';
2761 if (dir != NULL) {
2762#define MAX_SORT 20 // number of entries to sort
2763#define MAX_KEEP 10 // number of entries to keep
2764 struct Entry entries[MAX_SORT];
2765 size_t entryCount = 0;
2766 while (entryCount < MAX_SORT) {
2767 struct dirent de;
2768 struct dirent *result = NULL;
2769 int rc = readdir_r(dir, &de, &result);
2770 if (rc != 0) {
2771 ALOGW("readdir_r failed %d", rc);
2772 break;
2773 }
2774 if (result == NULL) {
2775 break;
2776 }
2777 if (result != &de) {
2778 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2779 break;
2780 }
2781 // ignore non .wav file entries
2782 size_t nameLen = strlen(de.d_name);
2783 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2784 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2785 continue;
2786 }
2787 strcpy(entries[entryCount++].mName, de.d_name);
2788 }
2789 (void) closedir(dir);
2790 if (entryCount > MAX_KEEP) {
2791 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2792 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2793 strcpy(&teePath[teePathLen], entries[i].mName);
2794 (void) unlink(teePath);
2795 }
2796 }
2797 } else {
2798 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002799 dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002800 }
2801 }
Eric Laurent81784c32012-11-19 14:55:58 -08002802 char teeTime[16];
2803 struct timeval tv;
2804 gettimeofday(&tv, NULL);
2805 struct tm tm;
2806 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002807 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2808 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2809 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2810 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002811 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07002812 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08002813 char wavHeader[44];
2814 memcpy(wavHeader,
2815 "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",
2816 sizeof(wavHeader));
2817 NBAIO_Format format = teeSource->format();
2818 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002819 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07002820 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002821 wavHeader[22] = channelCount; // number of channels
2822 wavHeader[24] = sampleRate; // sample rate
2823 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07002824 wavHeader[32] = frameSize; // block alignment
2825 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08002826 write(teeFd, wavHeader, sizeof(wavHeader));
2827 size_t total = 0;
2828 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07002829#define TEE_SINK_READ 1024 // frames per I/O operation
2830 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002831 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08002832 size_t count = TEE_SINK_READ;
2833 ssize_t actual = teeSource->read(buffer, count,
2834 AudioBufferProvider::kInvalidPTS);
2835 bool wasFirstRead = firstRead;
2836 firstRead = false;
2837 if (actual <= 0) {
2838 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2839 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002840 }
Eric Laurent81784c32012-11-19 14:55:58 -08002841 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002842 }
Eric Laurent81784c32012-11-19 14:55:58 -08002843 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07002844 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002845 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002846 }
Glenn Kasten329f6512014-08-28 16:23:16 -07002847 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08002848 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002849 uint32_t temp = 44 + total * frameSize - 8;
2850 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002851 write(teeFd, &temp, sizeof(temp));
2852 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002853 temp = total * frameSize;
2854 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002855 write(teeFd, &temp, sizeof(temp));
2856 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002857 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002858 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002859 }
Eric Laurent59255e42011-07-27 19:49:51 -07002860 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002861 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002862 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002863 }
Eric Laurent59255e42011-07-27 19:49:51 -07002864 }
2865 }
2866}
Glenn Kasten46909e72013-02-26 09:20:22 -08002867#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002868
Mathias Agopian65ab4712010-07-14 17:59:35 -07002869// ----------------------------------------------------------------------------
2870
2871status_t AudioFlinger::onTransact(
2872 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2873{
2874 return BnAudioFlinger::onTransact(code, data, reply, flags);
2875}
2876
Mathias Agopian65ab4712010-07-14 17:59:35 -07002877}; // namespace android