blob: 1bac09e90c777fbecd4e7e70a35fc4e7f4f91cbf [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";
Mathias Agopian65ab4712010-07-14 17:59:35 -070085
Glenn Kasten58912562012-04-03 10:45:00 -070086
John Grossman4ff14ba2012-02-08 16:37:41 -080087nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080088
Eric Laurent81784c32012-11-19 14:55:58 -080089uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070090
Glenn Kasten46909e72013-02-26 09:20:22 -080091#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080092bool AudioFlinger::mTeeSinkInputEnabled = false;
93bool AudioFlinger::mTeeSinkOutputEnabled = false;
94bool AudioFlinger::mTeeSinkTrackEnabled = false;
95
96size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
97size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
98size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -080099#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800100
Mathias Agopian65ab4712010-07-14 17:59:35 -0700101// ----------------------------------------------------------------------------
102
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700103static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700104{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700105 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700106 int rc;
107
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700108 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
109 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
110 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
111 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700112 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700113 }
114 rc = audio_hw_device_open(mod, dev);
115 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
116 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
117 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700118 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700119 }
120 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
121 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
122 rc = BAD_VALUE;
123 goto out;
124 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700125 return 0;
126
127out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700128 *dev = NULL;
129 return rc;
130}
131
Mathias Agopian65ab4712010-07-14 17:59:35 -0700132// ----------------------------------------------------------------------------
133
134AudioFlinger::AudioFlinger()
135 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800136 mPrimaryHardwareDev(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700137 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800138 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800139 mMasterMute(false),
140 mNextUniqueId(1),
141 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700142 mBtNrecIsOff(false),
143 mIsLowRamDevice(true),
144 mIsDeviceTypeKnown(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700145{
Glenn Kasten949a9262013-04-16 12:35:20 -0700146 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800147 char value[PROPERTY_VALUE_MAX];
148 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
149 if (doLog) {
150 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
151 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800152#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800153 (void) property_get("ro.debuggable", value, "0");
154 int debuggable = atoi(value);
155 int teeEnabled = 0;
156 if (debuggable) {
157 (void) property_get("af.tee", value, "0");
158 teeEnabled = atoi(value);
159 }
160 if (teeEnabled & 1)
161 mTeeSinkInputEnabled = true;
162 if (teeEnabled & 2)
163 mTeeSinkOutputEnabled = true;
164 if (teeEnabled & 4)
165 mTeeSinkTrackEnabled = true;
Glenn Kasten46909e72013-02-26 09:20:22 -0800166#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700167}
168
169void AudioFlinger::onFirstRef()
170{
Dima Zavin799a70e2011-04-18 16:57:27 -0700171 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700172
Eric Laurent93575202011-01-18 18:39:02 -0800173 Mutex::Autolock _l(mLock);
174
Dima Zavin799a70e2011-04-18 16:57:27 -0700175 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800176 char val_str[PROPERTY_VALUE_MAX] = { 0 };
177 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
178 uint32_t int_val;
179 if (1 == sscanf(val_str, "%u", &int_val)) {
180 mStandbyTimeInNsecs = milliseconds(int_val);
181 ALOGI("Using %u mSec as standby time.", int_val);
182 } else {
183 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
184 ALOGI("Using default %u mSec as standby time.",
185 (uint32_t)(mStandbyTimeInNsecs / 1000000));
186 }
187 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700188
Eric Laurenta4c5a552012-03-29 10:12:40 -0700189 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700190}
191
192AudioFlinger::~AudioFlinger()
193{
194 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700195 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700196 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700197 }
198 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700199 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700200 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700201 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700202
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800203 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
204 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700205 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
206 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700207 }
208}
209
Eric Laurenta4c5a552012-03-29 10:12:40 -0700210static const char * const audio_interfaces[] = {
211 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
212 AUDIO_HARDWARE_MODULE_ID_A2DP,
213 AUDIO_HARDWARE_MODULE_ID_USB,
214};
215#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
216
John Grossmanee578c02012-07-23 17:05:46 -0700217AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
218 audio_module_handle_t module,
219 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700220{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700221 // if module is 0, the request comes from an old policy manager and we should load
222 // well known modules
223 if (module == 0) {
224 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
225 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
226 loadHwModule_l(audio_interfaces[i]);
227 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700228 // then try to find a module supporting the requested device.
229 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
230 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
231 audio_hw_device_t *dev = audioHwDevice->hwDevice();
232 if ((dev->get_supported_devices != NULL) &&
233 (dev->get_supported_devices(dev) & devices) == devices)
234 return audioHwDevice;
235 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700236 } else {
237 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700238 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
239 if (audioHwDevice != NULL) {
240 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700241 }
242 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700243
Dima Zavin799a70e2011-04-18 16:57:27 -0700244 return NULL;
245}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700246
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700247void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700248{
249 const size_t SIZE = 256;
250 char buffer[SIZE];
251 String8 result;
252
253 result.append("Clients:\n");
254 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800255 sp<Client> client = mClients.valueAt(i).promote();
256 if (client != 0) {
257 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
258 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700259 }
260 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700261
262 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800263 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700264 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
265 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800266 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700267 result.append(buffer);
268 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700269 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700270}
271
272
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700273void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700274{
275 const size_t SIZE = 256;
276 char buffer[SIZE];
277 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800278 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700279
John Grossman4ff14ba2012-02-08 16:37:41 -0800280 snprintf(buffer, SIZE, "Hardware status: %d\n"
281 "Standby Time mSec: %u\n",
282 hardwareStatus,
283 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700284 result.append(buffer);
285 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700286}
287
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700288void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700289{
290 const size_t SIZE = 256;
291 char buffer[SIZE];
292 String8 result;
293 snprintf(buffer, SIZE, "Permission Denial: "
294 "can't dump AudioFlinger from pid=%d, uid=%d\n",
295 IPCThreadState::self()->getCallingPid(),
296 IPCThreadState::self()->getCallingUid());
297 result.append(buffer);
298 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700299}
300
Eric Laurent81784c32012-11-19 14:55:58 -0800301bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700302{
303 bool locked = false;
304 for (int i = 0; i < kDumpLockRetries; ++i) {
305 if (mutex.tryLock() == NO_ERROR) {
306 locked = true;
307 break;
308 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800309 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310 }
311 return locked;
312}
313
314status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
315{
Glenn Kasten44deb052012-02-05 18:09:08 -0800316 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700317 dumpPermissionDenial(fd, args);
318 } else {
319 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800320 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700321 if (!hardwareLocked) {
322 String8 result(kHardwareLockedString);
323 write(fd, result.string(), result.size());
324 } else {
325 mHardwareLock.unlock();
326 }
327
Eric Laurent81784c32012-11-19 14:55:58 -0800328 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700329
330 // failed to lock - AudioFlinger is probably deadlocked
331 if (!locked) {
332 String8 result(kDeadlockedString);
333 write(fd, result.string(), result.size());
334 }
335
336 dumpClients(fd, args);
337 dumpInternals(fd, args);
338
339 // dump playback threads
340 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
341 mPlaybackThreads.valueAt(i)->dump(fd, args);
342 }
343
344 // dump record threads
345 for (size_t i = 0; i < mRecordThreads.size(); i++) {
346 mRecordThreads.valueAt(i)->dump(fd, args);
347 }
348
Dima Zavin799a70e2011-04-18 16:57:27 -0700349 // dump all hardware devs
350 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700351 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700352 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700353 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700354
Glenn Kasten46909e72013-02-26 09:20:22 -0800355#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700356 // dump the serially shared record tee sink
357 if (mRecordTeeSource != 0) {
358 dumpTee(fd, mRecordTeeSource);
359 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800360#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700361
Glenn Kastend65d73c2012-06-22 17:21:07 -0700362 if (locked) {
363 mLock.unlock();
364 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800365
366 // append a copy of media.log here by forwarding fd to it, but don't attempt
367 // to lookup the service if it's not running, as it will block for a second
368 if (mLogMemoryDealer != 0) {
369 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
370 if (binder != 0) {
371 fdprintf(fd, "\nmedia.log:\n");
372 Vector<String16> args;
373 binder->dump(fd, args);
374 }
375 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700376 }
377 return NO_ERROR;
378}
379
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800380sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
381{
382 // If pid is already in the mClients wp<> map, then use that entry
383 // (for which promote() is always != 0), otherwise create a new entry and Client.
384 sp<Client> client = mClients.valueFor(pid).promote();
385 if (client == 0) {
386 client = new Client(this, pid);
387 mClients.add(pid, client);
388 }
389
390 return client;
391}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392
Glenn Kasten9e58b552013-01-18 15:09:48 -0800393sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
394{
395 if (mLogMemoryDealer == 0) {
396 return new NBLog::Writer();
397 }
398 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
399 sp<NBLog::Writer> writer = new NBLog::Writer(size, shared);
400 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
401 if (binder != 0) {
402 interface_cast<IMediaLogService>(binder)->registerWriter(shared, size, name);
403 }
404 return writer;
405}
406
407void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
408{
Glenn Kasten685ef092013-02-04 08:15:34 -0800409 if (writer == 0) {
410 return;
411 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800412 sp<IMemory> iMemory(writer->getIMemory());
413 if (iMemory == 0) {
414 return;
415 }
416 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
417 if (binder != 0) {
418 interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory);
419 // Now the media.log remote reference to IMemory is gone.
420 // When our last local reference to IMemory also drops to zero,
421 // the IMemory destructor will deallocate the region from mMemoryDealer.
422 }
423}
424
Mathias Agopian65ab4712010-07-14 17:59:35 -0700425// IAudioFlinger interface
426
427
428sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800429 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700430 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800431 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700432 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800433 size_t frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800434 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800436 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800437 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700438 int *sessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700439 String8& name,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700440 status_t *status)
441{
442 sp<PlaybackThread::Track> track;
443 sp<TrackHandle> trackHandle;
444 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700445 status_t lStatus;
446 int lSessionId;
447
Glenn Kasten263709e2012-01-06 08:40:01 -0800448 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
449 // but if someone uses binder directly they could bypass that and cause us to crash
450 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000451 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700452 lStatus = BAD_VALUE;
453 goto Exit;
454 }
455
Glenn Kasten60a83922012-06-21 12:56:37 -0700456 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
457 // and we don't yet support 8.24 or 32-bit PCM
458 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
459 ALOGE("createTrack() invalid format %d", format);
460 lStatus = BAD_VALUE;
461 goto Exit;
462 }
463
Mathias Agopian65ab4712010-07-14 17:59:35 -0700464 {
465 Mutex::Autolock _l(mLock);
466 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700467 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700468 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800469 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700470 lStatus = BAD_VALUE;
471 goto Exit;
472 }
473
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800474 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800475 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700476
Steve Block3856b092011-10-20 11:56:00 +0100477 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700478 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700479 // check if an effect chain with the same session ID is present on another
480 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700481 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700482 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
483 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700484 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700485 if (sessions & PlaybackThread::EFFECT_SESSION) {
486 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700487 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700488 }
Eric Laurentde070132010-07-13 04:45:46 -0700489 }
490 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700491 lSessionId = *sessionId;
492 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700493 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700494 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700495 if (sessionId != NULL) {
496 *sessionId = lSessionId;
497 }
498 }
Steve Block3856b092011-10-20 11:56:00 +0100499 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700500
501 track = thread->createTrack_l(client, streamType, sampleRate, format,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800502 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus);
Eric Laurent39e94f82010-07-28 01:32:47 -0700503
504 // move effect chain to this output thread if an effect on same session was waiting
505 // for a track to be created
506 if (lStatus == NO_ERROR && effectThread != NULL) {
507 Mutex::Autolock _dl(thread->mLock);
508 Mutex::Autolock _sl(effectThread->mLock);
509 moveEffectChain_l(lSessionId, effectThread, thread, true);
510 }
Eric Laurenta011e352012-03-29 15:51:43 -0700511
512 // Look for sync events awaiting for a session to be used.
513 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
514 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
515 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700516 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700517 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700518 } else {
519 mPendingSyncEvents[i]->cancel();
520 }
Eric Laurenta011e352012-03-29 15:51:43 -0700521 mPendingSyncEvents.removeAt(i);
522 i--;
523 }
524 }
525 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700526 }
527 if (lStatus == NO_ERROR) {
Glenn Kastend054c322013-07-12 12:59:20 -0700528 // s for server's pid, n for normal mixer name, f for fast index
529 name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,
530 track->fastIndex());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700531 trackHandle = new TrackHandle(track);
532 } else {
533 // remove local strong reference to Client before deleting the Track so that the Client
534 // destructor is called by the TrackBase destructor with mLock held
535 client.clear();
536 track.clear();
537 }
538
539Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -0700540 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700541 return trackHandle;
542}
543
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800544uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700545{
546 Mutex::Autolock _l(mLock);
547 PlaybackThread *thread = checkPlaybackThread_l(output);
548 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000549 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550 return 0;
551 }
552 return thread->sampleRate();
553}
554
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800555int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700556{
557 Mutex::Autolock _l(mLock);
558 PlaybackThread *thread = checkPlaybackThread_l(output);
559 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000560 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700561 return 0;
562 }
563 return thread->channelCount();
564}
565
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800566audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700567{
568 Mutex::Autolock _l(mLock);
569 PlaybackThread *thread = checkPlaybackThread_l(output);
570 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000571 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800572 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700573 }
574 return thread->format();
575}
576
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800577size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700578{
579 Mutex::Autolock _l(mLock);
580 PlaybackThread *thread = checkPlaybackThread_l(output);
581 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000582 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700583 return 0;
584 }
Glenn Kasten58912562012-04-03 10:45:00 -0700585 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
586 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587 return thread->frameCount();
588}
589
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800590uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700591{
592 Mutex::Autolock _l(mLock);
593 PlaybackThread *thread = checkPlaybackThread_l(output);
594 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800595 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700596 return 0;
597 }
598 return thread->latency();
599}
600
601status_t AudioFlinger::setMasterVolume(float value)
602{
Eric Laurenta1884f92011-08-23 08:25:03 -0700603 status_t ret = initCheck();
604 if (ret != NO_ERROR) {
605 return ret;
606 }
607
Mathias Agopian65ab4712010-07-14 17:59:35 -0700608 // check calling permissions
609 if (!settingsAllowed()) {
610 return PERMISSION_DENIED;
611 }
612
Eric Laurenta4c5a552012-03-29 10:12:40 -0700613 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700614 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700615
John Grossmanee578c02012-07-23 17:05:46 -0700616 // Set master volume in the HALs which support it.
617 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
618 AutoMutex lock(mHardwareLock);
619 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800620
John Grossmanee578c02012-07-23 17:05:46 -0700621 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
622 if (dev->canSetMasterVolume()) {
623 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800624 }
John Grossmanee578c02012-07-23 17:05:46 -0700625 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700626 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700627
John Grossmanee578c02012-07-23 17:05:46 -0700628 // Now set the master volume in each playback thread. Playback threads
629 // assigned to HALs which do not have master volume support will apply
630 // master volume during the mix operation. Threads with HALs which do
631 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800632 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700633 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634
635 return NO_ERROR;
636}
637
Glenn Kastenf78aee72012-01-04 11:00:47 -0800638status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700639{
Eric Laurenta1884f92011-08-23 08:25:03 -0700640 status_t ret = initCheck();
641 if (ret != NO_ERROR) {
642 return ret;
643 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700644
645 // check calling permissions
646 if (!settingsAllowed()) {
647 return PERMISSION_DENIED;
648 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800649 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000650 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700651 return BAD_VALUE;
652 }
653
654 { // scope for the lock
655 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700656 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700658 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700659 mHardwareStatus = AUDIO_HW_IDLE;
660 }
661
662 if (NO_ERROR == ret) {
663 Mutex::Autolock _l(mLock);
664 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800665 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700666 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700667 }
668
669 return ret;
670}
671
672status_t AudioFlinger::setMicMute(bool state)
673{
Eric Laurenta1884f92011-08-23 08:25:03 -0700674 status_t ret = initCheck();
675 if (ret != NO_ERROR) {
676 return ret;
677 }
678
Mathias Agopian65ab4712010-07-14 17:59:35 -0700679 // check calling permissions
680 if (!settingsAllowed()) {
681 return PERMISSION_DENIED;
682 }
683
684 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700685 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700686 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700687 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700688 mHardwareStatus = AUDIO_HW_IDLE;
689 return ret;
690}
691
692bool AudioFlinger::getMicMute() const
693{
Eric Laurenta1884f92011-08-23 08:25:03 -0700694 status_t ret = initCheck();
695 if (ret != NO_ERROR) {
696 return false;
697 }
698
Dima Zavinfce7a472011-04-19 22:30:36 -0700699 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800700 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700701 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700702 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700703 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704 mHardwareStatus = AUDIO_HW_IDLE;
705 return state;
706}
707
708status_t AudioFlinger::setMasterMute(bool muted)
709{
John Grossmand8f178d2012-07-20 14:51:35 -0700710 status_t ret = initCheck();
711 if (ret != NO_ERROR) {
712 return ret;
713 }
714
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715 // check calling permissions
716 if (!settingsAllowed()) {
717 return PERMISSION_DENIED;
718 }
719
John Grossmanee578c02012-07-23 17:05:46 -0700720 Mutex::Autolock _l(mLock);
721 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700722
John Grossmanee578c02012-07-23 17:05:46 -0700723 // Set master mute in the HALs which support it.
724 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
725 AutoMutex lock(mHardwareLock);
726 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700727
John Grossmanee578c02012-07-23 17:05:46 -0700728 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
729 if (dev->canSetMasterMute()) {
730 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700731 }
John Grossmanee578c02012-07-23 17:05:46 -0700732 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700733 }
734
John Grossmanee578c02012-07-23 17:05:46 -0700735 // Now set the master mute in each playback thread. Playback threads
736 // assigned to HALs which do not have master mute support will apply master
737 // mute during the mix operation. Threads with HALs which do support master
738 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800739 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700740 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700741
742 return NO_ERROR;
743}
744
745float AudioFlinger::masterVolume() const
746{
Glenn Kasten98067102011-12-13 11:47:54 -0800747 Mutex::Autolock _l(mLock);
748 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700749}
750
751bool AudioFlinger::masterMute() const
752{
Glenn Kasten98067102011-12-13 11:47:54 -0800753 Mutex::Autolock _l(mLock);
754 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700755}
756
John Grossman4ff14ba2012-02-08 16:37:41 -0800757float AudioFlinger::masterVolume_l() const
758{
John Grossman4ff14ba2012-02-08 16:37:41 -0800759 return mMasterVolume;
760}
761
John Grossmand8f178d2012-07-20 14:51:35 -0700762bool AudioFlinger::masterMute_l() const
763{
John Grossmanee578c02012-07-23 17:05:46 -0700764 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700765}
766
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800767status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
768 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700769{
770 // check calling permissions
771 if (!settingsAllowed()) {
772 return PERMISSION_DENIED;
773 }
774
Glenn Kasten263709e2012-01-06 08:40:01 -0800775 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000776 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700777 return BAD_VALUE;
778 }
779
780 AutoMutex lock(mLock);
781 PlaybackThread *thread = NULL;
782 if (output) {
783 thread = checkPlaybackThread_l(output);
784 if (thread == NULL) {
785 return BAD_VALUE;
786 }
787 }
788
789 mStreamTypes[stream].volume = value;
790
791 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800792 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700793 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794 }
795 } else {
796 thread->setStreamVolume(stream, value);
797 }
798
799 return NO_ERROR;
800}
801
Glenn Kastenfff6d712012-01-12 16:38:12 -0800802status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700803{
804 // check calling permissions
805 if (!settingsAllowed()) {
806 return PERMISSION_DENIED;
807 }
808
Glenn Kasten263709e2012-01-06 08:40:01 -0800809 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700810 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000811 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812 return BAD_VALUE;
813 }
814
Eric Laurent93575202011-01-18 18:39:02 -0800815 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700816 mStreamTypes[stream].mute = muted;
817 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700818 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819
820 return NO_ERROR;
821}
822
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800823float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700824{
Glenn Kasten263709e2012-01-06 08:40:01 -0800825 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700826 return 0.0f;
827 }
828
829 AutoMutex lock(mLock);
830 float volume;
831 if (output) {
832 PlaybackThread *thread = checkPlaybackThread_l(output);
833 if (thread == NULL) {
834 return 0.0f;
835 }
836 volume = thread->streamVolume(stream);
837 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800838 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700839 }
840
841 return volume;
842}
843
Glenn Kastenfff6d712012-01-12 16:38:12 -0800844bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700845{
Glenn Kasten263709e2012-01-06 08:40:01 -0800846 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700847 return true;
848 }
849
Glenn Kasten6637baa2012-01-09 09:40:36 -0800850 AutoMutex lock(mLock);
851 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700852}
853
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800854status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700855{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700856 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
857 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800858
Mathias Agopian65ab4712010-07-14 17:59:35 -0700859 // check calling permissions
860 if (!settingsAllowed()) {
861 return PERMISSION_DENIED;
862 }
863
Mathias Agopian65ab4712010-07-14 17:59:35 -0700864 // ioHandle == 0 means the parameters are global to the audio hardware interface
865 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700866 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700867 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800868 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700869 AutoMutex lock(mHardwareLock);
870 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
871 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
872 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
873 status_t result = dev->set_parameters(dev, keyValuePairs.string());
874 final_result = result ?: final_result;
875 }
876 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800877 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700878 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
879 AudioParameter param = AudioParameter(keyValuePairs);
880 String8 value;
881 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700882 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
883 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700884 for (size_t i = 0; i < mRecordThreads.size(); i++) {
885 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700886 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700887 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
888 // collect all of the thread's session IDs
889 KeyedVector<int, bool> ids = thread->sessionIds();
890 // suspend effects associated with those session IDs
891 for (size_t j = 0; j < ids.size(); ++j) {
892 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700893 thread->setEffectSuspended(FX_IID_AEC,
894 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700895 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700896 thread->setEffectSuspended(FX_IID_NS,
897 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700898 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700899 }
900 }
Eric Laurentbee53372011-08-29 12:42:48 -0700901 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700902 }
903 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700904 String8 screenState;
905 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
906 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800907 if (isOff != (AudioFlinger::mScreenState & 1)) {
908 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700909 }
910 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700911 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700912 }
913
914 // hold a strong ref on thread in case closeOutput() or closeInput() is called
915 // and the thread is exited once the lock is released
916 sp<ThreadBase> thread;
917 {
918 Mutex::Autolock _l(mLock);
919 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700920 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700921 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800922 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700923 // indicate output device change to all input threads for pre processing
924 AudioParameter param = AudioParameter(keyValuePairs);
925 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700926 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
927 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700928 for (size_t i = 0; i < mRecordThreads.size(); i++) {
929 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
930 }
931 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700932 }
933 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800934 if (thread != 0) {
935 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700936 }
937 return BAD_VALUE;
938}
939
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800940String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700941{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700942 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
943 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944
Eric Laurenta4c5a552012-03-29 10:12:40 -0700945 Mutex::Autolock _l(mLock);
946
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700948 String8 out_s8;
949
Dima Zavin799a70e2011-04-18 16:57:27 -0700950 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800951 char *s;
952 {
953 AutoMutex lock(mHardwareLock);
954 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700955 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800956 s = dev->get_parameters(dev, keys.string());
957 mHardwareStatus = AUDIO_HW_IDLE;
958 }
John Grossmanef7740b2012-02-09 11:28:36 -0800959 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700960 free(s);
961 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700962 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700963 }
964
Mathias Agopian65ab4712010-07-14 17:59:35 -0700965 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
966 if (playbackThread != NULL) {
967 return playbackThread->getParameters(keys);
968 }
969 RecordThread *recordThread = checkRecordThread_l(ioHandle);
970 if (recordThread != NULL) {
971 return recordThread->getParameters(keys);
972 }
973 return String8("");
974}
975
Glenn Kastendd8104c2012-07-02 12:42:44 -0700976size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
977 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700978{
Eric Laurenta1884f92011-08-23 08:25:03 -0700979 status_t ret = initCheck();
980 if (ret != NO_ERROR) {
981 return 0;
982 }
983
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800984 AutoMutex lock(mHardwareLock);
985 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000986 struct audio_config config;
987 memset(&config, 0, sizeof(config));
988 config.sample_rate = sampleRate;
989 config.channel_mask = channelMask;
990 config.format = format;
991
John Grossmanee578c02012-07-23 17:05:46 -0700992 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
993 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800994 mHardwareStatus = AUDIO_HW_IDLE;
995 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700996}
997
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800998unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700999{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001000 Mutex::Autolock _l(mLock);
1001
1002 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1003 if (recordThread != NULL) {
1004 return recordThread->getInputFramesLost();
1005 }
1006 return 0;
1007}
1008
1009status_t AudioFlinger::setVoiceVolume(float value)
1010{
Eric Laurenta1884f92011-08-23 08:25:03 -07001011 status_t ret = initCheck();
1012 if (ret != NO_ERROR) {
1013 return ret;
1014 }
1015
Mathias Agopian65ab4712010-07-14 17:59:35 -07001016 // check calling permissions
1017 if (!settingsAllowed()) {
1018 return PERMISSION_DENIED;
1019 }
1020
1021 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001022 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001023 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001024 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001025 mHardwareStatus = AUDIO_HW_IDLE;
1026
1027 return ret;
1028}
1029
Glenn Kasten26c77552012-11-16 12:01:44 -08001030status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001031 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001032{
1033 status_t status;
1034
1035 Mutex::Autolock _l(mLock);
1036
1037 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1038 if (playbackThread != NULL) {
1039 return playbackThread->getRenderPosition(halFrames, dspFrames);
1040 }
1041
1042 return BAD_VALUE;
1043}
1044
1045void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1046{
1047
1048 Mutex::Autolock _l(mLock);
1049
Glenn Kastenbb001922012-02-03 11:10:26 -08001050 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001051 if (mNotificationClients.indexOfKey(pid) < 0) {
1052 sp<NotificationClient> notificationClient = new NotificationClient(this,
1053 client,
1054 pid);
Steve Block3856b092011-10-20 11:56:00 +01001055 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001056
1057 mNotificationClients.add(pid, notificationClient);
1058
1059 sp<IBinder> binder = client->asBinder();
1060 binder->linkToDeath(notificationClient);
1061
1062 // the config change is always sent from playback or record threads to avoid deadlock
1063 // with AudioSystem::gLock
1064 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001065 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001066 }
1067
1068 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001069 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001070 }
1071 }
1072}
1073
1074void AudioFlinger::removeNotificationClient(pid_t pid)
1075{
1076 Mutex::Autolock _l(mLock);
1077
Glenn Kastena3b09252012-01-20 09:19:01 -08001078 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001079
Steve Block3856b092011-10-20 11:56:00 +01001080 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001081 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001082 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001083 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001084 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001085 ALOGV(" pid %d @ %d", ref->mPid, i);
1086 if (ref->mPid == pid) {
1087 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001088 mAudioSessionRefs.removeAt(i);
1089 delete ref;
1090 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001091 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001092 } else {
1093 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001094 }
1095 }
1096 if (removed) {
1097 purgeStaleEffects_l();
1098 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001099}
1100
1101// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001102void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001103{
1104 size_t size = mNotificationClients.size();
1105 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001106 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1107 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001108 }
1109}
1110
1111// removeClient_l() must be called with AudioFlinger::mLock held
1112void AudioFlinger::removeClient_l(pid_t pid)
1113{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001114 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001115 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001116 mClients.removeItem(pid);
1117}
1118
Eric Laurent717e1282012-06-29 16:36:52 -07001119// getEffectThread_l() must be called with AudioFlinger::mLock held
1120sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1121{
1122 sp<PlaybackThread> thread;
1123
1124 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1125 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1126 ALOG_ASSERT(thread == 0);
1127 thread = mPlaybackThreads.valueAt(i);
1128 }
1129 }
1130
1131 return thread;
1132}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001133
Mathias Agopian65ab4712010-07-14 17:59:35 -07001134
Mathias Agopian65ab4712010-07-14 17:59:35 -07001135
1136// ----------------------------------------------------------------------------
1137
1138AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1139 : RefBase(),
1140 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001141 // 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 -07001142 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001143 mPid(pid),
1144 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001145{
1146 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1147}
1148
1149// Client destructor must be called with AudioFlinger::mLock held
1150AudioFlinger::Client::~Client()
1151{
1152 mAudioFlinger->removeClient_l(mPid);
1153}
1154
Glenn Kasten435dbe62012-01-30 10:15:48 -08001155sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001156{
1157 return mMemoryDealer;
1158}
1159
John Grossman4ff14ba2012-02-08 16:37:41 -08001160// Reserve one of the limited slots for a timed audio track associated
1161// with this client
1162bool AudioFlinger::Client::reserveTimedTrack()
1163{
1164 const int kMaxTimedTracksPerClient = 4;
1165
1166 Mutex::Autolock _l(mTimedTrackLock);
1167
1168 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1169 ALOGW("can not create timed track - pid %d has exceeded the limit",
1170 mPid);
1171 return false;
1172 }
1173
1174 mTimedTrackCount++;
1175 return true;
1176}
1177
1178// Release a slot for a timed audio track
1179void AudioFlinger::Client::releaseTimedTrack()
1180{
1181 Mutex::Autolock _l(mTimedTrackLock);
1182 mTimedTrackCount--;
1183}
1184
Mathias Agopian65ab4712010-07-14 17:59:35 -07001185// ----------------------------------------------------------------------------
1186
1187AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1188 const sp<IAudioFlingerClient>& client,
1189 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001190 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001191{
1192}
1193
1194AudioFlinger::NotificationClient::~NotificationClient()
1195{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001196}
1197
1198void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1199{
1200 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001201 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001202}
1203
Mathias Agopian65ab4712010-07-14 17:59:35 -07001204
1205// ----------------------------------------------------------------------------
1206
1207sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001208 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001209 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001210 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001211 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001212 size_t frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001213 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001214 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001215 int *sessionId,
1216 status_t *status)
1217{
1218 sp<RecordThread::RecordTrack> recordTrack;
1219 sp<RecordHandle> recordHandle;
1220 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001221 status_t lStatus;
1222 RecordThread *thread;
1223 size_t inFrameCount;
1224 int lSessionId;
1225
1226 // check calling permissions
1227 if (!recordingAllowed()) {
1228 lStatus = PERMISSION_DENIED;
1229 goto Exit;
1230 }
1231
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001232 if (format != AUDIO_FORMAT_PCM_16_BIT) {
1233 ALOGE("openRecord() invalid format %d", format);
1234 lStatus = BAD_VALUE;
1235 goto Exit;
1236 }
1237
Mathias Agopian65ab4712010-07-14 17:59:35 -07001238 // add client to list
1239 { // scope for mLock
1240 Mutex::Autolock _l(mLock);
1241 thread = checkRecordThread_l(input);
1242 if (thread == NULL) {
1243 lStatus = BAD_VALUE;
1244 goto Exit;
1245 }
1246
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001247 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001248 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001249
1250 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001251 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001252 lSessionId = *sessionId;
1253 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001254 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001255 if (sessionId != NULL) {
1256 *sessionId = lSessionId;
1257 }
1258 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001259 // create new record track.
1260 // The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07001261 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kastenddb0ccf2013-07-31 16:14:50 -07001262 frameCount, lSessionId, flags, tid, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001263 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001264 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001265 // remove local strong reference to Client before deleting the RecordTrack so that the
1266 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001267 client.clear();
1268 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001269 goto Exit;
1270 }
1271
1272 // return to handle to client
1273 recordHandle = new RecordHandle(recordTrack);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001274
1275Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07001276 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001277 return recordHandle;
1278}
1279
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001280
1281
Mathias Agopian65ab4712010-07-14 17:59:35 -07001282// ----------------------------------------------------------------------------
1283
Eric Laurenta4c5a552012-03-29 10:12:40 -07001284audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1285{
1286 if (!settingsAllowed()) {
1287 return 0;
1288 }
1289 Mutex::Autolock _l(mLock);
1290 return loadHwModule_l(name);
1291}
1292
1293// loadHwModule_l() must be called with AudioFlinger::mLock held
1294audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1295{
1296 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1297 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1298 ALOGW("loadHwModule() module %s already loaded", name);
1299 return mAudioHwDevs.keyAt(i);
1300 }
1301 }
1302
Eric Laurenta4c5a552012-03-29 10:12:40 -07001303 audio_hw_device_t *dev;
1304
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001305 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001306 if (rc) {
1307 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1308 return 0;
1309 }
1310
1311 mHardwareStatus = AUDIO_HW_INIT;
1312 rc = dev->init_check(dev);
1313 mHardwareStatus = AUDIO_HW_IDLE;
1314 if (rc) {
1315 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1316 return 0;
1317 }
1318
John Grossmanee578c02012-07-23 17:05:46 -07001319 // Check and cache this HAL's level of support for master mute and master
1320 // volume. If this is the first HAL opened, and it supports the get
1321 // methods, use the initial values provided by the HAL as the current
1322 // master mute and volume settings.
1323
1324 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1325 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001326 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001327
1328 if (0 == mAudioHwDevs.size()) {
1329 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1330 if (NULL != dev->get_master_volume) {
1331 float mv;
1332 if (OK == dev->get_master_volume(dev, &mv)) {
1333 mMasterVolume = mv;
1334 }
1335 }
1336
1337 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1338 if (NULL != dev->get_master_mute) {
1339 bool mm;
1340 if (OK == dev->get_master_mute(dev, &mm)) {
1341 mMasterMute = mm;
1342 }
1343 }
1344 }
1345
Eric Laurenta4c5a552012-03-29 10:12:40 -07001346 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001347 if ((NULL != dev->set_master_volume) &&
1348 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1349 flags = static_cast<AudioHwDevice::Flags>(flags |
1350 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1351 }
1352
1353 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1354 if ((NULL != dev->set_master_mute) &&
1355 (OK == dev->set_master_mute(dev, mMasterMute))) {
1356 flags = static_cast<AudioHwDevice::Flags>(flags |
1357 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1358 }
1359
Eric Laurenta4c5a552012-03-29 10:12:40 -07001360 mHardwareStatus = AUDIO_HW_IDLE;
1361 }
1362
1363 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001364 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001365
1366 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001367 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001368
1369 return handle;
1370
1371}
1372
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001373// ----------------------------------------------------------------------------
1374
Glenn Kasten3b16c762012-11-14 08:44:39 -08001375uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001376{
1377 Mutex::Autolock _l(mLock);
1378 PlaybackThread *thread = primaryPlaybackThread_l();
1379 return thread != NULL ? thread->sampleRate() : 0;
1380}
1381
Glenn Kastene33054e2012-11-14 12:54:39 -08001382size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001383{
1384 Mutex::Autolock _l(mLock);
1385 PlaybackThread *thread = primaryPlaybackThread_l();
1386 return thread != NULL ? thread->frameCountHAL() : 0;
1387}
1388
1389// ----------------------------------------------------------------------------
1390
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001391status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1392{
1393 uid_t uid = IPCThreadState::self()->getCallingUid();
1394 if (uid != AID_SYSTEM) {
1395 return PERMISSION_DENIED;
1396 }
1397 Mutex::Autolock _l(mLock);
1398 if (mIsDeviceTypeKnown) {
1399 return INVALID_OPERATION;
1400 }
1401 mIsLowRamDevice = isLowRamDevice;
1402 mIsDeviceTypeKnown = true;
1403 return NO_ERROR;
1404}
1405
1406// ----------------------------------------------------------------------------
1407
Eric Laurenta4c5a552012-03-29 10:12:40 -07001408audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1409 audio_devices_t *pDevices,
1410 uint32_t *pSamplingRate,
1411 audio_format_t *pFormat,
1412 audio_channel_mask_t *pChannelMask,
1413 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001414 audio_output_flags_t flags,
1415 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001416{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001417 PlaybackThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001418 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001419 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001420 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1421 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1422 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
Glenn Kasten937098b2013-06-26 11:19:36 -07001423 if (offloadInfo != NULL) {
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001424 config.offload_info = *offloadInfo;
1425 }
1426
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001427 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001428 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001429
Eric Laurentbfb1b832013-01-07 09:53:42 -08001430 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001431 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001432 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001433 config.sample_rate,
1434 config.format,
1435 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001436 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001437 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
1438 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001439
1440 if (pDevices == NULL || *pDevices == 0) {
1441 return 0;
1442 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001443
Mathias Agopian65ab4712010-07-14 17:59:35 -07001444 Mutex::Autolock _l(mLock);
1445
Eric Laurenta4c5a552012-03-29 10:12:40 -07001446 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001447 if (outHwDev == NULL)
1448 return 0;
1449
John Grossmanee578c02012-07-23 17:05:46 -07001450 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001451 audio_io_handle_t id = nextUniqueId();
1452
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001453 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001454
Glenn Kasten34542ac2013-06-26 11:29:02 -07001455 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001456 id,
1457 *pDevices,
1458 (audio_output_flags_t)flags,
1459 &config,
1460 &outStream);
1461
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001462 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001463 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001464 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001465 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001466 config.sample_rate,
1467 config.format,
1468 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001469 status);
1470
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001471 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001472 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001473
Eric Laurentbfb1b832013-01-07 09:53:42 -08001474 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1475 thread = new OffloadThread(this, output, id, *pDevices);
1476 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1477 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001478 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1479 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001480 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001481 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001482 } else {
1483 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001484 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001485 }
1486 mPlaybackThreads.add(id, thread);
1487
Glenn Kasten7c027242012-12-26 14:43:16 -08001488 if (pSamplingRate != NULL) {
1489 *pSamplingRate = config.sample_rate;
1490 }
1491 if (pFormat != NULL) {
1492 *pFormat = config.format;
1493 }
1494 if (pChannelMask != NULL) {
1495 *pChannelMask = config.channel_mask;
1496 }
1497 if (pLatencyMs != NULL) {
1498 *pLatencyMs = thread->latency();
1499 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001500
1501 // notify client processes of the new output creation
1502 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001503
1504 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001505 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001506 ALOGI("Using module %d has the primary audio interface", module);
1507 mPrimaryHardwareDev = outHwDev;
1508
1509 AutoMutex lock(mHardwareLock);
1510 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001511 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001512 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001513 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001514 return id;
1515 }
1516
1517 return 0;
1518}
1519
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001520audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1521 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001522{
1523 Mutex::Autolock _l(mLock);
1524 MixerThread *thread1 = checkMixerThread_l(output1);
1525 MixerThread *thread2 = checkMixerThread_l(output2);
1526
1527 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001528 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1529 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001530 return 0;
1531 }
1532
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001533 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001534 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1535 thread->addOutputTrack(thread2);
1536 mPlaybackThreads.add(id, thread);
1537 // notify client processes of the new output creation
1538 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1539 return id;
1540}
1541
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001542status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001543{
Glenn Kastend96c5722012-04-25 13:44:49 -07001544 return closeOutput_nonvirtual(output);
1545}
1546
1547status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1548{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001549 // keep strong reference on the playback thread so that
1550 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001551 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001552 {
1553 Mutex::Autolock _l(mLock);
1554 thread = checkPlaybackThread_l(output);
1555 if (thread == NULL) {
1556 return BAD_VALUE;
1557 }
1558
Steve Block3856b092011-10-20 11:56:00 +01001559 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001560
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001561 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001562 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001563 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001564 DuplicatingThread *dupThread =
1565 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001566 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001567
1568 }
1569 }
1570 }
1571
1572
1573 mPlaybackThreads.removeItem(output);
1574 // save all effects to the default thread
1575 if (mPlaybackThreads.size()) {
1576 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1577 if (dstThread != NULL) {
1578 // audioflinger lock is held here so the acquisition order of thread locks does not
1579 // matter
1580 Mutex::Autolock _dl(dstThread->mLock);
1581 Mutex::Autolock _sl(thread->mLock);
1582 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1583 for (size_t i = 0; i < effectChains.size(); i ++) {
1584 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001585 }
1586 }
1587 }
Glenn Kastena1117922012-01-26 10:53:32 -08001588 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001589 }
1590 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001591 // The thread entity (active unit of execution) is no longer running here,
1592 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001593
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001594 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001595 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001596 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001597 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001598 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001599 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001600 }
1601 return NO_ERROR;
1602}
1603
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001604status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001605{
1606 Mutex::Autolock _l(mLock);
1607 PlaybackThread *thread = checkPlaybackThread_l(output);
1608
1609 if (thread == NULL) {
1610 return BAD_VALUE;
1611 }
1612
Steve Block3856b092011-10-20 11:56:00 +01001613 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001614 thread->suspend();
1615
1616 return NO_ERROR;
1617}
1618
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001619status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001620{
1621 Mutex::Autolock _l(mLock);
1622 PlaybackThread *thread = checkPlaybackThread_l(output);
1623
1624 if (thread == NULL) {
1625 return BAD_VALUE;
1626 }
1627
Steve Block3856b092011-10-20 11:56:00 +01001628 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001629
1630 thread->restore();
1631
1632 return NO_ERROR;
1633}
1634
Eric Laurenta4c5a552012-03-29 10:12:40 -07001635audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1636 audio_devices_t *pDevices,
1637 uint32_t *pSamplingRate,
1638 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001639 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001640{
1641 status_t status;
1642 RecordThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001643 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001644 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001645 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1646 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1647 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1648
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001649 uint32_t reqSamplingRate = config.sample_rate;
1650 audio_format_t reqFormat = config.format;
Glenn Kastenf506e942013-08-06 10:49:43 -07001651 audio_channel_mask_t reqChannelMask = config.channel_mask;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001652 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001653 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001654
1655 if (pDevices == NULL || *pDevices == 0) {
1656 return 0;
1657 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001658
Mathias Agopian65ab4712010-07-14 17:59:35 -07001659 Mutex::Autolock _l(mLock);
1660
Eric Laurenta4c5a552012-03-29 10:12:40 -07001661 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001662 if (inHwDev == NULL)
1663 return 0;
1664
John Grossmanee578c02012-07-23 17:05:46 -07001665 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001666 audio_io_handle_t id = nextUniqueId();
1667
John Grossmanee578c02012-07-23 17:05:46 -07001668 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001669 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001670 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1671 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001672 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001673 config.sample_rate,
1674 config.format,
1675 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001676 status);
1677
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001678 // If the input could not be opened with the requested parameters and we can handle the
1679 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1680 // 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 -07001681 if (status == BAD_VALUE &&
1682 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1683 (config.sample_rate <= 2 * reqSamplingRate) &&
Glenn Kastenf506e942013-08-06 10:49:43 -07001684 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannelMask) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001685 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001686 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001687 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001688 }
1689
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001690 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001691
Glenn Kasten46909e72013-02-26 09:20:22 -08001692#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001693 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1694 // or (re-)create if current Pipe is idle and does not match the new format
1695 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001696 enum {
1697 TEE_SINK_NO, // don't copy input
1698 TEE_SINK_NEW, // copy input using a new pipe
1699 TEE_SINK_OLD, // copy input using an existing pipe
1700 } kind;
1701 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1702 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001703 if (!mTeeSinkInputEnabled) {
1704 kind = TEE_SINK_NO;
1705 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001706 kind = TEE_SINK_NO;
1707 } else if (mRecordTeeSink == 0) {
1708 kind = TEE_SINK_NEW;
1709 } else if (mRecordTeeSink->getStrongCount() != 1) {
1710 kind = TEE_SINK_NO;
1711 } else if (format == mRecordTeeSink->format()) {
1712 kind = TEE_SINK_OLD;
1713 } else {
1714 kind = TEE_SINK_NEW;
1715 }
1716 switch (kind) {
1717 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001718 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001719 size_t numCounterOffers = 0;
1720 const NBAIO_Format offers[1] = {format};
1721 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1722 ALOG_ASSERT(index == 0);
1723 PipeReader *pipeReader = new PipeReader(*pipe);
1724 numCounterOffers = 0;
1725 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1726 ALOG_ASSERT(index == 0);
1727 mRecordTeeSink = pipe;
1728 mRecordTeeSource = pipeReader;
1729 teeSink = pipe;
1730 }
1731 break;
1732 case TEE_SINK_OLD:
1733 teeSink = mRecordTeeSink;
1734 break;
1735 case TEE_SINK_NO:
1736 default:
1737 break;
1738 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001739#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001740
Dima Zavin799a70e2011-04-18 16:57:27 -07001741 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1742
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001743 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001744 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001745 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001746 thread = new RecordThread(this,
1747 input,
1748 reqSamplingRate,
Glenn Kastenf506e942013-08-06 10:49:43 -07001749 reqChannelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001750 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001751 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001752 *pDevices
1753#ifdef TEE_SINK
1754 , teeSink
1755#endif
1756 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001757 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001758 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001759 if (pSamplingRate != NULL) {
1760 *pSamplingRate = reqSamplingRate;
1761 }
1762 if (pFormat != NULL) {
1763 *pFormat = config.format;
1764 }
1765 if (pChannelMask != NULL) {
Glenn Kastenf506e942013-08-06 10:49:43 -07001766 *pChannelMask = reqChannelMask;
Glenn Kasten7c027242012-12-26 14:43:16 -08001767 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001768
Mathias Agopian65ab4712010-07-14 17:59:35 -07001769 // notify client processes of the new input creation
1770 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1771 return id;
1772 }
1773
1774 return 0;
1775}
1776
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001777status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001778{
Glenn Kastend96c5722012-04-25 13:44:49 -07001779 return closeInput_nonvirtual(input);
1780}
1781
1782status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1783{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001784 // keep strong reference on the record thread so that
1785 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001786 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001787 {
1788 Mutex::Autolock _l(mLock);
1789 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001790 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001791 return BAD_VALUE;
1792 }
1793
Steve Block3856b092011-10-20 11:56:00 +01001794 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001795 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001796 mRecordThreads.removeItem(input);
1797 }
1798 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001799 // The thread entity (active unit of execution) is no longer running here,
1800 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001801
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001802 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001803 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001804 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001805 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001806 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001807
1808 return NO_ERROR;
1809}
1810
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001811status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001812{
1813 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001814 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001815
1816 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1817 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001818 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001819 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001820
1821 return NO_ERROR;
1822}
1823
1824
1825int AudioFlinger::newAudioSessionId()
1826{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001827 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001828}
1829
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001830void AudioFlinger::acquireAudioSessionId(int audioSession)
1831{
1832 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001833 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001834 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001835 size_t num = mAudioSessionRefs.size();
1836 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001837 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001838 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1839 ref->mCnt++;
1840 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001841 return;
1842 }
1843 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001844 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1845 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001846}
1847
1848void AudioFlinger::releaseAudioSessionId(int audioSession)
1849{
1850 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001851 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001852 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001853 size_t num = mAudioSessionRefs.size();
1854 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001855 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001856 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1857 ref->mCnt--;
1858 ALOGV(" decremented refcount to %d", ref->mCnt);
1859 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001860 mAudioSessionRefs.removeAt(i);
1861 delete ref;
1862 purgeStaleEffects_l();
1863 }
1864 return;
1865 }
1866 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001867 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001868}
1869
1870void AudioFlinger::purgeStaleEffects_l() {
1871
Steve Block3856b092011-10-20 11:56:00 +01001872 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001873
1874 Vector< sp<EffectChain> > chains;
1875
1876 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1877 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1878 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1879 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001880 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1881 chains.push(ec);
1882 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001883 }
1884 }
1885 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1886 sp<RecordThread> t = mRecordThreads.valueAt(i);
1887 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1888 sp<EffectChain> ec = t->mEffectChains[j];
1889 chains.push(ec);
1890 }
1891 }
1892
1893 for (size_t i = 0; i < chains.size(); i++) {
1894 sp<EffectChain> ec = chains[i];
1895 int sessionid = ec->sessionId();
1896 sp<ThreadBase> t = ec->mThread.promote();
1897 if (t == 0) {
1898 continue;
1899 }
1900 size_t numsessionrefs = mAudioSessionRefs.size();
1901 bool found = false;
1902 for (size_t k = 0; k < numsessionrefs; k++) {
1903 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001904 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001905 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001906 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001907 found = true;
1908 break;
1909 }
1910 }
1911 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001912 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001913 // remove all effects from the chain
1914 while (ec->mEffects.size()) {
1915 sp<EffectModule> effect = ec->mEffects[0];
1916 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001917 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001918 if (effect->purgeHandles()) {
1919 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001920 }
1921 AudioSystem::unregisterEffect(effect->id());
1922 }
1923 }
1924 }
1925 return;
1926}
1927
Mathias Agopian65ab4712010-07-14 17:59:35 -07001928// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001929AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001930{
Glenn Kastena1117922012-01-26 10:53:32 -08001931 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001932}
1933
1934// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001935AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001936{
1937 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001938 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001939}
1940
1941// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001942AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001943{
Glenn Kastena1117922012-01-26 10:53:32 -08001944 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001945}
1946
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001947uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001948{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001949 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001950}
1951
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001952AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001953{
1954 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1955 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001956 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07001957 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001958 return thread;
1959 }
1960 }
1961 return NULL;
1962}
1963
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001964audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001965{
1966 PlaybackThread *thread = primaryPlaybackThread_l();
1967
1968 if (thread == NULL) {
1969 return 0;
1970 }
1971
Eric Laurentf1c04f92012-08-28 14:26:53 -07001972 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001973}
1974
Eric Laurenta011e352012-03-29 15:51:43 -07001975sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
1976 int triggerSession,
1977 int listenerSession,
1978 sync_event_callback_t callBack,
1979 void *cookie)
1980{
1981 Mutex::Autolock _l(mLock);
1982
1983 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
1984 status_t playStatus = NAME_NOT_FOUND;
1985 status_t recStatus = NAME_NOT_FOUND;
1986 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1987 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
1988 if (playStatus == NO_ERROR) {
1989 return event;
1990 }
1991 }
1992 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1993 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
1994 if (recStatus == NO_ERROR) {
1995 return event;
1996 }
1997 }
1998 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
1999 mPendingSyncEvents.add(event);
2000 } else {
2001 ALOGV("createSyncEvent() invalid event %d", event->type());
2002 event.clear();
2003 }
2004 return event;
2005}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002006
Mathias Agopian65ab4712010-07-14 17:59:35 -07002007// ----------------------------------------------------------------------------
2008// Effect management
2009// ----------------------------------------------------------------------------
2010
2011
Glenn Kastenf587ba52012-01-26 16:25:10 -08002012status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002013{
2014 Mutex::Autolock _l(mLock);
2015 return EffectQueryNumberEffects(numEffects);
2016}
2017
Glenn Kastenf587ba52012-01-26 16:25:10 -08002018status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002019{
2020 Mutex::Autolock _l(mLock);
2021 return EffectQueryEffect(index, descriptor);
2022}
2023
Glenn Kasten5e92a782012-01-30 07:40:52 -08002024status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002025 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002026{
2027 Mutex::Autolock _l(mLock);
2028 return EffectGetDescriptor(pUuid, descriptor);
2029}
2030
2031
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002032sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002033 effect_descriptor_t *pDesc,
2034 const sp<IEffectClient>& effectClient,
2035 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002036 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002037 int sessionId,
2038 status_t *status,
2039 int *id,
2040 int *enabled)
2041{
2042 status_t lStatus = NO_ERROR;
2043 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002044 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002045
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002046 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002047 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002048 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002049
2050 if (pDesc == NULL) {
2051 lStatus = BAD_VALUE;
2052 goto Exit;
2053 }
2054
Eric Laurent84e9a102010-09-23 16:10:16 -07002055 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002056 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002057 lStatus = PERMISSION_DENIED;
2058 goto Exit;
2059 }
2060
Dima Zavinfce7a472011-04-19 22:30:36 -07002061 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002062 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002063 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002064 lStatus = PERMISSION_DENIED;
2065 goto Exit;
2066 }
2067
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002068 if (io == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07002069 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002070 // output must be specified by AudioPolicyManager when using session
Dima Zavinfce7a472011-04-19 22:30:36 -07002071 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent84e9a102010-09-23 16:10:16 -07002072 lStatus = BAD_VALUE;
2073 goto Exit;
Dima Zavinfce7a472011-04-19 22:30:36 -07002074 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002075 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002076 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent84e9a102010-09-23 16:10:16 -07002077 // and we will exit safely
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002078 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent84e9a102010-09-23 16:10:16 -07002079 }
2080 }
2081
Mathias Agopian65ab4712010-07-14 17:59:35 -07002082 {
2083 Mutex::Autolock _l(mLock);
2084
Mathias Agopian65ab4712010-07-14 17:59:35 -07002085
2086 if (!EffectIsNullUuid(&pDesc->uuid)) {
2087 // if uuid is specified, request effect descriptor
2088 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2089 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002090 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002091 goto Exit;
2092 }
2093 } else {
2094 // if uuid is not specified, look for an available implementation
2095 // of the required type in effect factory
2096 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002097 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002098 lStatus = BAD_VALUE;
2099 goto Exit;
2100 }
2101 uint32_t numEffects = 0;
2102 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002103 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002104 bool found = false;
2105
2106 lStatus = EffectQueryNumberEffects(&numEffects);
2107 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002108 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002109 goto Exit;
2110 }
2111 for (uint32_t i = 0; i < numEffects; i++) {
2112 lStatus = EffectQueryEffect(i, &desc);
2113 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002114 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002115 continue;
2116 }
2117 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2118 // If matching type found save effect descriptor. If the session is
2119 // 0 and the effect is not auxiliary, continue enumeration in case
2120 // an auxiliary version of this effect type is available
2121 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002122 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002123 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002124 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2125 break;
2126 }
2127 }
2128 }
2129 if (!found) {
2130 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002131 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002132 goto Exit;
2133 }
2134 // For same effect type, chose auxiliary version over insert version if
2135 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002136 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002137 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002138 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002139 }
2140 }
2141
2142 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002143 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002144 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2145 lStatus = INVALID_OPERATION;
2146 goto Exit;
2147 }
2148
Eric Laurent59255e42011-07-27 19:49:51 -07002149 // check recording permission for visualizer
2150 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2151 !recordingAllowed()) {
2152 lStatus = PERMISSION_DENIED;
2153 goto Exit;
2154 }
2155
Mathias Agopian65ab4712010-07-14 17:59:35 -07002156 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002157 *pDesc = desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002158
2159 // If output is not specified try to find a matching audio session ID in one of the
2160 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002161 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2162 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002163 // Note: io is never 0 when creating an effect on an input
2164 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002165 // look for the thread where the specified audio session is present
Eric Laurent84e9a102010-09-23 16:10:16 -07002166 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2167 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002168 io = mPlaybackThreads.keyAt(i);
Eric Laurent84e9a102010-09-23 16:10:16 -07002169 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002170 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002171 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002172 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002173 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2174 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2175 io = mRecordThreads.keyAt(i);
2176 break;
2177 }
2178 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002179 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002180 // If no output thread contains the requested session ID, default to
2181 // first output. The effect chain will be moved to the correct output
2182 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002183 if (io == 0 && mPlaybackThreads.size()) {
2184 io = mPlaybackThreads.keyAt(0);
2185 }
Steve Block3856b092011-10-20 11:56:00 +01002186 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002187 }
2188 ThreadBase *thread = checkRecordThread_l(io);
2189 if (thread == NULL) {
2190 thread = checkPlaybackThread_l(io);
2191 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002192 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002193 lStatus = BAD_VALUE;
2194 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002195 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002196 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002197
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002198 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002199
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002200 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002201 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2202 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002203 if (handle != 0 && id != NULL) {
2204 *id = handle->id();
2205 }
2206 }
2207
2208Exit:
Glenn Kasten9156ef32013-08-06 15:39:08 -07002209 *status = lStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002210 return handle;
2211}
2212
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002213status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2214 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002215{
Steve Block3856b092011-10-20 11:56:00 +01002216 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002217 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002218 Mutex::Autolock _l(mLock);
2219 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002220 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002221 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002222 }
Eric Laurentde070132010-07-13 04:45:46 -07002223 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2224 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002225 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002226 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002227 }
Eric Laurentde070132010-07-13 04:45:46 -07002228 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2229 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002230 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002231 return BAD_VALUE;
2232 }
2233
2234 Mutex::Autolock _dl(dstThread->mLock);
2235 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent59255e42011-07-27 19:49:51 -07002236 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurentde070132010-07-13 04:45:46 -07002237
Mathias Agopian65ab4712010-07-14 17:59:35 -07002238 return NO_ERROR;
2239}
2240
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002241// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002242status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002243 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002244 AudioFlinger::PlaybackThread *dstThread,
2245 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002246{
Steve Block3856b092011-10-20 11:56:00 +01002247 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002248 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002249
Eric Laurent59255e42011-07-27 19:49:51 -07002250 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002251 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002252 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002253 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002254 return INVALID_OPERATION;
2255 }
2256
Eric Laurent39e94f82010-07-28 01:32:47 -07002257 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002258 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002259 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002260 // removed.
2261 srcThread->removeEffectChain_l(chain);
2262
2263 // transfer all effects one by one so that new effect chain is created on new thread with
2264 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002265 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent39e94f82010-07-28 01:32:47 -07002266 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002267 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002268 sp<EffectModule> effect = chain->getEffectFromId_l(0);
2269 while (effect != 0) {
2270 srcThread->removeEffect_l(effect);
2271 dstThread->addEffect_l(effect);
Eric Laurentec35a142011-10-05 17:42:25 -07002272 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2273 if (effect->state() == EffectModule::ACTIVE ||
2274 effect->state() == EffectModule::STOPPING) {
2275 effect->start();
2276 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002277 // if the move request is not received from audio policy manager, the effect must be
2278 // re-registered with the new strategy and output
2279 if (dstChain == 0) {
2280 dstChain = effect->chain().promote();
2281 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002282 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent39e94f82010-07-28 01:32:47 -07002283 srcThread->addEffect_l(effect);
2284 return NO_INIT;
2285 }
2286 strategy = dstChain->strategy();
2287 }
2288 if (reRegister) {
2289 AudioSystem::unregisterEffect(effect->id());
2290 AudioSystem::registerEffect(&effect->desc(),
2291 dstOutput,
2292 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002293 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002294 effect->id());
2295 }
Eric Laurentde070132010-07-13 04:45:46 -07002296 effect = chain->getEffectFromId_l(0);
2297 }
2298
2299 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002300}
2301
Glenn Kastenda6ef132013-01-10 12:31:01 -08002302struct Entry {
2303#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2304 char mName[MAX_NAME];
2305};
2306
2307int comparEntry(const void *p1, const void *p2)
2308{
2309 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2310}
2311
Glenn Kasten46909e72013-02-26 09:20:22 -08002312#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002313void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002314{
Eric Laurent81784c32012-11-19 14:55:58 -08002315 NBAIO_Source *teeSource = source.get();
2316 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002317 // .wav rotation
2318 // There is a benign race condition if 2 threads call this simultaneously.
2319 // They would both traverse the directory, but the result would simply be
2320 // failures at unlink() which are ignored. It's also unlikely since
2321 // normally dumpsys is only done by bugreport or from the command line.
2322 char teePath[32+256];
2323 strcpy(teePath, "/data/misc/media");
2324 size_t teePathLen = strlen(teePath);
2325 DIR *dir = opendir(teePath);
2326 teePath[teePathLen++] = '/';
2327 if (dir != NULL) {
2328#define MAX_SORT 20 // number of entries to sort
2329#define MAX_KEEP 10 // number of entries to keep
2330 struct Entry entries[MAX_SORT];
2331 size_t entryCount = 0;
2332 while (entryCount < MAX_SORT) {
2333 struct dirent de;
2334 struct dirent *result = NULL;
2335 int rc = readdir_r(dir, &de, &result);
2336 if (rc != 0) {
2337 ALOGW("readdir_r failed %d", rc);
2338 break;
2339 }
2340 if (result == NULL) {
2341 break;
2342 }
2343 if (result != &de) {
2344 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2345 break;
2346 }
2347 // ignore non .wav file entries
2348 size_t nameLen = strlen(de.d_name);
2349 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2350 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2351 continue;
2352 }
2353 strcpy(entries[entryCount++].mName, de.d_name);
2354 }
2355 (void) closedir(dir);
2356 if (entryCount > MAX_KEEP) {
2357 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2358 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2359 strcpy(&teePath[teePathLen], entries[i].mName);
2360 (void) unlink(teePath);
2361 }
2362 }
2363 } else {
2364 if (fd >= 0) {
2365 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2366 }
2367 }
Eric Laurent81784c32012-11-19 14:55:58 -08002368 char teeTime[16];
2369 struct timeval tv;
2370 gettimeofday(&tv, NULL);
2371 struct tm tm;
2372 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002373 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2374 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2375 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2376 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002377 if (teeFd >= 0) {
2378 char wavHeader[44];
2379 memcpy(wavHeader,
2380 "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",
2381 sizeof(wavHeader));
2382 NBAIO_Format format = teeSource->format();
2383 unsigned channelCount = Format_channelCount(format);
2384 ALOG_ASSERT(channelCount <= FCC_2);
2385 uint32_t sampleRate = Format_sampleRate(format);
2386 wavHeader[22] = channelCount; // number of channels
2387 wavHeader[24] = sampleRate; // sample rate
2388 wavHeader[25] = sampleRate >> 8;
2389 wavHeader[32] = channelCount * 2; // block alignment
2390 write(teeFd, wavHeader, sizeof(wavHeader));
2391 size_t total = 0;
2392 bool firstRead = true;
2393 for (;;) {
2394#define TEE_SINK_READ 1024
2395 short buffer[TEE_SINK_READ * FCC_2];
2396 size_t count = TEE_SINK_READ;
2397 ssize_t actual = teeSource->read(buffer, count,
2398 AudioBufferProvider::kInvalidPTS);
2399 bool wasFirstRead = firstRead;
2400 firstRead = false;
2401 if (actual <= 0) {
2402 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2403 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002404 }
Eric Laurent81784c32012-11-19 14:55:58 -08002405 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002406 }
Eric Laurent81784c32012-11-19 14:55:58 -08002407 ALOG_ASSERT(actual <= (ssize_t)count);
2408 write(teeFd, buffer, actual * channelCount * sizeof(short));
2409 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002410 }
Eric Laurent81784c32012-11-19 14:55:58 -08002411 lseek(teeFd, (off_t) 4, SEEK_SET);
2412 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2413 write(teeFd, &temp, sizeof(temp));
2414 lseek(teeFd, (off_t) 40, SEEK_SET);
2415 temp = total * channelCount * sizeof(short);
2416 write(teeFd, &temp, sizeof(temp));
2417 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002418 if (fd >= 0) {
2419 fdprintf(fd, "tee copied to %s\n", teePath);
2420 }
Eric Laurent59255e42011-07-27 19:49:51 -07002421 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002422 if (fd >= 0) {
2423 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2424 }
Eric Laurent59255e42011-07-27 19:49:51 -07002425 }
2426 }
2427}
Glenn Kasten46909e72013-02-26 09:20:22 -08002428#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002429
Mathias Agopian65ab4712010-07-14 17:59:35 -07002430// ----------------------------------------------------------------------------
2431
2432status_t AudioFlinger::onTransact(
2433 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2434{
2435 return BnAudioFlinger::onTransact(code, data, reply, flags);
2436}
2437
Mathias Agopian65ab4712010-07-14 17:59:35 -07002438}; // namespace android