blob: 94f22b173b043be9b6612d8a1680f939b0667621 [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
Glenn Kasten411e4472012-11-02 10:00:06 -0700150 ALOGV("setDeviceConnectionState()");
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
Glenn Kasten411e4472012-11-02 10:00:06 -0700179 ALOGV("setPhoneState()");
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 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700204 ALOGV("setForceUse()");
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 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700230 ALOGV("getOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700231 Mutex::Autolock _l(mLock);
Glenn Kasten8af901c2012-11-01 11:11:38 -0700232 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, format, channelMask,
233 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700234}
235
Eric Laurentde070132010-07-13 04:45:46 -0700236status_t AudioPolicyService::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700237 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700238 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700239{
Dima Zavinfce7a472011-04-19 22:30:36 -0700240 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700241 return NO_INIT;
242 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700243 ALOGV("startOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700245 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700246}
247
Eric Laurentde070132010-07-13 04:45:46 -0700248status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700249 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700250 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700251{
Dima Zavinfce7a472011-04-19 22:30:36 -0700252 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700253 return NO_INIT;
254 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700255 ALOGV("stopOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700256 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700257 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700258}
259
260void AudioPolicyService::releaseOutput(audio_io_handle_t output)
261{
Dima Zavinfce7a472011-04-19 22:30:36 -0700262 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700263 return;
264 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700265 ALOGV("releaseOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700266 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700267 mpAudioPolicy->release_output(mpAudioPolicy, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700268}
269
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800270audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700271 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800272 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700273 audio_channel_mask_t channelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700274 int audioSession)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700275{
Dima Zavinfce7a472011-04-19 22:30:36 -0700276 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700277 return 0;
278 }
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800279 // already checked by client, but double-check in case the client wrapper is bypassed
280 if (uint32_t(inputSource) >= AUDIO_SOURCE_CNT) {
281 return 0;
282 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700283 Mutex::Autolock _l(mLock);
Glenn Kasten20010052012-06-22 13:43:51 -0700284 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700285 audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
Glenn Kasten8af901c2012-11-01 11:11:38 -0700286 format, channelMask, (audio_in_acoustics_t) 0);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700287
288 if (input == 0) {
289 return input;
290 }
291 // create audio pre processors according to input source
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800292 ssize_t index = mInputSources.indexOfKey(inputSource);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700293 if (index < 0) {
294 return input;
295 }
296 ssize_t idx = mInputs.indexOfKey(input);
297 InputDesc *inputDesc;
298 if (idx < 0) {
Glenn Kasten81872a22012-03-07 16:49:22 -0800299 inputDesc = new InputDesc(audioSession);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700300 mInputs.add(input, inputDesc);
301 } else {
302 inputDesc = mInputs.valueAt(idx);
303 }
304
305 Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects;
306 for (size_t i = 0; i < effects.size(); i++) {
307 EffectDesc *effect = effects[i];
308 sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input);
309 status_t status = fx->initCheck();
310 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000311 ALOGW("Failed to create Fx %s on input %d", effect->mName, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700312 // fx goes out of scope and strong ref on AudioEffect is released
313 continue;
314 }
315 for (size_t j = 0; j < effect->mParams.size(); j++) {
316 fx->setParameter(effect->mParams[j]);
317 }
318 inputDesc->mEffects.add(fx);
319 }
320 setPreProcessorEnabled(inputDesc, true);
321 return input;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700322}
323
324status_t AudioPolicyService::startInput(audio_io_handle_t input)
325{
Dima Zavinfce7a472011-04-19 22:30:36 -0700326 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700327 return NO_INIT;
328 }
329 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700330
Dima Zavinfce7a472011-04-19 22:30:36 -0700331 return mpAudioPolicy->start_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700332}
333
334status_t AudioPolicyService::stopInput(audio_io_handle_t input)
335{
Dima Zavinfce7a472011-04-19 22:30:36 -0700336 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700337 return NO_INIT;
338 }
339 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700340
Dima Zavinfce7a472011-04-19 22:30:36 -0700341 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700342}
343
344void AudioPolicyService::releaseInput(audio_io_handle_t input)
345{
Dima Zavinfce7a472011-04-19 22:30:36 -0700346 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700347 return;
348 }
349 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700350 mpAudioPolicy->release_input(mpAudioPolicy, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700351
352 ssize_t index = mInputs.indexOfKey(input);
353 if (index < 0) {
354 return;
355 }
356 InputDesc *inputDesc = mInputs.valueAt(index);
357 setPreProcessorEnabled(inputDesc, false);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700358 delete inputDesc;
359 mInputs.removeItemsAt(index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360}
361
Dima Zavinfce7a472011-04-19 22:30:36 -0700362status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700363 int indexMin,
364 int indexMax)
365{
Dima Zavinfce7a472011-04-19 22:30:36 -0700366 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700367 return NO_INIT;
368 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800369 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370 return PERMISSION_DENIED;
371 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800372 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373 return BAD_VALUE;
374 }
Eric Laurent5f121362012-06-15 14:45:03 -0700375 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700376 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700377 return NO_ERROR;
378}
379
Eric Laurent83844cc2011-11-18 16:43:31 -0800380status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
381 int index,
382 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700383{
Dima Zavinfce7a472011-04-19 22:30:36 -0700384 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385 return NO_INIT;
386 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800387 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388 return PERMISSION_DENIED;
389 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800390 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700391 return BAD_VALUE;
392 }
Eric Laurent5f121362012-06-15 14:45:03 -0700393 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800394 if (mpAudioPolicy->set_stream_volume_index_for_device) {
395 return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
396 stream,
397 index,
398 device);
399 } else {
400 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
401 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700402}
403
Eric Laurent83844cc2011-11-18 16:43:31 -0800404status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
405 int *index,
406 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700407{
Dima Zavinfce7a472011-04-19 22:30:36 -0700408 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409 return NO_INIT;
410 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800411 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700412 return BAD_VALUE;
413 }
Eric Laurent5f121362012-06-15 14:45:03 -0700414 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800415 if (mpAudioPolicy->get_stream_volume_index_for_device) {
416 return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
417 stream,
418 index,
419 device);
420 } else {
421 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
422 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700423}
424
Dima Zavinfce7a472011-04-19 22:30:36 -0700425uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700426{
Dima Zavinfce7a472011-04-19 22:30:36 -0700427 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700428 return 0;
429 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700430 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurentde070132010-07-13 04:45:46 -0700431}
432
Eric Laurent63742522012-03-08 13:42:42 -0800433//audio policy: use audio_device_t appropriately
434
435audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800436{
Dima Zavinfce7a472011-04-19 22:30:36 -0700437 if (mpAudioPolicy == NULL) {
Eric Laurent63742522012-03-08 13:42:42 -0800438 return (audio_devices_t)0;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800439 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700440 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800441}
442
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700443audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700444{
Dima Zavinfce7a472011-04-19 22:30:36 -0700445 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700446 return NO_INIT;
447 }
448 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700449 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700450}
451
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700452status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700453 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700454 uint32_t strategy,
455 int session,
456 int id)
457{
Dima Zavinfce7a472011-04-19 22:30:36 -0700458 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700459 return NO_INIT;
460 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700461 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700462}
463
464status_t AudioPolicyService::unregisterEffect(int id)
465{
Dima Zavinfce7a472011-04-19 22:30:36 -0700466 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700467 return NO_INIT;
468 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700469 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700470}
471
Eric Laurentdb7c0792011-08-10 10:37:50 -0700472status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
473{
474 if (mpAudioPolicy == NULL) {
475 return NO_INIT;
476 }
477 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
478}
479
Glenn Kastenfff6d712012-01-12 16:38:12 -0800480bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800481{
Dima Zavinfce7a472011-04-19 22:30:36 -0700482 if (mpAudioPolicy == NULL) {
Eric Laurenteda6c362011-02-02 09:33:30 -0800483 return 0;
484 }
485 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700486 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800487}
488
Jean-Michel Trivie336f912013-02-04 16:26:02 -0800489bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
490{
491 if (mpAudioPolicy == NULL) {
492 return 0;
493 }
494 Mutex::Autolock _l(mLock);
495 return mpAudioPolicy->is_stream_active_remotely(mpAudioPolicy, stream, inPastMs);
496}
497
Jean-Michel Trivie3f641f2012-10-10 12:11:16 -0700498bool AudioPolicyService::isSourceActive(audio_source_t source) const
499{
500 if (mpAudioPolicy == NULL) {
501 return false;
502 }
503 if (mpAudioPolicy->is_source_active == 0) {
504 return false;
505 }
506 Mutex::Autolock _l(mLock);
507 return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
508}
509
Eric Laurent57dae992011-07-24 13:36:09 -0700510status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
511 effect_descriptor_t *descriptors,
512 uint32_t *count)
513{
514
515 if (mpAudioPolicy == NULL) {
516 *count = 0;
517 return NO_INIT;
518 }
519 Mutex::Autolock _l(mLock);
520 status_t status = NO_ERROR;
521
522 size_t index;
523 for (index = 0; index < mInputs.size(); index++) {
524 if (mInputs.valueAt(index)->mSessionId == audioSession) {
525 break;
526 }
527 }
528 if (index == mInputs.size()) {
529 *count = 0;
530 return BAD_VALUE;
531 }
532 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
533
534 for (size_t i = 0; i < effects.size(); i++) {
535 effect_descriptor_t desc = effects[i]->descriptor();
536 if (i < *count) {
Glenn Kastena189a682012-02-20 12:16:30 -0800537 descriptors[i] = desc;
Eric Laurent57dae992011-07-24 13:36:09 -0700538 }
539 }
540 if (effects.size() > *count) {
541 status = NO_MEMORY;
542 }
543 *count = effects.size();
544 return status;
545}
546
Mathias Agopian65ab4712010-07-14 17:59:35 -0700547void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700548 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700549 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550}
551
552static bool tryLock(Mutex& mutex)
553{
554 bool locked = false;
555 for (int i = 0; i < kDumpLockRetries; ++i) {
556 if (mutex.tryLock() == NO_ERROR) {
557 locked = true;
558 break;
559 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800560 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700561 }
562 return locked;
563}
564
565status_t AudioPolicyService::dumpInternals(int fd)
566{
567 const size_t SIZE = 256;
568 char buffer[SIZE];
569 String8 result;
570
Dima Zavinfce7a472011-04-19 22:30:36 -0700571 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700572 result.append(buffer);
573 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
574 result.append(buffer);
575 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
576 result.append(buffer);
577
578 write(fd, result.string(), result.size());
579 return NO_ERROR;
580}
581
582status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
583{
Glenn Kasten44deb052012-02-05 18:09:08 -0800584 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700585 dumpPermissionDenial(fd);
586 } else {
587 bool locked = tryLock(mLock);
588 if (!locked) {
589 String8 result(kDeadlockedString);
590 write(fd, result.string(), result.size());
591 }
592
593 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800594 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 mAudioCommandThread->dump(fd);
596 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800597 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700598 mTonePlaybackThread->dump(fd);
599 }
600
Dima Zavinfce7a472011-04-19 22:30:36 -0700601 if (mpAudioPolicy) {
602 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700603 }
604
605 if (locked) mLock.unlock();
606 }
607 return NO_ERROR;
608}
609
610status_t AudioPolicyService::dumpPermissionDenial(int fd)
611{
612 const size_t SIZE = 256;
613 char buffer[SIZE];
614 String8 result;
615 snprintf(buffer, SIZE, "Permission Denial: "
616 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
617 IPCThreadState::self()->getCallingPid(),
618 IPCThreadState::self()->getCallingUid());
619 result.append(buffer);
620 write(fd, result.string(), result.size());
621 return NO_ERROR;
622}
623
Glenn Kasten81872a22012-03-07 16:49:22 -0800624void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700625{
Glenn Kasten81872a22012-03-07 16:49:22 -0800626 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700627 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800628 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700629 }
630}
631
Mathias Agopian65ab4712010-07-14 17:59:35 -0700632status_t AudioPolicyService::onTransact(
633 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
634{
635 return BnAudioPolicyService::onTransact(code, data, reply, flags);
636}
637
638
Mathias Agopian65ab4712010-07-14 17:59:35 -0700639// ----------- AudioPolicyService::AudioCommandThread implementation ----------
640
641AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name)
642 : Thread(false), mName(name)
643{
644 mpToneGenerator = NULL;
645}
646
647
648AudioPolicyService::AudioCommandThread::~AudioCommandThread()
649{
650 if (mName != "" && !mAudioCommands.isEmpty()) {
651 release_wake_lock(mName.string());
652 }
653 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800654 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700655}
656
657void AudioPolicyService::AudioCommandThread::onFirstRef()
658{
659 if (mName != "") {
660 run(mName.string(), ANDROID_PRIORITY_AUDIO);
661 } else {
Glenn Kasten480b4682012-02-28 12:30:08 -0800662 run("AudioCommand", ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700663 }
664}
665
666bool AudioPolicyService::AudioCommandThread::threadLoop()
667{
668 nsecs_t waitTime = INT64_MAX;
669
670 mLock.lock();
671 while (!exitPending())
672 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700673 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700674 nsecs_t curTime = systemTime();
675 // commands are sorted by increasing time stamp: execute them from index 0 and up
676 if (mAudioCommands[0]->mTime <= curTime) {
677 AudioCommand *command = mAudioCommands[0];
678 mAudioCommands.removeAt(0);
679 mLastCommand = *command;
680
681 switch (command->mCommand) {
682 case START_TONE: {
683 mLock.unlock();
684 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100685 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700686 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800687 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700688 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
689 mpToneGenerator->startTone(data->mType);
690 delete data;
691 mLock.lock();
692 }break;
693 case STOP_TONE: {
694 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100695 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700696 if (mpToneGenerator != NULL) {
697 mpToneGenerator->stopTone();
698 delete mpToneGenerator;
699 mpToneGenerator = NULL;
700 }
701 mLock.lock();
702 }break;
703 case SET_VOLUME: {
704 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100705 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700706 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
707 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
708 data->mVolume,
709 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700710 if (command->mWaitStatus) {
711 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100712 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700713 }
714 delete data;
715 }break;
716 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700717 ParametersData *data = (ParametersData *)command->mParam;
718 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
719 data->mKeyValuePairs.string(), data->mIO);
720 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
721 if (command->mWaitStatus) {
722 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100723 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700724 }
725 delete data;
726 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700727 case SET_VOICE_VOLUME: {
728 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100729 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700730 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700731 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
732 if (command->mWaitStatus) {
733 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100734 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735 }
736 delete data;
737 }break;
738 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000739 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700740 }
741 delete command;
742 waitTime = INT64_MAX;
743 } else {
744 waitTime = mAudioCommands[0]->mTime - curTime;
745 break;
746 }
747 }
748 // release delayed commands wake lock
749 if (mName != "" && mAudioCommands.isEmpty()) {
750 release_wake_lock(mName.string());
751 }
Steve Block3856b092011-10-20 11:56:00 +0100752 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700753 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100754 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700755 }
756 mLock.unlock();
757 return false;
758}
759
760status_t AudioPolicyService::AudioCommandThread::dump(int fd)
761{
762 const size_t SIZE = 256;
763 char buffer[SIZE];
764 String8 result;
765
766 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
767 result.append(buffer);
768 write(fd, result.string(), result.size());
769
770 bool locked = tryLock(mLock);
771 if (!locked) {
772 String8 result2(kCmdDeadlockedString);
773 write(fd, result2.string(), result2.size());
774 }
775
776 snprintf(buffer, SIZE, "- Commands:\n");
777 result = String8(buffer);
778 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800779 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700780 mAudioCommands[i]->dump(buffer, SIZE);
781 result.append(buffer);
782 }
783 result.append(" Last Command\n");
784 mLastCommand.dump(buffer, SIZE);
785 result.append(buffer);
786
787 write(fd, result.string(), result.size());
788
789 if (locked) mLock.unlock();
790
791 return NO_ERROR;
792}
793
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800794void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
795 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700796{
797 AudioCommand *command = new AudioCommand();
798 command->mCommand = START_TONE;
799 ToneData *data = new ToneData();
800 data->mType = type;
801 data->mStream = stream;
802 command->mParam = (void *)data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700803 Mutex::Autolock _l(mLock);
804 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100805 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700806 mWaitWorkCV.signal();
807}
808
809void AudioPolicyService::AudioCommandThread::stopToneCommand()
810{
811 AudioCommand *command = new AudioCommand();
812 command->mCommand = STOP_TONE;
813 command->mParam = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814 Mutex::Autolock _l(mLock);
815 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100816 ALOGV("AudioCommandThread() adding tone stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700817 mWaitWorkCV.signal();
818}
819
Glenn Kastenfff6d712012-01-12 16:38:12 -0800820status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700821 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800822 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700823 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700824{
825 status_t status = NO_ERROR;
826
827 AudioCommand *command = new AudioCommand();
828 command->mCommand = SET_VOLUME;
829 VolumeData *data = new VolumeData();
830 data->mStream = stream;
831 data->mVolume = volume;
832 data->mIO = output;
833 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700834 Mutex::Autolock _l(mLock);
835 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100836 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700837 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700838 mWaitWorkCV.signal();
839 if (command->mWaitStatus) {
840 command->mCond.wait(mLock);
841 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100842 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700843 }
844 return status;
845}
846
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800847status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700848 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700849 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700850{
851 status_t status = NO_ERROR;
852
853 AudioCommand *command = new AudioCommand();
854 command->mCommand = SET_PARAMETERS;
855 ParametersData *data = new ParametersData();
856 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700857 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700858 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700859 Mutex::Autolock _l(mLock);
860 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100861 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700862 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700863 mWaitWorkCV.signal();
864 if (command->mWaitStatus) {
865 command->mCond.wait(mLock);
866 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100867 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700868 }
869 return status;
870}
871
872status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
873{
874 status_t status = NO_ERROR;
875
876 AudioCommand *command = new AudioCommand();
877 command->mCommand = SET_VOICE_VOLUME;
878 VoiceVolumeData *data = new VoiceVolumeData();
879 data->mVolume = volume;
880 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700881 Mutex::Autolock _l(mLock);
882 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100883 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700884 mWaitWorkCV.signal();
885 if (command->mWaitStatus) {
886 command->mCond.wait(mLock);
887 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100888 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700889 }
890 return status;
891}
892
893// insertCommand_l() must be called with mLock held
894void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
895{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800896 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700897 Vector <AudioCommand *> removedCommands;
898
Eric Laurentcec4abb2012-07-03 12:23:02 -0700899 nsecs_t time = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700900 command->mTime = systemTime() + milliseconds(delayMs);
901
902 // acquire wake lock to make sure delayed commands are processed
903 if (mName != "" && mAudioCommands.isEmpty()) {
904 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
905 }
906
907 // check same pending commands with later time stamps and eliminate them
908 for (i = mAudioCommands.size()-1; i >= 0; i--) {
909 AudioCommand *command2 = mAudioCommands[i];
910 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
911 if (command2->mTime <= command->mTime) break;
912 if (command2->mCommand != command->mCommand) continue;
913
914 switch (command->mCommand) {
915 case SET_PARAMETERS: {
916 ParametersData *data = (ParametersData *)command->mParam;
917 ParametersData *data2 = (ParametersData *)command2->mParam;
918 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100919 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700920 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700921 AudioParameter param = AudioParameter(data->mKeyValuePairs);
922 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
923 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700924 String8 key;
925 String8 value;
926 param.getAt(j, key, value);
927 for (size_t k = 0; k < param2.size(); k++) {
928 String8 key2;
929 String8 value2;
930 param2.getAt(k, key2, value2);
931 if (key2 == key) {
932 param2.remove(key2);
933 ALOGV("Filtering out parameter %s", key2.string());
934 break;
935 }
936 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700937 }
938 // if all keys have been filtered out, remove the command.
939 // otherwise, update the key value pairs
940 if (param2.size() == 0) {
941 removedCommands.add(command2);
942 } else {
943 data2->mKeyValuePairs = param2.toString();
944 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700945 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946 } break;
947
948 case SET_VOLUME: {
949 VolumeData *data = (VolumeData *)command->mParam;
950 VolumeData *data2 = (VolumeData *)command2->mParam;
951 if (data->mIO != data2->mIO) break;
952 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100953 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700954 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700955 removedCommands.add(command2);
Eric Laurentcec4abb2012-07-03 12:23:02 -0700956 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700957 } break;
958 case START_TONE:
959 case STOP_TONE:
960 default:
961 break;
962 }
963 }
964
965 // remove filtered commands
966 for (size_t j = 0; j < removedCommands.size(); j++) {
967 // removed commands always have time stamps greater than current command
968 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
969 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +0100970 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971 mAudioCommands.removeAt(k);
972 break;
973 }
974 }
975 }
976 removedCommands.clear();
977
Eric Laurentcec4abb2012-07-03 12:23:02 -0700978 // wait for status only if delay is 0 and command time was not modified above
979 if (delayMs == 0 && time == 0) {
980 command->mWaitStatus = true;
981 } else {
982 command->mWaitStatus = false;
983 }
984 // update command time if modified above
985 if (time != 0) {
986 command->mTime = time;
987 }
988
Mathias Agopian65ab4712010-07-14 17:59:35 -0700989 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +0100990 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -0700991 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700992 mAudioCommands.insertAt(command, i + 1);
993}
994
995void AudioPolicyService::AudioCommandThread::exit()
996{
Steve Block3856b092011-10-20 11:56:00 +0100997 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700998 {
999 AutoMutex _l(mLock);
1000 requestExit();
1001 mWaitWorkCV.signal();
1002 }
1003 requestExitAndWait();
1004}
1005
1006void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1007{
1008 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1009 mCommand,
1010 (int)ns2s(mTime),
1011 (int)ns2ms(mTime)%1000,
1012 mWaitStatus,
1013 mParam);
1014}
1015
Dima Zavinfce7a472011-04-19 22:30:36 -07001016/******* helpers for the service_ops callbacks defined below *********/
1017void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1018 const char *keyValuePairs,
1019 int delayMs)
1020{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001021 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001022 delayMs);
1023}
1024
1025int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1026 float volume,
1027 audio_io_handle_t output,
1028 int delayMs)
1029{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001030 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001031 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001032}
1033
1034int AudioPolicyService::startTone(audio_policy_tone_t tone,
1035 audio_stream_type_t stream)
1036{
1037 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
Steve Block29357bc2012-01-06 19:20:56 +00001038 ALOGE("startTone: illegal tone requested (%d)", tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001039 if (stream != AUDIO_STREAM_VOICE_CALL)
Steve Block29357bc2012-01-06 19:20:56 +00001040 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001041 tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001042 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1043 AUDIO_STREAM_VOICE_CALL);
1044 return 0;
1045}
1046
1047int AudioPolicyService::stopTone()
1048{
1049 mTonePlaybackThread->stopToneCommand();
1050 return 0;
1051}
1052
1053int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1054{
1055 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1056}
1057
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001058// ----------------------------------------------------------------------------
1059// Audio pre-processing configuration
1060// ----------------------------------------------------------------------------
1061
Glenn Kasten8dad0e32012-01-09 08:41:22 -08001062/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001063 MIC_SRC_TAG,
1064 VOICE_UL_SRC_TAG,
1065 VOICE_DL_SRC_TAG,
1066 VOICE_CALL_SRC_TAG,
1067 CAMCORDER_SRC_TAG,
1068 VOICE_REC_SRC_TAG,
1069 VOICE_COMM_SRC_TAG
1070};
1071
1072// returns the audio_source_t enum corresponding to the input source name or
1073// AUDIO_SOURCE_CNT is no match found
1074audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
1075{
1076 int i;
1077 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
1078 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001079 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001080 break;
1081 }
1082 }
1083 return (audio_source_t)i;
1084}
1085
1086size_t AudioPolicyService::growParamSize(char *param,
1087 size_t size,
1088 size_t *curSize,
1089 size_t *totSize)
1090{
1091 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
1092 size_t pos = ((*curSize - 1 ) / size + 1) * size;
1093
1094 if (pos + size > *totSize) {
1095 while (pos + size > *totSize) {
1096 *totSize += ((*totSize + 7) / 8) * 4;
1097 }
1098 param = (char *)realloc(param, *totSize);
1099 }
1100 *curSize = pos + size;
1101 return pos;
1102}
1103
1104size_t AudioPolicyService::readParamValue(cnode *node,
1105 char *param,
1106 size_t *curSize,
1107 size_t *totSize)
1108{
1109 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
1110 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
1111 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001112 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001113 return sizeof(short);
1114 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
1115 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
1116 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001117 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001118 return sizeof(int);
1119 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
1120 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
1121 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001122 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001123 return sizeof(float);
1124 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
1125 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
1126 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1127 *(bool *)((char *)param + pos) = false;
1128 } else {
1129 *(bool *)((char *)param + pos) = true;
1130 }
Steve Block3856b092011-10-20 11:56:00 +01001131 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001132 return sizeof(bool);
1133 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1134 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1135 if (*curSize + len + 1 > *totSize) {
1136 *totSize = *curSize + len + 1;
1137 param = (char *)realloc(param, *totSize);
1138 }
1139 strncpy(param + *curSize, node->value, len);
1140 *curSize += len;
1141 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +01001142 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001143 return len;
1144 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001145 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001146 return 0;
1147}
1148
1149effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1150{
1151 cnode *param;
1152 cnode *value;
1153 size_t curSize = sizeof(effect_param_t);
1154 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1155 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1156
1157 param = config_find(root, PARAM_TAG);
1158 value = config_find(root, VALUE_TAG);
1159 if (param == NULL && value == NULL) {
1160 // try to parse simple parameter form {int int}
1161 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -08001162 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001163 // Note: that a pair of random strings is read as 0 0
1164 int *ptr = (int *)fx_param->data;
1165 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +00001166 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001167 *ptr++ = atoi(param->name);
1168 *ptr = atoi(param->value);
1169 fx_param->psize = sizeof(int);
1170 fx_param->vsize = sizeof(int);
1171 return fx_param;
1172 }
1173 }
1174 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001175 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001176 goto error;
1177 }
1178
1179 fx_param->psize = 0;
1180 param = param->first_child;
1181 while (param) {
Steve Block3856b092011-10-20 11:56:00 +01001182 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001183 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1184 if (size == 0) {
1185 goto error;
1186 }
1187 fx_param->psize += size;
1188 param = param->next;
1189 }
1190
1191 // align start of value field on 32 bit boundary
1192 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1193
1194 fx_param->vsize = 0;
1195 value = value->first_child;
1196 while (value) {
Steve Block3856b092011-10-20 11:56:00 +01001197 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001198 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1199 if (size == 0) {
1200 goto error;
1201 }
1202 fx_param->vsize += size;
1203 value = value->next;
1204 }
1205
1206 return fx_param;
1207
1208error:
1209 delete fx_param;
1210 return NULL;
1211}
1212
1213void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1214{
1215 cnode *node = root->first_child;
1216 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001217 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001218 effect_param_t *param = loadEffectParameter(node);
1219 if (param == NULL) {
1220 node = node->next;
1221 continue;
1222 }
1223 params.add(param);
1224 node = node->next;
1225 }
1226}
1227
1228AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1229 cnode *root,
1230 const Vector <EffectDesc *>& effects)
1231{
1232 cnode *node = root->first_child;
1233 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001234 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001235 return NULL;
1236 }
1237 InputSourceDesc *source = new InputSourceDesc();
1238 while (node) {
1239 size_t i;
1240 for (i = 0; i < effects.size(); i++) {
1241 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001242 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001243 break;
1244 }
1245 }
1246 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +01001247 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001248 node = node->next;
1249 continue;
1250 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001251 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001252 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +01001253 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001254 source->mEffects.add(effect);
1255 node = node->next;
1256 }
1257 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001258 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001259 delete source;
1260 return NULL;
1261 }
1262 return source;
1263}
1264
1265status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1266{
1267 cnode *node = config_find(root, PREPROCESSING_TAG);
1268 if (node == NULL) {
1269 return -ENOENT;
1270 }
1271 node = node->first_child;
1272 while (node) {
1273 audio_source_t source = inputSourceNameToEnum(node->name);
1274 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001275 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001276 node = node->next;
1277 continue;
1278 }
Steve Block3856b092011-10-20 11:56:00 +01001279 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001280 InputSourceDesc *desc = loadInputSource(node, effects);
1281 if (desc == NULL) {
1282 node = node->next;
1283 continue;
1284 }
1285 mInputSources.add(source, desc);
1286 node = node->next;
1287 }
1288 return NO_ERROR;
1289}
1290
1291AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1292{
1293 cnode *node = config_find(root, UUID_TAG);
1294 if (node == NULL) {
1295 return NULL;
1296 }
1297 effect_uuid_t uuid;
1298 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001299 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001300 return NULL;
1301 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001302 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001303}
1304
1305status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1306{
1307 cnode *node = config_find(root, EFFECTS_TAG);
1308 if (node == NULL) {
1309 return -ENOENT;
1310 }
1311 node = node->first_child;
1312 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001313 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001314 EffectDesc *effect = loadEffect(node);
1315 if (effect == NULL) {
1316 node = node->next;
1317 continue;
1318 }
1319 effects.add(effect);
1320 node = node->next;
1321 }
1322 return NO_ERROR;
1323}
1324
1325status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1326{
1327 cnode *root;
1328 char *data;
1329
1330 data = (char *)load_file(path, NULL);
1331 if (data == NULL) {
1332 return -ENODEV;
1333 }
1334 root = config_node("", "");
1335 config_load(root, data);
1336
1337 Vector <EffectDesc *> effects;
1338 loadEffects(root, effects);
1339 loadInputSources(root, effects);
1340
Yu Yezhonge6056ba2013-10-15 14:32:34 +08001341 // delete effects to fix memory leak.
1342 // as effects is local var and valgrind would treat this as memory leak
1343 // and although it only did in mediaserver init, but free it in case mediaserver reboot
1344 size_t i;
1345 for (i = 0; i < effects.size(); i++) {
1346 delete effects[i];
1347 }
1348
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001349 config_free(root);
1350 free(root);
1351 free(data);
1352
1353 return NO_ERROR;
1354}
1355
Dima Zavinfce7a472011-04-19 22:30:36 -07001356/* implementation of the interface to the policy manager */
1357extern "C" {
1358
Eric Laurenta4c5a552012-03-29 10:12:40 -07001359
1360static audio_module_handle_t aps_load_hw_module(void *service,
1361 const char *name)
Dima Zavinfce7a472011-04-19 22:30:36 -07001362{
1363 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001364 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001365 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001366 return 0;
1367 }
1368
Eric Laurenta4c5a552012-03-29 10:12:40 -07001369 return af->loadHwModule(name);
1370}
1371
1372// deprecated: replaced by aps_open_output_on_module()
1373static audio_io_handle_t aps_open_output(void *service,
1374 audio_devices_t *pDevices,
1375 uint32_t *pSamplingRate,
1376 audio_format_t *pFormat,
1377 audio_channel_mask_t *pChannelMask,
1378 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001379 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001380{
1381 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1382 if (af == 0) {
1383 ALOGW("%s: could not get AudioFlinger", __func__);
1384 return 0;
1385 }
1386
1387 return af->openOutput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask,
1388 pLatencyMs, flags);
1389}
1390
1391static audio_io_handle_t aps_open_output_on_module(void *service,
1392 audio_module_handle_t module,
1393 audio_devices_t *pDevices,
1394 uint32_t *pSamplingRate,
1395 audio_format_t *pFormat,
1396 audio_channel_mask_t *pChannelMask,
1397 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001398 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001399{
1400 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1401 if (af == 0) {
1402 ALOGW("%s: could not get AudioFlinger", __func__);
1403 return 0;
1404 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001405 return af->openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
Dima Zavinfce7a472011-04-19 22:30:36 -07001406 pLatencyMs, flags);
1407}
1408
1409static audio_io_handle_t aps_open_dup_output(void *service,
1410 audio_io_handle_t output1,
1411 audio_io_handle_t output2)
1412{
1413 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001414 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001415 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001416 return 0;
1417 }
1418 return af->openDuplicateOutput(output1, output2);
1419}
1420
1421static int aps_close_output(void *service, audio_io_handle_t output)
1422{
1423 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001424 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001425 return PERMISSION_DENIED;
1426
1427 return af->closeOutput(output);
1428}
1429
1430static int aps_suspend_output(void *service, audio_io_handle_t output)
1431{
1432 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001433 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001434 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001435 return PERMISSION_DENIED;
1436 }
1437
1438 return af->suspendOutput(output);
1439}
1440
1441static int aps_restore_output(void *service, audio_io_handle_t output)
1442{
1443 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001444 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001445 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001446 return PERMISSION_DENIED;
1447 }
1448
1449 return af->restoreOutput(output);
1450}
1451
Glenn Kasten20010052012-06-22 13:43:51 -07001452// deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
Dima Zavinfce7a472011-04-19 22:30:36 -07001453static audio_io_handle_t aps_open_input(void *service,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001454 audio_devices_t *pDevices,
1455 uint32_t *pSamplingRate,
1456 audio_format_t *pFormat,
1457 audio_channel_mask_t *pChannelMask,
1458 audio_in_acoustics_t acoustics)
Dima Zavinfce7a472011-04-19 22:30:36 -07001459{
1460 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001461 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001462 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001463 return 0;
1464 }
1465
Eric Laurenta4c5a552012-03-29 10:12:40 -07001466 return af->openInput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask);
1467}
1468
1469static audio_io_handle_t aps_open_input_on_module(void *service,
1470 audio_module_handle_t module,
1471 audio_devices_t *pDevices,
1472 uint32_t *pSamplingRate,
1473 audio_format_t *pFormat,
1474 audio_channel_mask_t *pChannelMask)
1475{
1476 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1477 if (af == 0) {
1478 ALOGW("%s: could not get AudioFlinger", __func__);
1479 return 0;
1480 }
1481
1482 return af->openInput(module, pDevices, pSamplingRate, pFormat, pChannelMask);
Dima Zavinfce7a472011-04-19 22:30:36 -07001483}
1484
1485static int aps_close_input(void *service, audio_io_handle_t input)
1486{
1487 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001488 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001489 return PERMISSION_DENIED;
1490
1491 return af->closeInput(input);
1492}
1493
1494static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1495 audio_io_handle_t output)
1496{
1497 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001498 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001499 return PERMISSION_DENIED;
1500
1501 return af->setStreamOutput(stream, output);
1502}
1503
1504static int aps_move_effects(void *service, int session,
1505 audio_io_handle_t src_output,
1506 audio_io_handle_t dst_output)
1507{
1508 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001509 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001510 return PERMISSION_DENIED;
1511
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001512 return af->moveEffects(session, src_output, dst_output);
Dima Zavinfce7a472011-04-19 22:30:36 -07001513}
1514
1515static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1516 const char *keys)
1517{
1518 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1519 return strdup(result.string());
1520}
1521
1522static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1523 const char *kv_pairs, int delay_ms)
1524{
1525 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1526
1527 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1528}
1529
1530static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1531 float volume, audio_io_handle_t output,
1532 int delay_ms)
1533{
1534 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1535
1536 return audioPolicyService->setStreamVolume(stream, volume, output,
1537 delay_ms);
1538}
1539
1540static int aps_start_tone(void *service, audio_policy_tone_t tone,
1541 audio_stream_type_t stream)
1542{
1543 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1544
1545 return audioPolicyService->startTone(tone, stream);
1546}
1547
1548static int aps_stop_tone(void *service)
1549{
1550 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1551
1552 return audioPolicyService->stopTone();
1553}
1554
1555static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1556{
1557 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1558
1559 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1560}
1561
1562}; // extern "C"
1563
1564namespace {
1565 struct audio_policy_service_ops aps_ops = {
1566 open_output : aps_open_output,
1567 open_duplicate_output : aps_open_dup_output,
1568 close_output : aps_close_output,
1569 suspend_output : aps_suspend_output,
1570 restore_output : aps_restore_output,
1571 open_input : aps_open_input,
1572 close_input : aps_close_input,
1573 set_stream_volume : aps_set_stream_volume,
1574 set_stream_output : aps_set_stream_output,
1575 set_parameters : aps_set_parameters,
1576 get_parameters : aps_get_parameters,
1577 start_tone : aps_start_tone,
1578 stop_tone : aps_stop_tone,
1579 set_voice_volume : aps_set_voice_volume,
1580 move_effects : aps_move_effects,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001581 load_hw_module : aps_load_hw_module,
1582 open_output_on_module : aps_open_output_on_module,
1583 open_input_on_module : aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001584 };
1585}; // namespace <unnamed>
1586
Mathias Agopian65ab4712010-07-14 17:59:35 -07001587}; // namespace android