blob: 6a115ff1e0f80b4261855bfd61fb646522139586 [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{
Glenn Kastenefa6ea92014-01-08 09:10:43 -0800478 // FIXME change return type to status_t, and return NO_INIT here
Dima Zavinfce7a472011-04-19 22:30:36 -0700479 if (mpAudioPolicy == NULL) {
Glenn Kastenefa6ea92014-01-08 09:10:43 -0800480 return 0;
Eric Laurentde070132010-07-13 04:45:46 -0700481 }
482 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700483 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700484}
485
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700486status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700487 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700488 uint32_t strategy,
489 int session,
490 int id)
491{
Dima Zavinfce7a472011-04-19 22:30:36 -0700492 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700493 return NO_INIT;
494 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700495 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700496}
497
498status_t AudioPolicyService::unregisterEffect(int id)
499{
Dima Zavinfce7a472011-04-19 22:30:36 -0700500 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700501 return NO_INIT;
502 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700503 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700504}
505
Eric Laurentdb7c0792011-08-10 10:37:50 -0700506status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
507{
508 if (mpAudioPolicy == NULL) {
509 return NO_INIT;
510 }
511 return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
512}
513
Glenn Kastenfff6d712012-01-12 16:38:12 -0800514bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800515{
Dima Zavinfce7a472011-04-19 22:30:36 -0700516 if (mpAudioPolicy == NULL) {
Eric Laurenteda6c362011-02-02 09:33:30 -0800517 return 0;
518 }
519 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700520 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800521}
522
Jean-Michel Trivie336f912013-02-04 16:26:02 -0800523bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
524{
525 if (mpAudioPolicy == NULL) {
526 return 0;
527 }
528 Mutex::Autolock _l(mLock);
529 return mpAudioPolicy->is_stream_active_remotely(mpAudioPolicy, stream, inPastMs);
530}
531
Jean-Michel Trivie3f641f2012-10-10 12:11:16 -0700532bool AudioPolicyService::isSourceActive(audio_source_t source) const
533{
534 if (mpAudioPolicy == NULL) {
535 return false;
536 }
537 if (mpAudioPolicy->is_source_active == 0) {
538 return false;
539 }
540 Mutex::Autolock _l(mLock);
541 return mpAudioPolicy->is_source_active(mpAudioPolicy, source);
542}
543
Eric Laurent57dae992011-07-24 13:36:09 -0700544status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
545 effect_descriptor_t *descriptors,
546 uint32_t *count)
547{
548
549 if (mpAudioPolicy == NULL) {
550 *count = 0;
551 return NO_INIT;
552 }
553 Mutex::Autolock _l(mLock);
554 status_t status = NO_ERROR;
555
556 size_t index;
557 for (index = 0; index < mInputs.size(); index++) {
558 if (mInputs.valueAt(index)->mSessionId == audioSession) {
559 break;
560 }
561 }
562 if (index == mInputs.size()) {
563 *count = 0;
564 return BAD_VALUE;
565 }
566 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
567
568 for (size_t i = 0; i < effects.size(); i++) {
569 effect_descriptor_t desc = effects[i]->descriptor();
570 if (i < *count) {
Glenn Kastena189a682012-02-20 12:16:30 -0800571 descriptors[i] = desc;
Eric Laurent57dae992011-07-24 13:36:09 -0700572 }
573 }
574 if (effects.size() > *count) {
575 status = NO_MEMORY;
576 }
577 *count = effects.size();
578 return status;
579}
580
Mathias Agopian65ab4712010-07-14 17:59:35 -0700581void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Glenn Kasten411e4472012-11-02 10:00:06 -0700582 ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
Eric Laurentde070132010-07-13 04:45:46 -0700583 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700584}
585
586static bool tryLock(Mutex& mutex)
587{
588 bool locked = false;
589 for (int i = 0; i < kDumpLockRetries; ++i) {
590 if (mutex.tryLock() == NO_ERROR) {
591 locked = true;
592 break;
593 }
Glenn Kasten22ecc912012-01-09 08:33:38 -0800594 usleep(kDumpLockSleepUs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 }
596 return locked;
597}
598
599status_t AudioPolicyService::dumpInternals(int fd)
600{
601 const size_t SIZE = 256;
602 char buffer[SIZE];
603 String8 result;
604
Dima Zavinfce7a472011-04-19 22:30:36 -0700605 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700606 result.append(buffer);
607 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
608 result.append(buffer);
609 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
610 result.append(buffer);
611
612 write(fd, result.string(), result.size());
613 return NO_ERROR;
614}
615
Glenn Kasten0f11b512014-01-31 16:18:54 -0800616status_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700617{
Glenn Kasten44deb052012-02-05 18:09:08 -0800618 if (!dumpAllowed()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700619 dumpPermissionDenial(fd);
620 } else {
621 bool locked = tryLock(mLock);
622 if (!locked) {
623 String8 result(kDeadlockedString);
624 write(fd, result.string(), result.size());
625 }
626
627 dumpInternals(fd);
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800628 if (mAudioCommandThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700629 mAudioCommandThread->dump(fd);
630 }
Glenn Kasten9d1f02d2012-02-08 17:47:58 -0800631 if (mTonePlaybackThread != 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700632 mTonePlaybackThread->dump(fd);
633 }
634
Dima Zavinfce7a472011-04-19 22:30:36 -0700635 if (mpAudioPolicy) {
636 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700637 }
638
639 if (locked) mLock.unlock();
640 }
641 return NO_ERROR;
642}
643
644status_t AudioPolicyService::dumpPermissionDenial(int fd)
645{
646 const size_t SIZE = 256;
647 char buffer[SIZE];
648 String8 result;
649 snprintf(buffer, SIZE, "Permission Denial: "
650 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
651 IPCThreadState::self()->getCallingPid(),
652 IPCThreadState::self()->getCallingUid());
653 result.append(buffer);
654 write(fd, result.string(), result.size());
655 return NO_ERROR;
656}
657
Glenn Kasten81872a22012-03-07 16:49:22 -0800658void AudioPolicyService::setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled)
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700659{
Glenn Kasten81872a22012-03-07 16:49:22 -0800660 const Vector<sp<AudioEffect> > &fxVector = inputDesc->mEffects;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700661 for (size_t i = 0; i < fxVector.size(); i++) {
Glenn Kastena1117922012-01-26 10:53:32 -0800662 fxVector.itemAt(i)->setEnabled(enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700663 }
664}
665
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666status_t AudioPolicyService::onTransact(
667 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
668{
669 return BnAudioPolicyService::onTransact(code, data, reply, flags);
670}
671
672
Mathias Agopian65ab4712010-07-14 17:59:35 -0700673// ----------- AudioPolicyService::AudioCommandThread implementation ----------
674
Eric Laurentbfb1b832013-01-07 09:53:42 -0800675AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
676 const wp<AudioPolicyService>& service)
677 : Thread(false), mName(name), mService(service)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678{
679 mpToneGenerator = NULL;
680}
681
682
683AudioPolicyService::AudioCommandThread::~AudioCommandThread()
684{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800685 if (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700686 release_wake_lock(mName.string());
687 }
688 mAudioCommands.clear();
Glenn Kastene9dd0172012-01-27 18:08:45 -0800689 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700690}
691
692void AudioPolicyService::AudioCommandThread::onFirstRef()
693{
Eric Laurentbfb1b832013-01-07 09:53:42 -0800694 run(mName.string(), ANDROID_PRIORITY_AUDIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700695}
696
697bool AudioPolicyService::AudioCommandThread::threadLoop()
698{
699 nsecs_t waitTime = INT64_MAX;
700
701 mLock.lock();
702 while (!exitPending())
703 {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700704 while (!mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700705 nsecs_t curTime = systemTime();
706 // commands are sorted by increasing time stamp: execute them from index 0 and up
707 if (mAudioCommands[0]->mTime <= curTime) {
708 AudioCommand *command = mAudioCommands[0];
709 mAudioCommands.removeAt(0);
710 mLastCommand = *command;
711
712 switch (command->mCommand) {
713 case START_TONE: {
714 mLock.unlock();
715 ToneData *data = (ToneData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100716 ALOGV("AudioCommandThread() processing start tone %d on stream %d",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700717 data->mType, data->mStream);
Glenn Kastene9dd0172012-01-27 18:08:45 -0800718 delete mpToneGenerator;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700719 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
720 mpToneGenerator->startTone(data->mType);
721 delete data;
722 mLock.lock();
723 }break;
724 case STOP_TONE: {
725 mLock.unlock();
Steve Block3856b092011-10-20 11:56:00 +0100726 ALOGV("AudioCommandThread() processing stop tone");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700727 if (mpToneGenerator != NULL) {
728 mpToneGenerator->stopTone();
729 delete mpToneGenerator;
730 mpToneGenerator = NULL;
731 }
732 mLock.lock();
733 }break;
734 case SET_VOLUME: {
735 VolumeData *data = (VolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100736 ALOGV("AudioCommandThread() processing set volume stream %d, \
Eric Laurentde070132010-07-13 04:45:46 -0700737 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
738 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
739 data->mVolume,
740 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700741 if (command->mWaitStatus) {
742 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100743 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700744 }
745 delete data;
746 }break;
747 case SET_PARAMETERS: {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700748 ParametersData *data = (ParametersData *)command->mParam;
749 ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
750 data->mKeyValuePairs.string(), data->mIO);
751 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
752 if (command->mWaitStatus) {
753 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100754 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700755 }
756 delete data;
757 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700758 case SET_VOICE_VOLUME: {
759 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Steve Block3856b092011-10-20 11:56:00 +0100760 ALOGV("AudioCommandThread() processing set voice volume volume %f",
Eric Laurentde070132010-07-13 04:45:46 -0700761 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700762 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
763 if (command->mWaitStatus) {
764 command->mCond.signal();
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100765 command->mCond.waitRelative(mLock, kAudioCommandTimeout);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700766 }
767 delete data;
768 }break;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800769 case STOP_OUTPUT: {
770 StopOutputData *data = (StopOutputData *)command->mParam;
771 ALOGV("AudioCommandThread() processing stop output %d",
772 data->mIO);
773 sp<AudioPolicyService> svc = mService.promote();
774 if (svc == 0) {
775 break;
776 }
777 mLock.unlock();
778 svc->doStopOutput(data->mIO, data->mStream, data->mSession);
779 mLock.lock();
780 delete data;
781 }break;
782 case RELEASE_OUTPUT: {
783 ReleaseOutputData *data = (ReleaseOutputData *)command->mParam;
784 ALOGV("AudioCommandThread() processing release output %d",
785 data->mIO);
786 sp<AudioPolicyService> svc = mService.promote();
787 if (svc == 0) {
788 break;
789 }
790 mLock.unlock();
791 svc->doReleaseOutput(data->mIO);
792 mLock.lock();
793 delete data;
794 }break;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700795 default:
Steve Block5ff1dd52012-01-05 23:22:43 +0000796 ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700797 }
798 delete command;
799 waitTime = INT64_MAX;
800 } else {
801 waitTime = mAudioCommands[0]->mTime - curTime;
802 break;
803 }
804 }
805 // release delayed commands wake lock
Eric Laurentbfb1b832013-01-07 09:53:42 -0800806 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700807 release_wake_lock(mName.string());
808 }
Steve Block3856b092011-10-20 11:56:00 +0100809 ALOGV("AudioCommandThread() going to sleep");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700810 mWaitWorkCV.waitRelative(mLock, waitTime);
Steve Block3856b092011-10-20 11:56:00 +0100811 ALOGV("AudioCommandThread() waking up");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700812 }
813 mLock.unlock();
814 return false;
815}
816
817status_t AudioPolicyService::AudioCommandThread::dump(int fd)
818{
819 const size_t SIZE = 256;
820 char buffer[SIZE];
821 String8 result;
822
823 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
824 result.append(buffer);
825 write(fd, result.string(), result.size());
826
827 bool locked = tryLock(mLock);
828 if (!locked) {
829 String8 result2(kCmdDeadlockedString);
830 write(fd, result2.string(), result2.size());
831 }
832
833 snprintf(buffer, SIZE, "- Commands:\n");
834 result = String8(buffer);
835 result.append(" Command Time Wait pParam\n");
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800836 for (size_t i = 0; i < mAudioCommands.size(); i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700837 mAudioCommands[i]->dump(buffer, SIZE);
838 result.append(buffer);
839 }
840 result.append(" Last Command\n");
841 mLastCommand.dump(buffer, SIZE);
842 result.append(buffer);
843
844 write(fd, result.string(), result.size());
845
846 if (locked) mLock.unlock();
847
848 return NO_ERROR;
849}
850
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800851void AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
852 audio_stream_type_t stream)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700853{
854 AudioCommand *command = new AudioCommand();
855 command->mCommand = START_TONE;
856 ToneData *data = new ToneData();
857 data->mType = type;
858 data->mStream = stream;
859 command->mParam = (void *)data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700860 Mutex::Autolock _l(mLock);
861 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100862 ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700863 mWaitWorkCV.signal();
864}
865
866void AudioPolicyService::AudioCommandThread::stopToneCommand()
867{
868 AudioCommand *command = new AudioCommand();
869 command->mCommand = STOP_TONE;
870 command->mParam = NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700871 Mutex::Autolock _l(mLock);
872 insertCommand_l(command);
Steve Block3856b092011-10-20 11:56:00 +0100873 ALOGV("AudioCommandThread() adding tone stop");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700874 mWaitWorkCV.signal();
875}
876
Glenn Kastenfff6d712012-01-12 16:38:12 -0800877status_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700878 float volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800879 audio_io_handle_t output,
Eric Laurentde070132010-07-13 04:45:46 -0700880 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700881{
882 status_t status = NO_ERROR;
883
884 AudioCommand *command = new AudioCommand();
885 command->mCommand = SET_VOLUME;
886 VolumeData *data = new VolumeData();
887 data->mStream = stream;
888 data->mVolume = volume;
889 data->mIO = output;
890 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700891 Mutex::Autolock _l(mLock);
892 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100893 ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
Eric Laurentde070132010-07-13 04:45:46 -0700894 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700895 mWaitWorkCV.signal();
896 if (command->mWaitStatus) {
897 command->mCond.wait(mLock);
898 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100899 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700900 }
901 return status;
902}
903
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800904status_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700905 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700906 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700907{
908 status_t status = NO_ERROR;
909
910 AudioCommand *command = new AudioCommand();
911 command->mCommand = SET_PARAMETERS;
912 ParametersData *data = new ParametersData();
913 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700914 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700915 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700916 Mutex::Autolock _l(mLock);
917 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100918 ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700919 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700920 mWaitWorkCV.signal();
921 if (command->mWaitStatus) {
922 command->mCond.wait(mLock);
923 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100924 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700925 }
926 return status;
927}
928
929status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
930{
931 status_t status = NO_ERROR;
932
933 AudioCommand *command = new AudioCommand();
934 command->mCommand = SET_VOICE_VOLUME;
935 VoiceVolumeData *data = new VoiceVolumeData();
936 data->mVolume = volume;
937 command->mParam = data;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700938 Mutex::Autolock _l(mLock);
939 insertCommand_l(command, delayMs);
Steve Block3856b092011-10-20 11:56:00 +0100940 ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700941 mWaitWorkCV.signal();
942 if (command->mWaitStatus) {
943 command->mCond.wait(mLock);
944 status = command->mStatus;
Christer Fletcher5fa8c4b2013-01-18 15:27:03 +0100945 command->mCond.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946 }
947 return status;
948}
949
Eric Laurentbfb1b832013-01-07 09:53:42 -0800950void AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
951 audio_stream_type_t stream,
952 int session)
953{
954 AudioCommand *command = new AudioCommand();
955 command->mCommand = STOP_OUTPUT;
956 StopOutputData *data = new StopOutputData();
957 data->mIO = output;
958 data->mStream = stream;
959 data->mSession = session;
960 command->mParam = (void *)data;
961 Mutex::Autolock _l(mLock);
962 insertCommand_l(command);
963 ALOGV("AudioCommandThread() adding stop output %d", output);
964 mWaitWorkCV.signal();
965}
966
967void AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output)
968{
969 AudioCommand *command = new AudioCommand();
970 command->mCommand = RELEASE_OUTPUT;
971 ReleaseOutputData *data = new ReleaseOutputData();
972 data->mIO = output;
973 command->mParam = (void *)data;
974 Mutex::Autolock _l(mLock);
975 insertCommand_l(command);
976 ALOGV("AudioCommandThread() adding release output %d", output);
977 mWaitWorkCV.signal();
978}
979
Mathias Agopian65ab4712010-07-14 17:59:35 -0700980// insertCommand_l() must be called with mLock held
981void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
982{
Glenn Kasten8d6a2442012-02-08 14:04:28 -0800983 ssize_t i; // not size_t because i will count down to -1
Mathias Agopian65ab4712010-07-14 17:59:35 -0700984 Vector <AudioCommand *> removedCommands;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700985 command->mTime = systemTime() + milliseconds(delayMs);
986
987 // acquire wake lock to make sure delayed commands are processed
Eric Laurentbfb1b832013-01-07 09:53:42 -0800988 if (mAudioCommands.isEmpty()) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700989 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
990 }
991
992 // check same pending commands with later time stamps and eliminate them
993 for (i = mAudioCommands.size()-1; i >= 0; i--) {
994 AudioCommand *command2 = mAudioCommands[i];
995 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
996 if (command2->mTime <= command->mTime) break;
997 if (command2->mCommand != command->mCommand) continue;
998
999 switch (command->mCommand) {
1000 case SET_PARAMETERS: {
1001 ParametersData *data = (ParametersData *)command->mParam;
1002 ParametersData *data2 = (ParametersData *)command2->mParam;
1003 if (data->mIO != data2->mIO) break;
Steve Block3856b092011-10-20 11:56:00 +01001004 ALOGV("Comparing parameter command %s to new command %s",
Eric Laurentde070132010-07-13 04:45:46 -07001005 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001006 AudioParameter param = AudioParameter(data->mKeyValuePairs);
1007 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
1008 for (size_t j = 0; j < param.size(); j++) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001009 String8 key;
1010 String8 value;
1011 param.getAt(j, key, value);
1012 for (size_t k = 0; k < param2.size(); k++) {
1013 String8 key2;
1014 String8 value2;
1015 param2.getAt(k, key2, value2);
1016 if (key2 == key) {
1017 param2.remove(key2);
1018 ALOGV("Filtering out parameter %s", key2.string());
1019 break;
1020 }
1021 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001022 }
1023 // if all keys have been filtered out, remove the command.
1024 // otherwise, update the key value pairs
1025 if (param2.size() == 0) {
1026 removedCommands.add(command2);
1027 } else {
1028 data2->mKeyValuePairs = param2.toString();
1029 }
Eric Laurent21e54562013-09-23 12:08:05 -07001030 command->mTime = command2->mTime;
1031 // force delayMs to non 0 so that code below does not request to wait for
1032 // command status as the command is now delayed
1033 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001034 } break;
1035
1036 case SET_VOLUME: {
1037 VolumeData *data = (VolumeData *)command->mParam;
1038 VolumeData *data2 = (VolumeData *)command2->mParam;
1039 if (data->mIO != data2->mIO) break;
1040 if (data->mStream != data2->mStream) break;
Steve Block3856b092011-10-20 11:56:00 +01001041 ALOGV("Filtering out volume command on output %d for stream %d",
Eric Laurentde070132010-07-13 04:45:46 -07001042 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001043 removedCommands.add(command2);
Eric Laurent21e54562013-09-23 12:08:05 -07001044 command->mTime = command2->mTime;
1045 // force delayMs to non 0 so that code below does not request to wait for
1046 // command status as the command is now delayed
1047 delayMs = 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001048 } break;
1049 case START_TONE:
1050 case STOP_TONE:
1051 default:
1052 break;
1053 }
1054 }
1055
1056 // remove filtered commands
1057 for (size_t j = 0; j < removedCommands.size(); j++) {
1058 // removed commands always have time stamps greater than current command
1059 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
1060 if (mAudioCommands[k] == removedCommands[j]) {
Steve Block3856b092011-10-20 11:56:00 +01001061 ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062 mAudioCommands.removeAt(k);
1063 break;
1064 }
1065 }
1066 }
1067 removedCommands.clear();
1068
Eric Laurent21e54562013-09-23 12:08:05 -07001069 // wait for status only if delay is 0
1070 if (delayMs == 0) {
Eric Laurentcec4abb2012-07-03 12:23:02 -07001071 command->mWaitStatus = true;
1072 } else {
1073 command->mWaitStatus = false;
1074 }
Eric Laurentcec4abb2012-07-03 12:23:02 -07001075
Mathias Agopian65ab4712010-07-14 17:59:35 -07001076 // insert command at the right place according to its time stamp
Steve Block3856b092011-10-20 11:56:00 +01001077 ALOGV("inserting command: %d at index %d, num commands %d",
Eric Laurentde070132010-07-13 04:45:46 -07001078 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001079 mAudioCommands.insertAt(command, i + 1);
1080}
1081
1082void AudioPolicyService::AudioCommandThread::exit()
1083{
Steve Block3856b092011-10-20 11:56:00 +01001084 ALOGV("AudioCommandThread::exit");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001085 {
1086 AutoMutex _l(mLock);
1087 requestExit();
1088 mWaitWorkCV.signal();
1089 }
1090 requestExitAndWait();
1091}
1092
1093void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1094{
1095 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1096 mCommand,
1097 (int)ns2s(mTime),
1098 (int)ns2ms(mTime)%1000,
1099 mWaitStatus,
1100 mParam);
1101}
1102
Dima Zavinfce7a472011-04-19 22:30:36 -07001103/******* helpers for the service_ops callbacks defined below *********/
1104void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1105 const char *keyValuePairs,
1106 int delayMs)
1107{
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001108 mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
Dima Zavinfce7a472011-04-19 22:30:36 -07001109 delayMs);
1110}
1111
1112int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1113 float volume,
1114 audio_io_handle_t output,
1115 int delayMs)
1116{
Glenn Kastenfff6d712012-01-12 16:38:12 -08001117 return (int)mAudioCommandThread->volumeCommand(stream, volume,
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001118 output, delayMs);
Dima Zavinfce7a472011-04-19 22:30:36 -07001119}
1120
1121int AudioPolicyService::startTone(audio_policy_tone_t tone,
1122 audio_stream_type_t stream)
1123{
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001124 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
Steve Block29357bc2012-01-06 19:20:56 +00001125 ALOGE("startTone: illegal tone requested (%d)", tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001126 }
1127 if (stream != AUDIO_STREAM_VOICE_CALL) {
Steve Block29357bc2012-01-06 19:20:56 +00001128 ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001129 tone);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001130 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001131 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1132 AUDIO_STREAM_VOICE_CALL);
1133 return 0;
1134}
1135
1136int AudioPolicyService::stopTone()
1137{
1138 mTonePlaybackThread->stopToneCommand();
1139 return 0;
1140}
1141
1142int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1143{
1144 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1145}
1146
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001147bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
1148{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001149 if (mpAudioPolicy == NULL) {
1150 ALOGV("mpAudioPolicy == NULL");
1151 return false;
1152 }
1153
1154 if (mpAudioPolicy->is_offload_supported == NULL) {
1155 ALOGV("HAL does not implement is_offload_supported");
1156 return false;
1157 }
1158
1159 return mpAudioPolicy->is_offload_supported(mpAudioPolicy, &info);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001160}
1161
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001162// ----------------------------------------------------------------------------
1163// Audio pre-processing configuration
1164// ----------------------------------------------------------------------------
1165
Glenn Kasten8dad0e32012-01-09 08:41:22 -08001166/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001167 MIC_SRC_TAG,
1168 VOICE_UL_SRC_TAG,
1169 VOICE_DL_SRC_TAG,
1170 VOICE_CALL_SRC_TAG,
1171 CAMCORDER_SRC_TAG,
1172 VOICE_REC_SRC_TAG,
1173 VOICE_COMM_SRC_TAG
1174};
1175
1176// returns the audio_source_t enum corresponding to the input source name or
1177// AUDIO_SOURCE_CNT is no match found
1178audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
1179{
1180 int i;
1181 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
1182 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001183 ALOGV("inputSourceNameToEnum found source %s %d", name, i);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001184 break;
1185 }
1186 }
1187 return (audio_source_t)i;
1188}
1189
1190size_t AudioPolicyService::growParamSize(char *param,
1191 size_t size,
1192 size_t *curSize,
1193 size_t *totSize)
1194{
1195 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
1196 size_t pos = ((*curSize - 1 ) / size + 1) * size;
1197
1198 if (pos + size > *totSize) {
1199 while (pos + size > *totSize) {
1200 *totSize += ((*totSize + 7) / 8) * 4;
1201 }
1202 param = (char *)realloc(param, *totSize);
1203 }
1204 *curSize = pos + size;
1205 return pos;
1206}
1207
1208size_t AudioPolicyService::readParamValue(cnode *node,
1209 char *param,
1210 size_t *curSize,
1211 size_t *totSize)
1212{
1213 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
1214 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
1215 *(short *)((char *)param + pos) = (short)atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001216 ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001217 return sizeof(short);
1218 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
1219 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
1220 *(int *)((char *)param + pos) = atoi(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001221 ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001222 return sizeof(int);
1223 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
1224 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
1225 *(float *)((char *)param + pos) = (float)atof(node->value);
Steve Block3856b092011-10-20 11:56:00 +01001226 ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001227 return sizeof(float);
1228 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
1229 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
1230 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1231 *(bool *)((char *)param + pos) = false;
1232 } else {
1233 *(bool *)((char *)param + pos) = true;
1234 }
Steve Block3856b092011-10-20 11:56:00 +01001235 ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001236 return sizeof(bool);
1237 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1238 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1239 if (*curSize + len + 1 > *totSize) {
1240 *totSize = *curSize + len + 1;
1241 param = (char *)realloc(param, *totSize);
1242 }
1243 strncpy(param + *curSize, node->value, len);
1244 *curSize += len;
1245 param[*curSize] = '\0';
Steve Block3856b092011-10-20 11:56:00 +01001246 ALOGV("readParamValue() reading string %s", param + *curSize - len);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001247 return len;
1248 }
Steve Block5ff1dd52012-01-05 23:22:43 +00001249 ALOGW("readParamValue() unknown param type %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001250 return 0;
1251}
1252
1253effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1254{
1255 cnode *param;
1256 cnode *value;
1257 size_t curSize = sizeof(effect_param_t);
1258 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1259 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1260
1261 param = config_find(root, PARAM_TAG);
1262 value = config_find(root, VALUE_TAG);
1263 if (param == NULL && value == NULL) {
1264 // try to parse simple parameter form {int int}
1265 param = root->first_child;
Glenn Kastena0d68332012-01-27 16:47:15 -08001266 if (param != NULL) {
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001267 // Note: that a pair of random strings is read as 0 0
1268 int *ptr = (int *)fx_param->data;
1269 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
Steve Block5ff1dd52012-01-05 23:22:43 +00001270 ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001271 *ptr++ = atoi(param->name);
1272 *ptr = atoi(param->value);
1273 fx_param->psize = sizeof(int);
1274 fx_param->vsize = sizeof(int);
1275 return fx_param;
1276 }
1277 }
1278 if (param == NULL || value == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001279 ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001280 goto error;
1281 }
1282
1283 fx_param->psize = 0;
1284 param = param->first_child;
1285 while (param) {
Steve Block3856b092011-10-20 11:56:00 +01001286 ALOGV("loadEffectParameter() reading param of type %s", param->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001287 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1288 if (size == 0) {
1289 goto error;
1290 }
1291 fx_param->psize += size;
1292 param = param->next;
1293 }
1294
1295 // align start of value field on 32 bit boundary
1296 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1297
1298 fx_param->vsize = 0;
1299 value = value->first_child;
1300 while (value) {
Steve Block3856b092011-10-20 11:56:00 +01001301 ALOGV("loadEffectParameter() reading value of type %s", value->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001302 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1303 if (size == 0) {
1304 goto error;
1305 }
1306 fx_param->vsize += size;
1307 value = value->next;
1308 }
1309
1310 return fx_param;
1311
1312error:
1313 delete fx_param;
1314 return NULL;
1315}
1316
1317void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1318{
1319 cnode *node = root->first_child;
1320 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001321 ALOGV("loadEffectParameters() loading param %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001322 effect_param_t *param = loadEffectParameter(node);
1323 if (param == NULL) {
1324 node = node->next;
1325 continue;
1326 }
1327 params.add(param);
1328 node = node->next;
1329 }
1330}
1331
1332AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1333 cnode *root,
1334 const Vector <EffectDesc *>& effects)
1335{
1336 cnode *node = root->first_child;
1337 if (node == NULL) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001338 ALOGW("loadInputSource() empty element %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001339 return NULL;
1340 }
1341 InputSourceDesc *source = new InputSourceDesc();
1342 while (node) {
1343 size_t i;
1344 for (i = 0; i < effects.size(); i++) {
1345 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
Steve Block3856b092011-10-20 11:56:00 +01001346 ALOGV("loadInputSource() found effect %s in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001347 break;
1348 }
1349 }
1350 if (i == effects.size()) {
Steve Block3856b092011-10-20 11:56:00 +01001351 ALOGV("loadInputSource() effect %s not in list", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001352 node = node->next;
1353 continue;
1354 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001355 EffectDesc *effect = new EffectDesc(*effects[i]); // deep copy
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001356 loadEffectParameters(node, effect->mParams);
Steve Block3856b092011-10-20 11:56:00 +01001357 ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001358 source->mEffects.add(effect);
1359 node = node->next;
1360 }
1361 if (source->mEffects.size() == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001362 ALOGW("loadInputSource() no valid effects found in source %s", root->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001363 delete source;
1364 return NULL;
1365 }
1366 return source;
1367}
1368
1369status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1370{
1371 cnode *node = config_find(root, PREPROCESSING_TAG);
1372 if (node == NULL) {
1373 return -ENOENT;
1374 }
1375 node = node->first_child;
1376 while (node) {
1377 audio_source_t source = inputSourceNameToEnum(node->name);
1378 if (source == AUDIO_SOURCE_CNT) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001379 ALOGW("loadInputSources() invalid input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001380 node = node->next;
1381 continue;
1382 }
Steve Block3856b092011-10-20 11:56:00 +01001383 ALOGV("loadInputSources() loading input source %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001384 InputSourceDesc *desc = loadInputSource(node, effects);
1385 if (desc == NULL) {
1386 node = node->next;
1387 continue;
1388 }
1389 mInputSources.add(source, desc);
1390 node = node->next;
1391 }
1392 return NO_ERROR;
1393}
1394
1395AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1396{
1397 cnode *node = config_find(root, UUID_TAG);
1398 if (node == NULL) {
1399 return NULL;
1400 }
1401 effect_uuid_t uuid;
1402 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001403 ALOGW("loadEffect() invalid uuid %s", node->value);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001404 return NULL;
1405 }
Glenn Kasten9fda4b82012-02-02 14:04:37 -08001406 return new EffectDesc(root->name, uuid);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001407}
1408
1409status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1410{
1411 cnode *node = config_find(root, EFFECTS_TAG);
1412 if (node == NULL) {
1413 return -ENOENT;
1414 }
1415 node = node->first_child;
1416 while (node) {
Steve Block3856b092011-10-20 11:56:00 +01001417 ALOGV("loadEffects() loading effect %s", node->name);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001418 EffectDesc *effect = loadEffect(node);
1419 if (effect == NULL) {
1420 node = node->next;
1421 continue;
1422 }
1423 effects.add(effect);
1424 node = node->next;
1425 }
1426 return NO_ERROR;
1427}
1428
1429status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1430{
1431 cnode *root;
1432 char *data;
1433
1434 data = (char *)load_file(path, NULL);
1435 if (data == NULL) {
1436 return -ENODEV;
1437 }
1438 root = config_node("", "");
1439 config_load(root, data);
1440
1441 Vector <EffectDesc *> effects;
1442 loadEffects(root, effects);
1443 loadInputSources(root, effects);
1444
Yu Yezhonge6056ba2013-10-15 14:32:34 +08001445 // delete effects to fix memory leak.
1446 // as effects is local var and valgrind would treat this as memory leak
1447 // and although it only did in mediaserver init, but free it in case mediaserver reboot
1448 size_t i;
1449 for (i = 0; i < effects.size(); i++) {
1450 delete effects[i];
1451 }
1452
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001453 config_free(root);
1454 free(root);
1455 free(data);
1456
1457 return NO_ERROR;
1458}
1459
Dima Zavinfce7a472011-04-19 22:30:36 -07001460/* implementation of the interface to the policy manager */
1461extern "C" {
1462
Eric Laurenta4c5a552012-03-29 10:12:40 -07001463
Glenn Kasten0f11b512014-01-31 16:18:54 -08001464static audio_module_handle_t aps_load_hw_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001465 const char *name)
Dima Zavinfce7a472011-04-19 22:30:36 -07001466{
1467 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001468 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001469 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001470 return 0;
1471 }
1472
Eric Laurenta4c5a552012-03-29 10:12:40 -07001473 return af->loadHwModule(name);
1474}
1475
1476// deprecated: replaced by aps_open_output_on_module()
Glenn Kasten0f11b512014-01-31 16:18:54 -08001477static audio_io_handle_t aps_open_output(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001478 audio_devices_t *pDevices,
1479 uint32_t *pSamplingRate,
1480 audio_format_t *pFormat,
1481 audio_channel_mask_t *pChannelMask,
1482 uint32_t *pLatencyMs,
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001483 audio_output_flags_t flags)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001484{
1485 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1486 if (af == 0) {
1487 ALOGW("%s: could not get AudioFlinger", __func__);
1488 return 0;
1489 }
1490
1491 return af->openOutput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask,
1492 pLatencyMs, flags);
1493}
1494
Glenn Kasten0f11b512014-01-31 16:18:54 -08001495static audio_io_handle_t aps_open_output_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001496 audio_module_handle_t module,
1497 audio_devices_t *pDevices,
1498 uint32_t *pSamplingRate,
1499 audio_format_t *pFormat,
1500 audio_channel_mask_t *pChannelMask,
1501 uint32_t *pLatencyMs,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001502 audio_output_flags_t flags,
1503 const audio_offload_info_t *offloadInfo)
Eric Laurenta4c5a552012-03-29 10:12:40 -07001504{
1505 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1506 if (af == 0) {
1507 ALOGW("%s: could not get AudioFlinger", __func__);
1508 return 0;
1509 }
Eric Laurenta4c5a552012-03-29 10:12:40 -07001510 return af->openOutput(module, pDevices, pSamplingRate, pFormat, pChannelMask,
Eric Laurentbfb1b832013-01-07 09:53:42 -08001511 pLatencyMs, flags, offloadInfo);
Dima Zavinfce7a472011-04-19 22:30:36 -07001512}
1513
Glenn Kasten0f11b512014-01-31 16:18:54 -08001514static audio_io_handle_t aps_open_dup_output(void *service __unused,
Dima Zavinfce7a472011-04-19 22:30:36 -07001515 audio_io_handle_t output1,
1516 audio_io_handle_t output2)
1517{
1518 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001519 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001520 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001521 return 0;
1522 }
1523 return af->openDuplicateOutput(output1, output2);
1524}
1525
Glenn Kasten0f11b512014-01-31 16:18:54 -08001526static int aps_close_output(void *service __unused, audio_io_handle_t output)
Dima Zavinfce7a472011-04-19 22:30:36 -07001527{
1528 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001529 if (af == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001530 return PERMISSION_DENIED;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001531 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001532
1533 return af->closeOutput(output);
1534}
1535
Glenn Kasten0f11b512014-01-31 16:18:54 -08001536static int aps_suspend_output(void *service __unused, audio_io_handle_t output)
Dima Zavinfce7a472011-04-19 22:30:36 -07001537{
1538 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001539 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001540 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001541 return PERMISSION_DENIED;
1542 }
1543
1544 return af->suspendOutput(output);
1545}
1546
Glenn Kasten0f11b512014-01-31 16:18:54 -08001547static int aps_restore_output(void *service __unused, audio_io_handle_t output)
Dima Zavinfce7a472011-04-19 22:30:36 -07001548{
1549 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001550 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001551 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001552 return PERMISSION_DENIED;
1553 }
1554
1555 return af->restoreOutput(output);
1556}
1557
Glenn Kasten20010052012-06-22 13:43:51 -07001558// deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
Glenn Kasten0f11b512014-01-31 16:18:54 -08001559static audio_io_handle_t aps_open_input(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001560 audio_devices_t *pDevices,
1561 uint32_t *pSamplingRate,
1562 audio_format_t *pFormat,
1563 audio_channel_mask_t *pChannelMask,
Glenn Kasten0f11b512014-01-31 16:18:54 -08001564 audio_in_acoustics_t acoustics __unused)
Dima Zavinfce7a472011-04-19 22:30:36 -07001565{
1566 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten7378ca52012-01-20 13:44:40 -08001567 if (af == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001568 ALOGW("%s: could not get AudioFlinger", __func__);
Dima Zavinfce7a472011-04-19 22:30:36 -07001569 return 0;
1570 }
1571
Eric Laurenta4c5a552012-03-29 10:12:40 -07001572 return af->openInput((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask);
1573}
1574
Glenn Kasten0f11b512014-01-31 16:18:54 -08001575static audio_io_handle_t aps_open_input_on_module(void *service __unused,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001576 audio_module_handle_t module,
1577 audio_devices_t *pDevices,
1578 uint32_t *pSamplingRate,
1579 audio_format_t *pFormat,
1580 audio_channel_mask_t *pChannelMask)
1581{
1582 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1583 if (af == 0) {
1584 ALOGW("%s: could not get AudioFlinger", __func__);
1585 return 0;
1586 }
1587
1588 return af->openInput(module, pDevices, pSamplingRate, pFormat, pChannelMask);
Dima Zavinfce7a472011-04-19 22:30:36 -07001589}
1590
Glenn Kasten0f11b512014-01-31 16:18:54 -08001591static int aps_close_input(void *service __unused, audio_io_handle_t input)
Dima Zavinfce7a472011-04-19 22:30:36 -07001592{
1593 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001594 if (af == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001595 return PERMISSION_DENIED;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001596 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001597
1598 return af->closeInput(input);
1599}
1600
Glenn Kasten0f11b512014-01-31 16:18:54 -08001601static int aps_set_stream_output(void *service __unused, audio_stream_type_t stream,
Dima Zavinfce7a472011-04-19 22:30:36 -07001602 audio_io_handle_t output)
1603{
1604 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001605 if (af == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001606 return PERMISSION_DENIED;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001607 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001608
1609 return af->setStreamOutput(stream, output);
1610}
1611
Glenn Kasten0f11b512014-01-31 16:18:54 -08001612static int aps_move_effects(void *service __unused, int session,
Dima Zavinfce7a472011-04-19 22:30:36 -07001613 audio_io_handle_t src_output,
1614 audio_io_handle_t dst_output)
1615{
1616 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001617 if (af == 0) {
Dima Zavinfce7a472011-04-19 22:30:36 -07001618 return PERMISSION_DENIED;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -07001619 }
Dima Zavinfce7a472011-04-19 22:30:36 -07001620
Glenn Kasten72ef00d2012-01-17 11:09:42 -08001621 return af->moveEffects(session, src_output, dst_output);
Dima Zavinfce7a472011-04-19 22:30:36 -07001622}
1623
Glenn Kasten0f11b512014-01-31 16:18:54 -08001624static char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
Dima Zavinfce7a472011-04-19 22:30:36 -07001625 const char *keys)
1626{
1627 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1628 return strdup(result.string());
1629}
1630
1631static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1632 const char *kv_pairs, int delay_ms)
1633{
1634 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1635
1636 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1637}
1638
1639static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1640 float volume, audio_io_handle_t output,
1641 int delay_ms)
1642{
1643 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1644
1645 return audioPolicyService->setStreamVolume(stream, volume, output,
1646 delay_ms);
1647}
1648
1649static int aps_start_tone(void *service, audio_policy_tone_t tone,
1650 audio_stream_type_t stream)
1651{
1652 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1653
1654 return audioPolicyService->startTone(tone, stream);
1655}
1656
1657static int aps_stop_tone(void *service)
1658{
1659 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1660
1661 return audioPolicyService->stopTone();
1662}
1663
1664static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1665{
1666 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1667
1668 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1669}
1670
1671}; // extern "C"
1672
1673namespace {
1674 struct audio_policy_service_ops aps_ops = {
1675 open_output : aps_open_output,
1676 open_duplicate_output : aps_open_dup_output,
1677 close_output : aps_close_output,
1678 suspend_output : aps_suspend_output,
1679 restore_output : aps_restore_output,
1680 open_input : aps_open_input,
1681 close_input : aps_close_input,
1682 set_stream_volume : aps_set_stream_volume,
1683 set_stream_output : aps_set_stream_output,
1684 set_parameters : aps_set_parameters,
1685 get_parameters : aps_get_parameters,
1686 start_tone : aps_start_tone,
1687 stop_tone : aps_stop_tone,
1688 set_voice_volume : aps_set_voice_volume,
1689 move_effects : aps_move_effects,
Eric Laurenta4c5a552012-03-29 10:12:40 -07001690 load_hw_module : aps_load_hw_module,
1691 open_output_on_module : aps_open_output_on_module,
1692 open_input_on_module : aps_open_input_on_module,
Dima Zavinfce7a472011-04-19 22:30:36 -07001693 };
1694}; // namespace <unnamed>
1695
Mathias Agopian65ab4712010-07-14 17:59:35 -07001696}; // namespace android