blob: d6ea75846a599a632f4a95d2998743bac09b7d24 [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);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070080 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070081 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070082 }
Mathias Agopian65ab4712010-07-14 17:59:35 -070083
Dima Zavinfce7a472011-04-19 22:30:36 -070084 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
Steve Block29357bc2012-01-06 19:20:56 +000085 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070086 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070087 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070088 }
Eric Laurent93575202011-01-18 18:39:02 -080089
Dima Zavinfce7a472011-04-19 22:30:36 -070090 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
91 &mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000092 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070093 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070094 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070095 }
Dima Zavinfce7a472011-04-19 22:30:36 -070096
97 rc = mpAudioPolicy->init_check(mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000098 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070099 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700100 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700101 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700102
Steve Blockdf64d152012-01-04 20:05:49 +0000103 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700104
105 // load audio pre processing modules
106 if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) {
107 loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE);
108 } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) {
109 loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE);
110 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700111}
112
113AudioPolicyService::~AudioPolicyService()
114{
115 mTonePlaybackThread->exit();
116 mTonePlaybackThread.clear();
117 mAudioCommandThread->exit();
118 mAudioCommandThread.clear();
119
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700120
121 // release audio pre processing resources
122 for (size_t i = 0; i < mInputSources.size(); i++) {
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800123 delete mInputSources.valueAt(i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700124 }
125 mInputSources.clear();
126
127 for (size_t i = 0; i < mInputs.size(); i++) {
128 mInputs.valueAt(i)->mEffects.clear();
129 delete mInputs.valueAt(i);
130 }
131 mInputs.clear();
132
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700133 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700134 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700135 }
136 if (mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700137 audio_policy_dev_close(mpAudioPolicyDev);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700138 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700139}
140
Dima Zavinfce7a472011-04-19 22:30:36 -0700141status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
142 audio_policy_dev_state_t state,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700143 const char *device_address)
144{
Dima Zavinfce7a472011-04-19 22:30:36 -0700145 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700146 return NO_INIT;
147 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800148 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700149 return PERMISSION_DENIED;
150 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700151 if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700152 return BAD_VALUE;
153 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700154 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
155 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700156 return BAD_VALUE;
157 }
158
Glenn Kasten411e4472012-11-02 10:00:06 -0700159 ALOGV("setDeviceConnectionState()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700160 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700161 return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device,
162 state, device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700163}
164
Dima Zavinfce7a472011-04-19 22:30:36 -0700165audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
166 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -0700167 const char *device_address)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700168{
Dima Zavinfce7a472011-04-19 22:30:36 -0700169 if (mpAudioPolicy == NULL) {
170 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700171 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700172 return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device,
173 device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700174}
175
Glenn Kastenf78aee72012-01-04 11:00:47 -0800176status_t AudioPolicyService::setPhoneState(audio_mode_t state)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700177{
Dima Zavinfce7a472011-04-19 22:30:36 -0700178 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700179 return NO_INIT;
180 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800181 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700182 return PERMISSION_DENIED;
183 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800184 if (uint32_t(state) >= AUDIO_MODE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700185 return BAD_VALUE;
186 }
187
Glenn Kasten411e4472012-11-02 10:00:06 -0700188 ALOGV("setPhoneState()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700189
190 // TODO: check if it is more appropriate to do it in platform specific policy manager
191 AudioSystem::setMode(state);
192
193 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700194 mpAudioPolicy->set_phone_state(mpAudioPolicy, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700195 return NO_ERROR;
196}
197
Dima Zavinfce7a472011-04-19 22:30:36 -0700198status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
199 audio_policy_forced_cfg_t config)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700200{
Dima Zavinfce7a472011-04-19 22:30:36 -0700201 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700202 return NO_INIT;
203 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800204 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700205 return PERMISSION_DENIED;
206 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700207 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700208 return BAD_VALUE;
209 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700210 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700211 return BAD_VALUE;
212 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700213 ALOGV("setForceUse()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700214 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700215 mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700216 return NO_ERROR;
217}
218
Dima Zavinfce7a472011-04-19 22:30:36 -0700219audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700220{
Dima Zavinfce7a472011-04-19 22:30:36 -0700221 if (mpAudioPolicy == NULL) {
222 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700223 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700224 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
225 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700226 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700227 return mpAudioPolicy->get_force_use(mpAudioPolicy, usage);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700228}
229
Dima Zavinfce7a472011-04-19 22:30:36 -0700230audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700231 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800232 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700233 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000234 audio_output_flags_t flags,
235 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700236{
Dima Zavinfce7a472011-04-19 22:30:36 -0700237 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700238 return 0;
239 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700240 ALOGV("getOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700241 Mutex::Autolock _l(mLock);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000242 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate,
243 format, channelMask, flags, offloadInfo);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244}
245
Eric Laurentde070132010-07-13 04:45:46 -0700246status_t AudioPolicyService::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700247 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700248 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700249{
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{
Dima Zavinfce7a472011-04-19 22:30:36 -0700262 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700263 return NO_INIT;
264 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700265 ALOGV("stopOutput()");
Eric Laurentbfb1b832013-01-07 09:53:42 -0800266 mOutputCommandThread->stopOutputCommand(output, stream, session);
267 return NO_ERROR;
268}
269
270status_t AudioPolicyService::doStopOutput(audio_io_handle_t output,
271 audio_stream_type_t stream,
272 int session)
273{
274 ALOGV("doStopOutput from tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700275 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700276 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700277}
278
279void AudioPolicyService::releaseOutput(audio_io_handle_t output)
280{
Dima Zavinfce7a472011-04-19 22:30:36 -0700281 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700282 return;
283 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700284 ALOGV("releaseOutput()");
Eric Laurentbfb1b832013-01-07 09:53:42 -0800285 mOutputCommandThread->releaseOutputCommand(output);
286}
287
288void AudioPolicyService::doReleaseOutput(audio_io_handle_t output)
289{
290 ALOGV("doReleaseOutput from tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700291 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700292 mpAudioPolicy->release_output(mpAudioPolicy, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700293}
294
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800295audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700296 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800297 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700298 audio_channel_mask_t channelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700299 int audioSession)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300{
Dima Zavinfce7a472011-04-19 22:30:36 -0700301 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700302 return 0;
303 }
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800304 // already checked by client, but double-check in case the client wrapper is bypassed
305 if (uint32_t(inputSource) >= AUDIO_SOURCE_CNT) {
306 return 0;
307 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700308 Mutex::Autolock _l(mLock);
Glenn Kasten20010052012-06-22 13:43:51 -0700309 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700310 audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
Glenn Kasten8af901c2012-11-01 11:11:38 -0700311 format, channelMask, (audio_in_acoustics_t) 0);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700312
313 if (input == 0) {
314 return input;
315 }
316 // create audio pre processors according to input source
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800317 ssize_t index = mInputSources.indexOfKey(inputSource);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700318 if (index < 0) {
319 return input;
320 }
321 ssize_t idx = mInputs.indexOfKey(input);
322 InputDesc *inputDesc;
323 if (idx < 0) {
Glenn Kasten81872a22012-03-07 16:49:22 -0800324 inputDesc = new InputDesc(audioSession);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700325 mInputs.add(input, inputDesc);
326 } else {
327 inputDesc = mInputs.valueAt(idx);
328 }
329
330 Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects;
331 for (size_t i = 0; i < effects.size(); i++) {
332 EffectDesc *effect = effects[i];
333 sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input);
334 status_t status = fx->initCheck();
335 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000336 ALOGW("Failed to create Fx %s on input %d", effect->mName, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700337 // fx goes out of scope and strong ref on AudioEffect is released
338 continue;
339 }
340 for (size_t j = 0; j < effect->mParams.size(); j++) {
341 fx->setParameter(effect->mParams[j]);
342 }
343 inputDesc->mEffects.add(fx);
344 }
345 setPreProcessorEnabled(inputDesc, true);
346 return input;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700347}
348
349status_t AudioPolicyService::startInput(audio_io_handle_t input)
350{
Dima Zavinfce7a472011-04-19 22:30:36 -0700351 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700352 return NO_INIT;
353 }
354 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700355
Dima Zavinfce7a472011-04-19 22:30:36 -0700356 return mpAudioPolicy->start_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700357}
358
359status_t AudioPolicyService::stopInput(audio_io_handle_t input)
360{
Dima Zavinfce7a472011-04-19 22:30:36 -0700361 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362 return NO_INIT;
363 }
364 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700365
Dima Zavinfce7a472011-04-19 22:30:36 -0700366 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700367}
368
369void AudioPolicyService::releaseInput(audio_io_handle_t input)
370{
Dima Zavinfce7a472011-04-19 22:30:36 -0700371 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700372 return;
373 }
374 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700375 mpAudioPolicy->release_input(mpAudioPolicy, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700376
377 ssize_t index = mInputs.indexOfKey(input);
378 if (index < 0) {
379 return;
380 }
381 InputDesc *inputDesc = mInputs.valueAt(index);
382 setPreProcessorEnabled(inputDesc, false);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700383 delete inputDesc;
384 mInputs.removeItemsAt(index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385}
386
Dima Zavinfce7a472011-04-19 22:30:36 -0700387status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388 int indexMin,
389 int indexMax)
390{
Dima Zavinfce7a472011-04-19 22:30:36 -0700391 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700392 return NO_INIT;
393 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800394 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700395 return PERMISSION_DENIED;
396 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800397 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700398 return BAD_VALUE;
399 }
Eric Laurent5f121362012-06-15 14:45:03 -0700400 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700401 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700402 return NO_ERROR;
403}
404
Eric Laurent83844cc2011-11-18 16:43:31 -0800405status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
406 int index,
407 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700408{
Dima Zavinfce7a472011-04-19 22:30:36 -0700409 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700410 return NO_INIT;
411 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800412 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700413 return PERMISSION_DENIED;
414 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800415 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700416 return BAD_VALUE;
417 }
Eric Laurent5f121362012-06-15 14:45:03 -0700418 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800419 if (mpAudioPolicy->set_stream_volume_index_for_device) {
420 return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
421 stream,
422 index,
423 device);
424 } else {
425 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
426 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700427}
428
Eric Laurent83844cc2011-11-18 16:43:31 -0800429status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
430 int *index,
431 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700432{
Dima Zavinfce7a472011-04-19 22:30:36 -0700433 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700434 return NO_INIT;
435 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800436 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700437 return BAD_VALUE;
438 }
Eric Laurent5f121362012-06-15 14:45:03 -0700439 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800440 if (mpAudioPolicy->get_stream_volume_index_for_device) {
441 return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
442 stream,
443 index,
444 device);
445 } else {
446 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
447 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700448}
449
Dima Zavinfce7a472011-04-19 22:30:36 -0700450uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700451{
Dima Zavinfce7a472011-04-19 22:30:36 -0700452 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700453 return 0;
454 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700455 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurentde070132010-07-13 04:45:46 -0700456}
457
Eric Laurent63742522012-03-08 13:42:42 -0800458//audio policy: use audio_device_t appropriately
459
460audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800461{
Dima Zavinfce7a472011-04-19 22:30:36 -0700462 if (mpAudioPolicy == NULL) {
Eric Laurent63742522012-03-08 13:42:42 -0800463 return (audio_devices_t)0;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800464 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700465 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800466}
467
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700468audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700469{
Dima Zavinfce7a472011-04-19 22:30:36 -0700470 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700471 return NO_INIT;
472 }
473 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700474 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700475}
476
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700477status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700478 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700479 uint32_t strategy,
480 int session,
481 int id)
482{
Dima Zavinfce7a472011-04-19 22:30:36 -0700483 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700484 return NO_INIT;
485 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700486 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700487}
488
489status_t AudioPolicyService::unregisterEffect(int id)
490{
Dima Zavinfce7a472011-04-19 22:30:36 -0700491 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700492 return NO_INIT;
493 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700494 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700495}
496
Eric Laurentdb7c0792011-08-10 10:37:50 -0700497status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
498{
499 if (mpAudioPolicy == NULL) {
500 return NO_INIT;
501 }
502 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
503}
504
Glenn Kastenfff6d712012-01-12 16:38:12 -0800505bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800506{
Dima Zavinfce7a472011-04-19 22:30:36 -0700507 if (mpAudioPolicy == NULL) {
Eric Laurenteda6c362011-02-02 09:33:30 -0800508 return 0;
509 }
510 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700511 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800512}
513
Jean-Michel Trivie336f912013-02-04 16:26:02 -0800514bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
515{
516 if (mpAudioPolicy == NULL) {
517 return 0;
518 }
519 Mutex::Autolock _l(mLock);
520 return mpAudioPolicy->is_stream_active_remotely(mpAudioPolicy, stream, inPastMs);
521}
522
Jean-Michel Trivie3f641f2012-10-10 12:11:16 -0700523bool AudioPolicyService::isSourceActive(audio_source_t source) const
524{
525 if (mpAudioPolicy == NULL) {
526 return false;
527 }
528 if (mpAudioPolicy->is_source_active == 0) {
529 return false;
530 }
531 Mutex::Autolock _l(mLock);
532 return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
533}
534
Eric Laurent57dae992011-07-24 13:36:09 -0700535status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
536 effect_descriptor_t *descriptors,
537 uint32_t *count)
538{
539
540 if (mpAudioPolicy == NULL) {
541 *count = 0;
542 return NO_INIT;
543 }
544 Mutex::Autolock _l(mLock);
545 status_t status = NO_ERROR;
546
547 size_t index;
548 for (index = 0; index < mInputs.size(); index++) {
549 if (mInputs.valueAt(index)->mSessionId == audioSession) {
550 break;
551 }
552 }
553 if (index == mInputs.size()) {
554 *count = 0;
555 return BAD_VALUE;
556 }
557 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
558
559 for (size_t i = 0; i < effects.size(); i++) {
560 effect_descriptor_t desc = effects[i]->descriptor();
561 if (i < *count) {
Glenn Kastena189a682012-02-20 12:16:30 -0800562 descriptors[i] = desc;
Eric Laurent57dae992011-07-24 13:36:09 -0700563 }
564 }
565 if (effects.size() > *count) {
566 status = NO_MEMORY;
567 }
568 *count = effects.size();
569 return status;
570}
571
Mathias Agopian65ab4712010-07-14 17:59:35 -0700572void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700573 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700574 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700575}
576
577static bool tryLock(Mutex& mutex)
578{
579 bool locked = false;
580 for (int i = 0; i < kDumpLockRetries; ++i) {
581 if (mutex.tryLock() == NO_ERROR) {
582 locked = true;
583 break;
584 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800585 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700586 }
587 return locked;
588}
589
590status_t AudioPolicyService::dumpInternals(int fd)
591{
592 const size_t SIZE = 256;
593 char buffer[SIZE];
594 String8 result;
595
Dima Zavinfce7a472011-04-19 22:30:36 -0700596 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700597 result.append(buffer);
598 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
599 result.append(buffer);
600 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
601 result.append(buffer);
602
603 write(fd, result.string(), result.size());
604 return NO_ERROR;
605}
606
607status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
608{
Glenn Kasten44deb052012-02-05 18:09:08 -0800609 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700610 dumpPermissionDenial(fd);
611 } else {
612 bool locked = tryLock(mLock);
613 if (!locked) {
614 String8 result(kDeadlockedString);
615 write(fd, result.string(), result.size());
616 }
617
618 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800619 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700620 mAudioCommandThread->dump(fd);
621 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800622 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700623 mTonePlaybackThread->dump(fd);
624 }
625
Dima Zavinfce7a472011-04-19 22:30:36 -0700626 if (mpAudioPolicy) {
627 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628 }
629
630 if (locked) mLock.unlock();
631 }
632 return NO_ERROR;
633}
634
635status_t AudioPolicyService::dumpPermissionDenial(int fd)
636{
637 const size_t SIZE = 256;
638 char buffer[SIZE];
639 String8 result;
640 snprintf(buffer, SIZE, "Permission Denial: "
641 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
642 IPCThreadState::self()->getCallingPid(),
643 IPCThreadState::self()->getCallingUid());
644 result.append(buffer);
645 write(fd, result.string(), result.size());
646 return NO_ERROR;
647}
648
Glenn Kasten81872a22012-03-07 16:49:22 -0800649void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700650{
Glenn Kasten81872a22012-03-07 16:49:22 -0800651 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700652 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800653 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700654 }
655}
656
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657status_t AudioPolicyService::onTransact(
658 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
659{
660 return BnAudioPolicyService::onTransact(code, data, reply, flags);
661}
662
663
Mathias Agopian65ab4712010-07-14 17:59:35 -0700664// ----------- AudioPolicyService::AudioCommandThread implementation ----------
665
Eric Laurentbfb1b832013-01-07 09:53:42 -0800666AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
667 const wp<AudioPolicyService>& service)
668 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700669{
670 mpToneGenerator = NULL;
671}
672
673
674AudioPolicyService::AudioCommandThread::~AudioCommandThread()
675{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800676 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700677 release_wake_lock(mName.string());
678 }
679 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800680 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700681}
682
683void AudioPolicyService::AudioCommandThread::onFirstRef()
684{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800685 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700686}
687
688bool AudioPolicyService::AudioCommandThread::threadLoop()
689{
690 nsecs_t waitTime = INT64_MAX;
691
692 mLock.lock();
693 while (!exitPending())
694 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700695 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700696 nsecs_t curTime = systemTime();
697 // commands are sorted by increasing time stamp: execute them from index 0 and up
698 if (mAudioCommands[0]->mTime <= curTime) {
699 AudioCommand *command = mAudioCommands[0];
700 mAudioCommands.removeAt(0);
701 mLastCommand = *command;
702
703 switch (command->mCommand) {
704 case START_TONE: {
705 mLock.unlock();
706 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100707 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700708 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800709 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700710 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
711 mpToneGenerator->startTone(data->mType);
712 delete data;
713 mLock.lock();
714 }break;
715 case STOP_TONE: {
716 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100717 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700718 if (mpToneGenerator != NULL) {
719 mpToneGenerator->stopTone();
720 delete mpToneGenerator;
721 mpToneGenerator = NULL;
722 }
723 mLock.lock();
724 }break;
725 case SET_VOLUME: {
726 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100727 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700728 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
729 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
730 data->mVolume,
731 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700732 if (command->mWaitStatus) {
733 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100734 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735 }
736 delete data;
737 }break;
738 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700739 ParametersData *data = (ParametersData *)command->mParam;
740 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
741 data->mKeyValuePairs.string(), data->mIO);
742 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
743 if (command->mWaitStatus) {
744 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100745 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700746 }
747 delete data;
748 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700749 case SET_VOICE_VOLUME: {
750 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100751 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700752 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700753 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
754 if (command->mWaitStatus) {
755 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100756 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700757 }
758 delete data;
759 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800760 case STOP_OUTPUT: {
761 StopOutputData *data = (StopOutputData *)command->mParam;
762 ALOGV("AudioCommandThread() processing stop output %d",
763 data->mIO);
764 sp<AudioPolicyService> svc = mService.promote();
765 if (svc == 0) {
766 break;
767 }
768 mLock.unlock();
769 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
770 mLock.lock();
771 delete data;
772 }break;
773 case RELEASE_OUTPUT: {
774 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam;
775 ALOGV("AudioCommandThread() processing release output %d",
776 data->mIO);
777 sp<AudioPolicyService> svc = mService.promote();
778 if (svc == 0) {
779 break;
780 }
781 mLock.unlock();
782 svc->doReleaseOutput(data->mIO);
783 mLock.lock();
784 delete data;
785 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700786 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000787 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700788 }
789 delete command;
790 waitTime = INT64_MAX;
791 } else {
792 waitTime = mAudioCommands[0]->mTime - curTime;
793 break;
794 }
795 }
796 // release delayed commands wake lock
Eric Laurentbfb1b832013-01-07 09:53:42 -0800797 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700798 release_wake_lock(mName.string());
799 }
Steve Block3856b092011-10-20 11:56:00 +0100800 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700801 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100802 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700803 }
804 mLock.unlock();
805 return false;
806}
807
808status_t AudioPolicyService::AudioCommandThread::dump(int fd)
809{
810 const size_t SIZE = 256;
811 char buffer[SIZE];
812 String8 result;
813
814 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
815 result.append(buffer);
816 write(fd, result.string(), result.size());
817
818 bool locked = tryLock(mLock);
819 if (!locked) {
820 String8 result2(kCmdDeadlockedString);
821 write(fd, result2.string(), result2.size());
822 }
823
824 snprintf(buffer, SIZE, "- Commands:\n");
825 result = String8(buffer);
826 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800827 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700828 mAudioCommands[i]->dump(buffer, SIZE);
829 result.append(buffer);
830 }
831 result.append(" Last Command\n");
832 mLastCommand.dump(buffer, SIZE);
833 result.append(buffer);
834
835 write(fd, result.string(), result.size());
836
837 if (locked) mLock.unlock();
838
839 return NO_ERROR;
840}
841
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800842void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
843 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700844{
845 AudioCommand *command = new AudioCommand();
846 command->mCommand = START_TONE;
847 ToneData *data = new ToneData();
848 data->mType = type;
849 data->mStream = stream;
850 command->mParam = (void *)data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700851 Mutex::Autolock _l(mLock);
852 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100853 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700854 mWaitWorkCV.signal();
855}
856
857void AudioPolicyService::AudioCommandThread::stopToneCommand()
858{
859 AudioCommand *command = new AudioCommand();
860 command->mCommand = STOP_TONE;
861 command->mParam = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700862 Mutex::Autolock _l(mLock);
863 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100864 ALOGV("AudioCommandThread() adding tone stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700865 mWaitWorkCV.signal();
866}
867
Glenn Kastenfff6d712012-01-12 16:38:12 -0800868status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700869 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800870 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700871 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700872{
873 status_t status = NO_ERROR;
874
875 AudioCommand *command = new AudioCommand();
876 command->mCommand = SET_VOLUME;
877 VolumeData *data = new VolumeData();
878 data->mStream = stream;
879 data->mVolume = volume;
880 data->mIO = output;
881 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700882 Mutex::Autolock _l(mLock);
883 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100884 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700885 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700886 mWaitWorkCV.signal();
887 if (command->mWaitStatus) {
888 command->mCond.wait(mLock);
889 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100890 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700891 }
892 return status;
893}
894
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800895status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700896 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700897 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700898{
899 status_t status = NO_ERROR;
900
901 AudioCommand *command = new AudioCommand();
902 command->mCommand = SET_PARAMETERS;
903 ParametersData *data = new ParametersData();
904 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700905 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700906 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700907 Mutex::Autolock _l(mLock);
908 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100909 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700910 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911 mWaitWorkCV.signal();
912 if (command->mWaitStatus) {
913 command->mCond.wait(mLock);
914 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100915 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700916 }
917 return status;
918}
919
920status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
921{
922 status_t status = NO_ERROR;
923
924 AudioCommand *command = new AudioCommand();
925 command->mCommand = SET_VOICE_VOLUME;
926 VoiceVolumeData *data = new VoiceVolumeData();
927 data->mVolume = volume;
928 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700929 Mutex::Autolock _l(mLock);
930 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100931 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700932 mWaitWorkCV.signal();
933 if (command->mWaitStatus) {
934 command->mCond.wait(mLock);
935 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100936 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700937 }
938 return status;
939}
940
Eric Laurentbfb1b832013-01-07 09:53:42 -0800941void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
942 audio_stream_type_t stream,
943 int session)
944{
945 AudioCommand *command = new AudioCommand();
946 command->mCommand = STOP_OUTPUT;
947 StopOutputData *data = new StopOutputData();
948 data->mIO = output;
949 data->mStream = stream;
950 data->mSession = session;
951 command->mParam = (void *)data;
952 Mutex::Autolock _l(mLock);
953 insertCommand_l(command);
954 ALOGV("AudioCommandThread() adding stop output %d", output);
955 mWaitWorkCV.signal();
956}
957
958void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
959{
960 AudioCommand *command = new AudioCommand();
961 command->mCommand = RELEASE_OUTPUT;
962 ReleaseOutputData *data = new ReleaseOutputData();
963 data->mIO = output;
964 command->mParam = (void *)data;
965 Mutex::Autolock _l(mLock);
966 insertCommand_l(command);
967 ALOGV("AudioCommandThread() adding release output %d", output);
968 mWaitWorkCV.signal();
969}
970
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971// insertCommand_l() must be called with mLock held
972void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
973{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800974 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700975 Vector <AudioCommand *> removedCommands;
Eric Laurentcec4abb2012-07-03 12:23:02 -0700976 nsecs_t time = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700977 command->mTime = systemTime() + milliseconds(delayMs);
978
979 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800980 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700981 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
982 }
983
984 // check same pending commands with later time stamps and eliminate them
985 for (i = mAudioCommands.size()-1; i >= 0; i--) {
986 AudioCommand *command2 = mAudioCommands[i];
987 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
988 if (command2->mTime <= command->mTime) break;
989 if (command2->mCommand != command->mCommand) continue;
990
991 switch (command->mCommand) {
992 case SET_PARAMETERS: {
993 ParametersData *data = (ParametersData *)command->mParam;
994 ParametersData *data2 = (ParametersData *)command2->mParam;
995 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100996 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700997 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700998 AudioParameter param = AudioParameter(data->mKeyValuePairs);
999 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1000 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001001 String8 key;
1002 String8 value;
1003 param.getAt(j, key, value);
1004 for (size_t k = 0; k < param2.size(); k++) {
1005 String8 key2;
1006 String8 value2;
1007 param2.getAt(k, key2, value2);
1008 if (key2 == key) {
1009 param2.remove(key2);
1010 ALOGV("Filtering out parameter %s", key2.string());
1011 break;
1012 }
1013 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001014 }
1015 // if all keys have been filtered out, remove the command.
1016 // otherwise, update the key value pairs
1017 if (param2.size() == 0) {
1018 removedCommands.add(command2);
1019 } else {
1020 data2->mKeyValuePairs = param2.toString();
1021 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001022 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001023 } break;
1024
1025 case SET_VOLUME: {
1026 VolumeData *data = (VolumeData *)command->mParam;
1027 VolumeData *data2 = (VolumeData *)command2->mParam;
1028 if (data->mIO != data2->mIO) break;
1029 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001030 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001031 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001032 removedCommands.add(command2);
Eric Laurentcec4abb2012-07-03 12:23:02 -07001033 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001034 } break;
1035 case START_TONE:
1036 case STOP_TONE:
1037 default:
1038 break;
1039 }
1040 }
1041
1042 // remove filtered commands
1043 for (size_t j = 0; j < removedCommands.size(); j++) {
1044 // removed commands always have time stamps greater than current command
1045 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
1046 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +01001047 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001048 mAudioCommands.removeAt(k);
1049 break;
1050 }
1051 }
1052 }
1053 removedCommands.clear();
1054
Eric Laurentcec4abb2012-07-03 12:23:02 -07001055 // wait for status only if delay is 0 and command time was not modified above
1056 if (delayMs == 0 && time == 0) {
1057 command->mWaitStatus = true;
1058 } else {
1059 command->mWaitStatus = false;
1060 }
1061 // update command time if modified above
1062 if (time != 0) {
1063 command->mTime = time;
1064 }
1065
Mathias Agopian65ab4712010-07-14 17:59:35 -07001066 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +01001067 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -07001068 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001069 mAudioCommands.insertAt(command, i + 1);
1070}
1071
1072void AudioPolicyService::AudioCommandThread::exit()
1073{
Steve Block3856b092011-10-20 11:56:00 +01001074 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001075 {
1076 AutoMutex _l(mLock);
1077 requestExit();
1078 mWaitWorkCV.signal();
1079 }
1080 requestExitAndWait();
1081}
1082
1083void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1084{
1085 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1086 mCommand,
1087 (int)ns2s(mTime),
1088 (int)ns2ms(mTime)%1000,
1089 mWaitStatus,
1090 mParam);
1091}
1092
Dima Zavinfce7a472011-04-19 22:30:36 -07001093/******* helpers for the service_ops callbacks defined below *********/
1094void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1095 const char *keyValuePairs,
1096 int delayMs)
1097{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001098 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001099 delayMs);
1100}
1101
1102int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1103 float volume,
1104 audio_io_handle_t output,
1105 int delayMs)
1106{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001107 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001108 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001109}
1110
1111int AudioPolicyService::startTone(audio_policy_tone_t tone,
1112 audio_stream_type_t stream)
1113{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001114 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +00001115 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001116 }
1117 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +00001118 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001119 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001120 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001121 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1122 AUDIO_STREAM_VOICE_CALL);
1123 return 0;
1124}
1125
1126int AudioPolicyService::stopTone()
1127{
1128 mTonePlaybackThread->stopToneCommand();
1129 return 0;
1130}
1131
1132int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1133{
1134 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1135}
1136
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001137bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
1138{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001139 if (mpAudioPolicy == NULL) {
1140 ALOGV("mpAudioPolicy == NULL");
1141 return false;
1142 }
1143
1144 if (mpAudioPolicy->is_offload_supported == NULL) {
1145 ALOGV("HAL does not implement is_offload_supported");
1146 return false;
1147 }
1148
1149 return mpAudioPolicy->is_offload_supported(mpAudioPolicy, &info);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001150}
1151
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001152// ----------------------------------------------------------------------------
1153// Audio pre-processing configuration
1154// ----------------------------------------------------------------------------
1155
Glenn Kasten8dad0e32012-01-09 08:41:22 -08001156/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001157 MIC_SRC_TAG,
1158 VOICE_UL_SRC_TAG,
1159 VOICE_DL_SRC_TAG,
1160 VOICE_CALL_SRC_TAG,
1161 CAMCORDER_SRC_TAG,
1162 VOICE_REC_SRC_TAG,
1163 VOICE_COMM_SRC_TAG
1164};
1165
1166// returns the audio_source_t enum corresponding to the input source name or
1167// AUDIO_SOURCE_CNT is no match found
1168audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
1169{
1170 int i;
1171 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
1172 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001173 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001174 break;
1175 }
1176 }
1177 return (audio_source_t)i;
1178}
1179
1180size_t AudioPolicyService::growParamSize(char *param,
1181 size_t size,
1182 size_t *curSize,
1183 size_t *totSize)
1184{
1185 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
1186 size_t pos = ((*curSize - 1 ) / size + 1) * size;
1187
1188 if (pos + size > *totSize) {
1189 while (pos + size > *totSize) {
1190 *totSize += ((*totSize + 7) / 8) * 4;
1191 }
1192 param = (char *)realloc(param, *totSize);
1193 }
1194 *curSize = pos + size;
1195 return pos;
1196}
1197
1198size_t AudioPolicyService::readParamValue(cnode *node,
1199 char *param,
1200 size_t *curSize,
1201 size_t *totSize)
1202{
1203 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
1204 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
1205 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001206 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001207 return sizeof(short);
1208 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
1209 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
1210 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001211 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001212 return sizeof(int);
1213 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
1214 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
1215 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001216 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001217 return sizeof(float);
1218 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
1219 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
1220 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1221 *(bool *)((char *)param + pos) = false;
1222 } else {
1223 *(bool *)((char *)param + pos) = true;
1224 }
Steve Block3856b092011-10-20 11:56:00 +01001225 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001226 return sizeof(bool);
1227 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1228 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1229 if (*curSize + len + 1 > *totSize) {
1230 *totSize = *curSize + len + 1;
1231 param = (char *)realloc(param, *totSize);
1232 }
1233 strncpy(param + *curSize, node->value, len);
1234 *curSize += len;
1235 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +01001236 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001237 return len;
1238 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001239 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001240 return 0;
1241}
1242
1243effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1244{
1245 cnode *param;
1246 cnode *value;
1247 size_t curSize = sizeof(effect_param_t);
1248 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1249 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1250
1251 param = config_find(root, PARAM_TAG);
1252 value = config_find(root, VALUE_TAG);
1253 if (param == NULL && value == NULL) {
1254 // try to parse simple parameter form {int int}
1255 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -08001256 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001257 // Note: that a pair of random strings is read as 0 0
1258 int *ptr = (int *)fx_param->data;
1259 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +00001260 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001261 *ptr++ = atoi(param->name);
1262 *ptr = atoi(param->value);
1263 fx_param->psize = sizeof(int);
1264 fx_param->vsize = sizeof(int);
1265 return fx_param;
1266 }
1267 }
1268 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001269 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001270 goto error;
1271 }
1272
1273 fx_param->psize = 0;
1274 param = param->first_child;
1275 while (param) {
Steve Block3856b092011-10-20 11:56:00 +01001276 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001277 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1278 if (size == 0) {
1279 goto error;
1280 }
1281 fx_param->psize += size;
1282 param = param->next;
1283 }
1284
1285 // align start of value field on 32 bit boundary
1286 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1287
1288 fx_param->vsize = 0;
1289 value = value->first_child;
1290 while (value) {
Steve Block3856b092011-10-20 11:56:00 +01001291 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001292 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1293 if (size == 0) {
1294 goto error;
1295 }
1296 fx_param->vsize += size;
1297 value = value->next;
1298 }
1299
1300 return fx_param;
1301
1302error:
1303 delete fx_param;
1304 return NULL;
1305}
1306
1307void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1308{
1309 cnode *node = root->first_child;
1310 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001311 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001312 effect_param_t *param = loadEffectParameter(node);
1313 if (param == NULL) {
1314 node = node->next;
1315 continue;
1316 }
1317 params.add(param);
1318 node = node->next;
1319 }
1320}
1321
1322AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1323 cnode *root,
1324 const Vector <EffectDesc *>& effects)
1325{
1326 cnode *node = root->first_child;
1327 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001328 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001329 return NULL;
1330 }
1331 InputSourceDesc *source = new InputSourceDesc();
1332 while (node) {
1333 size_t i;
1334 for (i = 0; i < effects.size(); i++) {
1335 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001336 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001337 break;
1338 }
1339 }
1340 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +01001341 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001342 node = node->next;
1343 continue;
1344 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001345 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001346 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +01001347 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001348 source->mEffects.add(effect);
1349 node = node->next;
1350 }
1351 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001352 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001353 delete source;
1354 return NULL;
1355 }
1356 return source;
1357}
1358
1359status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1360{
1361 cnode *node = config_find(root, PREPROCESSING_TAG);
1362 if (node == NULL) {
1363 return -ENOENT;
1364 }
1365 node = node->first_child;
1366 while (node) {
1367 audio_source_t source = inputSourceNameToEnum(node->name);
1368 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001369 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001370 node = node->next;
1371 continue;
1372 }
Steve Block3856b092011-10-20 11:56:00 +01001373 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001374 InputSourceDesc *desc = loadInputSource(node, effects);
1375 if (desc == NULL) {
1376 node = node->next;
1377 continue;
1378 }
1379 mInputSources.add(source, desc);
1380 node = node->next;
1381 }
1382 return NO_ERROR;
1383}
1384
1385AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1386{
1387 cnode *node = config_find(root, UUID_TAG);
1388 if (node == NULL) {
1389 return NULL;
1390 }
1391 effect_uuid_t uuid;
1392 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001393 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001394 return NULL;
1395 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001396 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001397}
1398
1399status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1400{
1401 cnode *node = config_find(root, EFFECTS_TAG);
1402 if (node == NULL) {
1403 return -ENOENT;
1404 }
1405 node = node->first_child;
1406 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001407 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001408 EffectDesc *effect = loadEffect(node);
1409 if (effect == NULL) {
1410 node = node->next;
1411 continue;
1412 }
1413 effects.add(effect);
1414 node = node->next;
1415 }
1416 return NO_ERROR;
1417}
1418
1419status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1420{
1421 cnode *root;
1422 char *data;
1423
1424 data = (char *)load_file(path, NULL);
1425 if (data == NULL) {
1426 return -ENODEV;
1427 }
1428 root = config_node("", "");
1429 config_load(root, data);
1430
1431 Vector <EffectDesc *> effects;
1432 loadEffects(root, effects);
1433 loadInputSources(root, effects);
1434
1435 config_free(root);
1436 free(root);
1437 free(data);
1438
1439 return NO_ERROR;
1440}
1441
Dima Zavinfce7a472011-04-19 22:30:36 -07001442/* implementation of the interface to the policy manager */
1443extern "C" {
1444
Eric Laurenta4c5a552012-03-29 10:12:40 -07001445
1446static audio_module_handle_t aps_load_hw_module(void *service,
1447 const char *name)
Dima Zavinfce7a472011-04-19 22:30:36 -07001448{
1449 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001450 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001451 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001452 return 0;
1453 }
1454
Eric Laurenta4c5a552012-03-29 10:12:40 -07001455 return af->loadHwModule(name);
1456}
1457
1458// deprecated: replaced by aps_open_output_on_module()
1459static audio_io_handle_t aps_open_output(void *service,
1460 audio_devices_t *pDevices,
1461 uint32_t *pSamplingRate,
1462 audio_format_t *pFormat,
1463 audio_channel_mask_t *pChannelMask,
1464 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001465 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001466{
1467 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1468 if (af == 0) {
1469 ALOGW("%s: could not get AudioFlinger", __func__);
1470 return 0;
1471 }
1472
1473 return af->openOutput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask,
1474 pLatencyMs, flags);
1475}
1476
1477static audio_io_handle_t aps_open_output_on_module(void *service,
1478 audio_module_handle_t module,
1479 audio_devices_t *pDevices,
1480 uint32_t *pSamplingRate,
1481 audio_format_t *pFormat,
1482 audio_channel_mask_t *pChannelMask,
1483 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001484 audio_output_flags_t flags,
1485 const audio_offload_info_t *offloadInfo)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001486{
1487 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1488 if (af == 0) {
1489 ALOGW("%s: could not get AudioFlinger", __func__);
1490 return 0;
1491 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001492 return af->openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
Eric Laurentbfb1b832013-01-07 09:53:42 -08001493 pLatencyMs, flags, offloadInfo);
Dima Zavinfce7a472011-04-19 22:30:36 -07001494}
1495
1496static audio_io_handle_t aps_open_dup_output(void *service,
1497 audio_io_handle_t output1,
1498 audio_io_handle_t output2)
1499{
1500 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001501 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001502 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001503 return 0;
1504 }
1505 return af->openDuplicateOutput(output1, output2);
1506}
1507
1508static int aps_close_output(void *service, audio_io_handle_t output)
1509{
1510 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001511 if (af == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001512 return PERMISSION_DENIED;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001513 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001514
1515 return af->closeOutput(output);
1516}
1517
1518static int aps_suspend_output(void *service, audio_io_handle_t output)
1519{
1520 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001521 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001522 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001523 return PERMISSION_DENIED;
1524 }
1525
1526 return af->suspendOutput(output);
1527}
1528
1529static int aps_restore_output(void *service, audio_io_handle_t output)
1530{
1531 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001532 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001533 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001534 return PERMISSION_DENIED;
1535 }
1536
1537 return af->restoreOutput(output);
1538}
1539
Glenn Kasten20010052012-06-22 13:43:51 -07001540// deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
Dima Zavinfce7a472011-04-19 22:30:36 -07001541static audio_io_handle_t aps_open_input(void *service,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001542 audio_devices_t *pDevices,
1543 uint32_t *pSamplingRate,
1544 audio_format_t *pFormat,
1545 audio_channel_mask_t *pChannelMask,
1546 audio_in_acoustics_t acoustics)
Dima Zavinfce7a472011-04-19 22:30:36 -07001547{
1548 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001549 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001550 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001551 return 0;
1552 }
1553
Eric Laurenta4c5a552012-03-29 10:12:40 -07001554 return af->openInput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask);
1555}
1556
1557static audio_io_handle_t aps_open_input_on_module(void *service,
1558 audio_module_handle_t module,
1559 audio_devices_t *pDevices,
1560 uint32_t *pSamplingRate,
1561 audio_format_t *pFormat,
1562 audio_channel_mask_t *pChannelMask)
1563{
1564 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1565 if (af == 0) {
1566 ALOGW("%s: could not get AudioFlinger", __func__);
1567 return 0;
1568 }
1569
1570 return af->openInput(module, pDevices, pSamplingRate, pFormat, pChannelMask);
Dima Zavinfce7a472011-04-19 22:30:36 -07001571}
1572
1573static int aps_close_input(void *service, audio_io_handle_t input)
1574{
1575 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001576 if (af == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001577 return PERMISSION_DENIED;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001578 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001579
1580 return af->closeInput(input);
1581}
1582
1583static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1584 audio_io_handle_t output)
1585{
1586 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001587 if (af == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001588 return PERMISSION_DENIED;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001589 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001590
1591 return af->setStreamOutput(stream, output);
1592}
1593
1594static int aps_move_effects(void *service, int session,
1595 audio_io_handle_t src_output,
1596 audio_io_handle_t dst_output)
1597{
1598 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001599 if (af == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001600 return PERMISSION_DENIED;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001601 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001602
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001603 return af->moveEffects(session, src_output, dst_output);
Dima Zavinfce7a472011-04-19 22:30:36 -07001604}
1605
1606static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1607 const char *keys)
1608{
1609 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1610 return strdup(result.string());
1611}
1612
1613static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1614 const char *kv_pairs, int delay_ms)
1615{
1616 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1617
1618 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1619}
1620
1621static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1622 float volume, audio_io_handle_t output,
1623 int delay_ms)
1624{
1625 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1626
1627 return audioPolicyService->setStreamVolume(stream, volume, output,
1628 delay_ms);
1629}
1630
1631static int aps_start_tone(void *service, audio_policy_tone_t tone,
1632 audio_stream_type_t stream)
1633{
1634 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1635
1636 return audioPolicyService->startTone(tone, stream);
1637}
1638
1639static int aps_stop_tone(void *service)
1640{
1641 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1642
1643 return audioPolicyService->stopTone();
1644}
1645
1646static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1647{
1648 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1649
1650 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1651}
1652
1653}; // extern "C"
1654
1655namespace {
1656 struct audio_policy_service_ops aps_ops = {
1657 open_output : aps_open_output,
1658 open_duplicate_output : aps_open_dup_output,
1659 close_output : aps_close_output,
1660 suspend_output : aps_suspend_output,
1661 restore_output : aps_restore_output,
1662 open_input : aps_open_input,
1663 close_input : aps_close_input,
1664 set_stream_volume : aps_set_stream_volume,
1665 set_stream_output : aps_set_stream_output,
1666 set_parameters : aps_set_parameters,
1667 get_parameters : aps_get_parameters,
1668 start_tone : aps_start_tone,
1669 stop_tone : aps_stop_tone,
1670 set_voice_volume : aps_set_voice_volume,
1671 move_effects : aps_move_effects,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001672 load_hw_module : aps_load_hw_module,
1673 open_output_on_module : aps_open_output_on_module,
1674 open_input_on_module : aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001675 };
1676}; // namespace <unnamed>
1677
Mathias Agopian65ab4712010-07-14 17:59:35 -07001678}; // namespace android