blob: 500e2b87892f63c7b8615c6016fb6d48757dd56e [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
Mathias Agopian65ab4712010-07-14 17:59:35 -07002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
Glenn Kasten153b9fe2013-07-15 11:23:36 -070022#include "Configuration.h"
Glenn Kastenda6ef132013-01-10 12:31:01 -080023#include <dirent.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070024#include <math.h>
25#include <signal.h>
26#include <sys/time.h>
27#include <sys/resource.h>
28
Gloria Wang9ee159b2011-02-24 14:51:45 -080029#include <binder/IPCThreadState.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070030#include <binder/IServiceManager.h>
31#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070032#include <utils/Trace.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070033#include <binder/Parcel.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/String16.h>
35#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070036#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070037
Dima Zavinfce7a472011-04-19 22:30:36 -070038#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070039#include <cutils/properties.h>
40
Dima Zavin64760242011-05-11 14:15:23 -070041#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070042#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070043
44#include "AudioMixer.h"
45#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080046#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070047
Mathias Agopian65ab4712010-07-14 17:59:35 -070048#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070049#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070050#include <audio_effects/effect_ns.h>
51#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Glenn Kasten3b21c502011-12-15 09:52:39 -080053#include <audio_utils/primitives.h>
54
Eric Laurentfeb0db62011-07-22 09:04:31 -070055#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080056
John Grossman4ff14ba2012-02-08 16:37:41 -080057#include <common_time/cc_helper.h>
Glenn Kasten58912562012-04-03 10:45:00 -070058
Glenn Kasten9e58b552013-01-18 15:09:48 -080059#include <media/IMediaLogService.h>
60
Glenn Kastenda6ef132013-01-10 12:31:01 -080061#include <media/nbaio/Pipe.h>
62#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070063#include <media/AudioParameter.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070064#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080065
Mathias Agopian65ab4712010-07-14 17:59:35 -070066// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070067
John Grossman1c345192012-03-27 14:00:17 -070068// Note: the following macro is used for extremely verbose logging message. In
69// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
70// 0; but one side effect of this is to turn all LOGV's as well. Some messages
71// are so verbose that we want to suppress them even when we have ALOG_ASSERT
72// turned on. Do not uncomment the #def below unless you really know what you
73// are doing and want to see all of the extremely verbose messages.
74//#define VERY_VERY_VERBOSE_LOGGING
75#ifdef VERY_VERY_VERBOSE_LOGGING
76#define ALOGVV ALOGV
77#else
78#define ALOGVV(a...) do { } while(0)
79#endif
Eric Laurentde070132010-07-13 04:45:46 -070080
Mathias Agopian65ab4712010-07-14 17:59:35 -070081namespace android {
82
Glenn Kastenec1d6b52011-12-12 09:04:45 -080083static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
84static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Eric Laurent021cf962014-05-13 10:18:14 -070085static const char kClientLockedString[] = "Client lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070086
Glenn Kasten58912562012-04-03 10:45:00 -070087
John Grossman4ff14ba2012-02-08 16:37:41 -080088nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080089
Eric Laurent81784c32012-11-19 14:55:58 -080090uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070091
Glenn Kasten46909e72013-02-26 09:20:22 -080092#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080093bool AudioFlinger::mTeeSinkInputEnabled = false;
94bool AudioFlinger::mTeeSinkOutputEnabled = false;
95bool AudioFlinger::mTeeSinkTrackEnabled = false;
96
97size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
98size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
99size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800100#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800101
Eric Laurent813e2a72013-08-31 12:59:48 -0700102// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off
103// we define a minimum time during which a global effect is considered enabled.
104static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200);
105
Mathias Agopian65ab4712010-07-14 17:59:35 -0700106// ----------------------------------------------------------------------------
107
Marco Nelissenb2208842014-02-07 14:00:50 -0800108const char *formatToString(audio_format_t format) {
109 switch(format) {
110 case AUDIO_FORMAT_PCM_SUB_8_BIT: return "pcm8";
111 case AUDIO_FORMAT_PCM_SUB_16_BIT: return "pcm16";
112 case AUDIO_FORMAT_PCM_SUB_32_BIT: return "pcm32";
113 case AUDIO_FORMAT_PCM_SUB_8_24_BIT: return "pcm8.24";
114 case AUDIO_FORMAT_PCM_SUB_24_BIT_PACKED: return "pcm24";
115 case AUDIO_FORMAT_PCM_SUB_FLOAT: return "pcmfloat";
116 case AUDIO_FORMAT_MP3: return "mp3";
117 case AUDIO_FORMAT_AMR_NB: return "amr-nb";
118 case AUDIO_FORMAT_AMR_WB: return "amr-wb";
119 case AUDIO_FORMAT_AAC: return "aac";
120 case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
121 case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
122 case AUDIO_FORMAT_VORBIS: return "vorbis";
123 default:
124 break;
125 }
126 return "unknown";
127}
128
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700129static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700130{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700131 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700132 int rc;
133
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700134 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
135 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
136 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
137 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700138 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700139 }
140 rc = audio_hw_device_open(mod, dev);
141 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
142 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
143 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700144 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700145 }
Eric Laurentd6461902014-05-22 12:23:26 -0700146 if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700147 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
148 rc = BAD_VALUE;
149 goto out;
150 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700151 return 0;
152
153out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700154 *dev = NULL;
155 return rc;
156}
157
Mathias Agopian65ab4712010-07-14 17:59:35 -0700158// ----------------------------------------------------------------------------
159
160AudioFlinger::AudioFlinger()
161 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800162 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800163 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700164 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800165 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800166 mMasterMute(false),
167 mNextUniqueId(1),
168 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700169 mBtNrecIsOff(false),
170 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700171 mIsDeviceTypeKnown(false),
Glenn Kastenc263ca02014-06-04 20:31:46 -0700172 mGlobalEffectEnableTime(0),
173 mPrimaryOutputSampleRate(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700174{
Glenn Kasten949a9262013-04-16 12:35:20 -0700175 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800176 char value[PROPERTY_VALUE_MAX];
177 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
178 if (doLog) {
Glenn Kastencf515ab2014-03-14 16:01:58 -0700179 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters", MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800180 }
Eric Laurent951f4552014-05-20 10:48:17 -0700181
Glenn Kasten46909e72013-02-26 09:20:22 -0800182#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800183 (void) property_get("ro.debuggable", value, "0");
184 int debuggable = atoi(value);
185 int teeEnabled = 0;
186 if (debuggable) {
187 (void) property_get("af.tee", value, "0");
188 teeEnabled = atoi(value);
189 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800190 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700191 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800192 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700193 }
194 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800195 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700196 }
197 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800198 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700199 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800200#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700201}
202
203void AudioFlinger::onFirstRef()
204{
Dima Zavin799a70e2011-04-18 16:57:27 -0700205 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700206
Eric Laurent93575202011-01-18 18:39:02 -0800207 Mutex::Autolock _l(mLock);
208
Dima Zavin799a70e2011-04-18 16:57:27 -0700209 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800210 char val_str[PROPERTY_VALUE_MAX] = { 0 };
211 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
212 uint32_t int_val;
213 if (1 == sscanf(val_str, "%u", &int_val)) {
214 mStandbyTimeInNsecs = milliseconds(int_val);
215 ALOGI("Using %u mSec as standby time.", int_val);
216 } else {
217 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
218 ALOGI("Using default %u mSec as standby time.",
219 (uint32_t)(mStandbyTimeInNsecs / 1000000));
220 }
221 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700222
Eric Laurent951f4552014-05-20 10:48:17 -0700223 mPatchPanel = new PatchPanel(this);
224
Eric Laurenta4c5a552012-03-29 10:12:40 -0700225 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700226}
227
228AudioFlinger::~AudioFlinger()
229{
230 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700231 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700232 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700233 }
234 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700235 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700236 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700237 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700238
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800239 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
240 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700241 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
242 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700243 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700244
245 // Tell media.log service about any old writers that still need to be unregistered
246 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
247 if (binder != 0) {
248 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
249 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
250 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
251 mUnregisteredWriters.pop();
252 mediaLogService->unregisterWriter(iMemory);
253 }
254 }
255
Mathias Agopian65ab4712010-07-14 17:59:35 -0700256}
257
Eric Laurenta4c5a552012-03-29 10:12:40 -0700258static const char * const audio_interfaces[] = {
259 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
260 AUDIO_HARDWARE_MODULE_ID_A2DP,
261 AUDIO_HARDWARE_MODULE_ID_USB,
262};
263#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
264
John Grossmanee578c02012-07-23 17:05:46 -0700265AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
266 audio_module_handle_t module,
267 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700268{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700269 // if module is 0, the request comes from an old policy manager and we should load
270 // well known modules
271 if (module == 0) {
272 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
273 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
274 loadHwModule_l(audio_interfaces[i]);
275 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700276 // then try to find a module supporting the requested device.
277 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
278 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
279 audio_hw_device_t *dev = audioHwDevice->hwDevice();
280 if ((dev->get_supported_devices != NULL) &&
281 (dev->get_supported_devices(dev) & devices) == devices)
282 return audioHwDevice;
283 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700284 } else {
285 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700286 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
287 if (audioHwDevice != NULL) {
288 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700289 }
290 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700291
Dima Zavin799a70e2011-04-18 16:57:27 -0700292 return NULL;
293}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700294
Glenn Kasten0f11b512014-01-31 16:18:54 -0800295void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700296{
297 const size_t SIZE = 256;
298 char buffer[SIZE];
299 String8 result;
300
301 result.append("Clients:\n");
302 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800303 sp<Client> client = mClients.valueAt(i).promote();
304 if (client != 0) {
305 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
306 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700307 }
308 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700309
Eric Laurentd1b28d42013-09-18 18:47:13 -0700310 result.append("Notification Clients:\n");
311 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
312 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
313 result.append(buffer);
314 }
315
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700316 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800317 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700318 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
319 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800320 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700321 result.append(buffer);
322 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324}
325
326
Glenn Kasten0f11b512014-01-31 16:18:54 -0800327void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700328{
329 const size_t SIZE = 256;
330 char buffer[SIZE];
331 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800332 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700333
John Grossman4ff14ba2012-02-08 16:37:41 -0800334 snprintf(buffer, SIZE, "Hardware status: %d\n"
335 "Standby Time mSec: %u\n",
336 hardwareStatus,
337 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338 result.append(buffer);
339 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700340}
341
Glenn Kasten0f11b512014-01-31 16:18:54 -0800342void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700343{
344 const size_t SIZE = 256;
345 char buffer[SIZE];
346 String8 result;
347 snprintf(buffer, SIZE, "Permission Denial: "
348 "can't dump AudioFlinger from pid=%d, uid=%d\n",
349 IPCThreadState::self()->getCallingPid(),
350 IPCThreadState::self()->getCallingUid());
351 result.append(buffer);
352 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700353}
354
Eric Laurent81784c32012-11-19 14:55:58 -0800355bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700356{
357 bool locked = false;
358 for (int i = 0; i < kDumpLockRetries; ++i) {
359 if (mutex.tryLock() == NO_ERROR) {
360 locked = true;
361 break;
362 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800363 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700364 }
365 return locked;
366}
367
368status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
369{
Glenn Kasten44deb052012-02-05 18:09:08 -0800370 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700371 dumpPermissionDenial(fd, args);
372 } else {
373 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800374 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700375 if (!hardwareLocked) {
376 String8 result(kHardwareLockedString);
377 write(fd, result.string(), result.size());
378 } else {
379 mHardwareLock.unlock();
380 }
381
Eric Laurent81784c32012-11-19 14:55:58 -0800382 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700383
384 // failed to lock - AudioFlinger is probably deadlocked
385 if (!locked) {
386 String8 result(kDeadlockedString);
387 write(fd, result.string(), result.size());
388 }
389
Eric Laurent021cf962014-05-13 10:18:14 -0700390 bool clientLocked = dumpTryLock(mClientLock);
391 if (!clientLocked) {
392 String8 result(kClientLockedString);
393 write(fd, result.string(), result.size());
394 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700395 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700396 if (clientLocked) {
397 mClientLock.unlock();
398 }
399
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400 dumpInternals(fd, args);
401
402 // dump playback threads
403 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
404 mPlaybackThreads.valueAt(i)->dump(fd, args);
405 }
406
407 // dump record threads
408 for (size_t i = 0; i < mRecordThreads.size(); i++) {
409 mRecordThreads.valueAt(i)->dump(fd, args);
410 }
411
Dima Zavin799a70e2011-04-18 16:57:27 -0700412 // dump all hardware devs
413 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700414 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700415 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700416 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700417
Glenn Kasten46909e72013-02-26 09:20:22 -0800418#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700419 // dump the serially shared record tee sink
420 if (mRecordTeeSource != 0) {
421 dumpTee(fd, mRecordTeeSource);
422 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800423#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700424
Glenn Kastend65d73c2012-06-22 17:21:07 -0700425 if (locked) {
426 mLock.unlock();
427 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800428
429 // append a copy of media.log here by forwarding fd to it, but don't attempt
430 // to lookup the service if it's not running, as it will block for a second
431 if (mLogMemoryDealer != 0) {
432 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
433 if (binder != 0) {
434 fdprintf(fd, "\nmedia.log:\n");
435 Vector<String16> args;
436 binder->dump(fd, args);
437 }
438 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700439 }
440 return NO_ERROR;
441}
442
Eric Laurent021cf962014-05-13 10:18:14 -0700443sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800444{
Eric Laurent021cf962014-05-13 10:18:14 -0700445 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800446 // If pid is already in the mClients wp<> map, then use that entry
447 // (for which promote() is always != 0), otherwise create a new entry and Client.
448 sp<Client> client = mClients.valueFor(pid).promote();
449 if (client == 0) {
450 client = new Client(this, pid);
451 mClients.add(pid, client);
452 }
453
454 return client;
455}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700456
Glenn Kasten9e58b552013-01-18 15:09:48 -0800457sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
458{
Glenn Kasten481fb672013-09-30 14:39:28 -0700459 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800460 if (mLogMemoryDealer == 0) {
461 return new NBLog::Writer();
462 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800463 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700464 // Similarly if we can't contact the media.log service, also return a dummy writer
465 if (binder == 0) {
466 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800467 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700468 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
469 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
470 // If allocation fails, consult the vector of previously unregistered writers
471 // and garbage-collect one or more them until an allocation succeeds
472 if (shared == 0) {
473 Mutex::Autolock _l(mUnregisteredWritersLock);
474 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
475 {
476 // Pick the oldest stale writer to garbage-collect
477 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
478 mUnregisteredWriters.removeAt(0);
479 mediaLogService->unregisterWriter(iMemory);
480 // Now the media.log remote reference to IMemory is gone. When our last local
481 // reference to IMemory also drops to zero at end of this block,
482 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
483 }
484 // Re-attempt the allocation
485 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
486 if (shared != 0) {
487 goto success;
488 }
489 }
490 // Even after garbage-collecting all old writers, there is still not enough memory,
491 // so return a dummy writer
492 return new NBLog::Writer();
493 }
494success:
495 mediaLogService->registerWriter(shared, size, name);
496 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800497}
498
499void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
500{
Glenn Kasten685ef092013-02-04 08:15:34 -0800501 if (writer == 0) {
502 return;
503 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800504 sp<IMemory> iMemory(writer->getIMemory());
505 if (iMemory == 0) {
506 return;
507 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700508 // Rather than removing the writer immediately, append it to a queue of old writers to
509 // be garbage-collected later. This allows us to continue to view old logs for a while.
510 Mutex::Autolock _l(mUnregisteredWritersLock);
511 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800512}
513
Mathias Agopian65ab4712010-07-14 17:59:35 -0700514// IAudioFlinger interface
515
516
517sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800518 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700519 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800520 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700521 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800522 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800523 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700524 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800525 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800526 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700527 int *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800528 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700529 status_t *status)
530{
531 sp<PlaybackThread::Track> track;
532 sp<TrackHandle> trackHandle;
533 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700534 status_t lStatus;
535 int lSessionId;
536
Glenn Kasten263709e2012-01-06 08:40:01 -0800537 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
538 // but if someone uses binder directly they could bypass that and cause us to crash
539 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000540 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700541 lStatus = BAD_VALUE;
542 goto Exit;
543 }
544
Glenn Kasten53b5d092014-02-05 10:00:23 -0800545 // further sample rate checks are performed by createTrack_l() depending on the thread type
546 if (sampleRate == 0) {
547 ALOGE("createTrack() invalid sample rate %u", sampleRate);
548 lStatus = BAD_VALUE;
549 goto Exit;
550 }
551
552 // further channel mask checks are performed by createTrack_l() depending on the thread type
553 if (!audio_is_output_channel(channelMask)) {
554 ALOGE("createTrack() invalid channel mask %#x", channelMask);
555 lStatus = BAD_VALUE;
556 goto Exit;
557 }
558
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700559 // further format checks are performed by createTrack_l() depending on the thread type
560 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800561 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700562 lStatus = BAD_VALUE;
563 goto Exit;
564 }
565
Glenn Kasten663c2242013-09-24 11:52:37 -0700566 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
567 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
568 lStatus = BAD_VALUE;
569 goto Exit;
570 }
571
Mathias Agopian65ab4712010-07-14 17:59:35 -0700572 {
573 Mutex::Autolock _l(mLock);
574 PlaybackThread *thread = checkPlaybackThread_l(output);
575 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800576 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700577 lStatus = BAD_VALUE;
578 goto Exit;
579 }
580
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800581 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -0700582 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700583
Glenn Kastene848bd92014-03-13 15:00:32 -0700584 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700585 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700586 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700587 // check if an effect chain with the same session ID is present on another
588 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700589 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700590 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
591 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700592 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700593 if (sessions & PlaybackThread::EFFECT_SESSION) {
594 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700595 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700596 }
Eric Laurentde070132010-07-13 04:45:46 -0700597 }
598 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700599 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700600 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700601 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700602 if (sessionId != NULL) {
603 *sessionId = lSessionId;
604 }
605 }
Steve Block3856b092011-10-20 11:56:00 +0100606 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700607
608 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800609 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800610 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700611 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700612
613 // move effect chain to this output thread if an effect on same session was waiting
614 // for a track to be created
615 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700616 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700617 Mutex::Autolock _dl(thread->mLock);
618 Mutex::Autolock _sl(effectThread->mLock);
619 moveEffectChain_l(lSessionId, effectThread, thread, true);
620 }
Eric Laurenta011e352012-03-29 15:51:43 -0700621
622 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700623 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700624 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
625 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700626 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700627 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700628 } else {
629 mPendingSyncEvents[i]->cancel();
630 }
Eric Laurenta011e352012-03-29 15:51:43 -0700631 mPendingSyncEvents.removeAt(i);
632 i--;
633 }
634 }
635 }
Glenn Kastene198c362013-08-13 09:13:36 -0700636
Mathias Agopian65ab4712010-07-14 17:59:35 -0700637 }
Glenn Kastene198c362013-08-13 09:13:36 -0700638
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700639 if (lStatus != NO_ERROR) {
640 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700641 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe9adc42014-05-26 16:03:08 -0700642 // Don't hold mClientLock when releasing the reference on the track as the
643 // destructor will acquire it.
644 {
645 Mutex::Autolock _cl(mClientLock);
646 client.clear();
647 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700648 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700649 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650 }
651
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700652 // return handle to client
653 trackHandle = new TrackHandle(track);
654
Mathias Agopian65ab4712010-07-14 17:59:35 -0700655Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700656 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657 return trackHandle;
658}
659
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800660uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700661{
662 Mutex::Autolock _l(mLock);
663 PlaybackThread *thread = checkPlaybackThread_l(output);
664 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000665 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666 return 0;
667 }
668 return thread->sampleRate();
669}
670
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800671int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672{
673 Mutex::Autolock _l(mLock);
674 PlaybackThread *thread = checkPlaybackThread_l(output);
675 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000676 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700677 return 0;
678 }
679 return thread->channelCount();
680}
681
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800682audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700683{
684 Mutex::Autolock _l(mLock);
685 PlaybackThread *thread = checkPlaybackThread_l(output);
686 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000687 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800688 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700689 }
690 return thread->format();
691}
692
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800693size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694{
695 Mutex::Autolock _l(mLock);
696 PlaybackThread *thread = checkPlaybackThread_l(output);
697 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000698 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699 return 0;
700 }
Glenn Kasten58912562012-04-03 10:45:00 -0700701 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
702 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700703 return thread->frameCount();
704}
705
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800706uint32_t AudioFlinger::latency(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) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800711 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712 return 0;
713 }
714 return thread->latency();
715}
716
717status_t AudioFlinger::setMasterVolume(float value)
718{
Eric Laurenta1884f92011-08-23 08:25:03 -0700719 status_t ret = initCheck();
720 if (ret != NO_ERROR) {
721 return ret;
722 }
723
Mathias Agopian65ab4712010-07-14 17:59:35 -0700724 // check calling permissions
725 if (!settingsAllowed()) {
726 return PERMISSION_DENIED;
727 }
728
Eric Laurenta4c5a552012-03-29 10:12:40 -0700729 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700730 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700731
John Grossmanee578c02012-07-23 17:05:46 -0700732 // Set master volume in the HALs which support it.
733 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
734 AutoMutex lock(mHardwareLock);
735 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800736
John Grossmanee578c02012-07-23 17:05:46 -0700737 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
738 if (dev->canSetMasterVolume()) {
739 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800740 }
John Grossmanee578c02012-07-23 17:05:46 -0700741 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700742 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700743
John Grossmanee578c02012-07-23 17:05:46 -0700744 // Now set the master volume in each playback thread. Playback threads
745 // assigned to HALs which do not have master volume support will apply
746 // master volume during the mix operation. Threads with HALs which do
747 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800748 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700749 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700750
751 return NO_ERROR;
752}
753
Glenn Kastenf78aee72012-01-04 11:00:47 -0800754status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700755{
Eric Laurenta1884f92011-08-23 08:25:03 -0700756 status_t ret = initCheck();
757 if (ret != NO_ERROR) {
758 return ret;
759 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700760
761 // check calling permissions
762 if (!settingsAllowed()) {
763 return PERMISSION_DENIED;
764 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800765 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000766 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700767 return BAD_VALUE;
768 }
769
770 { // scope for the lock
771 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700772 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700773 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700774 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700775 mHardwareStatus = AUDIO_HW_IDLE;
776 }
777
778 if (NO_ERROR == ret) {
779 Mutex::Autolock _l(mLock);
780 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800781 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700782 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700783 }
784
785 return ret;
786}
787
788status_t AudioFlinger::setMicMute(bool state)
789{
Eric Laurenta1884f92011-08-23 08:25:03 -0700790 status_t ret = initCheck();
791 if (ret != NO_ERROR) {
792 return ret;
793 }
794
Mathias Agopian65ab4712010-07-14 17:59:35 -0700795 // check calling permissions
796 if (!settingsAllowed()) {
797 return PERMISSION_DENIED;
798 }
799
800 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700801 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700802 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700803 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700804 mHardwareStatus = AUDIO_HW_IDLE;
805 return ret;
806}
807
808bool AudioFlinger::getMicMute() const
809{
Eric Laurenta1884f92011-08-23 08:25:03 -0700810 status_t ret = initCheck();
811 if (ret != NO_ERROR) {
812 return false;
813 }
814
Dima Zavinfce7a472011-04-19 22:30:36 -0700815 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800816 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700817 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700819 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820 mHardwareStatus = AUDIO_HW_IDLE;
821 return state;
822}
823
824status_t AudioFlinger::setMasterMute(bool muted)
825{
John Grossmand8f178d2012-07-20 14:51:35 -0700826 status_t ret = initCheck();
827 if (ret != NO_ERROR) {
828 return ret;
829 }
830
Mathias Agopian65ab4712010-07-14 17:59:35 -0700831 // check calling permissions
832 if (!settingsAllowed()) {
833 return PERMISSION_DENIED;
834 }
835
John Grossmanee578c02012-07-23 17:05:46 -0700836 Mutex::Autolock _l(mLock);
837 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700838
John Grossmanee578c02012-07-23 17:05:46 -0700839 // Set master mute in the HALs which support it.
840 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
841 AutoMutex lock(mHardwareLock);
842 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700843
John Grossmanee578c02012-07-23 17:05:46 -0700844 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
845 if (dev->canSetMasterMute()) {
846 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700847 }
John Grossmanee578c02012-07-23 17:05:46 -0700848 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700849 }
850
John Grossmanee578c02012-07-23 17:05:46 -0700851 // Now set the master mute in each playback thread. Playback threads
852 // assigned to HALs which do not have master mute support will apply master
853 // mute during the mix operation. Threads with HALs which do support master
854 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800855 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700856 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700857
858 return NO_ERROR;
859}
860
861float AudioFlinger::masterVolume() const
862{
Glenn Kasten98067102011-12-13 11:47:54 -0800863 Mutex::Autolock _l(mLock);
864 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700865}
866
867bool AudioFlinger::masterMute() const
868{
Glenn Kasten98067102011-12-13 11:47:54 -0800869 Mutex::Autolock _l(mLock);
870 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700871}
872
John Grossman4ff14ba2012-02-08 16:37:41 -0800873float AudioFlinger::masterVolume_l() const
874{
John Grossman4ff14ba2012-02-08 16:37:41 -0800875 return mMasterVolume;
876}
877
John Grossmand8f178d2012-07-20 14:51:35 -0700878bool AudioFlinger::masterMute_l() const
879{
John Grossmanee578c02012-07-23 17:05:46 -0700880 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700881}
882
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800883status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
884 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700885{
886 // check calling permissions
887 if (!settingsAllowed()) {
888 return PERMISSION_DENIED;
889 }
890
Glenn Kasten263709e2012-01-06 08:40:01 -0800891 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000892 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700893 return BAD_VALUE;
894 }
895
896 AutoMutex lock(mLock);
897 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700898 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700899 thread = checkPlaybackThread_l(output);
900 if (thread == NULL) {
901 return BAD_VALUE;
902 }
903 }
904
905 mStreamTypes[stream].volume = value;
906
907 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800908 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700909 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700910 }
911 } else {
912 thread->setStreamVolume(stream, value);
913 }
914
915 return NO_ERROR;
916}
917
Glenn Kastenfff6d712012-01-12 16:38:12 -0800918status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700919{
920 // check calling permissions
921 if (!settingsAllowed()) {
922 return PERMISSION_DENIED;
923 }
924
Glenn Kasten263709e2012-01-06 08:40:01 -0800925 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700926 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000927 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928 return BAD_VALUE;
929 }
930
Eric Laurent93575202011-01-18 18:39:02 -0800931 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700932 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700933 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700934 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700935
936 return NO_ERROR;
937}
938
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800939float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940{
Glenn Kasten263709e2012-01-06 08:40:01 -0800941 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700942 return 0.0f;
943 }
944
945 AutoMutex lock(mLock);
946 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -0700947 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700948 PlaybackThread *thread = checkPlaybackThread_l(output);
949 if (thread == NULL) {
950 return 0.0f;
951 }
952 volume = thread->streamVolume(stream);
953 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800954 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700955 }
956
957 return volume;
958}
959
Glenn Kastenfff6d712012-01-12 16:38:12 -0800960bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961{
Glenn Kasten263709e2012-01-06 08:40:01 -0800962 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700963 return true;
964 }
965
Glenn Kasten6637baa2012-01-09 09:40:36 -0800966 AutoMutex lock(mLock);
967 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700968}
969
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800970status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700972 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
973 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800974
Mathias Agopian65ab4712010-07-14 17:59:35 -0700975 // check calling permissions
976 if (!settingsAllowed()) {
977 return PERMISSION_DENIED;
978 }
979
Glenn Kasten142f5192014-03-25 17:44:59 -0700980 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
981 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700982 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700983 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800984 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700985 AutoMutex lock(mHardwareLock);
986 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
987 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
988 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
989 status_t result = dev->set_parameters(dev, keyValuePairs.string());
990 final_result = result ?: final_result;
991 }
992 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800993 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700994 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
995 AudioParameter param = AudioParameter(keyValuePairs);
996 String8 value;
997 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700998 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
999 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -07001000 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1001 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001002 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001003 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1004 // collect all of the thread's session IDs
1005 KeyedVector<int, bool> ids = thread->sessionIds();
1006 // suspend effects associated with those session IDs
1007 for (size_t j = 0; j < ids.size(); ++j) {
1008 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001009 thread->setEffectSuspended(FX_IID_AEC,
1010 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001011 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001012 thread->setEffectSuspended(FX_IID_NS,
1013 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001014 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001015 }
1016 }
Eric Laurentbee53372011-08-29 12:42:48 -07001017 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001018 }
1019 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001020 String8 screenState;
1021 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1022 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001023 if (isOff != (AudioFlinger::mScreenState & 1)) {
1024 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001025 }
1026 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001027 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001028 }
1029
1030 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1031 // and the thread is exited once the lock is released
1032 sp<ThreadBase> thread;
1033 {
1034 Mutex::Autolock _l(mLock);
1035 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001036 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001037 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001038 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001039 // indicate output device change to all input threads for pre processing
1040 AudioParameter param = AudioParameter(keyValuePairs);
1041 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001042 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1043 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001044 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1045 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1046 }
1047 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001048 }
1049 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001050 if (thread != 0) {
1051 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001052 }
1053 return BAD_VALUE;
1054}
1055
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001056String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001057{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001058 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1059 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001060
Eric Laurenta4c5a552012-03-29 10:12:40 -07001061 Mutex::Autolock _l(mLock);
1062
Glenn Kasten142f5192014-03-25 17:44:59 -07001063 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001064 String8 out_s8;
1065
Dima Zavin799a70e2011-04-18 16:57:27 -07001066 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001067 char *s;
1068 {
1069 AutoMutex lock(mHardwareLock);
1070 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001071 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001072 s = dev->get_parameters(dev, keys.string());
1073 mHardwareStatus = AUDIO_HW_IDLE;
1074 }
John Grossmanef7740b2012-02-09 11:28:36 -08001075 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001076 free(s);
1077 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001078 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001079 }
1080
Mathias Agopian65ab4712010-07-14 17:59:35 -07001081 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1082 if (playbackThread != NULL) {
1083 return playbackThread->getParameters(keys);
1084 }
1085 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1086 if (recordThread != NULL) {
1087 return recordThread->getParameters(keys);
1088 }
1089 return String8("");
1090}
1091
Glenn Kastendd8104c2012-07-02 12:42:44 -07001092size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1093 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001094{
Eric Laurenta1884f92011-08-23 08:25:03 -07001095 status_t ret = initCheck();
1096 if (ret != NO_ERROR) {
1097 return 0;
1098 }
1099
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001100 AutoMutex lock(mHardwareLock);
1101 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001102 struct audio_config config;
1103 memset(&config, 0, sizeof(config));
1104 config.sample_rate = sampleRate;
1105 config.channel_mask = channelMask;
1106 config.format = format;
1107
John Grossmanee578c02012-07-23 17:05:46 -07001108 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1109 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001110 mHardwareStatus = AUDIO_HW_IDLE;
1111 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001112}
1113
Glenn Kasten5f972c02014-01-13 09:59:31 -08001114uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001115{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001116 Mutex::Autolock _l(mLock);
1117
1118 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1119 if (recordThread != NULL) {
1120 return recordThread->getInputFramesLost();
1121 }
1122 return 0;
1123}
1124
1125status_t AudioFlinger::setVoiceVolume(float value)
1126{
Eric Laurenta1884f92011-08-23 08:25:03 -07001127 status_t ret = initCheck();
1128 if (ret != NO_ERROR) {
1129 return ret;
1130 }
1131
Mathias Agopian65ab4712010-07-14 17:59:35 -07001132 // check calling permissions
1133 if (!settingsAllowed()) {
1134 return PERMISSION_DENIED;
1135 }
1136
1137 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001138 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001139 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001140 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001141 mHardwareStatus = AUDIO_HW_IDLE;
1142
1143 return ret;
1144}
1145
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001146status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001147 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001148{
1149 status_t status;
1150
1151 Mutex::Autolock _l(mLock);
1152
1153 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1154 if (playbackThread != NULL) {
1155 return playbackThread->getRenderPosition(halFrames, dspFrames);
1156 }
1157
1158 return BAD_VALUE;
1159}
1160
1161void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1162{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001163 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001164 bool clientAdded = false;
1165 {
1166 Mutex::Autolock _cl(mClientLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001167
Eric Laurent021cf962014-05-13 10:18:14 -07001168 pid_t pid = IPCThreadState::self()->getCallingPid();
1169 if (mNotificationClients.indexOfKey(pid) < 0) {
1170 sp<NotificationClient> notificationClient = new NotificationClient(this,
1171 client,
1172 pid);
1173 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001174
Eric Laurent021cf962014-05-13 10:18:14 -07001175 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001176
Eric Laurent021cf962014-05-13 10:18:14 -07001177 sp<IBinder> binder = client->asBinder();
1178 binder->linkToDeath(notificationClient);
1179 clientAdded = true;
1180 }
1181 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001182
Eric Laurent021cf962014-05-13 10:18:14 -07001183 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe9adc42014-05-26 16:03:08 -07001184 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent021cf962014-05-13 10:18:14 -07001185 if (clientAdded) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001186 // the config change is always sent from playback or record threads to avoid deadlock
1187 // with AudioSystem::gLock
1188 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001189 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001190 }
1191
1192 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001193 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001194 }
1195 }
1196}
1197
1198void AudioFlinger::removeNotificationClient(pid_t pid)
1199{
1200 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001201 {
1202 Mutex::Autolock _cl(mClientLock);
1203 mNotificationClients.removeItem(pid);
1204 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001205
Steve Block3856b092011-10-20 11:56:00 +01001206 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001207 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001208 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001209 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001210 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001211 ALOGV(" pid %d @ %d", ref->mPid, i);
1212 if (ref->mPid == pid) {
1213 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001214 mAudioSessionRefs.removeAt(i);
1215 delete ref;
1216 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001217 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001218 } else {
1219 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001220 }
1221 }
1222 if (removed) {
1223 purgeStaleEffects_l();
1224 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001225}
1226
Eric Laurent021cf962014-05-13 10:18:14 -07001227void AudioFlinger::audioConfigChanged(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001228{
Eric Laurent021cf962014-05-13 10:18:14 -07001229 Mutex::Autolock _l(mClientLock);
1230 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001231 for (size_t i = 0; i < size; i++) {
Eric Laurent021cf962014-05-13 10:18:14 -07001232 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event,
Eric Laurent10351942014-05-08 18:49:52 -07001233 ioHandle,
1234 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001235 }
1236}
1237
Eric Laurent021cf962014-05-13 10:18:14 -07001238// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001239void AudioFlinger::removeClient_l(pid_t pid)
1240{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001241 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001242 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001243 mClients.removeItem(pid);
1244}
1245
Eric Laurent717e1282012-06-29 16:36:52 -07001246// getEffectThread_l() must be called with AudioFlinger::mLock held
1247sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1248{
1249 sp<PlaybackThread> thread;
1250
1251 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1252 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1253 ALOG_ASSERT(thread == 0);
1254 thread = mPlaybackThreads.valueAt(i);
1255 }
1256 }
1257
1258 return thread;
1259}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001260
Mathias Agopian65ab4712010-07-14 17:59:35 -07001261
Mathias Agopian65ab4712010-07-14 17:59:35 -07001262
1263// ----------------------------------------------------------------------------
1264
1265AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1266 : RefBase(),
1267 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001268 // 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 -07001269 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001270 mPid(pid),
1271 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001272{
1273 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1274}
1275
Eric Laurent021cf962014-05-13 10:18:14 -07001276// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001277AudioFlinger::Client::~Client()
1278{
1279 mAudioFlinger->removeClient_l(mPid);
1280}
1281
Glenn Kasten435dbe62012-01-30 10:15:48 -08001282sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001283{
1284 return mMemoryDealer;
1285}
1286
John Grossman4ff14ba2012-02-08 16:37:41 -08001287// Reserve one of the limited slots for a timed audio track associated
1288// with this client
1289bool AudioFlinger::Client::reserveTimedTrack()
1290{
1291 const int kMaxTimedTracksPerClient = 4;
1292
1293 Mutex::Autolock _l(mTimedTrackLock);
1294
1295 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1296 ALOGW("can not create timed track - pid %d has exceeded the limit",
1297 mPid);
1298 return false;
1299 }
1300
1301 mTimedTrackCount++;
1302 return true;
1303}
1304
1305// Release a slot for a timed audio track
1306void AudioFlinger::Client::releaseTimedTrack()
1307{
1308 Mutex::Autolock _l(mTimedTrackLock);
1309 mTimedTrackCount--;
1310}
1311
Mathias Agopian65ab4712010-07-14 17:59:35 -07001312// ----------------------------------------------------------------------------
1313
1314AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1315 const sp<IAudioFlingerClient>& client,
1316 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001317 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001318{
1319}
1320
1321AudioFlinger::NotificationClient::~NotificationClient()
1322{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001323}
1324
Glenn Kasten0f11b512014-01-31 16:18:54 -08001325void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001326{
1327 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001328 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001329}
1330
Mathias Agopian65ab4712010-07-14 17:59:35 -07001331
1332// ----------------------------------------------------------------------------
1333
Jeff Brown893a5642013-08-16 20:19:26 -07001334static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1335 return audio_is_remote_submix_device(inDevice);
1336}
1337
Mathias Agopian65ab4712010-07-14 17:59:35 -07001338sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001339 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001340 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001341 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001342 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001343 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001344 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001345 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001346 int *sessionId,
Glenn Kastend776ac62014-05-07 09:16:09 -07001347 sp<IMemory>& cblk,
1348 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001349 status_t *status)
1350{
1351 sp<RecordThread::RecordTrack> recordTrack;
1352 sp<RecordHandle> recordHandle;
1353 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001354 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001355 int lSessionId;
1356
Glenn Kastend776ac62014-05-07 09:16:09 -07001357 cblk.clear();
1358 buffers.clear();
1359
Mathias Agopian65ab4712010-07-14 17:59:35 -07001360 // check calling permissions
1361 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001362 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001363 lStatus = PERMISSION_DENIED;
1364 goto Exit;
1365 }
1366
Glenn Kasten53b5d092014-02-05 10:00:23 -08001367 // further sample rate checks are performed by createRecordTrack_l()
1368 if (sampleRate == 0) {
1369 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1370 lStatus = BAD_VALUE;
1371 goto Exit;
1372 }
1373
Glenn Kasten0e0e8462014-03-13 15:02:40 -07001374 // we don't yet support anything other than 16-bit PCM
1375 if (!(audio_is_valid_format(format) &&
1376 audio_is_linear_pcm(format) && format == AUDIO_FORMAT_PCM_16_BIT)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001377 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001378 lStatus = BAD_VALUE;
1379 goto Exit;
1380 }
1381
Glenn Kasten53b5d092014-02-05 10:00:23 -08001382 // further channel mask checks are performed by createRecordTrack_l()
1383 if (!audio_is_input_channel(channelMask)) {
1384 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1385 lStatus = BAD_VALUE;
1386 goto Exit;
1387 }
1388
Glenn Kasten05997e22014-03-13 15:08:33 -07001389 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001390 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001391 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001392 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001393 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001394 lStatus = BAD_VALUE;
1395 goto Exit;
1396 }
1397
Jeff Brown893a5642013-08-16 20:19:26 -07001398 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1399 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001400 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001401 lStatus = PERMISSION_DENIED;
1402 goto Exit;
1403 }
1404
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001405 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001406 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001407
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001408 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409 lSessionId = *sessionId;
1410 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001411 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001412 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001413 if (sessionId != NULL) {
1414 *sessionId = lSessionId;
1415 }
1416 }
Glenn Kasten570f6332014-03-13 15:01:06 -07001417 ALOGV("openRecord() lSessionId: %d", lSessionId);
1418
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001419 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001420 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001421 frameCount, lSessionId,
1422 IPCThreadState::self()->getCallingUid(),
1423 flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001424 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001425 }
Glenn Kastene198c362013-08-13 09:13:36 -07001426
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001427 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001428 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001429 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe9adc42014-05-26 16:03:08 -07001430 // Don't hold mClientLock when releasing the reference on the track as the
1431 // destructor will acquire it.
1432 {
1433 Mutex::Autolock _cl(mClientLock);
1434 client.clear();
1435 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001436 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001437 goto Exit;
1438 }
1439
Glenn Kastend776ac62014-05-07 09:16:09 -07001440 cblk = recordTrack->getCblk();
1441 buffers = recordTrack->getBuffers();
1442
Glenn Kasten2fc14732013-08-05 14:58:14 -07001443 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001444 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001445
1446Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001447 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001448 return recordHandle;
1449}
1450
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001451
1452
Mathias Agopian65ab4712010-07-14 17:59:35 -07001453// ----------------------------------------------------------------------------
1454
Eric Laurenta4c5a552012-03-29 10:12:40 -07001455audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1456{
1457 if (!settingsAllowed()) {
1458 return 0;
1459 }
1460 Mutex::Autolock _l(mLock);
1461 return loadHwModule_l(name);
1462}
1463
1464// loadHwModule_l() must be called with AudioFlinger::mLock held
1465audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1466{
1467 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1468 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1469 ALOGW("loadHwModule() module %s already loaded", name);
1470 return mAudioHwDevs.keyAt(i);
1471 }
1472 }
1473
Eric Laurenta4c5a552012-03-29 10:12:40 -07001474 audio_hw_device_t *dev;
1475
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001476 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001477 if (rc) {
1478 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1479 return 0;
1480 }
1481
1482 mHardwareStatus = AUDIO_HW_INIT;
1483 rc = dev->init_check(dev);
1484 mHardwareStatus = AUDIO_HW_IDLE;
1485 if (rc) {
1486 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1487 return 0;
1488 }
1489
John Grossmanee578c02012-07-23 17:05:46 -07001490 // Check and cache this HAL's level of support for master mute and master
1491 // volume. If this is the first HAL opened, and it supports the get
1492 // methods, use the initial values provided by the HAL as the current
1493 // master mute and volume settings.
1494
1495 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1496 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001497 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001498
1499 if (0 == mAudioHwDevs.size()) {
1500 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1501 if (NULL != dev->get_master_volume) {
1502 float mv;
1503 if (OK == dev->get_master_volume(dev, &mv)) {
1504 mMasterVolume = mv;
1505 }
1506 }
1507
1508 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1509 if (NULL != dev->get_master_mute) {
1510 bool mm;
1511 if (OK == dev->get_master_mute(dev, &mm)) {
1512 mMasterMute = mm;
1513 }
1514 }
1515 }
1516
Eric Laurenta4c5a552012-03-29 10:12:40 -07001517 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001518 if ((NULL != dev->set_master_volume) &&
1519 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1520 flags = static_cast<AudioHwDevice::Flags>(flags |
1521 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1522 }
1523
1524 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1525 if ((NULL != dev->set_master_mute) &&
1526 (OK == dev->set_master_mute(dev, mMasterMute))) {
1527 flags = static_cast<AudioHwDevice::Flags>(flags |
1528 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1529 }
1530
Eric Laurenta4c5a552012-03-29 10:12:40 -07001531 mHardwareStatus = AUDIO_HW_IDLE;
1532 }
1533
1534 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001535 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001536
1537 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001538 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001539
1540 return handle;
1541
1542}
1543
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001544// ----------------------------------------------------------------------------
1545
Glenn Kasten3b16c762012-11-14 08:44:39 -08001546uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001547{
1548 Mutex::Autolock _l(mLock);
1549 PlaybackThread *thread = primaryPlaybackThread_l();
1550 return thread != NULL ? thread->sampleRate() : 0;
1551}
1552
Glenn Kastene33054e2012-11-14 12:54:39 -08001553size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001554{
1555 Mutex::Autolock _l(mLock);
1556 PlaybackThread *thread = primaryPlaybackThread_l();
1557 return thread != NULL ? thread->frameCountHAL() : 0;
1558}
1559
1560// ----------------------------------------------------------------------------
1561
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001562status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1563{
1564 uid_t uid = IPCThreadState::self()->getCallingUid();
1565 if (uid != AID_SYSTEM) {
1566 return PERMISSION_DENIED;
1567 }
1568 Mutex::Autolock _l(mLock);
1569 if (mIsDeviceTypeKnown) {
1570 return INVALID_OPERATION;
1571 }
1572 mIsLowRamDevice = isLowRamDevice;
1573 mIsDeviceTypeKnown = true;
1574 return NO_ERROR;
1575}
1576
1577// ----------------------------------------------------------------------------
1578
Eric Laurenta4c5a552012-03-29 10:12:40 -07001579audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1580 audio_devices_t *pDevices,
1581 uint32_t *pSamplingRate,
1582 audio_format_t *pFormat,
1583 audio_channel_mask_t *pChannelMask,
1584 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001585 audio_output_flags_t flags,
1586 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001587{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001588 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001589 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001590 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1591 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1592 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
Glenn Kasten937098b2013-06-26 11:19:36 -07001593 if (offloadInfo != NULL) {
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001594 config.offload_info = *offloadInfo;
1595 }
1596
Eric Laurentbfb1b832013-01-07 09:53:42 -08001597 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001598 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001599 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001600 config.sample_rate,
1601 config.format,
1602 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001603 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001604 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
Glenn Kastene198c362013-08-13 09:13:36 -07001605 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001606
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001607 if (pDevices == NULL || *pDevices == AUDIO_DEVICE_NONE) {
Glenn Kasten142f5192014-03-25 17:44:59 -07001608 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001609 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001610
Mathias Agopian65ab4712010-07-14 17:59:35 -07001611 Mutex::Autolock _l(mLock);
1612
Glenn Kasten32550952013-08-06 10:45:10 -07001613 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001614 if (outHwDev == NULL) {
Glenn Kasten142f5192014-03-25 17:44:59 -07001615 return AUDIO_IO_HANDLE_NONE;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001616 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001617
John Grossmanee578c02012-07-23 17:05:46 -07001618 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001619 audio_io_handle_t id = nextUniqueId();
1620
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001621 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001622
Glenn Kasten32550952013-08-06 10:45:10 -07001623 audio_stream_out_t *outStream = NULL;
Glenn Kasten34542ac2013-06-26 11:29:02 -07001624 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001625 id,
1626 *pDevices,
1627 (audio_output_flags_t)flags,
1628 &config,
1629 &outStream);
1630
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001631 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001632 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001633 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001634 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001635 config.sample_rate,
1636 config.format,
1637 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001638 status);
1639
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001640 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001641 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001642
Glenn Kasten32550952013-08-06 10:45:10 -07001643 PlaybackThread *thread;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001644 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1645 thread = new OffloadThread(this, output, id, *pDevices);
1646 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1647 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001648 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1649 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001650 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001651 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001652 } else {
1653 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001654 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001655 }
1656 mPlaybackThreads.add(id, thread);
1657
Glenn Kasten7c027242012-12-26 14:43:16 -08001658 if (pSamplingRate != NULL) {
1659 *pSamplingRate = config.sample_rate;
1660 }
1661 if (pFormat != NULL) {
1662 *pFormat = config.format;
1663 }
1664 if (pChannelMask != NULL) {
1665 *pChannelMask = config.channel_mask;
1666 }
1667 if (pLatencyMs != NULL) {
1668 *pLatencyMs = thread->latency();
1669 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001670
1671 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001672 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001673
1674 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001675 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001676 ALOGI("Using module %d has the primary audio interface", module);
1677 mPrimaryHardwareDev = outHwDev;
1678
1679 AutoMutex lock(mHardwareLock);
1680 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001681 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001682 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kastenc263ca02014-06-04 20:31:46 -07001683
1684 mPrimaryOutputSampleRate = config.sample_rate;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001685 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001686 return id;
1687 }
1688
Glenn Kasten142f5192014-03-25 17:44:59 -07001689 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001690}
1691
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001692audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1693 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001694{
1695 Mutex::Autolock _l(mLock);
1696 MixerThread *thread1 = checkMixerThread_l(output1);
1697 MixerThread *thread2 = checkMixerThread_l(output2);
1698
1699 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001700 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1701 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001702 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001703 }
1704
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001705 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001706 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1707 thread->addOutputTrack(thread2);
1708 mPlaybackThreads.add(id, thread);
1709 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001710 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001711 return id;
1712}
1713
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001714status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001715{
Glenn Kastend96c5722012-04-25 13:44:49 -07001716 return closeOutput_nonvirtual(output);
1717}
1718
1719status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1720{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001721 // keep strong reference on the playback thread so that
1722 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001723 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001724 {
1725 Mutex::Autolock _l(mLock);
1726 thread = checkPlaybackThread_l(output);
1727 if (thread == NULL) {
1728 return BAD_VALUE;
1729 }
1730
Steve Block3856b092011-10-20 11:56:00 +01001731 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001732
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001733 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001734 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001735 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001736 DuplicatingThread *dupThread =
1737 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001738 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001739
1740 }
1741 }
1742 }
1743
1744
1745 mPlaybackThreads.removeItem(output);
1746 // save all effects to the default thread
1747 if (mPlaybackThreads.size()) {
1748 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1749 if (dstThread != NULL) {
1750 // audioflinger lock is held here so the acquisition order of thread locks does not
1751 // matter
1752 Mutex::Autolock _dl(dstThread->mLock);
1753 Mutex::Autolock _sl(thread->mLock);
1754 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1755 for (size_t i = 0; i < effectChains.size(); i ++) {
1756 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001757 }
1758 }
1759 }
Eric Laurent021cf962014-05-13 10:18:14 -07001760 audioConfigChanged(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001761 }
1762 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001763 // The thread entity (active unit of execution) is no longer running here,
1764 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001765
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001766 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001767 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001768 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001769 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001770 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001771 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001772 }
1773 return NO_ERROR;
1774}
1775
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001776status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001777{
1778 Mutex::Autolock _l(mLock);
1779 PlaybackThread *thread = checkPlaybackThread_l(output);
1780
1781 if (thread == NULL) {
1782 return BAD_VALUE;
1783 }
1784
Steve Block3856b092011-10-20 11:56:00 +01001785 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001786 thread->suspend();
1787
1788 return NO_ERROR;
1789}
1790
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001791status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001792{
1793 Mutex::Autolock _l(mLock);
1794 PlaybackThread *thread = checkPlaybackThread_l(output);
1795
1796 if (thread == NULL) {
1797 return BAD_VALUE;
1798 }
1799
Steve Block3856b092011-10-20 11:56:00 +01001800 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001801
1802 thread->restore();
1803
1804 return NO_ERROR;
1805}
1806
Eric Laurenta4c5a552012-03-29 10:12:40 -07001807audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1808 audio_devices_t *pDevices,
1809 uint32_t *pSamplingRate,
1810 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001811 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001812{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001813 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001814 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001815 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1816 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1817 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1818
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001819 uint32_t reqSamplingRate = config.sample_rate;
1820 audio_format_t reqFormat = config.format;
Glenn Kastenf506e942013-08-06 10:49:43 -07001821 audio_channel_mask_t reqChannelMask = config.channel_mask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001822
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001823 if (pDevices == NULL || *pDevices == AUDIO_DEVICE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001824 return 0;
1825 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001826
Mathias Agopian65ab4712010-07-14 17:59:35 -07001827 Mutex::Autolock _l(mLock);
1828
Glenn Kasten32550952013-08-06 10:45:10 -07001829 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001830 if (inHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001831 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001832 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001833
John Grossmanee578c02012-07-23 17:05:46 -07001834 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001835 audio_io_handle_t id = nextUniqueId();
1836
Glenn Kasten32550952013-08-06 10:45:10 -07001837 audio_stream_in_t *inStream = NULL;
1838 status_t status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001839 &inStream);
Glenn Kastencac3daa2014-02-07 09:47:14 -08001840 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %#x, Channels %x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001841 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001842 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001843 config.sample_rate,
1844 config.format,
1845 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001846 status);
1847
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001848 // If the input could not be opened with the requested parameters and we can handle the
1849 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1850 // resample the input and do mono to stereo or stereo to mono conversions on 16 bit PCM inputs.
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001851 if (status == BAD_VALUE &&
1852 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1853 (config.sample_rate <= 2 * reqSamplingRate) &&
Andy Hunge5412692014-05-16 11:25:07 -07001854 (audio_channel_count_from_in_mask(config.channel_mask) <= FCC_2) &&
1855 (audio_channel_count_from_in_mask(reqChannelMask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07001856 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Glenn Kasten254af182012-07-03 14:59:05 -07001857 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001858 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001859 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07001860 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07001861 }
1862
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001863 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001864
Glenn Kasten46909e72013-02-26 09:20:22 -08001865#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001866 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1867 // or (re-)create if current Pipe is idle and does not match the new format
1868 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001869 enum {
1870 TEE_SINK_NO, // don't copy input
1871 TEE_SINK_NEW, // copy input using a new pipe
1872 TEE_SINK_OLD, // copy input using an existing pipe
1873 } kind;
1874 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
Andy Hunge5412692014-05-16 11:25:07 -07001875 audio_channel_count_from_in_mask(
1876 inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001877 if (!mTeeSinkInputEnabled) {
1878 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08001879 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001880 kind = TEE_SINK_NO;
1881 } else if (mRecordTeeSink == 0) {
1882 kind = TEE_SINK_NEW;
1883 } else if (mRecordTeeSink->getStrongCount() != 1) {
1884 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08001885 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001886 kind = TEE_SINK_OLD;
1887 } else {
1888 kind = TEE_SINK_NEW;
1889 }
1890 switch (kind) {
1891 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001892 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001893 size_t numCounterOffers = 0;
1894 const NBAIO_Format offers[1] = {format};
1895 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1896 ALOG_ASSERT(index == 0);
1897 PipeReader *pipeReader = new PipeReader(*pipe);
1898 numCounterOffers = 0;
1899 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1900 ALOG_ASSERT(index == 0);
1901 mRecordTeeSink = pipe;
1902 mRecordTeeSource = pipeReader;
1903 teeSink = pipe;
1904 }
1905 break;
1906 case TEE_SINK_OLD:
1907 teeSink = mRecordTeeSink;
1908 break;
1909 case TEE_SINK_NO:
1910 default:
1911 break;
1912 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001913#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001914
Dima Zavin799a70e2011-04-18 16:57:27 -07001915 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1916
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001917 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001918 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001919 // pre processing modules
Glenn Kasten32550952013-08-06 10:45:10 -07001920 RecordThread *thread = new RecordThread(this,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001921 input,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001922 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001923 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001924 *pDevices
1925#ifdef TEE_SINK
1926 , teeSink
1927#endif
1928 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001929 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001930 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001931 if (pSamplingRate != NULL) {
1932 *pSamplingRate = reqSamplingRate;
1933 }
1934 if (pFormat != NULL) {
1935 *pFormat = config.format;
1936 }
1937 if (pChannelMask != NULL) {
Glenn Kastenf506e942013-08-06 10:49:43 -07001938 *pChannelMask = reqChannelMask;
Glenn Kasten7c027242012-12-26 14:43:16 -08001939 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001940
Mathias Agopian65ab4712010-07-14 17:59:35 -07001941 // notify client processes of the new input creation
Eric Laurent021cf962014-05-13 10:18:14 -07001942 thread->audioConfigChanged(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001943 return id;
1944 }
1945
1946 return 0;
1947}
1948
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001949status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001950{
Glenn Kastend96c5722012-04-25 13:44:49 -07001951 return closeInput_nonvirtual(input);
1952}
1953
1954status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1955{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001956 // keep strong reference on the record thread so that
1957 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001958 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001959 {
1960 Mutex::Autolock _l(mLock);
1961 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001962 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001963 return BAD_VALUE;
1964 }
1965
Steve Block3856b092011-10-20 11:56:00 +01001966 ALOGV("closeInput() %d", input);
Eric Laurent021cf962014-05-13 10:18:14 -07001967 audioConfigChanged(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001968 mRecordThreads.removeItem(input);
1969 }
1970 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001971 // The thread entity (active unit of execution) is no longer running here,
1972 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001973
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001974 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001975 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001976 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001977 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001978 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001979
1980 return NO_ERROR;
1981}
1982
Glenn Kastend2304db2014-02-03 07:40:31 -08001983status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001984{
1985 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08001986 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001987
1988 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1989 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001990 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001991 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001992
1993 return NO_ERROR;
1994}
1995
1996
1997int AudioFlinger::newAudioSessionId()
1998{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001999 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002000}
2001
Marco Nelissend457c972014-02-11 08:47:07 -08002002void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002003{
2004 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002005 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002006 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2007 if (pid != -1 && (caller == getpid_cached)) {
2008 caller = pid;
2009 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002010
Eric Laurent021cf962014-05-13 10:18:14 -07002011 {
2012 Mutex::Autolock _cl(mClientLock);
2013 // Ignore requests received from processes not known as notification client. The request
2014 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2015 // called from a different pid leaving a stale session reference. Also we don't know how
2016 // to clear this reference if the client process dies.
2017 if (mNotificationClients.indexOfKey(caller) < 0) {
2018 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2019 return;
2020 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002021 }
2022
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002023 size_t num = mAudioSessionRefs.size();
2024 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002025 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002026 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2027 ref->mCnt++;
2028 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002029 return;
2030 }
2031 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002032 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2033 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002034}
2035
Marco Nelissend457c972014-02-11 08:47:07 -08002036void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002037{
2038 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002039 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002040 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2041 if (pid != -1 && (caller == getpid_cached)) {
2042 caller = pid;
2043 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002044 size_t num = mAudioSessionRefs.size();
2045 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002046 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002047 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2048 ref->mCnt--;
2049 ALOGV(" decremented refcount to %d", ref->mCnt);
2050 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002051 mAudioSessionRefs.removeAt(i);
2052 delete ref;
2053 purgeStaleEffects_l();
2054 }
2055 return;
2056 }
2057 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002058 // If the caller is mediaserver it is likely that the session being released was acquired
2059 // on behalf of a process not in notification clients and we ignore the warning.
2060 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002061}
2062
2063void AudioFlinger::purgeStaleEffects_l() {
2064
Steve Block3856b092011-10-20 11:56:00 +01002065 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002066
2067 Vector< sp<EffectChain> > chains;
2068
2069 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2070 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2071 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2072 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002073 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2074 chains.push(ec);
2075 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002076 }
2077 }
2078 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2079 sp<RecordThread> t = mRecordThreads.valueAt(i);
2080 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2081 sp<EffectChain> ec = t->mEffectChains[j];
2082 chains.push(ec);
2083 }
2084 }
2085
2086 for (size_t i = 0; i < chains.size(); i++) {
2087 sp<EffectChain> ec = chains[i];
2088 int sessionid = ec->sessionId();
2089 sp<ThreadBase> t = ec->mThread.promote();
2090 if (t == 0) {
2091 continue;
2092 }
2093 size_t numsessionrefs = mAudioSessionRefs.size();
2094 bool found = false;
2095 for (size_t k = 0; k < numsessionrefs; k++) {
2096 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002097 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002098 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002099 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002100 found = true;
2101 break;
2102 }
2103 }
2104 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002105 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002106 // remove all effects from the chain
2107 while (ec->mEffects.size()) {
2108 sp<EffectModule> effect = ec->mEffects[0];
2109 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002110 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002111 if (effect->purgeHandles()) {
2112 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002113 }
2114 AudioSystem::unregisterEffect(effect->id());
2115 }
2116 }
2117 }
2118 return;
2119}
2120
Mathias Agopian65ab4712010-07-14 17:59:35 -07002121// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002122AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002123{
Glenn Kastena1117922012-01-26 10:53:32 -08002124 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002125}
2126
2127// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002128AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002129{
2130 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002131 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002132}
2133
2134// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002135AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002136{
Glenn Kastena1117922012-01-26 10:53:32 -08002137 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002138}
2139
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002140uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002141{
Glenn Kastenbcefec32014-01-17 12:09:05 -08002142 return (uint32_t) android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002143}
2144
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002145AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002146{
2147 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2148 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002149 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002150 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002151 return thread;
2152 }
2153 }
2154 return NULL;
2155}
2156
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002157audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002158{
2159 PlaybackThread *thread = primaryPlaybackThread_l();
2160
2161 if (thread == NULL) {
2162 return 0;
2163 }
2164
Eric Laurentf1c04f92012-08-28 14:26:53 -07002165 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002166}
2167
Eric Laurenta011e352012-03-29 15:51:43 -07002168sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2169 int triggerSession,
2170 int listenerSession,
2171 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002172 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002173{
2174 Mutex::Autolock _l(mLock);
2175
2176 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2177 status_t playStatus = NAME_NOT_FOUND;
2178 status_t recStatus = NAME_NOT_FOUND;
2179 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2180 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2181 if (playStatus == NO_ERROR) {
2182 return event;
2183 }
2184 }
2185 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2186 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2187 if (recStatus == NO_ERROR) {
2188 return event;
2189 }
2190 }
2191 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2192 mPendingSyncEvents.add(event);
2193 } else {
2194 ALOGV("createSyncEvent() invalid event %d", event->type());
2195 event.clear();
2196 }
2197 return event;
2198}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002199
Mathias Agopian65ab4712010-07-14 17:59:35 -07002200// ----------------------------------------------------------------------------
2201// Effect management
2202// ----------------------------------------------------------------------------
2203
2204
Glenn Kastenf587ba52012-01-26 16:25:10 -08002205status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002206{
2207 Mutex::Autolock _l(mLock);
2208 return EffectQueryNumberEffects(numEffects);
2209}
2210
Glenn Kastenf587ba52012-01-26 16:25:10 -08002211status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002212{
2213 Mutex::Autolock _l(mLock);
2214 return EffectQueryEffect(index, descriptor);
2215}
2216
Glenn Kasten5e92a782012-01-30 07:40:52 -08002217status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002218 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002219{
2220 Mutex::Autolock _l(mLock);
2221 return EffectGetDescriptor(pUuid, descriptor);
2222}
2223
2224
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002225sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002226 effect_descriptor_t *pDesc,
2227 const sp<IEffectClient>& effectClient,
2228 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002229 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002230 int sessionId,
2231 status_t *status,
2232 int *id,
2233 int *enabled)
2234{
2235 status_t lStatus = NO_ERROR;
2236 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002237 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002238
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002239 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002240 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002241 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002242
2243 if (pDesc == NULL) {
2244 lStatus = BAD_VALUE;
2245 goto Exit;
2246 }
2247
Eric Laurent84e9a102010-09-23 16:10:16 -07002248 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002249 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002250 lStatus = PERMISSION_DENIED;
2251 goto Exit;
2252 }
2253
Dima Zavinfce7a472011-04-19 22:30:36 -07002254 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002255 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002256 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002257 lStatus = PERMISSION_DENIED;
2258 goto Exit;
2259 }
2260
Mathias Agopian65ab4712010-07-14 17:59:35 -07002261 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002262 if (!EffectIsNullUuid(&pDesc->uuid)) {
2263 // if uuid is specified, request effect descriptor
2264 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2265 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002266 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002267 goto Exit;
2268 }
2269 } else {
2270 // if uuid is not specified, look for an available implementation
2271 // of the required type in effect factory
2272 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002273 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002274 lStatus = BAD_VALUE;
2275 goto Exit;
2276 }
2277 uint32_t numEffects = 0;
2278 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002279 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002280 bool found = false;
2281
2282 lStatus = EffectQueryNumberEffects(&numEffects);
2283 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002284 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002285 goto Exit;
2286 }
2287 for (uint32_t i = 0; i < numEffects; i++) {
2288 lStatus = EffectQueryEffect(i, &desc);
2289 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002290 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002291 continue;
2292 }
2293 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2294 // If matching type found save effect descriptor. If the session is
2295 // 0 and the effect is not auxiliary, continue enumeration in case
2296 // an auxiliary version of this effect type is available
2297 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002298 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002299 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002300 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2301 break;
2302 }
2303 }
2304 }
2305 if (!found) {
2306 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002307 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002308 goto Exit;
2309 }
2310 // For same effect type, chose auxiliary version over insert version if
2311 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002312 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002313 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002314 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002315 }
2316 }
2317
2318 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002319 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002320 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2321 lStatus = INVALID_OPERATION;
2322 goto Exit;
2323 }
2324
Eric Laurent59255e42011-07-27 19:49:51 -07002325 // check recording permission for visualizer
2326 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2327 !recordingAllowed()) {
2328 lStatus = PERMISSION_DENIED;
2329 goto Exit;
2330 }
2331
Mathias Agopian65ab4712010-07-14 17:59:35 -07002332 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002333 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002334 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002335 // if the output returned by getOutputForEffect() is removed before we lock the
2336 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2337 // and we will exit safely
2338 io = AudioSystem::getOutputForEffect(&desc);
2339 ALOGV("createEffect got output %d", io);
2340 }
2341
2342 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002343
2344 // If output is not specified try to find a matching audio session ID in one of the
2345 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002346 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2347 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002348 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002349 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002350 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2351 // output must be specified by AudioPolicyManager when using session
2352 // AUDIO_SESSION_OUTPUT_STAGE
2353 lStatus = BAD_VALUE;
2354 goto Exit;
2355 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002356 // look for the thread where the specified audio session is present
2357 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2358 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2359 io = mPlaybackThreads.keyAt(i);
2360 break;
2361 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002362 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002363 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002364 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2365 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2366 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002367 break;
2368 }
2369 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002370 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002371 // If no output thread contains the requested session ID, default to
2372 // first output. The effect chain will be moved to the correct output
2373 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002374 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002375 io = mPlaybackThreads.keyAt(0);
2376 }
Steve Block3856b092011-10-20 11:56:00 +01002377 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002378 }
2379 ThreadBase *thread = checkRecordThread_l(io);
2380 if (thread == NULL) {
2381 thread = checkPlaybackThread_l(io);
2382 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002383 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002384 lStatus = BAD_VALUE;
2385 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002386 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002387 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002388
Eric Laurent021cf962014-05-13 10:18:14 -07002389 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002390
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002391 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002392 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2393 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002394 if (handle != 0 && id != NULL) {
2395 *id = handle->id();
2396 }
Eric Laurentfe9adc42014-05-26 16:03:08 -07002397 if (handle == 0) {
2398 // remove local strong reference to Client with mClientLock held
2399 Mutex::Autolock _cl(mClientLock);
2400 client.clear();
2401 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002402 }
2403
2404Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002405 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002406 return handle;
2407}
2408
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002409status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2410 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002411{
Steve Block3856b092011-10-20 11:56:00 +01002412 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002413 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002414 Mutex::Autolock _l(mLock);
2415 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002416 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002417 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002418 }
Eric Laurentde070132010-07-13 04:45:46 -07002419 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2420 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002421 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002422 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002423 }
Eric Laurentde070132010-07-13 04:45:46 -07002424 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2425 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002426 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002427 return BAD_VALUE;
2428 }
2429
2430 Mutex::Autolock _dl(dstThread->mLock);
2431 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002432 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002433}
2434
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002435// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002436status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002437 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002438 AudioFlinger::PlaybackThread *dstThread,
2439 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002440{
Steve Block3856b092011-10-20 11:56:00 +01002441 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002442 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002443
Eric Laurent59255e42011-07-27 19:49:51 -07002444 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002445 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002446 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002447 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002448 return INVALID_OPERATION;
2449 }
2450
Eric Laurent39e94f82010-07-28 01:32:47 -07002451 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002452 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002453 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002454 // removed.
2455 srcThread->removeEffectChain_l(chain);
2456
2457 // transfer all effects one by one so that new effect chain is created on new thread with
2458 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002459 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002460 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002461 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002462 Vector< sp<EffectModule> > removed;
2463 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002464 while (effect != 0) {
2465 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002466 removed.add(effect);
2467 status = dstThread->addEffect_l(effect);
2468 if (status != NO_ERROR) {
2469 break;
2470 }
Eric Laurentec35a142011-10-05 17:42:25 -07002471 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2472 if (effect->state() == EffectModule::ACTIVE ||
2473 effect->state() == EffectModule::STOPPING) {
2474 effect->start();
2475 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002476 // if the move request is not received from audio policy manager, the effect must be
2477 // re-registered with the new strategy and output
2478 if (dstChain == 0) {
2479 dstChain = effect->chain().promote();
2480 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002481 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002482 status = NO_INIT;
2483 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002484 }
2485 strategy = dstChain->strategy();
2486 }
2487 if (reRegister) {
2488 AudioSystem::unregisterEffect(effect->id());
2489 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002490 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002491 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002492 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002493 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002494 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002495 }
Eric Laurentde070132010-07-13 04:45:46 -07002496 effect = chain->getEffectFromId_l(0);
2497 }
2498
Eric Laurent5baf2af2013-09-12 17:37:00 -07002499 if (status != NO_ERROR) {
2500 for (size_t i = 0; i < removed.size(); i++) {
2501 srcThread->addEffect_l(removed[i]);
2502 if (dstChain != 0 && reRegister) {
2503 AudioSystem::unregisterEffect(removed[i]->id());
2504 AudioSystem::registerEffect(&removed[i]->desc(),
2505 srcThread->id(),
2506 strategy,
2507 sessionId,
2508 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002509 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002510 }
2511 }
2512 }
2513
2514 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002515}
2516
Eric Laurent5baf2af2013-09-12 17:37:00 -07002517bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002518{
2519 if (mGlobalEffectEnableTime != 0 &&
2520 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2521 return true;
2522 }
2523
2524 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2525 sp<EffectChain> ec =
2526 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002527 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002528 return true;
2529 }
2530 }
2531 return false;
2532}
2533
Eric Laurent5baf2af2013-09-12 17:37:00 -07002534void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002535{
2536 Mutex::Autolock _l(mLock);
2537
2538 mGlobalEffectEnableTime = systemTime();
2539
2540 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2541 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2542 if (t->mType == ThreadBase::OFFLOAD) {
2543 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2544 }
2545 }
2546
2547}
2548
Glenn Kastenda6ef132013-01-10 12:31:01 -08002549struct Entry {
2550#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2551 char mName[MAX_NAME];
2552};
2553
2554int comparEntry(const void *p1, const void *p2)
2555{
2556 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2557}
2558
Glenn Kasten46909e72013-02-26 09:20:22 -08002559#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002560void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002561{
Eric Laurent81784c32012-11-19 14:55:58 -08002562 NBAIO_Source *teeSource = source.get();
2563 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002564 // .wav rotation
2565 // There is a benign race condition if 2 threads call this simultaneously.
2566 // They would both traverse the directory, but the result would simply be
2567 // failures at unlink() which are ignored. It's also unlikely since
2568 // normally dumpsys is only done by bugreport or from the command line.
2569 char teePath[32+256];
2570 strcpy(teePath, "/data/misc/media");
2571 size_t teePathLen = strlen(teePath);
2572 DIR *dir = opendir(teePath);
2573 teePath[teePathLen++] = '/';
2574 if (dir != NULL) {
2575#define MAX_SORT 20 // number of entries to sort
2576#define MAX_KEEP 10 // number of entries to keep
2577 struct Entry entries[MAX_SORT];
2578 size_t entryCount = 0;
2579 while (entryCount < MAX_SORT) {
2580 struct dirent de;
2581 struct dirent *result = NULL;
2582 int rc = readdir_r(dir, &de, &result);
2583 if (rc != 0) {
2584 ALOGW("readdir_r failed %d", rc);
2585 break;
2586 }
2587 if (result == NULL) {
2588 break;
2589 }
2590 if (result != &de) {
2591 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2592 break;
2593 }
2594 // ignore non .wav file entries
2595 size_t nameLen = strlen(de.d_name);
2596 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2597 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2598 continue;
2599 }
2600 strcpy(entries[entryCount++].mName, de.d_name);
2601 }
2602 (void) closedir(dir);
2603 if (entryCount > MAX_KEEP) {
2604 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2605 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2606 strcpy(&teePath[teePathLen], entries[i].mName);
2607 (void) unlink(teePath);
2608 }
2609 }
2610 } else {
2611 if (fd >= 0) {
2612 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2613 }
2614 }
Eric Laurent81784c32012-11-19 14:55:58 -08002615 char teeTime[16];
2616 struct timeval tv;
2617 gettimeofday(&tv, NULL);
2618 struct tm tm;
2619 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002620 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2621 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2622 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2623 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002624 if (teeFd >= 0) {
2625 char wavHeader[44];
2626 memcpy(wavHeader,
2627 "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",
2628 sizeof(wavHeader));
2629 NBAIO_Format format = teeSource->format();
2630 unsigned channelCount = Format_channelCount(format);
2631 ALOG_ASSERT(channelCount <= FCC_2);
2632 uint32_t sampleRate = Format_sampleRate(format);
2633 wavHeader[22] = channelCount; // number of channels
2634 wavHeader[24] = sampleRate; // sample rate
2635 wavHeader[25] = sampleRate >> 8;
2636 wavHeader[32] = channelCount * 2; // block alignment
2637 write(teeFd, wavHeader, sizeof(wavHeader));
2638 size_t total = 0;
2639 bool firstRead = true;
2640 for (;;) {
2641#define TEE_SINK_READ 1024
2642 short buffer[TEE_SINK_READ * FCC_2];
2643 size_t count = TEE_SINK_READ;
2644 ssize_t actual = teeSource->read(buffer, count,
2645 AudioBufferProvider::kInvalidPTS);
2646 bool wasFirstRead = firstRead;
2647 firstRead = false;
2648 if (actual <= 0) {
2649 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2650 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002651 }
Eric Laurent81784c32012-11-19 14:55:58 -08002652 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002653 }
Eric Laurent81784c32012-11-19 14:55:58 -08002654 ALOG_ASSERT(actual <= (ssize_t)count);
2655 write(teeFd, buffer, actual * channelCount * sizeof(short));
2656 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002657 }
Eric Laurent81784c32012-11-19 14:55:58 -08002658 lseek(teeFd, (off_t) 4, SEEK_SET);
2659 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2660 write(teeFd, &temp, sizeof(temp));
2661 lseek(teeFd, (off_t) 40, SEEK_SET);
2662 temp = total * channelCount * sizeof(short);
2663 write(teeFd, &temp, sizeof(temp));
2664 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002665 if (fd >= 0) {
2666 fdprintf(fd, "tee copied to %s\n", teePath);
2667 }
Eric Laurent59255e42011-07-27 19:49:51 -07002668 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002669 if (fd >= 0) {
2670 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2671 }
Eric Laurent59255e42011-07-27 19:49:51 -07002672 }
2673 }
2674}
Glenn Kasten46909e72013-02-26 09:20:22 -08002675#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002676
Mathias Agopian65ab4712010-07-14 17:59:35 -07002677// ----------------------------------------------------------------------------
2678
2679status_t AudioFlinger::onTransact(
2680 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2681{
2682 return BnAudioFlinger::onTransact(code, data, reply, flags);
2683}
2684
Mathias Agopian65ab4712010-07-14 17:59:35 -07002685}; // namespace android