blob: e0ab8cd6df80ef2ce11250fc004750805b4d7a9a [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 Kastenda6ef132013-01-10 12:31:01 -080022#include <dirent.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070023#include <math.h>
24#include <signal.h>
25#include <sys/time.h>
26#include <sys/resource.h>
27
Gloria Wang9ee159b2011-02-24 14:51:45 -080028#include <binder/IPCThreadState.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070029#include <binder/IServiceManager.h>
30#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070031#include <utils/Trace.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070032#include <binder/Parcel.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070033#include <utils/String16.h>
34#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070035#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070036
Dima Zavinfce7a472011-04-19 22:30:36 -070037#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070038#include <cutils/properties.h>
Glenn Kastenf6b16782011-12-15 09:51:17 -080039#include <cutils/compiler.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070040
Eric Laurent81784c32012-11-19 14:55:58 -080041//#include <private/media/AudioTrackShared.h>
42//#include <private/media/AudioEffectShared.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070043
Dima Zavin64760242011-05-11 14:15:23 -070044#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070045#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070046
47#include "AudioMixer.h"
48#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080049#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
Mathias Agopian65ab4712010-07-14 17:59:35 -070051#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070052#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070053#include <audio_effects/effect_ns.h>
54#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070055
Glenn Kasten3b21c502011-12-15 09:52:39 -080056#include <audio_utils/primitives.h>
57
Eric Laurentfeb0db62011-07-22 09:04:31 -070058#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080059
John Grossman4ff14ba2012-02-08 16:37:41 -080060#include <common_time/cc_helper.h>
Eric Laurent81784c32012-11-19 14:55:58 -080061//#include <common_time/local_clock.h>
Glenn Kasten58912562012-04-03 10:45:00 -070062
Glenn Kasten9e58b552013-01-18 15:09:48 -080063#include <media/IMediaLogService.h>
64
Glenn Kastenda6ef132013-01-10 12:31:01 -080065#include <media/nbaio/Pipe.h>
66#include <media/nbaio/PipeReader.h>
67
Mathias Agopian65ab4712010-07-14 17:59:35 -070068// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070069
John Grossman1c345192012-03-27 14:00:17 -070070// Note: the following macro is used for extremely verbose logging message. In
71// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
72// 0; but one side effect of this is to turn all LOGV's as well. Some messages
73// are so verbose that we want to suppress them even when we have ALOG_ASSERT
74// turned on. Do not uncomment the #def below unless you really know what you
75// are doing and want to see all of the extremely verbose messages.
76//#define VERY_VERY_VERBOSE_LOGGING
77#ifdef VERY_VERY_VERBOSE_LOGGING
78#define ALOGVV ALOGV
79#else
80#define ALOGVV(a...) do { } while(0)
81#endif
Eric Laurentde070132010-07-13 04:45:46 -070082
Mathias Agopian65ab4712010-07-14 17:59:35 -070083namespace android {
84
Glenn Kastenec1d6b52011-12-12 09:04:45 -080085static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
86static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070087
Glenn Kasten58912562012-04-03 10:45:00 -070088
John Grossman4ff14ba2012-02-08 16:37:41 -080089nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080090
Eric Laurent81784c32012-11-19 14:55:58 -080091uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070092
Glenn Kastenda6ef132013-01-10 12:31:01 -080093bool AudioFlinger::mTeeSinkInputEnabled = false;
94bool AudioFlinger::mTeeSinkOutputEnabled = false;
95bool AudioFlinger::mTeeSinkTrackEnabled = false;
96
97size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
98size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
99size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
100
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),
142 mBtNrecIsOff(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700143{
Glenn Kasten9e58b552013-01-18 15:09:48 -0800144 char value[PROPERTY_VALUE_MAX];
145 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
146 if (doLog) {
147 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
148 }
Glenn Kastenda6ef132013-01-10 12:31:01 -0800149 (void) property_get("ro.debuggable", value, "0");
150 int debuggable = atoi(value);
151 int teeEnabled = 0;
152 if (debuggable) {
153 (void) property_get("af.tee", value, "0");
154 teeEnabled = atoi(value);
155 }
156 if (teeEnabled & 1)
157 mTeeSinkInputEnabled = true;
158 if (teeEnabled & 2)
159 mTeeSinkOutputEnabled = true;
160 if (teeEnabled & 4)
161 mTeeSinkTrackEnabled = true;
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700162}
163
164void AudioFlinger::onFirstRef()
165{
Dima Zavin799a70e2011-04-18 16:57:27 -0700166 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700167
Eric Laurent93575202011-01-18 18:39:02 -0800168 Mutex::Autolock _l(mLock);
169
Dima Zavin799a70e2011-04-18 16:57:27 -0700170 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800171 char val_str[PROPERTY_VALUE_MAX] = { 0 };
172 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
173 uint32_t int_val;
174 if (1 == sscanf(val_str, "%u", &int_val)) {
175 mStandbyTimeInNsecs = milliseconds(int_val);
176 ALOGI("Using %u mSec as standby time.", int_val);
177 } else {
178 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
179 ALOGI("Using default %u mSec as standby time.",
180 (uint32_t)(mStandbyTimeInNsecs / 1000000));
181 }
182 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700183
Eric Laurenta4c5a552012-03-29 10:12:40 -0700184 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700185}
186
187AudioFlinger::~AudioFlinger()
188{
189 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700190 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700191 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700192 }
193 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700194 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700195 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700196 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700197
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800198 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
199 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700200 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
201 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700202 }
203}
204
Eric Laurenta4c5a552012-03-29 10:12:40 -0700205static const char * const audio_interfaces[] = {
206 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
207 AUDIO_HARDWARE_MODULE_ID_A2DP,
208 AUDIO_HARDWARE_MODULE_ID_USB,
209};
210#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
211
John Grossmanee578c02012-07-23 17:05:46 -0700212AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
213 audio_module_handle_t module,
214 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700215{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700216 // if module is 0, the request comes from an old policy manager and we should load
217 // well known modules
218 if (module == 0) {
219 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
220 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
221 loadHwModule_l(audio_interfaces[i]);
222 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700223 // then try to find a module supporting the requested device.
224 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
225 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
226 audio_hw_device_t *dev = audioHwDevice->hwDevice();
227 if ((dev->get_supported_devices != NULL) &&
228 (dev->get_supported_devices(dev) & devices) == devices)
229 return audioHwDevice;
230 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700231 } else {
232 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700233 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
234 if (audioHwDevice != NULL) {
235 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700236 }
237 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700238
Dima Zavin799a70e2011-04-18 16:57:27 -0700239 return NULL;
240}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700241
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700242void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700243{
244 const size_t SIZE = 256;
245 char buffer[SIZE];
246 String8 result;
247
248 result.append("Clients:\n");
249 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800250 sp<Client> client = mClients.valueAt(i).promote();
251 if (client != 0) {
252 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
253 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700254 }
255 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700256
257 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800258 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700259 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
260 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800261 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700262 result.append(buffer);
263 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700264 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700265}
266
267
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700268void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700269{
270 const size_t SIZE = 256;
271 char buffer[SIZE];
272 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800273 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700274
John Grossman4ff14ba2012-02-08 16:37:41 -0800275 snprintf(buffer, SIZE, "Hardware status: %d\n"
276 "Standby Time mSec: %u\n",
277 hardwareStatus,
278 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700279 result.append(buffer);
280 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700281}
282
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700283void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700284{
285 const size_t SIZE = 256;
286 char buffer[SIZE];
287 String8 result;
288 snprintf(buffer, SIZE, "Permission Denial: "
289 "can't dump AudioFlinger from pid=%d, uid=%d\n",
290 IPCThreadState::self()->getCallingPid(),
291 IPCThreadState::self()->getCallingUid());
292 result.append(buffer);
293 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700294}
295
Eric Laurent81784c32012-11-19 14:55:58 -0800296bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700297{
298 bool locked = false;
299 for (int i = 0; i < kDumpLockRetries; ++i) {
300 if (mutex.tryLock() == NO_ERROR) {
301 locked = true;
302 break;
303 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800304 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305 }
306 return locked;
307}
308
309status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
310{
Glenn Kasten44deb052012-02-05 18:09:08 -0800311 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700312 dumpPermissionDenial(fd, args);
313 } else {
314 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800315 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316 if (!hardwareLocked) {
317 String8 result(kHardwareLockedString);
318 write(fd, result.string(), result.size());
319 } else {
320 mHardwareLock.unlock();
321 }
322
Eric Laurent81784c32012-11-19 14:55:58 -0800323 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324
325 // failed to lock - AudioFlinger is probably deadlocked
326 if (!locked) {
327 String8 result(kDeadlockedString);
328 write(fd, result.string(), result.size());
329 }
330
331 dumpClients(fd, args);
332 dumpInternals(fd, args);
333
334 // dump playback threads
335 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
336 mPlaybackThreads.valueAt(i)->dump(fd, args);
337 }
338
339 // dump record threads
340 for (size_t i = 0; i < mRecordThreads.size(); i++) {
341 mRecordThreads.valueAt(i)->dump(fd, args);
342 }
343
Dima Zavin799a70e2011-04-18 16:57:27 -0700344 // dump all hardware devs
345 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700346 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700347 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700348 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700349
350 // dump the serially shared record tee sink
351 if (mRecordTeeSource != 0) {
352 dumpTee(fd, mRecordTeeSource);
353 }
354
Glenn Kastend65d73c2012-06-22 17:21:07 -0700355 if (locked) {
356 mLock.unlock();
357 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800358
359 // append a copy of media.log here by forwarding fd to it, but don't attempt
360 // to lookup the service if it's not running, as it will block for a second
361 if (mLogMemoryDealer != 0) {
362 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
363 if (binder != 0) {
364 fdprintf(fd, "\nmedia.log:\n");
365 Vector<String16> args;
366 binder->dump(fd, args);
367 }
368 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700369 }
370 return NO_ERROR;
371}
372
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800373sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
374{
375 // If pid is already in the mClients wp<> map, then use that entry
376 // (for which promote() is always != 0), otherwise create a new entry and Client.
377 sp<Client> client = mClients.valueFor(pid).promote();
378 if (client == 0) {
379 client = new Client(this, pid);
380 mClients.add(pid, client);
381 }
382
383 return client;
384}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385
Glenn Kasten9e58b552013-01-18 15:09:48 -0800386sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
387{
388 if (mLogMemoryDealer == 0) {
389 return new NBLog::Writer();
390 }
391 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
392 sp<NBLog::Writer> writer = new NBLog::Writer(size, shared);
393 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
394 if (binder != 0) {
395 interface_cast<IMediaLogService>(binder)->registerWriter(shared, size, name);
396 }
397 return writer;
398}
399
400void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
401{
Glenn Kasten685ef092013-02-04 08:15:34 -0800402 if (writer == 0) {
403 return;
404 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800405 sp<IMemory> iMemory(writer->getIMemory());
406 if (iMemory == 0) {
407 return;
408 }
409 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
410 if (binder != 0) {
411 interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory);
412 // Now the media.log remote reference to IMemory is gone.
413 // When our last local reference to IMemory also drops to zero,
414 // the IMemory destructor will deallocate the region from mMemoryDealer.
415 }
416}
417
Mathias Agopian65ab4712010-07-14 17:59:35 -0700418// IAudioFlinger interface
419
420
421sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800422 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700423 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800424 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700425 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800426 size_t frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800427 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700428 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800429 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800430 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700431 int *sessionId,
432 status_t *status)
433{
434 sp<PlaybackThread::Track> track;
435 sp<TrackHandle> trackHandle;
436 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700437 status_t lStatus;
438 int lSessionId;
439
Glenn Kasten263709e2012-01-06 08:40:01 -0800440 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
441 // but if someone uses binder directly they could bypass that and cause us to crash
442 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000443 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700444 lStatus = BAD_VALUE;
445 goto Exit;
446 }
447
Glenn Kasten60a83922012-06-21 12:56:37 -0700448 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
449 // and we don't yet support 8.24 or 32-bit PCM
450 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
451 ALOGE("createTrack() invalid format %d", format);
452 lStatus = BAD_VALUE;
453 goto Exit;
454 }
455
Mathias Agopian65ab4712010-07-14 17:59:35 -0700456 {
457 Mutex::Autolock _l(mLock);
458 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700459 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700460 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000461 ALOGE("unknown output thread");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700462 lStatus = BAD_VALUE;
463 goto Exit;
464 }
465
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800466 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800467 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700468
Steve Block3856b092011-10-20 11:56:00 +0100469 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700470 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700471 // check if an effect chain with the same session ID is present on another
472 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700473 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700474 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
475 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700476 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700477 if (sessions & PlaybackThread::EFFECT_SESSION) {
478 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700479 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700480 }
Eric Laurentde070132010-07-13 04:45:46 -0700481 }
482 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700483 lSessionId = *sessionId;
484 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700485 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700486 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700487 if (sessionId != NULL) {
488 *sessionId = lSessionId;
489 }
490 }
Steve Block3856b092011-10-20 11:56:00 +0100491 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700492
493 track = thread->createTrack_l(client, streamType, sampleRate, format,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800494 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus);
Eric Laurent39e94f82010-07-28 01:32:47 -0700495
496 // move effect chain to this output thread if an effect on same session was waiting
497 // for a track to be created
498 if (lStatus == NO_ERROR && effectThread != NULL) {
499 Mutex::Autolock _dl(thread->mLock);
500 Mutex::Autolock _sl(effectThread->mLock);
501 moveEffectChain_l(lSessionId, effectThread, thread, true);
502 }
Eric Laurenta011e352012-03-29 15:51:43 -0700503
504 // Look for sync events awaiting for a session to be used.
505 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
506 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
507 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700508 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700509 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700510 } else {
511 mPendingSyncEvents[i]->cancel();
512 }
Eric Laurenta011e352012-03-29 15:51:43 -0700513 mPendingSyncEvents.removeAt(i);
514 i--;
515 }
516 }
517 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700518 }
519 if (lStatus == NO_ERROR) {
520 trackHandle = new TrackHandle(track);
521 } else {
522 // remove local strong reference to Client before deleting the Track so that the Client
523 // destructor is called by the TrackBase destructor with mLock held
524 client.clear();
525 track.clear();
526 }
527
528Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700529 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700530 *status = lStatus;
531 }
532 return trackHandle;
533}
534
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800535uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700536{
537 Mutex::Autolock _l(mLock);
538 PlaybackThread *thread = checkPlaybackThread_l(output);
539 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000540 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700541 return 0;
542 }
543 return thread->sampleRate();
544}
545
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800546int AudioFlinger::channelCount(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("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700552 return 0;
553 }
554 return thread->channelCount();
555}
556
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800557audio_format_t AudioFlinger::format(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("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800563 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700564 }
565 return thread->format();
566}
567
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800568size_t AudioFlinger::frameCount(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("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700574 return 0;
575 }
Glenn Kasten58912562012-04-03 10:45:00 -0700576 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
577 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700578 return thread->frameCount();
579}
580
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800581uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700582{
583 Mutex::Autolock _l(mLock);
584 PlaybackThread *thread = checkPlaybackThread_l(output);
585 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000586 ALOGW("latency() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587 return 0;
588 }
589 return thread->latency();
590}
591
592status_t AudioFlinger::setMasterVolume(float value)
593{
Eric Laurenta1884f92011-08-23 08:25:03 -0700594 status_t ret = initCheck();
595 if (ret != NO_ERROR) {
596 return ret;
597 }
598
Mathias Agopian65ab4712010-07-14 17:59:35 -0700599 // check calling permissions
600 if (!settingsAllowed()) {
601 return PERMISSION_DENIED;
602 }
603
Eric Laurenta4c5a552012-03-29 10:12:40 -0700604 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700605 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700606
John Grossmanee578c02012-07-23 17:05:46 -0700607 // Set master volume in the HALs which support it.
608 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
609 AutoMutex lock(mHardwareLock);
610 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800611
John Grossmanee578c02012-07-23 17:05:46 -0700612 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
613 if (dev->canSetMasterVolume()) {
614 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800615 }
John Grossmanee578c02012-07-23 17:05:46 -0700616 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700617 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700618
John Grossmanee578c02012-07-23 17:05:46 -0700619 // Now set the master volume in each playback thread. Playback threads
620 // assigned to HALs which do not have master volume support will apply
621 // master volume during the mix operation. Threads with HALs which do
622 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800623 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700624 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700625
626 return NO_ERROR;
627}
628
Glenn Kastenf78aee72012-01-04 11:00:47 -0800629status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700630{
Eric Laurenta1884f92011-08-23 08:25:03 -0700631 status_t ret = initCheck();
632 if (ret != NO_ERROR) {
633 return ret;
634 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635
636 // check calling permissions
637 if (!settingsAllowed()) {
638 return PERMISSION_DENIED;
639 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800640 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000641 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700642 return BAD_VALUE;
643 }
644
645 { // scope for the lock
646 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700647 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700648 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700649 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650 mHardwareStatus = AUDIO_HW_IDLE;
651 }
652
653 if (NO_ERROR == ret) {
654 Mutex::Autolock _l(mLock);
655 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800656 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700657 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700658 }
659
660 return ret;
661}
662
663status_t AudioFlinger::setMicMute(bool state)
664{
Eric Laurenta1884f92011-08-23 08:25:03 -0700665 status_t ret = initCheck();
666 if (ret != NO_ERROR) {
667 return ret;
668 }
669
Mathias Agopian65ab4712010-07-14 17:59:35 -0700670 // check calling permissions
671 if (!settingsAllowed()) {
672 return PERMISSION_DENIED;
673 }
674
675 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700676 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700677 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700678 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700679 mHardwareStatus = AUDIO_HW_IDLE;
680 return ret;
681}
682
683bool AudioFlinger::getMicMute() const
684{
Eric Laurenta1884f92011-08-23 08:25:03 -0700685 status_t ret = initCheck();
686 if (ret != NO_ERROR) {
687 return false;
688 }
689
Dima Zavinfce7a472011-04-19 22:30:36 -0700690 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800691 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700692 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700693 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700694 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700695 mHardwareStatus = AUDIO_HW_IDLE;
696 return state;
697}
698
699status_t AudioFlinger::setMasterMute(bool muted)
700{
John Grossmand8f178d2012-07-20 14:51:35 -0700701 status_t ret = initCheck();
702 if (ret != NO_ERROR) {
703 return ret;
704 }
705
Mathias Agopian65ab4712010-07-14 17:59:35 -0700706 // check calling permissions
707 if (!settingsAllowed()) {
708 return PERMISSION_DENIED;
709 }
710
John Grossmanee578c02012-07-23 17:05:46 -0700711 Mutex::Autolock _l(mLock);
712 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700713
John Grossmanee578c02012-07-23 17:05:46 -0700714 // Set master mute in the HALs which support it.
715 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
716 AutoMutex lock(mHardwareLock);
717 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700718
John Grossmanee578c02012-07-23 17:05:46 -0700719 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
720 if (dev->canSetMasterMute()) {
721 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700722 }
John Grossmanee578c02012-07-23 17:05:46 -0700723 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700724 }
725
John Grossmanee578c02012-07-23 17:05:46 -0700726 // Now set the master mute in each playback thread. Playback threads
727 // assigned to HALs which do not have master mute support will apply master
728 // mute during the mix operation. Threads with HALs which do support master
729 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800730 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700731 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700732
733 return NO_ERROR;
734}
735
736float AudioFlinger::masterVolume() const
737{
Glenn Kasten98067102011-12-13 11:47:54 -0800738 Mutex::Autolock _l(mLock);
739 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700740}
741
742bool AudioFlinger::masterMute() const
743{
Glenn Kasten98067102011-12-13 11:47:54 -0800744 Mutex::Autolock _l(mLock);
745 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700746}
747
John Grossman4ff14ba2012-02-08 16:37:41 -0800748float AudioFlinger::masterVolume_l() const
749{
John Grossman4ff14ba2012-02-08 16:37:41 -0800750 return mMasterVolume;
751}
752
John Grossmand8f178d2012-07-20 14:51:35 -0700753bool AudioFlinger::masterMute_l() const
754{
John Grossmanee578c02012-07-23 17:05:46 -0700755 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700756}
757
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800758status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
759 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700760{
761 // check calling permissions
762 if (!settingsAllowed()) {
763 return PERMISSION_DENIED;
764 }
765
Glenn Kasten263709e2012-01-06 08:40:01 -0800766 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000767 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700768 return BAD_VALUE;
769 }
770
771 AutoMutex lock(mLock);
772 PlaybackThread *thread = NULL;
773 if (output) {
774 thread = checkPlaybackThread_l(output);
775 if (thread == NULL) {
776 return BAD_VALUE;
777 }
778 }
779
780 mStreamTypes[stream].volume = value;
781
782 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800783 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700784 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700785 }
786 } else {
787 thread->setStreamVolume(stream, value);
788 }
789
790 return NO_ERROR;
791}
792
Glenn Kastenfff6d712012-01-12 16:38:12 -0800793status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794{
795 // check calling permissions
796 if (!settingsAllowed()) {
797 return PERMISSION_DENIED;
798 }
799
Glenn Kasten263709e2012-01-06 08:40:01 -0800800 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700801 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000802 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700803 return BAD_VALUE;
804 }
805
Eric Laurent93575202011-01-18 18:39:02 -0800806 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700807 mStreamTypes[stream].mute = muted;
808 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700809 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700810
811 return NO_ERROR;
812}
813
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800814float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700815{
Glenn Kasten263709e2012-01-06 08:40:01 -0800816 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700817 return 0.0f;
818 }
819
820 AutoMutex lock(mLock);
821 float volume;
822 if (output) {
823 PlaybackThread *thread = checkPlaybackThread_l(output);
824 if (thread == NULL) {
825 return 0.0f;
826 }
827 volume = thread->streamVolume(stream);
828 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800829 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700830 }
831
832 return volume;
833}
834
Glenn Kastenfff6d712012-01-12 16:38:12 -0800835bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700836{
Glenn Kasten263709e2012-01-06 08:40:01 -0800837 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700838 return true;
839 }
840
Glenn Kasten6637baa2012-01-09 09:40:36 -0800841 AutoMutex lock(mLock);
842 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700843}
844
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800845status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700846{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700847 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
848 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800849
Mathias Agopian65ab4712010-07-14 17:59:35 -0700850 // check calling permissions
851 if (!settingsAllowed()) {
852 return PERMISSION_DENIED;
853 }
854
Mathias Agopian65ab4712010-07-14 17:59:35 -0700855 // ioHandle == 0 means the parameters are global to the audio hardware interface
856 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700857 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700858 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800859 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700860 AutoMutex lock(mHardwareLock);
861 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
862 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
863 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
864 status_t result = dev->set_parameters(dev, keyValuePairs.string());
865 final_result = result ?: final_result;
866 }
867 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800868 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700869 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
870 AudioParameter param = AudioParameter(keyValuePairs);
871 String8 value;
872 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700873 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
874 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700875 for (size_t i = 0; i < mRecordThreads.size(); i++) {
876 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700877 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700878 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
879 // collect all of the thread's session IDs
880 KeyedVector<int, bool> ids = thread->sessionIds();
881 // suspend effects associated with those session IDs
882 for (size_t j = 0; j < ids.size(); ++j) {
883 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700884 thread->setEffectSuspended(FX_IID_AEC,
885 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700886 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700887 thread->setEffectSuspended(FX_IID_NS,
888 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700889 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700890 }
891 }
Eric Laurentbee53372011-08-29 12:42:48 -0700892 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700893 }
894 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700895 String8 screenState;
896 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
897 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800898 if (isOff != (AudioFlinger::mScreenState & 1)) {
899 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700900 }
901 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700902 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700903 }
904
905 // hold a strong ref on thread in case closeOutput() or closeInput() is called
906 // and the thread is exited once the lock is released
907 sp<ThreadBase> thread;
908 {
909 Mutex::Autolock _l(mLock);
910 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700911 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700912 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800913 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700914 // indicate output device change to all input threads for pre processing
915 AudioParameter param = AudioParameter(keyValuePairs);
916 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700917 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
918 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700919 for (size_t i = 0; i < mRecordThreads.size(); i++) {
920 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
921 }
922 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700923 }
924 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800925 if (thread != 0) {
926 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700927 }
928 return BAD_VALUE;
929}
930
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800931String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700932{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700933 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
934 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700935
Eric Laurenta4c5a552012-03-29 10:12:40 -0700936 Mutex::Autolock _l(mLock);
937
Mathias Agopian65ab4712010-07-14 17:59:35 -0700938 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700939 String8 out_s8;
940
Dima Zavin799a70e2011-04-18 16:57:27 -0700941 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800942 char *s;
943 {
944 AutoMutex lock(mHardwareLock);
945 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700946 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800947 s = dev->get_parameters(dev, keys.string());
948 mHardwareStatus = AUDIO_HW_IDLE;
949 }
John Grossmanef7740b2012-02-09 11:28:36 -0800950 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700951 free(s);
952 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700953 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700954 }
955
Mathias Agopian65ab4712010-07-14 17:59:35 -0700956 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
957 if (playbackThread != NULL) {
958 return playbackThread->getParameters(keys);
959 }
960 RecordThread *recordThread = checkRecordThread_l(ioHandle);
961 if (recordThread != NULL) {
962 return recordThread->getParameters(keys);
963 }
964 return String8("");
965}
966
Glenn Kastendd8104c2012-07-02 12:42:44 -0700967size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
968 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700969{
Eric Laurenta1884f92011-08-23 08:25:03 -0700970 status_t ret = initCheck();
971 if (ret != NO_ERROR) {
972 return 0;
973 }
974
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800975 AutoMutex lock(mHardwareLock);
976 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700977 struct audio_config config = {
978 sample_rate: sampleRate,
Glenn Kastendd8104c2012-07-02 12:42:44 -0700979 channel_mask: channelMask,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700980 format: format,
981 };
John Grossmanee578c02012-07-23 17:05:46 -0700982 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
983 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800984 mHardwareStatus = AUDIO_HW_IDLE;
985 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700986}
987
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800988unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700989{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700990 Mutex::Autolock _l(mLock);
991
992 RecordThread *recordThread = checkRecordThread_l(ioHandle);
993 if (recordThread != NULL) {
994 return recordThread->getInputFramesLost();
995 }
996 return 0;
997}
998
999status_t AudioFlinger::setVoiceVolume(float value)
1000{
Eric Laurenta1884f92011-08-23 08:25:03 -07001001 status_t ret = initCheck();
1002 if (ret != NO_ERROR) {
1003 return ret;
1004 }
1005
Mathias Agopian65ab4712010-07-14 17:59:35 -07001006 // check calling permissions
1007 if (!settingsAllowed()) {
1008 return PERMISSION_DENIED;
1009 }
1010
1011 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001012 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001013 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001014 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001015 mHardwareStatus = AUDIO_HW_IDLE;
1016
1017 return ret;
1018}
1019
Glenn Kasten26c77552012-11-16 12:01:44 -08001020status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001021 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001022{
1023 status_t status;
1024
1025 Mutex::Autolock _l(mLock);
1026
1027 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1028 if (playbackThread != NULL) {
1029 return playbackThread->getRenderPosition(halFrames, dspFrames);
1030 }
1031
1032 return BAD_VALUE;
1033}
1034
1035void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1036{
1037
1038 Mutex::Autolock _l(mLock);
1039
Glenn Kastenbb001922012-02-03 11:10:26 -08001040 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001041 if (mNotificationClients.indexOfKey(pid) < 0) {
1042 sp<NotificationClient> notificationClient = new NotificationClient(this,
1043 client,
1044 pid);
Steve Block3856b092011-10-20 11:56:00 +01001045 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001046
1047 mNotificationClients.add(pid, notificationClient);
1048
1049 sp<IBinder> binder = client->asBinder();
1050 binder->linkToDeath(notificationClient);
1051
1052 // the config change is always sent from playback or record threads to avoid deadlock
1053 // with AudioSystem::gLock
1054 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001055 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001056 }
1057
1058 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001059 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001060 }
1061 }
1062}
1063
1064void AudioFlinger::removeNotificationClient(pid_t pid)
1065{
1066 Mutex::Autolock _l(mLock);
1067
Glenn Kastena3b09252012-01-20 09:19:01 -08001068 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001069
Steve Block3856b092011-10-20 11:56:00 +01001070 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001071 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001072 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001073 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001074 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001075 ALOGV(" pid %d @ %d", ref->mPid, i);
1076 if (ref->mPid == pid) {
1077 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001078 mAudioSessionRefs.removeAt(i);
1079 delete ref;
1080 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001081 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001082 } else {
1083 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001084 }
1085 }
1086 if (removed) {
1087 purgeStaleEffects_l();
1088 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001089}
1090
1091// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001092void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001093{
1094 size_t size = mNotificationClients.size();
1095 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001096 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1097 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001098 }
1099}
1100
1101// removeClient_l() must be called with AudioFlinger::mLock held
1102void AudioFlinger::removeClient_l(pid_t pid)
1103{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001104 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001105 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001106 mClients.removeItem(pid);
1107}
1108
Eric Laurent717e1282012-06-29 16:36:52 -07001109// getEffectThread_l() must be called with AudioFlinger::mLock held
1110sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1111{
1112 sp<PlaybackThread> thread;
1113
1114 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1115 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1116 ALOG_ASSERT(thread == 0);
1117 thread = mPlaybackThreads.valueAt(i);
1118 }
1119 }
1120
1121 return thread;
1122}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001123
Mathias Agopian65ab4712010-07-14 17:59:35 -07001124
Mathias Agopian65ab4712010-07-14 17:59:35 -07001125
1126// ----------------------------------------------------------------------------
1127
1128AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1129 : RefBase(),
1130 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001131 // 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 -07001132 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001133 mPid(pid),
1134 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001135{
1136 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1137}
1138
1139// Client destructor must be called with AudioFlinger::mLock held
1140AudioFlinger::Client::~Client()
1141{
1142 mAudioFlinger->removeClient_l(mPid);
1143}
1144
Glenn Kasten435dbe62012-01-30 10:15:48 -08001145sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001146{
1147 return mMemoryDealer;
1148}
1149
John Grossman4ff14ba2012-02-08 16:37:41 -08001150// Reserve one of the limited slots for a timed audio track associated
1151// with this client
1152bool AudioFlinger::Client::reserveTimedTrack()
1153{
1154 const int kMaxTimedTracksPerClient = 4;
1155
1156 Mutex::Autolock _l(mTimedTrackLock);
1157
1158 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1159 ALOGW("can not create timed track - pid %d has exceeded the limit",
1160 mPid);
1161 return false;
1162 }
1163
1164 mTimedTrackCount++;
1165 return true;
1166}
1167
1168// Release a slot for a timed audio track
1169void AudioFlinger::Client::releaseTimedTrack()
1170{
1171 Mutex::Autolock _l(mTimedTrackLock);
1172 mTimedTrackCount--;
1173}
1174
Mathias Agopian65ab4712010-07-14 17:59:35 -07001175// ----------------------------------------------------------------------------
1176
1177AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1178 const sp<IAudioFlingerClient>& client,
1179 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001180 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001181{
1182}
1183
1184AudioFlinger::NotificationClient::~NotificationClient()
1185{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001186}
1187
1188void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1189{
1190 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001191 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001192}
1193
Mathias Agopian65ab4712010-07-14 17:59:35 -07001194
1195// ----------------------------------------------------------------------------
1196
1197sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001198 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001199 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001200 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001201 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001202 size_t frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -08001203 IAudioFlinger::track_flags_t flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001204 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001205 int *sessionId,
1206 status_t *status)
1207{
1208 sp<RecordThread::RecordTrack> recordTrack;
1209 sp<RecordHandle> recordHandle;
1210 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211 status_t lStatus;
1212 RecordThread *thread;
1213 size_t inFrameCount;
1214 int lSessionId;
1215
1216 // check calling permissions
1217 if (!recordingAllowed()) {
1218 lStatus = PERMISSION_DENIED;
1219 goto Exit;
1220 }
1221
1222 // add client to list
1223 { // scope for mLock
1224 Mutex::Autolock _l(mLock);
1225 thread = checkRecordThread_l(input);
1226 if (thread == NULL) {
1227 lStatus = BAD_VALUE;
1228 goto Exit;
1229 }
1230
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001231 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001232 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001233
1234 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001235 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001236 lSessionId = *sessionId;
1237 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001238 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001239 if (sessionId != NULL) {
1240 *sessionId = lSessionId;
1241 }
1242 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001243 // create new record track.
1244 // The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07001245 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
1246 frameCount, lSessionId, flags, tid, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001247 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001248 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001249 // remove local strong reference to Client before deleting the RecordTrack so that the
1250 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001251 client.clear();
1252 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001253 goto Exit;
1254 }
1255
1256 // return to handle to client
1257 recordHandle = new RecordHandle(recordTrack);
1258 lStatus = NO_ERROR;
1259
1260Exit:
1261 if (status) {
1262 *status = lStatus;
1263 }
1264 return recordHandle;
1265}
1266
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001267
1268
Mathias Agopian65ab4712010-07-14 17:59:35 -07001269// ----------------------------------------------------------------------------
1270
Eric Laurenta4c5a552012-03-29 10:12:40 -07001271audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1272{
1273 if (!settingsAllowed()) {
1274 return 0;
1275 }
1276 Mutex::Autolock _l(mLock);
1277 return loadHwModule_l(name);
1278}
1279
1280// loadHwModule_l() must be called with AudioFlinger::mLock held
1281audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1282{
1283 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1284 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1285 ALOGW("loadHwModule() module %s already loaded", name);
1286 return mAudioHwDevs.keyAt(i);
1287 }
1288 }
1289
Eric Laurenta4c5a552012-03-29 10:12:40 -07001290 audio_hw_device_t *dev;
1291
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001292 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001293 if (rc) {
1294 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1295 return 0;
1296 }
1297
1298 mHardwareStatus = AUDIO_HW_INIT;
1299 rc = dev->init_check(dev);
1300 mHardwareStatus = AUDIO_HW_IDLE;
1301 if (rc) {
1302 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1303 return 0;
1304 }
1305
John Grossmanee578c02012-07-23 17:05:46 -07001306 // Check and cache this HAL's level of support for master mute and master
1307 // volume. If this is the first HAL opened, and it supports the get
1308 // methods, use the initial values provided by the HAL as the current
1309 // master mute and volume settings.
1310
1311 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1312 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001313 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001314
1315 if (0 == mAudioHwDevs.size()) {
1316 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1317 if (NULL != dev->get_master_volume) {
1318 float mv;
1319 if (OK == dev->get_master_volume(dev, &mv)) {
1320 mMasterVolume = mv;
1321 }
1322 }
1323
1324 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1325 if (NULL != dev->get_master_mute) {
1326 bool mm;
1327 if (OK == dev->get_master_mute(dev, &mm)) {
1328 mMasterMute = mm;
1329 }
1330 }
1331 }
1332
Eric Laurenta4c5a552012-03-29 10:12:40 -07001333 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001334 if ((NULL != dev->set_master_volume) &&
1335 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1336 flags = static_cast<AudioHwDevice::Flags>(flags |
1337 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1338 }
1339
1340 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1341 if ((NULL != dev->set_master_mute) &&
1342 (OK == dev->set_master_mute(dev, mMasterMute))) {
1343 flags = static_cast<AudioHwDevice::Flags>(flags |
1344 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1345 }
1346
Eric Laurenta4c5a552012-03-29 10:12:40 -07001347 mHardwareStatus = AUDIO_HW_IDLE;
1348 }
1349
1350 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001351 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001352
1353 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001354 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001355
1356 return handle;
1357
1358}
1359
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001360// ----------------------------------------------------------------------------
1361
Glenn Kasten3b16c762012-11-14 08:44:39 -08001362uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001363{
1364 Mutex::Autolock _l(mLock);
1365 PlaybackThread *thread = primaryPlaybackThread_l();
1366 return thread != NULL ? thread->sampleRate() : 0;
1367}
1368
Glenn Kastene33054e2012-11-14 12:54:39 -08001369size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001370{
1371 Mutex::Autolock _l(mLock);
1372 PlaybackThread *thread = primaryPlaybackThread_l();
1373 return thread != NULL ? thread->frameCountHAL() : 0;
1374}
1375
1376// ----------------------------------------------------------------------------
1377
Eric Laurenta4c5a552012-03-29 10:12:40 -07001378audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1379 audio_devices_t *pDevices,
1380 uint32_t *pSamplingRate,
1381 audio_format_t *pFormat,
1382 audio_channel_mask_t *pChannelMask,
1383 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001384 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001385{
1386 status_t status;
1387 PlaybackThread *thread = NULL;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001388 struct audio_config config = {
1389 sample_rate: pSamplingRate ? *pSamplingRate : 0,
1390 channel_mask: pChannelMask ? *pChannelMask : 0,
1391 format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT,
1392 };
1393 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001394 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001395
Eric Laurenta4c5a552012-03-29 10:12:40 -07001396 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %d, Channels %x, flags %x",
1397 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001398 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001399 config.sample_rate,
1400 config.format,
1401 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001402 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001403
1404 if (pDevices == NULL || *pDevices == 0) {
1405 return 0;
1406 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001407
Mathias Agopian65ab4712010-07-14 17:59:35 -07001408 Mutex::Autolock _l(mLock);
1409
Eric Laurenta4c5a552012-03-29 10:12:40 -07001410 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001411 if (outHwDev == NULL)
1412 return 0;
1413
John Grossmanee578c02012-07-23 17:05:46 -07001414 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001415 audio_io_handle_t id = nextUniqueId();
1416
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001417 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001418
John Grossmanee578c02012-07-23 17:05:46 -07001419 status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001420 id,
1421 *pDevices,
1422 (audio_output_flags_t)flags,
1423 &config,
1424 &outStream);
1425
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001426 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001427 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, "
1428 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001429 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001430 config.sample_rate,
1431 config.format,
1432 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001433 status);
1434
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001435 if (status == NO_ERROR && outStream != NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001436 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001437
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001438 if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001439 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1440 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001441 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001442 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001443 } else {
1444 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001445 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001446 }
1447 mPlaybackThreads.add(id, thread);
1448
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001449 if (pSamplingRate != NULL) *pSamplingRate = config.sample_rate;
1450 if (pFormat != NULL) *pFormat = config.format;
1451 if (pChannelMask != NULL) *pChannelMask = config.channel_mask;
Glenn Kastena0d68332012-01-27 16:47:15 -08001452 if (pLatencyMs != NULL) *pLatencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001453
1454 // notify client processes of the new output creation
1455 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001456
1457 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001458 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001459 ALOGI("Using module %d has the primary audio interface", module);
1460 mPrimaryHardwareDev = outHwDev;
1461
1462 AutoMutex lock(mHardwareLock);
1463 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001464 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001465 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001466 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001467 return id;
1468 }
1469
1470 return 0;
1471}
1472
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001473audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1474 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001475{
1476 Mutex::Autolock _l(mLock);
1477 MixerThread *thread1 = checkMixerThread_l(output1);
1478 MixerThread *thread2 = checkMixerThread_l(output2);
1479
1480 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001481 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1482 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001483 return 0;
1484 }
1485
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001486 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001487 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1488 thread->addOutputTrack(thread2);
1489 mPlaybackThreads.add(id, thread);
1490 // notify client processes of the new output creation
1491 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1492 return id;
1493}
1494
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001495status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001496{
Glenn Kastend96c5722012-04-25 13:44:49 -07001497 return closeOutput_nonvirtual(output);
1498}
1499
1500status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1501{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001502 // keep strong reference on the playback thread so that
1503 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001504 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001505 {
1506 Mutex::Autolock _l(mLock);
1507 thread = checkPlaybackThread_l(output);
1508 if (thread == NULL) {
1509 return BAD_VALUE;
1510 }
1511
Steve Block3856b092011-10-20 11:56:00 +01001512 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001513
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001514 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001515 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001516 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001517 DuplicatingThread *dupThread =
1518 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001519 dupThread->removeOutputTrack((MixerThread *)thread.get());
1520 }
1521 }
1522 }
Glenn Kastena1117922012-01-26 10:53:32 -08001523 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001524 mPlaybackThreads.removeItem(output);
1525 }
1526 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001527 // The thread entity (active unit of execution) is no longer running here,
1528 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001529
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001530 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001531 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001532 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001533 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001534 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001535 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001536 }
1537 return NO_ERROR;
1538}
1539
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001540status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001541{
1542 Mutex::Autolock _l(mLock);
1543 PlaybackThread *thread = checkPlaybackThread_l(output);
1544
1545 if (thread == NULL) {
1546 return BAD_VALUE;
1547 }
1548
Steve Block3856b092011-10-20 11:56:00 +01001549 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001550 thread->suspend();
1551
1552 return NO_ERROR;
1553}
1554
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001555status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001556{
1557 Mutex::Autolock _l(mLock);
1558 PlaybackThread *thread = checkPlaybackThread_l(output);
1559
1560 if (thread == NULL) {
1561 return BAD_VALUE;
1562 }
1563
Steve Block3856b092011-10-20 11:56:00 +01001564 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001565
1566 thread->restore();
1567
1568 return NO_ERROR;
1569}
1570
Eric Laurenta4c5a552012-03-29 10:12:40 -07001571audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1572 audio_devices_t *pDevices,
1573 uint32_t *pSamplingRate,
1574 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001575 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001576{
1577 status_t status;
1578 RecordThread *thread = NULL;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001579 struct audio_config config = {
1580 sample_rate: pSamplingRate ? *pSamplingRate : 0,
1581 channel_mask: pChannelMask ? *pChannelMask : 0,
1582 format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT,
1583 };
1584 uint32_t reqSamplingRate = config.sample_rate;
1585 audio_format_t reqFormat = config.format;
1586 audio_channel_mask_t reqChannels = config.channel_mask;
1587 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001588 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001589
1590 if (pDevices == NULL || *pDevices == 0) {
1591 return 0;
1592 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001593
Mathias Agopian65ab4712010-07-14 17:59:35 -07001594 Mutex::Autolock _l(mLock);
1595
Eric Laurenta4c5a552012-03-29 10:12:40 -07001596 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001597 if (inHwDev == NULL)
1598 return 0;
1599
John Grossmanee578c02012-07-23 17:05:46 -07001600 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001601 audio_io_handle_t id = nextUniqueId();
1602
John Grossmanee578c02012-07-23 17:05:46 -07001603 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001604 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001605 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1606 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001607 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001608 config.sample_rate,
1609 config.format,
1610 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001611 status);
1612
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001613 // If the input could not be opened with the requested parameters and we can handle the
1614 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1615 // 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 -07001616 if (status == BAD_VALUE &&
1617 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1618 (config.sample_rate <= 2 * reqSamplingRate) &&
1619 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001620 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001621 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001622 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001623 }
1624
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001625 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001626
1627 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1628 // or (re-)create if current Pipe is idle and does not match the new format
1629 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001630 enum {
1631 TEE_SINK_NO, // don't copy input
1632 TEE_SINK_NEW, // copy input using a new pipe
1633 TEE_SINK_OLD, // copy input using an existing pipe
1634 } kind;
1635 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1636 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001637 if (!mTeeSinkInputEnabled) {
1638 kind = TEE_SINK_NO;
1639 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001640 kind = TEE_SINK_NO;
1641 } else if (mRecordTeeSink == 0) {
1642 kind = TEE_SINK_NEW;
1643 } else if (mRecordTeeSink->getStrongCount() != 1) {
1644 kind = TEE_SINK_NO;
1645 } else if (format == mRecordTeeSink->format()) {
1646 kind = TEE_SINK_OLD;
1647 } else {
1648 kind = TEE_SINK_NEW;
1649 }
1650 switch (kind) {
1651 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001652 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001653 size_t numCounterOffers = 0;
1654 const NBAIO_Format offers[1] = {format};
1655 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1656 ALOG_ASSERT(index == 0);
1657 PipeReader *pipeReader = new PipeReader(*pipe);
1658 numCounterOffers = 0;
1659 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1660 ALOG_ASSERT(index == 0);
1661 mRecordTeeSink = pipe;
1662 mRecordTeeSource = pipeReader;
1663 teeSink = pipe;
1664 }
1665 break;
1666 case TEE_SINK_OLD:
1667 teeSink = mRecordTeeSink;
1668 break;
1669 case TEE_SINK_NO:
1670 default:
1671 break;
1672 }
Glenn Kastenda6ef132013-01-10 12:31:01 -08001673
Dima Zavin799a70e2011-04-18 16:57:27 -07001674 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1675
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001676 // Start record thread
1677 // RecorThread require both input and output device indication to forward to audio
1678 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001679 thread = new RecordThread(this,
1680 input,
1681 reqSamplingRate,
1682 reqChannels,
1683 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001684 primaryOutputDevice_l(),
1685 *pDevices,
1686 teeSink);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001687 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001688 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kastena0d68332012-01-27 16:47:15 -08001689 if (pSamplingRate != NULL) *pSamplingRate = reqSamplingRate;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001690 if (pFormat != NULL) *pFormat = config.format;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001691 if (pChannelMask != NULL) *pChannelMask = reqChannels;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001692
Mathias Agopian65ab4712010-07-14 17:59:35 -07001693 // notify client processes of the new input creation
1694 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1695 return id;
1696 }
1697
1698 return 0;
1699}
1700
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001701status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001702{
Glenn Kastend96c5722012-04-25 13:44:49 -07001703 return closeInput_nonvirtual(input);
1704}
1705
1706status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1707{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001708 // keep strong reference on the record thread so that
1709 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001710 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001711 {
1712 Mutex::Autolock _l(mLock);
1713 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001714 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001715 return BAD_VALUE;
1716 }
1717
Steve Block3856b092011-10-20 11:56:00 +01001718 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001719 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001720 mRecordThreads.removeItem(input);
1721 }
1722 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001723 // The thread entity (active unit of execution) is no longer running here,
1724 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001725
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001726 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001727 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001728 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001729 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001730 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001731
1732 return NO_ERROR;
1733}
1734
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001735status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001736{
1737 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001738 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001739
1740 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1741 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001742 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001743 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001744
1745 return NO_ERROR;
1746}
1747
1748
1749int AudioFlinger::newAudioSessionId()
1750{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001751 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001752}
1753
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001754void AudioFlinger::acquireAudioSessionId(int audioSession)
1755{
1756 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001757 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001758 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001759 size_t num = mAudioSessionRefs.size();
1760 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001761 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001762 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1763 ref->mCnt++;
1764 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001765 return;
1766 }
1767 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001768 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1769 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001770}
1771
1772void AudioFlinger::releaseAudioSessionId(int audioSession)
1773{
1774 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001775 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001776 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001777 size_t num = mAudioSessionRefs.size();
1778 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001779 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001780 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1781 ref->mCnt--;
1782 ALOGV(" decremented refcount to %d", ref->mCnt);
1783 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001784 mAudioSessionRefs.removeAt(i);
1785 delete ref;
1786 purgeStaleEffects_l();
1787 }
1788 return;
1789 }
1790 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001791 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001792}
1793
1794void AudioFlinger::purgeStaleEffects_l() {
1795
Steve Block3856b092011-10-20 11:56:00 +01001796 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001797
1798 Vector< sp<EffectChain> > chains;
1799
1800 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1801 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1802 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1803 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001804 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1805 chains.push(ec);
1806 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001807 }
1808 }
1809 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1810 sp<RecordThread> t = mRecordThreads.valueAt(i);
1811 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1812 sp<EffectChain> ec = t->mEffectChains[j];
1813 chains.push(ec);
1814 }
1815 }
1816
1817 for (size_t i = 0; i < chains.size(); i++) {
1818 sp<EffectChain> ec = chains[i];
1819 int sessionid = ec->sessionId();
1820 sp<ThreadBase> t = ec->mThread.promote();
1821 if (t == 0) {
1822 continue;
1823 }
1824 size_t numsessionrefs = mAudioSessionRefs.size();
1825 bool found = false;
1826 for (size_t k = 0; k < numsessionrefs; k++) {
1827 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001828 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001829 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001830 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001831 found = true;
1832 break;
1833 }
1834 }
1835 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001836 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001837 // remove all effects from the chain
1838 while (ec->mEffects.size()) {
1839 sp<EffectModule> effect = ec->mEffects[0];
1840 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001841 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001842 if (effect->purgeHandles()) {
1843 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001844 }
1845 AudioSystem::unregisterEffect(effect->id());
1846 }
1847 }
1848 }
1849 return;
1850}
1851
Mathias Agopian65ab4712010-07-14 17:59:35 -07001852// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001853AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001854{
Glenn Kastena1117922012-01-26 10:53:32 -08001855 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001856}
1857
1858// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001859AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001860{
1861 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001862 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001863}
1864
1865// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001866AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001867{
Glenn Kastena1117922012-01-26 10:53:32 -08001868 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001869}
1870
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001871uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001872{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001873 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001874}
1875
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001876AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001877{
1878 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1879 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001880 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07001881 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001882 return thread;
1883 }
1884 }
1885 return NULL;
1886}
1887
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001888audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001889{
1890 PlaybackThread *thread = primaryPlaybackThread_l();
1891
1892 if (thread == NULL) {
1893 return 0;
1894 }
1895
Eric Laurentf1c04f92012-08-28 14:26:53 -07001896 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001897}
1898
Eric Laurenta011e352012-03-29 15:51:43 -07001899sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
1900 int triggerSession,
1901 int listenerSession,
1902 sync_event_callback_t callBack,
1903 void *cookie)
1904{
1905 Mutex::Autolock _l(mLock);
1906
1907 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
1908 status_t playStatus = NAME_NOT_FOUND;
1909 status_t recStatus = NAME_NOT_FOUND;
1910 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1911 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
1912 if (playStatus == NO_ERROR) {
1913 return event;
1914 }
1915 }
1916 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1917 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
1918 if (recStatus == NO_ERROR) {
1919 return event;
1920 }
1921 }
1922 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
1923 mPendingSyncEvents.add(event);
1924 } else {
1925 ALOGV("createSyncEvent() invalid event %d", event->type());
1926 event.clear();
1927 }
1928 return event;
1929}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001930
Mathias Agopian65ab4712010-07-14 17:59:35 -07001931// ----------------------------------------------------------------------------
1932// Effect management
1933// ----------------------------------------------------------------------------
1934
1935
Glenn Kastenf587ba52012-01-26 16:25:10 -08001936status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001937{
1938 Mutex::Autolock _l(mLock);
1939 return EffectQueryNumberEffects(numEffects);
1940}
1941
Glenn Kastenf587ba52012-01-26 16:25:10 -08001942status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001943{
1944 Mutex::Autolock _l(mLock);
1945 return EffectQueryEffect(index, descriptor);
1946}
1947
Glenn Kasten5e92a782012-01-30 07:40:52 -08001948status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08001949 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001950{
1951 Mutex::Autolock _l(mLock);
1952 return EffectGetDescriptor(pUuid, descriptor);
1953}
1954
1955
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001956sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07001957 effect_descriptor_t *pDesc,
1958 const sp<IEffectClient>& effectClient,
1959 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001960 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001961 int sessionId,
1962 status_t *status,
1963 int *id,
1964 int *enabled)
1965{
1966 status_t lStatus = NO_ERROR;
1967 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001968 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001969
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001970 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001971 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001972 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001973
1974 if (pDesc == NULL) {
1975 lStatus = BAD_VALUE;
1976 goto Exit;
1977 }
1978
Eric Laurent84e9a102010-09-23 16:10:16 -07001979 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07001980 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07001981 lStatus = PERMISSION_DENIED;
1982 goto Exit;
1983 }
1984
Dima Zavinfce7a472011-04-19 22:30:36 -07001985 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07001986 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08001987 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07001988 lStatus = PERMISSION_DENIED;
1989 goto Exit;
1990 }
1991
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001992 if (io == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001993 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent84e9a102010-09-23 16:10:16 -07001994 // output must be specified by AudioPolicyManager when using session
Dima Zavinfce7a472011-04-19 22:30:36 -07001995 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent84e9a102010-09-23 16:10:16 -07001996 lStatus = BAD_VALUE;
1997 goto Exit;
Dima Zavinfce7a472011-04-19 22:30:36 -07001998 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent84e9a102010-09-23 16:10:16 -07001999 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002000 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent84e9a102010-09-23 16:10:16 -07002001 // and we will exit safely
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002002 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent84e9a102010-09-23 16:10:16 -07002003 }
2004 }
2005
Mathias Agopian65ab4712010-07-14 17:59:35 -07002006 {
2007 Mutex::Autolock _l(mLock);
2008
Mathias Agopian65ab4712010-07-14 17:59:35 -07002009
2010 if (!EffectIsNullUuid(&pDesc->uuid)) {
2011 // if uuid is specified, request effect descriptor
2012 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2013 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002014 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002015 goto Exit;
2016 }
2017 } else {
2018 // if uuid is not specified, look for an available implementation
2019 // of the required type in effect factory
2020 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002021 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002022 lStatus = BAD_VALUE;
2023 goto Exit;
2024 }
2025 uint32_t numEffects = 0;
2026 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002027 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002028 bool found = false;
2029
2030 lStatus = EffectQueryNumberEffects(&numEffects);
2031 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002032 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002033 goto Exit;
2034 }
2035 for (uint32_t i = 0; i < numEffects; i++) {
2036 lStatus = EffectQueryEffect(i, &desc);
2037 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002038 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002039 continue;
2040 }
2041 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2042 // If matching type found save effect descriptor. If the session is
2043 // 0 and the effect is not auxiliary, continue enumeration in case
2044 // an auxiliary version of this effect type is available
2045 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002046 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002047 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002048 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2049 break;
2050 }
2051 }
2052 }
2053 if (!found) {
2054 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002055 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002056 goto Exit;
2057 }
2058 // For same effect type, chose auxiliary version over insert version if
2059 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002060 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002061 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002062 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002063 }
2064 }
2065
2066 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002067 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002068 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2069 lStatus = INVALID_OPERATION;
2070 goto Exit;
2071 }
2072
Eric Laurent59255e42011-07-27 19:49:51 -07002073 // check recording permission for visualizer
2074 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2075 !recordingAllowed()) {
2076 lStatus = PERMISSION_DENIED;
2077 goto Exit;
2078 }
2079
Mathias Agopian65ab4712010-07-14 17:59:35 -07002080 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002081 *pDesc = desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002082
2083 // If output is not specified try to find a matching audio session ID in one of the
2084 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002085 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2086 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002087 // Note: io is never 0 when creating an effect on an input
2088 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002089 // look for the thread where the specified audio session is present
Eric Laurent84e9a102010-09-23 16:10:16 -07002090 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2091 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002092 io = mPlaybackThreads.keyAt(i);
Eric Laurent84e9a102010-09-23 16:10:16 -07002093 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002094 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002095 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002096 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002097 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2098 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2099 io = mRecordThreads.keyAt(i);
2100 break;
2101 }
2102 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002103 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002104 // If no output thread contains the requested session ID, default to
2105 // first output. The effect chain will be moved to the correct output
2106 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002107 if (io == 0 && mPlaybackThreads.size()) {
2108 io = mPlaybackThreads.keyAt(0);
2109 }
Steve Block3856b092011-10-20 11:56:00 +01002110 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002111 }
2112 ThreadBase *thread = checkRecordThread_l(io);
2113 if (thread == NULL) {
2114 thread = checkPlaybackThread_l(io);
2115 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002116 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002117 lStatus = BAD_VALUE;
2118 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002119 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002120 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002121
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002122 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002123
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002124 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002125 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2126 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002127 if (handle != 0 && id != NULL) {
2128 *id = handle->id();
2129 }
2130 }
2131
2132Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002133 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002134 *status = lStatus;
2135 }
2136 return handle;
2137}
2138
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002139status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2140 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002141{
Steve Block3856b092011-10-20 11:56:00 +01002142 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002143 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002144 Mutex::Autolock _l(mLock);
2145 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002146 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002147 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002148 }
Eric Laurentde070132010-07-13 04:45:46 -07002149 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2150 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002151 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002152 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002153 }
Eric Laurentde070132010-07-13 04:45:46 -07002154 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2155 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002156 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002157 return BAD_VALUE;
2158 }
2159
2160 Mutex::Autolock _dl(dstThread->mLock);
2161 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent59255e42011-07-27 19:49:51 -07002162 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurentde070132010-07-13 04:45:46 -07002163
Mathias Agopian65ab4712010-07-14 17:59:35 -07002164 return NO_ERROR;
2165}
2166
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002167// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002168status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002169 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002170 AudioFlinger::PlaybackThread *dstThread,
2171 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002172{
Steve Block3856b092011-10-20 11:56:00 +01002173 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002174 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002175
Eric Laurent59255e42011-07-27 19:49:51 -07002176 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002177 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002178 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002179 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002180 return INVALID_OPERATION;
2181 }
2182
Eric Laurent39e94f82010-07-28 01:32:47 -07002183 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002184 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002185 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002186 // removed.
2187 srcThread->removeEffectChain_l(chain);
2188
2189 // transfer all effects one by one so that new effect chain is created on new thread with
2190 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002191 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent39e94f82010-07-28 01:32:47 -07002192 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002193 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002194 sp<EffectModule> effect = chain->getEffectFromId_l(0);
2195 while (effect != 0) {
2196 srcThread->removeEffect_l(effect);
2197 dstThread->addEffect_l(effect);
Eric Laurentec35a142011-10-05 17:42:25 -07002198 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2199 if (effect->state() == EffectModule::ACTIVE ||
2200 effect->state() == EffectModule::STOPPING) {
2201 effect->start();
2202 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002203 // if the move request is not received from audio policy manager, the effect must be
2204 // re-registered with the new strategy and output
2205 if (dstChain == 0) {
2206 dstChain = effect->chain().promote();
2207 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002208 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent39e94f82010-07-28 01:32:47 -07002209 srcThread->addEffect_l(effect);
2210 return NO_INIT;
2211 }
2212 strategy = dstChain->strategy();
2213 }
2214 if (reRegister) {
2215 AudioSystem::unregisterEffect(effect->id());
2216 AudioSystem::registerEffect(&effect->desc(),
2217 dstOutput,
2218 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002219 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002220 effect->id());
2221 }
Eric Laurentde070132010-07-13 04:45:46 -07002222 effect = chain->getEffectFromId_l(0);
2223 }
2224
2225 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002226}
2227
Glenn Kastenda6ef132013-01-10 12:31:01 -08002228struct Entry {
2229#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2230 char mName[MAX_NAME];
2231};
2232
2233int comparEntry(const void *p1, const void *p2)
2234{
2235 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2236}
2237
Eric Laurent81784c32012-11-19 14:55:58 -08002238void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002239{
Eric Laurent81784c32012-11-19 14:55:58 -08002240 NBAIO_Source *teeSource = source.get();
2241 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002242 // .wav rotation
2243 // There is a benign race condition if 2 threads call this simultaneously.
2244 // They would both traverse the directory, but the result would simply be
2245 // failures at unlink() which are ignored. It's also unlikely since
2246 // normally dumpsys is only done by bugreport or from the command line.
2247 char teePath[32+256];
2248 strcpy(teePath, "/data/misc/media");
2249 size_t teePathLen = strlen(teePath);
2250 DIR *dir = opendir(teePath);
2251 teePath[teePathLen++] = '/';
2252 if (dir != NULL) {
2253#define MAX_SORT 20 // number of entries to sort
2254#define MAX_KEEP 10 // number of entries to keep
2255 struct Entry entries[MAX_SORT];
2256 size_t entryCount = 0;
2257 while (entryCount < MAX_SORT) {
2258 struct dirent de;
2259 struct dirent *result = NULL;
2260 int rc = readdir_r(dir, &de, &result);
2261 if (rc != 0) {
2262 ALOGW("readdir_r failed %d", rc);
2263 break;
2264 }
2265 if (result == NULL) {
2266 break;
2267 }
2268 if (result != &de) {
2269 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2270 break;
2271 }
2272 // ignore non .wav file entries
2273 size_t nameLen = strlen(de.d_name);
2274 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2275 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2276 continue;
2277 }
2278 strcpy(entries[entryCount++].mName, de.d_name);
2279 }
2280 (void) closedir(dir);
2281 if (entryCount > MAX_KEEP) {
2282 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2283 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2284 strcpy(&teePath[teePathLen], entries[i].mName);
2285 (void) unlink(teePath);
2286 }
2287 }
2288 } else {
2289 if (fd >= 0) {
2290 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2291 }
2292 }
Eric Laurent81784c32012-11-19 14:55:58 -08002293 char teeTime[16];
2294 struct timeval tv;
2295 gettimeofday(&tv, NULL);
2296 struct tm tm;
2297 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002298 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2299 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2300 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2301 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002302 if (teeFd >= 0) {
2303 char wavHeader[44];
2304 memcpy(wavHeader,
2305 "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",
2306 sizeof(wavHeader));
2307 NBAIO_Format format = teeSource->format();
2308 unsigned channelCount = Format_channelCount(format);
2309 ALOG_ASSERT(channelCount <= FCC_2);
2310 uint32_t sampleRate = Format_sampleRate(format);
2311 wavHeader[22] = channelCount; // number of channels
2312 wavHeader[24] = sampleRate; // sample rate
2313 wavHeader[25] = sampleRate >> 8;
2314 wavHeader[32] = channelCount * 2; // block alignment
2315 write(teeFd, wavHeader, sizeof(wavHeader));
2316 size_t total = 0;
2317 bool firstRead = true;
2318 for (;;) {
2319#define TEE_SINK_READ 1024
2320 short buffer[TEE_SINK_READ * FCC_2];
2321 size_t count = TEE_SINK_READ;
2322 ssize_t actual = teeSource->read(buffer, count,
2323 AudioBufferProvider::kInvalidPTS);
2324 bool wasFirstRead = firstRead;
2325 firstRead = false;
2326 if (actual <= 0) {
2327 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2328 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002329 }
Eric Laurent81784c32012-11-19 14:55:58 -08002330 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002331 }
Eric Laurent81784c32012-11-19 14:55:58 -08002332 ALOG_ASSERT(actual <= (ssize_t)count);
2333 write(teeFd, buffer, actual * channelCount * sizeof(short));
2334 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002335 }
Eric Laurent81784c32012-11-19 14:55:58 -08002336 lseek(teeFd, (off_t) 4, SEEK_SET);
2337 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2338 write(teeFd, &temp, sizeof(temp));
2339 lseek(teeFd, (off_t) 40, SEEK_SET);
2340 temp = total * channelCount * sizeof(short);
2341 write(teeFd, &temp, sizeof(temp));
2342 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002343 if (fd >= 0) {
2344 fdprintf(fd, "tee copied to %s\n", teePath);
2345 }
Eric Laurent59255e42011-07-27 19:49:51 -07002346 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002347 if (fd >= 0) {
2348 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2349 }
Eric Laurent59255e42011-07-27 19:49:51 -07002350 }
2351 }
2352}
2353
Mathias Agopian65ab4712010-07-14 17:59:35 -07002354// ----------------------------------------------------------------------------
2355
2356status_t AudioFlinger::onTransact(
2357 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2358{
2359 return BnAudioFlinger::onTransact(code, data, reply, flags);
2360}
2361
Mathias Agopian65ab4712010-07-14 17:59:35 -07002362}; // namespace android