blob: 8f1e050792880300fedde5be4177bd19c9c58adf [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 Laurent7c1ec5f2015-07-09 14:52:47 -07001255 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001256 {
1257 Mutex::Autolock _cl(mClientLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001258 if (mNotificationClients.indexOfKey(pid) < 0) {
1259 sp<NotificationClient> notificationClient = new NotificationClient(this,
1260 client,
1261 pid);
1262 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001263
Eric Laurent021cf962014-05-13 10:18:14 -07001264 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001265
Marco Nelissen06b46062014-11-14 07:58:25 -08001266 sp<IBinder> binder = IInterface::asBinder(client);
Eric Laurent021cf962014-05-13 10:18:14 -07001267 binder->linkToDeath(notificationClient);
Eric Laurent021cf962014-05-13 10:18:14 -07001268 }
1269 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001270
Eric Laurent021cf962014-05-13 10:18:14 -07001271 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001272 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001273 // the config change is always sent from playback or record threads to avoid deadlock
1274 // with AudioSystem::gLock
1275 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1276 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_OPENED, pid);
1277 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001278
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001279 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1280 mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_OPENED, pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001281 }
1282}
1283
1284void AudioFlinger::removeNotificationClient(pid_t pid)
1285{
1286 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001287 {
1288 Mutex::Autolock _cl(mClientLock);
1289 mNotificationClients.removeItem(pid);
1290 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001291
Steve Block3856b092011-10-20 11:56:00 +01001292 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001293 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001294 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001295 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001296 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001297 ALOGV(" pid %d @ %d", ref->mPid, i);
1298 if (ref->mPid == pid) {
1299 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001300 mAudioSessionRefs.removeAt(i);
1301 delete ref;
1302 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001303 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001304 } else {
1305 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001306 }
1307 }
1308 if (removed) {
1309 purgeStaleEffects_l();
1310 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001311}
1312
Eric Laurent73e26b62015-04-27 16:55:58 -07001313void AudioFlinger::ioConfigChanged(audio_io_config_event event,
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001314 const sp<AudioIoDescriptor>& ioDesc,
1315 pid_t pid)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001316{
Eric Laurent021cf962014-05-13 10:18:14 -07001317 Mutex::Autolock _l(mClientLock);
1318 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001319 for (size_t i = 0; i < size; i++) {
Eric Laurent7c1ec5f2015-07-09 14:52:47 -07001320 if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) {
1321 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc);
1322 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001323 }
1324}
1325
Eric Laurent021cf962014-05-13 10:18:14 -07001326// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001327void AudioFlinger::removeClient_l(pid_t pid)
1328{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001329 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001330 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001331 mClients.removeItem(pid);
1332}
1333
Eric Laurent717e1282012-06-29 16:36:52 -07001334// getEffectThread_l() must be called with AudioFlinger::mLock held
1335sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1336{
1337 sp<PlaybackThread> thread;
1338
1339 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1340 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1341 ALOG_ASSERT(thread == 0);
1342 thread = mPlaybackThreads.valueAt(i);
1343 }
1344 }
1345
1346 return thread;
1347}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001348
Mathias Agopian65ab4712010-07-14 17:59:35 -07001349
Mathias Agopian65ab4712010-07-14 17:59:35 -07001350
1351// ----------------------------------------------------------------------------
1352
1353AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1354 : RefBase(),
1355 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001356 // 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 -07001357 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001358 mPid(pid),
1359 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001360{
1361 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1362}
1363
Eric Laurent021cf962014-05-13 10:18:14 -07001364// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001365AudioFlinger::Client::~Client()
1366{
1367 mAudioFlinger->removeClient_l(mPid);
1368}
1369
Glenn Kasten435dbe62012-01-30 10:15:48 -08001370sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001371{
1372 return mMemoryDealer;
1373}
1374
John Grossman4ff14ba2012-02-08 16:37:41 -08001375// Reserve one of the limited slots for a timed audio track associated
1376// with this client
1377bool AudioFlinger::Client::reserveTimedTrack()
1378{
1379 const int kMaxTimedTracksPerClient = 4;
1380
1381 Mutex::Autolock _l(mTimedTrackLock);
1382
1383 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1384 ALOGW("can not create timed track - pid %d has exceeded the limit",
1385 mPid);
1386 return false;
1387 }
1388
1389 mTimedTrackCount++;
1390 return true;
1391}
1392
1393// Release a slot for a timed audio track
1394void AudioFlinger::Client::releaseTimedTrack()
1395{
1396 Mutex::Autolock _l(mTimedTrackLock);
1397 mTimedTrackCount--;
1398}
1399
Mathias Agopian65ab4712010-07-14 17:59:35 -07001400// ----------------------------------------------------------------------------
1401
1402AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1403 const sp<IAudioFlingerClient>& client,
1404 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001405 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001406{
1407}
1408
1409AudioFlinger::NotificationClient::~NotificationClient()
1410{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001411}
1412
Glenn Kasten0f11b512014-01-31 16:18:54 -08001413void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001414{
1415 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001416 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001417}
1418
Mathias Agopian65ab4712010-07-14 17:59:35 -07001419
1420// ----------------------------------------------------------------------------
1421
Jeff Brown893a5642013-08-16 20:19:26 -07001422static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1423 return audio_is_remote_submix_device(inDevice);
1424}
1425
Mathias Agopian65ab4712010-07-14 17:59:35 -07001426sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001427 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001428 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001429 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001430 audio_channel_mask_t channelMask,
Svet Ganovbe71aa22015-04-28 12:06:02 -07001431 const String16& opPackageName,
Glenn Kasten74935e42013-12-19 08:56:45 -08001432 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001433 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001434 pid_t tid,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001435 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001436 int *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001437 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001438 sp<IMemory>& cblk,
1439 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001440 status_t *status)
1441{
1442 sp<RecordThread::RecordTrack> recordTrack;
1443 sp<RecordHandle> recordHandle;
1444 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001445 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001446 int lSessionId;
1447
Glenn Kastend776ac62014-05-07 09:16:09 -07001448 cblk.clear();
1449 buffers.clear();
1450
Mathias Agopian65ab4712010-07-14 17:59:35 -07001451 // check calling permissions
Svet Ganovbe71aa22015-04-28 12:06:02 -07001452 if (!recordingAllowed(opPackageName)) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001453 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001454 lStatus = PERMISSION_DENIED;
1455 goto Exit;
1456 }
1457
Glenn Kasten53b5d092014-02-05 10:00:23 -08001458 // further sample rate checks are performed by createRecordTrack_l()
1459 if (sampleRate == 0) {
1460 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1461 lStatus = BAD_VALUE;
1462 goto Exit;
1463 }
1464
Andy Hung6770c6f2015-04-07 13:43:36 -07001465 // we don't yet support anything other than linear PCM
1466 if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001467 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001468 lStatus = BAD_VALUE;
1469 goto Exit;
1470 }
1471
Glenn Kasten53b5d092014-02-05 10:00:23 -08001472 // further channel mask checks are performed by createRecordTrack_l()
1473 if (!audio_is_input_channel(channelMask)) {
1474 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1475 lStatus = BAD_VALUE;
1476 goto Exit;
1477 }
1478
Glenn Kasten05997e22014-03-13 15:08:33 -07001479 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001480 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001481 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001482 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001483 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001484 lStatus = BAD_VALUE;
1485 goto Exit;
1486 }
1487
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001488 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001489 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001490
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001491 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001492 lSessionId = *sessionId;
1493 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001494 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001495 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001496 if (sessionId != NULL) {
1497 *sessionId = lSessionId;
1498 }
1499 }
Eric Laurentaaa44472014-09-12 17:41:50 -07001500 ALOGV("openRecord() lSessionId: %d input %d", lSessionId, input);
Glenn Kasten570f6332014-03-13 15:01:06 -07001501
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001502 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001503 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001504 frameCount, lSessionId, notificationFrames,
Jean-Michel Trivi4cb66832015-05-01 18:34:17 -07001505 clientUid, flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001506 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Eric Laurent1b928682014-10-02 19:41:47 -07001507
1508 if (lStatus == NO_ERROR) {
1509 // Check if one effect chain was awaiting for an AudioRecord to be created on this
1510 // session and move it to this thread.
1511 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)lSessionId);
1512 if (chain != 0) {
1513 Mutex::Autolock _l(thread->mLock);
1514 thread->addEffectChain_l(chain);
1515 }
1516 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001517 }
Glenn Kastene198c362013-08-13 09:13:36 -07001518
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001519 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001520 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001521 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001522 // Don't hold mClientLock when releasing the reference on the track as the
1523 // destructor will acquire it.
1524 {
1525 Mutex::Autolock _cl(mClientLock);
1526 client.clear();
1527 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001528 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001529 goto Exit;
1530 }
1531
Glenn Kastend776ac62014-05-07 09:16:09 -07001532 cblk = recordTrack->getCblk();
1533 buffers = recordTrack->getBuffers();
1534
Glenn Kasten2fc14732013-08-05 14:58:14 -07001535 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001536 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001537
1538Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001539 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001540 return recordHandle;
1541}
1542
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001543
1544
Mathias Agopian65ab4712010-07-14 17:59:35 -07001545// ----------------------------------------------------------------------------
1546
Eric Laurenta4c5a552012-03-29 10:12:40 -07001547audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1548{
Eric Laurent44622db2014-08-01 19:00:33 -07001549 if (name == NULL) {
1550 return 0;
1551 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001552 if (!settingsAllowed()) {
1553 return 0;
1554 }
1555 Mutex::Autolock _l(mLock);
1556 return loadHwModule_l(name);
1557}
1558
1559// loadHwModule_l() must be called with AudioFlinger::mLock held
1560audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1561{
1562 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1563 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1564 ALOGW("loadHwModule() module %s already loaded", name);
1565 return mAudioHwDevs.keyAt(i);
1566 }
1567 }
1568
Eric Laurenta4c5a552012-03-29 10:12:40 -07001569 audio_hw_device_t *dev;
1570
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001571 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001572 if (rc) {
1573 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1574 return 0;
1575 }
1576
1577 mHardwareStatus = AUDIO_HW_INIT;
1578 rc = dev->init_check(dev);
1579 mHardwareStatus = AUDIO_HW_IDLE;
1580 if (rc) {
1581 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1582 return 0;
1583 }
1584
John Grossmanee578c02012-07-23 17:05:46 -07001585 // Check and cache this HAL's level of support for master mute and master
1586 // volume. If this is the first HAL opened, and it supports the get
1587 // methods, use the initial values provided by the HAL as the current
1588 // master mute and volume settings.
1589
1590 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1591 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001592 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001593
1594 if (0 == mAudioHwDevs.size()) {
1595 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1596 if (NULL != dev->get_master_volume) {
1597 float mv;
1598 if (OK == dev->get_master_volume(dev, &mv)) {
1599 mMasterVolume = mv;
1600 }
1601 }
1602
1603 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1604 if (NULL != dev->get_master_mute) {
1605 bool mm;
1606 if (OK == dev->get_master_mute(dev, &mm)) {
1607 mMasterMute = mm;
1608 }
1609 }
1610 }
1611
Eric Laurenta4c5a552012-03-29 10:12:40 -07001612 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001613 if ((NULL != dev->set_master_volume) &&
1614 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1615 flags = static_cast<AudioHwDevice::Flags>(flags |
1616 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1617 }
1618
1619 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1620 if ((NULL != dev->set_master_mute) &&
1621 (OK == dev->set_master_mute(dev, mMasterMute))) {
1622 flags = static_cast<AudioHwDevice::Flags>(flags |
1623 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1624 }
1625
Eric Laurenta4c5a552012-03-29 10:12:40 -07001626 mHardwareStatus = AUDIO_HW_IDLE;
1627 }
1628
1629 audio_module_handle_t handle = nextUniqueId();
Eric Laurent83b88082014-06-20 18:31:16 -07001630 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001631
1632 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001633 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001634
1635 return handle;
1636
1637}
1638
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001639// ----------------------------------------------------------------------------
1640
Glenn Kasten3b16c762012-11-14 08:44:39 -08001641uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001642{
1643 Mutex::Autolock _l(mLock);
1644 PlaybackThread *thread = primaryPlaybackThread_l();
1645 return thread != NULL ? thread->sampleRate() : 0;
1646}
1647
Glenn Kastene33054e2012-11-14 12:54:39 -08001648size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001649{
1650 Mutex::Autolock _l(mLock);
1651 PlaybackThread *thread = primaryPlaybackThread_l();
1652 return thread != NULL ? thread->frameCountHAL() : 0;
1653}
1654
1655// ----------------------------------------------------------------------------
1656
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001657status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1658{
1659 uid_t uid = IPCThreadState::self()->getCallingUid();
1660 if (uid != AID_SYSTEM) {
1661 return PERMISSION_DENIED;
1662 }
1663 Mutex::Autolock _l(mLock);
1664 if (mIsDeviceTypeKnown) {
1665 return INVALID_OPERATION;
1666 }
1667 mIsLowRamDevice = isLowRamDevice;
1668 mIsDeviceTypeKnown = true;
1669 return NO_ERROR;
1670}
1671
Eric Laurent93c3d412014-08-01 14:48:35 -07001672audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1673{
1674 Mutex::Autolock _l(mLock);
Eric Laurentfa90e842014-10-17 18:12:31 -07001675
1676 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1677 if (index >= 0) {
1678 ALOGV("getAudioHwSyncForSession found ID %d for session %d",
1679 mHwAvSyncIds.valueAt(index), sessionId);
1680 return mHwAvSyncIds.valueAt(index);
1681 }
1682
1683 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1684 if (dev == NULL) {
1685 return AUDIO_HW_SYNC_INVALID;
1686 }
1687 char *reply = dev->get_parameters(dev, AUDIO_PARAMETER_HW_AV_SYNC);
1688 AudioParameter param = AudioParameter(String8(reply));
1689 free(reply);
1690
1691 int value;
1692 if (param.getInt(String8(AUDIO_PARAMETER_HW_AV_SYNC), value) != NO_ERROR) {
1693 ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId);
1694 return AUDIO_HW_SYNC_INVALID;
1695 }
1696
1697 // allow only one session for a given HW A/V sync ID.
1698 for (size_t i = 0; i < mHwAvSyncIds.size(); i++) {
1699 if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) {
1700 ALOGV("getAudioHwSyncForSession removing ID %d for session %d",
1701 value, mHwAvSyncIds.keyAt(i));
1702 mHwAvSyncIds.removeItemsAt(i);
Eric Laurent93c3d412014-08-01 14:48:35 -07001703 break;
1704 }
1705 }
Eric Laurentfa90e842014-10-17 18:12:31 -07001706
1707 mHwAvSyncIds.add(sessionId, value);
1708
1709 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1710 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1711 uint32_t sessions = thread->hasAudioSession(sessionId);
1712 if (sessions & PlaybackThread::TRACK_SESSION) {
1713 AudioParameter param = AudioParameter();
1714 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value);
1715 thread->setParameters(param.toString());
1716 break;
1717 }
1718 }
1719
1720 ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId);
1721 return (audio_hw_sync_t)value;
Eric Laurent93c3d412014-08-01 14:48:35 -07001722}
1723
Eric Laurent72e3f392015-05-20 14:43:50 -07001724status_t AudioFlinger::systemReady()
1725{
1726 Mutex::Autolock _l(mLock);
1727 ALOGI("%s", __FUNCTION__);
1728 if (mSystemReady) {
1729 ALOGW("%s called twice", __FUNCTION__);
1730 return NO_ERROR;
1731 }
1732 mSystemReady = true;
1733 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1734 ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get();
1735 thread->systemReady();
1736 }
1737 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1738 ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get();
1739 thread->systemReady();
1740 }
1741 return NO_ERROR;
1742}
1743
Eric Laurentfa90e842014-10-17 18:12:31 -07001744// setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held
1745void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId)
1746{
1747 ssize_t index = mHwAvSyncIds.indexOfKey(sessionId);
1748 if (index >= 0) {
1749 audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index);
1750 ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId);
1751 AudioParameter param = AudioParameter();
1752 param.addInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), syncId);
1753 thread->setParameters(param.toString());
1754 }
1755}
1756
1757
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001758// ----------------------------------------------------------------------------
1759
Eric Laurent83b88082014-06-20 18:31:16 -07001760
1761sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001762 audio_io_handle_t *output,
1763 audio_config_t *config,
1764 audio_devices_t devices,
1765 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001766 audio_output_flags_t flags)
1767{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001768 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001769 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001770 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001771 }
1772
1773 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentcf2c0212014-07-25 16:20:43 -07001774 if (*output == AUDIO_IO_HANDLE_NONE) {
1775 *output = nextUniqueId();
1776 }
Eric Laurent83b88082014-06-20 18:31:16 -07001777
1778 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1779
Eric Laurent83b88082014-06-20 18:31:16 -07001780 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001781 // This if statement allows overriding the audio policy settings
1782 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1783 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1784 // Check only for Normal Mixing mode
1785 if (kEnableExtendedPrecision) {
1786 // Specify format (uncomment one below to choose)
1787 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1788 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1789 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1790 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001791 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001792 }
1793 if (kEnableExtendedChannels) {
1794 // Specify channel mask (uncomment one below to choose)
1795 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1796 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1797 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1798 }
Eric Laurent83b88082014-06-20 18:31:16 -07001799 }
1800
Phil Burk062e67a2015-02-11 13:40:50 -08001801 AudioStreamOut *outputStream = NULL;
1802 status_t status = outHwDev->openOutputStream(
1803 &outputStream,
1804 *output,
1805 devices,
1806 flags,
1807 config,
1808 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001809
1810 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent83b88082014-06-20 18:31:16 -07001811
Phil Burk062e67a2015-02-11 13:40:50 -08001812 if (status == NO_ERROR) {
Eric Laurent83b88082014-06-20 18:31:16 -07001813
1814 PlaybackThread *thread;
1815 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001816 thread = new OffloadThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001817 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001818 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1819 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001820 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurent72e3f392015-05-20 14:43:50 -07001821 thread = new DirectOutputThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001822 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001823 } else {
Eric Laurent72e3f392015-05-20 14:43:50 -07001824 thread = new MixerThread(this, outputStream, *output, devices, mSystemReady);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001825 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001826 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001827 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001828 return thread;
1829 }
1830
1831 return 0;
1832}
1833
Eric Laurentcf2c0212014-07-25 16:20:43 -07001834status_t AudioFlinger::openOutput(audio_module_handle_t module,
1835 audio_io_handle_t *output,
1836 audio_config_t *config,
1837 audio_devices_t *devices,
1838 const String8& address,
1839 uint32_t *latencyMs,
1840 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001841{
Phil Burk062e67a2015-02-11 13:40:50 -08001842 ALOGI("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001843 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001844 (devices != NULL) ? *devices : 0,
1845 config->sample_rate,
1846 config->format,
1847 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001848 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001849
Eric Laurentcf2c0212014-07-25 16:20:43 -07001850 if (*devices == AUDIO_DEVICE_NONE) {
1851 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001852 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001853
Mathias Agopian65ab4712010-07-14 17:59:35 -07001854 Mutex::Autolock _l(mLock);
1855
Eric Laurentcf2c0212014-07-25 16:20:43 -07001856 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001857 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001858 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001859
1860 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001861 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001862
1863 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001864 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001865 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001866 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001867
1868 AutoMutex lock(mHardwareLock);
1869 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001870 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001871 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten4ea00a22014-06-02 08:29:22 -07001872
Eric Laurentcf2c0212014-07-25 16:20:43 -07001873 mPrimaryOutputSampleRate = config->sample_rate;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001874 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001875 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001876 }
1877
Eric Laurentcf2c0212014-07-25 16:20:43 -07001878 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001879}
1880
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001881audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1882 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001883{
1884 Mutex::Autolock _l(mLock);
1885 MixerThread *thread1 = checkMixerThread_l(output1);
1886 MixerThread *thread2 = checkMixerThread_l(output2);
1887
1888 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001889 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1890 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001891 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001892 }
1893
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001894 audio_io_handle_t id = nextUniqueId();
Eric Laurent72e3f392015-05-20 14:43:50 -07001895 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001896 thread->addOutputTrack(thread2);
1897 mPlaybackThreads.add(id, thread);
1898 // notify client processes of the new output creation
Eric Laurent73e26b62015-04-27 16:55:58 -07001899 thread->ioConfigChanged(AUDIO_OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001900 return id;
1901}
1902
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001903status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001904{
Glenn Kastend96c5722012-04-25 13:44:49 -07001905 return closeOutput_nonvirtual(output);
1906}
1907
1908status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1909{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001910 // keep strong reference on the playback thread so that
1911 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001912 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001913 {
1914 Mutex::Autolock _l(mLock);
1915 thread = checkPlaybackThread_l(output);
1916 if (thread == NULL) {
1917 return BAD_VALUE;
1918 }
1919
Steve Block3856b092011-10-20 11:56:00 +01001920 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001921
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001922 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001923 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurentf6870ae2015-05-08 10:50:03 -07001924 if (mPlaybackThreads.valueAt(i)->isDuplicating()) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001925 DuplicatingThread *dupThread =
1926 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001927 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001928 }
1929 }
1930 }
1931
1932
1933 mPlaybackThreads.removeItem(output);
1934 // save all effects to the default thread
1935 if (mPlaybackThreads.size()) {
1936 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1937 if (dstThread != NULL) {
1938 // audioflinger lock is held here so the acquisition order of thread locks does not
1939 // matter
1940 Mutex::Autolock _dl(dstThread->mLock);
1941 Mutex::Autolock _sl(thread->mLock);
1942 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1943 for (size_t i = 0; i < effectChains.size(); i ++) {
1944 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001945 }
1946 }
1947 }
Eric Laurent73e26b62015-04-27 16:55:58 -07001948 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
1949 ioDesc->mIoHandle = output;
1950 ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001951 }
1952 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001953 // The thread entity (active unit of execution) is no longer running here,
1954 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001955
Eric Laurentf6870ae2015-05-08 10:50:03 -07001956 if (!thread->isDuplicating()) {
Eric Laurent83b88082014-06-20 18:31:16 -07001957 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001958 }
Eric Laurent83b88082014-06-20 18:31:16 -07001959
Mathias Agopian65ab4712010-07-14 17:59:35 -07001960 return NO_ERROR;
1961}
1962
Eric Laurent83b88082014-06-20 18:31:16 -07001963void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
1964{
1965 AudioStreamOut *out = thread->clearOutput();
1966 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1967 // from now on thread->mOutput is NULL
1968 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
1969 delete out;
1970}
1971
1972void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
1973{
1974 mPlaybackThreads.removeItem(thread->mId);
1975 thread->exit();
1976 closeOutputFinish(thread);
1977}
1978
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001979status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001980{
1981 Mutex::Autolock _l(mLock);
1982 PlaybackThread *thread = checkPlaybackThread_l(output);
1983
1984 if (thread == NULL) {
1985 return BAD_VALUE;
1986 }
1987
Steve Block3856b092011-10-20 11:56:00 +01001988 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001989 thread->suspend();
1990
1991 return NO_ERROR;
1992}
1993
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001994status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001995{
1996 Mutex::Autolock _l(mLock);
1997 PlaybackThread *thread = checkPlaybackThread_l(output);
1998
1999 if (thread == NULL) {
2000 return BAD_VALUE;
2001 }
2002
Steve Block3856b092011-10-20 11:56:00 +01002003 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002004
2005 thread->restore();
2006
2007 return NO_ERROR;
2008}
2009
Eric Laurentcf2c0212014-07-25 16:20:43 -07002010status_t AudioFlinger::openInput(audio_module_handle_t module,
2011 audio_io_handle_t *input,
2012 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002013 audio_devices_t *devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002014 const String8& address,
2015 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07002016 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002017{
Eric Laurent83b88082014-06-20 18:31:16 -07002018 Mutex::Autolock _l(mLock);
2019
Glenn Kastene7d66712015-03-05 13:46:52 -08002020 if (*devices == AUDIO_DEVICE_NONE) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002021 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07002022 }
2023
Glenn Kastene7d66712015-03-05 13:46:52 -08002024 sp<RecordThread> thread = openInput_l(module, input, config, *devices, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07002025
2026 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07002027 // notify client processes of the new input creation
Eric Laurent73e26b62015-04-27 16:55:58 -07002028 thread->ioConfigChanged(AUDIO_INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07002029 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002030 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07002031 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07002032}
Dima Zavin799a70e2011-04-18 16:57:27 -07002033
Eric Laurent83b88082014-06-20 18:31:16 -07002034sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002035 audio_io_handle_t *input,
2036 audio_config_t *config,
Glenn Kastene7d66712015-03-05 13:46:52 -08002037 audio_devices_t devices,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002038 const String8& address,
2039 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07002040 audio_input_flags_t flags)
2041{
Glenn Kastene7d66712015-03-05 13:46:52 -08002042 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002043 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07002044 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07002045 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07002046 }
Dima Zavin799a70e2011-04-18 16:57:27 -07002047
Eric Laurentcf2c0212014-07-25 16:20:43 -07002048 if (*input == AUDIO_IO_HANDLE_NONE) {
2049 *input = nextUniqueId();
2050 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002051
Eric Laurentcf2c0212014-07-25 16:20:43 -07002052 audio_config_t halconfig = *config;
2053 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07002054 audio_stream_in_t *inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002055 status_t status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002056 &inStream, flags, address.string(), source);
2057 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002058 ", Format %#x, Channels %x, flags %#x, status %d addr %s",
Dima Zavin799a70e2011-04-18 16:57:27 -07002059 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002060 halconfig.sample_rate,
2061 halconfig.format,
2062 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07002063 flags,
Jean-Michel Trivifd4c1482014-08-06 16:02:28 -07002064 status, address.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002065
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002066 // If the input could not be opened with the requested parameters and we can handle the
Andy Hung6770c6f2015-04-07 13:43:36 -07002067 // conversion internally, try to open again with the proposed parameters.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002068 if (status == BAD_VALUE &&
Andy Hung6770c6f2015-04-07 13:43:36 -07002069 audio_is_linear_pcm(config->format) &&
2070 audio_is_linear_pcm(halconfig.format) &&
2071 (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
Eric Laurentcf2c0212014-07-25 16:20:43 -07002072 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
2073 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07002074 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07002075 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002076 inStream = NULL;
Glenn Kastene7d66712015-03-05 13:46:52 -08002077 status = inHwHal->open_input_stream(inHwHal, *input, devices, &halconfig,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002078 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07002079 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07002080 }
2081
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07002082 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002083
Glenn Kasten46909e72013-02-26 09:20:22 -08002084#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07002085 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
2086 // or (re-)create if current Pipe is idle and does not match the new format
2087 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07002088 enum {
2089 TEE_SINK_NO, // don't copy input
2090 TEE_SINK_NEW, // copy input using a new pipe
2091 TEE_SINK_OLD, // copy input using an existing pipe
2092 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07002093 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
2094 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002095 if (!mTeeSinkInputEnabled) {
2096 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08002097 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002098 kind = TEE_SINK_NO;
2099 } else if (mRecordTeeSink == 0) {
2100 kind = TEE_SINK_NEW;
2101 } else if (mRecordTeeSink->getStrongCount() != 1) {
2102 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08002103 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07002104 kind = TEE_SINK_OLD;
2105 } else {
2106 kind = TEE_SINK_NEW;
2107 }
2108 switch (kind) {
2109 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002110 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07002111 size_t numCounterOffers = 0;
2112 const NBAIO_Format offers[1] = {format};
2113 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
2114 ALOG_ASSERT(index == 0);
2115 PipeReader *pipeReader = new PipeReader(*pipe);
2116 numCounterOffers = 0;
2117 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
2118 ALOG_ASSERT(index == 0);
2119 mRecordTeeSink = pipe;
2120 mRecordTeeSource = pipeReader;
2121 teeSink = pipe;
2122 }
2123 break;
2124 case TEE_SINK_OLD:
2125 teeSink = mRecordTeeSink;
2126 break;
2127 case TEE_SINK_NO:
2128 default:
2129 break;
2130 }
Glenn Kasten46909e72013-02-26 09:20:22 -08002131#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08002132
Eric Laurentcf2c0212014-07-25 16:20:43 -07002133 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002134
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002135 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07002136 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002137 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07002138 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07002139 inputStream,
2140 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08002141 primaryOutputDevice_l(),
Eric Laurent72e3f392015-05-20 14:43:50 -07002142 devices,
2143 mSystemReady
Glenn Kasten46909e72013-02-26 09:20:22 -08002144#ifdef TEE_SINK
2145 , teeSink
2146#endif
2147 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07002148 mRecordThreads.add(*input, thread);
2149 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002150 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002151 }
2152
Eric Laurentcf2c0212014-07-25 16:20:43 -07002153 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002154 return 0;
2155}
2156
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002157status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002158{
Glenn Kastend96c5722012-04-25 13:44:49 -07002159 return closeInput_nonvirtual(input);
2160}
2161
2162status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2163{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002164 // keep strong reference on the record thread so that
2165 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002166 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002167 {
2168 Mutex::Autolock _l(mLock);
2169 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002170 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002171 return BAD_VALUE;
2172 }
2173
Steve Block3856b092011-10-20 11:56:00 +01002174 ALOGV("closeInput() %d", input);
Eric Laurent1b928682014-10-02 19:41:47 -07002175
2176 // If we still have effect chains, it means that a client still holds a handle
2177 // on at least one effect. We must either move the chain to an existing thread with the
2178 // same session ID or put it aside in case a new record thread is opened for a
2179 // new capture on the same session
2180 sp<EffectChain> chain;
Eric Laurentaaa44472014-09-12 17:41:50 -07002181 {
Eric Laurentaaa44472014-09-12 17:41:50 -07002182 Mutex::Autolock _sl(thread->mLock);
2183 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
Eric Laurent1b928682014-10-02 19:41:47 -07002184 // Note: maximum one chain per record thread
2185 if (effectChains.size() != 0) {
2186 chain = effectChains[0];
2187 }
2188 }
2189 if (chain != 0) {
2190 // first check if a record thread is already opened with a client on the same session.
2191 // This should only happen in case of overlap between one thread tear down and the
2192 // creation of its replacement
2193 size_t i;
2194 for (i = 0; i < mRecordThreads.size(); i++) {
2195 sp<RecordThread> t = mRecordThreads.valueAt(i);
2196 if (t == thread) {
2197 continue;
2198 }
2199 if (t->hasAudioSession(chain->sessionId()) != 0) {
2200 Mutex::Autolock _l(t->mLock);
2201 ALOGV("closeInput() found thread %d for effect session %d",
2202 t->id(), chain->sessionId());
2203 t->addEffectChain_l(chain);
2204 break;
2205 }
2206 }
2207 // put the chain aside if we could not find a record thread with the same session id.
2208 if (i == mRecordThreads.size()) {
2209 putOrphanEffectChain_l(chain);
Eric Laurentaaa44472014-09-12 17:41:50 -07002210 }
2211 }
Eric Laurent73e26b62015-04-27 16:55:58 -07002212 const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
2213 ioDesc->mIoHandle = input;
2214 ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002215 mRecordThreads.removeItem(input);
2216 }
Eric Laurent83b88082014-06-20 18:31:16 -07002217 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2218 // we have a different lock for notification client
2219 closeInputFinish(thread);
2220 return NO_ERROR;
2221}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002222
Eric Laurent83b88082014-06-20 18:31:16 -07002223void AudioFlinger::closeInputFinish(sp<RecordThread> thread)
2224{
2225 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002226 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002227 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002228 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002229 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002230 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002231}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002232
Eric Laurent83b88082014-06-20 18:31:16 -07002233void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
2234{
2235 mRecordThreads.removeItem(thread->mId);
2236 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002237}
2238
Glenn Kastend2304db2014-02-03 07:40:31 -08002239status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002240{
2241 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002242 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002243
2244 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2245 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002246 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002247 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002248
2249 return NO_ERROR;
2250}
2251
2252
Eric Laurentde3f8392014-07-27 18:38:22 -07002253audio_unique_id_t AudioFlinger::newAudioUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002254{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002255 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002256}
2257
Marco Nelissend457c972014-02-11 08:47:07 -08002258void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002259{
2260 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002261 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002262 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2263 if (pid != -1 && (caller == getpid_cached)) {
2264 caller = pid;
2265 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002266
Eric Laurent021cf962014-05-13 10:18:14 -07002267 {
2268 Mutex::Autolock _cl(mClientLock);
2269 // Ignore requests received from processes not known as notification client. The request
2270 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2271 // called from a different pid leaving a stale session reference. Also we don't know how
2272 // to clear this reference if the client process dies.
2273 if (mNotificationClients.indexOfKey(caller) < 0) {
2274 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2275 return;
2276 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002277 }
2278
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002279 size_t num = mAudioSessionRefs.size();
2280 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002281 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002282 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2283 ref->mCnt++;
2284 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002285 return;
2286 }
2287 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002288 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2289 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002290}
2291
Marco Nelissend457c972014-02-11 08:47:07 -08002292void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002293{
2294 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002295 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002296 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2297 if (pid != -1 && (caller == getpid_cached)) {
2298 caller = pid;
2299 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002300 size_t num = mAudioSessionRefs.size();
2301 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002302 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002303 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2304 ref->mCnt--;
2305 ALOGV(" decremented refcount to %d", ref->mCnt);
2306 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002307 mAudioSessionRefs.removeAt(i);
2308 delete ref;
2309 purgeStaleEffects_l();
2310 }
2311 return;
2312 }
2313 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002314 // If the caller is mediaserver it is likely that the session being released was acquired
2315 // on behalf of a process not in notification clients and we ignore the warning.
2316 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002317}
2318
2319void AudioFlinger::purgeStaleEffects_l() {
2320
Steve Block3856b092011-10-20 11:56:00 +01002321 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002322
2323 Vector< sp<EffectChain> > chains;
2324
2325 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2326 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2327 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2328 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002329 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2330 chains.push(ec);
2331 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002332 }
2333 }
2334 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2335 sp<RecordThread> t = mRecordThreads.valueAt(i);
2336 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2337 sp<EffectChain> ec = t->mEffectChains[j];
2338 chains.push(ec);
2339 }
2340 }
2341
2342 for (size_t i = 0; i < chains.size(); i++) {
2343 sp<EffectChain> ec = chains[i];
2344 int sessionid = ec->sessionId();
2345 sp<ThreadBase> t = ec->mThread.promote();
2346 if (t == 0) {
2347 continue;
2348 }
2349 size_t numsessionrefs = mAudioSessionRefs.size();
2350 bool found = false;
2351 for (size_t k = 0; k < numsessionrefs; k++) {
2352 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002353 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002354 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002355 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002356 found = true;
2357 break;
2358 }
2359 }
2360 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002361 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002362 // remove all effects from the chain
2363 while (ec->mEffects.size()) {
2364 sp<EffectModule> effect = ec->mEffects[0];
2365 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002366 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002367 if (effect->purgeHandles()) {
2368 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002369 }
2370 AudioSystem::unregisterEffect(effect->id());
2371 }
2372 }
2373 }
2374 return;
2375}
2376
Mathias Agopian65ab4712010-07-14 17:59:35 -07002377// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002378AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002379{
Glenn Kastena1117922012-01-26 10:53:32 -08002380 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002381}
2382
2383// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002384AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002385{
2386 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002387 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002388}
2389
2390// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002391AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002392{
Glenn Kastena1117922012-01-26 10:53:32 -08002393 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002394}
2395
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002396uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002397{
Glenn Kastenbcefec32014-01-17 12:09:05 -08002398 return (uint32_t) android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002399}
2400
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002401AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002402{
2403 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2404 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentf6870ae2015-05-08 10:50:03 -07002405 if(thread->isDuplicating()) {
2406 continue;
2407 }
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002408 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002409 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002410 return thread;
2411 }
2412 }
2413 return NULL;
2414}
2415
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002416audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002417{
2418 PlaybackThread *thread = primaryPlaybackThread_l();
2419
2420 if (thread == NULL) {
2421 return 0;
2422 }
2423
Eric Laurentf1c04f92012-08-28 14:26:53 -07002424 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002425}
2426
Eric Laurenta011e352012-03-29 15:51:43 -07002427sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2428 int triggerSession,
2429 int listenerSession,
2430 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002431 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002432{
2433 Mutex::Autolock _l(mLock);
2434
2435 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2436 status_t playStatus = NAME_NOT_FOUND;
2437 status_t recStatus = NAME_NOT_FOUND;
2438 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2439 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2440 if (playStatus == NO_ERROR) {
2441 return event;
2442 }
2443 }
2444 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2445 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2446 if (recStatus == NO_ERROR) {
2447 return event;
2448 }
2449 }
2450 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2451 mPendingSyncEvents.add(event);
2452 } else {
2453 ALOGV("createSyncEvent() invalid event %d", event->type());
2454 event.clear();
2455 }
2456 return event;
2457}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002458
Mathias Agopian65ab4712010-07-14 17:59:35 -07002459// ----------------------------------------------------------------------------
2460// Effect management
2461// ----------------------------------------------------------------------------
2462
2463
Glenn Kastenf587ba52012-01-26 16:25:10 -08002464status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002465{
2466 Mutex::Autolock _l(mLock);
2467 return EffectQueryNumberEffects(numEffects);
2468}
2469
Glenn Kastenf587ba52012-01-26 16:25:10 -08002470status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002471{
2472 Mutex::Autolock _l(mLock);
2473 return EffectQueryEffect(index, descriptor);
2474}
2475
Glenn Kasten5e92a782012-01-30 07:40:52 -08002476status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002477 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002478{
2479 Mutex::Autolock _l(mLock);
2480 return EffectGetDescriptor(pUuid, descriptor);
2481}
2482
2483
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002484sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002485 effect_descriptor_t *pDesc,
2486 const sp<IEffectClient>& effectClient,
2487 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002488 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002489 int sessionId,
Svet Ganovbe71aa22015-04-28 12:06:02 -07002490 const String16& opPackageName,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002491 status_t *status,
2492 int *id,
2493 int *enabled)
2494{
2495 status_t lStatus = NO_ERROR;
2496 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002497 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002498
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002499 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002500 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002501 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002502
2503 if (pDesc == NULL) {
2504 lStatus = BAD_VALUE;
2505 goto Exit;
2506 }
2507
Eric Laurent84e9a102010-09-23 16:10:16 -07002508 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002509 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002510 lStatus = PERMISSION_DENIED;
2511 goto Exit;
2512 }
2513
Dima Zavinfce7a472011-04-19 22:30:36 -07002514 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002515 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002516 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002517 lStatus = PERMISSION_DENIED;
2518 goto Exit;
2519 }
2520
Mathias Agopian65ab4712010-07-14 17:59:35 -07002521 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002522 if (!EffectIsNullUuid(&pDesc->uuid)) {
2523 // if uuid is specified, request effect descriptor
2524 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2525 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002526 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002527 goto Exit;
2528 }
2529 } else {
2530 // if uuid is not specified, look for an available implementation
2531 // of the required type in effect factory
2532 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002533 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002534 lStatus = BAD_VALUE;
2535 goto Exit;
2536 }
2537 uint32_t numEffects = 0;
2538 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002539 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002540 bool found = false;
2541
2542 lStatus = EffectQueryNumberEffects(&numEffects);
2543 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002544 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002545 goto Exit;
2546 }
2547 for (uint32_t i = 0; i < numEffects; i++) {
2548 lStatus = EffectQueryEffect(i, &desc);
2549 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002550 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002551 continue;
2552 }
2553 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2554 // If matching type found save effect descriptor. If the session is
2555 // 0 and the effect is not auxiliary, continue enumeration in case
2556 // an auxiliary version of this effect type is available
2557 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002558 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002559 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002560 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2561 break;
2562 }
2563 }
2564 }
2565 if (!found) {
2566 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002567 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002568 goto Exit;
2569 }
2570 // For same effect type, chose auxiliary version over insert version if
2571 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002572 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002573 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002574 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002575 }
2576 }
2577
2578 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002579 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002580 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2581 lStatus = INVALID_OPERATION;
2582 goto Exit;
2583 }
2584
Eric Laurent59255e42011-07-27 19:49:51 -07002585 // check recording permission for visualizer
2586 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
Svet Ganovbe71aa22015-04-28 12:06:02 -07002587 !recordingAllowed(opPackageName)) {
Eric Laurent59255e42011-07-27 19:49:51 -07002588 lStatus = PERMISSION_DENIED;
2589 goto Exit;
2590 }
2591
Mathias Agopian65ab4712010-07-14 17:59:35 -07002592 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002593 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002594 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002595 // if the output returned by getOutputForEffect() is removed before we lock the
2596 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2597 // and we will exit safely
2598 io = AudioSystem::getOutputForEffect(&desc);
2599 ALOGV("createEffect got output %d", io);
2600 }
2601
2602 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002603
2604 // If output is not specified try to find a matching audio session ID in one of the
2605 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002606 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2607 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002608 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002609 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002610 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2611 // output must be specified by AudioPolicyManager when using session
2612 // AUDIO_SESSION_OUTPUT_STAGE
2613 lStatus = BAD_VALUE;
2614 goto Exit;
2615 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002616 // look for the thread where the specified audio session is present
2617 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2618 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2619 io = mPlaybackThreads.keyAt(i);
2620 break;
2621 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002622 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002623 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002624 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2625 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2626 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002627 break;
2628 }
2629 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002630 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002631 // If no output thread contains the requested session ID, default to
2632 // first output. The effect chain will be moved to the correct output
2633 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002634 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002635 io = mPlaybackThreads.keyAt(0);
2636 }
Steve Block3856b092011-10-20 11:56:00 +01002637 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002638 }
2639 ThreadBase *thread = checkRecordThread_l(io);
2640 if (thread == NULL) {
2641 thread = checkPlaybackThread_l(io);
2642 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002643 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002644 lStatus = BAD_VALUE;
2645 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002646 }
Eric Laurentaaa44472014-09-12 17:41:50 -07002647 } else {
2648 // Check if one effect chain was awaiting for an effect to be created on this
2649 // session and used it instead of creating a new one.
2650 sp<EffectChain> chain = getOrphanEffectChain_l((audio_session_t)sessionId);
2651 if (chain != 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07002652 Mutex::Autolock _l(thread->mLock);
Eric Laurentaaa44472014-09-12 17:41:50 -07002653 thread->addEffectChain_l(chain);
2654 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002655 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002656
Eric Laurent021cf962014-05-13 10:18:14 -07002657 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002658
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002659 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002660 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2661 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002662 if (handle != 0 && id != NULL) {
2663 *id = handle->id();
2664 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002665 if (handle == 0) {
2666 // remove local strong reference to Client with mClientLock held
2667 Mutex::Autolock _cl(mClientLock);
2668 client.clear();
2669 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002670 }
2671
2672Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002673 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002674 return handle;
2675}
2676
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002677status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2678 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002679{
Steve Block3856b092011-10-20 11:56:00 +01002680 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002681 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002682 Mutex::Autolock _l(mLock);
2683 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002684 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002685 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002686 }
Eric Laurentde070132010-07-13 04:45:46 -07002687 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2688 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002689 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002690 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002691 }
Eric Laurentde070132010-07-13 04:45:46 -07002692 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2693 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002694 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002695 return BAD_VALUE;
2696 }
2697
2698 Mutex::Autolock _dl(dstThread->mLock);
2699 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002700 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002701}
2702
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002703// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002704status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002705 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002706 AudioFlinger::PlaybackThread *dstThread,
2707 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002708{
Steve Block3856b092011-10-20 11:56:00 +01002709 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002710 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002711
Eric Laurent59255e42011-07-27 19:49:51 -07002712 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002713 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002714 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002715 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002716 return INVALID_OPERATION;
2717 }
2718
Andy Hung9a592762014-07-21 21:56:01 -07002719 // Check whether the destination thread has a channel count of FCC_2, which is
2720 // currently required for (most) effects. Prevent moving the effect chain here rather
2721 // than disabling the addEffect_l() call in dstThread below.
Eric Laurentf6870ae2015-05-08 10:50:03 -07002722 if ((dstThread->type() == ThreadBase::MIXER || dstThread->isDuplicating()) &&
Eric Laurentb10352f2014-11-03 16:25:39 -08002723 dstThread->mChannelCount != FCC_2) {
Andy Hung9a592762014-07-21 21:56:01 -07002724 ALOGW("moveEffectChain_l() effect chain failed because"
2725 " destination thread %p channel count(%u) != %u",
2726 dstThread, dstThread->mChannelCount, FCC_2);
2727 return INVALID_OPERATION;
2728 }
2729
Eric Laurent39e94f82010-07-28 01:32:47 -07002730 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002731 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002732 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002733 // removed.
2734 srcThread->removeEffectChain_l(chain);
2735
2736 // transfer all effects one by one so that new effect chain is created on new thread with
2737 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002738 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002739 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002740 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002741 Vector< sp<EffectModule> > removed;
2742 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002743 while (effect != 0) {
2744 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002745 removed.add(effect);
2746 status = dstThread->addEffect_l(effect);
2747 if (status != NO_ERROR) {
2748 break;
2749 }
Eric Laurentec35a142011-10-05 17:42:25 -07002750 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2751 if (effect->state() == EffectModule::ACTIVE ||
2752 effect->state() == EffectModule::STOPPING) {
2753 effect->start();
2754 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002755 // if the move request is not received from audio policy manager, the effect must be
2756 // re-registered with the new strategy and output
2757 if (dstChain == 0) {
2758 dstChain = effect->chain().promote();
2759 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002760 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002761 status = NO_INIT;
2762 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002763 }
2764 strategy = dstChain->strategy();
2765 }
2766 if (reRegister) {
2767 AudioSystem::unregisterEffect(effect->id());
2768 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002769 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002770 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002771 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002772 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002773 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002774 }
Eric Laurentde070132010-07-13 04:45:46 -07002775 effect = chain->getEffectFromId_l(0);
2776 }
2777
Eric Laurent5baf2af2013-09-12 17:37:00 -07002778 if (status != NO_ERROR) {
2779 for (size_t i = 0; i < removed.size(); i++) {
2780 srcThread->addEffect_l(removed[i]);
2781 if (dstChain != 0 && reRegister) {
2782 AudioSystem::unregisterEffect(removed[i]->id());
2783 AudioSystem::registerEffect(&removed[i]->desc(),
2784 srcThread->id(),
2785 strategy,
2786 sessionId,
2787 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002788 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002789 }
2790 }
2791 }
2792
2793 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002794}
2795
Eric Laurent5baf2af2013-09-12 17:37:00 -07002796bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002797{
2798 if (mGlobalEffectEnableTime != 0 &&
2799 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2800 return true;
2801 }
2802
2803 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2804 sp<EffectChain> ec =
2805 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002806 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002807 return true;
2808 }
2809 }
2810 return false;
2811}
2812
Eric Laurent5baf2af2013-09-12 17:37:00 -07002813void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002814{
2815 Mutex::Autolock _l(mLock);
2816
2817 mGlobalEffectEnableTime = systemTime();
2818
2819 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2820 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2821 if (t->mType == ThreadBase::OFFLOAD) {
2822 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2823 }
2824 }
2825
2826}
2827
Eric Laurentaaa44472014-09-12 17:41:50 -07002828status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain)
2829{
2830 audio_session_t session = (audio_session_t)chain->sessionId();
2831 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2832 ALOGV("putOrphanEffectChain_l session %d index %d", session, index);
2833 if (index >= 0) {
2834 ALOGW("putOrphanEffectChain_l chain for session %d already present", session);
2835 return ALREADY_EXISTS;
2836 }
2837 mOrphanEffectChains.add(session, chain);
2838 return NO_ERROR;
2839}
2840
2841sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session)
2842{
2843 sp<EffectChain> chain;
2844 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2845 ALOGV("getOrphanEffectChain_l session %d index %d", session, index);
2846 if (index >= 0) {
2847 chain = mOrphanEffectChains.valueAt(index);
2848 mOrphanEffectChains.removeItemsAt(index);
2849 }
2850 return chain;
2851}
2852
2853bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect)
2854{
2855 Mutex::Autolock _l(mLock);
2856 audio_session_t session = (audio_session_t)effect->sessionId();
2857 ssize_t index = mOrphanEffectChains.indexOfKey(session);
2858 ALOGV("updateOrphanEffectChains session %d index %d", session, index);
2859 if (index >= 0) {
2860 sp<EffectChain> chain = mOrphanEffectChains.valueAt(index);
2861 if (chain->removeEffect_l(effect) == 0) {
2862 ALOGV("updateOrphanEffectChains removing effect chain at index %d", index);
2863 mOrphanEffectChains.removeItemsAt(index);
2864 }
2865 return true;
2866 }
2867 return false;
2868}
2869
2870
Glenn Kastenda6ef132013-01-10 12:31:01 -08002871struct Entry {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002872#define TEE_MAX_FILENAME 32 // %Y%m%d%H%M%S_%d.wav = 4+2+2+2+2+2+1+1+4+1 = 21
2873 char mFileName[TEE_MAX_FILENAME];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002874};
2875
2876int comparEntry(const void *p1, const void *p2)
2877{
Glenn Kasten2f55e762015-03-05 16:05:08 -08002878 return strcmp(((const Entry *) p1)->mFileName, ((const Entry *) p2)->mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002879}
2880
Glenn Kasten46909e72013-02-26 09:20:22 -08002881#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002882void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002883{
Eric Laurent81784c32012-11-19 14:55:58 -08002884 NBAIO_Source *teeSource = source.get();
2885 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002886 // .wav rotation
2887 // There is a benign race condition if 2 threads call this simultaneously.
2888 // They would both traverse the directory, but the result would simply be
2889 // failures at unlink() which are ignored. It's also unlikely since
2890 // normally dumpsys is only done by bugreport or from the command line.
2891 char teePath[32+256];
2892 strcpy(teePath, "/data/misc/media");
2893 size_t teePathLen = strlen(teePath);
2894 DIR *dir = opendir(teePath);
2895 teePath[teePathLen++] = '/';
2896 if (dir != NULL) {
Glenn Kasten2f55e762015-03-05 16:05:08 -08002897#define TEE_MAX_SORT 20 // number of entries to sort
2898#define TEE_MAX_KEEP 10 // number of entries to keep
2899 struct Entry entries[TEE_MAX_SORT];
Glenn Kastenda6ef132013-01-10 12:31:01 -08002900 size_t entryCount = 0;
Glenn Kasten2f55e762015-03-05 16:05:08 -08002901 while (entryCount < TEE_MAX_SORT) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002902 struct dirent de;
2903 struct dirent *result = NULL;
2904 int rc = readdir_r(dir, &de, &result);
2905 if (rc != 0) {
2906 ALOGW("readdir_r failed %d", rc);
2907 break;
2908 }
2909 if (result == NULL) {
2910 break;
2911 }
2912 if (result != &de) {
2913 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2914 break;
2915 }
2916 // ignore non .wav file entries
2917 size_t nameLen = strlen(de.d_name);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002918 if (nameLen <= 4 || nameLen >= TEE_MAX_FILENAME ||
Glenn Kastenda6ef132013-01-10 12:31:01 -08002919 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2920 continue;
2921 }
Glenn Kasten2f55e762015-03-05 16:05:08 -08002922 strcpy(entries[entryCount++].mFileName, de.d_name);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002923 }
2924 (void) closedir(dir);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002925 if (entryCount > TEE_MAX_KEEP) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002926 qsort(entries, entryCount, sizeof(Entry), comparEntry);
Glenn Kasten2f55e762015-03-05 16:05:08 -08002927 for (size_t i = 0; i < entryCount - TEE_MAX_KEEP; ++i) {
2928 strcpy(&teePath[teePathLen], entries[i].mFileName);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002929 (void) unlink(teePath);
2930 }
2931 }
2932 } else {
2933 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002934 dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002935 }
2936 }
Eric Laurent81784c32012-11-19 14:55:58 -08002937 char teeTime[16];
2938 struct timeval tv;
2939 gettimeofday(&tv, NULL);
2940 struct tm tm;
2941 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002942 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2943 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2944 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2945 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002946 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07002947 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08002948 char wavHeader[44];
2949 memcpy(wavHeader,
2950 "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",
2951 sizeof(wavHeader));
2952 NBAIO_Format format = teeSource->format();
2953 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002954 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07002955 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002956 wavHeader[22] = channelCount; // number of channels
2957 wavHeader[24] = sampleRate; // sample rate
2958 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07002959 wavHeader[32] = frameSize; // block alignment
2960 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08002961 write(teeFd, wavHeader, sizeof(wavHeader));
2962 size_t total = 0;
2963 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07002964#define TEE_SINK_READ 1024 // frames per I/O operation
2965 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002966 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08002967 size_t count = TEE_SINK_READ;
2968 ssize_t actual = teeSource->read(buffer, count,
2969 AudioBufferProvider::kInvalidPTS);
2970 bool wasFirstRead = firstRead;
2971 firstRead = false;
2972 if (actual <= 0) {
2973 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2974 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002975 }
Eric Laurent81784c32012-11-19 14:55:58 -08002976 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002977 }
Eric Laurent81784c32012-11-19 14:55:58 -08002978 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07002979 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002980 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002981 }
Glenn Kasten329f6512014-08-28 16:23:16 -07002982 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08002983 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002984 uint32_t temp = 44 + total * frameSize - 8;
2985 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002986 write(teeFd, &temp, sizeof(temp));
2987 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002988 temp = total * frameSize;
2989 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002990 write(teeFd, &temp, sizeof(temp));
2991 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002992 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002993 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002994 }
Eric Laurent59255e42011-07-27 19:49:51 -07002995 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002996 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002997 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002998 }
Eric Laurent59255e42011-07-27 19:49:51 -07002999 }
3000 }
3001}
Glenn Kasten46909e72013-02-26 09:20:22 -08003002#endif
Eric Laurent59255e42011-07-27 19:49:51 -07003003
Mathias Agopian65ab4712010-07-14 17:59:35 -07003004// ----------------------------------------------------------------------------
3005
3006status_t AudioFlinger::onTransact(
3007 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3008{
3009 return BnAudioFlinger::onTransact(code, data, reply, flags);
3010}
3011
Glenn Kasten63238ef2015-03-02 15:50:29 -08003012} // namespace android