blob: 6d06d83fab12c2afd244522270d554a861ab1f85 [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
Eric Laurent57dae992011-07-24 13:36:09 -0700500status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
501 effect_descriptor_t *descriptors,
502 uint32_t *count)
503{
504
505 if (mpAudioPolicy == NULL) {
506 *count = 0;
507 return NO_INIT;
508 }
509 Mutex::Autolock _l(mLock);
510 status_t status = NO_ERROR;
511
512 size_t index;
513 for (index = 0; index < mInputs.size(); index++) {
514 if (mInputs.valueAt(index)->mSessionId == audioSession) {
515 break;
516 }
517 }
518 if (index == mInputs.size()) {
519 *count = 0;
520 return BAD_VALUE;
521 }
522 Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
523
524 for (size_t i = 0; i < effects.size(); i++) {
525 effect_descriptor_t desc = effects[i]->descriptor();
526 if (i < *count) {
527 memcpy(descriptors + i, &desc, sizeof(effect_descriptor_t));
528 }
529 }
530 if (effects.size() > *count) {
531 status = NO_MEMORY;
532 }
533 *count = effects.size();
534 return status;
535}
536
Mathias Agopian65ab4712010-07-14 17:59:35 -0700537void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Eric Laurentde070132010-07-13 04:45:46 -0700538 LOGW("binderDied() %p, tid %d, calling tid %d", who.unsafe_get(), gettid(),
539 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700540}
541
542static bool tryLock(Mutex& mutex)
543{
544 bool locked = false;
545 for (int i = 0; i < kDumpLockRetries; ++i) {
546 if (mutex.tryLock() == NO_ERROR) {
547 locked = true;
548 break;
549 }
550 usleep(kDumpLockSleep);
551 }
552 return locked;
553}
554
555status_t AudioPolicyService::dumpInternals(int fd)
556{
557 const size_t SIZE = 256;
558 char buffer[SIZE];
559 String8 result;
560
Dima Zavinfce7a472011-04-19 22:30:36 -0700561 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700562 result.append(buffer);
563 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
564 result.append(buffer);
565 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
566 result.append(buffer);
567
568 write(fd, result.string(), result.size());
569 return NO_ERROR;
570}
571
572status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
573{
574 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
575 dumpPermissionDenial(fd);
576 } else {
577 bool locked = tryLock(mLock);
578 if (!locked) {
579 String8 result(kDeadlockedString);
580 write(fd, result.string(), result.size());
581 }
582
583 dumpInternals(fd);
584 if (mAudioCommandThread != NULL) {
585 mAudioCommandThread->dump(fd);
586 }
587 if (mTonePlaybackThread != NULL) {
588 mTonePlaybackThread->dump(fd);
589 }
590
Dima Zavinfce7a472011-04-19 22:30:36 -0700591 if (mpAudioPolicy) {
592 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700593 }
594
595 if (locked) mLock.unlock();
596 }
597 return NO_ERROR;
598}
599
600status_t AudioPolicyService::dumpPermissionDenial(int fd)
601{
602 const size_t SIZE = 256;
603 char buffer[SIZE];
604 String8 result;
605 snprintf(buffer, SIZE, "Permission Denial: "
606 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
607 IPCThreadState::self()->getCallingPid(),
608 IPCThreadState::self()->getCallingUid());
609 result.append(buffer);
610 write(fd, result.string(), result.size());
611 return NO_ERROR;
612}
613
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700614void AudioPolicyService::setPreProcessorEnabled(InputDesc *inputDesc, bool enabled)
615{
616 Vector<sp<AudioEffect> > fxVector = inputDesc->mEffects;
617 for (size_t i = 0; i < fxVector.size(); i++) {
618 sp<AudioEffect> fx = fxVector.itemAt(i);
619 fx->setEnabled(enabled);
620 }
621}
622
Mathias Agopian65ab4712010-07-14 17:59:35 -0700623status_t AudioPolicyService::onTransact(
624 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
625{
626 return BnAudioPolicyService::onTransact(code, data, reply, flags);
627}
628
629
Mathias Agopian65ab4712010-07-14 17:59:35 -0700630// ----------- AudioPolicyService::AudioCommandThread implementation ----------
631
632AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name)
633 : Thread(false), mName(name)
634{
635 mpToneGenerator = NULL;
636}
637
638
639AudioPolicyService::AudioCommandThread::~AudioCommandThread()
640{
641 if (mName != "" && !mAudioCommands.isEmpty()) {
642 release_wake_lock(mName.string());
643 }
644 mAudioCommands.clear();
645 if (mpToneGenerator != NULL) delete mpToneGenerator;
646}
647
648void AudioPolicyService::AudioCommandThread::onFirstRef()
649{
650 if (mName != "") {
651 run(mName.string(), ANDROID_PRIORITY_AUDIO);
652 } else {
653 run("AudioCommandThread", ANDROID_PRIORITY_AUDIO);
654 }
655}
656
657bool AudioPolicyService::AudioCommandThread::threadLoop()
658{
659 nsecs_t waitTime = INT64_MAX;
660
661 mLock.lock();
662 while (!exitPending())
663 {
664 while(!mAudioCommands.isEmpty()) {
665 nsecs_t curTime = systemTime();
666 // commands are sorted by increasing time stamp: execute them from index 0 and up
667 if (mAudioCommands[0]->mTime <= curTime) {
668 AudioCommand *command = mAudioCommands[0];
669 mAudioCommands.removeAt(0);
670 mLastCommand = *command;
671
672 switch (command->mCommand) {
673 case START_TONE: {
674 mLock.unlock();
675 ToneData *data = (ToneData *)command->mParam;
676 LOGV("AudioCommandThread() processing start tone %d on stream %d",
677 data->mType, data->mStream);
678 if (mpToneGenerator != NULL)
679 delete mpToneGenerator;
680 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
681 mpToneGenerator->startTone(data->mType);
682 delete data;
683 mLock.lock();
684 }break;
685 case STOP_TONE: {
686 mLock.unlock();
687 LOGV("AudioCommandThread() processing stop tone");
688 if (mpToneGenerator != NULL) {
689 mpToneGenerator->stopTone();
690 delete mpToneGenerator;
691 mpToneGenerator = NULL;
692 }
693 mLock.lock();
694 }break;
695 case SET_VOLUME: {
696 VolumeData *data = (VolumeData *)command->mParam;
Eric Laurentde070132010-07-13 04:45:46 -0700697 LOGV("AudioCommandThread() processing set volume stream %d, \
698 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
699 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
700 data->mVolume,
701 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700702 if (command->mWaitStatus) {
703 command->mCond.signal();
704 mWaitWorkCV.wait(mLock);
705 }
706 delete data;
707 }break;
708 case SET_PARAMETERS: {
709 ParametersData *data = (ParametersData *)command->mParam;
Eric Laurentde070132010-07-13 04:45:46 -0700710 LOGV("AudioCommandThread() processing set parameters string %s, io %d",
711 data->mKeyValuePairs.string(), data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700712 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
713 if (command->mWaitStatus) {
714 command->mCond.signal();
715 mWaitWorkCV.wait(mLock);
716 }
717 delete data;
718 }break;
719 case SET_VOICE_VOLUME: {
720 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Eric Laurentde070132010-07-13 04:45:46 -0700721 LOGV("AudioCommandThread() processing set voice volume volume %f",
722 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700723 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
724 if (command->mWaitStatus) {
725 command->mCond.signal();
726 mWaitWorkCV.wait(mLock);
727 }
728 delete data;
729 }break;
730 default:
731 LOGW("AudioCommandThread() unknown command %d", command->mCommand);
732 }
733 delete command;
734 waitTime = INT64_MAX;
735 } else {
736 waitTime = mAudioCommands[0]->mTime - curTime;
737 break;
738 }
739 }
740 // release delayed commands wake lock
741 if (mName != "" && mAudioCommands.isEmpty()) {
742 release_wake_lock(mName.string());
743 }
744 LOGV("AudioCommandThread() going to sleep");
745 mWaitWorkCV.waitRelative(mLock, waitTime);
746 LOGV("AudioCommandThread() waking up");
747 }
748 mLock.unlock();
749 return false;
750}
751
752status_t AudioPolicyService::AudioCommandThread::dump(int fd)
753{
754 const size_t SIZE = 256;
755 char buffer[SIZE];
756 String8 result;
757
758 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
759 result.append(buffer);
760 write(fd, result.string(), result.size());
761
762 bool locked = tryLock(mLock);
763 if (!locked) {
764 String8 result2(kCmdDeadlockedString);
765 write(fd, result2.string(), result2.size());
766 }
767
768 snprintf(buffer, SIZE, "- Commands:\n");
769 result = String8(buffer);
770 result.append(" Command Time Wait pParam\n");
771 for (int i = 0; i < (int)mAudioCommands.size(); i++) {
772 mAudioCommands[i]->dump(buffer, SIZE);
773 result.append(buffer);
774 }
775 result.append(" Last Command\n");
776 mLastCommand.dump(buffer, SIZE);
777 result.append(buffer);
778
779 write(fd, result.string(), result.size());
780
781 if (locked) mLock.unlock();
782
783 return NO_ERROR;
784}
785
786void AudioPolicyService::AudioCommandThread::startToneCommand(int type, int stream)
787{
788 AudioCommand *command = new AudioCommand();
789 command->mCommand = START_TONE;
790 ToneData *data = new ToneData();
791 data->mType = type;
792 data->mStream = stream;
793 command->mParam = (void *)data;
794 command->mWaitStatus = false;
795 Mutex::Autolock _l(mLock);
796 insertCommand_l(command);
797 LOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
798 mWaitWorkCV.signal();
799}
800
801void AudioPolicyService::AudioCommandThread::stopToneCommand()
802{
803 AudioCommand *command = new AudioCommand();
804 command->mCommand = STOP_TONE;
805 command->mParam = NULL;
806 command->mWaitStatus = false;
807 Mutex::Autolock _l(mLock);
808 insertCommand_l(command);
809 LOGV("AudioCommandThread() adding tone stop");
810 mWaitWorkCV.signal();
811}
812
Eric Laurentde070132010-07-13 04:45:46 -0700813status_t AudioPolicyService::AudioCommandThread::volumeCommand(int stream,
814 float volume,
815 int output,
816 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700817{
818 status_t status = NO_ERROR;
819
820 AudioCommand *command = new AudioCommand();
821 command->mCommand = SET_VOLUME;
822 VolumeData *data = new VolumeData();
823 data->mStream = stream;
824 data->mVolume = volume;
825 data->mIO = output;
826 command->mParam = data;
827 if (delayMs == 0) {
828 command->mWaitStatus = true;
829 } else {
830 command->mWaitStatus = false;
831 }
832 Mutex::Autolock _l(mLock);
833 insertCommand_l(command, delayMs);
Eric Laurentde070132010-07-13 04:45:46 -0700834 LOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
835 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700836 mWaitWorkCV.signal();
837 if (command->mWaitStatus) {
838 command->mCond.wait(mLock);
839 status = command->mStatus;
840 mWaitWorkCV.signal();
841 }
842 return status;
843}
844
Eric Laurentde070132010-07-13 04:45:46 -0700845status_t AudioPolicyService::AudioCommandThread::parametersCommand(int ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700846 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700847 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700848{
849 status_t status = NO_ERROR;
850
851 AudioCommand *command = new AudioCommand();
852 command->mCommand = SET_PARAMETERS;
853 ParametersData *data = new ParametersData();
854 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700855 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700856 command->mParam = data;
857 if (delayMs == 0) {
858 command->mWaitStatus = true;
859 } else {
860 command->mWaitStatus = false;
861 }
862 Mutex::Autolock _l(mLock);
863 insertCommand_l(command, delayMs);
Eric Laurentde070132010-07-13 04:45:46 -0700864 LOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700865 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700866 mWaitWorkCV.signal();
867 if (command->mWaitStatus) {
868 command->mCond.wait(mLock);
869 status = command->mStatus;
870 mWaitWorkCV.signal();
871 }
872 return status;
873}
874
875status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
876{
877 status_t status = NO_ERROR;
878
879 AudioCommand *command = new AudioCommand();
880 command->mCommand = SET_VOICE_VOLUME;
881 VoiceVolumeData *data = new VoiceVolumeData();
882 data->mVolume = volume;
883 command->mParam = data;
884 if (delayMs == 0) {
885 command->mWaitStatus = true;
886 } else {
887 command->mWaitStatus = false;
888 }
889 Mutex::Autolock _l(mLock);
890 insertCommand_l(command, delayMs);
891 LOGV("AudioCommandThread() adding set voice volume volume %f", volume);
892 mWaitWorkCV.signal();
893 if (command->mWaitStatus) {
894 command->mCond.wait(mLock);
895 status = command->mStatus;
896 mWaitWorkCV.signal();
897 }
898 return status;
899}
900
901// insertCommand_l() must be called with mLock held
902void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
903{
904 ssize_t i;
905 Vector <AudioCommand *> removedCommands;
906
907 command->mTime = systemTime() + milliseconds(delayMs);
908
909 // acquire wake lock to make sure delayed commands are processed
910 if (mName != "" && mAudioCommands.isEmpty()) {
911 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
912 }
913
914 // check same pending commands with later time stamps and eliminate them
915 for (i = mAudioCommands.size()-1; i >= 0; i--) {
916 AudioCommand *command2 = mAudioCommands[i];
917 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
918 if (command2->mTime <= command->mTime) break;
919 if (command2->mCommand != command->mCommand) continue;
920
921 switch (command->mCommand) {
922 case SET_PARAMETERS: {
923 ParametersData *data = (ParametersData *)command->mParam;
924 ParametersData *data2 = (ParametersData *)command2->mParam;
925 if (data->mIO != data2->mIO) break;
Eric Laurentde070132010-07-13 04:45:46 -0700926 LOGV("Comparing parameter command %s to new command %s",
927 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928 AudioParameter param = AudioParameter(data->mKeyValuePairs);
929 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
930 for (size_t j = 0; j < param.size(); j++) {
931 String8 key;
932 String8 value;
933 param.getAt(j, key, value);
934 for (size_t k = 0; k < param2.size(); k++) {
935 String8 key2;
936 String8 value2;
937 param2.getAt(k, key2, value2);
938 if (key2 == key) {
939 param2.remove(key2);
940 LOGV("Filtering out parameter %s", key2.string());
941 break;
942 }
943 }
944 }
945 // if all keys have been filtered out, remove the command.
946 // otherwise, update the key value pairs
947 if (param2.size() == 0) {
948 removedCommands.add(command2);
949 } else {
950 data2->mKeyValuePairs = param2.toString();
951 }
952 } break;
953
954 case SET_VOLUME: {
955 VolumeData *data = (VolumeData *)command->mParam;
956 VolumeData *data2 = (VolumeData *)command2->mParam;
957 if (data->mIO != data2->mIO) break;
958 if (data->mStream != data2->mStream) break;
Eric Laurentde070132010-07-13 04:45:46 -0700959 LOGV("Filtering out volume command on output %d for stream %d",
960 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700961 removedCommands.add(command2);
962 } break;
963 case START_TONE:
964 case STOP_TONE:
965 default:
966 break;
967 }
968 }
969
970 // remove filtered commands
971 for (size_t j = 0; j < removedCommands.size(); j++) {
972 // removed commands always have time stamps greater than current command
973 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
974 if (mAudioCommands[k] == removedCommands[j]) {
975 LOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
976 mAudioCommands.removeAt(k);
977 break;
978 }
979 }
980 }
981 removedCommands.clear();
982
983 // insert command at the right place according to its time stamp
Eric Laurentde070132010-07-13 04:45:46 -0700984 LOGV("inserting command: %d at index %d, num commands %d",
985 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700986 mAudioCommands.insertAt(command, i + 1);
987}
988
989void AudioPolicyService::AudioCommandThread::exit()
990{
991 LOGV("AudioCommandThread::exit");
992 {
993 AutoMutex _l(mLock);
994 requestExit();
995 mWaitWorkCV.signal();
996 }
997 requestExitAndWait();
998}
999
1000void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
1001{
1002 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
1003 mCommand,
1004 (int)ns2s(mTime),
1005 (int)ns2ms(mTime)%1000,
1006 mWaitStatus,
1007 mParam);
1008}
1009
Dima Zavinfce7a472011-04-19 22:30:36 -07001010/******* helpers for the service_ops callbacks defined below *********/
1011void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1012 const char *keyValuePairs,
1013 int delayMs)
1014{
1015 mAudioCommandThread->parametersCommand((int)ioHandle, keyValuePairs,
1016 delayMs);
1017}
1018
1019int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1020 float volume,
1021 audio_io_handle_t output,
1022 int delayMs)
1023{
1024 return (int)mAudioCommandThread->volumeCommand((int)stream, volume,
1025 (int)output, delayMs);
1026}
1027
1028int AudioPolicyService::startTone(audio_policy_tone_t tone,
1029 audio_stream_type_t stream)
1030{
1031 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
1032 LOGE("startTone: illegal tone requested (%d)", tone);
1033 if (stream != AUDIO_STREAM_VOICE_CALL)
1034 LOGE("startTone: illegal stream (%d) requested for tone %d", stream,
1035 tone);
1036 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1037 AUDIO_STREAM_VOICE_CALL);
1038 return 0;
1039}
1040
1041int AudioPolicyService::stopTone()
1042{
1043 mTonePlaybackThread->stopToneCommand();
1044 return 0;
1045}
1046
1047int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1048{
1049 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1050}
1051
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001052// ----------------------------------------------------------------------------
1053// Audio pre-processing configuration
1054// ----------------------------------------------------------------------------
1055
1056const char *AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
1057 MIC_SRC_TAG,
1058 VOICE_UL_SRC_TAG,
1059 VOICE_DL_SRC_TAG,
1060 VOICE_CALL_SRC_TAG,
1061 CAMCORDER_SRC_TAG,
1062 VOICE_REC_SRC_TAG,
1063 VOICE_COMM_SRC_TAG
1064};
1065
1066// returns the audio_source_t enum corresponding to the input source name or
1067// AUDIO_SOURCE_CNT is no match found
1068audio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
1069{
1070 int i;
1071 for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
1072 if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
1073 LOGV("inputSourceNameToEnum found source %s %d", name, i);
1074 break;
1075 }
1076 }
1077 return (audio_source_t)i;
1078}
1079
1080size_t AudioPolicyService::growParamSize(char *param,
1081 size_t size,
1082 size_t *curSize,
1083 size_t *totSize)
1084{
1085 // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
1086 size_t pos = ((*curSize - 1 ) / size + 1) * size;
1087
1088 if (pos + size > *totSize) {
1089 while (pos + size > *totSize) {
1090 *totSize += ((*totSize + 7) / 8) * 4;
1091 }
1092 param = (char *)realloc(param, *totSize);
1093 }
1094 *curSize = pos + size;
1095 return pos;
1096}
1097
1098size_t AudioPolicyService::readParamValue(cnode *node,
1099 char *param,
1100 size_t *curSize,
1101 size_t *totSize)
1102{
1103 if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
1104 size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
1105 *(short *)((char *)param + pos) = (short)atoi(node->value);
1106 LOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
1107 return sizeof(short);
1108 } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
1109 size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
1110 *(int *)((char *)param + pos) = atoi(node->value);
1111 LOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
1112 return sizeof(int);
1113 } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
1114 size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
1115 *(float *)((char *)param + pos) = (float)atof(node->value);
1116 LOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
1117 return sizeof(float);
1118 } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
1119 size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
1120 if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
1121 *(bool *)((char *)param + pos) = false;
1122 } else {
1123 *(bool *)((char *)param + pos) = true;
1124 }
1125 LOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
1126 return sizeof(bool);
1127 } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
1128 size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
1129 if (*curSize + len + 1 > *totSize) {
1130 *totSize = *curSize + len + 1;
1131 param = (char *)realloc(param, *totSize);
1132 }
1133 strncpy(param + *curSize, node->value, len);
1134 *curSize += len;
1135 param[*curSize] = '\0';
1136 LOGV("readParamValue() reading string %s", param + *curSize - len);
1137 return len;
1138 }
1139 LOGW("readParamValue() unknown param type %s", node->name);
1140 return 0;
1141}
1142
1143effect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
1144{
1145 cnode *param;
1146 cnode *value;
1147 size_t curSize = sizeof(effect_param_t);
1148 size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
1149 effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
1150
1151 param = config_find(root, PARAM_TAG);
1152 value = config_find(root, VALUE_TAG);
1153 if (param == NULL && value == NULL) {
1154 // try to parse simple parameter form {int int}
1155 param = root->first_child;
1156 if (param) {
1157 // Note: that a pair of random strings is read as 0 0
1158 int *ptr = (int *)fx_param->data;
1159 int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
1160 LOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
1161 *ptr++ = atoi(param->name);
1162 *ptr = atoi(param->value);
1163 fx_param->psize = sizeof(int);
1164 fx_param->vsize = sizeof(int);
1165 return fx_param;
1166 }
1167 }
1168 if (param == NULL || value == NULL) {
1169 LOGW("loadEffectParameter() invalid parameter description %s", root->name);
1170 goto error;
1171 }
1172
1173 fx_param->psize = 0;
1174 param = param->first_child;
1175 while (param) {
1176 LOGV("loadEffectParameter() reading param of type %s", param->name);
1177 size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
1178 if (size == 0) {
1179 goto error;
1180 }
1181 fx_param->psize += size;
1182 param = param->next;
1183 }
1184
1185 // align start of value field on 32 bit boundary
1186 curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
1187
1188 fx_param->vsize = 0;
1189 value = value->first_child;
1190 while (value) {
1191 LOGV("loadEffectParameter() reading value of type %s", value->name);
1192 size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
1193 if (size == 0) {
1194 goto error;
1195 }
1196 fx_param->vsize += size;
1197 value = value->next;
1198 }
1199
1200 return fx_param;
1201
1202error:
1203 delete fx_param;
1204 return NULL;
1205}
1206
1207void AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
1208{
1209 cnode *node = root->first_child;
1210 while (node) {
1211 LOGV("loadEffectParameters() loading param %s", node->name);
1212 effect_param_t *param = loadEffectParameter(node);
1213 if (param == NULL) {
1214 node = node->next;
1215 continue;
1216 }
1217 params.add(param);
1218 node = node->next;
1219 }
1220}
1221
1222AudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
1223 cnode *root,
1224 const Vector <EffectDesc *>& effects)
1225{
1226 cnode *node = root->first_child;
1227 if (node == NULL) {
1228 LOGW("loadInputSource() empty element %s", root->name);
1229 return NULL;
1230 }
1231 InputSourceDesc *source = new InputSourceDesc();
1232 while (node) {
1233 size_t i;
1234 for (i = 0; i < effects.size(); i++) {
1235 if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
1236 LOGV("loadInputSource() found effect %s in list", node->name);
1237 break;
1238 }
1239 }
1240 if (i == effects.size()) {
1241 LOGV("loadInputSource() effect %s not in list", node->name);
1242 node = node->next;
1243 continue;
1244 }
1245 EffectDesc *effect = new EffectDesc(*effects[i]);
1246 loadEffectParameters(node, effect->mParams);
1247 LOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
1248 source->mEffects.add(effect);
1249 node = node->next;
1250 }
1251 if (source->mEffects.size() == 0) {
1252 LOGW("loadInputSource() no valid effects found in source %s", root->name);
1253 delete source;
1254 return NULL;
1255 }
1256 return source;
1257}
1258
1259status_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
1260{
1261 cnode *node = config_find(root, PREPROCESSING_TAG);
1262 if (node == NULL) {
1263 return -ENOENT;
1264 }
1265 node = node->first_child;
1266 while (node) {
1267 audio_source_t source = inputSourceNameToEnum(node->name);
1268 if (source == AUDIO_SOURCE_CNT) {
1269 LOGW("loadInputSources() invalid input source %s", node->name);
1270 node = node->next;
1271 continue;
1272 }
1273 LOGV("loadInputSources() loading input source %s", node->name);
1274 InputSourceDesc *desc = loadInputSource(node, effects);
1275 if (desc == NULL) {
1276 node = node->next;
1277 continue;
1278 }
1279 mInputSources.add(source, desc);
1280 node = node->next;
1281 }
1282 return NO_ERROR;
1283}
1284
1285AudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
1286{
1287 cnode *node = config_find(root, UUID_TAG);
1288 if (node == NULL) {
1289 return NULL;
1290 }
1291 effect_uuid_t uuid;
1292 if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
1293 LOGW("loadEffect() invalid uuid %s", node->value);
1294 return NULL;
1295 }
1296 EffectDesc *effect = new EffectDesc();
1297 effect->mName = strdup(root->name);
1298 memcpy(&effect->mUuid, &uuid, sizeof(effect_uuid_t));
1299
1300 return effect;
1301}
1302
1303status_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
1304{
1305 cnode *node = config_find(root, EFFECTS_TAG);
1306 if (node == NULL) {
1307 return -ENOENT;
1308 }
1309 node = node->first_child;
1310 while (node) {
1311 LOGV("loadEffects() loading effect %s", node->name);
1312 EffectDesc *effect = loadEffect(node);
1313 if (effect == NULL) {
1314 node = node->next;
1315 continue;
1316 }
1317 effects.add(effect);
1318 node = node->next;
1319 }
1320 return NO_ERROR;
1321}
1322
1323status_t AudioPolicyService::loadPreProcessorConfig(const char *path)
1324{
1325 cnode *root;
1326 char *data;
1327
1328 data = (char *)load_file(path, NULL);
1329 if (data == NULL) {
1330 return -ENODEV;
1331 }
1332 root = config_node("", "");
1333 config_load(root, data);
1334
1335 Vector <EffectDesc *> effects;
1336 loadEffects(root, effects);
1337 loadInputSources(root, effects);
1338
1339 config_free(root);
1340 free(root);
1341 free(data);
1342
1343 return NO_ERROR;
1344}
1345
Dima Zavinfce7a472011-04-19 22:30:36 -07001346/* implementation of the interface to the policy manager */
1347extern "C" {
1348
1349static audio_io_handle_t aps_open_output(void *service,
1350 uint32_t *pDevices,
1351 uint32_t *pSamplingRate,
1352 uint32_t *pFormat,
1353 uint32_t *pChannels,
1354 uint32_t *pLatencyMs,
1355 audio_policy_output_flags_t flags)
1356{
1357 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1358 if (af == NULL) {
1359 LOGW("%s: could not get AudioFlinger", __func__);
1360 return 0;
1361 }
1362
1363 return af->openOutput(pDevices, pSamplingRate, pFormat, pChannels,
1364 pLatencyMs, flags);
1365}
1366
1367static audio_io_handle_t aps_open_dup_output(void *service,
1368 audio_io_handle_t output1,
1369 audio_io_handle_t output2)
1370{
1371 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1372 if (af == NULL) {
1373 LOGW("%s: could not get AudioFlinger", __func__);
1374 return 0;
1375 }
1376 return af->openDuplicateOutput(output1, output2);
1377}
1378
1379static int aps_close_output(void *service, audio_io_handle_t output)
1380{
1381 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1382 if (af == NULL)
1383 return PERMISSION_DENIED;
1384
1385 return af->closeOutput(output);
1386}
1387
1388static int aps_suspend_output(void *service, audio_io_handle_t output)
1389{
1390 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1391 if (af == NULL) {
1392 LOGW("%s: could not get AudioFlinger", __func__);
1393 return PERMISSION_DENIED;
1394 }
1395
1396 return af->suspendOutput(output);
1397}
1398
1399static int aps_restore_output(void *service, audio_io_handle_t output)
1400{
1401 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1402 if (af == NULL) {
1403 LOGW("%s: could not get AudioFlinger", __func__);
1404 return PERMISSION_DENIED;
1405 }
1406
1407 return af->restoreOutput(output);
1408}
1409
1410static audio_io_handle_t aps_open_input(void *service,
1411 uint32_t *pDevices,
1412 uint32_t *pSamplingRate,
1413 uint32_t *pFormat,
1414 uint32_t *pChannels,
1415 uint32_t acoustics)
1416{
1417 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1418 if (af == NULL) {
1419 LOGW("%s: could not get AudioFlinger", __func__);
1420 return 0;
1421 }
1422
1423 return af->openInput(pDevices, pSamplingRate, pFormat, pChannels,
1424 acoustics);
1425}
1426
1427static int aps_close_input(void *service, audio_io_handle_t input)
1428{
1429 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1430 if (af == NULL)
1431 return PERMISSION_DENIED;
1432
1433 return af->closeInput(input);
1434}
1435
1436static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1437 audio_io_handle_t output)
1438{
1439 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1440 if (af == NULL)
1441 return PERMISSION_DENIED;
1442
1443 return af->setStreamOutput(stream, output);
1444}
1445
1446static int aps_move_effects(void *service, int session,
1447 audio_io_handle_t src_output,
1448 audio_io_handle_t dst_output)
1449{
1450 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1451 if (af == NULL)
1452 return PERMISSION_DENIED;
1453
1454 return af->moveEffects(session, (int)src_output, (int)dst_output);
1455}
1456
1457static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1458 const char *keys)
1459{
1460 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1461 return strdup(result.string());
1462}
1463
1464static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1465 const char *kv_pairs, int delay_ms)
1466{
1467 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1468
1469 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1470}
1471
1472static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1473 float volume, audio_io_handle_t output,
1474 int delay_ms)
1475{
1476 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1477
1478 return audioPolicyService->setStreamVolume(stream, volume, output,
1479 delay_ms);
1480}
1481
1482static int aps_start_tone(void *service, audio_policy_tone_t tone,
1483 audio_stream_type_t stream)
1484{
1485 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1486
1487 return audioPolicyService->startTone(tone, stream);
1488}
1489
1490static int aps_stop_tone(void *service)
1491{
1492 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1493
1494 return audioPolicyService->stopTone();
1495}
1496
1497static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1498{
1499 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1500
1501 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1502}
1503
1504}; // extern "C"
1505
1506namespace {
1507 struct audio_policy_service_ops aps_ops = {
1508 open_output : aps_open_output,
1509 open_duplicate_output : aps_open_dup_output,
1510 close_output : aps_close_output,
1511 suspend_output : aps_suspend_output,
1512 restore_output : aps_restore_output,
1513 open_input : aps_open_input,
1514 close_input : aps_close_input,
1515 set_stream_volume : aps_set_stream_volume,
1516 set_stream_output : aps_set_stream_output,
1517 set_parameters : aps_set_parameters,
1518 get_parameters : aps_get_parameters,
1519 start_tone : aps_start_tone,
1520 stop_tone : aps_stop_tone,
1521 set_voice_volume : aps_set_voice_volume,
1522 move_effects : aps_move_effects,
1523 };
1524}; // namespace <unnamed>
1525
Mathias Agopian65ab4712010-07-14 17:59:35 -07001526}; // namespace android