blob: 447b208ec5f02a5eae3b7e82b7dfc45422503f46 [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 Laurent5806b352014-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),
172 mGlobalEffectEnableTime(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700173{
Glenn Kasten949a9262013-04-16 12:35:20 -0700174 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800175 char value[PROPERTY_VALUE_MAX];
176 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
177 if (doLog) {
Glenn Kastencf515ab2014-03-14 16:01:58 -0700178 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters", MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800179 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700180
Glenn Kasten46909e72013-02-26 09:20:22 -0800181#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800182 (void) property_get("ro.debuggable", value, "0");
183 int debuggable = atoi(value);
184 int teeEnabled = 0;
185 if (debuggable) {
186 (void) property_get("af.tee", value, "0");
187 teeEnabled = atoi(value);
188 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800189 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700190 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800191 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700192 }
193 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800194 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700195 }
196 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800197 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700198 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800199#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700200}
201
202void AudioFlinger::onFirstRef()
203{
Dima Zavin799a70e2011-04-18 16:57:27 -0700204 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700205
Eric Laurent93575202011-01-18 18:39:02 -0800206 Mutex::Autolock _l(mLock);
207
Dima Zavin799a70e2011-04-18 16:57:27 -0700208 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800209 char val_str[PROPERTY_VALUE_MAX] = { 0 };
210 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
211 uint32_t int_val;
212 if (1 == sscanf(val_str, "%u", &int_val)) {
213 mStandbyTimeInNsecs = milliseconds(int_val);
214 ALOGI("Using %u mSec as standby time.", int_val);
215 } else {
216 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
217 ALOGI("Using default %u mSec as standby time.",
218 (uint32_t)(mStandbyTimeInNsecs / 1000000));
219 }
220 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700221
Eric Laurent1c333e22014-05-20 10:48:17 -0700222 mPatchPanel = new PatchPanel(this);
223
Eric Laurenta4c5a552012-03-29 10:12:40 -0700224 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700225}
226
227AudioFlinger::~AudioFlinger()
228{
229 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700230 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700231 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700232 }
233 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700234 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700235 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700236 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700237
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800238 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
239 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700240 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
241 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700243
244 // Tell media.log service about any old writers that still need to be unregistered
245 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
246 if (binder != 0) {
247 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
248 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
249 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
250 mUnregisteredWriters.pop();
251 mediaLogService->unregisterWriter(iMemory);
252 }
253 }
254
Mathias Agopian65ab4712010-07-14 17:59:35 -0700255}
256
Eric Laurenta4c5a552012-03-29 10:12:40 -0700257static const char * const audio_interfaces[] = {
258 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
259 AUDIO_HARDWARE_MODULE_ID_A2DP,
260 AUDIO_HARDWARE_MODULE_ID_USB,
261};
262#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
263
John Grossmanee578c02012-07-23 17:05:46 -0700264AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
265 audio_module_handle_t module,
266 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700267{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700268 // if module is 0, the request comes from an old policy manager and we should load
269 // well known modules
270 if (module == 0) {
271 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
272 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
273 loadHwModule_l(audio_interfaces[i]);
274 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700275 // then try to find a module supporting the requested device.
276 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
277 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
278 audio_hw_device_t *dev = audioHwDevice->hwDevice();
279 if ((dev->get_supported_devices != NULL) &&
280 (dev->get_supported_devices(dev) & devices) == devices)
281 return audioHwDevice;
282 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700283 } else {
284 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700285 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
286 if (audioHwDevice != NULL) {
287 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700288 }
289 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700290
Dima Zavin799a70e2011-04-18 16:57:27 -0700291 return NULL;
292}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700293
Glenn Kasten0f11b512014-01-31 16:18:54 -0800294void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700295{
296 const size_t SIZE = 256;
297 char buffer[SIZE];
298 String8 result;
299
300 result.append("Clients:\n");
301 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800302 sp<Client> client = mClients.valueAt(i).promote();
303 if (client != 0) {
304 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
305 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700306 }
307 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700308
Eric Laurentd1b28d42013-09-18 18:47:13 -0700309 result.append("Notification Clients:\n");
310 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
311 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
312 result.append(buffer);
313 }
314
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700315 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800316 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700317 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
318 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800319 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700320 result.append(buffer);
321 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700322 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323}
324
325
Glenn Kasten0f11b512014-01-31 16:18:54 -0800326void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700327{
328 const size_t SIZE = 256;
329 char buffer[SIZE];
330 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800331 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700332
John Grossman4ff14ba2012-02-08 16:37:41 -0800333 snprintf(buffer, SIZE, "Hardware status: %d\n"
334 "Standby Time mSec: %u\n",
335 hardwareStatus,
336 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700337 result.append(buffer);
338 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700339}
340
Glenn Kasten0f11b512014-01-31 16:18:54 -0800341void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700342{
343 const size_t SIZE = 256;
344 char buffer[SIZE];
345 String8 result;
346 snprintf(buffer, SIZE, "Permission Denial: "
347 "can't dump AudioFlinger from pid=%d, uid=%d\n",
348 IPCThreadState::self()->getCallingPid(),
349 IPCThreadState::self()->getCallingUid());
350 result.append(buffer);
351 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700352}
353
Eric Laurent81784c32012-11-19 14:55:58 -0800354bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700355{
356 bool locked = false;
357 for (int i = 0; i < kDumpLockRetries; ++i) {
358 if (mutex.tryLock() == NO_ERROR) {
359 locked = true;
360 break;
361 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800362 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700363 }
364 return locked;
365}
366
367status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
368{
Glenn Kasten44deb052012-02-05 18:09:08 -0800369 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370 dumpPermissionDenial(fd, args);
371 } else {
372 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800373 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700374 if (!hardwareLocked) {
375 String8 result(kHardwareLockedString);
376 write(fd, result.string(), result.size());
377 } else {
378 mHardwareLock.unlock();
379 }
380
Eric Laurent81784c32012-11-19 14:55:58 -0800381 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700382
383 // failed to lock - AudioFlinger is probably deadlocked
384 if (!locked) {
385 String8 result(kDeadlockedString);
386 write(fd, result.string(), result.size());
387 }
388
Eric Laurent021cf962014-05-13 10:18:14 -0700389 bool clientLocked = dumpTryLock(mClientLock);
390 if (!clientLocked) {
391 String8 result(kClientLockedString);
392 write(fd, result.string(), result.size());
393 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700394 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700395 if (clientLocked) {
396 mClientLock.unlock();
397 }
398
Mathias Agopian65ab4712010-07-14 17:59:35 -0700399 dumpInternals(fd, args);
400
401 // dump playback threads
402 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
403 mPlaybackThreads.valueAt(i)->dump(fd, args);
404 }
405
406 // dump record threads
407 for (size_t i = 0; i < mRecordThreads.size(); i++) {
408 mRecordThreads.valueAt(i)->dump(fd, args);
409 }
410
Dima Zavin799a70e2011-04-18 16:57:27 -0700411 // dump all hardware devs
412 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700413 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700414 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700415 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700416
Glenn Kasten46909e72013-02-26 09:20:22 -0800417#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700418 // dump the serially shared record tee sink
419 if (mRecordTeeSource != 0) {
420 dumpTee(fd, mRecordTeeSource);
421 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800422#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700423
Glenn Kastend65d73c2012-06-22 17:21:07 -0700424 if (locked) {
425 mLock.unlock();
426 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800427
428 // append a copy of media.log here by forwarding fd to it, but don't attempt
429 // to lookup the service if it's not running, as it will block for a second
430 if (mLogMemoryDealer != 0) {
431 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
432 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700433 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800434 Vector<String16> args;
435 binder->dump(fd, args);
436 }
437 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700438 }
439 return NO_ERROR;
440}
441
Eric Laurent021cf962014-05-13 10:18:14 -0700442sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800443{
Eric Laurent021cf962014-05-13 10:18:14 -0700444 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800445 // If pid is already in the mClients wp<> map, then use that entry
446 // (for which promote() is always != 0), otherwise create a new entry and Client.
447 sp<Client> client = mClients.valueFor(pid).promote();
448 if (client == 0) {
449 client = new Client(this, pid);
450 mClients.add(pid, client);
451 }
452
453 return client;
454}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700455
Glenn Kasten9e58b552013-01-18 15:09:48 -0800456sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
457{
Glenn Kasten481fb672013-09-30 14:39:28 -0700458 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800459 if (mLogMemoryDealer == 0) {
460 return new NBLog::Writer();
461 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800462 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700463 // Similarly if we can't contact the media.log service, also return a dummy writer
464 if (binder == 0) {
465 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800466 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700467 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
468 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
469 // If allocation fails, consult the vector of previously unregistered writers
470 // and garbage-collect one or more them until an allocation succeeds
471 if (shared == 0) {
472 Mutex::Autolock _l(mUnregisteredWritersLock);
473 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
474 {
475 // Pick the oldest stale writer to garbage-collect
476 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
477 mUnregisteredWriters.removeAt(0);
478 mediaLogService->unregisterWriter(iMemory);
479 // Now the media.log remote reference to IMemory is gone. When our last local
480 // reference to IMemory also drops to zero at end of this block,
481 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
482 }
483 // Re-attempt the allocation
484 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
485 if (shared != 0) {
486 goto success;
487 }
488 }
489 // Even after garbage-collecting all old writers, there is still not enough memory,
490 // so return a dummy writer
491 return new NBLog::Writer();
492 }
493success:
494 mediaLogService->registerWriter(shared, size, name);
495 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800496}
497
498void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
499{
Glenn Kasten685ef092013-02-04 08:15:34 -0800500 if (writer == 0) {
501 return;
502 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800503 sp<IMemory> iMemory(writer->getIMemory());
504 if (iMemory == 0) {
505 return;
506 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700507 // Rather than removing the writer immediately, append it to a queue of old writers to
508 // be garbage-collected later. This allows us to continue to view old logs for a while.
509 Mutex::Autolock _l(mUnregisteredWritersLock);
510 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800511}
512
Mathias Agopian65ab4712010-07-14 17:59:35 -0700513// IAudioFlinger interface
514
515
516sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800517 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700518 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800519 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700520 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800521 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800522 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700523 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800524 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800525 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700526 int *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800527 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700528 status_t *status)
529{
530 sp<PlaybackThread::Track> track;
531 sp<TrackHandle> trackHandle;
532 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700533 status_t lStatus;
534 int lSessionId;
535
Glenn Kasten263709e2012-01-06 08:40:01 -0800536 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
537 // but if someone uses binder directly they could bypass that and cause us to crash
538 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000539 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700540 lStatus = BAD_VALUE;
541 goto Exit;
542 }
543
Glenn Kasten53b5d092014-02-05 10:00:23 -0800544 // further sample rate checks are performed by createTrack_l() depending on the thread type
545 if (sampleRate == 0) {
546 ALOGE("createTrack() invalid sample rate %u", sampleRate);
547 lStatus = BAD_VALUE;
548 goto Exit;
549 }
550
551 // further channel mask checks are performed by createTrack_l() depending on the thread type
552 if (!audio_is_output_channel(channelMask)) {
553 ALOGE("createTrack() invalid channel mask %#x", channelMask);
554 lStatus = BAD_VALUE;
555 goto Exit;
556 }
557
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700558 // further format checks are performed by createTrack_l() depending on the thread type
559 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800560 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700561 lStatus = BAD_VALUE;
562 goto Exit;
563 }
564
Glenn Kasten663c2242013-09-24 11:52:37 -0700565 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
566 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
567 lStatus = BAD_VALUE;
568 goto Exit;
569 }
570
Mathias Agopian65ab4712010-07-14 17:59:35 -0700571 {
572 Mutex::Autolock _l(mLock);
573 PlaybackThread *thread = checkPlaybackThread_l(output);
574 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800575 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700576 lStatus = BAD_VALUE;
577 goto Exit;
578 }
579
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800580 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -0700581 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700582
Glenn Kastene848bd92014-03-13 15:00:32 -0700583 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700584 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700585 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700586 // check if an effect chain with the same session ID is present on another
587 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700588 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700589 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
590 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700591 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700592 if (sessions & PlaybackThread::EFFECT_SESSION) {
593 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700594 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700595 }
Eric Laurentde070132010-07-13 04:45:46 -0700596 }
597 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700598 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700599 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700600 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700601 if (sessionId != NULL) {
602 *sessionId = lSessionId;
603 }
604 }
Steve Block3856b092011-10-20 11:56:00 +0100605 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700606
607 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800608 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800609 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700610 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700611
612 // move effect chain to this output thread if an effect on same session was waiting
613 // for a track to be created
614 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700615 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700616 Mutex::Autolock _dl(thread->mLock);
617 Mutex::Autolock _sl(effectThread->mLock);
618 moveEffectChain_l(lSessionId, effectThread, thread, true);
619 }
Eric Laurenta011e352012-03-29 15:51:43 -0700620
621 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700622 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700623 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
624 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700625 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700626 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700627 } else {
628 mPendingSyncEvents[i]->cancel();
629 }
Eric Laurenta011e352012-03-29 15:51:43 -0700630 mPendingSyncEvents.removeAt(i);
631 i--;
632 }
633 }
634 }
Glenn Kastene198c362013-08-13 09:13:36 -0700635
Mathias Agopian65ab4712010-07-14 17:59:35 -0700636 }
Glenn Kastene198c362013-08-13 09:13:36 -0700637
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700638 if (lStatus != NO_ERROR) {
639 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700640 // Client destructor is called by the TrackBase destructor with mClientLock held
641 Mutex::Autolock _cl(mClientLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700642 client.clear();
643 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700644 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700645 }
646
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700647 // return handle to client
648 trackHandle = new TrackHandle(track);
649
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700651 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700652 return trackHandle;
653}
654
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800655uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700656{
657 Mutex::Autolock _l(mLock);
658 PlaybackThread *thread = checkPlaybackThread_l(output);
659 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000660 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700661 return 0;
662 }
663 return thread->sampleRate();
664}
665
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800666int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700667{
668 Mutex::Autolock _l(mLock);
669 PlaybackThread *thread = checkPlaybackThread_l(output);
670 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000671 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672 return 0;
673 }
674 return thread->channelCount();
675}
676
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800677audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678{
679 Mutex::Autolock _l(mLock);
680 PlaybackThread *thread = checkPlaybackThread_l(output);
681 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000682 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800683 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700684 }
685 return thread->format();
686}
687
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800688size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700689{
690 Mutex::Autolock _l(mLock);
691 PlaybackThread *thread = checkPlaybackThread_l(output);
692 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000693 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694 return 0;
695 }
Glenn Kasten58912562012-04-03 10:45:00 -0700696 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
697 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700698 return thread->frameCount();
699}
700
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800701uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700702{
703 Mutex::Autolock _l(mLock);
704 PlaybackThread *thread = checkPlaybackThread_l(output);
705 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800706 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700707 return 0;
708 }
709 return thread->latency();
710}
711
712status_t AudioFlinger::setMasterVolume(float value)
713{
Eric Laurenta1884f92011-08-23 08:25:03 -0700714 status_t ret = initCheck();
715 if (ret != NO_ERROR) {
716 return ret;
717 }
718
Mathias Agopian65ab4712010-07-14 17:59:35 -0700719 // check calling permissions
720 if (!settingsAllowed()) {
721 return PERMISSION_DENIED;
722 }
723
Eric Laurenta4c5a552012-03-29 10:12:40 -0700724 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700725 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700726
John Grossmanee578c02012-07-23 17:05:46 -0700727 // Set master volume in the HALs which support it.
728 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
729 AutoMutex lock(mHardwareLock);
730 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800731
John Grossmanee578c02012-07-23 17:05:46 -0700732 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
733 if (dev->canSetMasterVolume()) {
734 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800735 }
John Grossmanee578c02012-07-23 17:05:46 -0700736 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700737 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700738
John Grossmanee578c02012-07-23 17:05:46 -0700739 // Now set the master volume in each playback thread. Playback threads
740 // assigned to HALs which do not have master volume support will apply
741 // master volume during the mix operation. Threads with HALs which do
742 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800743 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700744 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700745
746 return NO_ERROR;
747}
748
Glenn Kastenf78aee72012-01-04 11:00:47 -0800749status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700750{
Eric Laurenta1884f92011-08-23 08:25:03 -0700751 status_t ret = initCheck();
752 if (ret != NO_ERROR) {
753 return ret;
754 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700755
756 // check calling permissions
757 if (!settingsAllowed()) {
758 return PERMISSION_DENIED;
759 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800760 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000761 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700762 return BAD_VALUE;
763 }
764
765 { // scope for the lock
766 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700767 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700768 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700769 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700770 mHardwareStatus = AUDIO_HW_IDLE;
771 }
772
773 if (NO_ERROR == ret) {
774 Mutex::Autolock _l(mLock);
775 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800776 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700777 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700778 }
779
780 return ret;
781}
782
783status_t AudioFlinger::setMicMute(bool state)
784{
Eric Laurenta1884f92011-08-23 08:25:03 -0700785 status_t ret = initCheck();
786 if (ret != NO_ERROR) {
787 return ret;
788 }
789
Mathias Agopian65ab4712010-07-14 17:59:35 -0700790 // check calling permissions
791 if (!settingsAllowed()) {
792 return PERMISSION_DENIED;
793 }
794
795 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700796 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700797 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700798 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700799 mHardwareStatus = AUDIO_HW_IDLE;
800 return ret;
801}
802
803bool AudioFlinger::getMicMute() const
804{
Eric Laurenta1884f92011-08-23 08:25:03 -0700805 status_t ret = initCheck();
806 if (ret != NO_ERROR) {
807 return false;
808 }
809
Dima Zavinfce7a472011-04-19 22:30:36 -0700810 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800811 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700812 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700813 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700814 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700815 mHardwareStatus = AUDIO_HW_IDLE;
816 return state;
817}
818
819status_t AudioFlinger::setMasterMute(bool muted)
820{
John Grossmand8f178d2012-07-20 14:51:35 -0700821 status_t ret = initCheck();
822 if (ret != NO_ERROR) {
823 return ret;
824 }
825
Mathias Agopian65ab4712010-07-14 17:59:35 -0700826 // check calling permissions
827 if (!settingsAllowed()) {
828 return PERMISSION_DENIED;
829 }
830
John Grossmanee578c02012-07-23 17:05:46 -0700831 Mutex::Autolock _l(mLock);
832 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700833
John Grossmanee578c02012-07-23 17:05:46 -0700834 // Set master mute in the HALs which support it.
835 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
836 AutoMutex lock(mHardwareLock);
837 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700838
John Grossmanee578c02012-07-23 17:05:46 -0700839 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
840 if (dev->canSetMasterMute()) {
841 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700842 }
John Grossmanee578c02012-07-23 17:05:46 -0700843 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700844 }
845
John Grossmanee578c02012-07-23 17:05:46 -0700846 // Now set the master mute in each playback thread. Playback threads
847 // assigned to HALs which do not have master mute support will apply master
848 // mute during the mix operation. Threads with HALs which do support master
849 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800850 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700851 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700852
853 return NO_ERROR;
854}
855
856float AudioFlinger::masterVolume() const
857{
Glenn Kasten98067102011-12-13 11:47:54 -0800858 Mutex::Autolock _l(mLock);
859 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700860}
861
862bool AudioFlinger::masterMute() const
863{
Glenn Kasten98067102011-12-13 11:47:54 -0800864 Mutex::Autolock _l(mLock);
865 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700866}
867
John Grossman4ff14ba2012-02-08 16:37:41 -0800868float AudioFlinger::masterVolume_l() const
869{
John Grossman4ff14ba2012-02-08 16:37:41 -0800870 return mMasterVolume;
871}
872
John Grossmand8f178d2012-07-20 14:51:35 -0700873bool AudioFlinger::masterMute_l() const
874{
John Grossmanee578c02012-07-23 17:05:46 -0700875 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700876}
877
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800878status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
879 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700880{
881 // check calling permissions
882 if (!settingsAllowed()) {
883 return PERMISSION_DENIED;
884 }
885
Glenn Kasten263709e2012-01-06 08:40:01 -0800886 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000887 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700888 return BAD_VALUE;
889 }
890
891 AutoMutex lock(mLock);
892 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700893 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700894 thread = checkPlaybackThread_l(output);
895 if (thread == NULL) {
896 return BAD_VALUE;
897 }
898 }
899
900 mStreamTypes[stream].volume = value;
901
902 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800903 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700904 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700905 }
906 } else {
907 thread->setStreamVolume(stream, value);
908 }
909
910 return NO_ERROR;
911}
912
Glenn Kastenfff6d712012-01-12 16:38:12 -0800913status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700914{
915 // check calling permissions
916 if (!settingsAllowed()) {
917 return PERMISSION_DENIED;
918 }
919
Glenn Kasten263709e2012-01-06 08:40:01 -0800920 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700921 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000922 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700923 return BAD_VALUE;
924 }
925
Eric Laurent93575202011-01-18 18:39:02 -0800926 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700927 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700928 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700929 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700930
931 return NO_ERROR;
932}
933
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800934float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700935{
Glenn Kasten263709e2012-01-06 08:40:01 -0800936 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700937 return 0.0f;
938 }
939
940 AutoMutex lock(mLock);
941 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -0700942 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700943 PlaybackThread *thread = checkPlaybackThread_l(output);
944 if (thread == NULL) {
945 return 0.0f;
946 }
947 volume = thread->streamVolume(stream);
948 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800949 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700950 }
951
952 return volume;
953}
954
Glenn Kastenfff6d712012-01-12 16:38:12 -0800955bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700956{
Glenn Kasten263709e2012-01-06 08:40:01 -0800957 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700958 return true;
959 }
960
Glenn Kasten6637baa2012-01-09 09:40:36 -0800961 AutoMutex lock(mLock);
962 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700963}
964
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800965status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700966{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700967 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
968 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800969
Mathias Agopian65ab4712010-07-14 17:59:35 -0700970 // check calling permissions
971 if (!settingsAllowed()) {
972 return PERMISSION_DENIED;
973 }
974
Glenn Kasten142f5192014-03-25 17:44:59 -0700975 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
976 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700977 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700978 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800979 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700980 AutoMutex lock(mHardwareLock);
981 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
982 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
983 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
984 status_t result = dev->set_parameters(dev, keyValuePairs.string());
985 final_result = result ?: final_result;
986 }
987 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800988 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700989 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
990 AudioParameter param = AudioParameter(keyValuePairs);
991 String8 value;
992 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700993 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
994 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700995 for (size_t i = 0; i < mRecordThreads.size(); i++) {
996 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700997 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700998 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
999 // collect all of the thread's session IDs
1000 KeyedVector<int, bool> ids = thread->sessionIds();
1001 // suspend effects associated with those session IDs
1002 for (size_t j = 0; j < ids.size(); ++j) {
1003 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001004 thread->setEffectSuspended(FX_IID_AEC,
1005 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001006 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001007 thread->setEffectSuspended(FX_IID_NS,
1008 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001009 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001010 }
1011 }
Eric Laurentbee53372011-08-29 12:42:48 -07001012 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001013 }
1014 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001015 String8 screenState;
1016 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1017 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001018 if (isOff != (AudioFlinger::mScreenState & 1)) {
1019 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001020 }
1021 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001022 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001023 }
1024
1025 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1026 // and the thread is exited once the lock is released
1027 sp<ThreadBase> thread;
1028 {
1029 Mutex::Autolock _l(mLock);
1030 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001031 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001032 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001033 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001034 // indicate output device change to all input threads for pre processing
1035 AudioParameter param = AudioParameter(keyValuePairs);
1036 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001037 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1038 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001039 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1040 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1041 }
1042 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001043 }
1044 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001045 if (thread != 0) {
1046 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001047 }
1048 return BAD_VALUE;
1049}
1050
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001051String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001052{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001053 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1054 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001055
Eric Laurenta4c5a552012-03-29 10:12:40 -07001056 Mutex::Autolock _l(mLock);
1057
Glenn Kasten142f5192014-03-25 17:44:59 -07001058 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001059 String8 out_s8;
1060
Dima Zavin799a70e2011-04-18 16:57:27 -07001061 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001062 char *s;
1063 {
1064 AutoMutex lock(mHardwareLock);
1065 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001066 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001067 s = dev->get_parameters(dev, keys.string());
1068 mHardwareStatus = AUDIO_HW_IDLE;
1069 }
John Grossmanef7740b2012-02-09 11:28:36 -08001070 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001071 free(s);
1072 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001073 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001074 }
1075
Mathias Agopian65ab4712010-07-14 17:59:35 -07001076 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1077 if (playbackThread != NULL) {
1078 return playbackThread->getParameters(keys);
1079 }
1080 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1081 if (recordThread != NULL) {
1082 return recordThread->getParameters(keys);
1083 }
1084 return String8("");
1085}
1086
Glenn Kastendd8104c2012-07-02 12:42:44 -07001087size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1088 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001089{
Eric Laurenta1884f92011-08-23 08:25:03 -07001090 status_t ret = initCheck();
1091 if (ret != NO_ERROR) {
1092 return 0;
1093 }
1094
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001095 AutoMutex lock(mHardwareLock);
1096 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001097 struct audio_config config;
1098 memset(&config, 0, sizeof(config));
1099 config.sample_rate = sampleRate;
1100 config.channel_mask = channelMask;
1101 config.format = format;
1102
John Grossmanee578c02012-07-23 17:05:46 -07001103 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1104 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001105 mHardwareStatus = AUDIO_HW_IDLE;
1106 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001107}
1108
Glenn Kasten5f972c02014-01-13 09:59:31 -08001109uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001110{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001111 Mutex::Autolock _l(mLock);
1112
1113 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1114 if (recordThread != NULL) {
1115 return recordThread->getInputFramesLost();
1116 }
1117 return 0;
1118}
1119
1120status_t AudioFlinger::setVoiceVolume(float value)
1121{
Eric Laurenta1884f92011-08-23 08:25:03 -07001122 status_t ret = initCheck();
1123 if (ret != NO_ERROR) {
1124 return ret;
1125 }
1126
Mathias Agopian65ab4712010-07-14 17:59:35 -07001127 // check calling permissions
1128 if (!settingsAllowed()) {
1129 return PERMISSION_DENIED;
1130 }
1131
1132 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001133 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001134 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001135 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001136 mHardwareStatus = AUDIO_HW_IDLE;
1137
1138 return ret;
1139}
1140
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001141status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001142 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001143{
1144 status_t status;
1145
1146 Mutex::Autolock _l(mLock);
1147
1148 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1149 if (playbackThread != NULL) {
1150 return playbackThread->getRenderPosition(halFrames, dspFrames);
1151 }
1152
1153 return BAD_VALUE;
1154}
1155
1156void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1157{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001158 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001159 bool clientAdded = false;
1160 {
1161 Mutex::Autolock _cl(mClientLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001162
Eric Laurent021cf962014-05-13 10:18:14 -07001163 pid_t pid = IPCThreadState::self()->getCallingPid();
1164 if (mNotificationClients.indexOfKey(pid) < 0) {
1165 sp<NotificationClient> notificationClient = new NotificationClient(this,
1166 client,
1167 pid);
1168 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001169
Eric Laurent021cf962014-05-13 10:18:14 -07001170 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001171
Eric Laurent021cf962014-05-13 10:18:14 -07001172 sp<IBinder> binder = client->asBinder();
1173 binder->linkToDeath(notificationClient);
1174 clientAdded = true;
1175 }
1176 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001177
Eric Laurent021cf962014-05-13 10:18:14 -07001178 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
1179 // ThreadBase mutex and teh locknig order is ThreadBase::mLock then AudioFlinger::mClientLock.
1180 if (clientAdded) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001181 // the config change is always sent from playback or record threads to avoid deadlock
1182 // with AudioSystem::gLock
1183 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001184 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001185 }
1186
1187 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001188 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001189 }
1190 }
1191}
1192
1193void AudioFlinger::removeNotificationClient(pid_t pid)
1194{
1195 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001196 {
1197 Mutex::Autolock _cl(mClientLock);
1198 mNotificationClients.removeItem(pid);
1199 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001200
Steve Block3856b092011-10-20 11:56:00 +01001201 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001202 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001203 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001204 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001205 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001206 ALOGV(" pid %d @ %d", ref->mPid, i);
1207 if (ref->mPid == pid) {
1208 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001209 mAudioSessionRefs.removeAt(i);
1210 delete ref;
1211 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001212 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001213 } else {
1214 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001215 }
1216 }
1217 if (removed) {
1218 purgeStaleEffects_l();
1219 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001220}
1221
Eric Laurent021cf962014-05-13 10:18:14 -07001222void AudioFlinger::audioConfigChanged(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001223{
Eric Laurent021cf962014-05-13 10:18:14 -07001224 Mutex::Autolock _l(mClientLock);
1225 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001226 for (size_t i = 0; i < size; i++) {
Eric Laurent021cf962014-05-13 10:18:14 -07001227 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event,
Eric Laurent10351942014-05-08 18:49:52 -07001228 ioHandle,
1229 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001230 }
1231}
1232
Eric Laurent021cf962014-05-13 10:18:14 -07001233// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001234void AudioFlinger::removeClient_l(pid_t pid)
1235{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001236 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001237 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001238 mClients.removeItem(pid);
1239}
1240
Eric Laurent717e1282012-06-29 16:36:52 -07001241// getEffectThread_l() must be called with AudioFlinger::mLock held
1242sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1243{
1244 sp<PlaybackThread> thread;
1245
1246 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1247 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1248 ALOG_ASSERT(thread == 0);
1249 thread = mPlaybackThreads.valueAt(i);
1250 }
1251 }
1252
1253 return thread;
1254}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001255
Mathias Agopian65ab4712010-07-14 17:59:35 -07001256
Mathias Agopian65ab4712010-07-14 17:59:35 -07001257
1258// ----------------------------------------------------------------------------
1259
1260AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1261 : RefBase(),
1262 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001263 // 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 -07001264 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001265 mPid(pid),
1266 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001267{
1268 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1269}
1270
Eric Laurent021cf962014-05-13 10:18:14 -07001271// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001272AudioFlinger::Client::~Client()
1273{
1274 mAudioFlinger->removeClient_l(mPid);
1275}
1276
Glenn Kasten435dbe62012-01-30 10:15:48 -08001277sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001278{
1279 return mMemoryDealer;
1280}
1281
John Grossman4ff14ba2012-02-08 16:37:41 -08001282// Reserve one of the limited slots for a timed audio track associated
1283// with this client
1284bool AudioFlinger::Client::reserveTimedTrack()
1285{
1286 const int kMaxTimedTracksPerClient = 4;
1287
1288 Mutex::Autolock _l(mTimedTrackLock);
1289
1290 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1291 ALOGW("can not create timed track - pid %d has exceeded the limit",
1292 mPid);
1293 return false;
1294 }
1295
1296 mTimedTrackCount++;
1297 return true;
1298}
1299
1300// Release a slot for a timed audio track
1301void AudioFlinger::Client::releaseTimedTrack()
1302{
1303 Mutex::Autolock _l(mTimedTrackLock);
1304 mTimedTrackCount--;
1305}
1306
Mathias Agopian65ab4712010-07-14 17:59:35 -07001307// ----------------------------------------------------------------------------
1308
1309AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1310 const sp<IAudioFlingerClient>& client,
1311 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001312 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001313{
1314}
1315
1316AudioFlinger::NotificationClient::~NotificationClient()
1317{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001318}
1319
Glenn Kasten0f11b512014-01-31 16:18:54 -08001320void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001321{
1322 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001323 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001324}
1325
Mathias Agopian65ab4712010-07-14 17:59:35 -07001326
1327// ----------------------------------------------------------------------------
1328
Jeff Brown893a5642013-08-16 20:19:26 -07001329static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1330 return audio_is_remote_submix_device(inDevice);
1331}
1332
Mathias Agopian65ab4712010-07-14 17:59:35 -07001333sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001334 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001335 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001336 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001337 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001338 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001339 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001340 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001341 int *sessionId,
Glenn Kastend776ac62014-05-07 09:16:09 -07001342 sp<IMemory>& cblk,
1343 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001344 status_t *status)
1345{
1346 sp<RecordThread::RecordTrack> recordTrack;
1347 sp<RecordHandle> recordHandle;
1348 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001349 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001350 int lSessionId;
1351
Glenn Kastend776ac62014-05-07 09:16:09 -07001352 cblk.clear();
1353 buffers.clear();
1354
Mathias Agopian65ab4712010-07-14 17:59:35 -07001355 // check calling permissions
1356 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001357 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001358 lStatus = PERMISSION_DENIED;
1359 goto Exit;
1360 }
1361
Glenn Kasten53b5d092014-02-05 10:00:23 -08001362 // further sample rate checks are performed by createRecordTrack_l()
1363 if (sampleRate == 0) {
1364 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1365 lStatus = BAD_VALUE;
1366 goto Exit;
1367 }
1368
Glenn Kasten0e0e8462014-03-13 15:02:40 -07001369 // we don't yet support anything other than 16-bit PCM
1370 if (!(audio_is_valid_format(format) &&
1371 audio_is_linear_pcm(format) && format == AUDIO_FORMAT_PCM_16_BIT)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001372 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001373 lStatus = BAD_VALUE;
1374 goto Exit;
1375 }
1376
Glenn Kasten53b5d092014-02-05 10:00:23 -08001377 // further channel mask checks are performed by createRecordTrack_l()
1378 if (!audio_is_input_channel(channelMask)) {
1379 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1380 lStatus = BAD_VALUE;
1381 goto Exit;
1382 }
1383
Glenn Kasten05997e22014-03-13 15:08:33 -07001384 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001385 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001386 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001387 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001388 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001389 lStatus = BAD_VALUE;
1390 goto Exit;
1391 }
1392
Jeff Brown893a5642013-08-16 20:19:26 -07001393 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1394 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001395 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001396 lStatus = PERMISSION_DENIED;
1397 goto Exit;
1398 }
1399
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001400 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001401 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001402
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001403 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001404 lSessionId = *sessionId;
1405 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001406 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001407 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001408 if (sessionId != NULL) {
1409 *sessionId = lSessionId;
1410 }
1411 }
Glenn Kasten570f6332014-03-13 15:01:06 -07001412 ALOGV("openRecord() lSessionId: %d", lSessionId);
1413
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001414 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001415 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001416 frameCount, lSessionId,
1417 IPCThreadState::self()->getCallingUid(),
1418 flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001419 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001420 }
Glenn Kastene198c362013-08-13 09:13:36 -07001421
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001422 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001423 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001424 // Client destructor is called by the TrackBase destructor with mClientLock held
1425 Mutex::Autolock _cl(mClientLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001426 client.clear();
1427 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001428 goto Exit;
1429 }
1430
Glenn Kastend776ac62014-05-07 09:16:09 -07001431 cblk = recordTrack->getCblk();
1432 buffers = recordTrack->getBuffers();
1433
Glenn Kasten2fc14732013-08-05 14:58:14 -07001434 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001435 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001436
1437Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001438 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001439 return recordHandle;
1440}
1441
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001442
1443
Mathias Agopian65ab4712010-07-14 17:59:35 -07001444// ----------------------------------------------------------------------------
1445
Eric Laurenta4c5a552012-03-29 10:12:40 -07001446audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1447{
1448 if (!settingsAllowed()) {
1449 return 0;
1450 }
1451 Mutex::Autolock _l(mLock);
1452 return loadHwModule_l(name);
1453}
1454
1455// loadHwModule_l() must be called with AudioFlinger::mLock held
1456audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1457{
1458 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1459 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1460 ALOGW("loadHwModule() module %s already loaded", name);
1461 return mAudioHwDevs.keyAt(i);
1462 }
1463 }
1464
Eric Laurenta4c5a552012-03-29 10:12:40 -07001465 audio_hw_device_t *dev;
1466
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001467 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001468 if (rc) {
1469 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1470 return 0;
1471 }
1472
1473 mHardwareStatus = AUDIO_HW_INIT;
1474 rc = dev->init_check(dev);
1475 mHardwareStatus = AUDIO_HW_IDLE;
1476 if (rc) {
1477 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1478 return 0;
1479 }
1480
John Grossmanee578c02012-07-23 17:05:46 -07001481 // Check and cache this HAL's level of support for master mute and master
1482 // volume. If this is the first HAL opened, and it supports the get
1483 // methods, use the initial values provided by the HAL as the current
1484 // master mute and volume settings.
1485
1486 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1487 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001488 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001489
1490 if (0 == mAudioHwDevs.size()) {
1491 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1492 if (NULL != dev->get_master_volume) {
1493 float mv;
1494 if (OK == dev->get_master_volume(dev, &mv)) {
1495 mMasterVolume = mv;
1496 }
1497 }
1498
1499 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1500 if (NULL != dev->get_master_mute) {
1501 bool mm;
1502 if (OK == dev->get_master_mute(dev, &mm)) {
1503 mMasterMute = mm;
1504 }
1505 }
1506 }
1507
Eric Laurenta4c5a552012-03-29 10:12:40 -07001508 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001509 if ((NULL != dev->set_master_volume) &&
1510 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1511 flags = static_cast<AudioHwDevice::Flags>(flags |
1512 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1513 }
1514
1515 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1516 if ((NULL != dev->set_master_mute) &&
1517 (OK == dev->set_master_mute(dev, mMasterMute))) {
1518 flags = static_cast<AudioHwDevice::Flags>(flags |
1519 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1520 }
1521
Eric Laurenta4c5a552012-03-29 10:12:40 -07001522 mHardwareStatus = AUDIO_HW_IDLE;
1523 }
1524
1525 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001526 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001527
1528 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001529 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001530
1531 return handle;
1532
1533}
1534
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001535// ----------------------------------------------------------------------------
1536
Glenn Kasten3b16c762012-11-14 08:44:39 -08001537uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001538{
1539 Mutex::Autolock _l(mLock);
1540 PlaybackThread *thread = primaryPlaybackThread_l();
1541 return thread != NULL ? thread->sampleRate() : 0;
1542}
1543
Glenn Kastene33054e2012-11-14 12:54:39 -08001544size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001545{
1546 Mutex::Autolock _l(mLock);
1547 PlaybackThread *thread = primaryPlaybackThread_l();
1548 return thread != NULL ? thread->frameCountHAL() : 0;
1549}
1550
1551// ----------------------------------------------------------------------------
1552
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001553status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1554{
1555 uid_t uid = IPCThreadState::self()->getCallingUid();
1556 if (uid != AID_SYSTEM) {
1557 return PERMISSION_DENIED;
1558 }
1559 Mutex::Autolock _l(mLock);
1560 if (mIsDeviceTypeKnown) {
1561 return INVALID_OPERATION;
1562 }
1563 mIsLowRamDevice = isLowRamDevice;
1564 mIsDeviceTypeKnown = true;
1565 return NO_ERROR;
1566}
1567
1568// ----------------------------------------------------------------------------
1569
Eric Laurenta4c5a552012-03-29 10:12:40 -07001570audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1571 audio_devices_t *pDevices,
1572 uint32_t *pSamplingRate,
1573 audio_format_t *pFormat,
1574 audio_channel_mask_t *pChannelMask,
1575 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001576 audio_output_flags_t flags,
1577 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001578{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001579 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001580 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001581 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1582 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1583 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
Glenn Kasten937098b2013-06-26 11:19:36 -07001584 if (offloadInfo != NULL) {
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001585 config.offload_info = *offloadInfo;
1586 }
1587
Eric Laurentbfb1b832013-01-07 09:53:42 -08001588 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001589 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001590 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001591 config.sample_rate,
1592 config.format,
1593 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001594 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001595 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
Glenn Kastene198c362013-08-13 09:13:36 -07001596 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001597
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001598 if (pDevices == NULL || *pDevices == AUDIO_DEVICE_NONE) {
Glenn Kasten142f5192014-03-25 17:44:59 -07001599 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001600 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001601
Mathias Agopian65ab4712010-07-14 17:59:35 -07001602 Mutex::Autolock _l(mLock);
1603
Glenn Kasten32550952013-08-06 10:45:10 -07001604 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001605 if (outHwDev == NULL) {
Glenn Kasten142f5192014-03-25 17:44:59 -07001606 return AUDIO_IO_HANDLE_NONE;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001607 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001608
John Grossmanee578c02012-07-23 17:05:46 -07001609 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001610 audio_io_handle_t id = nextUniqueId();
1611
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001612 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001613
Glenn Kasten32550952013-08-06 10:45:10 -07001614 audio_stream_out_t *outStream = NULL;
Glenn Kasten34542ac2013-06-26 11:29:02 -07001615 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001616 id,
1617 *pDevices,
1618 (audio_output_flags_t)flags,
1619 &config,
1620 &outStream);
1621
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001622 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001623 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001624 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001625 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001626 config.sample_rate,
1627 config.format,
1628 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001629 status);
1630
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001631 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001632 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001633
Glenn Kasten32550952013-08-06 10:45:10 -07001634 PlaybackThread *thread;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001635 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1636 thread = new OffloadThread(this, output, id, *pDevices);
1637 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1638 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001639 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1640 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001641 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001642 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001643 } else {
1644 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001645 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001646 }
1647 mPlaybackThreads.add(id, thread);
1648
Glenn Kasten7c027242012-12-26 14:43:16 -08001649 if (pSamplingRate != NULL) {
1650 *pSamplingRate = config.sample_rate;
1651 }
1652 if (pFormat != NULL) {
1653 *pFormat = config.format;
1654 }
1655 if (pChannelMask != NULL) {
1656 *pChannelMask = config.channel_mask;
1657 }
1658 if (pLatencyMs != NULL) {
1659 *pLatencyMs = thread->latency();
1660 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001661
1662 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001663 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001664
1665 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001666 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001667 ALOGI("Using module %d has the primary audio interface", module);
1668 mPrimaryHardwareDev = outHwDev;
1669
1670 AutoMutex lock(mHardwareLock);
1671 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001672 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001673 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001674 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001675 return id;
1676 }
1677
Glenn Kasten142f5192014-03-25 17:44:59 -07001678 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001679}
1680
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001681audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1682 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001683{
1684 Mutex::Autolock _l(mLock);
1685 MixerThread *thread1 = checkMixerThread_l(output1);
1686 MixerThread *thread2 = checkMixerThread_l(output2);
1687
1688 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001689 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1690 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001691 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001692 }
1693
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001694 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001695 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1696 thread->addOutputTrack(thread2);
1697 mPlaybackThreads.add(id, thread);
1698 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001699 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001700 return id;
1701}
1702
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001703status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001704{
Glenn Kastend96c5722012-04-25 13:44:49 -07001705 return closeOutput_nonvirtual(output);
1706}
1707
1708status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1709{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001710 // keep strong reference on the playback thread so that
1711 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001712 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001713 {
1714 Mutex::Autolock _l(mLock);
1715 thread = checkPlaybackThread_l(output);
1716 if (thread == NULL) {
1717 return BAD_VALUE;
1718 }
1719
Steve Block3856b092011-10-20 11:56:00 +01001720 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001721
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001722 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001723 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001724 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001725 DuplicatingThread *dupThread =
1726 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001727 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001728
1729 }
1730 }
1731 }
1732
1733
1734 mPlaybackThreads.removeItem(output);
1735 // save all effects to the default thread
1736 if (mPlaybackThreads.size()) {
1737 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1738 if (dstThread != NULL) {
1739 // audioflinger lock is held here so the acquisition order of thread locks does not
1740 // matter
1741 Mutex::Autolock _dl(dstThread->mLock);
1742 Mutex::Autolock _sl(thread->mLock);
1743 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1744 for (size_t i = 0; i < effectChains.size(); i ++) {
1745 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001746 }
1747 }
1748 }
Eric Laurent021cf962014-05-13 10:18:14 -07001749 audioConfigChanged(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001750 }
1751 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001752 // The thread entity (active unit of execution) is no longer running here,
1753 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001754
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001755 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001756 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001757 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001758 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001759 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001760 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001761 }
1762 return NO_ERROR;
1763}
1764
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001765status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001766{
1767 Mutex::Autolock _l(mLock);
1768 PlaybackThread *thread = checkPlaybackThread_l(output);
1769
1770 if (thread == NULL) {
1771 return BAD_VALUE;
1772 }
1773
Steve Block3856b092011-10-20 11:56:00 +01001774 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001775 thread->suspend();
1776
1777 return NO_ERROR;
1778}
1779
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001780status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001781{
1782 Mutex::Autolock _l(mLock);
1783 PlaybackThread *thread = checkPlaybackThread_l(output);
1784
1785 if (thread == NULL) {
1786 return BAD_VALUE;
1787 }
1788
Steve Block3856b092011-10-20 11:56:00 +01001789 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001790
1791 thread->restore();
1792
1793 return NO_ERROR;
1794}
1795
Eric Laurenta4c5a552012-03-29 10:12:40 -07001796audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1797 audio_devices_t *pDevices,
1798 uint32_t *pSamplingRate,
1799 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001800 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001801{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001802 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001803 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001804 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1805 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1806 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1807
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001808 uint32_t reqSamplingRate = config.sample_rate;
1809 audio_format_t reqFormat = config.format;
Glenn Kastenf506e942013-08-06 10:49:43 -07001810 audio_channel_mask_t reqChannelMask = config.channel_mask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001811
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001812 if (pDevices == NULL || *pDevices == AUDIO_DEVICE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001813 return 0;
1814 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001815
Mathias Agopian65ab4712010-07-14 17:59:35 -07001816 Mutex::Autolock _l(mLock);
1817
Glenn Kasten32550952013-08-06 10:45:10 -07001818 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001819 if (inHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001820 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001821 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001822
John Grossmanee578c02012-07-23 17:05:46 -07001823 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001824 audio_io_handle_t id = nextUniqueId();
1825
Glenn Kasten32550952013-08-06 10:45:10 -07001826 audio_stream_in_t *inStream = NULL;
1827 status_t status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001828 &inStream);
Glenn Kastencac3daa2014-02-07 09:47:14 -08001829 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %#x, Channels %x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001830 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001831 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001832 config.sample_rate,
1833 config.format,
1834 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001835 status);
1836
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001837 // If the input could not be opened with the requested parameters and we can handle the
1838 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1839 // 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 -07001840 if (status == BAD_VALUE &&
1841 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1842 (config.sample_rate <= 2 * reqSamplingRate) &&
Andy Hunge5412692014-05-16 11:25:07 -07001843 (audio_channel_count_from_in_mask(config.channel_mask) <= FCC_2) &&
1844 (audio_channel_count_from_in_mask(reqChannelMask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07001845 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Glenn Kasten254af182012-07-03 14:59:05 -07001846 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001847 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001848 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07001849 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07001850 }
1851
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001852 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001853
Glenn Kasten46909e72013-02-26 09:20:22 -08001854#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001855 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1856 // or (re-)create if current Pipe is idle and does not match the new format
1857 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001858 enum {
1859 TEE_SINK_NO, // don't copy input
1860 TEE_SINK_NEW, // copy input using a new pipe
1861 TEE_SINK_OLD, // copy input using an existing pipe
1862 } kind;
1863 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
Andy Hunge5412692014-05-16 11:25:07 -07001864 audio_channel_count_from_in_mask(
1865 inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001866 if (!mTeeSinkInputEnabled) {
1867 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08001868 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001869 kind = TEE_SINK_NO;
1870 } else if (mRecordTeeSink == 0) {
1871 kind = TEE_SINK_NEW;
1872 } else if (mRecordTeeSink->getStrongCount() != 1) {
1873 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08001874 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001875 kind = TEE_SINK_OLD;
1876 } else {
1877 kind = TEE_SINK_NEW;
1878 }
1879 switch (kind) {
1880 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001881 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001882 size_t numCounterOffers = 0;
1883 const NBAIO_Format offers[1] = {format};
1884 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1885 ALOG_ASSERT(index == 0);
1886 PipeReader *pipeReader = new PipeReader(*pipe);
1887 numCounterOffers = 0;
1888 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1889 ALOG_ASSERT(index == 0);
1890 mRecordTeeSink = pipe;
1891 mRecordTeeSource = pipeReader;
1892 teeSink = pipe;
1893 }
1894 break;
1895 case TEE_SINK_OLD:
1896 teeSink = mRecordTeeSink;
1897 break;
1898 case TEE_SINK_NO:
1899 default:
1900 break;
1901 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001902#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001903
Dima Zavin799a70e2011-04-18 16:57:27 -07001904 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1905
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001906 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001907 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001908 // pre processing modules
Glenn Kasten32550952013-08-06 10:45:10 -07001909 RecordThread *thread = new RecordThread(this,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001910 input,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001911 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001912 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001913 *pDevices
1914#ifdef TEE_SINK
1915 , teeSink
1916#endif
1917 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001918 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001919 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001920 if (pSamplingRate != NULL) {
1921 *pSamplingRate = reqSamplingRate;
1922 }
1923 if (pFormat != NULL) {
1924 *pFormat = config.format;
1925 }
1926 if (pChannelMask != NULL) {
Glenn Kastenf506e942013-08-06 10:49:43 -07001927 *pChannelMask = reqChannelMask;
Glenn Kasten7c027242012-12-26 14:43:16 -08001928 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001929
Mathias Agopian65ab4712010-07-14 17:59:35 -07001930 // notify client processes of the new input creation
Eric Laurent021cf962014-05-13 10:18:14 -07001931 thread->audioConfigChanged(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001932 return id;
1933 }
1934
1935 return 0;
1936}
1937
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001938status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001939{
Glenn Kastend96c5722012-04-25 13:44:49 -07001940 return closeInput_nonvirtual(input);
1941}
1942
1943status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1944{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001945 // keep strong reference on the record thread so that
1946 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001947 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001948 {
1949 Mutex::Autolock _l(mLock);
1950 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001951 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001952 return BAD_VALUE;
1953 }
1954
Steve Block3856b092011-10-20 11:56:00 +01001955 ALOGV("closeInput() %d", input);
Eric Laurent021cf962014-05-13 10:18:14 -07001956 audioConfigChanged(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001957 mRecordThreads.removeItem(input);
1958 }
1959 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001960 // The thread entity (active unit of execution) is no longer running here,
1961 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001962
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001963 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001964 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001965 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001966 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001967 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001968
1969 return NO_ERROR;
1970}
1971
Glenn Kastend2304db2014-02-03 07:40:31 -08001972status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001973{
1974 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08001975 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001976
1977 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1978 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001979 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001980 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001981
1982 return NO_ERROR;
1983}
1984
1985
1986int AudioFlinger::newAudioSessionId()
1987{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001988 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001989}
1990
Marco Nelissend457c972014-02-11 08:47:07 -08001991void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001992{
1993 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001994 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08001995 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
1996 if (pid != -1 && (caller == getpid_cached)) {
1997 caller = pid;
1998 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07001999
Eric Laurent021cf962014-05-13 10:18:14 -07002000 {
2001 Mutex::Autolock _cl(mClientLock);
2002 // Ignore requests received from processes not known as notification client. The request
2003 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2004 // called from a different pid leaving a stale session reference. Also we don't know how
2005 // to clear this reference if the client process dies.
2006 if (mNotificationClients.indexOfKey(caller) < 0) {
2007 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2008 return;
2009 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002010 }
2011
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002012 size_t num = mAudioSessionRefs.size();
2013 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002014 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002015 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2016 ref->mCnt++;
2017 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002018 return;
2019 }
2020 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002021 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2022 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002023}
2024
Marco Nelissend457c972014-02-11 08:47:07 -08002025void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002026{
2027 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002028 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002029 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2030 if (pid != -1 && (caller == getpid_cached)) {
2031 caller = pid;
2032 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002033 size_t num = mAudioSessionRefs.size();
2034 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002035 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002036 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2037 ref->mCnt--;
2038 ALOGV(" decremented refcount to %d", ref->mCnt);
2039 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002040 mAudioSessionRefs.removeAt(i);
2041 delete ref;
2042 purgeStaleEffects_l();
2043 }
2044 return;
2045 }
2046 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002047 // If the caller is mediaserver it is likely that the session being released was acquired
2048 // on behalf of a process not in notification clients and we ignore the warning.
2049 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002050}
2051
2052void AudioFlinger::purgeStaleEffects_l() {
2053
Steve Block3856b092011-10-20 11:56:00 +01002054 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002055
2056 Vector< sp<EffectChain> > chains;
2057
2058 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2059 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2060 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2061 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002062 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2063 chains.push(ec);
2064 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002065 }
2066 }
2067 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2068 sp<RecordThread> t = mRecordThreads.valueAt(i);
2069 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2070 sp<EffectChain> ec = t->mEffectChains[j];
2071 chains.push(ec);
2072 }
2073 }
2074
2075 for (size_t i = 0; i < chains.size(); i++) {
2076 sp<EffectChain> ec = chains[i];
2077 int sessionid = ec->sessionId();
2078 sp<ThreadBase> t = ec->mThread.promote();
2079 if (t == 0) {
2080 continue;
2081 }
2082 size_t numsessionrefs = mAudioSessionRefs.size();
2083 bool found = false;
2084 for (size_t k = 0; k < numsessionrefs; k++) {
2085 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002086 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002087 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002088 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002089 found = true;
2090 break;
2091 }
2092 }
2093 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002094 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002095 // remove all effects from the chain
2096 while (ec->mEffects.size()) {
2097 sp<EffectModule> effect = ec->mEffects[0];
2098 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002099 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002100 if (effect->purgeHandles()) {
2101 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002102 }
2103 AudioSystem::unregisterEffect(effect->id());
2104 }
2105 }
2106 }
2107 return;
2108}
2109
Mathias Agopian65ab4712010-07-14 17:59:35 -07002110// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002111AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002112{
Glenn Kastena1117922012-01-26 10:53:32 -08002113 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002114}
2115
2116// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002117AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002118{
2119 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002120 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002121}
2122
2123// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002124AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002125{
Glenn Kastena1117922012-01-26 10:53:32 -08002126 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002127}
2128
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002129uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002130{
Glenn Kastenbcefec32014-01-17 12:09:05 -08002131 return (uint32_t) android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002132}
2133
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002134AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002135{
2136 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2137 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002138 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002139 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002140 return thread;
2141 }
2142 }
2143 return NULL;
2144}
2145
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002146audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002147{
2148 PlaybackThread *thread = primaryPlaybackThread_l();
2149
2150 if (thread == NULL) {
2151 return 0;
2152 }
2153
Eric Laurentf1c04f92012-08-28 14:26:53 -07002154 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002155}
2156
Eric Laurenta011e352012-03-29 15:51:43 -07002157sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2158 int triggerSession,
2159 int listenerSession,
2160 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002161 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002162{
2163 Mutex::Autolock _l(mLock);
2164
2165 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2166 status_t playStatus = NAME_NOT_FOUND;
2167 status_t recStatus = NAME_NOT_FOUND;
2168 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2169 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2170 if (playStatus == NO_ERROR) {
2171 return event;
2172 }
2173 }
2174 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2175 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2176 if (recStatus == NO_ERROR) {
2177 return event;
2178 }
2179 }
2180 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2181 mPendingSyncEvents.add(event);
2182 } else {
2183 ALOGV("createSyncEvent() invalid event %d", event->type());
2184 event.clear();
2185 }
2186 return event;
2187}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002188
Mathias Agopian65ab4712010-07-14 17:59:35 -07002189// ----------------------------------------------------------------------------
2190// Effect management
2191// ----------------------------------------------------------------------------
2192
2193
Glenn Kastenf587ba52012-01-26 16:25:10 -08002194status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002195{
2196 Mutex::Autolock _l(mLock);
2197 return EffectQueryNumberEffects(numEffects);
2198}
2199
Glenn Kastenf587ba52012-01-26 16:25:10 -08002200status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002201{
2202 Mutex::Autolock _l(mLock);
2203 return EffectQueryEffect(index, descriptor);
2204}
2205
Glenn Kasten5e92a782012-01-30 07:40:52 -08002206status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002207 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002208{
2209 Mutex::Autolock _l(mLock);
2210 return EffectGetDescriptor(pUuid, descriptor);
2211}
2212
2213
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002214sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002215 effect_descriptor_t *pDesc,
2216 const sp<IEffectClient>& effectClient,
2217 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002218 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002219 int sessionId,
2220 status_t *status,
2221 int *id,
2222 int *enabled)
2223{
2224 status_t lStatus = NO_ERROR;
2225 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002226 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002227
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002228 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002229 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002230 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002231
2232 if (pDesc == NULL) {
2233 lStatus = BAD_VALUE;
2234 goto Exit;
2235 }
2236
Eric Laurent84e9a102010-09-23 16:10:16 -07002237 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002238 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002239 lStatus = PERMISSION_DENIED;
2240 goto Exit;
2241 }
2242
Dima Zavinfce7a472011-04-19 22:30:36 -07002243 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002244 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002245 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002246 lStatus = PERMISSION_DENIED;
2247 goto Exit;
2248 }
2249
Mathias Agopian65ab4712010-07-14 17:59:35 -07002250 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002251 if (!EffectIsNullUuid(&pDesc->uuid)) {
2252 // if uuid is specified, request effect descriptor
2253 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2254 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002255 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002256 goto Exit;
2257 }
2258 } else {
2259 // if uuid is not specified, look for an available implementation
2260 // of the required type in effect factory
2261 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002262 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002263 lStatus = BAD_VALUE;
2264 goto Exit;
2265 }
2266 uint32_t numEffects = 0;
2267 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002268 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002269 bool found = false;
2270
2271 lStatus = EffectQueryNumberEffects(&numEffects);
2272 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002273 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002274 goto Exit;
2275 }
2276 for (uint32_t i = 0; i < numEffects; i++) {
2277 lStatus = EffectQueryEffect(i, &desc);
2278 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002279 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002280 continue;
2281 }
2282 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2283 // If matching type found save effect descriptor. If the session is
2284 // 0 and the effect is not auxiliary, continue enumeration in case
2285 // an auxiliary version of this effect type is available
2286 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002287 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002288 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002289 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2290 break;
2291 }
2292 }
2293 }
2294 if (!found) {
2295 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002296 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002297 goto Exit;
2298 }
2299 // For same effect type, chose auxiliary version over insert version if
2300 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002301 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002302 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002303 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002304 }
2305 }
2306
2307 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002308 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002309 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2310 lStatus = INVALID_OPERATION;
2311 goto Exit;
2312 }
2313
Eric Laurent59255e42011-07-27 19:49:51 -07002314 // check recording permission for visualizer
2315 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2316 !recordingAllowed()) {
2317 lStatus = PERMISSION_DENIED;
2318 goto Exit;
2319 }
2320
Mathias Agopian65ab4712010-07-14 17:59:35 -07002321 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002322 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002323 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002324 // if the output returned by getOutputForEffect() is removed before we lock the
2325 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2326 // and we will exit safely
2327 io = AudioSystem::getOutputForEffect(&desc);
2328 ALOGV("createEffect got output %d", io);
2329 }
2330
2331 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002332
2333 // If output is not specified try to find a matching audio session ID in one of the
2334 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002335 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2336 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002337 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002338 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002339 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2340 // output must be specified by AudioPolicyManager when using session
2341 // AUDIO_SESSION_OUTPUT_STAGE
2342 lStatus = BAD_VALUE;
2343 goto Exit;
2344 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002345 // look for the thread where the specified audio session is present
2346 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2347 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2348 io = mPlaybackThreads.keyAt(i);
2349 break;
2350 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002351 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002352 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002353 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2354 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2355 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002356 break;
2357 }
2358 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002359 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002360 // If no output thread contains the requested session ID, default to
2361 // first output. The effect chain will be moved to the correct output
2362 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002363 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002364 io = mPlaybackThreads.keyAt(0);
2365 }
Steve Block3856b092011-10-20 11:56:00 +01002366 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002367 }
2368 ThreadBase *thread = checkRecordThread_l(io);
2369 if (thread == NULL) {
2370 thread = checkPlaybackThread_l(io);
2371 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002372 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002373 lStatus = BAD_VALUE;
2374 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002375 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002376 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002377
Eric Laurent021cf962014-05-13 10:18:14 -07002378 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002379
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002380 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002381 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2382 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002383 if (handle != 0 && id != NULL) {
2384 *id = handle->id();
2385 }
2386 }
2387
2388Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002389 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002390 return handle;
2391}
2392
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002393status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2394 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002395{
Steve Block3856b092011-10-20 11:56:00 +01002396 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002397 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002398 Mutex::Autolock _l(mLock);
2399 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002400 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002401 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002402 }
Eric Laurentde070132010-07-13 04:45:46 -07002403 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2404 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002405 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002406 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002407 }
Eric Laurentde070132010-07-13 04:45:46 -07002408 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2409 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002410 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002411 return BAD_VALUE;
2412 }
2413
2414 Mutex::Autolock _dl(dstThread->mLock);
2415 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002416 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002417}
2418
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002419// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002420status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002421 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002422 AudioFlinger::PlaybackThread *dstThread,
2423 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002424{
Steve Block3856b092011-10-20 11:56:00 +01002425 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002426 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002427
Eric Laurent59255e42011-07-27 19:49:51 -07002428 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002429 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002430 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002431 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002432 return INVALID_OPERATION;
2433 }
2434
Eric Laurent39e94f82010-07-28 01:32:47 -07002435 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002436 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002437 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002438 // removed.
2439 srcThread->removeEffectChain_l(chain);
2440
2441 // transfer all effects one by one so that new effect chain is created on new thread with
2442 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002443 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002444 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002445 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002446 Vector< sp<EffectModule> > removed;
2447 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002448 while (effect != 0) {
2449 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002450 removed.add(effect);
2451 status = dstThread->addEffect_l(effect);
2452 if (status != NO_ERROR) {
2453 break;
2454 }
Eric Laurentec35a142011-10-05 17:42:25 -07002455 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2456 if (effect->state() == EffectModule::ACTIVE ||
2457 effect->state() == EffectModule::STOPPING) {
2458 effect->start();
2459 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002460 // if the move request is not received from audio policy manager, the effect must be
2461 // re-registered with the new strategy and output
2462 if (dstChain == 0) {
2463 dstChain = effect->chain().promote();
2464 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002465 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002466 status = NO_INIT;
2467 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002468 }
2469 strategy = dstChain->strategy();
2470 }
2471 if (reRegister) {
2472 AudioSystem::unregisterEffect(effect->id());
2473 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002474 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002475 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002476 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002477 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002478 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002479 }
Eric Laurentde070132010-07-13 04:45:46 -07002480 effect = chain->getEffectFromId_l(0);
2481 }
2482
Eric Laurent5baf2af2013-09-12 17:37:00 -07002483 if (status != NO_ERROR) {
2484 for (size_t i = 0; i < removed.size(); i++) {
2485 srcThread->addEffect_l(removed[i]);
2486 if (dstChain != 0 && reRegister) {
2487 AudioSystem::unregisterEffect(removed[i]->id());
2488 AudioSystem::registerEffect(&removed[i]->desc(),
2489 srcThread->id(),
2490 strategy,
2491 sessionId,
2492 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002493 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002494 }
2495 }
2496 }
2497
2498 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002499}
2500
Eric Laurent5baf2af2013-09-12 17:37:00 -07002501bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002502{
2503 if (mGlobalEffectEnableTime != 0 &&
2504 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2505 return true;
2506 }
2507
2508 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2509 sp<EffectChain> ec =
2510 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002511 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002512 return true;
2513 }
2514 }
2515 return false;
2516}
2517
Eric Laurent5baf2af2013-09-12 17:37:00 -07002518void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002519{
2520 Mutex::Autolock _l(mLock);
2521
2522 mGlobalEffectEnableTime = systemTime();
2523
2524 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2525 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2526 if (t->mType == ThreadBase::OFFLOAD) {
2527 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2528 }
2529 }
2530
2531}
2532
Glenn Kastenda6ef132013-01-10 12:31:01 -08002533struct Entry {
2534#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2535 char mName[MAX_NAME];
2536};
2537
2538int comparEntry(const void *p1, const void *p2)
2539{
2540 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2541}
2542
Glenn Kasten46909e72013-02-26 09:20:22 -08002543#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002544void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002545{
Eric Laurent81784c32012-11-19 14:55:58 -08002546 NBAIO_Source *teeSource = source.get();
2547 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002548 // .wav rotation
2549 // There is a benign race condition if 2 threads call this simultaneously.
2550 // They would both traverse the directory, but the result would simply be
2551 // failures at unlink() which are ignored. It's also unlikely since
2552 // normally dumpsys is only done by bugreport or from the command line.
2553 char teePath[32+256];
2554 strcpy(teePath, "/data/misc/media");
2555 size_t teePathLen = strlen(teePath);
2556 DIR *dir = opendir(teePath);
2557 teePath[teePathLen++] = '/';
2558 if (dir != NULL) {
2559#define MAX_SORT 20 // number of entries to sort
2560#define MAX_KEEP 10 // number of entries to keep
2561 struct Entry entries[MAX_SORT];
2562 size_t entryCount = 0;
2563 while (entryCount < MAX_SORT) {
2564 struct dirent de;
2565 struct dirent *result = NULL;
2566 int rc = readdir_r(dir, &de, &result);
2567 if (rc != 0) {
2568 ALOGW("readdir_r failed %d", rc);
2569 break;
2570 }
2571 if (result == NULL) {
2572 break;
2573 }
2574 if (result != &de) {
2575 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2576 break;
2577 }
2578 // ignore non .wav file entries
2579 size_t nameLen = strlen(de.d_name);
2580 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2581 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2582 continue;
2583 }
2584 strcpy(entries[entryCount++].mName, de.d_name);
2585 }
2586 (void) closedir(dir);
2587 if (entryCount > MAX_KEEP) {
2588 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2589 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2590 strcpy(&teePath[teePathLen], entries[i].mName);
2591 (void) unlink(teePath);
2592 }
2593 }
2594 } else {
2595 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002596 dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002597 }
2598 }
Eric Laurent81784c32012-11-19 14:55:58 -08002599 char teeTime[16];
2600 struct timeval tv;
2601 gettimeofday(&tv, NULL);
2602 struct tm tm;
2603 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002604 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2605 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2606 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2607 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002608 if (teeFd >= 0) {
2609 char wavHeader[44];
2610 memcpy(wavHeader,
2611 "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",
2612 sizeof(wavHeader));
2613 NBAIO_Format format = teeSource->format();
2614 unsigned channelCount = Format_channelCount(format);
2615 ALOG_ASSERT(channelCount <= FCC_2);
2616 uint32_t sampleRate = Format_sampleRate(format);
2617 wavHeader[22] = channelCount; // number of channels
2618 wavHeader[24] = sampleRate; // sample rate
2619 wavHeader[25] = sampleRate >> 8;
2620 wavHeader[32] = channelCount * 2; // block alignment
2621 write(teeFd, wavHeader, sizeof(wavHeader));
2622 size_t total = 0;
2623 bool firstRead = true;
2624 for (;;) {
2625#define TEE_SINK_READ 1024
2626 short buffer[TEE_SINK_READ * FCC_2];
2627 size_t count = TEE_SINK_READ;
2628 ssize_t actual = teeSource->read(buffer, count,
2629 AudioBufferProvider::kInvalidPTS);
2630 bool wasFirstRead = firstRead;
2631 firstRead = false;
2632 if (actual <= 0) {
2633 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2634 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002635 }
Eric Laurent81784c32012-11-19 14:55:58 -08002636 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002637 }
Eric Laurent81784c32012-11-19 14:55:58 -08002638 ALOG_ASSERT(actual <= (ssize_t)count);
2639 write(teeFd, buffer, actual * channelCount * sizeof(short));
2640 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002641 }
Eric Laurent81784c32012-11-19 14:55:58 -08002642 lseek(teeFd, (off_t) 4, SEEK_SET);
2643 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2644 write(teeFd, &temp, sizeof(temp));
2645 lseek(teeFd, (off_t) 40, SEEK_SET);
2646 temp = total * channelCount * sizeof(short);
2647 write(teeFd, &temp, sizeof(temp));
2648 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002649 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002650 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002651 }
Eric Laurent59255e42011-07-27 19:49:51 -07002652 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002653 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002654 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002655 }
Eric Laurent59255e42011-07-27 19:49:51 -07002656 }
2657 }
2658}
Glenn Kasten46909e72013-02-26 09:20:22 -08002659#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002660
Mathias Agopian65ab4712010-07-14 17:59:35 -07002661// ----------------------------------------------------------------------------
2662
2663status_t AudioFlinger::onTransact(
2664 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2665{
2666 return BnAudioFlinger::onTransact(code, data, reply, flags);
2667}
2668
Mathias Agopian65ab4712010-07-14 17:59:35 -07002669}; // namespace android