blob: c8e8aba8988d66e2730fbb5db5569303bc21199a [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 Kasten46909e72013-02-26 09:20:22 -080093#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -080094bool AudioFlinger::mTeeSinkInputEnabled = false;
95bool AudioFlinger::mTeeSinkOutputEnabled = false;
96bool AudioFlinger::mTeeSinkTrackEnabled = false;
97
98size_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault;
99size_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault;
100size_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault;
Glenn Kasten46909e72013-02-26 09:20:22 -0800101#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800102
Mathias Agopian65ab4712010-07-14 17:59:35 -0700103// ----------------------------------------------------------------------------
104
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700105static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -0700106{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700107 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -0700108 int rc;
109
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700110 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
111 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
112 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
113 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700114 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700115 }
116 rc = audio_hw_device_open(mod, dev);
117 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
118 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
119 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700120 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700121 }
122 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
123 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
124 rc = BAD_VALUE;
125 goto out;
126 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700127 return 0;
128
129out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700130 *dev = NULL;
131 return rc;
132}
133
Mathias Agopian65ab4712010-07-14 17:59:35 -0700134// ----------------------------------------------------------------------------
135
136AudioFlinger::AudioFlinger()
137 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800138 mPrimaryHardwareDev(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700139 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800140 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800141 mMasterMute(false),
142 mNextUniqueId(1),
143 mMode(AUDIO_MODE_INVALID),
144 mBtNrecIsOff(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700145{
Glenn Kasten949a9262013-04-16 12:35:20 -0700146 getpid_cached = getpid();
Glenn Kasten9e58b552013-01-18 15:09:48 -0800147 char value[PROPERTY_VALUE_MAX];
148 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
149 if (doLog) {
150 mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters");
151 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800152#ifdef TEE_SINK
Glenn Kastenda6ef132013-01-10 12:31:01 -0800153 (void) property_get("ro.debuggable", value, "0");
154 int debuggable = atoi(value);
155 int teeEnabled = 0;
156 if (debuggable) {
157 (void) property_get("af.tee", value, "0");
158 teeEnabled = atoi(value);
159 }
160 if (teeEnabled & 1)
161 mTeeSinkInputEnabled = true;
162 if (teeEnabled & 2)
163 mTeeSinkOutputEnabled = true;
164 if (teeEnabled & 4)
165 mTeeSinkTrackEnabled = true;
Glenn Kasten46909e72013-02-26 09:20:22 -0800166#endif
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700167}
168
169void AudioFlinger::onFirstRef()
170{
Dima Zavin799a70e2011-04-18 16:57:27 -0700171 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700172
Eric Laurent93575202011-01-18 18:39:02 -0800173 Mutex::Autolock _l(mLock);
174
Dima Zavin799a70e2011-04-18 16:57:27 -0700175 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800176 char val_str[PROPERTY_VALUE_MAX] = { 0 };
177 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
178 uint32_t int_val;
179 if (1 == sscanf(val_str, "%u", &int_val)) {
180 mStandbyTimeInNsecs = milliseconds(int_val);
181 ALOGI("Using %u mSec as standby time.", int_val);
182 } else {
183 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
184 ALOGI("Using default %u mSec as standby time.",
185 (uint32_t)(mStandbyTimeInNsecs / 1000000));
186 }
187 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700188
Eric Laurenta4c5a552012-03-29 10:12:40 -0700189 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700190}
191
192AudioFlinger::~AudioFlinger()
193{
194 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700195 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700196 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700197 }
198 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700199 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700200 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700201 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700202
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800203 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
204 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700205 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
206 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700207 }
208}
209
Eric Laurenta4c5a552012-03-29 10:12:40 -0700210static const char * const audio_interfaces[] = {
211 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
212 AUDIO_HARDWARE_MODULE_ID_A2DP,
213 AUDIO_HARDWARE_MODULE_ID_USB,
214};
215#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
216
John Grossmanee578c02012-07-23 17:05:46 -0700217AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
218 audio_module_handle_t module,
219 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700220{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700221 // if module is 0, the request comes from an old policy manager and we should load
222 // well known modules
223 if (module == 0) {
224 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
225 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
226 loadHwModule_l(audio_interfaces[i]);
227 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700228 // then try to find a module supporting the requested device.
229 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
230 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
231 audio_hw_device_t *dev = audioHwDevice->hwDevice();
232 if ((dev->get_supported_devices != NULL) &&
233 (dev->get_supported_devices(dev) & devices) == devices)
234 return audioHwDevice;
235 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700236 } else {
237 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700238 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
239 if (audioHwDevice != NULL) {
240 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700241 }
242 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700243
Dima Zavin799a70e2011-04-18 16:57:27 -0700244 return NULL;
245}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700246
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700247void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700248{
249 const size_t SIZE = 256;
250 char buffer[SIZE];
251 String8 result;
252
253 result.append("Clients:\n");
254 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800255 sp<Client> client = mClients.valueAt(i).promote();
256 if (client != 0) {
257 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
258 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700259 }
260 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700261
262 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800263 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700264 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
265 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800266 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700267 result.append(buffer);
268 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700269 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700270}
271
272
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700273void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700274{
275 const size_t SIZE = 256;
276 char buffer[SIZE];
277 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800278 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700279
John Grossman4ff14ba2012-02-08 16:37:41 -0800280 snprintf(buffer, SIZE, "Hardware status: %d\n"
281 "Standby Time mSec: %u\n",
282 hardwareStatus,
283 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700284 result.append(buffer);
285 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700286}
287
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700288void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700289{
290 const size_t SIZE = 256;
291 char buffer[SIZE];
292 String8 result;
293 snprintf(buffer, SIZE, "Permission Denial: "
294 "can't dump AudioFlinger from pid=%d, uid=%d\n",
295 IPCThreadState::self()->getCallingPid(),
296 IPCThreadState::self()->getCallingUid());
297 result.append(buffer);
298 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700299}
300
Eric Laurent81784c32012-11-19 14:55:58 -0800301bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700302{
303 bool locked = false;
304 for (int i = 0; i < kDumpLockRetries; ++i) {
305 if (mutex.tryLock() == NO_ERROR) {
306 locked = true;
307 break;
308 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800309 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310 }
311 return locked;
312}
313
314status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
315{
Glenn Kasten44deb052012-02-05 18:09:08 -0800316 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700317 dumpPermissionDenial(fd, args);
318 } else {
319 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800320 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700321 if (!hardwareLocked) {
322 String8 result(kHardwareLockedString);
323 write(fd, result.string(), result.size());
324 } else {
325 mHardwareLock.unlock();
326 }
327
Eric Laurent81784c32012-11-19 14:55:58 -0800328 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700329
330 // failed to lock - AudioFlinger is probably deadlocked
331 if (!locked) {
332 String8 result(kDeadlockedString);
333 write(fd, result.string(), result.size());
334 }
335
336 dumpClients(fd, args);
337 dumpInternals(fd, args);
338
339 // dump playback threads
340 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
341 mPlaybackThreads.valueAt(i)->dump(fd, args);
342 }
343
344 // dump record threads
345 for (size_t i = 0; i < mRecordThreads.size(); i++) {
346 mRecordThreads.valueAt(i)->dump(fd, args);
347 }
348
Dima Zavin799a70e2011-04-18 16:57:27 -0700349 // dump all hardware devs
350 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700351 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700352 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700353 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700354
Glenn Kasten46909e72013-02-26 09:20:22 -0800355#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -0700356 // dump the serially shared record tee sink
357 if (mRecordTeeSource != 0) {
358 dumpTee(fd, mRecordTeeSource);
359 }
Glenn Kasten46909e72013-02-26 09:20:22 -0800360#endif
Glenn Kastend06785b2012-09-30 12:29:28 -0700361
Glenn Kastend65d73c2012-06-22 17:21:07 -0700362 if (locked) {
363 mLock.unlock();
364 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800365
366 // append a copy of media.log here by forwarding fd to it, but don't attempt
367 // to lookup the service if it's not running, as it will block for a second
368 if (mLogMemoryDealer != 0) {
369 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
370 if (binder != 0) {
371 fdprintf(fd, "\nmedia.log:\n");
372 Vector<String16> args;
373 binder->dump(fd, args);
374 }
375 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700376 }
377 return NO_ERROR;
378}
379
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800380sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
381{
382 // If pid is already in the mClients wp<> map, then use that entry
383 // (for which promote() is always != 0), otherwise create a new entry and Client.
384 sp<Client> client = mClients.valueFor(pid).promote();
385 if (client == 0) {
386 client = new Client(this, pid);
387 mClients.add(pid, client);
388 }
389
390 return client;
391}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392
Glenn Kasten9e58b552013-01-18 15:09:48 -0800393sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name)
394{
395 if (mLogMemoryDealer == 0) {
396 return new NBLog::Writer();
397 }
398 sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size));
399 sp<NBLog::Writer> writer = new NBLog::Writer(size, shared);
400 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
401 if (binder != 0) {
402 interface_cast<IMediaLogService>(binder)->registerWriter(shared, size, name);
403 }
404 return writer;
405}
406
407void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer)
408{
Glenn Kasten685ef092013-02-04 08:15:34 -0800409 if (writer == 0) {
410 return;
411 }
Glenn Kasten9e58b552013-01-18 15:09:48 -0800412 sp<IMemory> iMemory(writer->getIMemory());
413 if (iMemory == 0) {
414 return;
415 }
416 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log"));
417 if (binder != 0) {
418 interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory);
419 // Now the media.log remote reference to IMemory is gone.
420 // When our last local reference to IMemory also drops to zero,
421 // the IMemory destructor will deallocate the region from mMemoryDealer.
422 }
423}
424
Mathias Agopian65ab4712010-07-14 17:59:35 -0700425// IAudioFlinger interface
426
427
428sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800429 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700430 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800431 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700432 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800433 size_t frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800434 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800436 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800437 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700438 int *sessionId,
439 status_t *status)
440{
441 sp<PlaybackThread::Track> track;
442 sp<TrackHandle> trackHandle;
443 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700444 status_t lStatus;
445 int lSessionId;
446
Glenn Kasten263709e2012-01-06 08:40:01 -0800447 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
448 // but if someone uses binder directly they could bypass that and cause us to crash
449 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000450 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700451 lStatus = BAD_VALUE;
452 goto Exit;
453 }
454
Glenn Kasten60a83922012-06-21 12:56:37 -0700455 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
456 // and we don't yet support 8.24 or 32-bit PCM
457 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
458 ALOGE("createTrack() invalid format %d", format);
459 lStatus = BAD_VALUE;
460 goto Exit;
461 }
462
Mathias Agopian65ab4712010-07-14 17:59:35 -0700463 {
464 Mutex::Autolock _l(mLock);
465 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700466 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700467 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800468 ALOGE("no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700469 lStatus = BAD_VALUE;
470 goto Exit;
471 }
472
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800473 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800474 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700475
Steve Block3856b092011-10-20 11:56:00 +0100476 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700477 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700478 // check if an effect chain with the same session ID is present on another
479 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700480 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700481 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
482 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700483 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700484 if (sessions & PlaybackThread::EFFECT_SESSION) {
485 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700486 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700487 }
Eric Laurentde070132010-07-13 04:45:46 -0700488 }
489 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700490 lSessionId = *sessionId;
491 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700492 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700493 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700494 if (sessionId != NULL) {
495 *sessionId = lSessionId;
496 }
497 }
Steve Block3856b092011-10-20 11:56:00 +0100498 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700499
500 track = thread->createTrack_l(client, streamType, sampleRate, format,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800501 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus);
Eric Laurent39e94f82010-07-28 01:32:47 -0700502
503 // move effect chain to this output thread if an effect on same session was waiting
504 // for a track to be created
505 if (lStatus == NO_ERROR && effectThread != NULL) {
506 Mutex::Autolock _dl(thread->mLock);
507 Mutex::Autolock _sl(effectThread->mLock);
508 moveEffectChain_l(lSessionId, effectThread, thread, true);
509 }
Eric Laurenta011e352012-03-29 15:51:43 -0700510
511 // Look for sync events awaiting for a session to be used.
512 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
513 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
514 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700515 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700516 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700517 } else {
518 mPendingSyncEvents[i]->cancel();
519 }
Eric Laurenta011e352012-03-29 15:51:43 -0700520 mPendingSyncEvents.removeAt(i);
521 i--;
522 }
523 }
524 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700525 }
526 if (lStatus == NO_ERROR) {
527 trackHandle = new TrackHandle(track);
528 } else {
529 // remove local strong reference to Client before deleting the Track so that the Client
530 // destructor is called by the TrackBase destructor with mLock held
531 client.clear();
532 track.clear();
533 }
534
535Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700536 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700537 *status = lStatus;
538 }
539 return trackHandle;
540}
541
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800542uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543{
544 Mutex::Autolock _l(mLock);
545 PlaybackThread *thread = checkPlaybackThread_l(output);
546 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000547 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700548 return 0;
549 }
550 return thread->sampleRate();
551}
552
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800553int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700554{
555 Mutex::Autolock _l(mLock);
556 PlaybackThread *thread = checkPlaybackThread_l(output);
557 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000558 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700559 return 0;
560 }
561 return thread->channelCount();
562}
563
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800564audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700565{
566 Mutex::Autolock _l(mLock);
567 PlaybackThread *thread = checkPlaybackThread_l(output);
568 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000569 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800570 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700571 }
572 return thread->format();
573}
574
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800575size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700576{
577 Mutex::Autolock _l(mLock);
578 PlaybackThread *thread = checkPlaybackThread_l(output);
579 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000580 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700581 return 0;
582 }
Glenn Kasten58912562012-04-03 10:45:00 -0700583 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
584 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700585 return thread->frameCount();
586}
587
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800588uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700589{
590 Mutex::Autolock _l(mLock);
591 PlaybackThread *thread = checkPlaybackThread_l(output);
592 if (thread == NULL) {
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800593 ALOGW("latency(): no playback thread found for output handle %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700594 return 0;
595 }
596 return thread->latency();
597}
598
599status_t AudioFlinger::setMasterVolume(float value)
600{
Eric Laurenta1884f92011-08-23 08:25:03 -0700601 status_t ret = initCheck();
602 if (ret != NO_ERROR) {
603 return ret;
604 }
605
Mathias Agopian65ab4712010-07-14 17:59:35 -0700606 // check calling permissions
607 if (!settingsAllowed()) {
608 return PERMISSION_DENIED;
609 }
610
Eric Laurenta4c5a552012-03-29 10:12:40 -0700611 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700612 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700613
John Grossmanee578c02012-07-23 17:05:46 -0700614 // Set master volume in the HALs which support it.
615 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
616 AutoMutex lock(mHardwareLock);
617 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800618
John Grossmanee578c02012-07-23 17:05:46 -0700619 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
620 if (dev->canSetMasterVolume()) {
621 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800622 }
John Grossmanee578c02012-07-23 17:05:46 -0700623 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700624 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700625
John Grossmanee578c02012-07-23 17:05:46 -0700626 // Now set the master volume in each playback thread. Playback threads
627 // assigned to HALs which do not have master volume support will apply
628 // master volume during the mix operation. Threads with HALs which do
629 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800630 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700631 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700632
633 return NO_ERROR;
634}
635
Glenn Kastenf78aee72012-01-04 11:00:47 -0800636status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700637{
Eric Laurenta1884f92011-08-23 08:25:03 -0700638 status_t ret = initCheck();
639 if (ret != NO_ERROR) {
640 return ret;
641 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700642
643 // check calling permissions
644 if (!settingsAllowed()) {
645 return PERMISSION_DENIED;
646 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800647 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000648 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700649 return BAD_VALUE;
650 }
651
652 { // scope for the lock
653 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700654 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700655 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700656 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657 mHardwareStatus = AUDIO_HW_IDLE;
658 }
659
660 if (NO_ERROR == ret) {
661 Mutex::Autolock _l(mLock);
662 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800663 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700664 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700665 }
666
667 return ret;
668}
669
670status_t AudioFlinger::setMicMute(bool state)
671{
Eric Laurenta1884f92011-08-23 08:25:03 -0700672 status_t ret = initCheck();
673 if (ret != NO_ERROR) {
674 return ret;
675 }
676
Mathias Agopian65ab4712010-07-14 17:59:35 -0700677 // check calling permissions
678 if (!settingsAllowed()) {
679 return PERMISSION_DENIED;
680 }
681
682 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700683 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700684 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700685 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700686 mHardwareStatus = AUDIO_HW_IDLE;
687 return ret;
688}
689
690bool AudioFlinger::getMicMute() const
691{
Eric Laurenta1884f92011-08-23 08:25:03 -0700692 status_t ret = initCheck();
693 if (ret != NO_ERROR) {
694 return false;
695 }
696
Dima Zavinfce7a472011-04-19 22:30:36 -0700697 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800698 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700699 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700700 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700701 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700702 mHardwareStatus = AUDIO_HW_IDLE;
703 return state;
704}
705
706status_t AudioFlinger::setMasterMute(bool muted)
707{
John Grossmand8f178d2012-07-20 14:51:35 -0700708 status_t ret = initCheck();
709 if (ret != NO_ERROR) {
710 return ret;
711 }
712
Mathias Agopian65ab4712010-07-14 17:59:35 -0700713 // check calling permissions
714 if (!settingsAllowed()) {
715 return PERMISSION_DENIED;
716 }
717
John Grossmanee578c02012-07-23 17:05:46 -0700718 Mutex::Autolock _l(mLock);
719 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700720
John Grossmanee578c02012-07-23 17:05:46 -0700721 // Set master mute in the HALs which support it.
722 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
723 AutoMutex lock(mHardwareLock);
724 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700725
John Grossmanee578c02012-07-23 17:05:46 -0700726 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
727 if (dev->canSetMasterMute()) {
728 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700729 }
John Grossmanee578c02012-07-23 17:05:46 -0700730 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700731 }
732
John Grossmanee578c02012-07-23 17:05:46 -0700733 // Now set the master mute in each playback thread. Playback threads
734 // assigned to HALs which do not have master mute support will apply master
735 // mute during the mix operation. Threads with HALs which do support master
736 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800737 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700738 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700739
740 return NO_ERROR;
741}
742
743float AudioFlinger::masterVolume() const
744{
Glenn Kasten98067102011-12-13 11:47:54 -0800745 Mutex::Autolock _l(mLock);
746 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700747}
748
749bool AudioFlinger::masterMute() const
750{
Glenn Kasten98067102011-12-13 11:47:54 -0800751 Mutex::Autolock _l(mLock);
752 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700753}
754
John Grossman4ff14ba2012-02-08 16:37:41 -0800755float AudioFlinger::masterVolume_l() const
756{
John Grossman4ff14ba2012-02-08 16:37:41 -0800757 return mMasterVolume;
758}
759
John Grossmand8f178d2012-07-20 14:51:35 -0700760bool AudioFlinger::masterMute_l() const
761{
John Grossmanee578c02012-07-23 17:05:46 -0700762 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700763}
764
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800765status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
766 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700767{
768 // check calling permissions
769 if (!settingsAllowed()) {
770 return PERMISSION_DENIED;
771 }
772
Glenn Kasten263709e2012-01-06 08:40:01 -0800773 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000774 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700775 return BAD_VALUE;
776 }
777
778 AutoMutex lock(mLock);
779 PlaybackThread *thread = NULL;
780 if (output) {
781 thread = checkPlaybackThread_l(output);
782 if (thread == NULL) {
783 return BAD_VALUE;
784 }
785 }
786
787 mStreamTypes[stream].volume = value;
788
789 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800790 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700791 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700792 }
793 } else {
794 thread->setStreamVolume(stream, value);
795 }
796
797 return NO_ERROR;
798}
799
Glenn Kastenfff6d712012-01-12 16:38:12 -0800800status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700801{
802 // check calling permissions
803 if (!settingsAllowed()) {
804 return PERMISSION_DENIED;
805 }
806
Glenn Kasten263709e2012-01-06 08:40:01 -0800807 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700808 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000809 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700810 return BAD_VALUE;
811 }
812
Eric Laurent93575202011-01-18 18:39:02 -0800813 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814 mStreamTypes[stream].mute = muted;
815 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700816 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700817
818 return NO_ERROR;
819}
820
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800821float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700822{
Glenn Kasten263709e2012-01-06 08:40:01 -0800823 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700824 return 0.0f;
825 }
826
827 AutoMutex lock(mLock);
828 float volume;
829 if (output) {
830 PlaybackThread *thread = checkPlaybackThread_l(output);
831 if (thread == NULL) {
832 return 0.0f;
833 }
834 volume = thread->streamVolume(stream);
835 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800836 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700837 }
838
839 return volume;
840}
841
Glenn Kastenfff6d712012-01-12 16:38:12 -0800842bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700843{
Glenn Kasten263709e2012-01-06 08:40:01 -0800844 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700845 return true;
846 }
847
Glenn Kasten6637baa2012-01-09 09:40:36 -0800848 AutoMutex lock(mLock);
849 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700850}
851
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800852status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700853{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700854 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
855 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800856
Mathias Agopian65ab4712010-07-14 17:59:35 -0700857 // check calling permissions
858 if (!settingsAllowed()) {
859 return PERMISSION_DENIED;
860 }
861
Mathias Agopian65ab4712010-07-14 17:59:35 -0700862 // ioHandle == 0 means the parameters are global to the audio hardware interface
863 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700864 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700865 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800866 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700867 AutoMutex lock(mHardwareLock);
868 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
869 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
870 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
871 status_t result = dev->set_parameters(dev, keyValuePairs.string());
872 final_result = result ?: final_result;
873 }
874 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800875 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700876 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
877 AudioParameter param = AudioParameter(keyValuePairs);
878 String8 value;
879 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700880 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
881 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700882 for (size_t i = 0; i < mRecordThreads.size(); i++) {
883 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700884 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700885 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
886 // collect all of the thread's session IDs
887 KeyedVector<int, bool> ids = thread->sessionIds();
888 // suspend effects associated with those session IDs
889 for (size_t j = 0; j < ids.size(); ++j) {
890 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700891 thread->setEffectSuspended(FX_IID_AEC,
892 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700893 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700894 thread->setEffectSuspended(FX_IID_NS,
895 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700896 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700897 }
898 }
Eric Laurentbee53372011-08-29 12:42:48 -0700899 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700900 }
901 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700902 String8 screenState;
903 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
904 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800905 if (isOff != (AudioFlinger::mScreenState & 1)) {
906 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700907 }
908 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700909 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700910 }
911
912 // hold a strong ref on thread in case closeOutput() or closeInput() is called
913 // and the thread is exited once the lock is released
914 sp<ThreadBase> thread;
915 {
916 Mutex::Autolock _l(mLock);
917 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700918 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700919 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800920 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700921 // indicate output device change to all input threads for pre processing
922 AudioParameter param = AudioParameter(keyValuePairs);
923 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700924 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
925 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700926 for (size_t i = 0; i < mRecordThreads.size(); i++) {
927 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
928 }
929 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700930 }
931 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800932 if (thread != 0) {
933 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700934 }
935 return BAD_VALUE;
936}
937
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800938String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700939{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700940 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
941 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700942
Eric Laurenta4c5a552012-03-29 10:12:40 -0700943 Mutex::Autolock _l(mLock);
944
Mathias Agopian65ab4712010-07-14 17:59:35 -0700945 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700946 String8 out_s8;
947
Dima Zavin799a70e2011-04-18 16:57:27 -0700948 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800949 char *s;
950 {
951 AutoMutex lock(mHardwareLock);
952 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700953 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800954 s = dev->get_parameters(dev, keys.string());
955 mHardwareStatus = AUDIO_HW_IDLE;
956 }
John Grossmanef7740b2012-02-09 11:28:36 -0800957 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700958 free(s);
959 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700960 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961 }
962
Mathias Agopian65ab4712010-07-14 17:59:35 -0700963 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
964 if (playbackThread != NULL) {
965 return playbackThread->getParameters(keys);
966 }
967 RecordThread *recordThread = checkRecordThread_l(ioHandle);
968 if (recordThread != NULL) {
969 return recordThread->getParameters(keys);
970 }
971 return String8("");
972}
973
Glenn Kastendd8104c2012-07-02 12:42:44 -0700974size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
975 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700976{
Eric Laurenta1884f92011-08-23 08:25:03 -0700977 status_t ret = initCheck();
978 if (ret != NO_ERROR) {
979 return 0;
980 }
981
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800982 AutoMutex lock(mHardwareLock);
983 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000984 struct audio_config config;
985 memset(&config, 0, sizeof(config));
986 config.sample_rate = sampleRate;
987 config.channel_mask = channelMask;
988 config.format = format;
989
John Grossmanee578c02012-07-23 17:05:46 -0700990 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
991 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800992 mHardwareStatus = AUDIO_HW_IDLE;
993 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700994}
995
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800996unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700997{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700998 Mutex::Autolock _l(mLock);
999
1000 RecordThread *recordThread = checkRecordThread_l(ioHandle);
1001 if (recordThread != NULL) {
1002 return recordThread->getInputFramesLost();
1003 }
1004 return 0;
1005}
1006
1007status_t AudioFlinger::setVoiceVolume(float value)
1008{
Eric Laurenta1884f92011-08-23 08:25:03 -07001009 status_t ret = initCheck();
1010 if (ret != NO_ERROR) {
1011 return ret;
1012 }
1013
Mathias Agopian65ab4712010-07-14 17:59:35 -07001014 // check calling permissions
1015 if (!settingsAllowed()) {
1016 return PERMISSION_DENIED;
1017 }
1018
1019 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001020 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001021 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001022 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001023 mHardwareStatus = AUDIO_HW_IDLE;
1024
1025 return ret;
1026}
1027
Glenn Kasten26c77552012-11-16 12:01:44 -08001028status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001029 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001030{
1031 status_t status;
1032
1033 Mutex::Autolock _l(mLock);
1034
1035 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
1036 if (playbackThread != NULL) {
1037 return playbackThread->getRenderPosition(halFrames, dspFrames);
1038 }
1039
1040 return BAD_VALUE;
1041}
1042
1043void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
1044{
1045
1046 Mutex::Autolock _l(mLock);
1047
Glenn Kastenbb001922012-02-03 11:10:26 -08001048 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001049 if (mNotificationClients.indexOfKey(pid) < 0) {
1050 sp<NotificationClient> notificationClient = new NotificationClient(this,
1051 client,
1052 pid);
Steve Block3856b092011-10-20 11:56:00 +01001053 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001054
1055 mNotificationClients.add(pid, notificationClient);
1056
1057 sp<IBinder> binder = client->asBinder();
1058 binder->linkToDeath(notificationClient);
1059
1060 // the config change is always sent from playback or record threads to avoid deadlock
1061 // with AudioSystem::gLock
1062 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001063 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001064 }
1065
1066 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -07001067 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001068 }
1069 }
1070}
1071
1072void AudioFlinger::removeNotificationClient(pid_t pid)
1073{
1074 Mutex::Autolock _l(mLock);
1075
Glenn Kastena3b09252012-01-20 09:19:01 -08001076 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001077
Steve Block3856b092011-10-20 11:56:00 +01001078 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001079 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001080 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001081 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001082 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001083 ALOGV(" pid %d @ %d", ref->mPid, i);
1084 if (ref->mPid == pid) {
1085 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001086 mAudioSessionRefs.removeAt(i);
1087 delete ref;
1088 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001089 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001090 } else {
1091 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001092 }
1093 }
1094 if (removed) {
1095 purgeStaleEffects_l();
1096 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001097}
1098
1099// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001100void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001101{
1102 size_t size = mNotificationClients.size();
1103 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001104 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1105 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001106 }
1107}
1108
1109// removeClient_l() must be called with AudioFlinger::mLock held
1110void AudioFlinger::removeClient_l(pid_t pid)
1111{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001112 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001113 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001114 mClients.removeItem(pid);
1115}
1116
Eric Laurent717e1282012-06-29 16:36:52 -07001117// getEffectThread_l() must be called with AudioFlinger::mLock held
1118sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1119{
1120 sp<PlaybackThread> thread;
1121
1122 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1123 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1124 ALOG_ASSERT(thread == 0);
1125 thread = mPlaybackThreads.valueAt(i);
1126 }
1127 }
1128
1129 return thread;
1130}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001131
Mathias Agopian65ab4712010-07-14 17:59:35 -07001132
Mathias Agopian65ab4712010-07-14 17:59:35 -07001133
1134// ----------------------------------------------------------------------------
1135
1136AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1137 : RefBase(),
1138 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001139 // 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 -07001140 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001141 mPid(pid),
1142 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001143{
1144 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1145}
1146
1147// Client destructor must be called with AudioFlinger::mLock held
1148AudioFlinger::Client::~Client()
1149{
1150 mAudioFlinger->removeClient_l(mPid);
1151}
1152
Glenn Kasten435dbe62012-01-30 10:15:48 -08001153sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001154{
1155 return mMemoryDealer;
1156}
1157
John Grossman4ff14ba2012-02-08 16:37:41 -08001158// Reserve one of the limited slots for a timed audio track associated
1159// with this client
1160bool AudioFlinger::Client::reserveTimedTrack()
1161{
1162 const int kMaxTimedTracksPerClient = 4;
1163
1164 Mutex::Autolock _l(mTimedTrackLock);
1165
1166 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1167 ALOGW("can not create timed track - pid %d has exceeded the limit",
1168 mPid);
1169 return false;
1170 }
1171
1172 mTimedTrackCount++;
1173 return true;
1174}
1175
1176// Release a slot for a timed audio track
1177void AudioFlinger::Client::releaseTimedTrack()
1178{
1179 Mutex::Autolock _l(mTimedTrackLock);
1180 mTimedTrackCount--;
1181}
1182
Mathias Agopian65ab4712010-07-14 17:59:35 -07001183// ----------------------------------------------------------------------------
1184
1185AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1186 const sp<IAudioFlingerClient>& client,
1187 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001188 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001189{
1190}
1191
1192AudioFlinger::NotificationClient::~NotificationClient()
1193{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001194}
1195
1196void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1197{
1198 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001199 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001200}
1201
Mathias Agopian65ab4712010-07-14 17:59:35 -07001202
1203// ----------------------------------------------------------------------------
1204
1205sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001206 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001207 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001208 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001209 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001210 size_t frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -08001211 IAudioFlinger::track_flags_t flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001212 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001213 int *sessionId,
1214 status_t *status)
1215{
1216 sp<RecordThread::RecordTrack> recordTrack;
1217 sp<RecordHandle> recordHandle;
1218 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001219 status_t lStatus;
1220 RecordThread *thread;
1221 size_t inFrameCount;
1222 int lSessionId;
1223
1224 // check calling permissions
1225 if (!recordingAllowed()) {
1226 lStatus = PERMISSION_DENIED;
1227 goto Exit;
1228 }
1229
1230 // add client to list
1231 { // scope for mLock
1232 Mutex::Autolock _l(mLock);
1233 thread = checkRecordThread_l(input);
1234 if (thread == NULL) {
1235 lStatus = BAD_VALUE;
1236 goto Exit;
1237 }
1238
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001239 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001240 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001241
1242 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001243 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001244 lSessionId = *sessionId;
1245 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001246 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001247 if (sessionId != NULL) {
1248 *sessionId = lSessionId;
1249 }
1250 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001251 // create new record track.
1252 // The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07001253 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
1254 frameCount, lSessionId, flags, tid, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001255 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001256 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001257 // remove local strong reference to Client before deleting the RecordTrack so that the
1258 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001259 client.clear();
1260 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001261 goto Exit;
1262 }
1263
1264 // return to handle to client
1265 recordHandle = new RecordHandle(recordTrack);
1266 lStatus = NO_ERROR;
1267
1268Exit:
1269 if (status) {
1270 *status = lStatus;
1271 }
1272 return recordHandle;
1273}
1274
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001275
1276
Mathias Agopian65ab4712010-07-14 17:59:35 -07001277// ----------------------------------------------------------------------------
1278
Eric Laurenta4c5a552012-03-29 10:12:40 -07001279audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1280{
1281 if (!settingsAllowed()) {
1282 return 0;
1283 }
1284 Mutex::Autolock _l(mLock);
1285 return loadHwModule_l(name);
1286}
1287
1288// loadHwModule_l() must be called with AudioFlinger::mLock held
1289audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1290{
1291 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1292 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1293 ALOGW("loadHwModule() module %s already loaded", name);
1294 return mAudioHwDevs.keyAt(i);
1295 }
1296 }
1297
Eric Laurenta4c5a552012-03-29 10:12:40 -07001298 audio_hw_device_t *dev;
1299
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001300 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001301 if (rc) {
1302 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1303 return 0;
1304 }
1305
1306 mHardwareStatus = AUDIO_HW_INIT;
1307 rc = dev->init_check(dev);
1308 mHardwareStatus = AUDIO_HW_IDLE;
1309 if (rc) {
1310 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1311 return 0;
1312 }
1313
John Grossmanee578c02012-07-23 17:05:46 -07001314 // Check and cache this HAL's level of support for master mute and master
1315 // volume. If this is the first HAL opened, and it supports the get
1316 // methods, use the initial values provided by the HAL as the current
1317 // master mute and volume settings.
1318
1319 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1320 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001321 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001322
1323 if (0 == mAudioHwDevs.size()) {
1324 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1325 if (NULL != dev->get_master_volume) {
1326 float mv;
1327 if (OK == dev->get_master_volume(dev, &mv)) {
1328 mMasterVolume = mv;
1329 }
1330 }
1331
1332 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1333 if (NULL != dev->get_master_mute) {
1334 bool mm;
1335 if (OK == dev->get_master_mute(dev, &mm)) {
1336 mMasterMute = mm;
1337 }
1338 }
1339 }
1340
Eric Laurenta4c5a552012-03-29 10:12:40 -07001341 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001342 if ((NULL != dev->set_master_volume) &&
1343 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1344 flags = static_cast<AudioHwDevice::Flags>(flags |
1345 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1346 }
1347
1348 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1349 if ((NULL != dev->set_master_mute) &&
1350 (OK == dev->set_master_mute(dev, mMasterMute))) {
1351 flags = static_cast<AudioHwDevice::Flags>(flags |
1352 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1353 }
1354
Eric Laurenta4c5a552012-03-29 10:12:40 -07001355 mHardwareStatus = AUDIO_HW_IDLE;
1356 }
1357
1358 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001359 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001360
1361 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001362 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001363
1364 return handle;
1365
1366}
1367
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001368// ----------------------------------------------------------------------------
1369
Glenn Kasten3b16c762012-11-14 08:44:39 -08001370uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001371{
1372 Mutex::Autolock _l(mLock);
1373 PlaybackThread *thread = primaryPlaybackThread_l();
1374 return thread != NULL ? thread->sampleRate() : 0;
1375}
1376
Glenn Kastene33054e2012-11-14 12:54:39 -08001377size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001378{
1379 Mutex::Autolock _l(mLock);
1380 PlaybackThread *thread = primaryPlaybackThread_l();
1381 return thread != NULL ? thread->frameCountHAL() : 0;
1382}
1383
1384// ----------------------------------------------------------------------------
1385
Eric Laurenta4c5a552012-03-29 10:12:40 -07001386audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1387 audio_devices_t *pDevices,
1388 uint32_t *pSamplingRate,
1389 audio_format_t *pFormat,
1390 audio_channel_mask_t *pChannelMask,
1391 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001392 audio_output_flags_t flags,
1393 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001394{
1395 status_t status;
1396 PlaybackThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001397 struct audio_config config;
1398 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1399 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1400 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1401 if (offloadInfo) {
1402 config.offload_info = *offloadInfo;
1403 }
1404
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001405 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001406 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001407
Eric Laurenta4c5a552012-03-29 10:12:40 -07001408 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %d, Channels %x, flags %x",
1409 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001410 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001411 config.sample_rate,
1412 config.format,
1413 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001414 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001415
1416 if (pDevices == NULL || *pDevices == 0) {
1417 return 0;
1418 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001419
Mathias Agopian65ab4712010-07-14 17:59:35 -07001420 Mutex::Autolock _l(mLock);
1421
Eric Laurenta4c5a552012-03-29 10:12:40 -07001422 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001423 if (outHwDev == NULL)
1424 return 0;
1425
John Grossmanee578c02012-07-23 17:05:46 -07001426 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001427 audio_io_handle_t id = nextUniqueId();
1428
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001429 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001430
John Grossmanee578c02012-07-23 17:05:46 -07001431 status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001432 id,
1433 *pDevices,
1434 (audio_output_flags_t)flags,
1435 &config,
1436 &outStream);
1437
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001438 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001439 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, "
1440 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001441 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001442 config.sample_rate,
1443 config.format,
1444 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001445 status);
1446
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001447 if (status == NO_ERROR && outStream != NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001448 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001449
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001450 if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001451 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1452 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001453 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001454 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001455 } else {
1456 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001457 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001458 }
1459 mPlaybackThreads.add(id, thread);
1460
Glenn Kasten7c027242012-12-26 14:43:16 -08001461 if (pSamplingRate != NULL) {
1462 *pSamplingRate = config.sample_rate;
1463 }
1464 if (pFormat != NULL) {
1465 *pFormat = config.format;
1466 }
1467 if (pChannelMask != NULL) {
1468 *pChannelMask = config.channel_mask;
1469 }
1470 if (pLatencyMs != NULL) {
1471 *pLatencyMs = thread->latency();
1472 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001473
1474 // notify client processes of the new output creation
1475 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001476
1477 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001478 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001479 ALOGI("Using module %d has the primary audio interface", module);
1480 mPrimaryHardwareDev = outHwDev;
1481
1482 AutoMutex lock(mHardwareLock);
1483 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001484 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001485 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001486 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001487 return id;
1488 }
1489
1490 return 0;
1491}
1492
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001493audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1494 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001495{
1496 Mutex::Autolock _l(mLock);
1497 MixerThread *thread1 = checkMixerThread_l(output1);
1498 MixerThread *thread2 = checkMixerThread_l(output2);
1499
1500 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001501 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1502 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001503 return 0;
1504 }
1505
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001506 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001507 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1508 thread->addOutputTrack(thread2);
1509 mPlaybackThreads.add(id, thread);
1510 // notify client processes of the new output creation
1511 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1512 return id;
1513}
1514
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001515status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001516{
Glenn Kastend96c5722012-04-25 13:44:49 -07001517 return closeOutput_nonvirtual(output);
1518}
1519
1520status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1521{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001522 // keep strong reference on the playback thread so that
1523 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001524 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001525 {
1526 Mutex::Autolock _l(mLock);
1527 thread = checkPlaybackThread_l(output);
1528 if (thread == NULL) {
1529 return BAD_VALUE;
1530 }
1531
Steve Block3856b092011-10-20 11:56:00 +01001532 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001533
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001534 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001535 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001536 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001537 DuplicatingThread *dupThread =
1538 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001539 dupThread->removeOutputTrack((MixerThread *)thread.get());
1540 }
1541 }
1542 }
Glenn Kastena1117922012-01-26 10:53:32 -08001543 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001544 mPlaybackThreads.removeItem(output);
1545 }
1546 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001547 // The thread entity (active unit of execution) is no longer running here,
1548 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001549
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001550 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001551 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001552 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001553 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001554 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001555 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001556 }
1557 return NO_ERROR;
1558}
1559
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001560status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001561{
1562 Mutex::Autolock _l(mLock);
1563 PlaybackThread *thread = checkPlaybackThread_l(output);
1564
1565 if (thread == NULL) {
1566 return BAD_VALUE;
1567 }
1568
Steve Block3856b092011-10-20 11:56:00 +01001569 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001570 thread->suspend();
1571
1572 return NO_ERROR;
1573}
1574
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001575status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001576{
1577 Mutex::Autolock _l(mLock);
1578 PlaybackThread *thread = checkPlaybackThread_l(output);
1579
1580 if (thread == NULL) {
1581 return BAD_VALUE;
1582 }
1583
Steve Block3856b092011-10-20 11:56:00 +01001584 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001585
1586 thread->restore();
1587
1588 return NO_ERROR;
1589}
1590
Eric Laurenta4c5a552012-03-29 10:12:40 -07001591audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1592 audio_devices_t *pDevices,
1593 uint32_t *pSamplingRate,
1594 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001595 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001596{
1597 status_t status;
1598 RecordThread *thread = NULL;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001599 struct audio_config config;
1600 config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0;
1601 config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0;
1602 config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT;
1603
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001604 uint32_t reqSamplingRate = config.sample_rate;
1605 audio_format_t reqFormat = config.format;
1606 audio_channel_mask_t reqChannels = config.channel_mask;
1607 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001608 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001609
1610 if (pDevices == NULL || *pDevices == 0) {
1611 return 0;
1612 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001613
Mathias Agopian65ab4712010-07-14 17:59:35 -07001614 Mutex::Autolock _l(mLock);
1615
Eric Laurenta4c5a552012-03-29 10:12:40 -07001616 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001617 if (inHwDev == NULL)
1618 return 0;
1619
John Grossmanee578c02012-07-23 17:05:46 -07001620 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001621 audio_io_handle_t id = nextUniqueId();
1622
John Grossmanee578c02012-07-23 17:05:46 -07001623 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001624 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001625 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1626 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001627 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001628 config.sample_rate,
1629 config.format,
1630 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001631 status);
1632
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001633 // If the input could not be opened with the requested parameters and we can handle the
1634 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1635 // 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 -07001636 if (status == BAD_VALUE &&
1637 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1638 (config.sample_rate <= 2 * reqSamplingRate) &&
1639 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001640 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001641 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001642 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001643 }
1644
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001645 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001646
Glenn Kasten46909e72013-02-26 09:20:22 -08001647#ifdef TEE_SINK
Glenn Kastend06785b2012-09-30 12:29:28 -07001648 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1649 // or (re-)create if current Pipe is idle and does not match the new format
1650 sp<NBAIO_Sink> teeSink;
Glenn Kastend06785b2012-09-30 12:29:28 -07001651 enum {
1652 TEE_SINK_NO, // don't copy input
1653 TEE_SINK_NEW, // copy input using a new pipe
1654 TEE_SINK_OLD, // copy input using an existing pipe
1655 } kind;
1656 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1657 popcount(inStream->common.get_channels(&inStream->common)));
Glenn Kastenda6ef132013-01-10 12:31:01 -08001658 if (!mTeeSinkInputEnabled) {
1659 kind = TEE_SINK_NO;
1660 } else if (format == Format_Invalid) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001661 kind = TEE_SINK_NO;
1662 } else if (mRecordTeeSink == 0) {
1663 kind = TEE_SINK_NEW;
1664 } else if (mRecordTeeSink->getStrongCount() != 1) {
1665 kind = TEE_SINK_NO;
1666 } else if (format == mRecordTeeSink->format()) {
1667 kind = TEE_SINK_OLD;
1668 } else {
1669 kind = TEE_SINK_NEW;
1670 }
1671 switch (kind) {
1672 case TEE_SINK_NEW: {
Glenn Kastenda6ef132013-01-10 12:31:01 -08001673 Pipe *pipe = new Pipe(mTeeSinkInputFrames, format);
Glenn Kastend06785b2012-09-30 12:29:28 -07001674 size_t numCounterOffers = 0;
1675 const NBAIO_Format offers[1] = {format};
1676 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1677 ALOG_ASSERT(index == 0);
1678 PipeReader *pipeReader = new PipeReader(*pipe);
1679 numCounterOffers = 0;
1680 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1681 ALOG_ASSERT(index == 0);
1682 mRecordTeeSink = pipe;
1683 mRecordTeeSource = pipeReader;
1684 teeSink = pipe;
1685 }
1686 break;
1687 case TEE_SINK_OLD:
1688 teeSink = mRecordTeeSink;
1689 break;
1690 case TEE_SINK_NO:
1691 default:
1692 break;
1693 }
Glenn Kasten46909e72013-02-26 09:20:22 -08001694#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -08001695
Dima Zavin799a70e2011-04-18 16:57:27 -07001696 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1697
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001698 // Start record thread
1699 // RecorThread require both input and output device indication to forward to audio
1700 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001701 thread = new RecordThread(this,
1702 input,
1703 reqSamplingRate,
1704 reqChannels,
1705 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001706 primaryOutputDevice_l(),
Glenn Kasten46909e72013-02-26 09:20:22 -08001707 *pDevices
1708#ifdef TEE_SINK
1709 , teeSink
1710#endif
1711 );
Mathias Agopian65ab4712010-07-14 17:59:35 -07001712 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001713 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kasten7c027242012-12-26 14:43:16 -08001714 if (pSamplingRate != NULL) {
1715 *pSamplingRate = reqSamplingRate;
1716 }
1717 if (pFormat != NULL) {
1718 *pFormat = config.format;
1719 }
1720 if (pChannelMask != NULL) {
1721 *pChannelMask = reqChannels;
1722 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001723
Mathias Agopian65ab4712010-07-14 17:59:35 -07001724 // notify client processes of the new input creation
1725 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1726 return id;
1727 }
1728
1729 return 0;
1730}
1731
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001732status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001733{
Glenn Kastend96c5722012-04-25 13:44:49 -07001734 return closeInput_nonvirtual(input);
1735}
1736
1737status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1738{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001739 // keep strong reference on the record thread so that
1740 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001741 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001742 {
1743 Mutex::Autolock _l(mLock);
1744 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001745 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001746 return BAD_VALUE;
1747 }
1748
Steve Block3856b092011-10-20 11:56:00 +01001749 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001750 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001751 mRecordThreads.removeItem(input);
1752 }
1753 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001754 // The thread entity (active unit of execution) is no longer running here,
1755 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001756
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001757 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001758 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001759 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001760 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001761 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001762
1763 return NO_ERROR;
1764}
1765
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001766status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001767{
1768 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001769 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001770
1771 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1772 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001773 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001774 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001775
1776 return NO_ERROR;
1777}
1778
1779
1780int AudioFlinger::newAudioSessionId()
1781{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001782 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001783}
1784
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001785void AudioFlinger::acquireAudioSessionId(int audioSession)
1786{
1787 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001788 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001789 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001790 size_t num = mAudioSessionRefs.size();
1791 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001792 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001793 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1794 ref->mCnt++;
1795 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001796 return;
1797 }
1798 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001799 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1800 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001801}
1802
1803void AudioFlinger::releaseAudioSessionId(int audioSession)
1804{
1805 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001806 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001807 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001808 size_t num = mAudioSessionRefs.size();
1809 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001810 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001811 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1812 ref->mCnt--;
1813 ALOGV(" decremented refcount to %d", ref->mCnt);
1814 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001815 mAudioSessionRefs.removeAt(i);
1816 delete ref;
1817 purgeStaleEffects_l();
1818 }
1819 return;
1820 }
1821 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001822 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001823}
1824
1825void AudioFlinger::purgeStaleEffects_l() {
1826
Steve Block3856b092011-10-20 11:56:00 +01001827 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001828
1829 Vector< sp<EffectChain> > chains;
1830
1831 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1832 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1833 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1834 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001835 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1836 chains.push(ec);
1837 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001838 }
1839 }
1840 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1841 sp<RecordThread> t = mRecordThreads.valueAt(i);
1842 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1843 sp<EffectChain> ec = t->mEffectChains[j];
1844 chains.push(ec);
1845 }
1846 }
1847
1848 for (size_t i = 0; i < chains.size(); i++) {
1849 sp<EffectChain> ec = chains[i];
1850 int sessionid = ec->sessionId();
1851 sp<ThreadBase> t = ec->mThread.promote();
1852 if (t == 0) {
1853 continue;
1854 }
1855 size_t numsessionrefs = mAudioSessionRefs.size();
1856 bool found = false;
1857 for (size_t k = 0; k < numsessionrefs; k++) {
1858 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001859 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001860 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001861 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001862 found = true;
1863 break;
1864 }
1865 }
1866 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001867 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001868 // remove all effects from the chain
1869 while (ec->mEffects.size()) {
1870 sp<EffectModule> effect = ec->mEffects[0];
1871 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001872 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001873 if (effect->purgeHandles()) {
1874 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001875 }
1876 AudioSystem::unregisterEffect(effect->id());
1877 }
1878 }
1879 }
1880 return;
1881}
1882
Mathias Agopian65ab4712010-07-14 17:59:35 -07001883// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001884AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001885{
Glenn Kastena1117922012-01-26 10:53:32 -08001886 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001887}
1888
1889// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001890AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001891{
1892 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001893 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001894}
1895
1896// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001897AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001898{
Glenn Kastena1117922012-01-26 10:53:32 -08001899 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001900}
1901
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001902uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001903{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001904 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001905}
1906
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001907AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001908{
1909 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1910 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001911 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07001912 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001913 return thread;
1914 }
1915 }
1916 return NULL;
1917}
1918
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001919audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001920{
1921 PlaybackThread *thread = primaryPlaybackThread_l();
1922
1923 if (thread == NULL) {
1924 return 0;
1925 }
1926
Eric Laurentf1c04f92012-08-28 14:26:53 -07001927 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001928}
1929
Eric Laurenta011e352012-03-29 15:51:43 -07001930sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
1931 int triggerSession,
1932 int listenerSession,
1933 sync_event_callback_t callBack,
1934 void *cookie)
1935{
1936 Mutex::Autolock _l(mLock);
1937
1938 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
1939 status_t playStatus = NAME_NOT_FOUND;
1940 status_t recStatus = NAME_NOT_FOUND;
1941 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1942 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
1943 if (playStatus == NO_ERROR) {
1944 return event;
1945 }
1946 }
1947 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1948 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
1949 if (recStatus == NO_ERROR) {
1950 return event;
1951 }
1952 }
1953 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
1954 mPendingSyncEvents.add(event);
1955 } else {
1956 ALOGV("createSyncEvent() invalid event %d", event->type());
1957 event.clear();
1958 }
1959 return event;
1960}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001961
Mathias Agopian65ab4712010-07-14 17:59:35 -07001962// ----------------------------------------------------------------------------
1963// Effect management
1964// ----------------------------------------------------------------------------
1965
1966
Glenn Kastenf587ba52012-01-26 16:25:10 -08001967status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001968{
1969 Mutex::Autolock _l(mLock);
1970 return EffectQueryNumberEffects(numEffects);
1971}
1972
Glenn Kastenf587ba52012-01-26 16:25:10 -08001973status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001974{
1975 Mutex::Autolock _l(mLock);
1976 return EffectQueryEffect(index, descriptor);
1977}
1978
Glenn Kasten5e92a782012-01-30 07:40:52 -08001979status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08001980 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001981{
1982 Mutex::Autolock _l(mLock);
1983 return EffectGetDescriptor(pUuid, descriptor);
1984}
1985
1986
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001987sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07001988 effect_descriptor_t *pDesc,
1989 const sp<IEffectClient>& effectClient,
1990 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001991 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001992 int sessionId,
1993 status_t *status,
1994 int *id,
1995 int *enabled)
1996{
1997 status_t lStatus = NO_ERROR;
1998 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001999 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002000
Glenn Kasten8d6cc842012-02-03 11:06:53 -08002001 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002002 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002003 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002004
2005 if (pDesc == NULL) {
2006 lStatus = BAD_VALUE;
2007 goto Exit;
2008 }
2009
Eric Laurent84e9a102010-09-23 16:10:16 -07002010 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07002011 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002012 lStatus = PERMISSION_DENIED;
2013 goto Exit;
2014 }
2015
Dima Zavinfce7a472011-04-19 22:30:36 -07002016 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07002017 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08002018 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002019 lStatus = PERMISSION_DENIED;
2020 goto Exit;
2021 }
2022
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002023 if (io == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07002024 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002025 // output must be specified by AudioPolicyManager when using session
Dima Zavinfce7a472011-04-19 22:30:36 -07002026 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent84e9a102010-09-23 16:10:16 -07002027 lStatus = BAD_VALUE;
2028 goto Exit;
Dima Zavinfce7a472011-04-19 22:30:36 -07002029 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent84e9a102010-09-23 16:10:16 -07002030 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002031 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent84e9a102010-09-23 16:10:16 -07002032 // and we will exit safely
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002033 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent84e9a102010-09-23 16:10:16 -07002034 }
2035 }
2036
Mathias Agopian65ab4712010-07-14 17:59:35 -07002037 {
2038 Mutex::Autolock _l(mLock);
2039
Mathias Agopian65ab4712010-07-14 17:59:35 -07002040
2041 if (!EffectIsNullUuid(&pDesc->uuid)) {
2042 // if uuid is specified, request effect descriptor
2043 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
2044 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002045 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002046 goto Exit;
2047 }
2048 } else {
2049 // if uuid is not specified, look for an available implementation
2050 // of the required type in effect factory
2051 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002052 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002053 lStatus = BAD_VALUE;
2054 goto Exit;
2055 }
2056 uint32_t numEffects = 0;
2057 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002058 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07002059 bool found = false;
2060
2061 lStatus = EffectQueryNumberEffects(&numEffects);
2062 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002063 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002064 goto Exit;
2065 }
2066 for (uint32_t i = 0; i < numEffects; i++) {
2067 lStatus = EffectQueryEffect(i, &desc);
2068 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002069 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002070 continue;
2071 }
2072 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
2073 // If matching type found save effect descriptor. If the session is
2074 // 0 and the effect is not auxiliary, continue enumeration in case
2075 // an auxiliary version of this effect type is available
2076 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08002077 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07002078 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07002079 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2080 break;
2081 }
2082 }
2083 }
2084 if (!found) {
2085 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00002086 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002087 goto Exit;
2088 }
2089 // For same effect type, chose auxiliary version over insert version if
2090 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07002091 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002092 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08002093 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002094 }
2095 }
2096
2097 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07002098 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07002099 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2100 lStatus = INVALID_OPERATION;
2101 goto Exit;
2102 }
2103
Eric Laurent59255e42011-07-27 19:49:51 -07002104 // check recording permission for visualizer
2105 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
2106 !recordingAllowed()) {
2107 lStatus = PERMISSION_DENIED;
2108 goto Exit;
2109 }
2110
Mathias Agopian65ab4712010-07-14 17:59:35 -07002111 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002112 *pDesc = desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002113
2114 // If output is not specified try to find a matching audio session ID in one of the
2115 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002116 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2117 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002118 // Note: io is never 0 when creating an effect on an input
2119 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002120 // look for the thread where the specified audio session is present
Eric Laurent84e9a102010-09-23 16:10:16 -07002121 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2122 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002123 io = mPlaybackThreads.keyAt(i);
Eric Laurent84e9a102010-09-23 16:10:16 -07002124 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002125 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002126 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002127 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002128 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2129 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2130 io = mRecordThreads.keyAt(i);
2131 break;
2132 }
2133 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002134 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002135 // If no output thread contains the requested session ID, default to
2136 // first output. The effect chain will be moved to the correct output
2137 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002138 if (io == 0 && mPlaybackThreads.size()) {
2139 io = mPlaybackThreads.keyAt(0);
2140 }
Steve Block3856b092011-10-20 11:56:00 +01002141 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002142 }
2143 ThreadBase *thread = checkRecordThread_l(io);
2144 if (thread == NULL) {
2145 thread = checkPlaybackThread_l(io);
2146 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002147 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002148 lStatus = BAD_VALUE;
2149 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002150 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002151 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002152
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002153 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002154
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002155 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002156 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2157 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002158 if (handle != 0 && id != NULL) {
2159 *id = handle->id();
2160 }
2161 }
2162
2163Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002164 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002165 *status = lStatus;
2166 }
2167 return handle;
2168}
2169
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002170status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2171 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002172{
Steve Block3856b092011-10-20 11:56:00 +01002173 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002174 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002175 Mutex::Autolock _l(mLock);
2176 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002177 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002178 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002179 }
Eric Laurentde070132010-07-13 04:45:46 -07002180 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2181 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002182 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002183 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002184 }
Eric Laurentde070132010-07-13 04:45:46 -07002185 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2186 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002187 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002188 return BAD_VALUE;
2189 }
2190
2191 Mutex::Autolock _dl(dstThread->mLock);
2192 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent59255e42011-07-27 19:49:51 -07002193 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurentde070132010-07-13 04:45:46 -07002194
Mathias Agopian65ab4712010-07-14 17:59:35 -07002195 return NO_ERROR;
2196}
2197
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002198// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002199status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002200 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002201 AudioFlinger::PlaybackThread *dstThread,
2202 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002203{
Steve Block3856b092011-10-20 11:56:00 +01002204 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002205 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002206
Eric Laurent59255e42011-07-27 19:49:51 -07002207 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002208 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002209 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002210 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002211 return INVALID_OPERATION;
2212 }
2213
Eric Laurent39e94f82010-07-28 01:32:47 -07002214 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002215 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002216 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002217 // removed.
2218 srcThread->removeEffectChain_l(chain);
2219
2220 // transfer all effects one by one so that new effect chain is created on new thread with
2221 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002222 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent39e94f82010-07-28 01:32:47 -07002223 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002224 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002225 sp<EffectModule> effect = chain->getEffectFromId_l(0);
2226 while (effect != 0) {
2227 srcThread->removeEffect_l(effect);
2228 dstThread->addEffect_l(effect);
Eric Laurentec35a142011-10-05 17:42:25 -07002229 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2230 if (effect->state() == EffectModule::ACTIVE ||
2231 effect->state() == EffectModule::STOPPING) {
2232 effect->start();
2233 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002234 // if the move request is not received from audio policy manager, the effect must be
2235 // re-registered with the new strategy and output
2236 if (dstChain == 0) {
2237 dstChain = effect->chain().promote();
2238 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002239 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent39e94f82010-07-28 01:32:47 -07002240 srcThread->addEffect_l(effect);
2241 return NO_INIT;
2242 }
2243 strategy = dstChain->strategy();
2244 }
2245 if (reRegister) {
2246 AudioSystem::unregisterEffect(effect->id());
2247 AudioSystem::registerEffect(&effect->desc(),
2248 dstOutput,
2249 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002250 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002251 effect->id());
2252 }
Eric Laurentde070132010-07-13 04:45:46 -07002253 effect = chain->getEffectFromId_l(0);
2254 }
2255
2256 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002257}
2258
Glenn Kastenda6ef132013-01-10 12:31:01 -08002259struct Entry {
2260#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav
2261 char mName[MAX_NAME];
2262};
2263
2264int comparEntry(const void *p1, const void *p2)
2265{
2266 return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName);
2267}
2268
Glenn Kasten46909e72013-02-26 09:20:22 -08002269#ifdef TEE_SINK
Eric Laurent81784c32012-11-19 14:55:58 -08002270void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002271{
Eric Laurent81784c32012-11-19 14:55:58 -08002272 NBAIO_Source *teeSource = source.get();
2273 if (teeSource != NULL) {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002274 // .wav rotation
2275 // There is a benign race condition if 2 threads call this simultaneously.
2276 // They would both traverse the directory, but the result would simply be
2277 // failures at unlink() which are ignored. It's also unlikely since
2278 // normally dumpsys is only done by bugreport or from the command line.
2279 char teePath[32+256];
2280 strcpy(teePath, "/data/misc/media");
2281 size_t teePathLen = strlen(teePath);
2282 DIR *dir = opendir(teePath);
2283 teePath[teePathLen++] = '/';
2284 if (dir != NULL) {
2285#define MAX_SORT 20 // number of entries to sort
2286#define MAX_KEEP 10 // number of entries to keep
2287 struct Entry entries[MAX_SORT];
2288 size_t entryCount = 0;
2289 while (entryCount < MAX_SORT) {
2290 struct dirent de;
2291 struct dirent *result = NULL;
2292 int rc = readdir_r(dir, &de, &result);
2293 if (rc != 0) {
2294 ALOGW("readdir_r failed %d", rc);
2295 break;
2296 }
2297 if (result == NULL) {
2298 break;
2299 }
2300 if (result != &de) {
2301 ALOGW("readdir_r returned unexpected result %p != %p", result, &de);
2302 break;
2303 }
2304 // ignore non .wav file entries
2305 size_t nameLen = strlen(de.d_name);
2306 if (nameLen <= 4 || nameLen >= MAX_NAME ||
2307 strcmp(&de.d_name[nameLen - 4], ".wav")) {
2308 continue;
2309 }
2310 strcpy(entries[entryCount++].mName, de.d_name);
2311 }
2312 (void) closedir(dir);
2313 if (entryCount > MAX_KEEP) {
2314 qsort(entries, entryCount, sizeof(Entry), comparEntry);
2315 for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) {
2316 strcpy(&teePath[teePathLen], entries[i].mName);
2317 (void) unlink(teePath);
2318 }
2319 }
2320 } else {
2321 if (fd >= 0) {
2322 fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno));
2323 }
2324 }
Eric Laurent81784c32012-11-19 14:55:58 -08002325 char teeTime[16];
2326 struct timeval tv;
2327 gettimeofday(&tv, NULL);
2328 struct tm tm;
2329 localtime_r(&tv.tv_sec, &tm);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002330 strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm);
2331 snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id);
2332 // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd
2333 int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR);
Eric Laurent81784c32012-11-19 14:55:58 -08002334 if (teeFd >= 0) {
2335 char wavHeader[44];
2336 memcpy(wavHeader,
2337 "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",
2338 sizeof(wavHeader));
2339 NBAIO_Format format = teeSource->format();
2340 unsigned channelCount = Format_channelCount(format);
2341 ALOG_ASSERT(channelCount <= FCC_2);
2342 uint32_t sampleRate = Format_sampleRate(format);
2343 wavHeader[22] = channelCount; // number of channels
2344 wavHeader[24] = sampleRate; // sample rate
2345 wavHeader[25] = sampleRate >> 8;
2346 wavHeader[32] = channelCount * 2; // block alignment
2347 write(teeFd, wavHeader, sizeof(wavHeader));
2348 size_t total = 0;
2349 bool firstRead = true;
2350 for (;;) {
2351#define TEE_SINK_READ 1024
2352 short buffer[TEE_SINK_READ * FCC_2];
2353 size_t count = TEE_SINK_READ;
2354 ssize_t actual = teeSource->read(buffer, count,
2355 AudioBufferProvider::kInvalidPTS);
2356 bool wasFirstRead = firstRead;
2357 firstRead = false;
2358 if (actual <= 0) {
2359 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2360 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002361 }
Eric Laurent81784c32012-11-19 14:55:58 -08002362 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002363 }
Eric Laurent81784c32012-11-19 14:55:58 -08002364 ALOG_ASSERT(actual <= (ssize_t)count);
2365 write(teeFd, buffer, actual * channelCount * sizeof(short));
2366 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002367 }
Eric Laurent81784c32012-11-19 14:55:58 -08002368 lseek(teeFd, (off_t) 4, SEEK_SET);
2369 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2370 write(teeFd, &temp, sizeof(temp));
2371 lseek(teeFd, (off_t) 40, SEEK_SET);
2372 temp = total * channelCount * sizeof(short);
2373 write(teeFd, &temp, sizeof(temp));
2374 close(teeFd);
Glenn Kastenda6ef132013-01-10 12:31:01 -08002375 if (fd >= 0) {
2376 fdprintf(fd, "tee copied to %s\n", teePath);
2377 }
Eric Laurent59255e42011-07-27 19:49:51 -07002378 } else {
Glenn Kastenda6ef132013-01-10 12:31:01 -08002379 if (fd >= 0) {
2380 fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno));
2381 }
Eric Laurent59255e42011-07-27 19:49:51 -07002382 }
2383 }
2384}
Glenn Kasten46909e72013-02-26 09:20:22 -08002385#endif
Eric Laurent59255e42011-07-27 19:49:51 -07002386
Mathias Agopian65ab4712010-07-14 17:59:35 -07002387// ----------------------------------------------------------------------------
2388
2389status_t AudioFlinger::onTransact(
2390 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2391{
2392 return BnAudioFlinger::onTransact(code, data, reply, flags);
2393}
2394
Mathias Agopian65ab4712010-07-14 17:59:35 -07002395}; // namespace android