blob: d19278726d14ec78533699a72f146e73fd8db2c1 [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
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010054static const nsecs_t kAudioCommandTimeout = 3000000000; // 3 seconds
55
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
73 mTonePlaybackThread = new AudioCommandThread(String8(""));
74 // start audio commands thread
Glenn Kasten480b4682012-02-28 12:30:08 -080075 mAudioCommandThread = new AudioCommandThread(String8("ApmCommand"));
Mathias Agopian65ab4712010-07-14 17:59:35 -070076
Dima Zavinfce7a472011-04-19 22:30:36 -070077 /* instantiate the audio policy manager */
78 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
79 if (rc)
80 return;
Mathias Agopian65ab4712010-07-14 17:59:35 -070081
Dima Zavinfce7a472011-04-19 22:30:36 -070082 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
Steve Block29357bc2012-01-06 19:20:56 +000083 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070084 if (rc)
85 return;
Eric Laurent93575202011-01-18 18:39:02 -080086
Dima Zavinfce7a472011-04-19 22:30:36 -070087 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
88 &mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000089 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070090 if (rc)
91 return;
92
93 rc = mpAudioPolicy->init_check(mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000094 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070095 if (rc)
96 return;
97
Steve Blockdf64d152012-01-04 20:05:49 +000098 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurent7c7f10b2011-06-17 21:29:58 -070099
100 // load audio pre processing modules
101 if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) {
102 loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE);
103 } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) {
104 loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE);
105 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700106}
107
108AudioPolicyService::~AudioPolicyService()
109{
110 mTonePlaybackThread->exit();
111 mTonePlaybackThread.clear();
112 mAudioCommandThread->exit();
113 mAudioCommandThread.clear();
114
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700115
116 // release audio pre processing resources
117 for (size_t i = 0; i < mInputSources.size(); i++) {
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800118 delete mInputSources.valueAt(i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700119 }
120 mInputSources.clear();
121
122 for (size_t i = 0; i < mInputs.size(); i++) {
123 mInputs.valueAt(i)->mEffects.clear();
124 delete mInputs.valueAt(i);
125 }
126 mInputs.clear();
127
Glenn Kastena0d68332012-01-27 16:47:15 -0800128 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL)
Dima Zavinfce7a472011-04-19 22:30:36 -0700129 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kastena0d68332012-01-27 16:47:15 -0800130 if (mpAudioPolicyDev != NULL)
Dima Zavinfce7a472011-04-19 22:30:36 -0700131 audio_policy_dev_close(mpAudioPolicyDev);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700132}
133
Dima Zavinfce7a472011-04-19 22:30:36 -0700134status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
135 audio_policy_dev_state_t state,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700136 const char *device_address)
137{
Dima Zavinfce7a472011-04-19 22:30:36 -0700138 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700139 return NO_INIT;
140 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800141 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700142 return PERMISSION_DENIED;
143 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700144 if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700145 return BAD_VALUE;
146 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700147 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
148 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700149 return BAD_VALUE;
150 }
151
Glenn Kasten411e4472012-11-02 10:00:06 -0700152 ALOGV("setDeviceConnectionState()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700153 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700154 return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device,
155 state, device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700156}
157
Dima Zavinfce7a472011-04-19 22:30:36 -0700158audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
159 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -0700160 const char *device_address)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700161{
Dima Zavinfce7a472011-04-19 22:30:36 -0700162 if (mpAudioPolicy == NULL) {
163 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700164 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700165 return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device,
166 device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700167}
168
Glenn Kastenf78aee72012-01-04 11:00:47 -0800169status_t AudioPolicyService::setPhoneState(audio_mode_t state)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700170{
Dima Zavinfce7a472011-04-19 22:30:36 -0700171 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700172 return NO_INIT;
173 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800174 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700175 return PERMISSION_DENIED;
176 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800177 if (uint32_t(state) >= AUDIO_MODE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700178 return BAD_VALUE;
179 }
180
Glenn Kasten411e4472012-11-02 10:00:06 -0700181 ALOGV("setPhoneState()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700182
183 // TODO: check if it is more appropriate to do it in platform specific policy manager
184 AudioSystem::setMode(state);
185
186 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700187 mpAudioPolicy->set_phone_state(mpAudioPolicy, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700188 return NO_ERROR;
189}
190
Dima Zavinfce7a472011-04-19 22:30:36 -0700191status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
192 audio_policy_forced_cfg_t config)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700193{
Dima Zavinfce7a472011-04-19 22:30:36 -0700194 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700195 return NO_INIT;
196 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800197 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700198 return PERMISSION_DENIED;
199 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700200 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700201 return BAD_VALUE;
202 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700203 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700204 return BAD_VALUE;
205 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700206 ALOGV("setForceUse()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700207 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700208 mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700209 return NO_ERROR;
210}
211
Dima Zavinfce7a472011-04-19 22:30:36 -0700212audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700213{
Dima Zavinfce7a472011-04-19 22:30:36 -0700214 if (mpAudioPolicy == NULL) {
215 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700216 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700217 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
218 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700219 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700220 return mpAudioPolicy->get_force_use(mpAudioPolicy, usage);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700221}
222
Dima Zavinfce7a472011-04-19 22:30:36 -0700223audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700224 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800225 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700226 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000227 audio_output_flags_t flags,
228 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700229{
Dima Zavinfce7a472011-04-19 22:30:36 -0700230 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700231 return 0;
232 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700233 ALOGV("getOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700234 Mutex::Autolock _l(mLock);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000235 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate,
236 format, channelMask, flags, offloadInfo);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700237}
238
Eric Laurentde070132010-07-13 04:45:46 -0700239status_t AudioPolicyService::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700240 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700241 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242{
Dima Zavinfce7a472011-04-19 22:30:36 -0700243 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244 return NO_INIT;
245 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700246 ALOGV("startOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700247 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700248 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700249}
250
Eric Laurentde070132010-07-13 04:45:46 -0700251status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700252 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700253 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700254{
Dima Zavinfce7a472011-04-19 22:30:36 -0700255 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700256 return NO_INIT;
257 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700258 ALOGV("stopOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700259 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700260 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700261}
262
263void AudioPolicyService::releaseOutput(audio_io_handle_t output)
264{
Dima Zavinfce7a472011-04-19 22:30:36 -0700265 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700266 return;
267 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700268 ALOGV("releaseOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700269 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700270 mpAudioPolicy->release_output(mpAudioPolicy, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700271}
272
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800273audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700274 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800275 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700276 audio_channel_mask_t channelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700277 int audioSession)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700278{
Dima Zavinfce7a472011-04-19 22:30:36 -0700279 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700280 return 0;
281 }
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800282 // already checked by client, but double-check in case the client wrapper is bypassed
283 if (uint32_t(inputSource) >= AUDIO_SOURCE_CNT) {
284 return 0;
285 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700286 Mutex::Autolock _l(mLock);
Glenn Kasten20010052012-06-22 13:43:51 -0700287 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700288 audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
Glenn Kasten8af901c2012-11-01 11:11:38 -0700289 format, channelMask, (audio_in_acoustics_t) 0);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700290
291 if (input == 0) {
292 return input;
293 }
294 // create audio pre processors according to input source
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800295 ssize_t index = mInputSources.indexOfKey(inputSource);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700296 if (index < 0) {
297 return input;
298 }
299 ssize_t idx = mInputs.indexOfKey(input);
300 InputDesc *inputDesc;
301 if (idx < 0) {
Glenn Kasten81872a22012-03-07 16:49:22 -0800302 inputDesc = new InputDesc(audioSession);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700303 mInputs.add(input, inputDesc);
304 } else {
305 inputDesc = mInputs.valueAt(idx);
306 }
307
308 Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects;
309 for (size_t i = 0; i < effects.size(); i++) {
310 EffectDesc *effect = effects[i];
311 sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input);
312 status_t status = fx->initCheck();
313 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000314 ALOGW("Failed to create Fx %s on input %d", effect->mName, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700315 // fx goes out of scope and strong ref on AudioEffect is released
316 continue;
317 }
318 for (size_t j = 0; j < effect->mParams.size(); j++) {
319 fx->setParameter(effect->mParams[j]);
320 }
321 inputDesc->mEffects.add(fx);
322 }
323 setPreProcessorEnabled(inputDesc, true);
324 return input;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700325}
326
327status_t AudioPolicyService::startInput(audio_io_handle_t input)
328{
Dima Zavinfce7a472011-04-19 22:30:36 -0700329 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700330 return NO_INIT;
331 }
332 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700333
Dima Zavinfce7a472011-04-19 22:30:36 -0700334 return mpAudioPolicy->start_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335}
336
337status_t AudioPolicyService::stopInput(audio_io_handle_t input)
338{
Dima Zavinfce7a472011-04-19 22:30:36 -0700339 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700340 return NO_INIT;
341 }
342 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700343
Dima Zavinfce7a472011-04-19 22:30:36 -0700344 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345}
346
347void AudioPolicyService::releaseInput(audio_io_handle_t input)
348{
Dima Zavinfce7a472011-04-19 22:30:36 -0700349 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700350 return;
351 }
352 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700353 mpAudioPolicy->release_input(mpAudioPolicy, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700354
355 ssize_t index = mInputs.indexOfKey(input);
356 if (index < 0) {
357 return;
358 }
359 InputDesc *inputDesc = mInputs.valueAt(index);
360 setPreProcessorEnabled(inputDesc, false);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700361 delete inputDesc;
362 mInputs.removeItemsAt(index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700363}
364
Dima Zavinfce7a472011-04-19 22:30:36 -0700365status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700366 int indexMin,
367 int indexMax)
368{
Dima Zavinfce7a472011-04-19 22:30:36 -0700369 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370 return NO_INIT;
371 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800372 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700373 return PERMISSION_DENIED;
374 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800375 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700376 return BAD_VALUE;
377 }
Eric Laurent5f121362012-06-15 14:45:03 -0700378 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700379 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700380 return NO_ERROR;
381}
382
Eric Laurent83844cc2011-11-18 16:43:31 -0800383status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
384 int index,
385 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700386{
Dima Zavinfce7a472011-04-19 22:30:36 -0700387 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388 return NO_INIT;
389 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800390 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700391 return PERMISSION_DENIED;
392 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800393 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700394 return BAD_VALUE;
395 }
Eric Laurent5f121362012-06-15 14:45:03 -0700396 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800397 if (mpAudioPolicy->set_stream_volume_index_for_device) {
398 return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
399 stream,
400 index,
401 device);
402 } else {
403 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
404 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700405}
406
Eric Laurent83844cc2011-11-18 16:43:31 -0800407status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
408 int *index,
409 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700410{
Dima Zavinfce7a472011-04-19 22:30:36 -0700411 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700412 return NO_INIT;
413 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800414 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700415 return BAD_VALUE;
416 }
Eric Laurent5f121362012-06-15 14:45:03 -0700417 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800418 if (mpAudioPolicy->get_stream_volume_index_for_device) {
419 return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
420 stream,
421 index,
422 device);
423 } else {
424 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
425 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700426}
427
Dima Zavinfce7a472011-04-19 22:30:36 -0700428uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700429{
Dima Zavinfce7a472011-04-19 22:30:36 -0700430 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700431 return 0;
432 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700433 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurentde070132010-07-13 04:45:46 -0700434}
435
Eric Laurent63742522012-03-08 13:42:42 -0800436//audio policy: use audio_device_t appropriately
437
438audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800439{
Dima Zavinfce7a472011-04-19 22:30:36 -0700440 if (mpAudioPolicy == NULL) {
Eric Laurent63742522012-03-08 13:42:42 -0800441 return (audio_devices_t)0;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800442 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700443 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800444}
445
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700446audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700447{
Dima Zavinfce7a472011-04-19 22:30:36 -0700448 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700449 return NO_INIT;
450 }
451 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700452 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700453}
454
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700455status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700456 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700457 uint32_t strategy,
458 int session,
459 int id)
460{
Dima Zavinfce7a472011-04-19 22:30:36 -0700461 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700462 return NO_INIT;
463 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700464 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700465}
466
467status_t AudioPolicyService::unregisterEffect(int id)
468{
Dima Zavinfce7a472011-04-19 22:30:36 -0700469 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700470 return NO_INIT;
471 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700472 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700473}
474
Eric Laurentdb7c0792011-08-10 10:37:50 -0700475status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
476{
477 if (mpAudioPolicy == NULL) {
478 return NO_INIT;
479 }
480 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
481}
482
Glenn Kastenfff6d712012-01-12 16:38:12 -0800483bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800484{
Dima Zavinfce7a472011-04-19 22:30:36 -0700485 if (mpAudioPolicy == NULL) {
Eric Laurenteda6c362011-02-02 09:33:30 -0800486 return 0;
487 }
488 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700489 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800490}
491
Jean-Michel Trivie336f912013-02-04 16:26:02 -0800492bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
493{
494 if (mpAudioPolicy == NULL) {
495 return 0;
496 }
497 Mutex::Autolock _l(mLock);
498 return mpAudioPolicy->is_stream_active_remotely(mpAudioPolicy, stream, inPastMs);
499}
500
Jean-Michel Trivie3f641f2012-10-10 12:11:16 -0700501bool AudioPolicyService::isSourceActive(audio_source_t source) const
502{
503 if (mpAudioPolicy == NULL) {
504 return false;
505 }
506 if (mpAudioPolicy->is_source_active == 0) {
507 return false;
508 }
509 Mutex::Autolock _l(mLock);
510 return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
511}
512
Eric Laurent57dae992011-07-24 13:36:09 -0700513status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
514 effect_descriptor_t *descriptors,
515 uint32_t *count)
516{
517
518 if (mpAudioPolicy == NULL) {
519 *count = 0;
520 return NO_INIT;
521 }
522 Mutex::Autolock _l(mLock);
523 status_t status = NO_ERROR;
524
525 size_t index;
526 for (index = 0; index < mInputs.size(); index++) {
527 if (mInputs.valueAt(index)->mSessionId == audioSession) {
528 break;
529 }
530 }
531 if (index == mInputs.size()) {
532 *count = 0;
533 return BAD_VALUE;
534 }
535 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
536
537 for (size_t i = 0; i < effects.size(); i++) {
538 effect_descriptor_t desc = effects[i]->descriptor();
539 if (i < *count) {
Glenn Kastena189a682012-02-20 12:16:30 -0800540 descriptors[i] = desc;
Eric Laurent57dae992011-07-24 13:36:09 -0700541 }
542 }
543 if (effects.size() > *count) {
544 status = NO_MEMORY;
545 }
546 *count = effects.size();
547 return status;
548}
549
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700551 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700552 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553}
554
555static bool tryLock(Mutex& mutex)
556{
557 bool locked = false;
558 for (int i = 0; i < kDumpLockRetries; ++i) {
559 if (mutex.tryLock() == NO_ERROR) {
560 locked = true;
561 break;
562 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800563 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700564 }
565 return locked;
566}
567
568status_t AudioPolicyService::dumpInternals(int fd)
569{
570 const size_t SIZE = 256;
571 char buffer[SIZE];
572 String8 result;
573
Dima Zavinfce7a472011-04-19 22:30:36 -0700574 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700575 result.append(buffer);
576 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
577 result.append(buffer);
578 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
579 result.append(buffer);
580
581 write(fd, result.string(), result.size());
582 return NO_ERROR;
583}
584
585status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
586{
Glenn Kasten44deb052012-02-05 18:09:08 -0800587 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700588 dumpPermissionDenial(fd);
589 } else {
590 bool locked = tryLock(mLock);
591 if (!locked) {
592 String8 result(kDeadlockedString);
593 write(fd, result.string(), result.size());
594 }
595
596 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800597 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700598 mAudioCommandThread->dump(fd);
599 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800600 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700601 mTonePlaybackThread->dump(fd);
602 }
603
Dima Zavinfce7a472011-04-19 22:30:36 -0700604 if (mpAudioPolicy) {
605 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700606 }
607
608 if (locked) mLock.unlock();
609 }
610 return NO_ERROR;
611}
612
613status_t AudioPolicyService::dumpPermissionDenial(int fd)
614{
615 const size_t SIZE = 256;
616 char buffer[SIZE];
617 String8 result;
618 snprintf(buffer, SIZE, "Permission Denial: "
619 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
620 IPCThreadState::self()->getCallingPid(),
621 IPCThreadState::self()->getCallingUid());
622 result.append(buffer);
623 write(fd, result.string(), result.size());
624 return NO_ERROR;
625}
626
Glenn Kasten81872a22012-03-07 16:49:22 -0800627void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700628{
Glenn Kasten81872a22012-03-07 16:49:22 -0800629 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700630 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800631 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700632 }
633}
634
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635status_t AudioPolicyService::onTransact(
636 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
637{
638 return BnAudioPolicyService::onTransact(code, data, reply, flags);
639}
640
641
Mathias Agopian65ab4712010-07-14 17:59:35 -0700642// ----------- AudioPolicyService::AudioCommandThread implementation ----------
643
644AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name)
645 : Thread(false), mName(name)
646{
647 mpToneGenerator = NULL;
648}
649
650
651AudioPolicyService::AudioCommandThread::~AudioCommandThread()
652{
653 if (mName != "" && !mAudioCommands.isEmpty()) {
654 release_wake_lock(mName.string());
655 }
656 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800657 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700658}
659
660void AudioPolicyService::AudioCommandThread::onFirstRef()
661{
662 if (mName != "") {
663 run(mName.string(), ANDROID_PRIORITY_AUDIO);
664 } else {
Glenn Kasten480b4682012-02-28 12:30:08 -0800665 run("AudioCommand", ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666 }
667}
668
669bool AudioPolicyService::AudioCommandThread::threadLoop()
670{
671 nsecs_t waitTime = INT64_MAX;
672
673 mLock.lock();
674 while (!exitPending())
675 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700676 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700677 nsecs_t curTime = systemTime();
678 // commands are sorted by increasing time stamp: execute them from index 0 and up
679 if (mAudioCommands[0]->mTime <= curTime) {
680 AudioCommand *command = mAudioCommands[0];
681 mAudioCommands.removeAt(0);
682 mLastCommand = *command;
683
684 switch (command->mCommand) {
685 case START_TONE: {
686 mLock.unlock();
687 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100688 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700689 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800690 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700691 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
692 mpToneGenerator->startTone(data->mType);
693 delete data;
694 mLock.lock();
695 }break;
696 case STOP_TONE: {
697 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100698 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699 if (mpToneGenerator != NULL) {
700 mpToneGenerator->stopTone();
701 delete mpToneGenerator;
702 mpToneGenerator = NULL;
703 }
704 mLock.lock();
705 }break;
706 case SET_VOLUME: {
707 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100708 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700709 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
710 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
711 data->mVolume,
712 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700713 if (command->mWaitStatus) {
714 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100715 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700716 }
717 delete data;
718 }break;
719 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700720 ParametersData *data = (ParametersData *)command->mParam;
721 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
722 data->mKeyValuePairs.string(), data->mIO);
723 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
724 if (command->mWaitStatus) {
725 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100726 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700727 }
728 delete data;
729 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700730 case SET_VOICE_VOLUME: {
731 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100732 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700733 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700734 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
735 if (command->mWaitStatus) {
736 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100737 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700738 }
739 delete data;
740 }break;
741 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000742 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700743 }
744 delete command;
745 waitTime = INT64_MAX;
746 } else {
747 waitTime = mAudioCommands[0]->mTime - curTime;
748 break;
749 }
750 }
751 // release delayed commands wake lock
752 if (mName != "" && mAudioCommands.isEmpty()) {
753 release_wake_lock(mName.string());
754 }
Steve Block3856b092011-10-20 11:56:00 +0100755 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700756 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100757 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700758 }
759 mLock.unlock();
760 return false;
761}
762
763status_t AudioPolicyService::AudioCommandThread::dump(int fd)
764{
765 const size_t SIZE = 256;
766 char buffer[SIZE];
767 String8 result;
768
769 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
770 result.append(buffer);
771 write(fd, result.string(), result.size());
772
773 bool locked = tryLock(mLock);
774 if (!locked) {
775 String8 result2(kCmdDeadlockedString);
776 write(fd, result2.string(), result2.size());
777 }
778
779 snprintf(buffer, SIZE, "- Commands:\n");
780 result = String8(buffer);
781 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800782 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700783 mAudioCommands[i]->dump(buffer, SIZE);
784 result.append(buffer);
785 }
786 result.append(" Last Command\n");
787 mLastCommand.dump(buffer, SIZE);
788 result.append(buffer);
789
790 write(fd, result.string(), result.size());
791
792 if (locked) mLock.unlock();
793
794 return NO_ERROR;
795}
796
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800797void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
798 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700799{
800 AudioCommand *command = new AudioCommand();
801 command->mCommand = START_TONE;
802 ToneData *data = new ToneData();
803 data->mType = type;
804 data->mStream = stream;
805 command->mParam = (void *)data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700806 Mutex::Autolock _l(mLock);
807 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100808 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700809 mWaitWorkCV.signal();
810}
811
812void AudioPolicyService::AudioCommandThread::stopToneCommand()
813{
814 AudioCommand *command = new AudioCommand();
815 command->mCommand = STOP_TONE;
816 command->mParam = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700817 Mutex::Autolock _l(mLock);
818 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100819 ALOGV("AudioCommandThread() adding tone stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820 mWaitWorkCV.signal();
821}
822
Glenn Kastenfff6d712012-01-12 16:38:12 -0800823status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700824 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800825 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700826 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700827{
828 status_t status = NO_ERROR;
829
830 AudioCommand *command = new AudioCommand();
831 command->mCommand = SET_VOLUME;
832 VolumeData *data = new VolumeData();
833 data->mStream = stream;
834 data->mVolume = volume;
835 data->mIO = output;
836 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700837 Mutex::Autolock _l(mLock);
838 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100839 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700840 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700841 mWaitWorkCV.signal();
842 if (command->mWaitStatus) {
843 command->mCond.wait(mLock);
844 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100845 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700846 }
847 return status;
848}
849
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800850status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700851 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700852 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700853{
854 status_t status = NO_ERROR;
855
856 AudioCommand *command = new AudioCommand();
857 command->mCommand = SET_PARAMETERS;
858 ParametersData *data = new ParametersData();
859 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700860 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700861 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700862 Mutex::Autolock _l(mLock);
863 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100864 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700865 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700866 mWaitWorkCV.signal();
867 if (command->mWaitStatus) {
868 command->mCond.wait(mLock);
869 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100870 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700871 }
872 return status;
873}
874
875status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
876{
877 status_t status = NO_ERROR;
878
879 AudioCommand *command = new AudioCommand();
880 command->mCommand = SET_VOICE_VOLUME;
881 VoiceVolumeData *data = new VoiceVolumeData();
882 data->mVolume = volume;
883 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700884 Mutex::Autolock _l(mLock);
885 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100886 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700887 mWaitWorkCV.signal();
888 if (command->mWaitStatus) {
889 command->mCond.wait(mLock);
890 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100891 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700892 }
893 return status;
894}
895
896// insertCommand_l() must be called with mLock held
897void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
898{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800899 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700900 Vector <AudioCommand *> removedCommands;
901
Eric Laurentcec4abb2012-07-03 12:23:02 -0700902 nsecs_t time = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700903 command->mTime = systemTime() + milliseconds(delayMs);
904
905 // acquire wake lock to make sure delayed commands are processed
906 if (mName != "" && mAudioCommands.isEmpty()) {
907 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
908 }
909
910 // check same pending commands with later time stamps and eliminate them
911 for (i = mAudioCommands.size()-1; i >= 0; i--) {
912 AudioCommand *command2 = mAudioCommands[i];
913 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
914 if (command2->mTime <= command->mTime) break;
915 if (command2->mCommand != command->mCommand) continue;
916
917 switch (command->mCommand) {
918 case SET_PARAMETERS: {
919 ParametersData *data = (ParametersData *)command->mParam;
920 ParametersData *data2 = (ParametersData *)command2->mParam;
921 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100922 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700923 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700924 AudioParameter param = AudioParameter(data->mKeyValuePairs);
925 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
926 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700927 String8 key;
928 String8 value;
929 param.getAt(j, key, value);
930 for (size_t k = 0; k < param2.size(); k++) {
931 String8 key2;
932 String8 value2;
933 param2.getAt(k, key2, value2);
934 if (key2 == key) {
935 param2.remove(key2);
936 ALOGV("Filtering out parameter %s", key2.string());
937 break;
938 }
939 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940 }
941 // if all keys have been filtered out, remove the command.
942 // otherwise, update the key value pairs
943 if (param2.size() == 0) {
944 removedCommands.add(command2);
945 } else {
946 data2->mKeyValuePairs = param2.toString();
947 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700948 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700949 } break;
950
951 case SET_VOLUME: {
952 VolumeData *data = (VolumeData *)command->mParam;
953 VolumeData *data2 = (VolumeData *)command2->mParam;
954 if (data->mIO != data2->mIO) break;
955 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100956 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700957 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700958 removedCommands.add(command2);
Eric Laurentcec4abb2012-07-03 12:23:02 -0700959 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960 } break;
961 case START_TONE:
962 case STOP_TONE:
963 default:
964 break;
965 }
966 }
967
968 // remove filtered commands
969 for (size_t j = 0; j < removedCommands.size(); j++) {
970 // removed commands always have time stamps greater than current command
971 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
972 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +0100973 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700974 mAudioCommands.removeAt(k);
975 break;
976 }
977 }
978 }
979 removedCommands.clear();
980
Eric Laurentcec4abb2012-07-03 12:23:02 -0700981 // wait for status only if delay is 0 and command time was not modified above
982 if (delayMs == 0 && time == 0) {
983 command->mWaitStatus = true;
984 } else {
985 command->mWaitStatus = false;
986 }
987 // update command time if modified above
988 if (time != 0) {
989 command->mTime = time;
990 }
991
Mathias Agopian65ab4712010-07-14 17:59:35 -0700992 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +0100993 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -0700994 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700995 mAudioCommands.insertAt(command, i + 1);
996}
997
998void AudioPolicyService::AudioCommandThread::exit()
999{
Steve Block3856b092011-10-20 11:56:00 +01001000 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001001 {
1002 AutoMutex _l(mLock);
1003 requestExit();
1004 mWaitWorkCV.signal();
1005 }
1006 requestExitAndWait();
1007}
1008
1009void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1010{
1011 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1012 mCommand,
1013 (int)ns2s(mTime),
1014 (int)ns2ms(mTime)%1000,
1015 mWaitStatus,
1016 mParam);
1017}
1018
Dima Zavinfce7a472011-04-19 22:30:36 -07001019/******* helpers for the service_ops callbacks defined below *********/
1020void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1021 const char *keyValuePairs,
1022 int delayMs)
1023{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001024 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001025 delayMs);
1026}
1027
1028int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1029 float volume,
1030 audio_io_handle_t output,
1031 int delayMs)
1032{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001033 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001034 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001035}
1036
1037int AudioPolicyService::startTone(audio_policy_tone_t tone,
1038 audio_stream_type_t stream)
1039{
1040 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
Steve Block29357bc2012-01-06 19:20:56 +00001041 ALOGE("startTone: illegal tone requested (%d)", tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001042 if (stream != AUDIO_STREAM_VOICE_CALL)
Steve Block29357bc2012-01-06 19:20:56 +00001043 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001044 tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001045 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1046 AUDIO_STREAM_VOICE_CALL);
1047 return 0;
1048}
1049
1050int AudioPolicyService::stopTone()
1051{
1052 mTonePlaybackThread->stopToneCommand();
1053 return 0;
1054}
1055
1056int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1057{
1058 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1059}
1060
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001061bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
1062{
1063 return false; // stub function
1064}
1065
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001066// ----------------------------------------------------------------------------
1067// Audio pre-processing configuration
1068// ----------------------------------------------------------------------------
1069
Glenn Kasten8dad0e32012-01-09 08:41:22 -08001070/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001071 MIC_SRC_TAG,
1072 VOICE_UL_SRC_TAG,
1073 VOICE_DL_SRC_TAG,
1074 VOICE_CALL_SRC_TAG,
1075 CAMCORDER_SRC_TAG,
1076 VOICE_REC_SRC_TAG,
1077 VOICE_COMM_SRC_TAG
1078};
1079
1080// returns the audio_source_t enum corresponding to the input source name or
1081// AUDIO_SOURCE_CNT is no match found
1082audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
1083{
1084 int i;
1085 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
1086 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001087 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001088 break;
1089 }
1090 }
1091 return (audio_source_t)i;
1092}
1093
1094size_t AudioPolicyService::growParamSize(char *param,
1095 size_t size,
1096 size_t *curSize,
1097 size_t *totSize)
1098{
1099 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
1100 size_t pos = ((*curSize - 1 ) / size + 1) * size;
1101
1102 if (pos + size > *totSize) {
1103 while (pos + size > *totSize) {
1104 *totSize += ((*totSize + 7) / 8) * 4;
1105 }
1106 param = (char *)realloc(param, *totSize);
1107 }
1108 *curSize = pos + size;
1109 return pos;
1110}
1111
1112size_t AudioPolicyService::readParamValue(cnode *node,
1113 char *param,
1114 size_t *curSize,
1115 size_t *totSize)
1116{
1117 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
1118 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
1119 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001120 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001121 return sizeof(short);
1122 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
1123 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
1124 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001125 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001126 return sizeof(int);
1127 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
1128 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
1129 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001130 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001131 return sizeof(float);
1132 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
1133 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
1134 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1135 *(bool *)((char *)param + pos) = false;
1136 } else {
1137 *(bool *)((char *)param + pos) = true;
1138 }
Steve Block3856b092011-10-20 11:56:00 +01001139 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001140 return sizeof(bool);
1141 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1142 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1143 if (*curSize + len + 1 > *totSize) {
1144 *totSize = *curSize + len + 1;
1145 param = (char *)realloc(param, *totSize);
1146 }
1147 strncpy(param + *curSize, node->value, len);
1148 *curSize += len;
1149 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +01001150 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001151 return len;
1152 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001153 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001154 return 0;
1155}
1156
1157effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1158{
1159 cnode *param;
1160 cnode *value;
1161 size_t curSize = sizeof(effect_param_t);
1162 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1163 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1164
1165 param = config_find(root, PARAM_TAG);
1166 value = config_find(root, VALUE_TAG);
1167 if (param == NULL && value == NULL) {
1168 // try to parse simple parameter form {int int}
1169 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -08001170 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001171 // Note: that a pair of random strings is read as 0 0
1172 int *ptr = (int *)fx_param->data;
1173 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +00001174 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001175 *ptr++ = atoi(param->name);
1176 *ptr = atoi(param->value);
1177 fx_param->psize = sizeof(int);
1178 fx_param->vsize = sizeof(int);
1179 return fx_param;
1180 }
1181 }
1182 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001183 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001184 goto error;
1185 }
1186
1187 fx_param->psize = 0;
1188 param = param->first_child;
1189 while (param) {
Steve Block3856b092011-10-20 11:56:00 +01001190 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001191 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1192 if (size == 0) {
1193 goto error;
1194 }
1195 fx_param->psize += size;
1196 param = param->next;
1197 }
1198
1199 // align start of value field on 32 bit boundary
1200 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1201
1202 fx_param->vsize = 0;
1203 value = value->first_child;
1204 while (value) {
Steve Block3856b092011-10-20 11:56:00 +01001205 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001206 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1207 if (size == 0) {
1208 goto error;
1209 }
1210 fx_param->vsize += size;
1211 value = value->next;
1212 }
1213
1214 return fx_param;
1215
1216error:
1217 delete fx_param;
1218 return NULL;
1219}
1220
1221void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1222{
1223 cnode *node = root->first_child;
1224 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001225 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001226 effect_param_t *param = loadEffectParameter(node);
1227 if (param == NULL) {
1228 node = node->next;
1229 continue;
1230 }
1231 params.add(param);
1232 node = node->next;
1233 }
1234}
1235
1236AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1237 cnode *root,
1238 const Vector <EffectDesc *>& effects)
1239{
1240 cnode *node = root->first_child;
1241 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001242 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001243 return NULL;
1244 }
1245 InputSourceDesc *source = new InputSourceDesc();
1246 while (node) {
1247 size_t i;
1248 for (i = 0; i < effects.size(); i++) {
1249 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001250 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001251 break;
1252 }
1253 }
1254 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +01001255 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001256 node = node->next;
1257 continue;
1258 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001259 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001260 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +01001261 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001262 source->mEffects.add(effect);
1263 node = node->next;
1264 }
1265 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001266 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001267 delete source;
1268 return NULL;
1269 }
1270 return source;
1271}
1272
1273status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1274{
1275 cnode *node = config_find(root, PREPROCESSING_TAG);
1276 if (node == NULL) {
1277 return -ENOENT;
1278 }
1279 node = node->first_child;
1280 while (node) {
1281 audio_source_t source = inputSourceNameToEnum(node->name);
1282 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001283 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001284 node = node->next;
1285 continue;
1286 }
Steve Block3856b092011-10-20 11:56:00 +01001287 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001288 InputSourceDesc *desc = loadInputSource(node, effects);
1289 if (desc == NULL) {
1290 node = node->next;
1291 continue;
1292 }
1293 mInputSources.add(source, desc);
1294 node = node->next;
1295 }
1296 return NO_ERROR;
1297}
1298
1299AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1300{
1301 cnode *node = config_find(root, UUID_TAG);
1302 if (node == NULL) {
1303 return NULL;
1304 }
1305 effect_uuid_t uuid;
1306 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001307 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001308 return NULL;
1309 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001310 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001311}
1312
1313status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1314{
1315 cnode *node = config_find(root, EFFECTS_TAG);
1316 if (node == NULL) {
1317 return -ENOENT;
1318 }
1319 node = node->first_child;
1320 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001321 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001322 EffectDesc *effect = loadEffect(node);
1323 if (effect == NULL) {
1324 node = node->next;
1325 continue;
1326 }
1327 effects.add(effect);
1328 node = node->next;
1329 }
1330 return NO_ERROR;
1331}
1332
1333status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1334{
1335 cnode *root;
1336 char *data;
1337
1338 data = (char *)load_file(path, NULL);
1339 if (data == NULL) {
1340 return -ENODEV;
1341 }
1342 root = config_node("", "");
1343 config_load(root, data);
1344
1345 Vector <EffectDesc *> effects;
1346 loadEffects(root, effects);
1347 loadInputSources(root, effects);
1348
1349 config_free(root);
1350 free(root);
1351 free(data);
1352
1353 return NO_ERROR;
1354}
1355
Dima Zavinfce7a472011-04-19 22:30:36 -07001356/* implementation of the interface to the policy manager */
1357extern "C" {
1358
Eric Laurenta4c5a552012-03-29 10:12:40 -07001359
1360static audio_module_handle_t aps_load_hw_module(void *service,
1361 const char *name)
Dima Zavinfce7a472011-04-19 22:30:36 -07001362{
1363 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001364 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001365 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001366 return 0;
1367 }
1368
Eric Laurenta4c5a552012-03-29 10:12:40 -07001369 return af->loadHwModule(name);
1370}
1371
1372// deprecated: replaced by aps_open_output_on_module()
1373static audio_io_handle_t aps_open_output(void *service,
1374 audio_devices_t *pDevices,
1375 uint32_t *pSamplingRate,
1376 audio_format_t *pFormat,
1377 audio_channel_mask_t *pChannelMask,
1378 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001379 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001380{
1381 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1382 if (af == 0) {
1383 ALOGW("%s: could not get AudioFlinger", __func__);
1384 return 0;
1385 }
1386
1387 return af->openOutput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask,
1388 pLatencyMs, flags);
1389}
1390
1391static audio_io_handle_t aps_open_output_on_module(void *service,
1392 audio_module_handle_t module,
1393 audio_devices_t *pDevices,
1394 uint32_t *pSamplingRate,
1395 audio_format_t *pFormat,
1396 audio_channel_mask_t *pChannelMask,
1397 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001398 audio_output_flags_t flags,
1399 const audio_offload_info_t *offloadInfo)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001400{
1401 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1402 if (af == 0) {
1403 ALOGW("%s: could not get AudioFlinger", __func__);
1404 return 0;
1405 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001406 return af->openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
Dima Zavinfce7a472011-04-19 22:30:36 -07001407 pLatencyMs, flags);
1408}
1409
1410static audio_io_handle_t aps_open_dup_output(void *service,
1411 audio_io_handle_t output1,
1412 audio_io_handle_t output2)
1413{
1414 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001415 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001416 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001417 return 0;
1418 }
1419 return af->openDuplicateOutput(output1, output2);
1420}
1421
1422static int aps_close_output(void *service, audio_io_handle_t output)
1423{
1424 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001425 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001426 return PERMISSION_DENIED;
1427
1428 return af->closeOutput(output);
1429}
1430
1431static int aps_suspend_output(void *service, audio_io_handle_t output)
1432{
1433 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001434 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001435 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001436 return PERMISSION_DENIED;
1437 }
1438
1439 return af->suspendOutput(output);
1440}
1441
1442static int aps_restore_output(void *service, audio_io_handle_t output)
1443{
1444 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001445 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001446 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001447 return PERMISSION_DENIED;
1448 }
1449
1450 return af->restoreOutput(output);
1451}
1452
Glenn Kasten20010052012-06-22 13:43:51 -07001453// deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
Dima Zavinfce7a472011-04-19 22:30:36 -07001454static audio_io_handle_t aps_open_input(void *service,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001455 audio_devices_t *pDevices,
1456 uint32_t *pSamplingRate,
1457 audio_format_t *pFormat,
1458 audio_channel_mask_t *pChannelMask,
1459 audio_in_acoustics_t acoustics)
Dima Zavinfce7a472011-04-19 22:30:36 -07001460{
1461 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001462 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001463 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001464 return 0;
1465 }
1466
Eric Laurenta4c5a552012-03-29 10:12:40 -07001467 return af->openInput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask);
1468}
1469
1470static audio_io_handle_t aps_open_input_on_module(void *service,
1471 audio_module_handle_t module,
1472 audio_devices_t *pDevices,
1473 uint32_t *pSamplingRate,
1474 audio_format_t *pFormat,
1475 audio_channel_mask_t *pChannelMask)
1476{
1477 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1478 if (af == 0) {
1479 ALOGW("%s: could not get AudioFlinger", __func__);
1480 return 0;
1481 }
1482
1483 return af->openInput(module, pDevices, pSamplingRate, pFormat, pChannelMask);
Dima Zavinfce7a472011-04-19 22:30:36 -07001484}
1485
1486static int aps_close_input(void *service, audio_io_handle_t input)
1487{
1488 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001489 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001490 return PERMISSION_DENIED;
1491
1492 return af->closeInput(input);
1493}
1494
1495static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1496 audio_io_handle_t output)
1497{
1498 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001499 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001500 return PERMISSION_DENIED;
1501
1502 return af->setStreamOutput(stream, output);
1503}
1504
1505static int aps_move_effects(void *service, int session,
1506 audio_io_handle_t src_output,
1507 audio_io_handle_t dst_output)
1508{
1509 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001510 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001511 return PERMISSION_DENIED;
1512
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001513 return af->moveEffects(session, src_output, dst_output);
Dima Zavinfce7a472011-04-19 22:30:36 -07001514}
1515
1516static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1517 const char *keys)
1518{
1519 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1520 return strdup(result.string());
1521}
1522
1523static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1524 const char *kv_pairs, int delay_ms)
1525{
1526 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1527
1528 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1529}
1530
1531static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1532 float volume, audio_io_handle_t output,
1533 int delay_ms)
1534{
1535 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1536
1537 return audioPolicyService->setStreamVolume(stream, volume, output,
1538 delay_ms);
1539}
1540
1541static int aps_start_tone(void *service, audio_policy_tone_t tone,
1542 audio_stream_type_t stream)
1543{
1544 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1545
1546 return audioPolicyService->startTone(tone, stream);
1547}
1548
1549static int aps_stop_tone(void *service)
1550{
1551 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1552
1553 return audioPolicyService->stopTone();
1554}
1555
1556static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1557{
1558 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1559
1560 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1561}
1562
1563}; // extern "C"
1564
1565namespace {
1566 struct audio_policy_service_ops aps_ops = {
1567 open_output : aps_open_output,
1568 open_duplicate_output : aps_open_dup_output,
1569 close_output : aps_close_output,
1570 suspend_output : aps_suspend_output,
1571 restore_output : aps_restore_output,
1572 open_input : aps_open_input,
1573 close_input : aps_close_input,
1574 set_stream_volume : aps_set_stream_volume,
1575 set_stream_output : aps_set_stream_output,
1576 set_parameters : aps_set_parameters,
1577 get_parameters : aps_get_parameters,
1578 start_tone : aps_start_tone,
1579 stop_tone : aps_stop_tone,
1580 set_voice_volume : aps_set_voice_volume,
1581 move_effects : aps_move_effects,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001582 load_hw_module : aps_load_hw_module,
1583 open_output_on_module : aps_open_output_on_module,
1584 open_input_on_module : aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001585 };
1586}; // namespace <unnamed>
1587
Mathias Agopian65ab4712010-07-14 17:59:35 -07001588}; // namespace android