blob: 89e9b525022e584b2d580cf31693554e9fead928 [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
22#include <math.h>
23#include <signal.h>
24#include <sys/time.h>
25#include <sys/resource.h>
26
Gloria Wang9ee159b2011-02-24 14:51:45 -080027#include <binder/IPCThreadState.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070028#include <binder/IServiceManager.h>
29#include <utils/Log.h>
Glenn Kastend8e6fd32012-05-07 11:07:57 -070030#include <utils/Trace.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070031#include <binder/Parcel.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070032#include <utils/String16.h>
33#include <utils/threads.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070034#include <utils/Atomic.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035
Dima Zavinfce7a472011-04-19 22:30:36 -070036#include <cutils/bitops.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070037#include <cutils/properties.h>
Glenn Kastenf6b16782011-12-15 09:51:17 -080038#include <cutils/compiler.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070039
Eric Laurent81784c32012-11-19 14:55:58 -080040//#include <private/media/AudioTrackShared.h>
41//#include <private/media/AudioEffectShared.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070042
Dima Zavin64760242011-05-11 14:15:23 -070043#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070044#include <hardware/audio.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070045
46#include "AudioMixer.h"
47#include "AudioFlinger.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080048#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070049
Mathias Agopian65ab4712010-07-14 17:59:35 -070050#include <media/EffectsFactoryApi.h>
Eric Laurent6d8b6942011-06-24 07:01:31 -070051#include <audio_effects/effect_visualizer.h>
Eric Laurent59bd0da2011-08-01 09:52:20 -070052#include <audio_effects/effect_ns.h>
53#include <audio_effects/effect_aec.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070054
Glenn Kasten3b21c502011-12-15 09:52:39 -080055#include <audio_utils/primitives.h>
56
Eric Laurentfeb0db62011-07-22 09:04:31 -070057#include <powermanager/PowerManager.h>
Glenn Kasten190a46f2012-03-06 11:27:10 -080058
John Grossman4ff14ba2012-02-08 16:37:41 -080059#include <common_time/cc_helper.h>
Eric Laurent81784c32012-11-19 14:55:58 -080060//#include <common_time/local_clock.h>
Glenn Kasten58912562012-04-03 10:45:00 -070061
Mathias Agopian65ab4712010-07-14 17:59:35 -070062// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -070063
John Grossman1c345192012-03-27 14:00:17 -070064// Note: the following macro is used for extremely verbose logging message. In
65// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
66// 0; but one side effect of this is to turn all LOGV's as well. Some messages
67// are so verbose that we want to suppress them even when we have ALOG_ASSERT
68// turned on. Do not uncomment the #def below unless you really know what you
69// are doing and want to see all of the extremely verbose messages.
70//#define VERY_VERY_VERBOSE_LOGGING
71#ifdef VERY_VERY_VERBOSE_LOGGING
72#define ALOGVV ALOGV
73#else
74#define ALOGVV(a...) do { } while(0)
75#endif
Eric Laurentde070132010-07-13 04:45:46 -070076
Mathias Agopian65ab4712010-07-14 17:59:35 -070077namespace android {
78
Glenn Kastenec1d6b52011-12-12 09:04:45 -080079static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
80static const char kHardwareLockedString[] = "Hardware lock is taken\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070081
Glenn Kasten58912562012-04-03 10:45:00 -070082
John Grossman4ff14ba2012-02-08 16:37:41 -080083nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
Eric Laurent7cafbb32011-11-22 18:50:29 -080084
Eric Laurent81784c32012-11-19 14:55:58 -080085uint32_t AudioFlinger::mScreenState;
Glenn Kasten3ed29202012-08-07 15:24:44 -070086
Mathias Agopian65ab4712010-07-14 17:59:35 -070087// ----------------------------------------------------------------------------
88
Eric Laurentf7ffb8b2012-04-14 09:06:57 -070089static int load_audio_interface(const char *if_name, audio_hw_device_t **dev)
Dima Zavin799a70e2011-04-18 16:57:27 -070090{
Eric Laurentf7ffb8b2012-04-14 09:06:57 -070091 const hw_module_t *mod;
Dima Zavin799a70e2011-04-18 16:57:27 -070092 int rc;
93
Eric Laurentf7ffb8b2012-04-14 09:06:57 -070094 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
95 ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__,
96 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
97 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -070098 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -070099 }
100 rc = audio_hw_device_open(mod, dev);
101 ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__,
102 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
103 if (rc) {
Dima Zavin799a70e2011-04-18 16:57:27 -0700104 goto out;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700105 }
106 if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) {
107 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
108 rc = BAD_VALUE;
109 goto out;
110 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700111 return 0;
112
113out:
Dima Zavin799a70e2011-04-18 16:57:27 -0700114 *dev = NULL;
115 return rc;
116}
117
Mathias Agopian65ab4712010-07-14 17:59:35 -0700118// ----------------------------------------------------------------------------
119
120AudioFlinger::AudioFlinger()
121 : BnAudioFlinger(),
John Grossman4ff14ba2012-02-08 16:37:41 -0800122 mPrimaryHardwareDev(NULL),
Glenn Kasten7d6c35b2012-07-02 12:45:10 -0700123 mHardwareStatus(AUDIO_HW_IDLE),
John Grossman4ff14ba2012-02-08 16:37:41 -0800124 mMasterVolume(1.0f),
John Grossman4ff14ba2012-02-08 16:37:41 -0800125 mMasterMute(false),
126 mNextUniqueId(1),
127 mMode(AUDIO_MODE_INVALID),
128 mBtNrecIsOff(false)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700129{
Dima Zavin5a61d2f2011-04-19 19:04:32 -0700130}
131
132void AudioFlinger::onFirstRef()
133{
Dima Zavin799a70e2011-04-18 16:57:27 -0700134 int rc = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -0700135
Eric Laurent93575202011-01-18 18:39:02 -0800136 Mutex::Autolock _l(mLock);
137
Dima Zavin799a70e2011-04-18 16:57:27 -0700138 /* TODO: move all this work into an Init() function */
John Grossman4ff14ba2012-02-08 16:37:41 -0800139 char val_str[PROPERTY_VALUE_MAX] = { 0 };
140 if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
141 uint32_t int_val;
142 if (1 == sscanf(val_str, "%u", &int_val)) {
143 mStandbyTimeInNsecs = milliseconds(int_val);
144 ALOGI("Using %u mSec as standby time.", int_val);
145 } else {
146 mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
147 ALOGI("Using default %u mSec as standby time.",
148 (uint32_t)(mStandbyTimeInNsecs / 1000000));
149 }
150 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700151
Eric Laurenta4c5a552012-03-29 10:12:40 -0700152 mMode = AUDIO_MODE_NORMAL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700153}
154
155AudioFlinger::~AudioFlinger()
156{
157 while (!mRecordThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700158 // closeInput_nonvirtual() will remove specified entry from mRecordThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700159 closeInput_nonvirtual(mRecordThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700160 }
161 while (!mPlaybackThreads.isEmpty()) {
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700162 // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads
Glenn Kastend96c5722012-04-25 13:44:49 -0700163 closeOutput_nonvirtual(mPlaybackThreads.keyAt(0));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700164 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700165
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800166 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
167 // no mHardwareLock needed, as there are no other references to this
Eric Laurenta4c5a552012-03-29 10:12:40 -0700168 audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice());
169 delete mAudioHwDevs.valueAt(i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700170 }
171}
172
Eric Laurenta4c5a552012-03-29 10:12:40 -0700173static const char * const audio_interfaces[] = {
174 AUDIO_HARDWARE_MODULE_ID_PRIMARY,
175 AUDIO_HARDWARE_MODULE_ID_A2DP,
176 AUDIO_HARDWARE_MODULE_ID_USB,
177};
178#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
179
John Grossmanee578c02012-07-23 17:05:46 -0700180AudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l(
181 audio_module_handle_t module,
182 audio_devices_t devices)
Dima Zavin799a70e2011-04-18 16:57:27 -0700183{
Eric Laurenta4c5a552012-03-29 10:12:40 -0700184 // if module is 0, the request comes from an old policy manager and we should load
185 // well known modules
186 if (module == 0) {
187 ALOGW("findSuitableHwDev_l() loading well know audio hw modules");
188 for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
189 loadHwModule_l(audio_interfaces[i]);
190 }
Eric Laurentf1c04f92012-08-28 14:26:53 -0700191 // then try to find a module supporting the requested device.
192 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
193 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i);
194 audio_hw_device_t *dev = audioHwDevice->hwDevice();
195 if ((dev->get_supported_devices != NULL) &&
196 (dev->get_supported_devices(dev) & devices) == devices)
197 return audioHwDevice;
198 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700199 } else {
200 // check a match for the requested module handle
John Grossmanee578c02012-07-23 17:05:46 -0700201 AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module);
202 if (audioHwDevice != NULL) {
203 return audioHwDevice;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700204 }
205 }
Eric Laurenta4c5a552012-03-29 10:12:40 -0700206
Dima Zavin799a70e2011-04-18 16:57:27 -0700207 return NULL;
208}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700209
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700210void AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700211{
212 const size_t SIZE = 256;
213 char buffer[SIZE];
214 String8 result;
215
216 result.append("Clients:\n");
217 for (size_t i = 0; i < mClients.size(); ++i) {
Glenn Kasten77c11192012-01-25 14:27:41 -0800218 sp<Client> client = mClients.valueAt(i).promote();
219 if (client != 0) {
220 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
221 result.append(buffer);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700222 }
223 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700224
225 result.append("Global session refs:\n");
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800226 result.append(" session pid count\n");
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700227 for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
228 AudioSessionRef *r = mAudioSessionRefs[i];
Glenn Kasten012ca6b2012-03-06 11:22:01 -0800229 snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700230 result.append(buffer);
231 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700232 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700233}
234
235
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700236void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700237{
238 const size_t SIZE = 256;
239 char buffer[SIZE];
240 String8 result;
Glenn Kastena4454b42012-01-04 11:02:33 -0800241 hardware_call_state hardwareStatus = mHardwareStatus;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242
John Grossman4ff14ba2012-02-08 16:37:41 -0800243 snprintf(buffer, SIZE, "Hardware status: %d\n"
244 "Standby Time mSec: %u\n",
245 hardwareStatus,
246 (uint32_t)(mStandbyTimeInNsecs / 1000000));
Mathias Agopian65ab4712010-07-14 17:59:35 -0700247 result.append(buffer);
248 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700249}
250
Glenn Kastenbe5f05e2012-07-18 15:24:02 -0700251void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700252{
253 const size_t SIZE = 256;
254 char buffer[SIZE];
255 String8 result;
256 snprintf(buffer, SIZE, "Permission Denial: "
257 "can't dump AudioFlinger from pid=%d, uid=%d\n",
258 IPCThreadState::self()->getCallingPid(),
259 IPCThreadState::self()->getCallingUid());
260 result.append(buffer);
261 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700262}
263
Eric Laurent81784c32012-11-19 14:55:58 -0800264bool AudioFlinger::dumpTryLock(Mutex& mutex)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700265{
266 bool locked = false;
267 for (int i = 0; i < kDumpLockRetries; ++i) {
268 if (mutex.tryLock() == NO_ERROR) {
269 locked = true;
270 break;
271 }
Glenn Kasten7dede872011-12-13 11:04:14 -0800272 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700273 }
274 return locked;
275}
276
277status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
278{
Glenn Kasten44deb052012-02-05 18:09:08 -0800279 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700280 dumpPermissionDenial(fd, args);
281 } else {
282 // get state of hardware lock
Eric Laurent81784c32012-11-19 14:55:58 -0800283 bool hardwareLocked = dumpTryLock(mHardwareLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700284 if (!hardwareLocked) {
285 String8 result(kHardwareLockedString);
286 write(fd, result.string(), result.size());
287 } else {
288 mHardwareLock.unlock();
289 }
290
Eric Laurent81784c32012-11-19 14:55:58 -0800291 bool locked = dumpTryLock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700292
293 // failed to lock - AudioFlinger is probably deadlocked
294 if (!locked) {
295 String8 result(kDeadlockedString);
296 write(fd, result.string(), result.size());
297 }
298
299 dumpClients(fd, args);
300 dumpInternals(fd, args);
301
302 // dump playback threads
303 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
304 mPlaybackThreads.valueAt(i)->dump(fd, args);
305 }
306
307 // dump record threads
308 for (size_t i = 0; i < mRecordThreads.size(); i++) {
309 mRecordThreads.valueAt(i)->dump(fd, args);
310 }
311
Dima Zavin799a70e2011-04-18 16:57:27 -0700312 // dump all hardware devs
313 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700314 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Dima Zavin799a70e2011-04-18 16:57:27 -0700315 dev->dump(dev, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316 }
Glenn Kastend06785b2012-09-30 12:29:28 -0700317
318 // dump the serially shared record tee sink
319 if (mRecordTeeSource != 0) {
320 dumpTee(fd, mRecordTeeSource);
321 }
322
Glenn Kastend65d73c2012-06-22 17:21:07 -0700323 if (locked) {
324 mLock.unlock();
325 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700326 }
327 return NO_ERROR;
328}
329
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800330sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
331{
332 // If pid is already in the mClients wp<> map, then use that entry
333 // (for which promote() is always != 0), otherwise create a new entry and Client.
334 sp<Client> client = mClients.valueFor(pid).promote();
335 if (client == 0) {
336 client = new Client(this, pid);
337 mClients.add(pid, client);
338 }
339
340 return client;
341}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700342
343// IAudioFlinger interface
344
345
346sp<IAudioTrack> AudioFlinger::createTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800347 audio_stream_type_t streamType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700348 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800349 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700350 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800351 size_t frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800352 IAudioFlinger::track_flags_t *flags,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700353 const sp<IMemory>& sharedBuffer,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800354 audio_io_handle_t output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800355 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700356 int *sessionId,
357 status_t *status)
358{
359 sp<PlaybackThread::Track> track;
360 sp<TrackHandle> trackHandle;
361 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362 status_t lStatus;
363 int lSessionId;
364
Glenn Kasten263709e2012-01-06 08:40:01 -0800365 // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
366 // but if someone uses binder directly they could bypass that and cause us to crash
367 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000368 ALOGE("createTrack() invalid stream type %d", streamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700369 lStatus = BAD_VALUE;
370 goto Exit;
371 }
372
Glenn Kasten60a83922012-06-21 12:56:37 -0700373 // client is responsible for conversion of 8-bit PCM to 16-bit PCM,
374 // and we don't yet support 8.24 or 32-bit PCM
375 if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) {
376 ALOGE("createTrack() invalid format %d", format);
377 lStatus = BAD_VALUE;
378 goto Exit;
379 }
380
Mathias Agopian65ab4712010-07-14 17:59:35 -0700381 {
382 Mutex::Autolock _l(mLock);
383 PlaybackThread *thread = checkPlaybackThread_l(output);
Eric Laurent39e94f82010-07-28 01:32:47 -0700384 PlaybackThread *effectThread = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000386 ALOGE("unknown output thread");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700387 lStatus = BAD_VALUE;
388 goto Exit;
389 }
390
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800391 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -0800392 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393
Steve Block3856b092011-10-20 11:56:00 +0100394 ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700395 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurentf436fdc2012-05-24 11:07:14 -0700396 // check if an effect chain with the same session ID is present on another
397 // output thread and move it here.
Eric Laurentde070132010-07-13 04:45:46 -0700398 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700399 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
400 if (mPlaybackThreads.keyAt(i) != output) {
Eric Laurent39e94f82010-07-28 01:32:47 -0700401 uint32_t sessions = t->hasAudioSession(*sessionId);
Eric Laurent39e94f82010-07-28 01:32:47 -0700402 if (sessions & PlaybackThread::EFFECT_SESSION) {
403 effectThread = t.get();
Eric Laurentf436fdc2012-05-24 11:07:14 -0700404 break;
Eric Laurent39e94f82010-07-28 01:32:47 -0700405 }
Eric Laurentde070132010-07-13 04:45:46 -0700406 }
407 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700408 lSessionId = *sessionId;
409 } else {
Eric Laurentde070132010-07-13 04:45:46 -0700410 // if no audio session id is provided, create one here
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700411 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700412 if (sessionId != NULL) {
413 *sessionId = lSessionId;
414 }
415 }
Steve Block3856b092011-10-20 11:56:00 +0100416 ALOGV("createTrack() lSessionId: %d", lSessionId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700417
418 track = thread->createTrack_l(client, streamType, sampleRate, format,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800419 channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus);
Eric Laurent39e94f82010-07-28 01:32:47 -0700420
421 // move effect chain to this output thread if an effect on same session was waiting
422 // for a track to be created
423 if (lStatus == NO_ERROR && effectThread != NULL) {
424 Mutex::Autolock _dl(thread->mLock);
425 Mutex::Autolock _sl(effectThread->mLock);
426 moveEffectChain_l(lSessionId, effectThread, thread, true);
427 }
Eric Laurenta011e352012-03-29 15:51:43 -0700428
429 // Look for sync events awaiting for a session to be used.
430 for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) {
431 if (mPendingSyncEvents[i]->triggerSession() == lSessionId) {
432 if (thread->isValidSyncEvent(mPendingSyncEvents[i])) {
Eric Laurent29864602012-05-08 18:57:51 -0700433 if (lStatus == NO_ERROR) {
Glenn Kastend23eedc2012-08-02 13:35:47 -0700434 (void) track->setSyncEvent(mPendingSyncEvents[i]);
Eric Laurent29864602012-05-08 18:57:51 -0700435 } else {
436 mPendingSyncEvents[i]->cancel();
437 }
Eric Laurenta011e352012-03-29 15:51:43 -0700438 mPendingSyncEvents.removeAt(i);
439 i--;
440 }
441 }
442 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700443 }
444 if (lStatus == NO_ERROR) {
445 trackHandle = new TrackHandle(track);
446 } else {
447 // remove local strong reference to Client before deleting the Track so that the Client
448 // destructor is called by the TrackBase destructor with mLock held
449 client.clear();
450 track.clear();
451 }
452
453Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700454 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700455 *status = lStatus;
456 }
457 return trackHandle;
458}
459
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800460uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700461{
462 Mutex::Autolock _l(mLock);
463 PlaybackThread *thread = checkPlaybackThread_l(output);
464 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000465 ALOGW("sampleRate() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700466 return 0;
467 }
468 return thread->sampleRate();
469}
470
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800471int AudioFlinger::channelCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700472{
473 Mutex::Autolock _l(mLock);
474 PlaybackThread *thread = checkPlaybackThread_l(output);
475 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000476 ALOGW("channelCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700477 return 0;
478 }
479 return thread->channelCount();
480}
481
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800482audio_format_t AudioFlinger::format(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700483{
484 Mutex::Autolock _l(mLock);
485 PlaybackThread *thread = checkPlaybackThread_l(output);
486 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000487 ALOGW("format() unknown thread %d", output);
Glenn Kasten58f30212012-01-12 12:27:51 -0800488 return AUDIO_FORMAT_INVALID;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700489 }
490 return thread->format();
491}
492
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800493size_t AudioFlinger::frameCount(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700494{
495 Mutex::Autolock _l(mLock);
496 PlaybackThread *thread = checkPlaybackThread_l(output);
497 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000498 ALOGW("frameCount() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700499 return 0;
500 }
Glenn Kasten58912562012-04-03 10:45:00 -0700501 // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers;
502 // should examine all callers and fix them to handle smaller counts
Mathias Agopian65ab4712010-07-14 17:59:35 -0700503 return thread->frameCount();
504}
505
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800506uint32_t AudioFlinger::latency(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700507{
508 Mutex::Autolock _l(mLock);
509 PlaybackThread *thread = checkPlaybackThread_l(output);
510 if (thread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000511 ALOGW("latency() unknown thread %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700512 return 0;
513 }
514 return thread->latency();
515}
516
517status_t AudioFlinger::setMasterVolume(float value)
518{
Eric Laurenta1884f92011-08-23 08:25:03 -0700519 status_t ret = initCheck();
520 if (ret != NO_ERROR) {
521 return ret;
522 }
523
Mathias Agopian65ab4712010-07-14 17:59:35 -0700524 // check calling permissions
525 if (!settingsAllowed()) {
526 return PERMISSION_DENIED;
527 }
528
Eric Laurenta4c5a552012-03-29 10:12:40 -0700529 Mutex::Autolock _l(mLock);
John Grossmanee578c02012-07-23 17:05:46 -0700530 mMasterVolume = value;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700531
John Grossmanee578c02012-07-23 17:05:46 -0700532 // Set master volume in the HALs which support it.
533 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
534 AutoMutex lock(mHardwareLock);
535 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossman4ff14ba2012-02-08 16:37:41 -0800536
John Grossmanee578c02012-07-23 17:05:46 -0700537 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
538 if (dev->canSetMasterVolume()) {
539 dev->hwDevice()->set_master_volume(dev->hwDevice(), value);
Eric Laurent93575202011-01-18 18:39:02 -0800540 }
John Grossmanee578c02012-07-23 17:05:46 -0700541 mHardwareStatus = AUDIO_HW_IDLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700542 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543
John Grossmanee578c02012-07-23 17:05:46 -0700544 // Now set the master volume in each playback thread. Playback threads
545 // assigned to HALs which do not have master volume support will apply
546 // master volume during the mix operation. Threads with HALs which do
547 // support master volume will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800548 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700549 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550
551 return NO_ERROR;
552}
553
Glenn Kastenf78aee72012-01-04 11:00:47 -0800554status_t AudioFlinger::setMode(audio_mode_t mode)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700555{
Eric Laurenta1884f92011-08-23 08:25:03 -0700556 status_t ret = initCheck();
557 if (ret != NO_ERROR) {
558 return ret;
559 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700560
561 // check calling permissions
562 if (!settingsAllowed()) {
563 return PERMISSION_DENIED;
564 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800565 if (uint32_t(mode) >= AUDIO_MODE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000566 ALOGW("Illegal value: setMode(%d)", mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700567 return BAD_VALUE;
568 }
569
570 { // scope for the lock
571 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700572 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700573 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -0700574 ret = dev->set_mode(dev, mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700575 mHardwareStatus = AUDIO_HW_IDLE;
576 }
577
578 if (NO_ERROR == ret) {
579 Mutex::Autolock _l(mLock);
580 mMode = mode;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800581 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700582 mPlaybackThreads.valueAt(i)->setMode(mode);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700583 }
584
585 return ret;
586}
587
588status_t AudioFlinger::setMicMute(bool state)
589{
Eric Laurenta1884f92011-08-23 08:25:03 -0700590 status_t ret = initCheck();
591 if (ret != NO_ERROR) {
592 return ret;
593 }
594
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 // check calling permissions
596 if (!settingsAllowed()) {
597 return PERMISSION_DENIED;
598 }
599
600 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700601 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700602 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700603 ret = dev->set_mic_mute(dev, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700604 mHardwareStatus = AUDIO_HW_IDLE;
605 return ret;
606}
607
608bool AudioFlinger::getMicMute() const
609{
Eric Laurenta1884f92011-08-23 08:25:03 -0700610 status_t ret = initCheck();
611 if (ret != NO_ERROR) {
612 return false;
613 }
614
Dima Zavinfce7a472011-04-19 22:30:36 -0700615 bool state = AUDIO_MODE_INVALID;
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800616 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700617 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700618 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
John Grossmanee578c02012-07-23 17:05:46 -0700619 dev->get_mic_mute(dev, &state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700620 mHardwareStatus = AUDIO_HW_IDLE;
621 return state;
622}
623
624status_t AudioFlinger::setMasterMute(bool muted)
625{
John Grossmand8f178d2012-07-20 14:51:35 -0700626 status_t ret = initCheck();
627 if (ret != NO_ERROR) {
628 return ret;
629 }
630
Mathias Agopian65ab4712010-07-14 17:59:35 -0700631 // check calling permissions
632 if (!settingsAllowed()) {
633 return PERMISSION_DENIED;
634 }
635
John Grossmanee578c02012-07-23 17:05:46 -0700636 Mutex::Autolock _l(mLock);
637 mMasterMute = muted;
John Grossmand8f178d2012-07-20 14:51:35 -0700638
John Grossmanee578c02012-07-23 17:05:46 -0700639 // Set master mute in the HALs which support it.
640 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
641 AutoMutex lock(mHardwareLock);
642 AudioHwDevice *dev = mAudioHwDevs.valueAt(i);
John Grossmand8f178d2012-07-20 14:51:35 -0700643
John Grossmanee578c02012-07-23 17:05:46 -0700644 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
645 if (dev->canSetMasterMute()) {
646 dev->hwDevice()->set_master_mute(dev->hwDevice(), muted);
John Grossmand8f178d2012-07-20 14:51:35 -0700647 }
John Grossmanee578c02012-07-23 17:05:46 -0700648 mHardwareStatus = AUDIO_HW_IDLE;
John Grossmand8f178d2012-07-20 14:51:35 -0700649 }
650
John Grossmanee578c02012-07-23 17:05:46 -0700651 // Now set the master mute in each playback thread. Playback threads
652 // assigned to HALs which do not have master mute support will apply master
653 // mute during the mix operation. Threads with HALs which do support master
654 // mute will simply ignore the setting.
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800655 for (size_t i = 0; i < mPlaybackThreads.size(); i++)
John Grossmanee578c02012-07-23 17:05:46 -0700656 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657
658 return NO_ERROR;
659}
660
661float AudioFlinger::masterVolume() const
662{
Glenn Kasten98067102011-12-13 11:47:54 -0800663 Mutex::Autolock _l(mLock);
664 return masterVolume_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700665}
666
667bool AudioFlinger::masterMute() const
668{
Glenn Kasten98067102011-12-13 11:47:54 -0800669 Mutex::Autolock _l(mLock);
670 return masterMute_l();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700671}
672
John Grossman4ff14ba2012-02-08 16:37:41 -0800673float AudioFlinger::masterVolume_l() const
674{
John Grossman4ff14ba2012-02-08 16:37:41 -0800675 return mMasterVolume;
676}
677
John Grossmand8f178d2012-07-20 14:51:35 -0700678bool AudioFlinger::masterMute_l() const
679{
John Grossmanee578c02012-07-23 17:05:46 -0700680 return mMasterMute;
John Grossmand8f178d2012-07-20 14:51:35 -0700681}
682
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800683status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
684 audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685{
686 // check calling permissions
687 if (!settingsAllowed()) {
688 return PERMISSION_DENIED;
689 }
690
Glenn Kasten263709e2012-01-06 08:40:01 -0800691 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Steve Block29357bc2012-01-06 19:20:56 +0000692 ALOGE("setStreamVolume() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700693 return BAD_VALUE;
694 }
695
696 AutoMutex lock(mLock);
697 PlaybackThread *thread = NULL;
698 if (output) {
699 thread = checkPlaybackThread_l(output);
700 if (thread == NULL) {
701 return BAD_VALUE;
702 }
703 }
704
705 mStreamTypes[stream].volume = value;
706
707 if (thread == NULL) {
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800708 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700709 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700710 }
711 } else {
712 thread->setStreamVolume(stream, value);
713 }
714
715 return NO_ERROR;
716}
717
Glenn Kastenfff6d712012-01-12 16:38:12 -0800718status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700719{
720 // check calling permissions
721 if (!settingsAllowed()) {
722 return PERMISSION_DENIED;
723 }
724
Glenn Kasten263709e2012-01-06 08:40:01 -0800725 if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
Dima Zavinfce7a472011-04-19 22:30:36 -0700726 uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Steve Block29357bc2012-01-06 19:20:56 +0000727 ALOGE("setStreamMute() invalid stream %d", stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700728 return BAD_VALUE;
729 }
730
Eric Laurent93575202011-01-18 18:39:02 -0800731 AutoMutex lock(mLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700732 mStreamTypes[stream].mute = muted;
733 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700734 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735
736 return NO_ERROR;
737}
738
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800739float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700740{
Glenn Kasten263709e2012-01-06 08:40:01 -0800741 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700742 return 0.0f;
743 }
744
745 AutoMutex lock(mLock);
746 float volume;
747 if (output) {
748 PlaybackThread *thread = checkPlaybackThread_l(output);
749 if (thread == NULL) {
750 return 0.0f;
751 }
752 volume = thread->streamVolume(stream);
753 } else {
Glenn Kasten6637baa2012-01-09 09:40:36 -0800754 volume = streamVolume_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700755 }
756
757 return volume;
758}
759
Glenn Kastenfff6d712012-01-12 16:38:12 -0800760bool AudioFlinger::streamMute(audio_stream_type_t stream) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700761{
Glenn Kasten263709e2012-01-06 08:40:01 -0800762 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700763 return true;
764 }
765
Glenn Kasten6637baa2012-01-09 09:40:36 -0800766 AutoMutex lock(mLock);
767 return streamMute_l(stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700768}
769
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800770status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700771{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700772 ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d",
773 ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid());
Eric Laurent81784c32012-11-19 14:55:58 -0800774
Mathias Agopian65ab4712010-07-14 17:59:35 -0700775 // check calling permissions
776 if (!settingsAllowed()) {
777 return PERMISSION_DENIED;
778 }
779
Mathias Agopian65ab4712010-07-14 17:59:35 -0700780 // ioHandle == 0 means the parameters are global to the audio hardware interface
781 if (ioHandle == 0) {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700782 Mutex::Autolock _l(mLock);
Dima Zavin799a70e2011-04-18 16:57:27 -0700783 status_t final_result = NO_ERROR;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800784 {
Eric Laurenta4c5a552012-03-29 10:12:40 -0700785 AutoMutex lock(mHardwareLock);
786 mHardwareStatus = AUDIO_HW_SET_PARAMETER;
787 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
788 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
789 status_t result = dev->set_parameters(dev, keyValuePairs.string());
790 final_result = result ?: final_result;
791 }
792 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800793 }
Eric Laurent59bd0da2011-08-01 09:52:20 -0700794 // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
795 AudioParameter param = AudioParameter(keyValuePairs);
796 String8 value;
797 if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
Eric Laurentbee53372011-08-29 12:42:48 -0700798 bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
799 if (mBtNrecIsOff != btNrecIsOff) {
Eric Laurent59bd0da2011-08-01 09:52:20 -0700800 for (size_t i = 0; i < mRecordThreads.size(); i++) {
801 sp<RecordThread> thread = mRecordThreads.valueAt(i);
Eric Laurentf1c04f92012-08-28 14:26:53 -0700802 audio_devices_t device = thread->inDevice();
Glenn Kasten510a3d62012-07-16 14:24:34 -0700803 bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
804 // collect all of the thread's session IDs
805 KeyedVector<int, bool> ids = thread->sessionIds();
806 // suspend effects associated with those session IDs
807 for (size_t j = 0; j < ids.size(); ++j) {
808 int sessionId = ids.keyAt(j);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700809 thread->setEffectSuspended(FX_IID_AEC,
810 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700811 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700812 thread->setEffectSuspended(FX_IID_NS,
813 suspend,
Glenn Kasten510a3d62012-07-16 14:24:34 -0700814 sessionId);
Eric Laurent59bd0da2011-08-01 09:52:20 -0700815 }
816 }
Eric Laurentbee53372011-08-29 12:42:48 -0700817 mBtNrecIsOff = btNrecIsOff;
Eric Laurent59bd0da2011-08-01 09:52:20 -0700818 }
819 }
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700820 String8 screenState;
821 if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) {
822 bool isOff = screenState == "off";
Eric Laurent81784c32012-11-19 14:55:58 -0800823 if (isOff != (AudioFlinger::mScreenState & 1)) {
824 AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff;
Glenn Kasten28ed2f92012-06-07 10:17:54 -0700825 }
826 }
Dima Zavin799a70e2011-04-18 16:57:27 -0700827 return final_result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700828 }
829
830 // hold a strong ref on thread in case closeOutput() or closeInput() is called
831 // and the thread is exited once the lock is released
832 sp<ThreadBase> thread;
833 {
834 Mutex::Autolock _l(mLock);
835 thread = checkPlaybackThread_l(ioHandle);
Glenn Kastend5903ec2012-03-18 10:33:27 -0700836 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700837 thread = checkRecordThread_l(ioHandle);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -0800838 } else if (thread == primaryPlaybackThread_l()) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700839 // indicate output device change to all input threads for pre processing
840 AudioParameter param = AudioParameter(keyValuePairs);
841 int value;
Eric Laurent89d94e72012-03-16 20:37:59 -0700842 if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) &&
843 (value != 0)) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700844 for (size_t i = 0; i < mRecordThreads.size(); i++) {
845 mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
846 }
847 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700848 }
849 }
Glenn Kasten7378ca52012-01-20 13:44:40 -0800850 if (thread != 0) {
851 return thread->setParameters(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700852 }
853 return BAD_VALUE;
854}
855
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800856String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700857{
Glenn Kasten827e5f12012-11-02 10:00:06 -0700858 ALOGVV("getParameters() io %d, keys %s, calling pid %d",
859 ioHandle, keys.string(), IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700860
Eric Laurenta4c5a552012-03-29 10:12:40 -0700861 Mutex::Autolock _l(mLock);
862
Mathias Agopian65ab4712010-07-14 17:59:35 -0700863 if (ioHandle == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700864 String8 out_s8;
865
Dima Zavin799a70e2011-04-18 16:57:27 -0700866 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800867 char *s;
868 {
869 AutoMutex lock(mHardwareLock);
870 mHardwareStatus = AUDIO_HW_GET_PARAMETER;
Eric Laurenta4c5a552012-03-29 10:12:40 -0700871 audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800872 s = dev->get_parameters(dev, keys.string());
873 mHardwareStatus = AUDIO_HW_IDLE;
874 }
John Grossmanef7740b2012-02-09 11:28:36 -0800875 out_s8 += String8(s ? s : "");
Dima Zavin799a70e2011-04-18 16:57:27 -0700876 free(s);
877 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700878 return out_s8;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700879 }
880
Mathias Agopian65ab4712010-07-14 17:59:35 -0700881 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
882 if (playbackThread != NULL) {
883 return playbackThread->getParameters(keys);
884 }
885 RecordThread *recordThread = checkRecordThread_l(ioHandle);
886 if (recordThread != NULL) {
887 return recordThread->getParameters(keys);
888 }
889 return String8("");
890}
891
Glenn Kastendd8104c2012-07-02 12:42:44 -0700892size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
893 audio_channel_mask_t channelMask) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700894{
Eric Laurenta1884f92011-08-23 08:25:03 -0700895 status_t ret = initCheck();
896 if (ret != NO_ERROR) {
897 return 0;
898 }
899
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800900 AutoMutex lock(mHardwareLock);
901 mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700902 struct audio_config config = {
903 sample_rate: sampleRate,
Glenn Kastendd8104c2012-07-02 12:42:44 -0700904 channel_mask: channelMask,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -0700905 format: format,
906 };
John Grossmanee578c02012-07-23 17:05:46 -0700907 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
908 size_t size = dev->get_input_buffer_size(dev, &config);
Glenn Kasten2b213bc2012-02-02 14:05:20 -0800909 mHardwareStatus = AUDIO_HW_IDLE;
910 return size;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911}
912
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800913unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700914{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700915 Mutex::Autolock _l(mLock);
916
917 RecordThread *recordThread = checkRecordThread_l(ioHandle);
918 if (recordThread != NULL) {
919 return recordThread->getInputFramesLost();
920 }
921 return 0;
922}
923
924status_t AudioFlinger::setVoiceVolume(float value)
925{
Eric Laurenta1884f92011-08-23 08:25:03 -0700926 status_t ret = initCheck();
927 if (ret != NO_ERROR) {
928 return ret;
929 }
930
Mathias Agopian65ab4712010-07-14 17:59:35 -0700931 // check calling permissions
932 if (!settingsAllowed()) {
933 return PERMISSION_DENIED;
934 }
935
936 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -0700937 audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice();
Glenn Kasten8abf44d2012-02-02 14:16:03 -0800938 mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -0700939 ret = dev->set_voice_volume(dev, value);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940 mHardwareStatus = AUDIO_HW_IDLE;
941
942 return ret;
943}
944
Glenn Kasten26c77552012-11-16 12:01:44 -0800945status_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800946 audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947{
948 status_t status;
949
950 Mutex::Autolock _l(mLock);
951
952 PlaybackThread *playbackThread = checkPlaybackThread_l(output);
953 if (playbackThread != NULL) {
954 return playbackThread->getRenderPosition(halFrames, dspFrames);
955 }
956
957 return BAD_VALUE;
958}
959
960void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
961{
962
963 Mutex::Autolock _l(mLock);
964
Glenn Kastenbb001922012-02-03 11:10:26 -0800965 pid_t pid = IPCThreadState::self()->getCallingPid();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700966 if (mNotificationClients.indexOfKey(pid) < 0) {
967 sp<NotificationClient> notificationClient = new NotificationClient(this,
968 client,
969 pid);
Steve Block3856b092011-10-20 11:56:00 +0100970 ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971
972 mNotificationClients.add(pid, notificationClient);
973
974 sp<IBinder> binder = client->asBinder();
975 binder->linkToDeath(notificationClient);
976
977 // the config change is always sent from playback or record threads to avoid deadlock
978 // with AudioSystem::gLock
979 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700980 mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700981 }
982
983 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700984 mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700985 }
986 }
987}
988
989void AudioFlinger::removeNotificationClient(pid_t pid)
990{
991 Mutex::Autolock _l(mLock);
992
Glenn Kastena3b09252012-01-20 09:19:01 -0800993 mNotificationClients.removeItem(pid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700994
Steve Block3856b092011-10-20 11:56:00 +0100995 ALOGV("%d died, releasing its sessions", pid);
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800996 size_t num = mAudioSessionRefs.size();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700997 bool removed = false;
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800998 for (size_t i = 0; i< num; ) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700999 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001000 ALOGV(" pid %d @ %d", ref->mPid, i);
1001 if (ref->mPid == pid) {
1002 ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001003 mAudioSessionRefs.removeAt(i);
1004 delete ref;
1005 removed = true;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001006 num--;
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001007 } else {
1008 i++;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001009 }
1010 }
1011 if (removed) {
1012 purgeStaleEffects_l();
1013 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001014}
1015
1016// audioConfigChanged_l() must be called with AudioFlinger::mLock held
Glenn Kastenb81cc8c2012-03-01 09:14:51 -08001017void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001018{
1019 size_t size = mNotificationClients.size();
1020 for (size_t i = 0; i < size; i++) {
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001021 mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1022 param2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001023 }
1024}
1025
1026// removeClient_l() must be called with AudioFlinger::mLock held
1027void AudioFlinger::removeClient_l(pid_t pid)
1028{
Glenn Kasten827e5f12012-11-02 10:00:06 -07001029 ALOGV("removeClient_l() pid %d, calling pid %d", pid,
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001030 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001031 mClients.removeItem(pid);
1032}
1033
Eric Laurent717e1282012-06-29 16:36:52 -07001034// getEffectThread_l() must be called with AudioFlinger::mLock held
1035sp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId)
1036{
1037 sp<PlaybackThread> thread;
1038
1039 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1040 if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) {
1041 ALOG_ASSERT(thread == 0);
1042 thread = mPlaybackThreads.valueAt(i);
1043 }
1044 }
1045
1046 return thread;
1047}
Mathias Agopian65ab4712010-07-14 17:59:35 -07001048
Mathias Agopian65ab4712010-07-14 17:59:35 -07001049
Mathias Agopian65ab4712010-07-14 17:59:35 -07001050
1051// ----------------------------------------------------------------------------
1052
1053AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
1054 : RefBase(),
1055 mAudioFlinger(audioFlinger),
Glenn Kasten99e53b82012-01-19 08:59:58 -08001056 // 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 -07001057 mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
John Grossman4ff14ba2012-02-08 16:37:41 -08001058 mPid(pid),
1059 mTimedTrackCount(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001060{
1061 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
1062}
1063
1064// Client destructor must be called with AudioFlinger::mLock held
1065AudioFlinger::Client::~Client()
1066{
1067 mAudioFlinger->removeClient_l(mPid);
1068}
1069
Glenn Kasten435dbe62012-01-30 10:15:48 -08001070sp<MemoryDealer> AudioFlinger::Client::heap() const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001071{
1072 return mMemoryDealer;
1073}
1074
John Grossman4ff14ba2012-02-08 16:37:41 -08001075// Reserve one of the limited slots for a timed audio track associated
1076// with this client
1077bool AudioFlinger::Client::reserveTimedTrack()
1078{
1079 const int kMaxTimedTracksPerClient = 4;
1080
1081 Mutex::Autolock _l(mTimedTrackLock);
1082
1083 if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
1084 ALOGW("can not create timed track - pid %d has exceeded the limit",
1085 mPid);
1086 return false;
1087 }
1088
1089 mTimedTrackCount++;
1090 return true;
1091}
1092
1093// Release a slot for a timed audio track
1094void AudioFlinger::Client::releaseTimedTrack()
1095{
1096 Mutex::Autolock _l(mTimedTrackLock);
1097 mTimedTrackCount--;
1098}
1099
Mathias Agopian65ab4712010-07-14 17:59:35 -07001100// ----------------------------------------------------------------------------
1101
1102AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
1103 const sp<IAudioFlingerClient>& client,
1104 pid_t pid)
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001105 : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001106{
1107}
1108
1109AudioFlinger::NotificationClient::~NotificationClient()
1110{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001111}
1112
1113void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
1114{
1115 sp<NotificationClient> keep(this);
Glenn Kastena1117922012-01-26 10:53:32 -08001116 mAudioFlinger->removeNotificationClient(mPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001117}
1118
Mathias Agopian65ab4712010-07-14 17:59:35 -07001119
1120// ----------------------------------------------------------------------------
1121
1122sp<IAudioRecord> AudioFlinger::openRecord(
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001123 audio_io_handle_t input,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001124 uint32_t sampleRate,
Glenn Kasten58f30212012-01-12 12:27:51 -08001125 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -07001126 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -08001127 size_t frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -08001128 IAudioFlinger::track_flags_t flags,
Glenn Kasten1879fff2012-07-11 15:36:59 -07001129 pid_t tid,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001130 int *sessionId,
1131 status_t *status)
1132{
1133 sp<RecordThread::RecordTrack> recordTrack;
1134 sp<RecordHandle> recordHandle;
1135 sp<Client> client;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001136 status_t lStatus;
1137 RecordThread *thread;
1138 size_t inFrameCount;
1139 int lSessionId;
1140
1141 // check calling permissions
1142 if (!recordingAllowed()) {
1143 lStatus = PERMISSION_DENIED;
1144 goto Exit;
1145 }
1146
1147 // add client to list
1148 { // scope for mLock
1149 Mutex::Autolock _l(mLock);
1150 thread = checkRecordThread_l(input);
1151 if (thread == NULL) {
1152 lStatus = BAD_VALUE;
1153 goto Exit;
1154 }
1155
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001156 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001157 client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001158
1159 // If no audio session id is provided, create one here
Dima Zavinfce7a472011-04-19 22:30:36 -07001160 if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001161 lSessionId = *sessionId;
1162 } else {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001163 lSessionId = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001164 if (sessionId != NULL) {
1165 *sessionId = lSessionId;
1166 }
1167 }
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001168 // create new record track.
1169 // The record track uses one track in mHardwareMixerThread by convention.
Glenn Kasten1879fff2012-07-11 15:36:59 -07001170 recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask,
1171 frameCount, lSessionId, flags, tid, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001172 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001173 if (lStatus != NO_ERROR) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001174 // remove local strong reference to Client before deleting the RecordTrack so that the
1175 // Client destructor is called by the TrackBase destructor with mLock held
Mathias Agopian65ab4712010-07-14 17:59:35 -07001176 client.clear();
1177 recordTrack.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001178 goto Exit;
1179 }
1180
1181 // return to handle to client
1182 recordHandle = new RecordHandle(recordTrack);
1183 lStatus = NO_ERROR;
1184
1185Exit:
1186 if (status) {
1187 *status = lStatus;
1188 }
1189 return recordHandle;
1190}
1191
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001192
1193
Mathias Agopian65ab4712010-07-14 17:59:35 -07001194// ----------------------------------------------------------------------------
1195
Eric Laurenta4c5a552012-03-29 10:12:40 -07001196audio_module_handle_t AudioFlinger::loadHwModule(const char *name)
1197{
1198 if (!settingsAllowed()) {
1199 return 0;
1200 }
1201 Mutex::Autolock _l(mLock);
1202 return loadHwModule_l(name);
1203}
1204
1205// loadHwModule_l() must be called with AudioFlinger::mLock held
1206audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name)
1207{
1208 for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
1209 if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) {
1210 ALOGW("loadHwModule() module %s already loaded", name);
1211 return mAudioHwDevs.keyAt(i);
1212 }
1213 }
1214
Eric Laurenta4c5a552012-03-29 10:12:40 -07001215 audio_hw_device_t *dev;
1216
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001217 int rc = load_audio_interface(name, &dev);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001218 if (rc) {
1219 ALOGI("loadHwModule() error %d loading module %s ", rc, name);
1220 return 0;
1221 }
1222
1223 mHardwareStatus = AUDIO_HW_INIT;
1224 rc = dev->init_check(dev);
1225 mHardwareStatus = AUDIO_HW_IDLE;
1226 if (rc) {
1227 ALOGI("loadHwModule() init check error %d for module %s ", rc, name);
1228 return 0;
1229 }
1230
John Grossmanee578c02012-07-23 17:05:46 -07001231 // Check and cache this HAL's level of support for master mute and master
1232 // volume. If this is the first HAL opened, and it supports the get
1233 // methods, use the initial values provided by the HAL as the current
1234 // master mute and volume settings.
1235
1236 AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0);
1237 { // scope for auto-lock pattern
Eric Laurenta4c5a552012-03-29 10:12:40 -07001238 AutoMutex lock(mHardwareLock);
John Grossmanee578c02012-07-23 17:05:46 -07001239
1240 if (0 == mAudioHwDevs.size()) {
1241 mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
1242 if (NULL != dev->get_master_volume) {
1243 float mv;
1244 if (OK == dev->get_master_volume(dev, &mv)) {
1245 mMasterVolume = mv;
1246 }
1247 }
1248
1249 mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE;
1250 if (NULL != dev->get_master_mute) {
1251 bool mm;
1252 if (OK == dev->get_master_mute(dev, &mm)) {
1253 mMasterMute = mm;
1254 }
1255 }
1256 }
1257
Eric Laurenta4c5a552012-03-29 10:12:40 -07001258 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
John Grossmanee578c02012-07-23 17:05:46 -07001259 if ((NULL != dev->set_master_volume) &&
1260 (OK == dev->set_master_volume(dev, mMasterVolume))) {
1261 flags = static_cast<AudioHwDevice::Flags>(flags |
1262 AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME);
1263 }
1264
1265 mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE;
1266 if ((NULL != dev->set_master_mute) &&
1267 (OK == dev->set_master_mute(dev, mMasterMute))) {
1268 flags = static_cast<AudioHwDevice::Flags>(flags |
1269 AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE);
1270 }
1271
Eric Laurenta4c5a552012-03-29 10:12:40 -07001272 mHardwareStatus = AUDIO_HW_IDLE;
1273 }
1274
1275 audio_module_handle_t handle = nextUniqueId();
John Grossmanee578c02012-07-23 17:05:46 -07001276 mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags));
Eric Laurenta4c5a552012-03-29 10:12:40 -07001277
1278 ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d",
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001279 name, dev->common.module->name, dev->common.module->id, handle);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001280
1281 return handle;
1282
1283}
1284
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001285// ----------------------------------------------------------------------------
1286
Glenn Kasten3b16c762012-11-14 08:44:39 -08001287uint32_t AudioFlinger::getPrimaryOutputSamplingRate()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001288{
1289 Mutex::Autolock _l(mLock);
1290 PlaybackThread *thread = primaryPlaybackThread_l();
1291 return thread != NULL ? thread->sampleRate() : 0;
1292}
1293
Glenn Kastene33054e2012-11-14 12:54:39 -08001294size_t AudioFlinger::getPrimaryOutputFrameCount()
Glenn Kastencc0f1cf2012-09-24 11:27:18 -07001295{
1296 Mutex::Autolock _l(mLock);
1297 PlaybackThread *thread = primaryPlaybackThread_l();
1298 return thread != NULL ? thread->frameCountHAL() : 0;
1299}
1300
1301// ----------------------------------------------------------------------------
1302
Eric Laurenta4c5a552012-03-29 10:12:40 -07001303audio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module,
1304 audio_devices_t *pDevices,
1305 uint32_t *pSamplingRate,
1306 audio_format_t *pFormat,
1307 audio_channel_mask_t *pChannelMask,
1308 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001309 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001310{
1311 status_t status;
1312 PlaybackThread *thread = NULL;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001313 struct audio_config config = {
1314 sample_rate: pSamplingRate ? *pSamplingRate : 0,
1315 channel_mask: pChannelMask ? *pChannelMask : 0,
1316 format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT,
1317 };
1318 audio_stream_out_t *outStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001319 AudioHwDevice *outHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001320
Eric Laurenta4c5a552012-03-29 10:12:40 -07001321 ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %d, Channels %x, flags %x",
1322 module,
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001323 (pDevices != NULL) ? *pDevices : 0,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001324 config.sample_rate,
1325 config.format,
1326 config.channel_mask,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001327 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001328
1329 if (pDevices == NULL || *pDevices == 0) {
1330 return 0;
1331 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001332
Mathias Agopian65ab4712010-07-14 17:59:35 -07001333 Mutex::Autolock _l(mLock);
1334
Eric Laurenta4c5a552012-03-29 10:12:40 -07001335 outHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001336 if (outHwDev == NULL)
1337 return 0;
1338
John Grossmanee578c02012-07-23 17:05:46 -07001339 audio_hw_device_t *hwDevHal = outHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001340 audio_io_handle_t id = nextUniqueId();
1341
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001342 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001343
John Grossmanee578c02012-07-23 17:05:46 -07001344 status = hwDevHal->open_output_stream(hwDevHal,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001345 id,
1346 *pDevices,
1347 (audio_output_flags_t)flags,
1348 &config,
1349 &outStream);
1350
Glenn Kasten8abf44d2012-02-02 14:16:03 -08001351 mHardwareStatus = AUDIO_HW_IDLE;
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001352 ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, "
1353 "Channels %x, status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001354 outStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001355 config.sample_rate,
1356 config.format,
1357 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001358 status);
1359
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001360 if (status == NO_ERROR && outStream != NULL) {
Dima Zavin799a70e2011-04-18 16:57:27 -07001361 AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001362
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001363 if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) ||
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001364 (config.format != AUDIO_FORMAT_PCM_16_BIT) ||
1365 (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001366 thread = new DirectOutputThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001367 ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001368 } else {
1369 thread = new MixerThread(this, output, id, *pDevices);
Steve Block3856b092011-10-20 11:56:00 +01001370 ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001371 }
1372 mPlaybackThreads.add(id, thread);
1373
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001374 if (pSamplingRate != NULL) *pSamplingRate = config.sample_rate;
1375 if (pFormat != NULL) *pFormat = config.format;
1376 if (pChannelMask != NULL) *pChannelMask = config.channel_mask;
Glenn Kastena0d68332012-01-27 16:47:15 -08001377 if (pLatencyMs != NULL) *pLatencyMs = thread->latency();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001378
1379 // notify client processes of the new output creation
1380 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001381
1382 // the first primary output opened designates the primary hw device
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001383 if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
Eric Laurenta4c5a552012-03-29 10:12:40 -07001384 ALOGI("Using module %d has the primary audio interface", module);
1385 mPrimaryHardwareDev = outHwDev;
1386
1387 AutoMutex lock(mHardwareLock);
1388 mHardwareStatus = AUDIO_HW_SET_MODE;
John Grossmanee578c02012-07-23 17:05:46 -07001389 hwDevHal->set_mode(hwDevHal, mMode);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001390 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001391 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001392 return id;
1393 }
1394
1395 return 0;
1396}
1397
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001398audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
1399 audio_io_handle_t output2)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001400{
1401 Mutex::Autolock _l(mLock);
1402 MixerThread *thread1 = checkMixerThread_l(output1);
1403 MixerThread *thread2 = checkMixerThread_l(output2);
1404
1405 if (thread1 == NULL || thread2 == NULL) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001406 ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1,
1407 output2);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001408 return 0;
1409 }
1410
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001411 audio_io_handle_t id = nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001412 DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
1413 thread->addOutputTrack(thread2);
1414 mPlaybackThreads.add(id, thread);
1415 // notify client processes of the new output creation
1416 thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
1417 return id;
1418}
1419
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001420status_t AudioFlinger::closeOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001421{
Glenn Kastend96c5722012-04-25 13:44:49 -07001422 return closeOutput_nonvirtual(output);
1423}
1424
1425status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output)
1426{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001427 // keep strong reference on the playback thread so that
1428 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001429 sp<PlaybackThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001430 {
1431 Mutex::Autolock _l(mLock);
1432 thread = checkPlaybackThread_l(output);
1433 if (thread == NULL) {
1434 return BAD_VALUE;
1435 }
1436
Steve Block3856b092011-10-20 11:56:00 +01001437 ALOGV("closeOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001438
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001439 if (thread->type() == ThreadBase::MIXER) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001440 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001441 if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001442 DuplicatingThread *dupThread =
1443 (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001444 dupThread->removeOutputTrack((MixerThread *)thread.get());
1445 }
1446 }
1447 }
Glenn Kastena1117922012-01-26 10:53:32 -08001448 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001449 mPlaybackThreads.removeItem(output);
1450 }
1451 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001452 // The thread entity (active unit of execution) is no longer running here,
1453 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001454
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001455 if (thread->type() != ThreadBase::DUPLICATING) {
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001456 AudioStreamOut *out = thread->clearOutput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001457 ALOG_ASSERT(out != NULL, "out shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001458 // from now on thread->mOutput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001459 out->hwDev()->close_output_stream(out->hwDev(), out->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001460 delete out;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001461 }
1462 return NO_ERROR;
1463}
1464
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001465status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001466{
1467 Mutex::Autolock _l(mLock);
1468 PlaybackThread *thread = checkPlaybackThread_l(output);
1469
1470 if (thread == NULL) {
1471 return BAD_VALUE;
1472 }
1473
Steve Block3856b092011-10-20 11:56:00 +01001474 ALOGV("suspendOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001475 thread->suspend();
1476
1477 return NO_ERROR;
1478}
1479
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001480status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001481{
1482 Mutex::Autolock _l(mLock);
1483 PlaybackThread *thread = checkPlaybackThread_l(output);
1484
1485 if (thread == NULL) {
1486 return BAD_VALUE;
1487 }
1488
Steve Block3856b092011-10-20 11:56:00 +01001489 ALOGV("restoreOutput() %d", output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001490
1491 thread->restore();
1492
1493 return NO_ERROR;
1494}
1495
Eric Laurenta4c5a552012-03-29 10:12:40 -07001496audio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module,
1497 audio_devices_t *pDevices,
1498 uint32_t *pSamplingRate,
1499 audio_format_t *pFormat,
Glenn Kasten254af182012-07-03 14:59:05 -07001500 audio_channel_mask_t *pChannelMask)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001501{
1502 status_t status;
1503 RecordThread *thread = NULL;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001504 struct audio_config config = {
1505 sample_rate: pSamplingRate ? *pSamplingRate : 0,
1506 channel_mask: pChannelMask ? *pChannelMask : 0,
1507 format: pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT,
1508 };
1509 uint32_t reqSamplingRate = config.sample_rate;
1510 audio_format_t reqFormat = config.format;
1511 audio_channel_mask_t reqChannels = config.channel_mask;
1512 audio_stream_in_t *inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001513 AudioHwDevice *inHwDev;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001514
1515 if (pDevices == NULL || *pDevices == 0) {
1516 return 0;
1517 }
Dima Zavin799a70e2011-04-18 16:57:27 -07001518
Mathias Agopian65ab4712010-07-14 17:59:35 -07001519 Mutex::Autolock _l(mLock);
1520
Eric Laurenta4c5a552012-03-29 10:12:40 -07001521 inHwDev = findSuitableHwDev_l(module, *pDevices);
Dima Zavin799a70e2011-04-18 16:57:27 -07001522 if (inHwDev == NULL)
1523 return 0;
1524
John Grossmanee578c02012-07-23 17:05:46 -07001525 audio_hw_device_t *inHwHal = inHwDev->hwDevice();
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001526 audio_io_handle_t id = nextUniqueId();
1527
John Grossmanee578c02012-07-23 17:05:46 -07001528 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config,
Dima Zavin799a70e2011-04-18 16:57:27 -07001529 &inStream);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001530 ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, "
1531 "status %d",
Dima Zavin799a70e2011-04-18 16:57:27 -07001532 inStream,
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001533 config.sample_rate,
1534 config.format,
1535 config.channel_mask,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001536 status);
1537
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001538 // If the input could not be opened with the requested parameters and we can handle the
1539 // conversion internally, try to open again with the proposed parameters. The AudioFlinger can
1540 // 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 -07001541 if (status == BAD_VALUE &&
1542 reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT &&
1543 (config.sample_rate <= 2 * reqSamplingRate) &&
1544 (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) {
Glenn Kasten254af182012-07-03 14:59:05 -07001545 ALOGV("openInput() reopening with proposed sampling rate and channel mask");
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001546 inStream = NULL;
John Grossmanee578c02012-07-23 17:05:46 -07001547 status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001548 }
1549
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001550 if (status == NO_ERROR && inStream != NULL) {
Glenn Kastend06785b2012-09-30 12:29:28 -07001551
1552 // Try to re-use most recently used Pipe to archive a copy of input for dumpsys,
1553 // or (re-)create if current Pipe is idle and does not match the new format
1554 sp<NBAIO_Sink> teeSink;
1555#ifdef TEE_SINK_INPUT_FRAMES
1556 enum {
1557 TEE_SINK_NO, // don't copy input
1558 TEE_SINK_NEW, // copy input using a new pipe
1559 TEE_SINK_OLD, // copy input using an existing pipe
1560 } kind;
1561 NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common),
1562 popcount(inStream->common.get_channels(&inStream->common)));
1563 if (format == Format_Invalid) {
1564 kind = TEE_SINK_NO;
1565 } else if (mRecordTeeSink == 0) {
1566 kind = TEE_SINK_NEW;
1567 } else if (mRecordTeeSink->getStrongCount() != 1) {
1568 kind = TEE_SINK_NO;
1569 } else if (format == mRecordTeeSink->format()) {
1570 kind = TEE_SINK_OLD;
1571 } else {
1572 kind = TEE_SINK_NEW;
1573 }
1574 switch (kind) {
1575 case TEE_SINK_NEW: {
1576 Pipe *pipe = new Pipe(TEE_SINK_INPUT_FRAMES, format);
1577 size_t numCounterOffers = 0;
1578 const NBAIO_Format offers[1] = {format};
1579 ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
1580 ALOG_ASSERT(index == 0);
1581 PipeReader *pipeReader = new PipeReader(*pipe);
1582 numCounterOffers = 0;
1583 index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
1584 ALOG_ASSERT(index == 0);
1585 mRecordTeeSink = pipe;
1586 mRecordTeeSource = pipeReader;
1587 teeSink = pipe;
1588 }
1589 break;
1590 case TEE_SINK_OLD:
1591 teeSink = mRecordTeeSink;
1592 break;
1593 case TEE_SINK_NO:
1594 default:
1595 break;
1596 }
1597#endif
Dima Zavin799a70e2011-04-18 16:57:27 -07001598 AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
1599
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001600 // Start record thread
1601 // RecorThread require both input and output device indication to forward to audio
1602 // pre processing modules
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001603 thread = new RecordThread(this,
1604 input,
1605 reqSamplingRate,
1606 reqChannels,
1607 id,
Eric Laurentd3922f72013-02-01 17:57:04 -08001608 primaryOutputDevice_l(),
1609 *pDevices,
1610 teeSink);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001611 mRecordThreads.add(id, thread);
Steve Block3856b092011-10-20 11:56:00 +01001612 ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
Glenn Kastena0d68332012-01-27 16:47:15 -08001613 if (pSamplingRate != NULL) *pSamplingRate = reqSamplingRate;
Eric Laurentf7ffb8b2012-04-14 09:06:57 -07001614 if (pFormat != NULL) *pFormat = config.format;
Eric Laurenta4c5a552012-03-29 10:12:40 -07001615 if (pChannelMask != NULL) *pChannelMask = reqChannels;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001616
Mathias Agopian65ab4712010-07-14 17:59:35 -07001617 // notify client processes of the new input creation
1618 thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
1619 return id;
1620 }
1621
1622 return 0;
1623}
1624
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001625status_t AudioFlinger::closeInput(audio_io_handle_t input)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001626{
Glenn Kastend96c5722012-04-25 13:44:49 -07001627 return closeInput_nonvirtual(input);
1628}
1629
1630status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input)
1631{
Mathias Agopian65ab4712010-07-14 17:59:35 -07001632 // keep strong reference on the record thread so that
1633 // it is not destroyed while exit() is executed
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001634 sp<RecordThread> thread;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001635 {
1636 Mutex::Autolock _l(mLock);
1637 thread = checkRecordThread_l(input);
Glenn Kastend5903ec2012-03-18 10:33:27 -07001638 if (thread == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001639 return BAD_VALUE;
1640 }
1641
Steve Block3856b092011-10-20 11:56:00 +01001642 ALOGV("closeInput() %d", input);
Glenn Kastena1117922012-01-26 10:53:32 -08001643 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001644 mRecordThreads.removeItem(input);
1645 }
1646 thread->exit();
Glenn Kastenb28686f2012-01-06 08:39:38 -08001647 // The thread entity (active unit of execution) is no longer running here,
1648 // but the ThreadBase container still exists.
Mathias Agopian65ab4712010-07-14 17:59:35 -07001649
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001650 AudioStreamIn *in = thread->clearInput();
Glenn Kasten5798d4e2012-03-08 12:18:35 -08001651 ALOG_ASSERT(in != NULL, "in shouldn't be NULL");
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001652 // from now on thread->mInput is NULL
John Grossmanee578c02012-07-23 17:05:46 -07001653 in->hwDev()->close_input_stream(in->hwDev(), in->stream);
Dima Zavin799a70e2011-04-18 16:57:27 -07001654 delete in;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001655
1656 return NO_ERROR;
1657}
1658
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001659status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
Mathias Agopian65ab4712010-07-14 17:59:35 -07001660{
1661 Mutex::Autolock _l(mLock);
Steve Block3856b092011-10-20 11:56:00 +01001662 ALOGV("setStreamOutput() stream %d to output %d", stream, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001663
1664 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1665 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent22167852012-06-20 12:26:32 -07001666 thread->invalidateTracks(stream);
Eric Laurentde070132010-07-13 04:45:46 -07001667 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001668
1669 return NO_ERROR;
1670}
1671
1672
1673int AudioFlinger::newAudioSessionId()
1674{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001675 return nextUniqueId();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001676}
1677
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001678void AudioFlinger::acquireAudioSessionId(int audioSession)
1679{
1680 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001681 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001682 ALOGV("acquiring %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001683 size_t num = mAudioSessionRefs.size();
1684 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001685 AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001686 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1687 ref->mCnt++;
1688 ALOGV(" incremented refcount to %d", ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001689 return;
1690 }
1691 }
Glenn Kasten84afa3b2012-01-25 15:28:08 -08001692 mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
1693 ALOGV(" added new entry for %d", audioSession);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001694}
1695
1696void AudioFlinger::releaseAudioSessionId(int audioSession)
1697{
1698 Mutex::Autolock _l(mLock);
Glenn Kastenbb001922012-02-03 11:10:26 -08001699 pid_t caller = IPCThreadState::self()->getCallingPid();
Steve Block3856b092011-10-20 11:56:00 +01001700 ALOGV("releasing %d from %d", audioSession, caller);
Glenn Kasten8d6a2442012-02-08 14:04:28 -08001701 size_t num = mAudioSessionRefs.size();
1702 for (size_t i = 0; i< num; i++) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001703 AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001704 if (ref->mSessionid == audioSession && ref->mPid == caller) {
1705 ref->mCnt--;
1706 ALOGV(" decremented refcount to %d", ref->mCnt);
1707 if (ref->mCnt == 0) {
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001708 mAudioSessionRefs.removeAt(i);
1709 delete ref;
1710 purgeStaleEffects_l();
1711 }
1712 return;
1713 }
1714 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001715 ALOGW("session id %d not found for pid %d", audioSession, caller);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001716}
1717
1718void AudioFlinger::purgeStaleEffects_l() {
1719
Steve Block3856b092011-10-20 11:56:00 +01001720 ALOGV("purging stale effects");
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001721
1722 Vector< sp<EffectChain> > chains;
1723
1724 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1725 sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
1726 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1727 sp<EffectChain> ec = t->mEffectChains[j];
Marco Nelissen0270b182011-08-12 14:14:39 -07001728 if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
1729 chains.push(ec);
1730 }
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001731 }
1732 }
1733 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1734 sp<RecordThread> t = mRecordThreads.valueAt(i);
1735 for (size_t j = 0; j < t->mEffectChains.size(); j++) {
1736 sp<EffectChain> ec = t->mEffectChains[j];
1737 chains.push(ec);
1738 }
1739 }
1740
1741 for (size_t i = 0; i < chains.size(); i++) {
1742 sp<EffectChain> ec = chains[i];
1743 int sessionid = ec->sessionId();
1744 sp<ThreadBase> t = ec->mThread.promote();
1745 if (t == 0) {
1746 continue;
1747 }
1748 size_t numsessionrefs = mAudioSessionRefs.size();
1749 bool found = false;
1750 for (size_t k = 0; k < numsessionrefs; k++) {
1751 AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
Glenn Kasten012ca6b2012-03-06 11:22:01 -08001752 if (ref->mSessionid == sessionid) {
Steve Block3856b092011-10-20 11:56:00 +01001753 ALOGV(" session %d still exists for %d with %d refs",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001754 sessionid, ref->mPid, ref->mCnt);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001755 found = true;
1756 break;
1757 }
1758 }
1759 if (!found) {
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001760 Mutex::Autolock _l (t->mLock);
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001761 // remove all effects from the chain
1762 while (ec->mEffects.size()) {
1763 sp<EffectModule> effect = ec->mEffects[0];
1764 effect->unPin();
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001765 t->removeEffect_l(effect);
Eric Laurenta5f44eb2012-06-25 11:38:29 -07001766 if (effect->purgeHandles()) {
1767 t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001768 }
1769 AudioSystem::unregisterEffect(effect->id());
1770 }
1771 }
1772 }
1773 return;
1774}
1775
Mathias Agopian65ab4712010-07-14 17:59:35 -07001776// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001777AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001778{
Glenn Kastena1117922012-01-26 10:53:32 -08001779 return mPlaybackThreads.valueFor(output).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001780}
1781
1782// checkMixerThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001783AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001784{
1785 PlaybackThread *thread = checkPlaybackThread_l(output);
Glenn Kastena1117922012-01-26 10:53:32 -08001786 return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001787}
1788
1789// checkRecordThread_l() must be called with AudioFlinger::mLock held
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001790AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001791{
Glenn Kastena1117922012-01-26 10:53:32 -08001792 return mRecordThreads.valueFor(input).get();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001793}
1794
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001795uint32_t AudioFlinger::nextUniqueId()
Mathias Agopian65ab4712010-07-14 17:59:35 -07001796{
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001797 return android_atomic_inc(&mNextUniqueId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001798}
1799
Glenn Kasten02fe1bf2012-02-24 15:42:17 -08001800AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001801{
1802 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1803 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurentb8ba0a92011-08-07 16:32:26 -07001804 AudioStreamOut *output = thread->getOutput();
John Grossmanee578c02012-07-23 17:05:46 -07001805 if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001806 return thread;
1807 }
1808 }
1809 return NULL;
1810}
1811
Glenn Kastenbb4350d2012-07-03 15:56:38 -07001812audio_devices_t AudioFlinger::primaryOutputDevice_l() const
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001813{
1814 PlaybackThread *thread = primaryPlaybackThread_l();
1815
1816 if (thread == NULL) {
1817 return 0;
1818 }
1819
Eric Laurentf1c04f92012-08-28 14:26:53 -07001820 return thread->outDevice();
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001821}
1822
Eric Laurenta011e352012-03-29 15:51:43 -07001823sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type,
1824 int triggerSession,
1825 int listenerSession,
1826 sync_event_callback_t callBack,
1827 void *cookie)
1828{
1829 Mutex::Autolock _l(mLock);
1830
1831 sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie);
1832 status_t playStatus = NAME_NOT_FOUND;
1833 status_t recStatus = NAME_NOT_FOUND;
1834 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1835 playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event);
1836 if (playStatus == NO_ERROR) {
1837 return event;
1838 }
1839 }
1840 for (size_t i = 0; i < mRecordThreads.size(); i++) {
1841 recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event);
1842 if (recStatus == NO_ERROR) {
1843 return event;
1844 }
1845 }
1846 if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) {
1847 mPendingSyncEvents.add(event);
1848 } else {
1849 ALOGV("createSyncEvent() invalid event %d", event->type());
1850 event.clear();
1851 }
1852 return event;
1853}
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001854
Mathias Agopian65ab4712010-07-14 17:59:35 -07001855// ----------------------------------------------------------------------------
1856// Effect management
1857// ----------------------------------------------------------------------------
1858
1859
Glenn Kastenf587ba52012-01-26 16:25:10 -08001860status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001861{
1862 Mutex::Autolock _l(mLock);
1863 return EffectQueryNumberEffects(numEffects);
1864}
1865
Glenn Kastenf587ba52012-01-26 16:25:10 -08001866status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001867{
1868 Mutex::Autolock _l(mLock);
1869 return EffectQueryEffect(index, descriptor);
1870}
1871
Glenn Kasten5e92a782012-01-30 07:40:52 -08001872status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
Glenn Kastenf587ba52012-01-26 16:25:10 -08001873 effect_descriptor_t *descriptor) const
Mathias Agopian65ab4712010-07-14 17:59:35 -07001874{
1875 Mutex::Autolock _l(mLock);
1876 return EffectGetDescriptor(pUuid, descriptor);
1877}
1878
1879
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001880sp<IEffect> AudioFlinger::createEffect(
Mathias Agopian65ab4712010-07-14 17:59:35 -07001881 effect_descriptor_t *pDesc,
1882 const sp<IEffectClient>& effectClient,
1883 int32_t priority,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001884 audio_io_handle_t io,
Mathias Agopian65ab4712010-07-14 17:59:35 -07001885 int sessionId,
1886 status_t *status,
1887 int *id,
1888 int *enabled)
1889{
1890 status_t lStatus = NO_ERROR;
1891 sp<EffectHandle> handle;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001892 effect_descriptor_t desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001893
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001894 pid_t pid = IPCThreadState::self()->getCallingPid();
Glenn Kasten98ec94c2012-01-25 14:28:29 -08001895 ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001896 pid, effectClient.get(), priority, sessionId, io);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001897
1898 if (pDesc == NULL) {
1899 lStatus = BAD_VALUE;
1900 goto Exit;
1901 }
1902
Eric Laurent84e9a102010-09-23 16:10:16 -07001903 // check audio settings permission for global effects
Dima Zavinfce7a472011-04-19 22:30:36 -07001904 if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
Eric Laurent84e9a102010-09-23 16:10:16 -07001905 lStatus = PERMISSION_DENIED;
1906 goto Exit;
1907 }
1908
Dima Zavinfce7a472011-04-19 22:30:36 -07001909 // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
Eric Laurent84e9a102010-09-23 16:10:16 -07001910 // that can only be created by audio policy manager (running in same process)
Glenn Kasten44deb052012-02-05 18:09:08 -08001911 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
Eric Laurent84e9a102010-09-23 16:10:16 -07001912 lStatus = PERMISSION_DENIED;
1913 goto Exit;
1914 }
1915
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001916 if (io == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001917 if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurent84e9a102010-09-23 16:10:16 -07001918 // output must be specified by AudioPolicyManager when using session
Dima Zavinfce7a472011-04-19 22:30:36 -07001919 // AUDIO_SESSION_OUTPUT_STAGE
Eric Laurent84e9a102010-09-23 16:10:16 -07001920 lStatus = BAD_VALUE;
1921 goto Exit;
Dima Zavinfce7a472011-04-19 22:30:36 -07001922 } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
Eric Laurent84e9a102010-09-23 16:10:16 -07001923 // if the output returned by getOutputForEffect() is removed before we lock the
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001924 // mutex below, the call to checkPlaybackThread_l(io) below will detect it
Eric Laurent84e9a102010-09-23 16:10:16 -07001925 // and we will exit safely
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001926 io = AudioSystem::getOutputForEffect(&desc);
Eric Laurent84e9a102010-09-23 16:10:16 -07001927 }
1928 }
1929
Mathias Agopian65ab4712010-07-14 17:59:35 -07001930 {
1931 Mutex::Autolock _l(mLock);
1932
Mathias Agopian65ab4712010-07-14 17:59:35 -07001933
1934 if (!EffectIsNullUuid(&pDesc->uuid)) {
1935 // if uuid is specified, request effect descriptor
1936 lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
1937 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001938 ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001939 goto Exit;
1940 }
1941 } else {
1942 // if uuid is not specified, look for an available implementation
1943 // of the required type in effect factory
1944 if (EffectIsNullUuid(&pDesc->type)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001945 ALOGW("createEffect() no effect type");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001946 lStatus = BAD_VALUE;
1947 goto Exit;
1948 }
1949 uint32_t numEffects = 0;
1950 effect_descriptor_t d;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07001951 d.flags = 0; // prevent compiler warning
Mathias Agopian65ab4712010-07-14 17:59:35 -07001952 bool found = false;
1953
1954 lStatus = EffectQueryNumberEffects(&numEffects);
1955 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001956 ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001957 goto Exit;
1958 }
1959 for (uint32_t i = 0; i < numEffects; i++) {
1960 lStatus = EffectQueryEffect(i, &desc);
1961 if (lStatus < 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001962 ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001963 continue;
1964 }
1965 if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
1966 // If matching type found save effect descriptor. If the session is
1967 // 0 and the effect is not auxiliary, continue enumeration in case
1968 // an auxiliary version of this effect type is available
1969 found = true;
Glenn Kastena189a682012-02-20 12:16:30 -08001970 d = desc;
Dima Zavinfce7a472011-04-19 22:30:36 -07001971 if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
Mathias Agopian65ab4712010-07-14 17:59:35 -07001972 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1973 break;
1974 }
1975 }
1976 }
1977 if (!found) {
1978 lStatus = BAD_VALUE;
Steve Block5ff1dd52012-01-05 23:22:43 +00001979 ALOGW("createEffect() effect not found");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001980 goto Exit;
1981 }
1982 // For same effect type, chose auxiliary version over insert version if
1983 // connect to output mix (Compliance to OpenSL ES)
Dima Zavinfce7a472011-04-19 22:30:36 -07001984 if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07001985 (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Glenn Kastena189a682012-02-20 12:16:30 -08001986 desc = d;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001987 }
1988 }
1989
1990 // Do not allow auxiliary effects on a session different from 0 (output mix)
Dima Zavinfce7a472011-04-19 22:30:36 -07001991 if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
Mathias Agopian65ab4712010-07-14 17:59:35 -07001992 (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
1993 lStatus = INVALID_OPERATION;
1994 goto Exit;
1995 }
1996
Eric Laurent59255e42011-07-27 19:49:51 -07001997 // check recording permission for visualizer
1998 if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
1999 !recordingAllowed()) {
2000 lStatus = PERMISSION_DENIED;
2001 goto Exit;
2002 }
2003
Mathias Agopian65ab4712010-07-14 17:59:35 -07002004 // return effect descriptor
Glenn Kastena189a682012-02-20 12:16:30 -08002005 *pDesc = desc;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002006
2007 // If output is not specified try to find a matching audio session ID in one of the
2008 // output threads.
Eric Laurent84e9a102010-09-23 16:10:16 -07002009 // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
2010 // because of code checking output when entering the function.
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002011 // Note: io is never 0 when creating an effect on an input
2012 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002013 // look for the thread where the specified audio session is present
Eric Laurent84e9a102010-09-23 16:10:16 -07002014 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
2015 if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002016 io = mPlaybackThreads.keyAt(i);
Eric Laurent84e9a102010-09-23 16:10:16 -07002017 break;
Eric Laurent39e94f82010-07-28 01:32:47 -07002018 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002019 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002020 if (io == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002021 for (size_t i = 0; i < mRecordThreads.size(); i++) {
2022 if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
2023 io = mRecordThreads.keyAt(i);
2024 break;
2025 }
2026 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002027 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002028 // If no output thread contains the requested session ID, default to
2029 // first output. The effect chain will be moved to the correct output
2030 // thread when a track with the same session ID is created
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002031 if (io == 0 && mPlaybackThreads.size()) {
2032 io = mPlaybackThreads.keyAt(0);
2033 }
Steve Block3856b092011-10-20 11:56:00 +01002034 ALOGV("createEffect() got io %d for effect %s", io, desc.name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002035 }
2036 ThreadBase *thread = checkRecordThread_l(io);
2037 if (thread == NULL) {
2038 thread = checkPlaybackThread_l(io);
2039 if (thread == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00002040 ALOGE("createEffect() unknown output thread");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07002041 lStatus = BAD_VALUE;
2042 goto Exit;
Eric Laurent84e9a102010-09-23 16:10:16 -07002043 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002044 }
Eric Laurent84e9a102010-09-23 16:10:16 -07002045
Glenn Kasten98ec94c2012-01-25 14:28:29 -08002046 sp<Client> client = registerPid_l(pid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002047
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002048 // create effect on selected output thread
Eric Laurentde070132010-07-13 04:45:46 -07002049 handle = thread->createEffect_l(client, effectClient, priority, sessionId,
2050 &desc, enabled, &lStatus);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002051 if (handle != 0 && id != NULL) {
2052 *id = handle->id();
2053 }
2054 }
2055
2056Exit:
Glenn Kastene53b9ea2012-03-12 16:29:55 -07002057 if (status != NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002058 *status = lStatus;
2059 }
2060 return handle;
2061}
2062
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002063status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
2064 audio_io_handle_t dstOutput)
Eric Laurentde070132010-07-13 04:45:46 -07002065{
Steve Block3856b092011-10-20 11:56:00 +01002066 ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
Eric Laurent59255e42011-07-27 19:49:51 -07002067 sessionId, srcOutput, dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002068 Mutex::Autolock _l(mLock);
2069 if (srcOutput == dstOutput) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002070 ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002071 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002072 }
Eric Laurentde070132010-07-13 04:45:46 -07002073 PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
2074 if (srcThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002075 ALOGW("moveEffects() bad srcOutput %d", srcOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002076 return BAD_VALUE;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002077 }
Eric Laurentde070132010-07-13 04:45:46 -07002078 PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
2079 if (dstThread == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002080 ALOGW("moveEffects() bad dstOutput %d", dstOutput);
Eric Laurentde070132010-07-13 04:45:46 -07002081 return BAD_VALUE;
2082 }
2083
2084 Mutex::Autolock _dl(dstThread->mLock);
2085 Mutex::Autolock _sl(srcThread->mLock);
Eric Laurent59255e42011-07-27 19:49:51 -07002086 moveEffectChain_l(sessionId, srcThread, dstThread, false);
Eric Laurentde070132010-07-13 04:45:46 -07002087
Mathias Agopian65ab4712010-07-14 17:59:35 -07002088 return NO_ERROR;
2089}
2090
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002091// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
Eric Laurent59255e42011-07-27 19:49:51 -07002092status_t AudioFlinger::moveEffectChain_l(int sessionId,
Eric Laurentde070132010-07-13 04:45:46 -07002093 AudioFlinger::PlaybackThread *srcThread,
Eric Laurent39e94f82010-07-28 01:32:47 -07002094 AudioFlinger::PlaybackThread *dstThread,
2095 bool reRegister)
Eric Laurentde070132010-07-13 04:45:46 -07002096{
Steve Block3856b092011-10-20 11:56:00 +01002097 ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002098 sessionId, srcThread, dstThread);
Eric Laurentde070132010-07-13 04:45:46 -07002099
Eric Laurent59255e42011-07-27 19:49:51 -07002100 sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
Eric Laurentde070132010-07-13 04:45:46 -07002101 if (chain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002102 ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
Eric Laurent59255e42011-07-27 19:49:51 -07002103 sessionId, srcThread);
Eric Laurentde070132010-07-13 04:45:46 -07002104 return INVALID_OPERATION;
2105 }
2106
Eric Laurent39e94f82010-07-28 01:32:47 -07002107 // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
Eric Laurentde070132010-07-13 04:45:46 -07002108 // so that a new chain is created with correct parameters when first effect is added. This is
Eric Laurentec35a142011-10-05 17:42:25 -07002109 // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
Eric Laurentde070132010-07-13 04:45:46 -07002110 // removed.
2111 srcThread->removeEffectChain_l(chain);
2112
2113 // transfer all effects one by one so that new effect chain is created on new thread with
2114 // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
Glenn Kasten72ef00d2012-01-17 11:09:42 -08002115 audio_io_handle_t dstOutput = dstThread->id();
Eric Laurent39e94f82010-07-28 01:32:47 -07002116 sp<EffectChain> dstChain;
Marco Nelissen3a34bef2011-08-02 13:33:41 -07002117 uint32_t strategy = 0; // prevent compiler warning
Eric Laurentde070132010-07-13 04:45:46 -07002118 sp<EffectModule> effect = chain->getEffectFromId_l(0);
2119 while (effect != 0) {
2120 srcThread->removeEffect_l(effect);
2121 dstThread->addEffect_l(effect);
Eric Laurentec35a142011-10-05 17:42:25 -07002122 // removeEffect_l() has stopped the effect if it was active so it must be restarted
2123 if (effect->state() == EffectModule::ACTIVE ||
2124 effect->state() == EffectModule::STOPPING) {
2125 effect->start();
2126 }
Eric Laurent39e94f82010-07-28 01:32:47 -07002127 // if the move request is not received from audio policy manager, the effect must be
2128 // re-registered with the new strategy and output
2129 if (dstChain == 0) {
2130 dstChain = effect->chain().promote();
2131 if (dstChain == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002132 ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
Eric Laurent39e94f82010-07-28 01:32:47 -07002133 srcThread->addEffect_l(effect);
2134 return NO_INIT;
2135 }
2136 strategy = dstChain->strategy();
2137 }
2138 if (reRegister) {
2139 AudioSystem::unregisterEffect(effect->id());
2140 AudioSystem::registerEffect(&effect->desc(),
2141 dstOutput,
2142 strategy,
Eric Laurent59255e42011-07-27 19:49:51 -07002143 sessionId,
Eric Laurent39e94f82010-07-28 01:32:47 -07002144 effect->id());
2145 }
Eric Laurentde070132010-07-13 04:45:46 -07002146 effect = chain->getEffectFromId_l(0);
2147 }
2148
2149 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002150}
2151
Eric Laurent81784c32012-11-19 14:55:58 -08002152void AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id)
Mathias Agopian65ab4712010-07-14 17:59:35 -07002153{
Eric Laurent81784c32012-11-19 14:55:58 -08002154 NBAIO_Source *teeSource = source.get();
2155 if (teeSource != NULL) {
2156 char teeTime[16];
2157 struct timeval tv;
2158 gettimeofday(&tv, NULL);
2159 struct tm tm;
2160 localtime_r(&tv.tv_sec, &tm);
2161 strftime(teeTime, sizeof(teeTime), "%T", &tm);
2162 char teePath[64];
2163 sprintf(teePath, "/data/misc/media/%s_%d.wav", teeTime, id);
2164 int teeFd = open(teePath, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
2165 if (teeFd >= 0) {
2166 char wavHeader[44];
2167 memcpy(wavHeader,
2168 "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",
2169 sizeof(wavHeader));
2170 NBAIO_Format format = teeSource->format();
2171 unsigned channelCount = Format_channelCount(format);
2172 ALOG_ASSERT(channelCount <= FCC_2);
2173 uint32_t sampleRate = Format_sampleRate(format);
2174 wavHeader[22] = channelCount; // number of channels
2175 wavHeader[24] = sampleRate; // sample rate
2176 wavHeader[25] = sampleRate >> 8;
2177 wavHeader[32] = channelCount * 2; // block alignment
2178 write(teeFd, wavHeader, sizeof(wavHeader));
2179 size_t total = 0;
2180 bool firstRead = true;
2181 for (;;) {
2182#define TEE_SINK_READ 1024
2183 short buffer[TEE_SINK_READ * FCC_2];
2184 size_t count = TEE_SINK_READ;
2185 ssize_t actual = teeSource->read(buffer, count,
2186 AudioBufferProvider::kInvalidPTS);
2187 bool wasFirstRead = firstRead;
2188 firstRead = false;
2189 if (actual <= 0) {
2190 if (actual == (ssize_t) OVERRUN && wasFirstRead) {
2191 continue;
Eric Laurent59255e42011-07-27 19:49:51 -07002192 }
Eric Laurent81784c32012-11-19 14:55:58 -08002193 break;
Eric Laurent59255e42011-07-27 19:49:51 -07002194 }
Eric Laurent81784c32012-11-19 14:55:58 -08002195 ALOG_ASSERT(actual <= (ssize_t)count);
2196 write(teeFd, buffer, actual * channelCount * sizeof(short));
2197 total += actual;
Eric Laurent59255e42011-07-27 19:49:51 -07002198 }
Eric Laurent81784c32012-11-19 14:55:58 -08002199 lseek(teeFd, (off_t) 4, SEEK_SET);
2200 uint32_t temp = 44 + total * channelCount * sizeof(short) - 8;
2201 write(teeFd, &temp, sizeof(temp));
2202 lseek(teeFd, (off_t) 40, SEEK_SET);
2203 temp = total * channelCount * sizeof(short);
2204 write(teeFd, &temp, sizeof(temp));
2205 close(teeFd);
2206 fdprintf(fd, "FastMixer tee copied to %s\n", teePath);
Eric Laurent59255e42011-07-27 19:49:51 -07002207 } else {
Eric Laurent81784c32012-11-19 14:55:58 -08002208 fdprintf(fd, "FastMixer unable to create tee %s: \n", strerror(errno));
Eric Laurent59255e42011-07-27 19:49:51 -07002209 }
2210 }
2211}
2212
Mathias Agopian65ab4712010-07-14 17:59:35 -07002213// ----------------------------------------------------------------------------
2214
2215status_t AudioFlinger::onTransact(
2216 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2217{
2218 return BnAudioFlinger::onTransact(code, data, reply, flags);
2219}
2220
Mathias Agopian65ab4712010-07-14 17:59:35 -07002221}; // namespace android