blob: 49145a522e2ef0b2bd573cf066178657b78e24d2 [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()
Glenn Kasten4944acb2013-08-19 08:39:20 -070063 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL)
Mathias Agopian65ab4712010-07-14 17:59:35 -070064{
65 char value[PROPERTY_VALUE_MAX];
Dima Zavinfce7a472011-04-19 22:30:36 -070066 const struct hw_module_t *module;
67 int forced_val;
68 int rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -070069
Eric Laurent93575202011-01-18 18:39:02 -080070 Mutex::Autolock _l(mLock);
71
Mathias Agopian65ab4712010-07-14 17:59:35 -070072 // start tone playback thread
Eric Laurentbfb1b832013-01-07 09:53:42 -080073 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
Mathias Agopian65ab4712010-07-14 17:59:35 -070074 // start audio commands thread
Eric Laurentbfb1b832013-01-07 09:53:42 -080075 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
76 // start output activity command thread
77 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Dima Zavinfce7a472011-04-19 22:30:36 -070078 /* instantiate the audio policy manager */
79 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070080 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070081 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070082 }
Mathias Agopian65ab4712010-07-14 17:59:35 -070083
Dima Zavinfce7a472011-04-19 22:30:36 -070084 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
Steve Block29357bc2012-01-06 19:20:56 +000085 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070086 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070087 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070088 }
Eric Laurent93575202011-01-18 18:39:02 -080089
Dima Zavinfce7a472011-04-19 22:30:36 -070090 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
91 &mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000092 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070093 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070094 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070095 }
Dima Zavinfce7a472011-04-19 22:30:36 -070096
97 rc = mpAudioPolicy->init_check(mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000098 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070099 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700100 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700101 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700102
Steve Blockdf64d152012-01-04 20:05:49 +0000103 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700104
105 // load audio pre processing modules
106 if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) {
107 loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE);
108 } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) {
109 loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE);
110 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700111}
112
113AudioPolicyService::~AudioPolicyService()
114{
115 mTonePlaybackThread->exit();
116 mTonePlaybackThread.clear();
117 mAudioCommandThread->exit();
118 mAudioCommandThread.clear();
119
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700120
121 // release audio pre processing resources
122 for (size_t i = 0; i < mInputSources.size(); i++) {
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800123 delete mInputSources.valueAt(i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700124 }
125 mInputSources.clear();
126
127 for (size_t i = 0; i < mInputs.size(); i++) {
128 mInputs.valueAt(i)->mEffects.clear();
129 delete mInputs.valueAt(i);
130 }
131 mInputs.clear();
132
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700133 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700134 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700135 }
136 if (mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700137 audio_policy_dev_close(mpAudioPolicyDev);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700138 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700139}
140
Eric Laurent57dae992011-07-24 13:36:09 -0700141
Mathias Agopian65ab4712010-07-14 17:59:35 -0700142void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700143 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700144 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700145}
146
147static bool tryLock(Mutex& mutex)
148{
149 bool locked = false;
150 for (int i = 0; i < kDumpLockRetries; ++i) {
151 if (mutex.tryLock() == NO_ERROR) {
152 locked = true;
153 break;
154 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800155 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700156 }
157 return locked;
158}
159
160status_t AudioPolicyService::dumpInternals(int fd)
161{
162 const size_t SIZE = 256;
163 char buffer[SIZE];
164 String8 result;
165
Dima Zavinfce7a472011-04-19 22:30:36 -0700166 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700167 result.append(buffer);
168 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
169 result.append(buffer);
170 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
171 result.append(buffer);
172
173 write(fd, result.string(), result.size());
174 return NO_ERROR;
175}
176
Glenn Kasten0f11b512014-01-31 16:18:54 -0800177status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700178{
Glenn Kasten44deb052012-02-05 18:09:08 -0800179 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700180 dumpPermissionDenial(fd);
181 } else {
182 bool locked = tryLock(mLock);
183 if (!locked) {
184 String8 result(kDeadlockedString);
185 write(fd, result.string(), result.size());
186 }
187
188 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800189 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700190 mAudioCommandThread->dump(fd);
191 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800192 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700193 mTonePlaybackThread->dump(fd);
194 }
195
Dima Zavinfce7a472011-04-19 22:30:36 -0700196 if (mpAudioPolicy) {
197 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700198 }
199
200 if (locked) mLock.unlock();
201 }
202 return NO_ERROR;
203}
204
205status_t AudioPolicyService::dumpPermissionDenial(int fd)
206{
207 const size_t SIZE = 256;
208 char buffer[SIZE];
209 String8 result;
210 snprintf(buffer, SIZE, "Permission Denial: "
211 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
212 IPCThreadState::self()->getCallingPid(),
213 IPCThreadState::self()->getCallingUid());
214 result.append(buffer);
215 write(fd, result.string(), result.size());
216 return NO_ERROR;
217}
218
Glenn Kasten81872a22012-03-07 16:49:22 -0800219void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700220{
Glenn Kasten81872a22012-03-07 16:49:22 -0800221 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700222 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800223 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700224 }
225}
226
Mathias Agopian65ab4712010-07-14 17:59:35 -0700227status_t AudioPolicyService::onTransact(
228 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
229{
230 return BnAudioPolicyService::onTransact(code, data, reply, flags);
231}
232
233
Mathias Agopian65ab4712010-07-14 17:59:35 -0700234// ----------- AudioPolicyService::AudioCommandThread implementation ----------
235
Eric Laurentbfb1b832013-01-07 09:53:42 -0800236AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
237 const wp<AudioPolicyService>& service)
238 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700239{
240 mpToneGenerator = NULL;
241}
242
243
244AudioPolicyService::AudioCommandThread::~AudioCommandThread()
245{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800246 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700247 release_wake_lock(mName.string());
248 }
249 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800250 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700251}
252
253void AudioPolicyService::AudioCommandThread::onFirstRef()
254{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800255 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700256}
257
258bool AudioPolicyService::AudioCommandThread::threadLoop()
259{
260 nsecs_t waitTime = INT64_MAX;
261
262 mLock.lock();
263 while (!exitPending())
264 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700265 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700266 nsecs_t curTime = systemTime();
267 // commands are sorted by increasing time stamp: execute them from index 0 and up
268 if (mAudioCommands[0]->mTime <= curTime) {
269 AudioCommand *command = mAudioCommands[0];
270 mAudioCommands.removeAt(0);
271 mLastCommand = *command;
272
273 switch (command->mCommand) {
274 case START_TONE: {
275 mLock.unlock();
276 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100277 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700278 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800279 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700280 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
281 mpToneGenerator->startTone(data->mType);
282 delete data;
283 mLock.lock();
284 }break;
285 case STOP_TONE: {
286 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100287 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700288 if (mpToneGenerator != NULL) {
289 mpToneGenerator->stopTone();
290 delete mpToneGenerator;
291 mpToneGenerator = NULL;
292 }
293 mLock.lock();
294 }break;
295 case SET_VOLUME: {
296 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100297 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700298 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
299 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
300 data->mVolume,
301 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700302 if (command->mWaitStatus) {
303 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100304 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305 }
306 delete data;
307 }break;
308 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700309 ParametersData *data = (ParametersData *)command->mParam;
310 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
311 data->mKeyValuePairs.string(), data->mIO);
312 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
313 if (command->mWaitStatus) {
314 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100315 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700316 }
317 delete data;
318 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700319 case SET_VOICE_VOLUME: {
320 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100321 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700322 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
324 if (command->mWaitStatus) {
325 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100326 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700327 }
328 delete data;
329 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800330 case STOP_OUTPUT: {
331 StopOutputData *data = (StopOutputData *)command->mParam;
332 ALOGV("AudioCommandThread() processing stop output %d",
333 data->mIO);
334 sp<AudioPolicyService> svc = mService.promote();
335 if (svc == 0) {
336 break;
337 }
338 mLock.unlock();
339 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
340 mLock.lock();
341 delete data;
342 }break;
343 case RELEASE_OUTPUT: {
344 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam;
345 ALOGV("AudioCommandThread() processing release output %d",
346 data->mIO);
347 sp<AudioPolicyService> svc = mService.promote();
348 if (svc == 0) {
349 break;
350 }
351 mLock.unlock();
352 svc->doReleaseOutput(data->mIO);
353 mLock.lock();
354 delete data;
355 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700356 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000357 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700358 }
359 delete command;
360 waitTime = INT64_MAX;
361 } else {
362 waitTime = mAudioCommands[0]->mTime - curTime;
363 break;
364 }
365 }
366 // release delayed commands wake lock
Eric Laurentbfb1b832013-01-07 09:53:42 -0800367 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700368 release_wake_lock(mName.string());
369 }
Steve Block3856b092011-10-20 11:56:00 +0100370 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700371 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100372 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373 }
374 mLock.unlock();
375 return false;
376}
377
378status_t AudioPolicyService::AudioCommandThread::dump(int fd)
379{
380 const size_t SIZE = 256;
381 char buffer[SIZE];
382 String8 result;
383
384 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
385 result.append(buffer);
386 write(fd, result.string(), result.size());
387
388 bool locked = tryLock(mLock);
389 if (!locked) {
390 String8 result2(kCmdDeadlockedString);
391 write(fd, result2.string(), result2.size());
392 }
393
394 snprintf(buffer, SIZE, "- Commands:\n");
395 result = String8(buffer);
396 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800397 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700398 mAudioCommands[i]->dump(buffer, SIZE);
399 result.append(buffer);
400 }
401 result.append(" Last Command\n");
402 mLastCommand.dump(buffer, SIZE);
403 result.append(buffer);
404
405 write(fd, result.string(), result.size());
406
407 if (locked) mLock.unlock();
408
409 return NO_ERROR;
410}
411
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800412void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
413 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700414{
415 AudioCommand *command = new AudioCommand();
416 command->mCommand = START_TONE;
417 ToneData *data = new ToneData();
418 data->mType = type;
419 data->mStream = stream;
420 command->mParam = (void *)data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700421 Mutex::Autolock _l(mLock);
422 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100423 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700424 mWaitWorkCV.signal();
425}
426
427void AudioPolicyService::AudioCommandThread::stopToneCommand()
428{
429 AudioCommand *command = new AudioCommand();
430 command->mCommand = STOP_TONE;
431 command->mParam = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700432 Mutex::Autolock _l(mLock);
433 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100434 ALOGV("AudioCommandThread() adding tone stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 mWaitWorkCV.signal();
436}
437
Glenn Kastenfff6d712012-01-12 16:38:12 -0800438status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700439 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800440 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700441 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700442{
443 status_t status = NO_ERROR;
444
445 AudioCommand *command = new AudioCommand();
446 command->mCommand = SET_VOLUME;
447 VolumeData *data = new VolumeData();
448 data->mStream = stream;
449 data->mVolume = volume;
450 data->mIO = output;
451 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700452 Mutex::Autolock _l(mLock);
453 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100454 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700455 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700456 mWaitWorkCV.signal();
457 if (command->mWaitStatus) {
458 command->mCond.wait(mLock);
459 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100460 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700461 }
462 return status;
463}
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{
469 status_t status = NO_ERROR;
470
471 AudioCommand *command = new AudioCommand();
472 command->mCommand = SET_PARAMETERS;
473 ParametersData *data = new ParametersData();
474 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700475 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700476 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700477 Mutex::Autolock _l(mLock);
478 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100479 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700480 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700481 mWaitWorkCV.signal();
482 if (command->mWaitStatus) {
483 command->mCond.wait(mLock);
484 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100485 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700486 }
487 return status;
488}
489
490status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
491{
492 status_t status = NO_ERROR;
493
494 AudioCommand *command = new AudioCommand();
495 command->mCommand = SET_VOICE_VOLUME;
496 VoiceVolumeData *data = new VoiceVolumeData();
497 data->mVolume = volume;
498 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700499 Mutex::Autolock _l(mLock);
500 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100501 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700502 mWaitWorkCV.signal();
503 if (command->mWaitStatus) {
504 command->mCond.wait(mLock);
505 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100506 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700507 }
508 return status;
509}
510
Eric Laurentbfb1b832013-01-07 09:53:42 -0800511void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
512 audio_stream_type_t stream,
513 int session)
514{
515 AudioCommand *command = new AudioCommand();
516 command->mCommand = STOP_OUTPUT;
517 StopOutputData *data = new StopOutputData();
518 data->mIO = output;
519 data->mStream = stream;
520 data->mSession = session;
521 command->mParam = (void *)data;
522 Mutex::Autolock _l(mLock);
523 insertCommand_l(command);
524 ALOGV("AudioCommandThread() adding stop output %d", output);
525 mWaitWorkCV.signal();
526}
527
528void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
529{
530 AudioCommand *command = new AudioCommand();
531 command->mCommand = RELEASE_OUTPUT;
532 ReleaseOutputData *data = new ReleaseOutputData();
533 data->mIO = output;
534 command->mParam = (void *)data;
535 Mutex::Autolock _l(mLock);
536 insertCommand_l(command);
537 ALOGV("AudioCommandThread() adding release output %d", output);
538 mWaitWorkCV.signal();
539}
540
Mathias Agopian65ab4712010-07-14 17:59:35 -0700541// insertCommand_l() must be called with mLock held
542void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
543{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800544 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700545 Vector <AudioCommand *> removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700546 command->mTime = systemTime() + milliseconds(delayMs);
547
548 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800549 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
551 }
552
553 // check same pending commands with later time stamps and eliminate them
554 for (i = mAudioCommands.size()-1; i >= 0; i--) {
555 AudioCommand *command2 = mAudioCommands[i];
556 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
557 if (command2->mTime <= command->mTime) break;
558 if (command2->mCommand != command->mCommand) continue;
559
560 switch (command->mCommand) {
561 case SET_PARAMETERS: {
562 ParametersData *data = (ParametersData *)command->mParam;
563 ParametersData *data2 = (ParametersData *)command2->mParam;
564 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100565 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700566 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700567 AudioParameter param = AudioParameter(data->mKeyValuePairs);
568 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
569 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700570 String8 key;
571 String8 value;
572 param.getAt(j, key, value);
573 for (size_t k = 0; k < param2.size(); k++) {
574 String8 key2;
575 String8 value2;
576 param2.getAt(k, key2, value2);
577 if (key2 == key) {
578 param2.remove(key2);
579 ALOGV("Filtering out parameter %s", key2.string());
580 break;
581 }
582 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700583 }
584 // if all keys have been filtered out, remove the command.
585 // otherwise, update the key value pairs
586 if (param2.size() == 0) {
587 removedCommands.add(command2);
588 } else {
589 data2->mKeyValuePairs = param2.toString();
590 }
Eric Laurent21e54562013-09-23 12:08:05 -0700591 command->mTime = command2->mTime;
592 // force delayMs to non 0 so that code below does not request to wait for
593 // command status as the command is now delayed
594 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 } break;
596
597 case SET_VOLUME: {
598 VolumeData *data = (VolumeData *)command->mParam;
599 VolumeData *data2 = (VolumeData *)command2->mParam;
600 if (data->mIO != data2->mIO) break;
601 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100602 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700603 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700604 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700605 command->mTime = command2->mTime;
606 // force delayMs to non 0 so that code below does not request to wait for
607 // command status as the command is now delayed
608 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700609 } break;
610 case START_TONE:
611 case STOP_TONE:
612 default:
613 break;
614 }
615 }
616
617 // remove filtered commands
618 for (size_t j = 0; j < removedCommands.size(); j++) {
619 // removed commands always have time stamps greater than current command
620 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
621 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +0100622 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700623 mAudioCommands.removeAt(k);
624 break;
625 }
626 }
627 }
628 removedCommands.clear();
629
Eric Laurent21e54562013-09-23 12:08:05 -0700630 // wait for status only if delay is 0
631 if (delayMs == 0) {
Eric Laurentcec4abb2012-07-03 12:23:02 -0700632 command->mWaitStatus = true;
633 } else {
634 command->mWaitStatus = false;
635 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700636
Mathias Agopian65ab4712010-07-14 17:59:35 -0700637 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +0100638 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -0700639 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700640 mAudioCommands.insertAt(command, i + 1);
641}
642
643void AudioPolicyService::AudioCommandThread::exit()
644{
Steve Block3856b092011-10-20 11:56:00 +0100645 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646 {
647 AutoMutex _l(mLock);
648 requestExit();
649 mWaitWorkCV.signal();
650 }
651 requestExitAndWait();
652}
653
654void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
655{
656 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
657 mCommand,
658 (int)ns2s(mTime),
659 (int)ns2ms(mTime)%1000,
660 mWaitStatus,
661 mParam);
662}
663
Dima Zavinfce7a472011-04-19 22:30:36 -0700664/******* helpers for the service_ops callbacks defined below *********/
665void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
666 const char *keyValuePairs,
667 int delayMs)
668{
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800669 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -0700670 delayMs);
671}
672
673int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
674 float volume,
675 audio_io_handle_t output,
676 int delayMs)
677{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800678 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800679 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -0700680}
681
682int AudioPolicyService::startTone(audio_policy_tone_t tone,
683 audio_stream_type_t stream)
684{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700685 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +0000686 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700687 }
688 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +0000689 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700690 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700691 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700692 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
693 AUDIO_STREAM_VOICE_CALL);
694 return 0;
695}
696
697int AudioPolicyService::stopTone()
698{
699 mTonePlaybackThread->stopToneCommand();
700 return 0;
701}
702
703int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
704{
705 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
706}
707
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700708// ----------------------------------------------------------------------------
709// Audio pre-processing configuration
710// ----------------------------------------------------------------------------
711
Glenn Kasten8dad0e32012-01-09 08:41:22 -0800712/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700713 MIC_SRC_TAG,
714 VOICE_UL_SRC_TAG,
715 VOICE_DL_SRC_TAG,
716 VOICE_CALL_SRC_TAG,
717 CAMCORDER_SRC_TAG,
718 VOICE_REC_SRC_TAG,
719 VOICE_COMM_SRC_TAG
720};
721
722// returns the audio_source_t enum corresponding to the input source name or
723// AUDIO_SOURCE_CNT is no match found
724audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
725{
726 int i;
727 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
728 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100729 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700730 break;
731 }
732 }
733 return (audio_source_t)i;
734}
735
736size_t AudioPolicyService::growParamSize(char *param,
737 size_t size,
738 size_t *curSize,
739 size_t *totSize)
740{
741 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
742 size_t pos = ((*curSize - 1 ) / size + 1) * size;
743
744 if (pos + size > *totSize) {
745 while (pos + size > *totSize) {
746 *totSize += ((*totSize + 7) / 8) * 4;
747 }
748 param = (char *)realloc(param, *totSize);
749 }
750 *curSize = pos + size;
751 return pos;
752}
753
754size_t AudioPolicyService::readParamValue(cnode *node,
755 char *param,
756 size_t *curSize,
757 size_t *totSize)
758{
759 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
760 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
761 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100762 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700763 return sizeof(short);
764 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
765 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
766 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100767 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700768 return sizeof(int);
769 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
770 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
771 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100772 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700773 return sizeof(float);
774 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
775 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
776 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
777 *(bool *)((char *)param + pos) = false;
778 } else {
779 *(bool *)((char *)param + pos) = true;
780 }
Steve Block3856b092011-10-20 11:56:00 +0100781 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700782 return sizeof(bool);
783 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
784 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
785 if (*curSize + len + 1 > *totSize) {
786 *totSize = *curSize + len + 1;
787 param = (char *)realloc(param, *totSize);
788 }
789 strncpy(param + *curSize, node->value, len);
790 *curSize += len;
791 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +0100792 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700793 return len;
794 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000795 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700796 return 0;
797}
798
799effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
800{
801 cnode *param;
802 cnode *value;
803 size_t curSize = sizeof(effect_param_t);
804 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
805 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
806
807 param = config_find(root, PARAM_TAG);
808 value = config_find(root, VALUE_TAG);
809 if (param == NULL && value == NULL) {
810 // try to parse simple parameter form {int int}
811 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -0800812 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700813 // Note: that a pair of random strings is read as 0 0
814 int *ptr = (int *)fx_param->data;
815 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +0000816 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700817 *ptr++ = atoi(param->name);
818 *ptr = atoi(param->value);
819 fx_param->psize = sizeof(int);
820 fx_param->vsize = sizeof(int);
821 return fx_param;
822 }
823 }
824 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000825 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700826 goto error;
827 }
828
829 fx_param->psize = 0;
830 param = param->first_child;
831 while (param) {
Steve Block3856b092011-10-20 11:56:00 +0100832 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700833 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
834 if (size == 0) {
835 goto error;
836 }
837 fx_param->psize += size;
838 param = param->next;
839 }
840
841 // align start of value field on 32 bit boundary
842 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
843
844 fx_param->vsize = 0;
845 value = value->first_child;
846 while (value) {
Steve Block3856b092011-10-20 11:56:00 +0100847 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700848 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
849 if (size == 0) {
850 goto error;
851 }
852 fx_param->vsize += size;
853 value = value->next;
854 }
855
856 return fx_param;
857
858error:
859 delete fx_param;
860 return NULL;
861}
862
863void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
864{
865 cnode *node = root->first_child;
866 while (node) {
Steve Block3856b092011-10-20 11:56:00 +0100867 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700868 effect_param_t *param = loadEffectParameter(node);
869 if (param == NULL) {
870 node = node->next;
871 continue;
872 }
873 params.add(param);
874 node = node->next;
875 }
876}
877
878AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
879 cnode *root,
880 const Vector <EffectDesc *>& effects)
881{
882 cnode *node = root->first_child;
883 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000884 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700885 return NULL;
886 }
887 InputSourceDesc *source = new InputSourceDesc();
888 while (node) {
889 size_t i;
890 for (i = 0; i < effects.size(); i++) {
891 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100892 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700893 break;
894 }
895 }
896 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +0100897 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700898 node = node->next;
899 continue;
900 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800901 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700902 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +0100903 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700904 source->mEffects.add(effect);
905 node = node->next;
906 }
907 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000908 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700909 delete source;
910 return NULL;
911 }
912 return source;
913}
914
915status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
916{
917 cnode *node = config_find(root, PREPROCESSING_TAG);
918 if (node == NULL) {
919 return -ENOENT;
920 }
921 node = node->first_child;
922 while (node) {
923 audio_source_t source = inputSourceNameToEnum(node->name);
924 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000925 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700926 node = node->next;
927 continue;
928 }
Steve Block3856b092011-10-20 11:56:00 +0100929 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700930 InputSourceDesc *desc = loadInputSource(node, effects);
931 if (desc == NULL) {
932 node = node->next;
933 continue;
934 }
935 mInputSources.add(source, desc);
936 node = node->next;
937 }
938 return NO_ERROR;
939}
940
941AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
942{
943 cnode *node = config_find(root, UUID_TAG);
944 if (node == NULL) {
945 return NULL;
946 }
947 effect_uuid_t uuid;
948 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000949 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700950 return NULL;
951 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800952 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700953}
954
955status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
956{
957 cnode *node = config_find(root, EFFECTS_TAG);
958 if (node == NULL) {
959 return -ENOENT;
960 }
961 node = node->first_child;
962 while (node) {
Steve Block3856b092011-10-20 11:56:00 +0100963 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700964 EffectDesc *effect = loadEffect(node);
965 if (effect == NULL) {
966 node = node->next;
967 continue;
968 }
969 effects.add(effect);
970 node = node->next;
971 }
972 return NO_ERROR;
973}
974
975status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
976{
977 cnode *root;
978 char *data;
979
980 data = (char *)load_file(path, NULL);
981 if (data == NULL) {
982 return -ENODEV;
983 }
984 root = config_node("", "");
985 config_load(root, data);
986
987 Vector <EffectDesc *> effects;
988 loadEffects(root, effects);
989 loadInputSources(root, effects);
990
Yu Yezhonge6056ba2013-10-15 14:32:34 +0800991 // delete effects to fix memory leak.
992 // as effects is local var and valgrind would treat this as memory leak
993 // and although it only did in mediaserver init, but free it in case mediaserver reboot
994 size_t i;
995 for (i = 0; i < effects.size(); i++) {
996 delete effects[i];
997 }
998
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700999 config_free(root);
1000 free(root);
1001 free(data);
1002
1003 return NO_ERROR;
1004}
1005
Dima Zavinfce7a472011-04-19 22:30:36 -07001006extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001007audio_module_handle_t aps_load_hw_module(void *service __unused,
1008 const char *name);
1009audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001010 audio_devices_t *pDevices,
1011 uint32_t *pSamplingRate,
1012 audio_format_t *pFormat,
1013 audio_channel_mask_t *pChannelMask,
1014 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001015 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001016
Eric Laurent2d388ec2014-03-07 13:25:54 -08001017audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001018 audio_module_handle_t module,
1019 audio_devices_t *pDevices,
1020 uint32_t *pSamplingRate,
1021 audio_format_t *pFormat,
1022 audio_channel_mask_t *pChannelMask,
1023 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001024 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001025 const audio_offload_info_t *offloadInfo);
1026audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001027 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001028 audio_io_handle_t output2);
1029int aps_close_output(void *service __unused, audio_io_handle_t output);
1030int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1031int aps_restore_output(void *service __unused, audio_io_handle_t output);
1032audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001033 audio_devices_t *pDevices,
1034 uint32_t *pSamplingRate,
1035 audio_format_t *pFormat,
1036 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001037 audio_in_acoustics_t acoustics __unused);
1038audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001039 audio_module_handle_t module,
1040 audio_devices_t *pDevices,
1041 uint32_t *pSamplingRate,
1042 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001043 audio_channel_mask_t *pChannelMask);
1044int aps_close_input(void *service __unused, audio_io_handle_t input);
1045int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
1046int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001047 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001048 audio_io_handle_t dst_output);
1049char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1050 const char *keys);
1051void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1052 const char *kv_pairs, int delay_ms);
1053int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001054 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001055 int delay_ms);
1056int aps_start_tone(void *service, audio_policy_tone_t tone,
1057 audio_stream_type_t stream);
1058int aps_stop_tone(void *service);
1059int aps_set_voice_volume(void *service, float volume, int delay_ms);
1060};
Dima Zavinfce7a472011-04-19 22:30:36 -07001061
1062namespace {
1063 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001064 .open_output = aps_open_output,
1065 .open_duplicate_output = aps_open_dup_output,
1066 .close_output = aps_close_output,
1067 .suspend_output = aps_suspend_output,
1068 .restore_output = aps_restore_output,
1069 .open_input = aps_open_input,
1070 .close_input = aps_close_input,
1071 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -08001072 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001073 .set_parameters = aps_set_parameters,
1074 .get_parameters = aps_get_parameters,
1075 .start_tone = aps_start_tone,
1076 .stop_tone = aps_stop_tone,
1077 .set_voice_volume = aps_set_voice_volume,
1078 .move_effects = aps_move_effects,
1079 .load_hw_module = aps_load_hw_module,
1080 .open_output_on_module = aps_open_output_on_module,
1081 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001082 };
1083}; // namespace <unnamed>
1084
Mathias Agopian65ab4712010-07-14 17:59:35 -07001085}; // namespace android