blob: 7dd46f26191518200bad7aa23b248c75a3c5e260 [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
Steve Block3856b092011-10-20 11:56:00 +0100148 ALOGV("setDeviceConnectionState() tid %d", gettid());
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
Steve Block3856b092011-10-20 11:56:00 +0100177 ALOGV("setPhoneState() tid %d", gettid());
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 }
Steve Block3856b092011-10-20 11:56:00 +0100202 ALOGV("setForceUse() tid %d", gettid());
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 }
Steve Block3856b092011-10-20 11:56:00 +0100228 ALOGV("getOutput() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700229 Mutex::Autolock _l(mLock);
Glenn Kasten254af182012-07-03 14:59:05 -0700230 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, format, channelMask, flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700231}
232
Eric Laurentde070132010-07-13 04:45:46 -0700233status_t AudioPolicyService::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700234 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700235 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700236{
Dima Zavinfce7a472011-04-19 22:30:36 -0700237 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700238 return NO_INIT;
239 }
Steve Block3856b092011-10-20 11:56:00 +0100240 ALOGV("startOutput() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700241 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700242 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700243}
244
Eric Laurentde070132010-07-13 04:45:46 -0700245status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700246 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700247 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700248{
Dima Zavinfce7a472011-04-19 22:30:36 -0700249 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700250 return NO_INIT;
251 }
Steve Block3856b092011-10-20 11:56:00 +0100252 ALOGV("stopOutput() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700253 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700254 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700255}
256
257void AudioPolicyService::releaseOutput(audio_io_handle_t output)
258{
Dima Zavinfce7a472011-04-19 22:30:36 -0700259 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700260 return;
261 }
Steve Block3856b092011-10-20 11:56:00 +0100262 ALOGV("releaseOutput() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700263 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700264 mpAudioPolicy->release_output(mpAudioPolicy, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700265}
266
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800267audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700268 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800269 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700270 audio_channel_mask_t channelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700271 int audioSession)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700272{
Dima Zavinfce7a472011-04-19 22:30:36 -0700273 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700274 return 0;
275 }
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800276 // already checked by client, but double-check in case the client wrapper is bypassed
277 if (uint32_t(inputSource) >= AUDIO_SOURCE_CNT) {
278 return 0;
279 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700280 Mutex::Autolock _l(mLock);
Glenn Kasten20010052012-06-22 13:43:51 -0700281 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700282 audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
Glenn Kasten254af182012-07-03 14:59:05 -0700283 format, channelMask, (audio_in_acoustics_t) 0);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700284
285 if (input == 0) {
286 return input;
287 }
288 // create audio pre processors according to input source
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800289 ssize_t index = mInputSources.indexOfKey(inputSource);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700290 if (index < 0) {
291 return input;
292 }
293 ssize_t idx = mInputs.indexOfKey(input);
294 InputDesc *inputDesc;
295 if (idx < 0) {
Glenn Kasten81872a22012-03-07 16:49:22 -0800296 inputDesc = new InputDesc(audioSession);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700297 mInputs.add(input, inputDesc);
298 } else {
299 inputDesc = mInputs.valueAt(idx);
300 }
301
302 Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects;
303 for (size_t i = 0; i < effects.size(); i++) {
304 EffectDesc *effect = effects[i];
305 sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input);
306 status_t status = fx->initCheck();
307 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000308 ALOGW("Failed to create Fx %s on input %d", effect->mName, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700309 // fx goes out of scope and strong ref on AudioEffect is released
310 continue;
311 }
312 for (size_t j = 0; j < effect->mParams.size(); j++) {
313 fx->setParameter(effect->mParams[j]);
314 }
315 inputDesc->mEffects.add(fx);
316 }
317 setPreProcessorEnabled(inputDesc, true);
318 return input;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700319}
320
321status_t AudioPolicyService::startInput(audio_io_handle_t input)
322{
Dima Zavinfce7a472011-04-19 22:30:36 -0700323 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324 return NO_INIT;
325 }
326 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700327
Dima Zavinfce7a472011-04-19 22:30:36 -0700328 return mpAudioPolicy->start_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700329}
330
331status_t AudioPolicyService::stopInput(audio_io_handle_t input)
332{
Dima Zavinfce7a472011-04-19 22:30:36 -0700333 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700334 return NO_INIT;
335 }
336 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700337
Dima Zavinfce7a472011-04-19 22:30:36 -0700338 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700339}
340
341void AudioPolicyService::releaseInput(audio_io_handle_t input)
342{
Dima Zavinfce7a472011-04-19 22:30:36 -0700343 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700344 return;
345 }
346 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700347 mpAudioPolicy->release_input(mpAudioPolicy, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700348
349 ssize_t index = mInputs.indexOfKey(input);
350 if (index < 0) {
351 return;
352 }
353 InputDesc *inputDesc = mInputs.valueAt(index);
354 setPreProcessorEnabled(inputDesc, false);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700355 delete inputDesc;
356 mInputs.removeItemsAt(index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700357}
358
Dima Zavinfce7a472011-04-19 22:30:36 -0700359status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360 int indexMin,
361 int indexMax)
362{
Dima Zavinfce7a472011-04-19 22:30:36 -0700363 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700364 return NO_INIT;
365 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800366 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700367 return PERMISSION_DENIED;
368 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800369 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370 return BAD_VALUE;
371 }
Eric Laurent5f121362012-06-15 14:45:03 -0700372 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700373 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700374 return NO_ERROR;
375}
376
Eric Laurent83844cc2011-11-18 16:43:31 -0800377status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
378 int index,
379 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700380{
Dima Zavinfce7a472011-04-19 22:30:36 -0700381 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700382 return NO_INIT;
383 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800384 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385 return PERMISSION_DENIED;
386 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800387 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388 return BAD_VALUE;
389 }
Eric Laurent5f121362012-06-15 14:45:03 -0700390 Mutex::Autolock _l(mLock);
Ricardo Cerqueira59199b12012-07-11 23:39:44 +0100391
392#ifndef ICS_AUDIO_BLOB
Eric Laurent83844cc2011-11-18 16:43:31 -0800393 if (mpAudioPolicy->set_stream_volume_index_for_device) {
394 return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
395 stream,
396 index,
397 device);
Ricardo Cerqueira59199b12012-07-11 23:39:44 +0100398 } else
399#endif
400 {
Eric Laurent83844cc2011-11-18 16:43:31 -0800401 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
402 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403}
404
Eric Laurent83844cc2011-11-18 16:43:31 -0800405status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
406 int *index,
407 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700408{
Dima Zavinfce7a472011-04-19 22:30:36 -0700409 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700410 return NO_INIT;
411 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800412 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700413 return BAD_VALUE;
414 }
Eric Laurent5f121362012-06-15 14:45:03 -0700415 Mutex::Autolock _l(mLock);
Ricardo Cerqueira59199b12012-07-11 23:39:44 +0100416#ifndef ICS_AUDIO_BLOB
Eric Laurent83844cc2011-11-18 16:43:31 -0800417 if (mpAudioPolicy->get_stream_volume_index_for_device) {
418 return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
419 stream,
420 index,
421 device);
Ricardo Cerqueira59199b12012-07-11 23:39:44 +0100422 } else
423#endif
424 {
Eric Laurent83844cc2011-11-18 16:43:31 -0800425 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
426 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700427}
428
Dima Zavinfce7a472011-04-19 22:30:36 -0700429uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700430{
Dima Zavinfce7a472011-04-19 22:30:36 -0700431 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700432 return 0;
433 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700434 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurentde070132010-07-13 04:45:46 -0700435}
436
Eric Laurent63742522012-03-08 13:42:42 -0800437//audio policy: use audio_device_t appropriately
438
439audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800440{
Dima Zavinfce7a472011-04-19 22:30:36 -0700441 if (mpAudioPolicy == NULL) {
Eric Laurent63742522012-03-08 13:42:42 -0800442 return (audio_devices_t)0;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800443 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700444 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800445}
446
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700447audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700448{
Dima Zavinfce7a472011-04-19 22:30:36 -0700449 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700450 return NO_INIT;
451 }
452 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700453 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700454}
455
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700456status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700457 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700458 uint32_t strategy,
459 int session,
460 int id)
461{
Dima Zavinfce7a472011-04-19 22:30:36 -0700462 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700463 return NO_INIT;
464 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700465 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700466}
467
468status_t AudioPolicyService::unregisterEffect(int id)
469{
Dima Zavinfce7a472011-04-19 22:30:36 -0700470 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700471 return NO_INIT;
472 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700473 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700474}
475
Eric Laurentdb7c0792011-08-10 10:37:50 -0700476status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
477{
478 if (mpAudioPolicy == NULL) {
479 return NO_INIT;
480 }
481 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
482}
483
Glenn Kastenfff6d712012-01-12 16:38:12 -0800484bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800485{
Dima Zavinfce7a472011-04-19 22:30:36 -0700486 if (mpAudioPolicy == NULL) {
Eric Laurenteda6c362011-02-02 09:33:30 -0800487 return 0;
488 }
489 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700490 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800491}
492
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700493bool AudioPolicyService::isSourceActive(audio_source_t source) const
494{
495 if (mpAudioPolicy == NULL) {
496 return false;
497 }
498 if (mpAudioPolicy->is_source_active == 0) {
499 return false;
500 }
501 Mutex::Autolock _l(mLock);
502 return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
503}
504
Eric Laurent57dae992011-07-24 13:36:09 -0700505status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
506 effect_descriptor_t *descriptors,
507 uint32_t *count)
508{
509
510 if (mpAudioPolicy == NULL) {
511 *count = 0;
512 return NO_INIT;
513 }
514 Mutex::Autolock _l(mLock);
515 status_t status = NO_ERROR;
516
517 size_t index;
518 for (index = 0; index < mInputs.size(); index++) {
519 if (mInputs.valueAt(index)->mSessionId == audioSession) {
520 break;
521 }
522 }
523 if (index == mInputs.size()) {
524 *count = 0;
525 return BAD_VALUE;
526 }
527 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
528
529 for (size_t i = 0; i < effects.size(); i++) {
530 effect_descriptor_t desc = effects[i]->descriptor();
531 if (i < *count) {
Glenn Kastena189a682012-02-20 12:16:30 -0800532 descriptors[i] = desc;
Eric Laurent57dae992011-07-24 13:36:09 -0700533 }
534 }
535 if (effects.size() > *count) {
536 status = NO_MEMORY;
537 }
538 *count = effects.size();
539 return status;
540}
541
Mathias Agopian65ab4712010-07-14 17:59:35 -0700542void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten23d82a92012-02-03 11:10:00 -0800543 ALOGW("binderDied() %p, tid %d, calling pid %d", who.unsafe_get(), gettid(),
Eric Laurentde070132010-07-13 04:45:46 -0700544 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700545}
546
547static bool tryLock(Mutex& mutex)
548{
549 bool locked = false;
550 for (int i = 0; i < kDumpLockRetries; ++i) {
551 if (mutex.tryLock() == NO_ERROR) {
552 locked = true;
553 break;
554 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800555 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700556 }
557 return locked;
558}
559
560status_t AudioPolicyService::dumpInternals(int fd)
561{
562 const size_t SIZE = 256;
563 char buffer[SIZE];
564 String8 result;
565
Dima Zavinfce7a472011-04-19 22:30:36 -0700566 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700567 result.append(buffer);
568 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
569 result.append(buffer);
570 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
571 result.append(buffer);
572
573 write(fd, result.string(), result.size());
574 return NO_ERROR;
575}
576
577status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
578{
Glenn Kasten44deb052012-02-05 18:09:08 -0800579 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700580 dumpPermissionDenial(fd);
581 } else {
582 bool locked = tryLock(mLock);
583 if (!locked) {
584 String8 result(kDeadlockedString);
585 write(fd, result.string(), result.size());
586 }
587
588 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800589 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700590 mAudioCommandThread->dump(fd);
591 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800592 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593 mTonePlaybackThread->dump(fd);
594 }
595
Dima Zavinfce7a472011-04-19 22:30:36 -0700596 if (mpAudioPolicy) {
597 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700598 }
599
600 if (locked) mLock.unlock();
601 }
602 return NO_ERROR;
603}
604
605status_t AudioPolicyService::dumpPermissionDenial(int fd)
606{
607 const size_t SIZE = 256;
608 char buffer[SIZE];
609 String8 result;
610 snprintf(buffer, SIZE, "Permission Denial: "
611 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
612 IPCThreadState::self()->getCallingPid(),
613 IPCThreadState::self()->getCallingUid());
614 result.append(buffer);
615 write(fd, result.string(), result.size());
616 return NO_ERROR;
617}
618
Glenn Kasten81872a22012-03-07 16:49:22 -0800619void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700620{
Glenn Kasten81872a22012-03-07 16:49:22 -0800621 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700622 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800623 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700624 }
625}
626
Mathias Agopian65ab4712010-07-14 17:59:35 -0700627status_t AudioPolicyService::onTransact(
628 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
629{
630 return BnAudioPolicyService::onTransact(code, data, reply, flags);
631}
632
633
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634// ----------- AudioPolicyService::AudioCommandThread implementation ----------
635
636AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name)
637 : Thread(false), mName(name)
638{
639 mpToneGenerator = NULL;
640}
641
642
643AudioPolicyService::AudioCommandThread::~AudioCommandThread()
644{
645 if (mName != "" && !mAudioCommands.isEmpty()) {
646 release_wake_lock(mName.string());
647 }
648 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800649 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650}
651
652void AudioPolicyService::AudioCommandThread::onFirstRef()
653{
654 if (mName != "") {
655 run(mName.string(), ANDROID_PRIORITY_AUDIO);
656 } else {
Glenn Kasten480b4682012-02-28 12:30:08 -0800657 run("AudioCommand", ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700658 }
659}
660
661bool AudioPolicyService::AudioCommandThread::threadLoop()
662{
663 nsecs_t waitTime = INT64_MAX;
664
665 mLock.lock();
666 while (!exitPending())
667 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700668 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700669 nsecs_t curTime = systemTime();
670 // commands are sorted by increasing time stamp: execute them from index 0 and up
671 if (mAudioCommands[0]->mTime <= curTime) {
672 AudioCommand *command = mAudioCommands[0];
673 mAudioCommands.removeAt(0);
674 mLastCommand = *command;
675
676 switch (command->mCommand) {
677 case START_TONE: {
678 mLock.unlock();
679 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100680 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700681 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800682 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700683 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
684 mpToneGenerator->startTone(data->mType);
685 delete data;
686 mLock.lock();
687 }break;
688 case STOP_TONE: {
689 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100690 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700691 if (mpToneGenerator != NULL) {
692 mpToneGenerator->stopTone();
693 delete mpToneGenerator;
694 mpToneGenerator = NULL;
695 }
696 mLock.lock();
697 }break;
698 case SET_VOLUME: {
699 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100700 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700701 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
702 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
703 data->mVolume,
704 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700705 if (command->mWaitStatus) {
706 command->mCond.signal();
707 mWaitWorkCV.wait(mLock);
708 }
709 delete data;
710 }break;
711 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700712 ParametersData *data = (ParametersData *)command->mParam;
713 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
714 data->mKeyValuePairs.string(), data->mIO);
715 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
716 if (command->mWaitStatus) {
717 command->mCond.signal();
718 mWaitWorkCV.wait(mLock);
719 }
720 delete data;
721 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700722 case SET_VOICE_VOLUME: {
723 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100724 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700725 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700726 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
727 if (command->mWaitStatus) {
728 command->mCond.signal();
729 mWaitWorkCV.wait(mLock);
730 }
731 delete data;
732 }break;
733 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000734 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735 }
736 delete command;
737 waitTime = INT64_MAX;
738 } else {
739 waitTime = mAudioCommands[0]->mTime - curTime;
740 break;
741 }
742 }
743 // release delayed commands wake lock
744 if (mName != "" && mAudioCommands.isEmpty()) {
745 release_wake_lock(mName.string());
746 }
Steve Block3856b092011-10-20 11:56:00 +0100747 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700748 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100749 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700750 }
751 mLock.unlock();
752 return false;
753}
754
755status_t AudioPolicyService::AudioCommandThread::dump(int fd)
756{
757 const size_t SIZE = 256;
758 char buffer[SIZE];
759 String8 result;
760
761 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
762 result.append(buffer);
763 write(fd, result.string(), result.size());
764
765 bool locked = tryLock(mLock);
766 if (!locked) {
767 String8 result2(kCmdDeadlockedString);
768 write(fd, result2.string(), result2.size());
769 }
770
771 snprintf(buffer, SIZE, "- Commands:\n");
772 result = String8(buffer);
773 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800774 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700775 mAudioCommands[i]->dump(buffer, SIZE);
776 result.append(buffer);
777 }
778 result.append(" Last Command\n");
779 mLastCommand.dump(buffer, SIZE);
780 result.append(buffer);
781
782 write(fd, result.string(), result.size());
783
784 if (locked) mLock.unlock();
785
786 return NO_ERROR;
787}
788
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800789void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
790 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700791{
792 AudioCommand *command = new AudioCommand();
793 command->mCommand = START_TONE;
794 ToneData *data = new ToneData();
795 data->mType = type;
796 data->mStream = stream;
797 command->mParam = (void *)data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700798 Mutex::Autolock _l(mLock);
799 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100800 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700801 mWaitWorkCV.signal();
802}
803
804void AudioPolicyService::AudioCommandThread::stopToneCommand()
805{
806 AudioCommand *command = new AudioCommand();
807 command->mCommand = STOP_TONE;
808 command->mParam = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700809 Mutex::Autolock _l(mLock);
810 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100811 ALOGV("AudioCommandThread() adding tone stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812 mWaitWorkCV.signal();
813}
814
Glenn Kastenfff6d712012-01-12 16:38:12 -0800815status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700816 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800817 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700818 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819{
820 status_t status = NO_ERROR;
821
822 AudioCommand *command = new AudioCommand();
823 command->mCommand = SET_VOLUME;
824 VolumeData *data = new VolumeData();
825 data->mStream = stream;
826 data->mVolume = volume;
827 data->mIO = output;
828 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700829 Mutex::Autolock _l(mLock);
830 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100831 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700832 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700833 mWaitWorkCV.signal();
834 if (command->mWaitStatus) {
835 command->mCond.wait(mLock);
836 status = command->mStatus;
837 mWaitWorkCV.signal();
838 }
839 return status;
840}
841
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800842status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700843 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700844 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700845{
846 status_t status = NO_ERROR;
847
848 AudioCommand *command = new AudioCommand();
849 command->mCommand = SET_PARAMETERS;
850 ParametersData *data = new ParametersData();
851 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700852 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700853 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700854 Mutex::Autolock _l(mLock);
855 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100856 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700857 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700858 mWaitWorkCV.signal();
859 if (command->mWaitStatus) {
860 command->mCond.wait(mLock);
861 status = command->mStatus;
862 mWaitWorkCV.signal();
863 }
864 return status;
865}
866
867status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
868{
869 status_t status = NO_ERROR;
870
871 AudioCommand *command = new AudioCommand();
872 command->mCommand = SET_VOICE_VOLUME;
873 VoiceVolumeData *data = new VoiceVolumeData();
874 data->mVolume = volume;
875 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700876 Mutex::Autolock _l(mLock);
877 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100878 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700879 mWaitWorkCV.signal();
880 if (command->mWaitStatus) {
881 command->mCond.wait(mLock);
882 status = command->mStatus;
883 mWaitWorkCV.signal();
884 }
885 return status;
886}
887
888// insertCommand_l() must be called with mLock held
889void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
890{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800891 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700892 Vector <AudioCommand *> removedCommands;
893
Eric Laurentcec4abb2012-07-03 12:23:02 -0700894 nsecs_t time = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700895 command->mTime = systemTime() + milliseconds(delayMs);
896
897 // acquire wake lock to make sure delayed commands are processed
898 if (mName != "" && mAudioCommands.isEmpty()) {
899 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
900 }
901
902 // check same pending commands with later time stamps and eliminate them
903 for (i = mAudioCommands.size()-1; i >= 0; i--) {
904 AudioCommand *command2 = mAudioCommands[i];
905 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
906 if (command2->mTime <= command->mTime) break;
907 if (command2->mCommand != command->mCommand) continue;
908
909 switch (command->mCommand) {
910 case SET_PARAMETERS: {
911 ParametersData *data = (ParametersData *)command->mParam;
912 ParametersData *data2 = (ParametersData *)command2->mParam;
913 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100914 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700915 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700916 AudioParameter param = AudioParameter(data->mKeyValuePairs);
917 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
918 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700919 String8 key;
920 String8 value;
921 param.getAt(j, key, value);
922 for (size_t k = 0; k < param2.size(); k++) {
923 String8 key2;
924 String8 value2;
925 param2.getAt(k, key2, value2);
926 if (key2 == key) {
927 param2.remove(key2);
928 ALOGV("Filtering out parameter %s", key2.string());
929 break;
930 }
931 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700932 }
933 // if all keys have been filtered out, remove the command.
934 // otherwise, update the key value pairs
935 if (param2.size() == 0) {
936 removedCommands.add(command2);
937 } else {
938 data2->mKeyValuePairs = param2.toString();
939 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700940 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700941 } break;
942
943 case SET_VOLUME: {
944 VolumeData *data = (VolumeData *)command->mParam;
945 VolumeData *data2 = (VolumeData *)command2->mParam;
946 if (data->mIO != data2->mIO) break;
947 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100948 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700949 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700950 removedCommands.add(command2);
Eric Laurentcec4abb2012-07-03 12:23:02 -0700951 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700952 } break;
953 case START_TONE:
954 case STOP_TONE:
955 default:
956 break;
957 }
958 }
959
960 // remove filtered commands
961 for (size_t j = 0; j < removedCommands.size(); j++) {
962 // removed commands always have time stamps greater than current command
963 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
964 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +0100965 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700966 mAudioCommands.removeAt(k);
967 break;
968 }
969 }
970 }
971 removedCommands.clear();
972
Eric Laurentcec4abb2012-07-03 12:23:02 -0700973 // wait for status only if delay is 0 and command time was not modified above
974 if (delayMs == 0 && time == 0) {
975 command->mWaitStatus = true;
976 } else {
977 command->mWaitStatus = false;
978 }
979 // update command time if modified above
980 if (time != 0) {
981 command->mTime = time;
982 }
983
Mathias Agopian65ab4712010-07-14 17:59:35 -0700984 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +0100985 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -0700986 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700987 mAudioCommands.insertAt(command, i + 1);
988}
989
990void AudioPolicyService::AudioCommandThread::exit()
991{
Steve Block3856b092011-10-20 11:56:00 +0100992 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700993 {
994 AutoMutex _l(mLock);
995 requestExit();
996 mWaitWorkCV.signal();
997 }
998 requestExitAndWait();
999}
1000
1001void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1002{
1003 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1004 mCommand,
1005 (int)ns2s(mTime),
1006 (int)ns2ms(mTime)%1000,
1007 mWaitStatus,
1008 mParam);
1009}
1010
Dima Zavinfce7a472011-04-19 22:30:36 -07001011/******* helpers for the service_ops callbacks defined below *********/
1012void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1013 const char *keyValuePairs,
1014 int delayMs)
1015{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001016 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001017 delayMs);
1018}
1019
1020int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1021 float volume,
1022 audio_io_handle_t output,
1023 int delayMs)
1024{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001025 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001026 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001027}
1028
1029int AudioPolicyService::startTone(audio_policy_tone_t tone,
1030 audio_stream_type_t stream)
1031{
1032 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
Steve Block29357bc2012-01-06 19:20:56 +00001033 ALOGE("startTone: illegal tone requested (%d)", tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001034 if (stream != AUDIO_STREAM_VOICE_CALL)
Steve Block29357bc2012-01-06 19:20:56 +00001035 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001036 tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001037 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1038 AUDIO_STREAM_VOICE_CALL);
1039 return 0;
1040}
1041
1042int AudioPolicyService::stopTone()
1043{
1044 mTonePlaybackThread->stopToneCommand();
1045 return 0;
1046}
1047
1048int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1049{
1050 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1051}
1052
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001053// ----------------------------------------------------------------------------
1054// Audio pre-processing configuration
1055// ----------------------------------------------------------------------------
1056
Glenn Kasten8dad0e32012-01-09 08:41:22 -08001057/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001058 MIC_SRC_TAG,
1059 VOICE_UL_SRC_TAG,
1060 VOICE_DL_SRC_TAG,
1061 VOICE_CALL_SRC_TAG,
1062 CAMCORDER_SRC_TAG,
1063 VOICE_REC_SRC_TAG,
1064 VOICE_COMM_SRC_TAG
1065};
1066
1067// returns the audio_source_t enum corresponding to the input source name or
1068// AUDIO_SOURCE_CNT is no match found
1069audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
1070{
1071 int i;
1072 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
1073 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001074 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001075 break;
1076 }
1077 }
1078 return (audio_source_t)i;
1079}
1080
1081size_t AudioPolicyService::growParamSize(char *param,
1082 size_t size,
1083 size_t *curSize,
1084 size_t *totSize)
1085{
1086 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
1087 size_t pos = ((*curSize - 1 ) / size + 1) * size;
1088
1089 if (pos + size > *totSize) {
1090 while (pos + size > *totSize) {
1091 *totSize += ((*totSize + 7) / 8) * 4;
1092 }
1093 param = (char *)realloc(param, *totSize);
1094 }
1095 *curSize = pos + size;
1096 return pos;
1097}
1098
1099size_t AudioPolicyService::readParamValue(cnode *node,
1100 char *param,
1101 size_t *curSize,
1102 size_t *totSize)
1103{
1104 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
1105 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
1106 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001107 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001108 return sizeof(short);
1109 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
1110 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
1111 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001112 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001113 return sizeof(int);
1114 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
1115 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
1116 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001117 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001118 return sizeof(float);
1119 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
1120 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
1121 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1122 *(bool *)((char *)param + pos) = false;
1123 } else {
1124 *(bool *)((char *)param + pos) = true;
1125 }
Steve Block3856b092011-10-20 11:56:00 +01001126 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001127 return sizeof(bool);
1128 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1129 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1130 if (*curSize + len + 1 > *totSize) {
1131 *totSize = *curSize + len + 1;
1132 param = (char *)realloc(param, *totSize);
1133 }
1134 strncpy(param + *curSize, node->value, len);
1135 *curSize += len;
1136 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +01001137 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001138 return len;
1139 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001140 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001141 return 0;
1142}
1143
1144effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1145{
1146 cnode *param;
1147 cnode *value;
1148 size_t curSize = sizeof(effect_param_t);
1149 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1150 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1151
1152 param = config_find(root, PARAM_TAG);
1153 value = config_find(root, VALUE_TAG);
1154 if (param == NULL && value == NULL) {
1155 // try to parse simple parameter form {int int}
1156 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -08001157 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001158 // Note: that a pair of random strings is read as 0 0
1159 int *ptr = (int *)fx_param->data;
1160 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +00001161 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001162 *ptr++ = atoi(param->name);
1163 *ptr = atoi(param->value);
1164 fx_param->psize = sizeof(int);
1165 fx_param->vsize = sizeof(int);
1166 return fx_param;
1167 }
1168 }
1169 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001170 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001171 goto error;
1172 }
1173
1174 fx_param->psize = 0;
1175 param = param->first_child;
1176 while (param) {
Steve Block3856b092011-10-20 11:56:00 +01001177 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001178 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1179 if (size == 0) {
1180 goto error;
1181 }
1182 fx_param->psize += size;
1183 param = param->next;
1184 }
1185
1186 // align start of value field on 32 bit boundary
1187 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1188
1189 fx_param->vsize = 0;
1190 value = value->first_child;
1191 while (value) {
Steve Block3856b092011-10-20 11:56:00 +01001192 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001193 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1194 if (size == 0) {
1195 goto error;
1196 }
1197 fx_param->vsize += size;
1198 value = value->next;
1199 }
1200
1201 return fx_param;
1202
1203error:
1204 delete fx_param;
1205 return NULL;
1206}
1207
1208void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1209{
1210 cnode *node = root->first_child;
1211 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001212 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001213 effect_param_t *param = loadEffectParameter(node);
1214 if (param == NULL) {
1215 node = node->next;
1216 continue;
1217 }
1218 params.add(param);
1219 node = node->next;
1220 }
1221}
1222
1223AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1224 cnode *root,
1225 const Vector <EffectDesc *>& effects)
1226{
1227 cnode *node = root->first_child;
1228 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001229 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001230 return NULL;
1231 }
1232 InputSourceDesc *source = new InputSourceDesc();
1233 while (node) {
1234 size_t i;
1235 for (i = 0; i < effects.size(); i++) {
1236 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001237 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001238 break;
1239 }
1240 }
1241 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +01001242 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001243 node = node->next;
1244 continue;
1245 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001246 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001247 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +01001248 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001249 source->mEffects.add(effect);
1250 node = node->next;
1251 }
1252 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001253 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001254 delete source;
1255 return NULL;
1256 }
1257 return source;
1258}
1259
1260status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1261{
1262 cnode *node = config_find(root, PREPROCESSING_TAG);
1263 if (node == NULL) {
1264 return -ENOENT;
1265 }
1266 node = node->first_child;
1267 while (node) {
1268 audio_source_t source = inputSourceNameToEnum(node->name);
1269 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001270 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001271 node = node->next;
1272 continue;
1273 }
Steve Block3856b092011-10-20 11:56:00 +01001274 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001275 InputSourceDesc *desc = loadInputSource(node, effects);
1276 if (desc == NULL) {
1277 node = node->next;
1278 continue;
1279 }
1280 mInputSources.add(source, desc);
1281 node = node->next;
1282 }
1283 return NO_ERROR;
1284}
1285
1286AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1287{
1288 cnode *node = config_find(root, UUID_TAG);
1289 if (node == NULL) {
1290 return NULL;
1291 }
1292 effect_uuid_t uuid;
1293 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001294 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001295 return NULL;
1296 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001297 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001298}
1299
1300status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1301{
1302 cnode *node = config_find(root, EFFECTS_TAG);
1303 if (node == NULL) {
1304 return -ENOENT;
1305 }
1306 node = node->first_child;
1307 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001308 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001309 EffectDesc *effect = loadEffect(node);
1310 if (effect == NULL) {
1311 node = node->next;
1312 continue;
1313 }
1314 effects.add(effect);
1315 node = node->next;
1316 }
1317 return NO_ERROR;
1318}
1319
1320status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1321{
1322 cnode *root;
1323 char *data;
1324
1325 data = (char *)load_file(path, NULL);
1326 if (data == NULL) {
1327 return -ENODEV;
1328 }
1329 root = config_node("", "");
1330 config_load(root, data);
1331
1332 Vector <EffectDesc *> effects;
1333 loadEffects(root, effects);
1334 loadInputSources(root, effects);
1335
1336 config_free(root);
1337 free(root);
1338 free(data);
1339
1340 return NO_ERROR;
1341}
1342
Dima Zavinfce7a472011-04-19 22:30:36 -07001343/* implementation of the interface to the policy manager */
1344extern "C" {
1345
Eric Laurenta4c5a552012-03-29 10:12:40 -07001346
1347static audio_module_handle_t aps_load_hw_module(void *service,
1348 const char *name)
Dima Zavinfce7a472011-04-19 22:30:36 -07001349{
1350 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001351 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001352 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001353 return 0;
1354 }
1355
Eric Laurenta4c5a552012-03-29 10:12:40 -07001356 return af->loadHwModule(name);
1357}
1358
1359// deprecated: replaced by aps_open_output_on_module()
1360static audio_io_handle_t aps_open_output(void *service,
1361 audio_devices_t *pDevices,
1362 uint32_t *pSamplingRate,
1363 audio_format_t *pFormat,
1364 audio_channel_mask_t *pChannelMask,
1365 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001366 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001367{
1368 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1369 if (af == 0) {
1370 ALOGW("%s: could not get AudioFlinger", __func__);
1371 return 0;
1372 }
1373
1374 return af->openOutput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask,
1375 pLatencyMs, flags);
1376}
1377
1378static audio_io_handle_t aps_open_output_on_module(void *service,
1379 audio_module_handle_t module,
1380 audio_devices_t *pDevices,
1381 uint32_t *pSamplingRate,
1382 audio_format_t *pFormat,
1383 audio_channel_mask_t *pChannelMask,
1384 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001385 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001386{
1387 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1388 if (af == 0) {
1389 ALOGW("%s: could not get AudioFlinger", __func__);
1390 return 0;
1391 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001392 return af->openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
Dima Zavinfce7a472011-04-19 22:30:36 -07001393 pLatencyMs, flags);
1394}
1395
1396static audio_io_handle_t aps_open_dup_output(void *service,
1397 audio_io_handle_t output1,
1398 audio_io_handle_t output2)
1399{
1400 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001401 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001402 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001403 return 0;
1404 }
1405 return af->openDuplicateOutput(output1, output2);
1406}
1407
1408static int aps_close_output(void *service, audio_io_handle_t output)
1409{
1410 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001411 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001412 return PERMISSION_DENIED;
1413
1414 return af->closeOutput(output);
1415}
1416
1417static int aps_suspend_output(void *service, audio_io_handle_t output)
1418{
1419 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001420 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001421 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001422 return PERMISSION_DENIED;
1423 }
1424
1425 return af->suspendOutput(output);
1426}
1427
1428static int aps_restore_output(void *service, audio_io_handle_t output)
1429{
1430 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001431 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001432 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001433 return PERMISSION_DENIED;
1434 }
1435
1436 return af->restoreOutput(output);
1437}
1438
Glenn Kasten20010052012-06-22 13:43:51 -07001439// deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
Dima Zavinfce7a472011-04-19 22:30:36 -07001440static audio_io_handle_t aps_open_input(void *service,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001441 audio_devices_t *pDevices,
1442 uint32_t *pSamplingRate,
1443 audio_format_t *pFormat,
1444 audio_channel_mask_t *pChannelMask,
1445 audio_in_acoustics_t acoustics)
Dima Zavinfce7a472011-04-19 22:30:36 -07001446{
1447 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001448 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001449 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001450 return 0;
1451 }
1452
Eric Laurenta4c5a552012-03-29 10:12:40 -07001453 return af->openInput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask);
1454}
1455
1456static audio_io_handle_t aps_open_input_on_module(void *service,
1457 audio_module_handle_t module,
1458 audio_devices_t *pDevices,
1459 uint32_t *pSamplingRate,
1460 audio_format_t *pFormat,
1461 audio_channel_mask_t *pChannelMask)
1462{
1463 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1464 if (af == 0) {
1465 ALOGW("%s: could not get AudioFlinger", __func__);
1466 return 0;
1467 }
1468
1469 return af->openInput(module, pDevices, pSamplingRate, pFormat, pChannelMask);
Dima Zavinfce7a472011-04-19 22:30:36 -07001470}
1471
1472static int aps_close_input(void *service, audio_io_handle_t input)
1473{
1474 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001475 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001476 return PERMISSION_DENIED;
1477
1478 return af->closeInput(input);
1479}
1480
1481static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1482 audio_io_handle_t output)
1483{
1484 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001485 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001486 return PERMISSION_DENIED;
1487
1488 return af->setStreamOutput(stream, output);
1489}
1490
1491static int aps_move_effects(void *service, int session,
1492 audio_io_handle_t src_output,
1493 audio_io_handle_t dst_output)
1494{
1495 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001496 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001497 return PERMISSION_DENIED;
1498
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001499 return af->moveEffects(session, src_output, dst_output);
Dima Zavinfce7a472011-04-19 22:30:36 -07001500}
1501
1502static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1503 const char *keys)
1504{
1505 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1506 return strdup(result.string());
1507}
1508
1509static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1510 const char *kv_pairs, int delay_ms)
1511{
1512 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1513
1514 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1515}
1516
1517static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1518 float volume, audio_io_handle_t output,
1519 int delay_ms)
1520{
1521 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1522
1523 return audioPolicyService->setStreamVolume(stream, volume, output,
1524 delay_ms);
1525}
1526
1527static int aps_start_tone(void *service, audio_policy_tone_t tone,
1528 audio_stream_type_t stream)
1529{
1530 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1531
1532 return audioPolicyService->startTone(tone, stream);
1533}
1534
1535static int aps_stop_tone(void *service)
1536{
1537 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1538
1539 return audioPolicyService->stopTone();
1540}
1541
1542static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1543{
1544 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1545
1546 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1547}
1548
1549}; // extern "C"
1550
1551namespace {
1552 struct audio_policy_service_ops aps_ops = {
1553 open_output : aps_open_output,
1554 open_duplicate_output : aps_open_dup_output,
1555 close_output : aps_close_output,
1556 suspend_output : aps_suspend_output,
1557 restore_output : aps_restore_output,
1558 open_input : aps_open_input,
1559 close_input : aps_close_input,
1560 set_stream_volume : aps_set_stream_volume,
1561 set_stream_output : aps_set_stream_output,
1562 set_parameters : aps_set_parameters,
1563 get_parameters : aps_get_parameters,
1564 start_tone : aps_start_tone,
1565 stop_tone : aps_stop_tone,
1566 set_voice_volume : aps_set_voice_volume,
1567 move_effects : aps_move_effects,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001568 load_hw_module : aps_load_hw_module,
1569 open_output_on_module : aps_open_output_on_module,
1570 open_input_on_module : aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001571 };
1572}; // namespace <unnamed>
1573
Mathias Agopian65ab4712010-07-14 17:59:35 -07001574}; // namespace android