blob: dd1e153621c755d589005f1852f906aa0f27722b [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AudioPolicyService"
18//#define LOG_NDEBUG 0
19
20#undef __STRICT_ANSI__
21#define __STDINT_LIMITS
22#define __STDC_LIMIT_MACROS
23#include <stdint.h>
24
25#include <sys/time.h>
26#include <binder/IServiceManager.h>
27#include <utils/Log.h>
28#include <cutils/properties.h>
29#include <binder/IPCThreadState.h>
30#include <utils/String16.h>
31#include <utils/threads.h>
32#include "AudioPolicyService.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070033#include <cutils/properties.h>
34#include <dlfcn.h>
35#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>
Dima Zavinfce7a472011-04-19 22:30:36 -070044
Mathias Agopian65ab4712010-07-14 17:59:35 -070045namespace android {
46
Mathias Agopian65ab4712010-07-14 17:59:35 -070047static const char *kDeadlockedString = "AudioPolicyService may be deadlocked\n";
48static const char *kCmdDeadlockedString = "AudioPolicyService command thread may be deadlocked\n";
49
50static const int kDumpLockRetries = 50;
51static const int kDumpLockSleep = 20000;
52
53static bool checkPermission() {
Mathias Agopian65ab4712010-07-14 17:59:35 -070054 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
55 bool ok = checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS"));
56 if (!ok) LOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS");
57 return ok;
58}
59
Dima Zavinfce7a472011-04-19 22:30:36 -070060namespace {
61 extern struct audio_policy_service_ops aps_ops;
62};
63
Mathias Agopian65ab4712010-07-14 17:59:35 -070064// ----------------------------------------------------------------------------
65
66AudioPolicyService::AudioPolicyService()
Dima Zavinfce7a472011-04-19 22:30:36 -070067 : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL)
Mathias Agopian65ab4712010-07-14 17:59:35 -070068{
69 char value[PROPERTY_VALUE_MAX];
Dima Zavinfce7a472011-04-19 22:30:36 -070070 const struct hw_module_t *module;
71 int forced_val;
72 int rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -070073
Eric Laurent93575202011-01-18 18:39:02 -080074 Mutex::Autolock _l(mLock);
75
Mathias Agopian65ab4712010-07-14 17:59:35 -070076 // start tone playback thread
77 mTonePlaybackThread = new AudioCommandThread(String8(""));
78 // start audio commands thread
79 mAudioCommandThread = new AudioCommandThread(String8("ApmCommandThread"));
80
Dima Zavinfce7a472011-04-19 22:30:36 -070081 /* instantiate the audio policy manager */
82 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
83 if (rc)
84 return;
Mathias Agopian65ab4712010-07-14 17:59:35 -070085
Dima Zavinfce7a472011-04-19 22:30:36 -070086 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
87 LOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
88 if (rc)
89 return;
Eric Laurent93575202011-01-18 18:39:02 -080090
Dima Zavinfce7a472011-04-19 22:30:36 -070091 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
92 &mpAudioPolicy);
93 LOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
94 if (rc)
95 return;
96
97 rc = mpAudioPolicy->init_check(mpAudioPolicy);
98 LOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
99 if (rc)
100 return;
101
102 property_get("ro.camera.sound.forced", value, "0");
103 forced_val = strtol(value, NULL, 0);
104 mpAudioPolicy->set_can_mute_enforced_audible(mpAudioPolicy, !forced_val);
105
106 LOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700107
108 // load audio pre processing modules
109 if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) {
110 loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE);
111 } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) {
112 loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE);
113 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700114}
115
116AudioPolicyService::~AudioPolicyService()
117{
118 mTonePlaybackThread->exit();
119 mTonePlaybackThread.clear();
120 mAudioCommandThread->exit();
121 mAudioCommandThread.clear();
122
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700123
124 // release audio pre processing resources
125 for (size_t i = 0; i < mInputSources.size(); i++) {
126 InputSourceDesc *source = mInputSources.valueAt(i);
127 Vector <EffectDesc *> effects = source->mEffects;
128 for (size_t j = 0; j < effects.size(); j++) {
129 delete effects[j]->mName;
130 Vector <effect_param_t *> params = effects[j]->mParams;
131 for (size_t k = 0; k < params.size(); k++) {
132 delete params[k];
133 }
134 params.clear();
135 delete effects[j];
136 }
137 effects.clear();
138 delete source;
139 }
140 mInputSources.clear();
141
142 for (size_t i = 0; i < mInputs.size(); i++) {
143 mInputs.valueAt(i)->mEffects.clear();
144 delete mInputs.valueAt(i);
145 }
146 mInputs.clear();
147
Dima Zavinfce7a472011-04-19 22:30:36 -0700148 if (mpAudioPolicy && mpAudioPolicyDev)
149 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
150 if (mpAudioPolicyDev)
151 audio_policy_dev_close(mpAudioPolicyDev);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700152}
153
Dima Zavinfce7a472011-04-19 22:30:36 -0700154status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
155 audio_policy_dev_state_t state,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700156 const char *device_address)
157{
Dima Zavinfce7a472011-04-19 22:30:36 -0700158 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700159 return NO_INIT;
160 }
161 if (!checkPermission()) {
162 return PERMISSION_DENIED;
163 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700164 if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700165 return BAD_VALUE;
166 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700167 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
168 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700169 return BAD_VALUE;
170 }
171
172 LOGV("setDeviceConnectionState() tid %d", gettid());
173 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700174 return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device,
175 state, device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700176}
177
Dima Zavinfce7a472011-04-19 22:30:36 -0700178audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
179 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -0700180 const char *device_address)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700181{
Dima Zavinfce7a472011-04-19 22:30:36 -0700182 if (mpAudioPolicy == NULL) {
183 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700184 }
185 if (!checkPermission()) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700186 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700187 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700188 return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device,
189 device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700190}
191
192status_t AudioPolicyService::setPhoneState(int state)
193{
Dima Zavinfce7a472011-04-19 22:30:36 -0700194 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700195 return NO_INIT;
196 }
197 if (!checkPermission()) {
198 return PERMISSION_DENIED;
199 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700200 if (state < 0 || state >= AUDIO_MODE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700201 return BAD_VALUE;
202 }
203
204 LOGV("setPhoneState() tid %d", gettid());
205
206 // TODO: check if it is more appropriate to do it in platform specific policy manager
207 AudioSystem::setMode(state);
208
209 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700210 mpAudioPolicy->set_phone_state(mpAudioPolicy, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700211 return NO_ERROR;
212}
213
214status_t AudioPolicyService::setRingerMode(uint32_t mode, uint32_t mask)
215{
Dima Zavinfce7a472011-04-19 22:30:36 -0700216 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700217 return NO_INIT;
218 }
219 if (!checkPermission()) {
220 return PERMISSION_DENIED;
221 }
222
Dima Zavinfce7a472011-04-19 22:30:36 -0700223 mpAudioPolicy->set_ringer_mode(mpAudioPolicy, mode, mask);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700224 return NO_ERROR;
225}
226
Dima Zavinfce7a472011-04-19 22:30:36 -0700227status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
228 audio_policy_forced_cfg_t config)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700229{
Dima Zavinfce7a472011-04-19 22:30:36 -0700230 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700231 return NO_INIT;
232 }
233 if (!checkPermission()) {
234 return PERMISSION_DENIED;
235 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700236 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700237 return BAD_VALUE;
238 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700239 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700240 return BAD_VALUE;
241 }
242 LOGV("setForceUse() tid %d", gettid());
243 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700244 mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700245 return NO_ERROR;
246}
247
Dima Zavinfce7a472011-04-19 22:30:36 -0700248audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700249{
Dima Zavinfce7a472011-04-19 22:30:36 -0700250 if (mpAudioPolicy == NULL) {
251 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700252 }
253 if (!checkPermission()) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700254 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700255 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700256 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
257 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700258 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700259 return mpAudioPolicy->get_force_use(mpAudioPolicy, usage);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700260}
261
Dima Zavinfce7a472011-04-19 22:30:36 -0700262audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700263 uint32_t samplingRate,
264 uint32_t format,
265 uint32_t channels,
Dima Zavinfce7a472011-04-19 22:30:36 -0700266 audio_policy_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700267{
Dima Zavinfce7a472011-04-19 22:30:36 -0700268 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700269 return 0;
270 }
271 LOGV("getOutput() tid %d", gettid());
272 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700273 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, format, channels, flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700274}
275
Eric Laurentde070132010-07-13 04:45:46 -0700276status_t AudioPolicyService::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700277 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700278 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700279{
Dima Zavinfce7a472011-04-19 22:30:36 -0700280 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700281 return NO_INIT;
282 }
283 LOGV("startOutput() tid %d", gettid());
284 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700285 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700286}
287
Eric Laurentde070132010-07-13 04:45:46 -0700288status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700289 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700290 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700291{
Dima Zavinfce7a472011-04-19 22:30:36 -0700292 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700293 return NO_INIT;
294 }
295 LOGV("stopOutput() tid %d", gettid());
296 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700297 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700298}
299
300void AudioPolicyService::releaseOutput(audio_io_handle_t output)
301{
Dima Zavinfce7a472011-04-19 22:30:36 -0700302 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700303 return;
304 }
305 LOGV("releaseOutput() tid %d", gettid());
306 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700307 mpAudioPolicy->release_output(mpAudioPolicy, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700308}
309
310audio_io_handle_t AudioPolicyService::getInput(int inputSource,
311 uint32_t samplingRate,
312 uint32_t format,
313 uint32_t channels,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700314 audio_in_acoustics_t acoustics,
315 int audioSession)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316{
Dima Zavinfce7a472011-04-19 22:30:36 -0700317 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700318 return 0;
319 }
320 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700321 audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
322 format, channels, acoustics);
323
324 if (input == 0) {
325 return input;
326 }
327 // create audio pre processors according to input source
328 ssize_t index = mInputSources.indexOfKey((audio_source_t)inputSource);
329 if (index < 0) {
330 return input;
331 }
332 ssize_t idx = mInputs.indexOfKey(input);
333 InputDesc *inputDesc;
334 if (idx < 0) {
335 inputDesc = new InputDesc();
336 inputDesc->mSessionId = audioSession;
337 mInputs.add(input, inputDesc);
338 } else {
339 inputDesc = mInputs.valueAt(idx);
340 }
341
342 Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects;
343 for (size_t i = 0; i < effects.size(); i++) {
344 EffectDesc *effect = effects[i];
345 sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input);
346 status_t status = fx->initCheck();
347 if (status != NO_ERROR && status != ALREADY_EXISTS) {
348 LOGW("Failed to create Fx %s on input %d", effect->mName, input);
349 // fx goes out of scope and strong ref on AudioEffect is released
350 continue;
351 }
352 for (size_t j = 0; j < effect->mParams.size(); j++) {
353 fx->setParameter(effect->mParams[j]);
354 }
355 inputDesc->mEffects.add(fx);
356 }
357 setPreProcessorEnabled(inputDesc, true);
358 return input;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700359}
360
361status_t AudioPolicyService::startInput(audio_io_handle_t input)
362{
Dima Zavinfce7a472011-04-19 22:30:36 -0700363 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700364 return NO_INIT;
365 }
366 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700367
Dima Zavinfce7a472011-04-19 22:30:36 -0700368 return mpAudioPolicy->start_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700369}
370
371status_t AudioPolicyService::stopInput(audio_io_handle_t input)
372{
Dima Zavinfce7a472011-04-19 22:30:36 -0700373 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700374 return NO_INIT;
375 }
376 Mutex::Autolock _l(mLock);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700377
Dima Zavinfce7a472011-04-19 22:30:36 -0700378 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700379}
380
381void AudioPolicyService::releaseInput(audio_io_handle_t input)
382{
Dima Zavinfce7a472011-04-19 22:30:36 -0700383 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384 return;
385 }
386 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700387 mpAudioPolicy->release_input(mpAudioPolicy, input);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700388
389 ssize_t index = mInputs.indexOfKey(input);
390 if (index < 0) {
391 return;
392 }
393 InputDesc *inputDesc = mInputs.valueAt(index);
394 setPreProcessorEnabled(inputDesc, false);
395 inputDesc->mEffects.clear();
396 delete inputDesc;
397 mInputs.removeItemsAt(index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700398}
399
Dima Zavinfce7a472011-04-19 22:30:36 -0700400status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700401 int indexMin,
402 int indexMax)
403{
Dima Zavinfce7a472011-04-19 22:30:36 -0700404 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700405 return NO_INIT;
406 }
407 if (!checkPermission()) {
408 return PERMISSION_DENIED;
409 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700410 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700411 return BAD_VALUE;
412 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700413 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700414 return NO_ERROR;
415}
416
Dima Zavinfce7a472011-04-19 22:30:36 -0700417status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream, int index)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700418{
Dima Zavinfce7a472011-04-19 22:30:36 -0700419 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700420 return NO_INIT;
421 }
422 if (!checkPermission()) {
423 return PERMISSION_DENIED;
424 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700425 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700426 return BAD_VALUE;
427 }
428
Dima Zavinfce7a472011-04-19 22:30:36 -0700429 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700430}
431
Dima Zavinfce7a472011-04-19 22:30:36 -0700432status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream, int *index)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700433{
Dima Zavinfce7a472011-04-19 22:30:36 -0700434 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 return NO_INIT;
436 }
437 if (!checkPermission()) {
438 return PERMISSION_DENIED;
439 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700440 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700441 return BAD_VALUE;
442 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700443 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700444}
445
Dima Zavinfce7a472011-04-19 22:30:36 -0700446uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700447{
Dima Zavinfce7a472011-04-19 22:30:36 -0700448 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700449 return 0;
450 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700451 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurentde070132010-07-13 04:45:46 -0700452}
453
Dima Zavinfce7a472011-04-19 22:30:36 -0700454uint32_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800455{
Dima Zavinfce7a472011-04-19 22:30:36 -0700456 if (mpAudioPolicy == NULL) {
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800457 return 0;
458 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700459 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800460}
461
Eric Laurentde070132010-07-13 04:45:46 -0700462audio_io_handle_t AudioPolicyService::getOutputForEffect(effect_descriptor_t *desc)
463{
Dima Zavinfce7a472011-04-19 22:30:36 -0700464 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700465 return NO_INIT;
466 }
467 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700468 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700469}
470
471status_t AudioPolicyService::registerEffect(effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700472 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700473 uint32_t strategy,
474 int session,
475 int id)
476{
Dima Zavinfce7a472011-04-19 22:30:36 -0700477 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700478 return NO_INIT;
479 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700480 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700481}
482
483status_t AudioPolicyService::unregisterEffect(int id)
484{
Dima Zavinfce7a472011-04-19 22:30:36 -0700485 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700486 return NO_INIT;
487 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700488 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700489}
490
Eric Laurenteda6c362011-02-02 09:33:30 -0800491bool AudioPolicyService::isStreamActive(int stream, uint32_t inPastMs) const
492{
Dima Zavinfce7a472011-04-19 22:30:36 -0700493 if (mpAudioPolicy == NULL) {
Eric Laurenteda6c362011-02-02 09:33:30 -0800494 return 0;
495 }
496 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700497 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800498}
499
Mathias Agopian65ab4712010-07-14 17:59:35 -0700500void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Eric Laurentde070132010-07-13 04:45:46 -0700501 LOGW("binderDied() %p, tid %d, calling tid %d", who.unsafe_get(), gettid(),
502 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700503}
504
505static bool tryLock(Mutex& mutex)
506{
507 bool locked = false;
508 for (int i = 0; i < kDumpLockRetries; ++i) {
509 if (mutex.tryLock() == NO_ERROR) {
510 locked = true;
511 break;
512 }
513 usleep(kDumpLockSleep);
514 }
515 return locked;
516}
517
518status_t AudioPolicyService::dumpInternals(int fd)
519{
520 const size_t SIZE = 256;
521 char buffer[SIZE];
522 String8 result;
523
Dima Zavinfce7a472011-04-19 22:30:36 -0700524 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700525 result.append(buffer);
526 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
527 result.append(buffer);
528 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
529 result.append(buffer);
530
531 write(fd, result.string(), result.size());
532 return NO_ERROR;
533}
534
535status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
536{
537 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
538 dumpPermissionDenial(fd);
539 } else {
540 bool locked = tryLock(mLock);
541 if (!locked) {
542 String8 result(kDeadlockedString);
543 write(fd, result.string(), result.size());
544 }
545
546 dumpInternals(fd);
547 if (mAudioCommandThread != NULL) {
548 mAudioCommandThread->dump(fd);
549 }
550 if (mTonePlaybackThread != NULL) {
551 mTonePlaybackThread->dump(fd);
552 }
553
Dima Zavinfce7a472011-04-19 22:30:36 -0700554 if (mpAudioPolicy) {
555 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700556 }
557
558 if (locked) mLock.unlock();
559 }
560 return NO_ERROR;
561}
562
563status_t AudioPolicyService::dumpPermissionDenial(int fd)
564{
565 const size_t SIZE = 256;
566 char buffer[SIZE];
567 String8 result;
568 snprintf(buffer, SIZE, "Permission Denial: "
569 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
570 IPCThreadState::self()->getCallingPid(),
571 IPCThreadState::self()->getCallingUid());
572 result.append(buffer);
573 write(fd, result.string(), result.size());
574 return NO_ERROR;
575}
576
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700577void AudioPolicyService::setPreProcessorEnabled(InputDesc *inputDesc, bool enabled)
578{
579 Vector<sp<AudioEffect> > fxVector = inputDesc->mEffects;
580 for (size_t i = 0; i < fxVector.size(); i++) {
581 sp<AudioEffect> fx = fxVector.itemAt(i);
582 fx->setEnabled(enabled);
583 }
584}
585
Mathias Agopian65ab4712010-07-14 17:59:35 -0700586status_t AudioPolicyService::onTransact(
587 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
588{
589 return BnAudioPolicyService::onTransact(code, data, reply, flags);
590}
591
592
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593// ----------- AudioPolicyService::AudioCommandThread implementation ----------
594
595AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name)
596 : Thread(false), mName(name)
597{
598 mpToneGenerator = NULL;
599}
600
601
602AudioPolicyService::AudioCommandThread::~AudioCommandThread()
603{
604 if (mName != "" && !mAudioCommands.isEmpty()) {
605 release_wake_lock(mName.string());
606 }
607 mAudioCommands.clear();
608 if (mpToneGenerator != NULL) delete mpToneGenerator;
609}
610
611void AudioPolicyService::AudioCommandThread::onFirstRef()
612{
613 if (mName != "") {
614 run(mName.string(), ANDROID_PRIORITY_AUDIO);
615 } else {
616 run("AudioCommandThread", ANDROID_PRIORITY_AUDIO);
617 }
618}
619
620bool AudioPolicyService::AudioCommandThread::threadLoop()
621{
622 nsecs_t waitTime = INT64_MAX;
623
624 mLock.lock();
625 while (!exitPending())
626 {
627 while(!mAudioCommands.isEmpty()) {
628 nsecs_t curTime = systemTime();
629 // commands are sorted by increasing time stamp: execute them from index 0 and up
630 if (mAudioCommands[0]->mTime <= curTime) {
631 AudioCommand *command = mAudioCommands[0];
632 mAudioCommands.removeAt(0);
633 mLastCommand = *command;
634
635 switch (command->mCommand) {
636 case START_TONE: {
637 mLock.unlock();
638 ToneData *data = (ToneData *)command->mParam;
639 LOGV("AudioCommandThread() processing start tone %d on stream %d",
640 data->mType, data->mStream);
641 if (mpToneGenerator != NULL)
642 delete mpToneGenerator;
643 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
644 mpToneGenerator->startTone(data->mType);
645 delete data;
646 mLock.lock();
647 }break;
648 case STOP_TONE: {
649 mLock.unlock();
650 LOGV("AudioCommandThread() processing stop tone");
651 if (mpToneGenerator != NULL) {
652 mpToneGenerator->stopTone();
653 delete mpToneGenerator;
654 mpToneGenerator = NULL;
655 }
656 mLock.lock();
657 }break;
658 case SET_VOLUME: {
659 VolumeData *data = (VolumeData *)command->mParam;
Eric Laurentde070132010-07-13 04:45:46 -0700660 LOGV("AudioCommandThread() processing set volume stream %d, \
661 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
662 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
663 data->mVolume,
664 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700665 if (command->mWaitStatus) {
666 command->mCond.signal();
667 mWaitWorkCV.wait(mLock);
668 }
669 delete data;
670 }break;
671 case SET_PARAMETERS: {
672 ParametersData *data = (ParametersData *)command->mParam;
Eric Laurentde070132010-07-13 04:45:46 -0700673 LOGV("AudioCommandThread() processing set parameters string %s, io %d",
674 data->mKeyValuePairs.string(), data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700675 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
676 if (command->mWaitStatus) {
677 command->mCond.signal();
678 mWaitWorkCV.wait(mLock);
679 }
680 delete data;
681 }break;
682 case SET_VOICE_VOLUME: {
683 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Eric Laurentde070132010-07-13 04:45:46 -0700684 LOGV("AudioCommandThread() processing set voice volume volume %f",
685 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700686 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
687 if (command->mWaitStatus) {
688 command->mCond.signal();
689 mWaitWorkCV.wait(mLock);
690 }
691 delete data;
692 }break;
693 default:
694 LOGW("AudioCommandThread() unknown command %d", command->mCommand);
695 }
696 delete command;
697 waitTime = INT64_MAX;
698 } else {
699 waitTime = mAudioCommands[0]->mTime - curTime;
700 break;
701 }
702 }
703 // release delayed commands wake lock
704 if (mName != "" && mAudioCommands.isEmpty()) {
705 release_wake_lock(mName.string());
706 }
707 LOGV("AudioCommandThread() going to sleep");
708 mWaitWorkCV.waitRelative(mLock, waitTime);
709 LOGV("AudioCommandThread() waking up");
710 }
711 mLock.unlock();
712 return false;
713}
714
715status_t AudioPolicyService::AudioCommandThread::dump(int fd)
716{
717 const size_t SIZE = 256;
718 char buffer[SIZE];
719 String8 result;
720
721 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
722 result.append(buffer);
723 write(fd, result.string(), result.size());
724
725 bool locked = tryLock(mLock);
726 if (!locked) {
727 String8 result2(kCmdDeadlockedString);
728 write(fd, result2.string(), result2.size());
729 }
730
731 snprintf(buffer, SIZE, "- Commands:\n");
732 result = String8(buffer);
733 result.append(" Command Time Wait pParam\n");
734 for (int i = 0; i < (int)mAudioCommands.size(); i++) {
735 mAudioCommands[i]->dump(buffer, SIZE);
736 result.append(buffer);
737 }
738 result.append(" Last Command\n");
739 mLastCommand.dump(buffer, SIZE);
740 result.append(buffer);
741
742 write(fd, result.string(), result.size());
743
744 if (locked) mLock.unlock();
745
746 return NO_ERROR;
747}
748
749void AudioPolicyService::AudioCommandThread::startToneCommand(int type, int stream)
750{
751 AudioCommand *command = new AudioCommand();
752 command->mCommand = START_TONE;
753 ToneData *data = new ToneData();
754 data->mType = type;
755 data->mStream = stream;
756 command->mParam = (void *)data;
757 command->mWaitStatus = false;
758 Mutex::Autolock _l(mLock);
759 insertCommand_l(command);
760 LOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
761 mWaitWorkCV.signal();
762}
763
764void AudioPolicyService::AudioCommandThread::stopToneCommand()
765{
766 AudioCommand *command = new AudioCommand();
767 command->mCommand = STOP_TONE;
768 command->mParam = NULL;
769 command->mWaitStatus = false;
770 Mutex::Autolock _l(mLock);
771 insertCommand_l(command);
772 LOGV("AudioCommandThread() adding tone stop");
773 mWaitWorkCV.signal();
774}
775
Eric Laurentde070132010-07-13 04:45:46 -0700776status_t AudioPolicyService::AudioCommandThread::volumeCommand(int stream,
777 float volume,
778 int output,
779 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700780{
781 status_t status = NO_ERROR;
782
783 AudioCommand *command = new AudioCommand();
784 command->mCommand = SET_VOLUME;
785 VolumeData *data = new VolumeData();
786 data->mStream = stream;
787 data->mVolume = volume;
788 data->mIO = output;
789 command->mParam = data;
790 if (delayMs == 0) {
791 command->mWaitStatus = true;
792 } else {
793 command->mWaitStatus = false;
794 }
795 Mutex::Autolock _l(mLock);
796 insertCommand_l(command, delayMs);
Eric Laurentde070132010-07-13 04:45:46 -0700797 LOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
798 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700799 mWaitWorkCV.signal();
800 if (command->mWaitStatus) {
801 command->mCond.wait(mLock);
802 status = command->mStatus;
803 mWaitWorkCV.signal();
804 }
805 return status;
806}
807
Eric Laurentde070132010-07-13 04:45:46 -0700808status_t AudioPolicyService::AudioCommandThread::parametersCommand(int ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700809 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700810 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700811{
812 status_t status = NO_ERROR;
813
814 AudioCommand *command = new AudioCommand();
815 command->mCommand = SET_PARAMETERS;
816 ParametersData *data = new ParametersData();
817 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700818 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700819 command->mParam = data;
820 if (delayMs == 0) {
821 command->mWaitStatus = true;
822 } else {
823 command->mWaitStatus = false;
824 }
825 Mutex::Autolock _l(mLock);
826 insertCommand_l(command, delayMs);
Eric Laurentde070132010-07-13 04:45:46 -0700827 LOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700828 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700829 mWaitWorkCV.signal();
830 if (command->mWaitStatus) {
831 command->mCond.wait(mLock);
832 status = command->mStatus;
833 mWaitWorkCV.signal();
834 }
835 return status;
836}
837
838status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
839{
840 status_t status = NO_ERROR;
841
842 AudioCommand *command = new AudioCommand();
843 command->mCommand = SET_VOICE_VOLUME;
844 VoiceVolumeData *data = new VoiceVolumeData();
845 data->mVolume = volume;
846 command->mParam = data;
847 if (delayMs == 0) {
848 command->mWaitStatus = true;
849 } else {
850 command->mWaitStatus = false;
851 }
852 Mutex::Autolock _l(mLock);
853 insertCommand_l(command, delayMs);
854 LOGV("AudioCommandThread() adding set voice volume volume %f", volume);
855 mWaitWorkCV.signal();
856 if (command->mWaitStatus) {
857 command->mCond.wait(mLock);
858 status = command->mStatus;
859 mWaitWorkCV.signal();
860 }
861 return status;
862}
863
864// insertCommand_l() must be called with mLock held
865void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
866{
867 ssize_t i;
868 Vector <AudioCommand *> removedCommands;
869
870 command->mTime = systemTime() + milliseconds(delayMs);
871
872 // acquire wake lock to make sure delayed commands are processed
873 if (mName != "" && mAudioCommands.isEmpty()) {
874 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
875 }
876
877 // check same pending commands with later time stamps and eliminate them
878 for (i = mAudioCommands.size()-1; i >= 0; i--) {
879 AudioCommand *command2 = mAudioCommands[i];
880 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
881 if (command2->mTime <= command->mTime) break;
882 if (command2->mCommand != command->mCommand) continue;
883
884 switch (command->mCommand) {
885 case SET_PARAMETERS: {
886 ParametersData *data = (ParametersData *)command->mParam;
887 ParametersData *data2 = (ParametersData *)command2->mParam;
888 if (data->mIO != data2->mIO) break;
Eric Laurentde070132010-07-13 04:45:46 -0700889 LOGV("Comparing parameter command %s to new command %s",
890 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700891 AudioParameter param = AudioParameter(data->mKeyValuePairs);
892 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
893 for (size_t j = 0; j < param.size(); j++) {
894 String8 key;
895 String8 value;
896 param.getAt(j, key, value);
897 for (size_t k = 0; k < param2.size(); k++) {
898 String8 key2;
899 String8 value2;
900 param2.getAt(k, key2, value2);
901 if (key2 == key) {
902 param2.remove(key2);
903 LOGV("Filtering out parameter %s", key2.string());
904 break;
905 }
906 }
907 }
908 // if all keys have been filtered out, remove the command.
909 // otherwise, update the key value pairs
910 if (param2.size() == 0) {
911 removedCommands.add(command2);
912 } else {
913 data2->mKeyValuePairs = param2.toString();
914 }
915 } break;
916
917 case SET_VOLUME: {
918 VolumeData *data = (VolumeData *)command->mParam;
919 VolumeData *data2 = (VolumeData *)command2->mParam;
920 if (data->mIO != data2->mIO) break;
921 if (data->mStream != data2->mStream) break;
Eric Laurentde070132010-07-13 04:45:46 -0700922 LOGV("Filtering out volume command on output %d for stream %d",
923 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700924 removedCommands.add(command2);
925 } break;
926 case START_TONE:
927 case STOP_TONE:
928 default:
929 break;
930 }
931 }
932
933 // remove filtered commands
934 for (size_t j = 0; j < removedCommands.size(); j++) {
935 // removed commands always have time stamps greater than current command
936 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
937 if (mAudioCommands[k] == removedCommands[j]) {
938 LOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
939 mAudioCommands.removeAt(k);
940 break;
941 }
942 }
943 }
944 removedCommands.clear();
945
946 // insert command at the right place according to its time stamp
Eric Laurentde070132010-07-13 04:45:46 -0700947 LOGV("inserting command: %d at index %d, num commands %d",
948 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700949 mAudioCommands.insertAt(command, i + 1);
950}
951
952void AudioPolicyService::AudioCommandThread::exit()
953{
954 LOGV("AudioCommandThread::exit");
955 {
956 AutoMutex _l(mLock);
957 requestExit();
958 mWaitWorkCV.signal();
959 }
960 requestExitAndWait();
961}
962
963void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
964{
965 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
966 mCommand,
967 (int)ns2s(mTime),
968 (int)ns2ms(mTime)%1000,
969 mWaitStatus,
970 mParam);
971}
972
Dima Zavinfce7a472011-04-19 22:30:36 -0700973/******* helpers for the service_ops callbacks defined below *********/
974void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
975 const char *keyValuePairs,
976 int delayMs)
977{
978 mAudioCommandThread->parametersCommand((int)ioHandle, keyValuePairs,
979 delayMs);
980}
981
982int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
983 float volume,
984 audio_io_handle_t output,
985 int delayMs)
986{
987 return (int)mAudioCommandThread->volumeCommand((int)stream, volume,
988 (int)output, delayMs);
989}
990
991int AudioPolicyService::startTone(audio_policy_tone_t tone,
992 audio_stream_type_t stream)
993{
994 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
995 LOGE("startTone: illegal tone requested (%d)", tone);
996 if (stream != AUDIO_STREAM_VOICE_CALL)
997 LOGE("startTone: illegal stream (%d) requested for tone %d", stream,
998 tone);
999 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1000 AUDIO_STREAM_VOICE_CALL);
1001 return 0;
1002}
1003
1004int AudioPolicyService::stopTone()
1005{
1006 mTonePlaybackThread->stopToneCommand();
1007 return 0;
1008}
1009
1010int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1011{
1012 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1013}
1014
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001015// ----------------------------------------------------------------------------
1016// Audio pre-processing configuration
1017// ----------------------------------------------------------------------------
1018
1019const char *AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
1020 MIC_SRC_TAG,
1021 VOICE_UL_SRC_TAG,
1022 VOICE_DL_SRC_TAG,
1023 VOICE_CALL_SRC_TAG,
1024 CAMCORDER_SRC_TAG,
1025 VOICE_REC_SRC_TAG,
1026 VOICE_COMM_SRC_TAG
1027};
1028
1029// returns the audio_source_t enum corresponding to the input source name or
1030// AUDIO_SOURCE_CNT is no match found
1031audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
1032{
1033 int i;
1034 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
1035 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
1036 LOGV("inputSourceNameToEnum found source %s %d", name, i);
1037 break;
1038 }
1039 }
1040 return (audio_source_t)i;
1041}
1042
1043size_t AudioPolicyService::growParamSize(char *param,
1044 size_t size,
1045 size_t *curSize,
1046 size_t *totSize)
1047{
1048 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
1049 size_t pos = ((*curSize - 1 ) / size + 1) * size;
1050
1051 if (pos + size > *totSize) {
1052 while (pos + size > *totSize) {
1053 *totSize += ((*totSize + 7) / 8) * 4;
1054 }
1055 param = (char *)realloc(param, *totSize);
1056 }
1057 *curSize = pos + size;
1058 return pos;
1059}
1060
1061size_t AudioPolicyService::readParamValue(cnode *node,
1062 char *param,
1063 size_t *curSize,
1064 size_t *totSize)
1065{
1066 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
1067 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
1068 *(short *)((char *)param + pos) = (short)atoi(node->value);
1069 LOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
1070 return sizeof(short);
1071 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
1072 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
1073 *(int *)((char *)param + pos) = atoi(node->value);
1074 LOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
1075 return sizeof(int);
1076 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
1077 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
1078 *(float *)((char *)param + pos) = (float)atof(node->value);
1079 LOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
1080 return sizeof(float);
1081 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
1082 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
1083 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1084 *(bool *)((char *)param + pos) = false;
1085 } else {
1086 *(bool *)((char *)param + pos) = true;
1087 }
1088 LOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
1089 return sizeof(bool);
1090 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1091 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1092 if (*curSize + len + 1 > *totSize) {
1093 *totSize = *curSize + len + 1;
1094 param = (char *)realloc(param, *totSize);
1095 }
1096 strncpy(param + *curSize, node->value, len);
1097 *curSize += len;
1098 param[*curSize] = '\0';
1099 LOGV("readParamValue() reading string %s", param + *curSize - len);
1100 return len;
1101 }
1102 LOGW("readParamValue() unknown param type %s", node->name);
1103 return 0;
1104}
1105
1106effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1107{
1108 cnode *param;
1109 cnode *value;
1110 size_t curSize = sizeof(effect_param_t);
1111 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1112 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1113
1114 param = config_find(root, PARAM_TAG);
1115 value = config_find(root, VALUE_TAG);
1116 if (param == NULL && value == NULL) {
1117 // try to parse simple parameter form {int int}
1118 param = root->first_child;
1119 if (param) {
1120 // Note: that a pair of random strings is read as 0 0
1121 int *ptr = (int *)fx_param->data;
1122 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
1123 LOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
1124 *ptr++ = atoi(param->name);
1125 *ptr = atoi(param->value);
1126 fx_param->psize = sizeof(int);
1127 fx_param->vsize = sizeof(int);
1128 return fx_param;
1129 }
1130 }
1131 if (param == NULL || value == NULL) {
1132 LOGW("loadEffectParameter() invalid parameter description %s", root->name);
1133 goto error;
1134 }
1135
1136 fx_param->psize = 0;
1137 param = param->first_child;
1138 while (param) {
1139 LOGV("loadEffectParameter() reading param of type %s", param->name);
1140 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1141 if (size == 0) {
1142 goto error;
1143 }
1144 fx_param->psize += size;
1145 param = param->next;
1146 }
1147
1148 // align start of value field on 32 bit boundary
1149 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1150
1151 fx_param->vsize = 0;
1152 value = value->first_child;
1153 while (value) {
1154 LOGV("loadEffectParameter() reading value of type %s", value->name);
1155 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1156 if (size == 0) {
1157 goto error;
1158 }
1159 fx_param->vsize += size;
1160 value = value->next;
1161 }
1162
1163 return fx_param;
1164
1165error:
1166 delete fx_param;
1167 return NULL;
1168}
1169
1170void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1171{
1172 cnode *node = root->first_child;
1173 while (node) {
1174 LOGV("loadEffectParameters() loading param %s", node->name);
1175 effect_param_t *param = loadEffectParameter(node);
1176 if (param == NULL) {
1177 node = node->next;
1178 continue;
1179 }
1180 params.add(param);
1181 node = node->next;
1182 }
1183}
1184
1185AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1186 cnode *root,
1187 const Vector <EffectDesc *>& effects)
1188{
1189 cnode *node = root->first_child;
1190 if (node == NULL) {
1191 LOGW("loadInputSource() empty element %s", root->name);
1192 return NULL;
1193 }
1194 InputSourceDesc *source = new InputSourceDesc();
1195 while (node) {
1196 size_t i;
1197 for (i = 0; i < effects.size(); i++) {
1198 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
1199 LOGV("loadInputSource() found effect %s in list", node->name);
1200 break;
1201 }
1202 }
1203 if (i == effects.size()) {
1204 LOGV("loadInputSource() effect %s not in list", node->name);
1205 node = node->next;
1206 continue;
1207 }
1208 EffectDesc *effect = new EffectDesc(*effects[i]);
1209 loadEffectParameters(node, effect->mParams);
1210 LOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
1211 source->mEffects.add(effect);
1212 node = node->next;
1213 }
1214 if (source->mEffects.size() == 0) {
1215 LOGW("loadInputSource() no valid effects found in source %s", root->name);
1216 delete source;
1217 return NULL;
1218 }
1219 return source;
1220}
1221
1222status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1223{
1224 cnode *node = config_find(root, PREPROCESSING_TAG);
1225 if (node == NULL) {
1226 return -ENOENT;
1227 }
1228 node = node->first_child;
1229 while (node) {
1230 audio_source_t source = inputSourceNameToEnum(node->name);
1231 if (source == AUDIO_SOURCE_CNT) {
1232 LOGW("loadInputSources() invalid input source %s", node->name);
1233 node = node->next;
1234 continue;
1235 }
1236 LOGV("loadInputSources() loading input source %s", node->name);
1237 InputSourceDesc *desc = loadInputSource(node, effects);
1238 if (desc == NULL) {
1239 node = node->next;
1240 continue;
1241 }
1242 mInputSources.add(source, desc);
1243 node = node->next;
1244 }
1245 return NO_ERROR;
1246}
1247
1248AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1249{
1250 cnode *node = config_find(root, UUID_TAG);
1251 if (node == NULL) {
1252 return NULL;
1253 }
1254 effect_uuid_t uuid;
1255 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
1256 LOGW("loadEffect() invalid uuid %s", node->value);
1257 return NULL;
1258 }
1259 EffectDesc *effect = new EffectDesc();
1260 effect->mName = strdup(root->name);
1261 memcpy(&effect->mUuid, &uuid, sizeof(effect_uuid_t));
1262
1263 return effect;
1264}
1265
1266status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1267{
1268 cnode *node = config_find(root, EFFECTS_TAG);
1269 if (node == NULL) {
1270 return -ENOENT;
1271 }
1272 node = node->first_child;
1273 while (node) {
1274 LOGV("loadEffects() loading effect %s", node->name);
1275 EffectDesc *effect = loadEffect(node);
1276 if (effect == NULL) {
1277 node = node->next;
1278 continue;
1279 }
1280 effects.add(effect);
1281 node = node->next;
1282 }
1283 return NO_ERROR;
1284}
1285
1286status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1287{
1288 cnode *root;
1289 char *data;
1290
1291 data = (char *)load_file(path, NULL);
1292 if (data == NULL) {
1293 return -ENODEV;
1294 }
1295 root = config_node("", "");
1296 config_load(root, data);
1297
1298 Vector <EffectDesc *> effects;
1299 loadEffects(root, effects);
1300 loadInputSources(root, effects);
1301
1302 config_free(root);
1303 free(root);
1304 free(data);
1305
1306 return NO_ERROR;
1307}
1308
Dima Zavinfce7a472011-04-19 22:30:36 -07001309/* implementation of the interface to the policy manager */
1310extern "C" {
1311
1312static audio_io_handle_t aps_open_output(void *service,
1313 uint32_t *pDevices,
1314 uint32_t *pSamplingRate,
1315 uint32_t *pFormat,
1316 uint32_t *pChannels,
1317 uint32_t *pLatencyMs,
1318 audio_policy_output_flags_t flags)
1319{
1320 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1321 if (af == NULL) {
1322 LOGW("%s: could not get AudioFlinger", __func__);
1323 return 0;
1324 }
1325
1326 return af->openOutput(pDevices, pSamplingRate, pFormat, pChannels,
1327 pLatencyMs, flags);
1328}
1329
1330static audio_io_handle_t aps_open_dup_output(void *service,
1331 audio_io_handle_t output1,
1332 audio_io_handle_t output2)
1333{
1334 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1335 if (af == NULL) {
1336 LOGW("%s: could not get AudioFlinger", __func__);
1337 return 0;
1338 }
1339 return af->openDuplicateOutput(output1, output2);
1340}
1341
1342static int aps_close_output(void *service, audio_io_handle_t output)
1343{
1344 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1345 if (af == NULL)
1346 return PERMISSION_DENIED;
1347
1348 return af->closeOutput(output);
1349}
1350
1351static int aps_suspend_output(void *service, audio_io_handle_t output)
1352{
1353 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1354 if (af == NULL) {
1355 LOGW("%s: could not get AudioFlinger", __func__);
1356 return PERMISSION_DENIED;
1357 }
1358
1359 return af->suspendOutput(output);
1360}
1361
1362static int aps_restore_output(void *service, audio_io_handle_t output)
1363{
1364 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1365 if (af == NULL) {
1366 LOGW("%s: could not get AudioFlinger", __func__);
1367 return PERMISSION_DENIED;
1368 }
1369
1370 return af->restoreOutput(output);
1371}
1372
1373static audio_io_handle_t aps_open_input(void *service,
1374 uint32_t *pDevices,
1375 uint32_t *pSamplingRate,
1376 uint32_t *pFormat,
1377 uint32_t *pChannels,
1378 uint32_t acoustics)
1379{
1380 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1381 if (af == NULL) {
1382 LOGW("%s: could not get AudioFlinger", __func__);
1383 return 0;
1384 }
1385
1386 return af->openInput(pDevices, pSamplingRate, pFormat, pChannels,
1387 acoustics);
1388}
1389
1390static int aps_close_input(void *service, audio_io_handle_t input)
1391{
1392 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1393 if (af == NULL)
1394 return PERMISSION_DENIED;
1395
1396 return af->closeInput(input);
1397}
1398
1399static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1400 audio_io_handle_t output)
1401{
1402 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1403 if (af == NULL)
1404 return PERMISSION_DENIED;
1405
1406 return af->setStreamOutput(stream, output);
1407}
1408
1409static int aps_move_effects(void *service, int session,
1410 audio_io_handle_t src_output,
1411 audio_io_handle_t dst_output)
1412{
1413 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1414 if (af == NULL)
1415 return PERMISSION_DENIED;
1416
1417 return af->moveEffects(session, (int)src_output, (int)dst_output);
1418}
1419
1420static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1421 const char *keys)
1422{
1423 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1424 return strdup(result.string());
1425}
1426
1427static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1428 const char *kv_pairs, int delay_ms)
1429{
1430 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1431
1432 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1433}
1434
1435static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1436 float volume, audio_io_handle_t output,
1437 int delay_ms)
1438{
1439 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1440
1441 return audioPolicyService->setStreamVolume(stream, volume, output,
1442 delay_ms);
1443}
1444
1445static int aps_start_tone(void *service, audio_policy_tone_t tone,
1446 audio_stream_type_t stream)
1447{
1448 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1449
1450 return audioPolicyService->startTone(tone, stream);
1451}
1452
1453static int aps_stop_tone(void *service)
1454{
1455 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1456
1457 return audioPolicyService->stopTone();
1458}
1459
1460static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1461{
1462 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1463
1464 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1465}
1466
1467}; // extern "C"
1468
1469namespace {
1470 struct audio_policy_service_ops aps_ops = {
1471 open_output : aps_open_output,
1472 open_duplicate_output : aps_open_dup_output,
1473 close_output : aps_close_output,
1474 suspend_output : aps_suspend_output,
1475 restore_output : aps_restore_output,
1476 open_input : aps_open_input,
1477 close_input : aps_close_input,
1478 set_stream_volume : aps_set_stream_volume,
1479 set_stream_output : aps_set_stream_output,
1480 set_parameters : aps_set_parameters,
1481 get_parameters : aps_get_parameters,
1482 start_tone : aps_start_tone,
1483 stop_tone : aps_stop_tone,
1484 set_voice_volume : aps_set_voice_volume,
1485 move_effects : aps_move_effects,
1486 };
1487}; // namespace <unnamed>
1488
Mathias Agopian65ab4712010-07-14 17:59:35 -07001489}; // namespace android