blob: b86d3aeb214bd0588b7a1237299d565a4764afc2 [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
Dima Zavinfce7a472011-04-19 22:30:36 -070052namespace {
53 extern struct audio_policy_service_ops aps_ops;
54};
55
Mathias Agopian65ab4712010-07-14 17:59:35 -070056// ----------------------------------------------------------------------------
57
58AudioPolicyService::AudioPolicyService()
Dima Zavinfce7a472011-04-19 22:30:36 -070059 : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL)
Mathias Agopian65ab4712010-07-14 17:59:35 -070060{
61 char value[PROPERTY_VALUE_MAX];
Dima Zavinfce7a472011-04-19 22:30:36 -070062 const struct hw_module_t *module;
63 int forced_val;
64 int rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -070065
Eric Laurent93575202011-01-18 18:39:02 -080066 Mutex::Autolock _l(mLock);
67
Mathias Agopian65ab4712010-07-14 17:59:35 -070068 // start tone playback thread
69 mTonePlaybackThread = new AudioCommandThread(String8(""));
70 // start audio commands thread
Glenn Kasten480b4682012-02-28 12:30:08 -080071 mAudioCommandThread = new AudioCommandThread(String8("ApmCommand"));
Mathias Agopian65ab4712010-07-14 17:59:35 -070072
Dima Zavinfce7a472011-04-19 22:30:36 -070073 /* instantiate the audio policy manager */
74 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
75 if (rc)
76 return;
Mathias Agopian65ab4712010-07-14 17:59:35 -070077
Dima Zavinfce7a472011-04-19 22:30:36 -070078 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
Steve Block29357bc2012-01-06 19:20:56 +000079 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070080 if (rc)
81 return;
Eric Laurent93575202011-01-18 18:39:02 -080082
Dima Zavinfce7a472011-04-19 22:30:36 -070083 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
84 &mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000085 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070086 if (rc)
87 return;
88
89 rc = mpAudioPolicy->init_check(mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000090 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070091 if (rc)
92 return;
93
Steve Blockdf64d152012-01-04 20:05:49 +000094 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurent7c7f10b2011-06-17 21:29:58 -070095
96 // load audio pre processing modules
97 if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) {
98 loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE);
99 } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) {
100 loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE);
101 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700102}
103
104AudioPolicyService::~AudioPolicyService()
105{
106 mTonePlaybackThread->exit();
107 mTonePlaybackThread.clear();
108 mAudioCommandThread->exit();
109 mAudioCommandThread.clear();
110
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700111
112 // release audio pre processing resources
113 for (size_t i = 0; i < mInputSources.size(); i++) {
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800114 delete mInputSources.valueAt(i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700115 }
116 mInputSources.clear();
117
118 for (size_t i = 0; i < mInputs.size(); i++) {
119 mInputs.valueAt(i)->mEffects.clear();
120 delete mInputs.valueAt(i);
121 }
122 mInputs.clear();
123
Glenn Kastena0d68332012-01-27 16:47:15 -0800124 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL)
Dima Zavinfce7a472011-04-19 22:30:36 -0700125 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kastena0d68332012-01-27 16:47:15 -0800126 if (mpAudioPolicyDev != NULL)
Dima Zavinfce7a472011-04-19 22:30:36 -0700127 audio_policy_dev_close(mpAudioPolicyDev);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700128}
129
Dima Zavinfce7a472011-04-19 22:30:36 -0700130status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
131 audio_policy_dev_state_t state,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700132 const char *device_address)
133{
Dima Zavinfce7a472011-04-19 22:30:36 -0700134 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700135 return NO_INIT;
136 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800137 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700138 return PERMISSION_DENIED;
139 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700140 if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700141 return BAD_VALUE;
142 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700143 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
144 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700145 return BAD_VALUE;
146 }
147
Glenn Kasten411e4472012-11-02 10:00:06 -0700148 ALOGV("setDeviceConnectionState()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700149 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700150 return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device,
151 state, device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700152}
153
Dima Zavinfce7a472011-04-19 22:30:36 -0700154audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
155 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -0700156 const char *device_address)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700157{
Dima Zavinfce7a472011-04-19 22:30:36 -0700158 if (mpAudioPolicy == NULL) {
159 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700160 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700161 return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device,
162 device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700163}
164
Glenn Kastenf78aee72012-01-04 11:00:47 -0800165status_t AudioPolicyService::setPhoneState(audio_mode_t state)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700166{
Dima Zavinfce7a472011-04-19 22:30:36 -0700167 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700168 return NO_INIT;
169 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800170 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700171 return PERMISSION_DENIED;
172 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800173 if (uint32_t(state) >= AUDIO_MODE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700174 return BAD_VALUE;
175 }
176
Glenn Kasten411e4472012-11-02 10:00:06 -0700177 ALOGV("setPhoneState()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700178
179 // TODO: check if it is more appropriate to do it in platform specific policy manager
180 AudioSystem::setMode(state);
181
182 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700183 mpAudioPolicy->set_phone_state(mpAudioPolicy, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700184 return NO_ERROR;
185}
186
Dima Zavinfce7a472011-04-19 22:30:36 -0700187status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
188 audio_policy_forced_cfg_t config)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700189{
Dima Zavinfce7a472011-04-19 22:30:36 -0700190 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700191 return NO_INIT;
192 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800193 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700194 return PERMISSION_DENIED;
195 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700196 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700197 return BAD_VALUE;
198 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700199 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700200 return BAD_VALUE;
201 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700202 ALOGV("setForceUse()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700203 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700204 mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700205 return NO_ERROR;
206}
207
Dima Zavinfce7a472011-04-19 22:30:36 -0700208audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700209{
Dima Zavinfce7a472011-04-19 22:30:36 -0700210 if (mpAudioPolicy == NULL) {
211 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700212 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700213 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
214 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700215 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700216 return mpAudioPolicy->get_force_use(mpAudioPolicy, usage);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700217}
218
Dima Zavinfce7a472011-04-19 22:30:36 -0700219audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700220 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800221 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700222 audio_channel_mask_t channelMask,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700223 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700224{
Dima Zavinfce7a472011-04-19 22:30:36 -0700225 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700226 return 0;
227 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700228 ALOGV("getOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700229 Mutex::Autolock _l(mLock);
Glenn Kasten8af901c2012-11-01 11:11:38 -0700230 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, format, channelMask,
231 flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700232}
233
Eric Laurentde070132010-07-13 04:45:46 -0700234status_t AudioPolicyService::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700235 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700236 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700237{
Dima Zavinfce7a472011-04-19 22:30:36 -0700238 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700239 return NO_INIT;
240 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700241 ALOGV("startOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700243 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244}
245
Eric Laurentde070132010-07-13 04:45:46 -0700246status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700247 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700248 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700249{
Dima Zavinfce7a472011-04-19 22:30:36 -0700250 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700251 return NO_INIT;
252 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700253 ALOGV("stopOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700254 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700255 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700256}
257
258void AudioPolicyService::releaseOutput(audio_io_handle_t output)
259{
Dima Zavinfce7a472011-04-19 22:30:36 -0700260 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700261 return;
262 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700263 ALOGV("releaseOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700264 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700265 mpAudioPolicy->release_output(mpAudioPolicy, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700266}
267
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800268audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700269 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800270 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700271 audio_channel_mask_t channelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700272 int audioSession)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700273{
Dima Zavinfce7a472011-04-19 22:30:36 -0700274 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700275 return 0;
276 }
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800277 // already checked by client, but double-check in case the client wrapper is bypassed
278 if (uint32_t(inputSource) >= AUDIO_SOURCE_CNT) {
279 return 0;
280 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700281 Mutex::Autolock _l(mLock);
Glenn Kasten20010052012-06-22 13:43:51 -0700282 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700283 audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
Glenn Kasten8af901c2012-11-01 11:11:38 -0700284 format, channelMask, (audio_in_acoustics_t) 0);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700285
286 if (input == 0) {
287 return input;
288 }
289 // create audio pre processors according to input source
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800290 ssize_t index = mInputSources.indexOfKey(inputSource);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700291 if (index < 0) {
292 return input;
293 }
294 ssize_t idx = mInputs.indexOfKey(input);
295 InputDesc *inputDesc;
296 if (idx < 0) {
Glenn Kasten81872a22012-03-07 16:49:22 -0800297 inputDesc = new InputDesc(audioSession);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700298 mInputs.add(input, inputDesc);
299 } else {
300 inputDesc = mInputs.valueAt(idx);
301 }
302
303 Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects;
304 for (size_t i = 0; i < effects.size(); i++) {
305 EffectDesc *effect = effects[i];
306 sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input);
307 status_t status = fx->initCheck();
308 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000309 ALOGW("Failed to create Fx %s on input %d", effect->mName, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700310 // fx goes out of scope and strong ref on AudioEffect is released
311 continue;
312 }
313 for (size_t j = 0; j < effect->mParams.size(); j++) {
314 fx->setParameter(effect->mParams[j]);
315 }
316 inputDesc->mEffects.add(fx);
317 }
318 setPreProcessorEnabled(inputDesc, true);
319 return input;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320}
321
322status_t AudioPolicyService::startInput(audio_io_handle_t input)
323{
Dima Zavinfce7a472011-04-19 22:30:36 -0700324 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700325 return NO_INIT;
326 }
327 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700328
Dima Zavinfce7a472011-04-19 22:30:36 -0700329 return mpAudioPolicy->start_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700330}
331
332status_t AudioPolicyService::stopInput(audio_io_handle_t input)
333{
Dima Zavinfce7a472011-04-19 22:30:36 -0700334 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335 return NO_INIT;
336 }
337 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700338
Dima Zavinfce7a472011-04-19 22:30:36 -0700339 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700340}
341
342void AudioPolicyService::releaseInput(audio_io_handle_t input)
343{
Dima Zavinfce7a472011-04-19 22:30:36 -0700344 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345 return;
346 }
347 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700348 mpAudioPolicy->release_input(mpAudioPolicy, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700349
350 ssize_t index = mInputs.indexOfKey(input);
351 if (index < 0) {
352 return;
353 }
354 InputDesc *inputDesc = mInputs.valueAt(index);
355 setPreProcessorEnabled(inputDesc, false);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700356 delete inputDesc;
357 mInputs.removeItemsAt(index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700358}
359
Dima Zavinfce7a472011-04-19 22:30:36 -0700360status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700361 int indexMin,
362 int indexMax)
363{
Dima Zavinfce7a472011-04-19 22:30:36 -0700364 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365 return NO_INIT;
366 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800367 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700368 return PERMISSION_DENIED;
369 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800370 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700371 return BAD_VALUE;
372 }
Eric Laurent5f121362012-06-15 14:45:03 -0700373 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700374 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700375 return NO_ERROR;
376}
377
Eric Laurent83844cc2011-11-18 16:43:31 -0800378status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
379 int index,
380 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700381{
Dima Zavinfce7a472011-04-19 22:30:36 -0700382 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700383 return NO_INIT;
384 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800385 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700386 return PERMISSION_DENIED;
387 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800388 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700389 return BAD_VALUE;
390 }
Eric Laurent5f121362012-06-15 14:45:03 -0700391 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800392 if (mpAudioPolicy->set_stream_volume_index_for_device) {
393 return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
394 stream,
395 index,
396 device);
397 } else {
398 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
399 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400}
401
Eric Laurent83844cc2011-11-18 16:43:31 -0800402status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
403 int *index,
404 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700405{
Dima Zavinfce7a472011-04-19 22:30:36 -0700406 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700407 return NO_INIT;
408 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800409 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700410 return BAD_VALUE;
411 }
Eric Laurent5f121362012-06-15 14:45:03 -0700412 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800413 if (mpAudioPolicy->get_stream_volume_index_for_device) {
414 return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
415 stream,
416 index,
417 device);
418 } else {
419 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
420 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700421}
422
Dima Zavinfce7a472011-04-19 22:30:36 -0700423uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700424{
Dima Zavinfce7a472011-04-19 22:30:36 -0700425 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700426 return 0;
427 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700428 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurentde070132010-07-13 04:45:46 -0700429}
430
Eric Laurent63742522012-03-08 13:42:42 -0800431//audio policy: use audio_device_t appropriately
432
433audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800434{
Dima Zavinfce7a472011-04-19 22:30:36 -0700435 if (mpAudioPolicy == NULL) {
Eric Laurent63742522012-03-08 13:42:42 -0800436 return (audio_devices_t)0;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800437 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700438 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800439}
440
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700441audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700442{
Dima Zavinfce7a472011-04-19 22:30:36 -0700443 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700444 return NO_INIT;
445 }
446 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700447 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700448}
449
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700450status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700451 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700452 uint32_t strategy,
453 int session,
454 int id)
455{
Dima Zavinfce7a472011-04-19 22:30:36 -0700456 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700457 return NO_INIT;
458 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700459 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700460}
461
462status_t AudioPolicyService::unregisterEffect(int id)
463{
Dima Zavinfce7a472011-04-19 22:30:36 -0700464 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700465 return NO_INIT;
466 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700467 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700468}
469
Eric Laurentdb7c0792011-08-10 10:37:50 -0700470status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
471{
472 if (mpAudioPolicy == NULL) {
473 return NO_INIT;
474 }
475 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
476}
477
Glenn Kastenfff6d712012-01-12 16:38:12 -0800478bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800479{
Dima Zavinfce7a472011-04-19 22:30:36 -0700480 if (mpAudioPolicy == NULL) {
Eric Laurenteda6c362011-02-02 09:33:30 -0800481 return 0;
482 }
483 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700484 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800485}
486
Jean-Michel Trivie3f641f2012-10-10 12:11:16 -0700487bool AudioPolicyService::isSourceActive(audio_source_t source) const
488{
489 if (mpAudioPolicy == NULL) {
490 return false;
491 }
492 if (mpAudioPolicy->is_source_active == 0) {
493 return false;
494 }
495 Mutex::Autolock _l(mLock);
496 return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
497}
498
Eric Laurent57dae992011-07-24 13:36:09 -0700499status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
500 effect_descriptor_t *descriptors,
501 uint32_t *count)
502{
503
504 if (mpAudioPolicy == NULL) {
505 *count = 0;
506 return NO_INIT;
507 }
508 Mutex::Autolock _l(mLock);
509 status_t status = NO_ERROR;
510
511 size_t index;
512 for (index = 0; index < mInputs.size(); index++) {
513 if (mInputs.valueAt(index)->mSessionId == audioSession) {
514 break;
515 }
516 }
517 if (index == mInputs.size()) {
518 *count = 0;
519 return BAD_VALUE;
520 }
521 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
522
523 for (size_t i = 0; i < effects.size(); i++) {
524 effect_descriptor_t desc = effects[i]->descriptor();
525 if (i < *count) {
Glenn Kastena189a682012-02-20 12:16:30 -0800526 descriptors[i] = desc;
Eric Laurent57dae992011-07-24 13:36:09 -0700527 }
528 }
529 if (effects.size() > *count) {
530 status = NO_MEMORY;
531 }
532 *count = effects.size();
533 return status;
534}
535
Mathias Agopian65ab4712010-07-14 17:59:35 -0700536void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700537 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700538 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700539}
540
541static bool tryLock(Mutex& mutex)
542{
543 bool locked = false;
544 for (int i = 0; i < kDumpLockRetries; ++i) {
545 if (mutex.tryLock() == NO_ERROR) {
546 locked = true;
547 break;
548 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800549 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550 }
551 return locked;
552}
553
554status_t AudioPolicyService::dumpInternals(int fd)
555{
556 const size_t SIZE = 256;
557 char buffer[SIZE];
558 String8 result;
559
Dima Zavinfce7a472011-04-19 22:30:36 -0700560 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700561 result.append(buffer);
562 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
563 result.append(buffer);
564 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
565 result.append(buffer);
566
567 write(fd, result.string(), result.size());
568 return NO_ERROR;
569}
570
571status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
572{
Glenn Kasten44deb052012-02-05 18:09:08 -0800573 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700574 dumpPermissionDenial(fd);
575 } else {
576 bool locked = tryLock(mLock);
577 if (!locked) {
578 String8 result(kDeadlockedString);
579 write(fd, result.string(), result.size());
580 }
581
582 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800583 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700584 mAudioCommandThread->dump(fd);
585 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800586 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587 mTonePlaybackThread->dump(fd);
588 }
589
Dima Zavinfce7a472011-04-19 22:30:36 -0700590 if (mpAudioPolicy) {
591 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700592 }
593
594 if (locked) mLock.unlock();
595 }
596 return NO_ERROR;
597}
598
599status_t AudioPolicyService::dumpPermissionDenial(int fd)
600{
601 const size_t SIZE = 256;
602 char buffer[SIZE];
603 String8 result;
604 snprintf(buffer, SIZE, "Permission Denial: "
605 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
606 IPCThreadState::self()->getCallingPid(),
607 IPCThreadState::self()->getCallingUid());
608 result.append(buffer);
609 write(fd, result.string(), result.size());
610 return NO_ERROR;
611}
612
Glenn Kasten81872a22012-03-07 16:49:22 -0800613void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700614{
Glenn Kasten81872a22012-03-07 16:49:22 -0800615 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700616 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800617 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700618 }
619}
620
Mathias Agopian65ab4712010-07-14 17:59:35 -0700621status_t AudioPolicyService::onTransact(
622 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
623{
624 return BnAudioPolicyService::onTransact(code, data, reply, flags);
625}
626
627
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628// ----------- AudioPolicyService::AudioCommandThread implementation ----------
629
630AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name)
631 : Thread(false), mName(name)
632{
633 mpToneGenerator = NULL;
634}
635
636
637AudioPolicyService::AudioCommandThread::~AudioCommandThread()
638{
639 if (mName != "" && !mAudioCommands.isEmpty()) {
640 release_wake_lock(mName.string());
641 }
642 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800643 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700644}
645
646void AudioPolicyService::AudioCommandThread::onFirstRef()
647{
648 if (mName != "") {
649 run(mName.string(), ANDROID_PRIORITY_AUDIO);
650 } else {
Glenn Kasten480b4682012-02-28 12:30:08 -0800651 run("AudioCommand", ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700652 }
653}
654
655bool AudioPolicyService::AudioCommandThread::threadLoop()
656{
657 nsecs_t waitTime = INT64_MAX;
658
659 mLock.lock();
660 while (!exitPending())
661 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700662 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700663 nsecs_t curTime = systemTime();
664 // commands are sorted by increasing time stamp: execute them from index 0 and up
665 if (mAudioCommands[0]->mTime <= curTime) {
666 AudioCommand *command = mAudioCommands[0];
667 mAudioCommands.removeAt(0);
668 mLastCommand = *command;
669
670 switch (command->mCommand) {
671 case START_TONE: {
672 mLock.unlock();
673 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100674 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700675 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800676 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700677 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
678 mpToneGenerator->startTone(data->mType);
679 delete data;
680 mLock.lock();
681 }break;
682 case STOP_TONE: {
683 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100684 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 if (mpToneGenerator != NULL) {
686 mpToneGenerator->stopTone();
687 delete mpToneGenerator;
688 mpToneGenerator = NULL;
689 }
690 mLock.lock();
691 }break;
692 case SET_VOLUME: {
693 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100694 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700695 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
696 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
697 data->mVolume,
698 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699 if (command->mWaitStatus) {
700 command->mCond.signal();
701 mWaitWorkCV.wait(mLock);
702 }
703 delete data;
704 }break;
705 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700706 ParametersData *data = (ParametersData *)command->mParam;
707 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
708 data->mKeyValuePairs.string(), data->mIO);
709 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
710 if (command->mWaitStatus) {
711 command->mCond.signal();
712 mWaitWorkCV.wait(mLock);
713 }
714 delete data;
715 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700716 case SET_VOICE_VOLUME: {
717 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100718 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700719 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
721 if (command->mWaitStatus) {
722 command->mCond.signal();
723 mWaitWorkCV.wait(mLock);
724 }
725 delete data;
726 }break;
727 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000728 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700729 }
730 delete command;
731 waitTime = INT64_MAX;
732 } else {
733 waitTime = mAudioCommands[0]->mTime - curTime;
734 break;
735 }
736 }
737 // release delayed commands wake lock
738 if (mName != "" && mAudioCommands.isEmpty()) {
739 release_wake_lock(mName.string());
740 }
Steve Block3856b092011-10-20 11:56:00 +0100741 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700742 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100743 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700744 }
745 mLock.unlock();
746 return false;
747}
748
749status_t AudioPolicyService::AudioCommandThread::dump(int fd)
750{
751 const size_t SIZE = 256;
752 char buffer[SIZE];
753 String8 result;
754
755 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
756 result.append(buffer);
757 write(fd, result.string(), result.size());
758
759 bool locked = tryLock(mLock);
760 if (!locked) {
761 String8 result2(kCmdDeadlockedString);
762 write(fd, result2.string(), result2.size());
763 }
764
765 snprintf(buffer, SIZE, "- Commands:\n");
766 result = String8(buffer);
767 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800768 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700769 mAudioCommands[i]->dump(buffer, SIZE);
770 result.append(buffer);
771 }
772 result.append(" Last Command\n");
773 mLastCommand.dump(buffer, SIZE);
774 result.append(buffer);
775
776 write(fd, result.string(), result.size());
777
778 if (locked) mLock.unlock();
779
780 return NO_ERROR;
781}
782
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800783void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
784 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700785{
786 AudioCommand *command = new AudioCommand();
787 command->mCommand = START_TONE;
788 ToneData *data = new ToneData();
789 data->mType = type;
790 data->mStream = stream;
791 command->mParam = (void *)data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700792 Mutex::Autolock _l(mLock);
793 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100794 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700795 mWaitWorkCV.signal();
796}
797
798void AudioPolicyService::AudioCommandThread::stopToneCommand()
799{
800 AudioCommand *command = new AudioCommand();
801 command->mCommand = STOP_TONE;
802 command->mParam = NULL;
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 stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700806 mWaitWorkCV.signal();
807}
808
Glenn Kastenfff6d712012-01-12 16:38:12 -0800809status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700810 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800811 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700812 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700813{
814 status_t status = NO_ERROR;
815
816 AudioCommand *command = new AudioCommand();
817 command->mCommand = SET_VOLUME;
818 VolumeData *data = new VolumeData();
819 data->mStream = stream;
820 data->mVolume = volume;
821 data->mIO = output;
822 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700823 Mutex::Autolock _l(mLock);
824 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100825 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700826 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700827 mWaitWorkCV.signal();
828 if (command->mWaitStatus) {
829 command->mCond.wait(mLock);
830 status = command->mStatus;
831 mWaitWorkCV.signal();
832 }
833 return status;
834}
835
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800836status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700837 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700838 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700839{
840 status_t status = NO_ERROR;
841
842 AudioCommand *command = new AudioCommand();
843 command->mCommand = SET_PARAMETERS;
844 ParametersData *data = new ParametersData();
845 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700846 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700847 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700848 Mutex::Autolock _l(mLock);
849 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100850 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700851 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700852 mWaitWorkCV.signal();
853 if (command->mWaitStatus) {
854 command->mCond.wait(mLock);
855 status = command->mStatus;
856 mWaitWorkCV.signal();
857 }
858 return status;
859}
860
861status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
862{
863 status_t status = NO_ERROR;
864
865 AudioCommand *command = new AudioCommand();
866 command->mCommand = SET_VOICE_VOLUME;
867 VoiceVolumeData *data = new VoiceVolumeData();
868 data->mVolume = volume;
869 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700870 Mutex::Autolock _l(mLock);
871 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100872 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700873 mWaitWorkCV.signal();
874 if (command->mWaitStatus) {
875 command->mCond.wait(mLock);
876 status = command->mStatus;
877 mWaitWorkCV.signal();
878 }
879 return status;
880}
881
882// insertCommand_l() must be called with mLock held
883void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
884{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800885 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700886 Vector <AudioCommand *> removedCommands;
887
Eric Laurentcec4abb2012-07-03 12:23:02 -0700888 nsecs_t time = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700889 command->mTime = systemTime() + milliseconds(delayMs);
890
891 // acquire wake lock to make sure delayed commands are processed
892 if (mName != "" && mAudioCommands.isEmpty()) {
893 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
894 }
895
896 // check same pending commands with later time stamps and eliminate them
897 for (i = mAudioCommands.size()-1; i >= 0; i--) {
898 AudioCommand *command2 = mAudioCommands[i];
899 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
900 if (command2->mTime <= command->mTime) break;
901 if (command2->mCommand != command->mCommand) continue;
902
903 switch (command->mCommand) {
904 case SET_PARAMETERS: {
905 ParametersData *data = (ParametersData *)command->mParam;
906 ParametersData *data2 = (ParametersData *)command2->mParam;
907 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100908 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700909 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700910 AudioParameter param = AudioParameter(data->mKeyValuePairs);
911 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
912 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700913 String8 key;
914 String8 value;
915 param.getAt(j, key, value);
916 for (size_t k = 0; k < param2.size(); k++) {
917 String8 key2;
918 String8 value2;
919 param2.getAt(k, key2, value2);
920 if (key2 == key) {
921 param2.remove(key2);
922 ALOGV("Filtering out parameter %s", key2.string());
923 break;
924 }
925 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700926 }
927 // if all keys have been filtered out, remove the command.
928 // otherwise, update the key value pairs
929 if (param2.size() == 0) {
930 removedCommands.add(command2);
931 } else {
932 data2->mKeyValuePairs = param2.toString();
933 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700934 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700935 } break;
936
937 case SET_VOLUME: {
938 VolumeData *data = (VolumeData *)command->mParam;
939 VolumeData *data2 = (VolumeData *)command2->mParam;
940 if (data->mIO != data2->mIO) break;
941 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100942 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700943 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944 removedCommands.add(command2);
Eric Laurentcec4abb2012-07-03 12:23:02 -0700945 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946 } break;
947 case START_TONE:
948 case STOP_TONE:
949 default:
950 break;
951 }
952 }
953
954 // remove filtered commands
955 for (size_t j = 0; j < removedCommands.size(); j++) {
956 // removed commands always have time stamps greater than current command
957 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
958 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +0100959 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960 mAudioCommands.removeAt(k);
961 break;
962 }
963 }
964 }
965 removedCommands.clear();
966
Eric Laurentcec4abb2012-07-03 12:23:02 -0700967 // wait for status only if delay is 0 and command time was not modified above
968 if (delayMs == 0 && time == 0) {
969 command->mWaitStatus = true;
970 } else {
971 command->mWaitStatus = false;
972 }
973 // update command time if modified above
974 if (time != 0) {
975 command->mTime = time;
976 }
977
Mathias Agopian65ab4712010-07-14 17:59:35 -0700978 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +0100979 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -0700980 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700981 mAudioCommands.insertAt(command, i + 1);
982}
983
984void AudioPolicyService::AudioCommandThread::exit()
985{
Steve Block3856b092011-10-20 11:56:00 +0100986 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700987 {
988 AutoMutex _l(mLock);
989 requestExit();
990 mWaitWorkCV.signal();
991 }
992 requestExitAndWait();
993}
994
995void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
996{
997 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
998 mCommand,
999 (int)ns2s(mTime),
1000 (int)ns2ms(mTime)%1000,
1001 mWaitStatus,
1002 mParam);
1003}
1004
Dima Zavinfce7a472011-04-19 22:30:36 -07001005/******* helpers for the service_ops callbacks defined below *********/
1006void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1007 const char *keyValuePairs,
1008 int delayMs)
1009{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001010 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001011 delayMs);
1012}
1013
1014int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1015 float volume,
1016 audio_io_handle_t output,
1017 int delayMs)
1018{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001019 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001020 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001021}
1022
1023int AudioPolicyService::startTone(audio_policy_tone_t tone,
1024 audio_stream_type_t stream)
1025{
1026 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
Steve Block29357bc2012-01-06 19:20:56 +00001027 ALOGE("startTone: illegal tone requested (%d)", tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001028 if (stream != AUDIO_STREAM_VOICE_CALL)
Steve Block29357bc2012-01-06 19:20:56 +00001029 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001030 tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001031 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1032 AUDIO_STREAM_VOICE_CALL);
1033 return 0;
1034}
1035
1036int AudioPolicyService::stopTone()
1037{
1038 mTonePlaybackThread->stopToneCommand();
1039 return 0;
1040}
1041
1042int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1043{
1044 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1045}
1046
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001047// ----------------------------------------------------------------------------
1048// Audio pre-processing configuration
1049// ----------------------------------------------------------------------------
1050
Glenn Kasten8dad0e32012-01-09 08:41:22 -08001051/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001052 MIC_SRC_TAG,
1053 VOICE_UL_SRC_TAG,
1054 VOICE_DL_SRC_TAG,
1055 VOICE_CALL_SRC_TAG,
1056 CAMCORDER_SRC_TAG,
1057 VOICE_REC_SRC_TAG,
1058 VOICE_COMM_SRC_TAG
1059};
1060
1061// returns the audio_source_t enum corresponding to the input source name or
1062// AUDIO_SOURCE_CNT is no match found
1063audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
1064{
1065 int i;
1066 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
1067 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001068 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001069 break;
1070 }
1071 }
1072 return (audio_source_t)i;
1073}
1074
1075size_t AudioPolicyService::growParamSize(char *param,
1076 size_t size,
1077 size_t *curSize,
1078 size_t *totSize)
1079{
1080 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
1081 size_t pos = ((*curSize - 1 ) / size + 1) * size;
1082
1083 if (pos + size > *totSize) {
1084 while (pos + size > *totSize) {
1085 *totSize += ((*totSize + 7) / 8) * 4;
1086 }
1087 param = (char *)realloc(param, *totSize);
1088 }
1089 *curSize = pos + size;
1090 return pos;
1091}
1092
1093size_t AudioPolicyService::readParamValue(cnode *node,
1094 char *param,
1095 size_t *curSize,
1096 size_t *totSize)
1097{
1098 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
1099 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
1100 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001101 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001102 return sizeof(short);
1103 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
1104 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
1105 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001106 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001107 return sizeof(int);
1108 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
1109 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
1110 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001111 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001112 return sizeof(float);
1113 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
1114 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
1115 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1116 *(bool *)((char *)param + pos) = false;
1117 } else {
1118 *(bool *)((char *)param + pos) = true;
1119 }
Steve Block3856b092011-10-20 11:56:00 +01001120 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001121 return sizeof(bool);
1122 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1123 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1124 if (*curSize + len + 1 > *totSize) {
1125 *totSize = *curSize + len + 1;
1126 param = (char *)realloc(param, *totSize);
1127 }
1128 strncpy(param + *curSize, node->value, len);
1129 *curSize += len;
1130 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +01001131 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001132 return len;
1133 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001134 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001135 return 0;
1136}
1137
1138effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1139{
1140 cnode *param;
1141 cnode *value;
1142 size_t curSize = sizeof(effect_param_t);
1143 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1144 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1145
1146 param = config_find(root, PARAM_TAG);
1147 value = config_find(root, VALUE_TAG);
1148 if (param == NULL && value == NULL) {
1149 // try to parse simple parameter form {int int}
1150 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -08001151 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001152 // Note: that a pair of random strings is read as 0 0
1153 int *ptr = (int *)fx_param->data;
1154 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +00001155 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001156 *ptr++ = atoi(param->name);
1157 *ptr = atoi(param->value);
1158 fx_param->psize = sizeof(int);
1159 fx_param->vsize = sizeof(int);
1160 return fx_param;
1161 }
1162 }
1163 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001164 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001165 goto error;
1166 }
1167
1168 fx_param->psize = 0;
1169 param = param->first_child;
1170 while (param) {
Steve Block3856b092011-10-20 11:56:00 +01001171 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001172 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1173 if (size == 0) {
1174 goto error;
1175 }
1176 fx_param->psize += size;
1177 param = param->next;
1178 }
1179
1180 // align start of value field on 32 bit boundary
1181 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1182
1183 fx_param->vsize = 0;
1184 value = value->first_child;
1185 while (value) {
Steve Block3856b092011-10-20 11:56:00 +01001186 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001187 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1188 if (size == 0) {
1189 goto error;
1190 }
1191 fx_param->vsize += size;
1192 value = value->next;
1193 }
1194
1195 return fx_param;
1196
1197error:
1198 delete fx_param;
1199 return NULL;
1200}
1201
1202void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1203{
1204 cnode *node = root->first_child;
1205 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001206 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001207 effect_param_t *param = loadEffectParameter(node);
1208 if (param == NULL) {
1209 node = node->next;
1210 continue;
1211 }
1212 params.add(param);
1213 node = node->next;
1214 }
1215}
1216
1217AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1218 cnode *root,
1219 const Vector <EffectDesc *>& effects)
1220{
1221 cnode *node = root->first_child;
1222 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001223 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001224 return NULL;
1225 }
1226 InputSourceDesc *source = new InputSourceDesc();
1227 while (node) {
1228 size_t i;
1229 for (i = 0; i < effects.size(); i++) {
1230 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001231 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001232 break;
1233 }
1234 }
1235 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +01001236 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001237 node = node->next;
1238 continue;
1239 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001240 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001241 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +01001242 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001243 source->mEffects.add(effect);
1244 node = node->next;
1245 }
1246 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001247 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001248 delete source;
1249 return NULL;
1250 }
1251 return source;
1252}
1253
1254status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1255{
1256 cnode *node = config_find(root, PREPROCESSING_TAG);
1257 if (node == NULL) {
1258 return -ENOENT;
1259 }
1260 node = node->first_child;
1261 while (node) {
1262 audio_source_t source = inputSourceNameToEnum(node->name);
1263 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001264 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001265 node = node->next;
1266 continue;
1267 }
Steve Block3856b092011-10-20 11:56:00 +01001268 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001269 InputSourceDesc *desc = loadInputSource(node, effects);
1270 if (desc == NULL) {
1271 node = node->next;
1272 continue;
1273 }
1274 mInputSources.add(source, desc);
1275 node = node->next;
1276 }
1277 return NO_ERROR;
1278}
1279
1280AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1281{
1282 cnode *node = config_find(root, UUID_TAG);
1283 if (node == NULL) {
1284 return NULL;
1285 }
1286 effect_uuid_t uuid;
1287 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001288 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001289 return NULL;
1290 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001291 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001292}
1293
1294status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1295{
1296 cnode *node = config_find(root, EFFECTS_TAG);
1297 if (node == NULL) {
1298 return -ENOENT;
1299 }
1300 node = node->first_child;
1301 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001302 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001303 EffectDesc *effect = loadEffect(node);
1304 if (effect == NULL) {
1305 node = node->next;
1306 continue;
1307 }
1308 effects.add(effect);
1309 node = node->next;
1310 }
1311 return NO_ERROR;
1312}
1313
1314status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1315{
1316 cnode *root;
1317 char *data;
1318
1319 data = (char *)load_file(path, NULL);
1320 if (data == NULL) {
1321 return -ENODEV;
1322 }
1323 root = config_node("", "");
1324 config_load(root, data);
1325
1326 Vector <EffectDesc *> effects;
1327 loadEffects(root, effects);
1328 loadInputSources(root, effects);
1329
1330 config_free(root);
1331 free(root);
1332 free(data);
1333
1334 return NO_ERROR;
1335}
1336
Dima Zavinfce7a472011-04-19 22:30:36 -07001337/* implementation of the interface to the policy manager */
1338extern "C" {
1339
Eric Laurenta4c5a552012-03-29 10:12:40 -07001340
1341static audio_module_handle_t aps_load_hw_module(void *service,
1342 const char *name)
Dima Zavinfce7a472011-04-19 22:30:36 -07001343{
1344 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001345 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001346 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001347 return 0;
1348 }
1349
Eric Laurenta4c5a552012-03-29 10:12:40 -07001350 return af->loadHwModule(name);
1351}
1352
1353// deprecated: replaced by aps_open_output_on_module()
1354static audio_io_handle_t aps_open_output(void *service,
1355 audio_devices_t *pDevices,
1356 uint32_t *pSamplingRate,
1357 audio_format_t *pFormat,
1358 audio_channel_mask_t *pChannelMask,
1359 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001360 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001361{
1362 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1363 if (af == 0) {
1364 ALOGW("%s: could not get AudioFlinger", __func__);
1365 return 0;
1366 }
1367
1368 return af->openOutput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask,
1369 pLatencyMs, flags);
1370}
1371
1372static audio_io_handle_t aps_open_output_on_module(void *service,
1373 audio_module_handle_t module,
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 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001386 return af->openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
Dima Zavinfce7a472011-04-19 22:30:36 -07001387 pLatencyMs, flags);
1388}
1389
1390static audio_io_handle_t aps_open_dup_output(void *service,
1391 audio_io_handle_t output1,
1392 audio_io_handle_t output2)
1393{
1394 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001395 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001396 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001397 return 0;
1398 }
1399 return af->openDuplicateOutput(output1, output2);
1400}
1401
1402static int aps_close_output(void *service, audio_io_handle_t output)
1403{
1404 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001405 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001406 return PERMISSION_DENIED;
1407
1408 return af->closeOutput(output);
1409}
1410
1411static int aps_suspend_output(void *service, audio_io_handle_t output)
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 PERMISSION_DENIED;
1417 }
1418
1419 return af->suspendOutput(output);
1420}
1421
1422static int aps_restore_output(void *service, audio_io_handle_t output)
1423{
1424 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001425 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001426 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001427 return PERMISSION_DENIED;
1428 }
1429
1430 return af->restoreOutput(output);
1431}
1432
Glenn Kasten20010052012-06-22 13:43:51 -07001433// deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
Dima Zavinfce7a472011-04-19 22:30:36 -07001434static audio_io_handle_t aps_open_input(void *service,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001435 audio_devices_t *pDevices,
1436 uint32_t *pSamplingRate,
1437 audio_format_t *pFormat,
1438 audio_channel_mask_t *pChannelMask,
1439 audio_in_acoustics_t acoustics)
Dima Zavinfce7a472011-04-19 22:30:36 -07001440{
1441 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001442 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001443 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001444 return 0;
1445 }
1446
Eric Laurenta4c5a552012-03-29 10:12:40 -07001447 return af->openInput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask);
1448}
1449
1450static audio_io_handle_t aps_open_input_on_module(void *service,
1451 audio_module_handle_t module,
1452 audio_devices_t *pDevices,
1453 uint32_t *pSamplingRate,
1454 audio_format_t *pFormat,
1455 audio_channel_mask_t *pChannelMask)
1456{
1457 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1458 if (af == 0) {
1459 ALOGW("%s: could not get AudioFlinger", __func__);
1460 return 0;
1461 }
1462
1463 return af->openInput(module, pDevices, pSamplingRate, pFormat, pChannelMask);
Dima Zavinfce7a472011-04-19 22:30:36 -07001464}
1465
1466static int aps_close_input(void *service, audio_io_handle_t input)
1467{
1468 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001469 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001470 return PERMISSION_DENIED;
1471
1472 return af->closeInput(input);
1473}
1474
1475static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1476 audio_io_handle_t output)
1477{
1478 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001479 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001480 return PERMISSION_DENIED;
1481
1482 return af->setStreamOutput(stream, output);
1483}
1484
1485static int aps_move_effects(void *service, int session,
1486 audio_io_handle_t src_output,
1487 audio_io_handle_t dst_output)
1488{
1489 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001490 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001491 return PERMISSION_DENIED;
1492
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001493 return af->moveEffects(session, src_output, dst_output);
Dima Zavinfce7a472011-04-19 22:30:36 -07001494}
1495
1496static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1497 const char *keys)
1498{
1499 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1500 return strdup(result.string());
1501}
1502
1503static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1504 const char *kv_pairs, int delay_ms)
1505{
1506 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1507
1508 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1509}
1510
1511static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1512 float volume, audio_io_handle_t output,
1513 int delay_ms)
1514{
1515 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1516
1517 return audioPolicyService->setStreamVolume(stream, volume, output,
1518 delay_ms);
1519}
1520
1521static int aps_start_tone(void *service, audio_policy_tone_t tone,
1522 audio_stream_type_t stream)
1523{
1524 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1525
1526 return audioPolicyService->startTone(tone, stream);
1527}
1528
1529static int aps_stop_tone(void *service)
1530{
1531 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1532
1533 return audioPolicyService->stopTone();
1534}
1535
1536static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1537{
1538 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1539
1540 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1541}
1542
1543}; // extern "C"
1544
1545namespace {
1546 struct audio_policy_service_ops aps_ops = {
1547 open_output : aps_open_output,
1548 open_duplicate_output : aps_open_dup_output,
1549 close_output : aps_close_output,
1550 suspend_output : aps_suspend_output,
1551 restore_output : aps_restore_output,
1552 open_input : aps_open_input,
1553 close_input : aps_close_input,
1554 set_stream_volume : aps_set_stream_volume,
1555 set_stream_output : aps_set_stream_output,
1556 set_parameters : aps_set_parameters,
1557 get_parameters : aps_get_parameters,
1558 start_tone : aps_start_tone,
1559 stop_tone : aps_stop_tone,
1560 set_voice_volume : aps_set_voice_volume,
1561 move_effects : aps_move_effects,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001562 load_hw_module : aps_load_hw_module,
1563 open_output_on_module : aps_open_output_on_module,
1564 open_input_on_module : aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001565 };
1566}; // namespace <unnamed>
1567
Mathias Agopian65ab4712010-07-14 17:59:35 -07001568}; // namespace android