blob: 037faa784da9a862928d6db705882ec28ea52411 [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();
125 mTonePlaybackThread.clear();
126 mAudioCommandThread->exit();
127 mAudioCommandThread.clear();
128
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700129
130 // release audio pre processing resources
131 for (size_t i = 0; i < mInputSources.size(); i++) {
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800132 delete mInputSources.valueAt(i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700133 }
134 mInputSources.clear();
135
136 for (size_t i = 0; i < mInputs.size(); i++) {
137 mInputs.valueAt(i)->mEffects.clear();
138 delete mInputs.valueAt(i);
139 }
140 mInputs.clear();
141
Eric Laurentdce54a12014-03-10 12:19:46 -0700142#ifdef USE_LEGACY_AUDIO_POLICY
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700143 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700144 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700145 }
146 if (mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700147 audio_policy_dev_close(mpAudioPolicyDev);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700148 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700149#else
150 delete mAudioPolicyManager;
151 delete mAudioPolicyClient;
152#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700153}
154
Eric Laurent57dae992011-07-24 13:36:09 -0700155
Mathias Agopian65ab4712010-07-14 17:59:35 -0700156void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700157 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700158 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700159}
160
161static bool tryLock(Mutex& mutex)
162{
163 bool locked = false;
164 for (int i = 0; i < kDumpLockRetries; ++i) {
165 if (mutex.tryLock() == NO_ERROR) {
166 locked = true;
167 break;
168 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800169 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700170 }
171 return locked;
172}
173
174status_t AudioPolicyService::dumpInternals(int fd)
175{
176 const size_t SIZE = 256;
177 char buffer[SIZE];
178 String8 result;
179
Eric Laurentdce54a12014-03-10 12:19:46 -0700180#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700181 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Eric Laurentdce54a12014-03-10 12:19:46 -0700182#else
183 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
184#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700185 result.append(buffer);
186 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
187 result.append(buffer);
188 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
189 result.append(buffer);
190
191 write(fd, result.string(), result.size());
192 return NO_ERROR;
193}
194
Glenn Kasten0f11b512014-01-31 16:18:54 -0800195status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700196{
Glenn Kasten44deb052012-02-05 18:09:08 -0800197 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700198 dumpPermissionDenial(fd);
199 } else {
200 bool locked = tryLock(mLock);
201 if (!locked) {
202 String8 result(kDeadlockedString);
203 write(fd, result.string(), result.size());
204 }
205
206 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800207 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700208 mAudioCommandThread->dump(fd);
209 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800210 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700211 mTonePlaybackThread->dump(fd);
212 }
213
Eric Laurentdce54a12014-03-10 12:19:46 -0700214#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700215 if (mpAudioPolicy) {
216 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700217 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700218#else
219 if (mAudioPolicyManager) {
220 mAudioPolicyManager->dump(fd);
221 }
222#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700223
224 if (locked) mLock.unlock();
225 }
226 return NO_ERROR;
227}
228
229status_t AudioPolicyService::dumpPermissionDenial(int fd)
230{
231 const size_t SIZE = 256;
232 char buffer[SIZE];
233 String8 result;
234 snprintf(buffer, SIZE, "Permission Denial: "
235 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
236 IPCThreadState::self()->getCallingPid(),
237 IPCThreadState::self()->getCallingUid());
238 result.append(buffer);
239 write(fd, result.string(), result.size());
240 return NO_ERROR;
241}
242
Glenn Kasten81872a22012-03-07 16:49:22 -0800243void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700244{
Glenn Kasten81872a22012-03-07 16:49:22 -0800245 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700246 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800247 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700248 }
249}
250
Mathias Agopian65ab4712010-07-14 17:59:35 -0700251status_t AudioPolicyService::onTransact(
252 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
253{
254 return BnAudioPolicyService::onTransact(code, data, reply, flags);
255}
256
257
Mathias Agopian65ab4712010-07-14 17:59:35 -0700258// ----------- AudioPolicyService::AudioCommandThread implementation ----------
259
Eric Laurentbfb1b832013-01-07 09:53:42 -0800260AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
261 const wp<AudioPolicyService>& service)
262 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700263{
264 mpToneGenerator = NULL;
265}
266
267
268AudioPolicyService::AudioCommandThread::~AudioCommandThread()
269{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800270 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700271 release_wake_lock(mName.string());
272 }
273 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800274 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700275}
276
277void AudioPolicyService::AudioCommandThread::onFirstRef()
278{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800279 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700280}
281
282bool AudioPolicyService::AudioCommandThread::threadLoop()
283{
284 nsecs_t waitTime = INT64_MAX;
285
286 mLock.lock();
287 while (!exitPending())
288 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700289 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700290 nsecs_t curTime = systemTime();
291 // commands are sorted by increasing time stamp: execute them from index 0 and up
292 if (mAudioCommands[0]->mTime <= curTime) {
293 AudioCommand *command = mAudioCommands[0];
294 mAudioCommands.removeAt(0);
295 mLastCommand = *command;
296
297 switch (command->mCommand) {
298 case START_TONE: {
299 mLock.unlock();
300 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100301 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700302 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800303 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700304 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
305 mpToneGenerator->startTone(data->mType);
306 delete data;
307 mLock.lock();
308 }break;
309 case STOP_TONE: {
310 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100311 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700312 if (mpToneGenerator != NULL) {
313 mpToneGenerator->stopTone();
314 delete mpToneGenerator;
315 mpToneGenerator = NULL;
316 }
317 mLock.lock();
318 }break;
319 case SET_VOLUME: {
320 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100321 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700322 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
323 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
324 data->mVolume,
325 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700326 if (command->mWaitStatus) {
327 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100328 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700329 }
330 delete data;
331 }break;
332 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700333 ParametersData *data = (ParametersData *)command->mParam;
334 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
335 data->mKeyValuePairs.string(), data->mIO);
336 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
337 if (command->mWaitStatus) {
338 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100339 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700340 }
341 delete data;
342 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700343 case SET_VOICE_VOLUME: {
344 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100345 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700346 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700347 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
348 if (command->mWaitStatus) {
349 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100350 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351 }
352 delete data;
353 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800354 case STOP_OUTPUT: {
355 StopOutputData *data = (StopOutputData *)command->mParam;
356 ALOGV("AudioCommandThread() processing stop output %d",
357 data->mIO);
358 sp<AudioPolicyService> svc = mService.promote();
359 if (svc == 0) {
360 break;
361 }
362 mLock.unlock();
363 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
364 mLock.lock();
365 delete data;
366 }break;
367 case RELEASE_OUTPUT: {
368 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam;
369 ALOGV("AudioCommandThread() processing release output %d",
370 data->mIO);
371 sp<AudioPolicyService> svc = mService.promote();
372 if (svc == 0) {
373 break;
374 }
375 mLock.unlock();
376 svc->doReleaseOutput(data->mIO);
377 mLock.lock();
378 delete data;
379 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700380 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000381 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700382 }
383 delete command;
384 waitTime = INT64_MAX;
385 } else {
386 waitTime = mAudioCommands[0]->mTime - curTime;
387 break;
388 }
389 }
390 // release delayed commands wake lock
Eric Laurentbfb1b832013-01-07 09:53:42 -0800391 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392 release_wake_lock(mName.string());
393 }
Steve Block3856b092011-10-20 11:56:00 +0100394 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700395 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100396 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700397 }
398 mLock.unlock();
399 return false;
400}
401
402status_t AudioPolicyService::AudioCommandThread::dump(int fd)
403{
404 const size_t SIZE = 256;
405 char buffer[SIZE];
406 String8 result;
407
408 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
409 result.append(buffer);
410 write(fd, result.string(), result.size());
411
412 bool locked = tryLock(mLock);
413 if (!locked) {
414 String8 result2(kCmdDeadlockedString);
415 write(fd, result2.string(), result2.size());
416 }
417
418 snprintf(buffer, SIZE, "- Commands:\n");
419 result = String8(buffer);
420 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800421 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700422 mAudioCommands[i]->dump(buffer, SIZE);
423 result.append(buffer);
424 }
425 result.append(" Last Command\n");
426 mLastCommand.dump(buffer, SIZE);
427 result.append(buffer);
428
429 write(fd, result.string(), result.size());
430
431 if (locked) mLock.unlock();
432
433 return NO_ERROR;
434}
435
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800436void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
437 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700438{
439 AudioCommand *command = new AudioCommand();
440 command->mCommand = START_TONE;
441 ToneData *data = new ToneData();
442 data->mType = type;
443 data->mStream = stream;
444 command->mParam = (void *)data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700445 Mutex::Autolock _l(mLock);
446 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100447 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700448 mWaitWorkCV.signal();
449}
450
451void AudioPolicyService::AudioCommandThread::stopToneCommand()
452{
453 AudioCommand *command = new AudioCommand();
454 command->mCommand = STOP_TONE;
455 command->mParam = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700456 Mutex::Autolock _l(mLock);
457 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100458 ALOGV("AudioCommandThread() adding tone stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700459 mWaitWorkCV.signal();
460}
461
Glenn Kastenfff6d712012-01-12 16:38:12 -0800462status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700463 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800464 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700465 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700466{
467 status_t status = NO_ERROR;
468
469 AudioCommand *command = new AudioCommand();
470 command->mCommand = SET_VOLUME;
471 VolumeData *data = new VolumeData();
472 data->mStream = stream;
473 data->mVolume = volume;
474 data->mIO = output;
475 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700476 Mutex::Autolock _l(mLock);
477 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100478 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700479 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700480 mWaitWorkCV.signal();
481 if (command->mWaitStatus) {
482 command->mCond.wait(mLock);
483 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100484 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700485 }
486 return status;
487}
488
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800489status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700490 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700491 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700492{
493 status_t status = NO_ERROR;
494
495 AudioCommand *command = new AudioCommand();
496 command->mCommand = SET_PARAMETERS;
497 ParametersData *data = new ParametersData();
498 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700499 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700500 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700501 Mutex::Autolock _l(mLock);
502 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100503 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700504 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700505 mWaitWorkCV.signal();
506 if (command->mWaitStatus) {
507 command->mCond.wait(mLock);
508 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100509 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700510 }
511 return status;
512}
513
514status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
515{
516 status_t status = NO_ERROR;
517
518 AudioCommand *command = new AudioCommand();
519 command->mCommand = SET_VOICE_VOLUME;
520 VoiceVolumeData *data = new VoiceVolumeData();
521 data->mVolume = volume;
522 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700523 Mutex::Autolock _l(mLock);
524 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100525 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700526 mWaitWorkCV.signal();
527 if (command->mWaitStatus) {
528 command->mCond.wait(mLock);
529 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100530 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700531 }
532 return status;
533}
534
Eric Laurentbfb1b832013-01-07 09:53:42 -0800535void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
536 audio_stream_type_t stream,
537 int session)
538{
539 AudioCommand *command = new AudioCommand();
540 command->mCommand = STOP_OUTPUT;
541 StopOutputData *data = new StopOutputData();
542 data->mIO = output;
543 data->mStream = stream;
544 data->mSession = session;
545 command->mParam = (void *)data;
546 Mutex::Autolock _l(mLock);
547 insertCommand_l(command);
548 ALOGV("AudioCommandThread() adding stop output %d", output);
549 mWaitWorkCV.signal();
550}
551
552void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
553{
554 AudioCommand *command = new AudioCommand();
555 command->mCommand = RELEASE_OUTPUT;
556 ReleaseOutputData *data = new ReleaseOutputData();
557 data->mIO = output;
558 command->mParam = (void *)data;
559 Mutex::Autolock _l(mLock);
560 insertCommand_l(command);
561 ALOGV("AudioCommandThread() adding release output %d", output);
562 mWaitWorkCV.signal();
563}
564
Mathias Agopian65ab4712010-07-14 17:59:35 -0700565// insertCommand_l() must be called with mLock held
566void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
567{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800568 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700569 Vector <AudioCommand *> removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700570 command->mTime = systemTime() + milliseconds(delayMs);
571
572 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800573 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700574 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
575 }
576
577 // check same pending commands with later time stamps and eliminate them
578 for (i = mAudioCommands.size()-1; i >= 0; i--) {
579 AudioCommand *command2 = mAudioCommands[i];
580 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
581 if (command2->mTime <= command->mTime) break;
582 if (command2->mCommand != command->mCommand) continue;
583
584 switch (command->mCommand) {
585 case SET_PARAMETERS: {
586 ParametersData *data = (ParametersData *)command->mParam;
587 ParametersData *data2 = (ParametersData *)command2->mParam;
588 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100589 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700590 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700591 AudioParameter param = AudioParameter(data->mKeyValuePairs);
592 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
593 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700594 String8 key;
595 String8 value;
596 param.getAt(j, key, value);
597 for (size_t k = 0; k < param2.size(); k++) {
598 String8 key2;
599 String8 value2;
600 param2.getAt(k, key2, value2);
601 if (key2 == key) {
602 param2.remove(key2);
603 ALOGV("Filtering out parameter %s", key2.string());
604 break;
605 }
606 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700607 }
608 // if all keys have been filtered out, remove the command.
609 // otherwise, update the key value pairs
610 if (param2.size() == 0) {
611 removedCommands.add(command2);
612 } else {
613 data2->mKeyValuePairs = param2.toString();
614 }
Eric Laurent21e54562013-09-23 12:08:05 -0700615 command->mTime = command2->mTime;
616 // force delayMs to non 0 so that code below does not request to wait for
617 // command status as the command is now delayed
618 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700619 } break;
620
621 case SET_VOLUME: {
622 VolumeData *data = (VolumeData *)command->mParam;
623 VolumeData *data2 = (VolumeData *)command2->mParam;
624 if (data->mIO != data2->mIO) break;
625 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100626 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700627 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700629 command->mTime = command2->mTime;
630 // force delayMs to non 0 so that code below does not request to wait for
631 // command status as the command is now delayed
632 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633 } break;
634 case START_TONE:
635 case STOP_TONE:
636 default:
637 break;
638 }
639 }
640
641 // remove filtered commands
642 for (size_t j = 0; j < removedCommands.size(); j++) {
643 // removed commands always have time stamps greater than current command
644 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
645 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +0100646 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700647 mAudioCommands.removeAt(k);
648 break;
649 }
650 }
651 }
652 removedCommands.clear();
653
Eric Laurent21e54562013-09-23 12:08:05 -0700654 // wait for status only if delay is 0
655 if (delayMs == 0) {
Eric Laurentcec4abb2012-07-03 12:23:02 -0700656 command->mWaitStatus = true;
657 } else {
658 command->mWaitStatus = false;
659 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700660
Mathias Agopian65ab4712010-07-14 17:59:35 -0700661 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +0100662 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -0700663 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700664 mAudioCommands.insertAt(command, i + 1);
665}
666
667void AudioPolicyService::AudioCommandThread::exit()
668{
Steve Block3856b092011-10-20 11:56:00 +0100669 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700670 {
671 AutoMutex _l(mLock);
672 requestExit();
673 mWaitWorkCV.signal();
674 }
675 requestExitAndWait();
676}
677
678void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
679{
680 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
681 mCommand,
682 (int)ns2s(mTime),
683 (int)ns2ms(mTime)%1000,
684 mWaitStatus,
685 mParam);
686}
687
Dima Zavinfce7a472011-04-19 22:30:36 -0700688/******* helpers for the service_ops callbacks defined below *********/
689void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
690 const char *keyValuePairs,
691 int delayMs)
692{
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800693 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -0700694 delayMs);
695}
696
697int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
698 float volume,
699 audio_io_handle_t output,
700 int delayMs)
701{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800702 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800703 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -0700704}
705
706int AudioPolicyService::startTone(audio_policy_tone_t tone,
707 audio_stream_type_t stream)
708{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700709 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +0000710 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700711 }
712 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +0000713 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700714 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700715 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700716 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
717 AUDIO_STREAM_VOICE_CALL);
718 return 0;
719}
720
721int AudioPolicyService::stopTone()
722{
723 mTonePlaybackThread->stopToneCommand();
724 return 0;
725}
726
727int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
728{
729 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
730}
731
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700732// ----------------------------------------------------------------------------
733// Audio pre-processing configuration
734// ----------------------------------------------------------------------------
735
Glenn Kasten8dad0e32012-01-09 08:41:22 -0800736/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700737 MIC_SRC_TAG,
738 VOICE_UL_SRC_TAG,
739 VOICE_DL_SRC_TAG,
740 VOICE_CALL_SRC_TAG,
741 CAMCORDER_SRC_TAG,
742 VOICE_REC_SRC_TAG,
743 VOICE_COMM_SRC_TAG
744};
745
746// returns the audio_source_t enum corresponding to the input source name or
747// AUDIO_SOURCE_CNT is no match found
748audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
749{
750 int i;
751 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
752 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100753 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700754 break;
755 }
756 }
757 return (audio_source_t)i;
758}
759
760size_t AudioPolicyService::growParamSize(char *param,
761 size_t size,
762 size_t *curSize,
763 size_t *totSize)
764{
765 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
766 size_t pos = ((*curSize - 1 ) / size + 1) * size;
767
768 if (pos + size > *totSize) {
769 while (pos + size > *totSize) {
770 *totSize += ((*totSize + 7) / 8) * 4;
771 }
772 param = (char *)realloc(param, *totSize);
773 }
774 *curSize = pos + size;
775 return pos;
776}
777
778size_t AudioPolicyService::readParamValue(cnode *node,
779 char *param,
780 size_t *curSize,
781 size_t *totSize)
782{
783 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
784 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
785 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100786 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700787 return sizeof(short);
788 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
789 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
790 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100791 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700792 return sizeof(int);
793 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
794 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
795 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100796 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700797 return sizeof(float);
798 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
799 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
800 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
801 *(bool *)((char *)param + pos) = false;
802 } else {
803 *(bool *)((char *)param + pos) = true;
804 }
Steve Block3856b092011-10-20 11:56:00 +0100805 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700806 return sizeof(bool);
807 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
808 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
809 if (*curSize + len + 1 > *totSize) {
810 *totSize = *curSize + len + 1;
811 param = (char *)realloc(param, *totSize);
812 }
813 strncpy(param + *curSize, node->value, len);
814 *curSize += len;
815 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +0100816 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700817 return len;
818 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000819 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700820 return 0;
821}
822
823effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
824{
825 cnode *param;
826 cnode *value;
827 size_t curSize = sizeof(effect_param_t);
828 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
829 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
830
831 param = config_find(root, PARAM_TAG);
832 value = config_find(root, VALUE_TAG);
833 if (param == NULL && value == NULL) {
834 // try to parse simple parameter form {int int}
835 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -0800836 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700837 // Note: that a pair of random strings is read as 0 0
838 int *ptr = (int *)fx_param->data;
839 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +0000840 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700841 *ptr++ = atoi(param->name);
842 *ptr = atoi(param->value);
843 fx_param->psize = sizeof(int);
844 fx_param->vsize = sizeof(int);
845 return fx_param;
846 }
847 }
848 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000849 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700850 goto error;
851 }
852
853 fx_param->psize = 0;
854 param = param->first_child;
855 while (param) {
Steve Block3856b092011-10-20 11:56:00 +0100856 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700857 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
858 if (size == 0) {
859 goto error;
860 }
861 fx_param->psize += size;
862 param = param->next;
863 }
864
865 // align start of value field on 32 bit boundary
866 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
867
868 fx_param->vsize = 0;
869 value = value->first_child;
870 while (value) {
Steve Block3856b092011-10-20 11:56:00 +0100871 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700872 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
873 if (size == 0) {
874 goto error;
875 }
876 fx_param->vsize += size;
877 value = value->next;
878 }
879
880 return fx_param;
881
882error:
883 delete fx_param;
884 return NULL;
885}
886
887void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
888{
889 cnode *node = root->first_child;
890 while (node) {
Steve Block3856b092011-10-20 11:56:00 +0100891 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700892 effect_param_t *param = loadEffectParameter(node);
893 if (param == NULL) {
894 node = node->next;
895 continue;
896 }
897 params.add(param);
898 node = node->next;
899 }
900}
901
902AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
903 cnode *root,
904 const Vector <EffectDesc *>& effects)
905{
906 cnode *node = root->first_child;
907 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000908 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700909 return NULL;
910 }
911 InputSourceDesc *source = new InputSourceDesc();
912 while (node) {
913 size_t i;
914 for (i = 0; i < effects.size(); i++) {
915 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100916 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700917 break;
918 }
919 }
920 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +0100921 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700922 node = node->next;
923 continue;
924 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800925 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700926 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +0100927 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700928 source->mEffects.add(effect);
929 node = node->next;
930 }
931 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000932 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700933 delete source;
934 return NULL;
935 }
936 return source;
937}
938
939status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
940{
941 cnode *node = config_find(root, PREPROCESSING_TAG);
942 if (node == NULL) {
943 return -ENOENT;
944 }
945 node = node->first_child;
946 while (node) {
947 audio_source_t source = inputSourceNameToEnum(node->name);
948 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000949 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700950 node = node->next;
951 continue;
952 }
Steve Block3856b092011-10-20 11:56:00 +0100953 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700954 InputSourceDesc *desc = loadInputSource(node, effects);
955 if (desc == NULL) {
956 node = node->next;
957 continue;
958 }
959 mInputSources.add(source, desc);
960 node = node->next;
961 }
962 return NO_ERROR;
963}
964
965AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
966{
967 cnode *node = config_find(root, UUID_TAG);
968 if (node == NULL) {
969 return NULL;
970 }
971 effect_uuid_t uuid;
972 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000973 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700974 return NULL;
975 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800976 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700977}
978
979status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
980{
981 cnode *node = config_find(root, EFFECTS_TAG);
982 if (node == NULL) {
983 return -ENOENT;
984 }
985 node = node->first_child;
986 while (node) {
Steve Block3856b092011-10-20 11:56:00 +0100987 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700988 EffectDesc *effect = loadEffect(node);
989 if (effect == NULL) {
990 node = node->next;
991 continue;
992 }
993 effects.add(effect);
994 node = node->next;
995 }
996 return NO_ERROR;
997}
998
999status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1000{
1001 cnode *root;
1002 char *data;
1003
1004 data = (char *)load_file(path, NULL);
1005 if (data == NULL) {
1006 return -ENODEV;
1007 }
1008 root = config_node("", "");
1009 config_load(root, data);
1010
1011 Vector <EffectDesc *> effects;
1012 loadEffects(root, effects);
1013 loadInputSources(root, effects);
1014
Yu Yezhonge6056ba2013-10-15 14:32:34 +08001015 // delete effects to fix memory leak.
1016 // as effects is local var and valgrind would treat this as memory leak
1017 // and although it only did in mediaserver init, but free it in case mediaserver reboot
1018 size_t i;
1019 for (i = 0; i < effects.size(); i++) {
1020 delete effects[i];
1021 }
1022
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001023 config_free(root);
1024 free(root);
1025 free(data);
1026
1027 return NO_ERROR;
1028}
1029
Dima Zavinfce7a472011-04-19 22:30:36 -07001030extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001031audio_module_handle_t aps_load_hw_module(void *service __unused,
1032 const char *name);
1033audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001034 audio_devices_t *pDevices,
1035 uint32_t *pSamplingRate,
1036 audio_format_t *pFormat,
1037 audio_channel_mask_t *pChannelMask,
1038 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001039 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001040
Eric Laurent2d388ec2014-03-07 13:25:54 -08001041audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001042 audio_module_handle_t module,
1043 audio_devices_t *pDevices,
1044 uint32_t *pSamplingRate,
1045 audio_format_t *pFormat,
1046 audio_channel_mask_t *pChannelMask,
1047 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001048 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001049 const audio_offload_info_t *offloadInfo);
1050audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001051 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001052 audio_io_handle_t output2);
1053int aps_close_output(void *service __unused, audio_io_handle_t output);
1054int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1055int aps_restore_output(void *service __unused, audio_io_handle_t output);
1056audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001057 audio_devices_t *pDevices,
1058 uint32_t *pSamplingRate,
1059 audio_format_t *pFormat,
1060 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001061 audio_in_acoustics_t acoustics __unused);
1062audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001063 audio_module_handle_t module,
1064 audio_devices_t *pDevices,
1065 uint32_t *pSamplingRate,
1066 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001067 audio_channel_mask_t *pChannelMask);
1068int aps_close_input(void *service __unused, audio_io_handle_t input);
1069int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
1070int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001071 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001072 audio_io_handle_t dst_output);
1073char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1074 const char *keys);
1075void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1076 const char *kv_pairs, int delay_ms);
1077int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001078 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001079 int delay_ms);
1080int aps_start_tone(void *service, audio_policy_tone_t tone,
1081 audio_stream_type_t stream);
1082int aps_stop_tone(void *service);
1083int aps_set_voice_volume(void *service, float volume, int delay_ms);
1084};
Dima Zavinfce7a472011-04-19 22:30:36 -07001085
1086namespace {
1087 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001088 .open_output = aps_open_output,
1089 .open_duplicate_output = aps_open_dup_output,
1090 .close_output = aps_close_output,
1091 .suspend_output = aps_suspend_output,
1092 .restore_output = aps_restore_output,
1093 .open_input = aps_open_input,
1094 .close_input = aps_close_input,
1095 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -08001096 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001097 .set_parameters = aps_set_parameters,
1098 .get_parameters = aps_get_parameters,
1099 .start_tone = aps_start_tone,
1100 .stop_tone = aps_stop_tone,
1101 .set_voice_volume = aps_set_voice_volume,
1102 .move_effects = aps_move_effects,
1103 .load_hw_module = aps_load_hw_module,
1104 .open_output_on_module = aps_open_output_on_module,
1105 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001106 };
1107}; // namespace <unnamed>
1108
Mathias Agopian65ab4712010-07-14 17:59:35 -07001109}; // namespace android