blob: 68fa553ab74e5015af62189c552adba9c0a0ca4d [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
638 Mutex::Autolock _cl(mClientLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700639 client.clear();
640 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700641 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700642 }
643
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700644 // return handle to client
645 trackHandle = new TrackHandle(track);
646
Mathias Agopian65ab4712010-07-14 17:59:35 -0700647Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700648 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700649 return trackHandle;
650}
651
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800652uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700653{
654 Mutex::Autolock _l(mLock);
655 PlaybackThread *thread = checkPlaybackThread_l(output);
656 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000657 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700658 return 0;
659 }
660 return thread->sampleRate();
661}
662
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800663int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700664{
665 Mutex::Autolock _l(mLock);
666 PlaybackThread *thread = checkPlaybackThread_l(output);
667 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000668 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700669 return 0;
670 }
671 return thread->channelCount();
672}
673
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800674audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700675{
676 Mutex::Autolock _l(mLock);
677 PlaybackThread *thread = checkPlaybackThread_l(output);
678 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000679 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800680 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700681 }
682 return thread->format();
683}
684
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800685size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700686{
687 Mutex::Autolock _l(mLock);
688 PlaybackThread *thread = checkPlaybackThread_l(output);
689 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000690 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700691 return 0;
692 }
Glenn Kasten58912562012-04-03 10:45:00 -0700693 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
694 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700695 return thread->frameCount();
696}
697
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800698uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699{
700 Mutex::Autolock _l(mLock);
701 PlaybackThread *thread = checkPlaybackThread_l(output);
702 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800703 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704 return 0;
705 }
706 return thread->latency();
707}
708
709status_t AudioFlinger::setMasterVolume(float value)
710{
Eric Laurenta1884f92011-08-23 08:25:03 -0700711 status_t ret = initCheck();
712 if (ret != NO_ERROR) {
713 return ret;
714 }
715
Mathias Agopian65ab4712010-07-14 17:59:35 -0700716 // check calling permissions
717 if (!settingsAllowed()) {
718 return PERMISSION_DENIED;
719 }
720
Eric Laurenta4c5a552012-03-29 10:12:40 -0700721 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700722 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700723
John Grossmanee578c02012-07-23 17:05:46 -0700724 // Set master volume in the HALs which support it.
725 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
726 AutoMutex lock(mHardwareLock);
727 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800728
John Grossmanee578c02012-07-23 17:05:46 -0700729 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
730 if (dev->canSetMasterVolume()) {
731 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800732 }
John Grossmanee578c02012-07-23 17:05:46 -0700733 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700734 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735
John Grossmanee578c02012-07-23 17:05:46 -0700736 // Now set the master volume in each playback thread. Playback threads
737 // assigned to HALs which do not have master volume support will apply
738 // master volume during the mix operation. Threads with HALs which do
739 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800740 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700741 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700742
743 return NO_ERROR;
744}
745
Glenn Kastenf78aee72012-01-04 11:00:47 -0800746status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700747{
Eric Laurenta1884f92011-08-23 08:25:03 -0700748 status_t ret = initCheck();
749 if (ret != NO_ERROR) {
750 return ret;
751 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700752
753 // check calling permissions
754 if (!settingsAllowed()) {
755 return PERMISSION_DENIED;
756 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800757 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000758 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700759 return BAD_VALUE;
760 }
761
762 { // scope for the lock
763 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700764 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700765 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700766 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700767 mHardwareStatus = AUDIO_HW_IDLE;
768 }
769
770 if (NO_ERROR == ret) {
771 Mutex::Autolock _l(mLock);
772 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800773 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700774 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700775 }
776
777 return ret;
778}
779
780status_t AudioFlinger::setMicMute(bool state)
781{
Eric Laurenta1884f92011-08-23 08:25:03 -0700782 status_t ret = initCheck();
783 if (ret != NO_ERROR) {
784 return ret;
785 }
786
Mathias Agopian65ab4712010-07-14 17:59:35 -0700787 // check calling permissions
788 if (!settingsAllowed()) {
789 return PERMISSION_DENIED;
790 }
791
792 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700793 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700795 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700796 mHardwareStatus = AUDIO_HW_IDLE;
797 return ret;
798}
799
800bool AudioFlinger::getMicMute() const
801{
Eric Laurenta1884f92011-08-23 08:25:03 -0700802 status_t ret = initCheck();
803 if (ret != NO_ERROR) {
804 return false;
805 }
806
Dima Zavinfce7a472011-04-19 22:30:36 -0700807 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800808 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700809 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700810 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700811 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812 mHardwareStatus = AUDIO_HW_IDLE;
813 return state;
814}
815
816status_t AudioFlinger::setMasterMute(bool muted)
817{
John Grossmand8f178d2012-07-20 14:51:35 -0700818 status_t ret = initCheck();
819 if (ret != NO_ERROR) {
820 return ret;
821 }
822
Mathias Agopian65ab4712010-07-14 17:59:35 -0700823 // check calling permissions
824 if (!settingsAllowed()) {
825 return PERMISSION_DENIED;
826 }
827
John Grossmanee578c02012-07-23 17:05:46 -0700828 Mutex::Autolock _l(mLock);
829 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700830
John Grossmanee578c02012-07-23 17:05:46 -0700831 // Set master mute in the HALs which support it.
832 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
833 AutoMutex lock(mHardwareLock);
834 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700835
John Grossmanee578c02012-07-23 17:05:46 -0700836 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
837 if (dev->canSetMasterMute()) {
838 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700839 }
John Grossmanee578c02012-07-23 17:05:46 -0700840 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700841 }
842
John Grossmanee578c02012-07-23 17:05:46 -0700843 // Now set the master mute in each playback thread. Playback threads
844 // assigned to HALs which do not have master mute support will apply master
845 // mute during the mix operation. Threads with HALs which do support master
846 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800847 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700848 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700849
850 return NO_ERROR;
851}
852
853float AudioFlinger::masterVolume() const
854{
Glenn Kasten98067102011-12-13 11:47:54 -0800855 Mutex::Autolock _l(mLock);
856 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700857}
858
859bool AudioFlinger::masterMute() const
860{
Glenn Kasten98067102011-12-13 11:47:54 -0800861 Mutex::Autolock _l(mLock);
862 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700863}
864
John Grossman4ff14ba2012-02-08 16:37:41 -0800865float AudioFlinger::masterVolume_l() const
866{
John Grossman4ff14ba2012-02-08 16:37:41 -0800867 return mMasterVolume;
868}
869
John Grossmand8f178d2012-07-20 14:51:35 -0700870bool AudioFlinger::masterMute_l() const
871{
John Grossmanee578c02012-07-23 17:05:46 -0700872 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700873}
874
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800875status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
876 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700877{
878 // check calling permissions
879 if (!settingsAllowed()) {
880 return PERMISSION_DENIED;
881 }
882
Glenn Kasten263709e2012-01-06 08:40:01 -0800883 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000884 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700885 return BAD_VALUE;
886 }
887
888 AutoMutex lock(mLock);
889 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700890 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700891 thread = checkPlaybackThread_l(output);
892 if (thread == NULL) {
893 return BAD_VALUE;
894 }
895 }
896
897 mStreamTypes[stream].volume = value;
898
899 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800900 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700901 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700902 }
903 } else {
904 thread->setStreamVolume(stream, value);
905 }
906
907 return NO_ERROR;
908}
909
Glenn Kastenfff6d712012-01-12 16:38:12 -0800910status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911{
912 // check calling permissions
913 if (!settingsAllowed()) {
914 return PERMISSION_DENIED;
915 }
916
Glenn Kasten263709e2012-01-06 08:40:01 -0800917 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700918 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000919 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700920 return BAD_VALUE;
921 }
922
Eric Laurent93575202011-01-18 18:39:02 -0800923 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700924 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700925 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700926 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700927
928 return NO_ERROR;
929}
930
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800931float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700932{
Glenn Kasten263709e2012-01-06 08:40:01 -0800933 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700934 return 0.0f;
935 }
936
937 AutoMutex lock(mLock);
938 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -0700939 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940 PlaybackThread *thread = checkPlaybackThread_l(output);
941 if (thread == NULL) {
942 return 0.0f;
943 }
944 volume = thread->streamVolume(stream);
945 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800946 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947 }
948
949 return volume;
950}
951
Glenn Kastenfff6d712012-01-12 16:38:12 -0800952bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700953{
Glenn Kasten263709e2012-01-06 08:40:01 -0800954 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700955 return true;
956 }
957
Glenn Kasten6637baa2012-01-09 09:40:36 -0800958 AutoMutex lock(mLock);
959 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960}
961
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800962status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700963{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700964 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
965 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800966
Mathias Agopian65ab4712010-07-14 17:59:35 -0700967 // check calling permissions
968 if (!settingsAllowed()) {
969 return PERMISSION_DENIED;
970 }
971
Glenn Kasten142f5192014-03-25 17:44:59 -0700972 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
973 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700974 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700975 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800976 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700977 AutoMutex lock(mHardwareLock);
978 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
979 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
980 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
981 status_t result = dev->set_parameters(dev, keyValuePairs.string());
982 final_result = result ?: final_result;
983 }
984 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800985 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700986 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
987 AudioParameter param = AudioParameter(keyValuePairs);
988 String8 value;
989 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700990 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
991 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700992 for (size_t i = 0; i < mRecordThreads.size(); i++) {
993 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700994 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700995 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
996 // collect all of the thread's session IDs
997 KeyedVector<int, bool> ids = thread->sessionIds();
998 // suspend effects associated with those session IDs
999 for (size_t j = 0; j < ids.size(); ++j) {
1000 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001001 thread->setEffectSuspended(FX_IID_AEC,
1002 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001003 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001004 thread->setEffectSuspended(FX_IID_NS,
1005 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001006 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001007 }
1008 }
Eric Laurentbee53372011-08-29 12:42:48 -07001009 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001010 }
1011 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001012 String8 screenState;
1013 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1014 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001015 if (isOff != (AudioFlinger::mScreenState & 1)) {
1016 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001017 }
1018 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001019 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001020 }
1021
1022 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1023 // and the thread is exited once the lock is released
1024 sp<ThreadBase> thread;
1025 {
1026 Mutex::Autolock _l(mLock);
1027 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001028 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001029 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001030 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001031 // indicate output device change to all input threads for pre processing
1032 AudioParameter param = AudioParameter(keyValuePairs);
1033 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001034 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1035 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001036 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1037 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1038 }
1039 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001040 }
1041 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001042 if (thread != 0) {
1043 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001044 }
1045 return BAD_VALUE;
1046}
1047
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001048String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001049{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001050 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1051 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001052
Eric Laurenta4c5a552012-03-29 10:12:40 -07001053 Mutex::Autolock _l(mLock);
1054
Glenn Kasten142f5192014-03-25 17:44:59 -07001055 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001056 String8 out_s8;
1057
Dima Zavin799a70e2011-04-18 16:57:27 -07001058 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001059 char *s;
1060 {
1061 AutoMutex lock(mHardwareLock);
1062 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001063 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001064 s = dev->get_parameters(dev, keys.string());
1065 mHardwareStatus = AUDIO_HW_IDLE;
1066 }
John Grossmanef7740b2012-02-09 11:28:36 -08001067 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001068 free(s);
1069 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001070 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001071 }
1072
Mathias Agopian65ab4712010-07-14 17:59:35 -07001073 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1074 if (playbackThread != NULL) {
1075 return playbackThread->getParameters(keys);
1076 }
1077 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1078 if (recordThread != NULL) {
1079 return recordThread->getParameters(keys);
1080 }
1081 return String8("");
1082}
1083
Glenn Kastendd8104c2012-07-02 12:42:44 -07001084size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1085 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001086{
Eric Laurenta1884f92011-08-23 08:25:03 -07001087 status_t ret = initCheck();
1088 if (ret != NO_ERROR) {
1089 return 0;
1090 }
1091
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001092 AutoMutex lock(mHardwareLock);
1093 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001094 struct audio_config config;
1095 memset(&config, 0, sizeof(config));
1096 config.sample_rate = sampleRate;
1097 config.channel_mask = channelMask;
1098 config.format = format;
1099
John Grossmanee578c02012-07-23 17:05:46 -07001100 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1101 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001102 mHardwareStatus = AUDIO_HW_IDLE;
1103 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001104}
1105
Glenn Kasten5f972c02014-01-13 09:59:31 -08001106uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001107{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001108 Mutex::Autolock _l(mLock);
1109
1110 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1111 if (recordThread != NULL) {
1112 return recordThread->getInputFramesLost();
1113 }
1114 return 0;
1115}
1116
1117status_t AudioFlinger::setVoiceVolume(float value)
1118{
Eric Laurenta1884f92011-08-23 08:25:03 -07001119 status_t ret = initCheck();
1120 if (ret != NO_ERROR) {
1121 return ret;
1122 }
1123
Mathias Agopian65ab4712010-07-14 17:59:35 -07001124 // check calling permissions
1125 if (!settingsAllowed()) {
1126 return PERMISSION_DENIED;
1127 }
1128
1129 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001130 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001131 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001132 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001133 mHardwareStatus = AUDIO_HW_IDLE;
1134
1135 return ret;
1136}
1137
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001138status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001139 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001140{
1141 status_t status;
1142
1143 Mutex::Autolock _l(mLock);
1144
1145 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1146 if (playbackThread != NULL) {
1147 return playbackThread->getRenderPosition(halFrames, dspFrames);
1148 }
1149
1150 return BAD_VALUE;
1151}
1152
1153void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1154{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001155 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001156 bool clientAdded = false;
1157 {
1158 Mutex::Autolock _cl(mClientLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001159
Eric Laurent021cf962014-05-13 10:18:14 -07001160 pid_t pid = IPCThreadState::self()->getCallingPid();
1161 if (mNotificationClients.indexOfKey(pid) < 0) {
1162 sp<NotificationClient> notificationClient = new NotificationClient(this,
1163 client,
1164 pid);
1165 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001166
Eric Laurent021cf962014-05-13 10:18:14 -07001167 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001168
Eric Laurent021cf962014-05-13 10:18:14 -07001169 sp<IBinder> binder = client->asBinder();
1170 binder->linkToDeath(notificationClient);
1171 clientAdded = true;
1172 }
1173 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001174
Eric Laurent021cf962014-05-13 10:18:14 -07001175 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
1176 // ThreadBase mutex and teh locknig order is ThreadBase::mLock then AudioFlinger::mClientLock.
1177 if (clientAdded) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001178 // the config change is always sent from playback or record threads to avoid deadlock
1179 // with AudioSystem::gLock
1180 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001181 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001182 }
1183
1184 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001185 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001186 }
1187 }
1188}
1189
1190void AudioFlinger::removeNotificationClient(pid_t pid)
1191{
1192 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001193 {
1194 Mutex::Autolock _cl(mClientLock);
1195 mNotificationClients.removeItem(pid);
1196 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001197
Steve Block3856b092011-10-20 11:56:00 +01001198 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001199 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001200 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001201 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001202 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001203 ALOGV(" pid %d @ %d", ref->mPid, i);
1204 if (ref->mPid == pid) {
1205 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001206 mAudioSessionRefs.removeAt(i);
1207 delete ref;
1208 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001209 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001210 } else {
1211 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001212 }
1213 }
1214 if (removed) {
1215 purgeStaleEffects_l();
1216 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001217}
1218
Eric Laurent021cf962014-05-13 10:18:14 -07001219void AudioFlinger::audioConfigChanged(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001220{
Eric Laurent021cf962014-05-13 10:18:14 -07001221 Mutex::Autolock _l(mClientLock);
1222 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001223 for (size_t i = 0; i < size; i++) {
Eric Laurent021cf962014-05-13 10:18:14 -07001224 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event,
Eric Laurent10351942014-05-08 18:49:52 -07001225 ioHandle,
1226 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001227 }
1228}
1229
Eric Laurent021cf962014-05-13 10:18:14 -07001230// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001231void AudioFlinger::removeClient_l(pid_t pid)
1232{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001233 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001234 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001235 mClients.removeItem(pid);
1236}
1237
Eric Laurent717e1282012-06-29 16:36:52 -07001238// getEffectThread_l() must be called with AudioFlinger::mLock held
1239sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1240{
1241 sp<PlaybackThread> thread;
1242
1243 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1244 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1245 ALOG_ASSERT(thread == 0);
1246 thread = mPlaybackThreads.valueAt(i);
1247 }
1248 }
1249
1250 return thread;
1251}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001252
Mathias Agopian65ab4712010-07-14 17:59:35 -07001253
Mathias Agopian65ab4712010-07-14 17:59:35 -07001254
1255// ----------------------------------------------------------------------------
1256
1257AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1258 : RefBase(),
1259 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001260 // 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 -07001261 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001262 mPid(pid),
1263 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001264{
1265 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1266}
1267
Eric Laurent021cf962014-05-13 10:18:14 -07001268// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001269AudioFlinger::Client::~Client()
1270{
1271 mAudioFlinger->removeClient_l(mPid);
1272}
1273
Glenn Kasten435dbe62012-01-30 10:15:48 -08001274sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001275{
1276 return mMemoryDealer;
1277}
1278
John Grossman4ff14ba2012-02-08 16:37:41 -08001279// Reserve one of the limited slots for a timed audio track associated
1280// with this client
1281bool AudioFlinger::Client::reserveTimedTrack()
1282{
1283 const int kMaxTimedTracksPerClient = 4;
1284
1285 Mutex::Autolock _l(mTimedTrackLock);
1286
1287 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1288 ALOGW("can not create timed track - pid %d has exceeded the limit",
1289 mPid);
1290 return false;
1291 }
1292
1293 mTimedTrackCount++;
1294 return true;
1295}
1296
1297// Release a slot for a timed audio track
1298void AudioFlinger::Client::releaseTimedTrack()
1299{
1300 Mutex::Autolock _l(mTimedTrackLock);
1301 mTimedTrackCount--;
1302}
1303
Mathias Agopian65ab4712010-07-14 17:59:35 -07001304// ----------------------------------------------------------------------------
1305
1306AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1307 const sp<IAudioFlingerClient>& client,
1308 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001309 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001310{
1311}
1312
1313AudioFlinger::NotificationClient::~NotificationClient()
1314{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001315}
1316
Glenn Kasten0f11b512014-01-31 16:18:54 -08001317void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001318{
1319 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001320 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001321}
1322
Mathias Agopian65ab4712010-07-14 17:59:35 -07001323
1324// ----------------------------------------------------------------------------
1325
Jeff Brown893a5642013-08-16 20:19:26 -07001326static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1327 return audio_is_remote_submix_device(inDevice);
1328}
1329
Mathias Agopian65ab4712010-07-14 17:59:35 -07001330sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001331 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001332 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001333 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001334 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001335 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001336 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001337 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001338 int *sessionId,
Glenn Kastend776ac62014-05-07 09:16:09 -07001339 sp<IMemory>& cblk,
1340 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001341 status_t *status)
1342{
1343 sp<RecordThread::RecordTrack> recordTrack;
1344 sp<RecordHandle> recordHandle;
1345 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001346 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001347 int lSessionId;
1348
Glenn Kastend776ac62014-05-07 09:16:09 -07001349 cblk.clear();
1350 buffers.clear();
1351
Mathias Agopian65ab4712010-07-14 17:59:35 -07001352 // check calling permissions
1353 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001354 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001355 lStatus = PERMISSION_DENIED;
1356 goto Exit;
1357 }
1358
Glenn Kasten53b5d092014-02-05 10:00:23 -08001359 // further sample rate checks are performed by createRecordTrack_l()
1360 if (sampleRate == 0) {
1361 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1362 lStatus = BAD_VALUE;
1363 goto Exit;
1364 }
1365
Glenn Kasten0e0e8462014-03-13 15:02:40 -07001366 // we don't yet support anything other than 16-bit PCM
1367 if (!(audio_is_valid_format(format) &&
1368 audio_is_linear_pcm(format) && format == AUDIO_FORMAT_PCM_16_BIT)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001369 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001370 lStatus = BAD_VALUE;
1371 goto Exit;
1372 }
1373
Glenn Kasten53b5d092014-02-05 10:00:23 -08001374 // further channel mask checks are performed by createRecordTrack_l()
1375 if (!audio_is_input_channel(channelMask)) {
1376 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1377 lStatus = BAD_VALUE;
1378 goto Exit;
1379 }
1380
Glenn Kasten05997e22014-03-13 15:08:33 -07001381 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001382 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001383 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001384 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001385 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001386 lStatus = BAD_VALUE;
1387 goto Exit;
1388 }
1389
Jeff Brown893a5642013-08-16 20:19:26 -07001390 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1391 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001392 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001393 lStatus = PERMISSION_DENIED;
1394 goto Exit;
1395 }
1396
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001397 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001398 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001399
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001400 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001401 lSessionId = *sessionId;
1402 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001403 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001404 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001405 if (sessionId != NULL) {
1406 *sessionId = lSessionId;
1407 }
1408 }
Glenn Kasten570f6332014-03-13 15:01:06 -07001409 ALOGV("openRecord() lSessionId: %d", lSessionId);
1410
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001411 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001412 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001413 frameCount, lSessionId,
1414 IPCThreadState::self()->getCallingUid(),
1415 flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001416 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001417 }
Glenn Kastene198c362013-08-13 09:13:36 -07001418
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001419 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001420 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001421 // Client destructor is called by the TrackBase destructor with mClientLock held
1422 Mutex::Autolock _cl(mClientLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001423 client.clear();
1424 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001425 goto Exit;
1426 }
1427
Glenn Kastend776ac62014-05-07 09:16:09 -07001428 cblk = recordTrack->getCblk();
1429 buffers = recordTrack->getBuffers();
1430
Glenn Kasten2fc14732013-08-05 14:58:14 -07001431 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001432 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001433
1434Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001435 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001436 return recordHandle;
1437}
1438
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001439
1440
Mathias Agopian65ab4712010-07-14 17:59:35 -07001441// ----------------------------------------------------------------------------
1442
Eric Laurenta4c5a552012-03-29 10:12:40 -07001443audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1444{
1445 if (!settingsAllowed()) {
1446 return 0;
1447 }
1448 Mutex::Autolock _l(mLock);
1449 return loadHwModule_l(name);
1450}
1451
1452// loadHwModule_l() must be called with AudioFlinger::mLock held
1453audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1454{
1455 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1456 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1457 ALOGW("loadHwModule() module %s already loaded", name);
1458 return mAudioHwDevs.keyAt(i);
1459 }
1460 }
1461
Eric Laurenta4c5a552012-03-29 10:12:40 -07001462 audio_hw_device_t *dev;
1463
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001464 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001465 if (rc) {
1466 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1467 return 0;
1468 }
1469
1470 mHardwareStatus = AUDIO_HW_INIT;
1471 rc = dev->init_check(dev);
1472 mHardwareStatus = AUDIO_HW_IDLE;
1473 if (rc) {
1474 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1475 return 0;
1476 }
1477
John Grossmanee578c02012-07-23 17:05:46 -07001478 // Check and cache this HAL's level of support for master mute and master
1479 // volume. If this is the first HAL opened, and it supports the get
1480 // methods, use the initial values provided by the HAL as the current
1481 // master mute and volume settings.
1482
1483 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1484 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001485 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001486
1487 if (0 == mAudioHwDevs.size()) {
1488 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1489 if (NULL != dev->get_master_volume) {
1490 float mv;
1491 if (OK == dev->get_master_volume(dev, &mv)) {
1492 mMasterVolume = mv;
1493 }
1494 }
1495
1496 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1497 if (NULL != dev->get_master_mute) {
1498 bool mm;
1499 if (OK == dev->get_master_mute(dev, &mm)) {
1500 mMasterMute = mm;
1501 }
1502 }
1503 }
1504
Eric Laurenta4c5a552012-03-29 10:12:40 -07001505 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001506 if ((NULL != dev->set_master_volume) &&
1507 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1508 flags = static_cast<AudioHwDevice::Flags>(flags |
1509 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1510 }
1511
1512 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1513 if ((NULL != dev->set_master_mute) &&
1514 (OK == dev->set_master_mute(dev, mMasterMute))) {
1515 flags = static_cast<AudioHwDevice::Flags>(flags |
1516 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1517 }
1518
Eric Laurenta4c5a552012-03-29 10:12:40 -07001519 mHardwareStatus = AUDIO_HW_IDLE;
1520 }
1521
1522 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001523 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001524
1525 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001526 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001527
1528 return handle;
1529
1530}
1531
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001532// ----------------------------------------------------------------------------
1533
Glenn Kasten3b16c762012-11-14 08:44:39 -08001534uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001535{
1536 Mutex::Autolock _l(mLock);
1537 PlaybackThread *thread = primaryPlaybackThread_l();
1538 return thread != NULL ? thread->sampleRate() : 0;
1539}
1540
Glenn Kastene33054e2012-11-14 12:54:39 -08001541size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001542{
1543 Mutex::Autolock _l(mLock);
1544 PlaybackThread *thread = primaryPlaybackThread_l();
1545 return thread != NULL ? thread->frameCountHAL() : 0;
1546}
1547
1548// ----------------------------------------------------------------------------
1549
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001550status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1551{
1552 uid_t uid = IPCThreadState::self()->getCallingUid();
1553 if (uid != AID_SYSTEM) {
1554 return PERMISSION_DENIED;
1555 }
1556 Mutex::Autolock _l(mLock);
1557 if (mIsDeviceTypeKnown) {
1558 return INVALID_OPERATION;
1559 }
1560 mIsLowRamDevice = isLowRamDevice;
1561 mIsDeviceTypeKnown = true;
1562 return NO_ERROR;
1563}
1564
1565// ----------------------------------------------------------------------------
1566
Eric Laurenta4c5a552012-03-29 10:12:40 -07001567audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1568 audio_devices_t *pDevices,
1569 uint32_t *pSamplingRate,
1570 audio_format_t *pFormat,
1571 audio_channel_mask_t *pChannelMask,
1572 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001573 audio_output_flags_t flags,
1574 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001575{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001576 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001577 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001578 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1579 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1580 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
Glenn Kasten937098b2013-06-26 11:19:36 -07001581 if (offloadInfo != NULL) {
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001582 config.offload_info = *offloadInfo;
1583 }
1584
Eric Laurentbfb1b832013-01-07 09:53:42 -08001585 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001586 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001587 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001588 config.sample_rate,
1589 config.format,
1590 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001591 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001592 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
Glenn Kastene198c362013-08-13 09:13:36 -07001593 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001594
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001595 if (pDevices == NULL || *pDevices == AUDIO_DEVICE_NONE) {
Glenn Kasten142f5192014-03-25 17:44:59 -07001596 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001597 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001598
Mathias Agopian65ab4712010-07-14 17:59:35 -07001599 Mutex::Autolock _l(mLock);
1600
Glenn Kasten32550952013-08-06 10:45:10 -07001601 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001602 if (outHwDev == NULL) {
Glenn Kasten142f5192014-03-25 17:44:59 -07001603 return AUDIO_IO_HANDLE_NONE;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001604 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001605
John Grossmanee578c02012-07-23 17:05:46 -07001606 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001607 audio_io_handle_t id = nextUniqueId();
1608
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001609 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001610
Glenn Kasten32550952013-08-06 10:45:10 -07001611 audio_stream_out_t *outStream = NULL;
Glenn Kasten34542ac2013-06-26 11:29:02 -07001612 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001613 id,
1614 *pDevices,
1615 (audio_output_flags_t)flags,
1616 &config,
1617 &outStream);
1618
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001619 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001620 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001621 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001622 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001623 config.sample_rate,
1624 config.format,
1625 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001626 status);
1627
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001628 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001629 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001630
Glenn Kasten32550952013-08-06 10:45:10 -07001631 PlaybackThread *thread;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001632 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1633 thread = new OffloadThread(this, output, id, *pDevices);
1634 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1635 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001636 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1637 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001638 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001639 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001640 } else {
1641 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001642 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001643 }
1644 mPlaybackThreads.add(id, thread);
1645
Glenn Kasten7c027242012-12-26 14:43:16 -08001646 if (pSamplingRate != NULL) {
1647 *pSamplingRate = config.sample_rate;
1648 }
1649 if (pFormat != NULL) {
1650 *pFormat = config.format;
1651 }
1652 if (pChannelMask != NULL) {
1653 *pChannelMask = config.channel_mask;
1654 }
1655 if (pLatencyMs != NULL) {
1656 *pLatencyMs = thread->latency();
1657 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001658
1659 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001660 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001661
1662 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001663 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001664 ALOGI("Using module %d has the primary audio interface", module);
1665 mPrimaryHardwareDev = outHwDev;
1666
1667 AutoMutex lock(mHardwareLock);
1668 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001669 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001670 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001671 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001672 return id;
1673 }
1674
Glenn Kasten142f5192014-03-25 17:44:59 -07001675 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001676}
1677
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001678audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1679 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001680{
1681 Mutex::Autolock _l(mLock);
1682 MixerThread *thread1 = checkMixerThread_l(output1);
1683 MixerThread *thread2 = checkMixerThread_l(output2);
1684
1685 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001686 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1687 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001688 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001689 }
1690
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001691 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001692 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1693 thread->addOutputTrack(thread2);
1694 mPlaybackThreads.add(id, thread);
1695 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001696 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001697 return id;
1698}
1699
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001700status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001701{
Glenn Kastend96c5722012-04-25 13:44:49 -07001702 return closeOutput_nonvirtual(output);
1703}
1704
1705status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1706{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001707 // keep strong reference on the playback thread so that
1708 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001709 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001710 {
1711 Mutex::Autolock _l(mLock);
1712 thread = checkPlaybackThread_l(output);
1713 if (thread == NULL) {
1714 return BAD_VALUE;
1715 }
1716
Steve Block3856b092011-10-20 11:56:00 +01001717 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001718
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001719 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001720 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001721 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001722 DuplicatingThread *dupThread =
1723 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001724 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001725
1726 }
1727 }
1728 }
1729
1730
1731 mPlaybackThreads.removeItem(output);
1732 // save all effects to the default thread
1733 if (mPlaybackThreads.size()) {
1734 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1735 if (dstThread != NULL) {
1736 // audioflinger lock is held here so the acquisition order of thread locks does not
1737 // matter
1738 Mutex::Autolock _dl(dstThread->mLock);
1739 Mutex::Autolock _sl(thread->mLock);
1740 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1741 for (size_t i = 0; i < effectChains.size(); i ++) {
1742 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001743 }
1744 }
1745 }
Eric Laurent021cf962014-05-13 10:18:14 -07001746 audioConfigChanged(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001747 }
1748 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001749 // The thread entity (active unit of execution) is no longer running here,
1750 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001751
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001752 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001753 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001754 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001755 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001756 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001757 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001758 }
1759 return NO_ERROR;
1760}
1761
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001762status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001763{
1764 Mutex::Autolock _l(mLock);
1765 PlaybackThread *thread = checkPlaybackThread_l(output);
1766
1767 if (thread == NULL) {
1768 return BAD_VALUE;
1769 }
1770
Steve Block3856b092011-10-20 11:56:00 +01001771 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001772 thread->suspend();
1773
1774 return NO_ERROR;
1775}
1776
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001777status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001778{
1779 Mutex::Autolock _l(mLock);
1780 PlaybackThread *thread = checkPlaybackThread_l(output);
1781
1782 if (thread == NULL) {
1783 return BAD_VALUE;
1784 }
1785
Steve Block3856b092011-10-20 11:56:00 +01001786 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001787
1788 thread->restore();
1789
1790 return NO_ERROR;
1791}
1792
Eric Laurenta4c5a552012-03-29 10:12:40 -07001793audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1794 audio_devices_t *pDevices,
1795 uint32_t *pSamplingRate,
1796 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001797 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001798{
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001799 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001800 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001801 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1802 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1803 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1804
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001805 uint32_t reqSamplingRate = config.sample_rate;
1806 audio_format_t reqFormat = config.format;
Glenn Kastenf506e942013-08-06 10:49:43 -07001807 audio_channel_mask_t reqChannelMask = config.channel_mask;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001808
Glenn Kasten45faf7e2014-01-17 10:23:01 -08001809 if (pDevices == NULL || *pDevices == AUDIO_DEVICE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001810 return 0;
1811 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001812
Mathias Agopian65ab4712010-07-14 17:59:35 -07001813 Mutex::Autolock _l(mLock);
1814
Glenn Kasten32550952013-08-06 10:45:10 -07001815 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, *pDevices);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001816 if (inHwDev == NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001817 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001818 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001819
John Grossmanee578c02012-07-23 17:05:46 -07001820 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001821 audio_io_handle_t id = nextUniqueId();
1822
Glenn Kasten32550952013-08-06 10:45:10 -07001823 audio_stream_in_t *inStream = NULL;
1824 status_t status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001825 &inStream);
Glenn Kastencac3daa2014-02-07 09:47:14 -08001826 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %#x, Channels %x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001827 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001828 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001829 config.sample_rate,
1830 config.format,
1831 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001832 status);
1833
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001834 // If the input could not be opened with the requested parameters and we can handle the
1835 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1836 // 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 -07001837 if (status == BAD_VALUE &&
1838 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1839 (config.sample_rate <= 2 * reqSamplingRate) &&
Andy Hunge5412692014-05-16 11:25:07 -07001840 (audio_channel_count_from_in_mask(config.channel_mask) <= FCC_2) &&
1841 (audio_channel_count_from_in_mask(reqChannelMask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07001842 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Glenn Kasten254af182012-07-03 14:59:05 -07001843 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001844 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001845 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Glenn Kasten85948432013-08-19 12:09:05 -07001846 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07001847 }
1848
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001849 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001850
Glenn Kasten46909e72013-02-26 09:20:22 -08001851#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001852 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1853 // or (re-)create if current Pipe is idle and does not match the new format
1854 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001855 enum {
1856 TEE_SINK_NO, // don't copy input
1857 TEE_SINK_NEW, // copy input using a new pipe
1858 TEE_SINK_OLD, // copy input using an existing pipe
1859 } kind;
1860 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
Andy Hunge5412692014-05-16 11:25:07 -07001861 audio_channel_count_from_in_mask(
1862 inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001863 if (!mTeeSinkInputEnabled) {
1864 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08001865 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001866 kind = TEE_SINK_NO;
1867 } else if (mRecordTeeSink == 0) {
1868 kind = TEE_SINK_NEW;
1869 } else if (mRecordTeeSink->getStrongCount() != 1) {
1870 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08001871 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001872 kind = TEE_SINK_OLD;
1873 } else {
1874 kind = TEE_SINK_NEW;
1875 }
1876 switch (kind) {
1877 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001878 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001879 size_t numCounterOffers = 0;
1880 const NBAIO_Format offers[1] = {format};
1881 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1882 ALOG_ASSERT(index == 0);
1883 PipeReader *pipeReader = new PipeReader(*pipe);
1884 numCounterOffers = 0;
1885 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1886 ALOG_ASSERT(index == 0);
1887 mRecordTeeSink = pipe;
1888 mRecordTeeSource = pipeReader;
1889 teeSink = pipe;
1890 }
1891 break;
1892 case TEE_SINK_OLD:
1893 teeSink = mRecordTeeSink;
1894 break;
1895 case TEE_SINK_NO:
1896 default:
1897 break;
1898 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001899#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001900
Dima Zavin799a70e2011-04-18 16:57:27 -07001901 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1902
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001903 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001904 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001905 // pre processing modules
Glenn Kasten32550952013-08-06 10:45:10 -07001906 RecordThread *thread = new RecordThread(this,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001907 input,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001908 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001909 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001910 *pDevices
1911#ifdef TEE_SINK
1912 , teeSink
1913#endif
1914 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001915 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001916 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001917 if (pSamplingRate != NULL) {
1918 *pSamplingRate = reqSamplingRate;
1919 }
1920 if (pFormat != NULL) {
1921 *pFormat = config.format;
1922 }
1923 if (pChannelMask != NULL) {
Glenn Kastenf506e942013-08-06 10:49:43 -07001924 *pChannelMask = reqChannelMask;
Glenn Kasten7c027242012-12-26 14:43:16 -08001925 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001926
Mathias Agopian65ab4712010-07-14 17:59:35 -07001927 // notify client processes of the new input creation
Eric Laurent021cf962014-05-13 10:18:14 -07001928 thread->audioConfigChanged(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001929 return id;
1930 }
1931
1932 return 0;
1933}
1934
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001935status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001936{
Glenn Kastend96c5722012-04-25 13:44:49 -07001937 return closeInput_nonvirtual(input);
1938}
1939
1940status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1941{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001942 // keep strong reference on the record thread so that
1943 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001944 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001945 {
1946 Mutex::Autolock _l(mLock);
1947 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001948 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001949 return BAD_VALUE;
1950 }
1951
Steve Block3856b092011-10-20 11:56:00 +01001952 ALOGV("closeInput() %d", input);
Eric Laurent021cf962014-05-13 10:18:14 -07001953 audioConfigChanged(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001954 mRecordThreads.removeItem(input);
1955 }
1956 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001957 // The thread entity (active unit of execution) is no longer running here,
1958 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001959
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001960 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001961 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001962 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001963 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001964 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001965
1966 return NO_ERROR;
1967}
1968
Glenn Kastend2304db2014-02-03 07:40:31 -08001969status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001970{
1971 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08001972 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001973
1974 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1975 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001976 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001977 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001978
1979 return NO_ERROR;
1980}
1981
1982
1983int AudioFlinger::newAudioSessionId()
1984{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001985 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001986}
1987
Marco Nelissend457c972014-02-11 08:47:07 -08001988void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001989{
1990 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001991 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08001992 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
1993 if (pid != -1 && (caller == getpid_cached)) {
1994 caller = pid;
1995 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07001996
Eric Laurent021cf962014-05-13 10:18:14 -07001997 {
1998 Mutex::Autolock _cl(mClientLock);
1999 // Ignore requests received from processes not known as notification client. The request
2000 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2001 // called from a different pid leaving a stale session reference. Also we don't know how
2002 // to clear this reference if the client process dies.
2003 if (mNotificationClients.indexOfKey(caller) < 0) {
2004 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2005 return;
2006 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002007 }
2008
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002009 size_t num = mAudioSessionRefs.size();
2010 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002011 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002012 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2013 ref->mCnt++;
2014 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002015 return;
2016 }
2017 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002018 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2019 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002020}
2021
Marco Nelissend457c972014-02-11 08:47:07 -08002022void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002023{
2024 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002025 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002026 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2027 if (pid != -1 && (caller == getpid_cached)) {
2028 caller = pid;
2029 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002030 size_t num = mAudioSessionRefs.size();
2031 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002032 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002033 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2034 ref->mCnt--;
2035 ALOGV(" decremented refcount to %d", ref->mCnt);
2036 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002037 mAudioSessionRefs.removeAt(i);
2038 delete ref;
2039 purgeStaleEffects_l();
2040 }
2041 return;
2042 }
2043 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002044 // If the caller is mediaserver it is likely that the session being released was acquired
2045 // on behalf of a process not in notification clients and we ignore the warning.
2046 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002047}
2048
2049void AudioFlinger::purgeStaleEffects_l() {
2050
Steve Block3856b092011-10-20 11:56:00 +01002051 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002052
2053 Vector< sp<EffectChain> > chains;
2054
2055 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2056 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2057 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2058 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002059 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2060 chains.push(ec);
2061 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002062 }
2063 }
2064 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2065 sp<RecordThread> t = mRecordThreads.valueAt(i);
2066 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2067 sp<EffectChain> ec = t->mEffectChains[j];
2068 chains.push(ec);
2069 }
2070 }
2071
2072 for (size_t i = 0; i < chains.size(); i++) {
2073 sp<EffectChain> ec = chains[i];
2074 int sessionid = ec->sessionId();
2075 sp<ThreadBase> t = ec->mThread.promote();
2076 if (t == 0) {
2077 continue;
2078 }
2079 size_t numsessionrefs = mAudioSessionRefs.size();
2080 bool found = false;
2081 for (size_t k = 0; k < numsessionrefs; k++) {
2082 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002083 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002084 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002085 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002086 found = true;
2087 break;
2088 }
2089 }
2090 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002091 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002092 // remove all effects from the chain
2093 while (ec->mEffects.size()) {
2094 sp<EffectModule> effect = ec->mEffects[0];
2095 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002096 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002097 if (effect->purgeHandles()) {
2098 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002099 }
2100 AudioSystem::unregisterEffect(effect->id());
2101 }
2102 }
2103 }
2104 return;
2105}
2106
Mathias Agopian65ab4712010-07-14 17:59:35 -07002107// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002108AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002109{
Glenn Kastena1117922012-01-26 10:53:32 -08002110 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002111}
2112
2113// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002114AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002115{
2116 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002117 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002118}
2119
2120// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002121AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002122{
Glenn Kastena1117922012-01-26 10:53:32 -08002123 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002124}
2125
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002126uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002127{
Glenn Kastenbcefec32014-01-17 12:09:05 -08002128 return (uint32_t) android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002129}
2130
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002131AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002132{
2133 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2134 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002135 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002136 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002137 return thread;
2138 }
2139 }
2140 return NULL;
2141}
2142
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002143audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002144{
2145 PlaybackThread *thread = primaryPlaybackThread_l();
2146
2147 if (thread == NULL) {
2148 return 0;
2149 }
2150
Eric Laurentf1c04f92012-08-28 14:26:53 -07002151 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002152}
2153
Eric Laurenta011e352012-03-29 15:51:43 -07002154sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2155 int triggerSession,
2156 int listenerSession,
2157 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002158 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002159{
2160 Mutex::Autolock _l(mLock);
2161
2162 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2163 status_t playStatus = NAME_NOT_FOUND;
2164 status_t recStatus = NAME_NOT_FOUND;
2165 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2166 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2167 if (playStatus == NO_ERROR) {
2168 return event;
2169 }
2170 }
2171 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2172 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2173 if (recStatus == NO_ERROR) {
2174 return event;
2175 }
2176 }
2177 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2178 mPendingSyncEvents.add(event);
2179 } else {
2180 ALOGV("createSyncEvent() invalid event %d", event->type());
2181 event.clear();
2182 }
2183 return event;
2184}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002185
Mathias Agopian65ab4712010-07-14 17:59:35 -07002186// ----------------------------------------------------------------------------
2187// Effect management
2188// ----------------------------------------------------------------------------
2189
2190
Glenn Kastenf587ba52012-01-26 16:25:10 -08002191status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002192{
2193 Mutex::Autolock _l(mLock);
2194 return EffectQueryNumberEffects(numEffects);
2195}
2196
Glenn Kastenf587ba52012-01-26 16:25:10 -08002197status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002198{
2199 Mutex::Autolock _l(mLock);
2200 return EffectQueryEffect(index, descriptor);
2201}
2202
Glenn Kasten5e92a782012-01-30 07:40:52 -08002203status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002204 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002205{
2206 Mutex::Autolock _l(mLock);
2207 return EffectGetDescriptor(pUuid, descriptor);
2208}
2209
2210
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002211sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002212 effect_descriptor_t *pDesc,
2213 const sp<IEffectClient>& effectClient,
2214 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002215 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002216 int sessionId,
2217 status_t *status,
2218 int *id,
2219 int *enabled)
2220{
2221 status_t lStatus = NO_ERROR;
2222 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002223 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002224
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002225 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002226 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002227 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002228
2229 if (pDesc == NULL) {
2230 lStatus = BAD_VALUE;
2231 goto Exit;
2232 }
2233
Eric Laurent84e9a102010-09-23 16:10:16 -07002234 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002235 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002236 lStatus = PERMISSION_DENIED;
2237 goto Exit;
2238 }
2239
Dima Zavinfce7a472011-04-19 22:30:36 -07002240 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002241 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002242 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002243 lStatus = PERMISSION_DENIED;
2244 goto Exit;
2245 }
2246
Mathias Agopian65ab4712010-07-14 17:59:35 -07002247 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002248 if (!EffectIsNullUuid(&pDesc->uuid)) {
2249 // if uuid is specified, request effect descriptor
2250 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2251 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002252 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002253 goto Exit;
2254 }
2255 } else {
2256 // if uuid is not specified, look for an available implementation
2257 // of the required type in effect factory
2258 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002259 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002260 lStatus = BAD_VALUE;
2261 goto Exit;
2262 }
2263 uint32_t numEffects = 0;
2264 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002265 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002266 bool found = false;
2267
2268 lStatus = EffectQueryNumberEffects(&numEffects);
2269 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002270 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002271 goto Exit;
2272 }
2273 for (uint32_t i = 0; i < numEffects; i++) {
2274 lStatus = EffectQueryEffect(i, &desc);
2275 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002276 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002277 continue;
2278 }
2279 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2280 // If matching type found save effect descriptor. If the session is
2281 // 0 and the effect is not auxiliary, continue enumeration in case
2282 // an auxiliary version of this effect type is available
2283 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002284 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002285 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002286 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2287 break;
2288 }
2289 }
2290 }
2291 if (!found) {
2292 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002293 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002294 goto Exit;
2295 }
2296 // For same effect type, chose auxiliary version over insert version if
2297 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002298 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002299 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002300 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002301 }
2302 }
2303
2304 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002305 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002306 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2307 lStatus = INVALID_OPERATION;
2308 goto Exit;
2309 }
2310
Eric Laurent59255e42011-07-27 19:49:51 -07002311 // check recording permission for visualizer
2312 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2313 !recordingAllowed()) {
2314 lStatus = PERMISSION_DENIED;
2315 goto Exit;
2316 }
2317
Mathias Agopian65ab4712010-07-14 17:59:35 -07002318 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002319 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002320 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002321 // if the output returned by getOutputForEffect() is removed before we lock the
2322 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2323 // and we will exit safely
2324 io = AudioSystem::getOutputForEffect(&desc);
2325 ALOGV("createEffect got output %d", io);
2326 }
2327
2328 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002329
2330 // If output is not specified try to find a matching audio session ID in one of the
2331 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002332 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2333 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002334 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002335 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002336 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2337 // output must be specified by AudioPolicyManager when using session
2338 // AUDIO_SESSION_OUTPUT_STAGE
2339 lStatus = BAD_VALUE;
2340 goto Exit;
2341 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002342 // look for the thread where the specified audio session is present
2343 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2344 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2345 io = mPlaybackThreads.keyAt(i);
2346 break;
2347 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002348 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002349 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002350 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2351 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2352 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002353 break;
2354 }
2355 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002356 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002357 // If no output thread contains the requested session ID, default to
2358 // first output. The effect chain will be moved to the correct output
2359 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002360 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002361 io = mPlaybackThreads.keyAt(0);
2362 }
Steve Block3856b092011-10-20 11:56:00 +01002363 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002364 }
2365 ThreadBase *thread = checkRecordThread_l(io);
2366 if (thread == NULL) {
2367 thread = checkPlaybackThread_l(io);
2368 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002369 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002370 lStatus = BAD_VALUE;
2371 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002372 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002373 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002374
Eric Laurent021cf962014-05-13 10:18:14 -07002375 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002376
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002377 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002378 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2379 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002380 if (handle != 0 && id != NULL) {
2381 *id = handle->id();
2382 }
2383 }
2384
2385Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002386 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002387 return handle;
2388}
2389
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002390status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2391 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002392{
Steve Block3856b092011-10-20 11:56:00 +01002393 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002394 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002395 Mutex::Autolock _l(mLock);
2396 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002397 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002398 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002399 }
Eric Laurentde070132010-07-13 04:45:46 -07002400 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2401 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002402 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002403 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002404 }
Eric Laurentde070132010-07-13 04:45:46 -07002405 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2406 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002407 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002408 return BAD_VALUE;
2409 }
2410
2411 Mutex::Autolock _dl(dstThread->mLock);
2412 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002413 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002414}
2415
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002416// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002417status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002418 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002419 AudioFlinger::PlaybackThread *dstThread,
2420 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002421{
Steve Block3856b092011-10-20 11:56:00 +01002422 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002423 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002424
Eric Laurent59255e42011-07-27 19:49:51 -07002425 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002426 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002427 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002428 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002429 return INVALID_OPERATION;
2430 }
2431
Eric Laurent39e94f82010-07-28 01:32:47 -07002432 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002433 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002434 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002435 // removed.
2436 srcThread->removeEffectChain_l(chain);
2437
2438 // transfer all effects one by one so that new effect chain is created on new thread with
2439 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002440 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002441 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002442 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002443 Vector< sp<EffectModule> > removed;
2444 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002445 while (effect != 0) {
2446 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002447 removed.add(effect);
2448 status = dstThread->addEffect_l(effect);
2449 if (status != NO_ERROR) {
2450 break;
2451 }
Eric Laurentec35a142011-10-05 17:42:25 -07002452 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2453 if (effect->state() == EffectModule::ACTIVE ||
2454 effect->state() == EffectModule::STOPPING) {
2455 effect->start();
2456 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002457 // if the move request is not received from audio policy manager, the effect must be
2458 // re-registered with the new strategy and output
2459 if (dstChain == 0) {
2460 dstChain = effect->chain().promote();
2461 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002462 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002463 status = NO_INIT;
2464 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002465 }
2466 strategy = dstChain->strategy();
2467 }
2468 if (reRegister) {
2469 AudioSystem::unregisterEffect(effect->id());
2470 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002471 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002472 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002473 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002474 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002475 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002476 }
Eric Laurentde070132010-07-13 04:45:46 -07002477 effect = chain->getEffectFromId_l(0);
2478 }
2479
Eric Laurent5baf2af2013-09-12 17:37:00 -07002480 if (status != NO_ERROR) {
2481 for (size_t i = 0; i < removed.size(); i++) {
2482 srcThread->addEffect_l(removed[i]);
2483 if (dstChain != 0 && reRegister) {
2484 AudioSystem::unregisterEffect(removed[i]->id());
2485 AudioSystem::registerEffect(&removed[i]->desc(),
2486 srcThread->id(),
2487 strategy,
2488 sessionId,
2489 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002490 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002491 }
2492 }
2493 }
2494
2495 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002496}
2497
Eric Laurent5baf2af2013-09-12 17:37:00 -07002498bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002499{
2500 if (mGlobalEffectEnableTime != 0 &&
2501 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2502 return true;
2503 }
2504
2505 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2506 sp<EffectChain> ec =
2507 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002508 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002509 return true;
2510 }
2511 }
2512 return false;
2513}
2514
Eric Laurent5baf2af2013-09-12 17:37:00 -07002515void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002516{
2517 Mutex::Autolock _l(mLock);
2518
2519 mGlobalEffectEnableTime = systemTime();
2520
2521 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2522 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2523 if (t->mType == ThreadBase::OFFLOAD) {
2524 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2525 }
2526 }
2527
2528}
2529
Glenn Kastenda6ef132013-01-10 12:31:01 -08002530struct Entry {
2531#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2532 char mName[MAX_NAME];
2533};
2534
2535int comparEntry(const void *p1, const void *p2)
2536{
2537 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2538}
2539
Glenn Kasten46909e72013-02-26 09:20:22 -08002540#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002541void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002542{
Eric Laurent81784c32012-11-19 14:55:58 -08002543 NBAIO_Source *teeSource = source.get();
2544 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002545 // .wav rotation
2546 // There is a benign race condition if 2 threads call this simultaneously.
2547 // They would both traverse the directory, but the result would simply be
2548 // failures at unlink() which are ignored. It's also unlikely since
2549 // normally dumpsys is only done by bugreport or from the command line.
2550 char teePath[32+256];
2551 strcpy(teePath, "/data/misc/media");
2552 size_t teePathLen = strlen(teePath);
2553 DIR *dir = opendir(teePath);
2554 teePath[teePathLen++] = '/';
2555 if (dir != NULL) {
2556#define MAX_SORT 20 // number of entries to sort
2557#define MAX_KEEP 10 // number of entries to keep
2558 struct Entry entries[MAX_SORT];
2559 size_t entryCount = 0;
2560 while (entryCount < MAX_SORT) {
2561 struct dirent de;
2562 struct dirent *result = NULL;
2563 int rc = readdir_r(dir, &de, &result);
2564 if (rc != 0) {
2565 ALOGW("readdir_r failed %d", rc);
2566 break;
2567 }
2568 if (result == NULL) {
2569 break;
2570 }
2571 if (result != &de) {
2572 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2573 break;
2574 }
2575 // ignore non .wav file entries
2576 size_t nameLen = strlen(de.d_name);
2577 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2578 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2579 continue;
2580 }
2581 strcpy(entries[entryCount++].mName, de.d_name);
2582 }
2583 (void) closedir(dir);
2584 if (entryCount > MAX_KEEP) {
2585 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2586 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2587 strcpy(&teePath[teePathLen], entries[i].mName);
2588 (void) unlink(teePath);
2589 }
2590 }
2591 } else {
2592 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002593 dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002594 }
2595 }
Eric Laurent81784c32012-11-19 14:55:58 -08002596 char teeTime[16];
2597 struct timeval tv;
2598 gettimeofday(&tv, NULL);
2599 struct tm tm;
2600 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002601 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2602 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2603 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2604 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002605 if (teeFd >= 0) {
2606 char wavHeader[44];
2607 memcpy(wavHeader,
2608 "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",
2609 sizeof(wavHeader));
2610 NBAIO_Format format = teeSource->format();
2611 unsigned channelCount = Format_channelCount(format);
2612 ALOG_ASSERT(channelCount <= FCC_2);
2613 uint32_t sampleRate = Format_sampleRate(format);
2614 wavHeader[22] = channelCount; // number of channels
2615 wavHeader[24] = sampleRate; // sample rate
2616 wavHeader[25] = sampleRate >> 8;
2617 wavHeader[32] = channelCount * 2; // block alignment
2618 write(teeFd, wavHeader, sizeof(wavHeader));
2619 size_t total = 0;
2620 bool firstRead = true;
2621 for (;;) {
2622#define TEE_SINK_READ 1024
2623 short buffer[TEE_SINK_READ * FCC_2];
2624 size_t count = TEE_SINK_READ;
2625 ssize_t actual = teeSource->read(buffer, count,
2626 AudioBufferProvider::kInvalidPTS);
2627 bool wasFirstRead = firstRead;
2628 firstRead = false;
2629 if (actual <= 0) {
2630 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2631 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002632 }
Eric Laurent81784c32012-11-19 14:55:58 -08002633 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002634 }
Eric Laurent81784c32012-11-19 14:55:58 -08002635 ALOG_ASSERT(actual <= (ssize_t)count);
2636 write(teeFd, buffer, actual * channelCount * sizeof(short));
2637 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002638 }
Eric Laurent81784c32012-11-19 14:55:58 -08002639 lseek(teeFd, (off_t) 4, SEEK_SET);
2640 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2641 write(teeFd, &temp, sizeof(temp));
2642 lseek(teeFd, (off_t) 40, SEEK_SET);
2643 temp = total * channelCount * sizeof(short);
2644 write(teeFd, &temp, sizeof(temp));
2645 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002646 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002647 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002648 }
Eric Laurent59255e42011-07-27 19:49:51 -07002649 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002650 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002651 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002652 }
Eric Laurent59255e42011-07-27 19:49:51 -07002653 }
2654 }
2655}
Glenn Kasten46909e72013-02-26 09:20:22 -08002656#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002657
Mathias Agopian65ab4712010-07-14 17:59:35 -07002658// ----------------------------------------------------------------------------
2659
2660status_t AudioFlinger::onTransact(
2661 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2662{
2663 return BnAudioFlinger::onTransact(code, data, reply, flags);
2664}
2665
Mathias Agopian65ab4712010-07-14 17:59:35 -07002666}; // namespace android