blob: 4e9a2f049d3aafdfb63e723d1eb4abd243fe9c3d [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioPolicyService"
18//#define LOG_NDEBUG 0
19
Glenn Kasten153b9fe2013-07-15 11:23:36 -070020#include "Configuration.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070021#undef __STRICT_ANSI__
22#define __STDINT_LIMITS
23#define __STDC_LIMIT_MACROS
24#include <stdint.h>
25
26#include <sys/time.h>
27#include <binder/IServiceManager.h>
28#include <utils/Log.h>
29#include <cutils/properties.h>
30#include <binder/IPCThreadState.h>
31#include <utils/String16.h>
32#include <utils/threads.h>
33#include "AudioPolicyService.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080034#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070036#include <media/AudioEffect.h>
37#include <media/EffectsFactoryApi.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070038
Dima Zavinfce7a472011-04-19 22:30:36 -070039#include <hardware/hardware.h>
Dima Zavin64760242011-05-11 14:15:23 -070040#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070041#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070042#include <hardware/audio_policy.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070043#include <audio_effects/audio_effects_conf.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070044#include <media/AudioParameter.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070045
Mathias Agopian65ab4712010-07-14 17:59:35 -070046namespace android {
47
Glenn Kasten8dad0e32012-01-09 08:41:22 -080048static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
49static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
51static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080052static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
Eric Laurent0ede8922014-05-09 18:04:42 -070054static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010055
Dima Zavinfce7a472011-04-19 22:30:36 -070056namespace {
57 extern struct audio_policy_service_ops aps_ops;
58};
59
Mathias Agopian65ab4712010-07-14 17:59:35 -070060// ----------------------------------------------------------------------------
61
62AudioPolicyService::AudioPolicyService()
Eric Laurentdce54a12014-03-10 12:19:46 -070063 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
64 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL)
Mathias Agopian65ab4712010-07-14 17:59:35 -070065{
66 char value[PROPERTY_VALUE_MAX];
Dima Zavinfce7a472011-04-19 22:30:36 -070067 const struct hw_module_t *module;
68 int forced_val;
69 int rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -070070
Eric Laurent93575202011-01-18 18:39:02 -080071 Mutex::Autolock _l(mLock);
72
Mathias Agopian65ab4712010-07-14 17:59:35 -070073 // start tone playback thread
Eric Laurentbfb1b832013-01-07 09:53:42 -080074 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
Mathias Agopian65ab4712010-07-14 17:59:35 -070075 // start audio commands thread
Eric Laurentbfb1b832013-01-07 09:53:42 -080076 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
77 // start output activity command thread
78 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070079
80#ifdef USE_LEGACY_AUDIO_POLICY
81 ALOGI("AudioPolicyService CSTOR in legacy mode");
82
Dima Zavinfce7a472011-04-19 22:30:36 -070083 /* instantiate the audio policy manager */
84 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070085 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070086 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070087 }
Dima Zavinfce7a472011-04-19 22:30:36 -070088 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
Steve Block29357bc2012-01-06 19:20:56 +000089 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070090 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070091 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070092 }
Eric Laurent93575202011-01-18 18:39:02 -080093
Dima Zavinfce7a472011-04-19 22:30:36 -070094 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
95 &mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000096 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070097 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070098 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070099 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700100
101 rc = mpAudioPolicy->init_check(mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +0000102 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700103 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700104 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700105 }
Steve Blockdf64d152012-01-04 20:05:49 +0000106 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurentdce54a12014-03-10 12:19:46 -0700107#else
108 ALOGI("AudioPolicyService CSTOR in new mode");
109
110 mAudioPolicyClient = new AudioPolicyClient(this);
111 mAudioPolicyManager = new AudioPolicyManager(mAudioPolicyClient);
112#endif
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700113
114 // load audio pre processing modules
115 if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) {
116 loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE);
117 } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) {
118 loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE);
119 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700120}
121
122AudioPolicyService::~AudioPolicyService()
123{
124 mTonePlaybackThread->exit();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700125 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -0700126 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700127
128 // release audio pre processing resources
129 for (size_t i = 0; i < mInputSources.size(); i++) {
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800130 delete mInputSources.valueAt(i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700131 }
132 mInputSources.clear();
133
134 for (size_t i = 0; i < mInputs.size(); i++) {
135 mInputs.valueAt(i)->mEffects.clear();
136 delete mInputs.valueAt(i);
137 }
138 mInputs.clear();
139
Eric Laurentdce54a12014-03-10 12:19:46 -0700140#ifdef USE_LEGACY_AUDIO_POLICY
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700141 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700142 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700143 }
144 if (mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700145 audio_policy_dev_close(mpAudioPolicyDev);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700146 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700147#else
148 delete mAudioPolicyManager;
149 delete mAudioPolicyClient;
150#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700151}
152
Eric Laurent57dae992011-07-24 13:36:09 -0700153
Mathias Agopian65ab4712010-07-14 17:59:35 -0700154void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700155 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700156 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700157}
158
159static bool tryLock(Mutex& mutex)
160{
161 bool locked = false;
162 for (int i = 0; i < kDumpLockRetries; ++i) {
163 if (mutex.tryLock() == NO_ERROR) {
164 locked = true;
165 break;
166 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800167 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700168 }
169 return locked;
170}
171
172status_t AudioPolicyService::dumpInternals(int fd)
173{
174 const size_t SIZE = 256;
175 char buffer[SIZE];
176 String8 result;
177
Eric Laurentdce54a12014-03-10 12:19:46 -0700178#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700179 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Eric Laurentdce54a12014-03-10 12:19:46 -0700180#else
181 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
182#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700183 result.append(buffer);
184 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
185 result.append(buffer);
186 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
187 result.append(buffer);
188
189 write(fd, result.string(), result.size());
190 return NO_ERROR;
191}
192
Glenn Kasten0f11b512014-01-31 16:18:54 -0800193status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700194{
Glenn Kasten44deb052012-02-05 18:09:08 -0800195 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700196 dumpPermissionDenial(fd);
197 } else {
198 bool locked = tryLock(mLock);
199 if (!locked) {
200 String8 result(kDeadlockedString);
201 write(fd, result.string(), result.size());
202 }
203
204 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800205 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700206 mAudioCommandThread->dump(fd);
207 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800208 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700209 mTonePlaybackThread->dump(fd);
210 }
211
Eric Laurentdce54a12014-03-10 12:19:46 -0700212#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700213 if (mpAudioPolicy) {
214 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700215 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700216#else
217 if (mAudioPolicyManager) {
218 mAudioPolicyManager->dump(fd);
219 }
220#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700221
222 if (locked) mLock.unlock();
223 }
224 return NO_ERROR;
225}
226
227status_t AudioPolicyService::dumpPermissionDenial(int fd)
228{
229 const size_t SIZE = 256;
230 char buffer[SIZE];
231 String8 result;
232 snprintf(buffer, SIZE, "Permission Denial: "
233 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
234 IPCThreadState::self()->getCallingPid(),
235 IPCThreadState::self()->getCallingUid());
236 result.append(buffer);
237 write(fd, result.string(), result.size());
238 return NO_ERROR;
239}
240
Glenn Kasten81872a22012-03-07 16:49:22 -0800241void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700242{
Glenn Kasten81872a22012-03-07 16:49:22 -0800243 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700244 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800245 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700246 }
247}
248
Mathias Agopian65ab4712010-07-14 17:59:35 -0700249status_t AudioPolicyService::onTransact(
250 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
251{
252 return BnAudioPolicyService::onTransact(code, data, reply, flags);
253}
254
255
Mathias Agopian65ab4712010-07-14 17:59:35 -0700256// ----------- AudioPolicyService::AudioCommandThread implementation ----------
257
Eric Laurentbfb1b832013-01-07 09:53:42 -0800258AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
259 const wp<AudioPolicyService>& service)
260 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700261{
262 mpToneGenerator = NULL;
263}
264
265
266AudioPolicyService::AudioCommandThread::~AudioCommandThread()
267{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800268 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700269 release_wake_lock(mName.string());
270 }
271 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800272 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700273}
274
275void AudioPolicyService::AudioCommandThread::onFirstRef()
276{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800277 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700278}
279
280bool AudioPolicyService::AudioCommandThread::threadLoop()
281{
282 nsecs_t waitTime = INT64_MAX;
283
284 mLock.lock();
285 while (!exitPending())
286 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700287 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700288 nsecs_t curTime = systemTime();
289 // commands are sorted by increasing time stamp: execute them from index 0 and up
290 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700291 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700292 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700293 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700294
295 switch (command->mCommand) {
296 case START_TONE: {
297 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700298 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100299 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800301 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700302 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
303 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700304 mLock.lock();
305 }break;
306 case STOP_TONE: {
307 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100308 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700309 if (mpToneGenerator != NULL) {
310 mpToneGenerator->stopTone();
311 delete mpToneGenerator;
312 mpToneGenerator = NULL;
313 }
314 mLock.lock();
315 }break;
316 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700317 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100318 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700319 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
320 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
321 data->mVolume,
322 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323 }break;
324 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700325 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700326 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
327 data->mKeyValuePairs.string(), data->mIO);
328 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700329 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700330 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700331 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100332 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700333 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700334 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800336 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700337 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800338 ALOGV("AudioCommandThread() processing stop output %d",
339 data->mIO);
340 sp<AudioPolicyService> svc = mService.promote();
341 if (svc == 0) {
342 break;
343 }
344 mLock.unlock();
345 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
346 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800347 }break;
348 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700349 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800350 ALOGV("AudioCommandThread() processing release output %d",
351 data->mIO);
352 sp<AudioPolicyService> svc = mService.promote();
353 if (svc == 0) {
354 break;
355 }
356 mLock.unlock();
357 svc->doReleaseOutput(data->mIO);
358 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800359 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000361 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700363 {
364 Mutex::Autolock _l(command->mLock);
365 if (command->mWaitStatus) {
366 command->mWaitStatus = false;
367 command->mCond.signal();
368 }
369 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370 waitTime = INT64_MAX;
371 } else {
372 waitTime = mAudioCommands[0]->mTime - curTime;
373 break;
374 }
375 }
376 // release delayed commands wake lock
Eric Laurentbfb1b832013-01-07 09:53:42 -0800377 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378 release_wake_lock(mName.string());
379 }
Steve Block3856b092011-10-20 11:56:00 +0100380 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700381 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100382 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700383 }
384 mLock.unlock();
385 return false;
386}
387
388status_t AudioPolicyService::AudioCommandThread::dump(int fd)
389{
390 const size_t SIZE = 256;
391 char buffer[SIZE];
392 String8 result;
393
394 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
395 result.append(buffer);
396 write(fd, result.string(), result.size());
397
398 bool locked = tryLock(mLock);
399 if (!locked) {
400 String8 result2(kCmdDeadlockedString);
401 write(fd, result2.string(), result2.size());
402 }
403
404 snprintf(buffer, SIZE, "- Commands:\n");
405 result = String8(buffer);
406 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800407 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700408 mAudioCommands[i]->dump(buffer, SIZE);
409 result.append(buffer);
410 }
411 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700412 if (mLastCommand != 0) {
413 mLastCommand->dump(buffer, SIZE);
414 result.append(buffer);
415 } else {
416 result.append(" none\n");
417 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700418
419 write(fd, result.string(), result.size());
420
421 if (locked) mLock.unlock();
422
423 return NO_ERROR;
424}
425
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800426void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
427 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700428{
Eric Laurent0ede8922014-05-09 18:04:42 -0700429 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700430 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700431 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700432 data->mType = type;
433 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100434 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100435 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700436 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700437}
438
439void AudioPolicyService::AudioCommandThread::stopToneCommand()
440{
Eric Laurent0ede8922014-05-09 18:04:42 -0700441 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700442 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100443 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700444 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700445}
446
Glenn Kastenfff6d712012-01-12 16:38:12 -0800447status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700448 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800449 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700450 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700451{
Eric Laurent0ede8922014-05-09 18:04:42 -0700452 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700453 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700454 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700455 data->mStream = stream;
456 data->mVolume = volume;
457 data->mIO = output;
458 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700459 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100460 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700461 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700462 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700463}
464
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800465status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700466 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700467 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700468{
Eric Laurent0ede8922014-05-09 18:04:42 -0700469 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700470 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700471 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700472 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700473 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700474 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700475 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100476 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700477 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700478 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700479}
480
481status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
482{
Eric Laurent0ede8922014-05-09 18:04:42 -0700483 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700484 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700485 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700486 data->mVolume = volume;
487 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700488 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100489 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700490 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700491}
492
Eric Laurentbfb1b832013-01-07 09:53:42 -0800493void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
494 audio_stream_type_t stream,
495 int session)
496{
Eric Laurent0ede8922014-05-09 18:04:42 -0700497 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800498 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700499 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800500 data->mIO = output;
501 data->mStream = stream;
502 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100503 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800504 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700505 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800506}
507
508void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
509{
Eric Laurent0ede8922014-05-09 18:04:42 -0700510 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800511 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700512 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800513 data->mIO = output;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100514 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800515 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700516 sendCommand(command);
517}
518
519status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
520{
521 {
522 Mutex::Autolock _l(mLock);
523 insertCommand_l(command, delayMs);
524 mWaitWorkCV.signal();
525 }
526 Mutex::Autolock _l(command->mLock);
527 while (command->mWaitStatus) {
528 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
529 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
530 command->mStatus = TIMED_OUT;
531 command->mWaitStatus = false;
532 }
533 }
534 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800535}
536
Mathias Agopian65ab4712010-07-14 17:59:35 -0700537// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -0700538void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700539{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800540 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -0700541 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700542 command->mTime = systemTime() + milliseconds(delayMs);
543
544 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800545 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700546 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
547 }
548
549 // check same pending commands with later time stamps and eliminate them
550 for (i = mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700551 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700552 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
553 if (command2->mTime <= command->mTime) break;
554 if (command2->mCommand != command->mCommand) continue;
555
556 switch (command->mCommand) {
557 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700558 ParametersData *data = (ParametersData *)command->mParam.get();
559 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700560 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100561 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700562 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700563 AudioParameter param = AudioParameter(data->mKeyValuePairs);
564 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
565 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700566 String8 key;
567 String8 value;
568 param.getAt(j, key, value);
569 for (size_t k = 0; k < param2.size(); k++) {
570 String8 key2;
571 String8 value2;
572 param2.getAt(k, key2, value2);
573 if (key2 == key) {
574 param2.remove(key2);
575 ALOGV("Filtering out parameter %s", key2.string());
576 break;
577 }
578 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700579 }
580 // if all keys have been filtered out, remove the command.
581 // otherwise, update the key value pairs
582 if (param2.size() == 0) {
583 removedCommands.add(command2);
584 } else {
585 data2->mKeyValuePairs = param2.toString();
586 }
Eric Laurent21e54562013-09-23 12:08:05 -0700587 command->mTime = command2->mTime;
588 // force delayMs to non 0 so that code below does not request to wait for
589 // command status as the command is now delayed
590 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700591 } break;
592
593 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700594 VolumeData *data = (VolumeData *)command->mParam.get();
595 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700596 if (data->mIO != data2->mIO) break;
597 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100598 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700599 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700600 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700601 command->mTime = command2->mTime;
602 // force delayMs to non 0 so that code below does not request to wait for
603 // command status as the command is now delayed
604 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700605 } break;
606 case START_TONE:
607 case STOP_TONE:
608 default:
609 break;
610 }
611 }
612
613 // remove filtered commands
614 for (size_t j = 0; j < removedCommands.size(); j++) {
615 // removed commands always have time stamps greater than current command
616 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700617 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +0100618 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700619 mAudioCommands.removeAt(k);
620 break;
621 }
622 }
623 }
624 removedCommands.clear();
625
Eric Laurent0ede8922014-05-09 18:04:42 -0700626 // Disable wait for status if delay is not 0
627 if (delayMs != 0) {
Eric Laurentcec4abb2012-07-03 12:23:02 -0700628 command->mWaitStatus = false;
629 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700630
Mathias Agopian65ab4712010-07-14 17:59:35 -0700631 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +0100632 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -0700633 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634 mAudioCommands.insertAt(command, i + 1);
635}
636
637void AudioPolicyService::AudioCommandThread::exit()
638{
Steve Block3856b092011-10-20 11:56:00 +0100639 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700640 {
641 AutoMutex _l(mLock);
642 requestExit();
643 mWaitWorkCV.signal();
644 }
645 requestExitAndWait();
646}
647
648void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
649{
650 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
651 mCommand,
652 (int)ns2s(mTime),
653 (int)ns2ms(mTime)%1000,
654 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -0700655 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700656}
657
Dima Zavinfce7a472011-04-19 22:30:36 -0700658/******* helpers for the service_ops callbacks defined below *********/
659void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
660 const char *keyValuePairs,
661 int delayMs)
662{
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800663 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -0700664 delayMs);
665}
666
667int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
668 float volume,
669 audio_io_handle_t output,
670 int delayMs)
671{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800672 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800673 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -0700674}
675
676int AudioPolicyService::startTone(audio_policy_tone_t tone,
677 audio_stream_type_t stream)
678{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700679 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +0000680 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700681 }
682 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +0000683 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700684 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700685 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700686 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
687 AUDIO_STREAM_VOICE_CALL);
688 return 0;
689}
690
691int AudioPolicyService::stopTone()
692{
693 mTonePlaybackThread->stopToneCommand();
694 return 0;
695}
696
697int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
698{
699 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
700}
701
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700702// ----------------------------------------------------------------------------
703// Audio pre-processing configuration
704// ----------------------------------------------------------------------------
705
Glenn Kasten8dad0e32012-01-09 08:41:22 -0800706/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700707 MIC_SRC_TAG,
708 VOICE_UL_SRC_TAG,
709 VOICE_DL_SRC_TAG,
710 VOICE_CALL_SRC_TAG,
711 CAMCORDER_SRC_TAG,
712 VOICE_REC_SRC_TAG,
713 VOICE_COMM_SRC_TAG
714};
715
716// returns the audio_source_t enum corresponding to the input source name or
717// AUDIO_SOURCE_CNT is no match found
718audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
719{
720 int i;
721 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
722 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100723 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700724 break;
725 }
726 }
727 return (audio_source_t)i;
728}
729
730size_t AudioPolicyService::growParamSize(char *param,
731 size_t size,
732 size_t *curSize,
733 size_t *totSize)
734{
735 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
736 size_t pos = ((*curSize - 1 ) / size + 1) * size;
737
738 if (pos + size > *totSize) {
739 while (pos + size > *totSize) {
740 *totSize += ((*totSize + 7) / 8) * 4;
741 }
742 param = (char *)realloc(param, *totSize);
743 }
744 *curSize = pos + size;
745 return pos;
746}
747
748size_t AudioPolicyService::readParamValue(cnode *node,
749 char *param,
750 size_t *curSize,
751 size_t *totSize)
752{
753 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
754 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
755 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100756 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700757 return sizeof(short);
758 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
759 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
760 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100761 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700762 return sizeof(int);
763 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
764 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
765 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100766 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700767 return sizeof(float);
768 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
769 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
770 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
771 *(bool *)((char *)param + pos) = false;
772 } else {
773 *(bool *)((char *)param + pos) = true;
774 }
Steve Block3856b092011-10-20 11:56:00 +0100775 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700776 return sizeof(bool);
777 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
778 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
779 if (*curSize + len + 1 > *totSize) {
780 *totSize = *curSize + len + 1;
781 param = (char *)realloc(param, *totSize);
782 }
783 strncpy(param + *curSize, node->value, len);
784 *curSize += len;
785 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +0100786 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700787 return len;
788 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000789 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700790 return 0;
791}
792
793effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
794{
795 cnode *param;
796 cnode *value;
797 size_t curSize = sizeof(effect_param_t);
798 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
799 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
800
801 param = config_find(root, PARAM_TAG);
802 value = config_find(root, VALUE_TAG);
803 if (param == NULL && value == NULL) {
804 // try to parse simple parameter form {int int}
805 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -0800806 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700807 // Note: that a pair of random strings is read as 0 0
808 int *ptr = (int *)fx_param->data;
809 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +0000810 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700811 *ptr++ = atoi(param->name);
812 *ptr = atoi(param->value);
813 fx_param->psize = sizeof(int);
814 fx_param->vsize = sizeof(int);
815 return fx_param;
816 }
817 }
818 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000819 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700820 goto error;
821 }
822
823 fx_param->psize = 0;
824 param = param->first_child;
825 while (param) {
Steve Block3856b092011-10-20 11:56:00 +0100826 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700827 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
828 if (size == 0) {
829 goto error;
830 }
831 fx_param->psize += size;
832 param = param->next;
833 }
834
835 // align start of value field on 32 bit boundary
836 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
837
838 fx_param->vsize = 0;
839 value = value->first_child;
840 while (value) {
Steve Block3856b092011-10-20 11:56:00 +0100841 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700842 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
843 if (size == 0) {
844 goto error;
845 }
846 fx_param->vsize += size;
847 value = value->next;
848 }
849
850 return fx_param;
851
852error:
You Kimdb358af2013-09-12 20:48:08 +0900853 free(fx_param);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700854 return NULL;
855}
856
857void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
858{
859 cnode *node = root->first_child;
860 while (node) {
Steve Block3856b092011-10-20 11:56:00 +0100861 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700862 effect_param_t *param = loadEffectParameter(node);
863 if (param == NULL) {
864 node = node->next;
865 continue;
866 }
867 params.add(param);
868 node = node->next;
869 }
870}
871
872AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
873 cnode *root,
874 const Vector <EffectDesc *>& effects)
875{
876 cnode *node = root->first_child;
877 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000878 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700879 return NULL;
880 }
881 InputSourceDesc *source = new InputSourceDesc();
882 while (node) {
883 size_t i;
884 for (i = 0; i < effects.size(); i++) {
885 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100886 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700887 break;
888 }
889 }
890 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +0100891 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700892 node = node->next;
893 continue;
894 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800895 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700896 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +0100897 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700898 source->mEffects.add(effect);
899 node = node->next;
900 }
901 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000902 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700903 delete source;
904 return NULL;
905 }
906 return source;
907}
908
909status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
910{
911 cnode *node = config_find(root, PREPROCESSING_TAG);
912 if (node == NULL) {
913 return -ENOENT;
914 }
915 node = node->first_child;
916 while (node) {
917 audio_source_t source = inputSourceNameToEnum(node->name);
918 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000919 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700920 node = node->next;
921 continue;
922 }
Steve Block3856b092011-10-20 11:56:00 +0100923 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700924 InputSourceDesc *desc = loadInputSource(node, effects);
925 if (desc == NULL) {
926 node = node->next;
927 continue;
928 }
929 mInputSources.add(source, desc);
930 node = node->next;
931 }
932 return NO_ERROR;
933}
934
935AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
936{
937 cnode *node = config_find(root, UUID_TAG);
938 if (node == NULL) {
939 return NULL;
940 }
941 effect_uuid_t uuid;
942 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000943 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700944 return NULL;
945 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800946 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700947}
948
949status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
950{
951 cnode *node = config_find(root, EFFECTS_TAG);
952 if (node == NULL) {
953 return -ENOENT;
954 }
955 node = node->first_child;
956 while (node) {
Steve Block3856b092011-10-20 11:56:00 +0100957 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700958 EffectDesc *effect = loadEffect(node);
959 if (effect == NULL) {
960 node = node->next;
961 continue;
962 }
963 effects.add(effect);
964 node = node->next;
965 }
966 return NO_ERROR;
967}
968
969status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
970{
971 cnode *root;
972 char *data;
973
974 data = (char *)load_file(path, NULL);
975 if (data == NULL) {
976 return -ENODEV;
977 }
978 root = config_node("", "");
979 config_load(root, data);
980
981 Vector <EffectDesc *> effects;
982 loadEffects(root, effects);
983 loadInputSources(root, effects);
984
Yu Yezhonge6056ba2013-10-15 14:32:34 +0800985 // delete effects to fix memory leak.
986 // as effects is local var and valgrind would treat this as memory leak
987 // and although it only did in mediaserver init, but free it in case mediaserver reboot
988 size_t i;
989 for (i = 0; i < effects.size(); i++) {
990 delete effects[i];
991 }
992
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700993 config_free(root);
994 free(root);
995 free(data);
996
997 return NO_ERROR;
998}
999
Dima Zavinfce7a472011-04-19 22:30:36 -07001000extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001001audio_module_handle_t aps_load_hw_module(void *service __unused,
1002 const char *name);
1003audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001004 audio_devices_t *pDevices,
1005 uint32_t *pSamplingRate,
1006 audio_format_t *pFormat,
1007 audio_channel_mask_t *pChannelMask,
1008 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001009 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001010
Eric Laurent2d388ec2014-03-07 13:25:54 -08001011audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001012 audio_module_handle_t module,
1013 audio_devices_t *pDevices,
1014 uint32_t *pSamplingRate,
1015 audio_format_t *pFormat,
1016 audio_channel_mask_t *pChannelMask,
1017 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001018 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001019 const audio_offload_info_t *offloadInfo);
1020audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001021 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001022 audio_io_handle_t output2);
1023int aps_close_output(void *service __unused, audio_io_handle_t output);
1024int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1025int aps_restore_output(void *service __unused, audio_io_handle_t output);
1026audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001027 audio_devices_t *pDevices,
1028 uint32_t *pSamplingRate,
1029 audio_format_t *pFormat,
1030 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001031 audio_in_acoustics_t acoustics __unused);
1032audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001033 audio_module_handle_t module,
1034 audio_devices_t *pDevices,
1035 uint32_t *pSamplingRate,
1036 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001037 audio_channel_mask_t *pChannelMask);
1038int aps_close_input(void *service __unused, audio_io_handle_t input);
1039int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
1040int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001041 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001042 audio_io_handle_t dst_output);
1043char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1044 const char *keys);
1045void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1046 const char *kv_pairs, int delay_ms);
1047int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001048 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001049 int delay_ms);
1050int aps_start_tone(void *service, audio_policy_tone_t tone,
1051 audio_stream_type_t stream);
1052int aps_stop_tone(void *service);
1053int aps_set_voice_volume(void *service, float volume, int delay_ms);
1054};
Dima Zavinfce7a472011-04-19 22:30:36 -07001055
1056namespace {
1057 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001058 .open_output = aps_open_output,
1059 .open_duplicate_output = aps_open_dup_output,
1060 .close_output = aps_close_output,
1061 .suspend_output = aps_suspend_output,
1062 .restore_output = aps_restore_output,
1063 .open_input = aps_open_input,
1064 .close_input = aps_close_input,
1065 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -08001066 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001067 .set_parameters = aps_set_parameters,
1068 .get_parameters = aps_get_parameters,
1069 .start_tone = aps_start_tone,
1070 .stop_tone = aps_stop_tone,
1071 .set_voice_volume = aps_set_voice_volume,
1072 .move_effects = aps_move_effects,
1073 .load_hw_module = aps_load_hw_module,
1074 .open_output_on_module = aps_open_output_on_module,
1075 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001076 };
1077}; // namespace <unnamed>
1078
Mathias Agopian65ab4712010-07-14 17:59:35 -07001079}; // namespace android