blob: 388ad6c74fd8a4bf96d053424d334036d9c86461 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
Mingming Yin84f109b2012-12-27 17:43:05 -08003 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
4 *
5 * Not a Contribution, Apache license notifications and license are retained
6 * for attribution purposes only
Mathias Agopian65ab4712010-07-14 17:59:35 -07007 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#define LOG_TAG "AudioPolicyService"
22//#define LOG_NDEBUG 0
23
24#undef __STRICT_ANSI__
25#define __STDINT_LIMITS
26#define __STDC_LIMIT_MACROS
27#include <stdint.h>
28
29#include <sys/time.h>
30#include <binder/IServiceManager.h>
31#include <utils/Log.h>
32#include <cutils/properties.h>
33#include <binder/IPCThreadState.h>
34#include <utils/String16.h>
35#include <utils/threads.h>
36#include "AudioPolicyService.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080037#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070038#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070039#include <media/AudioEffect.h>
40#include <media/EffectsFactoryApi.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070041
Dima Zavinfce7a472011-04-19 22:30:36 -070042#include <hardware/hardware.h>
Dima Zavin64760242011-05-11 14:15:23 -070043#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070044#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070045#include <hardware/audio_policy.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070046#include <audio_effects/audio_effects_conf.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070047
Mathias Agopian65ab4712010-07-14 17:59:35 -070048namespace android {
49
Glenn Kasten8dad0e32012-01-09 08:41:22 -080050static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
51static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
53static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080054static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070055
Dima Zavinfce7a472011-04-19 22:30:36 -070056namespace {
57 extern struct audio_policy_service_ops aps_ops;
58};
59
Mathias Agopian65ab4712010-07-14 17:59:35 -070060// ----------------------------------------------------------------------------
61
62AudioPolicyService::AudioPolicyService()
Dima Zavinfce7a472011-04-19 22:30:36 -070063 : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL)
Mathias Agopian65ab4712010-07-14 17:59:35 -070064{
65 char value[PROPERTY_VALUE_MAX];
Dima Zavinfce7a472011-04-19 22:30:36 -070066 const struct hw_module_t *module;
67 int forced_val;
68 int rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -070069
Eric Laurent93575202011-01-18 18:39:02 -080070 Mutex::Autolock _l(mLock);
71
Mathias Agopian65ab4712010-07-14 17:59:35 -070072 // start tone playback thread
73 mTonePlaybackThread = new AudioCommandThread(String8(""));
74 // start audio commands thread
Glenn Kasten480b4682012-02-28 12:30:08 -080075 mAudioCommandThread = new AudioCommandThread(String8("ApmCommand"));
Mathias Agopian65ab4712010-07-14 17:59:35 -070076
Dima Zavinfce7a472011-04-19 22:30:36 -070077 /* instantiate the audio policy manager */
78 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
79 if (rc)
80 return;
Mathias Agopian65ab4712010-07-14 17:59:35 -070081
Dima Zavinfce7a472011-04-19 22:30:36 -070082 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
Steve Block29357bc2012-01-06 19:20:56 +000083 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070084 if (rc)
85 return;
Eric Laurent93575202011-01-18 18:39:02 -080086
Dima Zavinfce7a472011-04-19 22:30:36 -070087 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
88 &mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000089 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070090 if (rc)
91 return;
92
93 rc = mpAudioPolicy->init_check(mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000094 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070095 if (rc)
96 return;
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);
Ricardo Cerqueira59199b12012-07-11 23:39:44 +0100395
396#ifndef ICS_AUDIO_BLOB
Eric Laurent83844cc2011-11-18 16:43:31 -0800397 if (mpAudioPolicy->set_stream_volume_index_for_device) {
398 return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
399 stream,
400 index,
401 device);
Ricardo Cerqueira59199b12012-07-11 23:39:44 +0100402 } else
403#endif
404 {
Eric Laurent83844cc2011-11-18 16:43:31 -0800405 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
406 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700407}
408
Eric Laurent83844cc2011-11-18 16:43:31 -0800409status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
410 int *index,
411 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700412{
Dima Zavinfce7a472011-04-19 22:30:36 -0700413 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700414 return NO_INIT;
415 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800416 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700417 return BAD_VALUE;
418 }
Eric Laurent5f121362012-06-15 14:45:03 -0700419 Mutex::Autolock _l(mLock);
Ricardo Cerqueira59199b12012-07-11 23:39:44 +0100420#ifndef ICS_AUDIO_BLOB
Eric Laurent83844cc2011-11-18 16:43:31 -0800421 if (mpAudioPolicy->get_stream_volume_index_for_device) {
422 return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
423 stream,
424 index,
425 device);
Ricardo Cerqueira59199b12012-07-11 23:39:44 +0100426 } else
427#endif
428 {
Eric Laurent83844cc2011-11-18 16:43:31 -0800429 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
430 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700431}
432
Dima Zavinfce7a472011-04-19 22:30:36 -0700433uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700434{
Dima Zavinfce7a472011-04-19 22:30:36 -0700435 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700436 return 0;
437 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700438 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurentde070132010-07-13 04:45:46 -0700439}
440
Eric Laurent63742522012-03-08 13:42:42 -0800441//audio policy: use audio_device_t appropriately
442
443audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800444{
Dima Zavinfce7a472011-04-19 22:30:36 -0700445 if (mpAudioPolicy == NULL) {
Eric Laurent63742522012-03-08 13:42:42 -0800446 return (audio_devices_t)0;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800447 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700448 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800449}
450
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700451audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700452{
Dima Zavinfce7a472011-04-19 22:30:36 -0700453 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700454 return NO_INIT;
455 }
456 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700457 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700458}
459
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700460status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700461 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700462 uint32_t strategy,
463 int session,
464 int id)
465{
Dima Zavinfce7a472011-04-19 22:30:36 -0700466 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700467 return NO_INIT;
468 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700469 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700470}
471
472status_t AudioPolicyService::unregisterEffect(int id)
473{
Dima Zavinfce7a472011-04-19 22:30:36 -0700474 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700475 return NO_INIT;
476 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700477 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700478}
479
Eric Laurentdb7c0792011-08-10 10:37:50 -0700480status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
481{
482 if (mpAudioPolicy == NULL) {
483 return NO_INIT;
484 }
485 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
486}
487
Glenn Kastenfff6d712012-01-12 16:38:12 -0800488bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800489{
Dima Zavinfce7a472011-04-19 22:30:36 -0700490 if (mpAudioPolicy == NULL) {
Eric Laurenteda6c362011-02-02 09:33:30 -0800491 return 0;
492 }
493 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700494 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800495}
496
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700497bool AudioPolicyService::isSourceActive(audio_source_t source) const
498{
499 if (mpAudioPolicy == NULL) {
500 return false;
501 }
502 if (mpAudioPolicy->is_source_active == 0) {
503 return false;
504 }
505 Mutex::Autolock _l(mLock);
506 return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
507}
508
Eric Laurent57dae992011-07-24 13:36:09 -0700509status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
510 effect_descriptor_t *descriptors,
511 uint32_t *count)
512{
513
514 if (mpAudioPolicy == NULL) {
515 *count = 0;
516 return NO_INIT;
517 }
518 Mutex::Autolock _l(mLock);
519 status_t status = NO_ERROR;
520
521 size_t index;
522 for (index = 0; index < mInputs.size(); index++) {
523 if (mInputs.valueAt(index)->mSessionId == audioSession) {
524 break;
525 }
526 }
527 if (index == mInputs.size()) {
528 *count = 0;
529 return BAD_VALUE;
530 }
531 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
532
533 for (size_t i = 0; i < effects.size(); i++) {
534 effect_descriptor_t desc = effects[i]->descriptor();
535 if (i < *count) {
Glenn Kastena189a682012-02-20 12:16:30 -0800536 descriptors[i] = desc;
Eric Laurent57dae992011-07-24 13:36:09 -0700537 }
538 }
539 if (effects.size() > *count) {
540 status = NO_MEMORY;
541 }
542 *count = effects.size();
543 return status;
544}
545
Mathias Agopian65ab4712010-07-14 17:59:35 -0700546void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten23d82a92012-02-03 11:10:00 -0800547 ALOGW("binderDied() %p, tid %d, calling pid %d", who.unsafe_get(), gettid(),
Eric Laurentde070132010-07-13 04:45:46 -0700548 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700549}
550
551static bool tryLock(Mutex& mutex)
552{
553 bool locked = false;
554 for (int i = 0; i < kDumpLockRetries; ++i) {
555 if (mutex.tryLock() == NO_ERROR) {
556 locked = true;
557 break;
558 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800559 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700560 }
561 return locked;
562}
563
564status_t AudioPolicyService::dumpInternals(int fd)
565{
566 const size_t SIZE = 256;
567 char buffer[SIZE];
568 String8 result;
569
Dima Zavinfce7a472011-04-19 22:30:36 -0700570 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700571 result.append(buffer);
572 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
573 result.append(buffer);
574 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
575 result.append(buffer);
576
577 write(fd, result.string(), result.size());
578 return NO_ERROR;
579}
580
581status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
582{
Glenn Kasten44deb052012-02-05 18:09:08 -0800583 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700584 dumpPermissionDenial(fd);
585 } else {
586 bool locked = tryLock(mLock);
587 if (!locked) {
588 String8 result(kDeadlockedString);
589 write(fd, result.string(), result.size());
590 }
591
592 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800593 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700594 mAudioCommandThread->dump(fd);
595 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800596 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700597 mTonePlaybackThread->dump(fd);
598 }
599
Dima Zavinfce7a472011-04-19 22:30:36 -0700600 if (mpAudioPolicy) {
601 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700602 }
603
604 if (locked) mLock.unlock();
605 }
606 return NO_ERROR;
607}
608
609status_t AudioPolicyService::dumpPermissionDenial(int fd)
610{
611 const size_t SIZE = 256;
612 char buffer[SIZE];
613 String8 result;
614 snprintf(buffer, SIZE, "Permission Denial: "
615 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
616 IPCThreadState::self()->getCallingPid(),
617 IPCThreadState::self()->getCallingUid());
618 result.append(buffer);
619 write(fd, result.string(), result.size());
620 return NO_ERROR;
621}
622
Glenn Kasten81872a22012-03-07 16:49:22 -0800623void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700624{
Glenn Kasten81872a22012-03-07 16:49:22 -0800625 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700626 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800627 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700628 }
629}
630
Mathias Agopian65ab4712010-07-14 17:59:35 -0700631status_t AudioPolicyService::onTransact(
632 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
633{
634 return BnAudioPolicyService::onTransact(code, data, reply, flags);
635}
636
637
Mathias Agopian65ab4712010-07-14 17:59:35 -0700638// ----------- AudioPolicyService::AudioCommandThread implementation ----------
639
640AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name)
641 : Thread(false), mName(name)
642{
643 mpToneGenerator = NULL;
644}
645
646
647AudioPolicyService::AudioCommandThread::~AudioCommandThread()
648{
649 if (mName != "" && !mAudioCommands.isEmpty()) {
650 release_wake_lock(mName.string());
651 }
652 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800653 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700654}
655
656void AudioPolicyService::AudioCommandThread::onFirstRef()
657{
658 if (mName != "") {
659 run(mName.string(), ANDROID_PRIORITY_AUDIO);
660 } else {
Glenn Kasten480b4682012-02-28 12:30:08 -0800661 run("AudioCommand", ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700662 }
663}
664
665bool AudioPolicyService::AudioCommandThread::threadLoop()
666{
667 nsecs_t waitTime = INT64_MAX;
668
669 mLock.lock();
670 while (!exitPending())
671 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700672 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700673 nsecs_t curTime = systemTime();
674 // commands are sorted by increasing time stamp: execute them from index 0 and up
675 if (mAudioCommands[0]->mTime <= curTime) {
676 AudioCommand *command = mAudioCommands[0];
677 mAudioCommands.removeAt(0);
678 mLastCommand = *command;
679
680 switch (command->mCommand) {
681 case START_TONE: {
682 mLock.unlock();
683 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100684 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800686 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700687 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
688 mpToneGenerator->startTone(data->mType);
689 delete data;
690 mLock.lock();
691 }break;
692 case STOP_TONE: {
693 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100694 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700695 if (mpToneGenerator != NULL) {
696 mpToneGenerator->stopTone();
697 delete mpToneGenerator;
698 mpToneGenerator = NULL;
699 }
700 mLock.lock();
701 }break;
702 case SET_VOLUME: {
703 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100704 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700705 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
706 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
707 data->mVolume,
708 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700709 if (command->mWaitStatus) {
710 command->mCond.signal();
711 mWaitWorkCV.wait(mLock);
712 }
713 delete data;
714 }break;
715 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700716 ParametersData *data = (ParametersData *)command->mParam;
717 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
718 data->mKeyValuePairs.string(), data->mIO);
719 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
720 if (command->mWaitStatus) {
721 command->mCond.signal();
722 mWaitWorkCV.wait(mLock);
723 }
724 delete data;
725 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700726 case SET_VOICE_VOLUME: {
727 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100728 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700729 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700730 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
731 if (command->mWaitStatus) {
732 command->mCond.signal();
733 mWaitWorkCV.wait(mLock);
734 }
735 delete data;
736 }break;
Mingming Yin84f109b2012-12-27 17:43:05 -0800737#ifdef QCOM_FM_ENABLED
738 case SET_FM_VOLUME: {
739 FmVolumeData *data = (FmVolumeData *)command->mParam;
740 ALOGV("AudioCommandThread() processing set fm volume volume %f", data->mVolume);
741 command->mStatus = AudioSystem::setFmVolume(data->mVolume);
742 if (command->mWaitStatus) {
743 command->mCond.signal();
744 mWaitWorkCV.wait(mLock);
745 }
746 delete data;
747 }break;
748#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -0700749 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000750 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700751 }
752 delete command;
753 waitTime = INT64_MAX;
754 } else {
755 waitTime = mAudioCommands[0]->mTime - curTime;
756 break;
757 }
758 }
759 // release delayed commands wake lock
760 if (mName != "" && mAudioCommands.isEmpty()) {
761 release_wake_lock(mName.string());
762 }
Steve Block3856b092011-10-20 11:56:00 +0100763 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700764 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100765 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700766 }
767 mLock.unlock();
768 return false;
769}
770
771status_t AudioPolicyService::AudioCommandThread::dump(int fd)
772{
773 const size_t SIZE = 256;
774 char buffer[SIZE];
775 String8 result;
776
777 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
778 result.append(buffer);
779 write(fd, result.string(), result.size());
780
781 bool locked = tryLock(mLock);
782 if (!locked) {
783 String8 result2(kCmdDeadlockedString);
784 write(fd, result2.string(), result2.size());
785 }
786
787 snprintf(buffer, SIZE, "- Commands:\n");
788 result = String8(buffer);
789 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800790 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700791 mAudioCommands[i]->dump(buffer, SIZE);
792 result.append(buffer);
793 }
794 result.append(" Last Command\n");
795 mLastCommand.dump(buffer, SIZE);
796 result.append(buffer);
797
798 write(fd, result.string(), result.size());
799
800 if (locked) mLock.unlock();
801
802 return NO_ERROR;
803}
804
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800805void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
806 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700807{
808 AudioCommand *command = new AudioCommand();
809 command->mCommand = START_TONE;
810 ToneData *data = new ToneData();
811 data->mType = type;
812 data->mStream = stream;
813 command->mParam = (void *)data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814 Mutex::Autolock _l(mLock);
815 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100816 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700817 mWaitWorkCV.signal();
818}
819
820void AudioPolicyService::AudioCommandThread::stopToneCommand()
821{
822 AudioCommand *command = new AudioCommand();
823 command->mCommand = STOP_TONE;
824 command->mParam = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700825 Mutex::Autolock _l(mLock);
826 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100827 ALOGV("AudioCommandThread() adding tone stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700828 mWaitWorkCV.signal();
829}
830
Glenn Kastenfff6d712012-01-12 16:38:12 -0800831status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700832 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800833 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700834 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700835{
836 status_t status = NO_ERROR;
837
838 AudioCommand *command = new AudioCommand();
839 command->mCommand = SET_VOLUME;
840 VolumeData *data = new VolumeData();
841 data->mStream = stream;
842 data->mVolume = volume;
843 data->mIO = output;
844 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700845 Mutex::Autolock _l(mLock);
846 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100847 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700848 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700849 mWaitWorkCV.signal();
850 if (command->mWaitStatus) {
851 command->mCond.wait(mLock);
852 status = command->mStatus;
853 mWaitWorkCV.signal();
854 }
855 return status;
856}
857
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800858status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700859 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700860 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700861{
862 status_t status = NO_ERROR;
863
864 AudioCommand *command = new AudioCommand();
865 command->mCommand = SET_PARAMETERS;
866 ParametersData *data = new ParametersData();
867 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700868 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700869 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700870 Mutex::Autolock _l(mLock);
871 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100872 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700873 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700874 mWaitWorkCV.signal();
875 if (command->mWaitStatus) {
876 command->mCond.wait(mLock);
877 status = command->mStatus;
878 mWaitWorkCV.signal();
879 }
880 return status;
881}
882
883status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
884{
885 status_t status = NO_ERROR;
886
887 AudioCommand *command = new AudioCommand();
888 command->mCommand = SET_VOICE_VOLUME;
889 VoiceVolumeData *data = new VoiceVolumeData();
890 data->mVolume = volume;
891 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700892 Mutex::Autolock _l(mLock);
893 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100894 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700895 mWaitWorkCV.signal();
896 if (command->mWaitStatus) {
897 command->mCond.wait(mLock);
898 status = command->mStatus;
899 mWaitWorkCV.signal();
900 }
901 return status;
902}
903
Mingming Yin84f109b2012-12-27 17:43:05 -0800904#ifdef QCOM_FM_ENABLED
905status_t AudioPolicyService::AudioCommandThread::fmVolumeCommand(float volume, int delayMs)
906{
907 status_t status = NO_ERROR;
908
909 AudioCommand *command = new AudioCommand();
910 command->mCommand = SET_FM_VOLUME;
911 FmVolumeData *data = new FmVolumeData();
912 data->mVolume = volume;
913 command->mParam = data;
914 if (delayMs == 0) {
915 command->mWaitStatus = true;
916 } else {
917 command->mWaitStatus = false;
918 }
919 Mutex::Autolock _l(mLock);
920 insertCommand_l(command, delayMs);
921 ALOGV("AudioCommandThread() adding set fm volume volume %f", volume);
922 mWaitWorkCV.signal();
923 if (command->mWaitStatus) {
924 command->mCond.wait(mLock);
925 status = command->mStatus;
926 mWaitWorkCV.signal();
927 }
928 return status;
929}
930#endif
931
Mathias Agopian65ab4712010-07-14 17:59:35 -0700932// insertCommand_l() must be called with mLock held
933void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
934{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800935 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700936 Vector <AudioCommand *> removedCommands;
937
Eric Laurentcec4abb2012-07-03 12:23:02 -0700938 nsecs_t time = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700939 command->mTime = systemTime() + milliseconds(delayMs);
940
941 // acquire wake lock to make sure delayed commands are processed
942 if (mName != "" && mAudioCommands.isEmpty()) {
943 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
944 }
945
946 // check same pending commands with later time stamps and eliminate them
947 for (i = mAudioCommands.size()-1; i >= 0; i--) {
948 AudioCommand *command2 = mAudioCommands[i];
949 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
950 if (command2->mTime <= command->mTime) break;
951 if (command2->mCommand != command->mCommand) continue;
952
953 switch (command->mCommand) {
954 case SET_PARAMETERS: {
955 ParametersData *data = (ParametersData *)command->mParam;
956 ParametersData *data2 = (ParametersData *)command2->mParam;
957 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100958 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700959 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960 AudioParameter param = AudioParameter(data->mKeyValuePairs);
961 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
962 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700963 String8 key;
964 String8 value;
965 param.getAt(j, key, value);
966 for (size_t k = 0; k < param2.size(); k++) {
967 String8 key2;
968 String8 value2;
969 param2.getAt(k, key2, value2);
970 if (key2 == key) {
971 param2.remove(key2);
972 ALOGV("Filtering out parameter %s", key2.string());
973 break;
974 }
975 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700976 }
977 // if all keys have been filtered out, remove the command.
978 // otherwise, update the key value pairs
979 if (param2.size() == 0) {
980 removedCommands.add(command2);
981 } else {
982 data2->mKeyValuePairs = param2.toString();
983 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700984 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700985 } break;
986
987 case SET_VOLUME: {
988 VolumeData *data = (VolumeData *)command->mParam;
989 VolumeData *data2 = (VolumeData *)command2->mParam;
990 if (data->mIO != data2->mIO) break;
991 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100992 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700993 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700994 removedCommands.add(command2);
Eric Laurentcec4abb2012-07-03 12:23:02 -0700995 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700996 } break;
Mingming Yin84f109b2012-12-27 17:43:05 -0800997#ifdef QCOM_FM_ENABLED
998 case SET_FM_VOLUME: {
999 removedCommands.add(command2);
1000 time = command2->mTime;
1001 } break;
1002#endif
Mathias Agopian65ab4712010-07-14 17:59:35 -07001003 case START_TONE:
1004 case STOP_TONE:
1005 default:
1006 break;
1007 }
1008 }
1009
1010 // remove filtered commands
1011 for (size_t j = 0; j < removedCommands.size(); j++) {
1012 // removed commands always have time stamps greater than current command
1013 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
1014 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +01001015 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001016 mAudioCommands.removeAt(k);
1017 break;
1018 }
1019 }
1020 }
1021 removedCommands.clear();
1022
Eric Laurentcec4abb2012-07-03 12:23:02 -07001023 // wait for status only if delay is 0 and command time was not modified above
1024 if (delayMs == 0 && time == 0) {
1025 command->mWaitStatus = true;
1026 } else {
1027 command->mWaitStatus = false;
1028 }
1029 // update command time if modified above
1030 if (time != 0) {
1031 command->mTime = time;
1032 }
1033
Mathias Agopian65ab4712010-07-14 17:59:35 -07001034 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +01001035 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -07001036 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001037 mAudioCommands.insertAt(command, i + 1);
1038}
1039
1040void AudioPolicyService::AudioCommandThread::exit()
1041{
Steve Block3856b092011-10-20 11:56:00 +01001042 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001043 {
1044 AutoMutex _l(mLock);
1045 requestExit();
1046 mWaitWorkCV.signal();
1047 }
1048 requestExitAndWait();
1049}
1050
1051void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1052{
1053 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1054 mCommand,
1055 (int)ns2s(mTime),
1056 (int)ns2ms(mTime)%1000,
1057 mWaitStatus,
1058 mParam);
1059}
1060
Dima Zavinfce7a472011-04-19 22:30:36 -07001061/******* helpers for the service_ops callbacks defined below *********/
1062void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1063 const char *keyValuePairs,
1064 int delayMs)
1065{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001066 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001067 delayMs);
1068}
1069
1070int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1071 float volume,
1072 audio_io_handle_t output,
1073 int delayMs)
1074{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001075 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001076 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001077}
1078
Mingming Yin84f109b2012-12-27 17:43:05 -08001079#ifdef QCOM_FM_ENABLED
1080status_t AudioPolicyService::setFmVolume(float volume, int delayMs)
1081{
1082 return mAudioCommandThread->fmVolumeCommand(volume, delayMs);
1083}
1084#endif
1085
Dima Zavinfce7a472011-04-19 22:30:36 -07001086int AudioPolicyService::startTone(audio_policy_tone_t tone,
1087 audio_stream_type_t stream)
1088{
1089 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
Steve Block29357bc2012-01-06 19:20:56 +00001090 ALOGE("startTone: illegal tone requested (%d)", tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001091 if (stream != AUDIO_STREAM_VOICE_CALL)
Steve Block29357bc2012-01-06 19:20:56 +00001092 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001093 tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001094 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1095 AUDIO_STREAM_VOICE_CALL);
1096 return 0;
1097}
1098
1099int AudioPolicyService::stopTone()
1100{
1101 mTonePlaybackThread->stopToneCommand();
1102 return 0;
1103}
1104
1105int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1106{
1107 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1108}
1109
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001110// ----------------------------------------------------------------------------
1111// Audio pre-processing configuration
1112// ----------------------------------------------------------------------------
1113
Glenn Kasten8dad0e32012-01-09 08:41:22 -08001114/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001115 MIC_SRC_TAG,
1116 VOICE_UL_SRC_TAG,
1117 VOICE_DL_SRC_TAG,
1118 VOICE_CALL_SRC_TAG,
1119 CAMCORDER_SRC_TAG,
1120 VOICE_REC_SRC_TAG,
1121 VOICE_COMM_SRC_TAG
1122};
1123
1124// returns the audio_source_t enum corresponding to the input source name or
1125// AUDIO_SOURCE_CNT is no match found
1126audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
1127{
1128 int i;
1129 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
1130 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001131 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001132 break;
1133 }
1134 }
1135 return (audio_source_t)i;
1136}
1137
1138size_t AudioPolicyService::growParamSize(char *param,
1139 size_t size,
1140 size_t *curSize,
1141 size_t *totSize)
1142{
1143 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
1144 size_t pos = ((*curSize - 1 ) / size + 1) * size;
1145
1146 if (pos + size > *totSize) {
1147 while (pos + size > *totSize) {
1148 *totSize += ((*totSize + 7) / 8) * 4;
1149 }
1150 param = (char *)realloc(param, *totSize);
1151 }
1152 *curSize = pos + size;
1153 return pos;
1154}
1155
1156size_t AudioPolicyService::readParamValue(cnode *node,
1157 char *param,
1158 size_t *curSize,
1159 size_t *totSize)
1160{
1161 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
1162 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
1163 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001164 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001165 return sizeof(short);
1166 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
1167 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
1168 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001169 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001170 return sizeof(int);
1171 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
1172 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
1173 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001174 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001175 return sizeof(float);
1176 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
1177 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
1178 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1179 *(bool *)((char *)param + pos) = false;
1180 } else {
1181 *(bool *)((char *)param + pos) = true;
1182 }
Steve Block3856b092011-10-20 11:56:00 +01001183 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001184 return sizeof(bool);
1185 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1186 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1187 if (*curSize + len + 1 > *totSize) {
1188 *totSize = *curSize + len + 1;
1189 param = (char *)realloc(param, *totSize);
1190 }
1191 strncpy(param + *curSize, node->value, len);
1192 *curSize += len;
1193 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +01001194 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001195 return len;
1196 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001197 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001198 return 0;
1199}
1200
1201effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1202{
1203 cnode *param;
1204 cnode *value;
1205 size_t curSize = sizeof(effect_param_t);
1206 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1207 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1208
1209 param = config_find(root, PARAM_TAG);
1210 value = config_find(root, VALUE_TAG);
1211 if (param == NULL && value == NULL) {
1212 // try to parse simple parameter form {int int}
1213 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -08001214 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001215 // Note: that a pair of random strings is read as 0 0
1216 int *ptr = (int *)fx_param->data;
1217 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +00001218 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001219 *ptr++ = atoi(param->name);
1220 *ptr = atoi(param->value);
1221 fx_param->psize = sizeof(int);
1222 fx_param->vsize = sizeof(int);
1223 return fx_param;
1224 }
1225 }
1226 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001227 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001228 goto error;
1229 }
1230
1231 fx_param->psize = 0;
1232 param = param->first_child;
1233 while (param) {
Steve Block3856b092011-10-20 11:56:00 +01001234 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001235 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1236 if (size == 0) {
1237 goto error;
1238 }
1239 fx_param->psize += size;
1240 param = param->next;
1241 }
1242
1243 // align start of value field on 32 bit boundary
1244 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1245
1246 fx_param->vsize = 0;
1247 value = value->first_child;
1248 while (value) {
Steve Block3856b092011-10-20 11:56:00 +01001249 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001250 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1251 if (size == 0) {
1252 goto error;
1253 }
1254 fx_param->vsize += size;
1255 value = value->next;
1256 }
1257
1258 return fx_param;
1259
1260error:
1261 delete fx_param;
1262 return NULL;
1263}
1264
1265void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1266{
1267 cnode *node = root->first_child;
1268 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001269 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001270 effect_param_t *param = loadEffectParameter(node);
1271 if (param == NULL) {
1272 node = node->next;
1273 continue;
1274 }
1275 params.add(param);
1276 node = node->next;
1277 }
1278}
1279
1280AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1281 cnode *root,
1282 const Vector <EffectDesc *>& effects)
1283{
1284 cnode *node = root->first_child;
1285 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001286 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001287 return NULL;
1288 }
1289 InputSourceDesc *source = new InputSourceDesc();
1290 while (node) {
1291 size_t i;
1292 for (i = 0; i < effects.size(); i++) {
1293 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001294 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001295 break;
1296 }
1297 }
1298 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +01001299 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001300 node = node->next;
1301 continue;
1302 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001303 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001304 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +01001305 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001306 source->mEffects.add(effect);
1307 node = node->next;
1308 }
1309 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001310 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001311 delete source;
1312 return NULL;
1313 }
1314 return source;
1315}
1316
1317status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1318{
1319 cnode *node = config_find(root, PREPROCESSING_TAG);
1320 if (node == NULL) {
1321 return -ENOENT;
1322 }
1323 node = node->first_child;
1324 while (node) {
1325 audio_source_t source = inputSourceNameToEnum(node->name);
1326 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001327 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001328 node = node->next;
1329 continue;
1330 }
Steve Block3856b092011-10-20 11:56:00 +01001331 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001332 InputSourceDesc *desc = loadInputSource(node, effects);
1333 if (desc == NULL) {
1334 node = node->next;
1335 continue;
1336 }
1337 mInputSources.add(source, desc);
1338 node = node->next;
1339 }
1340 return NO_ERROR;
1341}
1342
1343AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1344{
1345 cnode *node = config_find(root, UUID_TAG);
1346 if (node == NULL) {
1347 return NULL;
1348 }
1349 effect_uuid_t uuid;
1350 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001351 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001352 return NULL;
1353 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001354 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001355}
1356
1357status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1358{
1359 cnode *node = config_find(root, EFFECTS_TAG);
1360 if (node == NULL) {
1361 return -ENOENT;
1362 }
1363 node = node->first_child;
1364 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001365 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001366 EffectDesc *effect = loadEffect(node);
1367 if (effect == NULL) {
1368 node = node->next;
1369 continue;
1370 }
1371 effects.add(effect);
1372 node = node->next;
1373 }
1374 return NO_ERROR;
1375}
1376
1377status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1378{
1379 cnode *root;
1380 char *data;
1381
1382 data = (char *)load_file(path, NULL);
1383 if (data == NULL) {
1384 return -ENODEV;
1385 }
1386 root = config_node("", "");
1387 config_load(root, data);
1388
1389 Vector <EffectDesc *> effects;
1390 loadEffects(root, effects);
1391 loadInputSources(root, effects);
1392
1393 config_free(root);
1394 free(root);
1395 free(data);
1396
1397 return NO_ERROR;
1398}
1399
Dima Zavinfce7a472011-04-19 22:30:36 -07001400/* implementation of the interface to the policy manager */
1401extern "C" {
1402
Eric Laurenta4c5a552012-03-29 10:12:40 -07001403
1404static audio_module_handle_t aps_load_hw_module(void *service,
1405 const char *name)
Dima Zavinfce7a472011-04-19 22:30:36 -07001406{
1407 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001408 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001409 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001410 return 0;
1411 }
1412
Eric Laurenta4c5a552012-03-29 10:12:40 -07001413 return af->loadHwModule(name);
1414}
1415
1416// deprecated: replaced by aps_open_output_on_module()
1417static audio_io_handle_t aps_open_output(void *service,
1418 audio_devices_t *pDevices,
1419 uint32_t *pSamplingRate,
1420 audio_format_t *pFormat,
1421 audio_channel_mask_t *pChannelMask,
1422 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001423 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001424{
1425 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1426 if (af == 0) {
1427 ALOGW("%s: could not get AudioFlinger", __func__);
1428 return 0;
1429 }
1430
1431 return af->openOutput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask,
1432 pLatencyMs, flags);
1433}
1434
1435static audio_io_handle_t aps_open_output_on_module(void *service,
1436 audio_module_handle_t module,
1437 audio_devices_t *pDevices,
1438 uint32_t *pSamplingRate,
1439 audio_format_t *pFormat,
1440 audio_channel_mask_t *pChannelMask,
1441 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001442 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001443{
1444 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1445 if (af == 0) {
1446 ALOGW("%s: could not get AudioFlinger", __func__);
1447 return 0;
1448 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001449 return af->openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
Dima Zavinfce7a472011-04-19 22:30:36 -07001450 pLatencyMs, flags);
1451}
1452
1453static audio_io_handle_t aps_open_dup_output(void *service,
1454 audio_io_handle_t output1,
1455 audio_io_handle_t output2)
1456{
1457 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001458 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001459 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001460 return 0;
1461 }
1462 return af->openDuplicateOutput(output1, output2);
1463}
1464
1465static int aps_close_output(void *service, audio_io_handle_t output)
1466{
1467 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001468 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001469 return PERMISSION_DENIED;
1470
1471 return af->closeOutput(output);
1472}
1473
1474static int aps_suspend_output(void *service, audio_io_handle_t output)
1475{
1476 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001477 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001478 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001479 return PERMISSION_DENIED;
1480 }
1481
1482 return af->suspendOutput(output);
1483}
1484
1485static int aps_restore_output(void *service, audio_io_handle_t output)
1486{
1487 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001488 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001489 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001490 return PERMISSION_DENIED;
1491 }
1492
1493 return af->restoreOutput(output);
1494}
1495
Glenn Kasten20010052012-06-22 13:43:51 -07001496// deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
Dima Zavinfce7a472011-04-19 22:30:36 -07001497static audio_io_handle_t aps_open_input(void *service,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001498 audio_devices_t *pDevices,
1499 uint32_t *pSamplingRate,
1500 audio_format_t *pFormat,
1501 audio_channel_mask_t *pChannelMask,
1502 audio_in_acoustics_t acoustics)
Dima Zavinfce7a472011-04-19 22:30:36 -07001503{
1504 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001505 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001506 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001507 return 0;
1508 }
1509
Eric Laurenta4c5a552012-03-29 10:12:40 -07001510 return af->openInput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask);
1511}
1512
1513static audio_io_handle_t aps_open_input_on_module(void *service,
1514 audio_module_handle_t module,
1515 audio_devices_t *pDevices,
1516 uint32_t *pSamplingRate,
1517 audio_format_t *pFormat,
1518 audio_channel_mask_t *pChannelMask)
1519{
1520 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1521 if (af == 0) {
1522 ALOGW("%s: could not get AudioFlinger", __func__);
1523 return 0;
1524 }
1525
1526 return af->openInput(module, pDevices, pSamplingRate, pFormat, pChannelMask);
Dima Zavinfce7a472011-04-19 22:30:36 -07001527}
1528
1529static int aps_close_input(void *service, audio_io_handle_t input)
1530{
1531 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001532 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001533 return PERMISSION_DENIED;
1534
1535 return af->closeInput(input);
1536}
1537
1538static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1539 audio_io_handle_t output)
1540{
1541 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001542 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001543 return PERMISSION_DENIED;
1544
1545 return af->setStreamOutput(stream, output);
1546}
1547
1548static int aps_move_effects(void *service, int session,
1549 audio_io_handle_t src_output,
1550 audio_io_handle_t dst_output)
1551{
1552 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001553 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001554 return PERMISSION_DENIED;
1555
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001556 return af->moveEffects(session, src_output, dst_output);
Dima Zavinfce7a472011-04-19 22:30:36 -07001557}
1558
1559static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1560 const char *keys)
1561{
1562 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1563 return strdup(result.string());
1564}
1565
1566static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1567 const char *kv_pairs, int delay_ms)
1568{
1569 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1570
1571 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1572}
1573
1574static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1575 float volume, audio_io_handle_t output,
1576 int delay_ms)
1577{
1578 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1579
1580 return audioPolicyService->setStreamVolume(stream, volume, output,
1581 delay_ms);
1582}
1583
1584static int aps_start_tone(void *service, audio_policy_tone_t tone,
1585 audio_stream_type_t stream)
1586{
1587 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1588
1589 return audioPolicyService->startTone(tone, stream);
1590}
1591
1592static int aps_stop_tone(void *service)
1593{
1594 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1595
1596 return audioPolicyService->stopTone();
1597}
1598
1599static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1600{
1601 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1602
1603 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1604}
1605
Mingming Yin84f109b2012-12-27 17:43:05 -08001606#ifdef QCOM_FM_ENABLED
1607static int aps_set_fm_volume(void *service, float volume, int delay_ms)
1608{
1609 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1610
1611 return audioPolicyService->setFmVolume(volume, delay_ms);
1612}
1613#endif
1614
Dima Zavinfce7a472011-04-19 22:30:36 -07001615}; // extern "C"
1616
1617namespace {
1618 struct audio_policy_service_ops aps_ops = {
1619 open_output : aps_open_output,
1620 open_duplicate_output : aps_open_dup_output,
1621 close_output : aps_close_output,
1622 suspend_output : aps_suspend_output,
1623 restore_output : aps_restore_output,
1624 open_input : aps_open_input,
1625 close_input : aps_close_input,
1626 set_stream_volume : aps_set_stream_volume,
1627 set_stream_output : aps_set_stream_output,
1628 set_parameters : aps_set_parameters,
1629 get_parameters : aps_get_parameters,
1630 start_tone : aps_start_tone,
1631 stop_tone : aps_stop_tone,
1632 set_voice_volume : aps_set_voice_volume,
1633 move_effects : aps_move_effects,
Mingming Yin84f109b2012-12-27 17:43:05 -08001634#ifdef QCOM_FM_ENABLED
1635 set_fm_volume : aps_set_fm_volume,
1636#endif
Eric Laurenta4c5a552012-03-29 10:12:40 -07001637 load_hw_module : aps_load_hw_module,
1638 open_output_on_module : aps_open_output_on_module,
1639 open_input_on_module : aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001640 };
1641}; // namespace <unnamed>
1642
Mathias Agopian65ab4712010-07-14 17:59:35 -07001643}; // namespace android