blob: f3206cb71e6dc12500f9bcac020b797981526506 [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 Kastenb187de12014-12-30 08:18:15 -0800188 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters",
189 MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800190 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700191
Glenn Kasten46909e72013-02-26 09:20:22 -0800192#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800193 (void) property_get("ro.debuggable", value, "0");
194 int debuggable = atoi(value);
195 int teeEnabled = 0;
196 if (debuggable) {
197 (void) property_get("af.tee", value, "0");
198 teeEnabled = atoi(value);
199 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800200 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700201 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800202 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700203 }
204 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800205 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700206 }
207 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800208 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700209 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800210#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700211}
212
213void AudioFlinger::onFirstRef()
214{
Dima Zavin799a70e2011-04-18 16:57:27 -0700215 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700216
Eric Laurent93575202011-01-18 18:39:02 -0800217 Mutex::Autolock _l(mLock);
218
Dima Zavin799a70e2011-04-18 16:57:27 -0700219 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800220 char val_str[PROPERTY_VALUE_MAX] = { 0 };
221 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
222 uint32_t int_val;
223 if (1 == sscanf(val_str, "%u", &int_val)) {
224 mStandbyTimeInNsecs = milliseconds(int_val);
225 ALOGI("Using %u mSec as standby time.", int_val);
226 } else {
227 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
228 ALOGI("Using default %u mSec as standby time.",
229 (uint32_t)(mStandbyTimeInNsecs / 1000000));
230 }
231 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700232
Eric Laurent1c333e22014-05-20 10:48:17 -0700233 mPatchPanel = new PatchPanel(this);
234
Eric Laurenta4c5a552012-03-29 10:12:40 -0700235 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700236}
237
238AudioFlinger::~AudioFlinger()
239{
240 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700241 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700242 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700243 }
244 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700245 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700246 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700247 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700248
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800249 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
250 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700251 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
252 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700253 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700254
255 // Tell media.log service about any old writers that still need to be unregistered
256 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
257 if (binder != 0) {
258 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
259 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
260 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
261 mUnregisteredWriters.pop();
262 mediaLogService->unregisterWriter(iMemory);
263 }
264 }
265
Mathias Agopian65ab4712010-07-14 17:59:35 -0700266}
267
Eric Laurenta4c5a552012-03-29 10:12:40 -0700268static const char * const audio_interfaces[] = {
269 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
270 AUDIO_HARDWARE_MODULE_ID_A2DP,
271 AUDIO_HARDWARE_MODULE_ID_USB,
272};
273#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
274
Phil Burk062e67a2015-02-11 13:40:50 -0800275AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700276 audio_module_handle_t module,
277 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700278{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700279 // if module is 0, the request comes from an old policy manager and we should load
280 // well known modules
281 if (module == 0) {
282 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
283 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
284 loadHwModule_l(audio_interfaces[i]);
285 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700286 // then try to find a module supporting the requested device.
287 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
288 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
289 audio_hw_device_t *dev = audioHwDevice->hwDevice();
290 if ((dev->get_supported_devices != NULL) &&
291 (dev->get_supported_devices(dev) & devices) == devices)
292 return audioHwDevice;
293 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700294 } else {
295 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700296 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
297 if (audioHwDevice != NULL) {
298 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700299 }
300 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700301
Dima Zavin799a70e2011-04-18 16:57:27 -0700302 return NULL;
303}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700304
Glenn Kasten0f11b512014-01-31 16:18:54 -0800305void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700306{
307 const size_t SIZE = 256;
308 char buffer[SIZE];
309 String8 result;
310
311 result.append("Clients:\n");
312 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800313 sp<Client> client = mClients.valueAt(i).promote();
314 if (client != 0) {
315 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
316 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700317 }
318 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700319
Eric Laurentd1b28d42013-09-18 18:47:13 -0700320 result.append("Notification Clients:\n");
321 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
322 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
323 result.append(buffer);
324 }
325
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700326 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800327 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700328 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
329 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800330 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700331 result.append(buffer);
332 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700333 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700334}
335
336
Glenn Kasten0f11b512014-01-31 16:18:54 -0800337void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338{
339 const size_t SIZE = 256;
340 char buffer[SIZE];
341 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800342 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700343
John Grossman4ff14ba2012-02-08 16:37:41 -0800344 snprintf(buffer, SIZE, "Hardware status: %d\n"
345 "Standby Time mSec: %u\n",
346 hardwareStatus,
347 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700348 result.append(buffer);
349 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700350}
351
Glenn Kasten0f11b512014-01-31 16:18:54 -0800352void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700353{
354 const size_t SIZE = 256;
355 char buffer[SIZE];
356 String8 result;
357 snprintf(buffer, SIZE, "Permission Denial: "
358 "can't dump AudioFlinger from pid=%d, uid=%d\n",
359 IPCThreadState::self()->getCallingPid(),
360 IPCThreadState::self()->getCallingUid());
361 result.append(buffer);
362 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700363}
364
Eric Laurent81784c32012-11-19 14:55:58 -0800365bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700366{
367 bool locked = false;
368 for (int i = 0; i < kDumpLockRetries; ++i) {
369 if (mutex.tryLock() == NO_ERROR) {
370 locked = true;
371 break;
372 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800373 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700374 }
375 return locked;
376}
377
378status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
379{
Glenn Kasten44deb052012-02-05 18:09:08 -0800380 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700381 dumpPermissionDenial(fd, args);
382 } else {
383 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800384 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385 if (!hardwareLocked) {
386 String8 result(kHardwareLockedString);
387 write(fd, result.string(), result.size());
388 } else {
389 mHardwareLock.unlock();
390 }
391
Eric Laurent81784c32012-11-19 14:55:58 -0800392 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393
394 // failed to lock - AudioFlinger is probably deadlocked
395 if (!locked) {
396 String8 result(kDeadlockedString);
397 write(fd, result.string(), result.size());
398 }
399
Eric Laurent021cf962014-05-13 10:18:14 -0700400 bool clientLocked = dumpTryLock(mClientLock);
401 if (!clientLocked) {
402 String8 result(kClientLockedString);
403 write(fd, result.string(), result.size());
404 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700405
406 EffectDumpEffects(fd);
407
Mathias Agopian65ab4712010-07-14 17:59:35 -0700408 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700409 if (clientLocked) {
410 mClientLock.unlock();
411 }
412
Mathias Agopian65ab4712010-07-14 17:59:35 -0700413 dumpInternals(fd, args);
414
415 // dump playback threads
416 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
417 mPlaybackThreads.valueAt(i)->dump(fd, args);
418 }
419
420 // dump record threads
421 for (size_t i = 0; i < mRecordThreads.size(); i++) {
422 mRecordThreads.valueAt(i)->dump(fd, args);
423 }
424
Eric Laurentaaa44472014-09-12 17:41:50 -0700425 // dump orphan effect chains
426 if (mOrphanEffectChains.size() != 0) {
427 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
428 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
429 mOrphanEffectChains.valueAt(i)->dump(fd, args);
430 }
431 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700432 // dump all hardware devs
433 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700434 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700435 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700436 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700437
Glenn Kasten46909e72013-02-26 09:20:22 -0800438#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700439 // dump the serially shared record tee sink
440 if (mRecordTeeSource != 0) {
441 dumpTee(fd, mRecordTeeSource);
442 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800443#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700444
Glenn Kastend65d73c2012-06-22 17:21:07 -0700445 if (locked) {
446 mLock.unlock();
447 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800448
449 // append a copy of media.log here by forwarding fd to it, but don't attempt
450 // to lookup the service if it's not running, as it will block for a second
451 if (mLogMemoryDealer != 0) {
452 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
453 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700454 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800455 Vector<String16> args;
456 binder->dump(fd, args);
457 }
458 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700459 }
460 return NO_ERROR;
461}
462
Eric Laurent021cf962014-05-13 10:18:14 -0700463sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800464{
Eric Laurent021cf962014-05-13 10:18:14 -0700465 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800466 // If pid is already in the mClients wp<> map, then use that entry
467 // (for which promote() is always != 0), otherwise create a new entry and Client.
468 sp<Client> client = mClients.valueFor(pid).promote();
469 if (client == 0) {
470 client = new Client(this, pid);
471 mClients.add(pid, client);
472 }
473
474 return client;
475}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700476
Glenn Kasten9e58b552013-01-18 15:09:48 -0800477sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
478{
Glenn Kasten481fb672013-09-30 14:39:28 -0700479 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800480 if (mLogMemoryDealer == 0) {
481 return new NBLog::Writer();
482 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800483 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700484 // Similarly if we can't contact the media.log service, also return a dummy writer
485 if (binder == 0) {
486 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800487 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700488 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
489 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
490 // If allocation fails, consult the vector of previously unregistered writers
491 // and garbage-collect one or more them until an allocation succeeds
492 if (shared == 0) {
493 Mutex::Autolock _l(mUnregisteredWritersLock);
494 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
495 {
496 // Pick the oldest stale writer to garbage-collect
497 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
498 mUnregisteredWriters.removeAt(0);
499 mediaLogService->unregisterWriter(iMemory);
500 // Now the media.log remote reference to IMemory is gone. When our last local
501 // reference to IMemory also drops to zero at end of this block,
502 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
503 }
504 // Re-attempt the allocation
505 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
506 if (shared != 0) {
507 goto success;
508 }
509 }
510 // Even after garbage-collecting all old writers, there is still not enough memory,
511 // so return a dummy writer
512 return new NBLog::Writer();
513 }
514success:
515 mediaLogService->registerWriter(shared, size, name);
516 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800517}
518
519void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
520{
Glenn Kasten685ef092013-02-04 08:15:34 -0800521 if (writer == 0) {
522 return;
523 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800524 sp<IMemory> iMemory(writer->getIMemory());
525 if (iMemory == 0) {
526 return;
527 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700528 // Rather than removing the writer immediately, append it to a queue of old writers to
529 // be garbage-collected later. This allows us to continue to view old logs for a while.
530 Mutex::Autolock _l(mUnregisteredWritersLock);
531 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800532}
533
Mathias Agopian65ab4712010-07-14 17:59:35 -0700534// IAudioFlinger interface
535
536
537sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800538 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700539 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800540 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700541 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800542 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800543 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700544 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800545 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800546 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700547 int *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800548 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700549 status_t *status)
550{
551 sp<PlaybackThread::Track> track;
552 sp<TrackHandle> trackHandle;
553 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700554 status_t lStatus;
555 int lSessionId;
556
Glenn Kasten263709e2012-01-06 08:40:01 -0800557 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
558 // but if someone uses binder directly they could bypass that and cause us to crash
559 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000560 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700561 lStatus = BAD_VALUE;
562 goto Exit;
563 }
564
Glenn Kasten53b5d092014-02-05 10:00:23 -0800565 // further sample rate checks are performed by createTrack_l() depending on the thread type
566 if (sampleRate == 0) {
567 ALOGE("createTrack() invalid sample rate %u", sampleRate);
568 lStatus = BAD_VALUE;
569 goto Exit;
570 }
571
572 // further channel mask checks are performed by createTrack_l() depending on the thread type
573 if (!audio_is_output_channel(channelMask)) {
574 ALOGE("createTrack() invalid channel mask %#x", channelMask);
575 lStatus = BAD_VALUE;
576 goto Exit;
577 }
578
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700579 // further format checks are performed by createTrack_l() depending on the thread type
580 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800581 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700582 lStatus = BAD_VALUE;
583 goto Exit;
584 }
585
Glenn Kasten663c2242013-09-24 11:52:37 -0700586 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
587 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
588 lStatus = BAD_VALUE;
589 goto Exit;
590 }
591
Mathias Agopian65ab4712010-07-14 17:59:35 -0700592 {
593 Mutex::Autolock _l(mLock);
594 PlaybackThread *thread = checkPlaybackThread_l(output);
595 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800596 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700597 lStatus = BAD_VALUE;
598 goto Exit;
599 }
600
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800601 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -0700602 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700603
Glenn Kastene848bd92014-03-13 15:00:32 -0700604 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700605 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700606 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700607 // check if an effect chain with the same session ID is present on another
608 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700609 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700610 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
611 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700612 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700613 if (sessions & PlaybackThread::EFFECT_SESSION) {
614 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700615 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700616 }
Eric Laurentde070132010-07-13 04:45:46 -0700617 }
618 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700619 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700620 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700621 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700622 if (sessionId != NULL) {
623 *sessionId = lSessionId;
624 }
625 }
Steve Block3856b092011-10-20 11:56:00 +0100626 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700627
628 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800629 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800630 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700631 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700632
633 // move effect chain to this output thread if an effect on same session was waiting
634 // for a track to be created
635 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700636 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700637 Mutex::Autolock _dl(thread->mLock);
638 Mutex::Autolock _sl(effectThread->mLock);
639 moveEffectChain_l(lSessionId, effectThread, thread, true);
640 }
Eric Laurenta011e352012-03-29 15:51:43 -0700641
642 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700643 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700644 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
645 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700646 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700647 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700648 } else {
649 mPendingSyncEvents[i]->cancel();
650 }
Eric Laurenta011e352012-03-29 15:51:43 -0700651 mPendingSyncEvents.removeAt(i);
652 i--;
653 }
654 }
655 }
Glenn Kastene198c362013-08-13 09:13:36 -0700656
Eric Laurentfa90e842014-10-17 18:12:31 -0700657 setAudioHwSyncForSession_l(thread, (audio_session_t)lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700658 }
Glenn Kastene198c362013-08-13 09:13:36 -0700659
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700660 if (lStatus != NO_ERROR) {
661 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700662 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700663 // Don't hold mClientLock when releasing the reference on the track as the
664 // destructor will acquire it.
665 {
666 Mutex::Autolock _cl(mClientLock);
667 client.clear();
668 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700669 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700670 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700671 }
672
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700673 // return handle to client
674 trackHandle = new TrackHandle(track);
675
Mathias Agopian65ab4712010-07-14 17:59:35 -0700676Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700677 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678 return trackHandle;
679}
680
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800681uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700682{
683 Mutex::Autolock _l(mLock);
684 PlaybackThread *thread = checkPlaybackThread_l(output);
685 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000686 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700687 return 0;
688 }
689 return thread->sampleRate();
690}
691
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800692audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700693{
694 Mutex::Autolock _l(mLock);
695 PlaybackThread *thread = checkPlaybackThread_l(output);
696 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000697 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800698 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699 }
700 return thread->format();
701}
702
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800703size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704{
705 Mutex::Autolock _l(mLock);
706 PlaybackThread *thread = checkPlaybackThread_l(output);
707 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000708 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700709 return 0;
710 }
Glenn Kasten58912562012-04-03 10:45:00 -0700711 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
712 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700713 return thread->frameCount();
714}
715
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800716uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700717{
718 Mutex::Autolock _l(mLock);
719 PlaybackThread *thread = checkPlaybackThread_l(output);
720 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800721 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700722 return 0;
723 }
724 return thread->latency();
725}
726
727status_t AudioFlinger::setMasterVolume(float value)
728{
Eric Laurenta1884f92011-08-23 08:25:03 -0700729 status_t ret = initCheck();
730 if (ret != NO_ERROR) {
731 return ret;
732 }
733
Mathias Agopian65ab4712010-07-14 17:59:35 -0700734 // check calling permissions
735 if (!settingsAllowed()) {
736 return PERMISSION_DENIED;
737 }
738
Eric Laurenta4c5a552012-03-29 10:12:40 -0700739 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700740 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700741
John Grossmanee578c02012-07-23 17:05:46 -0700742 // Set master volume in the HALs which support it.
743 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
744 AutoMutex lock(mHardwareLock);
745 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800746
John Grossmanee578c02012-07-23 17:05:46 -0700747 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
748 if (dev->canSetMasterVolume()) {
749 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800750 }
John Grossmanee578c02012-07-23 17:05:46 -0700751 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700752 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700753
John Grossmanee578c02012-07-23 17:05:46 -0700754 // Now set the master volume in each playback thread. Playback threads
755 // assigned to HALs which do not have master volume support will apply
756 // master volume during the mix operation. Threads with HALs which do
757 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800758 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700759 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700760
761 return NO_ERROR;
762}
763
Glenn Kastenf78aee72012-01-04 11:00:47 -0800764status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700765{
Eric Laurenta1884f92011-08-23 08:25:03 -0700766 status_t ret = initCheck();
767 if (ret != NO_ERROR) {
768 return ret;
769 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700770
771 // check calling permissions
772 if (!settingsAllowed()) {
773 return PERMISSION_DENIED;
774 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800775 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000776 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700777 return BAD_VALUE;
778 }
779
780 { // scope for the lock
781 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700782 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700783 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700784 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700785 mHardwareStatus = AUDIO_HW_IDLE;
786 }
787
788 if (NO_ERROR == ret) {
789 Mutex::Autolock _l(mLock);
790 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800791 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700792 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700793 }
794
795 return ret;
796}
797
798status_t AudioFlinger::setMicMute(bool state)
799{
Eric Laurenta1884f92011-08-23 08:25:03 -0700800 status_t ret = initCheck();
801 if (ret != NO_ERROR) {
802 return ret;
803 }
804
Mathias Agopian65ab4712010-07-14 17:59:35 -0700805 // check calling permissions
806 if (!settingsAllowed()) {
807 return PERMISSION_DENIED;
808 }
809
810 AutoMutex lock(mHardwareLock);
811 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700812 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
813 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
814 status_t result = dev->set_mic_mute(dev, state);
815 if (result != NO_ERROR) {
816 ret = result;
817 }
818 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819 mHardwareStatus = AUDIO_HW_IDLE;
820 return ret;
821}
822
823bool AudioFlinger::getMicMute() const
824{
Eric Laurenta1884f92011-08-23 08:25:03 -0700825 status_t ret = initCheck();
826 if (ret != NO_ERROR) {
827 return false;
828 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800829 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700830 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800831 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700832 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800833 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
834 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
835 status_t result = dev->get_mic_mute(dev, &state);
836 if (result == NO_ERROR) {
837 mute = mute && state;
838 }
839 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700840 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800841
842 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700843}
844
845status_t AudioFlinger::setMasterMute(bool muted)
846{
John Grossmand8f178d2012-07-20 14:51:35 -0700847 status_t ret = initCheck();
848 if (ret != NO_ERROR) {
849 return ret;
850 }
851
Mathias Agopian65ab4712010-07-14 17:59:35 -0700852 // check calling permissions
853 if (!settingsAllowed()) {
854 return PERMISSION_DENIED;
855 }
856
John Grossmanee578c02012-07-23 17:05:46 -0700857 Mutex::Autolock _l(mLock);
858 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700859
John Grossmanee578c02012-07-23 17:05:46 -0700860 // Set master mute in the HALs which support it.
861 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
862 AutoMutex lock(mHardwareLock);
863 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700864
John Grossmanee578c02012-07-23 17:05:46 -0700865 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
866 if (dev->canSetMasterMute()) {
867 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700868 }
John Grossmanee578c02012-07-23 17:05:46 -0700869 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700870 }
871
John Grossmanee578c02012-07-23 17:05:46 -0700872 // Now set the master mute in each playback thread. Playback threads
873 // assigned to HALs which do not have master mute support will apply master
874 // mute during the mix operation. Threads with HALs which do support master
875 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800876 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700877 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700878
879 return NO_ERROR;
880}
881
882float AudioFlinger::masterVolume() const
883{
Glenn Kasten98067102011-12-13 11:47:54 -0800884 Mutex::Autolock _l(mLock);
885 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700886}
887
888bool AudioFlinger::masterMute() const
889{
Glenn Kasten98067102011-12-13 11:47:54 -0800890 Mutex::Autolock _l(mLock);
891 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700892}
893
John Grossman4ff14ba2012-02-08 16:37:41 -0800894float AudioFlinger::masterVolume_l() const
895{
John Grossman4ff14ba2012-02-08 16:37:41 -0800896 return mMasterVolume;
897}
898
John Grossmand8f178d2012-07-20 14:51:35 -0700899bool AudioFlinger::masterMute_l() const
900{
John Grossmanee578c02012-07-23 17:05:46 -0700901 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700902}
903
Eric Laurent223fd5c2014-11-11 13:43:36 -0800904status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
905{
906 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
907 ALOGW("setStreamVolume() invalid stream %d", stream);
908 return BAD_VALUE;
909 }
910 pid_t caller = IPCThreadState::self()->getCallingPid();
911 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
912 ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
913 return PERMISSION_DENIED;
914 }
915
916 return NO_ERROR;
917}
918
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800919status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
920 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700921{
922 // check calling permissions
923 if (!settingsAllowed()) {
924 return PERMISSION_DENIED;
925 }
926
Eric Laurent223fd5c2014-11-11 13:43:36 -0800927 status_t status = checkStreamType(stream);
928 if (status != NO_ERROR) {
929 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700930 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800931 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700932
933 AutoMutex lock(mLock);
934 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700935 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700936 thread = checkPlaybackThread_l(output);
937 if (thread == NULL) {
938 return BAD_VALUE;
939 }
940 }
941
942 mStreamTypes[stream].volume = value;
943
944 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800945 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700946 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947 }
948 } else {
949 thread->setStreamVolume(stream, value);
950 }
951
952 return NO_ERROR;
953}
954
Glenn Kastenfff6d712012-01-12 16:38:12 -0800955status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700956{
957 // check calling permissions
958 if (!settingsAllowed()) {
959 return PERMISSION_DENIED;
960 }
961
Eric Laurent223fd5c2014-11-11 13:43:36 -0800962 status_t status = checkStreamType(stream);
963 if (status != NO_ERROR) {
964 return status;
965 }
966 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
967
968 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000969 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700970 return BAD_VALUE;
971 }
972
Eric Laurent93575202011-01-18 18:39:02 -0800973 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700974 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700975 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700976 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700977
978 return NO_ERROR;
979}
980
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800981float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700982{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800983 status_t status = checkStreamType(stream);
984 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700985 return 0.0f;
986 }
987
988 AutoMutex lock(mLock);
989 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -0700990 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700991 PlaybackThread *thread = checkPlaybackThread_l(output);
992 if (thread == NULL) {
993 return 0.0f;
994 }
995 volume = thread->streamVolume(stream);
996 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800997 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700998 }
999
1000 return volume;
1001}
1002
Glenn Kastenfff6d712012-01-12 16:38:12 -08001003bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001004{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001005 status_t status = checkStreamType(stream);
1006 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001007 return true;
1008 }
1009
Glenn Kasten6637baa2012-01-09 09:40:36 -08001010 AutoMutex lock(mLock);
1011 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001012}
1013
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001014status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001015{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001016 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1017 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001018
Mathias Agopian65ab4712010-07-14 17:59:35 -07001019 // check calling permissions
1020 if (!settingsAllowed()) {
1021 return PERMISSION_DENIED;
1022 }
1023
Glenn Kasten142f5192014-03-25 17:44:59 -07001024 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1025 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001026 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -07001027 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001028 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001029 AutoMutex lock(mHardwareLock);
1030 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1031 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1032 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1033 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1034 final_result = result ?: final_result;
1035 }
1036 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001037 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001038 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1039 AudioParameter param = AudioParameter(keyValuePairs);
1040 String8 value;
1041 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001042 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1043 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001044 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1045 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001046 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001047 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1048 // collect all of the thread's session IDs
1049 KeyedVector<int, bool> ids = thread->sessionIds();
1050 // suspend effects associated with those session IDs
1051 for (size_t j = 0; j < ids.size(); ++j) {
1052 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001053 thread->setEffectSuspended(FX_IID_AEC,
1054 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001055 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001056 thread->setEffectSuspended(FX_IID_NS,
1057 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001058 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001059 }
1060 }
Eric Laurentbee53372011-08-29 12:42:48 -07001061 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001062 }
1063 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001064 String8 screenState;
1065 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1066 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001067 if (isOff != (AudioFlinger::mScreenState & 1)) {
1068 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001069 }
1070 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001071 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001072 }
1073
1074 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1075 // and the thread is exited once the lock is released
1076 sp<ThreadBase> thread;
1077 {
1078 Mutex::Autolock _l(mLock);
1079 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001080 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001081 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001082 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001083 // indicate output device change to all input threads for pre processing
1084 AudioParameter param = AudioParameter(keyValuePairs);
1085 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001086 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1087 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001088 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1089 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1090 }
1091 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001092 }
1093 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001094 if (thread != 0) {
1095 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001096 }
1097 return BAD_VALUE;
1098}
1099
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001100String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001101{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001102 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1103 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001104
Eric Laurenta4c5a552012-03-29 10:12:40 -07001105 Mutex::Autolock _l(mLock);
1106
Glenn Kasten142f5192014-03-25 17:44:59 -07001107 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001108 String8 out_s8;
1109
Dima Zavin799a70e2011-04-18 16:57:27 -07001110 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001111 char *s;
1112 {
1113 AutoMutex lock(mHardwareLock);
1114 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001115 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001116 s = dev->get_parameters(dev, keys.string());
1117 mHardwareStatus = AUDIO_HW_IDLE;
1118 }
John Grossmanef7740b2012-02-09 11:28:36 -08001119 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001120 free(s);
1121 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001122 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001123 }
1124
Mathias Agopian65ab4712010-07-14 17:59:35 -07001125 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1126 if (playbackThread != NULL) {
1127 return playbackThread->getParameters(keys);
1128 }
1129 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1130 if (recordThread != NULL) {
1131 return recordThread->getParameters(keys);
1132 }
1133 return String8("");
1134}
1135
Glenn Kastendd8104c2012-07-02 12:42:44 -07001136size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1137 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001138{
Eric Laurenta1884f92011-08-23 08:25:03 -07001139 status_t ret = initCheck();
1140 if (ret != NO_ERROR) {
1141 return 0;
1142 }
1143
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001144 AutoMutex lock(mHardwareLock);
1145 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001146 audio_config_t config;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001147 memset(&config, 0, sizeof(config));
1148 config.sample_rate = sampleRate;
1149 config.channel_mask = channelMask;
1150 config.format = format;
1151
John Grossmanee578c02012-07-23 17:05:46 -07001152 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1153 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001154 mHardwareStatus = AUDIO_HW_IDLE;
1155 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001156}
1157
Glenn Kasten5f972c02014-01-13 09:59:31 -08001158uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001159{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001160 Mutex::Autolock _l(mLock);
1161
1162 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1163 if (recordThread != NULL) {
1164 return recordThread->getInputFramesLost();
1165 }
1166 return 0;
1167}
1168
1169status_t AudioFlinger::setVoiceVolume(float value)
1170{
Eric Laurenta1884f92011-08-23 08:25:03 -07001171 status_t ret = initCheck();
1172 if (ret != NO_ERROR) {
1173 return ret;
1174 }
1175
Mathias Agopian65ab4712010-07-14 17:59:35 -07001176 // check calling permissions
1177 if (!settingsAllowed()) {
1178 return PERMISSION_DENIED;
1179 }
1180
1181 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001182 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001183 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001184 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001185 mHardwareStatus = AUDIO_HW_IDLE;
1186
1187 return ret;
1188}
1189
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001190status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001191 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001192{
1193 status_t status;
1194
1195 Mutex::Autolock _l(mLock);
1196
1197 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1198 if (playbackThread != NULL) {
1199 return playbackThread->getRenderPosition(halFrames, dspFrames);
1200 }
1201
1202 return BAD_VALUE;
1203}
1204
1205void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1206{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001207 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001208 if (client == 0) {
1209 return;
1210 }
Eric Laurent021cf962014-05-13 10:18:14 -07001211 bool clientAdded = false;
1212 {
1213 Mutex::Autolock _cl(mClientLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001214
Eric Laurent021cf962014-05-13 10:18:14 -07001215 pid_t pid = IPCThreadState::self()->getCallingPid();
1216 if (mNotificationClients.indexOfKey(pid) < 0) {
1217 sp<NotificationClient> notificationClient = new NotificationClient(this,
1218 client,
1219 pid);
1220 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001221
Eric Laurent021cf962014-05-13 10:18:14 -07001222 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001223
Marco Nelissen06b46062014-11-14 07:58:25 -08001224 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001225 binder->linkToDeath(notificationClient);
1226 clientAdded = true;
1227 }
1228 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001229
Eric Laurent021cf962014-05-13 10:18:14 -07001230 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001231 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent021cf962014-05-13 10:18:14 -07001232 if (clientAdded) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001233 // the config change is always sent from playback or record threads to avoid deadlock
1234 // with AudioSystem::gLock
1235 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001236 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001237 }
1238
1239 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001240 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001241 }
1242 }
1243}
1244
1245void AudioFlinger::removeNotificationClient(pid_t pid)
1246{
1247 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001248 {
1249 Mutex::Autolock _cl(mClientLock);
1250 mNotificationClients.removeItem(pid);
1251 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001252
Steve Block3856b092011-10-20 11:56:00 +01001253 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001254 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001255 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001256 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001257 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001258 ALOGV(" pid %d @ %d", ref->mPid, i);
1259 if (ref->mPid == pid) {
1260 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001261 mAudioSessionRefs.removeAt(i);
1262 delete ref;
1263 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001264 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001265 } else {
1266 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001267 }
1268 }
1269 if (removed) {
1270 purgeStaleEffects_l();
1271 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001272}
1273
Eric Laurent021cf962014-05-13 10:18:14 -07001274void AudioFlinger::audioConfigChanged(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001275{
Eric Laurent021cf962014-05-13 10:18:14 -07001276 Mutex::Autolock _l(mClientLock);
1277 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001278 for (size_t i = 0; i < size; i++) {
Eric Laurent021cf962014-05-13 10:18:14 -07001279 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event,
Eric Laurent10351942014-05-08 18:49:52 -07001280 ioHandle,
1281 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001282 }
1283}
1284
Eric Laurent021cf962014-05-13 10:18:14 -07001285// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001286void AudioFlinger::removeClient_l(pid_t pid)
1287{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001288 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001289 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001290 mClients.removeItem(pid);
1291}
1292
Eric Laurent717e1282012-06-29 16:36:52 -07001293// getEffectThread_l() must be called with AudioFlinger::mLock held
1294sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1295{
1296 sp<PlaybackThread> thread;
1297
1298 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1299 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1300 ALOG_ASSERT(thread == 0);
1301 thread = mPlaybackThreads.valueAt(i);
1302 }
1303 }
1304
1305 return thread;
1306}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001307
Mathias Agopian65ab4712010-07-14 17:59:35 -07001308
Mathias Agopian65ab4712010-07-14 17:59:35 -07001309
1310// ----------------------------------------------------------------------------
1311
1312AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1313 : RefBase(),
1314 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001315 // 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 -07001316 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001317 mPid(pid),
1318 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001319{
1320 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1321}
1322
Eric Laurent021cf962014-05-13 10:18:14 -07001323// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001324AudioFlinger::Client::~Client()
1325{
1326 mAudioFlinger->removeClient_l(mPid);
1327}
1328
Glenn Kasten435dbe62012-01-30 10:15:48 -08001329sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001330{
1331 return mMemoryDealer;
1332}
1333
John Grossman4ff14ba2012-02-08 16:37:41 -08001334// Reserve one of the limited slots for a timed audio track associated
1335// with this client
1336bool AudioFlinger::Client::reserveTimedTrack()
1337{
1338 const int kMaxTimedTracksPerClient = 4;
1339
1340 Mutex::Autolock _l(mTimedTrackLock);
1341
1342 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1343 ALOGW("can not create timed track - pid %d has exceeded the limit",
1344 mPid);
1345 return false;
1346 }
1347
1348 mTimedTrackCount++;
1349 return true;
1350}
1351
1352// Release a slot for a timed audio track
1353void AudioFlinger::Client::releaseTimedTrack()
1354{
1355 Mutex::Autolock _l(mTimedTrackLock);
1356 mTimedTrackCount--;
1357}
1358
Mathias Agopian65ab4712010-07-14 17:59:35 -07001359// ----------------------------------------------------------------------------
1360
1361AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1362 const sp<IAudioFlingerClient>& client,
1363 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001364 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001365{
1366}
1367
1368AudioFlinger::NotificationClient::~NotificationClient()
1369{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001370}
1371
Glenn Kasten0f11b512014-01-31 16:18:54 -08001372void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001373{
1374 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001375 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001376}
1377
Mathias Agopian65ab4712010-07-14 17:59:35 -07001378
1379// ----------------------------------------------------------------------------
1380
Jeff Brown893a5642013-08-16 20:19:26 -07001381static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1382 return audio_is_remote_submix_device(inDevice);
1383}
1384
Mathias Agopian65ab4712010-07-14 17:59:35 -07001385sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001386 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001387 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001388 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001389 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001390 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001391 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001392 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001393 int *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001394 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001395 sp<IMemory>& cblk,
1396 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001397 status_t *status)
1398{
1399 sp<RecordThread::RecordTrack> recordTrack;
1400 sp<RecordHandle> recordHandle;
1401 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001402 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001403 int lSessionId;
1404
Glenn Kastend776ac62014-05-07 09:16:09 -07001405 cblk.clear();
1406 buffers.clear();
1407
Mathias Agopian65ab4712010-07-14 17:59:35 -07001408 // check calling permissions
1409 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001410 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001411 lStatus = PERMISSION_DENIED;
1412 goto Exit;
1413 }
1414
Glenn Kasten53b5d092014-02-05 10:00:23 -08001415 // further sample rate checks are performed by createRecordTrack_l()
1416 if (sampleRate == 0) {
1417 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1418 lStatus = BAD_VALUE;
1419 goto Exit;
1420 }
1421
Glenn Kasten0e0e8462014-03-13 15:02:40 -07001422 // we don't yet support anything other than 16-bit PCM
1423 if (!(audio_is_valid_format(format) &&
1424 audio_is_linear_pcm(format) && format == AUDIO_FORMAT_PCM_16_BIT)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001425 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001426 lStatus = BAD_VALUE;
1427 goto Exit;
1428 }
1429
Glenn Kasten53b5d092014-02-05 10:00:23 -08001430 // further channel mask checks are performed by createRecordTrack_l()
1431 if (!audio_is_input_channel(channelMask)) {
1432 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1433 lStatus = BAD_VALUE;
1434 goto Exit;
1435 }
1436
Glenn Kasten05997e22014-03-13 15:08:33 -07001437 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001438 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001439 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001440 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001441 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001442 lStatus = BAD_VALUE;
1443 goto Exit;
1444 }
1445
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001446 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001447 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001448
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001449 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001450 lSessionId = *sessionId;
1451 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001452 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001453 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001454 if (sessionId != NULL) {
1455 *sessionId = lSessionId;
1456 }
1457 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001458 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001459
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001460 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001461 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001462 frameCount, lSessionId, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001463 IPCThreadState::self()->getCallingUid(),
1464 flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001465 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001466
1467 if (lStatus == NO_ERROR) {
1468 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1469 // session and move it to this thread.
1470 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)lSessionId);
1471 if (chain != 0) {
1472 Mutex::Autolock _l(thread->mLock);
1473 thread->addEffectChain_l(chain);
1474 }
1475 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001476 }
Glenn Kastene198c362013-08-13 09:13:36 -07001477
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001478 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001479 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001480 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001481 // Don't hold mClientLock when releasing the reference on the track as the
1482 // destructor will acquire it.
1483 {
1484 Mutex::Autolock _cl(mClientLock);
1485 client.clear();
1486 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001487 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001488 goto Exit;
1489 }
1490
Glenn Kastend776ac62014-05-07 09:16:09 -07001491 cblk = recordTrack->getCblk();
1492 buffers = recordTrack->getBuffers();
1493
Glenn Kasten2fc14732013-08-05 14:58:14 -07001494 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001495 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001496
1497Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001498 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001499 return recordHandle;
1500}
1501
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001502
1503
Mathias Agopian65ab4712010-07-14 17:59:35 -07001504// ----------------------------------------------------------------------------
1505
Eric Laurenta4c5a552012-03-29 10:12:40 -07001506audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1507{
Eric Laurent44622db2014-08-01 19:00:33 -07001508 if (name == NULL) {
1509 return 0;
1510 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001511 if (!settingsAllowed()) {
1512 return 0;
1513 }
1514 Mutex::Autolock _l(mLock);
1515 return loadHwModule_l(name);
1516}
1517
1518// loadHwModule_l() must be called with AudioFlinger::mLock held
1519audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1520{
1521 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1522 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1523 ALOGW("loadHwModule() module %s already loaded", name);
1524 return mAudioHwDevs.keyAt(i);
1525 }
1526 }
1527
Eric Laurenta4c5a552012-03-29 10:12:40 -07001528 audio_hw_device_t *dev;
1529
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001530 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001531 if (rc) {
1532 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1533 return 0;
1534 }
1535
1536 mHardwareStatus = AUDIO_HW_INIT;
1537 rc = dev->init_check(dev);
1538 mHardwareStatus = AUDIO_HW_IDLE;
1539 if (rc) {
1540 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1541 return 0;
1542 }
1543
John Grossmanee578c02012-07-23 17:05:46 -07001544 // Check and cache this HAL's level of support for master mute and master
1545 // volume. If this is the first HAL opened, and it supports the get
1546 // methods, use the initial values provided by the HAL as the current
1547 // master mute and volume settings.
1548
1549 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1550 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001551 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001552
1553 if (0 == mAudioHwDevs.size()) {
1554 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1555 if (NULL != dev->get_master_volume) {
1556 float mv;
1557 if (OK == dev->get_master_volume(dev, &mv)) {
1558 mMasterVolume = mv;
1559 }
1560 }
1561
1562 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1563 if (NULL != dev->get_master_mute) {
1564 bool mm;
1565 if (OK == dev->get_master_mute(dev, &mm)) {
1566 mMasterMute = mm;
1567 }
1568 }
1569 }
1570
Eric Laurenta4c5a552012-03-29 10:12:40 -07001571 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001572 if ((NULL != dev->set_master_volume) &&
1573 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1574 flags = static_cast<AudioHwDevice::Flags>(flags |
1575 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1576 }
1577
1578 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1579 if ((NULL != dev->set_master_mute) &&
1580 (OK == dev->set_master_mute(dev, mMasterMute))) {
1581 flags = static_cast<AudioHwDevice::Flags>(flags |
1582 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1583 }
1584
Eric Laurenta4c5a552012-03-29 10:12:40 -07001585 mHardwareStatus = AUDIO_HW_IDLE;
1586 }
1587
1588 audio_module_handle_t handle = nextUniqueId();
Eric Laurent83b88082014-06-20 18:31:16 -07001589 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001590
1591 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001592 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001593
1594 return handle;
1595
1596}
1597
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001598// ----------------------------------------------------------------------------
1599
Glenn Kasten3b16c762012-11-14 08:44:39 -08001600uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001601{
1602 Mutex::Autolock _l(mLock);
1603 PlaybackThread *thread = primaryPlaybackThread_l();
1604 return thread != NULL ? thread->sampleRate() : 0;
1605}
1606
Glenn Kastene33054e2012-11-14 12:54:39 -08001607size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001608{
1609 Mutex::Autolock _l(mLock);
1610 PlaybackThread *thread = primaryPlaybackThread_l();
1611 return thread != NULL ? thread->frameCountHAL() : 0;
1612}
1613
1614// ----------------------------------------------------------------------------
1615
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001616status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1617{
1618 uid_t uid = IPCThreadState::self()->getCallingUid();
1619 if (uid != AID_SYSTEM) {
1620 return PERMISSION_DENIED;
1621 }
1622 Mutex::Autolock _l(mLock);
1623 if (mIsDeviceTypeKnown) {
1624 return INVALID_OPERATION;
1625 }
1626 mIsLowRamDevice = isLowRamDevice;
1627 mIsDeviceTypeKnown = true;
1628 return NO_ERROR;
1629}
1630
Eric Laurent93c3d412014-08-01 14:48:35 -07001631audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1632{
1633 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001634
1635 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1636 if (index >= 0) {
1637 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1638 mHwAvSyncIds.valueAt(index), sessionId);
1639 return mHwAvSyncIds.valueAt(index);
1640 }
1641
1642 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1643 if (dev == NULL) {
1644 return AUDIO_HW_SYNC_INVALID;
1645 }
1646 char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1647 AudioParameter param = AudioParameter(String8(reply));
1648 free(reply);
1649
1650 int value;
1651 if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1652 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1653 return AUDIO_HW_SYNC_INVALID;
1654 }
1655
1656 // allow only one session for a given HW A/V sync ID.
1657 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1658 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1659 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1660 value, mHwAvSyncIds.keyAt(i));
1661 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001662 break;
1663 }
1664 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001665
1666 mHwAvSyncIds.add(sessionId, value);
1667
1668 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1669 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1670 uint32_t sessions = thread->hasAudioSession(sessionId);
1671 if (sessions & PlaybackThread::TRACK_SESSION) {
1672 AudioParameter param = AudioParameter();
1673 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1674 thread->setParameters(param.toString());
1675 break;
1676 }
1677 }
1678
1679 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1680 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001681}
1682
Eric Laurentfa90e842014-10-17 18:12:31 -07001683// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1684void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1685{
1686 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1687 if (index >= 0) {
1688 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1689 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1690 AudioParameter param = AudioParameter();
1691 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1692 thread->setParameters(param.toString());
1693 }
1694}
1695
1696
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001697// ----------------------------------------------------------------------------
1698
Eric Laurent83b88082014-06-20 18:31:16 -07001699
1700sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001701 audio_io_handle_t *output,
1702 audio_config_t *config,
1703 audio_devices_t devices,
1704 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001705 audio_output_flags_t flags)
1706{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001707 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001708 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001709 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001710 }
1711
1712 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentcf2c0212014-07-25 16:20:43 -07001713 if (*output == AUDIO_IO_HANDLE_NONE) {
1714 *output = nextUniqueId();
1715 }
Eric Laurent83b88082014-06-20 18:31:16 -07001716
1717 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1718
Eric Laurent83b88082014-06-20 18:31:16 -07001719 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001720 // This if statement allows overriding the audio policy settings
1721 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1722 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1723 // Check only for Normal Mixing mode
1724 if (kEnableExtendedPrecision) {
1725 // Specify format (uncomment one below to choose)
1726 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1727 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1728 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1729 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001730 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001731 }
1732 if (kEnableExtendedChannels) {
1733 // Specify channel mask (uncomment one below to choose)
1734 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1735 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1736 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1737 }
Eric Laurent83b88082014-06-20 18:31:16 -07001738 }
1739
Phil Burk062e67a2015-02-11 13:40:50 -08001740 AudioStreamOut *outputStream = NULL;
1741 status_t status = outHwDev->openOutputStream(
1742 &outputStream,
1743 *output,
1744 devices,
1745 flags,
1746 config,
1747 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001748
1749 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001750
Phil Burk062e67a2015-02-11 13:40:50 -08001751 if (status == NO_ERROR) {
Eric Laurent83b88082014-06-20 18:31:16 -07001752
1753 PlaybackThread *thread;
1754 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001755 thread = new OffloadThread(this, outputStream, *output, devices);
1756 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001757 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1758 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001759 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001760 thread = new DirectOutputThread(this, outputStream, *output, devices);
1761 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001762 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001763 thread = new MixerThread(this, outputStream, *output, devices);
1764 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001765 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001766 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001767 return thread;
1768 }
1769
1770 return 0;
1771}
1772
Eric Laurentcf2c0212014-07-25 16:20:43 -07001773status_t AudioFlinger::openOutput(audio_module_handle_t module,
1774 audio_io_handle_t *output,
1775 audio_config_t *config,
1776 audio_devices_t *devices,
1777 const String8& address,
1778 uint32_t *latencyMs,
1779 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001780{
Phil Burk062e67a2015-02-11 13:40:50 -08001781 ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001782 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001783 (devices != NULL) ? *devices : 0,
1784 config->sample_rate,
1785 config->format,
1786 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001787 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001788
Eric Laurentcf2c0212014-07-25 16:20:43 -07001789 if (*devices == AUDIO_DEVICE_NONE) {
1790 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001791 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001792
Mathias Agopian65ab4712010-07-14 17:59:35 -07001793 Mutex::Autolock _l(mLock);
1794
Eric Laurentcf2c0212014-07-25 16:20:43 -07001795 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001796 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001797 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001798
1799 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001800 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001801
1802 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001803 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001804 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001805 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001806
1807 AutoMutex lock(mHardwareLock);
1808 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001809 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001810 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten4ea00a22014-06-02 08:29:22 -07001811
Eric Laurentcf2c0212014-07-25 16:20:43 -07001812 mPrimaryOutputSampleRate = config->sample_rate;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001813 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001814 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001815 }
1816
Eric Laurentcf2c0212014-07-25 16:20:43 -07001817 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001818}
1819
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001820audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1821 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001822{
1823 Mutex::Autolock _l(mLock);
1824 MixerThread *thread1 = checkMixerThread_l(output1);
1825 MixerThread *thread2 = checkMixerThread_l(output2);
1826
1827 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001828 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1829 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001830 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001831 }
1832
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001833 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001834 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1835 thread->addOutputTrack(thread2);
1836 mPlaybackThreads.add(id, thread);
1837 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001838 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001839 return id;
1840}
1841
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001842status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001843{
Glenn Kastend96c5722012-04-25 13:44:49 -07001844 return closeOutput_nonvirtual(output);
1845}
1846
1847status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1848{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001849 // keep strong reference on the playback thread so that
1850 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001851 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001852 {
1853 Mutex::Autolock _l(mLock);
1854 thread = checkPlaybackThread_l(output);
1855 if (thread == NULL) {
1856 return BAD_VALUE;
1857 }
1858
Steve Block3856b092011-10-20 11:56:00 +01001859 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001860
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001861 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001862 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001863 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001864 DuplicatingThread *dupThread =
1865 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001866 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001867
1868 }
1869 }
1870 }
1871
1872
1873 mPlaybackThreads.removeItem(output);
1874 // save all effects to the default thread
1875 if (mPlaybackThreads.size()) {
1876 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1877 if (dstThread != NULL) {
1878 // audioflinger lock is held here so the acquisition order of thread locks does not
1879 // matter
1880 Mutex::Autolock _dl(dstThread->mLock);
1881 Mutex::Autolock _sl(thread->mLock);
1882 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1883 for (size_t i = 0; i < effectChains.size(); i ++) {
1884 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001885 }
1886 }
1887 }
Eric Laurent021cf962014-05-13 10:18:14 -07001888 audioConfigChanged(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001889 }
1890 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001891 // The thread entity (active unit of execution) is no longer running here,
1892 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001893
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001894 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurent83b88082014-06-20 18:31:16 -07001895 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001896 }
Eric Laurent83b88082014-06-20 18:31:16 -07001897
Mathias Agopian65ab4712010-07-14 17:59:35 -07001898 return NO_ERROR;
1899}
1900
Eric Laurent83b88082014-06-20 18:31:16 -07001901void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
1902{
1903 AudioStreamOut *out = thread->clearOutput();
1904 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1905 // from now on thread->mOutput is NULL
1906 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
1907 delete out;
1908}
1909
1910void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
1911{
1912 mPlaybackThreads.removeItem(thread->mId);
1913 thread->exit();
1914 closeOutputFinish(thread);
1915}
1916
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001917status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001918{
1919 Mutex::Autolock _l(mLock);
1920 PlaybackThread *thread = checkPlaybackThread_l(output);
1921
1922 if (thread == NULL) {
1923 return BAD_VALUE;
1924 }
1925
Steve Block3856b092011-10-20 11:56:00 +01001926 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001927 thread->suspend();
1928
1929 return NO_ERROR;
1930}
1931
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001932status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001933{
1934 Mutex::Autolock _l(mLock);
1935 PlaybackThread *thread = checkPlaybackThread_l(output);
1936
1937 if (thread == NULL) {
1938 return BAD_VALUE;
1939 }
1940
Steve Block3856b092011-10-20 11:56:00 +01001941 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001942
1943 thread->restore();
1944
1945 return NO_ERROR;
1946}
1947
Eric Laurentcf2c0212014-07-25 16:20:43 -07001948status_t AudioFlinger::openInput(audio_module_handle_t module,
1949 audio_io_handle_t *input,
1950 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08001951 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001952 const String8& address,
1953 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07001954 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001955{
Eric Laurent83b88082014-06-20 18:31:16 -07001956 Mutex::Autolock _l(mLock);
1957
Glenn Kastene7d66712015-03-05 13:46:52 -08001958 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001959 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07001960 }
1961
Glenn Kastene7d66712015-03-05 13:46:52 -08001962 sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001963
1964 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07001965 // notify client processes of the new input creation
1966 thread->audioConfigChanged(AudioSystem::INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001967 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001968 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001969 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07001970}
Dima Zavin799a70e2011-04-18 16:57:27 -07001971
Eric Laurent83b88082014-06-20 18:31:16 -07001972sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001973 audio_io_handle_t *input,
1974 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08001975 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001976 const String8& address,
1977 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07001978 audio_input_flags_t flags)
1979{
Glenn Kastene7d66712015-03-05 13:46:52 -08001980 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001981 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001982 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07001983 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001984 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001985
Eric Laurentcf2c0212014-07-25 16:20:43 -07001986 if (*input == AUDIO_IO_HANDLE_NONE) {
1987 *input = nextUniqueId();
1988 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001989
Eric Laurentcf2c0212014-07-25 16:20:43 -07001990 audio_config_t halconfig = *config;
1991 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07001992 audio_stream_in_t *inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08001993 status_t status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001994 &inStream, flags, address.string(), source);
1995 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07001996 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Dima Zavin799a70e2011-04-18 16:57:27 -07001997 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001998 halconfig.sample_rate,
1999 halconfig.format,
2000 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002001 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002002 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002003
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002004 // If the input could not be opened with the requested parameters and we can handle the
2005 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
2006 // 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 -07002007 if (status == BAD_VALUE &&
Eric Laurentcf2c0212014-07-25 16:20:43 -07002008 config->format == halconfig.format && halconfig.format == AUDIO_FORMAT_PCM_16_BIT &&
2009 (halconfig.sample_rate <= 2 * config->sample_rate) &&
2010 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
2011 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002012 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002013 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002014 inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002015 status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002016 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07002017 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002018 }
2019
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002020 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002021
Glenn Kasten46909e72013-02-26 09:20:22 -08002022#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002023 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2024 // or (re-)create if current Pipe is idle and does not match the new format
2025 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002026 enum {
2027 TEE_SINK_NO, // don't copy input
2028 TEE_SINK_NEW, // copy input using a new pipe
2029 TEE_SINK_OLD, // copy input using an existing pipe
2030 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002031 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2032 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002033 if (!mTeeSinkInputEnabled) {
2034 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002035 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002036 kind = TEE_SINK_NO;
2037 } else if (mRecordTeeSink == 0) {
2038 kind = TEE_SINK_NEW;
2039 } else if (mRecordTeeSink->getStrongCount() != 1) {
2040 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002041 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002042 kind = TEE_SINK_OLD;
2043 } else {
2044 kind = TEE_SINK_NEW;
2045 }
2046 switch (kind) {
2047 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002048 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002049 size_t numCounterOffers = 0;
2050 const NBAIO_Format offers[1] = {format};
2051 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2052 ALOG_ASSERT(index == 0);
2053 PipeReader *pipeReader = new PipeReader(*pipe);
2054 numCounterOffers = 0;
2055 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2056 ALOG_ASSERT(index == 0);
2057 mRecordTeeSink = pipe;
2058 mRecordTeeSource = pipeReader;
2059 teeSink = pipe;
2060 }
2061 break;
2062 case TEE_SINK_OLD:
2063 teeSink = mRecordTeeSink;
2064 break;
2065 case TEE_SINK_NO:
2066 default:
2067 break;
2068 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002069#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002070
Eric Laurentcf2c0212014-07-25 16:20:43 -07002071 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002072
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002073 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002074 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002075 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002076 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002077 inputStream,
2078 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002079 primaryOutputDevice_l(),
Glenn Kastene7d66712015-03-05 13:46:52 -08002080 devices
Glenn Kasten46909e72013-02-26 09:20:22 -08002081#ifdef TEE_SINK
2082 , teeSink
2083#endif
2084 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002085 mRecordThreads.add(*input, thread);
2086 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002087 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002088 }
2089
Eric Laurentcf2c0212014-07-25 16:20:43 -07002090 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002091 return 0;
2092}
2093
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002094status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002095{
Glenn Kastend96c5722012-04-25 13:44:49 -07002096 return closeInput_nonvirtual(input);
2097}
2098
2099status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2100{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002101 // keep strong reference on the record thread so that
2102 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002103 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002104 {
2105 Mutex::Autolock _l(mLock);
2106 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002107 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002108 return BAD_VALUE;
2109 }
2110
Steve Block3856b092011-10-20 11:56:00 +01002111 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002112
2113 // If we still have effect chains, it means that a client still holds a handle
2114 // on at least one effect. We must either move the chain to an existing thread with the
2115 // same session ID or put it aside in case a new record thread is opened for a
2116 // new capture on the same session
2117 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002118 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002119 Mutex::Autolock _sl(thread->mLock);
2120 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002121 // Note: maximum one chain per record thread
2122 if (effectChains.size() != 0) {
2123 chain = effectChains[0];
2124 }
2125 }
2126 if (chain != 0) {
2127 // first check if a record thread is already opened with a client on the same session.
2128 // This should only happen in case of overlap between one thread tear down and the
2129 // creation of its replacement
2130 size_t i;
2131 for (i = 0; i < mRecordThreads.size(); i++) {
2132 sp<RecordThread> t = mRecordThreads.valueAt(i);
2133 if (t == thread) {
2134 continue;
2135 }
2136 if (t->hasAudioSession(chain->sessionId()) != 0) {
2137 Mutex::Autolock _l(t->mLock);
2138 ALOGV("closeInput() found thread %d for effect session %d",
2139 t->id(), chain->sessionId());
2140 t->addEffectChain_l(chain);
2141 break;
2142 }
2143 }
2144 // put the chain aside if we could not find a record thread with the same session id.
2145 if (i == mRecordThreads.size()) {
2146 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002147 }
2148 }
Eric Laurent021cf962014-05-13 10:18:14 -07002149 audioConfigChanged(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002150 mRecordThreads.removeItem(input);
2151 }
Eric Laurent83b88082014-06-20 18:31:16 -07002152 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2153 // we have a different lock for notification client
2154 closeInputFinish(thread);
2155 return NO_ERROR;
2156}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002157
Eric Laurent83b88082014-06-20 18:31:16 -07002158void AudioFlinger::closeInputFinish(sp<RecordThread> thread)
2159{
2160 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002161 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002162 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002163 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002164 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002165 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002166}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002167
Eric Laurent83b88082014-06-20 18:31:16 -07002168void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
2169{
2170 mRecordThreads.removeItem(thread->mId);
2171 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002172}
2173
Glenn Kastend2304db2014-02-03 07:40:31 -08002174status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002175{
2176 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002177 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002178
2179 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2180 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002181 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002182 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002183
2184 return NO_ERROR;
2185}
2186
2187
Eric Laurentde3f8392014-07-27 18:38:22 -07002188audio_unique_id_t AudioFlinger::newAudioUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002189{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002190 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002191}
2192
Marco Nelissend457c972014-02-11 08:47:07 -08002193void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002194{
2195 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002196 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002197 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2198 if (pid != -1 && (caller == getpid_cached)) {
2199 caller = pid;
2200 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002201
Eric Laurent021cf962014-05-13 10:18:14 -07002202 {
2203 Mutex::Autolock _cl(mClientLock);
2204 // Ignore requests received from processes not known as notification client. The request
2205 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2206 // called from a different pid leaving a stale session reference. Also we don't know how
2207 // to clear this reference if the client process dies.
2208 if (mNotificationClients.indexOfKey(caller) < 0) {
2209 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2210 return;
2211 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002212 }
2213
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002214 size_t num = mAudioSessionRefs.size();
2215 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002216 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002217 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2218 ref->mCnt++;
2219 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002220 return;
2221 }
2222 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002223 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2224 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002225}
2226
Marco Nelissend457c972014-02-11 08:47:07 -08002227void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002228{
2229 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002230 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002231 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2232 if (pid != -1 && (caller == getpid_cached)) {
2233 caller = pid;
2234 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002235 size_t num = mAudioSessionRefs.size();
2236 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002237 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002238 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2239 ref->mCnt--;
2240 ALOGV(" decremented refcount to %d", ref->mCnt);
2241 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002242 mAudioSessionRefs.removeAt(i);
2243 delete ref;
2244 purgeStaleEffects_l();
2245 }
2246 return;
2247 }
2248 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002249 // If the caller is mediaserver it is likely that the session being released was acquired
2250 // on behalf of a process not in notification clients and we ignore the warning.
2251 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002252}
2253
2254void AudioFlinger::purgeStaleEffects_l() {
2255
Steve Block3856b092011-10-20 11:56:00 +01002256 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002257
2258 Vector< sp<EffectChain> > chains;
2259
2260 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2261 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2262 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2263 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002264 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2265 chains.push(ec);
2266 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002267 }
2268 }
2269 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2270 sp<RecordThread> t = mRecordThreads.valueAt(i);
2271 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2272 sp<EffectChain> ec = t->mEffectChains[j];
2273 chains.push(ec);
2274 }
2275 }
2276
2277 for (size_t i = 0; i < chains.size(); i++) {
2278 sp<EffectChain> ec = chains[i];
2279 int sessionid = ec->sessionId();
2280 sp<ThreadBase> t = ec->mThread.promote();
2281 if (t == 0) {
2282 continue;
2283 }
2284 size_t numsessionrefs = mAudioSessionRefs.size();
2285 bool found = false;
2286 for (size_t k = 0; k < numsessionrefs; k++) {
2287 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002288 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002289 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002290 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002291 found = true;
2292 break;
2293 }
2294 }
2295 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002296 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002297 // remove all effects from the chain
2298 while (ec->mEffects.size()) {
2299 sp<EffectModule> effect = ec->mEffects[0];
2300 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002301 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002302 if (effect->purgeHandles()) {
2303 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002304 }
2305 AudioSystem::unregisterEffect(effect->id());
2306 }
2307 }
2308 }
2309 return;
2310}
2311
Mathias Agopian65ab4712010-07-14 17:59:35 -07002312// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002313AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002314{
Glenn Kastena1117922012-01-26 10:53:32 -08002315 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002316}
2317
2318// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002319AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002320{
2321 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002322 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002323}
2324
2325// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002326AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002327{
Glenn Kastena1117922012-01-26 10:53:32 -08002328 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002329}
2330
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002331uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002332{
Glenn Kastenbcefec32014-01-17 12:09:05 -08002333 return (uint32_t) android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002334}
2335
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002336AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002337{
2338 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2339 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002340 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002341 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002342 return thread;
2343 }
2344 }
2345 return NULL;
2346}
2347
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002348audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002349{
2350 PlaybackThread *thread = primaryPlaybackThread_l();
2351
2352 if (thread == NULL) {
2353 return 0;
2354 }
2355
Eric Laurentf1c04f92012-08-28 14:26:53 -07002356 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002357}
2358
Eric Laurenta011e352012-03-29 15:51:43 -07002359sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2360 int triggerSession,
2361 int listenerSession,
2362 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002363 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002364{
2365 Mutex::Autolock _l(mLock);
2366
2367 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2368 status_t playStatus = NAME_NOT_FOUND;
2369 status_t recStatus = NAME_NOT_FOUND;
2370 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2371 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2372 if (playStatus == NO_ERROR) {
2373 return event;
2374 }
2375 }
2376 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2377 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2378 if (recStatus == NO_ERROR) {
2379 return event;
2380 }
2381 }
2382 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2383 mPendingSyncEvents.add(event);
2384 } else {
2385 ALOGV("createSyncEvent() invalid event %d", event->type());
2386 event.clear();
2387 }
2388 return event;
2389}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002390
Mathias Agopian65ab4712010-07-14 17:59:35 -07002391// ----------------------------------------------------------------------------
2392// Effect management
2393// ----------------------------------------------------------------------------
2394
2395
Glenn Kastenf587ba52012-01-26 16:25:10 -08002396status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002397{
2398 Mutex::Autolock _l(mLock);
2399 return EffectQueryNumberEffects(numEffects);
2400}
2401
Glenn Kastenf587ba52012-01-26 16:25:10 -08002402status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002403{
2404 Mutex::Autolock _l(mLock);
2405 return EffectQueryEffect(index, descriptor);
2406}
2407
Glenn Kasten5e92a782012-01-30 07:40:52 -08002408status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002409 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002410{
2411 Mutex::Autolock _l(mLock);
2412 return EffectGetDescriptor(pUuid, descriptor);
2413}
2414
2415
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002416sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002417 effect_descriptor_t *pDesc,
2418 const sp<IEffectClient>& effectClient,
2419 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002420 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002421 int sessionId,
2422 status_t *status,
2423 int *id,
2424 int *enabled)
2425{
2426 status_t lStatus = NO_ERROR;
2427 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002428 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002429
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002430 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002431 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002432 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002433
2434 if (pDesc == NULL) {
2435 lStatus = BAD_VALUE;
2436 goto Exit;
2437 }
2438
Eric Laurent84e9a102010-09-23 16:10:16 -07002439 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002440 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002441 lStatus = PERMISSION_DENIED;
2442 goto Exit;
2443 }
2444
Dima Zavinfce7a472011-04-19 22:30:36 -07002445 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002446 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002447 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002448 lStatus = PERMISSION_DENIED;
2449 goto Exit;
2450 }
2451
Mathias Agopian65ab4712010-07-14 17:59:35 -07002452 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002453 if (!EffectIsNullUuid(&pDesc->uuid)) {
2454 // if uuid is specified, request effect descriptor
2455 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2456 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002457 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002458 goto Exit;
2459 }
2460 } else {
2461 // if uuid is not specified, look for an available implementation
2462 // of the required type in effect factory
2463 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002464 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002465 lStatus = BAD_VALUE;
2466 goto Exit;
2467 }
2468 uint32_t numEffects = 0;
2469 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002470 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002471 bool found = false;
2472
2473 lStatus = EffectQueryNumberEffects(&numEffects);
2474 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002475 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002476 goto Exit;
2477 }
2478 for (uint32_t i = 0; i < numEffects; i++) {
2479 lStatus = EffectQueryEffect(i, &desc);
2480 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002481 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002482 continue;
2483 }
2484 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2485 // If matching type found save effect descriptor. If the session is
2486 // 0 and the effect is not auxiliary, continue enumeration in case
2487 // an auxiliary version of this effect type is available
2488 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002489 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002490 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002491 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2492 break;
2493 }
2494 }
2495 }
2496 if (!found) {
2497 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002498 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002499 goto Exit;
2500 }
2501 // For same effect type, chose auxiliary version over insert version if
2502 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002503 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002504 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002505 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002506 }
2507 }
2508
2509 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002510 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002511 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2512 lStatus = INVALID_OPERATION;
2513 goto Exit;
2514 }
2515
Eric Laurent59255e42011-07-27 19:49:51 -07002516 // check recording permission for visualizer
2517 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2518 !recordingAllowed()) {
2519 lStatus = PERMISSION_DENIED;
2520 goto Exit;
2521 }
2522
Mathias Agopian65ab4712010-07-14 17:59:35 -07002523 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002524 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002525 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002526 // if the output returned by getOutputForEffect() is removed before we lock the
2527 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2528 // and we will exit safely
2529 io = AudioSystem::getOutputForEffect(&desc);
2530 ALOGV("createEffect got output %d", io);
2531 }
2532
2533 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002534
2535 // If output is not specified try to find a matching audio session ID in one of the
2536 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002537 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2538 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002539 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002540 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002541 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2542 // output must be specified by AudioPolicyManager when using session
2543 // AUDIO_SESSION_OUTPUT_STAGE
2544 lStatus = BAD_VALUE;
2545 goto Exit;
2546 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002547 // look for the thread where the specified audio session is present
2548 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2549 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2550 io = mPlaybackThreads.keyAt(i);
2551 break;
2552 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002553 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002554 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002555 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2556 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2557 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002558 break;
2559 }
2560 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002561 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002562 // If no output thread contains the requested session ID, default to
2563 // first output. The effect chain will be moved to the correct output
2564 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002565 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002566 io = mPlaybackThreads.keyAt(0);
2567 }
Steve Block3856b092011-10-20 11:56:00 +01002568 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002569 }
2570 ThreadBase *thread = checkRecordThread_l(io);
2571 if (thread == NULL) {
2572 thread = checkPlaybackThread_l(io);
2573 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002574 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002575 lStatus = BAD_VALUE;
2576 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002577 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002578 } else {
2579 // Check if one effect chain was awaiting for an effect to be created on this
2580 // session and used it instead of creating a new one.
2581 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)sessionId);
2582 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002583 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002584 thread->addEffectChain_l(chain);
2585 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002586 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002587
Eric Laurent021cf962014-05-13 10:18:14 -07002588 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002589
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002590 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002591 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2592 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002593 if (handle != 0 && id != NULL) {
2594 *id = handle->id();
2595 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002596 if (handle == 0) {
2597 // remove local strong reference to Client with mClientLock held
2598 Mutex::Autolock _cl(mClientLock);
2599 client.clear();
2600 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002601 }
2602
2603Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002604 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002605 return handle;
2606}
2607
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002608status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2609 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002610{
Steve Block3856b092011-10-20 11:56:00 +01002611 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002612 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002613 Mutex::Autolock _l(mLock);
2614 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002615 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002616 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002617 }
Eric Laurentde070132010-07-13 04:45:46 -07002618 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2619 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002620 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002621 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002622 }
Eric Laurentde070132010-07-13 04:45:46 -07002623 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2624 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002625 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002626 return BAD_VALUE;
2627 }
2628
2629 Mutex::Autolock _dl(dstThread->mLock);
2630 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002631 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002632}
2633
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002634// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002635status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002636 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002637 AudioFlinger::PlaybackThread *dstThread,
2638 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002639{
Steve Block3856b092011-10-20 11:56:00 +01002640 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002641 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002642
Eric Laurent59255e42011-07-27 19:49:51 -07002643 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002644 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002645 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002646 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002647 return INVALID_OPERATION;
2648 }
2649
Andy Hung9a592762014-07-21 21:56:01 -07002650 // Check whether the destination thread has a channel count of FCC_2, which is
2651 // currently required for (most) effects. Prevent moving the effect chain here rather
2652 // than disabling the addEffect_l() call in dstThread below.
Eric Laurentb10352f2014-11-03 16:25:39 -08002653 if ((dstThread->type() == ThreadBase::MIXER || dstThread->type() == ThreadBase::DUPLICATING) &&
2654 dstThread->mChannelCount != FCC_2) {
Andy Hung9a592762014-07-21 21:56:01 -07002655 ALOGW("moveEffectChain_l() effect chain failed because"
2656 " destination thread %p channel count(%u) != %u",
2657 dstThread, dstThread->mChannelCount, FCC_2);
2658 return INVALID_OPERATION;
2659 }
2660
Eric Laurent39e94f82010-07-28 01:32:47 -07002661 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002662 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002663 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002664 // removed.
2665 srcThread->removeEffectChain_l(chain);
2666
2667 // transfer all effects one by one so that new effect chain is created on new thread with
2668 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002669 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002670 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002671 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002672 Vector< sp<EffectModule> > removed;
2673 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002674 while (effect != 0) {
2675 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002676 removed.add(effect);
2677 status = dstThread->addEffect_l(effect);
2678 if (status != NO_ERROR) {
2679 break;
2680 }
Eric Laurentec35a142011-10-05 17:42:25 -07002681 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2682 if (effect->state() == EffectModule::ACTIVE ||
2683 effect->state() == EffectModule::STOPPING) {
2684 effect->start();
2685 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002686 // if the move request is not received from audio policy manager, the effect must be
2687 // re-registered with the new strategy and output
2688 if (dstChain == 0) {
2689 dstChain = effect->chain().promote();
2690 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002691 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002692 status = NO_INIT;
2693 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002694 }
2695 strategy = dstChain->strategy();
2696 }
2697 if (reRegister) {
2698 AudioSystem::unregisterEffect(effect->id());
2699 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002700 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002701 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002702 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002703 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002704 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002705 }
Eric Laurentde070132010-07-13 04:45:46 -07002706 effect = chain->getEffectFromId_l(0);
2707 }
2708
Eric Laurent5baf2af2013-09-12 17:37:00 -07002709 if (status != NO_ERROR) {
2710 for (size_t i = 0; i < removed.size(); i++) {
2711 srcThread->addEffect_l(removed[i]);
2712 if (dstChain != 0 && reRegister) {
2713 AudioSystem::unregisterEffect(removed[i]->id());
2714 AudioSystem::registerEffect(&removed[i]->desc(),
2715 srcThread->id(),
2716 strategy,
2717 sessionId,
2718 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002719 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002720 }
2721 }
2722 }
2723
2724 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002725}
2726
Eric Laurent5baf2af2013-09-12 17:37:00 -07002727bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002728{
2729 if (mGlobalEffectEnableTime != 0 &&
2730 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2731 return true;
2732 }
2733
2734 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2735 sp<EffectChain> ec =
2736 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002737 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002738 return true;
2739 }
2740 }
2741 return false;
2742}
2743
Eric Laurent5baf2af2013-09-12 17:37:00 -07002744void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002745{
2746 Mutex::Autolock _l(mLock);
2747
2748 mGlobalEffectEnableTime = systemTime();
2749
2750 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2751 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2752 if (t->mType == ThreadBase::OFFLOAD) {
2753 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2754 }
2755 }
2756
2757}
2758
Eric Laurentaaa44472014-09-12 17:41:50 -07002759status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2760{
2761 audio_session_t session = (audio_session_t)chain->sessionId();
2762 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2763 ALOGV("putOrphanEffectChain_l session %d index %d", session, index);
2764 if (index >= 0) {
2765 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2766 return ALREADY_EXISTS;
2767 }
2768 mOrphanEffectChains.add(session, chain);
2769 return NO_ERROR;
2770}
2771
2772sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2773{
2774 sp<EffectChain> chain;
2775 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2776 ALOGV("getOrphanEffectChain_l session %d index %d", session, index);
2777 if (index >= 0) {
2778 chain = mOrphanEffectChains.valueAt(index);
2779 mOrphanEffectChains.removeItemsAt(index);
2780 }
2781 return chain;
2782}
2783
2784bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2785{
2786 Mutex::Autolock _l(mLock);
2787 audio_session_t session = (audio_session_t)effect->sessionId();
2788 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2789 ALOGV("updateOrphanEffectChains session %d index %d", session, index);
2790 if (index >= 0) {
2791 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2792 if (chain->removeEffect_l(effect) == 0) {
2793 ALOGV("updateOrphanEffectChains removing effect chain at index %d", index);
2794 mOrphanEffectChains.removeItemsAt(index);
2795 }
2796 return true;
2797 }
2798 return false;
2799}
2800
2801
Glenn Kastenda6ef132013-01-10 12:31:01 -08002802struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002803#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
2804 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002805};
2806
2807int comparEntry(const void *p1, const void *p2)
2808{
Glenn Kasten2f55e762015-03-05 16:05:08 -08002809 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002810}
2811
Glenn Kasten46909e72013-02-26 09:20:22 -08002812#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002813void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002814{
Eric Laurent81784c32012-11-19 14:55:58 -08002815 NBAIO_Source *teeSource = source.get();
2816 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002817 // .wav rotation
2818 // There is a benign race condition if 2 threads call this simultaneously.
2819 // They would both traverse the directory, but the result would simply be
2820 // failures at unlink() which are ignored. It's also unlikely since
2821 // normally dumpsys is only done by bugreport or from the command line.
2822 char teePath[32+256];
2823 strcpy(teePath, "/data/misc/media");
2824 size_t teePathLen = strlen(teePath);
2825 DIR *dir = opendir(teePath);
2826 teePath[teePathLen++] = '/';
2827 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002828#define TEE_MAX_SORT 20 // number of entries to sort
2829#define TEE_MAX_KEEP 10 // number of entries to keep
2830 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002831 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08002832 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002833 struct dirent de;
2834 struct dirent *result = NULL;
2835 int rc = readdir_r(dir, &de, &result);
2836 if (rc != 0) {
2837 ALOGW("readdir_r failed %d", rc);
2838 break;
2839 }
2840 if (result == NULL) {
2841 break;
2842 }
2843 if (result != &de) {
2844 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2845 break;
2846 }
2847 // ignore non .wav file entries
2848 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002849 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08002850 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2851 continue;
2852 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08002853 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002854 }
2855 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002856 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002857 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002858 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
2859 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002860 (void) unlink(teePath);
2861 }
2862 }
2863 } else {
2864 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002865 dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002866 }
2867 }
Eric Laurent81784c32012-11-19 14:55:58 -08002868 char teeTime[16];
2869 struct timeval tv;
2870 gettimeofday(&tv, NULL);
2871 struct tm tm;
2872 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002873 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2874 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2875 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2876 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002877 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07002878 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08002879 char wavHeader[44];
2880 memcpy(wavHeader,
2881 "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",
2882 sizeof(wavHeader));
2883 NBAIO_Format format = teeSource->format();
2884 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002885 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07002886 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002887 wavHeader[22] = channelCount; // number of channels
2888 wavHeader[24] = sampleRate; // sample rate
2889 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07002890 wavHeader[32] = frameSize; // block alignment
2891 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08002892 write(teeFd, wavHeader, sizeof(wavHeader));
2893 size_t total = 0;
2894 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07002895#define TEE_SINK_READ 1024 // frames per I/O operation
2896 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002897 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08002898 size_t count = TEE_SINK_READ;
2899 ssize_t actual = teeSource->read(buffer, count,
2900 AudioBufferProvider::kInvalidPTS);
2901 bool wasFirstRead = firstRead;
2902 firstRead = false;
2903 if (actual <= 0) {
2904 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2905 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002906 }
Eric Laurent81784c32012-11-19 14:55:58 -08002907 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002908 }
Eric Laurent81784c32012-11-19 14:55:58 -08002909 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07002910 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002911 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002912 }
Glenn Kasten329f6512014-08-28 16:23:16 -07002913 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08002914 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002915 uint32_t temp = 44 + total * frameSize - 8;
2916 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002917 write(teeFd, &temp, sizeof(temp));
2918 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002919 temp = total * frameSize;
2920 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002921 write(teeFd, &temp, sizeof(temp));
2922 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002923 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002924 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002925 }
Eric Laurent59255e42011-07-27 19:49:51 -07002926 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002927 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002928 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002929 }
Eric Laurent59255e42011-07-27 19:49:51 -07002930 }
2931 }
2932}
Glenn Kasten46909e72013-02-26 09:20:22 -08002933#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002934
Mathias Agopian65ab4712010-07-14 17:59:35 -07002935// ----------------------------------------------------------------------------
2936
2937status_t AudioFlinger::onTransact(
2938 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2939{
2940 return BnAudioFlinger::onTransact(code, data, reply, flags);
2941}
2942
Glenn Kasten63238ef2015-03-02 15:50:29 -08002943} // namespace android