blob: 900b411969c5349af19c1626c0a2d4cec46b3dad [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
299 if (uint32_t(inputSource) >= AUDIO_SOURCE_CNT) {
300 return 0;
301 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700302 Mutex::Autolock _l(mLock);
Glenn Kasten20010052012-06-22 13:43:51 -0700303 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700304 audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
Glenn Kasten8af901c2012-11-01 11:11:38 -0700305 format, channelMask, (audio_in_acoustics_t) 0);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700306
307 if (input == 0) {
308 return input;
309 }
310 // create audio pre processors according to input source
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800311 ssize_t index = mInputSources.indexOfKey(inputSource);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700312 if (index < 0) {
313 return input;
314 }
315 ssize_t idx = mInputs.indexOfKey(input);
316 InputDesc *inputDesc;
317 if (idx < 0) {
Glenn Kasten81872a22012-03-07 16:49:22 -0800318 inputDesc = new InputDesc(audioSession);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700319 mInputs.add(input, inputDesc);
320 } else {
321 inputDesc = mInputs.valueAt(idx);
322 }
323
324 Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects;
325 for (size_t i = 0; i < effects.size(); i++) {
326 EffectDesc *effect = effects[i];
327 sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input);
328 status_t status = fx->initCheck();
329 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000330 ALOGW("Failed to create Fx %s on input %d", effect->mName, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700331 // fx goes out of scope and strong ref on AudioEffect is released
332 continue;
333 }
334 for (size_t j = 0; j < effect->mParams.size(); j++) {
335 fx->setParameter(effect->mParams[j]);
336 }
337 inputDesc->mEffects.add(fx);
338 }
339 setPreProcessorEnabled(inputDesc, true);
340 return input;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700341}
342
343status_t AudioPolicyService::startInput(audio_io_handle_t input)
344{
Dima Zavinfce7a472011-04-19 22:30:36 -0700345 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700346 return NO_INIT;
347 }
348 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700349
Dima Zavinfce7a472011-04-19 22:30:36 -0700350 return mpAudioPolicy->start_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351}
352
353status_t AudioPolicyService::stopInput(audio_io_handle_t input)
354{
Dima Zavinfce7a472011-04-19 22:30:36 -0700355 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700356 return NO_INIT;
357 }
358 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700359
Dima Zavinfce7a472011-04-19 22:30:36 -0700360 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700361}
362
363void AudioPolicyService::releaseInput(audio_io_handle_t input)
364{
Dima Zavinfce7a472011-04-19 22:30:36 -0700365 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700366 return;
367 }
368 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700369 mpAudioPolicy->release_input(mpAudioPolicy, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700370
371 ssize_t index = mInputs.indexOfKey(input);
372 if (index < 0) {
373 return;
374 }
375 InputDesc *inputDesc = mInputs.valueAt(index);
376 setPreProcessorEnabled(inputDesc, false);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700377 delete inputDesc;
378 mInputs.removeItemsAt(index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700379}
380
Dima Zavinfce7a472011-04-19 22:30:36 -0700381status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700382 int indexMin,
383 int indexMax)
384{
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);
Dima Zavinfce7a472011-04-19 22:30:36 -0700395 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700396 return NO_ERROR;
397}
398
Eric Laurent83844cc2011-11-18 16:43:31 -0800399status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
400 int index,
401 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700402{
Dima Zavinfce7a472011-04-19 22:30:36 -0700403 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700404 return NO_INIT;
405 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800406 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700407 return PERMISSION_DENIED;
408 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800409 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700410 return BAD_VALUE;
411 }
Eric Laurent5f121362012-06-15 14:45:03 -0700412 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800413 if (mpAudioPolicy->set_stream_volume_index_for_device) {
414 return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
415 stream,
416 index,
417 device);
418 } else {
419 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
420 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700421}
422
Eric Laurent83844cc2011-11-18 16:43:31 -0800423status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
424 int *index,
425 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700426{
Dima Zavinfce7a472011-04-19 22:30:36 -0700427 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700428 return NO_INIT;
429 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800430 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700431 return BAD_VALUE;
432 }
Eric Laurent5f121362012-06-15 14:45:03 -0700433 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800434 if (mpAudioPolicy->get_stream_volume_index_for_device) {
435 return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
436 stream,
437 index,
438 device);
439 } else {
440 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
441 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700442}
443
Dima Zavinfce7a472011-04-19 22:30:36 -0700444uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
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 0;
448 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700449 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurentde070132010-07-13 04:45:46 -0700450}
451
Eric Laurent63742522012-03-08 13:42:42 -0800452//audio policy: use audio_device_t appropriately
453
454audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800455{
Dima Zavinfce7a472011-04-19 22:30:36 -0700456 if (mpAudioPolicy == NULL) {
Eric Laurent63742522012-03-08 13:42:42 -0800457 return (audio_devices_t)0;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800458 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700459 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800460}
461
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700462audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700463{
Dima Zavinfce7a472011-04-19 22:30:36 -0700464 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700465 return NO_INIT;
466 }
467 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700468 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700469}
470
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700471status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700472 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700473 uint32_t strategy,
474 int session,
475 int id)
476{
Dima Zavinfce7a472011-04-19 22:30:36 -0700477 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700478 return NO_INIT;
479 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700480 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700481}
482
483status_t AudioPolicyService::unregisterEffect(int id)
484{
Dima Zavinfce7a472011-04-19 22:30:36 -0700485 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700486 return NO_INIT;
487 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700488 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700489}
490
Eric Laurentdb7c0792011-08-10 10:37:50 -0700491status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
492{
493 if (mpAudioPolicy == NULL) {
494 return NO_INIT;
495 }
496 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
497}
498
Glenn Kastenfff6d712012-01-12 16:38:12 -0800499bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800500{
Dima Zavinfce7a472011-04-19 22:30:36 -0700501 if (mpAudioPolicy == NULL) {
Eric Laurenteda6c362011-02-02 09:33:30 -0800502 return 0;
503 }
504 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700505 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800506}
507
Jean-Michel Trivie336f912013-02-04 16:26:02 -0800508bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
509{
510 if (mpAudioPolicy == NULL) {
511 return 0;
512 }
513 Mutex::Autolock _l(mLock);
514 return mpAudioPolicy->is_stream_active_remotely(mpAudioPolicy, stream, inPastMs);
515}
516
Jean-Michel Trivie3f641f2012-10-10 12:11:16 -0700517bool AudioPolicyService::isSourceActive(audio_source_t source) const
518{
519 if (mpAudioPolicy == NULL) {
520 return false;
521 }
522 if (mpAudioPolicy->is_source_active == 0) {
523 return false;
524 }
525 Mutex::Autolock _l(mLock);
526 return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
527}
528
Eric Laurent57dae992011-07-24 13:36:09 -0700529status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
530 effect_descriptor_t *descriptors,
531 uint32_t *count)
532{
533
534 if (mpAudioPolicy == NULL) {
535 *count = 0;
536 return NO_INIT;
537 }
538 Mutex::Autolock _l(mLock);
539 status_t status = NO_ERROR;
540
541 size_t index;
542 for (index = 0; index < mInputs.size(); index++) {
543 if (mInputs.valueAt(index)->mSessionId == audioSession) {
544 break;
545 }
546 }
547 if (index == mInputs.size()) {
548 *count = 0;
549 return BAD_VALUE;
550 }
551 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
552
553 for (size_t i = 0; i < effects.size(); i++) {
554 effect_descriptor_t desc = effects[i]->descriptor();
555 if (i < *count) {
Glenn Kastena189a682012-02-20 12:16:30 -0800556 descriptors[i] = desc;
Eric Laurent57dae992011-07-24 13:36:09 -0700557 }
558 }
559 if (effects.size() > *count) {
560 status = NO_MEMORY;
561 }
562 *count = effects.size();
563 return status;
564}
565
Mathias Agopian65ab4712010-07-14 17:59:35 -0700566void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700567 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700568 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700569}
570
571static bool tryLock(Mutex& mutex)
572{
573 bool locked = false;
574 for (int i = 0; i < kDumpLockRetries; ++i) {
575 if (mutex.tryLock() == NO_ERROR) {
576 locked = true;
577 break;
578 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800579 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700580 }
581 return locked;
582}
583
584status_t AudioPolicyService::dumpInternals(int fd)
585{
586 const size_t SIZE = 256;
587 char buffer[SIZE];
588 String8 result;
589
Dima Zavinfce7a472011-04-19 22:30:36 -0700590 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700591 result.append(buffer);
592 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
593 result.append(buffer);
594 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
595 result.append(buffer);
596
597 write(fd, result.string(), result.size());
598 return NO_ERROR;
599}
600
601status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
602{
Glenn Kasten44deb052012-02-05 18:09:08 -0800603 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700604 dumpPermissionDenial(fd);
605 } else {
606 bool locked = tryLock(mLock);
607 if (!locked) {
608 String8 result(kDeadlockedString);
609 write(fd, result.string(), result.size());
610 }
611
612 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800613 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700614 mAudioCommandThread->dump(fd);
615 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800616 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700617 mTonePlaybackThread->dump(fd);
618 }
619
Dima Zavinfce7a472011-04-19 22:30:36 -0700620 if (mpAudioPolicy) {
621 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700622 }
623
624 if (locked) mLock.unlock();
625 }
626 return NO_ERROR;
627}
628
629status_t AudioPolicyService::dumpPermissionDenial(int fd)
630{
631 const size_t SIZE = 256;
632 char buffer[SIZE];
633 String8 result;
634 snprintf(buffer, SIZE, "Permission Denial: "
635 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
636 IPCThreadState::self()->getCallingPid(),
637 IPCThreadState::self()->getCallingUid());
638 result.append(buffer);
639 write(fd, result.string(), result.size());
640 return NO_ERROR;
641}
642
Glenn Kasten81872a22012-03-07 16:49:22 -0800643void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700644{
Glenn Kasten81872a22012-03-07 16:49:22 -0800645 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700646 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800647 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700648 }
649}
650
Mathias Agopian65ab4712010-07-14 17:59:35 -0700651status_t AudioPolicyService::onTransact(
652 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
653{
654 return BnAudioPolicyService::onTransact(code, data, reply, flags);
655}
656
657
Mathias Agopian65ab4712010-07-14 17:59:35 -0700658// ----------- AudioPolicyService::AudioCommandThread implementation ----------
659
Eric Laurentbfb1b832013-01-07 09:53:42 -0800660AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
661 const wp<AudioPolicyService>& service)
662 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700663{
664 mpToneGenerator = NULL;
665}
666
667
668AudioPolicyService::AudioCommandThread::~AudioCommandThread()
669{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800670 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700671 release_wake_lock(mName.string());
672 }
673 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800674 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700675}
676
677void AudioPolicyService::AudioCommandThread::onFirstRef()
678{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800679 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700680}
681
682bool AudioPolicyService::AudioCommandThread::threadLoop()
683{
684 nsecs_t waitTime = INT64_MAX;
685
686 mLock.lock();
687 while (!exitPending())
688 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700689 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690 nsecs_t curTime = systemTime();
691 // commands are sorted by increasing time stamp: execute them from index 0 and up
692 if (mAudioCommands[0]->mTime <= curTime) {
693 AudioCommand *command = mAudioCommands[0];
694 mAudioCommands.removeAt(0);
695 mLastCommand = *command;
696
697 switch (command->mCommand) {
698 case START_TONE: {
699 mLock.unlock();
700 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100701 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700702 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800703 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
705 mpToneGenerator->startTone(data->mType);
706 delete data;
707 mLock.lock();
708 }break;
709 case STOP_TONE: {
710 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100711 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712 if (mpToneGenerator != NULL) {
713 mpToneGenerator->stopTone();
714 delete mpToneGenerator;
715 mpToneGenerator = NULL;
716 }
717 mLock.lock();
718 }break;
719 case SET_VOLUME: {
720 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100721 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700722 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
723 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
724 data->mVolume,
725 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700726 if (command->mWaitStatus) {
727 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100728 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700729 }
730 delete data;
731 }break;
732 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700733 ParametersData *data = (ParametersData *)command->mParam;
734 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
735 data->mKeyValuePairs.string(), data->mIO);
736 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
737 if (command->mWaitStatus) {
738 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100739 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700740 }
741 delete data;
742 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700743 case SET_VOICE_VOLUME: {
744 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100745 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700746 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700747 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
748 if (command->mWaitStatus) {
749 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100750 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700751 }
752 delete data;
753 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800754 case STOP_OUTPUT: {
755 StopOutputData *data = (StopOutputData *)command->mParam;
756 ALOGV("AudioCommandThread() processing stop output %d",
757 data->mIO);
758 sp<AudioPolicyService> svc = mService.promote();
759 if (svc == 0) {
760 break;
761 }
762 mLock.unlock();
763 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
764 mLock.lock();
765 delete data;
766 }break;
767 case RELEASE_OUTPUT: {
768 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam;
769 ALOGV("AudioCommandThread() processing release output %d",
770 data->mIO);
771 sp<AudioPolicyService> svc = mService.promote();
772 if (svc == 0) {
773 break;
774 }
775 mLock.unlock();
776 svc->doReleaseOutput(data->mIO);
777 mLock.lock();
778 delete data;
779 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700780 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000781 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700782 }
783 delete command;
784 waitTime = INT64_MAX;
785 } else {
786 waitTime = mAudioCommands[0]->mTime - curTime;
787 break;
788 }
789 }
790 // release delayed commands wake lock
Eric Laurentbfb1b832013-01-07 09:53:42 -0800791 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700792 release_wake_lock(mName.string());
793 }
Steve Block3856b092011-10-20 11:56:00 +0100794 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700795 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100796 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700797 }
798 mLock.unlock();
799 return false;
800}
801
802status_t AudioPolicyService::AudioCommandThread::dump(int fd)
803{
804 const size_t SIZE = 256;
805 char buffer[SIZE];
806 String8 result;
807
808 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
809 result.append(buffer);
810 write(fd, result.string(), result.size());
811
812 bool locked = tryLock(mLock);
813 if (!locked) {
814 String8 result2(kCmdDeadlockedString);
815 write(fd, result2.string(), result2.size());
816 }
817
818 snprintf(buffer, SIZE, "- Commands:\n");
819 result = String8(buffer);
820 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800821 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700822 mAudioCommands[i]->dump(buffer, SIZE);
823 result.append(buffer);
824 }
825 result.append(" Last Command\n");
826 mLastCommand.dump(buffer, SIZE);
827 result.append(buffer);
828
829 write(fd, result.string(), result.size());
830
831 if (locked) mLock.unlock();
832
833 return NO_ERROR;
834}
835
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800836void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
837 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700838{
839 AudioCommand *command = new AudioCommand();
840 command->mCommand = START_TONE;
841 ToneData *data = new ToneData();
842 data->mType = type;
843 data->mStream = stream;
844 command->mParam = (void *)data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700845 Mutex::Autolock _l(mLock);
846 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100847 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700848 mWaitWorkCV.signal();
849}
850
851void AudioPolicyService::AudioCommandThread::stopToneCommand()
852{
853 AudioCommand *command = new AudioCommand();
854 command->mCommand = STOP_TONE;
855 command->mParam = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700856 Mutex::Autolock _l(mLock);
857 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100858 ALOGV("AudioCommandThread() adding tone stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700859 mWaitWorkCV.signal();
860}
861
Glenn Kastenfff6d712012-01-12 16:38:12 -0800862status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700863 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800864 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700865 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700866{
867 status_t status = NO_ERROR;
868
869 AudioCommand *command = new AudioCommand();
870 command->mCommand = SET_VOLUME;
871 VolumeData *data = new VolumeData();
872 data->mStream = stream;
873 data->mVolume = volume;
874 data->mIO = output;
875 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700876 Mutex::Autolock _l(mLock);
877 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100878 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700879 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700880 mWaitWorkCV.signal();
881 if (command->mWaitStatus) {
882 command->mCond.wait(mLock);
883 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100884 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700885 }
886 return status;
887}
888
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800889status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700890 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700891 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700892{
893 status_t status = NO_ERROR;
894
895 AudioCommand *command = new AudioCommand();
896 command->mCommand = SET_PARAMETERS;
897 ParametersData *data = new ParametersData();
898 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700899 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700900 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700901 Mutex::Autolock _l(mLock);
902 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100903 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700904 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700905 mWaitWorkCV.signal();
906 if (command->mWaitStatus) {
907 command->mCond.wait(mLock);
908 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100909 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700910 }
911 return status;
912}
913
914status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
915{
916 status_t status = NO_ERROR;
917
918 AudioCommand *command = new AudioCommand();
919 command->mCommand = SET_VOICE_VOLUME;
920 VoiceVolumeData *data = new VoiceVolumeData();
921 data->mVolume = volume;
922 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700923 Mutex::Autolock _l(mLock);
924 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100925 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
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
Eric Laurentbfb1b832013-01-07 09:53:42 -0800935void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
936 audio_stream_type_t stream,
937 int session)
938{
939 AudioCommand *command = new AudioCommand();
940 command->mCommand = STOP_OUTPUT;
941 StopOutputData *data = new StopOutputData();
942 data->mIO = output;
943 data->mStream = stream;
944 data->mSession = session;
945 command->mParam = (void *)data;
946 Mutex::Autolock _l(mLock);
947 insertCommand_l(command);
948 ALOGV("AudioCommandThread() adding stop output %d", output);
949 mWaitWorkCV.signal();
950}
951
952void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
953{
954 AudioCommand *command = new AudioCommand();
955 command->mCommand = RELEASE_OUTPUT;
956 ReleaseOutputData *data = new ReleaseOutputData();
957 data->mIO = output;
958 command->mParam = (void *)data;
959 Mutex::Autolock _l(mLock);
960 insertCommand_l(command);
961 ALOGV("AudioCommandThread() adding release output %d", output);
962 mWaitWorkCV.signal();
963}
964
Mathias Agopian65ab4712010-07-14 17:59:35 -0700965// insertCommand_l() must be called with mLock held
966void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
967{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800968 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700969 Vector <AudioCommand *> removedCommands;
Eric Laurentcec4abb2012-07-03 12:23:02 -0700970 nsecs_t time = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971 command->mTime = systemTime() + milliseconds(delayMs);
972
973 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800974 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700975 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
976 }
977
978 // check same pending commands with later time stamps and eliminate them
979 for (i = mAudioCommands.size()-1; i >= 0; i--) {
980 AudioCommand *command2 = mAudioCommands[i];
981 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
982 if (command2->mTime <= command->mTime) break;
983 if (command2->mCommand != command->mCommand) continue;
984
985 switch (command->mCommand) {
986 case SET_PARAMETERS: {
987 ParametersData *data = (ParametersData *)command->mParam;
988 ParametersData *data2 = (ParametersData *)command2->mParam;
989 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100990 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700991 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700992 AudioParameter param = AudioParameter(data->mKeyValuePairs);
993 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
994 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700995 String8 key;
996 String8 value;
997 param.getAt(j, key, value);
998 for (size_t k = 0; k < param2.size(); k++) {
999 String8 key2;
1000 String8 value2;
1001 param2.getAt(k, key2, value2);
1002 if (key2 == key) {
1003 param2.remove(key2);
1004 ALOGV("Filtering out parameter %s", key2.string());
1005 break;
1006 }
1007 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001008 }
1009 // if all keys have been filtered out, remove the command.
1010 // otherwise, update the key value pairs
1011 if (param2.size() == 0) {
1012 removedCommands.add(command2);
1013 } else {
1014 data2->mKeyValuePairs = param2.toString();
1015 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001016 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001017 } break;
1018
1019 case SET_VOLUME: {
1020 VolumeData *data = (VolumeData *)command->mParam;
1021 VolumeData *data2 = (VolumeData *)command2->mParam;
1022 if (data->mIO != data2->mIO) break;
1023 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001024 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001025 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001026 removedCommands.add(command2);
Eric Laurentcec4abb2012-07-03 12:23:02 -07001027 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001028 } break;
1029 case START_TONE:
1030 case STOP_TONE:
1031 default:
1032 break;
1033 }
1034 }
1035
1036 // remove filtered commands
1037 for (size_t j = 0; j < removedCommands.size(); j++) {
1038 // removed commands always have time stamps greater than current command
1039 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
1040 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +01001041 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001042 mAudioCommands.removeAt(k);
1043 break;
1044 }
1045 }
1046 }
1047 removedCommands.clear();
1048
Eric Laurentcec4abb2012-07-03 12:23:02 -07001049 // wait for status only if delay is 0 and command time was not modified above
1050 if (delayMs == 0 && time == 0) {
1051 command->mWaitStatus = true;
1052 } else {
1053 command->mWaitStatus = false;
1054 }
1055 // update command time if modified above
1056 if (time != 0) {
1057 command->mTime = time;
1058 }
1059
Mathias Agopian65ab4712010-07-14 17:59:35 -07001060 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +01001061 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -07001062 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001063 mAudioCommands.insertAt(command, i + 1);
1064}
1065
1066void AudioPolicyService::AudioCommandThread::exit()
1067{
Steve Block3856b092011-10-20 11:56:00 +01001068 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001069 {
1070 AutoMutex _l(mLock);
1071 requestExit();
1072 mWaitWorkCV.signal();
1073 }
1074 requestExitAndWait();
1075}
1076
1077void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1078{
1079 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1080 mCommand,
1081 (int)ns2s(mTime),
1082 (int)ns2ms(mTime)%1000,
1083 mWaitStatus,
1084 mParam);
1085}
1086
Dima Zavinfce7a472011-04-19 22:30:36 -07001087/******* helpers for the service_ops callbacks defined below *********/
1088void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1089 const char *keyValuePairs,
1090 int delayMs)
1091{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001092 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001093 delayMs);
1094}
1095
1096int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1097 float volume,
1098 audio_io_handle_t output,
1099 int delayMs)
1100{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001101 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001102 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001103}
1104
1105int AudioPolicyService::startTone(audio_policy_tone_t tone,
1106 audio_stream_type_t stream)
1107{
1108 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
Steve Block29357bc2012-01-06 19:20:56 +00001109 ALOGE("startTone: illegal tone requested (%d)", tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001110 if (stream != AUDIO_STREAM_VOICE_CALL)
Steve Block29357bc2012-01-06 19:20:56 +00001111 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001112 tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001113 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1114 AUDIO_STREAM_VOICE_CALL);
1115 return 0;
1116}
1117
1118int AudioPolicyService::stopTone()
1119{
1120 mTonePlaybackThread->stopToneCommand();
1121 return 0;
1122}
1123
1124int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1125{
1126 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1127}
1128
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001129bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
1130{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001131 if (mpAudioPolicy == NULL) {
1132 ALOGV("mpAudioPolicy == NULL");
1133 return false;
1134 }
1135
1136 if (mpAudioPolicy->is_offload_supported == NULL) {
1137 ALOGV("HAL does not implement is_offload_supported");
1138 return false;
1139 }
1140
1141 return mpAudioPolicy->is_offload_supported(mpAudioPolicy, &info);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001142}
1143
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001144// ----------------------------------------------------------------------------
1145// Audio pre-processing configuration
1146// ----------------------------------------------------------------------------
1147
Glenn Kasten8dad0e32012-01-09 08:41:22 -08001148/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001149 MIC_SRC_TAG,
1150 VOICE_UL_SRC_TAG,
1151 VOICE_DL_SRC_TAG,
1152 VOICE_CALL_SRC_TAG,
1153 CAMCORDER_SRC_TAG,
1154 VOICE_REC_SRC_TAG,
1155 VOICE_COMM_SRC_TAG
1156};
1157
1158// returns the audio_source_t enum corresponding to the input source name or
1159// AUDIO_SOURCE_CNT is no match found
1160audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
1161{
1162 int i;
1163 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
1164 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001165 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001166 break;
1167 }
1168 }
1169 return (audio_source_t)i;
1170}
1171
1172size_t AudioPolicyService::growParamSize(char *param,
1173 size_t size,
1174 size_t *curSize,
1175 size_t *totSize)
1176{
1177 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
1178 size_t pos = ((*curSize - 1 ) / size + 1) * size;
1179
1180 if (pos + size > *totSize) {
1181 while (pos + size > *totSize) {
1182 *totSize += ((*totSize + 7) / 8) * 4;
1183 }
1184 param = (char *)realloc(param, *totSize);
1185 }
1186 *curSize = pos + size;
1187 return pos;
1188}
1189
1190size_t AudioPolicyService::readParamValue(cnode *node,
1191 char *param,
1192 size_t *curSize,
1193 size_t *totSize)
1194{
1195 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
1196 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
1197 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001198 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001199 return sizeof(short);
1200 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
1201 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
1202 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001203 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001204 return sizeof(int);
1205 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
1206 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
1207 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001208 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001209 return sizeof(float);
1210 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
1211 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
1212 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1213 *(bool *)((char *)param + pos) = false;
1214 } else {
1215 *(bool *)((char *)param + pos) = true;
1216 }
Steve Block3856b092011-10-20 11:56:00 +01001217 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001218 return sizeof(bool);
1219 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1220 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1221 if (*curSize + len + 1 > *totSize) {
1222 *totSize = *curSize + len + 1;
1223 param = (char *)realloc(param, *totSize);
1224 }
1225 strncpy(param + *curSize, node->value, len);
1226 *curSize += len;
1227 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +01001228 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001229 return len;
1230 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001231 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001232 return 0;
1233}
1234
1235effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1236{
1237 cnode *param;
1238 cnode *value;
1239 size_t curSize = sizeof(effect_param_t);
1240 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1241 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1242
1243 param = config_find(root, PARAM_TAG);
1244 value = config_find(root, VALUE_TAG);
1245 if (param == NULL && value == NULL) {
1246 // try to parse simple parameter form {int int}
1247 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -08001248 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001249 // Note: that a pair of random strings is read as 0 0
1250 int *ptr = (int *)fx_param->data;
1251 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +00001252 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001253 *ptr++ = atoi(param->name);
1254 *ptr = atoi(param->value);
1255 fx_param->psize = sizeof(int);
1256 fx_param->vsize = sizeof(int);
1257 return fx_param;
1258 }
1259 }
1260 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001261 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001262 goto error;
1263 }
1264
1265 fx_param->psize = 0;
1266 param = param->first_child;
1267 while (param) {
Steve Block3856b092011-10-20 11:56:00 +01001268 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001269 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1270 if (size == 0) {
1271 goto error;
1272 }
1273 fx_param->psize += size;
1274 param = param->next;
1275 }
1276
1277 // align start of value field on 32 bit boundary
1278 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1279
1280 fx_param->vsize = 0;
1281 value = value->first_child;
1282 while (value) {
Steve Block3856b092011-10-20 11:56:00 +01001283 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001284 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1285 if (size == 0) {
1286 goto error;
1287 }
1288 fx_param->vsize += size;
1289 value = value->next;
1290 }
1291
1292 return fx_param;
1293
1294error:
1295 delete fx_param;
1296 return NULL;
1297}
1298
1299void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1300{
1301 cnode *node = root->first_child;
1302 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001303 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001304 effect_param_t *param = loadEffectParameter(node);
1305 if (param == NULL) {
1306 node = node->next;
1307 continue;
1308 }
1309 params.add(param);
1310 node = node->next;
1311 }
1312}
1313
1314AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1315 cnode *root,
1316 const Vector <EffectDesc *>& effects)
1317{
1318 cnode *node = root->first_child;
1319 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001320 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001321 return NULL;
1322 }
1323 InputSourceDesc *source = new InputSourceDesc();
1324 while (node) {
1325 size_t i;
1326 for (i = 0; i < effects.size(); i++) {
1327 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001328 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001329 break;
1330 }
1331 }
1332 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +01001333 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001334 node = node->next;
1335 continue;
1336 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001337 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001338 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +01001339 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001340 source->mEffects.add(effect);
1341 node = node->next;
1342 }
1343 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001344 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001345 delete source;
1346 return NULL;
1347 }
1348 return source;
1349}
1350
1351status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1352{
1353 cnode *node = config_find(root, PREPROCESSING_TAG);
1354 if (node == NULL) {
1355 return -ENOENT;
1356 }
1357 node = node->first_child;
1358 while (node) {
1359 audio_source_t source = inputSourceNameToEnum(node->name);
1360 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001361 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001362 node = node->next;
1363 continue;
1364 }
Steve Block3856b092011-10-20 11:56:00 +01001365 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001366 InputSourceDesc *desc = loadInputSource(node, effects);
1367 if (desc == NULL) {
1368 node = node->next;
1369 continue;
1370 }
1371 mInputSources.add(source, desc);
1372 node = node->next;
1373 }
1374 return NO_ERROR;
1375}
1376
1377AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1378{
1379 cnode *node = config_find(root, UUID_TAG);
1380 if (node == NULL) {
1381 return NULL;
1382 }
1383 effect_uuid_t uuid;
1384 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001385 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001386 return NULL;
1387 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001388 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001389}
1390
1391status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1392{
1393 cnode *node = config_find(root, EFFECTS_TAG);
1394 if (node == NULL) {
1395 return -ENOENT;
1396 }
1397 node = node->first_child;
1398 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001399 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001400 EffectDesc *effect = loadEffect(node);
1401 if (effect == NULL) {
1402 node = node->next;
1403 continue;
1404 }
1405 effects.add(effect);
1406 node = node->next;
1407 }
1408 return NO_ERROR;
1409}
1410
1411status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1412{
1413 cnode *root;
1414 char *data;
1415
1416 data = (char *)load_file(path, NULL);
1417 if (data == NULL) {
1418 return -ENODEV;
1419 }
1420 root = config_node("", "");
1421 config_load(root, data);
1422
1423 Vector <EffectDesc *> effects;
1424 loadEffects(root, effects);
1425 loadInputSources(root, effects);
1426
1427 config_free(root);
1428 free(root);
1429 free(data);
1430
1431 return NO_ERROR;
1432}
1433
Dima Zavinfce7a472011-04-19 22:30:36 -07001434/* implementation of the interface to the policy manager */
1435extern "C" {
1436
Eric Laurenta4c5a552012-03-29 10:12:40 -07001437
1438static audio_module_handle_t aps_load_hw_module(void *service,
1439 const char *name)
Dima Zavinfce7a472011-04-19 22:30:36 -07001440{
1441 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001442 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001443 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001444 return 0;
1445 }
1446
Eric Laurenta4c5a552012-03-29 10:12:40 -07001447 return af->loadHwModule(name);
1448}
1449
1450// deprecated: replaced by aps_open_output_on_module()
1451static audio_io_handle_t aps_open_output(void *service,
1452 audio_devices_t *pDevices,
1453 uint32_t *pSamplingRate,
1454 audio_format_t *pFormat,
1455 audio_channel_mask_t *pChannelMask,
1456 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001457 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001458{
1459 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1460 if (af == 0) {
1461 ALOGW("%s: could not get AudioFlinger", __func__);
1462 return 0;
1463 }
1464
1465 return af->openOutput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask,
1466 pLatencyMs, flags);
1467}
1468
1469static audio_io_handle_t aps_open_output_on_module(void *service,
1470 audio_module_handle_t module,
1471 audio_devices_t *pDevices,
1472 uint32_t *pSamplingRate,
1473 audio_format_t *pFormat,
1474 audio_channel_mask_t *pChannelMask,
1475 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001476 audio_output_flags_t flags,
1477 const audio_offload_info_t *offloadInfo)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001478{
1479 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1480 if (af == 0) {
1481 ALOGW("%s: could not get AudioFlinger", __func__);
1482 return 0;
1483 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001484 return af->openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
Eric Laurentbfb1b832013-01-07 09:53:42 -08001485 pLatencyMs, flags, offloadInfo);
Dima Zavinfce7a472011-04-19 22:30:36 -07001486}
1487
1488static audio_io_handle_t aps_open_dup_output(void *service,
1489 audio_io_handle_t output1,
1490 audio_io_handle_t output2)
1491{
1492 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001493 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001494 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001495 return 0;
1496 }
1497 return af->openDuplicateOutput(output1, output2);
1498}
1499
1500static int aps_close_output(void *service, audio_io_handle_t output)
1501{
1502 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001503 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001504 return PERMISSION_DENIED;
1505
1506 return af->closeOutput(output);
1507}
1508
1509static int aps_suspend_output(void *service, audio_io_handle_t output)
1510{
1511 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001512 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001513 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001514 return PERMISSION_DENIED;
1515 }
1516
1517 return af->suspendOutput(output);
1518}
1519
1520static int aps_restore_output(void *service, audio_io_handle_t output)
1521{
1522 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001523 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001524 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001525 return PERMISSION_DENIED;
1526 }
1527
1528 return af->restoreOutput(output);
1529}
1530
Glenn Kasten20010052012-06-22 13:43:51 -07001531// deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
Dima Zavinfce7a472011-04-19 22:30:36 -07001532static audio_io_handle_t aps_open_input(void *service,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001533 audio_devices_t *pDevices,
1534 uint32_t *pSamplingRate,
1535 audio_format_t *pFormat,
1536 audio_channel_mask_t *pChannelMask,
1537 audio_in_acoustics_t acoustics)
Dima Zavinfce7a472011-04-19 22:30:36 -07001538{
1539 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001540 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001541 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001542 return 0;
1543 }
1544
Eric Laurenta4c5a552012-03-29 10:12:40 -07001545 return af->openInput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask);
1546}
1547
1548static audio_io_handle_t aps_open_input_on_module(void *service,
1549 audio_module_handle_t module,
1550 audio_devices_t *pDevices,
1551 uint32_t *pSamplingRate,
1552 audio_format_t *pFormat,
1553 audio_channel_mask_t *pChannelMask)
1554{
1555 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1556 if (af == 0) {
1557 ALOGW("%s: could not get AudioFlinger", __func__);
1558 return 0;
1559 }
1560
1561 return af->openInput(module, pDevices, pSamplingRate, pFormat, pChannelMask);
Dima Zavinfce7a472011-04-19 22:30:36 -07001562}
1563
1564static int aps_close_input(void *service, audio_io_handle_t input)
1565{
1566 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001567 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001568 return PERMISSION_DENIED;
1569
1570 return af->closeInput(input);
1571}
1572
1573static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1574 audio_io_handle_t output)
1575{
1576 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001577 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001578 return PERMISSION_DENIED;
1579
1580 return af->setStreamOutput(stream, output);
1581}
1582
1583static int aps_move_effects(void *service, int session,
1584 audio_io_handle_t src_output,
1585 audio_io_handle_t dst_output)
1586{
1587 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001588 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001589 return PERMISSION_DENIED;
1590
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001591 return af->moveEffects(session, src_output, dst_output);
Dima Zavinfce7a472011-04-19 22:30:36 -07001592}
1593
1594static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1595 const char *keys)
1596{
1597 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1598 return strdup(result.string());
1599}
1600
1601static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1602 const char *kv_pairs, int delay_ms)
1603{
1604 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1605
1606 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1607}
1608
1609static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1610 float volume, audio_io_handle_t output,
1611 int delay_ms)
1612{
1613 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1614
1615 return audioPolicyService->setStreamVolume(stream, volume, output,
1616 delay_ms);
1617}
1618
1619static int aps_start_tone(void *service, audio_policy_tone_t tone,
1620 audio_stream_type_t stream)
1621{
1622 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1623
1624 return audioPolicyService->startTone(tone, stream);
1625}
1626
1627static int aps_stop_tone(void *service)
1628{
1629 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1630
1631 return audioPolicyService->stopTone();
1632}
1633
1634static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1635{
1636 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1637
1638 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1639}
1640
1641}; // extern "C"
1642
1643namespace {
1644 struct audio_policy_service_ops aps_ops = {
1645 open_output : aps_open_output,
1646 open_duplicate_output : aps_open_dup_output,
1647 close_output : aps_close_output,
1648 suspend_output : aps_suspend_output,
1649 restore_output : aps_restore_output,
1650 open_input : aps_open_input,
1651 close_input : aps_close_input,
1652 set_stream_volume : aps_set_stream_volume,
1653 set_stream_output : aps_set_stream_output,
1654 set_parameters : aps_set_parameters,
1655 get_parameters : aps_get_parameters,
1656 start_tone : aps_start_tone,
1657 stop_tone : aps_stop_tone,
1658 set_voice_volume : aps_set_voice_volume,
1659 move_effects : aps_move_effects,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001660 load_hw_module : aps_load_hw_module,
1661 open_output_on_module : aps_open_output_on_module,
1662 open_input_on_module : aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001663 };
1664}; // namespace <unnamed>
1665
Mathias Agopian65ab4712010-07-14 17:59:35 -07001666}; // namespace android