blob: 9bd0e9b59fbb5fa353ad758fd27866388e2ee172 [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 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800180#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800181 (void) property_get("ro.debuggable", value, "0");
182 int debuggable = atoi(value);
183 int teeEnabled = 0;
184 if (debuggable) {
185 (void) property_get("af.tee", value, "0");
186 teeEnabled = atoi(value);
187 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800188 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700189 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800190 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700191 }
192 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800193 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700194 }
195 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800196 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700197 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800198#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700199}
200
201void AudioFlinger::onFirstRef()
202{
Dima Zavin799a70e2011-04-18 16:57:27 -0700203 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700204
Eric Laurent93575202011-01-18 18:39:02 -0800205 Mutex::Autolock _l(mLock);
206
Dima Zavin799a70e2011-04-18 16:57:27 -0700207 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800208 char val_str[PROPERTY_VALUE_MAX] = { 0 };
209 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
210 uint32_t int_val;
211 if (1 == sscanf(val_str, "%u", &int_val)) {
212 mStandbyTimeInNsecs = milliseconds(int_val);
213 ALOGI("Using %u mSec as standby time.", int_val);
214 } else {
215 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
216 ALOGI("Using default %u mSec as standby time.",
217 (uint32_t)(mStandbyTimeInNsecs / 1000000));
218 }
219 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700220
Eric Laurenta4c5a552012-03-29 10:12:40 -0700221 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700222}
223
224AudioFlinger::~AudioFlinger()
225{
226 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700227 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700228 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700229 }
230 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700231 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700232 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700233 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700234
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800235 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
236 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700237 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
238 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700239 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700240
241 // Tell media.log service about any old writers that still need to be unregistered
242 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
243 if (binder != 0) {
244 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
245 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
246 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
247 mUnregisteredWriters.pop();
248 mediaLogService->unregisterWriter(iMemory);
249 }
250 }
251
Mathias Agopian65ab4712010-07-14 17:59:35 -0700252}
253
Eric Laurenta4c5a552012-03-29 10:12:40 -0700254static const char * const audio_interfaces[] = {
255 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
256 AUDIO_HARDWARE_MODULE_ID_A2DP,
257 AUDIO_HARDWARE_MODULE_ID_USB,
258};
259#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
260
John Grossmanee578c02012-07-23 17:05:46 -0700261AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
262 audio_module_handle_t module,
263 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700264{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700265 // if module is 0, the request comes from an old policy manager and we should load
266 // well known modules
267 if (module == 0) {
268 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
269 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
270 loadHwModule_l(audio_interfaces[i]);
271 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700272 // then try to find a module supporting the requested device.
273 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
274 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
275 audio_hw_device_t *dev = audioHwDevice->hwDevice();
276 if ((dev->get_supported_devices != NULL) &&
277 (dev->get_supported_devices(dev) & devices) == devices)
278 return audioHwDevice;
279 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700280 } else {
281 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700282 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
283 if (audioHwDevice != NULL) {
284 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700285 }
286 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700287
Dima Zavin799a70e2011-04-18 16:57:27 -0700288 return NULL;
289}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700290
Glenn Kasten0f11b512014-01-31 16:18:54 -0800291void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700292{
293 const size_t SIZE = 256;
294 char buffer[SIZE];
295 String8 result;
296
297 result.append("Clients:\n");
298 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800299 sp<Client> client = mClients.valueAt(i).promote();
300 if (client != 0) {
301 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
302 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700303 }
304 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700305
Eric Laurentd1b28d42013-09-18 18:47:13 -0700306 result.append("Notification Clients:\n");
307 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
308 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
309 result.append(buffer);
310 }
311
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700312 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800313 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700314 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
315 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800316 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700317 result.append(buffer);
318 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700319 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320}
321
322
Glenn Kasten0f11b512014-01-31 16:18:54 -0800323void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324{
325 const size_t SIZE = 256;
326 char buffer[SIZE];
327 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800328 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700329
John Grossman4ff14ba2012-02-08 16:37:41 -0800330 snprintf(buffer, SIZE, "Hardware status: %d\n"
331 "Standby Time mSec: %u\n",
332 hardwareStatus,
333 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700334 result.append(buffer);
335 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336}
337
Glenn Kasten0f11b512014-01-31 16:18:54 -0800338void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700339{
340 const size_t SIZE = 256;
341 char buffer[SIZE];
342 String8 result;
343 snprintf(buffer, SIZE, "Permission Denial: "
344 "can't dump AudioFlinger from pid=%d, uid=%d\n",
345 IPCThreadState::self()->getCallingPid(),
346 IPCThreadState::self()->getCallingUid());
347 result.append(buffer);
348 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349}
350
Eric Laurent81784c32012-11-19 14:55:58 -0800351bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700352{
353 bool locked = false;
354 for (int i = 0; i < kDumpLockRetries; ++i) {
355 if (mutex.tryLock() == NO_ERROR) {
356 locked = true;
357 break;
358 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800359 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360 }
361 return locked;
362}
363
364status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
365{
Glenn Kasten44deb052012-02-05 18:09:08 -0800366 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700367 dumpPermissionDenial(fd, args);
368 } else {
369 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800370 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700371 if (!hardwareLocked) {
372 String8 result(kHardwareLockedString);
373 write(fd, result.string(), result.size());
374 } else {
375 mHardwareLock.unlock();
376 }
377
Eric Laurent81784c32012-11-19 14:55:58 -0800378 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700379
380 // failed to lock - AudioFlinger is probably deadlocked
381 if (!locked) {
382 String8 result(kDeadlockedString);
383 write(fd, result.string(), result.size());
384 }
385
Eric Laurent021cf962014-05-13 10:18:14 -0700386 bool clientLocked = dumpTryLock(mClientLock);
387 if (!clientLocked) {
388 String8 result(kClientLockedString);
389 write(fd, result.string(), result.size());
390 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700391 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700392 if (clientLocked) {
393 mClientLock.unlock();
394 }
395
Mathias Agopian65ab4712010-07-14 17:59:35 -0700396 dumpInternals(fd, args);
397
398 // dump playback threads
399 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
400 mPlaybackThreads.valueAt(i)->dump(fd, args);
401 }
402
403 // dump record threads
404 for (size_t i = 0; i < mRecordThreads.size(); i++) {
405 mRecordThreads.valueAt(i)->dump(fd, args);
406 }
407
Dima Zavin799a70e2011-04-18 16:57:27 -0700408 // dump all hardware devs
409 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700410 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700411 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700412 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700413
Glenn Kasten46909e72013-02-26 09:20:22 -0800414#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700415 // dump the serially shared record tee sink
416 if (mRecordTeeSource != 0) {
417 dumpTee(fd, mRecordTeeSource);
418 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800419#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700420
Glenn Kastend65d73c2012-06-22 17:21:07 -0700421 if (locked) {
422 mLock.unlock();
423 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800424
425 // append a copy of media.log here by forwarding fd to it, but don't attempt
426 // to lookup the service if it's not running, as it will block for a second
427 if (mLogMemoryDealer != 0) {
428 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
429 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700430 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800431 Vector<String16> args;
432 binder->dump(fd, args);
433 }
434 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 }
436 return NO_ERROR;
437}
438
Eric Laurent021cf962014-05-13 10:18:14 -0700439sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800440{
Eric Laurent021cf962014-05-13 10:18:14 -0700441 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800442 // If pid is already in the mClients wp<> map, then use that entry
443 // (for which promote() is always != 0), otherwise create a new entry and Client.
444 sp<Client> client = mClients.valueFor(pid).promote();
445 if (client == 0) {
446 client = new Client(this, pid);
447 mClients.add(pid, client);
448 }
449
450 return client;
451}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700452
Glenn Kasten9e58b552013-01-18 15:09:48 -0800453sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
454{
Glenn Kasten481fb672013-09-30 14:39:28 -0700455 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800456 if (mLogMemoryDealer == 0) {
457 return new NBLog::Writer();
458 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800459 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700460 // Similarly if we can't contact the media.log service, also return a dummy writer
461 if (binder == 0) {
462 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800463 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700464 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
465 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
466 // If allocation fails, consult the vector of previously unregistered writers
467 // and garbage-collect one or more them until an allocation succeeds
468 if (shared == 0) {
469 Mutex::Autolock _l(mUnregisteredWritersLock);
470 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
471 {
472 // Pick the oldest stale writer to garbage-collect
473 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
474 mUnregisteredWriters.removeAt(0);
475 mediaLogService->unregisterWriter(iMemory);
476 // Now the media.log remote reference to IMemory is gone. When our last local
477 // reference to IMemory also drops to zero at end of this block,
478 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
479 }
480 // Re-attempt the allocation
481 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
482 if (shared != 0) {
483 goto success;
484 }
485 }
486 // Even after garbage-collecting all old writers, there is still not enough memory,
487 // so return a dummy writer
488 return new NBLog::Writer();
489 }
490success:
491 mediaLogService->registerWriter(shared, size, name);
492 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800493}
494
495void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
496{
Glenn Kasten685ef092013-02-04 08:15:34 -0800497 if (writer == 0) {
498 return;
499 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800500 sp<IMemory> iMemory(writer->getIMemory());
501 if (iMemory == 0) {
502 return;
503 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700504 // Rather than removing the writer immediately, append it to a queue of old writers to
505 // be garbage-collected later. This allows us to continue to view old logs for a while.
506 Mutex::Autolock _l(mUnregisteredWritersLock);
507 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800508}
509
Mathias Agopian65ab4712010-07-14 17:59:35 -0700510// IAudioFlinger interface
511
512
513sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800514 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700515 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800516 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700517 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800518 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800519 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700520 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800521 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800522 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700523 int *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800524 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700525 status_t *status)
526{
527 sp<PlaybackThread::Track> track;
528 sp<TrackHandle> trackHandle;
529 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700530 status_t lStatus;
531 int lSessionId;
532
Glenn Kasten263709e2012-01-06 08:40:01 -0800533 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
534 // but if someone uses binder directly they could bypass that and cause us to crash
535 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000536 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700537 lStatus = BAD_VALUE;
538 goto Exit;
539 }
540
Glenn Kasten53b5d092014-02-05 10:00:23 -0800541 // further sample rate checks are performed by createTrack_l() depending on the thread type
542 if (sampleRate == 0) {
543 ALOGE("createTrack() invalid sample rate %u", sampleRate);
544 lStatus = BAD_VALUE;
545 goto Exit;
546 }
547
548 // further channel mask checks are performed by createTrack_l() depending on the thread type
549 if (!audio_is_output_channel(channelMask)) {
550 ALOGE("createTrack() invalid channel mask %#x", channelMask);
551 lStatus = BAD_VALUE;
552 goto Exit;
553 }
554
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700555 // further format checks are performed by createTrack_l() depending on the thread type
556 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800557 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700558 lStatus = BAD_VALUE;
559 goto Exit;
560 }
561
Glenn Kasten663c2242013-09-24 11:52:37 -0700562 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
563 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
564 lStatus = BAD_VALUE;
565 goto Exit;
566 }
567
Mathias Agopian65ab4712010-07-14 17:59:35 -0700568 {
569 Mutex::Autolock _l(mLock);
570 PlaybackThread *thread = checkPlaybackThread_l(output);
571 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800572 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700573 lStatus = BAD_VALUE;
574 goto Exit;
575 }
576
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800577 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -0700578 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700579
Glenn Kastene848bd92014-03-13 15:00:32 -0700580 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700581 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700582 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700583 // check if an effect chain with the same session ID is present on another
584 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700585 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700586 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
587 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700588 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700589 if (sessions & PlaybackThread::EFFECT_SESSION) {
590 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700591 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700592 }
Eric Laurentde070132010-07-13 04:45:46 -0700593 }
594 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700596 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700597 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700598 if (sessionId != NULL) {
599 *sessionId = lSessionId;
600 }
601 }
Steve Block3856b092011-10-20 11:56:00 +0100602 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700603
604 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800605 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800606 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700607 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700608
609 // move effect chain to this output thread if an effect on same session was waiting
610 // for a track to be created
611 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700612 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700613 Mutex::Autolock _dl(thread->mLock);
614 Mutex::Autolock _sl(effectThread->mLock);
615 moveEffectChain_l(lSessionId, effectThread, thread, true);
616 }
Eric Laurenta011e352012-03-29 15:51:43 -0700617
618 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700619 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700620 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
621 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700622 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700623 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700624 } else {
625 mPendingSyncEvents[i]->cancel();
626 }
Eric Laurenta011e352012-03-29 15:51:43 -0700627 mPendingSyncEvents.removeAt(i);
628 i--;
629 }
630 }
631 }
Glenn Kastene198c362013-08-13 09:13:36 -0700632
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633 }
Glenn Kastene198c362013-08-13 09:13:36 -0700634
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700635 if (lStatus != NO_ERROR) {
636 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700637 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700638 // Don't hold mClientLock when releasing the reference on the track as the
639 // destructor will acquire it.
640 {
641 Mutex::Autolock _cl(mClientLock);
642 client.clear();
643 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700644 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700645 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646 }
647
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700648 // return handle to client
649 trackHandle = new TrackHandle(track);
650
Mathias Agopian65ab4712010-07-14 17:59:35 -0700651Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700652 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700653 return trackHandle;
654}
655
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800656uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657{
658 Mutex::Autolock _l(mLock);
659 PlaybackThread *thread = checkPlaybackThread_l(output);
660 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000661 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700662 return 0;
663 }
664 return thread->sampleRate();
665}
666
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800667int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700668{
669 Mutex::Autolock _l(mLock);
670 PlaybackThread *thread = checkPlaybackThread_l(output);
671 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000672 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700673 return 0;
674 }
675 return thread->channelCount();
676}
677
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800678audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700679{
680 Mutex::Autolock _l(mLock);
681 PlaybackThread *thread = checkPlaybackThread_l(output);
682 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000683 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800684 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 }
686 return thread->format();
687}
688
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800689size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690{
691 Mutex::Autolock _l(mLock);
692 PlaybackThread *thread = checkPlaybackThread_l(output);
693 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000694 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700695 return 0;
696 }
Glenn Kasten58912562012-04-03 10:45:00 -0700697 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
698 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699 return thread->frameCount();
700}
701
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800702uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700703{
704 Mutex::Autolock _l(mLock);
705 PlaybackThread *thread = checkPlaybackThread_l(output);
706 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800707 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700708 return 0;
709 }
710 return thread->latency();
711}
712
713status_t AudioFlinger::setMasterVolume(float value)
714{
Eric Laurenta1884f92011-08-23 08:25:03 -0700715 status_t ret = initCheck();
716 if (ret != NO_ERROR) {
717 return ret;
718 }
719
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720 // check calling permissions
721 if (!settingsAllowed()) {
722 return PERMISSION_DENIED;
723 }
724
Eric Laurenta4c5a552012-03-29 10:12:40 -0700725 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700726 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700727
John Grossmanee578c02012-07-23 17:05:46 -0700728 // Set master volume in the HALs which support it.
729 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
730 AutoMutex lock(mHardwareLock);
731 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800732
John Grossmanee578c02012-07-23 17:05:46 -0700733 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
734 if (dev->canSetMasterVolume()) {
735 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800736 }
John Grossmanee578c02012-07-23 17:05:46 -0700737 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700738 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700739
John Grossmanee578c02012-07-23 17:05:46 -0700740 // Now set the master volume in each playback thread. Playback threads
741 // assigned to HALs which do not have master volume support will apply
742 // master volume during the mix operation. Threads with HALs which do
743 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800744 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700745 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700746
747 return NO_ERROR;
748}
749
Glenn Kastenf78aee72012-01-04 11:00:47 -0800750status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700751{
Eric Laurenta1884f92011-08-23 08:25:03 -0700752 status_t ret = initCheck();
753 if (ret != NO_ERROR) {
754 return ret;
755 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700756
757 // check calling permissions
758 if (!settingsAllowed()) {
759 return PERMISSION_DENIED;
760 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800761 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000762 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700763 return BAD_VALUE;
764 }
765
766 { // scope for the lock
767 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700768 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700769 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700770 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700771 mHardwareStatus = AUDIO_HW_IDLE;
772 }
773
774 if (NO_ERROR == ret) {
775 Mutex::Autolock _l(mLock);
776 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800777 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700778 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700779 }
780
781 return ret;
782}
783
784status_t AudioFlinger::setMicMute(bool state)
785{
Eric Laurenta1884f92011-08-23 08:25:03 -0700786 status_t ret = initCheck();
787 if (ret != NO_ERROR) {
788 return ret;
789 }
790
Mathias Agopian65ab4712010-07-14 17:59:35 -0700791 // check calling permissions
792 if (!settingsAllowed()) {
793 return PERMISSION_DENIED;
794 }
795
796 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700797 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700798 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700799 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700800 mHardwareStatus = AUDIO_HW_IDLE;
801 return ret;
802}
803
804bool AudioFlinger::getMicMute() const
805{
Eric Laurenta1884f92011-08-23 08:25:03 -0700806 status_t ret = initCheck();
807 if (ret != NO_ERROR) {
808 return false;
809 }
810
Dima Zavinfce7a472011-04-19 22:30:36 -0700811 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800812 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700813 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700815 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700816 mHardwareStatus = AUDIO_HW_IDLE;
817 return state;
818}
819
820status_t AudioFlinger::setMasterMute(bool muted)
821{
John Grossmand8f178d2012-07-20 14:51:35 -0700822 status_t ret = initCheck();
823 if (ret != NO_ERROR) {
824 return ret;
825 }
826
Mathias Agopian65ab4712010-07-14 17:59:35 -0700827 // check calling permissions
828 if (!settingsAllowed()) {
829 return PERMISSION_DENIED;
830 }
831
John Grossmanee578c02012-07-23 17:05:46 -0700832 Mutex::Autolock _l(mLock);
833 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700834
John Grossmanee578c02012-07-23 17:05:46 -0700835 // Set master mute in the HALs which support it.
836 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
837 AutoMutex lock(mHardwareLock);
838 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700839
John Grossmanee578c02012-07-23 17:05:46 -0700840 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
841 if (dev->canSetMasterMute()) {
842 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700843 }
John Grossmanee578c02012-07-23 17:05:46 -0700844 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700845 }
846
John Grossmanee578c02012-07-23 17:05:46 -0700847 // Now set the master mute in each playback thread. Playback threads
848 // assigned to HALs which do not have master mute support will apply master
849 // mute during the mix operation. Threads with HALs which do support master
850 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800851 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700852 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700853
854 return NO_ERROR;
855}
856
857float AudioFlinger::masterVolume() const
858{
Glenn Kasten98067102011-12-13 11:47:54 -0800859 Mutex::Autolock _l(mLock);
860 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700861}
862
863bool AudioFlinger::masterMute() const
864{
Glenn Kasten98067102011-12-13 11:47:54 -0800865 Mutex::Autolock _l(mLock);
866 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700867}
868
John Grossman4ff14ba2012-02-08 16:37:41 -0800869float AudioFlinger::masterVolume_l() const
870{
John Grossman4ff14ba2012-02-08 16:37:41 -0800871 return mMasterVolume;
872}
873
John Grossmand8f178d2012-07-20 14:51:35 -0700874bool AudioFlinger::masterMute_l() const
875{
John Grossmanee578c02012-07-23 17:05:46 -0700876 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700877}
878
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800879status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
880 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700881{
882 // check calling permissions
883 if (!settingsAllowed()) {
884 return PERMISSION_DENIED;
885 }
886
Glenn Kasten263709e2012-01-06 08:40:01 -0800887 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000888 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700889 return BAD_VALUE;
890 }
891
892 AutoMutex lock(mLock);
893 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700894 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700895 thread = checkPlaybackThread_l(output);
896 if (thread == NULL) {
897 return BAD_VALUE;
898 }
899 }
900
901 mStreamTypes[stream].volume = value;
902
903 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800904 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700905 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700906 }
907 } else {
908 thread->setStreamVolume(stream, value);
909 }
910
911 return NO_ERROR;
912}
913
Glenn Kastenfff6d712012-01-12 16:38:12 -0800914status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700915{
916 // check calling permissions
917 if (!settingsAllowed()) {
918 return PERMISSION_DENIED;
919 }
920
Glenn Kasten263709e2012-01-06 08:40:01 -0800921 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700922 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000923 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700924 return BAD_VALUE;
925 }
926
Eric Laurent93575202011-01-18 18:39:02 -0800927 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700929 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700930 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700931
932 return NO_ERROR;
933}
934
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800935float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700936{
Glenn Kasten263709e2012-01-06 08:40:01 -0800937 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700938 return 0.0f;
939 }
940
941 AutoMutex lock(mLock);
942 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -0700943 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944 PlaybackThread *thread = checkPlaybackThread_l(output);
945 if (thread == NULL) {
946 return 0.0f;
947 }
948 volume = thread->streamVolume(stream);
949 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800950 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700951 }
952
953 return volume;
954}
955
Glenn Kastenfff6d712012-01-12 16:38:12 -0800956bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700957{
Glenn Kasten263709e2012-01-06 08:40:01 -0800958 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700959 return true;
960 }
961
Glenn Kasten6637baa2012-01-09 09:40:36 -0800962 AutoMutex lock(mLock);
963 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700964}
965
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800966status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700967{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700968 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
969 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800970
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971 // check calling permissions
972 if (!settingsAllowed()) {
973 return PERMISSION_DENIED;
974 }
975
Glenn Kasten142f5192014-03-25 17:44:59 -0700976 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
977 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700978 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700979 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800980 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700981 AutoMutex lock(mHardwareLock);
982 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
983 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
984 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
985 status_t result = dev->set_parameters(dev, keyValuePairs.string());
986 final_result = result ?: final_result;
987 }
988 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800989 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700990 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
991 AudioParameter param = AudioParameter(keyValuePairs);
992 String8 value;
993 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700994 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
995 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700996 for (size_t i = 0; i < mRecordThreads.size(); i++) {
997 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700998 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700999 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1000 // collect all of the thread's session IDs
1001 KeyedVector<int, bool> ids = thread->sessionIds();
1002 // suspend effects associated with those session IDs
1003 for (size_t j = 0; j < ids.size(); ++j) {
1004 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001005 thread->setEffectSuspended(FX_IID_AEC,
1006 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001007 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001008 thread->setEffectSuspended(FX_IID_NS,
1009 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001010 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001011 }
1012 }
Eric Laurentbee53372011-08-29 12:42:48 -07001013 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001014 }
1015 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001016 String8 screenState;
1017 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1018 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001019 if (isOff != (AudioFlinger::mScreenState & 1)) {
1020 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001021 }
1022 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001023 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001024 }
1025
1026 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1027 // and the thread is exited once the lock is released
1028 sp<ThreadBase> thread;
1029 {
1030 Mutex::Autolock _l(mLock);
1031 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001032 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001033 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001034 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001035 // indicate output device change to all input threads for pre processing
1036 AudioParameter param = AudioParameter(keyValuePairs);
1037 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001038 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1039 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001040 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1041 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1042 }
1043 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001044 }
1045 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001046 if (thread != 0) {
1047 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001048 }
1049 return BAD_VALUE;
1050}
1051
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001052String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001053{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001054 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1055 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001056
Eric Laurenta4c5a552012-03-29 10:12:40 -07001057 Mutex::Autolock _l(mLock);
1058
Glenn Kasten142f5192014-03-25 17:44:59 -07001059 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001060 String8 out_s8;
1061
Dima Zavin799a70e2011-04-18 16:57:27 -07001062 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001063 char *s;
1064 {
1065 AutoMutex lock(mHardwareLock);
1066 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001067 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001068 s = dev->get_parameters(dev, keys.string());
1069 mHardwareStatus = AUDIO_HW_IDLE;
1070 }
John Grossmanef7740b2012-02-09 11:28:36 -08001071 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001072 free(s);
1073 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001074 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001075 }
1076
Mathias Agopian65ab4712010-07-14 17:59:35 -07001077 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1078 if (playbackThread != NULL) {
1079 return playbackThread->getParameters(keys);
1080 }
1081 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1082 if (recordThread != NULL) {
1083 return recordThread->getParameters(keys);
1084 }
1085 return String8("");
1086}
1087
Glenn Kastendd8104c2012-07-02 12:42:44 -07001088size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1089 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001090{
Eric Laurenta1884f92011-08-23 08:25:03 -07001091 status_t ret = initCheck();
1092 if (ret != NO_ERROR) {
1093 return 0;
1094 }
1095
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001096 AutoMutex lock(mHardwareLock);
1097 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001098 struct audio_config config;
1099 memset(&config, 0, sizeof(config));
1100 config.sample_rate = sampleRate;
1101 config.channel_mask = channelMask;
1102 config.format = format;
1103
John Grossmanee578c02012-07-23 17:05:46 -07001104 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1105 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001106 mHardwareStatus = AUDIO_HW_IDLE;
1107 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001108}
1109
Glenn Kasten5f972c02014-01-13 09:59:31 -08001110uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001111{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001112 Mutex::Autolock _l(mLock);
1113
1114 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1115 if (recordThread != NULL) {
1116 return recordThread->getInputFramesLost();
1117 }
1118 return 0;
1119}
1120
1121status_t AudioFlinger::setVoiceVolume(float value)
1122{
Eric Laurenta1884f92011-08-23 08:25:03 -07001123 status_t ret = initCheck();
1124 if (ret != NO_ERROR) {
1125 return ret;
1126 }
1127
Mathias Agopian65ab4712010-07-14 17:59:35 -07001128 // check calling permissions
1129 if (!settingsAllowed()) {
1130 return PERMISSION_DENIED;
1131 }
1132
1133 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001134 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001135 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001136 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001137 mHardwareStatus = AUDIO_HW_IDLE;
1138
1139 return ret;
1140}
1141
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001142status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001143 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001144{
1145 status_t status;
1146
1147 Mutex::Autolock _l(mLock);
1148
1149 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1150 if (playbackThread != NULL) {
1151 return playbackThread->getRenderPosition(halFrames, dspFrames);
1152 }
1153
1154 return BAD_VALUE;
1155}
1156
1157void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1158{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001159 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001160 bool clientAdded = false;
1161 {
1162 Mutex::Autolock _cl(mClientLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001163
Eric Laurent021cf962014-05-13 10:18:14 -07001164 pid_t pid = IPCThreadState::self()->getCallingPid();
1165 if (mNotificationClients.indexOfKey(pid) < 0) {
1166 sp<NotificationClient> notificationClient = new NotificationClient(this,
1167 client,
1168 pid);
1169 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001170
Eric Laurent021cf962014-05-13 10:18:14 -07001171 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001172
Eric Laurent021cf962014-05-13 10:18:14 -07001173 sp<IBinder> binder = client->asBinder();
1174 binder->linkToDeath(notificationClient);
1175 clientAdded = true;
1176 }
1177 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001178
Eric Laurent021cf962014-05-13 10:18:14 -07001179 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001180 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent021cf962014-05-13 10:18:14 -07001181 if (clientAdded) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001182 // the config change is always sent from playback or record threads to avoid deadlock
1183 // with AudioSystem::gLock
1184 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001185 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001186 }
1187
1188 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001189 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001190 }
1191 }
1192}
1193
1194void AudioFlinger::removeNotificationClient(pid_t pid)
1195{
1196 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001197 {
1198 Mutex::Autolock _cl(mClientLock);
1199 mNotificationClients.removeItem(pid);
1200 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001201
Steve Block3856b092011-10-20 11:56:00 +01001202 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001203 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001204 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001205 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001206 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001207 ALOGV(" pid %d @ %d", ref->mPid, i);
1208 if (ref->mPid == pid) {
1209 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001210 mAudioSessionRefs.removeAt(i);
1211 delete ref;
1212 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001213 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001214 } else {
1215 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001216 }
1217 }
1218 if (removed) {
1219 purgeStaleEffects_l();
1220 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001221}
1222
Eric Laurent021cf962014-05-13 10:18:14 -07001223void AudioFlinger::audioConfigChanged(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001224{
Eric Laurent021cf962014-05-13 10:18:14 -07001225 Mutex::Autolock _l(mClientLock);
1226 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001227 for (size_t i = 0; i < size; i++) {
Eric Laurent021cf962014-05-13 10:18:14 -07001228 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event,
Eric Laurent10351942014-05-08 18:49:52 -07001229 ioHandle,
1230 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001231 }
1232}
1233
Eric Laurent021cf962014-05-13 10:18:14 -07001234// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001235void AudioFlinger::removeClient_l(pid_t pid)
1236{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001237 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001238 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001239 mClients.removeItem(pid);
1240}
1241
Eric Laurent717e1282012-06-29 16:36:52 -07001242// getEffectThread_l() must be called with AudioFlinger::mLock held
1243sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1244{
1245 sp<PlaybackThread> thread;
1246
1247 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1248 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1249 ALOG_ASSERT(thread == 0);
1250 thread = mPlaybackThreads.valueAt(i);
1251 }
1252 }
1253
1254 return thread;
1255}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001256
Mathias Agopian65ab4712010-07-14 17:59:35 -07001257
Mathias Agopian65ab4712010-07-14 17:59:35 -07001258
1259// ----------------------------------------------------------------------------
1260
1261AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1262 : RefBase(),
1263 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001264 // 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 -07001265 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001266 mPid(pid),
1267 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001268{
1269 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1270}
1271
Eric Laurent021cf962014-05-13 10:18:14 -07001272// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001273AudioFlinger::Client::~Client()
1274{
1275 mAudioFlinger->removeClient_l(mPid);
1276}
1277
Glenn Kasten435dbe62012-01-30 10:15:48 -08001278sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001279{
1280 return mMemoryDealer;
1281}
1282
John Grossman4ff14ba2012-02-08 16:37:41 -08001283// Reserve one of the limited slots for a timed audio track associated
1284// with this client
1285bool AudioFlinger::Client::reserveTimedTrack()
1286{
1287 const int kMaxTimedTracksPerClient = 4;
1288
1289 Mutex::Autolock _l(mTimedTrackLock);
1290
1291 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1292 ALOGW("can not create timed track - pid %d has exceeded the limit",
1293 mPid);
1294 return false;
1295 }
1296
1297 mTimedTrackCount++;
1298 return true;
1299}
1300
1301// Release a slot for a timed audio track
1302void AudioFlinger::Client::releaseTimedTrack()
1303{
1304 Mutex::Autolock _l(mTimedTrackLock);
1305 mTimedTrackCount--;
1306}
1307
Mathias Agopian65ab4712010-07-14 17:59:35 -07001308// ----------------------------------------------------------------------------
1309
1310AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1311 const sp<IAudioFlingerClient>& client,
1312 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001313 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001314{
1315}
1316
1317AudioFlinger::NotificationClient::~NotificationClient()
1318{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001319}
1320
Glenn Kasten0f11b512014-01-31 16:18:54 -08001321void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001322{
1323 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001324 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001325}
1326
Mathias Agopian65ab4712010-07-14 17:59:35 -07001327
1328// ----------------------------------------------------------------------------
1329
Jeff Brown893a5642013-08-16 20:19:26 -07001330static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1331 return audio_is_remote_submix_device(inDevice);
1332}
1333
Mathias Agopian65ab4712010-07-14 17:59:35 -07001334sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001335 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001336 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001337 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001338 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001339 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001340 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001341 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001342 int *sessionId,
Glenn Kastend776ac62014-05-07 09:16:09 -07001343 sp<IMemory>& cblk,
1344 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001345 status_t *status)
1346{
1347 sp<RecordThread::RecordTrack> recordTrack;
1348 sp<RecordHandle> recordHandle;
1349 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001350 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001351 int lSessionId;
1352
Glenn Kastend776ac62014-05-07 09:16:09 -07001353 cblk.clear();
1354 buffers.clear();
1355
Mathias Agopian65ab4712010-07-14 17:59:35 -07001356 // check calling permissions
1357 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001358 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001359 lStatus = PERMISSION_DENIED;
1360 goto Exit;
1361 }
1362
Glenn Kasten53b5d092014-02-05 10:00:23 -08001363 // further sample rate checks are performed by createRecordTrack_l()
1364 if (sampleRate == 0) {
1365 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1366 lStatus = BAD_VALUE;
1367 goto Exit;
1368 }
1369
Glenn Kasten0e0e8462014-03-13 15:02:40 -07001370 // we don't yet support anything other than 16-bit PCM
1371 if (!(audio_is_valid_format(format) &&
1372 audio_is_linear_pcm(format) && format == AUDIO_FORMAT_PCM_16_BIT)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001373 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001374 lStatus = BAD_VALUE;
1375 goto Exit;
1376 }
1377
Glenn Kasten53b5d092014-02-05 10:00:23 -08001378 // further channel mask checks are performed by createRecordTrack_l()
1379 if (!audio_is_input_channel(channelMask)) {
1380 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1381 lStatus = BAD_VALUE;
1382 goto Exit;
1383 }
1384
Glenn Kasten05997e22014-03-13 15:08:33 -07001385 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001386 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001387 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001388 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001389 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001390 lStatus = BAD_VALUE;
1391 goto Exit;
1392 }
1393
Jeff Brown893a5642013-08-16 20:19:26 -07001394 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1395 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001396 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001397 lStatus = PERMISSION_DENIED;
1398 goto Exit;
1399 }
1400
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001401 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001402 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001403
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001404 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001405 lSessionId = *sessionId;
1406 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001407 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001408 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409 if (sessionId != NULL) {
1410 *sessionId = lSessionId;
1411 }
1412 }
Glenn Kasten570f6332014-03-13 15:01:06 -07001413 ALOGV("openRecord() lSessionId: %d", lSessionId);
1414
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001415 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001416 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001417 frameCount, lSessionId,
1418 IPCThreadState::self()->getCallingUid(),
1419 flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001420 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001421 }
Glenn Kastene198c362013-08-13 09:13:36 -07001422
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001423 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001424 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001425 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001426 // Don't hold mClientLock when releasing the reference on the track as the
1427 // destructor will acquire it.
1428 {
1429 Mutex::Autolock _cl(mClientLock);
1430 client.clear();
1431 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001432 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001433 goto Exit;
1434 }
1435
Glenn Kastend776ac62014-05-07 09:16:09 -07001436 cblk = recordTrack->getCblk();
1437 buffers = recordTrack->getBuffers();
1438
Glenn Kasten2fc14732013-08-05 14:58:14 -07001439 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001440 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001441
1442Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001443 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001444 return recordHandle;
1445}
1446
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001447
1448
Mathias Agopian65ab4712010-07-14 17:59:35 -07001449// ----------------------------------------------------------------------------
1450
Eric Laurenta4c5a552012-03-29 10:12:40 -07001451audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1452{
1453 if (!settingsAllowed()) {
1454 return 0;
1455 }
1456 Mutex::Autolock _l(mLock);
1457 return loadHwModule_l(name);
1458}
1459
1460// loadHwModule_l() must be called with AudioFlinger::mLock held
1461audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1462{
1463 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1464 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1465 ALOGW("loadHwModule() module %s already loaded", name);
1466 return mAudioHwDevs.keyAt(i);
1467 }
1468 }
1469
Eric Laurenta4c5a552012-03-29 10:12:40 -07001470 audio_hw_device_t *dev;
1471
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001472 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001473 if (rc) {
1474 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1475 return 0;
1476 }
1477
1478 mHardwareStatus = AUDIO_HW_INIT;
1479 rc = dev->init_check(dev);
1480 mHardwareStatus = AUDIO_HW_IDLE;
1481 if (rc) {
1482 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1483 return 0;
1484 }
1485
John Grossmanee578c02012-07-23 17:05:46 -07001486 // Check and cache this HAL's level of support for master mute and master
1487 // volume. If this is the first HAL opened, and it supports the get
1488 // methods, use the initial values provided by the HAL as the current
1489 // master mute and volume settings.
1490
1491 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1492 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001493 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001494
1495 if (0 == mAudioHwDevs.size()) {
1496 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1497 if (NULL != dev->get_master_volume) {
1498 float mv;
1499 if (OK == dev->get_master_volume(dev, &mv)) {
1500 mMasterVolume = mv;
1501 }
1502 }
1503
1504 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1505 if (NULL != dev->get_master_mute) {
1506 bool mm;
1507 if (OK == dev->get_master_mute(dev, &mm)) {
1508 mMasterMute = mm;
1509 }
1510 }
1511 }
1512
Eric Laurenta4c5a552012-03-29 10:12:40 -07001513 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001514 if ((NULL != dev->set_master_volume) &&
1515 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1516 flags = static_cast<AudioHwDevice::Flags>(flags |
1517 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1518 }
1519
1520 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1521 if ((NULL != dev->set_master_mute) &&
1522 (OK == dev->set_master_mute(dev, mMasterMute))) {
1523 flags = static_cast<AudioHwDevice::Flags>(flags |
1524 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1525 }
1526
Eric Laurenta4c5a552012-03-29 10:12:40 -07001527 mHardwareStatus = AUDIO_HW_IDLE;
1528 }
1529
1530 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001531 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001532
1533 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001534 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001535
1536 return handle;
1537
1538}
1539
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001540// ----------------------------------------------------------------------------
1541
Glenn Kasten3b16c762012-11-14 08:44:39 -08001542uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001543{
1544 Mutex::Autolock _l(mLock);
1545 PlaybackThread *thread = primaryPlaybackThread_l();
1546 return thread != NULL ? thread->sampleRate() : 0;
1547}
1548
Glenn Kastene33054e2012-11-14 12:54:39 -08001549size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001550{
1551 Mutex::Autolock _l(mLock);
1552 PlaybackThread *thread = primaryPlaybackThread_l();
1553 return thread != NULL ? thread->frameCountHAL() : 0;
1554}
1555
1556// ----------------------------------------------------------------------------
1557
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001558status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1559{
1560 uid_t uid = IPCThreadState::self()->getCallingUid();
1561 if (uid != AID_SYSTEM) {
1562 return PERMISSION_DENIED;
1563 }
1564 Mutex::Autolock _l(mLock);
1565 if (mIsDeviceTypeKnown) {
1566 return INVALID_OPERATION;
1567 }
1568 mIsLowRamDevice = isLowRamDevice;
1569 mIsDeviceTypeKnown = true;
1570 return NO_ERROR;
1571}
1572
1573// ----------------------------------------------------------------------------
1574
Eric Laurenta4c5a552012-03-29 10:12:40 -07001575audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1576 audio_devices_t *pDevices,
1577 uint32_t *pSamplingRate,
1578 audio_format_t *pFormat,
1579 audio_channel_mask_t *pChannelMask,
1580 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001581 audio_output_flags_t flags,
1582 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001583{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001584 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001585 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001586 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1587 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1588 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
Glenn Kasten937098b2013-06-26 11:19:36 -07001589 if (offloadInfo != NULL) {
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001590 config.offload_info = *offloadInfo;
1591 }
1592
Eric Laurentbfb1b832013-01-07 09:53:42 -08001593 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001594 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001595 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001596 config.sample_rate,
1597 config.format,
1598 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001599 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001600 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
Glenn Kastene198c362013-08-13 09:13:36 -07001601 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001602
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001603 if (pDevices == NULL || *pDevices == AUDIO_DEVICE_NONE) {
Glenn Kasten142f5192014-03-25 17:44:59 -07001604 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001605 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001606
Mathias Agopian65ab4712010-07-14 17:59:35 -07001607 Mutex::Autolock _l(mLock);
1608
Glenn Kasten32550952013-08-06 10:45:10 -07001609 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001610 if (outHwDev == NULL) {
Glenn Kasten142f5192014-03-25 17:44:59 -07001611 return AUDIO_IO_HANDLE_NONE;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001612 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001613
John Grossmanee578c02012-07-23 17:05:46 -07001614 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001615 audio_io_handle_t id = nextUniqueId();
1616
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001617 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001618
Glenn Kasten32550952013-08-06 10:45:10 -07001619 audio_stream_out_t *outStream = NULL;
Glenn Kasten34542ac2013-06-26 11:29:02 -07001620 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001621 id,
1622 *pDevices,
1623 (audio_output_flags_t)flags,
1624 &config,
1625 &outStream);
1626
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001627 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001628 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001629 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001630 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001631 config.sample_rate,
1632 config.format,
1633 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001634 status);
1635
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001636 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001637 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001638
Glenn Kasten32550952013-08-06 10:45:10 -07001639 PlaybackThread *thread;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001640 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1641 thread = new OffloadThread(this, output, id, *pDevices);
1642 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1643 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001644 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1645 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001646 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001647 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001648 } else {
1649 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001650 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001651 }
1652 mPlaybackThreads.add(id, thread);
1653
Glenn Kasten7c027242012-12-26 14:43:16 -08001654 if (pSamplingRate != NULL) {
1655 *pSamplingRate = config.sample_rate;
1656 }
1657 if (pFormat != NULL) {
1658 *pFormat = config.format;
1659 }
1660 if (pChannelMask != NULL) {
1661 *pChannelMask = config.channel_mask;
1662 }
1663 if (pLatencyMs != NULL) {
1664 *pLatencyMs = thread->latency();
1665 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001666
1667 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001668 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001669
1670 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001671 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001672 ALOGI("Using module %d has the primary audio interface", module);
1673 mPrimaryHardwareDev = outHwDev;
1674
1675 AutoMutex lock(mHardwareLock);
1676 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001677 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001678 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001679 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001680 return id;
1681 }
1682
Glenn Kasten142f5192014-03-25 17:44:59 -07001683 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001684}
1685
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001686audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1687 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001688{
1689 Mutex::Autolock _l(mLock);
1690 MixerThread *thread1 = checkMixerThread_l(output1);
1691 MixerThread *thread2 = checkMixerThread_l(output2);
1692
1693 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001694 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1695 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001696 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001697 }
1698
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001699 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001700 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1701 thread->addOutputTrack(thread2);
1702 mPlaybackThreads.add(id, thread);
1703 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001704 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001705 return id;
1706}
1707
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001708status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001709{
Glenn Kastend96c5722012-04-25 13:44:49 -07001710 return closeOutput_nonvirtual(output);
1711}
1712
1713status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1714{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001715 // keep strong reference on the playback thread so that
1716 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001717 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001718 {
1719 Mutex::Autolock _l(mLock);
1720 thread = checkPlaybackThread_l(output);
1721 if (thread == NULL) {
1722 return BAD_VALUE;
1723 }
1724
Steve Block3856b092011-10-20 11:56:00 +01001725 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001726
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001727 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001728 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001729 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001730 DuplicatingThread *dupThread =
1731 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001732 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001733
1734 }
1735 }
1736 }
1737
1738
1739 mPlaybackThreads.removeItem(output);
1740 // save all effects to the default thread
1741 if (mPlaybackThreads.size()) {
1742 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1743 if (dstThread != NULL) {
1744 // audioflinger lock is held here so the acquisition order of thread locks does not
1745 // matter
1746 Mutex::Autolock _dl(dstThread->mLock);
1747 Mutex::Autolock _sl(thread->mLock);
1748 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1749 for (size_t i = 0; i < effectChains.size(); i ++) {
1750 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001751 }
1752 }
1753 }
Eric Laurent021cf962014-05-13 10:18:14 -07001754 audioConfigChanged(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001755 }
1756 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001757 // The thread entity (active unit of execution) is no longer running here,
1758 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001759
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001760 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001761 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001762 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001763 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001764 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001765 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001766 }
1767 return NO_ERROR;
1768}
1769
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001770status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001771{
1772 Mutex::Autolock _l(mLock);
1773 PlaybackThread *thread = checkPlaybackThread_l(output);
1774
1775 if (thread == NULL) {
1776 return BAD_VALUE;
1777 }
1778
Steve Block3856b092011-10-20 11:56:00 +01001779 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001780 thread->suspend();
1781
1782 return NO_ERROR;
1783}
1784
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001785status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001786{
1787 Mutex::Autolock _l(mLock);
1788 PlaybackThread *thread = checkPlaybackThread_l(output);
1789
1790 if (thread == NULL) {
1791 return BAD_VALUE;
1792 }
1793
Steve Block3856b092011-10-20 11:56:00 +01001794 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001795
1796 thread->restore();
1797
1798 return NO_ERROR;
1799}
1800
Eric Laurenta4c5a552012-03-29 10:12:40 -07001801audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1802 audio_devices_t *pDevices,
1803 uint32_t *pSamplingRate,
1804 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001805 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001806{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001807 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001808 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001809 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1810 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1811 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1812
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001813 uint32_t reqSamplingRate = config.sample_rate;
1814 audio_format_t reqFormat = config.format;
Glenn Kastenf506e942013-08-06 10:49:43 -07001815 audio_channel_mask_t reqChannelMask = config.channel_mask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001816
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001817 if (pDevices == NULL || *pDevices == AUDIO_DEVICE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001818 return 0;
1819 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001820
Mathias Agopian65ab4712010-07-14 17:59:35 -07001821 Mutex::Autolock _l(mLock);
1822
Glenn Kasten32550952013-08-06 10:45:10 -07001823 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001824 if (inHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001825 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001826 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001827
John Grossmanee578c02012-07-23 17:05:46 -07001828 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001829 audio_io_handle_t id = nextUniqueId();
1830
Glenn Kasten32550952013-08-06 10:45:10 -07001831 audio_stream_in_t *inStream = NULL;
1832 status_t status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001833 &inStream);
Glenn Kastencac3daa2014-02-07 09:47:14 -08001834 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %#x, Channels %x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001835 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001836 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001837 config.sample_rate,
1838 config.format,
1839 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001840 status);
1841
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001842 // If the input could not be opened with the requested parameters and we can handle the
1843 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1844 // 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 -07001845 if (status == BAD_VALUE &&
1846 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1847 (config.sample_rate <= 2 * reqSamplingRate) &&
Andy Hunge5412692014-05-16 11:25:07 -07001848 (audio_channel_count_from_in_mask(config.channel_mask) <= FCC_2) &&
1849 (audio_channel_count_from_in_mask(reqChannelMask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07001850 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Glenn Kasten254af182012-07-03 14:59:05 -07001851 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001852 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001853 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07001854 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07001855 }
1856
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001857 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001858
Glenn Kasten46909e72013-02-26 09:20:22 -08001859#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001860 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1861 // or (re-)create if current Pipe is idle and does not match the new format
1862 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001863 enum {
1864 TEE_SINK_NO, // don't copy input
1865 TEE_SINK_NEW, // copy input using a new pipe
1866 TEE_SINK_OLD, // copy input using an existing pipe
1867 } kind;
1868 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
Andy Hunge5412692014-05-16 11:25:07 -07001869 audio_channel_count_from_in_mask(
1870 inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001871 if (!mTeeSinkInputEnabled) {
1872 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08001873 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001874 kind = TEE_SINK_NO;
1875 } else if (mRecordTeeSink == 0) {
1876 kind = TEE_SINK_NEW;
1877 } else if (mRecordTeeSink->getStrongCount() != 1) {
1878 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08001879 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001880 kind = TEE_SINK_OLD;
1881 } else {
1882 kind = TEE_SINK_NEW;
1883 }
1884 switch (kind) {
1885 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001886 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001887 size_t numCounterOffers = 0;
1888 const NBAIO_Format offers[1] = {format};
1889 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1890 ALOG_ASSERT(index == 0);
1891 PipeReader *pipeReader = new PipeReader(*pipe);
1892 numCounterOffers = 0;
1893 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1894 ALOG_ASSERT(index == 0);
1895 mRecordTeeSink = pipe;
1896 mRecordTeeSource = pipeReader;
1897 teeSink = pipe;
1898 }
1899 break;
1900 case TEE_SINK_OLD:
1901 teeSink = mRecordTeeSink;
1902 break;
1903 case TEE_SINK_NO:
1904 default:
1905 break;
1906 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001907#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001908
Dima Zavin799a70e2011-04-18 16:57:27 -07001909 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1910
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001911 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001912 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001913 // pre processing modules
Glenn Kasten32550952013-08-06 10:45:10 -07001914 RecordThread *thread = new RecordThread(this,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001915 input,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001916 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001917 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001918 *pDevices
1919#ifdef TEE_SINK
1920 , teeSink
1921#endif
1922 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001923 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001924 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001925 if (pSamplingRate != NULL) {
1926 *pSamplingRate = reqSamplingRate;
1927 }
1928 if (pFormat != NULL) {
1929 *pFormat = config.format;
1930 }
1931 if (pChannelMask != NULL) {
Glenn Kastenf506e942013-08-06 10:49:43 -07001932 *pChannelMask = reqChannelMask;
Glenn Kasten7c027242012-12-26 14:43:16 -08001933 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001934
Mathias Agopian65ab4712010-07-14 17:59:35 -07001935 // notify client processes of the new input creation
Eric Laurent021cf962014-05-13 10:18:14 -07001936 thread->audioConfigChanged(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001937 return id;
1938 }
1939
1940 return 0;
1941}
1942
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001943status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001944{
Glenn Kastend96c5722012-04-25 13:44:49 -07001945 return closeInput_nonvirtual(input);
1946}
1947
1948status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1949{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001950 // keep strong reference on the record thread so that
1951 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001952 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001953 {
1954 Mutex::Autolock _l(mLock);
1955 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001956 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001957 return BAD_VALUE;
1958 }
1959
Steve Block3856b092011-10-20 11:56:00 +01001960 ALOGV("closeInput() %d", input);
Eric Laurent021cf962014-05-13 10:18:14 -07001961 audioConfigChanged(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001962 mRecordThreads.removeItem(input);
1963 }
1964 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001965 // The thread entity (active unit of execution) is no longer running here,
1966 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001967
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001968 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001969 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001970 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001971 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001972 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001973
1974 return NO_ERROR;
1975}
1976
Glenn Kastend2304db2014-02-03 07:40:31 -08001977status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001978{
1979 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08001980 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001981
1982 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1983 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001984 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001985 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001986
1987 return NO_ERROR;
1988}
1989
1990
1991int AudioFlinger::newAudioSessionId()
1992{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001993 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001994}
1995
Marco Nelissend457c972014-02-11 08:47:07 -08001996void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001997{
1998 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001999 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002000 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2001 if (pid != -1 && (caller == getpid_cached)) {
2002 caller = pid;
2003 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002004
Eric Laurent021cf962014-05-13 10:18:14 -07002005 {
2006 Mutex::Autolock _cl(mClientLock);
2007 // Ignore requests received from processes not known as notification client. The request
2008 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2009 // called from a different pid leaving a stale session reference. Also we don't know how
2010 // to clear this reference if the client process dies.
2011 if (mNotificationClients.indexOfKey(caller) < 0) {
2012 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2013 return;
2014 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002015 }
2016
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002017 size_t num = mAudioSessionRefs.size();
2018 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002019 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002020 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2021 ref->mCnt++;
2022 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002023 return;
2024 }
2025 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002026 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2027 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002028}
2029
Marco Nelissend457c972014-02-11 08:47:07 -08002030void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002031{
2032 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002033 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002034 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2035 if (pid != -1 && (caller == getpid_cached)) {
2036 caller = pid;
2037 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002038 size_t num = mAudioSessionRefs.size();
2039 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002040 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002041 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2042 ref->mCnt--;
2043 ALOGV(" decremented refcount to %d", ref->mCnt);
2044 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002045 mAudioSessionRefs.removeAt(i);
2046 delete ref;
2047 purgeStaleEffects_l();
2048 }
2049 return;
2050 }
2051 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002052 // If the caller is mediaserver it is likely that the session being released was acquired
2053 // on behalf of a process not in notification clients and we ignore the warning.
2054 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002055}
2056
2057void AudioFlinger::purgeStaleEffects_l() {
2058
Steve Block3856b092011-10-20 11:56:00 +01002059 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002060
2061 Vector< sp<EffectChain> > chains;
2062
2063 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2064 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2065 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2066 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002067 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2068 chains.push(ec);
2069 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002070 }
2071 }
2072 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2073 sp<RecordThread> t = mRecordThreads.valueAt(i);
2074 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2075 sp<EffectChain> ec = t->mEffectChains[j];
2076 chains.push(ec);
2077 }
2078 }
2079
2080 for (size_t i = 0; i < chains.size(); i++) {
2081 sp<EffectChain> ec = chains[i];
2082 int sessionid = ec->sessionId();
2083 sp<ThreadBase> t = ec->mThread.promote();
2084 if (t == 0) {
2085 continue;
2086 }
2087 size_t numsessionrefs = mAudioSessionRefs.size();
2088 bool found = false;
2089 for (size_t k = 0; k < numsessionrefs; k++) {
2090 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002091 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002092 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002093 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002094 found = true;
2095 break;
2096 }
2097 }
2098 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002099 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002100 // remove all effects from the chain
2101 while (ec->mEffects.size()) {
2102 sp<EffectModule> effect = ec->mEffects[0];
2103 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002104 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002105 if (effect->purgeHandles()) {
2106 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002107 }
2108 AudioSystem::unregisterEffect(effect->id());
2109 }
2110 }
2111 }
2112 return;
2113}
2114
Mathias Agopian65ab4712010-07-14 17:59:35 -07002115// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002116AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002117{
Glenn Kastena1117922012-01-26 10:53:32 -08002118 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002119}
2120
2121// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002122AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002123{
2124 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002125 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002126}
2127
2128// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002129AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002130{
Glenn Kastena1117922012-01-26 10:53:32 -08002131 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002132}
2133
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002134uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002135{
Glenn Kastenbcefec32014-01-17 12:09:05 -08002136 return (uint32_t) android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002137}
2138
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002139AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002140{
2141 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2142 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002143 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002144 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002145 return thread;
2146 }
2147 }
2148 return NULL;
2149}
2150
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002151audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002152{
2153 PlaybackThread *thread = primaryPlaybackThread_l();
2154
2155 if (thread == NULL) {
2156 return 0;
2157 }
2158
Eric Laurentf1c04f92012-08-28 14:26:53 -07002159 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002160}
2161
Eric Laurenta011e352012-03-29 15:51:43 -07002162sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2163 int triggerSession,
2164 int listenerSession,
2165 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002166 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002167{
2168 Mutex::Autolock _l(mLock);
2169
2170 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2171 status_t playStatus = NAME_NOT_FOUND;
2172 status_t recStatus = NAME_NOT_FOUND;
2173 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2174 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2175 if (playStatus == NO_ERROR) {
2176 return event;
2177 }
2178 }
2179 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2180 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2181 if (recStatus == NO_ERROR) {
2182 return event;
2183 }
2184 }
2185 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2186 mPendingSyncEvents.add(event);
2187 } else {
2188 ALOGV("createSyncEvent() invalid event %d", event->type());
2189 event.clear();
2190 }
2191 return event;
2192}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002193
Mathias Agopian65ab4712010-07-14 17:59:35 -07002194// ----------------------------------------------------------------------------
2195// Effect management
2196// ----------------------------------------------------------------------------
2197
2198
Glenn Kastenf587ba52012-01-26 16:25:10 -08002199status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002200{
2201 Mutex::Autolock _l(mLock);
2202 return EffectQueryNumberEffects(numEffects);
2203}
2204
Glenn Kastenf587ba52012-01-26 16:25:10 -08002205status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002206{
2207 Mutex::Autolock _l(mLock);
2208 return EffectQueryEffect(index, descriptor);
2209}
2210
Glenn Kasten5e92a782012-01-30 07:40:52 -08002211status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002212 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002213{
2214 Mutex::Autolock _l(mLock);
2215 return EffectGetDescriptor(pUuid, descriptor);
2216}
2217
2218
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002219sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002220 effect_descriptor_t *pDesc,
2221 const sp<IEffectClient>& effectClient,
2222 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002223 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002224 int sessionId,
2225 status_t *status,
2226 int *id,
2227 int *enabled)
2228{
2229 status_t lStatus = NO_ERROR;
2230 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002231 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002232
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002233 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002234 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002235 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002236
2237 if (pDesc == NULL) {
2238 lStatus = BAD_VALUE;
2239 goto Exit;
2240 }
2241
Eric Laurent84e9a102010-09-23 16:10:16 -07002242 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002243 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002244 lStatus = PERMISSION_DENIED;
2245 goto Exit;
2246 }
2247
Dima Zavinfce7a472011-04-19 22:30:36 -07002248 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002249 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002250 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002251 lStatus = PERMISSION_DENIED;
2252 goto Exit;
2253 }
2254
Mathias Agopian65ab4712010-07-14 17:59:35 -07002255 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002256 if (!EffectIsNullUuid(&pDesc->uuid)) {
2257 // if uuid is specified, request effect descriptor
2258 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2259 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002260 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002261 goto Exit;
2262 }
2263 } else {
2264 // if uuid is not specified, look for an available implementation
2265 // of the required type in effect factory
2266 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002267 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002268 lStatus = BAD_VALUE;
2269 goto Exit;
2270 }
2271 uint32_t numEffects = 0;
2272 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002273 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002274 bool found = false;
2275
2276 lStatus = EffectQueryNumberEffects(&numEffects);
2277 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002278 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002279 goto Exit;
2280 }
2281 for (uint32_t i = 0; i < numEffects; i++) {
2282 lStatus = EffectQueryEffect(i, &desc);
2283 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002284 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002285 continue;
2286 }
2287 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2288 // If matching type found save effect descriptor. If the session is
2289 // 0 and the effect is not auxiliary, continue enumeration in case
2290 // an auxiliary version of this effect type is available
2291 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002292 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002293 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002294 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2295 break;
2296 }
2297 }
2298 }
2299 if (!found) {
2300 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002301 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002302 goto Exit;
2303 }
2304 // For same effect type, chose auxiliary version over insert version if
2305 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002306 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002307 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002308 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002309 }
2310 }
2311
2312 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002313 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002314 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2315 lStatus = INVALID_OPERATION;
2316 goto Exit;
2317 }
2318
Eric Laurent59255e42011-07-27 19:49:51 -07002319 // check recording permission for visualizer
2320 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2321 !recordingAllowed()) {
2322 lStatus = PERMISSION_DENIED;
2323 goto Exit;
2324 }
2325
Mathias Agopian65ab4712010-07-14 17:59:35 -07002326 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002327 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002328 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002329 // if the output returned by getOutputForEffect() is removed before we lock the
2330 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2331 // and we will exit safely
2332 io = AudioSystem::getOutputForEffect(&desc);
2333 ALOGV("createEffect got output %d", io);
2334 }
2335
2336 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002337
2338 // If output is not specified try to find a matching audio session ID in one of the
2339 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002340 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2341 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002342 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002343 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002344 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2345 // output must be specified by AudioPolicyManager when using session
2346 // AUDIO_SESSION_OUTPUT_STAGE
2347 lStatus = BAD_VALUE;
2348 goto Exit;
2349 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002350 // look for the thread where the specified audio session is present
2351 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2352 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2353 io = mPlaybackThreads.keyAt(i);
2354 break;
2355 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002356 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002357 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002358 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2359 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2360 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002361 break;
2362 }
2363 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002364 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002365 // If no output thread contains the requested session ID, default to
2366 // first output. The effect chain will be moved to the correct output
2367 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002368 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002369 io = mPlaybackThreads.keyAt(0);
2370 }
Steve Block3856b092011-10-20 11:56:00 +01002371 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002372 }
2373 ThreadBase *thread = checkRecordThread_l(io);
2374 if (thread == NULL) {
2375 thread = checkPlaybackThread_l(io);
2376 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002377 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002378 lStatus = BAD_VALUE;
2379 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002380 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002381 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002382
Eric Laurent021cf962014-05-13 10:18:14 -07002383 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002384
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002385 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002386 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2387 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002388 if (handle != 0 && id != NULL) {
2389 *id = handle->id();
2390 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002391 if (handle == 0) {
2392 // remove local strong reference to Client with mClientLock held
2393 Mutex::Autolock _cl(mClientLock);
2394 client.clear();
2395 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002396 }
2397
2398Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002399 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002400 return handle;
2401}
2402
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002403status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2404 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002405{
Steve Block3856b092011-10-20 11:56:00 +01002406 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002407 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002408 Mutex::Autolock _l(mLock);
2409 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002410 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002411 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002412 }
Eric Laurentde070132010-07-13 04:45:46 -07002413 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2414 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002415 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002416 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002417 }
Eric Laurentde070132010-07-13 04:45:46 -07002418 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2419 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002420 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002421 return BAD_VALUE;
2422 }
2423
2424 Mutex::Autolock _dl(dstThread->mLock);
2425 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002426 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002427}
2428
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002429// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002430status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002431 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002432 AudioFlinger::PlaybackThread *dstThread,
2433 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002434{
Steve Block3856b092011-10-20 11:56:00 +01002435 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002436 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002437
Eric Laurent59255e42011-07-27 19:49:51 -07002438 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002439 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002440 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002441 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002442 return INVALID_OPERATION;
2443 }
2444
Eric Laurent39e94f82010-07-28 01:32:47 -07002445 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002446 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002447 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002448 // removed.
2449 srcThread->removeEffectChain_l(chain);
2450
2451 // transfer all effects one by one so that new effect chain is created on new thread with
2452 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002453 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002454 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002455 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002456 Vector< sp<EffectModule> > removed;
2457 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002458 while (effect != 0) {
2459 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002460 removed.add(effect);
2461 status = dstThread->addEffect_l(effect);
2462 if (status != NO_ERROR) {
2463 break;
2464 }
Eric Laurentec35a142011-10-05 17:42:25 -07002465 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2466 if (effect->state() == EffectModule::ACTIVE ||
2467 effect->state() == EffectModule::STOPPING) {
2468 effect->start();
2469 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002470 // if the move request is not received from audio policy manager, the effect must be
2471 // re-registered with the new strategy and output
2472 if (dstChain == 0) {
2473 dstChain = effect->chain().promote();
2474 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002475 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002476 status = NO_INIT;
2477 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002478 }
2479 strategy = dstChain->strategy();
2480 }
2481 if (reRegister) {
2482 AudioSystem::unregisterEffect(effect->id());
2483 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002484 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002485 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002486 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002487 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002488 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002489 }
Eric Laurentde070132010-07-13 04:45:46 -07002490 effect = chain->getEffectFromId_l(0);
2491 }
2492
Eric Laurent5baf2af2013-09-12 17:37:00 -07002493 if (status != NO_ERROR) {
2494 for (size_t i = 0; i < removed.size(); i++) {
2495 srcThread->addEffect_l(removed[i]);
2496 if (dstChain != 0 && reRegister) {
2497 AudioSystem::unregisterEffect(removed[i]->id());
2498 AudioSystem::registerEffect(&removed[i]->desc(),
2499 srcThread->id(),
2500 strategy,
2501 sessionId,
2502 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002503 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002504 }
2505 }
2506 }
2507
2508 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002509}
2510
Eric Laurent5baf2af2013-09-12 17:37:00 -07002511bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002512{
2513 if (mGlobalEffectEnableTime != 0 &&
2514 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2515 return true;
2516 }
2517
2518 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2519 sp<EffectChain> ec =
2520 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002521 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002522 return true;
2523 }
2524 }
2525 return false;
2526}
2527
Eric Laurent5baf2af2013-09-12 17:37:00 -07002528void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002529{
2530 Mutex::Autolock _l(mLock);
2531
2532 mGlobalEffectEnableTime = systemTime();
2533
2534 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2535 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2536 if (t->mType == ThreadBase::OFFLOAD) {
2537 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2538 }
2539 }
2540
2541}
2542
Glenn Kastenda6ef132013-01-10 12:31:01 -08002543struct Entry {
2544#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2545 char mName[MAX_NAME];
2546};
2547
2548int comparEntry(const void *p1, const void *p2)
2549{
2550 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2551}
2552
Glenn Kasten46909e72013-02-26 09:20:22 -08002553#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002554void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002555{
Eric Laurent81784c32012-11-19 14:55:58 -08002556 NBAIO_Source *teeSource = source.get();
2557 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002558 // .wav rotation
2559 // There is a benign race condition if 2 threads call this simultaneously.
2560 // They would both traverse the directory, but the result would simply be
2561 // failures at unlink() which are ignored. It's also unlikely since
2562 // normally dumpsys is only done by bugreport or from the command line.
2563 char teePath[32+256];
2564 strcpy(teePath, "/data/misc/media");
2565 size_t teePathLen = strlen(teePath);
2566 DIR *dir = opendir(teePath);
2567 teePath[teePathLen++] = '/';
2568 if (dir != NULL) {
2569#define MAX_SORT 20 // number of entries to sort
2570#define MAX_KEEP 10 // number of entries to keep
2571 struct Entry entries[MAX_SORT];
2572 size_t entryCount = 0;
2573 while (entryCount < MAX_SORT) {
2574 struct dirent de;
2575 struct dirent *result = NULL;
2576 int rc = readdir_r(dir, &de, &result);
2577 if (rc != 0) {
2578 ALOGW("readdir_r failed %d", rc);
2579 break;
2580 }
2581 if (result == NULL) {
2582 break;
2583 }
2584 if (result != &de) {
2585 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2586 break;
2587 }
2588 // ignore non .wav file entries
2589 size_t nameLen = strlen(de.d_name);
2590 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2591 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2592 continue;
2593 }
2594 strcpy(entries[entryCount++].mName, de.d_name);
2595 }
2596 (void) closedir(dir);
2597 if (entryCount > MAX_KEEP) {
2598 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2599 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2600 strcpy(&teePath[teePathLen], entries[i].mName);
2601 (void) unlink(teePath);
2602 }
2603 }
2604 } else {
2605 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002606 dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002607 }
2608 }
Eric Laurent81784c32012-11-19 14:55:58 -08002609 char teeTime[16];
2610 struct timeval tv;
2611 gettimeofday(&tv, NULL);
2612 struct tm tm;
2613 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002614 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2615 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2616 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2617 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002618 if (teeFd >= 0) {
2619 char wavHeader[44];
2620 memcpy(wavHeader,
2621 "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",
2622 sizeof(wavHeader));
2623 NBAIO_Format format = teeSource->format();
2624 unsigned channelCount = Format_channelCount(format);
2625 ALOG_ASSERT(channelCount <= FCC_2);
2626 uint32_t sampleRate = Format_sampleRate(format);
2627 wavHeader[22] = channelCount; // number of channels
2628 wavHeader[24] = sampleRate; // sample rate
2629 wavHeader[25] = sampleRate >> 8;
2630 wavHeader[32] = channelCount * 2; // block alignment
2631 write(teeFd, wavHeader, sizeof(wavHeader));
2632 size_t total = 0;
2633 bool firstRead = true;
2634 for (;;) {
2635#define TEE_SINK_READ 1024
2636 short buffer[TEE_SINK_READ * FCC_2];
2637 size_t count = TEE_SINK_READ;
2638 ssize_t actual = teeSource->read(buffer, count,
2639 AudioBufferProvider::kInvalidPTS);
2640 bool wasFirstRead = firstRead;
2641 firstRead = false;
2642 if (actual <= 0) {
2643 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2644 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002645 }
Eric Laurent81784c32012-11-19 14:55:58 -08002646 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002647 }
Eric Laurent81784c32012-11-19 14:55:58 -08002648 ALOG_ASSERT(actual <= (ssize_t)count);
2649 write(teeFd, buffer, actual * channelCount * sizeof(short));
2650 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002651 }
Eric Laurent81784c32012-11-19 14:55:58 -08002652 lseek(teeFd, (off_t) 4, SEEK_SET);
2653 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2654 write(teeFd, &temp, sizeof(temp));
2655 lseek(teeFd, (off_t) 40, SEEK_SET);
2656 temp = total * channelCount * sizeof(short);
2657 write(teeFd, &temp, sizeof(temp));
2658 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002659 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002660 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002661 }
Eric Laurent59255e42011-07-27 19:49:51 -07002662 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002663 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002664 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002665 }
Eric Laurent59255e42011-07-27 19:49:51 -07002666 }
2667 }
2668}
Glenn Kasten46909e72013-02-26 09:20:22 -08002669#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002670
Mathias Agopian65ab4712010-07-14 17:59:35 -07002671// ----------------------------------------------------------------------------
2672
2673status_t AudioFlinger::onTransact(
2674 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2675{
2676 return BnAudioFlinger::onTransact(code, data, reply, flags);
2677}
2678
Mathias Agopian65ab4712010-07-14 17:59:35 -07002679}; // namespace android