blob: ea573a476200e395ca5418e75ff3c65972b61796 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioPolicyService"
18//#define LOG_NDEBUG 0
19
Glenn Kasten153b9fe2013-07-15 11:23:36 -070020#include "Configuration.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070021#undef __STRICT_ANSI__
22#define __STDINT_LIMITS
23#define __STDC_LIMIT_MACROS
24#include <stdint.h>
25
26#include <sys/time.h>
27#include <binder/IServiceManager.h>
28#include <utils/Log.h>
29#include <cutils/properties.h>
30#include <binder/IPCThreadState.h>
31#include <utils/String16.h>
32#include <utils/threads.h>
33#include "AudioPolicyService.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080034#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070036#include <media/AudioEffect.h>
37#include <media/EffectsFactoryApi.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070038
Dima Zavinfce7a472011-04-19 22:30:36 -070039#include <hardware/hardware.h>
Dima Zavin64760242011-05-11 14:15:23 -070040#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070041#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070042#include <hardware/audio_policy.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070043#include <audio_effects/audio_effects_conf.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070044#include <media/AudioParameter.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070045
Mathias Agopian65ab4712010-07-14 17:59:35 -070046namespace android {
47
Glenn Kasten8dad0e32012-01-09 08:41:22 -080048static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
49static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
51static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080052static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
Eric Laurent0ede8922014-05-09 18:04:42 -070054static const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010055
Dima Zavinfce7a472011-04-19 22:30:36 -070056namespace {
57 extern struct audio_policy_service_ops aps_ops;
58};
59
Mathias Agopian65ab4712010-07-14 17:59:35 -070060// ----------------------------------------------------------------------------
61
62AudioPolicyService::AudioPolicyService()
Eric Laurentdce54a12014-03-10 12:19:46 -070063 : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
64 mAudioPolicyManager(NULL), mAudioPolicyClient(NULL)
Mathias Agopian65ab4712010-07-14 17:59:35 -070065{
66 char value[PROPERTY_VALUE_MAX];
Dima Zavinfce7a472011-04-19 22:30:36 -070067 const struct hw_module_t *module;
68 int forced_val;
69 int rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -070070
Eric Laurent93575202011-01-18 18:39:02 -080071 Mutex::Autolock _l(mLock);
72
Mathias Agopian65ab4712010-07-14 17:59:35 -070073 // start tone playback thread
Eric Laurentbfb1b832013-01-07 09:53:42 -080074 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
Mathias Agopian65ab4712010-07-14 17:59:35 -070075 // start audio commands thread
Eric Laurentbfb1b832013-01-07 09:53:42 -080076 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
77 // start output activity command thread
78 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Eric Laurentdce54a12014-03-10 12:19:46 -070079
80#ifdef USE_LEGACY_AUDIO_POLICY
81 ALOGI("AudioPolicyService CSTOR in legacy mode");
82
Dima Zavinfce7a472011-04-19 22:30:36 -070083 /* instantiate the audio policy manager */
84 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070085 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070086 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070087 }
Dima Zavinfce7a472011-04-19 22:30:36 -070088 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
Steve Block29357bc2012-01-06 19:20:56 +000089 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070090 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070091 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070092 }
Eric Laurent93575202011-01-18 18:39:02 -080093
Dima Zavinfce7a472011-04-19 22:30:36 -070094 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
95 &mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000096 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070097 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070098 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070099 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700100
101 rc = mpAudioPolicy->init_check(mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +0000102 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700103 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700104 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700105 }
Steve Blockdf64d152012-01-04 20:05:49 +0000106 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurentdce54a12014-03-10 12:19:46 -0700107#else
108 ALOGI("AudioPolicyService CSTOR in new mode");
109
110 mAudioPolicyClient = new AudioPolicyClient(this);
111 mAudioPolicyManager = new AudioPolicyManager(mAudioPolicyClient);
112#endif
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700113
114 // load audio pre processing modules
115 if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) {
116 loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE);
117 } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) {
118 loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE);
119 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700120}
121
122AudioPolicyService::~AudioPolicyService()
123{
124 mTonePlaybackThread->exit();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700125 mAudioCommandThread->exit();
Eric Laurent657ff612014-05-07 11:58:24 -0700126 mOutputCommandThread->exit();
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700127
128 // release audio pre processing resources
129 for (size_t i = 0; i < mInputSources.size(); i++) {
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800130 delete mInputSources.valueAt(i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700131 }
132 mInputSources.clear();
133
134 for (size_t i = 0; i < mInputs.size(); i++) {
135 mInputs.valueAt(i)->mEffects.clear();
136 delete mInputs.valueAt(i);
137 }
138 mInputs.clear();
139
Eric Laurentdce54a12014-03-10 12:19:46 -0700140#ifdef USE_LEGACY_AUDIO_POLICY
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700141 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700142 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700143 }
144 if (mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700145 audio_policy_dev_close(mpAudioPolicyDev);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700146 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700147#else
148 delete mAudioPolicyManager;
149 delete mAudioPolicyClient;
150#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700151}
152
Eric Laurent951f4552014-05-20 10:48:17 -0700153status_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
154 audio_patch_handle_t *handle,
155 int delayMs)
156{
157 return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
158}
159
160status_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
161 int delayMs)
162{
163 return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
164}
165
Eric Laurent57dae992011-07-24 13:36:09 -0700166
Mathias Agopian65ab4712010-07-14 17:59:35 -0700167void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700168 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700169 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700170}
171
172static bool tryLock(Mutex& mutex)
173{
174 bool locked = false;
175 for (int i = 0; i < kDumpLockRetries; ++i) {
176 if (mutex.tryLock() == NO_ERROR) {
177 locked = true;
178 break;
179 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800180 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700181 }
182 return locked;
183}
184
185status_t AudioPolicyService::dumpInternals(int fd)
186{
187 const size_t SIZE = 256;
188 char buffer[SIZE];
189 String8 result;
190
Eric Laurentdce54a12014-03-10 12:19:46 -0700191#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700192 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Eric Laurentdce54a12014-03-10 12:19:46 -0700193#else
194 snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
195#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700196 result.append(buffer);
197 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
198 result.append(buffer);
199 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
200 result.append(buffer);
201
202 write(fd, result.string(), result.size());
203 return NO_ERROR;
204}
205
Glenn Kasten0f11b512014-01-31 16:18:54 -0800206status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700207{
Glenn Kasten44deb052012-02-05 18:09:08 -0800208 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700209 dumpPermissionDenial(fd);
210 } else {
211 bool locked = tryLock(mLock);
212 if (!locked) {
213 String8 result(kDeadlockedString);
214 write(fd, result.string(), result.size());
215 }
216
217 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800218 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700219 mAudioCommandThread->dump(fd);
220 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800221 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700222 mTonePlaybackThread->dump(fd);
223 }
224
Eric Laurentdce54a12014-03-10 12:19:46 -0700225#ifdef USE_LEGACY_AUDIO_POLICY
Dima Zavinfce7a472011-04-19 22:30:36 -0700226 if (mpAudioPolicy) {
227 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700228 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700229#else
230 if (mAudioPolicyManager) {
231 mAudioPolicyManager->dump(fd);
232 }
233#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700234
235 if (locked) mLock.unlock();
236 }
237 return NO_ERROR;
238}
239
240status_t AudioPolicyService::dumpPermissionDenial(int fd)
241{
242 const size_t SIZE = 256;
243 char buffer[SIZE];
244 String8 result;
245 snprintf(buffer, SIZE, "Permission Denial: "
246 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
247 IPCThreadState::self()->getCallingPid(),
248 IPCThreadState::self()->getCallingUid());
249 result.append(buffer);
250 write(fd, result.string(), result.size());
251 return NO_ERROR;
252}
253
Glenn Kasten81872a22012-03-07 16:49:22 -0800254void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700255{
Glenn Kasten81872a22012-03-07 16:49:22 -0800256 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700257 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800258 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700259 }
260}
261
Mathias Agopian65ab4712010-07-14 17:59:35 -0700262status_t AudioPolicyService::onTransact(
263 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
264{
265 return BnAudioPolicyService::onTransact(code, data, reply, flags);
266}
267
268
Mathias Agopian65ab4712010-07-14 17:59:35 -0700269// ----------- AudioPolicyService::AudioCommandThread implementation ----------
270
Eric Laurentbfb1b832013-01-07 09:53:42 -0800271AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
272 const wp<AudioPolicyService>& service)
273 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700274{
275 mpToneGenerator = NULL;
276}
277
278
279AudioPolicyService::AudioCommandThread::~AudioCommandThread()
280{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800281 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700282 release_wake_lock(mName.string());
283 }
284 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800285 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700286}
287
288void AudioPolicyService::AudioCommandThread::onFirstRef()
289{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800290 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700291}
292
293bool AudioPolicyService::AudioCommandThread::threadLoop()
294{
295 nsecs_t waitTime = INT64_MAX;
296
297 mLock.lock();
298 while (!exitPending())
299 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700300 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700301 nsecs_t curTime = systemTime();
302 // commands are sorted by increasing time stamp: execute them from index 0 and up
303 if (mAudioCommands[0]->mTime <= curTime) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700304 sp<AudioCommand> command = mAudioCommands[0];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305 mAudioCommands.removeAt(0);
Eric Laurent0ede8922014-05-09 18:04:42 -0700306 mLastCommand = command;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700307
308 switch (command->mCommand) {
309 case START_TONE: {
310 mLock.unlock();
Eric Laurent0ede8922014-05-09 18:04:42 -0700311 ToneData *data = (ToneData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100312 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800314 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700315 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
316 mpToneGenerator->startTone(data->mType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700317 mLock.lock();
318 }break;
319 case STOP_TONE: {
320 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100321 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700322 if (mpToneGenerator != NULL) {
323 mpToneGenerator->stopTone();
324 delete mpToneGenerator;
325 mpToneGenerator = NULL;
326 }
327 mLock.lock();
328 }break;
329 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700330 VolumeData *data = (VolumeData *)command->mParam.get();
Steve Block3856b092011-10-20 11:56:00 +0100331 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700332 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
333 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
334 data->mVolume,
335 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336 }break;
337 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700338 ParametersData *data = (ParametersData *)command->mParam.get();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700339 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
340 data->mKeyValuePairs.string(), data->mIO);
341 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700342 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700343 case SET_VOICE_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700344 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
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);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700348 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800349 case STOP_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700350 StopOutputData *data = (StopOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800351 ALOGV("AudioCommandThread() processing stop output %d",
352 data->mIO);
353 sp<AudioPolicyService> svc = mService.promote();
354 if (svc == 0) {
355 break;
356 }
357 mLock.unlock();
358 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
359 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800360 }break;
361 case RELEASE_OUTPUT: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700362 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800363 ALOGV("AudioCommandThread() processing release output %d",
364 data->mIO);
365 sp<AudioPolicyService> svc = mService.promote();
366 if (svc == 0) {
367 break;
368 }
369 mLock.unlock();
370 svc->doReleaseOutput(data->mIO);
371 mLock.lock();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800372 }break;
Eric Laurent951f4552014-05-20 10:48:17 -0700373 case CREATE_AUDIO_PATCH: {
374 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
375 ALOGV("AudioCommandThread() processing create audio patch");
376 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
377 if (af == 0) {
378 command->mStatus = PERMISSION_DENIED;
379 } else {
380 command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
381 }
382 } break;
383 case RELEASE_AUDIO_PATCH: {
384 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
385 ALOGV("AudioCommandThread() processing release audio patch");
386 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
387 if (af == 0) {
388 command->mStatus = PERMISSION_DENIED;
389 } else {
390 command->mStatus = af->releaseAudioPatch(data->mHandle);
391 }
392 } break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000394 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700395 }
Eric Laurent0ede8922014-05-09 18:04:42 -0700396 {
397 Mutex::Autolock _l(command->mLock);
398 if (command->mWaitStatus) {
399 command->mWaitStatus = false;
400 command->mCond.signal();
401 }
402 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403 waitTime = INT64_MAX;
404 } else {
405 waitTime = mAudioCommands[0]->mTime - curTime;
406 break;
407 }
408 }
409 // release delayed commands wake lock
Eric Laurentbfb1b832013-01-07 09:53:42 -0800410 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700411 release_wake_lock(mName.string());
412 }
Steve Block3856b092011-10-20 11:56:00 +0100413 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700414 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100415 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700416 }
417 mLock.unlock();
418 return false;
419}
420
421status_t AudioPolicyService::AudioCommandThread::dump(int fd)
422{
423 const size_t SIZE = 256;
424 char buffer[SIZE];
425 String8 result;
426
427 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
428 result.append(buffer);
429 write(fd, result.string(), result.size());
430
431 bool locked = tryLock(mLock);
432 if (!locked) {
433 String8 result2(kCmdDeadlockedString);
434 write(fd, result2.string(), result2.size());
435 }
436
437 snprintf(buffer, SIZE, "- Commands:\n");
438 result = String8(buffer);
439 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800440 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700441 mAudioCommands[i]->dump(buffer, SIZE);
442 result.append(buffer);
443 }
444 result.append(" Last Command\n");
Eric Laurent0ede8922014-05-09 18:04:42 -0700445 if (mLastCommand != 0) {
446 mLastCommand->dump(buffer, SIZE);
447 result.append(buffer);
448 } else {
449 result.append(" none\n");
450 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700451
452 write(fd, result.string(), result.size());
453
454 if (locked) mLock.unlock();
455
456 return NO_ERROR;
457}
458
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800459void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
460 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700461{
Eric Laurent0ede8922014-05-09 18:04:42 -0700462 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700463 command->mCommand = START_TONE;
Eric Laurent0ede8922014-05-09 18:04:42 -0700464 sp<ToneData> data = new ToneData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700465 data->mType = type;
466 data->mStream = stream;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100467 command->mParam = data;
Steve Block3856b092011-10-20 11:56:00 +0100468 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurent0ede8922014-05-09 18:04:42 -0700469 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700470}
471
472void AudioPolicyService::AudioCommandThread::stopToneCommand()
473{
Eric Laurent0ede8922014-05-09 18:04:42 -0700474 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700475 command->mCommand = STOP_TONE;
Steve Block3856b092011-10-20 11:56:00 +0100476 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurent0ede8922014-05-09 18:04:42 -0700477 sendCommand(command);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700478}
479
Glenn Kastenfff6d712012-01-12 16:38:12 -0800480status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700481 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800482 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700483 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700484{
Eric Laurent0ede8922014-05-09 18:04:42 -0700485 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700486 command->mCommand = SET_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700487 sp<VolumeData> data = new VolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700488 data->mStream = stream;
489 data->mVolume = volume;
490 data->mIO = output;
491 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700492 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100493 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700494 stream, volume, output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700495 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700496}
497
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800498status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700499 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700500 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700501{
Eric Laurent0ede8922014-05-09 18:04:42 -0700502 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700503 command->mCommand = SET_PARAMETERS;
Eric Laurent0ede8922014-05-09 18:04:42 -0700504 sp<ParametersData> data = new ParametersData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700505 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700506 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700507 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700508 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100509 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700510 keyValuePairs, ioHandle, delayMs);
Eric Laurent0ede8922014-05-09 18:04:42 -0700511 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700512}
513
514status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
515{
Eric Laurent0ede8922014-05-09 18:04:42 -0700516 sp<AudioCommand> command = new AudioCommand();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700517 command->mCommand = SET_VOICE_VOLUME;
Eric Laurent0ede8922014-05-09 18:04:42 -0700518 sp<VoiceVolumeData> data = new VoiceVolumeData();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700519 data->mVolume = volume;
520 command->mParam = data;
Eric Laurent0ede8922014-05-09 18:04:42 -0700521 command->mWaitStatus = true;
Steve Block3856b092011-10-20 11:56:00 +0100522 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent0ede8922014-05-09 18:04:42 -0700523 return sendCommand(command, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700524}
525
Eric Laurentbfb1b832013-01-07 09:53:42 -0800526void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
527 audio_stream_type_t stream,
528 int session)
529{
Eric Laurent0ede8922014-05-09 18:04:42 -0700530 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800531 command->mCommand = STOP_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700532 sp<StopOutputData> data = new StopOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800533 data->mIO = output;
534 data->mStream = stream;
535 data->mSession = session;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100536 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800537 ALOGV("AudioCommandThread() adding stop output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700538 sendCommand(command);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800539}
540
541void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
542{
Eric Laurent0ede8922014-05-09 18:04:42 -0700543 sp<AudioCommand> command = new AudioCommand();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800544 command->mCommand = RELEASE_OUTPUT;
Eric Laurent0ede8922014-05-09 18:04:42 -0700545 sp<ReleaseOutputData> data = new ReleaseOutputData();
Eric Laurentbfb1b832013-01-07 09:53:42 -0800546 data->mIO = output;
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100547 command->mParam = data;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800548 ALOGV("AudioCommandThread() adding release output %d", output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700549 sendCommand(command);
550}
551
Eric Laurent951f4552014-05-20 10:48:17 -0700552status_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
553 const struct audio_patch *patch,
554 audio_patch_handle_t *handle,
555 int delayMs)
556{
557 status_t status = NO_ERROR;
558
559 sp<AudioCommand> command = new AudioCommand();
560 command->mCommand = CREATE_AUDIO_PATCH;
561 CreateAudioPatchData *data = new CreateAudioPatchData();
562 data->mPatch = *patch;
563 data->mHandle = *handle;
564 command->mParam = data;
565 command->mWaitStatus = true;
566 ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
567 status = sendCommand(command, delayMs);
568 if (status == NO_ERROR) {
569 *handle = data->mHandle;
570 }
571 return status;
572}
573
574status_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
575 int delayMs)
576{
577 sp<AudioCommand> command = new AudioCommand();
578 command->mCommand = RELEASE_AUDIO_PATCH;
579 ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
580 data->mHandle = handle;
581 command->mParam = data;
582 command->mWaitStatus = true;
583 ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
584 return sendCommand(command, delayMs);
585}
586
Eric Laurent0ede8922014-05-09 18:04:42 -0700587status_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
588{
589 {
590 Mutex::Autolock _l(mLock);
591 insertCommand_l(command, delayMs);
592 mWaitWorkCV.signal();
593 }
594 Mutex::Autolock _l(command->mLock);
595 while (command->mWaitStatus) {
596 nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
597 if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
598 command->mStatus = TIMED_OUT;
599 command->mWaitStatus = false;
600 }
601 }
602 return command->mStatus;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800603}
604
Eric Laurent951f4552014-05-20 10:48:17 -0700605
Mathias Agopian65ab4712010-07-14 17:59:35 -0700606// insertCommand_l() must be called with mLock held
Eric Laurent0ede8922014-05-09 18:04:42 -0700607void AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700608{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800609 ssize_t i; // not size_t because i will count down to -1
Eric Laurent0ede8922014-05-09 18:04:42 -0700610 Vector < sp<AudioCommand> > removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700611 command->mTime = systemTime() + milliseconds(delayMs);
612
613 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800614 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700615 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
616 }
617
618 // check same pending commands with later time stamps and eliminate them
619 for (i = mAudioCommands.size()-1; i >= 0; i--) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700620 sp<AudioCommand> command2 = mAudioCommands[i];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700621 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
622 if (command2->mTime <= command->mTime) break;
623 if (command2->mCommand != command->mCommand) continue;
624
625 switch (command->mCommand) {
626 case SET_PARAMETERS: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700627 ParametersData *data = (ParametersData *)command->mParam.get();
628 ParametersData *data2 = (ParametersData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700629 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100630 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700631 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700632 AudioParameter param = AudioParameter(data->mKeyValuePairs);
633 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
634 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700635 String8 key;
636 String8 value;
637 param.getAt(j, key, value);
638 for (size_t k = 0; k < param2.size(); k++) {
639 String8 key2;
640 String8 value2;
641 param2.getAt(k, key2, value2);
642 if (key2 == key) {
643 param2.remove(key2);
644 ALOGV("Filtering out parameter %s", key2.string());
645 break;
646 }
647 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700648 }
649 // if all keys have been filtered out, remove the command.
650 // otherwise, update the key value pairs
651 if (param2.size() == 0) {
652 removedCommands.add(command2);
653 } else {
654 data2->mKeyValuePairs = param2.toString();
655 }
Eric Laurent21e54562013-09-23 12:08:05 -0700656 command->mTime = command2->mTime;
657 // force delayMs to non 0 so that code below does not request to wait for
658 // command status as the command is now delayed
659 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700660 } break;
661
662 case SET_VOLUME: {
Eric Laurent0ede8922014-05-09 18:04:42 -0700663 VolumeData *data = (VolumeData *)command->mParam.get();
664 VolumeData *data2 = (VolumeData *)command2->mParam.get();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700665 if (data->mIO != data2->mIO) break;
666 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100667 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700668 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700669 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -0700670 command->mTime = command2->mTime;
671 // force delayMs to non 0 so that code below does not request to wait for
672 // command status as the command is now delayed
673 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700674 } break;
675 case START_TONE:
676 case STOP_TONE:
677 default:
678 break;
679 }
680 }
681
682 // remove filtered commands
683 for (size_t j = 0; j < removedCommands.size(); j++) {
684 // removed commands always have time stamps greater than current command
685 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
Eric Laurent0ede8922014-05-09 18:04:42 -0700686 if (mAudioCommands[k].get() == removedCommands[j].get()) {
Steve Block3856b092011-10-20 11:56:00 +0100687 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700688 mAudioCommands.removeAt(k);
689 break;
690 }
691 }
692 }
693 removedCommands.clear();
694
Eric Laurent0ede8922014-05-09 18:04:42 -0700695 // Disable wait for status if delay is not 0
696 if (delayMs != 0) {
Eric Laurentcec4abb2012-07-03 12:23:02 -0700697 command->mWaitStatus = false;
698 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700699
Mathias Agopian65ab4712010-07-14 17:59:35 -0700700 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +0100701 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -0700702 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700703 mAudioCommands.insertAt(command, i + 1);
704}
705
706void AudioPolicyService::AudioCommandThread::exit()
707{
Steve Block3856b092011-10-20 11:56:00 +0100708 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700709 {
710 AutoMutex _l(mLock);
711 requestExit();
712 mWaitWorkCV.signal();
713 }
714 requestExitAndWait();
715}
716
717void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
718{
719 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
720 mCommand,
721 (int)ns2s(mTime),
722 (int)ns2ms(mTime)%1000,
723 mWaitStatus,
Eric Laurent0ede8922014-05-09 18:04:42 -0700724 mParam.get());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700725}
726
Dima Zavinfce7a472011-04-19 22:30:36 -0700727/******* helpers for the service_ops callbacks defined below *********/
728void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
729 const char *keyValuePairs,
730 int delayMs)
731{
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800732 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -0700733 delayMs);
734}
735
736int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
737 float volume,
738 audio_io_handle_t output,
739 int delayMs)
740{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800741 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800742 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -0700743}
744
745int AudioPolicyService::startTone(audio_policy_tone_t tone,
746 audio_stream_type_t stream)
747{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700748 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +0000749 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700750 }
751 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +0000752 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700753 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700754 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700755 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
756 AUDIO_STREAM_VOICE_CALL);
757 return 0;
758}
759
760int AudioPolicyService::stopTone()
761{
762 mTonePlaybackThread->stopToneCommand();
763 return 0;
764}
765
766int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
767{
768 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
769}
770
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700771// ----------------------------------------------------------------------------
772// Audio pre-processing configuration
773// ----------------------------------------------------------------------------
774
Glenn Kasten8dad0e32012-01-09 08:41:22 -0800775/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700776 MIC_SRC_TAG,
777 VOICE_UL_SRC_TAG,
778 VOICE_DL_SRC_TAG,
779 VOICE_CALL_SRC_TAG,
780 CAMCORDER_SRC_TAG,
781 VOICE_REC_SRC_TAG,
782 VOICE_COMM_SRC_TAG
783};
784
785// returns the audio_source_t enum corresponding to the input source name or
786// AUDIO_SOURCE_CNT is no match found
787audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
788{
789 int i;
790 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
791 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100792 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700793 break;
794 }
795 }
796 return (audio_source_t)i;
797}
798
799size_t AudioPolicyService::growParamSize(char *param,
800 size_t size,
801 size_t *curSize,
802 size_t *totSize)
803{
804 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
805 size_t pos = ((*curSize - 1 ) / size + 1) * size;
806
807 if (pos + size > *totSize) {
808 while (pos + size > *totSize) {
809 *totSize += ((*totSize + 7) / 8) * 4;
810 }
811 param = (char *)realloc(param, *totSize);
812 }
813 *curSize = pos + size;
814 return pos;
815}
816
817size_t AudioPolicyService::readParamValue(cnode *node,
818 char *param,
819 size_t *curSize,
820 size_t *totSize)
821{
822 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
823 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
824 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100825 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700826 return sizeof(short);
827 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
828 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
829 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100830 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700831 return sizeof(int);
832 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
833 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
834 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +0100835 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700836 return sizeof(float);
837 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
838 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
839 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
840 *(bool *)((char *)param + pos) = false;
841 } else {
842 *(bool *)((char *)param + pos) = true;
843 }
Steve Block3856b092011-10-20 11:56:00 +0100844 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700845 return sizeof(bool);
846 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
847 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
848 if (*curSize + len + 1 > *totSize) {
849 *totSize = *curSize + len + 1;
850 param = (char *)realloc(param, *totSize);
851 }
852 strncpy(param + *curSize, node->value, len);
853 *curSize += len;
854 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +0100855 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700856 return len;
857 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000858 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700859 return 0;
860}
861
862effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
863{
864 cnode *param;
865 cnode *value;
866 size_t curSize = sizeof(effect_param_t);
867 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
868 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
869
870 param = config_find(root, PARAM_TAG);
871 value = config_find(root, VALUE_TAG);
872 if (param == NULL && value == NULL) {
873 // try to parse simple parameter form {int int}
874 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -0800875 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700876 // Note: that a pair of random strings is read as 0 0
877 int *ptr = (int *)fx_param->data;
878 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +0000879 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700880 *ptr++ = atoi(param->name);
881 *ptr = atoi(param->value);
882 fx_param->psize = sizeof(int);
883 fx_param->vsize = sizeof(int);
884 return fx_param;
885 }
886 }
887 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000888 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700889 goto error;
890 }
891
892 fx_param->psize = 0;
893 param = param->first_child;
894 while (param) {
Steve Block3856b092011-10-20 11:56:00 +0100895 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700896 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
897 if (size == 0) {
898 goto error;
899 }
900 fx_param->psize += size;
901 param = param->next;
902 }
903
904 // align start of value field on 32 bit boundary
905 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
906
907 fx_param->vsize = 0;
908 value = value->first_child;
909 while (value) {
Steve Block3856b092011-10-20 11:56:00 +0100910 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700911 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
912 if (size == 0) {
913 goto error;
914 }
915 fx_param->vsize += size;
916 value = value->next;
917 }
918
919 return fx_param;
920
921error:
You Kimdb358af2013-09-12 20:48:08 +0900922 free(fx_param);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700923 return NULL;
924}
925
926void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
927{
928 cnode *node = root->first_child;
929 while (node) {
Steve Block3856b092011-10-20 11:56:00 +0100930 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700931 effect_param_t *param = loadEffectParameter(node);
932 if (param == NULL) {
933 node = node->next;
934 continue;
935 }
936 params.add(param);
937 node = node->next;
938 }
939}
940
941AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
942 cnode *root,
943 const Vector <EffectDesc *>& effects)
944{
945 cnode *node = root->first_child;
946 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000947 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700948 return NULL;
949 }
950 InputSourceDesc *source = new InputSourceDesc();
951 while (node) {
952 size_t i;
953 for (i = 0; i < effects.size(); i++) {
954 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +0100955 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700956 break;
957 }
958 }
959 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +0100960 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700961 node = node->next;
962 continue;
963 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800964 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700965 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +0100966 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700967 source->mEffects.add(effect);
968 node = node->next;
969 }
970 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000971 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700972 delete source;
973 return NULL;
974 }
975 return source;
976}
977
978status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
979{
980 cnode *node = config_find(root, PREPROCESSING_TAG);
981 if (node == NULL) {
982 return -ENOENT;
983 }
984 node = node->first_child;
985 while (node) {
986 audio_source_t source = inputSourceNameToEnum(node->name);
987 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000988 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700989 node = node->next;
990 continue;
991 }
Steve Block3856b092011-10-20 11:56:00 +0100992 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700993 InputSourceDesc *desc = loadInputSource(node, effects);
994 if (desc == NULL) {
995 node = node->next;
996 continue;
997 }
998 mInputSources.add(source, desc);
999 node = node->next;
1000 }
1001 return NO_ERROR;
1002}
1003
1004AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1005{
1006 cnode *node = config_find(root, UUID_TAG);
1007 if (node == NULL) {
1008 return NULL;
1009 }
1010 effect_uuid_t uuid;
1011 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001012 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001013 return NULL;
1014 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001015 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001016}
1017
1018status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1019{
1020 cnode *node = config_find(root, EFFECTS_TAG);
1021 if (node == NULL) {
1022 return -ENOENT;
1023 }
1024 node = node->first_child;
1025 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001026 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001027 EffectDesc *effect = loadEffect(node);
1028 if (effect == NULL) {
1029 node = node->next;
1030 continue;
1031 }
1032 effects.add(effect);
1033 node = node->next;
1034 }
1035 return NO_ERROR;
1036}
1037
1038status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1039{
1040 cnode *root;
1041 char *data;
1042
1043 data = (char *)load_file(path, NULL);
1044 if (data == NULL) {
1045 return -ENODEV;
1046 }
1047 root = config_node("", "");
1048 config_load(root, data);
1049
1050 Vector <EffectDesc *> effects;
1051 loadEffects(root, effects);
1052 loadInputSources(root, effects);
1053
Yu Yezhonge6056ba2013-10-15 14:32:34 +08001054 // delete effects to fix memory leak.
1055 // as effects is local var and valgrind would treat this as memory leak
1056 // and although it only did in mediaserver init, but free it in case mediaserver reboot
1057 size_t i;
1058 for (i = 0; i < effects.size(); i++) {
1059 delete effects[i];
1060 }
1061
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001062 config_free(root);
1063 free(root);
1064 free(data);
1065
1066 return NO_ERROR;
1067}
1068
Dima Zavinfce7a472011-04-19 22:30:36 -07001069extern "C" {
Eric Laurent2d388ec2014-03-07 13:25:54 -08001070audio_module_handle_t aps_load_hw_module(void *service __unused,
1071 const char *name);
1072audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001073 audio_devices_t *pDevices,
1074 uint32_t *pSamplingRate,
1075 audio_format_t *pFormat,
1076 audio_channel_mask_t *pChannelMask,
1077 uint32_t *pLatencyMs,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001078 audio_output_flags_t flags);
Eric Laurenta4c5a552012-03-29 10:12:40 -07001079
Eric Laurent2d388ec2014-03-07 13:25:54 -08001080audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001081 audio_module_handle_t module,
1082 audio_devices_t *pDevices,
1083 uint32_t *pSamplingRate,
1084 audio_format_t *pFormat,
1085 audio_channel_mask_t *pChannelMask,
1086 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001087 audio_output_flags_t flags,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001088 const audio_offload_info_t *offloadInfo);
1089audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001090 audio_io_handle_t output1,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001091 audio_io_handle_t output2);
1092int aps_close_output(void *service __unused, audio_io_handle_t output);
1093int aps_suspend_output(void *service __unused, audio_io_handle_t output);
1094int aps_restore_output(void *service __unused, audio_io_handle_t output);
1095audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001096 audio_devices_t *pDevices,
1097 uint32_t *pSamplingRate,
1098 audio_format_t *pFormat,
1099 audio_channel_mask_t *pChannelMask,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001100 audio_in_acoustics_t acoustics __unused);
1101audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001102 audio_module_handle_t module,
1103 audio_devices_t *pDevices,
1104 uint32_t *pSamplingRate,
1105 audio_format_t *pFormat,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001106 audio_channel_mask_t *pChannelMask);
1107int aps_close_input(void *service __unused, audio_io_handle_t input);
1108int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
1109int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001110 audio_io_handle_t src_output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001111 audio_io_handle_t dst_output);
1112char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
1113 const char *keys);
1114void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1115 const char *kv_pairs, int delay_ms);
1116int aps_set_stream_volume(void *service, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001117 float volume, audio_io_handle_t output,
Eric Laurent2d388ec2014-03-07 13:25:54 -08001118 int delay_ms);
1119int aps_start_tone(void *service, audio_policy_tone_t tone,
1120 audio_stream_type_t stream);
1121int aps_stop_tone(void *service);
1122int aps_set_voice_volume(void *service, float volume, int delay_ms);
1123};
Dima Zavinfce7a472011-04-19 22:30:36 -07001124
1125namespace {
1126 struct audio_policy_service_ops aps_ops = {
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001127 .open_output = aps_open_output,
1128 .open_duplicate_output = aps_open_dup_output,
1129 .close_output = aps_close_output,
1130 .suspend_output = aps_suspend_output,
1131 .restore_output = aps_restore_output,
1132 .open_input = aps_open_input,
1133 .close_input = aps_close_input,
1134 .set_stream_volume = aps_set_stream_volume,
Glenn Kastend2304db2014-02-03 07:40:31 -08001135 .invalidate_stream = aps_invalidate_stream,
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001136 .set_parameters = aps_set_parameters,
1137 .get_parameters = aps_get_parameters,
1138 .start_tone = aps_start_tone,
1139 .stop_tone = aps_stop_tone,
1140 .set_voice_volume = aps_set_voice_volume,
1141 .move_effects = aps_move_effects,
1142 .load_hw_module = aps_load_hw_module,
1143 .open_output_on_module = aps_open_output_on_module,
1144 .open_input_on_module = aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001145 };
1146}; // namespace <unnamed>
1147
Mathias Agopian65ab4712010-07-14 17:59:35 -07001148}; // namespace android