blob: 2811475b4b70a8878f9f33d07e4f0192881d0990 [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
Glenn Kasten1b8ae3d2013-07-19 10:32:41 -070054static const nsecs_t kAudioCommandTimeout = 3000000000LL; // 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 }
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100271 for (size_t k=0; k < mAudioCommands.size(); k++) {
272 delete mAudioCommands[k]->mParam;
273 delete mAudioCommands[k];
274 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700275 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800276 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700277}
278
279void AudioPolicyService::AudioCommandThread::onFirstRef()
280{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800281 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700282}
283
284bool AudioPolicyService::AudioCommandThread::threadLoop()
285{
286 nsecs_t waitTime = INT64_MAX;
287
288 mLock.lock();
289 while (!exitPending())
290 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700291 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700292 nsecs_t curTime = systemTime();
293 // commands are sorted by increasing time stamp: execute them from index 0 and up
294 if (mAudioCommands[0]->mTime <= curTime) {
295 AudioCommand *command = mAudioCommands[0];
296 mAudioCommands.removeAt(0);
297 mLastCommand = *command;
298
299 switch (command->mCommand) {
300 case START_TONE: {
301 mLock.unlock();
302 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100303 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700304 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800305 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700306 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
307 mpToneGenerator->startTone(data->mType);
308 delete data;
309 mLock.lock();
310 }break;
311 case STOP_TONE: {
312 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100313 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700314 if (mpToneGenerator != NULL) {
315 mpToneGenerator->stopTone();
316 delete mpToneGenerator;
317 mpToneGenerator = NULL;
318 }
319 mLock.lock();
320 }break;
321 case SET_VOLUME: {
322 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100323 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700324 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
325 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
326 data->mVolume,
327 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700328 if (command->mWaitStatus) {
329 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100330 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700331 }
332 delete data;
333 }break;
334 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700335 ParametersData *data = (ParametersData *)command->mParam;
336 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
337 data->mKeyValuePairs.string(), data->mIO);
338 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
339 if (command->mWaitStatus) {
340 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100341 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700342 }
343 delete data;
344 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345 case SET_VOICE_VOLUME: {
346 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100347 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700348 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
350 if (command->mWaitStatus) {
351 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100352 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700353 }
354 delete data;
355 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800356 case STOP_OUTPUT: {
357 StopOutputData *data = (StopOutputData *)command->mParam;
358 ALOGV("AudioCommandThread() processing stop output %d",
359 data->mIO);
360 sp<AudioPolicyService> svc = mService.promote();
361 if (svc == 0) {
362 break;
363 }
364 mLock.unlock();
365 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
366 mLock.lock();
367 delete data;
368 }break;
369 case RELEASE_OUTPUT: {
370 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam;
371 ALOGV("AudioCommandThread() processing release output %d",
372 data->mIO);
373 sp<AudioPolicyService> svc = mService.promote();
374 if (svc == 0) {
375 break;
376 }
377 mLock.unlock();
378 svc->doReleaseOutput(data->mIO);
379 mLock.lock();
380 delete data;
381 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700382 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000383 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384 }
385 delete command;
386 waitTime = INT64_MAX;
387 } else {
388 waitTime = mAudioCommands[0]->mTime - curTime;
389 break;
390 }
391 }
392 // release delayed commands wake lock
Eric Laurentbfb1b832013-01-07 09:53:42 -0800393 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700394 release_wake_lock(mName.string());
395 }
Steve Block3856b092011-10-20 11:56:00 +0100396 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700397 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100398 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700399 }
400 mLock.unlock();
401 return false;
402}
403
404status_t AudioPolicyService::AudioCommandThread::dump(int fd)
405{
406 const size_t SIZE = 256;
407 char buffer[SIZE];
408 String8 result;
409
410 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
411 result.append(buffer);
412 write(fd, result.string(), result.size());
413
414 bool locked = tryLock(mLock);
415 if (!locked) {
416 String8 result2(kCmdDeadlockedString);
417 write(fd, result2.string(), result2.size());
418 }
419
420 snprintf(buffer, SIZE, "- Commands:\n");
421 result = String8(buffer);
422 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800423 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700424 mAudioCommands[i]->dump(buffer, SIZE);
425 result.append(buffer);
426 }
427 result.append(" Last Command\n");
428 mLastCommand.dump(buffer, SIZE);
429 result.append(buffer);
430
431 write(fd, result.string(), result.size());
432
433 if (locked) mLock.unlock();
434
435 return NO_ERROR;
436}
437
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800438void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
439 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700440{
441 AudioCommand *command = new AudioCommand();
442 command->mCommand = START_TONE;
443 ToneData *data = new ToneData();
444 data->mType = type;
445 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100446 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700447 Mutex::Autolock _l(mLock);
448 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100449 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700450 mWaitWorkCV.signal();
451}
452
453void AudioPolicyService::AudioCommandThread::stopToneCommand()
454{
455 AudioCommand *command = new AudioCommand();
456 command->mCommand = STOP_TONE;
457 command->mParam = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700458 Mutex::Autolock _l(mLock);
459 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100460 ALOGV("AudioCommandThread() adding tone stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700461 mWaitWorkCV.signal();
462}
463
Glenn Kastenfff6d712012-01-12 16:38:12 -0800464status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700465 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800466 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700467 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700468{
469 status_t status = NO_ERROR;
470
471 AudioCommand *command = new AudioCommand();
472 command->mCommand = SET_VOLUME;
473 VolumeData *data = new VolumeData();
474 data->mStream = stream;
475 data->mVolume = volume;
476 data->mIO = output;
477 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700478 Mutex::Autolock _l(mLock);
479 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100480 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700481 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700482 mWaitWorkCV.signal();
483 if (command->mWaitStatus) {
484 command->mCond.wait(mLock);
485 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100486 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700487 }
488 return status;
489}
490
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800491status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700492 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700493 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700494{
495 status_t status = NO_ERROR;
496
497 AudioCommand *command = new AudioCommand();
498 command->mCommand = SET_PARAMETERS;
499 ParametersData *data = new ParametersData();
500 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700501 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700502 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700503 Mutex::Autolock _l(mLock);
504 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100505 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700506 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700507 mWaitWorkCV.signal();
508 if (command->mWaitStatus) {
509 command->mCond.wait(mLock);
510 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100511 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700512 }
513 return status;
514}
515
516status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
517{
518 status_t status = NO_ERROR;
519
520 AudioCommand *command = new AudioCommand();
521 command->mCommand = SET_VOICE_VOLUME;
522 VoiceVolumeData *data = new VoiceVolumeData();
523 data->mVolume = volume;
524 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700525 Mutex::Autolock _l(mLock);
526 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100527 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700528 mWaitWorkCV.signal();
529 if (command->mWaitStatus) {
530 command->mCond.wait(mLock);
531 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100532 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700533 }
534 return status;
535}
536
Eric Laurentbfb1b832013-01-07 09:53:42 -0800537void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
538 audio_stream_type_t stream,
539 int session)
540{
541 AudioCommand *command = new AudioCommand();
542 command->mCommand = STOP_OUTPUT;
543 StopOutputData *data = new StopOutputData();
544 data->mIO = output;
545 data->mStream = stream;
546 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100547 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800548 Mutex::Autolock _l(mLock);
549 insertCommand_l(command);
550 ALOGV("AudioCommandThread() adding stop output %d", output);
551 mWaitWorkCV.signal();
552}
553
554void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
555{
556 AudioCommand *command = new AudioCommand();
557 command->mCommand = RELEASE_OUTPUT;
558 ReleaseOutputData *data = new ReleaseOutputData();
559 data->mIO = output;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100560 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800561 Mutex::Autolock _l(mLock);
562 insertCommand_l(command);
563 ALOGV("AudioCommandThread() adding release output %d", output);
564 mWaitWorkCV.signal();
565}
566
Mathias Agopian65ab4712010-07-14 17:59:35 -0700567// insertCommand_l() must be called with mLock held
568void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
569{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800570 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700571 Vector <AudioCommand *> removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700572 command->mTime = systemTime() + milliseconds(delayMs);
573
574 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800575 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700576 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
577 }
578
579 // check same pending commands with later time stamps and eliminate them
580 for (i = mAudioCommands.size()-1; i >= 0; i--) {
581 AudioCommand *command2 = mAudioCommands[i];
582 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
583 if (command2->mTime <= command->mTime) break;
584 if (command2->mCommand != command->mCommand) continue;
585
586 switch (command->mCommand) {
587 case SET_PARAMETERS: {
588 ParametersData *data = (ParametersData *)command->mParam;
589 ParametersData *data2 = (ParametersData *)command2->mParam;
590 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100591 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700592 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593 AudioParameter param = AudioParameter(data->mKeyValuePairs);
594 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
595 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700596 String8 key;
597 String8 value;
598 param.getAt(j, key, value);
599 for (size_t k = 0; k < param2.size(); k++) {
600 String8 key2;
601 String8 value2;
602 param2.getAt(k, key2, value2);
603 if (key2 == key) {
604 param2.remove(key2);
605 ALOGV("Filtering out parameter %s", key2.string());
606 break;
607 }
608 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700609 }
610 // if all keys have been filtered out, remove the command.
611 // otherwise, update the key value pairs
612 if (param2.size() == 0) {
613 removedCommands.add(command2);
614 } else {
615 data2->mKeyValuePairs = param2.toString();
616 }
Eric Laurent21e54562013-09-23 12:08:05 -0700617 command->mTime = command2->mTime;
618 // force delayMs to non 0 so that code below does not request to wait for
619 // command status as the command is now delayed
620 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700621 } break;
622
623 case SET_VOLUME: {
624 VolumeData *data = (VolumeData *)command->mParam;
625 VolumeData *data2 = (VolumeData *)command2->mParam;
626 if (data->mIO != data2->mIO) break;
627 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100628 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700629 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700630 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700631 command->mTime = command2->mTime;
632 // force delayMs to non 0 so that code below does not request to wait for
633 // command status as the command is now delayed
634 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635 } break;
636 case START_TONE:
637 case STOP_TONE:
638 default:
639 break;
640 }
641 }
642
643 // remove filtered commands
644 for (size_t j = 0; j < removedCommands.size(); j++) {
645 // removed commands always have time stamps greater than current command
646 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
647 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +0100648 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100649 // for commands that are not filtered,
650 // command->mParam is deleted in threadLoop
651 delete mAudioCommands[k]->mParam;
652 delete mAudioCommands[k];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700653 mAudioCommands.removeAt(k);
654 break;
655 }
656 }
657 }
658 removedCommands.clear();
659
Eric Laurent21e54562013-09-23 12:08:05 -0700660 // wait for status only if delay is 0
661 if (delayMs == 0) {
Eric Laurentcec4abb2012-07-03 12:23:02 -0700662 command->mWaitStatus = true;
663 } else {
664 command->mWaitStatus = false;
665 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700666
Mathias Agopian65ab4712010-07-14 17:59:35 -0700667 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +0100668 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -0700669 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700670 mAudioCommands.insertAt(command, i + 1);
671}
672
673void AudioPolicyService::AudioCommandThread::exit()
674{
Steve Block3856b092011-10-20 11:56:00 +0100675 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700676 {
677 AutoMutex _l(mLock);
678 requestExit();
679 mWaitWorkCV.signal();
680 }
681 requestExitAndWait();
682}
683
684void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
685{
686 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
687 mCommand,
688 (int)ns2s(mTime),
689 (int)ns2ms(mTime)%1000,
690 mWaitStatus,
691 mParam);
692}
693
Dima Zavinfce7a472011-04-19 22:30:36 -0700694/******* helpers for the service_ops callbacks defined below *********/
695void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
696 const char *keyValuePairs,
697 int delayMs)
698{
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800699 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -0700700 delayMs);
701}
702
703int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
704 float volume,
705 audio_io_handle_t output,
706 int delayMs)
707{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800708 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800709 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -0700710}
711
712int AudioPolicyService::startTone(audio_policy_tone_t tone,
713 audio_stream_type_t stream)
714{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700715 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +0000716 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700717 }
718 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +0000719 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700720 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700721 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700722 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
723 AUDIO_STREAM_VOICE_CALL);
724 return 0;
725}
726
727int AudioPolicyService::stopTone()
728{
729 mTonePlaybackThread->stopToneCommand();
730 return 0;
731}
732
733int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
734{
735 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
736}
737
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700738// ----------------------------------------------------------------------------
739// Audio pre-processing configuration
740// ----------------------------------------------------------------------------
741
Glenn Kasten8dad0e32012-01-09 08:41:22 -0800742/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700743 MIC_SRC_TAG,
744 VOICE_UL_SRC_TAG,
745 VOICE_DL_SRC_TAG,
746 VOICE_CALL_SRC_TAG,
747 CAMCORDER_SRC_TAG,
748 VOICE_REC_SRC_TAG,
749 VOICE_COMM_SRC_TAG
750};
751
752// returns the audio_source_t enum corresponding to the input source name or
753// AUDIO_SOURCE_CNT is no match found
754audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
755{
756 int i;
757 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
758 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100759 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700760 break;
761 }
762 }
763 return (audio_source_t)i;
764}
765
766size_t AudioPolicyService::growParamSize(char *param,
767 size_t size,
768 size_t *curSize,
769 size_t *totSize)
770{
771 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
772 size_t pos = ((*curSize - 1 ) / size + 1) * size;
773
774 if (pos + size > *totSize) {
775 while (pos + size > *totSize) {
776 *totSize += ((*totSize + 7) / 8) * 4;
777 }
778 param = (char *)realloc(param, *totSize);
779 }
780 *curSize = pos + size;
781 return pos;
782}
783
784size_t AudioPolicyService::readParamValue(cnode *node,
785 char *param,
786 size_t *curSize,
787 size_t *totSize)
788{
789 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
790 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
791 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100792 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700793 return sizeof(short);
794 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
795 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
796 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100797 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700798 return sizeof(int);
799 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
800 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
801 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100802 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700803 return sizeof(float);
804 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
805 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
806 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
807 *(bool *)((char *)param + pos) = false;
808 } else {
809 *(bool *)((char *)param + pos) = true;
810 }
Steve Block3856b092011-10-20 11:56:00 +0100811 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700812 return sizeof(bool);
813 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
814 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
815 if (*curSize + len + 1 > *totSize) {
816 *totSize = *curSize + len + 1;
817 param = (char *)realloc(param, *totSize);
818 }
819 strncpy(param + *curSize, node->value, len);
820 *curSize += len;
821 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +0100822 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700823 return len;
824 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000825 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700826 return 0;
827}
828
829effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
830{
831 cnode *param;
832 cnode *value;
833 size_t curSize = sizeof(effect_param_t);
834 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
835 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
836
837 param = config_find(root, PARAM_TAG);
838 value = config_find(root, VALUE_TAG);
839 if (param == NULL && value == NULL) {
840 // try to parse simple parameter form {int int}
841 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -0800842 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700843 // Note: that a pair of random strings is read as 0 0
844 int *ptr = (int *)fx_param->data;
845 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +0000846 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700847 *ptr++ = atoi(param->name);
848 *ptr = atoi(param->value);
849 fx_param->psize = sizeof(int);
850 fx_param->vsize = sizeof(int);
851 return fx_param;
852 }
853 }
854 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000855 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700856 goto error;
857 }
858
859 fx_param->psize = 0;
860 param = param->first_child;
861 while (param) {
Steve Block3856b092011-10-20 11:56:00 +0100862 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700863 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
864 if (size == 0) {
865 goto error;
866 }
867 fx_param->psize += size;
868 param = param->next;
869 }
870
871 // align start of value field on 32 bit boundary
872 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
873
874 fx_param->vsize = 0;
875 value = value->first_child;
876 while (value) {
Steve Block3856b092011-10-20 11:56:00 +0100877 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700878 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
879 if (size == 0) {
880 goto error;
881 }
882 fx_param->vsize += size;
883 value = value->next;
884 }
885
886 return fx_param;
887
888error:
You Kimdb358af2013-09-12 20:48:08 +0900889 free(fx_param);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700890 return NULL;
891}
892
893void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
894{
895 cnode *node = root->first_child;
896 while (node) {
Steve Block3856b092011-10-20 11:56:00 +0100897 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700898 effect_param_t *param = loadEffectParameter(node);
899 if (param == NULL) {
900 node = node->next;
901 continue;
902 }
903 params.add(param);
904 node = node->next;
905 }
906}
907
908AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
909 cnode *root,
910 const Vector <EffectDesc *>& effects)
911{
912 cnode *node = root->first_child;
913 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000914 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700915 return NULL;
916 }
917 InputSourceDesc *source = new InputSourceDesc();
918 while (node) {
919 size_t i;
920 for (i = 0; i < effects.size(); i++) {
921 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100922 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700923 break;
924 }
925 }
926 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +0100927 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700928 node = node->next;
929 continue;
930 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800931 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700932 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +0100933 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700934 source->mEffects.add(effect);
935 node = node->next;
936 }
937 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000938 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700939 delete source;
940 return NULL;
941 }
942 return source;
943}
944
945status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
946{
947 cnode *node = config_find(root, PREPROCESSING_TAG);
948 if (node == NULL) {
949 return -ENOENT;
950 }
951 node = node->first_child;
952 while (node) {
953 audio_source_t source = inputSourceNameToEnum(node->name);
954 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000955 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700956 node = node->next;
957 continue;
958 }
Steve Block3856b092011-10-20 11:56:00 +0100959 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700960 InputSourceDesc *desc = loadInputSource(node, effects);
961 if (desc == NULL) {
962 node = node->next;
963 continue;
964 }
965 mInputSources.add(source, desc);
966 node = node->next;
967 }
968 return NO_ERROR;
969}
970
971AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
972{
973 cnode *node = config_find(root, UUID_TAG);
974 if (node == NULL) {
975 return NULL;
976 }
977 effect_uuid_t uuid;
978 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000979 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700980 return NULL;
981 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800982 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700983}
984
985status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
986{
987 cnode *node = config_find(root, EFFECTS_TAG);
988 if (node == NULL) {
989 return -ENOENT;
990 }
991 node = node->first_child;
992 while (node) {
Steve Block3856b092011-10-20 11:56:00 +0100993 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700994 EffectDesc *effect = loadEffect(node);
995 if (effect == NULL) {
996 node = node->next;
997 continue;
998 }
999 effects.add(effect);
1000 node = node->next;
1001 }
1002 return NO_ERROR;
1003}
1004
1005status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1006{
1007 cnode *root;
1008 char *data;
1009
1010 data = (char *)load_file(path, NULL);
1011 if (data == NULL) {
1012 return -ENODEV;
1013 }
1014 root = config_node("", "");
1015 config_load(root, data);
1016
1017 Vector <EffectDesc *> effects;
1018 loadEffects(root, effects);
1019 loadInputSources(root, effects);
1020
Yu Yezhonge6056ba2013-10-15 14:32:34 +08001021 // delete effects to fix memory leak.
1022 // as effects is local var and valgrind would treat this as memory leak
1023 // and although it only did in mediaserver init, but free it in case mediaserver reboot
1024 size_t i;
1025 for (i = 0; i < effects.size(); i++) {
1026 delete effects[i];
1027 }
1028
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001029 config_free(root);
1030 free(root);
1031 free(data);
1032
1033 return NO_ERROR;
1034}
1035
Dima Zavinfce7a472011-04-19 22:30:36 -07001036extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001037audio_module_handle_t aps_load_hw_module(void *service __unused,
1038 const char *name);
1039audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001040 audio_devices_t *pDevices,
1041 uint32_t *pSamplingRate,
1042 audio_format_t *pFormat,
1043 audio_channel_mask_t *pChannelMask,
1044 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001045 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001046
Eric Laurent2d388ec2014-03-07 13:25:54 -08001047audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001048 audio_module_handle_t module,
1049 audio_devices_t *pDevices,
1050 uint32_t *pSamplingRate,
1051 audio_format_t *pFormat,
1052 audio_channel_mask_t *pChannelMask,
1053 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001054 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001055 const audio_offload_info_t *offloadInfo);
1056audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001057 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001058 audio_io_handle_t output2);
1059int aps_close_output(void *service __unused, audio_io_handle_t output);
1060int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1061int aps_restore_output(void *service __unused, audio_io_handle_t output);
1062audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001063 audio_devices_t *pDevices,
1064 uint32_t *pSamplingRate,
1065 audio_format_t *pFormat,
1066 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001067 audio_in_acoustics_t acoustics __unused);
1068audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001069 audio_module_handle_t module,
1070 audio_devices_t *pDevices,
1071 uint32_t *pSamplingRate,
1072 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001073 audio_channel_mask_t *pChannelMask);
1074int aps_close_input(void *service __unused, audio_io_handle_t input);
1075int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
1076int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001077 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001078 audio_io_handle_t dst_output);
1079char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1080 const char *keys);
1081void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1082 const char *kv_pairs, int delay_ms);
1083int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001084 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001085 int delay_ms);
1086int aps_start_tone(void *service, audio_policy_tone_t tone,
1087 audio_stream_type_t stream);
1088int aps_stop_tone(void *service);
1089int aps_set_voice_volume(void *service, float volume, int delay_ms);
1090};
Dima Zavinfce7a472011-04-19 22:30:36 -07001091
1092namespace {
1093 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001094 .open_output = aps_open_output,
1095 .open_duplicate_output = aps_open_dup_output,
1096 .close_output = aps_close_output,
1097 .suspend_output = aps_suspend_output,
1098 .restore_output = aps_restore_output,
1099 .open_input = aps_open_input,
1100 .close_input = aps_close_input,
1101 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -08001102 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001103 .set_parameters = aps_set_parameters,
1104 .get_parameters = aps_get_parameters,
1105 .start_tone = aps_start_tone,
1106 .stop_tone = aps_stop_tone,
1107 .set_voice_volume = aps_set_voice_volume,
1108 .move_effects = aps_move_effects,
1109 .load_hw_module = aps_load_hw_module,
1110 .open_output_on_module = aps_open_output_on_module,
1111 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001112 };
1113}; // namespace <unnamed>
1114
Mathias Agopian65ab4712010-07-14 17:59:35 -07001115}; // namespace android