blob: 7487f4bf94e89755a4eb17d927fffd8b40d67b31 [file] [log] [blame]
Eric Laurenta553c252009-07-17 12:17:14 -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
Eric Laurent327c27b2009-08-27 00:48:47 -070019
20#undef __STRICT_ANSI__
21#define __STDINT_LIMITS
22#define __STDC_LIMIT_MACROS
23#include <stdint.h>
24
25#include <sys/time.h>
Eric Laurenta553c252009-07-17 12:17:14 -070026#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 Kasten81211142012-02-05 18:09:08 -080033#include "ServiceUtilities.h"
Eric Laurenta553c252009-07-17 12:17:14 -070034#include <cutils/properties.h>
Eric Laurentcef3cd72009-12-10 01:03:50 -080035#include <hardware_legacy/power.h>
Eric Laurent464d5b32011-06-17 21:29:58 -070036#include <media/AudioEffect.h>
37#include <media/EffectsFactoryApi.h>
Eric Laurenta553c252009-07-17 12:17:14 -070038
Dima Zavin24fc2fb2011-04-19 22:30:36 -070039#include <hardware/hardware.h>
Dima Zavin34bb4192011-05-11 14:15:23 -070040#include <system/audio.h>
Dima Zavin290029d2011-06-13 18:16:26 -070041#include <system/audio_policy.h>
Dima Zavin24fc2fb2011-04-19 22:30:36 -070042#include <hardware/audio_policy.h>
Eric Laurent464d5b32011-06-17 21:29:58 -070043#include <audio_effects/audio_effects_conf.h>
Dima Zavin24fc2fb2011-04-19 22:30:36 -070044
Eric Laurenta553c252009-07-17 12:17:14 -070045namespace android {
46
Glenn Kasten05d33102012-01-09 08:41:22 -080047static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
48static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Eric Laurentf4ee40e2009-11-02 10:29:02 -080049
50static const int kDumpLockRetries = 50;
Glenn Kasten22c42412012-01-09 08:33:38 -080051static const int kDumpLockSleepUs = 20000;
Eric Laurentf4ee40e2009-11-02 10:29:02 -080052
Dima Zavin24fc2fb2011-04-19 22:30:36 -070053namespace {
54 extern struct audio_policy_service_ops aps_ops;
55};
56
Eric Laurenta553c252009-07-17 12:17:14 -070057// ----------------------------------------------------------------------------
58
59AudioPolicyService::AudioPolicyService()
Dima Zavin24fc2fb2011-04-19 22:30:36 -070060 : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL)
Eric Laurenta553c252009-07-17 12:17:14 -070061{
Eric Laurenta553c252009-07-17 12:17:14 -070062 char value[PROPERTY_VALUE_MAX];
Dima Zavin24fc2fb2011-04-19 22:30:36 -070063 const struct hw_module_t *module;
64 int forced_val;
65 int rc;
Eric Laurenta553c252009-07-17 12:17:14 -070066
Eric Laurent01635942011-01-18 18:39:02 -080067 Mutex::Autolock _l(mLock);
68
Eric Laurenta553c252009-07-17 12:17:14 -070069 // start tone playback thread
Eric Laurentcef3cd72009-12-10 01:03:50 -080070 mTonePlaybackThread = new AudioCommandThread(String8(""));
Eric Laurenta553c252009-07-17 12:17:14 -070071 // start audio commands thread
Glenn Kasten86e33622012-02-28 12:30:08 -080072 mAudioCommandThread = new AudioCommandThread(String8("ApmCommand"));
Eric Laurenta553c252009-07-17 12:17:14 -070073
Dima Zavin24fc2fb2011-04-19 22:30:36 -070074 /* instantiate the audio policy manager */
75 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
76 if (rc)
77 return;
Eric Laurenta553c252009-07-17 12:17:14 -070078
Dima Zavin24fc2fb2011-04-19 22:30:36 -070079 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
Steve Block3762c312012-01-06 19:20:56 +000080 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
Dima Zavin24fc2fb2011-04-19 22:30:36 -070081 if (rc)
82 return;
Eric Laurent01635942011-01-18 18:39:02 -080083
Dima Zavin24fc2fb2011-04-19 22:30:36 -070084 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
85 &mpAudioPolicy);
Steve Block3762c312012-01-06 19:20:56 +000086 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
Dima Zavin24fc2fb2011-04-19 22:30:36 -070087 if (rc)
88 return;
89
90 rc = mpAudioPolicy->init_check(mpAudioPolicy);
Steve Block3762c312012-01-06 19:20:56 +000091 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
Dima Zavin24fc2fb2011-04-19 22:30:36 -070092 if (rc)
93 return;
94
95 property_get("ro.camera.sound.forced", value, "0");
96 forced_val = strtol(value, NULL, 0);
97 mpAudioPolicy->set_can_mute_enforced_audible(mpAudioPolicy, !forced_val);
98
Steve Block6215d3f2012-01-04 20:05:49 +000099 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurent464d5b32011-06-17 21:29:58 -0700100
101 // load audio pre processing modules
102 if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) {
103 loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE);
104 } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) {
105 loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE);
106 }
Eric Laurenta553c252009-07-17 12:17:14 -0700107}
108
109AudioPolicyService::~AudioPolicyService()
110{
Eric Laurent327c27b2009-08-27 00:48:47 -0700111 mTonePlaybackThread->exit();
112 mTonePlaybackThread.clear();
Eric Laurenta553c252009-07-17 12:17:14 -0700113 mAudioCommandThread->exit();
114 mAudioCommandThread.clear();
115
Eric Laurent464d5b32011-06-17 21:29:58 -0700116
117 // release audio pre processing resources
118 for (size_t i = 0; i < mInputSources.size(); i++) {
Glenn Kastenafe98332012-02-02 14:04:37 -0800119 delete mInputSources.valueAt(i);
Eric Laurent464d5b32011-06-17 21:29:58 -0700120 }
121 mInputSources.clear();
122
123 for (size_t i = 0; i < mInputs.size(); i++) {
124 mInputs.valueAt(i)->mEffects.clear();
125 delete mInputs.valueAt(i);
126 }
127 mInputs.clear();
128
Glenn Kasten3694ec12012-01-27 16:47:15 -0800129 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL)
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700130 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kasten3694ec12012-01-27 16:47:15 -0800131 if (mpAudioPolicyDev != NULL)
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700132 audio_policy_dev_close(mpAudioPolicyDev);
Eric Laurenta553c252009-07-17 12:17:14 -0700133}
134
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700135status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
136 audio_policy_dev_state_t state,
Eric Laurenta553c252009-07-17 12:17:14 -0700137 const char *device_address)
138{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700139 if (mpAudioPolicy == NULL) {
Eric Laurenta553c252009-07-17 12:17:14 -0700140 return NO_INIT;
141 }
Glenn Kasten81211142012-02-05 18:09:08 -0800142 if (!settingsAllowed()) {
Eric Laurenta553c252009-07-17 12:17:14 -0700143 return PERMISSION_DENIED;
144 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700145 if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
Eric Laurenta553c252009-07-17 12:17:14 -0700146 return BAD_VALUE;
147 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700148 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
149 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
Eric Laurenta553c252009-07-17 12:17:14 -0700150 return BAD_VALUE;
151 }
152
Steve Block71f2cf12011-10-20 11:56:00 +0100153 ALOGV("setDeviceConnectionState() tid %d", gettid());
Eric Laurenta553c252009-07-17 12:17:14 -0700154 Mutex::Autolock _l(mLock);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700155 return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device,
156 state, device_address);
Eric Laurenta553c252009-07-17 12:17:14 -0700157}
158
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700159audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
160 audio_devices_t device,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700161 const char *device_address)
Eric Laurenta553c252009-07-17 12:17:14 -0700162{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700163 if (mpAudioPolicy == NULL) {
164 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Eric Laurenta553c252009-07-17 12:17:14 -0700165 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700166 return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device,
167 device_address);
Eric Laurenta553c252009-07-17 12:17:14 -0700168}
169
Glenn Kastenaccb1142012-01-04 11:00:47 -0800170status_t AudioPolicyService::setPhoneState(audio_mode_t state)
Eric Laurenta553c252009-07-17 12:17:14 -0700171{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700172 if (mpAudioPolicy == NULL) {
Eric Laurenta553c252009-07-17 12:17:14 -0700173 return NO_INIT;
174 }
Glenn Kasten81211142012-02-05 18:09:08 -0800175 if (!settingsAllowed()) {
Eric Laurenta553c252009-07-17 12:17:14 -0700176 return PERMISSION_DENIED;
177 }
Glenn Kasten01aaf2c2012-01-06 16:47:31 -0800178 if (uint32_t(state) >= AUDIO_MODE_CNT) {
Eric Laurenta553c252009-07-17 12:17:14 -0700179 return BAD_VALUE;
180 }
181
Steve Block71f2cf12011-10-20 11:56:00 +0100182 ALOGV("setPhoneState() tid %d", gettid());
Eric Laurenta553c252009-07-17 12:17:14 -0700183
184 // TODO: check if it is more appropriate to do it in platform specific policy manager
185 AudioSystem::setMode(state);
186
187 Mutex::Autolock _l(mLock);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700188 mpAudioPolicy->set_phone_state(mpAudioPolicy, state);
Eric Laurenta553c252009-07-17 12:17:14 -0700189 return NO_ERROR;
190}
191
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700192status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
193 audio_policy_forced_cfg_t config)
Eric Laurenta553c252009-07-17 12:17:14 -0700194{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700195 if (mpAudioPolicy == NULL) {
Eric Laurenta553c252009-07-17 12:17:14 -0700196 return NO_INIT;
197 }
Glenn Kasten81211142012-02-05 18:09:08 -0800198 if (!settingsAllowed()) {
Eric Laurenta553c252009-07-17 12:17:14 -0700199 return PERMISSION_DENIED;
200 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700201 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
Eric Laurenta553c252009-07-17 12:17:14 -0700202 return BAD_VALUE;
203 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700204 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
Eric Laurenta553c252009-07-17 12:17:14 -0700205 return BAD_VALUE;
206 }
Steve Block71f2cf12011-10-20 11:56:00 +0100207 ALOGV("setForceUse() tid %d", gettid());
Eric Laurenta553c252009-07-17 12:17:14 -0700208 Mutex::Autolock _l(mLock);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700209 mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config);
Eric Laurenta553c252009-07-17 12:17:14 -0700210 return NO_ERROR;
211}
212
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700213audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
Eric Laurenta553c252009-07-17 12:17:14 -0700214{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700215 if (mpAudioPolicy == NULL) {
216 return AUDIO_POLICY_FORCE_NONE;
Eric Laurenta553c252009-07-17 12:17:14 -0700217 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700218 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
219 return AUDIO_POLICY_FORCE_NONE;
Eric Laurenta553c252009-07-17 12:17:14 -0700220 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700221 return mpAudioPolicy->get_force_use(mpAudioPolicy, usage);
Eric Laurenta553c252009-07-17 12:17:14 -0700222}
223
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700224audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
Eric Laurenta553c252009-07-17 12:17:14 -0700225 uint32_t samplingRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -0800226 audio_format_t format,
Eric Laurenta553c252009-07-17 12:17:14 -0700227 uint32_t channels,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700228 audio_policy_output_flags_t flags)
Eric Laurenta553c252009-07-17 12:17:14 -0700229{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700230 if (mpAudioPolicy == NULL) {
Eric Laurentddb78e72009-07-28 08:44:33 -0700231 return 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700232 }
Steve Block71f2cf12011-10-20 11:56:00 +0100233 ALOGV("getOutput() tid %d", gettid());
Eric Laurenta553c252009-07-17 12:17:14 -0700234 Mutex::Autolock _l(mLock);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700235 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, format, channels, flags);
Eric Laurenta553c252009-07-17 12:17:14 -0700236}
237
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700238status_t AudioPolicyService::startOutput(audio_io_handle_t output,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700239 audio_stream_type_t stream,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700240 int session)
Eric Laurenta553c252009-07-17 12:17:14 -0700241{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700242 if (mpAudioPolicy == NULL) {
Eric Laurenta553c252009-07-17 12:17:14 -0700243 return NO_INIT;
244 }
Steve Block71f2cf12011-10-20 11:56:00 +0100245 ALOGV("startOutput() tid %d", gettid());
Eric Laurenta553c252009-07-17 12:17:14 -0700246 Mutex::Autolock _l(mLock);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700247 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
Eric Laurenta553c252009-07-17 12:17:14 -0700248}
249
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700250status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700251 audio_stream_type_t stream,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700252 int session)
Eric Laurenta553c252009-07-17 12:17:14 -0700253{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700254 if (mpAudioPolicy == NULL) {
Eric Laurenta553c252009-07-17 12:17:14 -0700255 return NO_INIT;
256 }
Steve Block71f2cf12011-10-20 11:56:00 +0100257 ALOGV("stopOutput() tid %d", gettid());
Eric Laurenta553c252009-07-17 12:17:14 -0700258 Mutex::Autolock _l(mLock);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700259 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
Eric Laurenta553c252009-07-17 12:17:14 -0700260}
261
262void AudioPolicyService::releaseOutput(audio_io_handle_t output)
263{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700264 if (mpAudioPolicy == NULL) {
Eric Laurenta553c252009-07-17 12:17:14 -0700265 return;
266 }
Steve Block71f2cf12011-10-20 11:56:00 +0100267 ALOGV("releaseOutput() tid %d", gettid());
Eric Laurenta553c252009-07-17 12:17:14 -0700268 Mutex::Autolock _l(mLock);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700269 mpAudioPolicy->release_output(mpAudioPolicy, output);
Eric Laurenta553c252009-07-17 12:17:14 -0700270}
271
Glenn Kasten0f0fbd92012-01-23 13:58:49 -0800272audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource,
Eric Laurenta553c252009-07-17 12:17:14 -0700273 uint32_t samplingRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -0800274 audio_format_t format,
Eric Laurenta553c252009-07-17 12:17:14 -0700275 uint32_t channels,
Eric Laurent464d5b32011-06-17 21:29:58 -0700276 audio_in_acoustics_t acoustics,
277 int audioSession)
Eric Laurenta553c252009-07-17 12:17:14 -0700278{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700279 if (mpAudioPolicy == NULL) {
Eric Laurentddb78e72009-07-28 08:44:33 -0700280 return 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700281 }
Glenn Kasten0f0fbd92012-01-23 13:58:49 -0800282 // already checked by client, but double-check in case the client wrapper is bypassed
283 if (uint32_t(inputSource) >= AUDIO_SOURCE_CNT) {
284 return 0;
285 }
Eric Laurenta553c252009-07-17 12:17:14 -0700286 Mutex::Autolock _l(mLock);
Eric Laurent464d5b32011-06-17 21:29:58 -0700287 audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
288 format, channels, acoustics);
289
290 if (input == 0) {
291 return input;
292 }
293 // create audio pre processors according to input source
Glenn Kasten0f0fbd92012-01-23 13:58:49 -0800294 ssize_t index = mInputSources.indexOfKey(inputSource);
Eric Laurent464d5b32011-06-17 21:29:58 -0700295 if (index < 0) {
296 return input;
297 }
298 ssize_t idx = mInputs.indexOfKey(input);
299 InputDesc *inputDesc;
300 if (idx < 0) {
Glenn Kastenfcf2ac02012-03-07 16:49:22 -0800301 inputDesc = new InputDesc(audioSession);
Eric Laurent464d5b32011-06-17 21:29:58 -0700302 mInputs.add(input, inputDesc);
303 } else {
304 inputDesc = mInputs.valueAt(idx);
305 }
306
307 Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects;
308 for (size_t i = 0; i < effects.size(); i++) {
309 EffectDesc *effect = effects[i];
310 sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input);
311 status_t status = fx->initCheck();
312 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Steve Block8564c8d2012-01-05 23:22:43 +0000313 ALOGW("Failed to create Fx %s on input %d", effect->mName, input);
Eric Laurent464d5b32011-06-17 21:29:58 -0700314 // fx goes out of scope and strong ref on AudioEffect is released
315 continue;
316 }
317 for (size_t j = 0; j < effect->mParams.size(); j++) {
318 fx->setParameter(effect->mParams[j]);
319 }
320 inputDesc->mEffects.add(fx);
321 }
322 setPreProcessorEnabled(inputDesc, true);
323 return input;
Eric Laurenta553c252009-07-17 12:17:14 -0700324}
325
326status_t AudioPolicyService::startInput(audio_io_handle_t input)
327{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700328 if (mpAudioPolicy == NULL) {
Eric Laurenta553c252009-07-17 12:17:14 -0700329 return NO_INIT;
330 }
331 Mutex::Autolock _l(mLock);
Eric Laurent464d5b32011-06-17 21:29:58 -0700332
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700333 return mpAudioPolicy->start_input(mpAudioPolicy, input);
Eric Laurenta553c252009-07-17 12:17:14 -0700334}
335
336status_t AudioPolicyService::stopInput(audio_io_handle_t input)
337{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700338 if (mpAudioPolicy == NULL) {
Eric Laurenta553c252009-07-17 12:17:14 -0700339 return NO_INIT;
340 }
341 Mutex::Autolock _l(mLock);
Eric Laurent464d5b32011-06-17 21:29:58 -0700342
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700343 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
Eric Laurenta553c252009-07-17 12:17:14 -0700344}
345
346void AudioPolicyService::releaseInput(audio_io_handle_t input)
347{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700348 if (mpAudioPolicy == NULL) {
Eric Laurenta553c252009-07-17 12:17:14 -0700349 return;
350 }
351 Mutex::Autolock _l(mLock);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700352 mpAudioPolicy->release_input(mpAudioPolicy, input);
Eric Laurent464d5b32011-06-17 21:29:58 -0700353
354 ssize_t index = mInputs.indexOfKey(input);
355 if (index < 0) {
356 return;
357 }
358 InputDesc *inputDesc = mInputs.valueAt(index);
359 setPreProcessorEnabled(inputDesc, false);
Eric Laurent464d5b32011-06-17 21:29:58 -0700360 delete inputDesc;
361 mInputs.removeItemsAt(index);
Eric Laurenta553c252009-07-17 12:17:14 -0700362}
363
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700364status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
Eric Laurenta553c252009-07-17 12:17:14 -0700365 int indexMin,
366 int indexMax)
367{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700368 if (mpAudioPolicy == NULL) {
Eric Laurenta553c252009-07-17 12:17:14 -0700369 return NO_INIT;
370 }
Glenn Kasten81211142012-02-05 18:09:08 -0800371 if (!settingsAllowed()) {
Eric Laurenta553c252009-07-17 12:17:14 -0700372 return PERMISSION_DENIED;
373 }
Glenn Kasten6e987a42012-01-06 08:40:01 -0800374 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Eric Laurenta553c252009-07-17 12:17:14 -0700375 return BAD_VALUE;
376 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700377 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
Eric Laurenta553c252009-07-17 12:17:14 -0700378 return NO_ERROR;
379}
380
Eric Laurent9bc8358d2011-11-18 16:43:31 -0800381status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
382 int index,
383 audio_devices_t device)
Eric Laurenta553c252009-07-17 12:17:14 -0700384{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700385 if (mpAudioPolicy == NULL) {
Eric Laurenta553c252009-07-17 12:17:14 -0700386 return NO_INIT;
387 }
Glenn Kasten81211142012-02-05 18:09:08 -0800388 if (!settingsAllowed()) {
Eric Laurenta553c252009-07-17 12:17:14 -0700389 return PERMISSION_DENIED;
390 }
Glenn Kasten6e987a42012-01-06 08:40:01 -0800391 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Eric Laurenta553c252009-07-17 12:17:14 -0700392 return BAD_VALUE;
393 }
394
Eric Laurent9bc8358d2011-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 }
Eric Laurenta553c252009-07-17 12:17:14 -0700403}
404
Eric Laurent9bc8358d2011-11-18 16:43:31 -0800405status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
406 int *index,
407 audio_devices_t device)
Eric Laurenta553c252009-07-17 12:17:14 -0700408{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700409 if (mpAudioPolicy == NULL) {
Eric Laurenta553c252009-07-17 12:17:14 -0700410 return NO_INIT;
411 }
Glenn Kasten6e987a42012-01-06 08:40:01 -0800412 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Eric Laurenta553c252009-07-17 12:17:14 -0700413 return BAD_VALUE;
414 }
Eric Laurent9bc8358d2011-11-18 16:43:31 -0800415 if (mpAudioPolicy->get_stream_volume_index_for_device) {
416 return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
417 stream,
418 index,
419 device);
420 } else {
421 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
422 }
Eric Laurenta553c252009-07-17 12:17:14 -0700423}
424
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700425uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700426{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700427 if (mpAudioPolicy == NULL) {
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700428 return 0;
429 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700430 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700431}
432
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700433uint32_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800434{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700435 if (mpAudioPolicy == NULL) {
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800436 return 0;
437 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700438 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten8b4b97a2011-02-04 13:54:26 -0800439}
440
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700441audio_io_handle_t AudioPolicyService::getOutputForEffect(effect_descriptor_t *desc)
442{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700443 if (mpAudioPolicy == NULL) {
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700444 return NO_INIT;
445 }
446 Mutex::Autolock _l(mLock);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700447 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700448}
449
450status_t AudioPolicyService::registerEffect(effect_descriptor_t *desc,
Eric Laurent464d5b32011-06-17 21:29:58 -0700451 audio_io_handle_t io,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700452 uint32_t strategy,
453 int session,
454 int id)
455{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700456 if (mpAudioPolicy == NULL) {
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700457 return NO_INIT;
458 }
Eric Laurent464d5b32011-06-17 21:29:58 -0700459 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700460}
461
462status_t AudioPolicyService::unregisterEffect(int id)
463{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700464 if (mpAudioPolicy == NULL) {
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700465 return NO_INIT;
466 }
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700467 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700468}
469
Eric Laurent6752ec82011-08-10 10:37:50 -0700470status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
471{
472 if (mpAudioPolicy == NULL) {
473 return NO_INIT;
474 }
475 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
476}
477
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800478bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurent25101b02011-02-02 09:33:30 -0800479{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700480 if (mpAudioPolicy == NULL) {
Eric Laurent25101b02011-02-02 09:33:30 -0800481 return 0;
482 }
483 Mutex::Autolock _l(mLock);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700484 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurent25101b02011-02-02 09:33:30 -0800485}
486
Eric Laurent0f7f4ec2011-07-24 13:36:09 -0700487status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
488 effect_descriptor_t *descriptors,
489 uint32_t *count)
490{
491
492 if (mpAudioPolicy == NULL) {
493 *count = 0;
494 return NO_INIT;
495 }
496 Mutex::Autolock _l(mLock);
497 status_t status = NO_ERROR;
498
499 size_t index;
500 for (index = 0; index < mInputs.size(); index++) {
501 if (mInputs.valueAt(index)->mSessionId == audioSession) {
502 break;
503 }
504 }
505 if (index == mInputs.size()) {
506 *count = 0;
507 return BAD_VALUE;
508 }
509 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
510
511 for (size_t i = 0; i < effects.size(); i++) {
512 effect_descriptor_t desc = effects[i]->descriptor();
513 if (i < *count) {
514 memcpy(descriptors + i, &desc, sizeof(effect_descriptor_t));
515 }
516 }
517 if (effects.size() > *count) {
518 status = NO_MEMORY;
519 }
520 *count = effects.size();
521 return status;
522}
523
Eric Laurenta553c252009-07-17 12:17:14 -0700524void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten7ed4f0c2012-02-03 11:10:00 -0800525 ALOGW("binderDied() %p, tid %d, calling pid %d", who.unsafe_get(), gettid(),
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700526 IPCThreadState::self()->getCallingPid());
Eric Laurenta553c252009-07-17 12:17:14 -0700527}
528
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800529static bool tryLock(Mutex& mutex)
530{
531 bool locked = false;
532 for (int i = 0; i < kDumpLockRetries; ++i) {
533 if (mutex.tryLock() == NO_ERROR) {
534 locked = true;
535 break;
536 }
Glenn Kasten22c42412012-01-09 08:33:38 -0800537 usleep(kDumpLockSleepUs);
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800538 }
539 return locked;
540}
541
542status_t AudioPolicyService::dumpInternals(int fd)
543{
544 const size_t SIZE = 256;
545 char buffer[SIZE];
546 String8 result;
547
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700548 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800549 result.append(buffer);
550 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
551 result.append(buffer);
552 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
553 result.append(buffer);
554
555 write(fd, result.string(), result.size());
556 return NO_ERROR;
557}
558
Eric Laurenta553c252009-07-17 12:17:14 -0700559status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
560{
Glenn Kasten81211142012-02-05 18:09:08 -0800561 if (!dumpAllowed()) {
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800562 dumpPermissionDenial(fd);
Eric Laurenta553c252009-07-17 12:17:14 -0700563 } else {
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800564 bool locked = tryLock(mLock);
565 if (!locked) {
566 String8 result(kDeadlockedString);
567 write(fd, result.string(), result.size());
568 }
Eric Laurenta553c252009-07-17 12:17:14 -0700569
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800570 dumpInternals(fd);
Glenn Kasten1137be12012-02-08 17:47:58 -0800571 if (mAudioCommandThread != 0) {
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800572 mAudioCommandThread->dump(fd);
573 }
Glenn Kasten1137be12012-02-08 17:47:58 -0800574 if (mTonePlaybackThread != 0) {
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800575 mTonePlaybackThread->dump(fd);
576 }
577
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700578 if (mpAudioPolicy) {
579 mpAudioPolicy->dump(mpAudioPolicy, fd);
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800580 }
581
582 if (locked) mLock.unlock();
Eric Laurenta553c252009-07-17 12:17:14 -0700583 }
584 return NO_ERROR;
585}
586
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800587status_t AudioPolicyService::dumpPermissionDenial(int fd)
Eric Laurenta553c252009-07-17 12:17:14 -0700588{
589 const size_t SIZE = 256;
590 char buffer[SIZE];
591 String8 result;
592 snprintf(buffer, SIZE, "Permission Denial: "
593 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
594 IPCThreadState::self()->getCallingPid(),
595 IPCThreadState::self()->getCallingUid());
596 result.append(buffer);
597 write(fd, result.string(), result.size());
598 return NO_ERROR;
599}
600
Glenn Kastenfcf2ac02012-03-07 16:49:22 -0800601void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent464d5b32011-06-17 21:29:58 -0700602{
Glenn Kastenfcf2ac02012-03-07 16:49:22 -0800603 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent464d5b32011-06-17 21:29:58 -0700604 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kasten7d3be3a2012-01-26 10:53:32 -0800605 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent464d5b32011-06-17 21:29:58 -0700606 }
607}
608
Eric Laurenta553c252009-07-17 12:17:14 -0700609status_t AudioPolicyService::onTransact(
610 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
611{
612 return BnAudioPolicyService::onTransact(code, data, reply, flags);
613}
614
615
Eric Laurenta553c252009-07-17 12:17:14 -0700616// ----------- AudioPolicyService::AudioCommandThread implementation ----------
617
Eric Laurentcef3cd72009-12-10 01:03:50 -0800618AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name)
619 : Thread(false), mName(name)
Eric Laurenta553c252009-07-17 12:17:14 -0700620{
621 mpToneGenerator = NULL;
622}
623
624
625AudioPolicyService::AudioCommandThread::~AudioCommandThread()
626{
Eric Laurentcef3cd72009-12-10 01:03:50 -0800627 if (mName != "" && !mAudioCommands.isEmpty()) {
628 release_wake_lock(mName.string());
629 }
Eric Laurenta553c252009-07-17 12:17:14 -0700630 mAudioCommands.clear();
Glenn Kasten2589cf92012-01-27 18:08:45 -0800631 delete mpToneGenerator;
Eric Laurenta553c252009-07-17 12:17:14 -0700632}
633
634void AudioPolicyService::AudioCommandThread::onFirstRef()
635{
Eric Laurentcef3cd72009-12-10 01:03:50 -0800636 if (mName != "") {
637 run(mName.string(), ANDROID_PRIORITY_AUDIO);
638 } else {
Glenn Kasten86e33622012-02-28 12:30:08 -0800639 run("AudioCommand", ANDROID_PRIORITY_AUDIO);
Eric Laurentcef3cd72009-12-10 01:03:50 -0800640 }
Eric Laurenta553c252009-07-17 12:17:14 -0700641}
642
643bool AudioPolicyService::AudioCommandThread::threadLoop()
644{
Eric Laurent327c27b2009-08-27 00:48:47 -0700645 nsecs_t waitTime = INT64_MAX;
646
Eric Laurenta553c252009-07-17 12:17:14 -0700647 mLock.lock();
648 while (!exitPending())
649 {
650 while(!mAudioCommands.isEmpty()) {
Eric Laurent327c27b2009-08-27 00:48:47 -0700651 nsecs_t curTime = systemTime();
652 // commands are sorted by increasing time stamp: execute them from index 0 and up
653 if (mAudioCommands[0]->mTime <= curTime) {
654 AudioCommand *command = mAudioCommands[0];
655 mAudioCommands.removeAt(0);
Eric Laurent3fdb1262009-11-07 00:01:32 -0800656 mLastCommand = *command;
657
Eric Laurent327c27b2009-08-27 00:48:47 -0700658 switch (command->mCommand) {
659 case START_TONE: {
660 mLock.unlock();
661 ToneData *data = (ToneData *)command->mParam;
Steve Block71f2cf12011-10-20 11:56:00 +0100662 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Eric Laurent327c27b2009-08-27 00:48:47 -0700663 data->mType, data->mStream);
Glenn Kasten2589cf92012-01-27 18:08:45 -0800664 delete mpToneGenerator;
Eric Laurent327c27b2009-08-27 00:48:47 -0700665 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
666 mpToneGenerator->startTone(data->mType);
667 delete data;
668 mLock.lock();
669 }break;
670 case STOP_TONE: {
671 mLock.unlock();
Steve Block71f2cf12011-10-20 11:56:00 +0100672 ALOGV("AudioCommandThread() processing stop tone");
Eric Laurent327c27b2009-08-27 00:48:47 -0700673 if (mpToneGenerator != NULL) {
674 mpToneGenerator->stopTone();
675 delete mpToneGenerator;
676 mpToneGenerator = NULL;
677 }
678 mLock.lock();
679 }break;
680 case SET_VOLUME: {
681 VolumeData *data = (VolumeData *)command->mParam;
Steve Block71f2cf12011-10-20 11:56:00 +0100682 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700683 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
684 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
685 data->mVolume,
686 data->mIO);
Eric Laurent327c27b2009-08-27 00:48:47 -0700687 if (command->mWaitStatus) {
688 command->mCond.signal();
689 mWaitWorkCV.wait(mLock);
690 }
691 delete data;
692 }break;
693 case SET_PARAMETERS: {
694 ParametersData *data = (ParametersData *)command->mParam;
Steve Block71f2cf12011-10-20 11:56:00 +0100695 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700696 data->mKeyValuePairs.string(), data->mIO);
Eric Laurent327c27b2009-08-27 00:48:47 -0700697 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
698 if (command->mWaitStatus) {
699 command->mCond.signal();
700 mWaitWorkCV.wait(mLock);
701 }
702 delete data;
703 }break;
Eric Laurent415f3e22009-10-21 08:14:22 -0700704 case SET_VOICE_VOLUME: {
705 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block71f2cf12011-10-20 11:56:00 +0100706 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700707 data->mVolume);
Eric Laurent415f3e22009-10-21 08:14:22 -0700708 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
709 if (command->mWaitStatus) {
710 command->mCond.signal();
711 mWaitWorkCV.wait(mLock);
712 }
713 delete data;
714 }break;
Eric Laurent327c27b2009-08-27 00:48:47 -0700715 default:
Steve Block8564c8d2012-01-05 23:22:43 +0000716 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Eric Laurenta553c252009-07-17 12:17:14 -0700717 }
Eric Laurent327c27b2009-08-27 00:48:47 -0700718 delete command;
719 waitTime = INT64_MAX;
720 } else {
721 waitTime = mAudioCommands[0]->mTime - curTime;
722 break;
Eric Laurenta553c252009-07-17 12:17:14 -0700723 }
Eric Laurenta553c252009-07-17 12:17:14 -0700724 }
Eric Laurentcef3cd72009-12-10 01:03:50 -0800725 // release delayed commands wake lock
726 if (mName != "" && mAudioCommands.isEmpty()) {
727 release_wake_lock(mName.string());
728 }
Steve Block71f2cf12011-10-20 11:56:00 +0100729 ALOGV("AudioCommandThread() going to sleep");
Eric Laurent327c27b2009-08-27 00:48:47 -0700730 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block71f2cf12011-10-20 11:56:00 +0100731 ALOGV("AudioCommandThread() waking up");
Eric Laurenta553c252009-07-17 12:17:14 -0700732 }
733 mLock.unlock();
734 return false;
735}
736
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800737status_t AudioPolicyService::AudioCommandThread::dump(int fd)
738{
739 const size_t SIZE = 256;
740 char buffer[SIZE];
741 String8 result;
742
743 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
744 result.append(buffer);
745 write(fd, result.string(), result.size());
746
747 bool locked = tryLock(mLock);
748 if (!locked) {
749 String8 result2(kCmdDeadlockedString);
750 write(fd, result2.string(), result2.size());
751 }
752
753 snprintf(buffer, SIZE, "- Commands:\n");
754 result = String8(buffer);
Eric Laurent3fdb1262009-11-07 00:01:32 -0800755 result.append(" Command Time Wait pParam\n");
Glenn Kasten150d2382012-02-08 14:04:28 -0800756 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800757 mAudioCommands[i]->dump(buffer, SIZE);
758 result.append(buffer);
759 }
Eric Laurent3fdb1262009-11-07 00:01:32 -0800760 result.append(" Last Command\n");
761 mLastCommand.dump(buffer, SIZE);
762 result.append(buffer);
763
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800764 write(fd, result.string(), result.size());
765
766 if (locked) mLock.unlock();
767
768 return NO_ERROR;
769}
770
Glenn Kasten23f7ad32012-01-27 15:25:25 -0800771void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
772 audio_stream_type_t stream)
Eric Laurenta553c252009-07-17 12:17:14 -0700773{
Eric Laurenta553c252009-07-17 12:17:14 -0700774 AudioCommand *command = new AudioCommand();
775 command->mCommand = START_TONE;
776 ToneData *data = new ToneData();
777 data->mType = type;
778 data->mStream = stream;
779 command->mParam = (void *)data;
Eric Laurent327c27b2009-08-27 00:48:47 -0700780 command->mWaitStatus = false;
Eric Laurent415f3e22009-10-21 08:14:22 -0700781 Mutex::Autolock _l(mLock);
Eric Laurent327c27b2009-08-27 00:48:47 -0700782 insertCommand_l(command);
Steve Block71f2cf12011-10-20 11:56:00 +0100783 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Eric Laurenta553c252009-07-17 12:17:14 -0700784 mWaitWorkCV.signal();
785}
786
787void AudioPolicyService::AudioCommandThread::stopToneCommand()
788{
Eric Laurenta553c252009-07-17 12:17:14 -0700789 AudioCommand *command = new AudioCommand();
790 command->mCommand = STOP_TONE;
791 command->mParam = NULL;
Eric Laurent327c27b2009-08-27 00:48:47 -0700792 command->mWaitStatus = false;
Eric Laurent415f3e22009-10-21 08:14:22 -0700793 Mutex::Autolock _l(mLock);
Eric Laurent327c27b2009-08-27 00:48:47 -0700794 insertCommand_l(command);
Steve Block71f2cf12011-10-20 11:56:00 +0100795 ALOGV("AudioCommandThread() adding tone stop");
Eric Laurenta553c252009-07-17 12:17:14 -0700796 mWaitWorkCV.signal();
797}
798
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800799status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700800 float volume,
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800801 audio_io_handle_t output,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700802 int delayMs)
Eric Laurenta553c252009-07-17 12:17:14 -0700803{
Eric Laurent327c27b2009-08-27 00:48:47 -0700804 status_t status = NO_ERROR;
805
Eric Laurenta553c252009-07-17 12:17:14 -0700806 AudioCommand *command = new AudioCommand();
807 command->mCommand = SET_VOLUME;
808 VolumeData *data = new VolumeData();
809 data->mStream = stream;
810 data->mVolume = volume;
811 data->mIO = output;
812 command->mParam = data;
Eric Laurent327c27b2009-08-27 00:48:47 -0700813 if (delayMs == 0) {
814 command->mWaitStatus = true;
815 } else {
816 command->mWaitStatus = false;
817 }
Eric Laurent415f3e22009-10-21 08:14:22 -0700818 Mutex::Autolock _l(mLock);
Eric Laurent327c27b2009-08-27 00:48:47 -0700819 insertCommand_l(command, delayMs);
Steve Block71f2cf12011-10-20 11:56:00 +0100820 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700821 stream, volume, output);
Eric Laurenta553c252009-07-17 12:17:14 -0700822 mWaitWorkCV.signal();
Eric Laurent327c27b2009-08-27 00:48:47 -0700823 if (command->mWaitStatus) {
824 command->mCond.wait(mLock);
825 status = command->mStatus;
826 mWaitWorkCV.signal();
827 }
Eric Laurenta553c252009-07-17 12:17:14 -0700828 return status;
829}
830
Glenn Kasten39d00cb2012-01-17 11:09:42 -0800831status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700832 const char *keyValuePairs,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700833 int delayMs)
Eric Laurenta553c252009-07-17 12:17:14 -0700834{
Eric Laurent327c27b2009-08-27 00:48:47 -0700835 status_t status = NO_ERROR;
836
Eric Laurenta553c252009-07-17 12:17:14 -0700837 AudioCommand *command = new AudioCommand();
838 command->mCommand = SET_PARAMETERS;
839 ParametersData *data = new ParametersData();
840 data->mIO = ioHandle;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700841 data->mKeyValuePairs = String8(keyValuePairs);
Eric Laurenta553c252009-07-17 12:17:14 -0700842 command->mParam = data;
Eric Laurent327c27b2009-08-27 00:48:47 -0700843 if (delayMs == 0) {
844 command->mWaitStatus = true;
845 } else {
846 command->mWaitStatus = false;
847 }
Eric Laurent415f3e22009-10-21 08:14:22 -0700848 Mutex::Autolock _l(mLock);
Eric Laurent327c27b2009-08-27 00:48:47 -0700849 insertCommand_l(command, delayMs);
Steve Block71f2cf12011-10-20 11:56:00 +0100850 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700851 keyValuePairs, ioHandle, delayMs);
Eric Laurenta553c252009-07-17 12:17:14 -0700852 mWaitWorkCV.signal();
Eric Laurent327c27b2009-08-27 00:48:47 -0700853 if (command->mWaitStatus) {
854 command->mCond.wait(mLock);
855 status = command->mStatus;
856 mWaitWorkCV.signal();
857 }
Eric Laurenta553c252009-07-17 12:17:14 -0700858 return status;
859}
860
Eric Laurent415f3e22009-10-21 08:14:22 -0700861status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
862{
863 status_t status = NO_ERROR;
864
865 AudioCommand *command = new AudioCommand();
866 command->mCommand = SET_VOICE_VOLUME;
867 VoiceVolumeData *data = new VoiceVolumeData();
868 data->mVolume = volume;
869 command->mParam = data;
870 if (delayMs == 0) {
871 command->mWaitStatus = true;
872 } else {
873 command->mWaitStatus = false;
874 }
875 Mutex::Autolock _l(mLock);
876 insertCommand_l(command, delayMs);
Steve Block71f2cf12011-10-20 11:56:00 +0100877 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Eric Laurent415f3e22009-10-21 08:14:22 -0700878 mWaitWorkCV.signal();
879 if (command->mWaitStatus) {
880 command->mCond.wait(mLock);
881 status = command->mStatus;
882 mWaitWorkCV.signal();
883 }
884 return status;
885}
886
Eric Laurent327c27b2009-08-27 00:48:47 -0700887// insertCommand_l() must be called with mLock held
888void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
889{
Glenn Kasten150d2382012-02-08 14:04:28 -0800890 ssize_t i; // not size_t because i will count down to -1
Eric Laurent327c27b2009-08-27 00:48:47 -0700891 Vector <AudioCommand *> removedCommands;
892
893 command->mTime = systemTime() + milliseconds(delayMs);
894
Eric Laurentcef3cd72009-12-10 01:03:50 -0800895 // acquire wake lock to make sure delayed commands are processed
896 if (mName != "" && mAudioCommands.isEmpty()) {
897 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
898 }
899
Eric Laurent327c27b2009-08-27 00:48:47 -0700900 // check same pending commands with later time stamps and eliminate them
901 for (i = mAudioCommands.size()-1; i >= 0; i--) {
902 AudioCommand *command2 = mAudioCommands[i];
903 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
904 if (command2->mTime <= command->mTime) break;
905 if (command2->mCommand != command->mCommand) continue;
906
907 switch (command->mCommand) {
908 case SET_PARAMETERS: {
909 ParametersData *data = (ParametersData *)command->mParam;
910 ParametersData *data2 = (ParametersData *)command2->mParam;
911 if (data->mIO != data2->mIO) break;
Steve Block71f2cf12011-10-20 11:56:00 +0100912 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700913 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Eric Laurent327c27b2009-08-27 00:48:47 -0700914 AudioParameter param = AudioParameter(data->mKeyValuePairs);
915 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
916 for (size_t j = 0; j < param.size(); j++) {
917 String8 key;
918 String8 value;
919 param.getAt(j, key, value);
920 for (size_t k = 0; k < param2.size(); k++) {
921 String8 key2;
922 String8 value2;
923 param2.getAt(k, key2, value2);
924 if (key2 == key) {
925 param2.remove(key2);
Steve Block71f2cf12011-10-20 11:56:00 +0100926 ALOGV("Filtering out parameter %s", key2.string());
Eric Laurent327c27b2009-08-27 00:48:47 -0700927 break;
928 }
929 }
930 }
931 // if all keys have been filtered out, remove the command.
932 // otherwise, update the key value pairs
933 if (param2.size() == 0) {
934 removedCommands.add(command2);
935 } else {
936 data2->mKeyValuePairs = param2.toString();
937 }
938 } break;
939
940 case SET_VOLUME: {
941 VolumeData *data = (VolumeData *)command->mParam;
942 VolumeData *data2 = (VolumeData *)command2->mParam;
943 if (data->mIO != data2->mIO) break;
944 if (data->mStream != data2->mStream) break;
Steve Block71f2cf12011-10-20 11:56:00 +0100945 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700946 data->mIO, data->mStream);
Eric Laurent327c27b2009-08-27 00:48:47 -0700947 removedCommands.add(command2);
948 } break;
949 case START_TONE:
950 case STOP_TONE:
951 default:
952 break;
953 }
954 }
955
956 // remove filtered commands
957 for (size_t j = 0; j < removedCommands.size(); j++) {
958 // removed commands always have time stamps greater than current command
959 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
960 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block71f2cf12011-10-20 11:56:00 +0100961 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Eric Laurent327c27b2009-08-27 00:48:47 -0700962 mAudioCommands.removeAt(k);
963 break;
964 }
965 }
966 }
967 removedCommands.clear();
968
969 // insert command at the right place according to its time stamp
Steve Block71f2cf12011-10-20 11:56:00 +0100970 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700971 command->mCommand, (int)i+1, mAudioCommands.size());
Eric Laurent327c27b2009-08-27 00:48:47 -0700972 mAudioCommands.insertAt(command, i + 1);
973}
974
Eric Laurenta553c252009-07-17 12:17:14 -0700975void AudioPolicyService::AudioCommandThread::exit()
976{
Steve Block71f2cf12011-10-20 11:56:00 +0100977 ALOGV("AudioCommandThread::exit");
Eric Laurenta553c252009-07-17 12:17:14 -0700978 {
979 AutoMutex _l(mLock);
980 requestExit();
981 mWaitWorkCV.signal();
982 }
983 requestExitAndWait();
984}
985
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800986void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
987{
Eric Laurent3fdb1262009-11-07 00:01:32 -0800988 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800989 mCommand,
990 (int)ns2s(mTime),
991 (int)ns2ms(mTime)%1000,
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800992 mWaitStatus,
993 mParam);
994}
995
Dima Zavin24fc2fb2011-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 Kasten39d00cb2012-01-17 11:09:42 -08001001 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavin24fc2fb2011-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 Kastenbc1d77b2012-01-12 16:38:12 -08001010 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten39d00cb2012-01-17 11:09:42 -08001011 output, delayMs);
Dima Zavin24fc2fb2011-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 Block3762c312012-01-06 19:20:56 +00001018 ALOGE("startTone: illegal tone requested (%d)", tone);
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001019 if (stream != AUDIO_STREAM_VOICE_CALL)
Steve Block3762c312012-01-06 19:20:56 +00001020 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001021 tone);
1022 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 Laurent464d5b32011-06-17 21:29:58 -07001038// ----------------------------------------------------------------------------
1039// Audio pre-processing configuration
1040// ----------------------------------------------------------------------------
1041
Glenn Kasten05d33102012-01-09 08:41:22 -08001042/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent464d5b32011-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 Block71f2cf12011-10-20 11:56:00 +01001059 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent464d5b32011-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 Block71f2cf12011-10-20 11:56:00 +01001092 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent464d5b32011-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 Block71f2cf12011-10-20 11:56:00 +01001097 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent464d5b32011-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 Block71f2cf12011-10-20 11:56:00 +01001102 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent464d5b32011-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 Block71f2cf12011-10-20 11:56:00 +01001111 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent464d5b32011-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 Block71f2cf12011-10-20 11:56:00 +01001122 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent464d5b32011-06-17 21:29:58 -07001123 return len;
1124 }
Steve Block8564c8d2012-01-05 23:22:43 +00001125 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent464d5b32011-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 Kasten3694ec12012-01-27 16:47:15 -08001142 if (param != NULL) {
Eric Laurent464d5b32011-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 Block8564c8d2012-01-05 23:22:43 +00001146 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent464d5b32011-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 Block8564c8d2012-01-05 23:22:43 +00001155 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent464d5b32011-06-17 21:29:58 -07001156 goto error;
1157 }
1158
1159 fx_param->psize = 0;
1160 param = param->first_child;
1161 while (param) {
Steve Block71f2cf12011-10-20 11:56:00 +01001162 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent464d5b32011-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 Block71f2cf12011-10-20 11:56:00 +01001177 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent464d5b32011-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 Block71f2cf12011-10-20 11:56:00 +01001197 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent464d5b32011-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 Block8564c8d2012-01-05 23:22:43 +00001214 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent464d5b32011-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 Block71f2cf12011-10-20 11:56:00 +01001222 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent464d5b32011-06-17 21:29:58 -07001223 break;
1224 }
1225 }
1226 if (i == effects.size()) {
Steve Block71f2cf12011-10-20 11:56:00 +01001227 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent464d5b32011-06-17 21:29:58 -07001228 node = node->next;
1229 continue;
1230 }
Glenn Kastenafe98332012-02-02 14:04:37 -08001231 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent464d5b32011-06-17 21:29:58 -07001232 loadEffectParameters(node, effect->mParams);
Steve Block71f2cf12011-10-20 11:56:00 +01001233 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent464d5b32011-06-17 21:29:58 -07001234 source->mEffects.add(effect);
1235 node = node->next;
1236 }
1237 if (source->mEffects.size() == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00001238 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent464d5b32011-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 Block8564c8d2012-01-05 23:22:43 +00001255 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent464d5b32011-06-17 21:29:58 -07001256 node = node->next;
1257 continue;
1258 }
Steve Block71f2cf12011-10-20 11:56:00 +01001259 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent464d5b32011-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 Block8564c8d2012-01-05 23:22:43 +00001279 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent464d5b32011-06-17 21:29:58 -07001280 return NULL;
1281 }
Glenn Kastenafe98332012-02-02 14:04:37 -08001282 return new EffectDesc(root->name, uuid);
Eric Laurent464d5b32011-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 Block71f2cf12011-10-20 11:56:00 +01001293 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent464d5b32011-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 Zavin24fc2fb2011-04-19 22:30:36 -07001328/* implementation of the interface to the policy manager */
1329extern "C" {
1330
1331static audio_io_handle_t aps_open_output(void *service,
1332 uint32_t *pDevices,
1333 uint32_t *pSamplingRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08001334 audio_format_t *pFormat,
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001335 uint32_t *pChannels,
1336 uint32_t *pLatencyMs,
1337 audio_policy_output_flags_t flags)
1338{
1339 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten706b6182012-01-20 13:44:40 -08001340 if (af == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00001341 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001342 return 0;
1343 }
1344
1345 return af->openOutput(pDevices, pSamplingRate, pFormat, pChannels,
1346 pLatencyMs, flags);
1347}
1348
1349static audio_io_handle_t aps_open_dup_output(void *service,
1350 audio_io_handle_t output1,
1351 audio_io_handle_t output2)
1352{
1353 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten706b6182012-01-20 13:44:40 -08001354 if (af == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00001355 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001356 return 0;
1357 }
1358 return af->openDuplicateOutput(output1, output2);
1359}
1360
1361static int aps_close_output(void *service, audio_io_handle_t output)
1362{
1363 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten706b6182012-01-20 13:44:40 -08001364 if (af == 0)
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001365 return PERMISSION_DENIED;
1366
1367 return af->closeOutput(output);
1368}
1369
1370static int aps_suspend_output(void *service, audio_io_handle_t output)
1371{
1372 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten706b6182012-01-20 13:44:40 -08001373 if (af == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00001374 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001375 return PERMISSION_DENIED;
1376 }
1377
1378 return af->suspendOutput(output);
1379}
1380
1381static int aps_restore_output(void *service, audio_io_handle_t output)
1382{
1383 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten706b6182012-01-20 13:44:40 -08001384 if (af == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00001385 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001386 return PERMISSION_DENIED;
1387 }
1388
1389 return af->restoreOutput(output);
1390}
1391
1392static audio_io_handle_t aps_open_input(void *service,
1393 uint32_t *pDevices,
1394 uint32_t *pSamplingRate,
Glenn Kasten0a204ed2012-01-12 12:27:51 -08001395 audio_format_t *pFormat,
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001396 uint32_t *pChannels,
Glenn Kasten882c0a22012-01-27 12:32:34 -08001397 audio_in_acoustics_t acoustics)
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001398{
1399 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten706b6182012-01-20 13:44:40 -08001400 if (af == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00001401 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001402 return 0;
1403 }
1404
1405 return af->openInput(pDevices, pSamplingRate, pFormat, pChannels,
1406 acoustics);
1407}
1408
1409static int aps_close_input(void *service, audio_io_handle_t input)
1410{
1411 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten706b6182012-01-20 13:44:40 -08001412 if (af == 0)
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001413 return PERMISSION_DENIED;
1414
1415 return af->closeInput(input);
1416}
1417
1418static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1419 audio_io_handle_t output)
1420{
1421 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten706b6182012-01-20 13:44:40 -08001422 if (af == 0)
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001423 return PERMISSION_DENIED;
1424
1425 return af->setStreamOutput(stream, output);
1426}
1427
1428static int aps_move_effects(void *service, int session,
1429 audio_io_handle_t src_output,
1430 audio_io_handle_t dst_output)
1431{
1432 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten706b6182012-01-20 13:44:40 -08001433 if (af == 0)
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001434 return PERMISSION_DENIED;
1435
Glenn Kasten39d00cb2012-01-17 11:09:42 -08001436 return af->moveEffects(session, src_output, dst_output);
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001437}
1438
1439static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1440 const char *keys)
1441{
1442 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1443 return strdup(result.string());
1444}
1445
1446static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1447 const char *kv_pairs, int delay_ms)
1448{
1449 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1450
1451 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1452}
1453
1454static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1455 float volume, audio_io_handle_t output,
1456 int delay_ms)
1457{
1458 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1459
1460 return audioPolicyService->setStreamVolume(stream, volume, output,
1461 delay_ms);
1462}
1463
1464static int aps_start_tone(void *service, audio_policy_tone_t tone,
1465 audio_stream_type_t stream)
1466{
1467 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1468
1469 return audioPolicyService->startTone(tone, stream);
1470}
1471
1472static int aps_stop_tone(void *service)
1473{
1474 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1475
1476 return audioPolicyService->stopTone();
1477}
1478
1479static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1480{
1481 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1482
1483 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1484}
1485
1486}; // extern "C"
1487
1488namespace {
1489 struct audio_policy_service_ops aps_ops = {
1490 open_output : aps_open_output,
1491 open_duplicate_output : aps_open_dup_output,
1492 close_output : aps_close_output,
1493 suspend_output : aps_suspend_output,
1494 restore_output : aps_restore_output,
1495 open_input : aps_open_input,
1496 close_input : aps_close_input,
1497 set_stream_volume : aps_set_stream_volume,
1498 set_stream_output : aps_set_stream_output,
1499 set_parameters : aps_set_parameters,
1500 get_parameters : aps_get_parameters,
1501 start_tone : aps_start_tone,
1502 stop_tone : aps_stop_tone,
1503 set_voice_volume : aps_set_voice_volume,
1504 move_effects : aps_move_effects,
1505 };
1506}; // namespace <unnamed>
1507
Eric Laurenta553c252009-07-17 12:17:14 -07001508}; // namespace android