blob: 9a654abb1ad8321fa03d6c8caab6e1273bd17d5d [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
94 property_get("ro.camera.sound.forced", value, "0");
95 forced_val = strtol(value, NULL, 0);
96 mpAudioPolicy->set_can_mute_enforced_audible(mpAudioPolicy, !forced_val);
97
Steve Blockdf64d152012-01-04 20:05:49 +000098 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurent7c7f10b2011-06-17 21:29:58 -070099
100 // load audio pre processing modules
101 if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) {
102 loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE);
103 } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) {
104 loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE);
105 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700106}
107
108AudioPolicyService::~AudioPolicyService()
109{
110 mTonePlaybackThread->exit();
111 mTonePlaybackThread.clear();
112 mAudioCommandThread->exit();
113 mAudioCommandThread.clear();
114
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700115
116 // release audio pre processing resources
117 for (size_t i = 0; i < mInputSources.size(); i++) {
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800118 delete mInputSources.valueAt(i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700119 }
120 mInputSources.clear();
121
122 for (size_t i = 0; i < mInputs.size(); i++) {
123 mInputs.valueAt(i)->mEffects.clear();
124 delete mInputs.valueAt(i);
125 }
126 mInputs.clear();
127
Glenn Kastena0d68332012-01-27 16:47:15 -0800128 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL)
Dima Zavinfce7a472011-04-19 22:30:36 -0700129 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kastena0d68332012-01-27 16:47:15 -0800130 if (mpAudioPolicyDev != NULL)
Dima Zavinfce7a472011-04-19 22:30:36 -0700131 audio_policy_dev_close(mpAudioPolicyDev);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700132}
133
Dima Zavinfce7a472011-04-19 22:30:36 -0700134status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
135 audio_policy_dev_state_t state,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700136 const char *device_address)
137{
Dima Zavinfce7a472011-04-19 22:30:36 -0700138 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700139 return NO_INIT;
140 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800141 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700142 return PERMISSION_DENIED;
143 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700144 if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700145 return BAD_VALUE;
146 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700147 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
148 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700149 return BAD_VALUE;
150 }
151
Steve Block3856b092011-10-20 11:56:00 +0100152 ALOGV("setDeviceConnectionState() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700153 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700154 return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device,
155 state, device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700156}
157
Dima Zavinfce7a472011-04-19 22:30:36 -0700158audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
159 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -0700160 const char *device_address)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700161{
Dima Zavinfce7a472011-04-19 22:30:36 -0700162 if (mpAudioPolicy == NULL) {
163 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700164 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700165 return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device,
166 device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700167}
168
Glenn Kastenf78aee72012-01-04 11:00:47 -0800169status_t AudioPolicyService::setPhoneState(audio_mode_t state)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700170{
Dima Zavinfce7a472011-04-19 22:30:36 -0700171 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700172 return NO_INIT;
173 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800174 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700175 return PERMISSION_DENIED;
176 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800177 if (uint32_t(state) >= AUDIO_MODE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700178 return BAD_VALUE;
179 }
180
Steve Block3856b092011-10-20 11:56:00 +0100181 ALOGV("setPhoneState() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700182
183 // TODO: check if it is more appropriate to do it in platform specific policy manager
184 AudioSystem::setMode(state);
185
186 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700187 mpAudioPolicy->set_phone_state(mpAudioPolicy, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700188 return NO_ERROR;
189}
190
Dima Zavinfce7a472011-04-19 22:30:36 -0700191status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
192 audio_policy_forced_cfg_t config)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700193{
Dima Zavinfce7a472011-04-19 22:30:36 -0700194 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700195 return NO_INIT;
196 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800197 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700198 return PERMISSION_DENIED;
199 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700200 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700201 return BAD_VALUE;
202 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700203 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700204 return BAD_VALUE;
205 }
Steve Block3856b092011-10-20 11:56:00 +0100206 ALOGV("setForceUse() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700207 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700208 mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700209 return NO_ERROR;
210}
211
Dima Zavinfce7a472011-04-19 22:30:36 -0700212audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700213{
Dima Zavinfce7a472011-04-19 22:30:36 -0700214 if (mpAudioPolicy == NULL) {
215 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700216 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700217 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
218 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700219 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700220 return mpAudioPolicy->get_force_use(mpAudioPolicy, usage);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700221}
222
Dima Zavinfce7a472011-04-19 22:30:36 -0700223audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700224 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800225 audio_format_t format,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700226 uint32_t channels,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700227 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700228{
Eric Laurent6d80b682014-10-30 14:46:18 -0700229 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
230 return 0;
231 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700232 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700233 return 0;
234 }
Steve Block3856b092011-10-20 11:56:00 +0100235 ALOGV("getOutput() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700236 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700237 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, format, channels, flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700238}
239
Eric Laurentde070132010-07-13 04:45:46 -0700240status_t AudioPolicyService::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700241 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700242 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700243{
Eric Laurent6d80b682014-10-30 14:46:18 -0700244 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
245 return BAD_VALUE;
246 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700247 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700248 return NO_INIT;
249 }
Steve Block3856b092011-10-20 11:56:00 +0100250 ALOGV("startOutput() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700251 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700252 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700253}
254
Eric Laurentde070132010-07-13 04:45:46 -0700255status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700256 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700257 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700258{
Eric Laurent6d80b682014-10-30 14:46:18 -0700259 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
260 return BAD_VALUE;
261 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700262 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700263 return NO_INIT;
264 }
Steve Block3856b092011-10-20 11:56:00 +0100265 ALOGV("stopOutput() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700266 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700267 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700268}
269
270void AudioPolicyService::releaseOutput(audio_io_handle_t output)
271{
Dima Zavinfce7a472011-04-19 22:30:36 -0700272 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700273 return;
274 }
Steve Block3856b092011-10-20 11:56:00 +0100275 ALOGV("releaseOutput() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700276 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700277 mpAudioPolicy->release_output(mpAudioPolicy, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700278}
279
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800280audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700281 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800282 audio_format_t format,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700283 uint32_t channels,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700284 audio_in_acoustics_t acoustics,
285 int audioSession)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700286{
Dima Zavinfce7a472011-04-19 22:30:36 -0700287 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700288 return 0;
289 }
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800290 // already checked by client, but double-check in case the client wrapper is bypassed
291 if (uint32_t(inputSource) >= AUDIO_SOURCE_CNT) {
292 return 0;
293 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700294 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700295 audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
296 format, channels, acoustics);
297
298 if (input == 0) {
299 return input;
300 }
301 // create audio pre processors according to input source
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800302 ssize_t index = mInputSources.indexOfKey(inputSource);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700303 if (index < 0) {
304 return input;
305 }
306 ssize_t idx = mInputs.indexOfKey(input);
307 InputDesc *inputDesc;
308 if (idx < 0) {
Glenn Kasten81872a22012-03-07 16:49:22 -0800309 inputDesc = new InputDesc(audioSession);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700310 mInputs.add(input, inputDesc);
311 } else {
312 inputDesc = mInputs.valueAt(idx);
313 }
314
315 Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects;
316 for (size_t i = 0; i < effects.size(); i++) {
317 EffectDesc *effect = effects[i];
318 sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input);
319 status_t status = fx->initCheck();
320 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000321 ALOGW("Failed to create Fx %s on input %d", effect->mName, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700322 // fx goes out of scope and strong ref on AudioEffect is released
323 continue;
324 }
325 for (size_t j = 0; j < effect->mParams.size(); j++) {
326 fx->setParameter(effect->mParams[j]);
327 }
328 inputDesc->mEffects.add(fx);
329 }
330 setPreProcessorEnabled(inputDesc, true);
331 return input;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700332}
333
334status_t AudioPolicyService::startInput(audio_io_handle_t input)
335{
Dima Zavinfce7a472011-04-19 22:30:36 -0700336 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700337 return NO_INIT;
338 }
339 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700340
Dima Zavinfce7a472011-04-19 22:30:36 -0700341 return mpAudioPolicy->start_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700342}
343
344status_t AudioPolicyService::stopInput(audio_io_handle_t input)
345{
Dima Zavinfce7a472011-04-19 22:30:36 -0700346 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700347 return NO_INIT;
348 }
349 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700350
Dima Zavinfce7a472011-04-19 22:30:36 -0700351 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700352}
353
354void AudioPolicyService::releaseInput(audio_io_handle_t input)
355{
Dima Zavinfce7a472011-04-19 22:30:36 -0700356 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700357 return;
358 }
359 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700360 mpAudioPolicy->release_input(mpAudioPolicy, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700361
362 ssize_t index = mInputs.indexOfKey(input);
363 if (index < 0) {
364 return;
365 }
366 InputDesc *inputDesc = mInputs.valueAt(index);
367 setPreProcessorEnabled(inputDesc, false);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700368 delete inputDesc;
369 mInputs.removeItemsAt(index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370}
371
Dima Zavinfce7a472011-04-19 22:30:36 -0700372status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373 int indexMin,
374 int indexMax)
375{
Dima Zavinfce7a472011-04-19 22:30:36 -0700376 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700377 return NO_INIT;
378 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800379 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700380 return PERMISSION_DENIED;
381 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800382 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700383 return BAD_VALUE;
384 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700385 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700386 return NO_ERROR;
387}
388
Eric Laurent83844cc2011-11-18 16:43:31 -0800389status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
390 int index,
391 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392{
Dima Zavinfce7a472011-04-19 22:30:36 -0700393 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700394 return NO_INIT;
395 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800396 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700397 return PERMISSION_DENIED;
398 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800399 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400 return BAD_VALUE;
401 }
402
Eric Laurent83844cc2011-11-18 16:43:31 -0800403 if (mpAudioPolicy->set_stream_volume_index_for_device) {
404 return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
405 stream,
406 index,
407 device);
408 } else {
409 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
410 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700411}
412
Eric Laurent83844cc2011-11-18 16:43:31 -0800413status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
414 int *index,
415 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700416{
Dima Zavinfce7a472011-04-19 22:30:36 -0700417 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700418 return NO_INIT;
419 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800420 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700421 return BAD_VALUE;
422 }
Eric Laurent83844cc2011-11-18 16:43:31 -0800423 if (mpAudioPolicy->get_stream_volume_index_for_device) {
424 return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
425 stream,
426 index,
427 device);
428 } else {
429 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
430 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700431}
432
Dima Zavinfce7a472011-04-19 22:30:36 -0700433uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700434{
Eric Laurent6d80b682014-10-30 14:46:18 -0700435 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
436 return 0;
437 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700438 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700439 return 0;
440 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700441 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurentde070132010-07-13 04:45:46 -0700442}
443
Eric Laurent63742522012-03-08 13:42:42 -0800444//audio policy: use audio_device_t appropriately
445
446audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800447{
Eric Laurent6d80b682014-10-30 14:46:18 -0700448 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
449 return (audio_devices_t)0;
450 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700451 if (mpAudioPolicy == NULL) {
Eric Laurent63742522012-03-08 13:42:42 -0800452 return (audio_devices_t)0;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800453 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700454 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800455}
456
Eric Laurentde070132010-07-13 04:45:46 -0700457audio_io_handle_t AudioPolicyService::getOutputForEffect(effect_descriptor_t *desc)
458{
Dima Zavinfce7a472011-04-19 22:30:36 -0700459 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700460 return NO_INIT;
461 }
462 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700463 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700464}
465
466status_t AudioPolicyService::registerEffect(effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700467 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700468 uint32_t strategy,
469 int session,
470 int id)
471{
Dima Zavinfce7a472011-04-19 22:30:36 -0700472 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700473 return NO_INIT;
474 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700475 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700476}
477
478status_t AudioPolicyService::unregisterEffect(int id)
479{
Dima Zavinfce7a472011-04-19 22:30:36 -0700480 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700481 return NO_INIT;
482 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700483 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700484}
485
Eric Laurentdb7c0792011-08-10 10:37:50 -0700486status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
487{
488 if (mpAudioPolicy == NULL) {
489 return NO_INIT;
490 }
491 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
492}
493
Glenn Kastenfff6d712012-01-12 16:38:12 -0800494bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800495{
Eric Laurent6d80b682014-10-30 14:46:18 -0700496 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
497 return false;
498 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700499 if (mpAudioPolicy == NULL) {
Eric Laurent6d80b682014-10-30 14:46:18 -0700500 return false;
Eric Laurenteda6c362011-02-02 09:33:30 -0800501 }
502 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700503 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800504}
505
Eric Laurent57dae992011-07-24 13:36:09 -0700506status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
507 effect_descriptor_t *descriptors,
508 uint32_t *count)
509{
510
511 if (mpAudioPolicy == NULL) {
512 *count = 0;
513 return NO_INIT;
514 }
515 Mutex::Autolock _l(mLock);
516 status_t status = NO_ERROR;
517
518 size_t index;
519 for (index = 0; index < mInputs.size(); index++) {
520 if (mInputs.valueAt(index)->mSessionId == audioSession) {
521 break;
522 }
523 }
524 if (index == mInputs.size()) {
525 *count = 0;
526 return BAD_VALUE;
527 }
528 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
529
530 for (size_t i = 0; i < effects.size(); i++) {
531 effect_descriptor_t desc = effects[i]->descriptor();
532 if (i < *count) {
533 memcpy(descriptors + i, &desc, sizeof(effect_descriptor_t));
534 }
535 }
536 if (effects.size() > *count) {
537 status = NO_MEMORY;
538 }
539 *count = effects.size();
540 return status;
541}
542
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten23d82a92012-02-03 11:10:00 -0800544 ALOGW("binderDied() %p, tid %d, calling pid %d", who.unsafe_get(), gettid(),
Eric Laurentde070132010-07-13 04:45:46 -0700545 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700546}
547
548static bool tryLock(Mutex& mutex)
549{
550 bool locked = false;
551 for (int i = 0; i < kDumpLockRetries; ++i) {
552 if (mutex.tryLock() == NO_ERROR) {
553 locked = true;
554 break;
555 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800556 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700557 }
558 return locked;
559}
560
561status_t AudioPolicyService::dumpInternals(int fd)
562{
563 const size_t SIZE = 256;
564 char buffer[SIZE];
565 String8 result;
566
Dima Zavinfce7a472011-04-19 22:30:36 -0700567 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700568 result.append(buffer);
569 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
570 result.append(buffer);
571 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
572 result.append(buffer);
573
574 write(fd, result.string(), result.size());
575 return NO_ERROR;
576}
577
578status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
579{
Glenn Kasten44deb052012-02-05 18:09:08 -0800580 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700581 dumpPermissionDenial(fd);
582 } else {
583 bool locked = tryLock(mLock);
584 if (!locked) {
585 String8 result(kDeadlockedString);
586 write(fd, result.string(), result.size());
587 }
588
589 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800590 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700591 mAudioCommandThread->dump(fd);
592 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800593 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700594 mTonePlaybackThread->dump(fd);
595 }
596
Dima Zavinfce7a472011-04-19 22:30:36 -0700597 if (mpAudioPolicy) {
598 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700599 }
600
601 if (locked) mLock.unlock();
602 }
603 return NO_ERROR;
604}
605
606status_t AudioPolicyService::dumpPermissionDenial(int fd)
607{
608 const size_t SIZE = 256;
609 char buffer[SIZE];
610 String8 result;
611 snprintf(buffer, SIZE, "Permission Denial: "
612 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
613 IPCThreadState::self()->getCallingPid(),
614 IPCThreadState::self()->getCallingUid());
615 result.append(buffer);
616 write(fd, result.string(), result.size());
617 return NO_ERROR;
618}
619
Glenn Kasten81872a22012-03-07 16:49:22 -0800620void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700621{
Glenn Kasten81872a22012-03-07 16:49:22 -0800622 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700623 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800624 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700625 }
626}
627
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628status_t AudioPolicyService::onTransact(
629 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
630{
631 return BnAudioPolicyService::onTransact(code, data, reply, flags);
632}
633
634
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635// ----------- AudioPolicyService::AudioCommandThread implementation ----------
636
637AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name)
638 : Thread(false), mName(name)
639{
640 mpToneGenerator = NULL;
641}
642
643
644AudioPolicyService::AudioCommandThread::~AudioCommandThread()
645{
646 if (mName != "" && !mAudioCommands.isEmpty()) {
647 release_wake_lock(mName.string());
648 }
649 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800650 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700651}
652
653void AudioPolicyService::AudioCommandThread::onFirstRef()
654{
655 if (mName != "") {
656 run(mName.string(), ANDROID_PRIORITY_AUDIO);
657 } else {
Glenn Kasten480b4682012-02-28 12:30:08 -0800658 run("AudioCommand", ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700659 }
660}
661
662bool AudioPolicyService::AudioCommandThread::threadLoop()
663{
664 nsecs_t waitTime = INT64_MAX;
665
666 mLock.lock();
667 while (!exitPending())
668 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700669 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700670 nsecs_t curTime = systemTime();
671 // commands are sorted by increasing time stamp: execute them from index 0 and up
672 if (mAudioCommands[0]->mTime <= curTime) {
673 AudioCommand *command = mAudioCommands[0];
674 mAudioCommands.removeAt(0);
675 mLastCommand = *command;
676
677 switch (command->mCommand) {
678 case START_TONE: {
679 mLock.unlock();
680 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100681 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700682 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800683 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700684 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
685 mpToneGenerator->startTone(data->mType);
686 delete data;
687 mLock.lock();
688 }break;
689 case STOP_TONE: {
690 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100691 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700692 if (mpToneGenerator != NULL) {
693 mpToneGenerator->stopTone();
694 delete mpToneGenerator;
695 mpToneGenerator = NULL;
696 }
697 mLock.lock();
698 }break;
699 case SET_VOLUME: {
700 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100701 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700702 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
703 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
704 data->mVolume,
705 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700706 if (command->mWaitStatus) {
707 command->mCond.signal();
708 mWaitWorkCV.wait(mLock);
709 }
710 delete data;
711 }break;
712 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700713 ParametersData *data = (ParametersData *)command->mParam;
714 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
715 data->mKeyValuePairs.string(), data->mIO);
716 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
717 if (command->mWaitStatus) {
718 command->mCond.signal();
719 mWaitWorkCV.wait(mLock);
720 }
721 delete data;
722 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700723 case SET_VOICE_VOLUME: {
724 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100725 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700726 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700727 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
728 if (command->mWaitStatus) {
729 command->mCond.signal();
730 mWaitWorkCV.wait(mLock);
731 }
732 delete data;
733 }break;
734 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000735 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700736 }
737 delete command;
738 waitTime = INT64_MAX;
739 } else {
740 waitTime = mAudioCommands[0]->mTime - curTime;
741 break;
742 }
743 }
744 // release delayed commands wake lock
745 if (mName != "" && mAudioCommands.isEmpty()) {
746 release_wake_lock(mName.string());
747 }
Steve Block3856b092011-10-20 11:56:00 +0100748 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700749 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100750 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700751 }
752 mLock.unlock();
753 return false;
754}
755
756status_t AudioPolicyService::AudioCommandThread::dump(int fd)
757{
758 const size_t SIZE = 256;
759 char buffer[SIZE];
760 String8 result;
761
762 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
763 result.append(buffer);
764 write(fd, result.string(), result.size());
765
766 bool locked = tryLock(mLock);
767 if (!locked) {
768 String8 result2(kCmdDeadlockedString);
769 write(fd, result2.string(), result2.size());
770 }
771
772 snprintf(buffer, SIZE, "- Commands:\n");
773 result = String8(buffer);
774 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800775 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700776 mAudioCommands[i]->dump(buffer, SIZE);
777 result.append(buffer);
778 }
779 result.append(" Last Command\n");
780 mLastCommand.dump(buffer, SIZE);
781 result.append(buffer);
782
783 write(fd, result.string(), result.size());
784
785 if (locked) mLock.unlock();
786
787 return NO_ERROR;
788}
789
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800790void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
791 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700792{
793 AudioCommand *command = new AudioCommand();
794 command->mCommand = START_TONE;
795 ToneData *data = new ToneData();
796 data->mType = type;
797 data->mStream = stream;
798 command->mParam = (void *)data;
799 command->mWaitStatus = false;
800 Mutex::Autolock _l(mLock);
801 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100802 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700803 mWaitWorkCV.signal();
804}
805
806void AudioPolicyService::AudioCommandThread::stopToneCommand()
807{
808 AudioCommand *command = new AudioCommand();
809 command->mCommand = STOP_TONE;
810 command->mParam = NULL;
811 command->mWaitStatus = false;
812 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;
832 if (delayMs == 0) {
833 command->mWaitStatus = true;
834 } else {
835 command->mWaitStatus = false;
836 }
837 Mutex::Autolock _l(mLock);
838 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100839 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700840 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700841 mWaitWorkCV.signal();
842 if (command->mWaitStatus) {
843 command->mCond.wait(mLock);
844 status = command->mStatus;
845 mWaitWorkCV.signal();
846 }
847 return status;
848}
849
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800850status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700851 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700852 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700853{
854 status_t status = NO_ERROR;
855
856 AudioCommand *command = new AudioCommand();
857 command->mCommand = SET_PARAMETERS;
858 ParametersData *data = new ParametersData();
859 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700860 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700861 command->mParam = data;
862 if (delayMs == 0) {
863 command->mWaitStatus = true;
864 } else {
865 command->mWaitStatus = false;
866 }
867 Mutex::Autolock _l(mLock);
868 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100869 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700870 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700871 mWaitWorkCV.signal();
872 if (command->mWaitStatus) {
873 command->mCond.wait(mLock);
874 status = command->mStatus;
875 mWaitWorkCV.signal();
876 }
877 return status;
878}
879
880status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
881{
882 status_t status = NO_ERROR;
883
884 AudioCommand *command = new AudioCommand();
885 command->mCommand = SET_VOICE_VOLUME;
886 VoiceVolumeData *data = new VoiceVolumeData();
887 data->mVolume = volume;
888 command->mParam = data;
889 if (delayMs == 0) {
890 command->mWaitStatus = true;
891 } else {
892 command->mWaitStatus = false;
893 }
894 Mutex::Autolock _l(mLock);
895 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100896 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700897 mWaitWorkCV.signal();
898 if (command->mWaitStatus) {
899 command->mCond.wait(mLock);
900 status = command->mStatus;
901 mWaitWorkCV.signal();
902 }
903 return status;
904}
905
906// insertCommand_l() must be called with mLock held
907void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
908{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800909 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700910 Vector <AudioCommand *> removedCommands;
911
912 command->mTime = systemTime() + milliseconds(delayMs);
913
914 // acquire wake lock to make sure delayed commands are processed
915 if (mName != "" && mAudioCommands.isEmpty()) {
916 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
917 }
918
919 // check same pending commands with later time stamps and eliminate them
920 for (i = mAudioCommands.size()-1; i >= 0; i--) {
921 AudioCommand *command2 = mAudioCommands[i];
922 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
923 if (command2->mTime <= command->mTime) break;
924 if (command2->mCommand != command->mCommand) continue;
925
926 switch (command->mCommand) {
927 case SET_PARAMETERS: {
928 ParametersData *data = (ParametersData *)command->mParam;
929 ParametersData *data2 = (ParametersData *)command2->mParam;
930 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100931 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700932 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700933 AudioParameter param = AudioParameter(data->mKeyValuePairs);
934 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
935 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700936 String8 key;
937 String8 value;
938 param.getAt(j, key, value);
939 for (size_t k = 0; k < param2.size(); k++) {
940 String8 key2;
941 String8 value2;
942 param2.getAt(k, key2, value2);
943 if (key2 == key) {
944 param2.remove(key2);
945 ALOGV("Filtering out parameter %s", key2.string());
946 break;
947 }
948 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700949 }
950 // if all keys have been filtered out, remove the command.
951 // otherwise, update the key value pairs
952 if (param2.size() == 0) {
953 removedCommands.add(command2);
954 } else {
955 data2->mKeyValuePairs = param2.toString();
956 }
957 } break;
958
959 case SET_VOLUME: {
960 VolumeData *data = (VolumeData *)command->mParam;
961 VolumeData *data2 = (VolumeData *)command2->mParam;
962 if (data->mIO != data2->mIO) break;
963 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100964 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700965 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700966 removedCommands.add(command2);
967 } break;
968 case START_TONE:
969 case STOP_TONE:
970 default:
971 break;
972 }
973 }
974
975 // remove filtered commands
976 for (size_t j = 0; j < removedCommands.size(); j++) {
977 // removed commands always have time stamps greater than current command
978 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
979 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +0100980 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700981 mAudioCommands.removeAt(k);
982 break;
983 }
984 }
985 }
986 removedCommands.clear();
987
988 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +0100989 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -0700990 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700991 mAudioCommands.insertAt(command, i + 1);
992}
993
994void AudioPolicyService::AudioCommandThread::exit()
995{
Steve Block3856b092011-10-20 11:56:00 +0100996 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700997 {
998 AutoMutex _l(mLock);
999 requestExit();
1000 mWaitWorkCV.signal();
1001 }
1002 requestExitAndWait();
1003}
1004
1005void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1006{
1007 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1008 mCommand,
1009 (int)ns2s(mTime),
1010 (int)ns2ms(mTime)%1000,
1011 mWaitStatus,
1012 mParam);
1013}
1014
Dima Zavinfce7a472011-04-19 22:30:36 -07001015/******* helpers for the service_ops callbacks defined below *********/
1016void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1017 const char *keyValuePairs,
1018 int delayMs)
1019{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001020 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001021 delayMs);
1022}
1023
1024int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1025 float volume,
1026 audio_io_handle_t output,
1027 int delayMs)
1028{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001029 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001030 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001031}
1032
1033int AudioPolicyService::startTone(audio_policy_tone_t tone,
1034 audio_stream_type_t stream)
1035{
1036 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
Steve Block29357bc2012-01-06 19:20:56 +00001037 ALOGE("startTone: illegal tone requested (%d)", tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001038 if (stream != AUDIO_STREAM_VOICE_CALL)
Steve Block29357bc2012-01-06 19:20:56 +00001039 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001040 tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001041 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1042 AUDIO_STREAM_VOICE_CALL);
1043 return 0;
1044}
1045
1046int AudioPolicyService::stopTone()
1047{
1048 mTonePlaybackThread->stopToneCommand();
1049 return 0;
1050}
1051
1052int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1053{
1054 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1055}
1056
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001057// ----------------------------------------------------------------------------
1058// Audio pre-processing configuration
1059// ----------------------------------------------------------------------------
1060
Glenn Kasten8dad0e32012-01-09 08:41:22 -08001061/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001062 MIC_SRC_TAG,
1063 VOICE_UL_SRC_TAG,
1064 VOICE_DL_SRC_TAG,
1065 VOICE_CALL_SRC_TAG,
1066 CAMCORDER_SRC_TAG,
1067 VOICE_REC_SRC_TAG,
1068 VOICE_COMM_SRC_TAG
1069};
1070
1071// returns the audio_source_t enum corresponding to the input source name or
1072// AUDIO_SOURCE_CNT is no match found
1073audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
1074{
1075 int i;
1076 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
1077 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001078 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001079 break;
1080 }
1081 }
1082 return (audio_source_t)i;
1083}
1084
1085size_t AudioPolicyService::growParamSize(char *param,
1086 size_t size,
1087 size_t *curSize,
1088 size_t *totSize)
1089{
1090 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
1091 size_t pos = ((*curSize - 1 ) / size + 1) * size;
1092
1093 if (pos + size > *totSize) {
1094 while (pos + size > *totSize) {
1095 *totSize += ((*totSize + 7) / 8) * 4;
1096 }
1097 param = (char *)realloc(param, *totSize);
1098 }
1099 *curSize = pos + size;
1100 return pos;
1101}
1102
1103size_t AudioPolicyService::readParamValue(cnode *node,
1104 char *param,
1105 size_t *curSize,
1106 size_t *totSize)
1107{
1108 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
1109 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
1110 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001111 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001112 return sizeof(short);
1113 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
1114 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
1115 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001116 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001117 return sizeof(int);
1118 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
1119 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
1120 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001121 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001122 return sizeof(float);
1123 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
1124 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
1125 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1126 *(bool *)((char *)param + pos) = false;
1127 } else {
1128 *(bool *)((char *)param + pos) = true;
1129 }
Steve Block3856b092011-10-20 11:56:00 +01001130 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001131 return sizeof(bool);
1132 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1133 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1134 if (*curSize + len + 1 > *totSize) {
1135 *totSize = *curSize + len + 1;
1136 param = (char *)realloc(param, *totSize);
1137 }
1138 strncpy(param + *curSize, node->value, len);
1139 *curSize += len;
1140 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +01001141 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001142 return len;
1143 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001144 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001145 return 0;
1146}
1147
1148effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1149{
1150 cnode *param;
1151 cnode *value;
1152 size_t curSize = sizeof(effect_param_t);
1153 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1154 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1155
1156 param = config_find(root, PARAM_TAG);
1157 value = config_find(root, VALUE_TAG);
1158 if (param == NULL && value == NULL) {
1159 // try to parse simple parameter form {int int}
1160 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -08001161 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001162 // Note: that a pair of random strings is read as 0 0
1163 int *ptr = (int *)fx_param->data;
1164 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +00001165 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001166 *ptr++ = atoi(param->name);
1167 *ptr = atoi(param->value);
1168 fx_param->psize = sizeof(int);
1169 fx_param->vsize = sizeof(int);
1170 return fx_param;
1171 }
1172 }
1173 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001174 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001175 goto error;
1176 }
1177
1178 fx_param->psize = 0;
1179 param = param->first_child;
1180 while (param) {
Steve Block3856b092011-10-20 11:56:00 +01001181 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001182 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1183 if (size == 0) {
1184 goto error;
1185 }
1186 fx_param->psize += size;
1187 param = param->next;
1188 }
1189
1190 // align start of value field on 32 bit boundary
1191 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1192
1193 fx_param->vsize = 0;
1194 value = value->first_child;
1195 while (value) {
Steve Block3856b092011-10-20 11:56:00 +01001196 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001197 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1198 if (size == 0) {
1199 goto error;
1200 }
1201 fx_param->vsize += size;
1202 value = value->next;
1203 }
1204
1205 return fx_param;
1206
1207error:
1208 delete fx_param;
1209 return NULL;
1210}
1211
1212void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1213{
1214 cnode *node = root->first_child;
1215 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001216 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001217 effect_param_t *param = loadEffectParameter(node);
1218 if (param == NULL) {
1219 node = node->next;
1220 continue;
1221 }
1222 params.add(param);
1223 node = node->next;
1224 }
1225}
1226
1227AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1228 cnode *root,
1229 const Vector <EffectDesc *>& effects)
1230{
1231 cnode *node = root->first_child;
1232 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001233 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001234 return NULL;
1235 }
1236 InputSourceDesc *source = new InputSourceDesc();
1237 while (node) {
1238 size_t i;
1239 for (i = 0; i < effects.size(); i++) {
1240 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001241 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001242 break;
1243 }
1244 }
1245 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +01001246 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001247 node = node->next;
1248 continue;
1249 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001250 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001251 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +01001252 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001253 source->mEffects.add(effect);
1254 node = node->next;
1255 }
1256 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001257 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001258 delete source;
1259 return NULL;
1260 }
1261 return source;
1262}
1263
1264status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1265{
1266 cnode *node = config_find(root, PREPROCESSING_TAG);
1267 if (node == NULL) {
1268 return -ENOENT;
1269 }
1270 node = node->first_child;
1271 while (node) {
1272 audio_source_t source = inputSourceNameToEnum(node->name);
1273 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001274 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001275 node = node->next;
1276 continue;
1277 }
Steve Block3856b092011-10-20 11:56:00 +01001278 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001279 InputSourceDesc *desc = loadInputSource(node, effects);
1280 if (desc == NULL) {
1281 node = node->next;
1282 continue;
1283 }
1284 mInputSources.add(source, desc);
1285 node = node->next;
1286 }
1287 return NO_ERROR;
1288}
1289
1290AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1291{
1292 cnode *node = config_find(root, UUID_TAG);
1293 if (node == NULL) {
1294 return NULL;
1295 }
1296 effect_uuid_t uuid;
1297 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001298 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001299 return NULL;
1300 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001301 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001302}
1303
1304status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1305{
1306 cnode *node = config_find(root, EFFECTS_TAG);
1307 if (node == NULL) {
1308 return -ENOENT;
1309 }
1310 node = node->first_child;
1311 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001312 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001313 EffectDesc *effect = loadEffect(node);
1314 if (effect == NULL) {
1315 node = node->next;
1316 continue;
1317 }
1318 effects.add(effect);
1319 node = node->next;
1320 }
1321 return NO_ERROR;
1322}
1323
1324status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1325{
1326 cnode *root;
1327 char *data;
1328
1329 data = (char *)load_file(path, NULL);
1330 if (data == NULL) {
1331 return -ENODEV;
1332 }
1333 root = config_node("", "");
1334 config_load(root, data);
1335
1336 Vector <EffectDesc *> effects;
1337 loadEffects(root, effects);
1338 loadInputSources(root, effects);
1339
1340 config_free(root);
1341 free(root);
1342 free(data);
1343
1344 return NO_ERROR;
1345}
1346
Dima Zavinfce7a472011-04-19 22:30:36 -07001347/* implementation of the interface to the policy manager */
1348extern "C" {
1349
Eric Laurenta4c5a552012-03-29 10:12:40 -07001350
1351static audio_module_handle_t aps_load_hw_module(void *service,
1352 const char *name)
Dima Zavinfce7a472011-04-19 22:30:36 -07001353{
1354 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001355 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001356 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001357 return 0;
1358 }
1359
Eric Laurenta4c5a552012-03-29 10:12:40 -07001360 return af->loadHwModule(name);
1361}
1362
1363// deprecated: replaced by aps_open_output_on_module()
1364static audio_io_handle_t aps_open_output(void *service,
1365 audio_devices_t *pDevices,
1366 uint32_t *pSamplingRate,
1367 audio_format_t *pFormat,
1368 audio_channel_mask_t *pChannelMask,
1369 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001370 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001371{
1372 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1373 if (af == 0) {
1374 ALOGW("%s: could not get AudioFlinger", __func__);
1375 return 0;
1376 }
1377
1378 return af->openOutput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask,
1379 pLatencyMs, flags);
1380}
1381
1382static audio_io_handle_t aps_open_output_on_module(void *service,
1383 audio_module_handle_t module,
1384 audio_devices_t *pDevices,
1385 uint32_t *pSamplingRate,
1386 audio_format_t *pFormat,
1387 audio_channel_mask_t *pChannelMask,
1388 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001389 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001390{
1391 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1392 if (af == 0) {
1393 ALOGW("%s: could not get AudioFlinger", __func__);
1394 return 0;
1395 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001396 return af->openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
Dima Zavinfce7a472011-04-19 22:30:36 -07001397 pLatencyMs, flags);
1398}
1399
1400static audio_io_handle_t aps_open_dup_output(void *service,
1401 audio_io_handle_t output1,
1402 audio_io_handle_t output2)
1403{
1404 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001405 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001406 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001407 return 0;
1408 }
1409 return af->openDuplicateOutput(output1, output2);
1410}
1411
1412static int aps_close_output(void *service, audio_io_handle_t output)
1413{
1414 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001415 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001416 return PERMISSION_DENIED;
1417
1418 return af->closeOutput(output);
1419}
1420
1421static int aps_suspend_output(void *service, audio_io_handle_t output)
1422{
1423 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001424 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001425 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001426 return PERMISSION_DENIED;
1427 }
1428
1429 return af->suspendOutput(output);
1430}
1431
1432static int aps_restore_output(void *service, audio_io_handle_t output)
1433{
1434 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001435 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001436 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001437 return PERMISSION_DENIED;
1438 }
1439
1440 return af->restoreOutput(output);
1441}
1442
Eric Laurenta4c5a552012-03-29 10:12:40 -07001443// deprecated: replaced by aps_open_input_on_module()
Dima Zavinfce7a472011-04-19 22:30:36 -07001444static audio_io_handle_t aps_open_input(void *service,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001445 audio_devices_t *pDevices,
1446 uint32_t *pSamplingRate,
1447 audio_format_t *pFormat,
1448 audio_channel_mask_t *pChannelMask,
1449 audio_in_acoustics_t acoustics)
Dima Zavinfce7a472011-04-19 22:30:36 -07001450{
1451 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001452 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001453 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001454 return 0;
1455 }
1456
Eric Laurenta4c5a552012-03-29 10:12:40 -07001457 return af->openInput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask);
1458}
1459
1460static audio_io_handle_t aps_open_input_on_module(void *service,
1461 audio_module_handle_t module,
1462 audio_devices_t *pDevices,
1463 uint32_t *pSamplingRate,
1464 audio_format_t *pFormat,
1465 audio_channel_mask_t *pChannelMask)
1466{
1467 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1468 if (af == 0) {
1469 ALOGW("%s: could not get AudioFlinger", __func__);
1470 return 0;
1471 }
1472
1473 return af->openInput(module, pDevices, pSamplingRate, pFormat, pChannelMask);
Dima Zavinfce7a472011-04-19 22:30:36 -07001474}
1475
1476static int aps_close_input(void *service, audio_io_handle_t input)
1477{
1478 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001479 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001480 return PERMISSION_DENIED;
1481
1482 return af->closeInput(input);
1483}
1484
1485static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1486 audio_io_handle_t output)
1487{
1488 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001489 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001490 return PERMISSION_DENIED;
1491
1492 return af->setStreamOutput(stream, output);
1493}
1494
1495static int aps_move_effects(void *service, int session,
1496 audio_io_handle_t src_output,
1497 audio_io_handle_t dst_output)
1498{
1499 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001500 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001501 return PERMISSION_DENIED;
1502
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001503 return af->moveEffects(session, src_output, dst_output);
Dima Zavinfce7a472011-04-19 22:30:36 -07001504}
1505
1506static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1507 const char *keys)
1508{
1509 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1510 return strdup(result.string());
1511}
1512
1513static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1514 const char *kv_pairs, int delay_ms)
1515{
1516 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1517
1518 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1519}
1520
1521static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1522 float volume, audio_io_handle_t output,
1523 int delay_ms)
1524{
1525 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1526
1527 return audioPolicyService->setStreamVolume(stream, volume, output,
1528 delay_ms);
1529}
1530
1531static int aps_start_tone(void *service, audio_policy_tone_t tone,
1532 audio_stream_type_t stream)
1533{
1534 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1535
1536 return audioPolicyService->startTone(tone, stream);
1537}
1538
1539static int aps_stop_tone(void *service)
1540{
1541 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1542
1543 return audioPolicyService->stopTone();
1544}
1545
1546static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1547{
1548 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1549
1550 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1551}
1552
1553}; // extern "C"
1554
1555namespace {
1556 struct audio_policy_service_ops aps_ops = {
1557 open_output : aps_open_output,
1558 open_duplicate_output : aps_open_dup_output,
1559 close_output : aps_close_output,
1560 suspend_output : aps_suspend_output,
1561 restore_output : aps_restore_output,
1562 open_input : aps_open_input,
1563 close_input : aps_close_input,
1564 set_stream_volume : aps_set_stream_volume,
1565 set_stream_output : aps_set_stream_output,
1566 set_parameters : aps_set_parameters,
1567 get_parameters : aps_get_parameters,
1568 start_tone : aps_start_tone,
1569 stop_tone : aps_stop_tone,
1570 set_voice_volume : aps_set_voice_volume,
1571 move_effects : aps_move_effects,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001572 load_hw_module : aps_load_hw_module,
1573 open_output_on_module : aps_open_output_on_module,
1574 open_input_on_module : aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001575 };
1576}; // namespace <unnamed>
1577
Mathias Agopian65ab4712010-07-14 17:59:35 -07001578}; // namespace android