blob: 07607e13c2082a9c6352b8c8331fb3725090fb16 [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
20#undef __STRICT_ANSI__
21#define __STDINT_LIMITS
22#define __STDC_LIMIT_MACROS
23#include <stdint.h>
24
25#include <sys/time.h>
26#include <binder/IServiceManager.h>
27#include <utils/Log.h>
28#include <cutils/properties.h>
29#include <binder/IPCThreadState.h>
30#include <utils/String16.h>
31#include <utils/threads.h>
32#include "AudioPolicyService.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080033#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070035#include <media/AudioEffect.h>
36#include <media/EffectsFactoryApi.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070037
Dima Zavinfce7a472011-04-19 22:30:36 -070038#include <hardware/hardware.h>
Dima Zavin64760242011-05-11 14:15:23 -070039#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070040#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070041#include <hardware/audio_policy.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070042#include <audio_effects/audio_effects_conf.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070043#include <media/AudioParameter.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070044
Mathias Agopian65ab4712010-07-14 17:59:35 -070045namespace android {
46
Glenn Kasten8dad0e32012-01-09 08:41:22 -080047static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
48static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070049
50static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080051static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
Glenn Kasten1b8ae3d2013-07-19 10:32:41 -070053static const nsecs_t kAudioCommandTimeout = 3000000000LL; // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010054
Dima Zavinfce7a472011-04-19 22:30:36 -070055namespace {
56 extern struct audio_policy_service_ops aps_ops;
57};
58
Mathias Agopian65ab4712010-07-14 17:59:35 -070059// ----------------------------------------------------------------------------
60
61AudioPolicyService::AudioPolicyService()
Dima Zavinfce7a472011-04-19 22:30:36 -070062 : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL)
Mathias Agopian65ab4712010-07-14 17:59:35 -070063{
64 char value[PROPERTY_VALUE_MAX];
Dima Zavinfce7a472011-04-19 22:30:36 -070065 const struct hw_module_t *module;
66 int forced_val;
67 int rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -070068
Eric Laurent93575202011-01-18 18:39:02 -080069 Mutex::Autolock _l(mLock);
70
Mathias Agopian65ab4712010-07-14 17:59:35 -070071 // start tone playback thread
72 mTonePlaybackThread = new AudioCommandThread(String8(""));
73 // start audio commands thread
Glenn Kasten480b4682012-02-28 12:30:08 -080074 mAudioCommandThread = new AudioCommandThread(String8("ApmCommand"));
Mathias Agopian65ab4712010-07-14 17:59:35 -070075
Dima Zavinfce7a472011-04-19 22:30:36 -070076 /* instantiate the audio policy manager */
77 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
78 if (rc)
79 return;
Mathias Agopian65ab4712010-07-14 17:59:35 -070080
Dima Zavinfce7a472011-04-19 22:30:36 -070081 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
Steve Block29357bc2012-01-06 19:20:56 +000082 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070083 if (rc)
84 return;
Eric Laurent93575202011-01-18 18:39:02 -080085
Dima Zavinfce7a472011-04-19 22:30:36 -070086 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
87 &mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000088 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070089 if (rc)
90 return;
91
92 rc = mpAudioPolicy->init_check(mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000093 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
Dima Zavinfce7a472011-04-19 22:30:36 -070094 if (rc)
95 return;
96
Steve Blockdf64d152012-01-04 20:05:49 +000097 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurent7c7f10b2011-06-17 21:29:58 -070098
99 // load audio pre processing modules
100 if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) {
101 loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE);
102 } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) {
103 loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE);
104 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700105}
106
107AudioPolicyService::~AudioPolicyService()
108{
109 mTonePlaybackThread->exit();
110 mTonePlaybackThread.clear();
111 mAudioCommandThread->exit();
112 mAudioCommandThread.clear();
113
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700114
115 // release audio pre processing resources
116 for (size_t i = 0; i < mInputSources.size(); i++) {
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800117 delete mInputSources.valueAt(i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700118 }
119 mInputSources.clear();
120
121 for (size_t i = 0; i < mInputs.size(); i++) {
122 mInputs.valueAt(i)->mEffects.clear();
123 delete mInputs.valueAt(i);
124 }
125 mInputs.clear();
126
Glenn Kastena0d68332012-01-27 16:47:15 -0800127 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL)
Dima Zavinfce7a472011-04-19 22:30:36 -0700128 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kastena0d68332012-01-27 16:47:15 -0800129 if (mpAudioPolicyDev != NULL)
Dima Zavinfce7a472011-04-19 22:30:36 -0700130 audio_policy_dev_close(mpAudioPolicyDev);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700131}
132
Dima Zavinfce7a472011-04-19 22:30:36 -0700133status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
134 audio_policy_dev_state_t state,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700135 const char *device_address)
136{
Dima Zavinfce7a472011-04-19 22:30:36 -0700137 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700138 return NO_INIT;
139 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800140 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700141 return PERMISSION_DENIED;
142 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700143 if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700144 return BAD_VALUE;
145 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700146 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
147 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700148 return BAD_VALUE;
149 }
150
Glenn Kasten411e4472012-11-02 10:00:06 -0700151 ALOGV("setDeviceConnectionState()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700152 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700153 return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device,
154 state, device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700155}
156
Dima Zavinfce7a472011-04-19 22:30:36 -0700157audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
158 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -0700159 const char *device_address)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700160{
Dima Zavinfce7a472011-04-19 22:30:36 -0700161 if (mpAudioPolicy == NULL) {
162 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700163 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700164 return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device,
165 device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700166}
167
Glenn Kastenf78aee72012-01-04 11:00:47 -0800168status_t AudioPolicyService::setPhoneState(audio_mode_t state)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700169{
Dima Zavinfce7a472011-04-19 22:30:36 -0700170 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700171 return NO_INIT;
172 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800173 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700174 return PERMISSION_DENIED;
175 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800176 if (uint32_t(state) >= AUDIO_MODE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700177 return BAD_VALUE;
178 }
179
Glenn Kasten411e4472012-11-02 10:00:06 -0700180 ALOGV("setPhoneState()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700181
182 // TODO: check if it is more appropriate to do it in platform specific policy manager
183 AudioSystem::setMode(state);
184
185 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700186 mpAudioPolicy->set_phone_state(mpAudioPolicy, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700187 return NO_ERROR;
188}
189
Dima Zavinfce7a472011-04-19 22:30:36 -0700190status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
191 audio_policy_forced_cfg_t config)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700192{
Dima Zavinfce7a472011-04-19 22:30:36 -0700193 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700194 return NO_INIT;
195 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800196 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700197 return PERMISSION_DENIED;
198 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700199 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700200 return BAD_VALUE;
201 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700202 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700203 return BAD_VALUE;
204 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700205 ALOGV("setForceUse()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700206 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700207 mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700208 return NO_ERROR;
209}
210
Dima Zavinfce7a472011-04-19 22:30:36 -0700211audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700212{
Dima Zavinfce7a472011-04-19 22:30:36 -0700213 if (mpAudioPolicy == NULL) {
214 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700215 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700216 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
217 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700218 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700219 return mpAudioPolicy->get_force_use(mpAudioPolicy, usage);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700220}
221
Dima Zavinfce7a472011-04-19 22:30:36 -0700222audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700223 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800224 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700225 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000226 audio_output_flags_t flags,
227 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700228{
Dima Zavinfce7a472011-04-19 22:30:36 -0700229 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700230 return 0;
231 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700232 ALOGV("getOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700233 Mutex::Autolock _l(mLock);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000234 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate,
235 format, channelMask, flags, offloadInfo);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700236}
237
Eric Laurentde070132010-07-13 04:45:46 -0700238status_t AudioPolicyService::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700239 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700240 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700241{
Dima Zavinfce7a472011-04-19 22:30:36 -0700242 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700243 return NO_INIT;
244 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700245 ALOGV("startOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700246 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700247 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700248}
249
Eric Laurentde070132010-07-13 04:45:46 -0700250status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700251 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700252 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700253{
Dima Zavinfce7a472011-04-19 22:30:36 -0700254 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700255 return NO_INIT;
256 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700257 ALOGV("stopOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700258 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700259 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700260}
261
262void AudioPolicyService::releaseOutput(audio_io_handle_t output)
263{
Dima Zavinfce7a472011-04-19 22:30:36 -0700264 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700265 return;
266 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700267 ALOGV("releaseOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700268 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700269 mpAudioPolicy->release_output(mpAudioPolicy, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700270}
271
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800272audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700273 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800274 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700275 audio_channel_mask_t channelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700276 int audioSession)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700277{
Dima Zavinfce7a472011-04-19 22:30:36 -0700278 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700279 return 0;
280 }
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800281 // already checked by client, but double-check in case the client wrapper is bypassed
282 if (uint32_t(inputSource) >= AUDIO_SOURCE_CNT) {
283 return 0;
284 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700285 Mutex::Autolock _l(mLock);
Glenn Kasten20010052012-06-22 13:43:51 -0700286 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700287 audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
Glenn Kasten8af901c2012-11-01 11:11:38 -0700288 format, channelMask, (audio_in_acoustics_t) 0);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700289
290 if (input == 0) {
291 return input;
292 }
293 // create audio pre processors according to input source
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800294 ssize_t index = mInputSources.indexOfKey(inputSource);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700295 if (index < 0) {
296 return input;
297 }
298 ssize_t idx = mInputs.indexOfKey(input);
299 InputDesc *inputDesc;
300 if (idx < 0) {
Glenn Kasten81872a22012-03-07 16:49:22 -0800301 inputDesc = new InputDesc(audioSession);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700302 mInputs.add(input, inputDesc);
303 } else {
304 inputDesc = mInputs.valueAt(idx);
305 }
306
307 Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects;
308 for (size_t i = 0; i < effects.size(); i++) {
309 EffectDesc *effect = effects[i];
310 sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input);
311 status_t status = fx->initCheck();
312 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000313 ALOGW("Failed to create Fx %s on input %d", effect->mName, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700314 // fx goes out of scope and strong ref on AudioEffect is released
315 continue;
316 }
317 for (size_t j = 0; j < effect->mParams.size(); j++) {
318 fx->setParameter(effect->mParams[j]);
319 }
320 inputDesc->mEffects.add(fx);
321 }
322 setPreProcessorEnabled(inputDesc, true);
323 return input;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324}
325
326status_t AudioPolicyService::startInput(audio_io_handle_t input)
327{
Dima Zavinfce7a472011-04-19 22:30:36 -0700328 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700329 return NO_INIT;
330 }
331 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700332
Dima Zavinfce7a472011-04-19 22:30:36 -0700333 return mpAudioPolicy->start_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700334}
335
336status_t AudioPolicyService::stopInput(audio_io_handle_t input)
337{
Dima Zavinfce7a472011-04-19 22:30:36 -0700338 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700339 return NO_INIT;
340 }
341 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700342
Dima Zavinfce7a472011-04-19 22:30:36 -0700343 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700344}
345
346void AudioPolicyService::releaseInput(audio_io_handle_t input)
347{
Dima Zavinfce7a472011-04-19 22:30:36 -0700348 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349 return;
350 }
351 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700352 mpAudioPolicy->release_input(mpAudioPolicy, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700353
354 ssize_t index = mInputs.indexOfKey(input);
355 if (index < 0) {
356 return;
357 }
358 InputDesc *inputDesc = mInputs.valueAt(index);
359 setPreProcessorEnabled(inputDesc, false);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700360 delete inputDesc;
361 mInputs.removeItemsAt(index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362}
363
Dima Zavinfce7a472011-04-19 22:30:36 -0700364status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365 int indexMin,
366 int indexMax)
367{
Dima Zavinfce7a472011-04-19 22:30:36 -0700368 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700369 return NO_INIT;
370 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800371 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700372 return PERMISSION_DENIED;
373 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800374 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700375 return BAD_VALUE;
376 }
Eric Laurent5f121362012-06-15 14:45:03 -0700377 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700378 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700379 return NO_ERROR;
380}
381
Eric Laurent83844cc2011-11-18 16:43:31 -0800382status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
383 int index,
384 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385{
Dima Zavinfce7a472011-04-19 22:30:36 -0700386 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700387 return NO_INIT;
388 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800389 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700390 return PERMISSION_DENIED;
391 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800392 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393 return BAD_VALUE;
394 }
Eric Laurent5f121362012-06-15 14:45:03 -0700395 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800396 if (mpAudioPolicy->set_stream_volume_index_for_device) {
397 return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
398 stream,
399 index,
400 device);
401 } else {
402 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
403 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700404}
405
Eric Laurent83844cc2011-11-18 16:43:31 -0800406status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
407 int *index,
408 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409{
Dima Zavinfce7a472011-04-19 22:30:36 -0700410 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700411 return NO_INIT;
412 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800413 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700414 return BAD_VALUE;
415 }
Eric Laurent5f121362012-06-15 14:45:03 -0700416 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800417 if (mpAudioPolicy->get_stream_volume_index_for_device) {
418 return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
419 stream,
420 index,
421 device);
422 } else {
423 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
424 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700425}
426
Dima Zavinfce7a472011-04-19 22:30:36 -0700427uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700428{
Dima Zavinfce7a472011-04-19 22:30:36 -0700429 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700430 return 0;
431 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700432 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurentde070132010-07-13 04:45:46 -0700433}
434
Eric Laurent63742522012-03-08 13:42:42 -0800435//audio policy: use audio_device_t appropriately
436
437audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800438{
Dima Zavinfce7a472011-04-19 22:30:36 -0700439 if (mpAudioPolicy == NULL) {
Eric Laurent63742522012-03-08 13:42:42 -0800440 return (audio_devices_t)0;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800441 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700442 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800443}
444
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700445audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700446{
Dima Zavinfce7a472011-04-19 22:30:36 -0700447 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700448 return NO_INIT;
449 }
450 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700451 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700452}
453
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700454status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700455 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700456 uint32_t strategy,
457 int session,
458 int id)
459{
Dima Zavinfce7a472011-04-19 22:30:36 -0700460 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700461 return NO_INIT;
462 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700463 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700464}
465
466status_t AudioPolicyService::unregisterEffect(int id)
467{
Dima Zavinfce7a472011-04-19 22:30:36 -0700468 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700469 return NO_INIT;
470 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700471 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700472}
473
Eric Laurentdb7c0792011-08-10 10:37:50 -0700474status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
475{
476 if (mpAudioPolicy == NULL) {
477 return NO_INIT;
478 }
479 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
480}
481
Glenn Kastenfff6d712012-01-12 16:38:12 -0800482bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800483{
Dima Zavinfce7a472011-04-19 22:30:36 -0700484 if (mpAudioPolicy == NULL) {
Eric Laurenteda6c362011-02-02 09:33:30 -0800485 return 0;
486 }
487 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700488 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800489}
490
Jean-Michel Trivie336f912013-02-04 16:26:02 -0800491bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
492{
493 if (mpAudioPolicy == NULL) {
494 return 0;
495 }
496 Mutex::Autolock _l(mLock);
497 return mpAudioPolicy->is_stream_active_remotely(mpAudioPolicy, stream, inPastMs);
498}
499
Jean-Michel Trivie3f641f2012-10-10 12:11:16 -0700500bool AudioPolicyService::isSourceActive(audio_source_t source) const
501{
502 if (mpAudioPolicy == NULL) {
503 return false;
504 }
505 if (mpAudioPolicy->is_source_active == 0) {
506 return false;
507 }
508 Mutex::Autolock _l(mLock);
509 return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
510}
511
Eric Laurent57dae992011-07-24 13:36:09 -0700512status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
513 effect_descriptor_t *descriptors,
514 uint32_t *count)
515{
516
517 if (mpAudioPolicy == NULL) {
518 *count = 0;
519 return NO_INIT;
520 }
521 Mutex::Autolock _l(mLock);
522 status_t status = NO_ERROR;
523
524 size_t index;
525 for (index = 0; index < mInputs.size(); index++) {
526 if (mInputs.valueAt(index)->mSessionId == audioSession) {
527 break;
528 }
529 }
530 if (index == mInputs.size()) {
531 *count = 0;
532 return BAD_VALUE;
533 }
534 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
535
536 for (size_t i = 0; i < effects.size(); i++) {
537 effect_descriptor_t desc = effects[i]->descriptor();
538 if (i < *count) {
Glenn Kastena189a682012-02-20 12:16:30 -0800539 descriptors[i] = desc;
Eric Laurent57dae992011-07-24 13:36:09 -0700540 }
541 }
542 if (effects.size() > *count) {
543 status = NO_MEMORY;
544 }
545 *count = effects.size();
546 return status;
547}
548
Mathias Agopian65ab4712010-07-14 17:59:35 -0700549void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700550 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700551 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700552}
553
554static bool tryLock(Mutex& mutex)
555{
556 bool locked = false;
557 for (int i = 0; i < kDumpLockRetries; ++i) {
558 if (mutex.tryLock() == NO_ERROR) {
559 locked = true;
560 break;
561 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800562 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700563 }
564 return locked;
565}
566
567status_t AudioPolicyService::dumpInternals(int fd)
568{
569 const size_t SIZE = 256;
570 char buffer[SIZE];
571 String8 result;
572
Dima Zavinfce7a472011-04-19 22:30:36 -0700573 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700574 result.append(buffer);
575 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
576 result.append(buffer);
577 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
578 result.append(buffer);
579
580 write(fd, result.string(), result.size());
581 return NO_ERROR;
582}
583
584status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
585{
Glenn Kasten44deb052012-02-05 18:09:08 -0800586 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587 dumpPermissionDenial(fd);
588 } else {
589 bool locked = tryLock(mLock);
590 if (!locked) {
591 String8 result(kDeadlockedString);
592 write(fd, result.string(), result.size());
593 }
594
595 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800596 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700597 mAudioCommandThread->dump(fd);
598 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800599 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700600 mTonePlaybackThread->dump(fd);
601 }
602
Dima Zavinfce7a472011-04-19 22:30:36 -0700603 if (mpAudioPolicy) {
604 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700605 }
606
607 if (locked) mLock.unlock();
608 }
609 return NO_ERROR;
610}
611
612status_t AudioPolicyService::dumpPermissionDenial(int fd)
613{
614 const size_t SIZE = 256;
615 char buffer[SIZE];
616 String8 result;
617 snprintf(buffer, SIZE, "Permission Denial: "
618 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
619 IPCThreadState::self()->getCallingPid(),
620 IPCThreadState::self()->getCallingUid());
621 result.append(buffer);
622 write(fd, result.string(), result.size());
623 return NO_ERROR;
624}
625
Glenn Kasten81872a22012-03-07 16:49:22 -0800626void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700627{
Glenn Kasten81872a22012-03-07 16:49:22 -0800628 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700629 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800630 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700631 }
632}
633
Mathias Agopian65ab4712010-07-14 17:59:35 -0700634status_t AudioPolicyService::onTransact(
635 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
636{
637 return BnAudioPolicyService::onTransact(code, data, reply, flags);
638}
639
640
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641// ----------- AudioPolicyService::AudioCommandThread implementation ----------
642
643AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name)
644 : Thread(false), mName(name)
645{
646 mpToneGenerator = NULL;
647}
648
649
650AudioPolicyService::AudioCommandThread::~AudioCommandThread()
651{
652 if (mName != "" && !mAudioCommands.isEmpty()) {
653 release_wake_lock(mName.string());
654 }
655 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800656 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700657}
658
659void AudioPolicyService::AudioCommandThread::onFirstRef()
660{
661 if (mName != "") {
662 run(mName.string(), ANDROID_PRIORITY_AUDIO);
663 } else {
Glenn Kasten480b4682012-02-28 12:30:08 -0800664 run("AudioCommand", ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700665 }
666}
667
668bool AudioPolicyService::AudioCommandThread::threadLoop()
669{
670 nsecs_t waitTime = INT64_MAX;
671
672 mLock.lock();
673 while (!exitPending())
674 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700675 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700676 nsecs_t curTime = systemTime();
677 // commands are sorted by increasing time stamp: execute them from index 0 and up
678 if (mAudioCommands[0]->mTime <= curTime) {
679 AudioCommand *command = mAudioCommands[0];
680 mAudioCommands.removeAt(0);
681 mLastCommand = *command;
682
683 switch (command->mCommand) {
684 case START_TONE: {
685 mLock.unlock();
686 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100687 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700688 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800689 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
691 mpToneGenerator->startTone(data->mType);
692 delete data;
693 mLock.lock();
694 }break;
695 case STOP_TONE: {
696 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100697 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700698 if (mpToneGenerator != NULL) {
699 mpToneGenerator->stopTone();
700 delete mpToneGenerator;
701 mpToneGenerator = NULL;
702 }
703 mLock.lock();
704 }break;
705 case SET_VOLUME: {
706 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100707 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700708 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
709 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
710 data->mVolume,
711 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712 if (command->mWaitStatus) {
713 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100714 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715 }
716 delete data;
717 }break;
718 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700719 ParametersData *data = (ParametersData *)command->mParam;
720 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
721 data->mKeyValuePairs.string(), data->mIO);
722 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
723 if (command->mWaitStatus) {
724 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100725 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700726 }
727 delete data;
728 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700729 case SET_VOICE_VOLUME: {
730 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100731 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700732 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700733 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
734 if (command->mWaitStatus) {
735 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100736 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700737 }
738 delete data;
739 }break;
740 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000741 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700742 }
743 delete command;
744 waitTime = INT64_MAX;
745 } else {
746 waitTime = mAudioCommands[0]->mTime - curTime;
747 break;
748 }
749 }
750 // release delayed commands wake lock
751 if (mName != "" && mAudioCommands.isEmpty()) {
752 release_wake_lock(mName.string());
753 }
Steve Block3856b092011-10-20 11:56:00 +0100754 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700755 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100756 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700757 }
758 mLock.unlock();
759 return false;
760}
761
762status_t AudioPolicyService::AudioCommandThread::dump(int fd)
763{
764 const size_t SIZE = 256;
765 char buffer[SIZE];
766 String8 result;
767
768 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
769 result.append(buffer);
770 write(fd, result.string(), result.size());
771
772 bool locked = tryLock(mLock);
773 if (!locked) {
774 String8 result2(kCmdDeadlockedString);
775 write(fd, result2.string(), result2.size());
776 }
777
778 snprintf(buffer, SIZE, "- Commands:\n");
779 result = String8(buffer);
780 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800781 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700782 mAudioCommands[i]->dump(buffer, SIZE);
783 result.append(buffer);
784 }
785 result.append(" Last Command\n");
786 mLastCommand.dump(buffer, SIZE);
787 result.append(buffer);
788
789 write(fd, result.string(), result.size());
790
791 if (locked) mLock.unlock();
792
793 return NO_ERROR;
794}
795
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800796void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
797 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700798{
799 AudioCommand *command = new AudioCommand();
800 command->mCommand = START_TONE;
801 ToneData *data = new ToneData();
802 data->mType = type;
803 data->mStream = stream;
804 command->mParam = (void *)data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700805 Mutex::Autolock _l(mLock);
806 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100807 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700808 mWaitWorkCV.signal();
809}
810
811void AudioPolicyService::AudioCommandThread::stopToneCommand()
812{
813 AudioCommand *command = new AudioCommand();
814 command->mCommand = STOP_TONE;
815 command->mParam = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700816 Mutex::Autolock _l(mLock);
817 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100818 ALOGV("AudioCommandThread() adding tone stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819 mWaitWorkCV.signal();
820}
821
Glenn Kastenfff6d712012-01-12 16:38:12 -0800822status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700823 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800824 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700825 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700826{
827 status_t status = NO_ERROR;
828
829 AudioCommand *command = new AudioCommand();
830 command->mCommand = SET_VOLUME;
831 VolumeData *data = new VolumeData();
832 data->mStream = stream;
833 data->mVolume = volume;
834 data->mIO = output;
835 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700836 Mutex::Autolock _l(mLock);
837 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100838 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700839 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700840 mWaitWorkCV.signal();
841 if (command->mWaitStatus) {
842 command->mCond.wait(mLock);
843 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100844 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700845 }
846 return status;
847}
848
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800849status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700850 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700851 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700852{
853 status_t status = NO_ERROR;
854
855 AudioCommand *command = new AudioCommand();
856 command->mCommand = SET_PARAMETERS;
857 ParametersData *data = new ParametersData();
858 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700859 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700860 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700861 Mutex::Autolock _l(mLock);
862 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100863 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700864 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700865 mWaitWorkCV.signal();
866 if (command->mWaitStatus) {
867 command->mCond.wait(mLock);
868 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100869 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700870 }
871 return status;
872}
873
874status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
875{
876 status_t status = NO_ERROR;
877
878 AudioCommand *command = new AudioCommand();
879 command->mCommand = SET_VOICE_VOLUME;
880 VoiceVolumeData *data = new VoiceVolumeData();
881 data->mVolume = volume;
882 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700883 Mutex::Autolock _l(mLock);
884 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100885 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
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
895// insertCommand_l() must be called with mLock held
896void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
897{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800898 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700899 Vector <AudioCommand *> removedCommands;
900
Eric Laurentcec4abb2012-07-03 12:23:02 -0700901 nsecs_t time = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700902 command->mTime = systemTime() + milliseconds(delayMs);
903
904 // acquire wake lock to make sure delayed commands are processed
905 if (mName != "" && mAudioCommands.isEmpty()) {
906 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
907 }
908
909 // check same pending commands with later time stamps and eliminate them
910 for (i = mAudioCommands.size()-1; i >= 0; i--) {
911 AudioCommand *command2 = mAudioCommands[i];
912 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
913 if (command2->mTime <= command->mTime) break;
914 if (command2->mCommand != command->mCommand) continue;
915
916 switch (command->mCommand) {
917 case SET_PARAMETERS: {
918 ParametersData *data = (ParametersData *)command->mParam;
919 ParametersData *data2 = (ParametersData *)command2->mParam;
920 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +0100921 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -0700922 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700923 AudioParameter param = AudioParameter(data->mKeyValuePairs);
924 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
925 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700926 String8 key;
927 String8 value;
928 param.getAt(j, key, value);
929 for (size_t k = 0; k < param2.size(); k++) {
930 String8 key2;
931 String8 value2;
932 param2.getAt(k, key2, value2);
933 if (key2 == key) {
934 param2.remove(key2);
935 ALOGV("Filtering out parameter %s", key2.string());
936 break;
937 }
938 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700939 }
940 // if all keys have been filtered out, remove the command.
941 // otherwise, update the key value pairs
942 if (param2.size() == 0) {
943 removedCommands.add(command2);
944 } else {
945 data2->mKeyValuePairs = param2.toString();
946 }
Eric Laurentcec4abb2012-07-03 12:23:02 -0700947 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700948 } break;
949
950 case SET_VOLUME: {
951 VolumeData *data = (VolumeData *)command->mParam;
952 VolumeData *data2 = (VolumeData *)command2->mParam;
953 if (data->mIO != data2->mIO) break;
954 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +0100955 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -0700956 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700957 removedCommands.add(command2);
Eric Laurentcec4abb2012-07-03 12:23:02 -0700958 time = command2->mTime;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700959 } break;
960 case START_TONE:
961 case STOP_TONE:
962 default:
963 break;
964 }
965 }
966
967 // remove filtered commands
968 for (size_t j = 0; j < removedCommands.size(); j++) {
969 // removed commands always have time stamps greater than current command
970 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
971 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +0100972 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700973 mAudioCommands.removeAt(k);
974 break;
975 }
976 }
977 }
978 removedCommands.clear();
979
Eric Laurentcec4abb2012-07-03 12:23:02 -0700980 // wait for status only if delay is 0 and command time was not modified above
981 if (delayMs == 0 && time == 0) {
982 command->mWaitStatus = true;
983 } else {
984 command->mWaitStatus = false;
985 }
986 // update command time if modified above
987 if (time != 0) {
988 command->mTime = time;
989 }
990
Mathias Agopian65ab4712010-07-14 17:59:35 -0700991 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +0100992 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -0700993 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700994 mAudioCommands.insertAt(command, i + 1);
995}
996
997void AudioPolicyService::AudioCommandThread::exit()
998{
Steve Block3856b092011-10-20 11:56:00 +0100999 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001000 {
1001 AutoMutex _l(mLock);
1002 requestExit();
1003 mWaitWorkCV.signal();
1004 }
1005 requestExitAndWait();
1006}
1007
1008void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1009{
1010 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1011 mCommand,
1012 (int)ns2s(mTime),
1013 (int)ns2ms(mTime)%1000,
1014 mWaitStatus,
1015 mParam);
1016}
1017
Dima Zavinfce7a472011-04-19 22:30:36 -07001018/******* helpers for the service_ops callbacks defined below *********/
1019void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1020 const char *keyValuePairs,
1021 int delayMs)
1022{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001023 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001024 delayMs);
1025}
1026
1027int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1028 float volume,
1029 audio_io_handle_t output,
1030 int delayMs)
1031{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001032 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001033 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001034}
1035
1036int AudioPolicyService::startTone(audio_policy_tone_t tone,
1037 audio_stream_type_t stream)
1038{
1039 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
Steve Block29357bc2012-01-06 19:20:56 +00001040 ALOGE("startTone: illegal tone requested (%d)", tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001041 if (stream != AUDIO_STREAM_VOICE_CALL)
Steve Block29357bc2012-01-06 19:20:56 +00001042 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001043 tone);
Dima Zavinfce7a472011-04-19 22:30:36 -07001044 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1045 AUDIO_STREAM_VOICE_CALL);
1046 return 0;
1047}
1048
1049int AudioPolicyService::stopTone()
1050{
1051 mTonePlaybackThread->stopToneCommand();
1052 return 0;
1053}
1054
1055int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1056{
1057 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1058}
1059
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001060bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
1061{
1062 return false; // stub function
1063}
1064
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001065// ----------------------------------------------------------------------------
1066// Audio pre-processing configuration
1067// ----------------------------------------------------------------------------
1068
Glenn Kasten8dad0e32012-01-09 08:41:22 -08001069/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001070 MIC_SRC_TAG,
1071 VOICE_UL_SRC_TAG,
1072 VOICE_DL_SRC_TAG,
1073 VOICE_CALL_SRC_TAG,
1074 CAMCORDER_SRC_TAG,
1075 VOICE_REC_SRC_TAG,
1076 VOICE_COMM_SRC_TAG
1077};
1078
1079// returns the audio_source_t enum corresponding to the input source name or
1080// AUDIO_SOURCE_CNT is no match found
1081audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
1082{
1083 int i;
1084 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
1085 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001086 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001087 break;
1088 }
1089 }
1090 return (audio_source_t)i;
1091}
1092
1093size_t AudioPolicyService::growParamSize(char *param,
1094 size_t size,
1095 size_t *curSize,
1096 size_t *totSize)
1097{
1098 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
1099 size_t pos = ((*curSize - 1 ) / size + 1) * size;
1100
1101 if (pos + size > *totSize) {
1102 while (pos + size > *totSize) {
1103 *totSize += ((*totSize + 7) / 8) * 4;
1104 }
1105 param = (char *)realloc(param, *totSize);
1106 }
1107 *curSize = pos + size;
1108 return pos;
1109}
1110
1111size_t AudioPolicyService::readParamValue(cnode *node,
1112 char *param,
1113 size_t *curSize,
1114 size_t *totSize)
1115{
1116 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
1117 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
1118 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001119 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001120 return sizeof(short);
1121 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
1122 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
1123 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001124 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001125 return sizeof(int);
1126 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
1127 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
1128 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001129 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001130 return sizeof(float);
1131 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
1132 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
1133 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1134 *(bool *)((char *)param + pos) = false;
1135 } else {
1136 *(bool *)((char *)param + pos) = true;
1137 }
Steve Block3856b092011-10-20 11:56:00 +01001138 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001139 return sizeof(bool);
1140 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1141 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1142 if (*curSize + len + 1 > *totSize) {
1143 *totSize = *curSize + len + 1;
1144 param = (char *)realloc(param, *totSize);
1145 }
1146 strncpy(param + *curSize, node->value, len);
1147 *curSize += len;
1148 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +01001149 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001150 return len;
1151 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001152 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001153 return 0;
1154}
1155
1156effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1157{
1158 cnode *param;
1159 cnode *value;
1160 size_t curSize = sizeof(effect_param_t);
1161 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1162 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1163
1164 param = config_find(root, PARAM_TAG);
1165 value = config_find(root, VALUE_TAG);
1166 if (param == NULL && value == NULL) {
1167 // try to parse simple parameter form {int int}
1168 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -08001169 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001170 // Note: that a pair of random strings is read as 0 0
1171 int *ptr = (int *)fx_param->data;
1172 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +00001173 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001174 *ptr++ = atoi(param->name);
1175 *ptr = atoi(param->value);
1176 fx_param->psize = sizeof(int);
1177 fx_param->vsize = sizeof(int);
1178 return fx_param;
1179 }
1180 }
1181 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001182 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001183 goto error;
1184 }
1185
1186 fx_param->psize = 0;
1187 param = param->first_child;
1188 while (param) {
Steve Block3856b092011-10-20 11:56:00 +01001189 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001190 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1191 if (size == 0) {
1192 goto error;
1193 }
1194 fx_param->psize += size;
1195 param = param->next;
1196 }
1197
1198 // align start of value field on 32 bit boundary
1199 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1200
1201 fx_param->vsize = 0;
1202 value = value->first_child;
1203 while (value) {
Steve Block3856b092011-10-20 11:56:00 +01001204 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001205 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1206 if (size == 0) {
1207 goto error;
1208 }
1209 fx_param->vsize += size;
1210 value = value->next;
1211 }
1212
1213 return fx_param;
1214
1215error:
1216 delete fx_param;
1217 return NULL;
1218}
1219
1220void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1221{
1222 cnode *node = root->first_child;
1223 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001224 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001225 effect_param_t *param = loadEffectParameter(node);
1226 if (param == NULL) {
1227 node = node->next;
1228 continue;
1229 }
1230 params.add(param);
1231 node = node->next;
1232 }
1233}
1234
1235AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1236 cnode *root,
1237 const Vector <EffectDesc *>& effects)
1238{
1239 cnode *node = root->first_child;
1240 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001241 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001242 return NULL;
1243 }
1244 InputSourceDesc *source = new InputSourceDesc();
1245 while (node) {
1246 size_t i;
1247 for (i = 0; i < effects.size(); i++) {
1248 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001249 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001250 break;
1251 }
1252 }
1253 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +01001254 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001255 node = node->next;
1256 continue;
1257 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001258 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001259 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +01001260 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001261 source->mEffects.add(effect);
1262 node = node->next;
1263 }
1264 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001265 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001266 delete source;
1267 return NULL;
1268 }
1269 return source;
1270}
1271
1272status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1273{
1274 cnode *node = config_find(root, PREPROCESSING_TAG);
1275 if (node == NULL) {
1276 return -ENOENT;
1277 }
1278 node = node->first_child;
1279 while (node) {
1280 audio_source_t source = inputSourceNameToEnum(node->name);
1281 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001282 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001283 node = node->next;
1284 continue;
1285 }
Steve Block3856b092011-10-20 11:56:00 +01001286 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001287 InputSourceDesc *desc = loadInputSource(node, effects);
1288 if (desc == NULL) {
1289 node = node->next;
1290 continue;
1291 }
1292 mInputSources.add(source, desc);
1293 node = node->next;
1294 }
1295 return NO_ERROR;
1296}
1297
1298AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1299{
1300 cnode *node = config_find(root, UUID_TAG);
1301 if (node == NULL) {
1302 return NULL;
1303 }
1304 effect_uuid_t uuid;
1305 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001306 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001307 return NULL;
1308 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001309 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001310}
1311
1312status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1313{
1314 cnode *node = config_find(root, EFFECTS_TAG);
1315 if (node == NULL) {
1316 return -ENOENT;
1317 }
1318 node = node->first_child;
1319 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001320 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001321 EffectDesc *effect = loadEffect(node);
1322 if (effect == NULL) {
1323 node = node->next;
1324 continue;
1325 }
1326 effects.add(effect);
1327 node = node->next;
1328 }
1329 return NO_ERROR;
1330}
1331
1332status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1333{
1334 cnode *root;
1335 char *data;
1336
1337 data = (char *)load_file(path, NULL);
1338 if (data == NULL) {
1339 return -ENODEV;
1340 }
1341 root = config_node("", "");
1342 config_load(root, data);
1343
1344 Vector <EffectDesc *> effects;
1345 loadEffects(root, effects);
1346 loadInputSources(root, effects);
1347
1348 config_free(root);
1349 free(root);
1350 free(data);
1351
1352 return NO_ERROR;
1353}
1354
Dima Zavinfce7a472011-04-19 22:30:36 -07001355/* implementation of the interface to the policy manager */
1356extern "C" {
1357
Eric Laurenta4c5a552012-03-29 10:12:40 -07001358
1359static audio_module_handle_t aps_load_hw_module(void *service,
1360 const char *name)
Dima Zavinfce7a472011-04-19 22:30:36 -07001361{
1362 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001363 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001364 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001365 return 0;
1366 }
1367
Eric Laurenta4c5a552012-03-29 10:12:40 -07001368 return af->loadHwModule(name);
1369}
1370
1371// deprecated: replaced by aps_open_output_on_module()
1372static audio_io_handle_t aps_open_output(void *service,
1373 audio_devices_t *pDevices,
1374 uint32_t *pSamplingRate,
1375 audio_format_t *pFormat,
1376 audio_channel_mask_t *pChannelMask,
1377 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001378 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001379{
1380 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1381 if (af == 0) {
1382 ALOGW("%s: could not get AudioFlinger", __func__);
1383 return 0;
1384 }
1385
1386 return af->openOutput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask,
1387 pLatencyMs, flags);
1388}
1389
1390static audio_io_handle_t aps_open_output_on_module(void *service,
1391 audio_module_handle_t module,
1392 audio_devices_t *pDevices,
1393 uint32_t *pSamplingRate,
1394 audio_format_t *pFormat,
1395 audio_channel_mask_t *pChannelMask,
1396 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001397 audio_output_flags_t flags,
1398 const audio_offload_info_t *offloadInfo)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001399{
1400 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1401 if (af == 0) {
1402 ALOGW("%s: could not get AudioFlinger", __func__);
1403 return 0;
1404 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001405 return af->openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
Dima Zavinfce7a472011-04-19 22:30:36 -07001406 pLatencyMs, flags);
1407}
1408
1409static audio_io_handle_t aps_open_dup_output(void *service,
1410 audio_io_handle_t output1,
1411 audio_io_handle_t output2)
1412{
1413 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001414 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001415 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001416 return 0;
1417 }
1418 return af->openDuplicateOutput(output1, output2);
1419}
1420
1421static int aps_close_output(void *service, audio_io_handle_t output)
1422{
1423 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001424 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001425 return PERMISSION_DENIED;
1426
1427 return af->closeOutput(output);
1428}
1429
1430static int aps_suspend_output(void *service, audio_io_handle_t output)
1431{
1432 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001433 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001434 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001435 return PERMISSION_DENIED;
1436 }
1437
1438 return af->suspendOutput(output);
1439}
1440
1441static int aps_restore_output(void *service, audio_io_handle_t output)
1442{
1443 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001444 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001445 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001446 return PERMISSION_DENIED;
1447 }
1448
1449 return af->restoreOutput(output);
1450}
1451
Glenn Kasten20010052012-06-22 13:43:51 -07001452// deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
Dima Zavinfce7a472011-04-19 22:30:36 -07001453static audio_io_handle_t aps_open_input(void *service,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001454 audio_devices_t *pDevices,
1455 uint32_t *pSamplingRate,
1456 audio_format_t *pFormat,
1457 audio_channel_mask_t *pChannelMask,
1458 audio_in_acoustics_t acoustics)
Dima Zavinfce7a472011-04-19 22:30:36 -07001459{
1460 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001461 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001462 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001463 return 0;
1464 }
1465
Eric Laurenta4c5a552012-03-29 10:12:40 -07001466 return af->openInput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask);
1467}
1468
1469static audio_io_handle_t aps_open_input_on_module(void *service,
1470 audio_module_handle_t module,
1471 audio_devices_t *pDevices,
1472 uint32_t *pSamplingRate,
1473 audio_format_t *pFormat,
1474 audio_channel_mask_t *pChannelMask)
1475{
1476 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1477 if (af == 0) {
1478 ALOGW("%s: could not get AudioFlinger", __func__);
1479 return 0;
1480 }
1481
1482 return af->openInput(module, pDevices, pSamplingRate, pFormat, pChannelMask);
Dima Zavinfce7a472011-04-19 22:30:36 -07001483}
1484
1485static int aps_close_input(void *service, audio_io_handle_t input)
1486{
1487 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001488 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001489 return PERMISSION_DENIED;
1490
1491 return af->closeInput(input);
1492}
1493
1494static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1495 audio_io_handle_t output)
1496{
1497 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001498 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001499 return PERMISSION_DENIED;
1500
1501 return af->setStreamOutput(stream, output);
1502}
1503
1504static int aps_move_effects(void *service, int session,
1505 audio_io_handle_t src_output,
1506 audio_io_handle_t dst_output)
1507{
1508 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001509 if (af == 0)
Dima Zavinfce7a472011-04-19 22:30:36 -07001510 return PERMISSION_DENIED;
1511
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001512 return af->moveEffects(session, src_output, dst_output);
Dima Zavinfce7a472011-04-19 22:30:36 -07001513}
1514
1515static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1516 const char *keys)
1517{
1518 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1519 return strdup(result.string());
1520}
1521
1522static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1523 const char *kv_pairs, int delay_ms)
1524{
1525 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1526
1527 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1528}
1529
1530static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1531 float volume, audio_io_handle_t output,
1532 int delay_ms)
1533{
1534 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1535
1536 return audioPolicyService->setStreamVolume(stream, volume, output,
1537 delay_ms);
1538}
1539
1540static int aps_start_tone(void *service, audio_policy_tone_t tone,
1541 audio_stream_type_t stream)
1542{
1543 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1544
1545 return audioPolicyService->startTone(tone, stream);
1546}
1547
1548static int aps_stop_tone(void *service)
1549{
1550 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1551
1552 return audioPolicyService->stopTone();
1553}
1554
1555static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1556{
1557 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1558
1559 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1560}
1561
1562}; // extern "C"
1563
1564namespace {
1565 struct audio_policy_service_ops aps_ops = {
1566 open_output : aps_open_output,
1567 open_duplicate_output : aps_open_dup_output,
1568 close_output : aps_close_output,
1569 suspend_output : aps_suspend_output,
1570 restore_output : aps_restore_output,
1571 open_input : aps_open_input,
1572 close_input : aps_close_input,
1573 set_stream_volume : aps_set_stream_volume,
1574 set_stream_output : aps_set_stream_output,
1575 set_parameters : aps_set_parameters,
1576 get_parameters : aps_get_parameters,
1577 start_tone : aps_start_tone,
1578 stop_tone : aps_stop_tone,
1579 set_voice_volume : aps_set_voice_volume,
1580 move_effects : aps_move_effects,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001581 load_hw_module : aps_load_hw_module,
1582 open_output_on_module : aps_open_output_on_module,
1583 open_input_on_module : aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001584 };
1585}; // namespace <unnamed>
1586
Mathias Agopian65ab4712010-07-14 17:59:35 -07001587}; // namespace android