blob: 52fce345898fcca89ea0dbad5136e704a323fb06 [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
Andy Hung6770c6f2015-04-07 13:43:36 -070048#include <media/AudioResamplerPublic.h>
49
Mathias Agopian65ab4712010-07-14 17:59:35 -070050#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070051#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070052#include <audio_effects/effect_ns.h>
53#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070054
Glenn Kasten3b21c502011-12-15 09:52:39 -080055#include <audio_utils/primitives.h>
56
Eric Laurentfeb0db62011-07-22 09:04:31 -070057#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080058
John Grossman4ff14ba2012-02-08 16:37:41 -080059#include <common_time/cc_helper.h>
Glenn Kasten58912562012-04-03 10:45:00 -070060
Glenn Kasten9e58b552013-01-18 15:09:48 -080061#include <media/IMediaLogService.h>
62
Glenn Kastenda6ef132013-01-10 12:31:01 -080063#include <media/nbaio/Pipe.h>
64#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070065#include <media/AudioParameter.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070066#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080067
Mathias Agopian65ab4712010-07-14 17:59:35 -070068// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070069
John Grossman1c345192012-03-27 14:00:17 -070070// Note: the following macro is used for extremely verbose logging message. In
71// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
72// 0; but one side effect of this is to turn all LOGV's as well. Some messages
73// are so verbose that we want to suppress them even when we have ALOG_ASSERT
74// turned on. Do not uncomment the #def below unless you really know what you
75// are doing and want to see all of the extremely verbose messages.
76//#define VERY_VERY_VERBOSE_LOGGING
77#ifdef VERY_VERY_VERBOSE_LOGGING
78#define ALOGVV ALOGV
79#else
80#define ALOGVV(a...) do { } while(0)
81#endif
Eric Laurentde070132010-07-13 04:45:46 -070082
Mathias Agopian65ab4712010-07-14 17:59:35 -070083namespace android {
84
Glenn Kastenec1d6b52011-12-12 09:04:45 -080085static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
86static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Eric Laurent021cf962014-05-13 10:18:14 -070087static const char kClientLockedString[] = "Client lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070088
Glenn Kasten58912562012-04-03 10:45:00 -070089
John Grossman4ff14ba2012-02-08 16:37:41 -080090nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080091
Eric Laurent81784c32012-11-19 14:55:58 -080092uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070093
Glenn Kasten46909e72013-02-26 09:20:22 -080094#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080095bool AudioFlinger::mTeeSinkInputEnabled = false;
96bool AudioFlinger::mTeeSinkOutputEnabled = false;
97bool AudioFlinger::mTeeSinkTrackEnabled = false;
98
99size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
100size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
101size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800102#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800103
Eric Laurent813e2a72013-08-31 12:59:48 -0700104// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
105// we define a minimum time during which a global effect is considered enabled.
106static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
107
Mathias Agopian65ab4712010-07-14 17:59:35 -0700108// ----------------------------------------------------------------------------
109
Marco Nelissenb2208842014-02-07 14:00:50 -0800110const char *formatToString(audio_format_t format) {
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530111 switch (format & AUDIO_FORMAT_MAIN_MASK) {
112 case AUDIO_FORMAT_PCM:
113 switch (format) {
114 case AUDIO_FORMAT_PCM_16_BIT: return "pcm16";
115 case AUDIO_FORMAT_PCM_8_BIT: return "pcm8";
116 case AUDIO_FORMAT_PCM_32_BIT: return "pcm32";
117 case AUDIO_FORMAT_PCM_8_24_BIT: return "pcm8.24";
118 case AUDIO_FORMAT_PCM_FLOAT: return "pcmfloat";
119 case AUDIO_FORMAT_PCM_24_BIT_PACKED: return "pcm24";
120 default:
121 break;
122 }
123 break;
Marco Nelissenb2208842014-02-07 14:00:50 -0800124 case AUDIO_FORMAT_MP3: return "mp3";
125 case AUDIO_FORMAT_AMR_NB: return "amr-nb";
126 case AUDIO_FORMAT_AMR_WB: return "amr-wb";
127 case AUDIO_FORMAT_AAC: return "aac";
128 case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
129 case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
130 case AUDIO_FORMAT_VORBIS: return "vorbis";
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530131 case AUDIO_FORMAT_OPUS: return "opus";
132 case AUDIO_FORMAT_AC3: return "ac-3";
133 case AUDIO_FORMAT_E_AC3: return "e-ac-3";
Marco Nelissenb2208842014-02-07 14:00:50 -0800134 default:
135 break;
136 }
137 return "unknown";
138}
139
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700140static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700141{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700142 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700143 int rc;
144
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700145 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
146 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
147 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
148 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700149 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700150 }
151 rc = audio_hw_device_open(mod, dev);
152 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
153 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
154 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700155 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700156 }
Eric Laurent5806b352014-05-22 12:23:26 -0700157 if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700158 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
159 rc = BAD_VALUE;
160 goto out;
161 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700162 return 0;
163
164out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700165 *dev = NULL;
166 return rc;
167}
168
Mathias Agopian65ab4712010-07-14 17:59:35 -0700169// ----------------------------------------------------------------------------
170
171AudioFlinger::AudioFlinger()
172 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800173 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800174 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700175 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800176 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800177 mMasterMute(false),
178 mNextUniqueId(1),
179 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700180 mBtNrecIsOff(false),
181 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700182 mIsDeviceTypeKnown(false),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700183 mGlobalEffectEnableTime(0),
Eric Laurent72e3f392015-05-20 14:43:50 -0700184 mPrimaryOutputSampleRate(0),
185 mSystemReady(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700186{
Glenn Kasten949a9262013-04-16 12:35:20 -0700187 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800188 char value[PROPERTY_VALUE_MAX];
189 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
190 if (doLog) {
Glenn Kastenb187de12014-12-30 08:18:15 -0800191 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters",
192 MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800193 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700194
Glenn Kasten46909e72013-02-26 09:20:22 -0800195#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800196 (void) property_get("ro.debuggable", value, "0");
197 int debuggable = atoi(value);
198 int teeEnabled = 0;
199 if (debuggable) {
200 (void) property_get("af.tee", value, "0");
201 teeEnabled = atoi(value);
202 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800203 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700204 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800205 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700206 }
207 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800208 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700209 }
210 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800211 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700212 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800213#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700214}
215
216void AudioFlinger::onFirstRef()
217{
Dima Zavin799a70e2011-04-18 16:57:27 -0700218 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700219
Eric Laurent93575202011-01-18 18:39:02 -0800220 Mutex::Autolock _l(mLock);
221
Dima Zavin799a70e2011-04-18 16:57:27 -0700222 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800223 char val_str[PROPERTY_VALUE_MAX] = { 0 };
224 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
225 uint32_t int_val;
226 if (1 == sscanf(val_str, "%u", &int_val)) {
227 mStandbyTimeInNsecs = milliseconds(int_val);
228 ALOGI("Using %u mSec as standby time.", int_val);
229 } else {
230 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
231 ALOGI("Using default %u mSec as standby time.",
232 (uint32_t)(mStandbyTimeInNsecs / 1000000));
233 }
234 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700235
Eric Laurent1c333e22014-05-20 10:48:17 -0700236 mPatchPanel = new PatchPanel(this);
237
Eric Laurenta4c5a552012-03-29 10:12:40 -0700238 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700239}
240
241AudioFlinger::~AudioFlinger()
242{
243 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700244 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700245 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700246 }
247 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700248 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700249 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700250 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700251
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800252 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
253 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700254 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
255 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700256 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700257
258 // Tell media.log service about any old writers that still need to be unregistered
259 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
260 if (binder != 0) {
261 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
262 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
263 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
264 mUnregisteredWriters.pop();
265 mediaLogService->unregisterWriter(iMemory);
266 }
267 }
268
Mathias Agopian65ab4712010-07-14 17:59:35 -0700269}
270
Eric Laurenta4c5a552012-03-29 10:12:40 -0700271static const char * const audio_interfaces[] = {
272 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
273 AUDIO_HARDWARE_MODULE_ID_A2DP,
274 AUDIO_HARDWARE_MODULE_ID_USB,
275};
276#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
277
Phil Burk062e67a2015-02-11 13:40:50 -0800278AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
John Grossmanee578c02012-07-23 17:05:46 -0700279 audio_module_handle_t module,
280 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700281{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700282 // if module is 0, the request comes from an old policy manager and we should load
283 // well known modules
284 if (module == 0) {
285 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
286 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
287 loadHwModule_l(audio_interfaces[i]);
288 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700289 // then try to find a module supporting the requested device.
290 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
291 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
292 audio_hw_device_t *dev = audioHwDevice->hwDevice();
293 if ((dev->get_supported_devices != NULL) &&
294 (dev->get_supported_devices(dev) & devices) == devices)
295 return audioHwDevice;
296 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700297 } else {
298 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700299 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
300 if (audioHwDevice != NULL) {
301 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700302 }
303 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700304
Dima Zavin799a70e2011-04-18 16:57:27 -0700305 return NULL;
306}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700307
Glenn Kasten0f11b512014-01-31 16:18:54 -0800308void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700309{
310 const size_t SIZE = 256;
311 char buffer[SIZE];
312 String8 result;
313
314 result.append("Clients:\n");
315 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800316 sp<Client> client = mClients.valueAt(i).promote();
317 if (client != 0) {
318 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
319 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320 }
321 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700322
Eric Laurentd1b28d42013-09-18 18:47:13 -0700323 result.append("Notification Clients:\n");
324 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
325 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
326 result.append(buffer);
327 }
328
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700329 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800330 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700331 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
332 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800333 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700334 result.append(buffer);
335 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700337}
338
339
Glenn Kasten0f11b512014-01-31 16:18:54 -0800340void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700341{
342 const size_t SIZE = 256;
343 char buffer[SIZE];
344 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800345 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700346
John Grossman4ff14ba2012-02-08 16:37:41 -0800347 snprintf(buffer, SIZE, "Hardware status: %d\n"
348 "Standby Time mSec: %u\n",
349 hardwareStatus,
350 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351 result.append(buffer);
352 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700353}
354
Glenn Kasten0f11b512014-01-31 16:18:54 -0800355void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700356{
357 const size_t SIZE = 256;
358 char buffer[SIZE];
359 String8 result;
360 snprintf(buffer, SIZE, "Permission Denial: "
361 "can't dump AudioFlinger from pid=%d, uid=%d\n",
362 IPCThreadState::self()->getCallingPid(),
363 IPCThreadState::self()->getCallingUid());
364 result.append(buffer);
365 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700366}
367
Eric Laurent81784c32012-11-19 14:55:58 -0800368bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700369{
370 bool locked = false;
371 for (int i = 0; i < kDumpLockRetries; ++i) {
372 if (mutex.tryLock() == NO_ERROR) {
373 locked = true;
374 break;
375 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800376 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700377 }
378 return locked;
379}
380
381status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
382{
Glenn Kasten44deb052012-02-05 18:09:08 -0800383 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384 dumpPermissionDenial(fd, args);
385 } else {
386 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800387 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388 if (!hardwareLocked) {
389 String8 result(kHardwareLockedString);
390 write(fd, result.string(), result.size());
391 } else {
392 mHardwareLock.unlock();
393 }
394
Eric Laurent81784c32012-11-19 14:55:58 -0800395 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700396
397 // failed to lock - AudioFlinger is probably deadlocked
398 if (!locked) {
399 String8 result(kDeadlockedString);
400 write(fd, result.string(), result.size());
401 }
402
Eric Laurent021cf962014-05-13 10:18:14 -0700403 bool clientLocked = dumpTryLock(mClientLock);
404 if (!clientLocked) {
405 String8 result(kClientLockedString);
406 write(fd, result.string(), result.size());
407 }
Marco Nelissend89eadd2014-10-07 13:28:44 -0700408
409 EffectDumpEffects(fd);
410
Mathias Agopian65ab4712010-07-14 17:59:35 -0700411 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700412 if (clientLocked) {
413 mClientLock.unlock();
414 }
415
Mathias Agopian65ab4712010-07-14 17:59:35 -0700416 dumpInternals(fd, args);
417
418 // dump playback threads
419 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
420 mPlaybackThreads.valueAt(i)->dump(fd, args);
421 }
422
423 // dump record threads
424 for (size_t i = 0; i < mRecordThreads.size(); i++) {
425 mRecordThreads.valueAt(i)->dump(fd, args);
426 }
427
Eric Laurentaaa44472014-09-12 17:41:50 -0700428 // dump orphan effect chains
429 if (mOrphanEffectChains.size() != 0) {
430 write(fd, " Orphan Effect Chains\n", strlen(" Orphan Effect Chains\n"));
431 for (size_t i = 0; i < mOrphanEffectChains.size(); i++) {
432 mOrphanEffectChains.valueAt(i)->dump(fd, args);
433 }
434 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700435 // dump all hardware devs
436 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700437 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700438 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700439 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700440
Glenn Kasten46909e72013-02-26 09:20:22 -0800441#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700442 // dump the serially shared record tee sink
443 if (mRecordTeeSource != 0) {
444 dumpTee(fd, mRecordTeeSource);
445 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800446#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700447
Glenn Kastend65d73c2012-06-22 17:21:07 -0700448 if (locked) {
449 mLock.unlock();
450 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800451
452 // append a copy of media.log here by forwarding fd to it, but don't attempt
453 // to lookup the service if it's not running, as it will block for a second
454 if (mLogMemoryDealer != 0) {
455 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
456 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700457 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800458 Vector<String16> args;
459 binder->dump(fd, args);
460 }
461 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700462 }
463 return NO_ERROR;
464}
465
Eric Laurent021cf962014-05-13 10:18:14 -0700466sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800467{
Eric Laurent021cf962014-05-13 10:18:14 -0700468 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800469 // If pid is already in the mClients wp<> map, then use that entry
470 // (for which promote() is always != 0), otherwise create a new entry and Client.
471 sp<Client> client = mClients.valueFor(pid).promote();
472 if (client == 0) {
473 client = new Client(this, pid);
474 mClients.add(pid, client);
475 }
476
477 return client;
478}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700479
Glenn Kasten9e58b552013-01-18 15:09:48 -0800480sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
481{
Glenn Kasten481fb672013-09-30 14:39:28 -0700482 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800483 if (mLogMemoryDealer == 0) {
484 return new NBLog::Writer();
485 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800486 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700487 // Similarly if we can't contact the media.log service, also return a dummy writer
488 if (binder == 0) {
489 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800490 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700491 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
492 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
493 // If allocation fails, consult the vector of previously unregistered writers
494 // and garbage-collect one or more them until an allocation succeeds
495 if (shared == 0) {
496 Mutex::Autolock _l(mUnregisteredWritersLock);
497 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
498 {
499 // Pick the oldest stale writer to garbage-collect
500 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
501 mUnregisteredWriters.removeAt(0);
502 mediaLogService->unregisterWriter(iMemory);
503 // Now the media.log remote reference to IMemory is gone. When our last local
504 // reference to IMemory also drops to zero at end of this block,
505 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
506 }
507 // Re-attempt the allocation
508 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
509 if (shared != 0) {
510 goto success;
511 }
512 }
513 // Even after garbage-collecting all old writers, there is still not enough memory,
514 // so return a dummy writer
515 return new NBLog::Writer();
516 }
517success:
518 mediaLogService->registerWriter(shared, size, name);
519 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800520}
521
522void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
523{
Glenn Kasten685ef092013-02-04 08:15:34 -0800524 if (writer == 0) {
525 return;
526 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800527 sp<IMemory> iMemory(writer->getIMemory());
528 if (iMemory == 0) {
529 return;
530 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700531 // Rather than removing the writer immediately, append it to a queue of old writers to
532 // be garbage-collected later. This allows us to continue to view old logs for a while.
533 Mutex::Autolock _l(mUnregisteredWritersLock);
534 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800535}
536
Mathias Agopian65ab4712010-07-14 17:59:35 -0700537// IAudioFlinger interface
538
539
540sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800541 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700542 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800543 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700544 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800545 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800546 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700547 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800548 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800549 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550 int *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800551 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700552 status_t *status)
553{
554 sp<PlaybackThread::Track> track;
555 sp<TrackHandle> trackHandle;
556 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700557 status_t lStatus;
558 int lSessionId;
559
Glenn Kasten263709e2012-01-06 08:40:01 -0800560 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
561 // but if someone uses binder directly they could bypass that and cause us to crash
562 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000563 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700564 lStatus = BAD_VALUE;
565 goto Exit;
566 }
567
Glenn Kasten53b5d092014-02-05 10:00:23 -0800568 // further sample rate checks are performed by createTrack_l() depending on the thread type
569 if (sampleRate == 0) {
570 ALOGE("createTrack() invalid sample rate %u", sampleRate);
571 lStatus = BAD_VALUE;
572 goto Exit;
573 }
574
575 // further channel mask checks are performed by createTrack_l() depending on the thread type
576 if (!audio_is_output_channel(channelMask)) {
577 ALOGE("createTrack() invalid channel mask %#x", channelMask);
578 lStatus = BAD_VALUE;
579 goto Exit;
580 }
581
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700582 // further format checks are performed by createTrack_l() depending on the thread type
583 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800584 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700585 lStatus = BAD_VALUE;
586 goto Exit;
587 }
588
Glenn Kasten663c2242013-09-24 11:52:37 -0700589 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
590 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
591 lStatus = BAD_VALUE;
592 goto Exit;
593 }
594
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 {
596 Mutex::Autolock _l(mLock);
597 PlaybackThread *thread = checkPlaybackThread_l(output);
598 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800599 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700600 lStatus = BAD_VALUE;
601 goto Exit;
602 }
603
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800604 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -0700605 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700606
Glenn Kastene848bd92014-03-13 15:00:32 -0700607 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700608 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700609 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700610 // check if an effect chain with the same session ID is present on another
611 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700612 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700613 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
614 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700615 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700616 if (sessions & PlaybackThread::EFFECT_SESSION) {
617 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700618 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700619 }
Eric Laurentde070132010-07-13 04:45:46 -0700620 }
621 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700622 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700623 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700624 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700625 if (sessionId != NULL) {
626 *sessionId = lSessionId;
627 }
628 }
Steve Block3856b092011-10-20 11:56:00 +0100629 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700630
631 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800632 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800633 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700634 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700635
636 // move effect chain to this output thread if an effect on same session was waiting
637 // for a track to be created
638 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700639 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700640 Mutex::Autolock _dl(thread->mLock);
641 Mutex::Autolock _sl(effectThread->mLock);
642 moveEffectChain_l(lSessionId, effectThread, thread, true);
643 }
Eric Laurenta011e352012-03-29 15:51:43 -0700644
645 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700646 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700647 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
648 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700649 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700650 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700651 } else {
652 mPendingSyncEvents[i]->cancel();
653 }
Eric Laurenta011e352012-03-29 15:51:43 -0700654 mPendingSyncEvents.removeAt(i);
655 i--;
656 }
657 }
658 }
Glenn Kastene198c362013-08-13 09:13:36 -0700659
Eric Laurentfa90e842014-10-17 18:12:31 -0700660 setAudioHwSyncForSession_l(thread, (audio_session_t)lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700661 }
Glenn Kastene198c362013-08-13 09:13:36 -0700662
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700663 if (lStatus != NO_ERROR) {
664 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700665 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700666 // Don't hold mClientLock when releasing the reference on the track as the
667 // destructor will acquire it.
668 {
669 Mutex::Autolock _cl(mClientLock);
670 client.clear();
671 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700673 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700674 }
675
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700676 // return handle to client
677 trackHandle = new TrackHandle(track);
678
Mathias Agopian65ab4712010-07-14 17:59:35 -0700679Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700680 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700681 return trackHandle;
682}
683
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800684uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685{
686 Mutex::Autolock _l(mLock);
687 PlaybackThread *thread = checkPlaybackThread_l(output);
688 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000689 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690 return 0;
691 }
692 return thread->sampleRate();
693}
694
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800695audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700696{
697 Mutex::Autolock _l(mLock);
698 PlaybackThread *thread = checkPlaybackThread_l(output);
699 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000700 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800701 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700702 }
703 return thread->format();
704}
705
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800706size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700707{
708 Mutex::Autolock _l(mLock);
709 PlaybackThread *thread = checkPlaybackThread_l(output);
710 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000711 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712 return 0;
713 }
Glenn Kasten58912562012-04-03 10:45:00 -0700714 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
715 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700716 return thread->frameCount();
717}
718
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800719uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720{
721 Mutex::Autolock _l(mLock);
722 PlaybackThread *thread = checkPlaybackThread_l(output);
723 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800724 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700725 return 0;
726 }
727 return thread->latency();
728}
729
730status_t AudioFlinger::setMasterVolume(float value)
731{
Eric Laurenta1884f92011-08-23 08:25:03 -0700732 status_t ret = initCheck();
733 if (ret != NO_ERROR) {
734 return ret;
735 }
736
Mathias Agopian65ab4712010-07-14 17:59:35 -0700737 // check calling permissions
738 if (!settingsAllowed()) {
739 return PERMISSION_DENIED;
740 }
741
Eric Laurenta4c5a552012-03-29 10:12:40 -0700742 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700743 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700744
John Grossmanee578c02012-07-23 17:05:46 -0700745 // Set master volume in the HALs which support it.
746 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
747 AutoMutex lock(mHardwareLock);
748 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800749
John Grossmanee578c02012-07-23 17:05:46 -0700750 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
751 if (dev->canSetMasterVolume()) {
752 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800753 }
John Grossmanee578c02012-07-23 17:05:46 -0700754 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700755 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700756
John Grossmanee578c02012-07-23 17:05:46 -0700757 // Now set the master volume in each playback thread. Playback threads
758 // assigned to HALs which do not have master volume support will apply
759 // master volume during the mix operation. Threads with HALs which do
760 // support master volume will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700761 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
762 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
763 continue;
764 }
John Grossmanee578c02012-07-23 17:05:46 -0700765 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700766 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700767
768 return NO_ERROR;
769}
770
Glenn Kastenf78aee72012-01-04 11:00:47 -0800771status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700772{
Eric Laurenta1884f92011-08-23 08:25:03 -0700773 status_t ret = initCheck();
774 if (ret != NO_ERROR) {
775 return ret;
776 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700777
778 // check calling permissions
779 if (!settingsAllowed()) {
780 return PERMISSION_DENIED;
781 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800782 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000783 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700784 return BAD_VALUE;
785 }
786
787 { // scope for the lock
788 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700789 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700790 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700791 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700792 mHardwareStatus = AUDIO_HW_IDLE;
793 }
794
795 if (NO_ERROR == ret) {
796 Mutex::Autolock _l(mLock);
797 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800798 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700799 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700800 }
801
802 return ret;
803}
804
805status_t AudioFlinger::setMicMute(bool state)
806{
Eric Laurenta1884f92011-08-23 08:25:03 -0700807 status_t ret = initCheck();
808 if (ret != NO_ERROR) {
809 return ret;
810 }
811
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812 // check calling permissions
813 if (!settingsAllowed()) {
814 return PERMISSION_DENIED;
815 }
816
817 AutoMutex lock(mHardwareLock);
818 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
Eric Laurent2f035f52014-09-14 12:11:52 -0700819 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
820 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
821 status_t result = dev->set_mic_mute(dev, state);
822 if (result != NO_ERROR) {
823 ret = result;
824 }
825 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700826 mHardwareStatus = AUDIO_HW_IDLE;
827 return ret;
828}
829
830bool AudioFlinger::getMicMute() const
831{
Eric Laurenta1884f92011-08-23 08:25:03 -0700832 status_t ret = initCheck();
833 if (ret != NO_ERROR) {
834 return false;
835 }
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800836 bool mute = true;
Dima Zavinfce7a472011-04-19 22:30:36 -0700837 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800838 AutoMutex lock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700839 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800840 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
841 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
842 status_t result = dev->get_mic_mute(dev, &state);
843 if (result == NO_ERROR) {
844 mute = mute && state;
845 }
846 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700847 mHardwareStatus = AUDIO_HW_IDLE;
Ricardo Garcia3e91b5d2015-02-19 10:54:52 -0800848
849 return mute;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700850}
851
852status_t AudioFlinger::setMasterMute(bool muted)
853{
John Grossmand8f178d2012-07-20 14:51:35 -0700854 status_t ret = initCheck();
855 if (ret != NO_ERROR) {
856 return ret;
857 }
858
Mathias Agopian65ab4712010-07-14 17:59:35 -0700859 // check calling permissions
860 if (!settingsAllowed()) {
861 return PERMISSION_DENIED;
862 }
863
John Grossmanee578c02012-07-23 17:05:46 -0700864 Mutex::Autolock _l(mLock);
865 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700866
John Grossmanee578c02012-07-23 17:05:46 -0700867 // Set master mute in the HALs which support it.
868 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
869 AutoMutex lock(mHardwareLock);
870 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700871
John Grossmanee578c02012-07-23 17:05:46 -0700872 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
873 if (dev->canSetMasterMute()) {
874 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700875 }
John Grossmanee578c02012-07-23 17:05:46 -0700876 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700877 }
878
John Grossmanee578c02012-07-23 17:05:46 -0700879 // Now set the master mute in each playback thread. Playback threads
880 // assigned to HALs which do not have master mute support will apply master
881 // mute during the mix operation. Threads with HALs which do support master
882 // mute will simply ignore the setting.
Eric Laurentf6870ae2015-05-08 10:50:03 -0700883 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
884 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
885 continue;
886 }
John Grossmanee578c02012-07-23 17:05:46 -0700887 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Eric Laurentf6870ae2015-05-08 10:50:03 -0700888 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700889
890 return NO_ERROR;
891}
892
893float AudioFlinger::masterVolume() const
894{
Glenn Kasten98067102011-12-13 11:47:54 -0800895 Mutex::Autolock _l(mLock);
896 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700897}
898
899bool AudioFlinger::masterMute() const
900{
Glenn Kasten98067102011-12-13 11:47:54 -0800901 Mutex::Autolock _l(mLock);
902 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700903}
904
John Grossman4ff14ba2012-02-08 16:37:41 -0800905float AudioFlinger::masterVolume_l() const
906{
John Grossman4ff14ba2012-02-08 16:37:41 -0800907 return mMasterVolume;
908}
909
John Grossmand8f178d2012-07-20 14:51:35 -0700910bool AudioFlinger::masterMute_l() const
911{
John Grossmanee578c02012-07-23 17:05:46 -0700912 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700913}
914
Eric Laurent223fd5c2014-11-11 13:43:36 -0800915status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const
916{
917 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
918 ALOGW("setStreamVolume() invalid stream %d", stream);
919 return BAD_VALUE;
920 }
921 pid_t caller = IPCThreadState::self()->getCallingPid();
922 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && caller != getpid_cached) {
923 ALOGW("setStreamVolume() pid %d cannot use internal stream type %d", caller, stream);
924 return PERMISSION_DENIED;
925 }
926
927 return NO_ERROR;
928}
929
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800930status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
931 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700932{
933 // check calling permissions
934 if (!settingsAllowed()) {
935 return PERMISSION_DENIED;
936 }
937
Eric Laurent223fd5c2014-11-11 13:43:36 -0800938 status_t status = checkStreamType(stream);
939 if (status != NO_ERROR) {
940 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700941 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800942 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to change AUDIO_STREAM_PATCH volume");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700943
944 AutoMutex lock(mLock);
945 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700946 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947 thread = checkPlaybackThread_l(output);
948 if (thread == NULL) {
949 return BAD_VALUE;
950 }
951 }
952
953 mStreamTypes[stream].volume = value;
954
955 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800956 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700957 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700958 }
959 } else {
960 thread->setStreamVolume(stream, value);
961 }
962
963 return NO_ERROR;
964}
965
Glenn Kastenfff6d712012-01-12 16:38:12 -0800966status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700967{
968 // check calling permissions
969 if (!settingsAllowed()) {
970 return PERMISSION_DENIED;
971 }
972
Eric Laurent223fd5c2014-11-11 13:43:36 -0800973 status_t status = checkStreamType(stream);
974 if (status != NO_ERROR) {
975 return status;
976 }
977 ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH");
978
979 if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000980 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700981 return BAD_VALUE;
982 }
983
Eric Laurent93575202011-01-18 18:39:02 -0800984 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700985 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700986 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700987 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700988
989 return NO_ERROR;
990}
991
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800992float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700993{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800994 status_t status = checkStreamType(stream);
995 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700996 return 0.0f;
997 }
998
999 AutoMutex lock(mLock);
1000 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -07001001 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001002 PlaybackThread *thread = checkPlaybackThread_l(output);
1003 if (thread == NULL) {
1004 return 0.0f;
1005 }
1006 volume = thread->streamVolume(stream);
1007 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -08001008 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001009 }
1010
1011 return volume;
1012}
1013
Glenn Kastenfff6d712012-01-12 16:38:12 -08001014bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001015{
Eric Laurent223fd5c2014-11-11 13:43:36 -08001016 status_t status = checkStreamType(stream);
1017 if (status != NO_ERROR) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001018 return true;
1019 }
1020
Glenn Kasten6637baa2012-01-09 09:40:36 -08001021 AutoMutex lock(mLock);
1022 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001023}
1024
Eric Laurent054d9d32015-04-24 08:48:48 -07001025
1026void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs)
1027{
1028 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1029 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1030 }
1031}
1032
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001033status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001034{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001035 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
1036 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -08001037
Mathias Agopian65ab4712010-07-14 17:59:35 -07001038 // check calling permissions
1039 if (!settingsAllowed()) {
1040 return PERMISSION_DENIED;
1041 }
1042
Glenn Kasten142f5192014-03-25 17:44:59 -07001043 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
1044 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001045 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -07001046 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001047 {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001048 AutoMutex lock(mHardwareLock);
1049 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
1050 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1051 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
1052 status_t result = dev->set_parameters(dev, keyValuePairs.string());
1053 final_result = result ?: final_result;
1054 }
1055 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001056 }
Eric Laurent59bd0da2011-08-01 09:52:20 -07001057 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
1058 AudioParameter param = AudioParameter(keyValuePairs);
1059 String8 value;
1060 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -07001061 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
1062 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001063 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1064 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001065 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001066 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1067 // collect all of the thread's session IDs
1068 KeyedVector<int, bool> ids = thread->sessionIds();
1069 // suspend effects associated with those session IDs
1070 for (size_t j = 0; j < ids.size(); ++j) {
1071 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001072 thread->setEffectSuspended(FX_IID_AEC,
1073 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001074 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001075 thread->setEffectSuspended(FX_IID_NS,
1076 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001077 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001078 }
1079 }
Eric Laurentbee53372011-08-29 12:42:48 -07001080 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001081 }
1082 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001083 String8 screenState;
1084 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1085 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001086 if (isOff != (AudioFlinger::mScreenState & 1)) {
1087 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001088 }
1089 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001090 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001091 }
1092
1093 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1094 // and the thread is exited once the lock is released
1095 sp<ThreadBase> thread;
1096 {
1097 Mutex::Autolock _l(mLock);
1098 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001099 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001100 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001101 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001102 // indicate output device change to all input threads for pre processing
1103 AudioParameter param = AudioParameter(keyValuePairs);
1104 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001105 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1106 (value != 0)) {
Eric Laurent054d9d32015-04-24 08:48:48 -07001107 broacastParametersToRecordThreads_l(keyValuePairs);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001108 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001109 }
1110 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001111 if (thread != 0) {
1112 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001113 }
1114 return BAD_VALUE;
1115}
1116
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001117String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001118{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001119 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1120 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001121
Eric Laurenta4c5a552012-03-29 10:12:40 -07001122 Mutex::Autolock _l(mLock);
1123
Glenn Kasten142f5192014-03-25 17:44:59 -07001124 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001125 String8 out_s8;
1126
Dima Zavin799a70e2011-04-18 16:57:27 -07001127 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001128 char *s;
1129 {
1130 AutoMutex lock(mHardwareLock);
1131 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001132 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001133 s = dev->get_parameters(dev, keys.string());
1134 mHardwareStatus = AUDIO_HW_IDLE;
1135 }
John Grossmanef7740b2012-02-09 11:28:36 -08001136 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001137 free(s);
1138 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001139 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001140 }
1141
Mathias Agopian65ab4712010-07-14 17:59:35 -07001142 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1143 if (playbackThread != NULL) {
1144 return playbackThread->getParameters(keys);
1145 }
1146 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1147 if (recordThread != NULL) {
1148 return recordThread->getParameters(keys);
1149 }
1150 return String8("");
1151}
1152
Glenn Kastendd8104c2012-07-02 12:42:44 -07001153size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1154 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001155{
Eric Laurenta1884f92011-08-23 08:25:03 -07001156 status_t ret = initCheck();
1157 if (ret != NO_ERROR) {
1158 return 0;
1159 }
Andy Hung6770c6f2015-04-07 13:43:36 -07001160 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
1161 return 0;
1162 }
Eric Laurenta1884f92011-08-23 08:25:03 -07001163
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001164 AutoMutex lock(mHardwareLock);
1165 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001166 audio_config_t config, proposed;
1167 memset(&proposed, 0, sizeof(proposed));
1168 proposed.sample_rate = sampleRate;
1169 proposed.channel_mask = channelMask;
1170 proposed.format = format;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001171
John Grossmanee578c02012-07-23 17:05:46 -07001172 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Andy Hung6770c6f2015-04-07 13:43:36 -07001173 size_t frames;
1174 for (;;) {
1175 // Note: config is currently a const parameter for get_input_buffer_size()
1176 // but we use a copy from proposed in case config changes from the call.
1177 config = proposed;
1178 frames = dev->get_input_buffer_size(dev, &config);
1179 if (frames != 0) {
1180 break; // hal success, config is the result
1181 }
1182 // change one parameter of the configuration each iteration to a more "common" value
1183 // to see if the device will support it.
1184 if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) {
1185 proposed.format = AUDIO_FORMAT_PCM_16_BIT;
1186 } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as
1187 proposed.sample_rate = 44100; // legacy AudioRecord.java. TODO: Query hw?
1188 } else {
1189 ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, "
1190 "format %#x, channelMask 0x%X",
1191 sampleRate, format, channelMask);
1192 break; // retries failed, break out of loop with frames == 0.
1193 }
1194 }
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001195 mHardwareStatus = AUDIO_HW_IDLE;
Andy Hung6770c6f2015-04-07 13:43:36 -07001196 if (frames > 0 && config.sample_rate != sampleRate) {
1197 frames = destinationFramesPossible(frames, sampleRate, config.sample_rate);
1198 }
1199 return frames; // may be converted to bytes at the Java level.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001200}
1201
Glenn Kasten5f972c02014-01-13 09:59:31 -08001202uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001203{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001204 Mutex::Autolock _l(mLock);
1205
1206 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1207 if (recordThread != NULL) {
1208 return recordThread->getInputFramesLost();
1209 }
1210 return 0;
1211}
1212
1213status_t AudioFlinger::setVoiceVolume(float value)
1214{
Eric Laurenta1884f92011-08-23 08:25:03 -07001215 status_t ret = initCheck();
1216 if (ret != NO_ERROR) {
1217 return ret;
1218 }
1219
Mathias Agopian65ab4712010-07-14 17:59:35 -07001220 // check calling permissions
1221 if (!settingsAllowed()) {
1222 return PERMISSION_DENIED;
1223 }
1224
1225 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001226 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001227 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001228 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001229 mHardwareStatus = AUDIO_HW_IDLE;
1230
1231 return ret;
1232}
1233
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001234status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001235 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001236{
1237 status_t status;
1238
1239 Mutex::Autolock _l(mLock);
1240
1241 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1242 if (playbackThread != NULL) {
1243 return playbackThread->getRenderPosition(halFrames, dspFrames);
1244 }
1245
1246 return BAD_VALUE;
1247}
1248
1249void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1250{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001251 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001252 if (client == 0) {
1253 return;
1254 }
Eric Laurent021cf962014-05-13 10:18:14 -07001255 bool clientAdded = false;
1256 {
1257 Mutex::Autolock _cl(mClientLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001258
Eric Laurent021cf962014-05-13 10:18:14 -07001259 pid_t pid = IPCThreadState::self()->getCallingPid();
1260 if (mNotificationClients.indexOfKey(pid) < 0) {
1261 sp<NotificationClient> notificationClient = new NotificationClient(this,
1262 client,
1263 pid);
1264 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001265
Eric Laurent021cf962014-05-13 10:18:14 -07001266 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001267
Marco Nelissen06b46062014-11-14 07:58:25 -08001268 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001269 binder->linkToDeath(notificationClient);
1270 clientAdded = true;
1271 }
1272 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001273
Eric Laurent021cf962014-05-13 10:18:14 -07001274 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001275 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent021cf962014-05-13 10:18:14 -07001276 if (clientAdded) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001277 // the config change is always sent from playback or record threads to avoid deadlock
1278 // with AudioSystem::gLock
1279 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent73e26b62015-04-27 16:55:58 -07001280 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001281 }
1282
1283 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent73e26b62015-04-27 16:55:58 -07001284 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001285 }
1286 }
1287}
1288
1289void AudioFlinger::removeNotificationClient(pid_t pid)
1290{
1291 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001292 {
1293 Mutex::Autolock _cl(mClientLock);
1294 mNotificationClients.removeItem(pid);
1295 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001296
Steve Block3856b092011-10-20 11:56:00 +01001297 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001298 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001299 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001300 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001301 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001302 ALOGV(" pid %d @ %d", ref->mPid, i);
1303 if (ref->mPid == pid) {
1304 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001305 mAudioSessionRefs.removeAt(i);
1306 delete ref;
1307 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001308 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001309 } else {
1310 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001311 }
1312 }
1313 if (removed) {
1314 purgeStaleEffects_l();
1315 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001316}
1317
Eric Laurent73e26b62015-04-27 16:55:58 -07001318void AudioFlinger::ioConfigChanged(audio_io_config_event event,
1319 const sp<AudioIoDescriptor>& ioDesc)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001320{
Eric Laurent021cf962014-05-13 10:18:14 -07001321 Mutex::Autolock _l(mClientLock);
1322 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001323 for (size_t i = 0; i < size; i++) {
Eric Laurent73e26b62015-04-27 16:55:58 -07001324 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001325 }
1326}
1327
Eric Laurent021cf962014-05-13 10:18:14 -07001328// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001329void AudioFlinger::removeClient_l(pid_t pid)
1330{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001331 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001332 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001333 mClients.removeItem(pid);
1334}
1335
Eric Laurent717e1282012-06-29 16:36:52 -07001336// getEffectThread_l() must be called with AudioFlinger::mLock held
1337sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1338{
1339 sp<PlaybackThread> thread;
1340
1341 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1342 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1343 ALOG_ASSERT(thread == 0);
1344 thread = mPlaybackThreads.valueAt(i);
1345 }
1346 }
1347
1348 return thread;
1349}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001350
Mathias Agopian65ab4712010-07-14 17:59:35 -07001351
Mathias Agopian65ab4712010-07-14 17:59:35 -07001352
1353// ----------------------------------------------------------------------------
1354
1355AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1356 : RefBase(),
1357 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001358 // 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 -07001359 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001360 mPid(pid),
1361 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001362{
1363 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1364}
1365
Eric Laurent021cf962014-05-13 10:18:14 -07001366// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001367AudioFlinger::Client::~Client()
1368{
1369 mAudioFlinger->removeClient_l(mPid);
1370}
1371
Glenn Kasten435dbe62012-01-30 10:15:48 -08001372sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001373{
1374 return mMemoryDealer;
1375}
1376
John Grossman4ff14ba2012-02-08 16:37:41 -08001377// Reserve one of the limited slots for a timed audio track associated
1378// with this client
1379bool AudioFlinger::Client::reserveTimedTrack()
1380{
1381 const int kMaxTimedTracksPerClient = 4;
1382
1383 Mutex::Autolock _l(mTimedTrackLock);
1384
1385 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1386 ALOGW("can not create timed track - pid %d has exceeded the limit",
1387 mPid);
1388 return false;
1389 }
1390
1391 mTimedTrackCount++;
1392 return true;
1393}
1394
1395// Release a slot for a timed audio track
1396void AudioFlinger::Client::releaseTimedTrack()
1397{
1398 Mutex::Autolock _l(mTimedTrackLock);
1399 mTimedTrackCount--;
1400}
1401
Mathias Agopian65ab4712010-07-14 17:59:35 -07001402// ----------------------------------------------------------------------------
1403
1404AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1405 const sp<IAudioFlingerClient>& client,
1406 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001407 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001408{
1409}
1410
1411AudioFlinger::NotificationClient::~NotificationClient()
1412{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001413}
1414
Glenn Kasten0f11b512014-01-31 16:18:54 -08001415void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001416{
1417 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001418 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001419}
1420
Mathias Agopian65ab4712010-07-14 17:59:35 -07001421
1422// ----------------------------------------------------------------------------
1423
Jeff Brown893a5642013-08-16 20:19:26 -07001424static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1425 return audio_is_remote_submix_device(inDevice);
1426}
1427
Mathias Agopian65ab4712010-07-14 17:59:35 -07001428sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001429 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001430 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001431 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001432 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001433 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001434 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001435 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001436 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001437 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001438 int *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001439 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001440 sp<IMemory>& cblk,
1441 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001442 status_t *status)
1443{
1444 sp<RecordThread::RecordTrack> recordTrack;
1445 sp<RecordHandle> recordHandle;
1446 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001447 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001448 int lSessionId;
1449
Glenn Kastend776ac62014-05-07 09:16:09 -07001450 cblk.clear();
1451 buffers.clear();
1452
Mathias Agopian65ab4712010-07-14 17:59:35 -07001453 // check calling permissions
Svet Ganovbe71aa22015-04-28 12:06:02 -07001454 if (!recordingAllowed(opPackageName)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001455 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001456 lStatus = PERMISSION_DENIED;
1457 goto Exit;
1458 }
1459
Glenn Kasten53b5d092014-02-05 10:00:23 -08001460 // further sample rate checks are performed by createRecordTrack_l()
1461 if (sampleRate == 0) {
1462 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1463 lStatus = BAD_VALUE;
1464 goto Exit;
1465 }
1466
Andy Hung6770c6f2015-04-07 13:43:36 -07001467 // we don't yet support anything other than linear PCM
1468 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001469 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001470 lStatus = BAD_VALUE;
1471 goto Exit;
1472 }
1473
Glenn Kasten53b5d092014-02-05 10:00:23 -08001474 // further channel mask checks are performed by createRecordTrack_l()
1475 if (!audio_is_input_channel(channelMask)) {
1476 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1477 lStatus = BAD_VALUE;
1478 goto Exit;
1479 }
1480
Glenn Kasten05997e22014-03-13 15:08:33 -07001481 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001482 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001483 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001484 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001485 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001486 lStatus = BAD_VALUE;
1487 goto Exit;
1488 }
1489
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001490 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001491 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001492
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001493 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001494 lSessionId = *sessionId;
1495 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001496 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001497 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001498 if (sessionId != NULL) {
1499 *sessionId = lSessionId;
1500 }
1501 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001502 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001503
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001504 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001505 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001506 frameCount, lSessionId, notificationFrames,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001507 clientUid, flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001508 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001509
1510 if (lStatus == NO_ERROR) {
1511 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1512 // session and move it to this thread.
1513 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)lSessionId);
1514 if (chain != 0) {
1515 Mutex::Autolock _l(thread->mLock);
1516 thread->addEffectChain_l(chain);
1517 }
1518 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001519 }
Glenn Kastene198c362013-08-13 09:13:36 -07001520
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001521 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001522 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001523 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001524 // Don't hold mClientLock when releasing the reference on the track as the
1525 // destructor will acquire it.
1526 {
1527 Mutex::Autolock _cl(mClientLock);
1528 client.clear();
1529 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001530 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001531 goto Exit;
1532 }
1533
Glenn Kastend776ac62014-05-07 09:16:09 -07001534 cblk = recordTrack->getCblk();
1535 buffers = recordTrack->getBuffers();
1536
Glenn Kasten2fc14732013-08-05 14:58:14 -07001537 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001538 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001539
1540Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001541 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001542 return recordHandle;
1543}
1544
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001545
1546
Mathias Agopian65ab4712010-07-14 17:59:35 -07001547// ----------------------------------------------------------------------------
1548
Eric Laurenta4c5a552012-03-29 10:12:40 -07001549audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1550{
Eric Laurent44622db2014-08-01 19:00:33 -07001551 if (name == NULL) {
1552 return 0;
1553 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001554 if (!settingsAllowed()) {
1555 return 0;
1556 }
1557 Mutex::Autolock _l(mLock);
1558 return loadHwModule_l(name);
1559}
1560
1561// loadHwModule_l() must be called with AudioFlinger::mLock held
1562audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1563{
1564 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1565 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1566 ALOGW("loadHwModule() module %s already loaded", name);
1567 return mAudioHwDevs.keyAt(i);
1568 }
1569 }
1570
Eric Laurenta4c5a552012-03-29 10:12:40 -07001571 audio_hw_device_t *dev;
1572
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001573 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001574 if (rc) {
1575 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1576 return 0;
1577 }
1578
1579 mHardwareStatus = AUDIO_HW_INIT;
1580 rc = dev->init_check(dev);
1581 mHardwareStatus = AUDIO_HW_IDLE;
1582 if (rc) {
1583 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1584 return 0;
1585 }
1586
John Grossmanee578c02012-07-23 17:05:46 -07001587 // Check and cache this HAL's level of support for master mute and master
1588 // volume. If this is the first HAL opened, and it supports the get
1589 // methods, use the initial values provided by the HAL as the current
1590 // master mute and volume settings.
1591
1592 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1593 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001594 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001595
1596 if (0 == mAudioHwDevs.size()) {
1597 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1598 if (NULL != dev->get_master_volume) {
1599 float mv;
1600 if (OK == dev->get_master_volume(dev, &mv)) {
1601 mMasterVolume = mv;
1602 }
1603 }
1604
1605 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1606 if (NULL != dev->get_master_mute) {
1607 bool mm;
1608 if (OK == dev->get_master_mute(dev, &mm)) {
1609 mMasterMute = mm;
1610 }
1611 }
1612 }
1613
Eric Laurenta4c5a552012-03-29 10:12:40 -07001614 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001615 if ((NULL != dev->set_master_volume) &&
1616 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1617 flags = static_cast<AudioHwDevice::Flags>(flags |
1618 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1619 }
1620
1621 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1622 if ((NULL != dev->set_master_mute) &&
1623 (OK == dev->set_master_mute(dev, mMasterMute))) {
1624 flags = static_cast<AudioHwDevice::Flags>(flags |
1625 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1626 }
1627
Eric Laurenta4c5a552012-03-29 10:12:40 -07001628 mHardwareStatus = AUDIO_HW_IDLE;
1629 }
1630
1631 audio_module_handle_t handle = nextUniqueId();
Eric Laurent83b88082014-06-20 18:31:16 -07001632 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001633
1634 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001635 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001636
1637 return handle;
1638
1639}
1640
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001641// ----------------------------------------------------------------------------
1642
Glenn Kasten3b16c762012-11-14 08:44:39 -08001643uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001644{
1645 Mutex::Autolock _l(mLock);
1646 PlaybackThread *thread = primaryPlaybackThread_l();
1647 return thread != NULL ? thread->sampleRate() : 0;
1648}
1649
Glenn Kastene33054e2012-11-14 12:54:39 -08001650size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001651{
1652 Mutex::Autolock _l(mLock);
1653 PlaybackThread *thread = primaryPlaybackThread_l();
1654 return thread != NULL ? thread->frameCountHAL() : 0;
1655}
1656
1657// ----------------------------------------------------------------------------
1658
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001659status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1660{
1661 uid_t uid = IPCThreadState::self()->getCallingUid();
1662 if (uid != AID_SYSTEM) {
1663 return PERMISSION_DENIED;
1664 }
1665 Mutex::Autolock _l(mLock);
1666 if (mIsDeviceTypeKnown) {
1667 return INVALID_OPERATION;
1668 }
1669 mIsLowRamDevice = isLowRamDevice;
1670 mIsDeviceTypeKnown = true;
1671 return NO_ERROR;
1672}
1673
Eric Laurent93c3d412014-08-01 14:48:35 -07001674audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1675{
1676 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001677
1678 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1679 if (index >= 0) {
1680 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1681 mHwAvSyncIds.valueAt(index), sessionId);
1682 return mHwAvSyncIds.valueAt(index);
1683 }
1684
1685 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1686 if (dev == NULL) {
1687 return AUDIO_HW_SYNC_INVALID;
1688 }
1689 char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1690 AudioParameter param = AudioParameter(String8(reply));
1691 free(reply);
1692
1693 int value;
1694 if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1695 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1696 return AUDIO_HW_SYNC_INVALID;
1697 }
1698
1699 // allow only one session for a given HW A/V sync ID.
1700 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1701 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1702 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1703 value, mHwAvSyncIds.keyAt(i));
1704 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001705 break;
1706 }
1707 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001708
1709 mHwAvSyncIds.add(sessionId, value);
1710
1711 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1712 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1713 uint32_t sessions = thread->hasAudioSession(sessionId);
1714 if (sessions & PlaybackThread::TRACK_SESSION) {
1715 AudioParameter param = AudioParameter();
1716 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1717 thread->setParameters(param.toString());
1718 break;
1719 }
1720 }
1721
1722 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1723 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001724}
1725
Eric Laurent72e3f392015-05-20 14:43:50 -07001726status_t AudioFlinger::systemReady()
1727{
1728 Mutex::Autolock _l(mLock);
1729 ALOGI("%s", __FUNCTION__);
1730 if (mSystemReady) {
1731 ALOGW("%s called twice", __FUNCTION__);
1732 return NO_ERROR;
1733 }
1734 mSystemReady = true;
1735 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1736 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1737 thread->systemReady();
1738 }
1739 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1740 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1741 thread->systemReady();
1742 }
1743 return NO_ERROR;
1744}
1745
Eric Laurentfa90e842014-10-17 18:12:31 -07001746// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1747void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1748{
1749 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1750 if (index >= 0) {
1751 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1752 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1753 AudioParameter param = AudioParameter();
1754 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1755 thread->setParameters(param.toString());
1756 }
1757}
1758
1759
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001760// ----------------------------------------------------------------------------
1761
Eric Laurent83b88082014-06-20 18:31:16 -07001762
1763sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001764 audio_io_handle_t *output,
1765 audio_config_t *config,
1766 audio_devices_t devices,
1767 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001768 audio_output_flags_t flags)
1769{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001770 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001771 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001772 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001773 }
1774
1775 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentcf2c0212014-07-25 16:20:43 -07001776 if (*output == AUDIO_IO_HANDLE_NONE) {
1777 *output = nextUniqueId();
1778 }
Eric Laurent83b88082014-06-20 18:31:16 -07001779
1780 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1781
Eric Laurent83b88082014-06-20 18:31:16 -07001782 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001783 // This if statement allows overriding the audio policy settings
1784 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1785 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1786 // Check only for Normal Mixing mode
1787 if (kEnableExtendedPrecision) {
1788 // Specify format (uncomment one below to choose)
1789 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1790 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1791 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1792 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001793 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001794 }
1795 if (kEnableExtendedChannels) {
1796 // Specify channel mask (uncomment one below to choose)
1797 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1798 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1799 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1800 }
Eric Laurent83b88082014-06-20 18:31:16 -07001801 }
1802
Phil Burk062e67a2015-02-11 13:40:50 -08001803 AudioStreamOut *outputStream = NULL;
1804 status_t status = outHwDev->openOutputStream(
1805 &outputStream,
1806 *output,
1807 devices,
1808 flags,
1809 config,
1810 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001811
1812 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001813
Phil Burk062e67a2015-02-11 13:40:50 -08001814 if (status == NO_ERROR) {
Eric Laurent83b88082014-06-20 18:31:16 -07001815
1816 PlaybackThread *thread;
1817 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001818 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001819 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001820 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1821 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001822 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001823 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001824 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001825 } else {
Eric Laurent72e3f392015-05-20 14:43:50 -07001826 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001827 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001828 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001829 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001830 return thread;
1831 }
1832
1833 return 0;
1834}
1835
Eric Laurentcf2c0212014-07-25 16:20:43 -07001836status_t AudioFlinger::openOutput(audio_module_handle_t module,
1837 audio_io_handle_t *output,
1838 audio_config_t *config,
1839 audio_devices_t *devices,
1840 const String8& address,
1841 uint32_t *latencyMs,
1842 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001843{
Phil Burk062e67a2015-02-11 13:40:50 -08001844 ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001845 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001846 (devices != NULL) ? *devices : 0,
1847 config->sample_rate,
1848 config->format,
1849 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001850 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001851
Eric Laurentcf2c0212014-07-25 16:20:43 -07001852 if (*devices == AUDIO_DEVICE_NONE) {
1853 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001854 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001855
Mathias Agopian65ab4712010-07-14 17:59:35 -07001856 Mutex::Autolock _l(mLock);
1857
Eric Laurentcf2c0212014-07-25 16:20:43 -07001858 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001859 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001860 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001861
1862 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001863 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001864
1865 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001866 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001867 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001868 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001869
1870 AutoMutex lock(mHardwareLock);
1871 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001872 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001873 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten4ea00a22014-06-02 08:29:22 -07001874
Eric Laurentcf2c0212014-07-25 16:20:43 -07001875 mPrimaryOutputSampleRate = config->sample_rate;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001876 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001877 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001878 }
1879
Eric Laurentcf2c0212014-07-25 16:20:43 -07001880 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001881}
1882
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001883audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1884 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001885{
1886 Mutex::Autolock _l(mLock);
1887 MixerThread *thread1 = checkMixerThread_l(output1);
1888 MixerThread *thread2 = checkMixerThread_l(output2);
1889
1890 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001891 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1892 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001893 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001894 }
1895
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001896 audio_io_handle_t id = nextUniqueId();
Eric Laurent72e3f392015-05-20 14:43:50 -07001897 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001898 thread->addOutputTrack(thread2);
1899 mPlaybackThreads.add(id, thread);
1900 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001901 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001902 return id;
1903}
1904
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001905status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001906{
Glenn Kastend96c5722012-04-25 13:44:49 -07001907 return closeOutput_nonvirtual(output);
1908}
1909
1910status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1911{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001912 // keep strong reference on the playback thread so that
1913 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001914 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001915 {
1916 Mutex::Autolock _l(mLock);
1917 thread = checkPlaybackThread_l(output);
1918 if (thread == NULL) {
1919 return BAD_VALUE;
1920 }
1921
Steve Block3856b092011-10-20 11:56:00 +01001922 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001923
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001924 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001925 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentf6870ae2015-05-08 10:50:03 -07001926 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001927 DuplicatingThread *dupThread =
1928 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001929 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001930 }
1931 }
1932 }
1933
1934
1935 mPlaybackThreads.removeItem(output);
1936 // save all effects to the default thread
1937 if (mPlaybackThreads.size()) {
1938 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1939 if (dstThread != NULL) {
1940 // audioflinger lock is held here so the acquisition order of thread locks does not
1941 // matter
1942 Mutex::Autolock _dl(dstThread->mLock);
1943 Mutex::Autolock _sl(thread->mLock);
1944 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1945 for (size_t i = 0; i < effectChains.size(); i ++) {
1946 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001947 }
1948 }
1949 }
Eric Laurent73e26b62015-04-27 16:55:58 -07001950 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
1951 ioDesc->mIoHandle = output;
1952 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001953 }
1954 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001955 // The thread entity (active unit of execution) is no longer running here,
1956 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001957
Eric Laurentf6870ae2015-05-08 10:50:03 -07001958 if (!thread->isDuplicating()) {
Eric Laurent83b88082014-06-20 18:31:16 -07001959 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001960 }
Eric Laurent83b88082014-06-20 18:31:16 -07001961
Mathias Agopian65ab4712010-07-14 17:59:35 -07001962 return NO_ERROR;
1963}
1964
Eric Laurent83b88082014-06-20 18:31:16 -07001965void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
1966{
1967 AudioStreamOut *out = thread->clearOutput();
1968 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1969 // from now on thread->mOutput is NULL
1970 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
1971 delete out;
1972}
1973
1974void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
1975{
1976 mPlaybackThreads.removeItem(thread->mId);
1977 thread->exit();
1978 closeOutputFinish(thread);
1979}
1980
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001981status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001982{
1983 Mutex::Autolock _l(mLock);
1984 PlaybackThread *thread = checkPlaybackThread_l(output);
1985
1986 if (thread == NULL) {
1987 return BAD_VALUE;
1988 }
1989
Steve Block3856b092011-10-20 11:56:00 +01001990 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001991 thread->suspend();
1992
1993 return NO_ERROR;
1994}
1995
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001996status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001997{
1998 Mutex::Autolock _l(mLock);
1999 PlaybackThread *thread = checkPlaybackThread_l(output);
2000
2001 if (thread == NULL) {
2002 return BAD_VALUE;
2003 }
2004
Steve Block3856b092011-10-20 11:56:00 +01002005 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002006
2007 thread->restore();
2008
2009 return NO_ERROR;
2010}
2011
Eric Laurentcf2c0212014-07-25 16:20:43 -07002012status_t AudioFlinger::openInput(audio_module_handle_t module,
2013 audio_io_handle_t *input,
2014 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002015 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002016 const String8& address,
2017 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002018 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002019{
Eric Laurent83b88082014-06-20 18:31:16 -07002020 Mutex::Autolock _l(mLock);
2021
Glenn Kastene7d66712015-03-05 13:46:52 -08002022 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002023 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002024 }
2025
Glenn Kastene7d66712015-03-05 13:46:52 -08002026 sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002027
2028 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002029 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002030 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002031 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002032 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002033 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002034}
Dima Zavin799a70e2011-04-18 16:57:27 -07002035
Eric Laurent83b88082014-06-20 18:31:16 -07002036sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002037 audio_io_handle_t *input,
2038 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002039 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002040 const String8& address,
2041 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002042 audio_input_flags_t flags)
2043{
Glenn Kastene7d66712015-03-05 13:46:52 -08002044 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002045 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002046 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002047 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002048 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002049
Eric Laurentcf2c0212014-07-25 16:20:43 -07002050 if (*input == AUDIO_IO_HANDLE_NONE) {
2051 *input = nextUniqueId();
2052 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002053
Eric Laurentcf2c0212014-07-25 16:20:43 -07002054 audio_config_t halconfig = *config;
2055 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07002056 audio_stream_in_t *inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002057 status_t status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002058 &inStream, flags, address.string(), source);
2059 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002060 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Dima Zavin799a70e2011-04-18 16:57:27 -07002061 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002062 halconfig.sample_rate,
2063 halconfig.format,
2064 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002065 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002066 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002067
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002068 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002069 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002070 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002071 audio_is_linear_pcm(config->format) &&
2072 audio_is_linear_pcm(halconfig.format) &&
2073 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
Eric Laurentcf2c0212014-07-25 16:20:43 -07002074 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
2075 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002076 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002077 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002078 inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002079 status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002080 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07002081 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002082 }
2083
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002084 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002085
Glenn Kasten46909e72013-02-26 09:20:22 -08002086#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002087 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2088 // or (re-)create if current Pipe is idle and does not match the new format
2089 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002090 enum {
2091 TEE_SINK_NO, // don't copy input
2092 TEE_SINK_NEW, // copy input using a new pipe
2093 TEE_SINK_OLD, // copy input using an existing pipe
2094 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002095 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2096 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002097 if (!mTeeSinkInputEnabled) {
2098 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002099 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002100 kind = TEE_SINK_NO;
2101 } else if (mRecordTeeSink == 0) {
2102 kind = TEE_SINK_NEW;
2103 } else if (mRecordTeeSink->getStrongCount() != 1) {
2104 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002105 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002106 kind = TEE_SINK_OLD;
2107 } else {
2108 kind = TEE_SINK_NEW;
2109 }
2110 switch (kind) {
2111 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002112 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002113 size_t numCounterOffers = 0;
2114 const NBAIO_Format offers[1] = {format};
2115 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2116 ALOG_ASSERT(index == 0);
2117 PipeReader *pipeReader = new PipeReader(*pipe);
2118 numCounterOffers = 0;
2119 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2120 ALOG_ASSERT(index == 0);
2121 mRecordTeeSink = pipe;
2122 mRecordTeeSource = pipeReader;
2123 teeSink = pipe;
2124 }
2125 break;
2126 case TEE_SINK_OLD:
2127 teeSink = mRecordTeeSink;
2128 break;
2129 case TEE_SINK_NO:
2130 default:
2131 break;
2132 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002133#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002134
Eric Laurentcf2c0212014-07-25 16:20:43 -07002135 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002136
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002137 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002138 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002139 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002140 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002141 inputStream,
2142 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002143 primaryOutputDevice_l(),
Eric Laurent72e3f392015-05-20 14:43:50 -07002144 devices,
2145 mSystemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08002146#ifdef TEE_SINK
2147 , teeSink
2148#endif
2149 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002150 mRecordThreads.add(*input, thread);
2151 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002152 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002153 }
2154
Eric Laurentcf2c0212014-07-25 16:20:43 -07002155 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002156 return 0;
2157}
2158
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002159status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002160{
Glenn Kastend96c5722012-04-25 13:44:49 -07002161 return closeInput_nonvirtual(input);
2162}
2163
2164status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2165{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002166 // keep strong reference on the record thread so that
2167 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002168 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002169 {
2170 Mutex::Autolock _l(mLock);
2171 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002172 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002173 return BAD_VALUE;
2174 }
2175
Steve Block3856b092011-10-20 11:56:00 +01002176 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002177
2178 // If we still have effect chains, it means that a client still holds a handle
2179 // on at least one effect. We must either move the chain to an existing thread with the
2180 // same session ID or put it aside in case a new record thread is opened for a
2181 // new capture on the same session
2182 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002183 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002184 Mutex::Autolock _sl(thread->mLock);
2185 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002186 // Note: maximum one chain per record thread
2187 if (effectChains.size() != 0) {
2188 chain = effectChains[0];
2189 }
2190 }
2191 if (chain != 0) {
2192 // first check if a record thread is already opened with a client on the same session.
2193 // This should only happen in case of overlap between one thread tear down and the
2194 // creation of its replacement
2195 size_t i;
2196 for (i = 0; i < mRecordThreads.size(); i++) {
2197 sp<RecordThread> t = mRecordThreads.valueAt(i);
2198 if (t == thread) {
2199 continue;
2200 }
2201 if (t->hasAudioSession(chain->sessionId()) != 0) {
2202 Mutex::Autolock _l(t->mLock);
2203 ALOGV("closeInput() found thread %d for effect session %d",
2204 t->id(), chain->sessionId());
2205 t->addEffectChain_l(chain);
2206 break;
2207 }
2208 }
2209 // put the chain aside if we could not find a record thread with the same session id.
2210 if (i == mRecordThreads.size()) {
2211 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002212 }
2213 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002214 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2215 ioDesc->mIoHandle = input;
2216 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002217 mRecordThreads.removeItem(input);
2218 }
Eric Laurent83b88082014-06-20 18:31:16 -07002219 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2220 // we have a different lock for notification client
2221 closeInputFinish(thread);
2222 return NO_ERROR;
2223}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002224
Eric Laurent83b88082014-06-20 18:31:16 -07002225void AudioFlinger::closeInputFinish(sp<RecordThread> thread)
2226{
2227 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002228 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002229 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002230 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002231 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002232 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002233}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002234
Eric Laurent83b88082014-06-20 18:31:16 -07002235void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
2236{
2237 mRecordThreads.removeItem(thread->mId);
2238 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002239}
2240
Glenn Kastend2304db2014-02-03 07:40:31 -08002241status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002242{
2243 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002244 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002245
2246 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2247 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002248 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002249 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002250
2251 return NO_ERROR;
2252}
2253
2254
Eric Laurentde3f8392014-07-27 18:38:22 -07002255audio_unique_id_t AudioFlinger::newAudioUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002256{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002257 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002258}
2259
Marco Nelissend457c972014-02-11 08:47:07 -08002260void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002261{
2262 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002263 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002264 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2265 if (pid != -1 && (caller == getpid_cached)) {
2266 caller = pid;
2267 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002268
Eric Laurent021cf962014-05-13 10:18:14 -07002269 {
2270 Mutex::Autolock _cl(mClientLock);
2271 // Ignore requests received from processes not known as notification client. The request
2272 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2273 // called from a different pid leaving a stale session reference. Also we don't know how
2274 // to clear this reference if the client process dies.
2275 if (mNotificationClients.indexOfKey(caller) < 0) {
2276 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2277 return;
2278 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002279 }
2280
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002281 size_t num = mAudioSessionRefs.size();
2282 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002283 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002284 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2285 ref->mCnt++;
2286 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002287 return;
2288 }
2289 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002290 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2291 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002292}
2293
Marco Nelissend457c972014-02-11 08:47:07 -08002294void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002295{
2296 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002297 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002298 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2299 if (pid != -1 && (caller == getpid_cached)) {
2300 caller = pid;
2301 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002302 size_t num = mAudioSessionRefs.size();
2303 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002304 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002305 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2306 ref->mCnt--;
2307 ALOGV(" decremented refcount to %d", ref->mCnt);
2308 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002309 mAudioSessionRefs.removeAt(i);
2310 delete ref;
2311 purgeStaleEffects_l();
2312 }
2313 return;
2314 }
2315 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002316 // If the caller is mediaserver it is likely that the session being released was acquired
2317 // on behalf of a process not in notification clients and we ignore the warning.
2318 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002319}
2320
2321void AudioFlinger::purgeStaleEffects_l() {
2322
Steve Block3856b092011-10-20 11:56:00 +01002323 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002324
2325 Vector< sp<EffectChain> > chains;
2326
2327 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2328 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2329 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2330 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002331 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2332 chains.push(ec);
2333 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002334 }
2335 }
2336 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2337 sp<RecordThread> t = mRecordThreads.valueAt(i);
2338 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2339 sp<EffectChain> ec = t->mEffectChains[j];
2340 chains.push(ec);
2341 }
2342 }
2343
2344 for (size_t i = 0; i < chains.size(); i++) {
2345 sp<EffectChain> ec = chains[i];
2346 int sessionid = ec->sessionId();
2347 sp<ThreadBase> t = ec->mThread.promote();
2348 if (t == 0) {
2349 continue;
2350 }
2351 size_t numsessionrefs = mAudioSessionRefs.size();
2352 bool found = false;
2353 for (size_t k = 0; k < numsessionrefs; k++) {
2354 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002355 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002356 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002357 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002358 found = true;
2359 break;
2360 }
2361 }
2362 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002363 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002364 // remove all effects from the chain
2365 while (ec->mEffects.size()) {
2366 sp<EffectModule> effect = ec->mEffects[0];
2367 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002368 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002369 if (effect->purgeHandles()) {
2370 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002371 }
2372 AudioSystem::unregisterEffect(effect->id());
2373 }
2374 }
2375 }
2376 return;
2377}
2378
Mathias Agopian65ab4712010-07-14 17:59:35 -07002379// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002380AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002381{
Glenn Kastena1117922012-01-26 10:53:32 -08002382 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002383}
2384
2385// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002386AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002387{
2388 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002389 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002390}
2391
2392// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002393AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002394{
Glenn Kastena1117922012-01-26 10:53:32 -08002395 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002396}
2397
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002398uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002399{
Glenn Kastenbcefec32014-01-17 12:09:05 -08002400 return (uint32_t) android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002401}
2402
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002403AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002404{
2405 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2406 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002407 if(thread->isDuplicating()) {
2408 continue;
2409 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002410 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002411 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002412 return thread;
2413 }
2414 }
2415 return NULL;
2416}
2417
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002418audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002419{
2420 PlaybackThread *thread = primaryPlaybackThread_l();
2421
2422 if (thread == NULL) {
2423 return 0;
2424 }
2425
Eric Laurentf1c04f92012-08-28 14:26:53 -07002426 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002427}
2428
Eric Laurenta011e352012-03-29 15:51:43 -07002429sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2430 int triggerSession,
2431 int listenerSession,
2432 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002433 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002434{
2435 Mutex::Autolock _l(mLock);
2436
2437 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2438 status_t playStatus = NAME_NOT_FOUND;
2439 status_t recStatus = NAME_NOT_FOUND;
2440 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2441 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2442 if (playStatus == NO_ERROR) {
2443 return event;
2444 }
2445 }
2446 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2447 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2448 if (recStatus == NO_ERROR) {
2449 return event;
2450 }
2451 }
2452 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2453 mPendingSyncEvents.add(event);
2454 } else {
2455 ALOGV("createSyncEvent() invalid event %d", event->type());
2456 event.clear();
2457 }
2458 return event;
2459}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002460
Mathias Agopian65ab4712010-07-14 17:59:35 -07002461// ----------------------------------------------------------------------------
2462// Effect management
2463// ----------------------------------------------------------------------------
2464
2465
Glenn Kastenf587ba52012-01-26 16:25:10 -08002466status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002467{
2468 Mutex::Autolock _l(mLock);
2469 return EffectQueryNumberEffects(numEffects);
2470}
2471
Glenn Kastenf587ba52012-01-26 16:25:10 -08002472status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002473{
2474 Mutex::Autolock _l(mLock);
2475 return EffectQueryEffect(index, descriptor);
2476}
2477
Glenn Kasten5e92a782012-01-30 07:40:52 -08002478status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002479 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002480{
2481 Mutex::Autolock _l(mLock);
2482 return EffectGetDescriptor(pUuid, descriptor);
2483}
2484
2485
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002486sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002487 effect_descriptor_t *pDesc,
2488 const sp<IEffectClient>& effectClient,
2489 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002490 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002491 int sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002492 const String16& opPackageName,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002493 status_t *status,
2494 int *id,
2495 int *enabled)
2496{
2497 status_t lStatus = NO_ERROR;
2498 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002499 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002500
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002501 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002502 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002503 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002504
2505 if (pDesc == NULL) {
2506 lStatus = BAD_VALUE;
2507 goto Exit;
2508 }
2509
Eric Laurent84e9a102010-09-23 16:10:16 -07002510 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002511 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002512 lStatus = PERMISSION_DENIED;
2513 goto Exit;
2514 }
2515
Dima Zavinfce7a472011-04-19 22:30:36 -07002516 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002517 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002518 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002519 lStatus = PERMISSION_DENIED;
2520 goto Exit;
2521 }
2522
Mathias Agopian65ab4712010-07-14 17:59:35 -07002523 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002524 if (!EffectIsNullUuid(&pDesc->uuid)) {
2525 // if uuid is specified, request effect descriptor
2526 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2527 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002528 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002529 goto Exit;
2530 }
2531 } else {
2532 // if uuid is not specified, look for an available implementation
2533 // of the required type in effect factory
2534 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002535 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002536 lStatus = BAD_VALUE;
2537 goto Exit;
2538 }
2539 uint32_t numEffects = 0;
2540 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002541 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002542 bool found = false;
2543
2544 lStatus = EffectQueryNumberEffects(&numEffects);
2545 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002546 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002547 goto Exit;
2548 }
2549 for (uint32_t i = 0; i < numEffects; i++) {
2550 lStatus = EffectQueryEffect(i, &desc);
2551 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002552 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002553 continue;
2554 }
2555 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2556 // If matching type found save effect descriptor. If the session is
2557 // 0 and the effect is not auxiliary, continue enumeration in case
2558 // an auxiliary version of this effect type is available
2559 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002560 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002561 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002562 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2563 break;
2564 }
2565 }
2566 }
2567 if (!found) {
2568 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002569 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002570 goto Exit;
2571 }
2572 // For same effect type, chose auxiliary version over insert version if
2573 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002574 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002575 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002576 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002577 }
2578 }
2579
2580 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002581 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002582 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2583 lStatus = INVALID_OPERATION;
2584 goto Exit;
2585 }
2586
Eric Laurent59255e42011-07-27 19:49:51 -07002587 // check recording permission for visualizer
2588 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Svet Ganovbe71aa22015-04-28 12:06:02 -07002589 !recordingAllowed(opPackageName)) {
Eric Laurent59255e42011-07-27 19:49:51 -07002590 lStatus = PERMISSION_DENIED;
2591 goto Exit;
2592 }
2593
Mathias Agopian65ab4712010-07-14 17:59:35 -07002594 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002595 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002596 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002597 // if the output returned by getOutputForEffect() is removed before we lock the
2598 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2599 // and we will exit safely
2600 io = AudioSystem::getOutputForEffect(&desc);
2601 ALOGV("createEffect got output %d", io);
2602 }
2603
2604 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002605
2606 // If output is not specified try to find a matching audio session ID in one of the
2607 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002608 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2609 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002610 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002611 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002612 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2613 // output must be specified by AudioPolicyManager when using session
2614 // AUDIO_SESSION_OUTPUT_STAGE
2615 lStatus = BAD_VALUE;
2616 goto Exit;
2617 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002618 // look for the thread where the specified audio session is present
2619 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2620 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2621 io = mPlaybackThreads.keyAt(i);
2622 break;
2623 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002624 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002625 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002626 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2627 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2628 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002629 break;
2630 }
2631 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002632 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002633 // If no output thread contains the requested session ID, default to
2634 // first output. The effect chain will be moved to the correct output
2635 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002636 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002637 io = mPlaybackThreads.keyAt(0);
2638 }
Steve Block3856b092011-10-20 11:56:00 +01002639 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002640 }
2641 ThreadBase *thread = checkRecordThread_l(io);
2642 if (thread == NULL) {
2643 thread = checkPlaybackThread_l(io);
2644 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002645 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002646 lStatus = BAD_VALUE;
2647 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002648 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002649 } else {
2650 // Check if one effect chain was awaiting for an effect to be created on this
2651 // session and used it instead of creating a new one.
2652 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)sessionId);
2653 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002654 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002655 thread->addEffectChain_l(chain);
2656 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002657 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002658
Eric Laurent021cf962014-05-13 10:18:14 -07002659 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002660
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002661 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002662 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2663 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002664 if (handle != 0 && id != NULL) {
2665 *id = handle->id();
2666 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002667 if (handle == 0) {
2668 // remove local strong reference to Client with mClientLock held
2669 Mutex::Autolock _cl(mClientLock);
2670 client.clear();
2671 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002672 }
2673
2674Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002675 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002676 return handle;
2677}
2678
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002679status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2680 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002681{
Steve Block3856b092011-10-20 11:56:00 +01002682 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002683 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002684 Mutex::Autolock _l(mLock);
2685 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002686 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002687 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002688 }
Eric Laurentde070132010-07-13 04:45:46 -07002689 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2690 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002691 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002692 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002693 }
Eric Laurentde070132010-07-13 04:45:46 -07002694 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2695 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002696 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002697 return BAD_VALUE;
2698 }
2699
2700 Mutex::Autolock _dl(dstThread->mLock);
2701 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002702 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002703}
2704
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002705// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002706status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002707 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002708 AudioFlinger::PlaybackThread *dstThread,
2709 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002710{
Steve Block3856b092011-10-20 11:56:00 +01002711 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002712 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002713
Eric Laurent59255e42011-07-27 19:49:51 -07002714 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002715 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002716 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002717 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002718 return INVALID_OPERATION;
2719 }
2720
Andy Hung9a592762014-07-21 21:56:01 -07002721 // Check whether the destination thread has a channel count of FCC_2, which is
2722 // currently required for (most) effects. Prevent moving the effect chain here rather
2723 // than disabling the addEffect_l() call in dstThread below.
Eric Laurentf6870ae2015-05-08 10:50:03 -07002724 if ((dstThread->type() == ThreadBase::MIXER || dstThread->isDuplicating()) &&
Eric Laurentb10352f2014-11-03 16:25:39 -08002725 dstThread->mChannelCount != FCC_2) {
Andy Hung9a592762014-07-21 21:56:01 -07002726 ALOGW("moveEffectChain_l() effect chain failed because"
2727 " destination thread %p channel count(%u) != %u",
2728 dstThread, dstThread->mChannelCount, FCC_2);
2729 return INVALID_OPERATION;
2730 }
2731
Eric Laurent39e94f82010-07-28 01:32:47 -07002732 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002733 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002734 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002735 // removed.
2736 srcThread->removeEffectChain_l(chain);
2737
2738 // transfer all effects one by one so that new effect chain is created on new thread with
2739 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002740 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002741 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002742 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002743 Vector< sp<EffectModule> > removed;
2744 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002745 while (effect != 0) {
2746 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002747 removed.add(effect);
2748 status = dstThread->addEffect_l(effect);
2749 if (status != NO_ERROR) {
2750 break;
2751 }
Eric Laurentec35a142011-10-05 17:42:25 -07002752 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2753 if (effect->state() == EffectModule::ACTIVE ||
2754 effect->state() == EffectModule::STOPPING) {
2755 effect->start();
2756 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002757 // if the move request is not received from audio policy manager, the effect must be
2758 // re-registered with the new strategy and output
2759 if (dstChain == 0) {
2760 dstChain = effect->chain().promote();
2761 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002762 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002763 status = NO_INIT;
2764 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002765 }
2766 strategy = dstChain->strategy();
2767 }
2768 if (reRegister) {
2769 AudioSystem::unregisterEffect(effect->id());
2770 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002771 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002772 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002773 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002774 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002775 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002776 }
Eric Laurentde070132010-07-13 04:45:46 -07002777 effect = chain->getEffectFromId_l(0);
2778 }
2779
Eric Laurent5baf2af2013-09-12 17:37:00 -07002780 if (status != NO_ERROR) {
2781 for (size_t i = 0; i < removed.size(); i++) {
2782 srcThread->addEffect_l(removed[i]);
2783 if (dstChain != 0 && reRegister) {
2784 AudioSystem::unregisterEffect(removed[i]->id());
2785 AudioSystem::registerEffect(&removed[i]->desc(),
2786 srcThread->id(),
2787 strategy,
2788 sessionId,
2789 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002790 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002791 }
2792 }
2793 }
2794
2795 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002796}
2797
Eric Laurent5baf2af2013-09-12 17:37:00 -07002798bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002799{
2800 if (mGlobalEffectEnableTime != 0 &&
2801 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2802 return true;
2803 }
2804
2805 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2806 sp<EffectChain> ec =
2807 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002808 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002809 return true;
2810 }
2811 }
2812 return false;
2813}
2814
Eric Laurent5baf2af2013-09-12 17:37:00 -07002815void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002816{
2817 Mutex::Autolock _l(mLock);
2818
2819 mGlobalEffectEnableTime = systemTime();
2820
2821 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2822 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2823 if (t->mType == ThreadBase::OFFLOAD) {
2824 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2825 }
2826 }
2827
2828}
2829
Eric Laurentaaa44472014-09-12 17:41:50 -07002830status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2831{
2832 audio_session_t session = (audio_session_t)chain->sessionId();
2833 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2834 ALOGV("putOrphanEffectChain_l session %d index %d", session, index);
2835 if (index >= 0) {
2836 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2837 return ALREADY_EXISTS;
2838 }
2839 mOrphanEffectChains.add(session, chain);
2840 return NO_ERROR;
2841}
2842
2843sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2844{
2845 sp<EffectChain> chain;
2846 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2847 ALOGV("getOrphanEffectChain_l session %d index %d", session, index);
2848 if (index >= 0) {
2849 chain = mOrphanEffectChains.valueAt(index);
2850 mOrphanEffectChains.removeItemsAt(index);
2851 }
2852 return chain;
2853}
2854
2855bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2856{
2857 Mutex::Autolock _l(mLock);
2858 audio_session_t session = (audio_session_t)effect->sessionId();
2859 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2860 ALOGV("updateOrphanEffectChains session %d index %d", session, index);
2861 if (index >= 0) {
2862 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2863 if (chain->removeEffect_l(effect) == 0) {
2864 ALOGV("updateOrphanEffectChains removing effect chain at index %d", index);
2865 mOrphanEffectChains.removeItemsAt(index);
2866 }
2867 return true;
2868 }
2869 return false;
2870}
2871
2872
Glenn Kastenda6ef132013-01-10 12:31:01 -08002873struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002874#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
2875 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002876};
2877
2878int comparEntry(const void *p1, const void *p2)
2879{
Glenn Kasten2f55e762015-03-05 16:05:08 -08002880 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002881}
2882
Glenn Kasten46909e72013-02-26 09:20:22 -08002883#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002884void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002885{
Eric Laurent81784c32012-11-19 14:55:58 -08002886 NBAIO_Source *teeSource = source.get();
2887 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002888 // .wav rotation
2889 // There is a benign race condition if 2 threads call this simultaneously.
2890 // They would both traverse the directory, but the result would simply be
2891 // failures at unlink() which are ignored. It's also unlikely since
2892 // normally dumpsys is only done by bugreport or from the command line.
2893 char teePath[32+256];
2894 strcpy(teePath, "/data/misc/media");
2895 size_t teePathLen = strlen(teePath);
2896 DIR *dir = opendir(teePath);
2897 teePath[teePathLen++] = '/';
2898 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002899#define TEE_MAX_SORT 20 // number of entries to sort
2900#define TEE_MAX_KEEP 10 // number of entries to keep
2901 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002902 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08002903 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002904 struct dirent de;
2905 struct dirent *result = NULL;
2906 int rc = readdir_r(dir, &de, &result);
2907 if (rc != 0) {
2908 ALOGW("readdir_r failed %d", rc);
2909 break;
2910 }
2911 if (result == NULL) {
2912 break;
2913 }
2914 if (result != &de) {
2915 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2916 break;
2917 }
2918 // ignore non .wav file entries
2919 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002920 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08002921 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2922 continue;
2923 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08002924 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002925 }
2926 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002927 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002928 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002929 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
2930 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002931 (void) unlink(teePath);
2932 }
2933 }
2934 } else {
2935 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002936 dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002937 }
2938 }
Eric Laurent81784c32012-11-19 14:55:58 -08002939 char teeTime[16];
2940 struct timeval tv;
2941 gettimeofday(&tv, NULL);
2942 struct tm tm;
2943 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002944 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2945 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2946 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2947 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002948 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07002949 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08002950 char wavHeader[44];
2951 memcpy(wavHeader,
2952 "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",
2953 sizeof(wavHeader));
2954 NBAIO_Format format = teeSource->format();
2955 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002956 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07002957 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002958 wavHeader[22] = channelCount; // number of channels
2959 wavHeader[24] = sampleRate; // sample rate
2960 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07002961 wavHeader[32] = frameSize; // block alignment
2962 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08002963 write(teeFd, wavHeader, sizeof(wavHeader));
2964 size_t total = 0;
2965 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07002966#define TEE_SINK_READ 1024 // frames per I/O operation
2967 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002968 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08002969 size_t count = TEE_SINK_READ;
2970 ssize_t actual = teeSource->read(buffer, count,
2971 AudioBufferProvider::kInvalidPTS);
2972 bool wasFirstRead = firstRead;
2973 firstRead = false;
2974 if (actual <= 0) {
2975 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2976 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002977 }
Eric Laurent81784c32012-11-19 14:55:58 -08002978 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002979 }
Eric Laurent81784c32012-11-19 14:55:58 -08002980 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07002981 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002982 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002983 }
Glenn Kasten329f6512014-08-28 16:23:16 -07002984 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08002985 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002986 uint32_t temp = 44 + total * frameSize - 8;
2987 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002988 write(teeFd, &temp, sizeof(temp));
2989 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002990 temp = total * frameSize;
2991 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002992 write(teeFd, &temp, sizeof(temp));
2993 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002994 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002995 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002996 }
Eric Laurent59255e42011-07-27 19:49:51 -07002997 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002998 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002999 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08003000 }
Eric Laurent59255e42011-07-27 19:49:51 -07003001 }
3002 }
3003}
Glenn Kasten46909e72013-02-26 09:20:22 -08003004#endif
Eric Laurent59255e42011-07-27 19:49:51 -07003005
Mathias Agopian65ab4712010-07-14 17:59:35 -07003006// ----------------------------------------------------------------------------
3007
3008status_t AudioFlinger::onTransact(
3009 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3010{
3011 return BnAudioFlinger::onTransact(code, data, reply, flags);
3012}
3013
Glenn Kasten63238ef2015-03-02 15:50:29 -08003014} // namespace android