blob: 918c25c530d09be0a010aaf111f6e04228487b16 [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 }
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100273 for (size_t k=0; k < mAudioCommands.size(); k++) {
274 delete mAudioCommands[k]->mParam;
275 delete mAudioCommands[k];
276 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700277 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800278 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700279}
280
281void AudioPolicyService::AudioCommandThread::onFirstRef()
282{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800283 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700284}
285
286bool AudioPolicyService::AudioCommandThread::threadLoop()
287{
288 nsecs_t waitTime = INT64_MAX;
289
290 mLock.lock();
291 while (!exitPending())
292 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700293 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700294 nsecs_t curTime = systemTime();
295 // commands are sorted by increasing time stamp: execute them from index 0 and up
296 if (mAudioCommands[0]->mTime <= curTime) {
297 AudioCommand *command = mAudioCommands[0];
298 mAudioCommands.removeAt(0);
299 mLastCommand = *command;
300
301 switch (command->mCommand) {
302 case START_TONE: {
303 mLock.unlock();
304 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100305 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700306 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800307 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700308 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
309 mpToneGenerator->startTone(data->mType);
310 delete data;
311 mLock.lock();
312 }break;
313 case STOP_TONE: {
314 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100315 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316 if (mpToneGenerator != NULL) {
317 mpToneGenerator->stopTone();
318 delete mpToneGenerator;
319 mpToneGenerator = NULL;
320 }
321 mLock.lock();
322 }break;
323 case SET_VOLUME: {
324 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100325 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700326 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
327 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
328 data->mVolume,
329 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700330 if (command->mWaitStatus) {
331 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100332 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700333 }
334 delete data;
335 }break;
336 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700337 ParametersData *data = (ParametersData *)command->mParam;
338 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
339 data->mKeyValuePairs.string(), data->mIO);
340 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
341 if (command->mWaitStatus) {
342 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100343 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700344 }
345 delete data;
346 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700347 case SET_VOICE_VOLUME: {
348 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100349 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700350 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
352 if (command->mWaitStatus) {
353 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100354 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700355 }
356 delete data;
357 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800358 case STOP_OUTPUT: {
359 StopOutputData *data = (StopOutputData *)command->mParam;
360 ALOGV("AudioCommandThread() processing stop output %d",
361 data->mIO);
362 sp<AudioPolicyService> svc = mService.promote();
363 if (svc == 0) {
364 break;
365 }
366 mLock.unlock();
367 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
368 mLock.lock();
369 delete data;
370 }break;
371 case RELEASE_OUTPUT: {
372 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam;
373 ALOGV("AudioCommandThread() processing release output %d",
374 data->mIO);
375 sp<AudioPolicyService> svc = mService.promote();
376 if (svc == 0) {
377 break;
378 }
379 mLock.unlock();
380 svc->doReleaseOutput(data->mIO);
381 mLock.lock();
382 delete data;
383 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000385 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700386 }
387 delete command;
388 waitTime = INT64_MAX;
389 } else {
390 waitTime = mAudioCommands[0]->mTime - curTime;
391 break;
392 }
393 }
394 // release delayed commands wake lock
Eric Laurentbfb1b832013-01-07 09:53:42 -0800395 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700396 release_wake_lock(mName.string());
397 }
Steve Block3856b092011-10-20 11:56:00 +0100398 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700399 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100400 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700401 }
402 mLock.unlock();
403 return false;
404}
405
406status_t AudioPolicyService::AudioCommandThread::dump(int fd)
407{
408 const size_t SIZE = 256;
409 char buffer[SIZE];
410 String8 result;
411
412 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
413 result.append(buffer);
414 write(fd, result.string(), result.size());
415
416 bool locked = tryLock(mLock);
417 if (!locked) {
418 String8 result2(kCmdDeadlockedString);
419 write(fd, result2.string(), result2.size());
420 }
421
422 snprintf(buffer, SIZE, "- Commands:\n");
423 result = String8(buffer);
424 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800425 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700426 mAudioCommands[i]->dump(buffer, SIZE);
427 result.append(buffer);
428 }
429 result.append(" Last Command\n");
430 mLastCommand.dump(buffer, SIZE);
431 result.append(buffer);
432
433 write(fd, result.string(), result.size());
434
435 if (locked) mLock.unlock();
436
437 return NO_ERROR;
438}
439
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800440void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
441 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700442{
443 AudioCommand *command = new AudioCommand();
444 command->mCommand = START_TONE;
445 ToneData *data = new ToneData();
446 data->mType = type;
447 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100448 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700449 Mutex::Autolock _l(mLock);
450 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100451 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700452 mWaitWorkCV.signal();
453}
454
455void AudioPolicyService::AudioCommandThread::stopToneCommand()
456{
457 AudioCommand *command = new AudioCommand();
458 command->mCommand = STOP_TONE;
459 command->mParam = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700460 Mutex::Autolock _l(mLock);
461 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100462 ALOGV("AudioCommandThread() adding tone stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700463 mWaitWorkCV.signal();
464}
465
Glenn Kastenfff6d712012-01-12 16:38:12 -0800466status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700467 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800468 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700469 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700470{
471 status_t status = NO_ERROR;
472
473 AudioCommand *command = new AudioCommand();
474 command->mCommand = SET_VOLUME;
475 VolumeData *data = new VolumeData();
476 data->mStream = stream;
477 data->mVolume = volume;
478 data->mIO = output;
479 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700480 Mutex::Autolock _l(mLock);
481 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100482 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700483 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700484 mWaitWorkCV.signal();
485 if (command->mWaitStatus) {
486 command->mCond.wait(mLock);
487 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100488 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700489 }
490 return status;
491}
492
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800493status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700494 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700495 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700496{
497 status_t status = NO_ERROR;
498
499 AudioCommand *command = new AudioCommand();
500 command->mCommand = SET_PARAMETERS;
501 ParametersData *data = new ParametersData();
502 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700503 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700504 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700505 Mutex::Autolock _l(mLock);
506 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100507 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700508 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700509 mWaitWorkCV.signal();
510 if (command->mWaitStatus) {
511 command->mCond.wait(mLock);
512 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100513 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700514 }
515 return status;
516}
517
518status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
519{
520 status_t status = NO_ERROR;
521
522 AudioCommand *command = new AudioCommand();
523 command->mCommand = SET_VOICE_VOLUME;
524 VoiceVolumeData *data = new VoiceVolumeData();
525 data->mVolume = volume;
526 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700527 Mutex::Autolock _l(mLock);
528 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100529 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700530 mWaitWorkCV.signal();
531 if (command->mWaitStatus) {
532 command->mCond.wait(mLock);
533 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100534 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700535 }
536 return status;
537}
538
Eric Laurentbfb1b832013-01-07 09:53:42 -0800539void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
540 audio_stream_type_t stream,
541 int session)
542{
543 AudioCommand *command = new AudioCommand();
544 command->mCommand = STOP_OUTPUT;
545 StopOutputData *data = new StopOutputData();
546 data->mIO = output;
547 data->mStream = stream;
548 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100549 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800550 Mutex::Autolock _l(mLock);
551 insertCommand_l(command);
552 ALOGV("AudioCommandThread() adding stop output %d", output);
553 mWaitWorkCV.signal();
554}
555
556void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
557{
558 AudioCommand *command = new AudioCommand();
559 command->mCommand = RELEASE_OUTPUT;
560 ReleaseOutputData *data = new ReleaseOutputData();
561 data->mIO = output;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100562 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800563 Mutex::Autolock _l(mLock);
564 insertCommand_l(command);
565 ALOGV("AudioCommandThread() adding release output %d", output);
566 mWaitWorkCV.signal();
567}
568
Mathias Agopian65ab4712010-07-14 17:59:35 -0700569// insertCommand_l() must be called with mLock held
570void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
571{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800572 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700573 Vector <AudioCommand *> removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700574 command->mTime = systemTime() + milliseconds(delayMs);
575
576 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800577 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700578 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
579 }
580
581 // check same pending commands with later time stamps and eliminate them
582 for (i = mAudioCommands.size()-1; i >= 0; i--) {
583 AudioCommand *command2 = mAudioCommands[i];
584 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
585 if (command2->mTime <= command->mTime) break;
586 if (command2->mCommand != command->mCommand) continue;
587
588 switch (command->mCommand) {
589 case SET_PARAMETERS: {
590 ParametersData *data = (ParametersData *)command->mParam;
591 ParametersData *data2 = (ParametersData *)command2->mParam;
592 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100593 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700594 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 AudioParameter param = AudioParameter(data->mKeyValuePairs);
596 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
597 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700598 String8 key;
599 String8 value;
600 param.getAt(j, key, value);
601 for (size_t k = 0; k < param2.size(); k++) {
602 String8 key2;
603 String8 value2;
604 param2.getAt(k, key2, value2);
605 if (key2 == key) {
606 param2.remove(key2);
607 ALOGV("Filtering out parameter %s", key2.string());
608 break;
609 }
610 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700611 }
612 // if all keys have been filtered out, remove the command.
613 // otherwise, update the key value pairs
614 if (param2.size() == 0) {
615 removedCommands.add(command2);
616 } else {
617 data2->mKeyValuePairs = param2.toString();
618 }
Eric Laurent21e54562013-09-23 12:08:05 -0700619 command->mTime = command2->mTime;
620 // force delayMs to non 0 so that code below does not request to wait for
621 // command status as the command is now delayed
622 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700623 } break;
624
625 case SET_VOLUME: {
626 VolumeData *data = (VolumeData *)command->mParam;
627 VolumeData *data2 = (VolumeData *)command2->mParam;
628 if (data->mIO != data2->mIO) break;
629 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100630 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700631 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700632 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700633 command->mTime = command2->mTime;
634 // force delayMs to non 0 so that code below does not request to wait for
635 // command status as the command is now delayed
636 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700637 } break;
638 case START_TONE:
639 case STOP_TONE:
640 default:
641 break;
642 }
643 }
644
645 // remove filtered commands
646 for (size_t j = 0; j < removedCommands.size(); j++) {
647 // removed commands always have time stamps greater than current command
648 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
649 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +0100650 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100651 // for commands that are not filtered,
652 // command->mParam is deleted in threadLoop
653 delete mAudioCommands[k]->mParam;
654 delete mAudioCommands[k];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700655 mAudioCommands.removeAt(k);
656 break;
657 }
658 }
659 }
660 removedCommands.clear();
661
Eric Laurent21e54562013-09-23 12:08:05 -0700662 // wait for status only if delay is 0
663 if (delayMs == 0) {
Eric Laurentcec4abb2012-07-03 12:23:02 -0700664 command->mWaitStatus = true;
665 } else {
666 command->mWaitStatus = false;
667 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700668
Mathias Agopian65ab4712010-07-14 17:59:35 -0700669 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +0100670 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -0700671 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672 mAudioCommands.insertAt(command, i + 1);
673}
674
675void AudioPolicyService::AudioCommandThread::exit()
676{
Steve Block3856b092011-10-20 11:56:00 +0100677 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678 {
679 AutoMutex _l(mLock);
680 requestExit();
681 mWaitWorkCV.signal();
682 }
683 requestExitAndWait();
684}
685
686void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
687{
688 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
689 mCommand,
690 (int)ns2s(mTime),
691 (int)ns2ms(mTime)%1000,
692 mWaitStatus,
693 mParam);
694}
695
Dima Zavinfce7a472011-04-19 22:30:36 -0700696/******* helpers for the service_ops callbacks defined below *********/
697void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
698 const char *keyValuePairs,
699 int delayMs)
700{
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800701 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -0700702 delayMs);
703}
704
705int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
706 float volume,
707 audio_io_handle_t output,
708 int delayMs)
709{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800710 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800711 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -0700712}
713
714int AudioPolicyService::startTone(audio_policy_tone_t tone,
715 audio_stream_type_t stream)
716{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700717 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +0000718 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700719 }
720 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +0000721 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700722 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700723 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700724 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
725 AUDIO_STREAM_VOICE_CALL);
726 return 0;
727}
728
729int AudioPolicyService::stopTone()
730{
731 mTonePlaybackThread->stopToneCommand();
732 return 0;
733}
734
735int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
736{
737 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
738}
739
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700740// ----------------------------------------------------------------------------
741// Audio pre-processing configuration
742// ----------------------------------------------------------------------------
743
Glenn Kasten8dad0e32012-01-09 08:41:22 -0800744/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700745 MIC_SRC_TAG,
746 VOICE_UL_SRC_TAG,
747 VOICE_DL_SRC_TAG,
748 VOICE_CALL_SRC_TAG,
749 CAMCORDER_SRC_TAG,
750 VOICE_REC_SRC_TAG,
751 VOICE_COMM_SRC_TAG
752};
753
754// returns the audio_source_t enum corresponding to the input source name or
755// AUDIO_SOURCE_CNT is no match found
756audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
757{
758 int i;
759 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
760 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100761 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700762 break;
763 }
764 }
765 return (audio_source_t)i;
766}
767
768size_t AudioPolicyService::growParamSize(char *param,
769 size_t size,
770 size_t *curSize,
771 size_t *totSize)
772{
773 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
774 size_t pos = ((*curSize - 1 ) / size + 1) * size;
775
776 if (pos + size > *totSize) {
777 while (pos + size > *totSize) {
778 *totSize += ((*totSize + 7) / 8) * 4;
779 }
780 param = (char *)realloc(param, *totSize);
781 }
782 *curSize = pos + size;
783 return pos;
784}
785
786size_t AudioPolicyService::readParamValue(cnode *node,
787 char *param,
788 size_t *curSize,
789 size_t *totSize)
790{
791 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
792 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
793 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100794 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700795 return sizeof(short);
796 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
797 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
798 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100799 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700800 return sizeof(int);
801 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
802 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
803 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100804 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700805 return sizeof(float);
806 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
807 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
808 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
809 *(bool *)((char *)param + pos) = false;
810 } else {
811 *(bool *)((char *)param + pos) = true;
812 }
Steve Block3856b092011-10-20 11:56:00 +0100813 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700814 return sizeof(bool);
815 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
816 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
817 if (*curSize + len + 1 > *totSize) {
818 *totSize = *curSize + len + 1;
819 param = (char *)realloc(param, *totSize);
820 }
821 strncpy(param + *curSize, node->value, len);
822 *curSize += len;
823 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +0100824 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700825 return len;
826 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000827 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700828 return 0;
829}
830
831effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
832{
833 cnode *param;
834 cnode *value;
835 size_t curSize = sizeof(effect_param_t);
836 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
837 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
838
839 param = config_find(root, PARAM_TAG);
840 value = config_find(root, VALUE_TAG);
841 if (param == NULL && value == NULL) {
842 // try to parse simple parameter form {int int}
843 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -0800844 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700845 // Note: that a pair of random strings is read as 0 0
846 int *ptr = (int *)fx_param->data;
847 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +0000848 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700849 *ptr++ = atoi(param->name);
850 *ptr = atoi(param->value);
851 fx_param->psize = sizeof(int);
852 fx_param->vsize = sizeof(int);
853 return fx_param;
854 }
855 }
856 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000857 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700858 goto error;
859 }
860
861 fx_param->psize = 0;
862 param = param->first_child;
863 while (param) {
Steve Block3856b092011-10-20 11:56:00 +0100864 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700865 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
866 if (size == 0) {
867 goto error;
868 }
869 fx_param->psize += size;
870 param = param->next;
871 }
872
873 // align start of value field on 32 bit boundary
874 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
875
876 fx_param->vsize = 0;
877 value = value->first_child;
878 while (value) {
Steve Block3856b092011-10-20 11:56:00 +0100879 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700880 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
881 if (size == 0) {
882 goto error;
883 }
884 fx_param->vsize += size;
885 value = value->next;
886 }
887
888 return fx_param;
889
890error:
You Kimdb358af2013-09-12 20:48:08 +0900891 free(fx_param);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700892 return NULL;
893}
894
895void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
896{
897 cnode *node = root->first_child;
898 while (node) {
Steve Block3856b092011-10-20 11:56:00 +0100899 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700900 effect_param_t *param = loadEffectParameter(node);
901 if (param == NULL) {
902 node = node->next;
903 continue;
904 }
905 params.add(param);
906 node = node->next;
907 }
908}
909
910AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
911 cnode *root,
912 const Vector <EffectDesc *>& effects)
913{
914 cnode *node = root->first_child;
915 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000916 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700917 return NULL;
918 }
919 InputSourceDesc *source = new InputSourceDesc();
920 while (node) {
921 size_t i;
922 for (i = 0; i < effects.size(); i++) {
923 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100924 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700925 break;
926 }
927 }
928 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +0100929 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700930 node = node->next;
931 continue;
932 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800933 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700934 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +0100935 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700936 source->mEffects.add(effect);
937 node = node->next;
938 }
939 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000940 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700941 delete source;
942 return NULL;
943 }
944 return source;
945}
946
947status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
948{
949 cnode *node = config_find(root, PREPROCESSING_TAG);
950 if (node == NULL) {
951 return -ENOENT;
952 }
953 node = node->first_child;
954 while (node) {
955 audio_source_t source = inputSourceNameToEnum(node->name);
956 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000957 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700958 node = node->next;
959 continue;
960 }
Steve Block3856b092011-10-20 11:56:00 +0100961 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700962 InputSourceDesc *desc = loadInputSource(node, effects);
963 if (desc == NULL) {
964 node = node->next;
965 continue;
966 }
967 mInputSources.add(source, desc);
968 node = node->next;
969 }
970 return NO_ERROR;
971}
972
973AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
974{
975 cnode *node = config_find(root, UUID_TAG);
976 if (node == NULL) {
977 return NULL;
978 }
979 effect_uuid_t uuid;
980 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000981 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700982 return NULL;
983 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800984 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700985}
986
987status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
988{
989 cnode *node = config_find(root, EFFECTS_TAG);
990 if (node == NULL) {
991 return -ENOENT;
992 }
993 node = node->first_child;
994 while (node) {
Steve Block3856b092011-10-20 11:56:00 +0100995 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700996 EffectDesc *effect = loadEffect(node);
997 if (effect == NULL) {
998 node = node->next;
999 continue;
1000 }
1001 effects.add(effect);
1002 node = node->next;
1003 }
1004 return NO_ERROR;
1005}
1006
1007status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1008{
1009 cnode *root;
1010 char *data;
1011
1012 data = (char *)load_file(path, NULL);
1013 if (data == NULL) {
1014 return -ENODEV;
1015 }
1016 root = config_node("", "");
1017 config_load(root, data);
1018
1019 Vector <EffectDesc *> effects;
1020 loadEffects(root, effects);
1021 loadInputSources(root, effects);
1022
Yu Yezhonge6056ba2013-10-15 14:32:34 +08001023 // delete effects to fix memory leak.
1024 // as effects is local var and valgrind would treat this as memory leak
1025 // and although it only did in mediaserver init, but free it in case mediaserver reboot
1026 size_t i;
1027 for (i = 0; i < effects.size(); i++) {
1028 delete effects[i];
1029 }
1030
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001031 config_free(root);
1032 free(root);
1033 free(data);
1034
1035 return NO_ERROR;
1036}
1037
Dima Zavinfce7a472011-04-19 22:30:36 -07001038extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001039audio_module_handle_t aps_load_hw_module(void *service __unused,
1040 const char *name);
1041audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001042 audio_devices_t *pDevices,
1043 uint32_t *pSamplingRate,
1044 audio_format_t *pFormat,
1045 audio_channel_mask_t *pChannelMask,
1046 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001047 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001048
Eric Laurent2d388ec2014-03-07 13:25:54 -08001049audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001050 audio_module_handle_t module,
1051 audio_devices_t *pDevices,
1052 uint32_t *pSamplingRate,
1053 audio_format_t *pFormat,
1054 audio_channel_mask_t *pChannelMask,
1055 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001056 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001057 const audio_offload_info_t *offloadInfo);
1058audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001059 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001060 audio_io_handle_t output2);
1061int aps_close_output(void *service __unused, audio_io_handle_t output);
1062int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1063int aps_restore_output(void *service __unused, audio_io_handle_t output);
1064audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001065 audio_devices_t *pDevices,
1066 uint32_t *pSamplingRate,
1067 audio_format_t *pFormat,
1068 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001069 audio_in_acoustics_t acoustics __unused);
1070audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001071 audio_module_handle_t module,
1072 audio_devices_t *pDevices,
1073 uint32_t *pSamplingRate,
1074 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001075 audio_channel_mask_t *pChannelMask);
1076int aps_close_input(void *service __unused, audio_io_handle_t input);
1077int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
1078int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001079 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001080 audio_io_handle_t dst_output);
1081char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1082 const char *keys);
1083void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1084 const char *kv_pairs, int delay_ms);
1085int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001086 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001087 int delay_ms);
1088int aps_start_tone(void *service, audio_policy_tone_t tone,
1089 audio_stream_type_t stream);
1090int aps_stop_tone(void *service);
1091int aps_set_voice_volume(void *service, float volume, int delay_ms);
1092};
Dima Zavinfce7a472011-04-19 22:30:36 -07001093
1094namespace {
1095 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001096 .open_output = aps_open_output,
1097 .open_duplicate_output = aps_open_dup_output,
1098 .close_output = aps_close_output,
1099 .suspend_output = aps_suspend_output,
1100 .restore_output = aps_restore_output,
1101 .open_input = aps_open_input,
1102 .close_input = aps_close_input,
1103 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -08001104 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001105 .set_parameters = aps_set_parameters,
1106 .get_parameters = aps_get_parameters,
1107 .start_tone = aps_start_tone,
1108 .stop_tone = aps_stop_tone,
1109 .set_voice_volume = aps_set_voice_volume,
1110 .move_effects = aps_move_effects,
1111 .load_hw_module = aps_load_hw_module,
1112 .open_output_on_module = aps_open_output_on_module,
1113 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001114 };
1115}; // namespace <unnamed>
1116
Mathias Agopian65ab4712010-07-14 17:59:35 -07001117}; // namespace android