blob: e81e25319d66cbfaf5e6f28324757c4a048e826a [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
Mathias Agopian65ab4712010-07-14 17:59:35 -07002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
Glenn Kasten153b9fe2013-07-15 11:23:36 -070022#include "Configuration.h"
Glenn Kastenda6ef132013-01-10 12:31:01 -080023#include <dirent.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070024#include <math.h>
25#include <signal.h>
26#include <sys/time.h>
27#include <sys/resource.h>
28
Gloria Wang9ee159b2011-02-24 14:51:45 -080029#include <binder/IPCThreadState.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070030#include <binder/IServiceManager.h>
31#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070032#include <utils/Trace.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070033#include <binder/Parcel.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <utils/String16.h>
35#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070036#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070037
Dima Zavinfce7a472011-04-19 22:30:36 -070038#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070039#include <cutils/properties.h>
40
Dima Zavin64760242011-05-11 14:15:23 -070041#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070042#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070043
44#include "AudioMixer.h"
45#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080046#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070047
Mathias Agopian65ab4712010-07-14 17:59:35 -070048#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070049#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070050#include <audio_effects/effect_ns.h>
51#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Glenn Kasten3b21c502011-12-15 09:52:39 -080053#include <audio_utils/primitives.h>
54
Eric Laurentfeb0db62011-07-22 09:04:31 -070055#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080056
John Grossman4ff14ba2012-02-08 16:37:41 -080057#include <common_time/cc_helper.h>
Glenn Kasten58912562012-04-03 10:45:00 -070058
Glenn Kasten9e58b552013-01-18 15:09:48 -080059#include <media/IMediaLogService.h>
60
Glenn Kastenda6ef132013-01-10 12:31:01 -080061#include <media/nbaio/Pipe.h>
62#include <media/nbaio/PipeReader.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070063#include <media/AudioParameter.h>
Glenn Kasten4182c4e2013-07-15 14:45:07 -070064#include <private/android_filesystem_config.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080065
Mathias Agopian65ab4712010-07-14 17:59:35 -070066// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070067
John Grossman1c345192012-03-27 14:00:17 -070068// Note: the following macro is used for extremely verbose logging message. In
69// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
70// 0; but one side effect of this is to turn all LOGV's as well. Some messages
71// are so verbose that we want to suppress them even when we have ALOG_ASSERT
72// turned on. Do not uncomment the #def below unless you really know what you
73// are doing and want to see all of the extremely verbose messages.
74//#define VERY_VERY_VERBOSE_LOGGING
75#ifdef VERY_VERY_VERBOSE_LOGGING
76#define ALOGVV ALOGV
77#else
78#define ALOGVV(a...) do { } while(0)
79#endif
Eric Laurentde070132010-07-13 04:45:46 -070080
Mathias Agopian65ab4712010-07-14 17:59:35 -070081namespace android {
82
Glenn Kastenec1d6b52011-12-12 09:04:45 -080083static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
84static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070085
Glenn Kasten58912562012-04-03 10:45:00 -070086
John Grossman4ff14ba2012-02-08 16:37:41 -080087nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080088
Eric Laurent81784c32012-11-19 14:55:58 -080089uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070090
Glenn Kasten46909e72013-02-26 09:20:22 -080091#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080092bool AudioFlinger::mTeeSinkInputEnabled = false;
93bool AudioFlinger::mTeeSinkOutputEnabled = false;
94bool AudioFlinger::mTeeSinkTrackEnabled = false;
95
96size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
97size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
98size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -080099#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800100
Mathias Agopian65ab4712010-07-14 17:59:35 -0700101// ----------------------------------------------------------------------------
102
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700103static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700104{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700105 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700106 int rc;
107
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700108 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
109 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
110 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
111 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700112 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700113 }
114 rc = audio_hw_device_open(mod, dev);
115 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
116 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
117 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700118 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700119 }
120 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
121 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
122 rc = BAD_VALUE;
123 goto out;
124 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700125 return 0;
126
127out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700128 *dev = NULL;
129 return rc;
130}
131
Mathias Agopian65ab4712010-07-14 17:59:35 -0700132// ----------------------------------------------------------------------------
133
134AudioFlinger::AudioFlinger()
135 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800136 mPrimaryHardwareDev(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700137 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800138 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800139 mMasterMute(false),
140 mNextUniqueId(1),
141 mMode(AUDIO_MODE_INVALID),
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700142 mBtNrecIsOff(false),
143 mIsLowRamDevice(true),
144 mIsDeviceTypeKnown(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700145{
Glenn Kasten949a9262013-04-16 12:35:20 -0700146 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800147 char value[PROPERTY_VALUE_MAX];
148 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
149 if (doLog) {
150 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
151 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800152#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800153 (void) property_get("ro.debuggable", value, "0");
154 int debuggable = atoi(value);
155 int teeEnabled = 0;
156 if (debuggable) {
157 (void) property_get("af.tee", value, "0");
158 teeEnabled = atoi(value);
159 }
160 if (teeEnabled & 1)
161 mTeeSinkInputEnabled = true;
162 if (teeEnabled & 2)
163 mTeeSinkOutputEnabled = true;
164 if (teeEnabled & 4)
165 mTeeSinkTrackEnabled = true;
Glenn Kasten46909e72013-02-26 09:20:22 -0800166#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700167}
168
169void AudioFlinger::onFirstRef()
170{
Dima Zavin799a70e2011-04-18 16:57:27 -0700171 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700172
Eric Laurent93575202011-01-18 18:39:02 -0800173 Mutex::Autolock _l(mLock);
174
Dima Zavin799a70e2011-04-18 16:57:27 -0700175 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800176 char val_str[PROPERTY_VALUE_MAX] = { 0 };
177 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
178 uint32_t int_val;
179 if (1 == sscanf(val_str, "%u", &int_val)) {
180 mStandbyTimeInNsecs = milliseconds(int_val);
181 ALOGI("Using %u mSec as standby time.", int_val);
182 } else {
183 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
184 ALOGI("Using default %u mSec as standby time.",
185 (uint32_t)(mStandbyTimeInNsecs / 1000000));
186 }
187 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700188
Eric Laurenta4c5a552012-03-29 10:12:40 -0700189 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700190}
191
192AudioFlinger::~AudioFlinger()
193{
194 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700195 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700196 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700197 }
198 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700199 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700200 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700201 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700202
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800203 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
204 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700205 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
206 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700207 }
208}
209
Eric Laurenta4c5a552012-03-29 10:12:40 -0700210static const char * const audio_interfaces[] = {
211 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
212 AUDIO_HARDWARE_MODULE_ID_A2DP,
213 AUDIO_HARDWARE_MODULE_ID_USB,
214};
215#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
216
John Grossmanee578c02012-07-23 17:05:46 -0700217AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
218 audio_module_handle_t module,
219 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700220{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700221 // if module is 0, the request comes from an old policy manager and we should load
222 // well known modules
223 if (module == 0) {
224 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
225 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
226 loadHwModule_l(audio_interfaces[i]);
227 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700228 // then try to find a module supporting the requested device.
229 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
230 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
231 audio_hw_device_t *dev = audioHwDevice->hwDevice();
232 if ((dev->get_supported_devices != NULL) &&
233 (dev->get_supported_devices(dev) & devices) == devices)
234 return audioHwDevice;
235 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700236 } else {
237 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700238 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
239 if (audioHwDevice != NULL) {
240 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700241 }
242 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700243
Dima Zavin799a70e2011-04-18 16:57:27 -0700244 return NULL;
245}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700246
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700247void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700248{
249 const size_t SIZE = 256;
250 char buffer[SIZE];
251 String8 result;
252
253 result.append("Clients:\n");
254 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800255 sp<Client> client = mClients.valueAt(i).promote();
256 if (client != 0) {
257 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
258 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700259 }
260 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700261
262 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800263 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700264 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
265 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800266 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700267 result.append(buffer);
268 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700269 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700270}
271
272
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700273void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700274{
275 const size_t SIZE = 256;
276 char buffer[SIZE];
277 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800278 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700279
John Grossman4ff14ba2012-02-08 16:37:41 -0800280 snprintf(buffer, SIZE, "Hardware status: %d\n"
281 "Standby Time mSec: %u\n",
282 hardwareStatus,
283 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700284 result.append(buffer);
285 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700286}
287
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700288void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700289{
290 const size_t SIZE = 256;
291 char buffer[SIZE];
292 String8 result;
293 snprintf(buffer, SIZE, "Permission Denial: "
294 "can't dump AudioFlinger from pid=%d, uid=%d\n",
295 IPCThreadState::self()->getCallingPid(),
296 IPCThreadState::self()->getCallingUid());
297 result.append(buffer);
298 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700299}
300
Eric Laurent81784c32012-11-19 14:55:58 -0800301bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700302{
303 bool locked = false;
304 for (int i = 0; i < kDumpLockRetries; ++i) {
305 if (mutex.tryLock() == NO_ERROR) {
306 locked = true;
307 break;
308 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800309 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310 }
311 return locked;
312}
313
314status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
315{
Glenn Kasten44deb052012-02-05 18:09:08 -0800316 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700317 dumpPermissionDenial(fd, args);
318 } else {
319 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800320 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700321 if (!hardwareLocked) {
322 String8 result(kHardwareLockedString);
323 write(fd, result.string(), result.size());
324 } else {
325 mHardwareLock.unlock();
326 }
327
Eric Laurent81784c32012-11-19 14:55:58 -0800328 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700329
330 // failed to lock - AudioFlinger is probably deadlocked
331 if (!locked) {
332 String8 result(kDeadlockedString);
333 write(fd, result.string(), result.size());
334 }
335
336 dumpClients(fd, args);
337 dumpInternals(fd, args);
338
339 // dump playback threads
340 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
341 mPlaybackThreads.valueAt(i)->dump(fd, args);
342 }
343
344 // dump record threads
345 for (size_t i = 0; i < mRecordThreads.size(); i++) {
346 mRecordThreads.valueAt(i)->dump(fd, args);
347 }
348
Dima Zavin799a70e2011-04-18 16:57:27 -0700349 // dump all hardware devs
350 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700351 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700352 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700353 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700354
Glenn Kasten46909e72013-02-26 09:20:22 -0800355#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700356 // dump the serially shared record tee sink
357 if (mRecordTeeSource != 0) {
358 dumpTee(fd, mRecordTeeSource);
359 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800360#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700361
Glenn Kastend65d73c2012-06-22 17:21:07 -0700362 if (locked) {
363 mLock.unlock();
364 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800365
366 // append a copy of media.log here by forwarding fd to it, but don't attempt
367 // to lookup the service if it's not running, as it will block for a second
368 if (mLogMemoryDealer != 0) {
369 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
370 if (binder != 0) {
371 fdprintf(fd, "\nmedia.log:\n");
372 Vector<String16> args;
373 binder->dump(fd, args);
374 }
375 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700376 }
377 return NO_ERROR;
378}
379
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800380sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
381{
382 // If pid is already in the mClients wp<> map, then use that entry
383 // (for which promote() is always != 0), otherwise create a new entry and Client.
384 sp<Client> client = mClients.valueFor(pid).promote();
385 if (client == 0) {
386 client = new Client(this, pid);
387 mClients.add(pid, client);
388 }
389
390 return client;
391}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392
Glenn Kasten9e58b552013-01-18 15:09:48 -0800393sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
394{
395 if (mLogMemoryDealer == 0) {
396 return new NBLog::Writer();
397 }
398 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
399 sp<NBLog::Writer> writer = new NBLog::Writer(size, shared);
400 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
401 if (binder != 0) {
402 interface_cast<IMediaLogService>(binder)->registerWriter(shared, size, name);
403 }
404 return writer;
405}
406
407void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
408{
Glenn Kasten685ef092013-02-04 08:15:34 -0800409 if (writer == 0) {
410 return;
411 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800412 sp<IMemory> iMemory(writer->getIMemory());
413 if (iMemory == 0) {
414 return;
415 }
416 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
417 if (binder != 0) {
418 interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory);
419 // Now the media.log remote reference to IMemory is gone.
420 // When our last local reference to IMemory also drops to zero,
421 // the IMemory destructor will deallocate the region from mMemoryDealer.
422 }
423}
424
Mathias Agopian65ab4712010-07-14 17:59:35 -0700425// IAudioFlinger interface
426
427
428sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800429 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700430 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800431 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700432 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800433 size_t frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800434 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800436 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800437 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700438 int *sessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700439 String8& name,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700440 status_t *status)
441{
442 sp<PlaybackThread::Track> track;
443 sp<TrackHandle> trackHandle;
444 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700445 status_t lStatus;
446 int lSessionId;
447
Glenn Kasten263709e2012-01-06 08:40:01 -0800448 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
449 // but if someone uses binder directly they could bypass that and cause us to crash
450 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000451 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700452 lStatus = BAD_VALUE;
453 goto Exit;
454 }
455
Glenn Kasten60a83922012-06-21 12:56:37 -0700456 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
457 // and we don't yet support 8.24 or 32-bit PCM
458 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
459 ALOGE("createTrack() invalid format %d", format);
460 lStatus = BAD_VALUE;
461 goto Exit;
462 }
463
Mathias Agopian65ab4712010-07-14 17:59:35 -0700464 {
465 Mutex::Autolock _l(mLock);
466 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700467 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700468 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800469 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700470 lStatus = BAD_VALUE;
471 goto Exit;
472 }
473
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800474 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800475 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700476
Steve Block3856b092011-10-20 11:56:00 +0100477 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700478 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700479 // check if an effect chain with the same session ID is present on another
480 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700481 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700482 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
483 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700484 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700485 if (sessions & PlaybackThread::EFFECT_SESSION) {
486 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700487 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700488 }
Eric Laurentde070132010-07-13 04:45:46 -0700489 }
490 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700491 lSessionId = *sessionId;
492 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700493 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700494 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700495 if (sessionId != NULL) {
496 *sessionId = lSessionId;
497 }
498 }
Steve Block3856b092011-10-20 11:56:00 +0100499 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700500
501 track = thread->createTrack_l(client, streamType, sampleRate, format,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800502 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus);
Eric Laurent39e94f82010-07-28 01:32:47 -0700503
504 // move effect chain to this output thread if an effect on same session was waiting
505 // for a track to be created
506 if (lStatus == NO_ERROR && effectThread != NULL) {
507 Mutex::Autolock _dl(thread->mLock);
508 Mutex::Autolock _sl(effectThread->mLock);
509 moveEffectChain_l(lSessionId, effectThread, thread, true);
510 }
Eric Laurenta011e352012-03-29 15:51:43 -0700511
512 // Look for sync events awaiting for a session to be used.
513 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
514 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
515 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700516 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700517 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700518 } else {
519 mPendingSyncEvents[i]->cancel();
520 }
Eric Laurenta011e352012-03-29 15:51:43 -0700521 mPendingSyncEvents.removeAt(i);
522 i--;
523 }
524 }
525 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700526 }
527 if (lStatus == NO_ERROR) {
Glenn Kastend054c322013-07-12 12:59:20 -0700528 // s for server's pid, n for normal mixer name, f for fast index
529 name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0,
530 track->fastIndex());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700531 trackHandle = new TrackHandle(track);
532 } else {
533 // remove local strong reference to Client before deleting the Track so that the Client
534 // destructor is called by the TrackBase destructor with mLock held
535 client.clear();
536 track.clear();
537 }
538
539Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700540 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700541 *status = lStatus;
542 }
543 return trackHandle;
544}
545
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800546uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700547{
548 Mutex::Autolock _l(mLock);
549 PlaybackThread *thread = checkPlaybackThread_l(output);
550 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000551 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700552 return 0;
553 }
554 return thread->sampleRate();
555}
556
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800557int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700558{
559 Mutex::Autolock _l(mLock);
560 PlaybackThread *thread = checkPlaybackThread_l(output);
561 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000562 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700563 return 0;
564 }
565 return thread->channelCount();
566}
567
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800568audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700569{
570 Mutex::Autolock _l(mLock);
571 PlaybackThread *thread = checkPlaybackThread_l(output);
572 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000573 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800574 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700575 }
576 return thread->format();
577}
578
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800579size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700580{
581 Mutex::Autolock _l(mLock);
582 PlaybackThread *thread = checkPlaybackThread_l(output);
583 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000584 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700585 return 0;
586 }
Glenn Kasten58912562012-04-03 10:45:00 -0700587 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
588 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700589 return thread->frameCount();
590}
591
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800592uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593{
594 Mutex::Autolock _l(mLock);
595 PlaybackThread *thread = checkPlaybackThread_l(output);
596 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800597 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700598 return 0;
599 }
600 return thread->latency();
601}
602
603status_t AudioFlinger::setMasterVolume(float value)
604{
Eric Laurenta1884f92011-08-23 08:25:03 -0700605 status_t ret = initCheck();
606 if (ret != NO_ERROR) {
607 return ret;
608 }
609
Mathias Agopian65ab4712010-07-14 17:59:35 -0700610 // check calling permissions
611 if (!settingsAllowed()) {
612 return PERMISSION_DENIED;
613 }
614
Eric Laurenta4c5a552012-03-29 10:12:40 -0700615 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700616 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700617
John Grossmanee578c02012-07-23 17:05:46 -0700618 // Set master volume in the HALs which support it.
619 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
620 AutoMutex lock(mHardwareLock);
621 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800622
John Grossmanee578c02012-07-23 17:05:46 -0700623 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
624 if (dev->canSetMasterVolume()) {
625 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800626 }
John Grossmanee578c02012-07-23 17:05:46 -0700627 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700629
John Grossmanee578c02012-07-23 17:05:46 -0700630 // Now set the master volume in each playback thread. Playback threads
631 // assigned to HALs which do not have master volume support will apply
632 // master volume during the mix operation. Threads with HALs which do
633 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800634 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700635 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700636
637 return NO_ERROR;
638}
639
Glenn Kastenf78aee72012-01-04 11:00:47 -0800640status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641{
Eric Laurenta1884f92011-08-23 08:25:03 -0700642 status_t ret = initCheck();
643 if (ret != NO_ERROR) {
644 return ret;
645 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646
647 // check calling permissions
648 if (!settingsAllowed()) {
649 return PERMISSION_DENIED;
650 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800651 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000652 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700653 return BAD_VALUE;
654 }
655
656 { // scope for the lock
657 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700658 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700659 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700660 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700661 mHardwareStatus = AUDIO_HW_IDLE;
662 }
663
664 if (NO_ERROR == ret) {
665 Mutex::Autolock _l(mLock);
666 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800667 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700668 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700669 }
670
671 return ret;
672}
673
674status_t AudioFlinger::setMicMute(bool state)
675{
Eric Laurenta1884f92011-08-23 08:25:03 -0700676 status_t ret = initCheck();
677 if (ret != NO_ERROR) {
678 return ret;
679 }
680
Mathias Agopian65ab4712010-07-14 17:59:35 -0700681 // check calling permissions
682 if (!settingsAllowed()) {
683 return PERMISSION_DENIED;
684 }
685
686 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700687 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700688 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700689 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690 mHardwareStatus = AUDIO_HW_IDLE;
691 return ret;
692}
693
694bool AudioFlinger::getMicMute() const
695{
Eric Laurenta1884f92011-08-23 08:25:03 -0700696 status_t ret = initCheck();
697 if (ret != NO_ERROR) {
698 return false;
699 }
700
Dima Zavinfce7a472011-04-19 22:30:36 -0700701 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800702 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700703 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700705 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700706 mHardwareStatus = AUDIO_HW_IDLE;
707 return state;
708}
709
710status_t AudioFlinger::setMasterMute(bool muted)
711{
John Grossmand8f178d2012-07-20 14:51:35 -0700712 status_t ret = initCheck();
713 if (ret != NO_ERROR) {
714 return ret;
715 }
716
Mathias Agopian65ab4712010-07-14 17:59:35 -0700717 // check calling permissions
718 if (!settingsAllowed()) {
719 return PERMISSION_DENIED;
720 }
721
John Grossmanee578c02012-07-23 17:05:46 -0700722 Mutex::Autolock _l(mLock);
723 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700724
John Grossmanee578c02012-07-23 17:05:46 -0700725 // Set master mute in the HALs which support it.
726 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
727 AutoMutex lock(mHardwareLock);
728 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700729
John Grossmanee578c02012-07-23 17:05:46 -0700730 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
731 if (dev->canSetMasterMute()) {
732 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700733 }
John Grossmanee578c02012-07-23 17:05:46 -0700734 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700735 }
736
John Grossmanee578c02012-07-23 17:05:46 -0700737 // Now set the master mute in each playback thread. Playback threads
738 // assigned to HALs which do not have master mute support will apply master
739 // mute during the mix operation. Threads with HALs which do support master
740 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800741 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700742 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700743
744 return NO_ERROR;
745}
746
747float AudioFlinger::masterVolume() const
748{
Glenn Kasten98067102011-12-13 11:47:54 -0800749 Mutex::Autolock _l(mLock);
750 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700751}
752
753bool AudioFlinger::masterMute() const
754{
Glenn Kasten98067102011-12-13 11:47:54 -0800755 Mutex::Autolock _l(mLock);
756 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700757}
758
John Grossman4ff14ba2012-02-08 16:37:41 -0800759float AudioFlinger::masterVolume_l() const
760{
John Grossman4ff14ba2012-02-08 16:37:41 -0800761 return mMasterVolume;
762}
763
John Grossmand8f178d2012-07-20 14:51:35 -0700764bool AudioFlinger::masterMute_l() const
765{
John Grossmanee578c02012-07-23 17:05:46 -0700766 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700767}
768
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800769status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
770 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700771{
772 // check calling permissions
773 if (!settingsAllowed()) {
774 return PERMISSION_DENIED;
775 }
776
Glenn Kasten263709e2012-01-06 08:40:01 -0800777 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000778 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700779 return BAD_VALUE;
780 }
781
782 AutoMutex lock(mLock);
783 PlaybackThread *thread = NULL;
784 if (output) {
785 thread = checkPlaybackThread_l(output);
786 if (thread == NULL) {
787 return BAD_VALUE;
788 }
789 }
790
791 mStreamTypes[stream].volume = value;
792
793 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800794 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700795 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700796 }
797 } else {
798 thread->setStreamVolume(stream, value);
799 }
800
801 return NO_ERROR;
802}
803
Glenn Kastenfff6d712012-01-12 16:38:12 -0800804status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700805{
806 // check calling permissions
807 if (!settingsAllowed()) {
808 return PERMISSION_DENIED;
809 }
810
Glenn Kasten263709e2012-01-06 08:40:01 -0800811 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700812 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000813 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814 return BAD_VALUE;
815 }
816
Eric Laurent93575202011-01-18 18:39:02 -0800817 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818 mStreamTypes[stream].mute = muted;
819 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700820 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700821
822 return NO_ERROR;
823}
824
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800825float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700826{
Glenn Kasten263709e2012-01-06 08:40:01 -0800827 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700828 return 0.0f;
829 }
830
831 AutoMutex lock(mLock);
832 float volume;
833 if (output) {
834 PlaybackThread *thread = checkPlaybackThread_l(output);
835 if (thread == NULL) {
836 return 0.0f;
837 }
838 volume = thread->streamVolume(stream);
839 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800840 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700841 }
842
843 return volume;
844}
845
Glenn Kastenfff6d712012-01-12 16:38:12 -0800846bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700847{
Glenn Kasten263709e2012-01-06 08:40:01 -0800848 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700849 return true;
850 }
851
Glenn Kasten6637baa2012-01-09 09:40:36 -0800852 AutoMutex lock(mLock);
853 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700854}
855
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800856status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700857{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700858 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
859 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800860
Mathias Agopian65ab4712010-07-14 17:59:35 -0700861 // check calling permissions
862 if (!settingsAllowed()) {
863 return PERMISSION_DENIED;
864 }
865
Mathias Agopian65ab4712010-07-14 17:59:35 -0700866 // ioHandle == 0 means the parameters are global to the audio hardware interface
867 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700868 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700869 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800870 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700871 AutoMutex lock(mHardwareLock);
872 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
873 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
874 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
875 status_t result = dev->set_parameters(dev, keyValuePairs.string());
876 final_result = result ?: final_result;
877 }
878 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800879 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700880 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
881 AudioParameter param = AudioParameter(keyValuePairs);
882 String8 value;
883 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700884 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
885 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700886 for (size_t i = 0; i < mRecordThreads.size(); i++) {
887 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700888 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700889 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
890 // collect all of the thread's session IDs
891 KeyedVector<int, bool> ids = thread->sessionIds();
892 // suspend effects associated with those session IDs
893 for (size_t j = 0; j < ids.size(); ++j) {
894 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700895 thread->setEffectSuspended(FX_IID_AEC,
896 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700897 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700898 thread->setEffectSuspended(FX_IID_NS,
899 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700900 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700901 }
902 }
Eric Laurentbee53372011-08-29 12:42:48 -0700903 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700904 }
905 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700906 String8 screenState;
907 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
908 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800909 if (isOff != (AudioFlinger::mScreenState & 1)) {
910 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700911 }
912 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700913 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700914 }
915
916 // hold a strong ref on thread in case closeOutput() or closeInput() is called
917 // and the thread is exited once the lock is released
918 sp<ThreadBase> thread;
919 {
920 Mutex::Autolock _l(mLock);
921 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700922 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700923 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800924 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700925 // indicate output device change to all input threads for pre processing
926 AudioParameter param = AudioParameter(keyValuePairs);
927 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700928 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
929 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700930 for (size_t i = 0; i < mRecordThreads.size(); i++) {
931 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
932 }
933 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700934 }
935 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800936 if (thread != 0) {
937 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700938 }
939 return BAD_VALUE;
940}
941
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800942String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700943{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700944 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
945 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946
Eric Laurenta4c5a552012-03-29 10:12:40 -0700947 Mutex::Autolock _l(mLock);
948
Mathias Agopian65ab4712010-07-14 17:59:35 -0700949 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700950 String8 out_s8;
951
Dima Zavin799a70e2011-04-18 16:57:27 -0700952 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800953 char *s;
954 {
955 AutoMutex lock(mHardwareLock);
956 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700957 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800958 s = dev->get_parameters(dev, keys.string());
959 mHardwareStatus = AUDIO_HW_IDLE;
960 }
John Grossmanef7740b2012-02-09 11:28:36 -0800961 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700962 free(s);
963 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700964 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700965 }
966
Mathias Agopian65ab4712010-07-14 17:59:35 -0700967 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
968 if (playbackThread != NULL) {
969 return playbackThread->getParameters(keys);
970 }
971 RecordThread *recordThread = checkRecordThread_l(ioHandle);
972 if (recordThread != NULL) {
973 return recordThread->getParameters(keys);
974 }
975 return String8("");
976}
977
Glenn Kastendd8104c2012-07-02 12:42:44 -0700978size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
979 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700980{
Eric Laurenta1884f92011-08-23 08:25:03 -0700981 status_t ret = initCheck();
982 if (ret != NO_ERROR) {
983 return 0;
984 }
985
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800986 AutoMutex lock(mHardwareLock);
987 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000988 struct audio_config config;
989 memset(&config, 0, sizeof(config));
990 config.sample_rate = sampleRate;
991 config.channel_mask = channelMask;
992 config.format = format;
993
John Grossmanee578c02012-07-23 17:05:46 -0700994 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
995 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800996 mHardwareStatus = AUDIO_HW_IDLE;
997 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700998}
999
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001000unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001001{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001002 Mutex::Autolock _l(mLock);
1003
1004 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1005 if (recordThread != NULL) {
1006 return recordThread->getInputFramesLost();
1007 }
1008 return 0;
1009}
1010
1011status_t AudioFlinger::setVoiceVolume(float value)
1012{
Eric Laurenta1884f92011-08-23 08:25:03 -07001013 status_t ret = initCheck();
1014 if (ret != NO_ERROR) {
1015 return ret;
1016 }
1017
Mathias Agopian65ab4712010-07-14 17:59:35 -07001018 // check calling permissions
1019 if (!settingsAllowed()) {
1020 return PERMISSION_DENIED;
1021 }
1022
1023 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001024 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001025 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001026 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001027 mHardwareStatus = AUDIO_HW_IDLE;
1028
1029 return ret;
1030}
1031
Glenn Kasten26c77552012-11-16 12:01:44 -08001032status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001033 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001034{
1035 status_t status;
1036
1037 Mutex::Autolock _l(mLock);
1038
1039 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1040 if (playbackThread != NULL) {
1041 return playbackThread->getRenderPosition(halFrames, dspFrames);
1042 }
1043
1044 return BAD_VALUE;
1045}
1046
1047void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1048{
1049
1050 Mutex::Autolock _l(mLock);
1051
Glenn Kastenbb001922012-02-03 11:10:26 -08001052 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001053 if (mNotificationClients.indexOfKey(pid) < 0) {
1054 sp<NotificationClient> notificationClient = new NotificationClient(this,
1055 client,
1056 pid);
Steve Block3856b092011-10-20 11:56:00 +01001057 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001058
1059 mNotificationClients.add(pid, notificationClient);
1060
1061 sp<IBinder> binder = client->asBinder();
1062 binder->linkToDeath(notificationClient);
1063
1064 // the config change is always sent from playback or record threads to avoid deadlock
1065 // with AudioSystem::gLock
1066 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001067 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001068 }
1069
1070 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001071 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001072 }
1073 }
1074}
1075
1076void AudioFlinger::removeNotificationClient(pid_t pid)
1077{
1078 Mutex::Autolock _l(mLock);
1079
Glenn Kastena3b09252012-01-20 09:19:01 -08001080 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001081
Steve Block3856b092011-10-20 11:56:00 +01001082 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001083 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001084 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001085 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001086 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001087 ALOGV(" pid %d @ %d", ref->mPid, i);
1088 if (ref->mPid == pid) {
1089 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001090 mAudioSessionRefs.removeAt(i);
1091 delete ref;
1092 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001093 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001094 } else {
1095 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001096 }
1097 }
1098 if (removed) {
1099 purgeStaleEffects_l();
1100 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001101}
1102
1103// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001104void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001105{
1106 size_t size = mNotificationClients.size();
1107 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001108 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1109 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001110 }
1111}
1112
1113// removeClient_l() must be called with AudioFlinger::mLock held
1114void AudioFlinger::removeClient_l(pid_t pid)
1115{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001116 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001117 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001118 mClients.removeItem(pid);
1119}
1120
Eric Laurent717e1282012-06-29 16:36:52 -07001121// getEffectThread_l() must be called with AudioFlinger::mLock held
1122sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1123{
1124 sp<PlaybackThread> thread;
1125
1126 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1127 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1128 ALOG_ASSERT(thread == 0);
1129 thread = mPlaybackThreads.valueAt(i);
1130 }
1131 }
1132
1133 return thread;
1134}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001135
Mathias Agopian65ab4712010-07-14 17:59:35 -07001136
Mathias Agopian65ab4712010-07-14 17:59:35 -07001137
1138// ----------------------------------------------------------------------------
1139
1140AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1141 : RefBase(),
1142 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001143 // 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 -07001144 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001145 mPid(pid),
1146 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001147{
1148 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1149}
1150
1151// Client destructor must be called with AudioFlinger::mLock held
1152AudioFlinger::Client::~Client()
1153{
1154 mAudioFlinger->removeClient_l(mPid);
1155}
1156
Glenn Kasten435dbe62012-01-30 10:15:48 -08001157sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001158{
1159 return mMemoryDealer;
1160}
1161
John Grossman4ff14ba2012-02-08 16:37:41 -08001162// Reserve one of the limited slots for a timed audio track associated
1163// with this client
1164bool AudioFlinger::Client::reserveTimedTrack()
1165{
1166 const int kMaxTimedTracksPerClient = 4;
1167
1168 Mutex::Autolock _l(mTimedTrackLock);
1169
1170 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1171 ALOGW("can not create timed track - pid %d has exceeded the limit",
1172 mPid);
1173 return false;
1174 }
1175
1176 mTimedTrackCount++;
1177 return true;
1178}
1179
1180// Release a slot for a timed audio track
1181void AudioFlinger::Client::releaseTimedTrack()
1182{
1183 Mutex::Autolock _l(mTimedTrackLock);
1184 mTimedTrackCount--;
1185}
1186
Mathias Agopian65ab4712010-07-14 17:59:35 -07001187// ----------------------------------------------------------------------------
1188
1189AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1190 const sp<IAudioFlingerClient>& client,
1191 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001192 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001193{
1194}
1195
1196AudioFlinger::NotificationClient::~NotificationClient()
1197{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001198}
1199
1200void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1201{
1202 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001203 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001204}
1205
Mathias Agopian65ab4712010-07-14 17:59:35 -07001206
1207// ----------------------------------------------------------------------------
1208
1209sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001210 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001212 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001213 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001214 size_t frameCount,
Glenn Kasteneeca3262013-07-31 16:12:48 -07001215 IAudioFlinger::track_flags_t *flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001216 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001217 int *sessionId,
1218 status_t *status)
1219{
1220 sp<RecordThread::RecordTrack> recordTrack;
1221 sp<RecordHandle> recordHandle;
1222 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001223 status_t lStatus;
1224 RecordThread *thread;
1225 size_t inFrameCount;
1226 int lSessionId;
1227
1228 // check calling permissions
1229 if (!recordingAllowed()) {
1230 lStatus = PERMISSION_DENIED;
1231 goto Exit;
1232 }
1233
Glenn Kasten291bb6d2013-07-16 17:23:39 -07001234 if (format != AUDIO_FORMAT_PCM_16_BIT) {
1235 ALOGE("openRecord() invalid format %d", format);
1236 lStatus = BAD_VALUE;
1237 goto Exit;
1238 }
1239
Mathias Agopian65ab4712010-07-14 17:59:35 -07001240 // add client to list
1241 { // scope for mLock
1242 Mutex::Autolock _l(mLock);
1243 thread = checkRecordThread_l(input);
1244 if (thread == NULL) {
1245 lStatus = BAD_VALUE;
1246 goto Exit;
1247 }
1248
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001249 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001250 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001251
1252 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001253 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001254 lSessionId = *sessionId;
1255 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001256 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001257 if (sessionId != NULL) {
1258 *sessionId = lSessionId;
1259 }
1260 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001261 // create new record track.
1262 // The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07001263 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
Glenn Kastenddb0ccf2013-07-31 16:14:50 -07001264 frameCount, lSessionId, flags, tid, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001265 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001266 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001267 // remove local strong reference to Client before deleting the RecordTrack so that the
1268 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001269 client.clear();
1270 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001271 goto Exit;
1272 }
1273
1274 // return to handle to client
1275 recordHandle = new RecordHandle(recordTrack);
1276 lStatus = NO_ERROR;
1277
1278Exit:
1279 if (status) {
1280 *status = lStatus;
1281 }
1282 return recordHandle;
1283}
1284
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001285
1286
Mathias Agopian65ab4712010-07-14 17:59:35 -07001287// ----------------------------------------------------------------------------
1288
Eric Laurenta4c5a552012-03-29 10:12:40 -07001289audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1290{
1291 if (!settingsAllowed()) {
1292 return 0;
1293 }
1294 Mutex::Autolock _l(mLock);
1295 return loadHwModule_l(name);
1296}
1297
1298// loadHwModule_l() must be called with AudioFlinger::mLock held
1299audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1300{
1301 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1302 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1303 ALOGW("loadHwModule() module %s already loaded", name);
1304 return mAudioHwDevs.keyAt(i);
1305 }
1306 }
1307
Eric Laurenta4c5a552012-03-29 10:12:40 -07001308 audio_hw_device_t *dev;
1309
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001310 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001311 if (rc) {
1312 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1313 return 0;
1314 }
1315
1316 mHardwareStatus = AUDIO_HW_INIT;
1317 rc = dev->init_check(dev);
1318 mHardwareStatus = AUDIO_HW_IDLE;
1319 if (rc) {
1320 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1321 return 0;
1322 }
1323
John Grossmanee578c02012-07-23 17:05:46 -07001324 // Check and cache this HAL's level of support for master mute and master
1325 // volume. If this is the first HAL opened, and it supports the get
1326 // methods, use the initial values provided by the HAL as the current
1327 // master mute and volume settings.
1328
1329 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1330 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001331 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001332
1333 if (0 == mAudioHwDevs.size()) {
1334 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1335 if (NULL != dev->get_master_volume) {
1336 float mv;
1337 if (OK == dev->get_master_volume(dev, &mv)) {
1338 mMasterVolume = mv;
1339 }
1340 }
1341
1342 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1343 if (NULL != dev->get_master_mute) {
1344 bool mm;
1345 if (OK == dev->get_master_mute(dev, &mm)) {
1346 mMasterMute = mm;
1347 }
1348 }
1349 }
1350
Eric Laurenta4c5a552012-03-29 10:12:40 -07001351 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001352 if ((NULL != dev->set_master_volume) &&
1353 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1354 flags = static_cast<AudioHwDevice::Flags>(flags |
1355 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1356 }
1357
1358 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1359 if ((NULL != dev->set_master_mute) &&
1360 (OK == dev->set_master_mute(dev, mMasterMute))) {
1361 flags = static_cast<AudioHwDevice::Flags>(flags |
1362 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1363 }
1364
Eric Laurenta4c5a552012-03-29 10:12:40 -07001365 mHardwareStatus = AUDIO_HW_IDLE;
1366 }
1367
1368 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001369 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001370
1371 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001372 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001373
1374 return handle;
1375
1376}
1377
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001378// ----------------------------------------------------------------------------
1379
Glenn Kasten3b16c762012-11-14 08:44:39 -08001380uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001381{
1382 Mutex::Autolock _l(mLock);
1383 PlaybackThread *thread = primaryPlaybackThread_l();
1384 return thread != NULL ? thread->sampleRate() : 0;
1385}
1386
Glenn Kastene33054e2012-11-14 12:54:39 -08001387size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001388{
1389 Mutex::Autolock _l(mLock);
1390 PlaybackThread *thread = primaryPlaybackThread_l();
1391 return thread != NULL ? thread->frameCountHAL() : 0;
1392}
1393
1394// ----------------------------------------------------------------------------
1395
Glenn Kasten4182c4e2013-07-15 14:45:07 -07001396status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice)
1397{
1398 uid_t uid = IPCThreadState::self()->getCallingUid();
1399 if (uid != AID_SYSTEM) {
1400 return PERMISSION_DENIED;
1401 }
1402 Mutex::Autolock _l(mLock);
1403 if (mIsDeviceTypeKnown) {
1404 return INVALID_OPERATION;
1405 }
1406 mIsLowRamDevice = isLowRamDevice;
1407 mIsDeviceTypeKnown = true;
1408 return NO_ERROR;
1409}
1410
1411// ----------------------------------------------------------------------------
1412
Eric Laurenta4c5a552012-03-29 10:12:40 -07001413audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1414 audio_devices_t *pDevices,
1415 uint32_t *pSamplingRate,
1416 audio_format_t *pFormat,
1417 audio_channel_mask_t *pChannelMask,
1418 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001419 audio_output_flags_t flags,
1420 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001421{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001422 PlaybackThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001423 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001424 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001425 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1426 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1427 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1428 if (offloadInfo) {
1429 config.offload_info = *offloadInfo;
1430 }
1431
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001432 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001433 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001434
Eric Laurentbfb1b832013-01-07 09:53:42 -08001435 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x",
Eric Laurenta4c5a552012-03-29 10:12:40 -07001436 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001437 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001438 config.sample_rate,
1439 config.format,
1440 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001441 flags);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001442 ALOGV("openOutput(), offloadInfo %p version 0x%04x",
1443 offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001444
1445 if (pDevices == NULL || *pDevices == 0) {
1446 return 0;
1447 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001448
Mathias Agopian65ab4712010-07-14 17:59:35 -07001449 Mutex::Autolock _l(mLock);
1450
Eric Laurenta4c5a552012-03-29 10:12:40 -07001451 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001452 if (outHwDev == NULL)
1453 return 0;
1454
John Grossmanee578c02012-07-23 17:05:46 -07001455 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001456 audio_io_handle_t id = nextUniqueId();
1457
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001458 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001459
Glenn Kasten34542ac2013-06-26 11:29:02 -07001460 status_t status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001461 id,
1462 *pDevices,
1463 (audio_output_flags_t)flags,
1464 &config,
1465 &outStream);
1466
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001467 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001468 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, "
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001469 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001470 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001471 config.sample_rate,
1472 config.format,
1473 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001474 status);
1475
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001476 if (status == NO_ERROR && outStream != NULL) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001477 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags);
Dima Zavin799a70e2011-04-18 16:57:27 -07001478
Eric Laurentbfb1b832013-01-07 09:53:42 -08001479 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1480 thread = new OffloadThread(this, output, id, *pDevices);
1481 ALOGV("openOutput() created offload output: ID %d thread %p", id, thread);
1482 } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001483 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1484 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001485 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001486 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001487 } else {
1488 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001489 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001490 }
1491 mPlaybackThreads.add(id, thread);
1492
Glenn Kasten7c027242012-12-26 14:43:16 -08001493 if (pSamplingRate != NULL) {
1494 *pSamplingRate = config.sample_rate;
1495 }
1496 if (pFormat != NULL) {
1497 *pFormat = config.format;
1498 }
1499 if (pChannelMask != NULL) {
1500 *pChannelMask = config.channel_mask;
1501 }
1502 if (pLatencyMs != NULL) {
1503 *pLatencyMs = thread->latency();
1504 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001505
1506 // notify client processes of the new output creation
1507 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001508
1509 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001510 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001511 ALOGI("Using module %d has the primary audio interface", module);
1512 mPrimaryHardwareDev = outHwDev;
1513
1514 AutoMutex lock(mHardwareLock);
1515 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001516 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001517 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001518 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001519 return id;
1520 }
1521
1522 return 0;
1523}
1524
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001525audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1526 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001527{
1528 Mutex::Autolock _l(mLock);
1529 MixerThread *thread1 = checkMixerThread_l(output1);
1530 MixerThread *thread2 = checkMixerThread_l(output2);
1531
1532 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001533 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1534 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001535 return 0;
1536 }
1537
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001538 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001539 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1540 thread->addOutputTrack(thread2);
1541 mPlaybackThreads.add(id, thread);
1542 // notify client processes of the new output creation
1543 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1544 return id;
1545}
1546
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001547status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001548{
Glenn Kastend96c5722012-04-25 13:44:49 -07001549 return closeOutput_nonvirtual(output);
1550}
1551
1552status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1553{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001554 // keep strong reference on the playback thread so that
1555 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001556 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001557 {
1558 Mutex::Autolock _l(mLock);
1559 thread = checkPlaybackThread_l(output);
1560 if (thread == NULL) {
1561 return BAD_VALUE;
1562 }
1563
Steve Block3856b092011-10-20 11:56:00 +01001564 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001565
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001566 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001567 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001568 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001569 DuplicatingThread *dupThread =
1570 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001571 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurentbfb1b832013-01-07 09:53:42 -08001572
1573 }
1574 }
1575 }
1576
1577
1578 mPlaybackThreads.removeItem(output);
1579 // save all effects to the default thread
1580 if (mPlaybackThreads.size()) {
1581 PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0));
1582 if (dstThread != NULL) {
1583 // audioflinger lock is held here so the acquisition order of thread locks does not
1584 // matter
1585 Mutex::Autolock _dl(dstThread->mLock);
1586 Mutex::Autolock _sl(thread->mLock);
1587 Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l();
1588 for (size_t i = 0; i < effectChains.size(); i ++) {
1589 moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001590 }
1591 }
1592 }
Glenn Kastena1117922012-01-26 10:53:32 -08001593 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001594 }
1595 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001596 // The thread entity (active unit of execution) is no longer running here,
1597 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001598
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001599 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001600 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001601 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001602 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001603 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001604 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001605 }
1606 return NO_ERROR;
1607}
1608
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001609status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001610{
1611 Mutex::Autolock _l(mLock);
1612 PlaybackThread *thread = checkPlaybackThread_l(output);
1613
1614 if (thread == NULL) {
1615 return BAD_VALUE;
1616 }
1617
Steve Block3856b092011-10-20 11:56:00 +01001618 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001619 thread->suspend();
1620
1621 return NO_ERROR;
1622}
1623
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001624status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001625{
1626 Mutex::Autolock _l(mLock);
1627 PlaybackThread *thread = checkPlaybackThread_l(output);
1628
1629 if (thread == NULL) {
1630 return BAD_VALUE;
1631 }
1632
Steve Block3856b092011-10-20 11:56:00 +01001633 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001634
1635 thread->restore();
1636
1637 return NO_ERROR;
1638}
1639
Eric Laurenta4c5a552012-03-29 10:12:40 -07001640audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1641 audio_devices_t *pDevices,
1642 uint32_t *pSamplingRate,
1643 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001644 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001645{
1646 status_t status;
1647 RecordThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001648 struct audio_config config;
Glenn Kastenfb872cc2013-08-06 10:45:39 -07001649 memset(&config, 0, sizeof(config));
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001650 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1651 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1652 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1653
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001654 uint32_t reqSamplingRate = config.sample_rate;
1655 audio_format_t reqFormat = config.format;
1656 audio_channel_mask_t reqChannels = config.channel_mask;
1657 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001658 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001659
1660 if (pDevices == NULL || *pDevices == 0) {
1661 return 0;
1662 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001663
Mathias Agopian65ab4712010-07-14 17:59:35 -07001664 Mutex::Autolock _l(mLock);
1665
Eric Laurenta4c5a552012-03-29 10:12:40 -07001666 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001667 if (inHwDev == NULL)
1668 return 0;
1669
John Grossmanee578c02012-07-23 17:05:46 -07001670 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001671 audio_io_handle_t id = nextUniqueId();
1672
John Grossmanee578c02012-07-23 17:05:46 -07001673 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001674 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001675 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1676 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001677 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001678 config.sample_rate,
1679 config.format,
1680 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001681 status);
1682
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001683 // If the input could not be opened with the requested parameters and we can handle the
1684 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1685 // 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 -07001686 if (status == BAD_VALUE &&
1687 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1688 (config.sample_rate <= 2 * reqSamplingRate) &&
1689 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001690 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001691 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001692 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001693 }
1694
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001695 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001696
Glenn Kasten46909e72013-02-26 09:20:22 -08001697#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001698 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1699 // or (re-)create if current Pipe is idle and does not match the new format
1700 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001701 enum {
1702 TEE_SINK_NO, // don't copy input
1703 TEE_SINK_NEW, // copy input using a new pipe
1704 TEE_SINK_OLD, // copy input using an existing pipe
1705 } kind;
1706 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1707 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001708 if (!mTeeSinkInputEnabled) {
1709 kind = TEE_SINK_NO;
1710 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001711 kind = TEE_SINK_NO;
1712 } else if (mRecordTeeSink == 0) {
1713 kind = TEE_SINK_NEW;
1714 } else if (mRecordTeeSink->getStrongCount() != 1) {
1715 kind = TEE_SINK_NO;
1716 } else if (format == mRecordTeeSink->format()) {
1717 kind = TEE_SINK_OLD;
1718 } else {
1719 kind = TEE_SINK_NEW;
1720 }
1721 switch (kind) {
1722 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001723 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001724 size_t numCounterOffers = 0;
1725 const NBAIO_Format offers[1] = {format};
1726 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1727 ALOG_ASSERT(index == 0);
1728 PipeReader *pipeReader = new PipeReader(*pipe);
1729 numCounterOffers = 0;
1730 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1731 ALOG_ASSERT(index == 0);
1732 mRecordTeeSink = pipe;
1733 mRecordTeeSource = pipeReader;
1734 teeSink = pipe;
1735 }
1736 break;
1737 case TEE_SINK_OLD:
1738 teeSink = mRecordTeeSink;
1739 break;
1740 case TEE_SINK_NO:
1741 default:
1742 break;
1743 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001744#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001745
Dima Zavin799a70e2011-04-18 16:57:27 -07001746 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1747
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001748 // Start record thread
Glenn Kasten34af0262013-07-30 11:52:39 -07001749 // RecordThread requires both input and output device indication to forward to audio
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001750 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001751 thread = new RecordThread(this,
1752 input,
1753 reqSamplingRate,
1754 reqChannels,
1755 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001756 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001757 *pDevices
1758#ifdef TEE_SINK
1759 , teeSink
1760#endif
1761 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001762 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001763 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001764 if (pSamplingRate != NULL) {
1765 *pSamplingRate = reqSamplingRate;
1766 }
1767 if (pFormat != NULL) {
1768 *pFormat = config.format;
1769 }
1770 if (pChannelMask != NULL) {
1771 *pChannelMask = reqChannels;
1772 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001773
Mathias Agopian65ab4712010-07-14 17:59:35 -07001774 // notify client processes of the new input creation
1775 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1776 return id;
1777 }
1778
1779 return 0;
1780}
1781
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001782status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001783{
Glenn Kastend96c5722012-04-25 13:44:49 -07001784 return closeInput_nonvirtual(input);
1785}
1786
1787status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1788{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001789 // keep strong reference on the record thread so that
1790 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001791 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001792 {
1793 Mutex::Autolock _l(mLock);
1794 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001795 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001796 return BAD_VALUE;
1797 }
1798
Steve Block3856b092011-10-20 11:56:00 +01001799 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001800 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001801 mRecordThreads.removeItem(input);
1802 }
1803 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001804 // The thread entity (active unit of execution) is no longer running here,
1805 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001806
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001807 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001808 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001809 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001810 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001811 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001812
1813 return NO_ERROR;
1814}
1815
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001816status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001817{
1818 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001819 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001820
1821 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1822 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001823 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001824 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001825
1826 return NO_ERROR;
1827}
1828
1829
1830int AudioFlinger::newAudioSessionId()
1831{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001832 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001833}
1834
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001835void AudioFlinger::acquireAudioSessionId(int audioSession)
1836{
1837 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001838 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001839 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001840 size_t num = mAudioSessionRefs.size();
1841 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001842 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001843 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1844 ref->mCnt++;
1845 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001846 return;
1847 }
1848 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001849 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1850 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001851}
1852
1853void AudioFlinger::releaseAudioSessionId(int audioSession)
1854{
1855 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001856 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001857 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001858 size_t num = mAudioSessionRefs.size();
1859 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001860 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001861 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1862 ref->mCnt--;
1863 ALOGV(" decremented refcount to %d", ref->mCnt);
1864 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001865 mAudioSessionRefs.removeAt(i);
1866 delete ref;
1867 purgeStaleEffects_l();
1868 }
1869 return;
1870 }
1871 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001872 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001873}
1874
1875void AudioFlinger::purgeStaleEffects_l() {
1876
Steve Block3856b092011-10-20 11:56:00 +01001877 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001878
1879 Vector< sp<EffectChain> > chains;
1880
1881 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1882 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1883 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1884 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001885 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1886 chains.push(ec);
1887 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001888 }
1889 }
1890 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1891 sp<RecordThread> t = mRecordThreads.valueAt(i);
1892 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1893 sp<EffectChain> ec = t->mEffectChains[j];
1894 chains.push(ec);
1895 }
1896 }
1897
1898 for (size_t i = 0; i < chains.size(); i++) {
1899 sp<EffectChain> ec = chains[i];
1900 int sessionid = ec->sessionId();
1901 sp<ThreadBase> t = ec->mThread.promote();
1902 if (t == 0) {
1903 continue;
1904 }
1905 size_t numsessionrefs = mAudioSessionRefs.size();
1906 bool found = false;
1907 for (size_t k = 0; k < numsessionrefs; k++) {
1908 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001909 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001910 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001911 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001912 found = true;
1913 break;
1914 }
1915 }
1916 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001917 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001918 // remove all effects from the chain
1919 while (ec->mEffects.size()) {
1920 sp<EffectModule> effect = ec->mEffects[0];
1921 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001922 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001923 if (effect->purgeHandles()) {
1924 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001925 }
1926 AudioSystem::unregisterEffect(effect->id());
1927 }
1928 }
1929 }
1930 return;
1931}
1932
Mathias Agopian65ab4712010-07-14 17:59:35 -07001933// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001934AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001935{
Glenn Kastena1117922012-01-26 10:53:32 -08001936 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001937}
1938
1939// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001940AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001941{
1942 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001943 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001944}
1945
1946// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001947AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001948{
Glenn Kastena1117922012-01-26 10:53:32 -08001949 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001950}
1951
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001952uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001953{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001954 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001955}
1956
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001957AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001958{
1959 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1960 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001961 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07001962 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001963 return thread;
1964 }
1965 }
1966 return NULL;
1967}
1968
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001969audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001970{
1971 PlaybackThread *thread = primaryPlaybackThread_l();
1972
1973 if (thread == NULL) {
1974 return 0;
1975 }
1976
Eric Laurentf1c04f92012-08-28 14:26:53 -07001977 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001978}
1979
Eric Laurenta011e352012-03-29 15:51:43 -07001980sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
1981 int triggerSession,
1982 int listenerSession,
1983 sync_event_callback_t callBack,
1984 void *cookie)
1985{
1986 Mutex::Autolock _l(mLock);
1987
1988 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
1989 status_t playStatus = NAME_NOT_FOUND;
1990 status_t recStatus = NAME_NOT_FOUND;
1991 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1992 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
1993 if (playStatus == NO_ERROR) {
1994 return event;
1995 }
1996 }
1997 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1998 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
1999 if (recStatus == NO_ERROR) {
2000 return event;
2001 }
2002 }
2003 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
2004 mPendingSyncEvents.add(event);
2005 } else {
2006 ALOGV("createSyncEvent() invalid event %d", event->type());
2007 event.clear();
2008 }
2009 return event;
2010}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002011
Mathias Agopian65ab4712010-07-14 17:59:35 -07002012// ----------------------------------------------------------------------------
2013// Effect management
2014// ----------------------------------------------------------------------------
2015
2016
Glenn Kastenf587ba52012-01-26 16:25:10 -08002017status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002018{
2019 Mutex::Autolock _l(mLock);
2020 return EffectQueryNumberEffects(numEffects);
2021}
2022
Glenn Kastenf587ba52012-01-26 16:25:10 -08002023status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002024{
2025 Mutex::Autolock _l(mLock);
2026 return EffectQueryEffect(index, descriptor);
2027}
2028
Glenn Kasten5e92a782012-01-30 07:40:52 -08002029status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08002030 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07002031{
2032 Mutex::Autolock _l(mLock);
2033 return EffectGetDescriptor(pUuid, descriptor);
2034}
2035
2036
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002037sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07002038 effect_descriptor_t *pDesc,
2039 const sp<IEffectClient>& effectClient,
2040 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002041 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07002042 int sessionId,
2043 status_t *status,
2044 int *id,
2045 int *enabled)
2046{
2047 status_t lStatus = NO_ERROR;
2048 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002049 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002050
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002051 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002052 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002053 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002054
2055 if (pDesc == NULL) {
2056 lStatus = BAD_VALUE;
2057 goto Exit;
2058 }
2059
Eric Laurent84e9a102010-09-23 16:10:16 -07002060 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002061 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002062 lStatus = PERMISSION_DENIED;
2063 goto Exit;
2064 }
2065
Dima Zavinfce7a472011-04-19 22:30:36 -07002066 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002067 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002068 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002069 lStatus = PERMISSION_DENIED;
2070 goto Exit;
2071 }
2072
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002073 if (io == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07002074 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002075 // output must be specified by AudioPolicyManager when using session
Dima Zavinfce7a472011-04-19 22:30:36 -07002076 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent84e9a102010-09-23 16:10:16 -07002077 lStatus = BAD_VALUE;
2078 goto Exit;
Dima Zavinfce7a472011-04-19 22:30:36 -07002079 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002080 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002081 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent84e9a102010-09-23 16:10:16 -07002082 // and we will exit safely
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002083 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent84e9a102010-09-23 16:10:16 -07002084 }
2085 }
2086
Mathias Agopian65ab4712010-07-14 17:59:35 -07002087 {
2088 Mutex::Autolock _l(mLock);
2089
Mathias Agopian65ab4712010-07-14 17:59:35 -07002090
2091 if (!EffectIsNullUuid(&pDesc->uuid)) {
2092 // if uuid is specified, request effect descriptor
2093 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2094 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002095 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002096 goto Exit;
2097 }
2098 } else {
2099 // if uuid is not specified, look for an available implementation
2100 // of the required type in effect factory
2101 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002102 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002103 lStatus = BAD_VALUE;
2104 goto Exit;
2105 }
2106 uint32_t numEffects = 0;
2107 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002108 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002109 bool found = false;
2110
2111 lStatus = EffectQueryNumberEffects(&numEffects);
2112 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002113 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002114 goto Exit;
2115 }
2116 for (uint32_t i = 0; i < numEffects; i++) {
2117 lStatus = EffectQueryEffect(i, &desc);
2118 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002119 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002120 continue;
2121 }
2122 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2123 // If matching type found save effect descriptor. If the session is
2124 // 0 and the effect is not auxiliary, continue enumeration in case
2125 // an auxiliary version of this effect type is available
2126 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002127 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002128 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002129 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2130 break;
2131 }
2132 }
2133 }
2134 if (!found) {
2135 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002136 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002137 goto Exit;
2138 }
2139 // For same effect type, chose auxiliary version over insert version if
2140 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002141 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002142 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002143 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002144 }
2145 }
2146
2147 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002148 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002149 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2150 lStatus = INVALID_OPERATION;
2151 goto Exit;
2152 }
2153
Eric Laurent59255e42011-07-27 19:49:51 -07002154 // check recording permission for visualizer
2155 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2156 !recordingAllowed()) {
2157 lStatus = PERMISSION_DENIED;
2158 goto Exit;
2159 }
2160
Mathias Agopian65ab4712010-07-14 17:59:35 -07002161 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002162 *pDesc = desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002163
2164 // If output is not specified try to find a matching audio session ID in one of the
2165 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002166 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2167 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002168 // Note: io is never 0 when creating an effect on an input
2169 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002170 // look for the thread where the specified audio session is present
Eric Laurent84e9a102010-09-23 16:10:16 -07002171 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2172 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002173 io = mPlaybackThreads.keyAt(i);
Eric Laurent84e9a102010-09-23 16:10:16 -07002174 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002175 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002176 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002177 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002178 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2179 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2180 io = mRecordThreads.keyAt(i);
2181 break;
2182 }
2183 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002184 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002185 // If no output thread contains the requested session ID, default to
2186 // first output. The effect chain will be moved to the correct output
2187 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002188 if (io == 0 && mPlaybackThreads.size()) {
2189 io = mPlaybackThreads.keyAt(0);
2190 }
Steve Block3856b092011-10-20 11:56:00 +01002191 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002192 }
2193 ThreadBase *thread = checkRecordThread_l(io);
2194 if (thread == NULL) {
2195 thread = checkPlaybackThread_l(io);
2196 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002197 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002198 lStatus = BAD_VALUE;
2199 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002200 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002201 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002202
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002203 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002204
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002205 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002206 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2207 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002208 if (handle != 0 && id != NULL) {
2209 *id = handle->id();
2210 }
2211 }
2212
2213Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002214 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002215 *status = lStatus;
2216 }
2217 return handle;
2218}
2219
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002220status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2221 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002222{
Steve Block3856b092011-10-20 11:56:00 +01002223 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002224 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002225 Mutex::Autolock _l(mLock);
2226 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002227 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002228 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002229 }
Eric Laurentde070132010-07-13 04:45:46 -07002230 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2231 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002232 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002233 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002234 }
Eric Laurentde070132010-07-13 04:45:46 -07002235 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2236 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002237 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002238 return BAD_VALUE;
2239 }
2240
2241 Mutex::Autolock _dl(dstThread->mLock);
2242 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent59255e42011-07-27 19:49:51 -07002243 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurentde070132010-07-13 04:45:46 -07002244
Mathias Agopian65ab4712010-07-14 17:59:35 -07002245 return NO_ERROR;
2246}
2247
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002248// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002249status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002250 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002251 AudioFlinger::PlaybackThread *dstThread,
2252 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002253{
Steve Block3856b092011-10-20 11:56:00 +01002254 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002255 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002256
Eric Laurent59255e42011-07-27 19:49:51 -07002257 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002258 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002259 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002260 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002261 return INVALID_OPERATION;
2262 }
2263
Eric Laurent39e94f82010-07-28 01:32:47 -07002264 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002265 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002266 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002267 // removed.
2268 srcThread->removeEffectChain_l(chain);
2269
2270 // transfer all effects one by one so that new effect chain is created on new thread with
2271 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002272 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent39e94f82010-07-28 01:32:47 -07002273 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002274 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002275 sp<EffectModule> effect = chain->getEffectFromId_l(0);
2276 while (effect != 0) {
2277 srcThread->removeEffect_l(effect);
2278 dstThread->addEffect_l(effect);
Eric Laurentec35a142011-10-05 17:42:25 -07002279 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2280 if (effect->state() == EffectModule::ACTIVE ||
2281 effect->state() == EffectModule::STOPPING) {
2282 effect->start();
2283 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002284 // if the move request is not received from audio policy manager, the effect must be
2285 // re-registered with the new strategy and output
2286 if (dstChain == 0) {
2287 dstChain = effect->chain().promote();
2288 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002289 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent39e94f82010-07-28 01:32:47 -07002290 srcThread->addEffect_l(effect);
2291 return NO_INIT;
2292 }
2293 strategy = dstChain->strategy();
2294 }
2295 if (reRegister) {
2296 AudioSystem::unregisterEffect(effect->id());
2297 AudioSystem::registerEffect(&effect->desc(),
2298 dstOutput,
2299 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002300 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002301 effect->id());
2302 }
Eric Laurentde070132010-07-13 04:45:46 -07002303 effect = chain->getEffectFromId_l(0);
2304 }
2305
2306 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002307}
2308
Glenn Kastenda6ef132013-01-10 12:31:01 -08002309struct Entry {
2310#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2311 char mName[MAX_NAME];
2312};
2313
2314int comparEntry(const void *p1, const void *p2)
2315{
2316 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2317}
2318
Glenn Kasten46909e72013-02-26 09:20:22 -08002319#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002320void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002321{
Eric Laurent81784c32012-11-19 14:55:58 -08002322 NBAIO_Source *teeSource = source.get();
2323 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002324 // .wav rotation
2325 // There is a benign race condition if 2 threads call this simultaneously.
2326 // They would both traverse the directory, but the result would simply be
2327 // failures at unlink() which are ignored. It's also unlikely since
2328 // normally dumpsys is only done by bugreport or from the command line.
2329 char teePath[32+256];
2330 strcpy(teePath, "/data/misc/media");
2331 size_t teePathLen = strlen(teePath);
2332 DIR *dir = opendir(teePath);
2333 teePath[teePathLen++] = '/';
2334 if (dir != NULL) {
2335#define MAX_SORT 20 // number of entries to sort
2336#define MAX_KEEP 10 // number of entries to keep
2337 struct Entry entries[MAX_SORT];
2338 size_t entryCount = 0;
2339 while (entryCount < MAX_SORT) {
2340 struct dirent de;
2341 struct dirent *result = NULL;
2342 int rc = readdir_r(dir, &de, &result);
2343 if (rc != 0) {
2344 ALOGW("readdir_r failed %d", rc);
2345 break;
2346 }
2347 if (result == NULL) {
2348 break;
2349 }
2350 if (result != &de) {
2351 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2352 break;
2353 }
2354 // ignore non .wav file entries
2355 size_t nameLen = strlen(de.d_name);
2356 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2357 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2358 continue;
2359 }
2360 strcpy(entries[entryCount++].mName, de.d_name);
2361 }
2362 (void) closedir(dir);
2363 if (entryCount > MAX_KEEP) {
2364 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2365 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2366 strcpy(&teePath[teePathLen], entries[i].mName);
2367 (void) unlink(teePath);
2368 }
2369 }
2370 } else {
2371 if (fd >= 0) {
2372 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2373 }
2374 }
Eric Laurent81784c32012-11-19 14:55:58 -08002375 char teeTime[16];
2376 struct timeval tv;
2377 gettimeofday(&tv, NULL);
2378 struct tm tm;
2379 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002380 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2381 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2382 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2383 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002384 if (teeFd >= 0) {
2385 char wavHeader[44];
2386 memcpy(wavHeader,
2387 "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",
2388 sizeof(wavHeader));
2389 NBAIO_Format format = teeSource->format();
2390 unsigned channelCount = Format_channelCount(format);
2391 ALOG_ASSERT(channelCount <= FCC_2);
2392 uint32_t sampleRate = Format_sampleRate(format);
2393 wavHeader[22] = channelCount; // number of channels
2394 wavHeader[24] = sampleRate; // sample rate
2395 wavHeader[25] = sampleRate >> 8;
2396 wavHeader[32] = channelCount * 2; // block alignment
2397 write(teeFd, wavHeader, sizeof(wavHeader));
2398 size_t total = 0;
2399 bool firstRead = true;
2400 for (;;) {
2401#define TEE_SINK_READ 1024
2402 short buffer[TEE_SINK_READ * FCC_2];
2403 size_t count = TEE_SINK_READ;
2404 ssize_t actual = teeSource->read(buffer, count,
2405 AudioBufferProvider::kInvalidPTS);
2406 bool wasFirstRead = firstRead;
2407 firstRead = false;
2408 if (actual <= 0) {
2409 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2410 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002411 }
Eric Laurent81784c32012-11-19 14:55:58 -08002412 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002413 }
Eric Laurent81784c32012-11-19 14:55:58 -08002414 ALOG_ASSERT(actual <= (ssize_t)count);
2415 write(teeFd, buffer, actual * channelCount * sizeof(short));
2416 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002417 }
Eric Laurent81784c32012-11-19 14:55:58 -08002418 lseek(teeFd, (off_t) 4, SEEK_SET);
2419 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2420 write(teeFd, &temp, sizeof(temp));
2421 lseek(teeFd, (off_t) 40, SEEK_SET);
2422 temp = total * channelCount * sizeof(short);
2423 write(teeFd, &temp, sizeof(temp));
2424 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002425 if (fd >= 0) {
2426 fdprintf(fd, "tee copied to %s\n", teePath);
2427 }
Eric Laurent59255e42011-07-27 19:49:51 -07002428 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002429 if (fd >= 0) {
2430 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2431 }
Eric Laurent59255e42011-07-27 19:49:51 -07002432 }
2433 }
2434}
Glenn Kasten46909e72013-02-26 09:20:22 -08002435#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002436
Mathias Agopian65ab4712010-07-14 17:59:35 -07002437// ----------------------------------------------------------------------------
2438
2439status_t AudioFlinger::onTransact(
2440 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2441{
2442 return BnAudioFlinger::onTransact(code, data, reply, flags);
2443}
2444
Mathias Agopian65ab4712010-07-14 17:59:35 -07002445}; // namespace android