blob: 1843722524c5157aa4f444879b331ea5dea21c9c [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) {
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530109 switch (format & AUDIO_FORMAT_MAIN_MASK) {
110 case AUDIO_FORMAT_PCM:
111 switch (format) {
112 case AUDIO_FORMAT_PCM_16_BIT: return "pcm16";
113 case AUDIO_FORMAT_PCM_8_BIT: return "pcm8";
114 case AUDIO_FORMAT_PCM_32_BIT: return "pcm32";
115 case AUDIO_FORMAT_PCM_8_24_BIT: return "pcm8.24";
116 case AUDIO_FORMAT_PCM_FLOAT: return "pcmfloat";
117 case AUDIO_FORMAT_PCM_24_BIT_PACKED: return "pcm24";
118 default:
119 break;
120 }
121 break;
Marco Nelissenb2208842014-02-07 14:00:50 -0800122 case AUDIO_FORMAT_MP3: return "mp3";
123 case AUDIO_FORMAT_AMR_NB: return "amr-nb";
124 case AUDIO_FORMAT_AMR_WB: return "amr-wb";
125 case AUDIO_FORMAT_AAC: return "aac";
126 case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1";
127 case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2";
128 case AUDIO_FORMAT_VORBIS: return "vorbis";
aarti jadhav-gaikwad2829edc2014-06-18 15:25:26 +0530129 case AUDIO_FORMAT_OPUS: return "opus";
130 case AUDIO_FORMAT_AC3: return "ac-3";
131 case AUDIO_FORMAT_E_AC3: return "e-ac-3";
Marco Nelissenb2208842014-02-07 14:00:50 -0800132 default:
133 break;
134 }
135 return "unknown";
136}
137
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700138static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700139{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700140 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700141 int rc;
142
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700143 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
144 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
145 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
146 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700147 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700148 }
149 rc = audio_hw_device_open(mod, dev);
150 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
151 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
152 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700153 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700154 }
Eric Laurent5806b352014-05-22 12:23:26 -0700155 if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700156 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
157 rc = BAD_VALUE;
158 goto out;
159 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700160 return 0;
161
162out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700163 *dev = NULL;
164 return rc;
165}
166
Mathias Agopian65ab4712010-07-14 17:59:35 -0700167// ----------------------------------------------------------------------------
168
169AudioFlinger::AudioFlinger()
170 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800171 mPrimaryHardwareDev(NULL),
Glenn Kasten9ea65d02014-01-17 10:21:24 -0800172 mAudioHwDevs(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700173 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800174 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800175 mMasterMute(false),
176 mNextUniqueId(1),
177 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700178 mBtNrecIsOff(false),
179 mIsLowRamDevice(true),
Eric Laurent813e2a72013-08-31 12:59:48 -0700180 mIsDeviceTypeKnown(false),
Glenn Kasten4ea00a22014-06-02 08:29:22 -0700181 mGlobalEffectEnableTime(0),
182 mPrimaryOutputSampleRate(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700183{
Glenn Kasten949a9262013-04-16 12:35:20 -0700184 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800185 char value[PROPERTY_VALUE_MAX];
186 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
187 if (doLog) {
Glenn Kastencf515ab2014-03-14 16:01:58 -0700188 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters", MemoryHeapBase::READ_ONLY);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800189 }
Eric Laurent1c333e22014-05-20 10:48:17 -0700190
Glenn Kasten46909e72013-02-26 09:20:22 -0800191#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800192 (void) property_get("ro.debuggable", value, "0");
193 int debuggable = atoi(value);
194 int teeEnabled = 0;
195 if (debuggable) {
196 (void) property_get("af.tee", value, "0");
197 teeEnabled = atoi(value);
198 }
Glenn Kastenf66b4222014-02-20 10:23:28 -0800199 // FIXME symbolic constants here
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700200 if (teeEnabled & 1) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800201 mTeeSinkInputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700202 }
203 if (teeEnabled & 2) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800204 mTeeSinkOutputEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700205 }
206 if (teeEnabled & 4) {
Glenn Kastenda6ef132013-01-10 12:31:01 -0800207 mTeeSinkTrackEnabled = true;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700208 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800209#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700210}
211
212void AudioFlinger::onFirstRef()
213{
Dima Zavin799a70e2011-04-18 16:57:27 -0700214 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700215
Eric Laurent93575202011-01-18 18:39:02 -0800216 Mutex::Autolock _l(mLock);
217
Dima Zavin799a70e2011-04-18 16:57:27 -0700218 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800219 char val_str[PROPERTY_VALUE_MAX] = { 0 };
220 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
221 uint32_t int_val;
222 if (1 == sscanf(val_str, "%u", &int_val)) {
223 mStandbyTimeInNsecs = milliseconds(int_val);
224 ALOGI("Using %u mSec as standby time.", int_val);
225 } else {
226 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
227 ALOGI("Using default %u mSec as standby time.",
228 (uint32_t)(mStandbyTimeInNsecs / 1000000));
229 }
230 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700231
Eric Laurent1c333e22014-05-20 10:48:17 -0700232 mPatchPanel = new PatchPanel(this);
233
Eric Laurenta4c5a552012-03-29 10:12:40 -0700234 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700235}
236
237AudioFlinger::~AudioFlinger()
238{
239 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700240 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700241 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242 }
243 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700244 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700245 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700246 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700247
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800248 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
249 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700250 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
251 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700252 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700253
254 // Tell media.log service about any old writers that still need to be unregistered
255 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
256 if (binder != 0) {
257 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
258 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
259 sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory());
260 mUnregisteredWriters.pop();
261 mediaLogService->unregisterWriter(iMemory);
262 }
263 }
264
Mathias Agopian65ab4712010-07-14 17:59:35 -0700265}
266
Eric Laurenta4c5a552012-03-29 10:12:40 -0700267static const char * const audio_interfaces[] = {
268 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
269 AUDIO_HARDWARE_MODULE_ID_A2DP,
270 AUDIO_HARDWARE_MODULE_ID_USB,
271};
272#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
273
John Grossmanee578c02012-07-23 17:05:46 -0700274AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
275 audio_module_handle_t module,
276 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700277{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700278 // if module is 0, the request comes from an old policy manager and we should load
279 // well known modules
280 if (module == 0) {
281 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
282 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
283 loadHwModule_l(audio_interfaces[i]);
284 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700285 // then try to find a module supporting the requested device.
286 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
287 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
288 audio_hw_device_t *dev = audioHwDevice->hwDevice();
289 if ((dev->get_supported_devices != NULL) &&
290 (dev->get_supported_devices(dev) & devices) == devices)
291 return audioHwDevice;
292 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700293 } else {
294 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700295 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
296 if (audioHwDevice != NULL) {
297 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700298 }
299 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700300
Dima Zavin799a70e2011-04-18 16:57:27 -0700301 return NULL;
302}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700303
Glenn Kasten0f11b512014-01-31 16:18:54 -0800304void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305{
306 const size_t SIZE = 256;
307 char buffer[SIZE];
308 String8 result;
309
310 result.append("Clients:\n");
311 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800312 sp<Client> client = mClients.valueAt(i).promote();
313 if (client != 0) {
314 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
315 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316 }
317 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700318
Eric Laurentd1b28d42013-09-18 18:47:13 -0700319 result.append("Notification Clients:\n");
320 for (size_t i = 0; i < mNotificationClients.size(); ++i) {
321 snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i));
322 result.append(buffer);
323 }
324
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700325 result.append("Global session refs:\n");
Marco Nelissenb2208842014-02-07 14:00:50 -0800326 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700327 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
328 AudioSessionRef *r = mAudioSessionRefs[i];
Marco Nelissenb2208842014-02-07 14:00:50 -0800329 snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700330 result.append(buffer);
331 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700332 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700333}
334
335
Glenn Kasten0f11b512014-01-31 16:18:54 -0800336void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700337{
338 const size_t SIZE = 256;
339 char buffer[SIZE];
340 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800341 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700342
John Grossman4ff14ba2012-02-08 16:37:41 -0800343 snprintf(buffer, SIZE, "Hardware status: %d\n"
344 "Standby Time mSec: %u\n",
345 hardwareStatus,
346 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700347 result.append(buffer);
348 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349}
350
Glenn Kasten0f11b512014-01-31 16:18:54 -0800351void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700352{
353 const size_t SIZE = 256;
354 char buffer[SIZE];
355 String8 result;
356 snprintf(buffer, SIZE, "Permission Denial: "
357 "can't dump AudioFlinger from pid=%d, uid=%d\n",
358 IPCThreadState::self()->getCallingPid(),
359 IPCThreadState::self()->getCallingUid());
360 result.append(buffer);
361 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362}
363
Eric Laurent81784c32012-11-19 14:55:58 -0800364bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365{
366 bool locked = false;
367 for (int i = 0; i < kDumpLockRetries; ++i) {
368 if (mutex.tryLock() == NO_ERROR) {
369 locked = true;
370 break;
371 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800372 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373 }
374 return locked;
375}
376
377status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
378{
Glenn Kasten44deb052012-02-05 18:09:08 -0800379 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700380 dumpPermissionDenial(fd, args);
381 } else {
382 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800383 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384 if (!hardwareLocked) {
385 String8 result(kHardwareLockedString);
386 write(fd, result.string(), result.size());
387 } else {
388 mHardwareLock.unlock();
389 }
390
Eric Laurent81784c32012-11-19 14:55:58 -0800391 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392
393 // failed to lock - AudioFlinger is probably deadlocked
394 if (!locked) {
395 String8 result(kDeadlockedString);
396 write(fd, result.string(), result.size());
397 }
398
Eric Laurent021cf962014-05-13 10:18:14 -0700399 bool clientLocked = dumpTryLock(mClientLock);
400 if (!clientLocked) {
401 String8 result(kClientLockedString);
402 write(fd, result.string(), result.size());
403 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700404 dumpClients(fd, args);
Eric Laurent021cf962014-05-13 10:18:14 -0700405 if (clientLocked) {
406 mClientLock.unlock();
407 }
408
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409 dumpInternals(fd, args);
410
411 // dump playback threads
412 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
413 mPlaybackThreads.valueAt(i)->dump(fd, args);
414 }
415
416 // dump record threads
417 for (size_t i = 0; i < mRecordThreads.size(); i++) {
418 mRecordThreads.valueAt(i)->dump(fd, args);
419 }
420
Dima Zavin799a70e2011-04-18 16:57:27 -0700421 // dump all hardware devs
422 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700423 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700424 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700425 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700426
Glenn Kasten46909e72013-02-26 09:20:22 -0800427#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700428 // dump the serially shared record tee sink
429 if (mRecordTeeSource != 0) {
430 dumpTee(fd, mRecordTeeSource);
431 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800432#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700433
Glenn Kastend65d73c2012-06-22 17:21:07 -0700434 if (locked) {
435 mLock.unlock();
436 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800437
438 // append a copy of media.log here by forwarding fd to it, but don't attempt
439 // to lookup the service if it's not running, as it will block for a second
440 if (mLogMemoryDealer != 0) {
441 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
442 if (binder != 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -0700443 dprintf(fd, "\nmedia.log:\n");
Glenn Kasten9e58b552013-01-18 15:09:48 -0800444 Vector<String16> args;
445 binder->dump(fd, args);
446 }
447 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700448 }
449 return NO_ERROR;
450}
451
Eric Laurent021cf962014-05-13 10:18:14 -0700452sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid)
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800453{
Eric Laurent021cf962014-05-13 10:18:14 -0700454 Mutex::Autolock _cl(mClientLock);
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800455 // If pid is already in the mClients wp<> map, then use that entry
456 // (for which promote() is always != 0), otherwise create a new entry and Client.
457 sp<Client> client = mClients.valueFor(pid).promote();
458 if (client == 0) {
459 client = new Client(this, pid);
460 mClients.add(pid, client);
461 }
462
463 return client;
464}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700465
Glenn Kasten9e58b552013-01-18 15:09:48 -0800466sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
467{
Glenn Kasten481fb672013-09-30 14:39:28 -0700468 // If there is no memory allocated for logs, return a dummy writer that does nothing
Glenn Kasten9e58b552013-01-18 15:09:48 -0800469 if (mLogMemoryDealer == 0) {
470 return new NBLog::Writer();
471 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800472 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
Glenn Kasten481fb672013-09-30 14:39:28 -0700473 // Similarly if we can't contact the media.log service, also return a dummy writer
474 if (binder == 0) {
475 return new NBLog::Writer();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800476 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700477 sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder));
478 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
479 // If allocation fails, consult the vector of previously unregistered writers
480 // and garbage-collect one or more them until an allocation succeeds
481 if (shared == 0) {
482 Mutex::Autolock _l(mUnregisteredWritersLock);
483 for (size_t count = mUnregisteredWriters.size(); count > 0; count--) {
484 {
485 // Pick the oldest stale writer to garbage-collect
486 sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory());
487 mUnregisteredWriters.removeAt(0);
488 mediaLogService->unregisterWriter(iMemory);
489 // Now the media.log remote reference to IMemory is gone. When our last local
490 // reference to IMemory also drops to zero at end of this block,
491 // the IMemory destructor will deallocate the region from mLogMemoryDealer.
492 }
493 // Re-attempt the allocation
494 shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
495 if (shared != 0) {
496 goto success;
497 }
498 }
499 // Even after garbage-collecting all old writers, there is still not enough memory,
500 // so return a dummy writer
501 return new NBLog::Writer();
502 }
503success:
504 mediaLogService->registerWriter(shared, size, name);
505 return new NBLog::Writer(size, shared);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800506}
507
508void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
509{
Glenn Kasten685ef092013-02-04 08:15:34 -0800510 if (writer == 0) {
511 return;
512 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800513 sp<IMemory> iMemory(writer->getIMemory());
514 if (iMemory == 0) {
515 return;
516 }
Glenn Kasten481fb672013-09-30 14:39:28 -0700517 // Rather than removing the writer immediately, append it to a queue of old writers to
518 // be garbage-collected later. This allows us to continue to view old logs for a while.
519 Mutex::Autolock _l(mUnregisteredWritersLock);
520 mUnregisteredWriters.push(writer);
Glenn Kasten9e58b552013-01-18 15:09:48 -0800521}
522
Mathias Agopian65ab4712010-07-14 17:59:35 -0700523// IAudioFlinger interface
524
525
526sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800527 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700528 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800529 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700530 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800531 size_t *frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800532 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700533 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800534 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800535 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700536 int *sessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800537 int clientUid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700538 status_t *status)
539{
540 sp<PlaybackThread::Track> track;
541 sp<TrackHandle> trackHandle;
542 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543 status_t lStatus;
544 int lSessionId;
545
Glenn Kasten263709e2012-01-06 08:40:01 -0800546 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
547 // but if someone uses binder directly they could bypass that and cause us to crash
548 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000549 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550 lStatus = BAD_VALUE;
551 goto Exit;
552 }
553
Glenn Kasten53b5d092014-02-05 10:00:23 -0800554 // further sample rate checks are performed by createTrack_l() depending on the thread type
555 if (sampleRate == 0) {
556 ALOGE("createTrack() invalid sample rate %u", sampleRate);
557 lStatus = BAD_VALUE;
558 goto Exit;
559 }
560
561 // further channel mask checks are performed by createTrack_l() depending on the thread type
562 if (!audio_is_output_channel(channelMask)) {
563 ALOGE("createTrack() invalid channel mask %#x", channelMask);
564 lStatus = BAD_VALUE;
565 goto Exit;
566 }
567
Glenn Kastenc4b88a82014-04-30 16:54:30 -0700568 // further format checks are performed by createTrack_l() depending on the thread type
569 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800570 ALOGE("createTrack() invalid format %#x", format);
Glenn Kasten60a83922012-06-21 12:56:37 -0700571 lStatus = BAD_VALUE;
572 goto Exit;
573 }
574
Glenn Kasten663c2242013-09-24 11:52:37 -0700575 if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) {
576 ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()");
577 lStatus = BAD_VALUE;
578 goto Exit;
579 }
580
Mathias Agopian65ab4712010-07-14 17:59:35 -0700581 {
582 Mutex::Autolock _l(mLock);
583 PlaybackThread *thread = checkPlaybackThread_l(output);
584 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800585 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700586 lStatus = BAD_VALUE;
587 goto Exit;
588 }
589
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800590 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -0700591 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700592
Glenn Kastene848bd92014-03-13 15:00:32 -0700593 PlaybackThread *effectThread = NULL;
Glenn Kastenaea7ea02013-06-26 09:25:47 -0700594 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700595 lSessionId = *sessionId;
Eric Laurentf436fdc2012-05-24 11:07:14 -0700596 // check if an effect chain with the same session ID is present on another
597 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700598 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700599 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
600 if (mPlaybackThreads.keyAt(i) != output) {
Glenn Kasten570f6332014-03-13 15:01:06 -0700601 uint32_t sessions = t->hasAudioSession(lSessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700602 if (sessions & PlaybackThread::EFFECT_SESSION) {
603 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700604 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700605 }
Eric Laurentde070132010-07-13 04:45:46 -0700606 }
607 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700608 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700609 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700610 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700611 if (sessionId != NULL) {
612 *sessionId = lSessionId;
613 }
614 }
Steve Block3856b092011-10-20 11:56:00 +0100615 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700616
617 track = thread->createTrack_l(client, streamType, sampleRate, format,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800618 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -0800619 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0));
Glenn Kasten2fc14732013-08-05 14:58:14 -0700620 // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless
Eric Laurent39e94f82010-07-28 01:32:47 -0700621
622 // move effect chain to this output thread if an effect on same session was waiting
623 // for a track to be created
624 if (lStatus == NO_ERROR && effectThread != NULL) {
Glenn Kasten2fc14732013-08-05 14:58:14 -0700625 // no risk of deadlock because AudioFlinger::mLock is held
Eric Laurent39e94f82010-07-28 01:32:47 -0700626 Mutex::Autolock _dl(thread->mLock);
627 Mutex::Autolock _sl(effectThread->mLock);
628 moveEffectChain_l(lSessionId, effectThread, thread, true);
629 }
Eric Laurenta011e352012-03-29 15:51:43 -0700630
631 // Look for sync events awaiting for a session to be used.
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700632 for (size_t i = 0; i < mPendingSyncEvents.size(); i++) {
Eric Laurenta011e352012-03-29 15:51:43 -0700633 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
634 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700635 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700636 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700637 } else {
638 mPendingSyncEvents[i]->cancel();
639 }
Eric Laurenta011e352012-03-29 15:51:43 -0700640 mPendingSyncEvents.removeAt(i);
641 i--;
642 }
643 }
644 }
Glenn Kastene198c362013-08-13 09:13:36 -0700645
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646 }
Glenn Kastene198c362013-08-13 09:13:36 -0700647
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700648 if (lStatus != NO_ERROR) {
649 // remove local strong reference to Client before deleting the Track so that the
Eric Laurent021cf962014-05-13 10:18:14 -0700650 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -0700651 // Don't hold mClientLock when releasing the reference on the track as the
652 // destructor will acquire it.
653 {
654 Mutex::Autolock _cl(mClientLock);
655 client.clear();
656 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657 track.clear();
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700658 goto Exit;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700659 }
660
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700661 // return handle to client
662 trackHandle = new TrackHandle(track);
663
Mathias Agopian65ab4712010-07-14 17:59:35 -0700664Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700665 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666 return trackHandle;
667}
668
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800669uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700670{
671 Mutex::Autolock _l(mLock);
672 PlaybackThread *thread = checkPlaybackThread_l(output);
673 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000674 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700675 return 0;
676 }
677 return thread->sampleRate();
678}
679
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800680audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700681{
682 Mutex::Autolock _l(mLock);
683 PlaybackThread *thread = checkPlaybackThread_l(output);
684 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000685 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800686 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700687 }
688 return thread->format();
689}
690
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800691size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700692{
693 Mutex::Autolock _l(mLock);
694 PlaybackThread *thread = checkPlaybackThread_l(output);
695 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000696 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700697 return 0;
698 }
Glenn Kasten58912562012-04-03 10:45:00 -0700699 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
700 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700701 return thread->frameCount();
702}
703
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800704uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700705{
706 Mutex::Autolock _l(mLock);
707 PlaybackThread *thread = checkPlaybackThread_l(output);
708 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800709 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700710 return 0;
711 }
712 return thread->latency();
713}
714
715status_t AudioFlinger::setMasterVolume(float value)
716{
Eric Laurenta1884f92011-08-23 08:25:03 -0700717 status_t ret = initCheck();
718 if (ret != NO_ERROR) {
719 return ret;
720 }
721
Mathias Agopian65ab4712010-07-14 17:59:35 -0700722 // check calling permissions
723 if (!settingsAllowed()) {
724 return PERMISSION_DENIED;
725 }
726
Eric Laurenta4c5a552012-03-29 10:12:40 -0700727 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700728 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700729
John Grossmanee578c02012-07-23 17:05:46 -0700730 // Set master volume in the HALs which support it.
731 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
732 AutoMutex lock(mHardwareLock);
733 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800734
John Grossmanee578c02012-07-23 17:05:46 -0700735 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
736 if (dev->canSetMasterVolume()) {
737 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800738 }
John Grossmanee578c02012-07-23 17:05:46 -0700739 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700740 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700741
John Grossmanee578c02012-07-23 17:05:46 -0700742 // Now set the master volume in each playback thread. Playback threads
743 // assigned to HALs which do not have master volume support will apply
744 // master volume during the mix operation. Threads with HALs which do
745 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800746 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700747 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700748
749 return NO_ERROR;
750}
751
Glenn Kastenf78aee72012-01-04 11:00:47 -0800752status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700753{
Eric Laurenta1884f92011-08-23 08:25:03 -0700754 status_t ret = initCheck();
755 if (ret != NO_ERROR) {
756 return ret;
757 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700758
759 // check calling permissions
760 if (!settingsAllowed()) {
761 return PERMISSION_DENIED;
762 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800763 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000764 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700765 return BAD_VALUE;
766 }
767
768 { // scope for the lock
769 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700770 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700771 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700772 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700773 mHardwareStatus = AUDIO_HW_IDLE;
774 }
775
776 if (NO_ERROR == ret) {
777 Mutex::Autolock _l(mLock);
778 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800779 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700780 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700781 }
782
783 return ret;
784}
785
786status_t AudioFlinger::setMicMute(bool state)
787{
Eric Laurenta1884f92011-08-23 08:25:03 -0700788 status_t ret = initCheck();
789 if (ret != NO_ERROR) {
790 return ret;
791 }
792
Mathias Agopian65ab4712010-07-14 17:59:35 -0700793 // check calling permissions
794 if (!settingsAllowed()) {
795 return PERMISSION_DENIED;
796 }
797
798 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700799 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700800 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700801 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700802 mHardwareStatus = AUDIO_HW_IDLE;
803 return ret;
804}
805
806bool AudioFlinger::getMicMute() const
807{
Eric Laurenta1884f92011-08-23 08:25:03 -0700808 status_t ret = initCheck();
809 if (ret != NO_ERROR) {
810 return false;
811 }
812
Dima Zavinfce7a472011-04-19 22:30:36 -0700813 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800814 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700815 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700816 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700817 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818 mHardwareStatus = AUDIO_HW_IDLE;
819 return state;
820}
821
822status_t AudioFlinger::setMasterMute(bool muted)
823{
John Grossmand8f178d2012-07-20 14:51:35 -0700824 status_t ret = initCheck();
825 if (ret != NO_ERROR) {
826 return ret;
827 }
828
Mathias Agopian65ab4712010-07-14 17:59:35 -0700829 // check calling permissions
830 if (!settingsAllowed()) {
831 return PERMISSION_DENIED;
832 }
833
John Grossmanee578c02012-07-23 17:05:46 -0700834 Mutex::Autolock _l(mLock);
835 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700836
John Grossmanee578c02012-07-23 17:05:46 -0700837 // Set master mute in the HALs which support it.
838 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
839 AutoMutex lock(mHardwareLock);
840 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700841
John Grossmanee578c02012-07-23 17:05:46 -0700842 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
843 if (dev->canSetMasterMute()) {
844 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700845 }
John Grossmanee578c02012-07-23 17:05:46 -0700846 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700847 }
848
John Grossmanee578c02012-07-23 17:05:46 -0700849 // Now set the master mute in each playback thread. Playback threads
850 // assigned to HALs which do not have master mute support will apply master
851 // mute during the mix operation. Threads with HALs which do support master
852 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800853 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700854 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700855
856 return NO_ERROR;
857}
858
859float AudioFlinger::masterVolume() const
860{
Glenn Kasten98067102011-12-13 11:47:54 -0800861 Mutex::Autolock _l(mLock);
862 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700863}
864
865bool AudioFlinger::masterMute() const
866{
Glenn Kasten98067102011-12-13 11:47:54 -0800867 Mutex::Autolock _l(mLock);
868 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700869}
870
John Grossman4ff14ba2012-02-08 16:37:41 -0800871float AudioFlinger::masterVolume_l() const
872{
John Grossman4ff14ba2012-02-08 16:37:41 -0800873 return mMasterVolume;
874}
875
John Grossmand8f178d2012-07-20 14:51:35 -0700876bool AudioFlinger::masterMute_l() const
877{
John Grossmanee578c02012-07-23 17:05:46 -0700878 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700879}
880
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800881status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
882 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700883{
884 // check calling permissions
885 if (!settingsAllowed()) {
886 return PERMISSION_DENIED;
887 }
888
Glenn Kasten263709e2012-01-06 08:40:01 -0800889 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000890 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700891 return BAD_VALUE;
892 }
893
894 AutoMutex lock(mLock);
895 PlaybackThread *thread = NULL;
Glenn Kasten142f5192014-03-25 17:44:59 -0700896 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700897 thread = checkPlaybackThread_l(output);
898 if (thread == NULL) {
899 return BAD_VALUE;
900 }
901 }
902
903 mStreamTypes[stream].volume = value;
904
905 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800906 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700907 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700908 }
909 } else {
910 thread->setStreamVolume(stream, value);
911 }
912
913 return NO_ERROR;
914}
915
Glenn Kastenfff6d712012-01-12 16:38:12 -0800916status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700917{
918 // check calling permissions
919 if (!settingsAllowed()) {
920 return PERMISSION_DENIED;
921 }
922
Glenn Kasten263709e2012-01-06 08:40:01 -0800923 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700924 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000925 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700926 return BAD_VALUE;
927 }
928
Eric Laurent93575202011-01-18 18:39:02 -0800929 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700930 mStreamTypes[stream].mute = muted;
Mark Salyzyn3ab368e2014-04-15 14:55:53 -0700931 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700932 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700933
934 return NO_ERROR;
935}
936
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800937float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700938{
Glenn Kasten263709e2012-01-06 08:40:01 -0800939 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940 return 0.0f;
941 }
942
943 AutoMutex lock(mLock);
944 float volume;
Glenn Kasten142f5192014-03-25 17:44:59 -0700945 if (output != AUDIO_IO_HANDLE_NONE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946 PlaybackThread *thread = checkPlaybackThread_l(output);
947 if (thread == NULL) {
948 return 0.0f;
949 }
950 volume = thread->streamVolume(stream);
951 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800952 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700953 }
954
955 return volume;
956}
957
Glenn Kastenfff6d712012-01-12 16:38:12 -0800958bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700959{
Glenn Kasten263709e2012-01-06 08:40:01 -0800960 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961 return true;
962 }
963
Glenn Kasten6637baa2012-01-09 09:40:36 -0800964 AutoMutex lock(mLock);
965 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700966}
967
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800968status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700969{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700970 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
971 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800972
Mathias Agopian65ab4712010-07-14 17:59:35 -0700973 // check calling permissions
974 if (!settingsAllowed()) {
975 return PERMISSION_DENIED;
976 }
977
Glenn Kasten142f5192014-03-25 17:44:59 -0700978 // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface
979 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700980 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700981 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800982 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700983 AutoMutex lock(mHardwareLock);
984 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
985 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
986 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
987 status_t result = dev->set_parameters(dev, keyValuePairs.string());
988 final_result = result ?: final_result;
989 }
990 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800991 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700992 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
993 AudioParameter param = AudioParameter(keyValuePairs);
994 String8 value;
995 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700996 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
997 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700998 for (size_t i = 0; i < mRecordThreads.size(); i++) {
999 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -07001000 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -07001001 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
1002 // collect all of the thread's session IDs
1003 KeyedVector<int, bool> ids = thread->sessionIds();
1004 // suspend effects associated with those session IDs
1005 for (size_t j = 0; j < ids.size(); ++j) {
1006 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001007 thread->setEffectSuspended(FX_IID_AEC,
1008 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001009 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001010 thread->setEffectSuspended(FX_IID_NS,
1011 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -07001012 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -07001013 }
1014 }
Eric Laurentbee53372011-08-29 12:42:48 -07001015 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -07001016 }
1017 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001018 String8 screenState;
1019 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
1020 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -08001021 if (isOff != (AudioFlinger::mScreenState & 1)) {
1022 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -07001023 }
1024 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001025 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001026 }
1027
1028 // hold a strong ref on thread in case closeOutput() or closeInput() is called
1029 // and the thread is exited once the lock is released
1030 sp<ThreadBase> thread;
1031 {
1032 Mutex::Autolock _l(mLock);
1033 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001034 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001035 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -08001036 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001037 // indicate output device change to all input threads for pre processing
1038 AudioParameter param = AudioParameter(keyValuePairs);
1039 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -07001040 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
1041 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001042 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1043 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
1044 }
1045 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001046 }
1047 }
Glenn Kasten7378ca52012-01-20 13:44:40 -08001048 if (thread != 0) {
1049 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001050 }
1051 return BAD_VALUE;
1052}
1053
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001054String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001055{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001056 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
1057 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001058
Eric Laurenta4c5a552012-03-29 10:12:40 -07001059 Mutex::Autolock _l(mLock);
1060
Glenn Kasten142f5192014-03-25 17:44:59 -07001061 if (ioHandle == AUDIO_IO_HANDLE_NONE) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001062 String8 out_s8;
1063
Dima Zavin799a70e2011-04-18 16:57:27 -07001064 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001065 char *s;
1066 {
1067 AutoMutex lock(mHardwareLock);
1068 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001069 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001070 s = dev->get_parameters(dev, keys.string());
1071 mHardwareStatus = AUDIO_HW_IDLE;
1072 }
John Grossmanef7740b2012-02-09 11:28:36 -08001073 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -07001074 free(s);
1075 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001076 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001077 }
1078
Mathias Agopian65ab4712010-07-14 17:59:35 -07001079 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
1080 if (playbackThread != NULL) {
1081 return playbackThread->getParameters(keys);
1082 }
1083 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1084 if (recordThread != NULL) {
1085 return recordThread->getParameters(keys);
1086 }
1087 return String8("");
1088}
1089
Glenn Kastendd8104c2012-07-02 12:42:44 -07001090size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
1091 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001092{
Eric Laurenta1884f92011-08-23 08:25:03 -07001093 status_t ret = initCheck();
1094 if (ret != NO_ERROR) {
1095 return 0;
1096 }
1097
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001098 AutoMutex lock(mHardwareLock);
1099 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001100 audio_config_t config;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001101 memset(&config, 0, sizeof(config));
1102 config.sample_rate = sampleRate;
1103 config.channel_mask = channelMask;
1104 config.format = format;
1105
John Grossmanee578c02012-07-23 17:05:46 -07001106 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
1107 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -08001108 mHardwareStatus = AUDIO_HW_IDLE;
1109 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001110}
1111
Glenn Kasten5f972c02014-01-13 09:59:31 -08001112uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001113{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001114 Mutex::Autolock _l(mLock);
1115
1116 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1117 if (recordThread != NULL) {
1118 return recordThread->getInputFramesLost();
1119 }
1120 return 0;
1121}
1122
1123status_t AudioFlinger::setVoiceVolume(float value)
1124{
Eric Laurenta1884f92011-08-23 08:25:03 -07001125 status_t ret = initCheck();
1126 if (ret != NO_ERROR) {
1127 return ret;
1128 }
1129
Mathias Agopian65ab4712010-07-14 17:59:35 -07001130 // check calling permissions
1131 if (!settingsAllowed()) {
1132 return PERMISSION_DENIED;
1133 }
1134
1135 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001136 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001137 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001138 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001139 mHardwareStatus = AUDIO_HW_IDLE;
1140
1141 return ret;
1142}
1143
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001144status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001145 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001146{
1147 status_t status;
1148
1149 Mutex::Autolock _l(mLock);
1150
1151 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1152 if (playbackThread != NULL) {
1153 return playbackThread->getRenderPosition(halFrames, dspFrames);
1154 }
1155
1156 return BAD_VALUE;
1157}
1158
1159void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1160{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001161 Mutex::Autolock _l(mLock);
Eric Laurent44622db2014-08-01 19:00:33 -07001162 if (client == 0) {
1163 return;
1164 }
Eric Laurent021cf962014-05-13 10:18:14 -07001165 bool clientAdded = false;
1166 {
1167 Mutex::Autolock _cl(mClientLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001168
Eric Laurent021cf962014-05-13 10:18:14 -07001169 pid_t pid = IPCThreadState::self()->getCallingPid();
1170 if (mNotificationClients.indexOfKey(pid) < 0) {
1171 sp<NotificationClient> notificationClient = new NotificationClient(this,
1172 client,
1173 pid);
1174 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001175
Eric Laurent021cf962014-05-13 10:18:14 -07001176 mNotificationClients.add(pid, notificationClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001177
Eric Laurent021cf962014-05-13 10:18:14 -07001178 sp<IBinder> binder = client->asBinder();
1179 binder->linkToDeath(notificationClient);
1180 clientAdded = true;
1181 }
1182 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001183
Eric Laurent021cf962014-05-13 10:18:14 -07001184 // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001185 // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock.
Eric Laurent021cf962014-05-13 10:18:14 -07001186 if (clientAdded) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001187 // the config change is always sent from playback or record threads to avoid deadlock
1188 // with AudioSystem::gLock
1189 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001190 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001191 }
1192
1193 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001194 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001195 }
1196 }
1197}
1198
1199void AudioFlinger::removeNotificationClient(pid_t pid)
1200{
1201 Mutex::Autolock _l(mLock);
Eric Laurent021cf962014-05-13 10:18:14 -07001202 {
1203 Mutex::Autolock _cl(mClientLock);
1204 mNotificationClients.removeItem(pid);
1205 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001206
Steve Block3856b092011-10-20 11:56:00 +01001207 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001208 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001209 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001210 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001211 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001212 ALOGV(" pid %d @ %d", ref->mPid, i);
1213 if (ref->mPid == pid) {
1214 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001215 mAudioSessionRefs.removeAt(i);
1216 delete ref;
1217 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001218 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001219 } else {
1220 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001221 }
1222 }
1223 if (removed) {
1224 purgeStaleEffects_l();
1225 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001226}
1227
Eric Laurent021cf962014-05-13 10:18:14 -07001228void AudioFlinger::audioConfigChanged(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001229{
Eric Laurent021cf962014-05-13 10:18:14 -07001230 Mutex::Autolock _l(mClientLock);
1231 size_t size = mNotificationClients.size();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001232 for (size_t i = 0; i < size; i++) {
Eric Laurent021cf962014-05-13 10:18:14 -07001233 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event,
Eric Laurent10351942014-05-08 18:49:52 -07001234 ioHandle,
1235 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001236 }
1237}
1238
Eric Laurent021cf962014-05-13 10:18:14 -07001239// removeClient_l() must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001240void AudioFlinger::removeClient_l(pid_t pid)
1241{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001242 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001243 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001244 mClients.removeItem(pid);
1245}
1246
Eric Laurent717e1282012-06-29 16:36:52 -07001247// getEffectThread_l() must be called with AudioFlinger::mLock held
1248sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1249{
1250 sp<PlaybackThread> thread;
1251
1252 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1253 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1254 ALOG_ASSERT(thread == 0);
1255 thread = mPlaybackThreads.valueAt(i);
1256 }
1257 }
1258
1259 return thread;
1260}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001261
Mathias Agopian65ab4712010-07-14 17:59:35 -07001262
Mathias Agopian65ab4712010-07-14 17:59:35 -07001263
1264// ----------------------------------------------------------------------------
1265
1266AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1267 : RefBase(),
1268 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001269 // 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 -07001270 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001271 mPid(pid),
1272 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001273{
1274 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1275}
1276
Eric Laurent021cf962014-05-13 10:18:14 -07001277// Client destructor must be called with AudioFlinger::mClientLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001278AudioFlinger::Client::~Client()
1279{
1280 mAudioFlinger->removeClient_l(mPid);
1281}
1282
Glenn Kasten435dbe62012-01-30 10:15:48 -08001283sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001284{
1285 return mMemoryDealer;
1286}
1287
John Grossman4ff14ba2012-02-08 16:37:41 -08001288// Reserve one of the limited slots for a timed audio track associated
1289// with this client
1290bool AudioFlinger::Client::reserveTimedTrack()
1291{
1292 const int kMaxTimedTracksPerClient = 4;
1293
1294 Mutex::Autolock _l(mTimedTrackLock);
1295
1296 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1297 ALOGW("can not create timed track - pid %d has exceeded the limit",
1298 mPid);
1299 return false;
1300 }
1301
1302 mTimedTrackCount++;
1303 return true;
1304}
1305
1306// Release a slot for a timed audio track
1307void AudioFlinger::Client::releaseTimedTrack()
1308{
1309 Mutex::Autolock _l(mTimedTrackLock);
1310 mTimedTrackCount--;
1311}
1312
Mathias Agopian65ab4712010-07-14 17:59:35 -07001313// ----------------------------------------------------------------------------
1314
1315AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1316 const sp<IAudioFlingerClient>& client,
1317 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001318 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001319{
1320}
1321
1322AudioFlinger::NotificationClient::~NotificationClient()
1323{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001324}
1325
Glenn Kasten0f11b512014-01-31 16:18:54 -08001326void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001327{
1328 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001329 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001330}
1331
Mathias Agopian65ab4712010-07-14 17:59:35 -07001332
1333// ----------------------------------------------------------------------------
1334
Jeff Brown893a5642013-08-16 20:19:26 -07001335static bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) {
1336 return audio_is_remote_submix_device(inDevice);
1337}
1338
Mathias Agopian65ab4712010-07-14 17:59:35 -07001339sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001340 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001341 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001342 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001343 audio_channel_mask_t channelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001344 size_t *frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001345 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001346 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001347 int *sessionId,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001348 size_t *notificationFrames,
Glenn Kastend776ac62014-05-07 09:16:09 -07001349 sp<IMemory>& cblk,
1350 sp<IMemory>& buffers,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001351 status_t *status)
1352{
1353 sp<RecordThread::RecordTrack> recordTrack;
1354 sp<RecordHandle> recordHandle;
1355 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001356 status_t lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001357 int lSessionId;
1358
Glenn Kastend776ac62014-05-07 09:16:09 -07001359 cblk.clear();
1360 buffers.clear();
1361
Mathias Agopian65ab4712010-07-14 17:59:35 -07001362 // check calling permissions
1363 if (!recordingAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001364 ALOGE("openRecord() permission denied: recording not allowed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001365 lStatus = PERMISSION_DENIED;
1366 goto Exit;
1367 }
1368
Glenn Kasten53b5d092014-02-05 10:00:23 -08001369 // further sample rate checks are performed by createRecordTrack_l()
1370 if (sampleRate == 0) {
1371 ALOGE("openRecord() invalid sample rate %u", sampleRate);
1372 lStatus = BAD_VALUE;
1373 goto Exit;
1374 }
1375
Glenn Kasten0e0e8462014-03-13 15:02:40 -07001376 // we don't yet support anything other than 16-bit PCM
1377 if (!(audio_is_valid_format(format) &&
1378 audio_is_linear_pcm(format) && format == AUDIO_FORMAT_PCM_16_BIT)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -08001379 ALOGE("openRecord() invalid format %#x", format);
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001380 lStatus = BAD_VALUE;
1381 goto Exit;
1382 }
1383
Glenn Kasten53b5d092014-02-05 10:00:23 -08001384 // further channel mask checks are performed by createRecordTrack_l()
1385 if (!audio_is_input_channel(channelMask)) {
1386 ALOGE("openRecord() invalid channel mask %#x", channelMask);
1387 lStatus = BAD_VALUE;
1388 goto Exit;
1389 }
1390
Glenn Kasten05997e22014-03-13 15:08:33 -07001391 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001392 Mutex::Autolock _l(mLock);
Glenn Kastene848bd92014-03-13 15:00:32 -07001393 RecordThread *thread = checkRecordThread_l(input);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001394 if (thread == NULL) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001395 ALOGE("openRecord() checkRecordThread_l failed");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001396 lStatus = BAD_VALUE;
1397 goto Exit;
1398 }
1399
Jeff Brown893a5642013-08-16 20:19:26 -07001400 if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice())
1401 && !captureAudioOutputAllowed()) {
Glenn Kastene93cf2c2013-09-24 11:52:37 -07001402 ALOGE("openRecord() permission denied: capture not allowed");
Jeff Brown893a5642013-08-16 20:19:26 -07001403 lStatus = PERMISSION_DENIED;
1404 goto Exit;
1405 }
1406
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001407 pid_t pid = IPCThreadState::self()->getCallingPid();
Eric Laurent021cf962014-05-13 10:18:14 -07001408 client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001409
Glenn Kastenaea7ea02013-06-26 09:25:47 -07001410 if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001411 lSessionId = *sessionId;
1412 } else {
Glenn Kasten570f6332014-03-13 15:01:06 -07001413 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001414 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001415 if (sessionId != NULL) {
1416 *sessionId = lSessionId;
1417 }
1418 }
Glenn Kasten570f6332014-03-13 15:01:06 -07001419 ALOGV("openRecord() lSessionId: %d", lSessionId);
1420
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001421 // TODO: the uid should be passed in as a parameter to openRecord
Glenn Kasten1879fff2012-07-11 15:36:59 -07001422 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kasten7df8c0b2014-07-03 12:23:29 -07001423 frameCount, lSessionId, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001424 IPCThreadState::self()->getCallingUid(),
1425 flags, tid, &lStatus);
Haynes Mathew George03e9e832013-12-13 15:40:13 -08001426 LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001427 }
Glenn Kastene198c362013-08-13 09:13:36 -07001428
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001429 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001430 // remove local strong reference to Client before deleting the RecordTrack so that the
Eric Laurent021cf962014-05-13 10:18:14 -07001431 // Client destructor is called by the TrackBase destructor with mClientLock held
Eric Laurentfe1a94e2014-05-26 16:03:08 -07001432 // Don't hold mClientLock when releasing the reference on the track as the
1433 // destructor will acquire it.
1434 {
1435 Mutex::Autolock _cl(mClientLock);
1436 client.clear();
1437 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001438 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001439 goto Exit;
1440 }
1441
Glenn Kastend776ac62014-05-07 09:16:09 -07001442 cblk = recordTrack->getCblk();
1443 buffers = recordTrack->getBuffers();
1444
Glenn Kasten2fc14732013-08-05 14:58:14 -07001445 // return handle to client
Mathias Agopian65ab4712010-07-14 17:59:35 -07001446 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001447
1448Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001449 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001450 return recordHandle;
1451}
1452
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001453
1454
Mathias Agopian65ab4712010-07-14 17:59:35 -07001455// ----------------------------------------------------------------------------
1456
Eric Laurenta4c5a552012-03-29 10:12:40 -07001457audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1458{
Eric Laurent44622db2014-08-01 19:00:33 -07001459 if (name == NULL) {
1460 return 0;
1461 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001462 if (!settingsAllowed()) {
1463 return 0;
1464 }
1465 Mutex::Autolock _l(mLock);
1466 return loadHwModule_l(name);
1467}
1468
1469// loadHwModule_l() must be called with AudioFlinger::mLock held
1470audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1471{
1472 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1473 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1474 ALOGW("loadHwModule() module %s already loaded", name);
1475 return mAudioHwDevs.keyAt(i);
1476 }
1477 }
1478
Eric Laurenta4c5a552012-03-29 10:12:40 -07001479 audio_hw_device_t *dev;
1480
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001481 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001482 if (rc) {
1483 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1484 return 0;
1485 }
1486
1487 mHardwareStatus = AUDIO_HW_INIT;
1488 rc = dev->init_check(dev);
1489 mHardwareStatus = AUDIO_HW_IDLE;
1490 if (rc) {
1491 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1492 return 0;
1493 }
1494
John Grossmanee578c02012-07-23 17:05:46 -07001495 // Check and cache this HAL's level of support for master mute and master
1496 // volume. If this is the first HAL opened, and it supports the get
1497 // methods, use the initial values provided by the HAL as the current
1498 // master mute and volume settings.
1499
1500 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1501 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001502 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001503
1504 if (0 == mAudioHwDevs.size()) {
1505 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1506 if (NULL != dev->get_master_volume) {
1507 float mv;
1508 if (OK == dev->get_master_volume(dev, &mv)) {
1509 mMasterVolume = mv;
1510 }
1511 }
1512
1513 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1514 if (NULL != dev->get_master_mute) {
1515 bool mm;
1516 if (OK == dev->get_master_mute(dev, &mm)) {
1517 mMasterMute = mm;
1518 }
1519 }
1520 }
1521
Eric Laurenta4c5a552012-03-29 10:12:40 -07001522 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001523 if ((NULL != dev->set_master_volume) &&
1524 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1525 flags = static_cast<AudioHwDevice::Flags>(flags |
1526 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1527 }
1528
1529 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1530 if ((NULL != dev->set_master_mute) &&
1531 (OK == dev->set_master_mute(dev, mMasterMute))) {
1532 flags = static_cast<AudioHwDevice::Flags>(flags |
1533 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1534 }
1535
Eric Laurenta4c5a552012-03-29 10:12:40 -07001536 mHardwareStatus = AUDIO_HW_IDLE;
1537 }
1538
1539 audio_module_handle_t handle = nextUniqueId();
Eric Laurent83b88082014-06-20 18:31:16 -07001540 mAudioHwDevs.add(handle, new AudioHwDevice(handle, name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001541
1542 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001543 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001544
1545 return handle;
1546
1547}
1548
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001549// ----------------------------------------------------------------------------
1550
Glenn Kasten3b16c762012-11-14 08:44:39 -08001551uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001552{
1553 Mutex::Autolock _l(mLock);
1554 PlaybackThread *thread = primaryPlaybackThread_l();
1555 return thread != NULL ? thread->sampleRate() : 0;
1556}
1557
Glenn Kastene33054e2012-11-14 12:54:39 -08001558size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001559{
1560 Mutex::Autolock _l(mLock);
1561 PlaybackThread *thread = primaryPlaybackThread_l();
1562 return thread != NULL ? thread->frameCountHAL() : 0;
1563}
1564
1565// ----------------------------------------------------------------------------
1566
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001567status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1568{
1569 uid_t uid = IPCThreadState::self()->getCallingUid();
1570 if (uid != AID_SYSTEM) {
1571 return PERMISSION_DENIED;
1572 }
1573 Mutex::Autolock _l(mLock);
1574 if (mIsDeviceTypeKnown) {
1575 return INVALID_OPERATION;
1576 }
1577 mIsLowRamDevice = isLowRamDevice;
1578 mIsDeviceTypeKnown = true;
1579 return NO_ERROR;
1580}
1581
Eric Laurent93c3d412014-08-01 14:48:35 -07001582audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId)
1583{
1584 Mutex::Autolock _l(mLock);
1585 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1586 sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i);
1587 if ((thread->hasAudioSession(sessionId) & ThreadBase::TRACK_SESSION) != 0) {
1588 // A session can only be on one thread, so exit after first match
1589 String8 reply = thread->getParameters(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC));
1590 AudioParameter param = AudioParameter(reply);
1591 int value;
1592 if (param.getInt(String8(AUDIO_PARAMETER_STREAM_HW_AV_SYNC), value) == NO_ERROR) {
1593 return value;
1594 }
1595 break;
1596 }
1597 }
1598 return AUDIO_HW_SYNC_INVALID;
1599}
1600
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001601// ----------------------------------------------------------------------------
1602
Eric Laurent83b88082014-06-20 18:31:16 -07001603
1604sp<AudioFlinger::PlaybackThread> AudioFlinger::openOutput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001605 audio_io_handle_t *output,
1606 audio_config_t *config,
1607 audio_devices_t devices,
1608 const String8& address,
Eric Laurent83b88082014-06-20 18:31:16 -07001609 audio_output_flags_t flags)
1610{
Eric Laurentcf2c0212014-07-25 16:20:43 -07001611 AudioHwDevice *outHwDev = findSuitableHwDev_l(module, devices);
Eric Laurent83b88082014-06-20 18:31:16 -07001612 if (outHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001613 return 0;
Eric Laurent83b88082014-06-20 18:31:16 -07001614 }
1615
1616 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentcf2c0212014-07-25 16:20:43 -07001617 if (*output == AUDIO_IO_HANDLE_NONE) {
1618 *output = nextUniqueId();
1619 }
Eric Laurent83b88082014-06-20 18:31:16 -07001620
1621 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
1622
1623 audio_stream_out_t *outStream = NULL;
1624
1625 // FOR TESTING ONLY:
Andy Hung9a592762014-07-21 21:56:01 -07001626 // This if statement allows overriding the audio policy settings
1627 // and forcing a specific format or channel mask to the HAL/Sink device for testing.
1628 if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) {
1629 // Check only for Normal Mixing mode
1630 if (kEnableExtendedPrecision) {
1631 // Specify format (uncomment one below to choose)
1632 //config->format = AUDIO_FORMAT_PCM_FLOAT;
1633 //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
1634 //config->format = AUDIO_FORMAT_PCM_32_BIT;
1635 //config->format = AUDIO_FORMAT_PCM_8_24_BIT;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001636 // ALOGV("openOutput_l() upgrading format to %#08x", config->format);
Andy Hung9a592762014-07-21 21:56:01 -07001637 }
1638 if (kEnableExtendedChannels) {
1639 // Specify channel mask (uncomment one below to choose)
1640 //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch
1641 //config->channel_mask = audio_channel_mask_from_representation_and_bits(
1642 // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example
1643 }
Eric Laurent83b88082014-06-20 18:31:16 -07001644 }
1645
1646 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001647 *output,
1648 devices,
1649 flags,
1650 config,
1651 &outStream,
1652 address.string());
Eric Laurent83b88082014-06-20 18:31:16 -07001653
1654 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001655 ALOGV("openOutput_l() openOutputStream returned output %p, sampleRate %d, Format %#x, "
Andy Hung9a592762014-07-21 21:56:01 -07001656 "channelMask %#x, status %d",
Eric Laurent83b88082014-06-20 18:31:16 -07001657 outStream,
1658 config->sample_rate,
1659 config->format,
1660 config->channel_mask,
1661 status);
1662
1663 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001664 AudioStreamOut *outputStream = new AudioStreamOut(outHwDev, outStream, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001665
1666 PlaybackThread *thread;
1667 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001668 thread = new OffloadThread(this, outputStream, *output, devices);
1669 ALOGV("openOutput_l() created offload output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001670 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT)
1671 || !isValidPcmSinkFormat(config->format)
Andy Hung9a592762014-07-21 21:56:01 -07001672 || !isValidPcmSinkChannelMask(config->channel_mask)) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001673 thread = new DirectOutputThread(this, outputStream, *output, devices);
1674 ALOGV("openOutput_l() created direct output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001675 } else {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001676 thread = new MixerThread(this, outputStream, *output, devices);
1677 ALOGV("openOutput_l() created mixer output: ID %d thread %p", *output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001678 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001679 mPlaybackThreads.add(*output, thread);
Eric Laurent83b88082014-06-20 18:31:16 -07001680 return thread;
1681 }
1682
1683 return 0;
1684}
1685
Eric Laurentcf2c0212014-07-25 16:20:43 -07001686status_t AudioFlinger::openOutput(audio_module_handle_t module,
1687 audio_io_handle_t *output,
1688 audio_config_t *config,
1689 audio_devices_t *devices,
1690 const String8& address,
1691 uint32_t *latencyMs,
1692 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001693{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001694 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001695 module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001696 (devices != NULL) ? *devices : 0,
1697 config->sample_rate,
1698 config->format,
1699 config->channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001700 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001701
Eric Laurentcf2c0212014-07-25 16:20:43 -07001702 if (*devices == AUDIO_DEVICE_NONE) {
1703 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001704 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001705
Mathias Agopian65ab4712010-07-14 17:59:35 -07001706 Mutex::Autolock _l(mLock);
1707
Eric Laurentcf2c0212014-07-25 16:20:43 -07001708 sp<PlaybackThread> thread = openOutput_l(module, output, config, *devices, address, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001709 if (thread != 0) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001710 *latencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001711
1712 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001713 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001714
1715 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001716 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001717 ALOGI("Using module %d has the primary audio interface", module);
Eric Laurent83b88082014-06-20 18:31:16 -07001718 mPrimaryHardwareDev = thread->getOutput()->audioHwDev;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001719
1720 AutoMutex lock(mHardwareLock);
1721 mHardwareStatus = AUDIO_HW_SET_MODE;
Eric Laurent83b88082014-06-20 18:31:16 -07001722 mPrimaryHardwareDev->hwDevice()->set_mode(mPrimaryHardwareDev->hwDevice(), mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001723 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten4ea00a22014-06-02 08:29:22 -07001724
Eric Laurentcf2c0212014-07-25 16:20:43 -07001725 mPrimaryOutputSampleRate = config->sample_rate;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001726 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001727 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001728 }
1729
Eric Laurentcf2c0212014-07-25 16:20:43 -07001730 return NO_INIT;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001731}
1732
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001733audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1734 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001735{
1736 Mutex::Autolock _l(mLock);
1737 MixerThread *thread1 = checkMixerThread_l(output1);
1738 MixerThread *thread2 = checkMixerThread_l(output2);
1739
1740 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001741 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1742 output2);
Glenn Kasten142f5192014-03-25 17:44:59 -07001743 return AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001744 }
1745
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001746 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001747 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1748 thread->addOutputTrack(thread2);
1749 mPlaybackThreads.add(id, thread);
1750 // notify client processes of the new output creation
Eric Laurent021cf962014-05-13 10:18:14 -07001751 thread->audioConfigChanged(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001752 return id;
1753}
1754
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001755status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001756{
Glenn Kastend96c5722012-04-25 13:44:49 -07001757 return closeOutput_nonvirtual(output);
1758}
1759
1760status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1761{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001762 // keep strong reference on the playback thread so that
1763 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001764 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001765 {
1766 Mutex::Autolock _l(mLock);
1767 thread = checkPlaybackThread_l(output);
1768 if (thread == NULL) {
1769 return BAD_VALUE;
1770 }
1771
Steve Block3856b092011-10-20 11:56:00 +01001772 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001773
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001774 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001775 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001776 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001777 DuplicatingThread *dupThread =
1778 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001779 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001780
1781 }
1782 }
1783 }
1784
1785
1786 mPlaybackThreads.removeItem(output);
1787 // save all effects to the default thread
1788 if (mPlaybackThreads.size()) {
1789 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1790 if (dstThread != NULL) {
1791 // audioflinger lock is held here so the acquisition order of thread locks does not
1792 // matter
1793 Mutex::Autolock _dl(dstThread->mLock);
1794 Mutex::Autolock _sl(thread->mLock);
1795 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1796 for (size_t i = 0; i < effectChains.size(); i ++) {
1797 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001798 }
1799 }
1800 }
Eric Laurent021cf962014-05-13 10:18:14 -07001801 audioConfigChanged(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001802 }
1803 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001804 // The thread entity (active unit of execution) is no longer running here,
1805 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001806
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001807 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurent83b88082014-06-20 18:31:16 -07001808 closeOutputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001809 }
Eric Laurent83b88082014-06-20 18:31:16 -07001810
Mathias Agopian65ab4712010-07-14 17:59:35 -07001811 return NO_ERROR;
1812}
1813
Eric Laurent83b88082014-06-20 18:31:16 -07001814void AudioFlinger::closeOutputFinish(sp<PlaybackThread> thread)
1815{
1816 AudioStreamOut *out = thread->clearOutput();
1817 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
1818 // from now on thread->mOutput is NULL
1819 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
1820 delete out;
1821}
1822
1823void AudioFlinger::closeOutputInternal_l(sp<PlaybackThread> thread)
1824{
1825 mPlaybackThreads.removeItem(thread->mId);
1826 thread->exit();
1827 closeOutputFinish(thread);
1828}
1829
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001830status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001831{
1832 Mutex::Autolock _l(mLock);
1833 PlaybackThread *thread = checkPlaybackThread_l(output);
1834
1835 if (thread == NULL) {
1836 return BAD_VALUE;
1837 }
1838
Steve Block3856b092011-10-20 11:56:00 +01001839 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001840 thread->suspend();
1841
1842 return NO_ERROR;
1843}
1844
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001845status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001846{
1847 Mutex::Autolock _l(mLock);
1848 PlaybackThread *thread = checkPlaybackThread_l(output);
1849
1850 if (thread == NULL) {
1851 return BAD_VALUE;
1852 }
1853
Steve Block3856b092011-10-20 11:56:00 +01001854 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001855
1856 thread->restore();
1857
1858 return NO_ERROR;
1859}
1860
Eric Laurentcf2c0212014-07-25 16:20:43 -07001861status_t AudioFlinger::openInput(audio_module_handle_t module,
1862 audio_io_handle_t *input,
1863 audio_config_t *config,
1864 audio_devices_t *device,
1865 const String8& address,
1866 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -07001867 audio_input_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001868{
Eric Laurent83b88082014-06-20 18:31:16 -07001869 Mutex::Autolock _l(mLock);
1870
Eric Laurentcf2c0212014-07-25 16:20:43 -07001871 if (*device == AUDIO_DEVICE_NONE) {
1872 return BAD_VALUE;
Eric Laurent83b88082014-06-20 18:31:16 -07001873 }
1874
Eric Laurentcf2c0212014-07-25 16:20:43 -07001875 sp<RecordThread> thread = openInput_l(module, input, config, *device, address, source, flags);
Eric Laurent83b88082014-06-20 18:31:16 -07001876
1877 if (thread != 0) {
Eric Laurent83b88082014-06-20 18:31:16 -07001878 // notify client processes of the new input creation
1879 thread->audioConfigChanged(AudioSystem::INPUT_OPENED);
Eric Laurentcf2c0212014-07-25 16:20:43 -07001880 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001881 }
Eric Laurentcf2c0212014-07-25 16:20:43 -07001882 return NO_INIT;
Eric Laurent83b88082014-06-20 18:31:16 -07001883}
Dima Zavin799a70e2011-04-18 16:57:27 -07001884
Eric Laurent83b88082014-06-20 18:31:16 -07001885sp<AudioFlinger::RecordThread> AudioFlinger::openInput_l(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001886 audio_io_handle_t *input,
1887 audio_config_t *config,
Eric Laurent83b88082014-06-20 18:31:16 -07001888 audio_devices_t device,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001889 const String8& address,
1890 audio_source_t source,
Eric Laurent83b88082014-06-20 18:31:16 -07001891 audio_input_flags_t flags)
1892{
Eric Laurent83b88082014-06-20 18:31:16 -07001893 AudioHwDevice *inHwDev = findSuitableHwDev_l(module, device);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001894 if (inHwDev == NULL) {
Eric Laurentcf2c0212014-07-25 16:20:43 -07001895 *input = AUDIO_IO_HANDLE_NONE;
Dima Zavin799a70e2011-04-18 16:57:27 -07001896 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001897 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001898
Eric Laurentcf2c0212014-07-25 16:20:43 -07001899 if (*input == AUDIO_IO_HANDLE_NONE) {
1900 *input = nextUniqueId();
1901 }
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001902
Eric Laurentcf2c0212014-07-25 16:20:43 -07001903 audio_config_t halconfig = *config;
1904 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Glenn Kasten32550952013-08-06 10:45:10 -07001905 audio_stream_in_t *inStream = NULL;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001906 status_t status = inHwHal->open_input_stream(inHwHal, *input, device, &halconfig,
1907 &inStream, flags, address.string(), source);
1908 ALOGV("openInput_l() openInputStream returned input %p, SamplingRate %d"
1909 ", Format %#x, Channels %x, flags %#x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001910 inStream,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001911 halconfig.sample_rate,
1912 halconfig.format,
1913 halconfig.channel_mask,
Glenn Kastenec40d282014-07-15 15:31:26 -07001914 flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001915 status);
1916
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001917 // If the input could not be opened with the requested parameters and we can handle the
1918 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1919 // 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 -07001920 if (status == BAD_VALUE &&
Eric Laurentcf2c0212014-07-25 16:20:43 -07001921 config->format == halconfig.format && halconfig.format == AUDIO_FORMAT_PCM_16_BIT &&
1922 (halconfig.sample_rate <= 2 * config->sample_rate) &&
1923 (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_2) &&
1924 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_2)) {
Glenn Kasten85948432013-08-19 12:09:05 -07001925 // FIXME describe the change proposed by HAL (save old values so we can log them here)
Eric Laurentcf2c0212014-07-25 16:20:43 -07001926 ALOGV("openInput_l() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001927 inStream = NULL;
Eric Laurentcf2c0212014-07-25 16:20:43 -07001928 status = inHwHal->open_input_stream(inHwHal, *input, device, &halconfig,
1929 &inStream, flags, address.string(), source);
Glenn Kasten85948432013-08-19 12:09:05 -07001930 // FIXME log this new status; HAL should not propose any further changes
Mathias Agopian65ab4712010-07-14 17:59:35 -07001931 }
1932
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001933 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001934
Glenn Kasten46909e72013-02-26 09:20:22 -08001935#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001936 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1937 // or (re-)create if current Pipe is idle and does not match the new format
1938 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001939 enum {
1940 TEE_SINK_NO, // don't copy input
1941 TEE_SINK_NEW, // copy input using a new pipe
1942 TEE_SINK_OLD, // copy input using an existing pipe
1943 } kind;
Glenn Kasten329f6512014-08-28 16:23:16 -07001944 NBAIO_Format format = Format_from_SR_C(halconfig.sample_rate,
1945 audio_channel_count_from_in_mask(halconfig.channel_mask), halconfig.format);
Glenn Kastenda6ef132013-01-10 12:31:01 -08001946 if (!mTeeSinkInputEnabled) {
1947 kind = TEE_SINK_NO;
Glenn Kasten6e0d67d2014-01-31 09:41:08 -08001948 } else if (!Format_isValid(format)) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001949 kind = TEE_SINK_NO;
1950 } else if (mRecordTeeSink == 0) {
1951 kind = TEE_SINK_NEW;
1952 } else if (mRecordTeeSink->getStrongCount() != 1) {
1953 kind = TEE_SINK_NO;
Glenn Kastenf66b4222014-02-20 10:23:28 -08001954 } else if (Format_isEqual(format, mRecordTeeSink->format())) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001955 kind = TEE_SINK_OLD;
1956 } else {
1957 kind = TEE_SINK_NEW;
1958 }
1959 switch (kind) {
1960 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001961 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001962 size_t numCounterOffers = 0;
1963 const NBAIO_Format offers[1] = {format};
1964 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1965 ALOG_ASSERT(index == 0);
1966 PipeReader *pipeReader = new PipeReader(*pipe);
1967 numCounterOffers = 0;
1968 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1969 ALOG_ASSERT(index == 0);
1970 mRecordTeeSink = pipe;
1971 mRecordTeeSource = pipeReader;
1972 teeSink = pipe;
1973 }
1974 break;
1975 case TEE_SINK_OLD:
1976 teeSink = mRecordTeeSink;
1977 break;
1978 case TEE_SINK_NO:
1979 default:
1980 break;
1981 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001982#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001983
Eric Laurentcf2c0212014-07-25 16:20:43 -07001984 AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001985
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001986 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001987 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001988 // pre processing modules
Eric Laurent83b88082014-06-20 18:31:16 -07001989 sp<RecordThread> thread = new RecordThread(this,
Eric Laurentcf2c0212014-07-25 16:20:43 -07001990 inputStream,
1991 *input,
Eric Laurentd3922f72013-02-01 17:57:04 -08001992 primaryOutputDevice_l(),
Eric Laurent83b88082014-06-20 18:31:16 -07001993 device
Glenn Kasten46909e72013-02-26 09:20:22 -08001994#ifdef TEE_SINK
1995 , teeSink
1996#endif
1997 );
Eric Laurentcf2c0212014-07-25 16:20:43 -07001998 mRecordThreads.add(*input, thread);
1999 ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get());
Eric Laurent83b88082014-06-20 18:31:16 -07002000 return thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002001 }
2002
Eric Laurentcf2c0212014-07-25 16:20:43 -07002003 *input = AUDIO_IO_HANDLE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002004 return 0;
2005}
2006
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002007status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002008{
Glenn Kastend96c5722012-04-25 13:44:49 -07002009 return closeInput_nonvirtual(input);
2010}
2011
2012status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
2013{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002014 // keep strong reference on the record thread so that
2015 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002016 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002017 {
2018 Mutex::Autolock _l(mLock);
2019 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07002020 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002021 return BAD_VALUE;
2022 }
2023
Steve Block3856b092011-10-20 11:56:00 +01002024 ALOGV("closeInput() %d", input);
Eric Laurent021cf962014-05-13 10:18:14 -07002025 audioConfigChanged(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002026 mRecordThreads.removeItem(input);
2027 }
Eric Laurent83b88082014-06-20 18:31:16 -07002028 // FIXME: calling thread->exit() without mLock held should not be needed anymore now that
2029 // we have a different lock for notification client
2030 closeInputFinish(thread);
2031 return NO_ERROR;
2032}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002033
Eric Laurent83b88082014-06-20 18:31:16 -07002034void AudioFlinger::closeInputFinish(sp<RecordThread> thread)
2035{
2036 thread->exit();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002037 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08002038 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002039 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07002040 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07002041 delete in;
Eric Laurent83b88082014-06-20 18:31:16 -07002042}
Mathias Agopian65ab4712010-07-14 17:59:35 -07002043
Eric Laurent83b88082014-06-20 18:31:16 -07002044void AudioFlinger::closeInputInternal_l(sp<RecordThread> thread)
2045{
2046 mRecordThreads.removeItem(thread->mId);
2047 closeInputFinish(thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002048}
2049
Glenn Kastend2304db2014-02-03 07:40:31 -08002050status_t AudioFlinger::invalidateStream(audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002051{
2052 Mutex::Autolock _l(mLock);
Glenn Kastend2304db2014-02-03 07:40:31 -08002053 ALOGV("invalidateStream() stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002054
2055 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2056 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07002057 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07002058 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002059
2060 return NO_ERROR;
2061}
2062
2063
Eric Laurentde3f8392014-07-27 18:38:22 -07002064audio_unique_id_t AudioFlinger::newAudioUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002065{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002066 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002067}
2068
Marco Nelissend457c972014-02-11 08:47:07 -08002069void AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002070{
2071 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002072 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002073 ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid);
2074 if (pid != -1 && (caller == getpid_cached)) {
2075 caller = pid;
2076 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002077
Eric Laurent021cf962014-05-13 10:18:14 -07002078 {
2079 Mutex::Autolock _cl(mClientLock);
2080 // Ignore requests received from processes not known as notification client. The request
2081 // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be
2082 // called from a different pid leaving a stale session reference. Also we don't know how
2083 // to clear this reference if the client process dies.
2084 if (mNotificationClients.indexOfKey(caller) < 0) {
2085 ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession);
2086 return;
2087 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002088 }
2089
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002090 size_t num = mAudioSessionRefs.size();
2091 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002092 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002093 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2094 ref->mCnt++;
2095 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002096 return;
2097 }
2098 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08002099 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
2100 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002101}
2102
Marco Nelissend457c972014-02-11 08:47:07 -08002103void AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid)
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002104{
2105 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08002106 pid_t caller = IPCThreadState::self()->getCallingPid();
Marco Nelissend457c972014-02-11 08:47:07 -08002107 ALOGV("releasing %d from %d for %d", audioSession, caller, pid);
2108 if (pid != -1 && (caller == getpid_cached)) {
2109 caller = pid;
2110 }
Glenn Kasten8d6a2442012-02-08 14:04:28 -08002111 size_t num = mAudioSessionRefs.size();
2112 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002113 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002114 if (ref->mSessionid == audioSession && ref->mPid == caller) {
2115 ref->mCnt--;
2116 ALOGV(" decremented refcount to %d", ref->mCnt);
2117 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002118 mAudioSessionRefs.removeAt(i);
2119 delete ref;
2120 purgeStaleEffects_l();
2121 }
2122 return;
2123 }
2124 }
Eric Laurentd1b28d42013-09-18 18:47:13 -07002125 // If the caller is mediaserver it is likely that the session being released was acquired
2126 // on behalf of a process not in notification clients and we ignore the warning.
2127 ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002128}
2129
2130void AudioFlinger::purgeStaleEffects_l() {
2131
Steve Block3856b092011-10-20 11:56:00 +01002132 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002133
2134 Vector< sp<EffectChain> > chains;
2135
2136 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2137 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2138 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2139 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07002140 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
2141 chains.push(ec);
2142 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002143 }
2144 }
2145 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2146 sp<RecordThread> t = mRecordThreads.valueAt(i);
2147 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
2148 sp<EffectChain> ec = t->mEffectChains[j];
2149 chains.push(ec);
2150 }
2151 }
2152
2153 for (size_t i = 0; i < chains.size(); i++) {
2154 sp<EffectChain> ec = chains[i];
2155 int sessionid = ec->sessionId();
2156 sp<ThreadBase> t = ec->mThread.promote();
2157 if (t == 0) {
2158 continue;
2159 }
2160 size_t numsessionrefs = mAudioSessionRefs.size();
2161 bool found = false;
2162 for (size_t k = 0; k < numsessionrefs; k++) {
2163 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08002164 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01002165 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002166 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002167 found = true;
2168 break;
2169 }
2170 }
2171 if (!found) {
Glenn Kastene198c362013-08-13 09:13:36 -07002172 Mutex::Autolock _l(t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002173 // remove all effects from the chain
2174 while (ec->mEffects.size()) {
2175 sp<EffectModule> effect = ec->mEffects[0];
2176 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002177 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07002178 if (effect->purgeHandles()) {
2179 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002180 }
2181 AudioSystem::unregisterEffect(effect->id());
2182 }
2183 }
2184 }
2185 return;
2186}
2187
Mathias Agopian65ab4712010-07-14 17:59:35 -07002188// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002189AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002190{
Glenn Kastena1117922012-01-26 10:53:32 -08002191 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002192}
2193
2194// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002195AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002196{
2197 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08002198 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002199}
2200
2201// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002202AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002203{
Glenn Kastena1117922012-01-26 10:53:32 -08002204 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002205}
2206
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002207uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07002208{
Glenn Kastenbcefec32014-01-17 12:09:05 -08002209 return (uint32_t) android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002210}
2211
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08002212AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002213{
2214 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2215 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07002216 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07002217 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002218 return thread;
2219 }
2220 }
2221 return NULL;
2222}
2223
Glenn Kastenbb4350d2012-07-03 15:56:38 -07002224audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002225{
2226 PlaybackThread *thread = primaryPlaybackThread_l();
2227
2228 if (thread == NULL) {
2229 return 0;
2230 }
2231
Eric Laurentf1c04f92012-08-28 14:26:53 -07002232 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002233}
2234
Eric Laurenta011e352012-03-29 15:51:43 -07002235sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
2236 int triggerSession,
2237 int listenerSession,
2238 sync_event_callback_t callBack,
Eric Laurent8ea16e42014-02-20 16:26:11 -08002239 wp<RefBase> cookie)
Eric Laurenta011e352012-03-29 15:51:43 -07002240{
2241 Mutex::Autolock _l(mLock);
2242
2243 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
2244 status_t playStatus = NAME_NOT_FOUND;
2245 status_t recStatus = NAME_NOT_FOUND;
2246 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2247 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
2248 if (playStatus == NO_ERROR) {
2249 return event;
2250 }
2251 }
2252 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2253 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
2254 if (recStatus == NO_ERROR) {
2255 return event;
2256 }
2257 }
2258 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2259 mPendingSyncEvents.add(event);
2260 } else {
2261 ALOGV("createSyncEvent() invalid event %d", event->type());
2262 event.clear();
2263 }
2264 return event;
2265}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002266
Mathias Agopian65ab4712010-07-14 17:59:35 -07002267// ----------------------------------------------------------------------------
2268// Effect management
2269// ----------------------------------------------------------------------------
2270
2271
Glenn Kastenf587ba52012-01-26 16:25:10 -08002272status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002273{
2274 Mutex::Autolock _l(mLock);
2275 return EffectQueryNumberEffects(numEffects);
2276}
2277
Glenn Kastenf587ba52012-01-26 16:25:10 -08002278status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002279{
2280 Mutex::Autolock _l(mLock);
2281 return EffectQueryEffect(index, descriptor);
2282}
2283
Glenn Kasten5e92a782012-01-30 07:40:52 -08002284status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002285 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002286{
2287 Mutex::Autolock _l(mLock);
2288 return EffectGetDescriptor(pUuid, descriptor);
2289}
2290
2291
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002292sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002293 effect_descriptor_t *pDesc,
2294 const sp<IEffectClient>& effectClient,
2295 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002296 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002297 int sessionId,
2298 status_t *status,
2299 int *id,
2300 int *enabled)
2301{
2302 status_t lStatus = NO_ERROR;
2303 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002304 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002305
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002306 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002307 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002308 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002309
2310 if (pDesc == NULL) {
2311 lStatus = BAD_VALUE;
2312 goto Exit;
2313 }
2314
Eric Laurent84e9a102010-09-23 16:10:16 -07002315 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002316 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002317 lStatus = PERMISSION_DENIED;
2318 goto Exit;
2319 }
2320
Dima Zavinfce7a472011-04-19 22:30:36 -07002321 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002322 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002323 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002324 lStatus = PERMISSION_DENIED;
2325 goto Exit;
2326 }
2327
Mathias Agopian65ab4712010-07-14 17:59:35 -07002328 {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002329 if (!EffectIsNullUuid(&pDesc->uuid)) {
2330 // if uuid is specified, request effect descriptor
2331 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2332 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002333 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002334 goto Exit;
2335 }
2336 } else {
2337 // if uuid is not specified, look for an available implementation
2338 // of the required type in effect factory
2339 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002340 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002341 lStatus = BAD_VALUE;
2342 goto Exit;
2343 }
2344 uint32_t numEffects = 0;
2345 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002346 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002347 bool found = false;
2348
2349 lStatus = EffectQueryNumberEffects(&numEffects);
2350 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002351 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002352 goto Exit;
2353 }
2354 for (uint32_t i = 0; i < numEffects; i++) {
2355 lStatus = EffectQueryEffect(i, &desc);
2356 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002357 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002358 continue;
2359 }
2360 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2361 // If matching type found save effect descriptor. If the session is
2362 // 0 and the effect is not auxiliary, continue enumeration in case
2363 // an auxiliary version of this effect type is available
2364 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002365 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002366 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002367 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2368 break;
2369 }
2370 }
2371 }
2372 if (!found) {
2373 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002374 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002375 goto Exit;
2376 }
2377 // For same effect type, chose auxiliary version over insert version if
2378 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002379 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002380 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002381 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002382 }
2383 }
2384
2385 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002386 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002387 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2388 lStatus = INVALID_OPERATION;
2389 goto Exit;
2390 }
2391
Eric Laurent59255e42011-07-27 19:49:51 -07002392 // check recording permission for visualizer
2393 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2394 !recordingAllowed()) {
2395 lStatus = PERMISSION_DENIED;
2396 goto Exit;
2397 }
2398
Mathias Agopian65ab4712010-07-14 17:59:35 -07002399 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002400 *pDesc = desc;
Glenn Kasten142f5192014-03-25 17:44:59 -07002401 if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002402 // if the output returned by getOutputForEffect() is removed before we lock the
2403 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
2404 // and we will exit safely
2405 io = AudioSystem::getOutputForEffect(&desc);
2406 ALOGV("createEffect got output %d", io);
2407 }
2408
2409 Mutex::Autolock _l(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002410
2411 // If output is not specified try to find a matching audio session ID in one of the
2412 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002413 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2414 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002415 // Note: io is never 0 when creating an effect on an input
Glenn Kasten142f5192014-03-25 17:44:59 -07002416 if (io == AUDIO_IO_HANDLE_NONE) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002417 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
2418 // output must be specified by AudioPolicyManager when using session
2419 // AUDIO_SESSION_OUTPUT_STAGE
2420 lStatus = BAD_VALUE;
2421 goto Exit;
2422 }
Eric Laurenteb3c3372013-09-25 12:25:29 -07002423 // look for the thread where the specified audio session is present
2424 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2425 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2426 io = mPlaybackThreads.keyAt(i);
2427 break;
2428 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002429 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002430 if (io == 0) {
Eric Laurenteb3c3372013-09-25 12:25:29 -07002431 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2432 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2433 io = mRecordThreads.keyAt(i);
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002434 break;
2435 }
2436 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002437 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002438 // If no output thread contains the requested session ID, default to
2439 // first output. The effect chain will be moved to the correct output
2440 // thread when a track with the same session ID is created
Glenn Kasten142f5192014-03-25 17:44:59 -07002441 if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002442 io = mPlaybackThreads.keyAt(0);
2443 }
Steve Block3856b092011-10-20 11:56:00 +01002444 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002445 }
2446 ThreadBase *thread = checkRecordThread_l(io);
2447 if (thread == NULL) {
2448 thread = checkPlaybackThread_l(io);
2449 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002450 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002451 lStatus = BAD_VALUE;
2452 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002453 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002454 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002455
Eric Laurent021cf962014-05-13 10:18:14 -07002456 sp<Client> client = registerPid(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002457
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002458 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002459 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2460 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002461 if (handle != 0 && id != NULL) {
2462 *id = handle->id();
2463 }
Eric Laurentfe1a94e2014-05-26 16:03:08 -07002464 if (handle == 0) {
2465 // remove local strong reference to Client with mClientLock held
2466 Mutex::Autolock _cl(mClientLock);
2467 client.clear();
2468 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002469 }
2470
2471Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002472 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002473 return handle;
2474}
2475
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002476status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2477 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002478{
Steve Block3856b092011-10-20 11:56:00 +01002479 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002480 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002481 Mutex::Autolock _l(mLock);
2482 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002483 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002484 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002485 }
Eric Laurentde070132010-07-13 04:45:46 -07002486 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2487 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002488 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002489 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002490 }
Eric Laurentde070132010-07-13 04:45:46 -07002491 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2492 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002493 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002494 return BAD_VALUE;
2495 }
2496
2497 Mutex::Autolock _dl(dstThread->mLock);
2498 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002499 return moveEffectChain_l(sessionId, srcThread, dstThread, false);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002500}
2501
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002502// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002503status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002504 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002505 AudioFlinger::PlaybackThread *dstThread,
2506 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002507{
Steve Block3856b092011-10-20 11:56:00 +01002508 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002509 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002510
Eric Laurent59255e42011-07-27 19:49:51 -07002511 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002512 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002513 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002514 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002515 return INVALID_OPERATION;
2516 }
2517
Andy Hung9a592762014-07-21 21:56:01 -07002518 // Check whether the destination thread has a channel count of FCC_2, which is
2519 // currently required for (most) effects. Prevent moving the effect chain here rather
2520 // than disabling the addEffect_l() call in dstThread below.
2521 if (dstThread->mChannelCount != FCC_2) {
2522 ALOGW("moveEffectChain_l() effect chain failed because"
2523 " destination thread %p channel count(%u) != %u",
2524 dstThread, dstThread->mChannelCount, FCC_2);
2525 return INVALID_OPERATION;
2526 }
2527
Eric Laurent39e94f82010-07-28 01:32:47 -07002528 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002529 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002530 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002531 // removed.
2532 srcThread->removeEffectChain_l(chain);
2533
2534 // transfer all effects one by one so that new effect chain is created on new thread with
2535 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Eric Laurent39e94f82010-07-28 01:32:47 -07002536 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002537 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002538 sp<EffectModule> effect = chain->getEffectFromId_l(0);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002539 Vector< sp<EffectModule> > removed;
2540 status_t status = NO_ERROR;
Eric Laurentde070132010-07-13 04:45:46 -07002541 while (effect != 0) {
2542 srcThread->removeEffect_l(effect);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002543 removed.add(effect);
2544 status = dstThread->addEffect_l(effect);
2545 if (status != NO_ERROR) {
2546 break;
2547 }
Eric Laurentec35a142011-10-05 17:42:25 -07002548 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2549 if (effect->state() == EffectModule::ACTIVE ||
2550 effect->state() == EffectModule::STOPPING) {
2551 effect->start();
2552 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002553 // if the move request is not received from audio policy manager, the effect must be
2554 // re-registered with the new strategy and output
2555 if (dstChain == 0) {
2556 dstChain = effect->chain().promote();
2557 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002558 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002559 status = NO_INIT;
2560 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002561 }
2562 strategy = dstChain->strategy();
2563 }
2564 if (reRegister) {
2565 AudioSystem::unregisterEffect(effect->id());
2566 AudioSystem::registerEffect(&effect->desc(),
Eric Laurent5baf2af2013-09-12 17:37:00 -07002567 dstThread->id(),
Eric Laurent39e94f82010-07-28 01:32:47 -07002568 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002569 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002570 effect->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002571 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent39e94f82010-07-28 01:32:47 -07002572 }
Eric Laurentde070132010-07-13 04:45:46 -07002573 effect = chain->getEffectFromId_l(0);
2574 }
2575
Eric Laurent5baf2af2013-09-12 17:37:00 -07002576 if (status != NO_ERROR) {
2577 for (size_t i = 0; i < removed.size(); i++) {
2578 srcThread->addEffect_l(removed[i]);
2579 if (dstChain != 0 && reRegister) {
2580 AudioSystem::unregisterEffect(removed[i]->id());
2581 AudioSystem::registerEffect(&removed[i]->desc(),
2582 srcThread->id(),
2583 strategy,
2584 sessionId,
2585 removed[i]->id());
Eric Laurentd72b7c02013-10-12 16:17:46 -07002586 AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled());
Eric Laurent5baf2af2013-09-12 17:37:00 -07002587 }
2588 }
2589 }
2590
2591 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002592}
2593
Eric Laurent5baf2af2013-09-12 17:37:00 -07002594bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l()
Eric Laurent813e2a72013-08-31 12:59:48 -07002595{
2596 if (mGlobalEffectEnableTime != 0 &&
2597 ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) {
2598 return true;
2599 }
2600
2601 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2602 sp<EffectChain> ec =
2603 mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
Eric Laurent5baf2af2013-09-12 17:37:00 -07002604 if (ec != 0 && ec->isNonOffloadableEnabled()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002605 return true;
2606 }
2607 }
2608 return false;
2609}
2610
Eric Laurent5baf2af2013-09-12 17:37:00 -07002611void AudioFlinger::onNonOffloadableGlobalEffectEnable()
Eric Laurent813e2a72013-08-31 12:59:48 -07002612{
2613 Mutex::Autolock _l(mLock);
2614
2615 mGlobalEffectEnableTime = systemTime();
2616
2617 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2618 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
2619 if (t->mType == ThreadBase::OFFLOAD) {
2620 t->invalidateTracks(AUDIO_STREAM_MUSIC);
2621 }
2622 }
2623
2624}
2625
Glenn Kastenda6ef132013-01-10 12:31:01 -08002626struct Entry {
2627#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2628 char mName[MAX_NAME];
2629};
2630
2631int comparEntry(const void *p1, const void *p2)
2632{
2633 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2634}
2635
Glenn Kasten46909e72013-02-26 09:20:22 -08002636#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002637void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002638{
Eric Laurent81784c32012-11-19 14:55:58 -08002639 NBAIO_Source *teeSource = source.get();
2640 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002641 // .wav rotation
2642 // There is a benign race condition if 2 threads call this simultaneously.
2643 // They would both traverse the directory, but the result would simply be
2644 // failures at unlink() which are ignored. It's also unlikely since
2645 // normally dumpsys is only done by bugreport or from the command line.
2646 char teePath[32+256];
2647 strcpy(teePath, "/data/misc/media");
2648 size_t teePathLen = strlen(teePath);
2649 DIR *dir = opendir(teePath);
2650 teePath[teePathLen++] = '/';
2651 if (dir != NULL) {
2652#define MAX_SORT 20 // number of entries to sort
2653#define MAX_KEEP 10 // number of entries to keep
2654 struct Entry entries[MAX_SORT];
2655 size_t entryCount = 0;
2656 while (entryCount < MAX_SORT) {
2657 struct dirent de;
2658 struct dirent *result = NULL;
2659 int rc = readdir_r(dir, &de, &result);
2660 if (rc != 0) {
2661 ALOGW("readdir_r failed %d", rc);
2662 break;
2663 }
2664 if (result == NULL) {
2665 break;
2666 }
2667 if (result != &de) {
2668 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2669 break;
2670 }
2671 // ignore non .wav file entries
2672 size_t nameLen = strlen(de.d_name);
2673 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2674 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2675 continue;
2676 }
2677 strcpy(entries[entryCount++].mName, de.d_name);
2678 }
2679 (void) closedir(dir);
2680 if (entryCount > MAX_KEEP) {
2681 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2682 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2683 strcpy(&teePath[teePathLen], entries[i].mName);
2684 (void) unlink(teePath);
2685 }
2686 }
2687 } else {
2688 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002689 dprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002690 }
2691 }
Eric Laurent81784c32012-11-19 14:55:58 -08002692 char teeTime[16];
2693 struct timeval tv;
2694 gettimeofday(&tv, NULL);
2695 struct tm tm;
2696 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002697 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2698 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2699 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2700 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002701 if (teeFd >= 0) {
Glenn Kasten329f6512014-08-28 16:23:16 -07002702 // FIXME use libsndfile
Eric Laurent81784c32012-11-19 14:55:58 -08002703 char wavHeader[44];
2704 memcpy(wavHeader,
2705 "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",
2706 sizeof(wavHeader));
2707 NBAIO_Format format = teeSource->format();
2708 unsigned channelCount = Format_channelCount(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002709 uint32_t sampleRate = Format_sampleRate(format);
Glenn Kasten329f6512014-08-28 16:23:16 -07002710 size_t frameSize = Format_frameSize(format);
Eric Laurent81784c32012-11-19 14:55:58 -08002711 wavHeader[22] = channelCount; // number of channels
2712 wavHeader[24] = sampleRate; // sample rate
2713 wavHeader[25] = sampleRate >> 8;
Glenn Kasten329f6512014-08-28 16:23:16 -07002714 wavHeader[32] = frameSize; // block alignment
2715 wavHeader[33] = frameSize >> 8;
Eric Laurent81784c32012-11-19 14:55:58 -08002716 write(teeFd, wavHeader, sizeof(wavHeader));
2717 size_t total = 0;
2718 bool firstRead = true;
Glenn Kasten329f6512014-08-28 16:23:16 -07002719#define TEE_SINK_READ 1024 // frames per I/O operation
2720 void *buffer = malloc(TEE_SINK_READ * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002721 for (;;) {
Eric Laurent81784c32012-11-19 14:55:58 -08002722 size_t count = TEE_SINK_READ;
2723 ssize_t actual = teeSource->read(buffer, count,
2724 AudioBufferProvider::kInvalidPTS);
2725 bool wasFirstRead = firstRead;
2726 firstRead = false;
2727 if (actual <= 0) {
2728 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2729 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002730 }
Eric Laurent81784c32012-11-19 14:55:58 -08002731 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002732 }
Eric Laurent81784c32012-11-19 14:55:58 -08002733 ALOG_ASSERT(actual <= (ssize_t)count);
Glenn Kasten329f6512014-08-28 16:23:16 -07002734 write(teeFd, buffer, actual * frameSize);
Eric Laurent81784c32012-11-19 14:55:58 -08002735 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002736 }
Glenn Kasten329f6512014-08-28 16:23:16 -07002737 free(buffer);
Eric Laurent81784c32012-11-19 14:55:58 -08002738 lseek(teeFd, (off_t) 4, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002739 uint32_t temp = 44 + total * frameSize - 8;
2740 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002741 write(teeFd, &temp, sizeof(temp));
2742 lseek(teeFd, (off_t) 40, SEEK_SET);
Glenn Kasten329f6512014-08-28 16:23:16 -07002743 temp = total * frameSize;
2744 // FIXME not big-endian safe
Eric Laurent81784c32012-11-19 14:55:58 -08002745 write(teeFd, &temp, sizeof(temp));
2746 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002747 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002748 dprintf(fd, "tee copied to %s\n", teePath);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002749 }
Eric Laurent59255e42011-07-27 19:49:51 -07002750 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002751 if (fd >= 0) {
Elliott Hughes8b5f6422014-05-22 01:22:06 -07002752 dprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
Glenn Kastenda6ef132013-01-10 12:31:01 -08002753 }
Eric Laurent59255e42011-07-27 19:49:51 -07002754 }
2755 }
2756}
Glenn Kasten46909e72013-02-26 09:20:22 -08002757#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002758
Mathias Agopian65ab4712010-07-14 17:59:35 -07002759// ----------------------------------------------------------------------------
2760
2761status_t AudioFlinger::onTransact(
2762 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2763{
2764 return BnAudioFlinger::onTransact(code, data, reply, flags);
2765}
2766
Mathias Agopian65ab4712010-07-14 17:59:35 -07002767}; // namespace android