blob: 55e08269f7f25fce6b1b135a5506e7dc2ee03499 [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
20#undef __STRICT_ANSI__
21#define __STDINT_LIMITS
22#define __STDC_LIMIT_MACROS
23#include <stdint.h>
24
25#include <sys/time.h>
26#include <binder/IServiceManager.h>
27#include <utils/Log.h>
28#include <cutils/properties.h>
29#include <binder/IPCThreadState.h>
30#include <utils/String16.h>
31#include <utils/threads.h>
32#include "AudioPolicyService.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080033#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070035#include <media/AudioEffect.h>
36#include <media/EffectsFactoryApi.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070037
Dima Zavinfce7a472011-04-19 22:30:36 -070038#include <hardware/hardware.h>
Dima Zavin64760242011-05-11 14:15:23 -070039#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070040#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070041#include <hardware/audio_policy.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070042#include <audio_effects/audio_effects_conf.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070043
Mathias Agopian65ab4712010-07-14 17:59:35 -070044namespace android {
45
Glenn Kasten8dad0e32012-01-09 08:41:22 -080046static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
47static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070048
49static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080050static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070051
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010052static const nsecs_t kAudioCommandTimeout = 3000000000; // 3 seconds
53
Dima Zavinfce7a472011-04-19 22:30:36 -070054namespace {
55 extern struct audio_policy_service_ops aps_ops;
56};
57
Mathias Agopian65ab4712010-07-14 17:59:35 -070058// ----------------------------------------------------------------------------
59
60AudioPolicyService::AudioPolicyService()
Dima Zavinfce7a472011-04-19 22:30:36 -070061 : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL)
Mathias Agopian65ab4712010-07-14 17:59:35 -070062{
63 char value[PROPERTY_VALUE_MAX];
Dima Zavinfce7a472011-04-19 22:30:36 -070064 const struct hw_module_t *module;
65 int forced_val;
66 int rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -070067
Eric Laurent93575202011-01-18 18:39:02 -080068 Mutex::Autolock _l(mLock);
69
Mathias Agopian65ab4712010-07-14 17:59:35 -070070 // start tone playback thread
71 mTonePlaybackThread = new AudioCommandThread(String8(""));
72 // start audio commands thread
Glenn Kasten480b4682012-02-28 12:30:08 -080073 mAudioCommandThread = new AudioCommandThread(String8("ApmCommand"));
Mathias Agopian65ab4712010-07-14 17:59:35 -070074
Dima Zavinfce7a472011-04-19 22:30:36 -070075 /* instantiate the audio policy manager */
76 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
77 if (rc)
78 return;
Mathias Agopian65ab4712010-07-14 17:59:35 -070079
Dima Zavinfce7a472011-04-19 22:30:36 -070080 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
Steve Block29357bc2012-01-06 19:20:56 +000081 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070082 if (rc)
83 return;
Eric Laurent93575202011-01-18 18:39:02 -080084
Dima Zavinfce7a472011-04-19 22:30:36 -070085 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
86 &mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000087 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070088 if (rc)
89 return;
90
91 rc = mpAudioPolicy->init_check(mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000092 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070093 if (rc)
94 return;
95
Steve Blockdf64d152012-01-04 20:05:49 +000096 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurent7c7f10b2011-06-17 21:29:58 -070097
98 // load audio pre processing modules
99 if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) {
100 loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE);
101 } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) {
102 loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE);
103 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700104}
105
106AudioPolicyService::~AudioPolicyService()
107{
108 mTonePlaybackThread->exit();
109 mTonePlaybackThread.clear();
110 mAudioCommandThread->exit();
111 mAudioCommandThread.clear();
112
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700113
114 // release audio pre processing resources
115 for (size_t i = 0; i < mInputSources.size(); i++) {
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800116 delete mInputSources.valueAt(i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700117 }
118 mInputSources.clear();
119
120 for (size_t i = 0; i < mInputs.size(); i++) {
121 mInputs.valueAt(i)->mEffects.clear();
122 delete mInputs.valueAt(i);
123 }
124 mInputs.clear();
125
Glenn Kastena0d68332012-01-27 16:47:15 -0800126 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL)
Dima Zavinfce7a472011-04-19 22:30:36 -0700127 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kastena0d68332012-01-27 16:47:15 -0800128 if (mpAudioPolicyDev != NULL)
Dima Zavinfce7a472011-04-19 22:30:36 -0700129 audio_policy_dev_close(mpAudioPolicyDev);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700130}
131
Dima Zavinfce7a472011-04-19 22:30:36 -0700132status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
133 audio_policy_dev_state_t state,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700134 const char *device_address)
135{
Dima Zavinfce7a472011-04-19 22:30:36 -0700136 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700137 return NO_INIT;
138 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800139 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700140 return PERMISSION_DENIED;
141 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700142 if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700143 return BAD_VALUE;
144 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700145 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
146 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700147 return BAD_VALUE;
148 }
149
Steve Block3856b092011-10-20 11:56:00 +0100150 ALOGV("setDeviceConnectionState() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700151 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700152 return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device,
153 state, device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700154}
155
Dima Zavinfce7a472011-04-19 22:30:36 -0700156audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
157 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -0700158 const char *device_address)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700159{
Dima Zavinfce7a472011-04-19 22:30:36 -0700160 if (mpAudioPolicy == NULL) {
161 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700162 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700163 return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device,
164 device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700165}
166
Glenn Kastenf78aee72012-01-04 11:00:47 -0800167status_t AudioPolicyService::setPhoneState(audio_mode_t state)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700168{
Dima Zavinfce7a472011-04-19 22:30:36 -0700169 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700170 return NO_INIT;
171 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800172 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700173 return PERMISSION_DENIED;
174 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800175 if (uint32_t(state) >= AUDIO_MODE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700176 return BAD_VALUE;
177 }
178
Steve Block3856b092011-10-20 11:56:00 +0100179 ALOGV("setPhoneState() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700180
181 // TODO: check if it is more appropriate to do it in platform specific policy manager
182 AudioSystem::setMode(state);
183
184 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700185 mpAudioPolicy->set_phone_state(mpAudioPolicy, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700186 return NO_ERROR;
187}
188
Dima Zavinfce7a472011-04-19 22:30:36 -0700189status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
190 audio_policy_forced_cfg_t config)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700191{
Dima Zavinfce7a472011-04-19 22:30:36 -0700192 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700193 return NO_INIT;
194 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800195 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700196 return PERMISSION_DENIED;
197 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700198 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700199 return BAD_VALUE;
200 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700201 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700202 return BAD_VALUE;
203 }
Steve Block3856b092011-10-20 11:56:00 +0100204 ALOGV("setForceUse() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700205 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700206 mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700207 return NO_ERROR;
208}
209
Dima Zavinfce7a472011-04-19 22:30:36 -0700210audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700211{
Dima Zavinfce7a472011-04-19 22:30:36 -0700212 if (mpAudioPolicy == NULL) {
213 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700214 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700215 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
216 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700217 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700218 return mpAudioPolicy->get_force_use(mpAudioPolicy, usage);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700219}
220
Dima Zavinfce7a472011-04-19 22:30:36 -0700221audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700222 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800223 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700224 audio_channel_mask_t channelMask,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700225 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700226{
Dima Zavinfce7a472011-04-19 22:30:36 -0700227 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700228 return 0;
229 }
Steve Block3856b092011-10-20 11:56:00 +0100230 ALOGV("getOutput() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700231 Mutex::Autolock _l(mLock);
Glenn Kasten254af182012-07-03 14:59:05 -0700232 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, format, channelMask, flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700233}
234
Eric Laurentde070132010-07-13 04:45:46 -0700235status_t AudioPolicyService::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700236 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700237 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700238{
Dima Zavinfce7a472011-04-19 22:30:36 -0700239 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700240 return NO_INIT;
241 }
Steve Block3856b092011-10-20 11:56:00 +0100242 ALOGV("startOutput() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700243 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700244 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700245}
246
Eric Laurentde070132010-07-13 04:45:46 -0700247status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700248 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700249 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700250{
Dima Zavinfce7a472011-04-19 22:30:36 -0700251 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700252 return NO_INIT;
253 }
Steve Block3856b092011-10-20 11:56:00 +0100254 ALOGV("stopOutput() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700255 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700256 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700257}
258
259void AudioPolicyService::releaseOutput(audio_io_handle_t output)
260{
Dima Zavinfce7a472011-04-19 22:30:36 -0700261 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700262 return;
263 }
Steve Block3856b092011-10-20 11:56:00 +0100264 ALOGV("releaseOutput() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700265 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700266 mpAudioPolicy->release_output(mpAudioPolicy, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700267}
268
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800269audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700270 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800271 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700272 audio_channel_mask_t channelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700273 int audioSession)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700274{
Dima Zavinfce7a472011-04-19 22:30:36 -0700275 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700276 return 0;
277 }
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800278 // already checked by client, but double-check in case the client wrapper is bypassed
279 if (uint32_t(inputSource) >= AUDIO_SOURCE_CNT) {
280 return 0;
281 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700282 Mutex::Autolock _l(mLock);
Glenn Kasten20010052012-06-22 13:43:51 -0700283 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700284 audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
Glenn Kasten254af182012-07-03 14:59:05 -0700285 format, channelMask, (audio_in_acoustics_t) 0);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700286
287 if (input == 0) {
288 return input;
289 }
290 // create audio pre processors according to input source
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800291 ssize_t index = mInputSources.indexOfKey(inputSource);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700292 if (index < 0) {
293 return input;
294 }
295 ssize_t idx = mInputs.indexOfKey(input);
296 InputDesc *inputDesc;
297 if (idx < 0) {
Glenn Kasten81872a22012-03-07 16:49:22 -0800298 inputDesc = new InputDesc(audioSession);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700299 mInputs.add(input, inputDesc);
300 } else {
301 inputDesc = mInputs.valueAt(idx);
302 }
303
304 Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects;
305 for (size_t i = 0; i < effects.size(); i++) {
306 EffectDesc *effect = effects[i];
307 sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input);
308 status_t status = fx->initCheck();
309 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000310 ALOGW("Failed to create Fx %s on input %d", effect->mName, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700311 // fx goes out of scope and strong ref on AudioEffect is released
312 continue;
313 }
314 for (size_t j = 0; j < effect->mParams.size(); j++) {
315 fx->setParameter(effect->mParams[j]);
316 }
317 inputDesc->mEffects.add(fx);
318 }
319 setPreProcessorEnabled(inputDesc, true);
320 return input;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700321}
322
323status_t AudioPolicyService::startInput(audio_io_handle_t input)
324{
Dima Zavinfce7a472011-04-19 22:30:36 -0700325 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700326 return NO_INIT;
327 }
328 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700329
Dima Zavinfce7a472011-04-19 22:30:36 -0700330 return mpAudioPolicy->start_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700331}
332
333status_t AudioPolicyService::stopInput(audio_io_handle_t input)
334{
Dima Zavinfce7a472011-04-19 22:30:36 -0700335 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336 return NO_INIT;
337 }
338 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700339
Dima Zavinfce7a472011-04-19 22:30:36 -0700340 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700341}
342
343void AudioPolicyService::releaseInput(audio_io_handle_t input)
344{
Dima Zavinfce7a472011-04-19 22:30:36 -0700345 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700346 return;
347 }
348 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700349 mpAudioPolicy->release_input(mpAudioPolicy, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700350
351 ssize_t index = mInputs.indexOfKey(input);
352 if (index < 0) {
353 return;
354 }
355 InputDesc *inputDesc = mInputs.valueAt(index);
356 setPreProcessorEnabled(inputDesc, false);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700357 delete inputDesc;
358 mInputs.removeItemsAt(index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700359}
360
Dima Zavinfce7a472011-04-19 22:30:36 -0700361status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362 int indexMin,
363 int indexMax)
364{
Dima Zavinfce7a472011-04-19 22:30:36 -0700365 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700366 return NO_INIT;
367 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800368 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700369 return PERMISSION_DENIED;
370 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800371 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700372 return BAD_VALUE;
373 }
Eric Laurent5f121362012-06-15 14:45:03 -0700374 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700375 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700376 return NO_ERROR;
377}
378
Eric Laurent83844cc2011-11-18 16:43:31 -0800379status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
380 int index,
381 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700382{
Dima Zavinfce7a472011-04-19 22:30:36 -0700383 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384 return NO_INIT;
385 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800386 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700387 return PERMISSION_DENIED;
388 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800389 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700390 return BAD_VALUE;
391 }
Eric Laurent5f121362012-06-15 14:45:03 -0700392 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800393 if (mpAudioPolicy->set_stream_volume_index_for_device) {
394 return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
395 stream,
396 index,
397 device);
398 } else {
399 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
400 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700401}
402
Eric Laurent83844cc2011-11-18 16:43:31 -0800403status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
404 int *index,
405 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700406{
Dima Zavinfce7a472011-04-19 22:30:36 -0700407 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700408 return NO_INIT;
409 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800410 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700411 return BAD_VALUE;
412 }
Eric Laurent5f121362012-06-15 14:45:03 -0700413 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800414 if (mpAudioPolicy->get_stream_volume_index_for_device) {
415 return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
416 stream,
417 index,
418 device);
419 } else {
420 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
421 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700422}
423
Dima Zavinfce7a472011-04-19 22:30:36 -0700424uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700425{
Dima Zavinfce7a472011-04-19 22:30:36 -0700426 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700427 return 0;
428 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700429 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurentde070132010-07-13 04:45:46 -0700430}
431
Eric Laurent63742522012-03-08 13:42:42 -0800432//audio policy: use audio_device_t appropriately
433
434audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800435{
Dima Zavinfce7a472011-04-19 22:30:36 -0700436 if (mpAudioPolicy == NULL) {
Eric Laurent63742522012-03-08 13:42:42 -0800437 return (audio_devices_t)0;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800438 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700439 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800440}
441
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700442audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700443{
Dima Zavinfce7a472011-04-19 22:30:36 -0700444 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700445 return NO_INIT;
446 }
447 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700448 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700449}
450
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700451status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700452 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700453 uint32_t strategy,
454 int session,
455 int id)
456{
Dima Zavinfce7a472011-04-19 22:30:36 -0700457 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700458 return NO_INIT;
459 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700460 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700461}
462
463status_t AudioPolicyService::unregisterEffect(int id)
464{
Dima Zavinfce7a472011-04-19 22:30:36 -0700465 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700466 return NO_INIT;
467 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700468 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700469}
470
Eric Laurentdb7c0792011-08-10 10:37:50 -0700471status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
472{
473 if (mpAudioPolicy == NULL) {
474 return NO_INIT;
475 }
476 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
477}
478
Glenn Kastenfff6d712012-01-12 16:38:12 -0800479bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800480{
Dima Zavinfce7a472011-04-19 22:30:36 -0700481 if (mpAudioPolicy == NULL) {
Eric Laurenteda6c362011-02-02 09:33:30 -0800482 return 0;
483 }
484 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700485 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800486}
487
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700488bool AudioPolicyService::isSourceActive(audio_source_t source) const
489{
490 if (mpAudioPolicy == NULL) {
491 return false;
492 }
493 if (mpAudioPolicy->is_source_active == 0) {
494 return false;
495 }
496 Mutex::Autolock _l(mLock);
497 return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
498}
499
Eric Laurent57dae992011-07-24 13:36:09 -0700500status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
501 effect_descriptor_t *descriptors,
502 uint32_t *count)
503{
504
505 if (mpAudioPolicy == NULL) {
506 *count = 0;
507 return NO_INIT;
508 }
509 Mutex::Autolock _l(mLock);
510 status_t status = NO_ERROR;
511
512 size_t index;
513 for (index = 0; index < mInputs.size(); index++) {
514 if (mInputs.valueAt(index)->mSessionId == audioSession) {
515 break;
516 }
517 }
518 if (index == mInputs.size()) {
519 *count = 0;
520 return BAD_VALUE;
521 }
522 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
523
524 for (size_t i = 0; i < effects.size(); i++) {
525 effect_descriptor_t desc = effects[i]->descriptor();
526 if (i < *count) {
Glenn Kastena189a682012-02-20 12:16:30 -0800527 descriptors[i] = desc;
Eric Laurent57dae992011-07-24 13:36:09 -0700528 }
529 }
530 if (effects.size() > *count) {
531 status = NO_MEMORY;
532 }
533 *count = effects.size();
534 return status;
535}
536
Mathias Agopian65ab4712010-07-14 17:59:35 -0700537void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten23d82a92012-02-03 11:10:00 -0800538 ALOGW("binderDied() %p, tid %d, calling pid %d", who.unsafe_get(), gettid(),
Eric Laurentde070132010-07-13 04:45:46 -0700539 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700540}
541
542static bool tryLock(Mutex& mutex)
543{
544 bool locked = false;
545 for (int i = 0; i < kDumpLockRetries; ++i) {
546 if (mutex.tryLock() == NO_ERROR) {
547 locked = true;
548 break;
549 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800550 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700551 }
552 return locked;
553}
554
555status_t AudioPolicyService::dumpInternals(int fd)
556{
557 const size_t SIZE = 256;
558 char buffer[SIZE];
559 String8 result;
560
Dima Zavinfce7a472011-04-19 22:30:36 -0700561 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700562 result.append(buffer);
563 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
564 result.append(buffer);
565 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
566 result.append(buffer);
567
568 write(fd, result.string(), result.size());
569 return NO_ERROR;
570}
571
572status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
573{
Glenn Kasten44deb052012-02-05 18:09:08 -0800574 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700575 dumpPermissionDenial(fd);
576 } else {
577 bool locked = tryLock(mLock);
578 if (!locked) {
579 String8 result(kDeadlockedString);
580 write(fd, result.string(), result.size());
581 }
582
583 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800584 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700585 mAudioCommandThread->dump(fd);
586 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800587 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700588 mTonePlaybackThread->dump(fd);
589 }
590
Dima Zavinfce7a472011-04-19 22:30:36 -0700591 if (mpAudioPolicy) {
592 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593 }
594
595 if (locked) mLock.unlock();
596 }
597 return NO_ERROR;
598}
599
600status_t AudioPolicyService::dumpPermissionDenial(int fd)
601{
602 const size_t SIZE = 256;
603 char buffer[SIZE];
604 String8 result;
605 snprintf(buffer, SIZE, "Permission Denial: "
606 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
607 IPCThreadState::self()->getCallingPid(),
608 IPCThreadState::self()->getCallingUid());
609 result.append(buffer);
610 write(fd, result.string(), result.size());
611 return NO_ERROR;
612}
613
Glenn Kasten81872a22012-03-07 16:49:22 -0800614void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700615{
Glenn Kasten81872a22012-03-07 16:49:22 -0800616 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700617 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800618 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700619 }
620}
621
Mathias Agopian65ab4712010-07-14 17:59:35 -0700622status_t AudioPolicyService::onTransact(
623 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
624{
625 return BnAudioPolicyService::onTransact(code, data, reply, flags);
626}
627
628
Mathias Agopian65ab4712010-07-14 17:59:35 -0700629// ----------- AudioPolicyService::AudioCommandThread implementation ----------
630
631AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name)
632 : Thread(false), mName(name)
633{
634 mpToneGenerator = NULL;
635}
636
637
638AudioPolicyService::AudioCommandThread::~AudioCommandThread()
639{
640 if (mName != "" && !mAudioCommands.isEmpty()) {
641 release_wake_lock(mName.string());
642 }
643 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800644 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700645}
646
647void AudioPolicyService::AudioCommandThread::onFirstRef()
648{
649 if (mName != "") {
650 run(mName.string(), ANDROID_PRIORITY_AUDIO);
651 } else {
Glenn Kasten480b4682012-02-28 12:30:08 -0800652 run("AudioCommand", ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700653 }
654}
655
656bool AudioPolicyService::AudioCommandThread::threadLoop()
657{
658 nsecs_t waitTime = INT64_MAX;
659
660 mLock.lock();
661 while (!exitPending())
662 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700663 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700664 nsecs_t curTime = systemTime();
665 // commands are sorted by increasing time stamp: execute them from index 0 and up
666 if (mAudioCommands[0]->mTime <= curTime) {
667 AudioCommand *command = mAudioCommands[0];
668 mAudioCommands.removeAt(0);
669 mLastCommand = *command;
670
671 switch (command->mCommand) {
672 case START_TONE: {
673 mLock.unlock();
674 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100675 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700676 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800677 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
679 mpToneGenerator->startTone(data->mType);
680 delete data;
681 mLock.lock();
682 }break;
683 case STOP_TONE: {
684 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100685 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700686 if (mpToneGenerator != NULL) {
687 mpToneGenerator->stopTone();
688 delete mpToneGenerator;
689 mpToneGenerator = NULL;
690 }
691 mLock.lock();
692 }break;
693 case SET_VOLUME: {
694 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100695 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700696 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
697 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
698 data->mVolume,
699 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700700 if (command->mWaitStatus) {
701 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100702 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700703 }
704 delete data;
705 }break;
706 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700707 ParametersData *data = (ParametersData *)command->mParam;
708 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
709 data->mKeyValuePairs.string(), data->mIO);
710 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
711 if (command->mWaitStatus) {
712 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100713 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700714 }
715 delete data;
716 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700717 case SET_VOICE_VOLUME: {
718 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100719 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700720 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700721 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
722 if (command->mWaitStatus) {
723 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100724 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700725 }
726 delete data;
727 }break;
728 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000729 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700730 }
731 delete command;
732 waitTime = INT64_MAX;
733 } else {
734 waitTime = mAudioCommands[0]->mTime - curTime;
735 break;
736 }
737 }
738 // release delayed commands wake lock
739 if (mName != "" && mAudioCommands.isEmpty()) {
740 release_wake_lock(mName.string());
741 }
Steve Block3856b092011-10-20 11:56:00 +0100742 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700743 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100744 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700745 }
746 mLock.unlock();
747 return false;
748}
749
750status_t AudioPolicyService::AudioCommandThread::dump(int fd)
751{
752 const size_t SIZE = 256;
753 char buffer[SIZE];
754 String8 result;
755
756 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
757 result.append(buffer);
758 write(fd, result.string(), result.size());
759
760 bool locked = tryLock(mLock);
761 if (!locked) {
762 String8 result2(kCmdDeadlockedString);
763 write(fd, result2.string(), result2.size());
764 }
765
766 snprintf(buffer, SIZE, "- Commands:\n");
767 result = String8(buffer);
768 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800769 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700770 mAudioCommands[i]->dump(buffer, SIZE);
771 result.append(buffer);
772 }
773 result.append(" Last Command\n");
774 mLastCommand.dump(buffer, SIZE);
775 result.append(buffer);
776
777 write(fd, result.string(), result.size());
778
779 if (locked) mLock.unlock();
780
781 return NO_ERROR;
782}
783
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800784void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
785 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700786{
787 AudioCommand *command = new AudioCommand();
788 command->mCommand = START_TONE;
789 ToneData *data = new ToneData();
790 data->mType = type;
791 data->mStream = stream;
792 command->mParam = (void *)data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700793 Mutex::Autolock _l(mLock);
794 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100795 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700796 mWaitWorkCV.signal();
797}
798
799void AudioPolicyService::AudioCommandThread::stopToneCommand()
800{
801 AudioCommand *command = new AudioCommand();
802 command->mCommand = STOP_TONE;
803 command->mParam = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700804 Mutex::Autolock _l(mLock);
805 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100806 ALOGV("AudioCommandThread() adding tone stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700807 mWaitWorkCV.signal();
808}
809
Glenn Kastenfff6d712012-01-12 16:38:12 -0800810status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700811 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800812 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700813 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814{
815 status_t status = NO_ERROR;
816
817 AudioCommand *command = new AudioCommand();
818 command->mCommand = SET_VOLUME;
819 VolumeData *data = new VolumeData();
820 data->mStream = stream;
821 data->mVolume = volume;
822 data->mIO = output;
823 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700824 Mutex::Autolock _l(mLock);
825 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100826 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700827 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700828 mWaitWorkCV.signal();
829 if (command->mWaitStatus) {
830 command->mCond.wait(mLock);
831 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100832 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700833 }
834 return status;
835}
836
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800837status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700838 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700839 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700840{
841 status_t status = NO_ERROR;
842
843 AudioCommand *command = new AudioCommand();
844 command->mCommand = SET_PARAMETERS;
845 ParametersData *data = new ParametersData();
846 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700847 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700848 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700849 Mutex::Autolock _l(mLock);
850 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100851 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700852 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700853 mWaitWorkCV.signal();
854 if (command->mWaitStatus) {
855 command->mCond.wait(mLock);
856 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100857 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700858 }
859 return status;
860}
861
862status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
863{
864 status_t status = NO_ERROR;
865
866 AudioCommand *command = new AudioCommand();
867 command->mCommand = SET_VOICE_VOLUME;
868 VoiceVolumeData *data = new VoiceVolumeData();
869 data->mVolume = volume;
870 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700871 Mutex::Autolock _l(mLock);
872 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100873 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700874 mWaitWorkCV.signal();
875 if (command->mWaitStatus) {
876 command->mCond.wait(mLock);
877 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100878 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700879 }
880 return status;
881}
882
883// insertCommand_l() must be called with mLock held
884void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
885{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800886 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700887 Vector <AudioCommand *> removedCommands;
888
Eric Laurentcec4abb2012-07-03 12:23:02 -0700889 nsecs_t time = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700890 command->mTime = systemTime() + milliseconds(delayMs);
891
892 // acquire wake lock to make sure delayed commands are processed
893 if (mName != "" && mAudioCommands.isEmpty()) {
894 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
895 }
896
897 // check same pending commands with later time stamps and eliminate them
898 for (i = mAudioCommands.size()-1; i >= 0; i--) {
899 AudioCommand *command2 = mAudioCommands[i];
900 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
901 if (command2->mTime <= command->mTime) break;
902 if (command2->mCommand != command->mCommand) continue;
903
904 switch (command->mCommand) {
905 case SET_PARAMETERS: {
906 ParametersData *data = (ParametersData *)command->mParam;
907 ParametersData *data2 = (ParametersData *)command2->mParam;
908 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100909 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700910 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911 AudioParameter param = AudioParameter(data->mKeyValuePairs);
912 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
913 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700914 String8 key;
915 String8 value;
916 param.getAt(j, key, value);
917 for (size_t k = 0; k < param2.size(); k++) {
918 String8 key2;
919 String8 value2;
920 param2.getAt(k, key2, value2);
921 if (key2 == key) {
922 param2.remove(key2);
923 ALOGV("Filtering out parameter %s", key2.string());
924 break;
925 }
926 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700927 }
928 // if all keys have been filtered out, remove the command.
929 // otherwise, update the key value pairs
930 if (param2.size() == 0) {
931 removedCommands.add(command2);
932 } else {
933 data2->mKeyValuePairs = param2.toString();
934 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700935 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700936 } break;
937
938 case SET_VOLUME: {
939 VolumeData *data = (VolumeData *)command->mParam;
940 VolumeData *data2 = (VolumeData *)command2->mParam;
941 if (data->mIO != data2->mIO) break;
942 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100943 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700944 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700945 removedCommands.add(command2);
Eric Laurentcec4abb2012-07-03 12:23:02 -0700946 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947 } break;
948 case START_TONE:
949 case STOP_TONE:
950 default:
951 break;
952 }
953 }
954
955 // remove filtered commands
956 for (size_t j = 0; j < removedCommands.size(); j++) {
957 // removed commands always have time stamps greater than current command
958 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
959 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +0100960 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961 mAudioCommands.removeAt(k);
962 break;
963 }
964 }
965 }
966 removedCommands.clear();
967
Eric Laurentcec4abb2012-07-03 12:23:02 -0700968 // wait for status only if delay is 0 and command time was not modified above
969 if (delayMs == 0 && time == 0) {
970 command->mWaitStatus = true;
971 } else {
972 command->mWaitStatus = false;
973 }
974 // update command time if modified above
975 if (time != 0) {
976 command->mTime = time;
977 }
978
Mathias Agopian65ab4712010-07-14 17:59:35 -0700979 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +0100980 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -0700981 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700982 mAudioCommands.insertAt(command, i + 1);
983}
984
985void AudioPolicyService::AudioCommandThread::exit()
986{
Steve Block3856b092011-10-20 11:56:00 +0100987 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700988 {
989 AutoMutex _l(mLock);
990 requestExit();
991 mWaitWorkCV.signal();
992 }
993 requestExitAndWait();
994}
995
996void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
997{
998 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
999 mCommand,
1000 (int)ns2s(mTime),
1001 (int)ns2ms(mTime)%1000,
1002 mWaitStatus,
1003 mParam);
1004}
1005
Dima Zavinfce7a472011-04-19 22:30:36 -07001006/******* helpers for the service_ops callbacks defined below *********/
1007void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1008 const char *keyValuePairs,
1009 int delayMs)
1010{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001011 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001012 delayMs);
1013}
1014
1015int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1016 float volume,
1017 audio_io_handle_t output,
1018 int delayMs)
1019{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001020 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001021 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001022}
1023
1024int AudioPolicyService::startTone(audio_policy_tone_t tone,
1025 audio_stream_type_t stream)
1026{
1027 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
Steve Block29357bc2012-01-06 19:20:56 +00001028 ALOGE("startTone: illegal tone requested (%d)", tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001029 if (stream != AUDIO_STREAM_VOICE_CALL)
Steve Block29357bc2012-01-06 19:20:56 +00001030 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001031 tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001032 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1033 AUDIO_STREAM_VOICE_CALL);
1034 return 0;
1035}
1036
1037int AudioPolicyService::stopTone()
1038{
1039 mTonePlaybackThread->stopToneCommand();
1040 return 0;
1041}
1042
1043int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1044{
1045 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1046}
1047
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001048// ----------------------------------------------------------------------------
1049// Audio pre-processing configuration
1050// ----------------------------------------------------------------------------
1051
Glenn Kasten8dad0e32012-01-09 08:41:22 -08001052/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001053 MIC_SRC_TAG,
1054 VOICE_UL_SRC_TAG,
1055 VOICE_DL_SRC_TAG,
1056 VOICE_CALL_SRC_TAG,
1057 CAMCORDER_SRC_TAG,
1058 VOICE_REC_SRC_TAG,
1059 VOICE_COMM_SRC_TAG
1060};
1061
1062// returns the audio_source_t enum corresponding to the input source name or
1063// AUDIO_SOURCE_CNT is no match found
1064audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
1065{
1066 int i;
1067 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
1068 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001069 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001070 break;
1071 }
1072 }
1073 return (audio_source_t)i;
1074}
1075
1076size_t AudioPolicyService::growParamSize(char *param,
1077 size_t size,
1078 size_t *curSize,
1079 size_t *totSize)
1080{
1081 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
1082 size_t pos = ((*curSize - 1 ) / size + 1) * size;
1083
1084 if (pos + size > *totSize) {
1085 while (pos + size > *totSize) {
1086 *totSize += ((*totSize + 7) / 8) * 4;
1087 }
1088 param = (char *)realloc(param, *totSize);
1089 }
1090 *curSize = pos + size;
1091 return pos;
1092}
1093
1094size_t AudioPolicyService::readParamValue(cnode *node,
1095 char *param,
1096 size_t *curSize,
1097 size_t *totSize)
1098{
1099 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
1100 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
1101 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001102 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001103 return sizeof(short);
1104 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
1105 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
1106 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001107 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001108 return sizeof(int);
1109 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
1110 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
1111 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001112 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001113 return sizeof(float);
1114 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
1115 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
1116 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1117 *(bool *)((char *)param + pos) = false;
1118 } else {
1119 *(bool *)((char *)param + pos) = true;
1120 }
Steve Block3856b092011-10-20 11:56:00 +01001121 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001122 return sizeof(bool);
1123 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1124 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1125 if (*curSize + len + 1 > *totSize) {
1126 *totSize = *curSize + len + 1;
1127 param = (char *)realloc(param, *totSize);
1128 }
1129 strncpy(param + *curSize, node->value, len);
1130 *curSize += len;
1131 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +01001132 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001133 return len;
1134 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001135 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001136 return 0;
1137}
1138
1139effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1140{
1141 cnode *param;
1142 cnode *value;
1143 size_t curSize = sizeof(effect_param_t);
1144 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1145 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1146
1147 param = config_find(root, PARAM_TAG);
1148 value = config_find(root, VALUE_TAG);
1149 if (param == NULL && value == NULL) {
1150 // try to parse simple parameter form {int int}
1151 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -08001152 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001153 // Note: that a pair of random strings is read as 0 0
1154 int *ptr = (int *)fx_param->data;
1155 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +00001156 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001157 *ptr++ = atoi(param->name);
1158 *ptr = atoi(param->value);
1159 fx_param->psize = sizeof(int);
1160 fx_param->vsize = sizeof(int);
1161 return fx_param;
1162 }
1163 }
1164 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001165 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001166 goto error;
1167 }
1168
1169 fx_param->psize = 0;
1170 param = param->first_child;
1171 while (param) {
Steve Block3856b092011-10-20 11:56:00 +01001172 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001173 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1174 if (size == 0) {
1175 goto error;
1176 }
1177 fx_param->psize += size;
1178 param = param->next;
1179 }
1180
1181 // align start of value field on 32 bit boundary
1182 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1183
1184 fx_param->vsize = 0;
1185 value = value->first_child;
1186 while (value) {
Steve Block3856b092011-10-20 11:56:00 +01001187 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001188 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1189 if (size == 0) {
1190 goto error;
1191 }
1192 fx_param->vsize += size;
1193 value = value->next;
1194 }
1195
1196 return fx_param;
1197
1198error:
1199 delete fx_param;
1200 return NULL;
1201}
1202
1203void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1204{
1205 cnode *node = root->first_child;
1206 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001207 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001208 effect_param_t *param = loadEffectParameter(node);
1209 if (param == NULL) {
1210 node = node->next;
1211 continue;
1212 }
1213 params.add(param);
1214 node = node->next;
1215 }
1216}
1217
1218AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1219 cnode *root,
1220 const Vector <EffectDesc *>& effects)
1221{
1222 cnode *node = root->first_child;
1223 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001224 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001225 return NULL;
1226 }
1227 InputSourceDesc *source = new InputSourceDesc();
1228 while (node) {
1229 size_t i;
1230 for (i = 0; i < effects.size(); i++) {
1231 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001232 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001233 break;
1234 }
1235 }
1236 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +01001237 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001238 node = node->next;
1239 continue;
1240 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001241 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001242 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +01001243 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001244 source->mEffects.add(effect);
1245 node = node->next;
1246 }
1247 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001248 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001249 delete source;
1250 return NULL;
1251 }
1252 return source;
1253}
1254
1255status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1256{
1257 cnode *node = config_find(root, PREPROCESSING_TAG);
1258 if (node == NULL) {
1259 return -ENOENT;
1260 }
1261 node = node->first_child;
1262 while (node) {
1263 audio_source_t source = inputSourceNameToEnum(node->name);
1264 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001265 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001266 node = node->next;
1267 continue;
1268 }
Steve Block3856b092011-10-20 11:56:00 +01001269 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001270 InputSourceDesc *desc = loadInputSource(node, effects);
1271 if (desc == NULL) {
1272 node = node->next;
1273 continue;
1274 }
1275 mInputSources.add(source, desc);
1276 node = node->next;
1277 }
1278 return NO_ERROR;
1279}
1280
1281AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1282{
1283 cnode *node = config_find(root, UUID_TAG);
1284 if (node == NULL) {
1285 return NULL;
1286 }
1287 effect_uuid_t uuid;
1288 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001289 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001290 return NULL;
1291 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001292 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001293}
1294
1295status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1296{
1297 cnode *node = config_find(root, EFFECTS_TAG);
1298 if (node == NULL) {
1299 return -ENOENT;
1300 }
1301 node = node->first_child;
1302 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001303 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001304 EffectDesc *effect = loadEffect(node);
1305 if (effect == NULL) {
1306 node = node->next;
1307 continue;
1308 }
1309 effects.add(effect);
1310 node = node->next;
1311 }
1312 return NO_ERROR;
1313}
1314
1315status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1316{
1317 cnode *root;
1318 char *data;
1319
1320 data = (char *)load_file(path, NULL);
1321 if (data == NULL) {
1322 return -ENODEV;
1323 }
1324 root = config_node("", "");
1325 config_load(root, data);
1326
1327 Vector <EffectDesc *> effects;
1328 loadEffects(root, effects);
1329 loadInputSources(root, effects);
1330
1331 config_free(root);
1332 free(root);
1333 free(data);
1334
1335 return NO_ERROR;
1336}
1337
Dima Zavinfce7a472011-04-19 22:30:36 -07001338/* implementation of the interface to the policy manager */
1339extern "C" {
1340
Eric Laurenta4c5a552012-03-29 10:12:40 -07001341
1342static audio_module_handle_t aps_load_hw_module(void *service,
1343 const char *name)
Dima Zavinfce7a472011-04-19 22:30:36 -07001344{
1345 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001346 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001347 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001348 return 0;
1349 }
1350
Eric Laurenta4c5a552012-03-29 10:12:40 -07001351 return af->loadHwModule(name);
1352}
1353
1354// deprecated: replaced by aps_open_output_on_module()
1355static audio_io_handle_t aps_open_output(void *service,
1356 audio_devices_t *pDevices,
1357 uint32_t *pSamplingRate,
1358 audio_format_t *pFormat,
1359 audio_channel_mask_t *pChannelMask,
1360 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001361 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001362{
1363 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1364 if (af == 0) {
1365 ALOGW("%s: could not get AudioFlinger", __func__);
1366 return 0;
1367 }
1368
1369 return af->openOutput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask,
1370 pLatencyMs, flags);
1371}
1372
1373static audio_io_handle_t aps_open_output_on_module(void *service,
1374 audio_module_handle_t module,
1375 audio_devices_t *pDevices,
1376 uint32_t *pSamplingRate,
1377 audio_format_t *pFormat,
1378 audio_channel_mask_t *pChannelMask,
1379 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001380 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001381{
1382 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1383 if (af == 0) {
1384 ALOGW("%s: could not get AudioFlinger", __func__);
1385 return 0;
1386 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001387 return af->openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
Dima Zavinfce7a472011-04-19 22:30:36 -07001388 pLatencyMs, flags);
1389}
1390
1391static audio_io_handle_t aps_open_dup_output(void *service,
1392 audio_io_handle_t output1,
1393 audio_io_handle_t output2)
1394{
1395 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001396 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001397 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001398 return 0;
1399 }
1400 return af->openDuplicateOutput(output1, output2);
1401}
1402
1403static int aps_close_output(void *service, audio_io_handle_t output)
1404{
1405 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001406 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001407 return PERMISSION_DENIED;
1408
1409 return af->closeOutput(output);
1410}
1411
1412static int aps_suspend_output(void *service, audio_io_handle_t output)
1413{
1414 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001415 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001416 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001417 return PERMISSION_DENIED;
1418 }
1419
1420 return af->suspendOutput(output);
1421}
1422
1423static int aps_restore_output(void *service, audio_io_handle_t output)
1424{
1425 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001426 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001427 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001428 return PERMISSION_DENIED;
1429 }
1430
1431 return af->restoreOutput(output);
1432}
1433
Glenn Kasten20010052012-06-22 13:43:51 -07001434// deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
Dima Zavinfce7a472011-04-19 22:30:36 -07001435static audio_io_handle_t aps_open_input(void *service,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001436 audio_devices_t *pDevices,
1437 uint32_t *pSamplingRate,
1438 audio_format_t *pFormat,
1439 audio_channel_mask_t *pChannelMask,
1440 audio_in_acoustics_t acoustics)
Dima Zavinfce7a472011-04-19 22:30:36 -07001441{
1442 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001443 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001444 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001445 return 0;
1446 }
1447
Eric Laurenta4c5a552012-03-29 10:12:40 -07001448 return af->openInput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask);
1449}
1450
1451static audio_io_handle_t aps_open_input_on_module(void *service,
1452 audio_module_handle_t module,
1453 audio_devices_t *pDevices,
1454 uint32_t *pSamplingRate,
1455 audio_format_t *pFormat,
1456 audio_channel_mask_t *pChannelMask)
1457{
1458 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1459 if (af == 0) {
1460 ALOGW("%s: could not get AudioFlinger", __func__);
1461 return 0;
1462 }
1463
1464 return af->openInput(module, pDevices, pSamplingRate, pFormat, pChannelMask);
Dima Zavinfce7a472011-04-19 22:30:36 -07001465}
1466
1467static int aps_close_input(void *service, audio_io_handle_t input)
1468{
1469 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001470 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001471 return PERMISSION_DENIED;
1472
1473 return af->closeInput(input);
1474}
1475
1476static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1477 audio_io_handle_t output)
1478{
1479 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001480 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001481 return PERMISSION_DENIED;
1482
1483 return af->setStreamOutput(stream, output);
1484}
1485
1486static int aps_move_effects(void *service, int session,
1487 audio_io_handle_t src_output,
1488 audio_io_handle_t dst_output)
1489{
1490 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001491 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001492 return PERMISSION_DENIED;
1493
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001494 return af->moveEffects(session, src_output, dst_output);
Dima Zavinfce7a472011-04-19 22:30:36 -07001495}
1496
1497static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1498 const char *keys)
1499{
1500 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1501 return strdup(result.string());
1502}
1503
1504static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1505 const char *kv_pairs, int delay_ms)
1506{
1507 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1508
1509 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1510}
1511
1512static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1513 float volume, audio_io_handle_t output,
1514 int delay_ms)
1515{
1516 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1517
1518 return audioPolicyService->setStreamVolume(stream, volume, output,
1519 delay_ms);
1520}
1521
1522static int aps_start_tone(void *service, audio_policy_tone_t tone,
1523 audio_stream_type_t stream)
1524{
1525 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1526
1527 return audioPolicyService->startTone(tone, stream);
1528}
1529
1530static int aps_stop_tone(void *service)
1531{
1532 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1533
1534 return audioPolicyService->stopTone();
1535}
1536
1537static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1538{
1539 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1540
1541 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1542}
1543
1544}; // extern "C"
1545
1546namespace {
1547 struct audio_policy_service_ops aps_ops = {
1548 open_output : aps_open_output,
1549 open_duplicate_output : aps_open_dup_output,
1550 close_output : aps_close_output,
1551 suspend_output : aps_suspend_output,
1552 restore_output : aps_restore_output,
1553 open_input : aps_open_input,
1554 close_input : aps_close_input,
1555 set_stream_volume : aps_set_stream_volume,
1556 set_stream_output : aps_set_stream_output,
1557 set_parameters : aps_set_parameters,
1558 get_parameters : aps_get_parameters,
1559 start_tone : aps_start_tone,
1560 stop_tone : aps_stop_tone,
1561 set_voice_volume : aps_set_voice_volume,
1562 move_effects : aps_move_effects,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001563 load_hw_module : aps_load_hw_module,
1564 open_output_on_module : aps_open_output_on_module,
1565 open_input_on_module : aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001566 };
1567}; // namespace <unnamed>
1568
Mathias Agopian65ab4712010-07-14 17:59:35 -07001569}; // namespace android