blob: 7f4081262708e8971e7f0e68a689d00f2ef1b30c [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
Glenn Kasten153b9fe2013-07-15 11:23:36 -070020#include "Configuration.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070021#undef __STRICT_ANSI__
22#define __STDINT_LIMITS
23#define __STDC_LIMIT_MACROS
24#include <stdint.h>
25
26#include <sys/time.h>
27#include <binder/IServiceManager.h>
28#include <utils/Log.h>
29#include <cutils/properties.h>
30#include <binder/IPCThreadState.h>
31#include <utils/String16.h>
32#include <utils/threads.h>
33#include "AudioPolicyService.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080034#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070036#include <media/AudioEffect.h>
37#include <media/EffectsFactoryApi.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070038
Dima Zavinfce7a472011-04-19 22:30:36 -070039#include <hardware/hardware.h>
Dima Zavin64760242011-05-11 14:15:23 -070040#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070041#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070042#include <hardware/audio_policy.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070043#include <audio_effects/audio_effects_conf.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070044#include <media/AudioParameter.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070045
Mathias Agopian65ab4712010-07-14 17:59:35 -070046namespace android {
47
Glenn Kasten8dad0e32012-01-09 08:41:22 -080048static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
49static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
51static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080052static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
Glenn Kasten1b8ae3d2013-07-19 10:32:41 -070054static const nsecs_t kAudioCommandTimeout = 3000000000LL; // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010055
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
Eric Laurentbfb1b832013-01-07 09:53:42 -080073 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
Mathias Agopian65ab4712010-07-14 17:59:35 -070074 // start audio commands thread
Eric Laurentbfb1b832013-01-07 09:53:42 -080075 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
76 // start output activity command thread
77 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Dima Zavinfce7a472011-04-19 22:30:36 -070078 /* instantiate the audio policy manager */
79 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
80 if (rc)
81 return;
Mathias Agopian65ab4712010-07-14 17:59:35 -070082
Dima Zavinfce7a472011-04-19 22:30:36 -070083 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
Steve Block29357bc2012-01-06 19:20:56 +000084 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070085 if (rc)
86 return;
Eric Laurent93575202011-01-18 18:39:02 -080087
Dima Zavinfce7a472011-04-19 22:30:36 -070088 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
89 &mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000090 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070091 if (rc)
92 return;
93
94 rc = mpAudioPolicy->init_check(mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000095 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070096 if (rc)
97 return;
98
Steve Blockdf64d152012-01-04 20:05:49 +000099 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700100
101 // load audio pre processing modules
102 if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) {
103 loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE);
104 } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) {
105 loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE);
106 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700107}
108
109AudioPolicyService::~AudioPolicyService()
110{
111 mTonePlaybackThread->exit();
112 mTonePlaybackThread.clear();
113 mAudioCommandThread->exit();
114 mAudioCommandThread.clear();
115
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700116
117 // release audio pre processing resources
118 for (size_t i = 0; i < mInputSources.size(); i++) {
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800119 delete mInputSources.valueAt(i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700120 }
121 mInputSources.clear();
122
123 for (size_t i = 0; i < mInputs.size(); i++) {
124 mInputs.valueAt(i)->mEffects.clear();
125 delete mInputs.valueAt(i);
126 }
127 mInputs.clear();
128
Glenn Kastena0d68332012-01-27 16:47:15 -0800129 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL)
Dima Zavinfce7a472011-04-19 22:30:36 -0700130 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kastena0d68332012-01-27 16:47:15 -0800131 if (mpAudioPolicyDev != NULL)
Dima Zavinfce7a472011-04-19 22:30:36 -0700132 audio_policy_dev_close(mpAudioPolicyDev);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700133}
134
Dima Zavinfce7a472011-04-19 22:30:36 -0700135status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
136 audio_policy_dev_state_t state,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700137 const char *device_address)
138{
Dima Zavinfce7a472011-04-19 22:30:36 -0700139 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700140 return NO_INIT;
141 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800142 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700143 return PERMISSION_DENIED;
144 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700145 if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700146 return BAD_VALUE;
147 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700148 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
149 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700150 return BAD_VALUE;
151 }
152
Glenn Kasten411e4472012-11-02 10:00:06 -0700153 ALOGV("setDeviceConnectionState()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700154 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700155 return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device,
156 state, device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700157}
158
Dima Zavinfce7a472011-04-19 22:30:36 -0700159audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
160 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -0700161 const char *device_address)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700162{
Dima Zavinfce7a472011-04-19 22:30:36 -0700163 if (mpAudioPolicy == NULL) {
164 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700165 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700166 return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device,
167 device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700168}
169
Glenn Kastenf78aee72012-01-04 11:00:47 -0800170status_t AudioPolicyService::setPhoneState(audio_mode_t state)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700171{
Dima Zavinfce7a472011-04-19 22:30:36 -0700172 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700173 return NO_INIT;
174 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800175 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700176 return PERMISSION_DENIED;
177 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800178 if (uint32_t(state) >= AUDIO_MODE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700179 return BAD_VALUE;
180 }
181
Glenn Kasten411e4472012-11-02 10:00:06 -0700182 ALOGV("setPhoneState()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700183
184 // TODO: check if it is more appropriate to do it in platform specific policy manager
185 AudioSystem::setMode(state);
186
187 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700188 mpAudioPolicy->set_phone_state(mpAudioPolicy, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700189 return NO_ERROR;
190}
191
Dima Zavinfce7a472011-04-19 22:30:36 -0700192status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
193 audio_policy_forced_cfg_t config)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700194{
Dima Zavinfce7a472011-04-19 22:30:36 -0700195 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700196 return NO_INIT;
197 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800198 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700199 return PERMISSION_DENIED;
200 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700201 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700202 return BAD_VALUE;
203 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700204 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700205 return BAD_VALUE;
206 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700207 ALOGV("setForceUse()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700208 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700209 mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700210 return NO_ERROR;
211}
212
Dima Zavinfce7a472011-04-19 22:30:36 -0700213audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700214{
Dima Zavinfce7a472011-04-19 22:30:36 -0700215 if (mpAudioPolicy == NULL) {
216 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700217 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700218 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
219 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700220 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700221 return mpAudioPolicy->get_force_use(mpAudioPolicy, usage);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700222}
223
Dima Zavinfce7a472011-04-19 22:30:36 -0700224audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700225 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800226 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700227 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000228 audio_output_flags_t flags,
229 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700230{
Dima Zavinfce7a472011-04-19 22:30:36 -0700231 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700232 return 0;
233 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700234 ALOGV("getOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700235 Mutex::Autolock _l(mLock);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000236 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate,
237 format, channelMask, flags, offloadInfo);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700238}
239
Eric Laurentde070132010-07-13 04:45:46 -0700240status_t AudioPolicyService::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700241 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700242 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700243{
Dima Zavinfce7a472011-04-19 22:30:36 -0700244 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700245 return NO_INIT;
246 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700247 ALOGV("startOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700248 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700249 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700250}
251
Eric Laurentde070132010-07-13 04:45:46 -0700252status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700253 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700254 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700255{
Dima Zavinfce7a472011-04-19 22:30:36 -0700256 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700257 return NO_INIT;
258 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700259 ALOGV("stopOutput()");
Eric Laurentbfb1b832013-01-07 09:53:42 -0800260 mOutputCommandThread->stopOutputCommand(output, stream, session);
261 return NO_ERROR;
262}
263
264status_t AudioPolicyService::doStopOutput(audio_io_handle_t output,
265 audio_stream_type_t stream,
266 int session)
267{
268 ALOGV("doStopOutput from tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700269 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700270 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700271}
272
273void AudioPolicyService::releaseOutput(audio_io_handle_t output)
274{
Dima Zavinfce7a472011-04-19 22:30:36 -0700275 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700276 return;
277 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700278 ALOGV("releaseOutput()");
Eric Laurentbfb1b832013-01-07 09:53:42 -0800279 mOutputCommandThread->releaseOutputCommand(output);
280}
281
282void AudioPolicyService::doReleaseOutput(audio_io_handle_t output)
283{
284 ALOGV("doReleaseOutput from tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700285 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700286 mpAudioPolicy->release_output(mpAudioPolicy, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700287}
288
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800289audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700290 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800291 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700292 audio_channel_mask_t channelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700293 int audioSession)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700294{
Dima Zavinfce7a472011-04-19 22:30:36 -0700295 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700296 return 0;
297 }
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800298 // already checked by client, but double-check in case the client wrapper is bypassed
Eric Laurent9a54bc22013-09-09 09:08:44 -0700299 if (inputSource >= AUDIO_SOURCE_CNT && inputSource != AUDIO_SOURCE_HOTWORD) {
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800300 return 0;
301 }
Eric Laurent9a54bc22013-09-09 09:08:44 -0700302
303 if ((inputSource == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) {
304 return 0;
305 }
306
Mathias Agopian65ab4712010-07-14 17:59:35 -0700307 Mutex::Autolock _l(mLock);
Glenn Kasten20010052012-06-22 13:43:51 -0700308 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700309 audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
Glenn Kasten8af901c2012-11-01 11:11:38 -0700310 format, channelMask, (audio_in_acoustics_t) 0);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700311
312 if (input == 0) {
313 return input;
314 }
315 // create audio pre processors according to input source
Eric Laurent9a54bc22013-09-09 09:08:44 -0700316 audio_source_t aliasSource = (inputSource == AUDIO_SOURCE_HOTWORD) ?
317 AUDIO_SOURCE_VOICE_RECOGNITION : inputSource;
318
319 ssize_t index = mInputSources.indexOfKey(aliasSource);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700320 if (index < 0) {
321 return input;
322 }
323 ssize_t idx = mInputs.indexOfKey(input);
324 InputDesc *inputDesc;
325 if (idx < 0) {
Glenn Kasten81872a22012-03-07 16:49:22 -0800326 inputDesc = new InputDesc(audioSession);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700327 mInputs.add(input, inputDesc);
328 } else {
329 inputDesc = mInputs.valueAt(idx);
330 }
331
332 Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects;
333 for (size_t i = 0; i < effects.size(); i++) {
334 EffectDesc *effect = effects[i];
335 sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input);
336 status_t status = fx->initCheck();
337 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000338 ALOGW("Failed to create Fx %s on input %d", effect->mName, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700339 // fx goes out of scope and strong ref on AudioEffect is released
340 continue;
341 }
342 for (size_t j = 0; j < effect->mParams.size(); j++) {
343 fx->setParameter(effect->mParams[j]);
344 }
345 inputDesc->mEffects.add(fx);
346 }
347 setPreProcessorEnabled(inputDesc, true);
348 return input;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349}
350
351status_t AudioPolicyService::startInput(audio_io_handle_t input)
352{
Dima Zavinfce7a472011-04-19 22:30:36 -0700353 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700354 return NO_INIT;
355 }
356 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700357
Dima Zavinfce7a472011-04-19 22:30:36 -0700358 return mpAudioPolicy->start_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700359}
360
361status_t AudioPolicyService::stopInput(audio_io_handle_t input)
362{
Dima Zavinfce7a472011-04-19 22:30:36 -0700363 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700364 return NO_INIT;
365 }
366 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700367
Dima Zavinfce7a472011-04-19 22:30:36 -0700368 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700369}
370
371void AudioPolicyService::releaseInput(audio_io_handle_t input)
372{
Dima Zavinfce7a472011-04-19 22:30:36 -0700373 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700374 return;
375 }
376 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700377 mpAudioPolicy->release_input(mpAudioPolicy, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700378
379 ssize_t index = mInputs.indexOfKey(input);
380 if (index < 0) {
381 return;
382 }
383 InputDesc *inputDesc = mInputs.valueAt(index);
384 setPreProcessorEnabled(inputDesc, false);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700385 delete inputDesc;
386 mInputs.removeItemsAt(index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700387}
388
Dima Zavinfce7a472011-04-19 22:30:36 -0700389status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700390 int indexMin,
391 int indexMax)
392{
Dima Zavinfce7a472011-04-19 22:30:36 -0700393 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700394 return NO_INIT;
395 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800396 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700397 return PERMISSION_DENIED;
398 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800399 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400 return BAD_VALUE;
401 }
Eric Laurent5f121362012-06-15 14:45:03 -0700402 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700403 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700404 return NO_ERROR;
405}
406
Eric Laurent83844cc2011-11-18 16:43:31 -0800407status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
408 int index,
409 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700410{
Dima Zavinfce7a472011-04-19 22:30:36 -0700411 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700412 return NO_INIT;
413 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800414 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700415 return PERMISSION_DENIED;
416 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800417 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700418 return BAD_VALUE;
419 }
Eric Laurent5f121362012-06-15 14:45:03 -0700420 Mutex::Autolock _l(mLock);
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +0000421#ifndef ICS_AUDIO_BLOB
Eric Laurent83844cc2011-11-18 16:43:31 -0800422 if (mpAudioPolicy->set_stream_volume_index_for_device) {
423 return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
424 stream,
425 index,
426 device);
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +0000427 } else
428#endif
429 {
Eric Laurent83844cc2011-11-18 16:43:31 -0800430 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
431 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700432}
433
Eric Laurent83844cc2011-11-18 16:43:31 -0800434status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
435 int *index,
436 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700437{
Dima Zavinfce7a472011-04-19 22:30:36 -0700438 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700439 return NO_INIT;
440 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800441 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700442 return BAD_VALUE;
443 }
Eric Laurent5f121362012-06-15 14:45:03 -0700444 Mutex::Autolock _l(mLock);
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +0000445#ifndef ICS_AUDIO_BLOB
Eric Laurent83844cc2011-11-18 16:43:31 -0800446 if (mpAudioPolicy->get_stream_volume_index_for_device) {
447 return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
448 stream,
449 index,
450 device);
Ricardo Cerqueira0d44b5b2013-11-03 02:46:27 +0000451 } else
452#endif
453 {
Eric Laurent83844cc2011-11-18 16:43:31 -0800454 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
455 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700456}
457
Dima Zavinfce7a472011-04-19 22:30:36 -0700458uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700459{
Dima Zavinfce7a472011-04-19 22:30:36 -0700460 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700461 return 0;
462 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700463 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurentde070132010-07-13 04:45:46 -0700464}
465
Eric Laurent63742522012-03-08 13:42:42 -0800466//audio policy: use audio_device_t appropriately
467
468audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800469{
Dima Zavinfce7a472011-04-19 22:30:36 -0700470 if (mpAudioPolicy == NULL) {
Eric Laurent63742522012-03-08 13:42:42 -0800471 return (audio_devices_t)0;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800472 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700473 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800474}
475
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700476audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700477{
Dima Zavinfce7a472011-04-19 22:30:36 -0700478 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700479 return NO_INIT;
480 }
481 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700482 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700483}
484
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700485status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700486 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700487 uint32_t strategy,
488 int session,
489 int id)
490{
Dima Zavinfce7a472011-04-19 22:30:36 -0700491 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700492 return NO_INIT;
493 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700494 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700495}
496
497status_t AudioPolicyService::unregisterEffect(int id)
498{
Dima Zavinfce7a472011-04-19 22:30:36 -0700499 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700500 return NO_INIT;
501 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700502 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700503}
504
Eric Laurentdb7c0792011-08-10 10:37:50 -0700505status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
506{
507 if (mpAudioPolicy == NULL) {
508 return NO_INIT;
509 }
510 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
511}
512
Glenn Kastenfff6d712012-01-12 16:38:12 -0800513bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800514{
Dima Zavinfce7a472011-04-19 22:30:36 -0700515 if (mpAudioPolicy == NULL) {
Eric Laurenteda6c362011-02-02 09:33:30 -0800516 return 0;
517 }
518 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700519 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800520}
521
Jean-Michel Trivie336f912013-02-04 16:26:02 -0800522bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
523{
Thomas Wendtc0952dc2013-10-27 22:44:49 +0100524#if !defined(ICS_AUDIO_BLOB) && !defined(MR1_AUDIO_BLOB)
Jean-Michel Trivie336f912013-02-04 16:26:02 -0800525 if (mpAudioPolicy == NULL) {
526 return 0;
527 }
528 Mutex::Autolock _l(mLock);
529 return mpAudioPolicy->is_stream_active_remotely(mpAudioPolicy, stream, inPastMs);
Tom Bridendb860892013-09-03 20:49:26 +0100530#endif
531 return 0;
Jean-Michel Trivie336f912013-02-04 16:26:02 -0800532}
533
Jean-Michel Trivie3f641f2012-10-10 12:11:16 -0700534bool AudioPolicyService::isSourceActive(audio_source_t source) const
535{
Tom Bridendb860892013-09-03 20:49:26 +0100536#ifndef ICS_AUDIO_BLOB
Jean-Michel Trivie3f641f2012-10-10 12:11:16 -0700537 if (mpAudioPolicy == NULL) {
538 return false;
539 }
540 if (mpAudioPolicy->is_source_active == 0) {
541 return false;
542 }
543 Mutex::Autolock _l(mLock);
544 return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
Tom Bridendb860892013-09-03 20:49:26 +0100545#else
546 return false;
547#endif
Jean-Michel Trivie3f641f2012-10-10 12:11:16 -0700548}
549
Eric Laurent57dae992011-07-24 13:36:09 -0700550status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
551 effect_descriptor_t *descriptors,
552 uint32_t *count)
553{
554
555 if (mpAudioPolicy == NULL) {
556 *count = 0;
557 return NO_INIT;
558 }
559 Mutex::Autolock _l(mLock);
560 status_t status = NO_ERROR;
561
562 size_t index;
563 for (index = 0; index < mInputs.size(); index++) {
564 if (mInputs.valueAt(index)->mSessionId == audioSession) {
565 break;
566 }
567 }
568 if (index == mInputs.size()) {
569 *count = 0;
570 return BAD_VALUE;
571 }
572 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
573
574 for (size_t i = 0; i < effects.size(); i++) {
575 effect_descriptor_t desc = effects[i]->descriptor();
576 if (i < *count) {
Glenn Kastena189a682012-02-20 12:16:30 -0800577 descriptors[i] = desc;
Eric Laurent57dae992011-07-24 13:36:09 -0700578 }
579 }
580 if (effects.size() > *count) {
581 status = NO_MEMORY;
582 }
583 *count = effects.size();
584 return status;
585}
586
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700588 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700589 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700590}
591
592static bool tryLock(Mutex& mutex)
593{
594 bool locked = false;
595 for (int i = 0; i < kDumpLockRetries; ++i) {
596 if (mutex.tryLock() == NO_ERROR) {
597 locked = true;
598 break;
599 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800600 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700601 }
602 return locked;
603}
604
605status_t AudioPolicyService::dumpInternals(int fd)
606{
607 const size_t SIZE = 256;
608 char buffer[SIZE];
609 String8 result;
610
Dima Zavinfce7a472011-04-19 22:30:36 -0700611 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700612 result.append(buffer);
613 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
614 result.append(buffer);
615 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
616 result.append(buffer);
617
618 write(fd, result.string(), result.size());
619 return NO_ERROR;
620}
621
622status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
623{
Glenn Kasten44deb052012-02-05 18:09:08 -0800624 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700625 dumpPermissionDenial(fd);
626 } else {
627 bool locked = tryLock(mLock);
628 if (!locked) {
629 String8 result(kDeadlockedString);
630 write(fd, result.string(), result.size());
631 }
632
633 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800634 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635 mAudioCommandThread->dump(fd);
636 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800637 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700638 mTonePlaybackThread->dump(fd);
639 }
640
Dima Zavinfce7a472011-04-19 22:30:36 -0700641 if (mpAudioPolicy) {
642 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700643 }
644
645 if (locked) mLock.unlock();
646 }
647 return NO_ERROR;
648}
649
650status_t AudioPolicyService::dumpPermissionDenial(int fd)
651{
652 const size_t SIZE = 256;
653 char buffer[SIZE];
654 String8 result;
655 snprintf(buffer, SIZE, "Permission Denial: "
656 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
657 IPCThreadState::self()->getCallingPid(),
658 IPCThreadState::self()->getCallingUid());
659 result.append(buffer);
660 write(fd, result.string(), result.size());
661 return NO_ERROR;
662}
663
Glenn Kasten81872a22012-03-07 16:49:22 -0800664void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700665{
Glenn Kasten81872a22012-03-07 16:49:22 -0800666 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700667 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800668 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700669 }
670}
671
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672status_t AudioPolicyService::onTransact(
673 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
674{
675 return BnAudioPolicyService::onTransact(code, data, reply, flags);
676}
677
678
Mathias Agopian65ab4712010-07-14 17:59:35 -0700679// ----------- AudioPolicyService::AudioCommandThread implementation ----------
680
Eric Laurentbfb1b832013-01-07 09:53:42 -0800681AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
682 const wp<AudioPolicyService>& service)
683 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700684{
685 mpToneGenerator = NULL;
686}
687
688
689AudioPolicyService::AudioCommandThread::~AudioCommandThread()
690{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800691 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700692 release_wake_lock(mName.string());
693 }
694 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800695 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700696}
697
698void AudioPolicyService::AudioCommandThread::onFirstRef()
699{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800700 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700701}
702
703bool AudioPolicyService::AudioCommandThread::threadLoop()
704{
705 nsecs_t waitTime = INT64_MAX;
706
707 mLock.lock();
708 while (!exitPending())
709 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700710 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700711 nsecs_t curTime = systemTime();
712 // commands are sorted by increasing time stamp: execute them from index 0 and up
713 if (mAudioCommands[0]->mTime <= curTime) {
714 AudioCommand *command = mAudioCommands[0];
715 mAudioCommands.removeAt(0);
716 mLastCommand = *command;
717
718 switch (command->mCommand) {
719 case START_TONE: {
720 mLock.unlock();
721 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100722 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700723 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800724 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700725 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
726 mpToneGenerator->startTone(data->mType);
727 delete data;
728 mLock.lock();
729 }break;
730 case STOP_TONE: {
731 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100732 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700733 if (mpToneGenerator != NULL) {
734 mpToneGenerator->stopTone();
735 delete mpToneGenerator;
736 mpToneGenerator = NULL;
737 }
738 mLock.lock();
739 }break;
740 case SET_VOLUME: {
741 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100742 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700743 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
744 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
745 data->mVolume,
746 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700747 if (command->mWaitStatus) {
748 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100749 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700750 }
751 delete data;
752 }break;
753 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700754 ParametersData *data = (ParametersData *)command->mParam;
755 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
756 data->mKeyValuePairs.string(), data->mIO);
757 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
758 if (command->mWaitStatus) {
759 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100760 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700761 }
762 delete data;
763 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700764 case SET_VOICE_VOLUME: {
765 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100766 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700767 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700768 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
769 if (command->mWaitStatus) {
770 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100771 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700772 }
773 delete data;
774 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800775 case STOP_OUTPUT: {
776 StopOutputData *data = (StopOutputData *)command->mParam;
777 ALOGV("AudioCommandThread() processing stop output %d",
778 data->mIO);
779 sp<AudioPolicyService> svc = mService.promote();
780 if (svc == 0) {
781 break;
782 }
783 mLock.unlock();
784 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
785 mLock.lock();
786 delete data;
787 }break;
788 case RELEASE_OUTPUT: {
789 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam;
790 ALOGV("AudioCommandThread() processing release output %d",
791 data->mIO);
792 sp<AudioPolicyService> svc = mService.promote();
793 if (svc == 0) {
794 break;
795 }
796 mLock.unlock();
797 svc->doReleaseOutput(data->mIO);
798 mLock.lock();
799 delete data;
800 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700801 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000802 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700803 }
804 delete command;
805 waitTime = INT64_MAX;
806 } else {
807 waitTime = mAudioCommands[0]->mTime - curTime;
808 break;
809 }
810 }
811 // release delayed commands wake lock
Eric Laurentbfb1b832013-01-07 09:53:42 -0800812 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700813 release_wake_lock(mName.string());
814 }
Steve Block3856b092011-10-20 11:56:00 +0100815 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700816 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100817 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700818 }
819 mLock.unlock();
820 return false;
821}
822
823status_t AudioPolicyService::AudioCommandThread::dump(int fd)
824{
825 const size_t SIZE = 256;
826 char buffer[SIZE];
827 String8 result;
828
829 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
830 result.append(buffer);
831 write(fd, result.string(), result.size());
832
833 bool locked = tryLock(mLock);
834 if (!locked) {
835 String8 result2(kCmdDeadlockedString);
836 write(fd, result2.string(), result2.size());
837 }
838
839 snprintf(buffer, SIZE, "- Commands:\n");
840 result = String8(buffer);
841 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800842 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700843 mAudioCommands[i]->dump(buffer, SIZE);
844 result.append(buffer);
845 }
846 result.append(" Last Command\n");
847 mLastCommand.dump(buffer, SIZE);
848 result.append(buffer);
849
850 write(fd, result.string(), result.size());
851
852 if (locked) mLock.unlock();
853
854 return NO_ERROR;
855}
856
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800857void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
858 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700859{
860 AudioCommand *command = new AudioCommand();
861 command->mCommand = START_TONE;
862 ToneData *data = new ToneData();
863 data->mType = type;
864 data->mStream = stream;
865 command->mParam = (void *)data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700866 Mutex::Autolock _l(mLock);
867 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100868 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700869 mWaitWorkCV.signal();
870}
871
872void AudioPolicyService::AudioCommandThread::stopToneCommand()
873{
874 AudioCommand *command = new AudioCommand();
875 command->mCommand = STOP_TONE;
876 command->mParam = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700877 Mutex::Autolock _l(mLock);
878 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100879 ALOGV("AudioCommandThread() adding tone stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700880 mWaitWorkCV.signal();
881}
882
Glenn Kastenfff6d712012-01-12 16:38:12 -0800883status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700884 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800885 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700886 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700887{
888 status_t status = NO_ERROR;
889
890 AudioCommand *command = new AudioCommand();
891 command->mCommand = SET_VOLUME;
892 VolumeData *data = new VolumeData();
893 data->mStream = stream;
894 data->mVolume = volume;
895 data->mIO = output;
896 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700897 Mutex::Autolock _l(mLock);
898 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100899 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700900 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700901 mWaitWorkCV.signal();
902 if (command->mWaitStatus) {
903 command->mCond.wait(mLock);
904 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100905 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700906 }
907 return status;
908}
909
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800910status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700911 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700912 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700913{
914 status_t status = NO_ERROR;
915
916 AudioCommand *command = new AudioCommand();
917 command->mCommand = SET_PARAMETERS;
918 ParametersData *data = new ParametersData();
919 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700920 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700921 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700922 Mutex::Autolock _l(mLock);
923 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100924 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700925 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700926 mWaitWorkCV.signal();
927 if (command->mWaitStatus) {
928 command->mCond.wait(mLock);
929 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100930 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700931 }
932 return status;
933}
934
935status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
936{
937 status_t status = NO_ERROR;
938
939 AudioCommand *command = new AudioCommand();
940 command->mCommand = SET_VOICE_VOLUME;
941 VoiceVolumeData *data = new VoiceVolumeData();
942 data->mVolume = volume;
943 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944 Mutex::Autolock _l(mLock);
945 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100946 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947 mWaitWorkCV.signal();
948 if (command->mWaitStatus) {
949 command->mCond.wait(mLock);
950 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100951 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700952 }
953 return status;
954}
955
Eric Laurentbfb1b832013-01-07 09:53:42 -0800956void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
957 audio_stream_type_t stream,
958 int session)
959{
960 AudioCommand *command = new AudioCommand();
961 command->mCommand = STOP_OUTPUT;
962 StopOutputData *data = new StopOutputData();
963 data->mIO = output;
964 data->mStream = stream;
965 data->mSession = session;
966 command->mParam = (void *)data;
967 Mutex::Autolock _l(mLock);
968 insertCommand_l(command);
969 ALOGV("AudioCommandThread() adding stop output %d", output);
970 mWaitWorkCV.signal();
971}
972
973void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
974{
975 AudioCommand *command = new AudioCommand();
976 command->mCommand = RELEASE_OUTPUT;
977 ReleaseOutputData *data = new ReleaseOutputData();
978 data->mIO = output;
979 command->mParam = (void *)data;
980 Mutex::Autolock _l(mLock);
981 insertCommand_l(command);
982 ALOGV("AudioCommandThread() adding release output %d", output);
983 mWaitWorkCV.signal();
984}
985
Mathias Agopian65ab4712010-07-14 17:59:35 -0700986// insertCommand_l() must be called with mLock held
987void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
988{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800989 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700990 Vector <AudioCommand *> removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700991 command->mTime = systemTime() + milliseconds(delayMs);
992
993 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800994 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700995 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
996 }
997
998 // check same pending commands with later time stamps and eliminate them
999 for (i = mAudioCommands.size()-1; i >= 0; i--) {
1000 AudioCommand *command2 = mAudioCommands[i];
1001 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1002 if (command2->mTime <= command->mTime) break;
1003 if (command2->mCommand != command->mCommand) continue;
1004
1005 switch (command->mCommand) {
1006 case SET_PARAMETERS: {
1007 ParametersData *data = (ParametersData *)command->mParam;
1008 ParametersData *data2 = (ParametersData *)command2->mParam;
1009 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001010 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001011 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001012 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1013 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1014 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001015 String8 key;
1016 String8 value;
1017 param.getAt(j, key, value);
1018 for (size_t k = 0; k < param2.size(); k++) {
1019 String8 key2;
1020 String8 value2;
1021 param2.getAt(k, key2, value2);
1022 if (key2 == key) {
1023 param2.remove(key2);
1024 ALOGV("Filtering out parameter %s", key2.string());
1025 break;
1026 }
1027 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001028 }
1029 // if all keys have been filtered out, remove the command.
1030 // otherwise, update the key value pairs
1031 if (param2.size() == 0) {
1032 removedCommands.add(command2);
1033 } else {
1034 data2->mKeyValuePairs = param2.toString();
1035 }
Eric Laurent21e54562013-09-23 12:08:05 -07001036 command->mTime = command2->mTime;
1037 // force delayMs to non 0 so that code below does not request to wait for
1038 // command status as the command is now delayed
1039 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001040 } break;
1041
1042 case SET_VOLUME: {
1043 VolumeData *data = (VolumeData *)command->mParam;
1044 VolumeData *data2 = (VolumeData *)command2->mParam;
1045 if (data->mIO != data2->mIO) break;
1046 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001047 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001048 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001049 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001050 command->mTime = command2->mTime;
1051 // force delayMs to non 0 so that code below does not request to wait for
1052 // command status as the command is now delayed
1053 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001054 } break;
1055 case START_TONE:
1056 case STOP_TONE:
1057 default:
1058 break;
1059 }
1060 }
1061
1062 // remove filtered commands
1063 for (size_t j = 0; j < removedCommands.size(); j++) {
1064 // removed commands always have time stamps greater than current command
1065 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
1066 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +01001067 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001068 mAudioCommands.removeAt(k);
1069 break;
1070 }
1071 }
1072 }
1073 removedCommands.clear();
1074
Eric Laurent21e54562013-09-23 12:08:05 -07001075 // wait for status only if delay is 0
1076 if (delayMs == 0) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001077 command->mWaitStatus = true;
1078 } else {
1079 command->mWaitStatus = false;
1080 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001081
Mathias Agopian65ab4712010-07-14 17:59:35 -07001082 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +01001083 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -07001084 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001085 mAudioCommands.insertAt(command, i + 1);
1086}
1087
1088void AudioPolicyService::AudioCommandThread::exit()
1089{
Steve Block3856b092011-10-20 11:56:00 +01001090 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001091 {
1092 AutoMutex _l(mLock);
1093 requestExit();
1094 mWaitWorkCV.signal();
1095 }
1096 requestExitAndWait();
1097}
1098
1099void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1100{
1101 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1102 mCommand,
1103 (int)ns2s(mTime),
1104 (int)ns2ms(mTime)%1000,
1105 mWaitStatus,
1106 mParam);
1107}
1108
Dima Zavinfce7a472011-04-19 22:30:36 -07001109/******* helpers for the service_ops callbacks defined below *********/
1110void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1111 const char *keyValuePairs,
1112 int delayMs)
1113{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001114 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001115 delayMs);
1116}
1117
1118int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1119 float volume,
1120 audio_io_handle_t output,
1121 int delayMs)
1122{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001123 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001124 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001125}
1126
1127int AudioPolicyService::startTone(audio_policy_tone_t tone,
1128 audio_stream_type_t stream)
1129{
1130 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
Steve Block29357bc2012-01-06 19:20:56 +00001131 ALOGE("startTone: illegal tone requested (%d)", tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001132 if (stream != AUDIO_STREAM_VOICE_CALL)
Steve Block29357bc2012-01-06 19:20:56 +00001133 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001134 tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001135 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1136 AUDIO_STREAM_VOICE_CALL);
1137 return 0;
1138}
1139
1140int AudioPolicyService::stopTone()
1141{
1142 mTonePlaybackThread->stopToneCommand();
1143 return 0;
1144}
1145
1146int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1147{
1148 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1149}
1150
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001151bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
1152{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001153 if (mpAudioPolicy == NULL) {
1154 ALOGV("mpAudioPolicy == NULL");
1155 return false;
1156 }
1157
1158 if (mpAudioPolicy->is_offload_supported == NULL) {
1159 ALOGV("HAL does not implement is_offload_supported");
1160 return false;
1161 }
1162
1163 return mpAudioPolicy->is_offload_supported(mpAudioPolicy, &info);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001164}
1165
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001166// ----------------------------------------------------------------------------
1167// Audio pre-processing configuration
1168// ----------------------------------------------------------------------------
1169
Glenn Kasten8dad0e32012-01-09 08:41:22 -08001170/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001171 MIC_SRC_TAG,
1172 VOICE_UL_SRC_TAG,
1173 VOICE_DL_SRC_TAG,
1174 VOICE_CALL_SRC_TAG,
1175 CAMCORDER_SRC_TAG,
1176 VOICE_REC_SRC_TAG,
1177 VOICE_COMM_SRC_TAG
1178};
1179
1180// returns the audio_source_t enum corresponding to the input source name or
1181// AUDIO_SOURCE_CNT is no match found
1182audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
1183{
1184 int i;
1185 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
1186 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001187 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001188 break;
1189 }
1190 }
1191 return (audio_source_t)i;
1192}
1193
1194size_t AudioPolicyService::growParamSize(char *param,
1195 size_t size,
1196 size_t *curSize,
1197 size_t *totSize)
1198{
1199 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
1200 size_t pos = ((*curSize - 1 ) / size + 1) * size;
1201
1202 if (pos + size > *totSize) {
1203 while (pos + size > *totSize) {
1204 *totSize += ((*totSize + 7) / 8) * 4;
1205 }
1206 param = (char *)realloc(param, *totSize);
1207 }
1208 *curSize = pos + size;
1209 return pos;
1210}
1211
1212size_t AudioPolicyService::readParamValue(cnode *node,
1213 char *param,
1214 size_t *curSize,
1215 size_t *totSize)
1216{
1217 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
1218 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
1219 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001220 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001221 return sizeof(short);
1222 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
1223 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
1224 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001225 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001226 return sizeof(int);
1227 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
1228 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
1229 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001230 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001231 return sizeof(float);
1232 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
1233 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
1234 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1235 *(bool *)((char *)param + pos) = false;
1236 } else {
1237 *(bool *)((char *)param + pos) = true;
1238 }
Steve Block3856b092011-10-20 11:56:00 +01001239 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001240 return sizeof(bool);
1241 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1242 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1243 if (*curSize + len + 1 > *totSize) {
1244 *totSize = *curSize + len + 1;
1245 param = (char *)realloc(param, *totSize);
1246 }
1247 strncpy(param + *curSize, node->value, len);
1248 *curSize += len;
1249 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +01001250 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001251 return len;
1252 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001253 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001254 return 0;
1255}
1256
1257effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1258{
1259 cnode *param;
1260 cnode *value;
1261 size_t curSize = sizeof(effect_param_t);
1262 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1263 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1264
1265 param = config_find(root, PARAM_TAG);
1266 value = config_find(root, VALUE_TAG);
1267 if (param == NULL && value == NULL) {
1268 // try to parse simple parameter form {int int}
1269 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -08001270 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001271 // Note: that a pair of random strings is read as 0 0
1272 int *ptr = (int *)fx_param->data;
1273 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +00001274 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001275 *ptr++ = atoi(param->name);
1276 *ptr = atoi(param->value);
1277 fx_param->psize = sizeof(int);
1278 fx_param->vsize = sizeof(int);
1279 return fx_param;
1280 }
1281 }
1282 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001283 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001284 goto error;
1285 }
1286
1287 fx_param->psize = 0;
1288 param = param->first_child;
1289 while (param) {
Steve Block3856b092011-10-20 11:56:00 +01001290 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001291 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1292 if (size == 0) {
1293 goto error;
1294 }
1295 fx_param->psize += size;
1296 param = param->next;
1297 }
1298
1299 // align start of value field on 32 bit boundary
1300 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1301
1302 fx_param->vsize = 0;
1303 value = value->first_child;
1304 while (value) {
Steve Block3856b092011-10-20 11:56:00 +01001305 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001306 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1307 if (size == 0) {
1308 goto error;
1309 }
1310 fx_param->vsize += size;
1311 value = value->next;
1312 }
1313
1314 return fx_param;
1315
1316error:
1317 delete fx_param;
1318 return NULL;
1319}
1320
1321void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1322{
1323 cnode *node = root->first_child;
1324 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001325 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001326 effect_param_t *param = loadEffectParameter(node);
1327 if (param == NULL) {
1328 node = node->next;
1329 continue;
1330 }
1331 params.add(param);
1332 node = node->next;
1333 }
1334}
1335
1336AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1337 cnode *root,
1338 const Vector <EffectDesc *>& effects)
1339{
1340 cnode *node = root->first_child;
1341 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001342 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001343 return NULL;
1344 }
1345 InputSourceDesc *source = new InputSourceDesc();
1346 while (node) {
1347 size_t i;
1348 for (i = 0; i < effects.size(); i++) {
1349 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001350 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001351 break;
1352 }
1353 }
1354 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +01001355 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001356 node = node->next;
1357 continue;
1358 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001359 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001360 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +01001361 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001362 source->mEffects.add(effect);
1363 node = node->next;
1364 }
1365 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001366 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001367 delete source;
1368 return NULL;
1369 }
1370 return source;
1371}
1372
1373status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1374{
1375 cnode *node = config_find(root, PREPROCESSING_TAG);
1376 if (node == NULL) {
1377 return -ENOENT;
1378 }
1379 node = node->first_child;
1380 while (node) {
1381 audio_source_t source = inputSourceNameToEnum(node->name);
1382 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001383 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001384 node = node->next;
1385 continue;
1386 }
Steve Block3856b092011-10-20 11:56:00 +01001387 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001388 InputSourceDesc *desc = loadInputSource(node, effects);
1389 if (desc == NULL) {
1390 node = node->next;
1391 continue;
1392 }
1393 mInputSources.add(source, desc);
1394 node = node->next;
1395 }
1396 return NO_ERROR;
1397}
1398
1399AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1400{
1401 cnode *node = config_find(root, UUID_TAG);
1402 if (node == NULL) {
1403 return NULL;
1404 }
1405 effect_uuid_t uuid;
1406 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001407 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001408 return NULL;
1409 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001410 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001411}
1412
1413status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1414{
1415 cnode *node = config_find(root, EFFECTS_TAG);
1416 if (node == NULL) {
1417 return -ENOENT;
1418 }
1419 node = node->first_child;
1420 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001421 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001422 EffectDesc *effect = loadEffect(node);
1423 if (effect == NULL) {
1424 node = node->next;
1425 continue;
1426 }
1427 effects.add(effect);
1428 node = node->next;
1429 }
1430 return NO_ERROR;
1431}
1432
1433status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1434{
1435 cnode *root;
1436 char *data;
1437
1438 data = (char *)load_file(path, NULL);
1439 if (data == NULL) {
1440 return -ENODEV;
1441 }
1442 root = config_node("", "");
1443 config_load(root, data);
1444
1445 Vector <EffectDesc *> effects;
1446 loadEffects(root, effects);
1447 loadInputSources(root, effects);
1448
1449 config_free(root);
1450 free(root);
1451 free(data);
1452
1453 return NO_ERROR;
1454}
1455
Dima Zavinfce7a472011-04-19 22:30:36 -07001456/* implementation of the interface to the policy manager */
1457extern "C" {
1458
Eric Laurenta4c5a552012-03-29 10:12:40 -07001459
1460static audio_module_handle_t aps_load_hw_module(void *service,
1461 const char *name)
Dima Zavinfce7a472011-04-19 22:30:36 -07001462{
1463 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001464 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001465 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001466 return 0;
1467 }
1468
Eric Laurenta4c5a552012-03-29 10:12:40 -07001469 return af->loadHwModule(name);
1470}
1471
1472// deprecated: replaced by aps_open_output_on_module()
1473static audio_io_handle_t aps_open_output(void *service,
1474 audio_devices_t *pDevices,
1475 uint32_t *pSamplingRate,
1476 audio_format_t *pFormat,
1477 audio_channel_mask_t *pChannelMask,
1478 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001479 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001480{
1481 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1482 if (af == 0) {
1483 ALOGW("%s: could not get AudioFlinger", __func__);
1484 return 0;
1485 }
1486
1487 return af->openOutput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask,
1488 pLatencyMs, flags);
1489}
1490
1491static audio_io_handle_t aps_open_output_on_module(void *service,
1492 audio_module_handle_t module,
1493 audio_devices_t *pDevices,
1494 uint32_t *pSamplingRate,
1495 audio_format_t *pFormat,
1496 audio_channel_mask_t *pChannelMask,
1497 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001498 audio_output_flags_t flags,
1499 const audio_offload_info_t *offloadInfo)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001500{
1501 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1502 if (af == 0) {
1503 ALOGW("%s: could not get AudioFlinger", __func__);
1504 return 0;
1505 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001506 return af->openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
Eric Laurentbfb1b832013-01-07 09:53:42 -08001507 pLatencyMs, flags, offloadInfo);
Dima Zavinfce7a472011-04-19 22:30:36 -07001508}
1509
1510static audio_io_handle_t aps_open_dup_output(void *service,
1511 audio_io_handle_t output1,
1512 audio_io_handle_t output2)
1513{
1514 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001515 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001516 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001517 return 0;
1518 }
1519 return af->openDuplicateOutput(output1, output2);
1520}
1521
1522static int aps_close_output(void *service, audio_io_handle_t output)
1523{
1524 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001525 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001526 return PERMISSION_DENIED;
1527
1528 return af->closeOutput(output);
1529}
1530
1531static int aps_suspend_output(void *service, audio_io_handle_t output)
1532{
1533 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001534 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001535 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001536 return PERMISSION_DENIED;
1537 }
1538
1539 return af->suspendOutput(output);
1540}
1541
1542static int aps_restore_output(void *service, audio_io_handle_t output)
1543{
1544 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001545 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001546 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001547 return PERMISSION_DENIED;
1548 }
1549
1550 return af->restoreOutput(output);
1551}
1552
Glenn Kasten20010052012-06-22 13:43:51 -07001553// deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
Dima Zavinfce7a472011-04-19 22:30:36 -07001554static audio_io_handle_t aps_open_input(void *service,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001555 audio_devices_t *pDevices,
1556 uint32_t *pSamplingRate,
1557 audio_format_t *pFormat,
1558 audio_channel_mask_t *pChannelMask,
1559 audio_in_acoustics_t acoustics)
Dima Zavinfce7a472011-04-19 22:30:36 -07001560{
1561 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001562 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001563 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001564 return 0;
1565 }
1566
Eric Laurenta4c5a552012-03-29 10:12:40 -07001567 return af->openInput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask);
1568}
1569
1570static audio_io_handle_t aps_open_input_on_module(void *service,
1571 audio_module_handle_t module,
1572 audio_devices_t *pDevices,
1573 uint32_t *pSamplingRate,
1574 audio_format_t *pFormat,
1575 audio_channel_mask_t *pChannelMask)
1576{
1577 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1578 if (af == 0) {
1579 ALOGW("%s: could not get AudioFlinger", __func__);
1580 return 0;
1581 }
1582
1583 return af->openInput(module, pDevices, pSamplingRate, pFormat, pChannelMask);
Dima Zavinfce7a472011-04-19 22:30:36 -07001584}
1585
1586static int aps_close_input(void *service, audio_io_handle_t input)
1587{
1588 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001589 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001590 return PERMISSION_DENIED;
1591
1592 return af->closeInput(input);
1593}
1594
1595static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1596 audio_io_handle_t output)
1597{
1598 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001599 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001600 return PERMISSION_DENIED;
1601
1602 return af->setStreamOutput(stream, output);
1603}
1604
1605static int aps_move_effects(void *service, int session,
1606 audio_io_handle_t src_output,
1607 audio_io_handle_t dst_output)
1608{
1609 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001610 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001611 return PERMISSION_DENIED;
1612
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001613 return af->moveEffects(session, src_output, dst_output);
Dima Zavinfce7a472011-04-19 22:30:36 -07001614}
1615
1616static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1617 const char *keys)
1618{
1619 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1620 return strdup(result.string());
1621}
1622
1623static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1624 const char *kv_pairs, int delay_ms)
1625{
1626 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1627
1628 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1629}
1630
1631static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1632 float volume, audio_io_handle_t output,
1633 int delay_ms)
1634{
1635 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1636
1637 return audioPolicyService->setStreamVolume(stream, volume, output,
1638 delay_ms);
1639}
1640
1641static int aps_start_tone(void *service, audio_policy_tone_t tone,
1642 audio_stream_type_t stream)
1643{
1644 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1645
1646 return audioPolicyService->startTone(tone, stream);
1647}
1648
1649static int aps_stop_tone(void *service)
1650{
1651 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1652
1653 return audioPolicyService->stopTone();
1654}
1655
1656static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1657{
1658 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1659
1660 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1661}
1662
1663}; // extern "C"
1664
1665namespace {
1666 struct audio_policy_service_ops aps_ops = {
1667 open_output : aps_open_output,
1668 open_duplicate_output : aps_open_dup_output,
1669 close_output : aps_close_output,
1670 suspend_output : aps_suspend_output,
1671 restore_output : aps_restore_output,
1672 open_input : aps_open_input,
1673 close_input : aps_close_input,
1674 set_stream_volume : aps_set_stream_volume,
1675 set_stream_output : aps_set_stream_output,
1676 set_parameters : aps_set_parameters,
1677 get_parameters : aps_get_parameters,
1678 start_tone : aps_start_tone,
1679 stop_tone : aps_stop_tone,
1680 set_voice_volume : aps_set_voice_volume,
1681 move_effects : aps_move_effects,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001682 load_hw_module : aps_load_hw_module,
1683 open_output_on_module : aps_open_output_on_module,
1684 open_input_on_module : aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001685 };
1686}; // namespace <unnamed>
1687
Mathias Agopian65ab4712010-07-14 17:59:35 -07001688}; // namespace android