blob: 4256fc4ab3fa7aa725f418efaa7c4db7857fca22 [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 Trivie336f912013-02-04 16:26:02 -0800487bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
488{
489 if (mpAudioPolicy == NULL) {
490 return 0;
491 }
492 Mutex::Autolock _l(mLock);
493 return mpAudioPolicy->is_stream_active_remotely(mpAudioPolicy, stream, inPastMs);
494}
495
Jean-Michel Trivie3f641f2012-10-10 12:11:16 -0700496bool AudioPolicyService::isSourceActive(audio_source_t source) const
497{
498 if (mpAudioPolicy == NULL) {
499 return false;
500 }
501 if (mpAudioPolicy->is_source_active == 0) {
502 return false;
503 }
504 Mutex::Autolock _l(mLock);
505 return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
506}
507
Eric Laurent57dae992011-07-24 13:36:09 -0700508status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
509 effect_descriptor_t *descriptors,
510 uint32_t *count)
511{
512
513 if (mpAudioPolicy == NULL) {
514 *count = 0;
515 return NO_INIT;
516 }
517 Mutex::Autolock _l(mLock);
518 status_t status = NO_ERROR;
519
520 size_t index;
521 for (index = 0; index < mInputs.size(); index++) {
522 if (mInputs.valueAt(index)->mSessionId == audioSession) {
523 break;
524 }
525 }
526 if (index == mInputs.size()) {
527 *count = 0;
528 return BAD_VALUE;
529 }
530 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
531
532 for (size_t i = 0; i < effects.size(); i++) {
533 effect_descriptor_t desc = effects[i]->descriptor();
534 if (i < *count) {
Glenn Kastena189a682012-02-20 12:16:30 -0800535 descriptors[i] = desc;
Eric Laurent57dae992011-07-24 13:36:09 -0700536 }
537 }
538 if (effects.size() > *count) {
539 status = NO_MEMORY;
540 }
541 *count = effects.size();
542 return status;
543}
544
Mathias Agopian65ab4712010-07-14 17:59:35 -0700545void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700546 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700547 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700548}
549
550static bool tryLock(Mutex& mutex)
551{
552 bool locked = false;
553 for (int i = 0; i < kDumpLockRetries; ++i) {
554 if (mutex.tryLock() == NO_ERROR) {
555 locked = true;
556 break;
557 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800558 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700559 }
560 return locked;
561}
562
563status_t AudioPolicyService::dumpInternals(int fd)
564{
565 const size_t SIZE = 256;
566 char buffer[SIZE];
567 String8 result;
568
Dima Zavinfce7a472011-04-19 22:30:36 -0700569 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700570 result.append(buffer);
571 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
572 result.append(buffer);
573 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
574 result.append(buffer);
575
576 write(fd, result.string(), result.size());
577 return NO_ERROR;
578}
579
580status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
581{
Glenn Kasten44deb052012-02-05 18:09:08 -0800582 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700583 dumpPermissionDenial(fd);
584 } else {
585 bool locked = tryLock(mLock);
586 if (!locked) {
587 String8 result(kDeadlockedString);
588 write(fd, result.string(), result.size());
589 }
590
591 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800592 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593 mAudioCommandThread->dump(fd);
594 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800595 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700596 mTonePlaybackThread->dump(fd);
597 }
598
Dima Zavinfce7a472011-04-19 22:30:36 -0700599 if (mpAudioPolicy) {
600 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700601 }
602
603 if (locked) mLock.unlock();
604 }
605 return NO_ERROR;
606}
607
608status_t AudioPolicyService::dumpPermissionDenial(int fd)
609{
610 const size_t SIZE = 256;
611 char buffer[SIZE];
612 String8 result;
613 snprintf(buffer, SIZE, "Permission Denial: "
614 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
615 IPCThreadState::self()->getCallingPid(),
616 IPCThreadState::self()->getCallingUid());
617 result.append(buffer);
618 write(fd, result.string(), result.size());
619 return NO_ERROR;
620}
621
Glenn Kasten81872a22012-03-07 16:49:22 -0800622void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700623{
Glenn Kasten81872a22012-03-07 16:49:22 -0800624 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700625 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800626 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700627 }
628}
629
Mathias Agopian65ab4712010-07-14 17:59:35 -0700630status_t AudioPolicyService::onTransact(
631 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
632{
633 return BnAudioPolicyService::onTransact(code, data, reply, flags);
634}
635
636
Mathias Agopian65ab4712010-07-14 17:59:35 -0700637// ----------- AudioPolicyService::AudioCommandThread implementation ----------
638
639AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name)
640 : Thread(false), mName(name)
641{
642 mpToneGenerator = NULL;
643}
644
645
646AudioPolicyService::AudioCommandThread::~AudioCommandThread()
647{
648 if (mName != "" && !mAudioCommands.isEmpty()) {
649 release_wake_lock(mName.string());
650 }
651 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800652 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700653}
654
655void AudioPolicyService::AudioCommandThread::onFirstRef()
656{
657 if (mName != "") {
658 run(mName.string(), ANDROID_PRIORITY_AUDIO);
659 } else {
Glenn Kasten480b4682012-02-28 12:30:08 -0800660 run("AudioCommand", ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700661 }
662}
663
664bool AudioPolicyService::AudioCommandThread::threadLoop()
665{
666 nsecs_t waitTime = INT64_MAX;
667
668 mLock.lock();
669 while (!exitPending())
670 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700671 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672 nsecs_t curTime = systemTime();
673 // commands are sorted by increasing time stamp: execute them from index 0 and up
674 if (mAudioCommands[0]->mTime <= curTime) {
675 AudioCommand *command = mAudioCommands[0];
676 mAudioCommands.removeAt(0);
677 mLastCommand = *command;
678
679 switch (command->mCommand) {
680 case START_TONE: {
681 mLock.unlock();
682 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100683 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700684 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800685 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700686 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
687 mpToneGenerator->startTone(data->mType);
688 delete data;
689 mLock.lock();
690 }break;
691 case STOP_TONE: {
692 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100693 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694 if (mpToneGenerator != NULL) {
695 mpToneGenerator->stopTone();
696 delete mpToneGenerator;
697 mpToneGenerator = NULL;
698 }
699 mLock.lock();
700 }break;
701 case SET_VOLUME: {
702 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100703 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700704 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
705 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
706 data->mVolume,
707 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700708 if (command->mWaitStatus) {
709 command->mCond.signal();
710 mWaitWorkCV.wait(mLock);
711 }
712 delete data;
713 }break;
714 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700715 ParametersData *data = (ParametersData *)command->mParam;
716 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
717 data->mKeyValuePairs.string(), data->mIO);
718 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
719 if (command->mWaitStatus) {
720 command->mCond.signal();
721 mWaitWorkCV.wait(mLock);
722 }
723 delete data;
724 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700725 case SET_VOICE_VOLUME: {
726 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100727 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700728 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700729 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
730 if (command->mWaitStatus) {
731 command->mCond.signal();
732 mWaitWorkCV.wait(mLock);
733 }
734 delete data;
735 }break;
736 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000737 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700738 }
739 delete command;
740 waitTime = INT64_MAX;
741 } else {
742 waitTime = mAudioCommands[0]->mTime - curTime;
743 break;
744 }
745 }
746 // release delayed commands wake lock
747 if (mName != "" && mAudioCommands.isEmpty()) {
748 release_wake_lock(mName.string());
749 }
Steve Block3856b092011-10-20 11:56:00 +0100750 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700751 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100752 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700753 }
754 mLock.unlock();
755 return false;
756}
757
758status_t AudioPolicyService::AudioCommandThread::dump(int fd)
759{
760 const size_t SIZE = 256;
761 char buffer[SIZE];
762 String8 result;
763
764 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
765 result.append(buffer);
766 write(fd, result.string(), result.size());
767
768 bool locked = tryLock(mLock);
769 if (!locked) {
770 String8 result2(kCmdDeadlockedString);
771 write(fd, result2.string(), result2.size());
772 }
773
774 snprintf(buffer, SIZE, "- Commands:\n");
775 result = String8(buffer);
776 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800777 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700778 mAudioCommands[i]->dump(buffer, SIZE);
779 result.append(buffer);
780 }
781 result.append(" Last Command\n");
782 mLastCommand.dump(buffer, SIZE);
783 result.append(buffer);
784
785 write(fd, result.string(), result.size());
786
787 if (locked) mLock.unlock();
788
789 return NO_ERROR;
790}
791
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800792void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
793 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794{
795 AudioCommand *command = new AudioCommand();
796 command->mCommand = START_TONE;
797 ToneData *data = new ToneData();
798 data->mType = type;
799 data->mStream = stream;
800 command->mParam = (void *)data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700801 Mutex::Autolock _l(mLock);
802 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100803 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700804 mWaitWorkCV.signal();
805}
806
807void AudioPolicyService::AudioCommandThread::stopToneCommand()
808{
809 AudioCommand *command = new AudioCommand();
810 command->mCommand = STOP_TONE;
811 command->mParam = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812 Mutex::Autolock _l(mLock);
813 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100814 ALOGV("AudioCommandThread() adding tone stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700815 mWaitWorkCV.signal();
816}
817
Glenn Kastenfff6d712012-01-12 16:38:12 -0800818status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700819 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800820 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700821 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700822{
823 status_t status = NO_ERROR;
824
825 AudioCommand *command = new AudioCommand();
826 command->mCommand = SET_VOLUME;
827 VolumeData *data = new VolumeData();
828 data->mStream = stream;
829 data->mVolume = volume;
830 data->mIO = output;
831 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700832 Mutex::Autolock _l(mLock);
833 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100834 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700835 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700836 mWaitWorkCV.signal();
837 if (command->mWaitStatus) {
838 command->mCond.wait(mLock);
839 status = command->mStatus;
840 mWaitWorkCV.signal();
841 }
842 return status;
843}
844
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800845status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700846 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700847 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700848{
849 status_t status = NO_ERROR;
850
851 AudioCommand *command = new AudioCommand();
852 command->mCommand = SET_PARAMETERS;
853 ParametersData *data = new ParametersData();
854 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700855 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700856 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700857 Mutex::Autolock _l(mLock);
858 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100859 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700860 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700861 mWaitWorkCV.signal();
862 if (command->mWaitStatus) {
863 command->mCond.wait(mLock);
864 status = command->mStatus;
865 mWaitWorkCV.signal();
866 }
867 return status;
868}
869
870status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
871{
872 status_t status = NO_ERROR;
873
874 AudioCommand *command = new AudioCommand();
875 command->mCommand = SET_VOICE_VOLUME;
876 VoiceVolumeData *data = new VoiceVolumeData();
877 data->mVolume = volume;
878 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700879 Mutex::Autolock _l(mLock);
880 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100881 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700882 mWaitWorkCV.signal();
883 if (command->mWaitStatus) {
884 command->mCond.wait(mLock);
885 status = command->mStatus;
886 mWaitWorkCV.signal();
887 }
888 return status;
889}
890
891// insertCommand_l() must be called with mLock held
892void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
893{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800894 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700895 Vector <AudioCommand *> removedCommands;
896
Eric Laurentcec4abb2012-07-03 12:23:02 -0700897 nsecs_t time = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700898 command->mTime = systemTime() + milliseconds(delayMs);
899
900 // acquire wake lock to make sure delayed commands are processed
901 if (mName != "" && mAudioCommands.isEmpty()) {
902 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
903 }
904
905 // check same pending commands with later time stamps and eliminate them
906 for (i = mAudioCommands.size()-1; i >= 0; i--) {
907 AudioCommand *command2 = mAudioCommands[i];
908 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
909 if (command2->mTime <= command->mTime) break;
910 if (command2->mCommand != command->mCommand) continue;
911
912 switch (command->mCommand) {
913 case SET_PARAMETERS: {
914 ParametersData *data = (ParametersData *)command->mParam;
915 ParametersData *data2 = (ParametersData *)command2->mParam;
916 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100917 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700918 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700919 AudioParameter param = AudioParameter(data->mKeyValuePairs);
920 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
921 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700922 String8 key;
923 String8 value;
924 param.getAt(j, key, value);
925 for (size_t k = 0; k < param2.size(); k++) {
926 String8 key2;
927 String8 value2;
928 param2.getAt(k, key2, value2);
929 if (key2 == key) {
930 param2.remove(key2);
931 ALOGV("Filtering out parameter %s", key2.string());
932 break;
933 }
934 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700935 }
936 // if all keys have been filtered out, remove the command.
937 // otherwise, update the key value pairs
938 if (param2.size() == 0) {
939 removedCommands.add(command2);
940 } else {
941 data2->mKeyValuePairs = param2.toString();
942 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700943 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944 } break;
945
946 case SET_VOLUME: {
947 VolumeData *data = (VolumeData *)command->mParam;
948 VolumeData *data2 = (VolumeData *)command2->mParam;
949 if (data->mIO != data2->mIO) break;
950 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100951 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700952 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700953 removedCommands.add(command2);
Eric Laurentcec4abb2012-07-03 12:23:02 -0700954 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700955 } break;
956 case START_TONE:
957 case STOP_TONE:
958 default:
959 break;
960 }
961 }
962
963 // remove filtered commands
964 for (size_t j = 0; j < removedCommands.size(); j++) {
965 // removed commands always have time stamps greater than current command
966 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
967 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +0100968 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700969 mAudioCommands.removeAt(k);
970 break;
971 }
972 }
973 }
974 removedCommands.clear();
975
Eric Laurentcec4abb2012-07-03 12:23:02 -0700976 // wait for status only if delay is 0 and command time was not modified above
977 if (delayMs == 0 && time == 0) {
978 command->mWaitStatus = true;
979 } else {
980 command->mWaitStatus = false;
981 }
982 // update command time if modified above
983 if (time != 0) {
984 command->mTime = time;
985 }
986
Mathias Agopian65ab4712010-07-14 17:59:35 -0700987 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +0100988 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -0700989 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700990 mAudioCommands.insertAt(command, i + 1);
991}
992
993void AudioPolicyService::AudioCommandThread::exit()
994{
Steve Block3856b092011-10-20 11:56:00 +0100995 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700996 {
997 AutoMutex _l(mLock);
998 requestExit();
999 mWaitWorkCV.signal();
1000 }
1001 requestExitAndWait();
1002}
1003
1004void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1005{
1006 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1007 mCommand,
1008 (int)ns2s(mTime),
1009 (int)ns2ms(mTime)%1000,
1010 mWaitStatus,
1011 mParam);
1012}
1013
Dima Zavinfce7a472011-04-19 22:30:36 -07001014/******* helpers for the service_ops callbacks defined below *********/
1015void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1016 const char *keyValuePairs,
1017 int delayMs)
1018{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001019 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001020 delayMs);
1021}
1022
1023int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1024 float volume,
1025 audio_io_handle_t output,
1026 int delayMs)
1027{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001028 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001029 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001030}
1031
1032int AudioPolicyService::startTone(audio_policy_tone_t tone,
1033 audio_stream_type_t stream)
1034{
1035 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
Steve Block29357bc2012-01-06 19:20:56 +00001036 ALOGE("startTone: illegal tone requested (%d)", tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001037 if (stream != AUDIO_STREAM_VOICE_CALL)
Steve Block29357bc2012-01-06 19:20:56 +00001038 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001039 tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001040 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1041 AUDIO_STREAM_VOICE_CALL);
1042 return 0;
1043}
1044
1045int AudioPolicyService::stopTone()
1046{
1047 mTonePlaybackThread->stopToneCommand();
1048 return 0;
1049}
1050
1051int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1052{
1053 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1054}
1055
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001056// ----------------------------------------------------------------------------
1057// Audio pre-processing configuration
1058// ----------------------------------------------------------------------------
1059
Glenn Kasten8dad0e32012-01-09 08:41:22 -08001060/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001061 MIC_SRC_TAG,
1062 VOICE_UL_SRC_TAG,
1063 VOICE_DL_SRC_TAG,
1064 VOICE_CALL_SRC_TAG,
1065 CAMCORDER_SRC_TAG,
1066 VOICE_REC_SRC_TAG,
1067 VOICE_COMM_SRC_TAG
1068};
1069
1070// returns the audio_source_t enum corresponding to the input source name or
1071// AUDIO_SOURCE_CNT is no match found
1072audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
1073{
1074 int i;
1075 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
1076 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001077 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001078 break;
1079 }
1080 }
1081 return (audio_source_t)i;
1082}
1083
1084size_t AudioPolicyService::growParamSize(char *param,
1085 size_t size,
1086 size_t *curSize,
1087 size_t *totSize)
1088{
1089 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
1090 size_t pos = ((*curSize - 1 ) / size + 1) * size;
1091
1092 if (pos + size > *totSize) {
1093 while (pos + size > *totSize) {
1094 *totSize += ((*totSize + 7) / 8) * 4;
1095 }
1096 param = (char *)realloc(param, *totSize);
1097 }
1098 *curSize = pos + size;
1099 return pos;
1100}
1101
1102size_t AudioPolicyService::readParamValue(cnode *node,
1103 char *param,
1104 size_t *curSize,
1105 size_t *totSize)
1106{
1107 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
1108 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
1109 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001110 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001111 return sizeof(short);
1112 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
1113 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
1114 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001115 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001116 return sizeof(int);
1117 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
1118 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
1119 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001120 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001121 return sizeof(float);
1122 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
1123 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
1124 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1125 *(bool *)((char *)param + pos) = false;
1126 } else {
1127 *(bool *)((char *)param + pos) = true;
1128 }
Steve Block3856b092011-10-20 11:56:00 +01001129 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001130 return sizeof(bool);
1131 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1132 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1133 if (*curSize + len + 1 > *totSize) {
1134 *totSize = *curSize + len + 1;
1135 param = (char *)realloc(param, *totSize);
1136 }
1137 strncpy(param + *curSize, node->value, len);
1138 *curSize += len;
1139 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +01001140 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001141 return len;
1142 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001143 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001144 return 0;
1145}
1146
1147effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1148{
1149 cnode *param;
1150 cnode *value;
1151 size_t curSize = sizeof(effect_param_t);
1152 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1153 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1154
1155 param = config_find(root, PARAM_TAG);
1156 value = config_find(root, VALUE_TAG);
1157 if (param == NULL && value == NULL) {
1158 // try to parse simple parameter form {int int}
1159 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -08001160 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001161 // Note: that a pair of random strings is read as 0 0
1162 int *ptr = (int *)fx_param->data;
1163 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +00001164 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001165 *ptr++ = atoi(param->name);
1166 *ptr = atoi(param->value);
1167 fx_param->psize = sizeof(int);
1168 fx_param->vsize = sizeof(int);
1169 return fx_param;
1170 }
1171 }
1172 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001173 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001174 goto error;
1175 }
1176
1177 fx_param->psize = 0;
1178 param = param->first_child;
1179 while (param) {
Steve Block3856b092011-10-20 11:56:00 +01001180 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001181 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1182 if (size == 0) {
1183 goto error;
1184 }
1185 fx_param->psize += size;
1186 param = param->next;
1187 }
1188
1189 // align start of value field on 32 bit boundary
1190 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1191
1192 fx_param->vsize = 0;
1193 value = value->first_child;
1194 while (value) {
Steve Block3856b092011-10-20 11:56:00 +01001195 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001196 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1197 if (size == 0) {
1198 goto error;
1199 }
1200 fx_param->vsize += size;
1201 value = value->next;
1202 }
1203
1204 return fx_param;
1205
1206error:
1207 delete fx_param;
1208 return NULL;
1209}
1210
1211void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1212{
1213 cnode *node = root->first_child;
1214 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001215 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001216 effect_param_t *param = loadEffectParameter(node);
1217 if (param == NULL) {
1218 node = node->next;
1219 continue;
1220 }
1221 params.add(param);
1222 node = node->next;
1223 }
1224}
1225
1226AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1227 cnode *root,
1228 const Vector <EffectDesc *>& effects)
1229{
1230 cnode *node = root->first_child;
1231 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001232 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001233 return NULL;
1234 }
1235 InputSourceDesc *source = new InputSourceDesc();
1236 while (node) {
1237 size_t i;
1238 for (i = 0; i < effects.size(); i++) {
1239 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001240 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001241 break;
1242 }
1243 }
1244 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +01001245 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001246 node = node->next;
1247 continue;
1248 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001249 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001250 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +01001251 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001252 source->mEffects.add(effect);
1253 node = node->next;
1254 }
1255 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001256 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001257 delete source;
1258 return NULL;
1259 }
1260 return source;
1261}
1262
1263status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1264{
1265 cnode *node = config_find(root, PREPROCESSING_TAG);
1266 if (node == NULL) {
1267 return -ENOENT;
1268 }
1269 node = node->first_child;
1270 while (node) {
1271 audio_source_t source = inputSourceNameToEnum(node->name);
1272 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001273 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001274 node = node->next;
1275 continue;
1276 }
Steve Block3856b092011-10-20 11:56:00 +01001277 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001278 InputSourceDesc *desc = loadInputSource(node, effects);
1279 if (desc == NULL) {
1280 node = node->next;
1281 continue;
1282 }
1283 mInputSources.add(source, desc);
1284 node = node->next;
1285 }
1286 return NO_ERROR;
1287}
1288
1289AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1290{
1291 cnode *node = config_find(root, UUID_TAG);
1292 if (node == NULL) {
1293 return NULL;
1294 }
1295 effect_uuid_t uuid;
1296 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001297 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001298 return NULL;
1299 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001300 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001301}
1302
1303status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1304{
1305 cnode *node = config_find(root, EFFECTS_TAG);
1306 if (node == NULL) {
1307 return -ENOENT;
1308 }
1309 node = node->first_child;
1310 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001311 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001312 EffectDesc *effect = loadEffect(node);
1313 if (effect == NULL) {
1314 node = node->next;
1315 continue;
1316 }
1317 effects.add(effect);
1318 node = node->next;
1319 }
1320 return NO_ERROR;
1321}
1322
1323status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1324{
1325 cnode *root;
1326 char *data;
1327
1328 data = (char *)load_file(path, NULL);
1329 if (data == NULL) {
1330 return -ENODEV;
1331 }
1332 root = config_node("", "");
1333 config_load(root, data);
1334
1335 Vector <EffectDesc *> effects;
1336 loadEffects(root, effects);
1337 loadInputSources(root, effects);
1338
1339 config_free(root);
1340 free(root);
1341 free(data);
1342
1343 return NO_ERROR;
1344}
1345
Dima Zavinfce7a472011-04-19 22:30:36 -07001346/* implementation of the interface to the policy manager */
1347extern "C" {
1348
Eric Laurenta4c5a552012-03-29 10:12:40 -07001349
1350static audio_module_handle_t aps_load_hw_module(void *service,
1351 const char *name)
Dima Zavinfce7a472011-04-19 22:30:36 -07001352{
1353 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001354 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001355 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001356 return 0;
1357 }
1358
Eric Laurenta4c5a552012-03-29 10:12:40 -07001359 return af->loadHwModule(name);
1360}
1361
1362// deprecated: replaced by aps_open_output_on_module()
1363static audio_io_handle_t aps_open_output(void *service,
1364 audio_devices_t *pDevices,
1365 uint32_t *pSamplingRate,
1366 audio_format_t *pFormat,
1367 audio_channel_mask_t *pChannelMask,
1368 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001369 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001370{
1371 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1372 if (af == 0) {
1373 ALOGW("%s: could not get AudioFlinger", __func__);
1374 return 0;
1375 }
1376
1377 return af->openOutput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask,
1378 pLatencyMs, flags);
1379}
1380
1381static audio_io_handle_t aps_open_output_on_module(void *service,
1382 audio_module_handle_t module,
1383 audio_devices_t *pDevices,
1384 uint32_t *pSamplingRate,
1385 audio_format_t *pFormat,
1386 audio_channel_mask_t *pChannelMask,
1387 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001388 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001389{
1390 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1391 if (af == 0) {
1392 ALOGW("%s: could not get AudioFlinger", __func__);
1393 return 0;
1394 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001395 return af->openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
Dima Zavinfce7a472011-04-19 22:30:36 -07001396 pLatencyMs, flags);
1397}
1398
1399static audio_io_handle_t aps_open_dup_output(void *service,
1400 audio_io_handle_t output1,
1401 audio_io_handle_t output2)
1402{
1403 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001404 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001405 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001406 return 0;
1407 }
1408 return af->openDuplicateOutput(output1, output2);
1409}
1410
1411static int aps_close_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)
Dima Zavinfce7a472011-04-19 22:30:36 -07001415 return PERMISSION_DENIED;
1416
1417 return af->closeOutput(output);
1418}
1419
1420static int aps_suspend_output(void *service, audio_io_handle_t output)
1421{
1422 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001423 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001424 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001425 return PERMISSION_DENIED;
1426 }
1427
1428 return af->suspendOutput(output);
1429}
1430
1431static int aps_restore_output(void *service, audio_io_handle_t output)
1432{
1433 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001434 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001435 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001436 return PERMISSION_DENIED;
1437 }
1438
1439 return af->restoreOutput(output);
1440}
1441
Glenn Kasten20010052012-06-22 13:43:51 -07001442// deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
Dima Zavinfce7a472011-04-19 22:30:36 -07001443static audio_io_handle_t aps_open_input(void *service,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001444 audio_devices_t *pDevices,
1445 uint32_t *pSamplingRate,
1446 audio_format_t *pFormat,
1447 audio_channel_mask_t *pChannelMask,
1448 audio_in_acoustics_t acoustics)
Dima Zavinfce7a472011-04-19 22:30:36 -07001449{
1450 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001451 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001452 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001453 return 0;
1454 }
1455
Eric Laurenta4c5a552012-03-29 10:12:40 -07001456 return af->openInput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask);
1457}
1458
1459static audio_io_handle_t aps_open_input_on_module(void *service,
1460 audio_module_handle_t module,
1461 audio_devices_t *pDevices,
1462 uint32_t *pSamplingRate,
1463 audio_format_t *pFormat,
1464 audio_channel_mask_t *pChannelMask)
1465{
1466 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1467 if (af == 0) {
1468 ALOGW("%s: could not get AudioFlinger", __func__);
1469 return 0;
1470 }
1471
1472 return af->openInput(module, pDevices, pSamplingRate, pFormat, pChannelMask);
Dima Zavinfce7a472011-04-19 22:30:36 -07001473}
1474
1475static int aps_close_input(void *service, audio_io_handle_t input)
1476{
1477 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001478 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001479 return PERMISSION_DENIED;
1480
1481 return af->closeInput(input);
1482}
1483
1484static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1485 audio_io_handle_t output)
1486{
1487 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001488 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001489 return PERMISSION_DENIED;
1490
1491 return af->setStreamOutput(stream, output);
1492}
1493
1494static int aps_move_effects(void *service, int session,
1495 audio_io_handle_t src_output,
1496 audio_io_handle_t dst_output)
1497{
1498 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001499 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001500 return PERMISSION_DENIED;
1501
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001502 return af->moveEffects(session, src_output, dst_output);
Dima Zavinfce7a472011-04-19 22:30:36 -07001503}
1504
1505static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1506 const char *keys)
1507{
1508 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1509 return strdup(result.string());
1510}
1511
1512static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1513 const char *kv_pairs, int delay_ms)
1514{
1515 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1516
1517 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1518}
1519
1520static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1521 float volume, audio_io_handle_t output,
1522 int delay_ms)
1523{
1524 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1525
1526 return audioPolicyService->setStreamVolume(stream, volume, output,
1527 delay_ms);
1528}
1529
1530static int aps_start_tone(void *service, audio_policy_tone_t tone,
1531 audio_stream_type_t stream)
1532{
1533 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1534
1535 return audioPolicyService->startTone(tone, stream);
1536}
1537
1538static int aps_stop_tone(void *service)
1539{
1540 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1541
1542 return audioPolicyService->stopTone();
1543}
1544
1545static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1546{
1547 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1548
1549 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1550}
1551
1552}; // extern "C"
1553
1554namespace {
1555 struct audio_policy_service_ops aps_ops = {
1556 open_output : aps_open_output,
1557 open_duplicate_output : aps_open_dup_output,
1558 close_output : aps_close_output,
1559 suspend_output : aps_suspend_output,
1560 restore_output : aps_restore_output,
1561 open_input : aps_open_input,
1562 close_input : aps_close_input,
1563 set_stream_volume : aps_set_stream_volume,
1564 set_stream_output : aps_set_stream_output,
1565 set_parameters : aps_set_parameters,
1566 get_parameters : aps_get_parameters,
1567 start_tone : aps_start_tone,
1568 stop_tone : aps_stop_tone,
1569 set_voice_volume : aps_set_voice_volume,
1570 move_effects : aps_move_effects,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001571 load_hw_module : aps_load_hw_module,
1572 open_output_on_module : aps_open_output_on_module,
1573 open_input_on_module : aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001574 };
1575}; // namespace <unnamed>
1576
Mathias Agopian65ab4712010-07-14 17:59:35 -07001577}; // namespace android