blob: fd4431c4c1113defca74a3ab19986f64618674e2 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioPolicyService"
18//#define LOG_NDEBUG 0
19
20#undef __STRICT_ANSI__
21#define __STDINT_LIMITS
22#define __STDC_LIMIT_MACROS
23#include <stdint.h>
24
25#include <sys/time.h>
26#include <binder/IServiceManager.h>
27#include <utils/Log.h>
28#include <cutils/properties.h>
29#include <binder/IPCThreadState.h>
30#include <utils/String16.h>
31#include <utils/threads.h>
32#include "AudioPolicyService.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080033#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070035#include <media/AudioEffect.h>
36#include <media/EffectsFactoryApi.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070037
Dima Zavinfce7a472011-04-19 22:30:36 -070038#include <hardware/hardware.h>
Dima Zavin64760242011-05-11 14:15:23 -070039#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070040#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070041#include <hardware/audio_policy.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070042#include <audio_effects/audio_effects_conf.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070043
Mathias Agopian65ab4712010-07-14 17:59:35 -070044namespace android {
45
Glenn Kasten8dad0e32012-01-09 08:41:22 -080046static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
47static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070048
49static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080050static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070051
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010052static const nsecs_t kAudioCommandTimeout = 3000000000; // 3 seconds
53
Dima Zavinfce7a472011-04-19 22:30:36 -070054namespace {
55 extern struct audio_policy_service_ops aps_ops;
56};
57
Mathias Agopian65ab4712010-07-14 17:59:35 -070058// ----------------------------------------------------------------------------
59
60AudioPolicyService::AudioPolicyService()
Dima Zavinfce7a472011-04-19 22:30:36 -070061 : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL)
Mathias Agopian65ab4712010-07-14 17:59:35 -070062{
63 char value[PROPERTY_VALUE_MAX];
Dima Zavinfce7a472011-04-19 22:30:36 -070064 const struct hw_module_t *module;
65 int forced_val;
66 int rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -070067
Eric Laurent93575202011-01-18 18:39:02 -080068 Mutex::Autolock _l(mLock);
69
Mathias Agopian65ab4712010-07-14 17:59:35 -070070 // start tone playback thread
71 mTonePlaybackThread = new AudioCommandThread(String8(""));
72 // start audio commands thread
Glenn Kasten480b4682012-02-28 12:30:08 -080073 mAudioCommandThread = new AudioCommandThread(String8("ApmCommand"));
Mathias Agopian65ab4712010-07-14 17:59:35 -070074
Dima Zavinfce7a472011-04-19 22:30:36 -070075 /* instantiate the audio policy manager */
76 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
77 if (rc)
78 return;
Mathias Agopian65ab4712010-07-14 17:59:35 -070079
Dima Zavinfce7a472011-04-19 22:30:36 -070080 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
Steve Block29357bc2012-01-06 19:20:56 +000081 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070082 if (rc)
83 return;
Eric Laurent93575202011-01-18 18:39:02 -080084
Dima Zavinfce7a472011-04-19 22:30:36 -070085 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
86 &mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000087 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070088 if (rc)
89 return;
90
91 rc = mpAudioPolicy->init_check(mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000092 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070093 if (rc)
94 return;
95
Steve Blockdf64d152012-01-04 20:05:49 +000096 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurent7c7f10b2011-06-17 21:29:58 -070097
98 // load audio pre processing modules
99 if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) {
100 loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE);
101 } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) {
102 loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE);
103 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700104}
105
106AudioPolicyService::~AudioPolicyService()
107{
108 mTonePlaybackThread->exit();
109 mTonePlaybackThread.clear();
110 mAudioCommandThread->exit();
111 mAudioCommandThread.clear();
112
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700113
114 // release audio pre processing resources
115 for (size_t i = 0; i < mInputSources.size(); i++) {
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800116 delete mInputSources.valueAt(i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700117 }
118 mInputSources.clear();
119
120 for (size_t i = 0; i < mInputs.size(); i++) {
121 mInputs.valueAt(i)->mEffects.clear();
122 delete mInputs.valueAt(i);
123 }
124 mInputs.clear();
125
Glenn Kastena0d68332012-01-27 16:47:15 -0800126 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL)
Dima Zavinfce7a472011-04-19 22:30:36 -0700127 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kastena0d68332012-01-27 16:47:15 -0800128 if (mpAudioPolicyDev != NULL)
Dima Zavinfce7a472011-04-19 22:30:36 -0700129 audio_policy_dev_close(mpAudioPolicyDev);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700130}
131
Dima Zavinfce7a472011-04-19 22:30:36 -0700132status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
133 audio_policy_dev_state_t state,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700134 const char *device_address)
135{
Dima Zavinfce7a472011-04-19 22:30:36 -0700136 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700137 return NO_INIT;
138 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800139 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700140 return PERMISSION_DENIED;
141 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700142 if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700143 return BAD_VALUE;
144 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700145 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
146 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700147 return BAD_VALUE;
148 }
149
Glenn Kasten411e4472012-11-02 10:00:06 -0700150 ALOGV("setDeviceConnectionState()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700151 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700152 return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device,
153 state, device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700154}
155
Dima Zavinfce7a472011-04-19 22:30:36 -0700156audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
157 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -0700158 const char *device_address)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700159{
Dima Zavinfce7a472011-04-19 22:30:36 -0700160 if (mpAudioPolicy == NULL) {
161 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700162 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700163 return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device,
164 device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700165}
166
Glenn Kastenf78aee72012-01-04 11:00:47 -0800167status_t AudioPolicyService::setPhoneState(audio_mode_t state)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700168{
Dima Zavinfce7a472011-04-19 22:30:36 -0700169 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700170 return NO_INIT;
171 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800172 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700173 return PERMISSION_DENIED;
174 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800175 if (uint32_t(state) >= AUDIO_MODE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700176 return BAD_VALUE;
177 }
178
Glenn Kasten411e4472012-11-02 10:00:06 -0700179 ALOGV("setPhoneState()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700180
181 // TODO: check if it is more appropriate to do it in platform specific policy manager
182 AudioSystem::setMode(state);
183
184 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700185 mpAudioPolicy->set_phone_state(mpAudioPolicy, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700186 return NO_ERROR;
187}
188
Dima Zavinfce7a472011-04-19 22:30:36 -0700189status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
190 audio_policy_forced_cfg_t config)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700191{
Dima Zavinfce7a472011-04-19 22:30:36 -0700192 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700193 return NO_INIT;
194 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800195 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700196 return PERMISSION_DENIED;
197 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700198 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700199 return BAD_VALUE;
200 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700201 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700202 return BAD_VALUE;
203 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700204 ALOGV("setForceUse()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700205 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700206 mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700207 return NO_ERROR;
208}
209
Dima Zavinfce7a472011-04-19 22:30:36 -0700210audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700211{
Dima Zavinfce7a472011-04-19 22:30:36 -0700212 if (mpAudioPolicy == NULL) {
213 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700214 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700215 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
216 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700217 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700218 return mpAudioPolicy->get_force_use(mpAudioPolicy, usage);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700219}
220
Dima Zavinfce7a472011-04-19 22:30:36 -0700221audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700222 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800223 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700224 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000225 audio_output_flags_t flags,
226 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700227{
Dima Zavinfce7a472011-04-19 22:30:36 -0700228 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700229 return 0;
230 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700231 ALOGV("getOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700232 Mutex::Autolock _l(mLock);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000233 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate,
234 format, channelMask, flags, offloadInfo);
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 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700244 ALOGV("startOutput()");
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 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700256 ALOGV("stopOutput()");
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 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700266 ALOGV("releaseOutput()");
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 Kasten8af901c2012-11-01 11:11:38 -0700287 format, channelMask, (audio_in_acoustics_t) 0);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700288
289 if (input == 0) {
290 return input;
291 }
292 // create audio pre processors according to input source
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800293 ssize_t index = mInputSources.indexOfKey(inputSource);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700294 if (index < 0) {
295 return input;
296 }
297 ssize_t idx = mInputs.indexOfKey(input);
298 InputDesc *inputDesc;
299 if (idx < 0) {
Glenn Kasten81872a22012-03-07 16:49:22 -0800300 inputDesc = new InputDesc(audioSession);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700301 mInputs.add(input, inputDesc);
302 } else {
303 inputDesc = mInputs.valueAt(idx);
304 }
305
306 Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects;
307 for (size_t i = 0; i < effects.size(); i++) {
308 EffectDesc *effect = effects[i];
309 sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input);
310 status_t status = fx->initCheck();
311 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000312 ALOGW("Failed to create Fx %s on input %d", effect->mName, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700313 // fx goes out of scope and strong ref on AudioEffect is released
314 continue;
315 }
316 for (size_t j = 0; j < effect->mParams.size(); j++) {
317 fx->setParameter(effect->mParams[j]);
318 }
319 inputDesc->mEffects.add(fx);
320 }
321 setPreProcessorEnabled(inputDesc, true);
322 return input;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323}
324
325status_t AudioPolicyService::startInput(audio_io_handle_t input)
326{
Dima Zavinfce7a472011-04-19 22:30:36 -0700327 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700328 return NO_INIT;
329 }
330 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700331
Dima Zavinfce7a472011-04-19 22:30:36 -0700332 return mpAudioPolicy->start_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700333}
334
335status_t AudioPolicyService::stopInput(audio_io_handle_t input)
336{
Dima Zavinfce7a472011-04-19 22:30:36 -0700337 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338 return NO_INIT;
339 }
340 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700341
Dima Zavinfce7a472011-04-19 22:30:36 -0700342 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700343}
344
345void AudioPolicyService::releaseInput(audio_io_handle_t input)
346{
Dima Zavinfce7a472011-04-19 22:30:36 -0700347 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700348 return;
349 }
350 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700351 mpAudioPolicy->release_input(mpAudioPolicy, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700352
353 ssize_t index = mInputs.indexOfKey(input);
354 if (index < 0) {
355 return;
356 }
357 InputDesc *inputDesc = mInputs.valueAt(index);
358 setPreProcessorEnabled(inputDesc, false);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700359 delete inputDesc;
360 mInputs.removeItemsAt(index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700361}
362
Dima Zavinfce7a472011-04-19 22:30:36 -0700363status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700364 int indexMin,
365 int indexMax)
366{
Dima Zavinfce7a472011-04-19 22:30:36 -0700367 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700368 return NO_INIT;
369 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800370 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700371 return PERMISSION_DENIED;
372 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800373 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700374 return BAD_VALUE;
375 }
Eric Laurent5f121362012-06-15 14:45:03 -0700376 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700377 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378 return NO_ERROR;
379}
380
Eric Laurent83844cc2011-11-18 16:43:31 -0800381status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
382 int index,
383 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384{
Dima Zavinfce7a472011-04-19 22:30:36 -0700385 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700386 return NO_INIT;
387 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800388 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700389 return PERMISSION_DENIED;
390 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800391 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392 return BAD_VALUE;
393 }
Eric Laurent5f121362012-06-15 14:45:03 -0700394 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800395 if (mpAudioPolicy->set_stream_volume_index_for_device) {
396 return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
397 stream,
398 index,
399 device);
400 } else {
401 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
402 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403}
404
Eric Laurent83844cc2011-11-18 16:43:31 -0800405status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
406 int *index,
407 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700408{
Dima Zavinfce7a472011-04-19 22:30:36 -0700409 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700410 return NO_INIT;
411 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800412 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700413 return BAD_VALUE;
414 }
Eric Laurent5f121362012-06-15 14:45:03 -0700415 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800416 if (mpAudioPolicy->get_stream_volume_index_for_device) {
417 return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
418 stream,
419 index,
420 device);
421 } else {
422 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
423 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700424}
425
Dima Zavinfce7a472011-04-19 22:30:36 -0700426uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700427{
Dima Zavinfce7a472011-04-19 22:30:36 -0700428 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700429 return 0;
430 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700431 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurentde070132010-07-13 04:45:46 -0700432}
433
Eric Laurent63742522012-03-08 13:42:42 -0800434//audio policy: use audio_device_t appropriately
435
436audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800437{
Dima Zavinfce7a472011-04-19 22:30:36 -0700438 if (mpAudioPolicy == NULL) {
Eric Laurent63742522012-03-08 13:42:42 -0800439 return (audio_devices_t)0;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800440 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700441 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800442}
443
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700444audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700445{
Dima Zavinfce7a472011-04-19 22:30:36 -0700446 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700447 return NO_INIT;
448 }
449 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700450 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700451}
452
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700453status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700454 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700455 uint32_t strategy,
456 int session,
457 int id)
458{
Dima Zavinfce7a472011-04-19 22:30:36 -0700459 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700460 return NO_INIT;
461 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700462 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700463}
464
465status_t AudioPolicyService::unregisterEffect(int id)
466{
Dima Zavinfce7a472011-04-19 22:30:36 -0700467 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700468 return NO_INIT;
469 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700470 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700471}
472
Eric Laurentdb7c0792011-08-10 10:37:50 -0700473status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
474{
475 if (mpAudioPolicy == NULL) {
476 return NO_INIT;
477 }
478 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
479}
480
Glenn Kastenfff6d712012-01-12 16:38:12 -0800481bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800482{
Dima Zavinfce7a472011-04-19 22:30:36 -0700483 if (mpAudioPolicy == NULL) {
Eric Laurenteda6c362011-02-02 09:33:30 -0800484 return 0;
485 }
486 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700487 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800488}
489
Jean-Michel Trivie336f912013-02-04 16:26:02 -0800490bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
491{
492 if (mpAudioPolicy == NULL) {
493 return 0;
494 }
495 Mutex::Autolock _l(mLock);
496 return mpAudioPolicy->is_stream_active_remotely(mpAudioPolicy, stream, inPastMs);
497}
498
Jean-Michel Trivie3f641f2012-10-10 12:11:16 -0700499bool AudioPolicyService::isSourceActive(audio_source_t source) const
500{
501 if (mpAudioPolicy == NULL) {
502 return false;
503 }
504 if (mpAudioPolicy->is_source_active == 0) {
505 return false;
506 }
507 Mutex::Autolock _l(mLock);
508 return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
509}
510
Eric Laurent57dae992011-07-24 13:36:09 -0700511status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
512 effect_descriptor_t *descriptors,
513 uint32_t *count)
514{
515
516 if (mpAudioPolicy == NULL) {
517 *count = 0;
518 return NO_INIT;
519 }
520 Mutex::Autolock _l(mLock);
521 status_t status = NO_ERROR;
522
523 size_t index;
524 for (index = 0; index < mInputs.size(); index++) {
525 if (mInputs.valueAt(index)->mSessionId == audioSession) {
526 break;
527 }
528 }
529 if (index == mInputs.size()) {
530 *count = 0;
531 return BAD_VALUE;
532 }
533 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
534
535 for (size_t i = 0; i < effects.size(); i++) {
536 effect_descriptor_t desc = effects[i]->descriptor();
537 if (i < *count) {
Glenn Kastena189a682012-02-20 12:16:30 -0800538 descriptors[i] = desc;
Eric Laurent57dae992011-07-24 13:36:09 -0700539 }
540 }
541 if (effects.size() > *count) {
542 status = NO_MEMORY;
543 }
544 *count = effects.size();
545 return status;
546}
547
Mathias Agopian65ab4712010-07-14 17:59:35 -0700548void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700549 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700550 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700551}
552
553static bool tryLock(Mutex& mutex)
554{
555 bool locked = false;
556 for (int i = 0; i < kDumpLockRetries; ++i) {
557 if (mutex.tryLock() == NO_ERROR) {
558 locked = true;
559 break;
560 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800561 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700562 }
563 return locked;
564}
565
566status_t AudioPolicyService::dumpInternals(int fd)
567{
568 const size_t SIZE = 256;
569 char buffer[SIZE];
570 String8 result;
571
Dima Zavinfce7a472011-04-19 22:30:36 -0700572 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700573 result.append(buffer);
574 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
575 result.append(buffer);
576 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
577 result.append(buffer);
578
579 write(fd, result.string(), result.size());
580 return NO_ERROR;
581}
582
583status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
584{
Glenn Kasten44deb052012-02-05 18:09:08 -0800585 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700586 dumpPermissionDenial(fd);
587 } else {
588 bool locked = tryLock(mLock);
589 if (!locked) {
590 String8 result(kDeadlockedString);
591 write(fd, result.string(), result.size());
592 }
593
594 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800595 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700596 mAudioCommandThread->dump(fd);
597 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800598 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700599 mTonePlaybackThread->dump(fd);
600 }
601
Dima Zavinfce7a472011-04-19 22:30:36 -0700602 if (mpAudioPolicy) {
603 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700604 }
605
606 if (locked) mLock.unlock();
607 }
608 return NO_ERROR;
609}
610
611status_t AudioPolicyService::dumpPermissionDenial(int fd)
612{
613 const size_t SIZE = 256;
614 char buffer[SIZE];
615 String8 result;
616 snprintf(buffer, SIZE, "Permission Denial: "
617 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
618 IPCThreadState::self()->getCallingPid(),
619 IPCThreadState::self()->getCallingUid());
620 result.append(buffer);
621 write(fd, result.string(), result.size());
622 return NO_ERROR;
623}
624
Glenn Kasten81872a22012-03-07 16:49:22 -0800625void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700626{
Glenn Kasten81872a22012-03-07 16:49:22 -0800627 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700628 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800629 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700630 }
631}
632
Mathias Agopian65ab4712010-07-14 17:59:35 -0700633status_t AudioPolicyService::onTransact(
634 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
635{
636 return BnAudioPolicyService::onTransact(code, data, reply, flags);
637}
638
639
Mathias Agopian65ab4712010-07-14 17:59:35 -0700640// ----------- AudioPolicyService::AudioCommandThread implementation ----------
641
642AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name)
643 : Thread(false), mName(name)
644{
645 mpToneGenerator = NULL;
646}
647
648
649AudioPolicyService::AudioCommandThread::~AudioCommandThread()
650{
651 if (mName != "" && !mAudioCommands.isEmpty()) {
652 release_wake_lock(mName.string());
653 }
654 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800655 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700656}
657
658void AudioPolicyService::AudioCommandThread::onFirstRef()
659{
660 if (mName != "") {
661 run(mName.string(), ANDROID_PRIORITY_AUDIO);
662 } else {
Glenn Kasten480b4682012-02-28 12:30:08 -0800663 run("AudioCommand", ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700664 }
665}
666
667bool AudioPolicyService::AudioCommandThread::threadLoop()
668{
669 nsecs_t waitTime = INT64_MAX;
670
671 mLock.lock();
672 while (!exitPending())
673 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700674 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700675 nsecs_t curTime = systemTime();
676 // commands are sorted by increasing time stamp: execute them from index 0 and up
677 if (mAudioCommands[0]->mTime <= curTime) {
678 AudioCommand *command = mAudioCommands[0];
679 mAudioCommands.removeAt(0);
680 mLastCommand = *command;
681
682 switch (command->mCommand) {
683 case START_TONE: {
684 mLock.unlock();
685 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100686 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700687 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800688 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700689 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
690 mpToneGenerator->startTone(data->mType);
691 delete data;
692 mLock.lock();
693 }break;
694 case STOP_TONE: {
695 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100696 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700697 if (mpToneGenerator != NULL) {
698 mpToneGenerator->stopTone();
699 delete mpToneGenerator;
700 mpToneGenerator = NULL;
701 }
702 mLock.lock();
703 }break;
704 case SET_VOLUME: {
705 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100706 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700707 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
708 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
709 data->mVolume,
710 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700711 if (command->mWaitStatus) {
712 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100713 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700714 }
715 delete data;
716 }break;
717 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700718 ParametersData *data = (ParametersData *)command->mParam;
719 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
720 data->mKeyValuePairs.string(), data->mIO);
721 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
722 if (command->mWaitStatus) {
723 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100724 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700725 }
726 delete data;
727 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700728 case SET_VOICE_VOLUME: {
729 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100730 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700731 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700732 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
733 if (command->mWaitStatus) {
734 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100735 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700736 }
737 delete data;
738 }break;
739 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000740 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700741 }
742 delete command;
743 waitTime = INT64_MAX;
744 } else {
745 waitTime = mAudioCommands[0]->mTime - curTime;
746 break;
747 }
748 }
749 // release delayed commands wake lock
750 if (mName != "" && mAudioCommands.isEmpty()) {
751 release_wake_lock(mName.string());
752 }
Steve Block3856b092011-10-20 11:56:00 +0100753 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700754 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100755 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700756 }
757 mLock.unlock();
758 return false;
759}
760
761status_t AudioPolicyService::AudioCommandThread::dump(int fd)
762{
763 const size_t SIZE = 256;
764 char buffer[SIZE];
765 String8 result;
766
767 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
768 result.append(buffer);
769 write(fd, result.string(), result.size());
770
771 bool locked = tryLock(mLock);
772 if (!locked) {
773 String8 result2(kCmdDeadlockedString);
774 write(fd, result2.string(), result2.size());
775 }
776
777 snprintf(buffer, SIZE, "- Commands:\n");
778 result = String8(buffer);
779 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800780 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700781 mAudioCommands[i]->dump(buffer, SIZE);
782 result.append(buffer);
783 }
784 result.append(" Last Command\n");
785 mLastCommand.dump(buffer, SIZE);
786 result.append(buffer);
787
788 write(fd, result.string(), result.size());
789
790 if (locked) mLock.unlock();
791
792 return NO_ERROR;
793}
794
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800795void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
796 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700797{
798 AudioCommand *command = new AudioCommand();
799 command->mCommand = START_TONE;
800 ToneData *data = new ToneData();
801 data->mType = type;
802 data->mStream = stream;
803 command->mParam = (void *)data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700804 Mutex::Autolock _l(mLock);
805 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100806 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700807 mWaitWorkCV.signal();
808}
809
810void AudioPolicyService::AudioCommandThread::stopToneCommand()
811{
812 AudioCommand *command = new AudioCommand();
813 command->mCommand = STOP_TONE;
814 command->mParam = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700815 Mutex::Autolock _l(mLock);
816 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100817 ALOGV("AudioCommandThread() adding tone stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818 mWaitWorkCV.signal();
819}
820
Glenn Kastenfff6d712012-01-12 16:38:12 -0800821status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700822 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800823 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700824 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700825{
826 status_t status = NO_ERROR;
827
828 AudioCommand *command = new AudioCommand();
829 command->mCommand = SET_VOLUME;
830 VolumeData *data = new VolumeData();
831 data->mStream = stream;
832 data->mVolume = volume;
833 data->mIO = output;
834 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700835 Mutex::Autolock _l(mLock);
836 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100837 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700838 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700839 mWaitWorkCV.signal();
840 if (command->mWaitStatus) {
841 command->mCond.wait(mLock);
842 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100843 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700844 }
845 return status;
846}
847
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800848status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700849 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700850 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700851{
852 status_t status = NO_ERROR;
853
854 AudioCommand *command = new AudioCommand();
855 command->mCommand = SET_PARAMETERS;
856 ParametersData *data = new ParametersData();
857 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700858 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700859 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700860 Mutex::Autolock _l(mLock);
861 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100862 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700863 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700864 mWaitWorkCV.signal();
865 if (command->mWaitStatus) {
866 command->mCond.wait(mLock);
867 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100868 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700869 }
870 return status;
871}
872
873status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
874{
875 status_t status = NO_ERROR;
876
877 AudioCommand *command = new AudioCommand();
878 command->mCommand = SET_VOICE_VOLUME;
879 VoiceVolumeData *data = new VoiceVolumeData();
880 data->mVolume = volume;
881 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700882 Mutex::Autolock _l(mLock);
883 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100884 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700885 mWaitWorkCV.signal();
886 if (command->mWaitStatus) {
887 command->mCond.wait(mLock);
888 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100889 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700890 }
891 return status;
892}
893
894// insertCommand_l() must be called with mLock held
895void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
896{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800897 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700898 Vector <AudioCommand *> removedCommands;
899
Eric Laurentcec4abb2012-07-03 12:23:02 -0700900 nsecs_t time = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700901 command->mTime = systemTime() + milliseconds(delayMs);
902
903 // acquire wake lock to make sure delayed commands are processed
904 if (mName != "" && mAudioCommands.isEmpty()) {
905 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
906 }
907
908 // check same pending commands with later time stamps and eliminate them
909 for (i = mAudioCommands.size()-1; i >= 0; i--) {
910 AudioCommand *command2 = mAudioCommands[i];
911 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
912 if (command2->mTime <= command->mTime) break;
913 if (command2->mCommand != command->mCommand) continue;
914
915 switch (command->mCommand) {
916 case SET_PARAMETERS: {
917 ParametersData *data = (ParametersData *)command->mParam;
918 ParametersData *data2 = (ParametersData *)command2->mParam;
919 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100920 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700921 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700922 AudioParameter param = AudioParameter(data->mKeyValuePairs);
923 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
924 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700925 String8 key;
926 String8 value;
927 param.getAt(j, key, value);
928 for (size_t k = 0; k < param2.size(); k++) {
929 String8 key2;
930 String8 value2;
931 param2.getAt(k, key2, value2);
932 if (key2 == key) {
933 param2.remove(key2);
934 ALOGV("Filtering out parameter %s", key2.string());
935 break;
936 }
937 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700938 }
939 // if all keys have been filtered out, remove the command.
940 // otherwise, update the key value pairs
941 if (param2.size() == 0) {
942 removedCommands.add(command2);
943 } else {
944 data2->mKeyValuePairs = param2.toString();
945 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700946 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947 } break;
948
949 case SET_VOLUME: {
950 VolumeData *data = (VolumeData *)command->mParam;
951 VolumeData *data2 = (VolumeData *)command2->mParam;
952 if (data->mIO != data2->mIO) break;
953 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100954 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700955 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700956 removedCommands.add(command2);
Eric Laurentcec4abb2012-07-03 12:23:02 -0700957 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700958 } break;
959 case START_TONE:
960 case STOP_TONE:
961 default:
962 break;
963 }
964 }
965
966 // remove filtered commands
967 for (size_t j = 0; j < removedCommands.size(); j++) {
968 // removed commands always have time stamps greater than current command
969 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
970 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +0100971 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700972 mAudioCommands.removeAt(k);
973 break;
974 }
975 }
976 }
977 removedCommands.clear();
978
Eric Laurentcec4abb2012-07-03 12:23:02 -0700979 // wait for status only if delay is 0 and command time was not modified above
980 if (delayMs == 0 && time == 0) {
981 command->mWaitStatus = true;
982 } else {
983 command->mWaitStatus = false;
984 }
985 // update command time if modified above
986 if (time != 0) {
987 command->mTime = time;
988 }
989
Mathias Agopian65ab4712010-07-14 17:59:35 -0700990 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +0100991 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -0700992 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700993 mAudioCommands.insertAt(command, i + 1);
994}
995
996void AudioPolicyService::AudioCommandThread::exit()
997{
Steve Block3856b092011-10-20 11:56:00 +0100998 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700999 {
1000 AutoMutex _l(mLock);
1001 requestExit();
1002 mWaitWorkCV.signal();
1003 }
1004 requestExitAndWait();
1005}
1006
1007void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1008{
1009 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1010 mCommand,
1011 (int)ns2s(mTime),
1012 (int)ns2ms(mTime)%1000,
1013 mWaitStatus,
1014 mParam);
1015}
1016
Dima Zavinfce7a472011-04-19 22:30:36 -07001017/******* helpers for the service_ops callbacks defined below *********/
1018void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1019 const char *keyValuePairs,
1020 int delayMs)
1021{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001022 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001023 delayMs);
1024}
1025
1026int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1027 float volume,
1028 audio_io_handle_t output,
1029 int delayMs)
1030{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001031 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001032 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001033}
1034
1035int AudioPolicyService::startTone(audio_policy_tone_t tone,
1036 audio_stream_type_t stream)
1037{
1038 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
Steve Block29357bc2012-01-06 19:20:56 +00001039 ALOGE("startTone: illegal tone requested (%d)", tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001040 if (stream != AUDIO_STREAM_VOICE_CALL)
Steve Block29357bc2012-01-06 19:20:56 +00001041 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001042 tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001043 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1044 AUDIO_STREAM_VOICE_CALL);
1045 return 0;
1046}
1047
1048int AudioPolicyService::stopTone()
1049{
1050 mTonePlaybackThread->stopToneCommand();
1051 return 0;
1052}
1053
1054int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1055{
1056 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1057}
1058
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001059bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
1060{
1061 return false; // stub function
1062}
1063
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001064// ----------------------------------------------------------------------------
1065// Audio pre-processing configuration
1066// ----------------------------------------------------------------------------
1067
Glenn Kasten8dad0e32012-01-09 08:41:22 -08001068/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001069 MIC_SRC_TAG,
1070 VOICE_UL_SRC_TAG,
1071 VOICE_DL_SRC_TAG,
1072 VOICE_CALL_SRC_TAG,
1073 CAMCORDER_SRC_TAG,
1074 VOICE_REC_SRC_TAG,
1075 VOICE_COMM_SRC_TAG
1076};
1077
1078// returns the audio_source_t enum corresponding to the input source name or
1079// AUDIO_SOURCE_CNT is no match found
1080audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
1081{
1082 int i;
1083 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
1084 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001085 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001086 break;
1087 }
1088 }
1089 return (audio_source_t)i;
1090}
1091
1092size_t AudioPolicyService::growParamSize(char *param,
1093 size_t size,
1094 size_t *curSize,
1095 size_t *totSize)
1096{
1097 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
1098 size_t pos = ((*curSize - 1 ) / size + 1) * size;
1099
1100 if (pos + size > *totSize) {
1101 while (pos + size > *totSize) {
1102 *totSize += ((*totSize + 7) / 8) * 4;
1103 }
1104 param = (char *)realloc(param, *totSize);
1105 }
1106 *curSize = pos + size;
1107 return pos;
1108}
1109
1110size_t AudioPolicyService::readParamValue(cnode *node,
1111 char *param,
1112 size_t *curSize,
1113 size_t *totSize)
1114{
1115 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
1116 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
1117 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001118 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001119 return sizeof(short);
1120 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
1121 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
1122 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001123 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001124 return sizeof(int);
1125 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
1126 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
1127 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001128 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001129 return sizeof(float);
1130 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
1131 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
1132 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1133 *(bool *)((char *)param + pos) = false;
1134 } else {
1135 *(bool *)((char *)param + pos) = true;
1136 }
Steve Block3856b092011-10-20 11:56:00 +01001137 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001138 return sizeof(bool);
1139 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1140 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1141 if (*curSize + len + 1 > *totSize) {
1142 *totSize = *curSize + len + 1;
1143 param = (char *)realloc(param, *totSize);
1144 }
1145 strncpy(param + *curSize, node->value, len);
1146 *curSize += len;
1147 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +01001148 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001149 return len;
1150 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001151 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001152 return 0;
1153}
1154
1155effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1156{
1157 cnode *param;
1158 cnode *value;
1159 size_t curSize = sizeof(effect_param_t);
1160 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1161 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1162
1163 param = config_find(root, PARAM_TAG);
1164 value = config_find(root, VALUE_TAG);
1165 if (param == NULL && value == NULL) {
1166 // try to parse simple parameter form {int int}
1167 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -08001168 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001169 // Note: that a pair of random strings is read as 0 0
1170 int *ptr = (int *)fx_param->data;
1171 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +00001172 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001173 *ptr++ = atoi(param->name);
1174 *ptr = atoi(param->value);
1175 fx_param->psize = sizeof(int);
1176 fx_param->vsize = sizeof(int);
1177 return fx_param;
1178 }
1179 }
1180 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001181 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001182 goto error;
1183 }
1184
1185 fx_param->psize = 0;
1186 param = param->first_child;
1187 while (param) {
Steve Block3856b092011-10-20 11:56:00 +01001188 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001189 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1190 if (size == 0) {
1191 goto error;
1192 }
1193 fx_param->psize += size;
1194 param = param->next;
1195 }
1196
1197 // align start of value field on 32 bit boundary
1198 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1199
1200 fx_param->vsize = 0;
1201 value = value->first_child;
1202 while (value) {
Steve Block3856b092011-10-20 11:56:00 +01001203 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001204 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1205 if (size == 0) {
1206 goto error;
1207 }
1208 fx_param->vsize += size;
1209 value = value->next;
1210 }
1211
1212 return fx_param;
1213
1214error:
1215 delete fx_param;
1216 return NULL;
1217}
1218
1219void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1220{
1221 cnode *node = root->first_child;
1222 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001223 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001224 effect_param_t *param = loadEffectParameter(node);
1225 if (param == NULL) {
1226 node = node->next;
1227 continue;
1228 }
1229 params.add(param);
1230 node = node->next;
1231 }
1232}
1233
1234AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1235 cnode *root,
1236 const Vector <EffectDesc *>& effects)
1237{
1238 cnode *node = root->first_child;
1239 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001240 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001241 return NULL;
1242 }
1243 InputSourceDesc *source = new InputSourceDesc();
1244 while (node) {
1245 size_t i;
1246 for (i = 0; i < effects.size(); i++) {
1247 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001248 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001249 break;
1250 }
1251 }
1252 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +01001253 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001254 node = node->next;
1255 continue;
1256 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001257 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001258 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +01001259 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001260 source->mEffects.add(effect);
1261 node = node->next;
1262 }
1263 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001264 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001265 delete source;
1266 return NULL;
1267 }
1268 return source;
1269}
1270
1271status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1272{
1273 cnode *node = config_find(root, PREPROCESSING_TAG);
1274 if (node == NULL) {
1275 return -ENOENT;
1276 }
1277 node = node->first_child;
1278 while (node) {
1279 audio_source_t source = inputSourceNameToEnum(node->name);
1280 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001281 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001282 node = node->next;
1283 continue;
1284 }
Steve Block3856b092011-10-20 11:56:00 +01001285 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001286 InputSourceDesc *desc = loadInputSource(node, effects);
1287 if (desc == NULL) {
1288 node = node->next;
1289 continue;
1290 }
1291 mInputSources.add(source, desc);
1292 node = node->next;
1293 }
1294 return NO_ERROR;
1295}
1296
1297AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1298{
1299 cnode *node = config_find(root, UUID_TAG);
1300 if (node == NULL) {
1301 return NULL;
1302 }
1303 effect_uuid_t uuid;
1304 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001305 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001306 return NULL;
1307 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001308 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001309}
1310
1311status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1312{
1313 cnode *node = config_find(root, EFFECTS_TAG);
1314 if (node == NULL) {
1315 return -ENOENT;
1316 }
1317 node = node->first_child;
1318 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001319 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001320 EffectDesc *effect = loadEffect(node);
1321 if (effect == NULL) {
1322 node = node->next;
1323 continue;
1324 }
1325 effects.add(effect);
1326 node = node->next;
1327 }
1328 return NO_ERROR;
1329}
1330
1331status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1332{
1333 cnode *root;
1334 char *data;
1335
1336 data = (char *)load_file(path, NULL);
1337 if (data == NULL) {
1338 return -ENODEV;
1339 }
1340 root = config_node("", "");
1341 config_load(root, data);
1342
1343 Vector <EffectDesc *> effects;
1344 loadEffects(root, effects);
1345 loadInputSources(root, effects);
1346
1347 config_free(root);
1348 free(root);
1349 free(data);
1350
1351 return NO_ERROR;
1352}
1353
Dima Zavinfce7a472011-04-19 22:30:36 -07001354/* implementation of the interface to the policy manager */
1355extern "C" {
1356
Eric Laurenta4c5a552012-03-29 10:12:40 -07001357
1358static audio_module_handle_t aps_load_hw_module(void *service,
1359 const char *name)
Dima Zavinfce7a472011-04-19 22:30:36 -07001360{
1361 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001362 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001363 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001364 return 0;
1365 }
1366
Eric Laurenta4c5a552012-03-29 10:12:40 -07001367 return af->loadHwModule(name);
1368}
1369
1370// deprecated: replaced by aps_open_output_on_module()
1371static audio_io_handle_t aps_open_output(void *service,
1372 audio_devices_t *pDevices,
1373 uint32_t *pSamplingRate,
1374 audio_format_t *pFormat,
1375 audio_channel_mask_t *pChannelMask,
1376 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001377 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001378{
1379 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1380 if (af == 0) {
1381 ALOGW("%s: could not get AudioFlinger", __func__);
1382 return 0;
1383 }
1384
1385 return af->openOutput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask,
1386 pLatencyMs, flags);
1387}
1388
1389static audio_io_handle_t aps_open_output_on_module(void *service,
1390 audio_module_handle_t module,
1391 audio_devices_t *pDevices,
1392 uint32_t *pSamplingRate,
1393 audio_format_t *pFormat,
1394 audio_channel_mask_t *pChannelMask,
1395 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001396 audio_output_flags_t flags,
1397 const audio_offload_info_t *offloadInfo)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001398{
1399 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1400 if (af == 0) {
1401 ALOGW("%s: could not get AudioFlinger", __func__);
1402 return 0;
1403 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001404 return af->openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
Dima Zavinfce7a472011-04-19 22:30:36 -07001405 pLatencyMs, flags);
1406}
1407
1408static audio_io_handle_t aps_open_dup_output(void *service,
1409 audio_io_handle_t output1,
1410 audio_io_handle_t output2)
1411{
1412 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001413 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001414 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001415 return 0;
1416 }
1417 return af->openDuplicateOutput(output1, output2);
1418}
1419
1420static int aps_close_output(void *service, audio_io_handle_t output)
1421{
1422 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001423 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001424 return PERMISSION_DENIED;
1425
1426 return af->closeOutput(output);
1427}
1428
1429static int aps_suspend_output(void *service, audio_io_handle_t output)
1430{
1431 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001432 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001433 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001434 return PERMISSION_DENIED;
1435 }
1436
1437 return af->suspendOutput(output);
1438}
1439
1440static int aps_restore_output(void *service, audio_io_handle_t output)
1441{
1442 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001443 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001444 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001445 return PERMISSION_DENIED;
1446 }
1447
1448 return af->restoreOutput(output);
1449}
1450
Glenn Kasten20010052012-06-22 13:43:51 -07001451// deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
Dima Zavinfce7a472011-04-19 22:30:36 -07001452static audio_io_handle_t aps_open_input(void *service,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001453 audio_devices_t *pDevices,
1454 uint32_t *pSamplingRate,
1455 audio_format_t *pFormat,
1456 audio_channel_mask_t *pChannelMask,
1457 audio_in_acoustics_t acoustics)
Dima Zavinfce7a472011-04-19 22:30:36 -07001458{
1459 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001460 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001461 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001462 return 0;
1463 }
1464
Eric Laurenta4c5a552012-03-29 10:12:40 -07001465 return af->openInput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask);
1466}
1467
1468static audio_io_handle_t aps_open_input_on_module(void *service,
1469 audio_module_handle_t module,
1470 audio_devices_t *pDevices,
1471 uint32_t *pSamplingRate,
1472 audio_format_t *pFormat,
1473 audio_channel_mask_t *pChannelMask)
1474{
1475 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1476 if (af == 0) {
1477 ALOGW("%s: could not get AudioFlinger", __func__);
1478 return 0;
1479 }
1480
1481 return af->openInput(module, pDevices, pSamplingRate, pFormat, pChannelMask);
Dima Zavinfce7a472011-04-19 22:30:36 -07001482}
1483
1484static int aps_close_input(void *service, audio_io_handle_t input)
1485{
1486 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001487 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001488 return PERMISSION_DENIED;
1489
1490 return af->closeInput(input);
1491}
1492
1493static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1494 audio_io_handle_t output)
1495{
1496 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001497 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001498 return PERMISSION_DENIED;
1499
1500 return af->setStreamOutput(stream, output);
1501}
1502
1503static int aps_move_effects(void *service, int session,
1504 audio_io_handle_t src_output,
1505 audio_io_handle_t dst_output)
1506{
1507 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001508 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001509 return PERMISSION_DENIED;
1510
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001511 return af->moveEffects(session, src_output, dst_output);
Dima Zavinfce7a472011-04-19 22:30:36 -07001512}
1513
1514static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1515 const char *keys)
1516{
1517 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1518 return strdup(result.string());
1519}
1520
1521static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1522 const char *kv_pairs, int delay_ms)
1523{
1524 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1525
1526 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1527}
1528
1529static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1530 float volume, audio_io_handle_t output,
1531 int delay_ms)
1532{
1533 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1534
1535 return audioPolicyService->setStreamVolume(stream, volume, output,
1536 delay_ms);
1537}
1538
1539static int aps_start_tone(void *service, audio_policy_tone_t tone,
1540 audio_stream_type_t stream)
1541{
1542 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1543
1544 return audioPolicyService->startTone(tone, stream);
1545}
1546
1547static int aps_stop_tone(void *service)
1548{
1549 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1550
1551 return audioPolicyService->stopTone();
1552}
1553
1554static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1555{
1556 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1557
1558 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1559}
1560
1561}; // extern "C"
1562
1563namespace {
1564 struct audio_policy_service_ops aps_ops = {
1565 open_output : aps_open_output,
1566 open_duplicate_output : aps_open_dup_output,
1567 close_output : aps_close_output,
1568 suspend_output : aps_suspend_output,
1569 restore_output : aps_restore_output,
1570 open_input : aps_open_input,
1571 close_input : aps_close_input,
1572 set_stream_volume : aps_set_stream_volume,
1573 set_stream_output : aps_set_stream_output,
1574 set_parameters : aps_set_parameters,
1575 get_parameters : aps_get_parameters,
1576 start_tone : aps_start_tone,
1577 stop_tone : aps_stop_tone,
1578 set_voice_volume : aps_set_voice_volume,
1579 move_effects : aps_move_effects,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001580 load_hw_module : aps_load_hw_module,
1581 open_output_on_module : aps_open_output_on_module,
1582 open_input_on_module : aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001583 };
1584}; // namespace <unnamed>
1585
Mathias Agopian65ab4712010-07-14 17:59:35 -07001586}; // namespace android