blob: f07fe8e1ca9aafc109cbf136138bbe8469f0ad52 [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,
Glenn Kasten254af182012-07-03 14:59:05 -0700226 audio_channel_mask_t channelMask,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700227 audio_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700228{
Dima Zavinfce7a472011-04-19 22:30:36 -0700229 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700230 return 0;
231 }
Steve Block3856b092011-10-20 11:56:00 +0100232 ALOGV("getOutput() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700233 Mutex::Autolock _l(mLock);
Glenn Kasten254af182012-07-03 14:59:05 -0700234 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, format, channelMask, flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700235}
236
Eric Laurentde070132010-07-13 04:45:46 -0700237status_t AudioPolicyService::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700238 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700239 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700240{
Dima Zavinfce7a472011-04-19 22:30:36 -0700241 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242 return NO_INIT;
243 }
Steve Block3856b092011-10-20 11:56:00 +0100244 ALOGV("startOutput() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700245 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700246 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700247}
248
Eric Laurentde070132010-07-13 04:45:46 -0700249status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700250 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700251 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700252{
Dima Zavinfce7a472011-04-19 22:30:36 -0700253 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700254 return NO_INIT;
255 }
Steve Block3856b092011-10-20 11:56:00 +0100256 ALOGV("stopOutput() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700257 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700258 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700259}
260
261void AudioPolicyService::releaseOutput(audio_io_handle_t output)
262{
Dima Zavinfce7a472011-04-19 22:30:36 -0700263 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700264 return;
265 }
Steve Block3856b092011-10-20 11:56:00 +0100266 ALOGV("releaseOutput() tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700267 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700268 mpAudioPolicy->release_output(mpAudioPolicy, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700269}
270
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800271audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700272 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800273 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700274 audio_channel_mask_t channelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700275 int audioSession)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700276{
Dima Zavinfce7a472011-04-19 22:30:36 -0700277 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700278 return 0;
279 }
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800280 // already checked by client, but double-check in case the client wrapper is bypassed
281 if (uint32_t(inputSource) >= AUDIO_SOURCE_CNT) {
282 return 0;
283 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700284 Mutex::Autolock _l(mLock);
Glenn Kasten20010052012-06-22 13:43:51 -0700285 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700286 audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
Glenn Kasten254af182012-07-03 14:59:05 -0700287 format, channelMask, (audio_in_acoustics_t) 0);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700288
289 if (input == 0) {
290 return input;
291 }
292 // create audio pre processors according to input source
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800293 ssize_t index = mInputSources.indexOfKey(inputSource);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700294 if (index < 0) {
295 return input;
296 }
297 ssize_t idx = mInputs.indexOfKey(input);
298 InputDesc *inputDesc;
299 if (idx < 0) {
Glenn Kasten81872a22012-03-07 16:49:22 -0800300 inputDesc = new InputDesc(audioSession);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700301 mInputs.add(input, inputDesc);
302 } else {
303 inputDesc = mInputs.valueAt(idx);
304 }
305
306 Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects;
307 for (size_t i = 0; i < effects.size(); i++) {
308 EffectDesc *effect = effects[i];
309 sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input);
310 status_t status = fx->initCheck();
311 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000312 ALOGW("Failed to create Fx %s on input %d", effect->mName, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700313 // fx goes out of scope and strong ref on AudioEffect is released
314 continue;
315 }
316 for (size_t j = 0; j < effect->mParams.size(); j++) {
317 fx->setParameter(effect->mParams[j]);
318 }
319 inputDesc->mEffects.add(fx);
320 }
321 setPreProcessorEnabled(inputDesc, true);
322 return input;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323}
324
325status_t AudioPolicyService::startInput(audio_io_handle_t input)
326{
Dima Zavinfce7a472011-04-19 22:30:36 -0700327 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700328 return NO_INIT;
329 }
330 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700331
Dima Zavinfce7a472011-04-19 22:30:36 -0700332 return mpAudioPolicy->start_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700333}
334
335status_t AudioPolicyService::stopInput(audio_io_handle_t input)
336{
Dima Zavinfce7a472011-04-19 22:30:36 -0700337 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338 return NO_INIT;
339 }
340 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700341
Dima Zavinfce7a472011-04-19 22:30:36 -0700342 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700343}
344
345void AudioPolicyService::releaseInput(audio_io_handle_t input)
346{
Dima Zavinfce7a472011-04-19 22:30:36 -0700347 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700348 return;
349 }
350 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700351 mpAudioPolicy->release_input(mpAudioPolicy, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700352
353 ssize_t index = mInputs.indexOfKey(input);
354 if (index < 0) {
355 return;
356 }
357 InputDesc *inputDesc = mInputs.valueAt(index);
358 setPreProcessorEnabled(inputDesc, false);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700359 delete inputDesc;
360 mInputs.removeItemsAt(index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700361}
362
Dima Zavinfce7a472011-04-19 22:30:36 -0700363status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700364 int indexMin,
365 int indexMax)
366{
Dima Zavinfce7a472011-04-19 22:30:36 -0700367 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700368 return NO_INIT;
369 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800370 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700371 return PERMISSION_DENIED;
372 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800373 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700374 return BAD_VALUE;
375 }
Eric Laurent5f121362012-06-15 14:45:03 -0700376 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700377 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378 return NO_ERROR;
379}
380
Eric Laurent83844cc2011-11-18 16:43:31 -0800381status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
382 int index,
383 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384{
Dima Zavinfce7a472011-04-19 22:30:36 -0700385 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700386 return NO_INIT;
387 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800388 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700389 return PERMISSION_DENIED;
390 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800391 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392 return BAD_VALUE;
393 }
Eric Laurent5f121362012-06-15 14:45:03 -0700394 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800395 if (mpAudioPolicy->set_stream_volume_index_for_device) {
396 return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
397 stream,
398 index,
399 device);
400 } else {
401 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
402 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403}
404
Eric Laurent83844cc2011-11-18 16:43:31 -0800405status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
406 int *index,
407 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700408{
Dima Zavinfce7a472011-04-19 22:30:36 -0700409 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700410 return NO_INIT;
411 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800412 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700413 return BAD_VALUE;
414 }
Eric Laurent5f121362012-06-15 14:45:03 -0700415 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800416 if (mpAudioPolicy->get_stream_volume_index_for_device) {
417 return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
418 stream,
419 index,
420 device);
421 } else {
422 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
423 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700424}
425
Dima Zavinfce7a472011-04-19 22:30:36 -0700426uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700427{
Dima Zavinfce7a472011-04-19 22:30:36 -0700428 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700429 return 0;
430 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700431 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurentde070132010-07-13 04:45:46 -0700432}
433
Eric Laurent63742522012-03-08 13:42:42 -0800434//audio policy: use audio_device_t appropriately
435
436audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800437{
Dima Zavinfce7a472011-04-19 22:30:36 -0700438 if (mpAudioPolicy == NULL) {
Eric Laurent63742522012-03-08 13:42:42 -0800439 return (audio_devices_t)0;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800440 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700441 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800442}
443
Eric Laurentde070132010-07-13 04:45:46 -0700444audio_io_handle_t AudioPolicyService::getOutputForEffect(effect_descriptor_t *desc)
445{
Dima Zavinfce7a472011-04-19 22:30:36 -0700446 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700447 return NO_INIT;
448 }
449 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700450 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700451}
452
453status_t AudioPolicyService::registerEffect(effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700454 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700455 uint32_t strategy,
456 int session,
457 int id)
458{
Dima Zavinfce7a472011-04-19 22:30:36 -0700459 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700460 return NO_INIT;
461 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700462 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700463}
464
465status_t AudioPolicyService::unregisterEffect(int id)
466{
Dima Zavinfce7a472011-04-19 22:30:36 -0700467 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700468 return NO_INIT;
469 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700470 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700471}
472
Eric Laurentdb7c0792011-08-10 10:37:50 -0700473status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
474{
475 if (mpAudioPolicy == NULL) {
476 return NO_INIT;
477 }
478 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
479}
480
Glenn Kastenfff6d712012-01-12 16:38:12 -0800481bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800482{
Dima Zavinfce7a472011-04-19 22:30:36 -0700483 if (mpAudioPolicy == NULL) {
Eric Laurenteda6c362011-02-02 09:33:30 -0800484 return 0;
485 }
486 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700487 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800488}
489
Eric Laurent57dae992011-07-24 13:36:09 -0700490status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
491 effect_descriptor_t *descriptors,
492 uint32_t *count)
493{
494
495 if (mpAudioPolicy == NULL) {
496 *count = 0;
497 return NO_INIT;
498 }
499 Mutex::Autolock _l(mLock);
500 status_t status = NO_ERROR;
501
502 size_t index;
503 for (index = 0; index < mInputs.size(); index++) {
504 if (mInputs.valueAt(index)->mSessionId == audioSession) {
505 break;
506 }
507 }
508 if (index == mInputs.size()) {
509 *count = 0;
510 return BAD_VALUE;
511 }
512 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
513
514 for (size_t i = 0; i < effects.size(); i++) {
515 effect_descriptor_t desc = effects[i]->descriptor();
516 if (i < *count) {
517 memcpy(descriptors + i, &desc, sizeof(effect_descriptor_t));
518 }
519 }
520 if (effects.size() > *count) {
521 status = NO_MEMORY;
522 }
523 *count = effects.size();
524 return status;
525}
526
Mathias Agopian65ab4712010-07-14 17:59:35 -0700527void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten23d82a92012-02-03 11:10:00 -0800528 ALOGW("binderDied() %p, tid %d, calling pid %d", who.unsafe_get(), gettid(),
Eric Laurentde070132010-07-13 04:45:46 -0700529 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700530}
531
532static bool tryLock(Mutex& mutex)
533{
534 bool locked = false;
535 for (int i = 0; i < kDumpLockRetries; ++i) {
536 if (mutex.tryLock() == NO_ERROR) {
537 locked = true;
538 break;
539 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800540 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700541 }
542 return locked;
543}
544
545status_t AudioPolicyService::dumpInternals(int fd)
546{
547 const size_t SIZE = 256;
548 char buffer[SIZE];
549 String8 result;
550
Dima Zavinfce7a472011-04-19 22:30:36 -0700551 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700552 result.append(buffer);
553 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
554 result.append(buffer);
555 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
556 result.append(buffer);
557
558 write(fd, result.string(), result.size());
559 return NO_ERROR;
560}
561
562status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
563{
Glenn Kasten44deb052012-02-05 18:09:08 -0800564 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700565 dumpPermissionDenial(fd);
566 } else {
567 bool locked = tryLock(mLock);
568 if (!locked) {
569 String8 result(kDeadlockedString);
570 write(fd, result.string(), result.size());
571 }
572
573 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800574 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700575 mAudioCommandThread->dump(fd);
576 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800577 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700578 mTonePlaybackThread->dump(fd);
579 }
580
Dima Zavinfce7a472011-04-19 22:30:36 -0700581 if (mpAudioPolicy) {
582 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700583 }
584
585 if (locked) mLock.unlock();
586 }
587 return NO_ERROR;
588}
589
590status_t AudioPolicyService::dumpPermissionDenial(int fd)
591{
592 const size_t SIZE = 256;
593 char buffer[SIZE];
594 String8 result;
595 snprintf(buffer, SIZE, "Permission Denial: "
596 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
597 IPCThreadState::self()->getCallingPid(),
598 IPCThreadState::self()->getCallingUid());
599 result.append(buffer);
600 write(fd, result.string(), result.size());
601 return NO_ERROR;
602}
603
Glenn Kasten81872a22012-03-07 16:49:22 -0800604void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700605{
Glenn Kasten81872a22012-03-07 16:49:22 -0800606 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700607 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800608 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700609 }
610}
611
Mathias Agopian65ab4712010-07-14 17:59:35 -0700612status_t AudioPolicyService::onTransact(
613 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
614{
615 return BnAudioPolicyService::onTransact(code, data, reply, flags);
616}
617
618
Mathias Agopian65ab4712010-07-14 17:59:35 -0700619// ----------- AudioPolicyService::AudioCommandThread implementation ----------
620
621AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name)
622 : Thread(false), mName(name)
623{
624 mpToneGenerator = NULL;
625}
626
627
628AudioPolicyService::AudioCommandThread::~AudioCommandThread()
629{
630 if (mName != "" && !mAudioCommands.isEmpty()) {
631 release_wake_lock(mName.string());
632 }
633 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800634 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635}
636
637void AudioPolicyService::AudioCommandThread::onFirstRef()
638{
639 if (mName != "") {
640 run(mName.string(), ANDROID_PRIORITY_AUDIO);
641 } else {
Glenn Kasten480b4682012-02-28 12:30:08 -0800642 run("AudioCommand", ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700643 }
644}
645
646bool AudioPolicyService::AudioCommandThread::threadLoop()
647{
648 nsecs_t waitTime = INT64_MAX;
649
650 mLock.lock();
651 while (!exitPending())
652 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700653 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700654 nsecs_t curTime = systemTime();
655 // commands are sorted by increasing time stamp: execute them from index 0 and up
656 if (mAudioCommands[0]->mTime <= curTime) {
657 AudioCommand *command = mAudioCommands[0];
658 mAudioCommands.removeAt(0);
659 mLastCommand = *command;
660
661 switch (command->mCommand) {
662 case START_TONE: {
663 mLock.unlock();
664 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100665 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800667 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700668 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
669 mpToneGenerator->startTone(data->mType);
670 delete data;
671 mLock.lock();
672 }break;
673 case STOP_TONE: {
674 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100675 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700676 if (mpToneGenerator != NULL) {
677 mpToneGenerator->stopTone();
678 delete mpToneGenerator;
679 mpToneGenerator = NULL;
680 }
681 mLock.lock();
682 }break;
683 case SET_VOLUME: {
684 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100685 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700686 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
687 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
688 data->mVolume,
689 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690 if (command->mWaitStatus) {
691 command->mCond.signal();
692 mWaitWorkCV.wait(mLock);
693 }
694 delete data;
695 }break;
696 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700697 ParametersData *data = (ParametersData *)command->mParam;
698 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
699 data->mKeyValuePairs.string(), data->mIO);
700 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
701 if (command->mWaitStatus) {
702 command->mCond.signal();
703 mWaitWorkCV.wait(mLock);
704 }
705 delete data;
706 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700707 case SET_VOICE_VOLUME: {
708 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100709 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700710 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700711 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
712 if (command->mWaitStatus) {
713 command->mCond.signal();
714 mWaitWorkCV.wait(mLock);
715 }
716 delete data;
717 }break;
718 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000719 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720 }
721 delete command;
722 waitTime = INT64_MAX;
723 } else {
724 waitTime = mAudioCommands[0]->mTime - curTime;
725 break;
726 }
727 }
728 // release delayed commands wake lock
729 if (mName != "" && mAudioCommands.isEmpty()) {
730 release_wake_lock(mName.string());
731 }
Steve Block3856b092011-10-20 11:56:00 +0100732 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700733 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100734 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735 }
736 mLock.unlock();
737 return false;
738}
739
740status_t AudioPolicyService::AudioCommandThread::dump(int fd)
741{
742 const size_t SIZE = 256;
743 char buffer[SIZE];
744 String8 result;
745
746 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
747 result.append(buffer);
748 write(fd, result.string(), result.size());
749
750 bool locked = tryLock(mLock);
751 if (!locked) {
752 String8 result2(kCmdDeadlockedString);
753 write(fd, result2.string(), result2.size());
754 }
755
756 snprintf(buffer, SIZE, "- Commands:\n");
757 result = String8(buffer);
758 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800759 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700760 mAudioCommands[i]->dump(buffer, SIZE);
761 result.append(buffer);
762 }
763 result.append(" Last Command\n");
764 mLastCommand.dump(buffer, SIZE);
765 result.append(buffer);
766
767 write(fd, result.string(), result.size());
768
769 if (locked) mLock.unlock();
770
771 return NO_ERROR;
772}
773
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800774void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
775 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700776{
777 AudioCommand *command = new AudioCommand();
778 command->mCommand = START_TONE;
779 ToneData *data = new ToneData();
780 data->mType = type;
781 data->mStream = stream;
782 command->mParam = (void *)data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700783 Mutex::Autolock _l(mLock);
784 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100785 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700786 mWaitWorkCV.signal();
787}
788
789void AudioPolicyService::AudioCommandThread::stopToneCommand()
790{
791 AudioCommand *command = new AudioCommand();
792 command->mCommand = STOP_TONE;
793 command->mParam = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794 Mutex::Autolock _l(mLock);
795 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100796 ALOGV("AudioCommandThread() adding tone stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700797 mWaitWorkCV.signal();
798}
799
Glenn Kastenfff6d712012-01-12 16:38:12 -0800800status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700801 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800802 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700803 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700804{
805 status_t status = NO_ERROR;
806
807 AudioCommand *command = new AudioCommand();
808 command->mCommand = SET_VOLUME;
809 VolumeData *data = new VolumeData();
810 data->mStream = stream;
811 data->mVolume = volume;
812 data->mIO = output;
813 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814 Mutex::Autolock _l(mLock);
815 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100816 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700817 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818 mWaitWorkCV.signal();
819 if (command->mWaitStatus) {
820 command->mCond.wait(mLock);
821 status = command->mStatus;
822 mWaitWorkCV.signal();
823 }
824 return status;
825}
826
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800827status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700828 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700829 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700830{
831 status_t status = NO_ERROR;
832
833 AudioCommand *command = new AudioCommand();
834 command->mCommand = SET_PARAMETERS;
835 ParametersData *data = new ParametersData();
836 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700837 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700838 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700839 Mutex::Autolock _l(mLock);
840 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100841 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700842 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700843 mWaitWorkCV.signal();
844 if (command->mWaitStatus) {
845 command->mCond.wait(mLock);
846 status = command->mStatus;
847 mWaitWorkCV.signal();
848 }
849 return status;
850}
851
852status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
853{
854 status_t status = NO_ERROR;
855
856 AudioCommand *command = new AudioCommand();
857 command->mCommand = SET_VOICE_VOLUME;
858 VoiceVolumeData *data = new VoiceVolumeData();
859 data->mVolume = volume;
860 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700861 Mutex::Autolock _l(mLock);
862 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100863 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700864 mWaitWorkCV.signal();
865 if (command->mWaitStatus) {
866 command->mCond.wait(mLock);
867 status = command->mStatus;
868 mWaitWorkCV.signal();
869 }
870 return status;
871}
872
873// insertCommand_l() must be called with mLock held
874void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
875{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800876 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700877 Vector <AudioCommand *> removedCommands;
878
Eric Laurentcec4abb2012-07-03 12:23:02 -0700879 nsecs_t time = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700880 command->mTime = systemTime() + milliseconds(delayMs);
881
882 // acquire wake lock to make sure delayed commands are processed
883 if (mName != "" && mAudioCommands.isEmpty()) {
884 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
885 }
886
887 // check same pending commands with later time stamps and eliminate them
888 for (i = mAudioCommands.size()-1; i >= 0; i--) {
889 AudioCommand *command2 = mAudioCommands[i];
890 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
891 if (command2->mTime <= command->mTime) break;
892 if (command2->mCommand != command->mCommand) continue;
893
894 switch (command->mCommand) {
895 case SET_PARAMETERS: {
896 ParametersData *data = (ParametersData *)command->mParam;
897 ParametersData *data2 = (ParametersData *)command2->mParam;
898 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100899 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700900 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700901 AudioParameter param = AudioParameter(data->mKeyValuePairs);
902 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
903 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700904 String8 key;
905 String8 value;
906 param.getAt(j, key, value);
907 for (size_t k = 0; k < param2.size(); k++) {
908 String8 key2;
909 String8 value2;
910 param2.getAt(k, key2, value2);
911 if (key2 == key) {
912 param2.remove(key2);
913 ALOGV("Filtering out parameter %s", key2.string());
914 break;
915 }
916 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700917 }
918 // if all keys have been filtered out, remove the command.
919 // otherwise, update the key value pairs
920 if (param2.size() == 0) {
921 removedCommands.add(command2);
922 } else {
923 data2->mKeyValuePairs = param2.toString();
924 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700925 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700926 } break;
927
928 case SET_VOLUME: {
929 VolumeData *data = (VolumeData *)command->mParam;
930 VolumeData *data2 = (VolumeData *)command2->mParam;
931 if (data->mIO != data2->mIO) break;
932 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100933 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700934 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700935 removedCommands.add(command2);
Eric Laurentcec4abb2012-07-03 12:23:02 -0700936 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700937 } break;
938 case START_TONE:
939 case STOP_TONE:
940 default:
941 break;
942 }
943 }
944
945 // remove filtered commands
946 for (size_t j = 0; j < removedCommands.size(); j++) {
947 // removed commands always have time stamps greater than current command
948 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
949 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +0100950 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700951 mAudioCommands.removeAt(k);
952 break;
953 }
954 }
955 }
956 removedCommands.clear();
957
Eric Laurentcec4abb2012-07-03 12:23:02 -0700958 // wait for status only if delay is 0 and command time was not modified above
959 if (delayMs == 0 && time == 0) {
960 command->mWaitStatus = true;
961 } else {
962 command->mWaitStatus = false;
963 }
964 // update command time if modified above
965 if (time != 0) {
966 command->mTime = time;
967 }
968
Mathias Agopian65ab4712010-07-14 17:59:35 -0700969 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +0100970 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -0700971 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700972 mAudioCommands.insertAt(command, i + 1);
973}
974
975void AudioPolicyService::AudioCommandThread::exit()
976{
Steve Block3856b092011-10-20 11:56:00 +0100977 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700978 {
979 AutoMutex _l(mLock);
980 requestExit();
981 mWaitWorkCV.signal();
982 }
983 requestExitAndWait();
984}
985
986void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
987{
988 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
989 mCommand,
990 (int)ns2s(mTime),
991 (int)ns2ms(mTime)%1000,
992 mWaitStatus,
993 mParam);
994}
995
Dima Zavinfce7a472011-04-19 22:30:36 -0700996/******* helpers for the service_ops callbacks defined below *********/
997void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
998 const char *keyValuePairs,
999 int delayMs)
1000{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001001 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001002 delayMs);
1003}
1004
1005int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1006 float volume,
1007 audio_io_handle_t output,
1008 int delayMs)
1009{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001010 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001011 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001012}
1013
1014int AudioPolicyService::startTone(audio_policy_tone_t tone,
1015 audio_stream_type_t stream)
1016{
1017 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
Steve Block29357bc2012-01-06 19:20:56 +00001018 ALOGE("startTone: illegal tone requested (%d)", tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001019 if (stream != AUDIO_STREAM_VOICE_CALL)
Steve Block29357bc2012-01-06 19:20:56 +00001020 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001021 tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001022 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1023 AUDIO_STREAM_VOICE_CALL);
1024 return 0;
1025}
1026
1027int AudioPolicyService::stopTone()
1028{
1029 mTonePlaybackThread->stopToneCommand();
1030 return 0;
1031}
1032
1033int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1034{
1035 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1036}
1037
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001038// ----------------------------------------------------------------------------
1039// Audio pre-processing configuration
1040// ----------------------------------------------------------------------------
1041
Glenn Kasten8dad0e32012-01-09 08:41:22 -08001042/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001043 MIC_SRC_TAG,
1044 VOICE_UL_SRC_TAG,
1045 VOICE_DL_SRC_TAG,
1046 VOICE_CALL_SRC_TAG,
1047 CAMCORDER_SRC_TAG,
1048 VOICE_REC_SRC_TAG,
1049 VOICE_COMM_SRC_TAG
1050};
1051
1052// returns the audio_source_t enum corresponding to the input source name or
1053// AUDIO_SOURCE_CNT is no match found
1054audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
1055{
1056 int i;
1057 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
1058 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001059 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001060 break;
1061 }
1062 }
1063 return (audio_source_t)i;
1064}
1065
1066size_t AudioPolicyService::growParamSize(char *param,
1067 size_t size,
1068 size_t *curSize,
1069 size_t *totSize)
1070{
1071 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
1072 size_t pos = ((*curSize - 1 ) / size + 1) * size;
1073
1074 if (pos + size > *totSize) {
1075 while (pos + size > *totSize) {
1076 *totSize += ((*totSize + 7) / 8) * 4;
1077 }
1078 param = (char *)realloc(param, *totSize);
1079 }
1080 *curSize = pos + size;
1081 return pos;
1082}
1083
1084size_t AudioPolicyService::readParamValue(cnode *node,
1085 char *param,
1086 size_t *curSize,
1087 size_t *totSize)
1088{
1089 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
1090 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
1091 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001092 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001093 return sizeof(short);
1094 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
1095 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
1096 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001097 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001098 return sizeof(int);
1099 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
1100 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
1101 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001102 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001103 return sizeof(float);
1104 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
1105 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
1106 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1107 *(bool *)((char *)param + pos) = false;
1108 } else {
1109 *(bool *)((char *)param + pos) = true;
1110 }
Steve Block3856b092011-10-20 11:56:00 +01001111 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001112 return sizeof(bool);
1113 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1114 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1115 if (*curSize + len + 1 > *totSize) {
1116 *totSize = *curSize + len + 1;
1117 param = (char *)realloc(param, *totSize);
1118 }
1119 strncpy(param + *curSize, node->value, len);
1120 *curSize += len;
1121 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +01001122 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001123 return len;
1124 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001125 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001126 return 0;
1127}
1128
1129effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1130{
1131 cnode *param;
1132 cnode *value;
1133 size_t curSize = sizeof(effect_param_t);
1134 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1135 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1136
1137 param = config_find(root, PARAM_TAG);
1138 value = config_find(root, VALUE_TAG);
1139 if (param == NULL && value == NULL) {
1140 // try to parse simple parameter form {int int}
1141 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -08001142 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001143 // Note: that a pair of random strings is read as 0 0
1144 int *ptr = (int *)fx_param->data;
1145 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +00001146 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001147 *ptr++ = atoi(param->name);
1148 *ptr = atoi(param->value);
1149 fx_param->psize = sizeof(int);
1150 fx_param->vsize = sizeof(int);
1151 return fx_param;
1152 }
1153 }
1154 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001155 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001156 goto error;
1157 }
1158
1159 fx_param->psize = 0;
1160 param = param->first_child;
1161 while (param) {
Steve Block3856b092011-10-20 11:56:00 +01001162 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001163 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1164 if (size == 0) {
1165 goto error;
1166 }
1167 fx_param->psize += size;
1168 param = param->next;
1169 }
1170
1171 // align start of value field on 32 bit boundary
1172 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1173
1174 fx_param->vsize = 0;
1175 value = value->first_child;
1176 while (value) {
Steve Block3856b092011-10-20 11:56:00 +01001177 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001178 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1179 if (size == 0) {
1180 goto error;
1181 }
1182 fx_param->vsize += size;
1183 value = value->next;
1184 }
1185
1186 return fx_param;
1187
1188error:
1189 delete fx_param;
1190 return NULL;
1191}
1192
1193void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1194{
1195 cnode *node = root->first_child;
1196 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001197 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001198 effect_param_t *param = loadEffectParameter(node);
1199 if (param == NULL) {
1200 node = node->next;
1201 continue;
1202 }
1203 params.add(param);
1204 node = node->next;
1205 }
1206}
1207
1208AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1209 cnode *root,
1210 const Vector <EffectDesc *>& effects)
1211{
1212 cnode *node = root->first_child;
1213 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001214 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001215 return NULL;
1216 }
1217 InputSourceDesc *source = new InputSourceDesc();
1218 while (node) {
1219 size_t i;
1220 for (i = 0; i < effects.size(); i++) {
1221 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001222 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001223 break;
1224 }
1225 }
1226 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +01001227 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001228 node = node->next;
1229 continue;
1230 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001231 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001232 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +01001233 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001234 source->mEffects.add(effect);
1235 node = node->next;
1236 }
1237 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001238 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001239 delete source;
1240 return NULL;
1241 }
1242 return source;
1243}
1244
1245status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1246{
1247 cnode *node = config_find(root, PREPROCESSING_TAG);
1248 if (node == NULL) {
1249 return -ENOENT;
1250 }
1251 node = node->first_child;
1252 while (node) {
1253 audio_source_t source = inputSourceNameToEnum(node->name);
1254 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001255 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001256 node = node->next;
1257 continue;
1258 }
Steve Block3856b092011-10-20 11:56:00 +01001259 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001260 InputSourceDesc *desc = loadInputSource(node, effects);
1261 if (desc == NULL) {
1262 node = node->next;
1263 continue;
1264 }
1265 mInputSources.add(source, desc);
1266 node = node->next;
1267 }
1268 return NO_ERROR;
1269}
1270
1271AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1272{
1273 cnode *node = config_find(root, UUID_TAG);
1274 if (node == NULL) {
1275 return NULL;
1276 }
1277 effect_uuid_t uuid;
1278 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001279 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001280 return NULL;
1281 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001282 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001283}
1284
1285status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1286{
1287 cnode *node = config_find(root, EFFECTS_TAG);
1288 if (node == NULL) {
1289 return -ENOENT;
1290 }
1291 node = node->first_child;
1292 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001293 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001294 EffectDesc *effect = loadEffect(node);
1295 if (effect == NULL) {
1296 node = node->next;
1297 continue;
1298 }
1299 effects.add(effect);
1300 node = node->next;
1301 }
1302 return NO_ERROR;
1303}
1304
1305status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1306{
1307 cnode *root;
1308 char *data;
1309
1310 data = (char *)load_file(path, NULL);
1311 if (data == NULL) {
1312 return -ENODEV;
1313 }
1314 root = config_node("", "");
1315 config_load(root, data);
1316
1317 Vector <EffectDesc *> effects;
1318 loadEffects(root, effects);
1319 loadInputSources(root, effects);
1320
1321 config_free(root);
1322 free(root);
1323 free(data);
1324
1325 return NO_ERROR;
1326}
1327
Dima Zavinfce7a472011-04-19 22:30:36 -07001328/* implementation of the interface to the policy manager */
1329extern "C" {
1330
Eric Laurenta4c5a552012-03-29 10:12:40 -07001331
1332static audio_module_handle_t aps_load_hw_module(void *service,
1333 const char *name)
Dima Zavinfce7a472011-04-19 22:30:36 -07001334{
1335 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001336 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001337 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001338 return 0;
1339 }
1340
Eric Laurenta4c5a552012-03-29 10:12:40 -07001341 return af->loadHwModule(name);
1342}
1343
1344// deprecated: replaced by aps_open_output_on_module()
1345static audio_io_handle_t aps_open_output(void *service,
1346 audio_devices_t *pDevices,
1347 uint32_t *pSamplingRate,
1348 audio_format_t *pFormat,
1349 audio_channel_mask_t *pChannelMask,
1350 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001351 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001352{
1353 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1354 if (af == 0) {
1355 ALOGW("%s: could not get AudioFlinger", __func__);
1356 return 0;
1357 }
1358
1359 return af->openOutput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask,
1360 pLatencyMs, flags);
1361}
1362
1363static audio_io_handle_t aps_open_output_on_module(void *service,
1364 audio_module_handle_t module,
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 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001377 return af->openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
Dima Zavinfce7a472011-04-19 22:30:36 -07001378 pLatencyMs, flags);
1379}
1380
1381static audio_io_handle_t aps_open_dup_output(void *service,
1382 audio_io_handle_t output1,
1383 audio_io_handle_t output2)
1384{
1385 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001386 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001387 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001388 return 0;
1389 }
1390 return af->openDuplicateOutput(output1, output2);
1391}
1392
1393static int aps_close_output(void *service, audio_io_handle_t output)
1394{
1395 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001396 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001397 return PERMISSION_DENIED;
1398
1399 return af->closeOutput(output);
1400}
1401
1402static int aps_suspend_output(void *service, audio_io_handle_t output)
1403{
1404 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001405 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001406 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001407 return PERMISSION_DENIED;
1408 }
1409
1410 return af->suspendOutput(output);
1411}
1412
1413static int aps_restore_output(void *service, audio_io_handle_t output)
1414{
1415 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001416 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001417 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001418 return PERMISSION_DENIED;
1419 }
1420
1421 return af->restoreOutput(output);
1422}
1423
Glenn Kasten20010052012-06-22 13:43:51 -07001424// deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
Dima Zavinfce7a472011-04-19 22:30:36 -07001425static audio_io_handle_t aps_open_input(void *service,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001426 audio_devices_t *pDevices,
1427 uint32_t *pSamplingRate,
1428 audio_format_t *pFormat,
1429 audio_channel_mask_t *pChannelMask,
1430 audio_in_acoustics_t acoustics)
Dima Zavinfce7a472011-04-19 22:30:36 -07001431{
1432 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001433 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001434 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001435 return 0;
1436 }
1437
Eric Laurenta4c5a552012-03-29 10:12:40 -07001438 return af->openInput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask);
1439}
1440
1441static audio_io_handle_t aps_open_input_on_module(void *service,
1442 audio_module_handle_t module,
1443 audio_devices_t *pDevices,
1444 uint32_t *pSamplingRate,
1445 audio_format_t *pFormat,
1446 audio_channel_mask_t *pChannelMask)
1447{
1448 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1449 if (af == 0) {
1450 ALOGW("%s: could not get AudioFlinger", __func__);
1451 return 0;
1452 }
1453
1454 return af->openInput(module, pDevices, pSamplingRate, pFormat, pChannelMask);
Dima Zavinfce7a472011-04-19 22:30:36 -07001455}
1456
1457static int aps_close_input(void *service, audio_io_handle_t input)
1458{
1459 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001460 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001461 return PERMISSION_DENIED;
1462
1463 return af->closeInput(input);
1464}
1465
1466static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1467 audio_io_handle_t output)
1468{
1469 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001470 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001471 return PERMISSION_DENIED;
1472
1473 return af->setStreamOutput(stream, output);
1474}
1475
1476static int aps_move_effects(void *service, int session,
1477 audio_io_handle_t src_output,
1478 audio_io_handle_t dst_output)
1479{
1480 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001481 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001482 return PERMISSION_DENIED;
1483
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001484 return af->moveEffects(session, src_output, dst_output);
Dima Zavinfce7a472011-04-19 22:30:36 -07001485}
1486
1487static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1488 const char *keys)
1489{
1490 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1491 return strdup(result.string());
1492}
1493
1494static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1495 const char *kv_pairs, int delay_ms)
1496{
1497 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1498
1499 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1500}
1501
1502static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1503 float volume, audio_io_handle_t output,
1504 int delay_ms)
1505{
1506 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1507
1508 return audioPolicyService->setStreamVolume(stream, volume, output,
1509 delay_ms);
1510}
1511
1512static int aps_start_tone(void *service, audio_policy_tone_t tone,
1513 audio_stream_type_t stream)
1514{
1515 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1516
1517 return audioPolicyService->startTone(tone, stream);
1518}
1519
1520static int aps_stop_tone(void *service)
1521{
1522 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1523
1524 return audioPolicyService->stopTone();
1525}
1526
1527static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1528{
1529 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1530
1531 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1532}
1533
1534}; // extern "C"
1535
1536namespace {
1537 struct audio_policy_service_ops aps_ops = {
1538 open_output : aps_open_output,
1539 open_duplicate_output : aps_open_dup_output,
1540 close_output : aps_close_output,
1541 suspend_output : aps_suspend_output,
1542 restore_output : aps_restore_output,
1543 open_input : aps_open_input,
1544 close_input : aps_close_input,
1545 set_stream_volume : aps_set_stream_volume,
1546 set_stream_output : aps_set_stream_output,
1547 set_parameters : aps_set_parameters,
1548 get_parameters : aps_get_parameters,
1549 start_tone : aps_start_tone,
1550 stop_tone : aps_stop_tone,
1551 set_voice_volume : aps_set_voice_volume,
1552 move_effects : aps_move_effects,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001553 load_hw_module : aps_load_hw_module,
1554 open_output_on_module : aps_open_output_on_module,
1555 open_input_on_module : aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001556 };
1557}; // namespace <unnamed>
1558
Mathias Agopian65ab4712010-07-14 17:59:35 -07001559}; // namespace android