blob: cc5af87b7b58d6d206dfd2ea10a20bf3d4d1f5e6 [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>
Glenn Kastenf6b16782011-12-15 09:51:17 -080040#include <cutils/compiler.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070041
Dima Zavin64760242011-05-11 14:15:23 -070042#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070043#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070044
45#include "AudioMixer.h"
46#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080047#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070048
Mathias Agopian65ab4712010-07-14 17:59:35 -070049#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070050#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070051#include <audio_effects/effect_ns.h>
52#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
Glenn Kasten3b21c502011-12-15 09:52:39 -080054#include <audio_utils/primitives.h>
55
Eric Laurentfeb0db62011-07-22 09:04:31 -070056#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080057
John Grossman4ff14ba2012-02-08 16:37:41 -080058#include <common_time/cc_helper.h>
Glenn Kasten58912562012-04-03 10:45:00 -070059
Glenn Kasten9e58b552013-01-18 15:09:48 -080060#include <media/IMediaLogService.h>
61
Glenn Kastenda6ef132013-01-10 12:31:01 -080062#include <media/nbaio/Pipe.h>
63#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070064#include <media/AudioParameter.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070065#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080066
Mathias Agopian65ab4712010-07-14 17:59:35 -070067// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070068
John Grossman1c345192012-03-27 14:00:17 -070069// Note: the following macro is used for extremely verbose logging message. In
70// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
71// 0; but one side effect of this is to turn all LOGV's as well. Some messages
72// are so verbose that we want to suppress them even when we have ALOG_ASSERT
73// turned on. Do not uncomment the #def below unless you really know what you
74// are doing and want to see all of the extremely verbose messages.
75//#define VERY_VERY_VERBOSE_LOGGING
76#ifdef VERY_VERY_VERBOSE_LOGGING
77#define ALOGVV ALOGV
78#else
79#define ALOGVV(a...) do { } while(0)
80#endif
Eric Laurentde070132010-07-13 04:45:46 -070081
Mathias Agopian65ab4712010-07-14 17:59:35 -070082namespace android {
83
Glenn Kastenec1d6b52011-12-12 09:04:45 -080084static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
85static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070086
Glenn Kasten58912562012-04-03 10:45:00 -070087
John Grossman4ff14ba2012-02-08 16:37:41 -080088nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080089
Eric Laurent81784c32012-11-19 14:55:58 -080090uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070091
Glenn Kasten46909e72013-02-26 09:20:22 -080092#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080093bool AudioFlinger::mTeeSinkInputEnabled = false;
94bool AudioFlinger::mTeeSinkOutputEnabled = false;
95bool AudioFlinger::mTeeSinkTrackEnabled = false;
96
97size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
98size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
99size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800100#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800101
Mathias Agopian65ab4712010-07-14 17:59:35 -0700102// ----------------------------------------------------------------------------
103
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700104static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700105{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700106 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700107 int rc;
108
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700109 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
110 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
111 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
112 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700113 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700114 }
115 rc = audio_hw_device_open(mod, dev);
116 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
117 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
118 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700119 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700120 }
121 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
122 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
123 rc = BAD_VALUE;
124 goto out;
125 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700126 return 0;
127
128out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700129 *dev = NULL;
130 return rc;
131}
132
Mathias Agopian65ab4712010-07-14 17:59:35 -0700133// ----------------------------------------------------------------------------
134
135AudioFlinger::AudioFlinger()
136 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800137 mPrimaryHardwareDev(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700138 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800139 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800140 mMasterMute(false),
141 mNextUniqueId(1),
142 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700143 mBtNrecIsOff(false),
144 mIsLowRamDevice(true),
145 mIsDeviceTypeKnown(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700146{
Glenn Kasten949a9262013-04-16 12:35:20 -0700147 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800148 char value[PROPERTY_VALUE_MAX];
149 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
150 if (doLog) {
151 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
152 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800153#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800154 (void) property_get("ro.debuggable", value, "0");
155 int debuggable = atoi(value);
156 int teeEnabled = 0;
157 if (debuggable) {
158 (void) property_get("af.tee", value, "0");
159 teeEnabled = atoi(value);
160 }
161 if (teeEnabled & 1)
162 mTeeSinkInputEnabled = true;
163 if (teeEnabled & 2)
164 mTeeSinkOutputEnabled = true;
165 if (teeEnabled & 4)
166 mTeeSinkTrackEnabled = true;
Glenn Kasten46909e72013-02-26 09:20:22 -0800167#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700168}
169
170void AudioFlinger::onFirstRef()
171{
Dima Zavin799a70e2011-04-18 16:57:27 -0700172 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700173
Eric Laurent93575202011-01-18 18:39:02 -0800174 Mutex::Autolock _l(mLock);
175
Dima Zavin799a70e2011-04-18 16:57:27 -0700176 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800177 char val_str[PROPERTY_VALUE_MAX] = { 0 };
178 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
179 uint32_t int_val;
180 if (1 == sscanf(val_str, "%u", &int_val)) {
181 mStandbyTimeInNsecs = milliseconds(int_val);
182 ALOGI("Using %u mSec as standby time.", int_val);
183 } else {
184 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
185 ALOGI("Using default %u mSec as standby time.",
186 (uint32_t)(mStandbyTimeInNsecs / 1000000));
187 }
188 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700189
Eric Laurenta4c5a552012-03-29 10:12:40 -0700190 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700191}
192
193AudioFlinger::~AudioFlinger()
194{
195 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700196 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700197 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700198 }
199 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700200 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700201 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700202 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700203
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800204 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
205 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700206 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
207 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700208 }
209}
210
Eric Laurenta4c5a552012-03-29 10:12:40 -0700211static const char * const audio_interfaces[] = {
212 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
213 AUDIO_HARDWARE_MODULE_ID_A2DP,
214 AUDIO_HARDWARE_MODULE_ID_USB,
215};
216#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
217
John Grossmanee578c02012-07-23 17:05:46 -0700218AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
219 audio_module_handle_t module,
220 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700221{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700222 // if module is 0, the request comes from an old policy manager and we should load
223 // well known modules
224 if (module == 0) {
225 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
226 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
227 loadHwModule_l(audio_interfaces[i]);
228 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700229 // then try to find a module supporting the requested device.
230 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
231 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
232 audio_hw_device_t *dev = audioHwDevice->hwDevice();
233 if ((dev->get_supported_devices != NULL) &&
234 (dev->get_supported_devices(dev) & devices) == devices)
235 return audioHwDevice;
236 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700237 } else {
238 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700239 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
240 if (audioHwDevice != NULL) {
241 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700242 }
243 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700244
Dima Zavin799a70e2011-04-18 16:57:27 -0700245 return NULL;
246}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700247
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700248void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700249{
250 const size_t SIZE = 256;
251 char buffer[SIZE];
252 String8 result;
253
254 result.append("Clients:\n");
255 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800256 sp<Client> client = mClients.valueAt(i).promote();
257 if (client != 0) {
258 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
259 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700260 }
261 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700262
263 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800264 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700265 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
266 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800267 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700268 result.append(buffer);
269 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700270 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700271}
272
273
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700274void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700275{
276 const size_t SIZE = 256;
277 char buffer[SIZE];
278 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800279 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700280
John Grossman4ff14ba2012-02-08 16:37:41 -0800281 snprintf(buffer, SIZE, "Hardware status: %d\n"
282 "Standby Time mSec: %u\n",
283 hardwareStatus,
284 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700285 result.append(buffer);
286 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700287}
288
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700289void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700290{
291 const size_t SIZE = 256;
292 char buffer[SIZE];
293 String8 result;
294 snprintf(buffer, SIZE, "Permission Denial: "
295 "can't dump AudioFlinger from pid=%d, uid=%d\n",
296 IPCThreadState::self()->getCallingPid(),
297 IPCThreadState::self()->getCallingUid());
298 result.append(buffer);
299 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300}
301
Eric Laurent81784c32012-11-19 14:55:58 -0800302bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700303{
304 bool locked = false;
305 for (int i = 0; i < kDumpLockRetries; ++i) {
306 if (mutex.tryLock() == NO_ERROR) {
307 locked = true;
308 break;
309 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800310 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700311 }
312 return locked;
313}
314
315status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
316{
Glenn Kasten44deb052012-02-05 18:09:08 -0800317 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700318 dumpPermissionDenial(fd, args);
319 } else {
320 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800321 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700322 if (!hardwareLocked) {
323 String8 result(kHardwareLockedString);
324 write(fd, result.string(), result.size());
325 } else {
326 mHardwareLock.unlock();
327 }
328
Eric Laurent81784c32012-11-19 14:55:58 -0800329 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700330
331 // failed to lock - AudioFlinger is probably deadlocked
332 if (!locked) {
333 String8 result(kDeadlockedString);
334 write(fd, result.string(), result.size());
335 }
336
337 dumpClients(fd, args);
338 dumpInternals(fd, args);
339
340 // dump playback threads
341 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
342 mPlaybackThreads.valueAt(i)->dump(fd, args);
343 }
344
345 // dump record threads
346 for (size_t i = 0; i < mRecordThreads.size(); i++) {
347 mRecordThreads.valueAt(i)->dump(fd, args);
348 }
349
Dima Zavin799a70e2011-04-18 16:57:27 -0700350 // dump all hardware devs
351 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700352 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700353 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700354 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700355
Glenn Kasten46909e72013-02-26 09:20:22 -0800356#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700357 // dump the serially shared record tee sink
358 if (mRecordTeeSource != 0) {
359 dumpTee(fd, mRecordTeeSource);
360 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800361#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700362
Glenn Kastend65d73c2012-06-22 17:21:07 -0700363 if (locked) {
364 mLock.unlock();
365 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800366
367 // append a copy of media.log here by forwarding fd to it, but don't attempt
368 // to lookup the service if it's not running, as it will block for a second
369 if (mLogMemoryDealer != 0) {
370 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
371 if (binder != 0) {
372 fdprintf(fd, "\nmedia.log:\n");
373 Vector<String16> args;
374 binder->dump(fd, args);
375 }
376 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700377 }
378 return NO_ERROR;
379}
380
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800381sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
382{
383 // If pid is already in the mClients wp<> map, then use that entry
384 // (for which promote() is always != 0), otherwise create a new entry and Client.
385 sp<Client> client = mClients.valueFor(pid).promote();
386 if (client == 0) {
387 client = new Client(this, pid);
388 mClients.add(pid, client);
389 }
390
391 return client;
392}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393
Glenn Kasten9e58b552013-01-18 15:09:48 -0800394sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
395{
396 if (mLogMemoryDealer == 0) {
397 return new NBLog::Writer();
398 }
399 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
400 sp<NBLog::Writer> writer = new NBLog::Writer(size, shared);
401 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
402 if (binder != 0) {
403 interface_cast<IMediaLogService>(binder)->registerWriter(shared, size, name);
404 }
405 return writer;
406}
407
408void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
409{
Glenn Kasten685ef092013-02-04 08:15:34 -0800410 if (writer == 0) {
411 return;
412 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800413 sp<IMemory> iMemory(writer->getIMemory());
414 if (iMemory == 0) {
415 return;
416 }
417 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
418 if (binder != 0) {
419 interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory);
420 // Now the media.log remote reference to IMemory is gone.
421 // When our last local reference to IMemory also drops to zero,
422 // the IMemory destructor will deallocate the region from mMemoryDealer.
423 }
424}
425
Mathias Agopian65ab4712010-07-14 17:59:35 -0700426// IAudioFlinger interface
427
428
429sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800430 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700431 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800432 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700433 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800434 size_t frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800435 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700436 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800437 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800438 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700439 int *sessionId,
440 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) {
528 trackHandle = new TrackHandle(track);
529 } else {
530 // remove local strong reference to Client before deleting the Track so that the Client
531 // destructor is called by the TrackBase destructor with mLock held
532 client.clear();
533 track.clear();
534 }
535
536Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700537 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700538 *status = lStatus;
539 }
540 return trackHandle;
541}
542
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800543uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700544{
545 Mutex::Autolock _l(mLock);
546 PlaybackThread *thread = checkPlaybackThread_l(output);
547 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000548 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700549 return 0;
550 }
551 return thread->sampleRate();
552}
553
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800554int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700555{
556 Mutex::Autolock _l(mLock);
557 PlaybackThread *thread = checkPlaybackThread_l(output);
558 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000559 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700560 return 0;
561 }
562 return thread->channelCount();
563}
564
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800565audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700566{
567 Mutex::Autolock _l(mLock);
568 PlaybackThread *thread = checkPlaybackThread_l(output);
569 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000570 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800571 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700572 }
573 return thread->format();
574}
575
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800576size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700577{
578 Mutex::Autolock _l(mLock);
579 PlaybackThread *thread = checkPlaybackThread_l(output);
580 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000581 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700582 return 0;
583 }
Glenn Kasten58912562012-04-03 10:45:00 -0700584 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
585 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700586 return thread->frameCount();
587}
588
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800589uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700590{
591 Mutex::Autolock _l(mLock);
592 PlaybackThread *thread = checkPlaybackThread_l(output);
593 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800594 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 return 0;
596 }
597 return thread->latency();
598}
599
600status_t AudioFlinger::setMasterVolume(float value)
601{
Eric Laurenta1884f92011-08-23 08:25:03 -0700602 status_t ret = initCheck();
603 if (ret != NO_ERROR) {
604 return ret;
605 }
606
Mathias Agopian65ab4712010-07-14 17:59:35 -0700607 // check calling permissions
608 if (!settingsAllowed()) {
609 return PERMISSION_DENIED;
610 }
611
Eric Laurenta4c5a552012-03-29 10:12:40 -0700612 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700613 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700614
John Grossmanee578c02012-07-23 17:05:46 -0700615 // Set master volume in the HALs which support it.
616 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
617 AutoMutex lock(mHardwareLock);
618 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800619
John Grossmanee578c02012-07-23 17:05:46 -0700620 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
621 if (dev->canSetMasterVolume()) {
622 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800623 }
John Grossmanee578c02012-07-23 17:05:46 -0700624 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700625 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700626
John Grossmanee578c02012-07-23 17:05:46 -0700627 // Now set the master volume in each playback thread. Playback threads
628 // assigned to HALs which do not have master volume support will apply
629 // master volume during the mix operation. Threads with HALs which do
630 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800631 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700632 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633
634 return NO_ERROR;
635}
636
Glenn Kastenf78aee72012-01-04 11:00:47 -0800637status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700638{
Eric Laurenta1884f92011-08-23 08:25:03 -0700639 status_t ret = initCheck();
640 if (ret != NO_ERROR) {
641 return ret;
642 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700643
644 // check calling permissions
645 if (!settingsAllowed()) {
646 return PERMISSION_DENIED;
647 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800648 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000649 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650 return BAD_VALUE;
651 }
652
653 { // scope for the lock
654 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700655 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700656 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700657 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700658 mHardwareStatus = AUDIO_HW_IDLE;
659 }
660
661 if (NO_ERROR == ret) {
662 Mutex::Autolock _l(mLock);
663 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800664 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700665 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666 }
667
668 return ret;
669}
670
671status_t AudioFlinger::setMicMute(bool state)
672{
Eric Laurenta1884f92011-08-23 08:25:03 -0700673 status_t ret = initCheck();
674 if (ret != NO_ERROR) {
675 return ret;
676 }
677
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678 // check calling permissions
679 if (!settingsAllowed()) {
680 return PERMISSION_DENIED;
681 }
682
683 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700684 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700686 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700687 mHardwareStatus = AUDIO_HW_IDLE;
688 return ret;
689}
690
691bool AudioFlinger::getMicMute() const
692{
Eric Laurenta1884f92011-08-23 08:25:03 -0700693 status_t ret = initCheck();
694 if (ret != NO_ERROR) {
695 return false;
696 }
697
Dima Zavinfce7a472011-04-19 22:30:36 -0700698 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800699 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700700 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700701 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700702 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700703 mHardwareStatus = AUDIO_HW_IDLE;
704 return state;
705}
706
707status_t AudioFlinger::setMasterMute(bool muted)
708{
John Grossmand8f178d2012-07-20 14:51:35 -0700709 status_t ret = initCheck();
710 if (ret != NO_ERROR) {
711 return ret;
712 }
713
Mathias Agopian65ab4712010-07-14 17:59:35 -0700714 // check calling permissions
715 if (!settingsAllowed()) {
716 return PERMISSION_DENIED;
717 }
718
John Grossmanee578c02012-07-23 17:05:46 -0700719 Mutex::Autolock _l(mLock);
720 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700721
John Grossmanee578c02012-07-23 17:05:46 -0700722 // Set master mute in the HALs which support it.
723 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
724 AutoMutex lock(mHardwareLock);
725 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700726
John Grossmanee578c02012-07-23 17:05:46 -0700727 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
728 if (dev->canSetMasterMute()) {
729 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700730 }
John Grossmanee578c02012-07-23 17:05:46 -0700731 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700732 }
733
John Grossmanee578c02012-07-23 17:05:46 -0700734 // Now set the master mute in each playback thread. Playback threads
735 // assigned to HALs which do not have master mute support will apply master
736 // mute during the mix operation. Threads with HALs which do support master
737 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800738 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700739 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700740
741 return NO_ERROR;
742}
743
744float AudioFlinger::masterVolume() const
745{
Glenn Kasten98067102011-12-13 11:47:54 -0800746 Mutex::Autolock _l(mLock);
747 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700748}
749
750bool AudioFlinger::masterMute() const
751{
Glenn Kasten98067102011-12-13 11:47:54 -0800752 Mutex::Autolock _l(mLock);
753 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700754}
755
John Grossman4ff14ba2012-02-08 16:37:41 -0800756float AudioFlinger::masterVolume_l() const
757{
John Grossman4ff14ba2012-02-08 16:37:41 -0800758 return mMasterVolume;
759}
760
John Grossmand8f178d2012-07-20 14:51:35 -0700761bool AudioFlinger::masterMute_l() const
762{
John Grossmanee578c02012-07-23 17:05:46 -0700763 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700764}
765
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800766status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
767 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700768{
769 // check calling permissions
770 if (!settingsAllowed()) {
771 return PERMISSION_DENIED;
772 }
773
Glenn Kasten263709e2012-01-06 08:40:01 -0800774 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000775 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700776 return BAD_VALUE;
777 }
778
779 AutoMutex lock(mLock);
780 PlaybackThread *thread = NULL;
781 if (output) {
782 thread = checkPlaybackThread_l(output);
783 if (thread == NULL) {
784 return BAD_VALUE;
785 }
786 }
787
788 mStreamTypes[stream].volume = value;
789
790 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800791 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700792 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700793 }
794 } else {
795 thread->setStreamVolume(stream, value);
796 }
797
798 return NO_ERROR;
799}
800
Glenn Kastenfff6d712012-01-12 16:38:12 -0800801status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700802{
803 // check calling permissions
804 if (!settingsAllowed()) {
805 return PERMISSION_DENIED;
806 }
807
Glenn Kasten263709e2012-01-06 08:40:01 -0800808 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700809 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000810 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700811 return BAD_VALUE;
812 }
813
Eric Laurent93575202011-01-18 18:39:02 -0800814 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700815 mStreamTypes[stream].mute = muted;
816 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700817 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818
819 return NO_ERROR;
820}
821
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800822float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700823{
Glenn Kasten263709e2012-01-06 08:40:01 -0800824 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700825 return 0.0f;
826 }
827
828 AutoMutex lock(mLock);
829 float volume;
830 if (output) {
831 PlaybackThread *thread = checkPlaybackThread_l(output);
832 if (thread == NULL) {
833 return 0.0f;
834 }
835 volume = thread->streamVolume(stream);
836 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800837 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700838 }
839
840 return volume;
841}
842
Glenn Kastenfff6d712012-01-12 16:38:12 -0800843bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700844{
Glenn Kasten263709e2012-01-06 08:40:01 -0800845 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700846 return true;
847 }
848
Glenn Kasten6637baa2012-01-09 09:40:36 -0800849 AutoMutex lock(mLock);
850 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700851}
852
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800853status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700854{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700855 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
856 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800857
Mathias Agopian65ab4712010-07-14 17:59:35 -0700858 // check calling permissions
859 if (!settingsAllowed()) {
860 return PERMISSION_DENIED;
861 }
862
Mathias Agopian65ab4712010-07-14 17:59:35 -0700863 // ioHandle == 0 means the parameters are global to the audio hardware interface
864 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700865 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700866 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800867 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700868 AutoMutex lock(mHardwareLock);
869 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
870 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
871 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
872 status_t result = dev->set_parameters(dev, keyValuePairs.string());
873 final_result = result ?: final_result;
874 }
875 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800876 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700877 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
878 AudioParameter param = AudioParameter(keyValuePairs);
879 String8 value;
880 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700881 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
882 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700883 for (size_t i = 0; i < mRecordThreads.size(); i++) {
884 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700885 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700886 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
887 // collect all of the thread's session IDs
888 KeyedVector<int, bool> ids = thread->sessionIds();
889 // suspend effects associated with those session IDs
890 for (size_t j = 0; j < ids.size(); ++j) {
891 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700892 thread->setEffectSuspended(FX_IID_AEC,
893 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700894 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700895 thread->setEffectSuspended(FX_IID_NS,
896 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700897 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700898 }
899 }
Eric Laurentbee53372011-08-29 12:42:48 -0700900 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700901 }
902 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700903 String8 screenState;
904 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
905 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800906 if (isOff != (AudioFlinger::mScreenState & 1)) {
907 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700908 }
909 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700910 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911 }
912
913 // hold a strong ref on thread in case closeOutput() or closeInput() is called
914 // and the thread is exited once the lock is released
915 sp<ThreadBase> thread;
916 {
917 Mutex::Autolock _l(mLock);
918 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700919 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700920 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800921 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700922 // indicate output device change to all input threads for pre processing
923 AudioParameter param = AudioParameter(keyValuePairs);
924 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700925 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
926 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700927 for (size_t i = 0; i < mRecordThreads.size(); i++) {
928 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
929 }
930 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700931 }
932 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800933 if (thread != 0) {
934 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700935 }
936 return BAD_VALUE;
937}
938
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800939String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700941 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
942 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700943
Eric Laurenta4c5a552012-03-29 10:12:40 -0700944 Mutex::Autolock _l(mLock);
945
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700947 String8 out_s8;
948
Dima Zavin799a70e2011-04-18 16:57:27 -0700949 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800950 char *s;
951 {
952 AutoMutex lock(mHardwareLock);
953 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700954 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800955 s = dev->get_parameters(dev, keys.string());
956 mHardwareStatus = AUDIO_HW_IDLE;
957 }
John Grossmanef7740b2012-02-09 11:28:36 -0800958 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700959 free(s);
960 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700961 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700962 }
963
Mathias Agopian65ab4712010-07-14 17:59:35 -0700964 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
965 if (playbackThread != NULL) {
966 return playbackThread->getParameters(keys);
967 }
968 RecordThread *recordThread = checkRecordThread_l(ioHandle);
969 if (recordThread != NULL) {
970 return recordThread->getParameters(keys);
971 }
972 return String8("");
973}
974
Glenn Kastendd8104c2012-07-02 12:42:44 -0700975size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
976 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700977{
Eric Laurenta1884f92011-08-23 08:25:03 -0700978 status_t ret = initCheck();
979 if (ret != NO_ERROR) {
980 return 0;
981 }
982
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800983 AutoMutex lock(mHardwareLock);
984 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000985 struct audio_config config;
986 memset(&config, 0, sizeof(config));
987 config.sample_rate = sampleRate;
988 config.channel_mask = channelMask;
989 config.format = format;
990
John Grossmanee578c02012-07-23 17:05:46 -0700991 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
992 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800993 mHardwareStatus = AUDIO_HW_IDLE;
994 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700995}
996
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800997unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700998{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700999 Mutex::Autolock _l(mLock);
1000
1001 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1002 if (recordThread != NULL) {
1003 return recordThread->getInputFramesLost();
1004 }
1005 return 0;
1006}
1007
1008status_t AudioFlinger::setVoiceVolume(float value)
1009{
Eric Laurenta1884f92011-08-23 08:25:03 -07001010 status_t ret = initCheck();
1011 if (ret != NO_ERROR) {
1012 return ret;
1013 }
1014
Mathias Agopian65ab4712010-07-14 17:59:35 -07001015 // check calling permissions
1016 if (!settingsAllowed()) {
1017 return PERMISSION_DENIED;
1018 }
1019
1020 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001021 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001022 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001023 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001024 mHardwareStatus = AUDIO_HW_IDLE;
1025
1026 return ret;
1027}
1028
Glenn Kasten26c77552012-11-16 12:01:44 -08001029status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001030 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001031{
1032 status_t status;
1033
1034 Mutex::Autolock _l(mLock);
1035
1036 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1037 if (playbackThread != NULL) {
1038 return playbackThread->getRenderPosition(halFrames, dspFrames);
1039 }
1040
1041 return BAD_VALUE;
1042}
1043
1044void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1045{
1046
1047 Mutex::Autolock _l(mLock);
1048
Glenn Kastenbb001922012-02-03 11:10:26 -08001049 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001050 if (mNotificationClients.indexOfKey(pid) < 0) {
1051 sp<NotificationClient> notificationClient = new NotificationClient(this,
1052 client,
1053 pid);
Steve Block3856b092011-10-20 11:56:00 +01001054 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001055
1056 mNotificationClients.add(pid, notificationClient);
1057
1058 sp<IBinder> binder = client->asBinder();
1059 binder->linkToDeath(notificationClient);
1060
1061 // the config change is always sent from playback or record threads to avoid deadlock
1062 // with AudioSystem::gLock
1063 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001064 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001065 }
1066
1067 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001068 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001069 }
1070 }
1071}
1072
1073void AudioFlinger::removeNotificationClient(pid_t pid)
1074{
1075 Mutex::Autolock _l(mLock);
1076
Glenn Kastena3b09252012-01-20 09:19:01 -08001077 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001078
Steve Block3856b092011-10-20 11:56:00 +01001079 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001080 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001081 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001082 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001083 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001084 ALOGV(" pid %d @ %d", ref->mPid, i);
1085 if (ref->mPid == pid) {
1086 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001087 mAudioSessionRefs.removeAt(i);
1088 delete ref;
1089 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001090 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001091 } else {
1092 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001093 }
1094 }
1095 if (removed) {
1096 purgeStaleEffects_l();
1097 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001098}
1099
1100// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001101void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001102{
1103 size_t size = mNotificationClients.size();
1104 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001105 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1106 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001107 }
1108}
1109
1110// removeClient_l() must be called with AudioFlinger::mLock held
1111void AudioFlinger::removeClient_l(pid_t pid)
1112{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001113 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001114 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001115 mClients.removeItem(pid);
1116}
1117
Eric Laurent717e1282012-06-29 16:36:52 -07001118// getEffectThread_l() must be called with AudioFlinger::mLock held
1119sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1120{
1121 sp<PlaybackThread> thread;
1122
1123 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1124 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1125 ALOG_ASSERT(thread == 0);
1126 thread = mPlaybackThreads.valueAt(i);
1127 }
1128 }
1129
1130 return thread;
1131}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001132
Mathias Agopian65ab4712010-07-14 17:59:35 -07001133
Mathias Agopian65ab4712010-07-14 17:59:35 -07001134
1135// ----------------------------------------------------------------------------
1136
1137AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1138 : RefBase(),
1139 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001140 // 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 -07001141 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001142 mPid(pid),
1143 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001144{
1145 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1146}
1147
1148// Client destructor must be called with AudioFlinger::mLock held
1149AudioFlinger::Client::~Client()
1150{
1151 mAudioFlinger->removeClient_l(mPid);
1152}
1153
Glenn Kasten435dbe62012-01-30 10:15:48 -08001154sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001155{
1156 return mMemoryDealer;
1157}
1158
John Grossman4ff14ba2012-02-08 16:37:41 -08001159// Reserve one of the limited slots for a timed audio track associated
1160// with this client
1161bool AudioFlinger::Client::reserveTimedTrack()
1162{
1163 const int kMaxTimedTracksPerClient = 4;
1164
1165 Mutex::Autolock _l(mTimedTrackLock);
1166
1167 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1168 ALOGW("can not create timed track - pid %d has exceeded the limit",
1169 mPid);
1170 return false;
1171 }
1172
1173 mTimedTrackCount++;
1174 return true;
1175}
1176
1177// Release a slot for a timed audio track
1178void AudioFlinger::Client::releaseTimedTrack()
1179{
1180 Mutex::Autolock _l(mTimedTrackLock);
1181 mTimedTrackCount--;
1182}
1183
Mathias Agopian65ab4712010-07-14 17:59:35 -07001184// ----------------------------------------------------------------------------
1185
1186AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1187 const sp<IAudioFlingerClient>& client,
1188 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001189 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001190{
1191}
1192
1193AudioFlinger::NotificationClient::~NotificationClient()
1194{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001195}
1196
1197void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1198{
1199 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001200 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001201}
1202
Mathias Agopian65ab4712010-07-14 17:59:35 -07001203
1204// ----------------------------------------------------------------------------
1205
1206sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001207 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001208 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001209 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001210 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001211 size_t frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -08001212 IAudioFlinger::track_flags_t flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001213 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001214 int *sessionId,
1215 status_t *status)
1216{
1217 sp<RecordThread::RecordTrack> recordTrack;
1218 sp<RecordHandle> recordHandle;
1219 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001220 status_t lStatus;
1221 RecordThread *thread;
1222 size_t inFrameCount;
1223 int lSessionId;
1224
1225 // check calling permissions
1226 if (!recordingAllowed()) {
1227 lStatus = PERMISSION_DENIED;
1228 goto Exit;
1229 }
1230
1231 // add client to list
1232 { // scope for mLock
1233 Mutex::Autolock _l(mLock);
1234 thread = checkRecordThread_l(input);
1235 if (thread == NULL) {
1236 lStatus = BAD_VALUE;
1237 goto Exit;
1238 }
1239
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001240 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001241 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001242
1243 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001244 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001245 lSessionId = *sessionId;
1246 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001247 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001248 if (sessionId != NULL) {
1249 *sessionId = lSessionId;
1250 }
1251 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001252 // create new record track.
1253 // The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07001254 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
1255 frameCount, lSessionId, flags, tid, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001256 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001257 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001258 // remove local strong reference to Client before deleting the RecordTrack so that the
1259 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001260 client.clear();
1261 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001262 goto Exit;
1263 }
1264
1265 // return to handle to client
1266 recordHandle = new RecordHandle(recordTrack);
1267 lStatus = NO_ERROR;
1268
1269Exit:
1270 if (status) {
1271 *status = lStatus;
1272 }
1273 return recordHandle;
1274}
1275
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001276
1277
Mathias Agopian65ab4712010-07-14 17:59:35 -07001278// ----------------------------------------------------------------------------
1279
Eric Laurenta4c5a552012-03-29 10:12:40 -07001280audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1281{
1282 if (!settingsAllowed()) {
1283 return 0;
1284 }
1285 Mutex::Autolock _l(mLock);
1286 return loadHwModule_l(name);
1287}
1288
1289// loadHwModule_l() must be called with AudioFlinger::mLock held
1290audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1291{
1292 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1293 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1294 ALOGW("loadHwModule() module %s already loaded", name);
1295 return mAudioHwDevs.keyAt(i);
1296 }
1297 }
1298
Eric Laurenta4c5a552012-03-29 10:12:40 -07001299 audio_hw_device_t *dev;
1300
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001301 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001302 if (rc) {
1303 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1304 return 0;
1305 }
1306
1307 mHardwareStatus = AUDIO_HW_INIT;
1308 rc = dev->init_check(dev);
1309 mHardwareStatus = AUDIO_HW_IDLE;
1310 if (rc) {
1311 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1312 return 0;
1313 }
1314
John Grossmanee578c02012-07-23 17:05:46 -07001315 // Check and cache this HAL's level of support for master mute and master
1316 // volume. If this is the first HAL opened, and it supports the get
1317 // methods, use the initial values provided by the HAL as the current
1318 // master mute and volume settings.
1319
1320 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1321 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001322 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001323
1324 if (0 == mAudioHwDevs.size()) {
1325 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1326 if (NULL != dev->get_master_volume) {
1327 float mv;
1328 if (OK == dev->get_master_volume(dev, &mv)) {
1329 mMasterVolume = mv;
1330 }
1331 }
1332
1333 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1334 if (NULL != dev->get_master_mute) {
1335 bool mm;
1336 if (OK == dev->get_master_mute(dev, &mm)) {
1337 mMasterMute = mm;
1338 }
1339 }
1340 }
1341
Eric Laurenta4c5a552012-03-29 10:12:40 -07001342 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001343 if ((NULL != dev->set_master_volume) &&
1344 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1345 flags = static_cast<AudioHwDevice::Flags>(flags |
1346 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1347 }
1348
1349 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1350 if ((NULL != dev->set_master_mute) &&
1351 (OK == dev->set_master_mute(dev, mMasterMute))) {
1352 flags = static_cast<AudioHwDevice::Flags>(flags |
1353 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1354 }
1355
Eric Laurenta4c5a552012-03-29 10:12:40 -07001356 mHardwareStatus = AUDIO_HW_IDLE;
1357 }
1358
1359 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001360 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001361
1362 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001363 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001364
1365 return handle;
1366
1367}
1368
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001369// ----------------------------------------------------------------------------
1370
Glenn Kasten3b16c762012-11-14 08:44:39 -08001371uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001372{
1373 Mutex::Autolock _l(mLock);
1374 PlaybackThread *thread = primaryPlaybackThread_l();
1375 return thread != NULL ? thread->sampleRate() : 0;
1376}
1377
Glenn Kastene33054e2012-11-14 12:54:39 -08001378size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001379{
1380 Mutex::Autolock _l(mLock);
1381 PlaybackThread *thread = primaryPlaybackThread_l();
1382 return thread != NULL ? thread->frameCountHAL() : 0;
1383}
1384
1385// ----------------------------------------------------------------------------
1386
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001387status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1388{
1389 uid_t uid = IPCThreadState::self()->getCallingUid();
1390 if (uid != AID_SYSTEM) {
1391 return PERMISSION_DENIED;
1392 }
1393 Mutex::Autolock _l(mLock);
1394 if (mIsDeviceTypeKnown) {
1395 return INVALID_OPERATION;
1396 }
1397 mIsLowRamDevice = isLowRamDevice;
1398 mIsDeviceTypeKnown = true;
1399 return NO_ERROR;
1400}
1401
1402// ----------------------------------------------------------------------------
1403
Eric Laurenta4c5a552012-03-29 10:12:40 -07001404audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1405 audio_devices_t *pDevices,
1406 uint32_t *pSamplingRate,
1407 audio_format_t *pFormat,
1408 audio_channel_mask_t *pChannelMask,
1409 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001410 audio_output_flags_t flags,
1411 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001412{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001413 PlaybackThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001414 struct audio_config config;
1415 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1416 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1417 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1418 if (offloadInfo) {
1419 config.offload_info = *offloadInfo;
1420 }
1421
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001422 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001423 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001424
Eric Laurentbfb1b832013-01-07 09:53:42 -08001425 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001426 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001427 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001428 config.sample_rate,
1429 config.format,
1430 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001431 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001432 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
1433 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001434
1435 if (pDevices == NULL || *pDevices == 0) {
1436 return 0;
1437 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001438
Mathias Agopian65ab4712010-07-14 17:59:35 -07001439 Mutex::Autolock _l(mLock);
1440
Eric Laurenta4c5a552012-03-29 10:12:40 -07001441 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001442 if (outHwDev == NULL)
1443 return 0;
1444
John Grossmanee578c02012-07-23 17:05:46 -07001445 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001446 audio_io_handle_t id = nextUniqueId();
1447
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001448 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001449
Glenn Kasten34542ac2013-06-26 11:29:02 -07001450 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001451 id,
1452 *pDevices,
1453 (audio_output_flags_t)flags,
1454 &config,
1455 &outStream);
1456
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001457 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001458 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001459 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001460 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001461 config.sample_rate,
1462 config.format,
1463 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001464 status);
1465
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001466 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001467 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001468
Eric Laurentbfb1b832013-01-07 09:53:42 -08001469 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1470 thread = new OffloadThread(this, output, id, *pDevices);
1471 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1472 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001473 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1474 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001475 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001476 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001477 } else {
1478 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001479 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001480 }
1481 mPlaybackThreads.add(id, thread);
1482
Glenn Kasten7c027242012-12-26 14:43:16 -08001483 if (pSamplingRate != NULL) {
1484 *pSamplingRate = config.sample_rate;
1485 }
1486 if (pFormat != NULL) {
1487 *pFormat = config.format;
1488 }
1489 if (pChannelMask != NULL) {
1490 *pChannelMask = config.channel_mask;
1491 }
1492 if (pLatencyMs != NULL) {
1493 *pLatencyMs = thread->latency();
1494 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001495
1496 // notify client processes of the new output creation
1497 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001498
1499 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001500 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001501 ALOGI("Using module %d has the primary audio interface", module);
1502 mPrimaryHardwareDev = outHwDev;
1503
1504 AutoMutex lock(mHardwareLock);
1505 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001506 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001507 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001508 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001509 return id;
1510 }
1511
1512 return 0;
1513}
1514
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001515audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1516 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001517{
1518 Mutex::Autolock _l(mLock);
1519 MixerThread *thread1 = checkMixerThread_l(output1);
1520 MixerThread *thread2 = checkMixerThread_l(output2);
1521
1522 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001523 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1524 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001525 return 0;
1526 }
1527
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001528 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001529 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1530 thread->addOutputTrack(thread2);
1531 mPlaybackThreads.add(id, thread);
1532 // notify client processes of the new output creation
1533 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1534 return id;
1535}
1536
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001537status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001538{
Glenn Kastend96c5722012-04-25 13:44:49 -07001539 return closeOutput_nonvirtual(output);
1540}
1541
1542status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1543{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001544 // keep strong reference on the playback thread so that
1545 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001546 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001547 {
1548 Mutex::Autolock _l(mLock);
1549 thread = checkPlaybackThread_l(output);
1550 if (thread == NULL) {
1551 return BAD_VALUE;
1552 }
1553
Steve Block3856b092011-10-20 11:56:00 +01001554 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001555
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001556 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001557 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001558 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001559 DuplicatingThread *dupThread =
1560 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001561 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001562
1563 }
1564 }
1565 }
1566
1567
1568 mPlaybackThreads.removeItem(output);
1569 // save all effects to the default thread
1570 if (mPlaybackThreads.size()) {
1571 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1572 if (dstThread != NULL) {
1573 // audioflinger lock is held here so the acquisition order of thread locks does not
1574 // matter
1575 Mutex::Autolock _dl(dstThread->mLock);
1576 Mutex::Autolock _sl(thread->mLock);
1577 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1578 for (size_t i = 0; i < effectChains.size(); i ++) {
1579 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001580 }
1581 }
1582 }
Glenn Kastena1117922012-01-26 10:53:32 -08001583 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001584 }
1585 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001586 // The thread entity (active unit of execution) is no longer running here,
1587 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001588
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001589 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001590 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001591 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001592 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001593 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001594 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001595 }
1596 return NO_ERROR;
1597}
1598
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001599status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001600{
1601 Mutex::Autolock _l(mLock);
1602 PlaybackThread *thread = checkPlaybackThread_l(output);
1603
1604 if (thread == NULL) {
1605 return BAD_VALUE;
1606 }
1607
Steve Block3856b092011-10-20 11:56:00 +01001608 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001609 thread->suspend();
1610
1611 return NO_ERROR;
1612}
1613
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001614status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001615{
1616 Mutex::Autolock _l(mLock);
1617 PlaybackThread *thread = checkPlaybackThread_l(output);
1618
1619 if (thread == NULL) {
1620 return BAD_VALUE;
1621 }
1622
Steve Block3856b092011-10-20 11:56:00 +01001623 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001624
1625 thread->restore();
1626
1627 return NO_ERROR;
1628}
1629
Eric Laurenta4c5a552012-03-29 10:12:40 -07001630audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1631 audio_devices_t *pDevices,
1632 uint32_t *pSamplingRate,
1633 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001634 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001635{
1636 status_t status;
1637 RecordThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001638 struct audio_config config;
1639 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1640 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1641 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1642
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001643 uint32_t reqSamplingRate = config.sample_rate;
1644 audio_format_t reqFormat = config.format;
1645 audio_channel_mask_t reqChannels = config.channel_mask;
1646 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001647 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001648
1649 if (pDevices == NULL || *pDevices == 0) {
1650 return 0;
1651 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001652
Mathias Agopian65ab4712010-07-14 17:59:35 -07001653 Mutex::Autolock _l(mLock);
1654
Eric Laurenta4c5a552012-03-29 10:12:40 -07001655 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001656 if (inHwDev == NULL)
1657 return 0;
1658
John Grossmanee578c02012-07-23 17:05:46 -07001659 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001660 audio_io_handle_t id = nextUniqueId();
1661
John Grossmanee578c02012-07-23 17:05:46 -07001662 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001663 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001664 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1665 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001666 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001667 config.sample_rate,
1668 config.format,
1669 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001670 status);
1671
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001672 // If the input could not be opened with the requested parameters and we can handle the
1673 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1674 // 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 -07001675 if (status == BAD_VALUE &&
1676 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1677 (config.sample_rate <= 2 * reqSamplingRate) &&
1678 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001679 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001680 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001681 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001682 }
1683
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001684 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001685
Glenn Kasten46909e72013-02-26 09:20:22 -08001686#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001687 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1688 // or (re-)create if current Pipe is idle and does not match the new format
1689 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001690 enum {
1691 TEE_SINK_NO, // don't copy input
1692 TEE_SINK_NEW, // copy input using a new pipe
1693 TEE_SINK_OLD, // copy input using an existing pipe
1694 } kind;
1695 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1696 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001697 if (!mTeeSinkInputEnabled) {
1698 kind = TEE_SINK_NO;
1699 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001700 kind = TEE_SINK_NO;
1701 } else if (mRecordTeeSink == 0) {
1702 kind = TEE_SINK_NEW;
1703 } else if (mRecordTeeSink->getStrongCount() != 1) {
1704 kind = TEE_SINK_NO;
1705 } else if (format == mRecordTeeSink->format()) {
1706 kind = TEE_SINK_OLD;
1707 } else {
1708 kind = TEE_SINK_NEW;
1709 }
1710 switch (kind) {
1711 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001712 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001713 size_t numCounterOffers = 0;
1714 const NBAIO_Format offers[1] = {format};
1715 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1716 ALOG_ASSERT(index == 0);
1717 PipeReader *pipeReader = new PipeReader(*pipe);
1718 numCounterOffers = 0;
1719 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1720 ALOG_ASSERT(index == 0);
1721 mRecordTeeSink = pipe;
1722 mRecordTeeSource = pipeReader;
1723 teeSink = pipe;
1724 }
1725 break;
1726 case TEE_SINK_OLD:
1727 teeSink = mRecordTeeSink;
1728 break;
1729 case TEE_SINK_NO:
1730 default:
1731 break;
1732 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001733#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001734
Dima Zavin799a70e2011-04-18 16:57:27 -07001735 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1736
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001737 // Start record thread
1738 // RecorThread require both input and output device indication to forward to audio
1739 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001740 thread = new RecordThread(this,
1741 input,
1742 reqSamplingRate,
1743 reqChannels,
1744 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001745 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001746 *pDevices
1747#ifdef TEE_SINK
1748 , teeSink
1749#endif
1750 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001751 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001752 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001753 if (pSamplingRate != NULL) {
1754 *pSamplingRate = reqSamplingRate;
1755 }
1756 if (pFormat != NULL) {
1757 *pFormat = config.format;
1758 }
1759 if (pChannelMask != NULL) {
1760 *pChannelMask = reqChannels;
1761 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001762
Mathias Agopian65ab4712010-07-14 17:59:35 -07001763 // notify client processes of the new input creation
1764 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1765 return id;
1766 }
1767
1768 return 0;
1769}
1770
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001771status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001772{
Glenn Kastend96c5722012-04-25 13:44:49 -07001773 return closeInput_nonvirtual(input);
1774}
1775
1776status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1777{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001778 // keep strong reference on the record thread so that
1779 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001780 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001781 {
1782 Mutex::Autolock _l(mLock);
1783 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001784 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001785 return BAD_VALUE;
1786 }
1787
Steve Block3856b092011-10-20 11:56:00 +01001788 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001789 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001790 mRecordThreads.removeItem(input);
1791 }
1792 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001793 // The thread entity (active unit of execution) is no longer running here,
1794 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001795
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001796 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001797 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001798 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001799 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001800 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001801
1802 return NO_ERROR;
1803}
1804
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001805status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001806{
1807 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001808 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001809
1810 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1811 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001812 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001813 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001814
1815 return NO_ERROR;
1816}
1817
1818
1819int AudioFlinger::newAudioSessionId()
1820{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001821 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001822}
1823
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001824void AudioFlinger::acquireAudioSessionId(int audioSession)
1825{
1826 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001827 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001828 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001829 size_t num = mAudioSessionRefs.size();
1830 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001831 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001832 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1833 ref->mCnt++;
1834 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001835 return;
1836 }
1837 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001838 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1839 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001840}
1841
1842void AudioFlinger::releaseAudioSessionId(int audioSession)
1843{
1844 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001845 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001846 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001847 size_t num = mAudioSessionRefs.size();
1848 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001849 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001850 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1851 ref->mCnt--;
1852 ALOGV(" decremented refcount to %d", ref->mCnt);
1853 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001854 mAudioSessionRefs.removeAt(i);
1855 delete ref;
1856 purgeStaleEffects_l();
1857 }
1858 return;
1859 }
1860 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001861 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001862}
1863
1864void AudioFlinger::purgeStaleEffects_l() {
1865
Steve Block3856b092011-10-20 11:56:00 +01001866 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001867
1868 Vector< sp<EffectChain> > chains;
1869
1870 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1871 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1872 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1873 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001874 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1875 chains.push(ec);
1876 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001877 }
1878 }
1879 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1880 sp<RecordThread> t = mRecordThreads.valueAt(i);
1881 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1882 sp<EffectChain> ec = t->mEffectChains[j];
1883 chains.push(ec);
1884 }
1885 }
1886
1887 for (size_t i = 0; i < chains.size(); i++) {
1888 sp<EffectChain> ec = chains[i];
1889 int sessionid = ec->sessionId();
1890 sp<ThreadBase> t = ec->mThread.promote();
1891 if (t == 0) {
1892 continue;
1893 }
1894 size_t numsessionrefs = mAudioSessionRefs.size();
1895 bool found = false;
1896 for (size_t k = 0; k < numsessionrefs; k++) {
1897 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001898 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001899 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001900 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001901 found = true;
1902 break;
1903 }
1904 }
1905 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001906 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001907 // remove all effects from the chain
1908 while (ec->mEffects.size()) {
1909 sp<EffectModule> effect = ec->mEffects[0];
1910 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001911 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001912 if (effect->purgeHandles()) {
1913 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001914 }
1915 AudioSystem::unregisterEffect(effect->id());
1916 }
1917 }
1918 }
1919 return;
1920}
1921
Mathias Agopian65ab4712010-07-14 17:59:35 -07001922// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001923AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001924{
Glenn Kastena1117922012-01-26 10:53:32 -08001925 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001926}
1927
1928// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001929AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001930{
1931 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001932 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001933}
1934
1935// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001936AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001937{
Glenn Kastena1117922012-01-26 10:53:32 -08001938 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001939}
1940
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001941uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001942{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001943 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001944}
1945
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001946AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001947{
1948 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1949 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001950 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07001951 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001952 return thread;
1953 }
1954 }
1955 return NULL;
1956}
1957
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001958audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001959{
1960 PlaybackThread *thread = primaryPlaybackThread_l();
1961
1962 if (thread == NULL) {
1963 return 0;
1964 }
1965
Eric Laurentf1c04f92012-08-28 14:26:53 -07001966 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001967}
1968
Eric Laurenta011e352012-03-29 15:51:43 -07001969sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
1970 int triggerSession,
1971 int listenerSession,
1972 sync_event_callback_t callBack,
1973 void *cookie)
1974{
1975 Mutex::Autolock _l(mLock);
1976
1977 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
1978 status_t playStatus = NAME_NOT_FOUND;
1979 status_t recStatus = NAME_NOT_FOUND;
1980 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1981 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
1982 if (playStatus == NO_ERROR) {
1983 return event;
1984 }
1985 }
1986 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1987 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
1988 if (recStatus == NO_ERROR) {
1989 return event;
1990 }
1991 }
1992 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
1993 mPendingSyncEvents.add(event);
1994 } else {
1995 ALOGV("createSyncEvent() invalid event %d", event->type());
1996 event.clear();
1997 }
1998 return event;
1999}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002000
Mathias Agopian65ab4712010-07-14 17:59:35 -07002001// ----------------------------------------------------------------------------
2002// Effect management
2003// ----------------------------------------------------------------------------
2004
2005
Glenn Kastenf587ba52012-01-26 16:25:10 -08002006status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002007{
2008 Mutex::Autolock _l(mLock);
2009 return EffectQueryNumberEffects(numEffects);
2010}
2011
Glenn Kastenf587ba52012-01-26 16:25:10 -08002012status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002013{
2014 Mutex::Autolock _l(mLock);
2015 return EffectQueryEffect(index, descriptor);
2016}
2017
Glenn Kasten5e92a782012-01-30 07:40:52 -08002018status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002019 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002020{
2021 Mutex::Autolock _l(mLock);
2022 return EffectGetDescriptor(pUuid, descriptor);
2023}
2024
2025
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002026sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002027 effect_descriptor_t *pDesc,
2028 const sp<IEffectClient>& effectClient,
2029 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002030 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002031 int sessionId,
2032 status_t *status,
2033 int *id,
2034 int *enabled)
2035{
2036 status_t lStatus = NO_ERROR;
2037 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002038 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002039
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002040 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002041 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002042 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002043
2044 if (pDesc == NULL) {
2045 lStatus = BAD_VALUE;
2046 goto Exit;
2047 }
2048
Eric Laurent84e9a102010-09-23 16:10:16 -07002049 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002050 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002051 lStatus = PERMISSION_DENIED;
2052 goto Exit;
2053 }
2054
Dima Zavinfce7a472011-04-19 22:30:36 -07002055 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002056 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002057 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002058 lStatus = PERMISSION_DENIED;
2059 goto Exit;
2060 }
2061
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002062 if (io == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07002063 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002064 // output must be specified by AudioPolicyManager when using session
Dima Zavinfce7a472011-04-19 22:30:36 -07002065 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent84e9a102010-09-23 16:10:16 -07002066 lStatus = BAD_VALUE;
2067 goto Exit;
Dima Zavinfce7a472011-04-19 22:30:36 -07002068 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002069 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002070 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent84e9a102010-09-23 16:10:16 -07002071 // and we will exit safely
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002072 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent84e9a102010-09-23 16:10:16 -07002073 }
2074 }
2075
Mathias Agopian65ab4712010-07-14 17:59:35 -07002076 {
2077 Mutex::Autolock _l(mLock);
2078
Mathias Agopian65ab4712010-07-14 17:59:35 -07002079
2080 if (!EffectIsNullUuid(&pDesc->uuid)) {
2081 // if uuid is specified, request effect descriptor
2082 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2083 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002084 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002085 goto Exit;
2086 }
2087 } else {
2088 // if uuid is not specified, look for an available implementation
2089 // of the required type in effect factory
2090 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002091 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002092 lStatus = BAD_VALUE;
2093 goto Exit;
2094 }
2095 uint32_t numEffects = 0;
2096 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002097 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002098 bool found = false;
2099
2100 lStatus = EffectQueryNumberEffects(&numEffects);
2101 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002102 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002103 goto Exit;
2104 }
2105 for (uint32_t i = 0; i < numEffects; i++) {
2106 lStatus = EffectQueryEffect(i, &desc);
2107 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002108 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002109 continue;
2110 }
2111 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2112 // If matching type found save effect descriptor. If the session is
2113 // 0 and the effect is not auxiliary, continue enumeration in case
2114 // an auxiliary version of this effect type is available
2115 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002116 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002117 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002118 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2119 break;
2120 }
2121 }
2122 }
2123 if (!found) {
2124 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002125 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002126 goto Exit;
2127 }
2128 // For same effect type, chose auxiliary version over insert version if
2129 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002130 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002131 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002132 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002133 }
2134 }
2135
2136 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002137 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002138 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2139 lStatus = INVALID_OPERATION;
2140 goto Exit;
2141 }
2142
Eric Laurent59255e42011-07-27 19:49:51 -07002143 // check recording permission for visualizer
2144 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2145 !recordingAllowed()) {
2146 lStatus = PERMISSION_DENIED;
2147 goto Exit;
2148 }
2149
Mathias Agopian65ab4712010-07-14 17:59:35 -07002150 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002151 *pDesc = desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002152
2153 // If output is not specified try to find a matching audio session ID in one of the
2154 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002155 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2156 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002157 // Note: io is never 0 when creating an effect on an input
2158 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002159 // look for the thread where the specified audio session is present
Eric Laurent84e9a102010-09-23 16:10:16 -07002160 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2161 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002162 io = mPlaybackThreads.keyAt(i);
Eric Laurent84e9a102010-09-23 16:10:16 -07002163 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002164 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002165 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002166 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002167 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2168 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2169 io = mRecordThreads.keyAt(i);
2170 break;
2171 }
2172 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002173 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002174 // If no output thread contains the requested session ID, default to
2175 // first output. The effect chain will be moved to the correct output
2176 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002177 if (io == 0 && mPlaybackThreads.size()) {
2178 io = mPlaybackThreads.keyAt(0);
2179 }
Steve Block3856b092011-10-20 11:56:00 +01002180 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002181 }
2182 ThreadBase *thread = checkRecordThread_l(io);
2183 if (thread == NULL) {
2184 thread = checkPlaybackThread_l(io);
2185 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002186 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002187 lStatus = BAD_VALUE;
2188 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002189 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002190 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002191
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002192 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002193
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002194 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002195 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2196 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002197 if (handle != 0 && id != NULL) {
2198 *id = handle->id();
2199 }
2200 }
2201
2202Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002203 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002204 *status = lStatus;
2205 }
2206 return handle;
2207}
2208
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002209status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2210 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002211{
Steve Block3856b092011-10-20 11:56:00 +01002212 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002213 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002214 Mutex::Autolock _l(mLock);
2215 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002216 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002217 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002218 }
Eric Laurentde070132010-07-13 04:45:46 -07002219 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2220 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002221 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002222 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002223 }
Eric Laurentde070132010-07-13 04:45:46 -07002224 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2225 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002226 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002227 return BAD_VALUE;
2228 }
2229
2230 Mutex::Autolock _dl(dstThread->mLock);
2231 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent59255e42011-07-27 19:49:51 -07002232 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurentde070132010-07-13 04:45:46 -07002233
Mathias Agopian65ab4712010-07-14 17:59:35 -07002234 return NO_ERROR;
2235}
2236
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002237// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002238status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002239 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002240 AudioFlinger::PlaybackThread *dstThread,
2241 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002242{
Steve Block3856b092011-10-20 11:56:00 +01002243 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002244 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002245
Eric Laurent59255e42011-07-27 19:49:51 -07002246 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002247 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002248 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002249 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002250 return INVALID_OPERATION;
2251 }
2252
Eric Laurent39e94f82010-07-28 01:32:47 -07002253 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002254 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002255 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002256 // removed.
2257 srcThread->removeEffectChain_l(chain);
2258
2259 // transfer all effects one by one so that new effect chain is created on new thread with
2260 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002261 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent39e94f82010-07-28 01:32:47 -07002262 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002263 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002264 sp<EffectModule> effect = chain->getEffectFromId_l(0);
2265 while (effect != 0) {
2266 srcThread->removeEffect_l(effect);
2267 dstThread->addEffect_l(effect);
Eric Laurentec35a142011-10-05 17:42:25 -07002268 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2269 if (effect->state() == EffectModule::ACTIVE ||
2270 effect->state() == EffectModule::STOPPING) {
2271 effect->start();
2272 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002273 // if the move request is not received from audio policy manager, the effect must be
2274 // re-registered with the new strategy and output
2275 if (dstChain == 0) {
2276 dstChain = effect->chain().promote();
2277 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002278 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent39e94f82010-07-28 01:32:47 -07002279 srcThread->addEffect_l(effect);
2280 return NO_INIT;
2281 }
2282 strategy = dstChain->strategy();
2283 }
2284 if (reRegister) {
2285 AudioSystem::unregisterEffect(effect->id());
2286 AudioSystem::registerEffect(&effect->desc(),
2287 dstOutput,
2288 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002289 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002290 effect->id());
2291 }
Eric Laurentde070132010-07-13 04:45:46 -07002292 effect = chain->getEffectFromId_l(0);
2293 }
2294
2295 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002296}
2297
Glenn Kastenda6ef132013-01-10 12:31:01 -08002298struct Entry {
2299#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2300 char mName[MAX_NAME];
2301};
2302
2303int comparEntry(const void *p1, const void *p2)
2304{
2305 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2306}
2307
Glenn Kasten46909e72013-02-26 09:20:22 -08002308#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002309void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002310{
Eric Laurent81784c32012-11-19 14:55:58 -08002311 NBAIO_Source *teeSource = source.get();
2312 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002313 // .wav rotation
2314 // There is a benign race condition if 2 threads call this simultaneously.
2315 // They would both traverse the directory, but the result would simply be
2316 // failures at unlink() which are ignored. It's also unlikely since
2317 // normally dumpsys is only done by bugreport or from the command line.
2318 char teePath[32+256];
2319 strcpy(teePath, "/data/misc/media");
2320 size_t teePathLen = strlen(teePath);
2321 DIR *dir = opendir(teePath);
2322 teePath[teePathLen++] = '/';
2323 if (dir != NULL) {
2324#define MAX_SORT 20 // number of entries to sort
2325#define MAX_KEEP 10 // number of entries to keep
2326 struct Entry entries[MAX_SORT];
2327 size_t entryCount = 0;
2328 while (entryCount < MAX_SORT) {
2329 struct dirent de;
2330 struct dirent *result = NULL;
2331 int rc = readdir_r(dir, &de, &result);
2332 if (rc != 0) {
2333 ALOGW("readdir_r failed %d", rc);
2334 break;
2335 }
2336 if (result == NULL) {
2337 break;
2338 }
2339 if (result != &de) {
2340 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2341 break;
2342 }
2343 // ignore non .wav file entries
2344 size_t nameLen = strlen(de.d_name);
2345 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2346 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2347 continue;
2348 }
2349 strcpy(entries[entryCount++].mName, de.d_name);
2350 }
2351 (void) closedir(dir);
2352 if (entryCount > MAX_KEEP) {
2353 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2354 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2355 strcpy(&teePath[teePathLen], entries[i].mName);
2356 (void) unlink(teePath);
2357 }
2358 }
2359 } else {
2360 if (fd >= 0) {
2361 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2362 }
2363 }
Eric Laurent81784c32012-11-19 14:55:58 -08002364 char teeTime[16];
2365 struct timeval tv;
2366 gettimeofday(&tv, NULL);
2367 struct tm tm;
2368 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002369 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2370 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2371 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2372 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002373 if (teeFd >= 0) {
2374 char wavHeader[44];
2375 memcpy(wavHeader,
2376 "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",
2377 sizeof(wavHeader));
2378 NBAIO_Format format = teeSource->format();
2379 unsigned channelCount = Format_channelCount(format);
2380 ALOG_ASSERT(channelCount <= FCC_2);
2381 uint32_t sampleRate = Format_sampleRate(format);
2382 wavHeader[22] = channelCount; // number of channels
2383 wavHeader[24] = sampleRate; // sample rate
2384 wavHeader[25] = sampleRate >> 8;
2385 wavHeader[32] = channelCount * 2; // block alignment
2386 write(teeFd, wavHeader, sizeof(wavHeader));
2387 size_t total = 0;
2388 bool firstRead = true;
2389 for (;;) {
2390#define TEE_SINK_READ 1024
2391 short buffer[TEE_SINK_READ * FCC_2];
2392 size_t count = TEE_SINK_READ;
2393 ssize_t actual = teeSource->read(buffer, count,
2394 AudioBufferProvider::kInvalidPTS);
2395 bool wasFirstRead = firstRead;
2396 firstRead = false;
2397 if (actual <= 0) {
2398 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2399 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002400 }
Eric Laurent81784c32012-11-19 14:55:58 -08002401 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002402 }
Eric Laurent81784c32012-11-19 14:55:58 -08002403 ALOG_ASSERT(actual <= (ssize_t)count);
2404 write(teeFd, buffer, actual * channelCount * sizeof(short));
2405 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002406 }
Eric Laurent81784c32012-11-19 14:55:58 -08002407 lseek(teeFd, (off_t) 4, SEEK_SET);
2408 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2409 write(teeFd, &temp, sizeof(temp));
2410 lseek(teeFd, (off_t) 40, SEEK_SET);
2411 temp = total * channelCount * sizeof(short);
2412 write(teeFd, &temp, sizeof(temp));
2413 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002414 if (fd >= 0) {
2415 fdprintf(fd, "tee copied to %s\n", teePath);
2416 }
Eric Laurent59255e42011-07-27 19:49:51 -07002417 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002418 if (fd >= 0) {
2419 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2420 }
Eric Laurent59255e42011-07-27 19:49:51 -07002421 }
2422 }
2423}
Glenn Kasten46909e72013-02-26 09:20:22 -08002424#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002425
Mathias Agopian65ab4712010-07-14 17:59:35 -07002426// ----------------------------------------------------------------------------
2427
2428status_t AudioFlinger::onTransact(
2429 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2430{
2431 return BnAudioFlinger::onTransact(code, data, reply, flags);
2432}
2433
Mathias Agopian65ab4712010-07-14 17:59:35 -07002434}; // namespace android