blob: 29cde2a7e83d77d08df6bc9ae7c366b11f045653 [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{
Eric Laurent6d80b682014-10-30 14:46:18 -0700231 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
232 return 0;
233 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700234 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700235 return 0;
236 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700237 ALOGV("getOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700238 Mutex::Autolock _l(mLock);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000239 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate,
240 format, channelMask, flags, offloadInfo);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700241}
242
Eric Laurentde070132010-07-13 04:45:46 -0700243status_t AudioPolicyService::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700244 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700245 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700246{
Eric Laurent6d80b682014-10-30 14:46:18 -0700247 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
248 return BAD_VALUE;
249 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700250 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700251 return NO_INIT;
252 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700253 ALOGV("startOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700254 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700255 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700256}
257
Eric Laurentde070132010-07-13 04:45:46 -0700258status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700259 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700260 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700261{
Eric Laurent6d80b682014-10-30 14:46:18 -0700262 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
263 return BAD_VALUE;
264 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700265 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700266 return NO_INIT;
267 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700268 ALOGV("stopOutput()");
Eric Laurentbfb1b832013-01-07 09:53:42 -0800269 mOutputCommandThread->stopOutputCommand(output, stream, session);
270 return NO_ERROR;
271}
272
273status_t AudioPolicyService::doStopOutput(audio_io_handle_t output,
274 audio_stream_type_t stream,
275 int session)
276{
277 ALOGV("doStopOutput from tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700278 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700279 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700280}
281
282void AudioPolicyService::releaseOutput(audio_io_handle_t output)
283{
Dima Zavinfce7a472011-04-19 22:30:36 -0700284 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700285 return;
286 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700287 ALOGV("releaseOutput()");
Eric Laurentbfb1b832013-01-07 09:53:42 -0800288 mOutputCommandThread->releaseOutputCommand(output);
289}
290
291void AudioPolicyService::doReleaseOutput(audio_io_handle_t output)
292{
293 ALOGV("doReleaseOutput from tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700294 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700295 mpAudioPolicy->release_output(mpAudioPolicy, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700296}
297
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800298audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700299 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800300 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700301 audio_channel_mask_t channelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700302 int audioSession)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700303{
Dima Zavinfce7a472011-04-19 22:30:36 -0700304 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305 return 0;
306 }
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800307 // already checked by client, but double-check in case the client wrapper is bypassed
Eric Laurent9a54bc22013-09-09 09:08:44 -0700308 if (inputSource >= AUDIO_SOURCE_CNT && inputSource != AUDIO_SOURCE_HOTWORD) {
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800309 return 0;
310 }
Eric Laurent9a54bc22013-09-09 09:08:44 -0700311
312 if ((inputSource == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) {
313 return 0;
314 }
315
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316 Mutex::Autolock _l(mLock);
Glenn Kasten20010052012-06-22 13:43:51 -0700317 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700318 audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
Glenn Kasten8af901c2012-11-01 11:11:38 -0700319 format, channelMask, (audio_in_acoustics_t) 0);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700320
321 if (input == 0) {
322 return input;
323 }
324 // create audio pre processors according to input source
Eric Laurent9a54bc22013-09-09 09:08:44 -0700325 audio_source_t aliasSource = (inputSource == AUDIO_SOURCE_HOTWORD) ?
326 AUDIO_SOURCE_VOICE_RECOGNITION : inputSource;
327
328 ssize_t index = mInputSources.indexOfKey(aliasSource);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700329 if (index < 0) {
330 return input;
331 }
332 ssize_t idx = mInputs.indexOfKey(input);
333 InputDesc *inputDesc;
334 if (idx < 0) {
Glenn Kasten81872a22012-03-07 16:49:22 -0800335 inputDesc = new InputDesc(audioSession);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700336 mInputs.add(input, inputDesc);
337 } else {
338 inputDesc = mInputs.valueAt(idx);
339 }
340
341 Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects;
342 for (size_t i = 0; i < effects.size(); i++) {
343 EffectDesc *effect = effects[i];
344 sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input);
345 status_t status = fx->initCheck();
346 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000347 ALOGW("Failed to create Fx %s on input %d", effect->mName, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700348 // fx goes out of scope and strong ref on AudioEffect is released
349 continue;
350 }
351 for (size_t j = 0; j < effect->mParams.size(); j++) {
352 fx->setParameter(effect->mParams[j]);
353 }
354 inputDesc->mEffects.add(fx);
355 }
356 setPreProcessorEnabled(inputDesc, true);
357 return input;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700358}
359
360status_t AudioPolicyService::startInput(audio_io_handle_t input)
361{
Dima Zavinfce7a472011-04-19 22:30:36 -0700362 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700363 return NO_INIT;
364 }
365 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700366
Dima Zavinfce7a472011-04-19 22:30:36 -0700367 return mpAudioPolicy->start_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700368}
369
370status_t AudioPolicyService::stopInput(audio_io_handle_t input)
371{
Dima Zavinfce7a472011-04-19 22:30:36 -0700372 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373 return NO_INIT;
374 }
375 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700376
Dima Zavinfce7a472011-04-19 22:30:36 -0700377 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378}
379
380void AudioPolicyService::releaseInput(audio_io_handle_t input)
381{
Dima Zavinfce7a472011-04-19 22:30:36 -0700382 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700383 return;
384 }
385 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700386 ssize_t index = mInputs.indexOfKey(input);
Eric Laurentb47a5ab2016-12-01 15:28:29 -0800387 if (index >= 0) {
388 InputDesc *inputDesc = mInputs.valueAt(index);
389 setPreProcessorEnabled(inputDesc, false);
390 delete inputDesc;
391 mInputs.removeItemsAt(index);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700392 }
Eric Laurentb47a5ab2016-12-01 15:28:29 -0800393 mpAudioPolicy->release_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700394}
395
Dima Zavinfce7a472011-04-19 22:30:36 -0700396status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700397 int indexMin,
398 int indexMax)
399{
Dima Zavinfce7a472011-04-19 22:30:36 -0700400 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700401 return NO_INIT;
402 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800403 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700404 return PERMISSION_DENIED;
405 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800406 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700407 return BAD_VALUE;
408 }
Eric Laurent5f121362012-06-15 14:45:03 -0700409 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700410 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700411 return NO_ERROR;
412}
413
Eric Laurent83844cc2011-11-18 16:43:31 -0800414status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
415 int index,
416 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700417{
Dima Zavinfce7a472011-04-19 22:30:36 -0700418 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700419 return NO_INIT;
420 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800421 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700422 return PERMISSION_DENIED;
423 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800424 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700425 return BAD_VALUE;
426 }
Eric Laurent5f121362012-06-15 14:45:03 -0700427 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800428 if (mpAudioPolicy->set_stream_volume_index_for_device) {
429 return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
430 stream,
431 index,
432 device);
433 } else {
434 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
435 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700436}
437
Eric Laurent83844cc2011-11-18 16:43:31 -0800438status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
439 int *index,
440 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700441{
Dima Zavinfce7a472011-04-19 22:30:36 -0700442 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700443 return NO_INIT;
444 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800445 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700446 return BAD_VALUE;
447 }
Eric Laurent5f121362012-06-15 14:45:03 -0700448 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800449 if (mpAudioPolicy->get_stream_volume_index_for_device) {
450 return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
451 stream,
452 index,
453 device);
454 } else {
455 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
456 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700457}
458
Dima Zavinfce7a472011-04-19 22:30:36 -0700459uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700460{
Eric Laurent6d80b682014-10-30 14:46:18 -0700461 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
462 return 0;
463 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700464 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700465 return 0;
466 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700467 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurentde070132010-07-13 04:45:46 -0700468}
469
Eric Laurent63742522012-03-08 13:42:42 -0800470//audio policy: use audio_device_t appropriately
471
472audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800473{
Eric Laurent6d80b682014-10-30 14:46:18 -0700474 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
475 return (audio_devices_t)0;
476 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700477 if (mpAudioPolicy == NULL) {
Eric Laurent63742522012-03-08 13:42:42 -0800478 return (audio_devices_t)0;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800479 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700480 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800481}
482
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700483audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700484{
Dima Zavinfce7a472011-04-19 22:30:36 -0700485 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700486 return NO_INIT;
487 }
488 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700489 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700490}
491
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700492status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700493 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700494 uint32_t strategy,
495 int session,
496 int id)
497{
Dima Zavinfce7a472011-04-19 22:30:36 -0700498 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700499 return NO_INIT;
500 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700501 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700502}
503
504status_t AudioPolicyService::unregisterEffect(int id)
505{
Dima Zavinfce7a472011-04-19 22:30:36 -0700506 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700507 return NO_INIT;
508 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700509 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700510}
511
Eric Laurentdb7c0792011-08-10 10:37:50 -0700512status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
513{
514 if (mpAudioPolicy == NULL) {
515 return NO_INIT;
516 }
517 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
518}
519
Glenn Kastenfff6d712012-01-12 16:38:12 -0800520bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800521{
Eric Laurent6d80b682014-10-30 14:46:18 -0700522 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
523 return false;
524 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700525 if (mpAudioPolicy == NULL) {
Eric Laurent6d80b682014-10-30 14:46:18 -0700526 return false;
Eric Laurenteda6c362011-02-02 09:33:30 -0800527 }
528 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700529 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800530}
531
Jean-Michel Trivie336f912013-02-04 16:26:02 -0800532bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
533{
534 if (mpAudioPolicy == NULL) {
535 return 0;
536 }
537 Mutex::Autolock _l(mLock);
538 return mpAudioPolicy->is_stream_active_remotely(mpAudioPolicy, stream, inPastMs);
539}
540
Jean-Michel Trivie3f641f2012-10-10 12:11:16 -0700541bool AudioPolicyService::isSourceActive(audio_source_t source) const
542{
543 if (mpAudioPolicy == NULL) {
544 return false;
545 }
546 if (mpAudioPolicy->is_source_active == 0) {
547 return false;
548 }
549 Mutex::Autolock _l(mLock);
550 return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
551}
552
Eric Laurent57dae992011-07-24 13:36:09 -0700553status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
554 effect_descriptor_t *descriptors,
555 uint32_t *count)
556{
557
558 if (mpAudioPolicy == NULL) {
559 *count = 0;
560 return NO_INIT;
561 }
562 Mutex::Autolock _l(mLock);
563 status_t status = NO_ERROR;
564
565 size_t index;
566 for (index = 0; index < mInputs.size(); index++) {
567 if (mInputs.valueAt(index)->mSessionId == audioSession) {
568 break;
569 }
570 }
571 if (index == mInputs.size()) {
572 *count = 0;
573 return BAD_VALUE;
574 }
575 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
576
577 for (size_t i = 0; i < effects.size(); i++) {
578 effect_descriptor_t desc = effects[i]->descriptor();
579 if (i < *count) {
Glenn Kastena189a682012-02-20 12:16:30 -0800580 descriptors[i] = desc;
Eric Laurent57dae992011-07-24 13:36:09 -0700581 }
582 }
583 if (effects.size() > *count) {
584 status = NO_MEMORY;
585 }
586 *count = effects.size();
587 return status;
588}
589
Mathias Agopian65ab4712010-07-14 17:59:35 -0700590void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700591 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700592 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593}
594
595static bool tryLock(Mutex& mutex)
596{
597 bool locked = false;
598 for (int i = 0; i < kDumpLockRetries; ++i) {
599 if (mutex.tryLock() == NO_ERROR) {
600 locked = true;
601 break;
602 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800603 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700604 }
605 return locked;
606}
607
608status_t AudioPolicyService::dumpInternals(int fd)
609{
610 const size_t SIZE = 256;
611 char buffer[SIZE];
612 String8 result;
613
Dima Zavinfce7a472011-04-19 22:30:36 -0700614 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700615 result.append(buffer);
616 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
617 result.append(buffer);
618 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
619 result.append(buffer);
620
621 write(fd, result.string(), result.size());
622 return NO_ERROR;
623}
624
625status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
626{
Glenn Kasten44deb052012-02-05 18:09:08 -0800627 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628 dumpPermissionDenial(fd);
629 } else {
630 bool locked = tryLock(mLock);
631 if (!locked) {
632 String8 result(kDeadlockedString);
633 write(fd, result.string(), result.size());
634 }
635
636 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800637 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700638 mAudioCommandThread->dump(fd);
639 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800640 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641 mTonePlaybackThread->dump(fd);
642 }
643
Dima Zavinfce7a472011-04-19 22:30:36 -0700644 if (mpAudioPolicy) {
645 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646 }
647
648 if (locked) mLock.unlock();
649 }
650 return NO_ERROR;
651}
652
653status_t AudioPolicyService::dumpPermissionDenial(int fd)
654{
655 const size_t SIZE = 256;
656 char buffer[SIZE];
657 String8 result;
658 snprintf(buffer, SIZE, "Permission Denial: "
659 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
660 IPCThreadState::self()->getCallingPid(),
661 IPCThreadState::self()->getCallingUid());
662 result.append(buffer);
663 write(fd, result.string(), result.size());
664 return NO_ERROR;
665}
666
Glenn Kasten81872a22012-03-07 16:49:22 -0800667void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700668{
Glenn Kasten81872a22012-03-07 16:49:22 -0800669 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700670 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800671 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700672 }
673}
674
Mathias Agopian65ab4712010-07-14 17:59:35 -0700675status_t AudioPolicyService::onTransact(
676 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
677{
678 return BnAudioPolicyService::onTransact(code, data, reply, flags);
679}
680
681
Mathias Agopian65ab4712010-07-14 17:59:35 -0700682// ----------- AudioPolicyService::AudioCommandThread implementation ----------
683
Eric Laurentbfb1b832013-01-07 09:53:42 -0800684AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
685 const wp<AudioPolicyService>& service)
686 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700687{
688 mpToneGenerator = NULL;
689}
690
691
692AudioPolicyService::AudioCommandThread::~AudioCommandThread()
693{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800694 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700695 release_wake_lock(mName.string());
696 }
697 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800698 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699}
700
701void AudioPolicyService::AudioCommandThread::onFirstRef()
702{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800703 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704}
705
706bool AudioPolicyService::AudioCommandThread::threadLoop()
707{
708 nsecs_t waitTime = INT64_MAX;
709
710 mLock.lock();
711 while (!exitPending())
712 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700713 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700714 nsecs_t curTime = systemTime();
715 // commands are sorted by increasing time stamp: execute them from index 0 and up
716 if (mAudioCommands[0]->mTime <= curTime) {
717 AudioCommand *command = mAudioCommands[0];
718 mAudioCommands.removeAt(0);
719 mLastCommand = *command;
720
721 switch (command->mCommand) {
722 case START_TONE: {
723 mLock.unlock();
724 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100725 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700726 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800727 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700728 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
729 mpToneGenerator->startTone(data->mType);
730 delete data;
731 mLock.lock();
732 }break;
733 case STOP_TONE: {
734 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100735 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700736 if (mpToneGenerator != NULL) {
737 mpToneGenerator->stopTone();
738 delete mpToneGenerator;
739 mpToneGenerator = NULL;
740 }
741 mLock.lock();
742 }break;
743 case SET_VOLUME: {
744 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100745 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700746 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
747 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
748 data->mVolume,
749 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700750 if (command->mWaitStatus) {
751 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100752 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700753 }
754 delete data;
755 }break;
756 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700757 ParametersData *data = (ParametersData *)command->mParam;
758 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
759 data->mKeyValuePairs.string(), data->mIO);
760 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
761 if (command->mWaitStatus) {
762 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100763 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700764 }
765 delete data;
766 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700767 case SET_VOICE_VOLUME: {
768 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100769 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700770 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700771 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
772 if (command->mWaitStatus) {
773 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100774 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700775 }
776 delete data;
777 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800778 case STOP_OUTPUT: {
779 StopOutputData *data = (StopOutputData *)command->mParam;
780 ALOGV("AudioCommandThread() processing stop output %d",
781 data->mIO);
782 sp<AudioPolicyService> svc = mService.promote();
783 if (svc == 0) {
784 break;
785 }
786 mLock.unlock();
787 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
788 mLock.lock();
789 delete data;
790 }break;
791 case RELEASE_OUTPUT: {
792 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam;
793 ALOGV("AudioCommandThread() processing release output %d",
794 data->mIO);
795 sp<AudioPolicyService> svc = mService.promote();
796 if (svc == 0) {
797 break;
798 }
799 mLock.unlock();
800 svc->doReleaseOutput(data->mIO);
801 mLock.lock();
802 delete data;
803 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700804 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000805 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700806 }
807 delete command;
808 waitTime = INT64_MAX;
809 } else {
810 waitTime = mAudioCommands[0]->mTime - curTime;
811 break;
812 }
813 }
814 // release delayed commands wake lock
Eric Laurentbfb1b832013-01-07 09:53:42 -0800815 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700816 release_wake_lock(mName.string());
817 }
Steve Block3856b092011-10-20 11:56:00 +0100818 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100820 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700821 }
822 mLock.unlock();
823 return false;
824}
825
826status_t AudioPolicyService::AudioCommandThread::dump(int fd)
827{
828 const size_t SIZE = 256;
829 char buffer[SIZE];
830 String8 result;
831
832 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
833 result.append(buffer);
834 write(fd, result.string(), result.size());
835
836 bool locked = tryLock(mLock);
837 if (!locked) {
838 String8 result2(kCmdDeadlockedString);
839 write(fd, result2.string(), result2.size());
840 }
841
842 snprintf(buffer, SIZE, "- Commands:\n");
843 result = String8(buffer);
844 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800845 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700846 mAudioCommands[i]->dump(buffer, SIZE);
847 result.append(buffer);
848 }
849 result.append(" Last Command\n");
850 mLastCommand.dump(buffer, SIZE);
851 result.append(buffer);
852
853 write(fd, result.string(), result.size());
854
855 if (locked) mLock.unlock();
856
857 return NO_ERROR;
858}
859
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800860void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
861 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700862{
863 AudioCommand *command = new AudioCommand();
864 command->mCommand = START_TONE;
865 ToneData *data = new ToneData();
866 data->mType = type;
867 data->mStream = stream;
868 command->mParam = (void *)data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700869 Mutex::Autolock _l(mLock);
870 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100871 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700872 mWaitWorkCV.signal();
873}
874
875void AudioPolicyService::AudioCommandThread::stopToneCommand()
876{
877 AudioCommand *command = new AudioCommand();
878 command->mCommand = STOP_TONE;
879 command->mParam = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700880 Mutex::Autolock _l(mLock);
881 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100882 ALOGV("AudioCommandThread() adding tone stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700883 mWaitWorkCV.signal();
884}
885
Glenn Kastenfff6d712012-01-12 16:38:12 -0800886status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700887 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800888 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700889 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700890{
891 status_t status = NO_ERROR;
892
893 AudioCommand *command = new AudioCommand();
894 command->mCommand = SET_VOLUME;
895 VolumeData *data = new VolumeData();
896 data->mStream = stream;
897 data->mVolume = volume;
898 data->mIO = output;
899 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700900 Mutex::Autolock _l(mLock);
901 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100902 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700903 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700904 mWaitWorkCV.signal();
905 if (command->mWaitStatus) {
906 command->mCond.wait(mLock);
907 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100908 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700909 }
910 return status;
911}
912
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800913status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700914 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700915 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700916{
917 status_t status = NO_ERROR;
918
919 AudioCommand *command = new AudioCommand();
920 command->mCommand = SET_PARAMETERS;
921 ParametersData *data = new ParametersData();
922 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700923 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700924 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700925 Mutex::Autolock _l(mLock);
926 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100927 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700928 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700929 mWaitWorkCV.signal();
930 if (command->mWaitStatus) {
931 command->mCond.wait(mLock);
932 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100933 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700934 }
935 return status;
936}
937
938status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
939{
940 status_t status = NO_ERROR;
941
942 AudioCommand *command = new AudioCommand();
943 command->mCommand = SET_VOICE_VOLUME;
944 VoiceVolumeData *data = new VoiceVolumeData();
945 data->mVolume = volume;
946 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700947 Mutex::Autolock _l(mLock);
948 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100949 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700950 mWaitWorkCV.signal();
951 if (command->mWaitStatus) {
952 command->mCond.wait(mLock);
953 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100954 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700955 }
956 return status;
957}
958
Eric Laurentbfb1b832013-01-07 09:53:42 -0800959void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
960 audio_stream_type_t stream,
961 int session)
962{
963 AudioCommand *command = new AudioCommand();
964 command->mCommand = STOP_OUTPUT;
965 StopOutputData *data = new StopOutputData();
966 data->mIO = output;
967 data->mStream = stream;
968 data->mSession = session;
969 command->mParam = (void *)data;
970 Mutex::Autolock _l(mLock);
971 insertCommand_l(command);
972 ALOGV("AudioCommandThread() adding stop output %d", output);
973 mWaitWorkCV.signal();
974}
975
976void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
977{
978 AudioCommand *command = new AudioCommand();
979 command->mCommand = RELEASE_OUTPUT;
980 ReleaseOutputData *data = new ReleaseOutputData();
981 data->mIO = output;
982 command->mParam = (void *)data;
983 Mutex::Autolock _l(mLock);
984 insertCommand_l(command);
985 ALOGV("AudioCommandThread() adding release output %d", output);
986 mWaitWorkCV.signal();
987}
988
Mathias Agopian65ab4712010-07-14 17:59:35 -0700989// insertCommand_l() must be called with mLock held
990void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
991{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800992 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700993 Vector <AudioCommand *> removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700994 command->mTime = systemTime() + milliseconds(delayMs);
995
996 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800997 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700998 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
999 }
1000
1001 // check same pending commands with later time stamps and eliminate them
1002 for (i = mAudioCommands.size()-1; i >= 0; i--) {
1003 AudioCommand *command2 = mAudioCommands[i];
1004 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
1005 if (command2->mTime <= command->mTime) break;
1006 if (command2->mCommand != command->mCommand) continue;
1007
1008 switch (command->mCommand) {
1009 case SET_PARAMETERS: {
1010 ParametersData *data = (ParametersData *)command->mParam;
1011 ParametersData *data2 = (ParametersData *)command2->mParam;
1012 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001013 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001014 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001015 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1016 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1017 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001018 String8 key;
1019 String8 value;
1020 param.getAt(j, key, value);
1021 for (size_t k = 0; k < param2.size(); k++) {
1022 String8 key2;
1023 String8 value2;
1024 param2.getAt(k, key2, value2);
1025 if (key2 == key) {
1026 param2.remove(key2);
1027 ALOGV("Filtering out parameter %s", key2.string());
1028 break;
1029 }
1030 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001031 }
1032 // if all keys have been filtered out, remove the command.
1033 // otherwise, update the key value pairs
1034 if (param2.size() == 0) {
1035 removedCommands.add(command2);
1036 } else {
1037 data2->mKeyValuePairs = param2.toString();
1038 }
Eric Laurent21e54562013-09-23 12:08:05 -07001039 command->mTime = command2->mTime;
1040 // force delayMs to non 0 so that code below does not request to wait for
1041 // command status as the command is now delayed
1042 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001043 } break;
1044
1045 case SET_VOLUME: {
1046 VolumeData *data = (VolumeData *)command->mParam;
1047 VolumeData *data2 = (VolumeData *)command2->mParam;
1048 if (data->mIO != data2->mIO) break;
1049 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001050 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001051 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001052 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001053 command->mTime = command2->mTime;
1054 // force delayMs to non 0 so that code below does not request to wait for
1055 // command status as the command is now delayed
1056 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001057 } break;
1058 case START_TONE:
1059 case STOP_TONE:
1060 default:
1061 break;
1062 }
1063 }
1064
1065 // remove filtered commands
1066 for (size_t j = 0; j < removedCommands.size(); j++) {
1067 // removed commands always have time stamps greater than current command
1068 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
1069 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +01001070 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001071 mAudioCommands.removeAt(k);
1072 break;
1073 }
1074 }
1075 }
1076 removedCommands.clear();
1077
Eric Laurent21e54562013-09-23 12:08:05 -07001078 // wait for status only if delay is 0
1079 if (delayMs == 0) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001080 command->mWaitStatus = true;
1081 } else {
1082 command->mWaitStatus = false;
1083 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001084
Mathias Agopian65ab4712010-07-14 17:59:35 -07001085 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +01001086 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -07001087 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001088 mAudioCommands.insertAt(command, i + 1);
1089}
1090
1091void AudioPolicyService::AudioCommandThread::exit()
1092{
Steve Block3856b092011-10-20 11:56:00 +01001093 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001094 {
1095 AutoMutex _l(mLock);
1096 requestExit();
1097 mWaitWorkCV.signal();
1098 }
1099 requestExitAndWait();
1100}
1101
1102void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1103{
1104 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1105 mCommand,
1106 (int)ns2s(mTime),
1107 (int)ns2ms(mTime)%1000,
1108 mWaitStatus,
1109 mParam);
1110}
1111
Dima Zavinfce7a472011-04-19 22:30:36 -07001112/******* helpers for the service_ops callbacks defined below *********/
1113void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1114 const char *keyValuePairs,
1115 int delayMs)
1116{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001117 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001118 delayMs);
1119}
1120
1121int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1122 float volume,
1123 audio_io_handle_t output,
1124 int delayMs)
1125{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001126 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001127 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001128}
1129
1130int AudioPolicyService::startTone(audio_policy_tone_t tone,
1131 audio_stream_type_t stream)
1132{
1133 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
Steve Block29357bc2012-01-06 19:20:56 +00001134 ALOGE("startTone: illegal tone requested (%d)", tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001135 if (stream != AUDIO_STREAM_VOICE_CALL)
Steve Block29357bc2012-01-06 19:20:56 +00001136 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001137 tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001138 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1139 AUDIO_STREAM_VOICE_CALL);
1140 return 0;
1141}
1142
1143int AudioPolicyService::stopTone()
1144{
1145 mTonePlaybackThread->stopToneCommand();
1146 return 0;
1147}
1148
1149int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1150{
1151 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1152}
1153
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001154bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
1155{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001156 if (mpAudioPolicy == NULL) {
1157 ALOGV("mpAudioPolicy == NULL");
1158 return false;
1159 }
1160
1161 if (mpAudioPolicy->is_offload_supported == NULL) {
1162 ALOGV("HAL does not implement is_offload_supported");
1163 return false;
1164 }
1165
1166 return mpAudioPolicy->is_offload_supported(mpAudioPolicy, &info);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001167}
1168
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001169// ----------------------------------------------------------------------------
1170// Audio pre-processing configuration
1171// ----------------------------------------------------------------------------
1172
Glenn Kasten8dad0e32012-01-09 08:41:22 -08001173/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001174 MIC_SRC_TAG,
1175 VOICE_UL_SRC_TAG,
1176 VOICE_DL_SRC_TAG,
1177 VOICE_CALL_SRC_TAG,
1178 CAMCORDER_SRC_TAG,
1179 VOICE_REC_SRC_TAG,
1180 VOICE_COMM_SRC_TAG
1181};
1182
1183// returns the audio_source_t enum corresponding to the input source name or
1184// AUDIO_SOURCE_CNT is no match found
1185audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
1186{
1187 int i;
1188 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
1189 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001190 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001191 break;
1192 }
1193 }
1194 return (audio_source_t)i;
1195}
1196
1197size_t AudioPolicyService::growParamSize(char *param,
1198 size_t size,
1199 size_t *curSize,
1200 size_t *totSize)
1201{
1202 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
1203 size_t pos = ((*curSize - 1 ) / size + 1) * size;
1204
1205 if (pos + size > *totSize) {
1206 while (pos + size > *totSize) {
1207 *totSize += ((*totSize + 7) / 8) * 4;
1208 }
1209 param = (char *)realloc(param, *totSize);
1210 }
1211 *curSize = pos + size;
1212 return pos;
1213}
1214
1215size_t AudioPolicyService::readParamValue(cnode *node,
1216 char *param,
1217 size_t *curSize,
1218 size_t *totSize)
1219{
1220 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
1221 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
1222 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001223 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001224 return sizeof(short);
1225 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
1226 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
1227 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001228 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001229 return sizeof(int);
1230 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
1231 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
1232 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001233 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001234 return sizeof(float);
1235 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
1236 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
1237 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1238 *(bool *)((char *)param + pos) = false;
1239 } else {
1240 *(bool *)((char *)param + pos) = true;
1241 }
Steve Block3856b092011-10-20 11:56:00 +01001242 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001243 return sizeof(bool);
1244 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1245 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1246 if (*curSize + len + 1 > *totSize) {
1247 *totSize = *curSize + len + 1;
1248 param = (char *)realloc(param, *totSize);
1249 }
1250 strncpy(param + *curSize, node->value, len);
1251 *curSize += len;
1252 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +01001253 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001254 return len;
1255 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001256 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001257 return 0;
1258}
1259
1260effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1261{
1262 cnode *param;
1263 cnode *value;
1264 size_t curSize = sizeof(effect_param_t);
1265 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1266 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1267
1268 param = config_find(root, PARAM_TAG);
1269 value = config_find(root, VALUE_TAG);
1270 if (param == NULL && value == NULL) {
1271 // try to parse simple parameter form {int int}
1272 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -08001273 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001274 // Note: that a pair of random strings is read as 0 0
1275 int *ptr = (int *)fx_param->data;
1276 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +00001277 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001278 *ptr++ = atoi(param->name);
1279 *ptr = atoi(param->value);
1280 fx_param->psize = sizeof(int);
1281 fx_param->vsize = sizeof(int);
1282 return fx_param;
1283 }
1284 }
1285 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001286 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001287 goto error;
1288 }
1289
1290 fx_param->psize = 0;
1291 param = param->first_child;
1292 while (param) {
Steve Block3856b092011-10-20 11:56:00 +01001293 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001294 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1295 if (size == 0) {
1296 goto error;
1297 }
1298 fx_param->psize += size;
1299 param = param->next;
1300 }
1301
1302 // align start of value field on 32 bit boundary
1303 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1304
1305 fx_param->vsize = 0;
1306 value = value->first_child;
1307 while (value) {
Steve Block3856b092011-10-20 11:56:00 +01001308 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001309 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1310 if (size == 0) {
1311 goto error;
1312 }
1313 fx_param->vsize += size;
1314 value = value->next;
1315 }
1316
1317 return fx_param;
1318
1319error:
1320 delete fx_param;
1321 return NULL;
1322}
1323
1324void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1325{
1326 cnode *node = root->first_child;
1327 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001328 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001329 effect_param_t *param = loadEffectParameter(node);
1330 if (param == NULL) {
1331 node = node->next;
1332 continue;
1333 }
1334 params.add(param);
1335 node = node->next;
1336 }
1337}
1338
1339AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1340 cnode *root,
1341 const Vector <EffectDesc *>& effects)
1342{
1343 cnode *node = root->first_child;
1344 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001345 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001346 return NULL;
1347 }
1348 InputSourceDesc *source = new InputSourceDesc();
1349 while (node) {
1350 size_t i;
1351 for (i = 0; i < effects.size(); i++) {
1352 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001353 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001354 break;
1355 }
1356 }
1357 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +01001358 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001359 node = node->next;
1360 continue;
1361 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001362 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001363 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +01001364 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001365 source->mEffects.add(effect);
1366 node = node->next;
1367 }
1368 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001369 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001370 delete source;
1371 return NULL;
1372 }
1373 return source;
1374}
1375
1376status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1377{
1378 cnode *node = config_find(root, PREPROCESSING_TAG);
1379 if (node == NULL) {
1380 return -ENOENT;
1381 }
1382 node = node->first_child;
1383 while (node) {
1384 audio_source_t source = inputSourceNameToEnum(node->name);
1385 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001386 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001387 node = node->next;
1388 continue;
1389 }
Steve Block3856b092011-10-20 11:56:00 +01001390 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001391 InputSourceDesc *desc = loadInputSource(node, effects);
1392 if (desc == NULL) {
1393 node = node->next;
1394 continue;
1395 }
1396 mInputSources.add(source, desc);
1397 node = node->next;
1398 }
1399 return NO_ERROR;
1400}
1401
1402AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1403{
1404 cnode *node = config_find(root, UUID_TAG);
1405 if (node == NULL) {
1406 return NULL;
1407 }
1408 effect_uuid_t uuid;
1409 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001410 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001411 return NULL;
1412 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001413 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001414}
1415
1416status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1417{
1418 cnode *node = config_find(root, EFFECTS_TAG);
1419 if (node == NULL) {
1420 return -ENOENT;
1421 }
1422 node = node->first_child;
1423 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001424 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001425 EffectDesc *effect = loadEffect(node);
1426 if (effect == NULL) {
1427 node = node->next;
1428 continue;
1429 }
1430 effects.add(effect);
1431 node = node->next;
1432 }
1433 return NO_ERROR;
1434}
1435
1436status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1437{
1438 cnode *root;
1439 char *data;
1440
1441 data = (char *)load_file(path, NULL);
1442 if (data == NULL) {
1443 return -ENODEV;
1444 }
1445 root = config_node("", "");
1446 config_load(root, data);
1447
1448 Vector <EffectDesc *> effects;
1449 loadEffects(root, effects);
1450 loadInputSources(root, effects);
1451
1452 config_free(root);
1453 free(root);
1454 free(data);
1455
1456 return NO_ERROR;
1457}
1458
Dima Zavinfce7a472011-04-19 22:30:36 -07001459/* implementation of the interface to the policy manager */
1460extern "C" {
1461
Eric Laurenta4c5a552012-03-29 10:12:40 -07001462
1463static audio_module_handle_t aps_load_hw_module(void *service,
1464 const char *name)
Dima Zavinfce7a472011-04-19 22:30:36 -07001465{
1466 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001467 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001468 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001469 return 0;
1470 }
1471
Eric Laurenta4c5a552012-03-29 10:12:40 -07001472 return af->loadHwModule(name);
1473}
1474
1475// deprecated: replaced by aps_open_output_on_module()
1476static audio_io_handle_t aps_open_output(void *service,
1477 audio_devices_t *pDevices,
1478 uint32_t *pSamplingRate,
1479 audio_format_t *pFormat,
1480 audio_channel_mask_t *pChannelMask,
1481 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001482 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001483{
1484 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1485 if (af == 0) {
1486 ALOGW("%s: could not get AudioFlinger", __func__);
1487 return 0;
1488 }
1489
1490 return af->openOutput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask,
1491 pLatencyMs, flags);
1492}
1493
1494static audio_io_handle_t aps_open_output_on_module(void *service,
1495 audio_module_handle_t module,
1496 audio_devices_t *pDevices,
1497 uint32_t *pSamplingRate,
1498 audio_format_t *pFormat,
1499 audio_channel_mask_t *pChannelMask,
1500 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001501 audio_output_flags_t flags,
1502 const audio_offload_info_t *offloadInfo)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001503{
1504 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1505 if (af == 0) {
1506 ALOGW("%s: could not get AudioFlinger", __func__);
1507 return 0;
1508 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001509 return af->openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
Eric Laurentbfb1b832013-01-07 09:53:42 -08001510 pLatencyMs, flags, offloadInfo);
Dima Zavinfce7a472011-04-19 22:30:36 -07001511}
1512
1513static audio_io_handle_t aps_open_dup_output(void *service,
1514 audio_io_handle_t output1,
1515 audio_io_handle_t output2)
1516{
1517 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001518 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001519 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001520 return 0;
1521 }
1522 return af->openDuplicateOutput(output1, output2);
1523}
1524
1525static int aps_close_output(void *service, audio_io_handle_t output)
1526{
1527 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001528 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001529 return PERMISSION_DENIED;
1530
1531 return af->closeOutput(output);
1532}
1533
1534static int aps_suspend_output(void *service, audio_io_handle_t output)
1535{
1536 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001537 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001538 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001539 return PERMISSION_DENIED;
1540 }
1541
1542 return af->suspendOutput(output);
1543}
1544
1545static int aps_restore_output(void *service, audio_io_handle_t output)
1546{
1547 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001548 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001549 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001550 return PERMISSION_DENIED;
1551 }
1552
1553 return af->restoreOutput(output);
1554}
1555
Glenn Kasten20010052012-06-22 13:43:51 -07001556// deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
Dima Zavinfce7a472011-04-19 22:30:36 -07001557static audio_io_handle_t aps_open_input(void *service,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001558 audio_devices_t *pDevices,
1559 uint32_t *pSamplingRate,
1560 audio_format_t *pFormat,
1561 audio_channel_mask_t *pChannelMask,
1562 audio_in_acoustics_t acoustics)
Dima Zavinfce7a472011-04-19 22:30:36 -07001563{
1564 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001565 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001566 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001567 return 0;
1568 }
1569
Eric Laurenta4c5a552012-03-29 10:12:40 -07001570 return af->openInput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask);
1571}
1572
1573static audio_io_handle_t aps_open_input_on_module(void *service,
1574 audio_module_handle_t module,
1575 audio_devices_t *pDevices,
1576 uint32_t *pSamplingRate,
1577 audio_format_t *pFormat,
1578 audio_channel_mask_t *pChannelMask)
1579{
1580 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1581 if (af == 0) {
1582 ALOGW("%s: could not get AudioFlinger", __func__);
1583 return 0;
1584 }
1585
1586 return af->openInput(module, pDevices, pSamplingRate, pFormat, pChannelMask);
Dima Zavinfce7a472011-04-19 22:30:36 -07001587}
1588
1589static int aps_close_input(void *service, audio_io_handle_t input)
1590{
1591 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001592 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001593 return PERMISSION_DENIED;
1594
1595 return af->closeInput(input);
1596}
1597
1598static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1599 audio_io_handle_t output)
1600{
1601 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001602 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001603 return PERMISSION_DENIED;
1604
1605 return af->setStreamOutput(stream, output);
1606}
1607
1608static int aps_move_effects(void *service, int session,
1609 audio_io_handle_t src_output,
1610 audio_io_handle_t dst_output)
1611{
1612 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001613 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001614 return PERMISSION_DENIED;
1615
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001616 return af->moveEffects(session, src_output, dst_output);
Dima Zavinfce7a472011-04-19 22:30:36 -07001617}
1618
1619static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1620 const char *keys)
1621{
1622 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1623 return strdup(result.string());
1624}
1625
1626static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1627 const char *kv_pairs, int delay_ms)
1628{
1629 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1630
1631 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1632}
1633
1634static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1635 float volume, audio_io_handle_t output,
1636 int delay_ms)
1637{
1638 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1639
1640 return audioPolicyService->setStreamVolume(stream, volume, output,
1641 delay_ms);
1642}
1643
1644static int aps_start_tone(void *service, audio_policy_tone_t tone,
1645 audio_stream_type_t stream)
1646{
1647 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1648
1649 return audioPolicyService->startTone(tone, stream);
1650}
1651
1652static int aps_stop_tone(void *service)
1653{
1654 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1655
1656 return audioPolicyService->stopTone();
1657}
1658
1659static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1660{
1661 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1662
1663 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1664}
1665
1666}; // extern "C"
1667
1668namespace {
1669 struct audio_policy_service_ops aps_ops = {
1670 open_output : aps_open_output,
1671 open_duplicate_output : aps_open_dup_output,
1672 close_output : aps_close_output,
1673 suspend_output : aps_suspend_output,
1674 restore_output : aps_restore_output,
1675 open_input : aps_open_input,
1676 close_input : aps_close_input,
1677 set_stream_volume : aps_set_stream_volume,
1678 set_stream_output : aps_set_stream_output,
1679 set_parameters : aps_set_parameters,
1680 get_parameters : aps_get_parameters,
1681 start_tone : aps_start_tone,
1682 stop_tone : aps_stop_tone,
1683 set_voice_volume : aps_set_voice_volume,
1684 move_effects : aps_move_effects,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001685 load_hw_module : aps_load_hw_module,
1686 open_output_on_module : aps_open_output_on_module,
1687 open_input_on_module : aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001688 };
1689}; // namespace <unnamed>
1690
Mathias Agopian65ab4712010-07-14 17:59:35 -07001691}; // namespace android