blob: c5ad2c00713732e3c505d34894f60e2a8fe9f1d5 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioPolicyService"
18//#define LOG_NDEBUG 0
19
Glenn Kasten153b9fe2013-07-15 11:23:36 -070020#include "Configuration.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070021#undef __STRICT_ANSI__
22#define __STDINT_LIMITS
23#define __STDC_LIMIT_MACROS
24#include <stdint.h>
25
26#include <sys/time.h>
27#include <binder/IServiceManager.h>
28#include <utils/Log.h>
29#include <cutils/properties.h>
30#include <binder/IPCThreadState.h>
31#include <utils/String16.h>
32#include <utils/threads.h>
33#include "AudioPolicyService.h"
Glenn Kasten44deb052012-02-05 18:09:08 -080034#include "ServiceUtilities.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <hardware_legacy/power.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070036#include <media/AudioEffect.h>
37#include <media/EffectsFactoryApi.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070038
Dima Zavinfce7a472011-04-19 22:30:36 -070039#include <hardware/hardware.h>
Dima Zavin64760242011-05-11 14:15:23 -070040#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070041#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070042#include <hardware/audio_policy.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070043#include <audio_effects/audio_effects_conf.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070044#include <media/AudioParameter.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070045
Mathias Agopian65ab4712010-07-14 17:59:35 -070046namespace android {
47
Glenn Kasten8dad0e32012-01-09 08:41:22 -080048static const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
49static const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
51static const int kDumpLockRetries = 50;
Glenn Kasten22ecc912012-01-09 08:33:38 -080052static const int kDumpLockSleepUs = 20000;
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
Glenn Kasten1b8ae3d2013-07-19 10:32:41 -070054static const nsecs_t kAudioCommandTimeout = 3000000000LL; // 3 seconds
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +010055
Dima Zavinfce7a472011-04-19 22:30:36 -070056namespace {
57 extern struct audio_policy_service_ops aps_ops;
58};
59
Mathias Agopian65ab4712010-07-14 17:59:35 -070060// ----------------------------------------------------------------------------
61
62AudioPolicyService::AudioPolicyService()
Dima Zavinfce7a472011-04-19 22:30:36 -070063 : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL)
Mathias Agopian65ab4712010-07-14 17:59:35 -070064{
65 char value[PROPERTY_VALUE_MAX];
Dima Zavinfce7a472011-04-19 22:30:36 -070066 const struct hw_module_t *module;
67 int forced_val;
68 int rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -070069
Eric Laurent93575202011-01-18 18:39:02 -080070 Mutex::Autolock _l(mLock);
71
Mathias Agopian65ab4712010-07-14 17:59:35 -070072 // start tone playback thread
Eric Laurentbfb1b832013-01-07 09:53:42 -080073 mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
Mathias Agopian65ab4712010-07-14 17:59:35 -070074 // start audio commands thread
Eric Laurentbfb1b832013-01-07 09:53:42 -080075 mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
76 // start output activity command thread
77 mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
Dima Zavinfce7a472011-04-19 22:30:36 -070078 /* instantiate the audio policy manager */
79 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070080 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070081 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070082 }
Mathias Agopian65ab4712010-07-14 17:59:35 -070083
Dima Zavinfce7a472011-04-19 22:30:36 -070084 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
Steve Block29357bc2012-01-06 19:20:56 +000085 ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070086 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070087 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070088 }
Eric Laurent93575202011-01-18 18:39:02 -080089
Dima Zavinfce7a472011-04-19 22:30:36 -070090 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
91 &mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000092 ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070093 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -070094 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070095 }
Dima Zavinfce7a472011-04-19 22:30:36 -070096
97 rc = mpAudioPolicy->init_check(mpAudioPolicy);
Steve Block29357bc2012-01-06 19:20:56 +000098 ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070099 if (rc) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700100 return;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700101 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700102
Steve Blockdf64d152012-01-04 20:05:49 +0000103 ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700104
105 // load audio pre processing modules
106 if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) {
107 loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE);
108 } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) {
109 loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE);
110 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700111}
112
113AudioPolicyService::~AudioPolicyService()
114{
115 mTonePlaybackThread->exit();
116 mTonePlaybackThread.clear();
117 mAudioCommandThread->exit();
118 mAudioCommandThread.clear();
119
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700120
121 // release audio pre processing resources
122 for (size_t i = 0; i < mInputSources.size(); i++) {
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800123 delete mInputSources.valueAt(i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700124 }
125 mInputSources.clear();
126
127 for (size_t i = 0; i < mInputs.size(); i++) {
128 mInputs.valueAt(i)->mEffects.clear();
129 delete mInputs.valueAt(i);
130 }
131 mInputs.clear();
132
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700133 if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700134 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700135 }
136 if (mpAudioPolicyDev != NULL) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700137 audio_policy_dev_close(mpAudioPolicyDev);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700138 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700139}
140
Dima Zavinfce7a472011-04-19 22:30:36 -0700141status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
142 audio_policy_dev_state_t state,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700143 const char *device_address)
144{
Dima Zavinfce7a472011-04-19 22:30:36 -0700145 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700146 return NO_INIT;
147 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800148 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700149 return PERMISSION_DENIED;
150 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700151 if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700152 return BAD_VALUE;
153 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700154 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
155 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700156 return BAD_VALUE;
157 }
158
Glenn Kasten411e4472012-11-02 10:00:06 -0700159 ALOGV("setDeviceConnectionState()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700160 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700161 return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device,
162 state, device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700163}
164
Dima Zavinfce7a472011-04-19 22:30:36 -0700165audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
166 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -0700167 const char *device_address)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700168{
Dima Zavinfce7a472011-04-19 22:30:36 -0700169 if (mpAudioPolicy == NULL) {
170 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700171 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700172 return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device,
173 device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700174}
175
Glenn Kastenf78aee72012-01-04 11:00:47 -0800176status_t AudioPolicyService::setPhoneState(audio_mode_t state)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700177{
Dima Zavinfce7a472011-04-19 22:30:36 -0700178 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700179 return NO_INIT;
180 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800181 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700182 return PERMISSION_DENIED;
183 }
Glenn Kasten930f4ca2012-01-06 16:47:31 -0800184 if (uint32_t(state) >= AUDIO_MODE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700185 return BAD_VALUE;
186 }
187
Glenn Kasten411e4472012-11-02 10:00:06 -0700188 ALOGV("setPhoneState()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700189
190 // TODO: check if it is more appropriate to do it in platform specific policy manager
191 AudioSystem::setMode(state);
192
193 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700194 mpAudioPolicy->set_phone_state(mpAudioPolicy, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700195 return NO_ERROR;
196}
197
Dima Zavinfce7a472011-04-19 22:30:36 -0700198status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
199 audio_policy_forced_cfg_t config)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700200{
Dima Zavinfce7a472011-04-19 22:30:36 -0700201 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700202 return NO_INIT;
203 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800204 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700205 return PERMISSION_DENIED;
206 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700207 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700208 return BAD_VALUE;
209 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700210 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700211 return BAD_VALUE;
212 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700213 ALOGV("setForceUse()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700214 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700215 mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700216 return NO_ERROR;
217}
218
Dima Zavinfce7a472011-04-19 22:30:36 -0700219audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700220{
Dima Zavinfce7a472011-04-19 22:30:36 -0700221 if (mpAudioPolicy == NULL) {
222 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700223 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700224 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
225 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700226 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700227 return mpAudioPolicy->get_force_use(mpAudioPolicy, usage);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700228}
229
Dima Zavinfce7a472011-04-19 22:30:36 -0700230audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700231 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800232 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700233 audio_channel_mask_t channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000234 audio_output_flags_t flags,
235 const audio_offload_info_t *offloadInfo)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700236{
Dima Zavinfce7a472011-04-19 22:30:36 -0700237 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700238 return 0;
239 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700240 ALOGV("getOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700241 Mutex::Autolock _l(mLock);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000242 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate,
243 format, channelMask, flags, offloadInfo);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244}
245
Eric Laurentde070132010-07-13 04:45:46 -0700246status_t AudioPolicyService::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700247 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700248 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700249{
Dima Zavinfce7a472011-04-19 22:30:36 -0700250 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700251 return NO_INIT;
252 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700253 ALOGV("startOutput()");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700254 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700255 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700256}
257
Eric Laurentde070132010-07-13 04:45:46 -0700258status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700259 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700260 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700261{
Dima Zavinfce7a472011-04-19 22:30:36 -0700262 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700263 return NO_INIT;
264 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700265 ALOGV("stopOutput()");
Eric Laurentbfb1b832013-01-07 09:53:42 -0800266 mOutputCommandThread->stopOutputCommand(output, stream, session);
267 return NO_ERROR;
268}
269
270status_t AudioPolicyService::doStopOutput(audio_io_handle_t output,
271 audio_stream_type_t stream,
272 int session)
273{
274 ALOGV("doStopOutput from tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700275 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700276 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700277}
278
279void AudioPolicyService::releaseOutput(audio_io_handle_t output)
280{
Dima Zavinfce7a472011-04-19 22:30:36 -0700281 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700282 return;
283 }
Glenn Kasten411e4472012-11-02 10:00:06 -0700284 ALOGV("releaseOutput()");
Eric Laurentbfb1b832013-01-07 09:53:42 -0800285 mOutputCommandThread->releaseOutputCommand(output);
286}
287
288void AudioPolicyService::doReleaseOutput(audio_io_handle_t output)
289{
290 ALOGV("doReleaseOutput from tid %d", gettid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700291 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700292 mpAudioPolicy->release_output(mpAudioPolicy, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700293}
294
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800295audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700296 uint32_t samplingRate,
Glenn Kasten58f30212012-01-12 12:27:51 -0800297 audio_format_t format,
Glenn Kasten254af182012-07-03 14:59:05 -0700298 audio_channel_mask_t channelMask,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700299 int audioSession)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300{
Dima Zavinfce7a472011-04-19 22:30:36 -0700301 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700302 return 0;
303 }
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800304 // already checked by client, but double-check in case the client wrapper is bypassed
Eric Laurent9a54bc22013-09-09 09:08:44 -0700305 if (inputSource >= AUDIO_SOURCE_CNT && inputSource != AUDIO_SOURCE_HOTWORD) {
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800306 return 0;
307 }
Eric Laurent9a54bc22013-09-09 09:08:44 -0700308
309 if ((inputSource == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) {
310 return 0;
311 }
312
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313 Mutex::Autolock _l(mLock);
Glenn Kasten20010052012-06-22 13:43:51 -0700314 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700315 audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
Glenn Kasten8af901c2012-11-01 11:11:38 -0700316 format, channelMask, (audio_in_acoustics_t) 0);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700317
318 if (input == 0) {
319 return input;
320 }
321 // create audio pre processors according to input source
Eric Laurent9a54bc22013-09-09 09:08:44 -0700322 audio_source_t aliasSource = (inputSource == AUDIO_SOURCE_HOTWORD) ?
323 AUDIO_SOURCE_VOICE_RECOGNITION : inputSource;
324
325 ssize_t index = mInputSources.indexOfKey(aliasSource);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700326 if (index < 0) {
327 return input;
328 }
329 ssize_t idx = mInputs.indexOfKey(input);
330 InputDesc *inputDesc;
331 if (idx < 0) {
Glenn Kasten81872a22012-03-07 16:49:22 -0800332 inputDesc = new InputDesc(audioSession);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700333 mInputs.add(input, inputDesc);
334 } else {
335 inputDesc = mInputs.valueAt(idx);
336 }
337
338 Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects;
339 for (size_t i = 0; i < effects.size(); i++) {
340 EffectDesc *effect = effects[i];
341 sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input);
342 status_t status = fx->initCheck();
343 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000344 ALOGW("Failed to create Fx %s on input %d", effect->mName, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700345 // fx goes out of scope and strong ref on AudioEffect is released
346 continue;
347 }
348 for (size_t j = 0; j < effect->mParams.size(); j++) {
349 fx->setParameter(effect->mParams[j]);
350 }
351 inputDesc->mEffects.add(fx);
352 }
353 setPreProcessorEnabled(inputDesc, true);
354 return input;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700355}
356
357status_t AudioPolicyService::startInput(audio_io_handle_t input)
358{
Dima Zavinfce7a472011-04-19 22:30:36 -0700359 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360 return NO_INIT;
361 }
362 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700363
Dima Zavinfce7a472011-04-19 22:30:36 -0700364 return mpAudioPolicy->start_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365}
366
367status_t AudioPolicyService::stopInput(audio_io_handle_t input)
368{
Dima Zavinfce7a472011-04-19 22:30:36 -0700369 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370 return NO_INIT;
371 }
372 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700373
Dima Zavinfce7a472011-04-19 22:30:36 -0700374 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700375}
376
377void AudioPolicyService::releaseInput(audio_io_handle_t input)
378{
Dima Zavinfce7a472011-04-19 22:30:36 -0700379 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700380 return;
381 }
382 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700383 mpAudioPolicy->release_input(mpAudioPolicy, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700384
385 ssize_t index = mInputs.indexOfKey(input);
386 if (index < 0) {
387 return;
388 }
389 InputDesc *inputDesc = mInputs.valueAt(index);
390 setPreProcessorEnabled(inputDesc, false);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700391 delete inputDesc;
392 mInputs.removeItemsAt(index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393}
394
Dima Zavinfce7a472011-04-19 22:30:36 -0700395status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700396 int indexMin,
397 int indexMax)
398{
Dima Zavinfce7a472011-04-19 22:30:36 -0700399 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400 return NO_INIT;
401 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800402 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403 return PERMISSION_DENIED;
404 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800405 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700406 return BAD_VALUE;
407 }
Eric Laurent5f121362012-06-15 14:45:03 -0700408 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700409 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700410 return NO_ERROR;
411}
412
Eric Laurent83844cc2011-11-18 16:43:31 -0800413status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
414 int index,
415 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700416{
Dima Zavinfce7a472011-04-19 22:30:36 -0700417 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700418 return NO_INIT;
419 }
Glenn Kasten44deb052012-02-05 18:09:08 -0800420 if (!settingsAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700421 return PERMISSION_DENIED;
422 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800423 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700424 return BAD_VALUE;
425 }
Eric Laurent5f121362012-06-15 14:45:03 -0700426 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800427 if (mpAudioPolicy->set_stream_volume_index_for_device) {
428 return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
429 stream,
430 index,
431 device);
432 } else {
433 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
434 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435}
436
Eric Laurent83844cc2011-11-18 16:43:31 -0800437status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
438 int *index,
439 audio_devices_t device)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700440{
Dima Zavinfce7a472011-04-19 22:30:36 -0700441 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700442 return NO_INIT;
443 }
Glenn Kasten263709e2012-01-06 08:40:01 -0800444 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700445 return BAD_VALUE;
446 }
Eric Laurent5f121362012-06-15 14:45:03 -0700447 Mutex::Autolock _l(mLock);
Eric Laurent83844cc2011-11-18 16:43:31 -0800448 if (mpAudioPolicy->get_stream_volume_index_for_device) {
449 return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
450 stream,
451 index,
452 device);
453 } else {
454 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
455 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700456}
457
Dima Zavinfce7a472011-04-19 22:30:36 -0700458uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700459{
Dima Zavinfce7a472011-04-19 22:30:36 -0700460 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700461 return 0;
462 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700463 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurentde070132010-07-13 04:45:46 -0700464}
465
Eric Laurent63742522012-03-08 13:42:42 -0800466//audio policy: use audio_device_t appropriately
467
468audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800469{
Dima Zavinfce7a472011-04-19 22:30:36 -0700470 if (mpAudioPolicy == NULL) {
Eric Laurent63742522012-03-08 13:42:42 -0800471 return (audio_devices_t)0;
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800472 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700473 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800474}
475
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700476audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700477{
Dima Zavinfce7a472011-04-19 22:30:36 -0700478 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700479 return NO_INIT;
480 }
481 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700482 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700483}
484
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700485status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700486 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700487 uint32_t strategy,
488 int session,
489 int id)
490{
Dima Zavinfce7a472011-04-19 22:30:36 -0700491 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700492 return NO_INIT;
493 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700494 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700495}
496
497status_t AudioPolicyService::unregisterEffect(int id)
498{
Dima Zavinfce7a472011-04-19 22:30:36 -0700499 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700500 return NO_INIT;
501 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700502 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700503}
504
Eric Laurentdb7c0792011-08-10 10:37:50 -0700505status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
506{
507 if (mpAudioPolicy == NULL) {
508 return NO_INIT;
509 }
510 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
511}
512
Glenn Kastenfff6d712012-01-12 16:38:12 -0800513bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800514{
Dima Zavinfce7a472011-04-19 22:30:36 -0700515 if (mpAudioPolicy == NULL) {
Eric Laurenteda6c362011-02-02 09:33:30 -0800516 return 0;
517 }
518 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700519 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800520}
521
Jean-Michel Trivie336f912013-02-04 16:26:02 -0800522bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
523{
524 if (mpAudioPolicy == NULL) {
525 return 0;
526 }
527 Mutex::Autolock _l(mLock);
528 return mpAudioPolicy->is_stream_active_remotely(mpAudioPolicy, stream, inPastMs);
529}
530
Jean-Michel Trivie3f641f2012-10-10 12:11:16 -0700531bool AudioPolicyService::isSourceActive(audio_source_t source) const
532{
533 if (mpAudioPolicy == NULL) {
534 return false;
535 }
536 if (mpAudioPolicy->is_source_active == 0) {
537 return false;
538 }
539 Mutex::Autolock _l(mLock);
540 return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
541}
542
Eric Laurent57dae992011-07-24 13:36:09 -0700543status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
544 effect_descriptor_t *descriptors,
545 uint32_t *count)
546{
547
548 if (mpAudioPolicy == NULL) {
549 *count = 0;
550 return NO_INIT;
551 }
552 Mutex::Autolock _l(mLock);
553 status_t status = NO_ERROR;
554
555 size_t index;
556 for (index = 0; index < mInputs.size(); index++) {
557 if (mInputs.valueAt(index)->mSessionId == audioSession) {
558 break;
559 }
560 }
561 if (index == mInputs.size()) {
562 *count = 0;
563 return BAD_VALUE;
564 }
565 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
566
567 for (size_t i = 0; i < effects.size(); i++) {
568 effect_descriptor_t desc = effects[i]->descriptor();
569 if (i < *count) {
Glenn Kastena189a682012-02-20 12:16:30 -0800570 descriptors[i] = desc;
Eric Laurent57dae992011-07-24 13:36:09 -0700571 }
572 }
573 if (effects.size() > *count) {
574 status = NO_MEMORY;
575 }
576 *count = effects.size();
577 return status;
578}
579
Mathias Agopian65ab4712010-07-14 17:59:35 -0700580void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700581 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700582 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700583}
584
585static bool tryLock(Mutex& mutex)
586{
587 bool locked = false;
588 for (int i = 0; i < kDumpLockRetries; ++i) {
589 if (mutex.tryLock() == NO_ERROR) {
590 locked = true;
591 break;
592 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800593 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700594 }
595 return locked;
596}
597
598status_t AudioPolicyService::dumpInternals(int fd)
599{
600 const size_t SIZE = 256;
601 char buffer[SIZE];
602 String8 result;
603
Dima Zavinfce7a472011-04-19 22:30:36 -0700604 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700605 result.append(buffer);
606 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
607 result.append(buffer);
608 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
609 result.append(buffer);
610
611 write(fd, result.string(), result.size());
612 return NO_ERROR;
613}
614
615status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
616{
Glenn Kasten44deb052012-02-05 18:09:08 -0800617 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700618 dumpPermissionDenial(fd);
619 } else {
620 bool locked = tryLock(mLock);
621 if (!locked) {
622 String8 result(kDeadlockedString);
623 write(fd, result.string(), result.size());
624 }
625
626 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800627 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628 mAudioCommandThread->dump(fd);
629 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800630 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700631 mTonePlaybackThread->dump(fd);
632 }
633
Dima Zavinfce7a472011-04-19 22:30:36 -0700634 if (mpAudioPolicy) {
635 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700636 }
637
638 if (locked) mLock.unlock();
639 }
640 return NO_ERROR;
641}
642
643status_t AudioPolicyService::dumpPermissionDenial(int fd)
644{
645 const size_t SIZE = 256;
646 char buffer[SIZE];
647 String8 result;
648 snprintf(buffer, SIZE, "Permission Denial: "
649 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
650 IPCThreadState::self()->getCallingPid(),
651 IPCThreadState::self()->getCallingUid());
652 result.append(buffer);
653 write(fd, result.string(), result.size());
654 return NO_ERROR;
655}
656
Glenn Kasten81872a22012-03-07 16:49:22 -0800657void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700658{
Glenn Kasten81872a22012-03-07 16:49:22 -0800659 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700660 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800661 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700662 }
663}
664
Mathias Agopian65ab4712010-07-14 17:59:35 -0700665status_t AudioPolicyService::onTransact(
666 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
667{
668 return BnAudioPolicyService::onTransact(code, data, reply, flags);
669}
670
671
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672// ----------- AudioPolicyService::AudioCommandThread implementation ----------
673
Eric Laurentbfb1b832013-01-07 09:53:42 -0800674AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
675 const wp<AudioPolicyService>& service)
676 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700677{
678 mpToneGenerator = NULL;
679}
680
681
682AudioPolicyService::AudioCommandThread::~AudioCommandThread()
683{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800684 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 release_wake_lock(mName.string());
686 }
687 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800688 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700689}
690
691void AudioPolicyService::AudioCommandThread::onFirstRef()
692{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800693 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694}
695
696bool AudioPolicyService::AudioCommandThread::threadLoop()
697{
698 nsecs_t waitTime = INT64_MAX;
699
700 mLock.lock();
701 while (!exitPending())
702 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700703 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704 nsecs_t curTime = systemTime();
705 // commands are sorted by increasing time stamp: execute them from index 0 and up
706 if (mAudioCommands[0]->mTime <= curTime) {
707 AudioCommand *command = mAudioCommands[0];
708 mAudioCommands.removeAt(0);
709 mLastCommand = *command;
710
711 switch (command->mCommand) {
712 case START_TONE: {
713 mLock.unlock();
714 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100715 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700716 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800717 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700718 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
719 mpToneGenerator->startTone(data->mType);
720 delete data;
721 mLock.lock();
722 }break;
723 case STOP_TONE: {
724 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100725 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700726 if (mpToneGenerator != NULL) {
727 mpToneGenerator->stopTone();
728 delete mpToneGenerator;
729 mpToneGenerator = NULL;
730 }
731 mLock.lock();
732 }break;
733 case SET_VOLUME: {
734 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100735 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700736 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
737 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
738 data->mVolume,
739 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700740 if (command->mWaitStatus) {
741 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100742 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700743 }
744 delete data;
745 }break;
746 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700747 ParametersData *data = (ParametersData *)command->mParam;
748 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
749 data->mKeyValuePairs.string(), data->mIO);
750 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
751 if (command->mWaitStatus) {
752 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100753 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700754 }
755 delete data;
756 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700757 case SET_VOICE_VOLUME: {
758 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100759 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700760 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700761 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
762 if (command->mWaitStatus) {
763 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100764 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700765 }
766 delete data;
767 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800768 case STOP_OUTPUT: {
769 StopOutputData *data = (StopOutputData *)command->mParam;
770 ALOGV("AudioCommandThread() processing stop output %d",
771 data->mIO);
772 sp<AudioPolicyService> svc = mService.promote();
773 if (svc == 0) {
774 break;
775 }
776 mLock.unlock();
777 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
778 mLock.lock();
779 delete data;
780 }break;
781 case RELEASE_OUTPUT: {
782 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam;
783 ALOGV("AudioCommandThread() processing release output %d",
784 data->mIO);
785 sp<AudioPolicyService> svc = mService.promote();
786 if (svc == 0) {
787 break;
788 }
789 mLock.unlock();
790 svc->doReleaseOutput(data->mIO);
791 mLock.lock();
792 delete data;
793 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000795 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700796 }
797 delete command;
798 waitTime = INT64_MAX;
799 } else {
800 waitTime = mAudioCommands[0]->mTime - curTime;
801 break;
802 }
803 }
804 // release delayed commands wake lock
Eric Laurentbfb1b832013-01-07 09:53:42 -0800805 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700806 release_wake_lock(mName.string());
807 }
Steve Block3856b092011-10-20 11:56:00 +0100808 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700809 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100810 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700811 }
812 mLock.unlock();
813 return false;
814}
815
816status_t AudioPolicyService::AudioCommandThread::dump(int fd)
817{
818 const size_t SIZE = 256;
819 char buffer[SIZE];
820 String8 result;
821
822 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
823 result.append(buffer);
824 write(fd, result.string(), result.size());
825
826 bool locked = tryLock(mLock);
827 if (!locked) {
828 String8 result2(kCmdDeadlockedString);
829 write(fd, result2.string(), result2.size());
830 }
831
832 snprintf(buffer, SIZE, "- Commands:\n");
833 result = String8(buffer);
834 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800835 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700836 mAudioCommands[i]->dump(buffer, SIZE);
837 result.append(buffer);
838 }
839 result.append(" Last Command\n");
840 mLastCommand.dump(buffer, SIZE);
841 result.append(buffer);
842
843 write(fd, result.string(), result.size());
844
845 if (locked) mLock.unlock();
846
847 return NO_ERROR;
848}
849
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800850void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
851 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700852{
853 AudioCommand *command = new AudioCommand();
854 command->mCommand = START_TONE;
855 ToneData *data = new ToneData();
856 data->mType = type;
857 data->mStream = stream;
858 command->mParam = (void *)data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700859 Mutex::Autolock _l(mLock);
860 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100861 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700862 mWaitWorkCV.signal();
863}
864
865void AudioPolicyService::AudioCommandThread::stopToneCommand()
866{
867 AudioCommand *command = new AudioCommand();
868 command->mCommand = STOP_TONE;
869 command->mParam = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700870 Mutex::Autolock _l(mLock);
871 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100872 ALOGV("AudioCommandThread() adding tone stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700873 mWaitWorkCV.signal();
874}
875
Glenn Kastenfff6d712012-01-12 16:38:12 -0800876status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700877 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800878 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700879 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700880{
881 status_t status = NO_ERROR;
882
883 AudioCommand *command = new AudioCommand();
884 command->mCommand = SET_VOLUME;
885 VolumeData *data = new VolumeData();
886 data->mStream = stream;
887 data->mVolume = volume;
888 data->mIO = output;
889 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700890 Mutex::Autolock _l(mLock);
891 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100892 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700893 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700894 mWaitWorkCV.signal();
895 if (command->mWaitStatus) {
896 command->mCond.wait(mLock);
897 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100898 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700899 }
900 return status;
901}
902
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800903status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700904 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700905 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700906{
907 status_t status = NO_ERROR;
908
909 AudioCommand *command = new AudioCommand();
910 command->mCommand = SET_PARAMETERS;
911 ParametersData *data = new ParametersData();
912 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700913 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700914 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700915 Mutex::Autolock _l(mLock);
916 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100917 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700918 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700919 mWaitWorkCV.signal();
920 if (command->mWaitStatus) {
921 command->mCond.wait(mLock);
922 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100923 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700924 }
925 return status;
926}
927
928status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
929{
930 status_t status = NO_ERROR;
931
932 AudioCommand *command = new AudioCommand();
933 command->mCommand = SET_VOICE_VOLUME;
934 VoiceVolumeData *data = new VoiceVolumeData();
935 data->mVolume = volume;
936 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700937 Mutex::Autolock _l(mLock);
938 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100939 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940 mWaitWorkCV.signal();
941 if (command->mWaitStatus) {
942 command->mCond.wait(mLock);
943 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100944 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700945 }
946 return status;
947}
948
Eric Laurentbfb1b832013-01-07 09:53:42 -0800949void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
950 audio_stream_type_t stream,
951 int session)
952{
953 AudioCommand *command = new AudioCommand();
954 command->mCommand = STOP_OUTPUT;
955 StopOutputData *data = new StopOutputData();
956 data->mIO = output;
957 data->mStream = stream;
958 data->mSession = session;
959 command->mParam = (void *)data;
960 Mutex::Autolock _l(mLock);
961 insertCommand_l(command);
962 ALOGV("AudioCommandThread() adding stop output %d", output);
963 mWaitWorkCV.signal();
964}
965
966void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
967{
968 AudioCommand *command = new AudioCommand();
969 command->mCommand = RELEASE_OUTPUT;
970 ReleaseOutputData *data = new ReleaseOutputData();
971 data->mIO = output;
972 command->mParam = (void *)data;
973 Mutex::Autolock _l(mLock);
974 insertCommand_l(command);
975 ALOGV("AudioCommandThread() adding release output %d", output);
976 mWaitWorkCV.signal();
977}
978
Mathias Agopian65ab4712010-07-14 17:59:35 -0700979// insertCommand_l() must be called with mLock held
980void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
981{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800982 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700983 Vector <AudioCommand *> removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700984 command->mTime = systemTime() + milliseconds(delayMs);
985
986 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800987 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700988 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
989 }
990
991 // check same pending commands with later time stamps and eliminate them
992 for (i = mAudioCommands.size()-1; i >= 0; i--) {
993 AudioCommand *command2 = mAudioCommands[i];
994 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
995 if (command2->mTime <= command->mTime) break;
996 if (command2->mCommand != command->mCommand) continue;
997
998 switch (command->mCommand) {
999 case SET_PARAMETERS: {
1000 ParametersData *data = (ParametersData *)command->mParam;
1001 ParametersData *data2 = (ParametersData *)command2->mParam;
1002 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001003 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001004 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001005 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1006 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1007 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001008 String8 key;
1009 String8 value;
1010 param.getAt(j, key, value);
1011 for (size_t k = 0; k < param2.size(); k++) {
1012 String8 key2;
1013 String8 value2;
1014 param2.getAt(k, key2, value2);
1015 if (key2 == key) {
1016 param2.remove(key2);
1017 ALOGV("Filtering out parameter %s", key2.string());
1018 break;
1019 }
1020 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001021 }
1022 // if all keys have been filtered out, remove the command.
1023 // otherwise, update the key value pairs
1024 if (param2.size() == 0) {
1025 removedCommands.add(command2);
1026 } else {
1027 data2->mKeyValuePairs = param2.toString();
1028 }
Eric Laurent21e54562013-09-23 12:08:05 -07001029 command->mTime = command2->mTime;
1030 // force delayMs to non 0 so that code below does not request to wait for
1031 // command status as the command is now delayed
1032 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001033 } break;
1034
1035 case SET_VOLUME: {
1036 VolumeData *data = (VolumeData *)command->mParam;
1037 VolumeData *data2 = (VolumeData *)command2->mParam;
1038 if (data->mIO != data2->mIO) break;
1039 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001040 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001041 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001042 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001043 command->mTime = command2->mTime;
1044 // force delayMs to non 0 so that code below does not request to wait for
1045 // command status as the command is now delayed
1046 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001047 } break;
1048 case START_TONE:
1049 case STOP_TONE:
1050 default:
1051 break;
1052 }
1053 }
1054
1055 // remove filtered commands
1056 for (size_t j = 0; j < removedCommands.size(); j++) {
1057 // removed commands always have time stamps greater than current command
1058 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
1059 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +01001060 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001061 mAudioCommands.removeAt(k);
1062 break;
1063 }
1064 }
1065 }
1066 removedCommands.clear();
1067
Eric Laurent21e54562013-09-23 12:08:05 -07001068 // wait for status only if delay is 0
1069 if (delayMs == 0) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001070 command->mWaitStatus = true;
1071 } else {
1072 command->mWaitStatus = false;
1073 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001074
Mathias Agopian65ab4712010-07-14 17:59:35 -07001075 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +01001076 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -07001077 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001078 mAudioCommands.insertAt(command, i + 1);
1079}
1080
1081void AudioPolicyService::AudioCommandThread::exit()
1082{
Steve Block3856b092011-10-20 11:56:00 +01001083 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001084 {
1085 AutoMutex _l(mLock);
1086 requestExit();
1087 mWaitWorkCV.signal();
1088 }
1089 requestExitAndWait();
1090}
1091
1092void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1093{
1094 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1095 mCommand,
1096 (int)ns2s(mTime),
1097 (int)ns2ms(mTime)%1000,
1098 mWaitStatus,
1099 mParam);
1100}
1101
Dima Zavinfce7a472011-04-19 22:30:36 -07001102/******* helpers for the service_ops callbacks defined below *********/
1103void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1104 const char *keyValuePairs,
1105 int delayMs)
1106{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001107 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001108 delayMs);
1109}
1110
1111int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1112 float volume,
1113 audio_io_handle_t output,
1114 int delayMs)
1115{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001116 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001117 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001118}
1119
1120int AudioPolicyService::startTone(audio_policy_tone_t tone,
1121 audio_stream_type_t stream)
1122{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001123 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +00001124 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001125 }
1126 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +00001127 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001128 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001129 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001130 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1131 AUDIO_STREAM_VOICE_CALL);
1132 return 0;
1133}
1134
1135int AudioPolicyService::stopTone()
1136{
1137 mTonePlaybackThread->stopToneCommand();
1138 return 0;
1139}
1140
1141int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1142{
1143 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1144}
1145
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001146bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
1147{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001148 if (mpAudioPolicy == NULL) {
1149 ALOGV("mpAudioPolicy == NULL");
1150 return false;
1151 }
1152
1153 if (mpAudioPolicy->is_offload_supported == NULL) {
1154 ALOGV("HAL does not implement is_offload_supported");
1155 return false;
1156 }
1157
1158 return mpAudioPolicy->is_offload_supported(mpAudioPolicy, &info);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001159}
1160
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001161// ----------------------------------------------------------------------------
1162// Audio pre-processing configuration
1163// ----------------------------------------------------------------------------
1164
Glenn Kasten8dad0e32012-01-09 08:41:22 -08001165/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001166 MIC_SRC_TAG,
1167 VOICE_UL_SRC_TAG,
1168 VOICE_DL_SRC_TAG,
1169 VOICE_CALL_SRC_TAG,
1170 CAMCORDER_SRC_TAG,
1171 VOICE_REC_SRC_TAG,
1172 VOICE_COMM_SRC_TAG
1173};
1174
1175// returns the audio_source_t enum corresponding to the input source name or
1176// AUDIO_SOURCE_CNT is no match found
1177audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
1178{
1179 int i;
1180 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
1181 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001182 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001183 break;
1184 }
1185 }
1186 return (audio_source_t)i;
1187}
1188
1189size_t AudioPolicyService::growParamSize(char *param,
1190 size_t size,
1191 size_t *curSize,
1192 size_t *totSize)
1193{
1194 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
1195 size_t pos = ((*curSize - 1 ) / size + 1) * size;
1196
1197 if (pos + size > *totSize) {
1198 while (pos + size > *totSize) {
1199 *totSize += ((*totSize + 7) / 8) * 4;
1200 }
1201 param = (char *)realloc(param, *totSize);
1202 }
1203 *curSize = pos + size;
1204 return pos;
1205}
1206
1207size_t AudioPolicyService::readParamValue(cnode *node,
1208 char *param,
1209 size_t *curSize,
1210 size_t *totSize)
1211{
1212 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
1213 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
1214 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001215 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001216 return sizeof(short);
1217 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
1218 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
1219 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001220 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001221 return sizeof(int);
1222 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
1223 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
1224 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001225 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001226 return sizeof(float);
1227 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
1228 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
1229 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1230 *(bool *)((char *)param + pos) = false;
1231 } else {
1232 *(bool *)((char *)param + pos) = true;
1233 }
Steve Block3856b092011-10-20 11:56:00 +01001234 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001235 return sizeof(bool);
1236 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1237 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1238 if (*curSize + len + 1 > *totSize) {
1239 *totSize = *curSize + len + 1;
1240 param = (char *)realloc(param, *totSize);
1241 }
1242 strncpy(param + *curSize, node->value, len);
1243 *curSize += len;
1244 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +01001245 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001246 return len;
1247 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001248 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001249 return 0;
1250}
1251
1252effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1253{
1254 cnode *param;
1255 cnode *value;
1256 size_t curSize = sizeof(effect_param_t);
1257 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1258 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1259
1260 param = config_find(root, PARAM_TAG);
1261 value = config_find(root, VALUE_TAG);
1262 if (param == NULL && value == NULL) {
1263 // try to parse simple parameter form {int int}
1264 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -08001265 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001266 // Note: that a pair of random strings is read as 0 0
1267 int *ptr = (int *)fx_param->data;
1268 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +00001269 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001270 *ptr++ = atoi(param->name);
1271 *ptr = atoi(param->value);
1272 fx_param->psize = sizeof(int);
1273 fx_param->vsize = sizeof(int);
1274 return fx_param;
1275 }
1276 }
1277 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001278 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001279 goto error;
1280 }
1281
1282 fx_param->psize = 0;
1283 param = param->first_child;
1284 while (param) {
Steve Block3856b092011-10-20 11:56:00 +01001285 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001286 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1287 if (size == 0) {
1288 goto error;
1289 }
1290 fx_param->psize += size;
1291 param = param->next;
1292 }
1293
1294 // align start of value field on 32 bit boundary
1295 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1296
1297 fx_param->vsize = 0;
1298 value = value->first_child;
1299 while (value) {
Steve Block3856b092011-10-20 11:56:00 +01001300 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001301 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1302 if (size == 0) {
1303 goto error;
1304 }
1305 fx_param->vsize += size;
1306 value = value->next;
1307 }
1308
1309 return fx_param;
1310
1311error:
1312 delete fx_param;
1313 return NULL;
1314}
1315
1316void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1317{
1318 cnode *node = root->first_child;
1319 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001320 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001321 effect_param_t *param = loadEffectParameter(node);
1322 if (param == NULL) {
1323 node = node->next;
1324 continue;
1325 }
1326 params.add(param);
1327 node = node->next;
1328 }
1329}
1330
1331AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1332 cnode *root,
1333 const Vector <EffectDesc *>& effects)
1334{
1335 cnode *node = root->first_child;
1336 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001337 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001338 return NULL;
1339 }
1340 InputSourceDesc *source = new InputSourceDesc();
1341 while (node) {
1342 size_t i;
1343 for (i = 0; i < effects.size(); i++) {
1344 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001345 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001346 break;
1347 }
1348 }
1349 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +01001350 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001351 node = node->next;
1352 continue;
1353 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001354 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001355 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +01001356 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001357 source->mEffects.add(effect);
1358 node = node->next;
1359 }
1360 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001361 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001362 delete source;
1363 return NULL;
1364 }
1365 return source;
1366}
1367
1368status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1369{
1370 cnode *node = config_find(root, PREPROCESSING_TAG);
1371 if (node == NULL) {
1372 return -ENOENT;
1373 }
1374 node = node->first_child;
1375 while (node) {
1376 audio_source_t source = inputSourceNameToEnum(node->name);
1377 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001378 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001379 node = node->next;
1380 continue;
1381 }
Steve Block3856b092011-10-20 11:56:00 +01001382 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001383 InputSourceDesc *desc = loadInputSource(node, effects);
1384 if (desc == NULL) {
1385 node = node->next;
1386 continue;
1387 }
1388 mInputSources.add(source, desc);
1389 node = node->next;
1390 }
1391 return NO_ERROR;
1392}
1393
1394AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1395{
1396 cnode *node = config_find(root, UUID_TAG);
1397 if (node == NULL) {
1398 return NULL;
1399 }
1400 effect_uuid_t uuid;
1401 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001402 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001403 return NULL;
1404 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001405 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001406}
1407
1408status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1409{
1410 cnode *node = config_find(root, EFFECTS_TAG);
1411 if (node == NULL) {
1412 return -ENOENT;
1413 }
1414 node = node->first_child;
1415 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001416 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001417 EffectDesc *effect = loadEffect(node);
1418 if (effect == NULL) {
1419 node = node->next;
1420 continue;
1421 }
1422 effects.add(effect);
1423 node = node->next;
1424 }
1425 return NO_ERROR;
1426}
1427
1428status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1429{
1430 cnode *root;
1431 char *data;
1432
1433 data = (char *)load_file(path, NULL);
1434 if (data == NULL) {
1435 return -ENODEV;
1436 }
1437 root = config_node("", "");
1438 config_load(root, data);
1439
1440 Vector <EffectDesc *> effects;
1441 loadEffects(root, effects);
1442 loadInputSources(root, effects);
1443
1444 config_free(root);
1445 free(root);
1446 free(data);
1447
1448 return NO_ERROR;
1449}
1450
Dima Zavinfce7a472011-04-19 22:30:36 -07001451/* implementation of the interface to the policy manager */
1452extern "C" {
1453
Eric Laurenta4c5a552012-03-29 10:12:40 -07001454
1455static audio_module_handle_t aps_load_hw_module(void *service,
1456 const char *name)
Dima Zavinfce7a472011-04-19 22:30:36 -07001457{
1458 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001459 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001460 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001461 return 0;
1462 }
1463
Eric Laurenta4c5a552012-03-29 10:12:40 -07001464 return af->loadHwModule(name);
1465}
1466
1467// deprecated: replaced by aps_open_output_on_module()
1468static audio_io_handle_t aps_open_output(void *service,
1469 audio_devices_t *pDevices,
1470 uint32_t *pSamplingRate,
1471 audio_format_t *pFormat,
1472 audio_channel_mask_t *pChannelMask,
1473 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001474 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001475{
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->openOutput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask,
1483 pLatencyMs, flags);
1484}
1485
1486static audio_io_handle_t aps_open_output_on_module(void *service,
1487 audio_module_handle_t module,
1488 audio_devices_t *pDevices,
1489 uint32_t *pSamplingRate,
1490 audio_format_t *pFormat,
1491 audio_channel_mask_t *pChannelMask,
1492 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001493 audio_output_flags_t flags,
1494 const audio_offload_info_t *offloadInfo)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001495{
1496 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1497 if (af == 0) {
1498 ALOGW("%s: could not get AudioFlinger", __func__);
1499 return 0;
1500 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001501 return af->openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
Eric Laurentbfb1b832013-01-07 09:53:42 -08001502 pLatencyMs, flags, offloadInfo);
Dima Zavinfce7a472011-04-19 22:30:36 -07001503}
1504
1505static audio_io_handle_t aps_open_dup_output(void *service,
1506 audio_io_handle_t output1,
1507 audio_io_handle_t output2)
1508{
1509 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001510 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001511 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001512 return 0;
1513 }
1514 return af->openDuplicateOutput(output1, output2);
1515}
1516
1517static int aps_close_output(void *service, audio_io_handle_t output)
1518{
1519 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001520 if (af == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001521 return PERMISSION_DENIED;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001522 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001523
1524 return af->closeOutput(output);
1525}
1526
1527static int aps_suspend_output(void *service, audio_io_handle_t output)
1528{
1529 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001530 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001531 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001532 return PERMISSION_DENIED;
1533 }
1534
1535 return af->suspendOutput(output);
1536}
1537
1538static int aps_restore_output(void *service, audio_io_handle_t output)
1539{
1540 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001541 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001542 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001543 return PERMISSION_DENIED;
1544 }
1545
1546 return af->restoreOutput(output);
1547}
1548
Glenn Kasten20010052012-06-22 13:43:51 -07001549// deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
Dima Zavinfce7a472011-04-19 22:30:36 -07001550static audio_io_handle_t aps_open_input(void *service,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001551 audio_devices_t *pDevices,
1552 uint32_t *pSamplingRate,
1553 audio_format_t *pFormat,
1554 audio_channel_mask_t *pChannelMask,
1555 audio_in_acoustics_t acoustics)
Dima Zavinfce7a472011-04-19 22:30:36 -07001556{
1557 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001558 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001559 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001560 return 0;
1561 }
1562
Eric Laurenta4c5a552012-03-29 10:12:40 -07001563 return af->openInput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask);
1564}
1565
1566static audio_io_handle_t aps_open_input_on_module(void *service,
1567 audio_module_handle_t module,
1568 audio_devices_t *pDevices,
1569 uint32_t *pSamplingRate,
1570 audio_format_t *pFormat,
1571 audio_channel_mask_t *pChannelMask)
1572{
1573 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1574 if (af == 0) {
1575 ALOGW("%s: could not get AudioFlinger", __func__);
1576 return 0;
1577 }
1578
1579 return af->openInput(module, pDevices, pSamplingRate, pFormat, pChannelMask);
Dima Zavinfce7a472011-04-19 22:30:36 -07001580}
1581
1582static int aps_close_input(void *service, audio_io_handle_t input)
1583{
1584 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001585 if (af == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001586 return PERMISSION_DENIED;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001587 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001588
1589 return af->closeInput(input);
1590}
1591
1592static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1593 audio_io_handle_t output)
1594{
1595 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001596 if (af == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001597 return PERMISSION_DENIED;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001598 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001599
1600 return af->setStreamOutput(stream, output);
1601}
1602
1603static int aps_move_effects(void *service, int session,
1604 audio_io_handle_t src_output,
1605 audio_io_handle_t dst_output)
1606{
1607 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001608 if (af == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001609 return PERMISSION_DENIED;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001610 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001611
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001612 return af->moveEffects(session, src_output, dst_output);
Dima Zavinfce7a472011-04-19 22:30:36 -07001613}
1614
1615static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1616 const char *keys)
1617{
1618 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1619 return strdup(result.string());
1620}
1621
1622static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1623 const char *kv_pairs, int delay_ms)
1624{
1625 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1626
1627 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1628}
1629
1630static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1631 float volume, audio_io_handle_t output,
1632 int delay_ms)
1633{
1634 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1635
1636 return audioPolicyService->setStreamVolume(stream, volume, output,
1637 delay_ms);
1638}
1639
1640static int aps_start_tone(void *service, audio_policy_tone_t tone,
1641 audio_stream_type_t stream)
1642{
1643 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1644
1645 return audioPolicyService->startTone(tone, stream);
1646}
1647
1648static int aps_stop_tone(void *service)
1649{
1650 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1651
1652 return audioPolicyService->stopTone();
1653}
1654
1655static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1656{
1657 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1658
1659 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1660}
1661
1662}; // extern "C"
1663
1664namespace {
1665 struct audio_policy_service_ops aps_ops = {
1666 open_output : aps_open_output,
1667 open_duplicate_output : aps_open_dup_output,
1668 close_output : aps_close_output,
1669 suspend_output : aps_suspend_output,
1670 restore_output : aps_restore_output,
1671 open_input : aps_open_input,
1672 close_input : aps_close_input,
1673 set_stream_volume : aps_set_stream_volume,
1674 set_stream_output : aps_set_stream_output,
1675 set_parameters : aps_set_parameters,
1676 get_parameters : aps_get_parameters,
1677 start_tone : aps_start_tone,
1678 stop_tone : aps_stop_tone,
1679 set_voice_volume : aps_set_voice_volume,
1680 move_effects : aps_move_effects,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001681 load_hw_module : aps_load_hw_module,
1682 open_output_on_module : aps_open_output_on_module,
1683 open_input_on_module : aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001684 };
1685}; // namespace <unnamed>
1686
Mathias Agopian65ab4712010-07-14 17:59:35 -07001687}; // namespace android