blob: 47ca3a03c7dca6ae3960e19cfa5378dc7f1ec462 [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>
36
Dima Zavinfce7a472011-04-19 22:30:36 -070037#include <hardware/hardware.h>
Dima Zavin64760242011-05-11 14:15:23 -070038#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070039#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070040#include <hardware/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070041
Mathias Agopian65ab4712010-07-14 17:59:35 -070042namespace android {
43
Mathias Agopian65ab4712010-07-14 17:59:35 -070044static const char *kDeadlockedString = "AudioPolicyService may be deadlocked\n";
45static const char *kCmdDeadlockedString = "AudioPolicyService command thread may be deadlocked\n";
46
47static const int kDumpLockRetries = 50;
48static const int kDumpLockSleep = 20000;
49
50static bool checkPermission() {
51#ifndef HAVE_ANDROID_OS
52 return true;
53#endif
54 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);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700107}
108
109AudioPolicyService::~AudioPolicyService()
110{
111 mTonePlaybackThread->exit();
112 mTonePlaybackThread.clear();
113 mAudioCommandThread->exit();
114 mAudioCommandThread.clear();
115
Dima Zavinfce7a472011-04-19 22:30:36 -0700116 if (mpAudioPolicy && mpAudioPolicyDev)
117 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
118 if (mpAudioPolicyDev)
119 audio_policy_dev_close(mpAudioPolicyDev);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700120}
121
Dima Zavinfce7a472011-04-19 22:30:36 -0700122status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
123 audio_policy_dev_state_t state,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700124 const char *device_address)
125{
Dima Zavinfce7a472011-04-19 22:30:36 -0700126 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700127 return NO_INIT;
128 }
129 if (!checkPermission()) {
130 return PERMISSION_DENIED;
131 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700132 if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700133 return BAD_VALUE;
134 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700135 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
136 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700137 return BAD_VALUE;
138 }
139
140 LOGV("setDeviceConnectionState() tid %d", gettid());
141 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700142 return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device,
143 state, device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700144}
145
Dima Zavinfce7a472011-04-19 22:30:36 -0700146audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
147 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -0700148 const char *device_address)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700149{
Dima Zavinfce7a472011-04-19 22:30:36 -0700150 if (mpAudioPolicy == NULL) {
151 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700152 }
153 if (!checkPermission()) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700154 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700155 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700156 return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device,
157 device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700158}
159
160status_t AudioPolicyService::setPhoneState(int state)
161{
Dima Zavinfce7a472011-04-19 22:30:36 -0700162 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700163 return NO_INIT;
164 }
165 if (!checkPermission()) {
166 return PERMISSION_DENIED;
167 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700168 if (state < 0 || state >= AUDIO_MODE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700169 return BAD_VALUE;
170 }
171
172 LOGV("setPhoneState() tid %d", gettid());
173
174 // TODO: check if it is more appropriate to do it in platform specific policy manager
175 AudioSystem::setMode(state);
176
177 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700178 mpAudioPolicy->set_phone_state(mpAudioPolicy, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700179 return NO_ERROR;
180}
181
182status_t AudioPolicyService::setRingerMode(uint32_t mode, uint32_t mask)
183{
Dima Zavinfce7a472011-04-19 22:30:36 -0700184 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700185 return NO_INIT;
186 }
187 if (!checkPermission()) {
188 return PERMISSION_DENIED;
189 }
190
Dima Zavinfce7a472011-04-19 22:30:36 -0700191 mpAudioPolicy->set_ringer_mode(mpAudioPolicy, mode, mask);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700192 return NO_ERROR;
193}
194
Dima Zavinfce7a472011-04-19 22:30:36 -0700195status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
196 audio_policy_forced_cfg_t config)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700197{
Dima Zavinfce7a472011-04-19 22:30:36 -0700198 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700199 return NO_INIT;
200 }
201 if (!checkPermission()) {
202 return PERMISSION_DENIED;
203 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700204 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700205 return BAD_VALUE;
206 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700207 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700208 return BAD_VALUE;
209 }
210 LOGV("setForceUse() tid %d", gettid());
211 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700212 mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700213 return NO_ERROR;
214}
215
Dima Zavinfce7a472011-04-19 22:30:36 -0700216audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700217{
Dima Zavinfce7a472011-04-19 22:30:36 -0700218 if (mpAudioPolicy == NULL) {
219 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700220 }
221 if (!checkPermission()) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700222 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700223 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700224 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
225 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700226 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700227 return mpAudioPolicy->get_force_use(mpAudioPolicy, usage);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700228}
229
Dima Zavinfce7a472011-04-19 22:30:36 -0700230audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700231 uint32_t samplingRate,
232 uint32_t format,
233 uint32_t channels,
Dima Zavinfce7a472011-04-19 22:30:36 -0700234 audio_policy_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700235{
Dima Zavinfce7a472011-04-19 22:30:36 -0700236 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700237 return 0;
238 }
239 LOGV("getOutput() tid %d", gettid());
240 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700241 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, format, channels, flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242}
243
Eric Laurentde070132010-07-13 04:45:46 -0700244status_t AudioPolicyService::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700245 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700246 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700247{
Dima Zavinfce7a472011-04-19 22:30:36 -0700248 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700249 return NO_INIT;
250 }
251 LOGV("startOutput() tid %d", gettid());
252 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700253 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700254}
255
Eric Laurentde070132010-07-13 04:45:46 -0700256status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700257 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700258 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700259{
Dima Zavinfce7a472011-04-19 22:30:36 -0700260 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700261 return NO_INIT;
262 }
263 LOGV("stopOutput() tid %d", gettid());
264 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700265 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700266}
267
268void AudioPolicyService::releaseOutput(audio_io_handle_t output)
269{
Dima Zavinfce7a472011-04-19 22:30:36 -0700270 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700271 return;
272 }
273 LOGV("releaseOutput() tid %d", gettid());
274 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700275 mpAudioPolicy->release_output(mpAudioPolicy, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700276}
277
278audio_io_handle_t AudioPolicyService::getInput(int inputSource,
279 uint32_t samplingRate,
280 uint32_t format,
281 uint32_t channels,
Dima Zavinfce7a472011-04-19 22:30:36 -0700282 audio_in_acoustics_t acoustics)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700283{
Dima Zavinfce7a472011-04-19 22:30:36 -0700284 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700285 return 0;
286 }
287 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700288 return mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate, format, channels, acoustics);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700289}
290
291status_t AudioPolicyService::startInput(audio_io_handle_t input)
292{
Dima Zavinfce7a472011-04-19 22:30:36 -0700293 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700294 return NO_INIT;
295 }
296 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700297 return mpAudioPolicy->start_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700298}
299
300status_t AudioPolicyService::stopInput(audio_io_handle_t input)
301{
Dima Zavinfce7a472011-04-19 22:30:36 -0700302 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700303 return NO_INIT;
304 }
305 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700306 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700307}
308
309void AudioPolicyService::releaseInput(audio_io_handle_t input)
310{
Dima Zavinfce7a472011-04-19 22:30:36 -0700311 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700312 return;
313 }
314 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700315 mpAudioPolicy->release_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316}
317
Dima Zavinfce7a472011-04-19 22:30:36 -0700318status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700319 int indexMin,
320 int indexMax)
321{
Dima Zavinfce7a472011-04-19 22:30:36 -0700322 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323 return NO_INIT;
324 }
325 if (!checkPermission()) {
326 return PERMISSION_DENIED;
327 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700328 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700329 return BAD_VALUE;
330 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700331 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700332 return NO_ERROR;
333}
334
Dima Zavinfce7a472011-04-19 22:30:36 -0700335status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream, int index)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336{
Dima Zavinfce7a472011-04-19 22:30:36 -0700337 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338 return NO_INIT;
339 }
340 if (!checkPermission()) {
341 return PERMISSION_DENIED;
342 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700343 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700344 return BAD_VALUE;
345 }
346
Dima Zavinfce7a472011-04-19 22:30:36 -0700347 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700348}
349
Dima Zavinfce7a472011-04-19 22:30:36 -0700350status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream, int *index)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351{
Dima Zavinfce7a472011-04-19 22:30:36 -0700352 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700353 return NO_INIT;
354 }
355 if (!checkPermission()) {
356 return PERMISSION_DENIED;
357 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700358 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700359 return BAD_VALUE;
360 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700361 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362}
363
Dima Zavinfce7a472011-04-19 22:30:36 -0700364uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700365{
Dima Zavinfce7a472011-04-19 22:30:36 -0700366 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700367 return 0;
368 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700369 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurentde070132010-07-13 04:45:46 -0700370}
371
Dima Zavinfce7a472011-04-19 22:30:36 -0700372uint32_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800373{
Dima Zavinfce7a472011-04-19 22:30:36 -0700374 if (mpAudioPolicy == NULL) {
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800375 return 0;
376 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700377 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800378}
379
Eric Laurentde070132010-07-13 04:45:46 -0700380audio_io_handle_t AudioPolicyService::getOutputForEffect(effect_descriptor_t *desc)
381{
Dima Zavinfce7a472011-04-19 22:30:36 -0700382 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700383 return NO_INIT;
384 }
385 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700386 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700387}
388
389status_t AudioPolicyService::registerEffect(effect_descriptor_t *desc,
390 audio_io_handle_t output,
391 uint32_t strategy,
392 int session,
393 int id)
394{
Dima Zavinfce7a472011-04-19 22:30:36 -0700395 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700396 return NO_INIT;
397 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700398 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, output, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700399}
400
401status_t AudioPolicyService::unregisterEffect(int id)
402{
Dima Zavinfce7a472011-04-19 22:30:36 -0700403 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700404 return NO_INIT;
405 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700406 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700407}
408
Eric Laurenteda6c362011-02-02 09:33:30 -0800409bool AudioPolicyService::isStreamActive(int stream, uint32_t inPastMs) const
410{
Dima Zavinfce7a472011-04-19 22:30:36 -0700411 if (mpAudioPolicy == NULL) {
Eric Laurenteda6c362011-02-02 09:33:30 -0800412 return 0;
413 }
414 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700415 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800416}
417
Mathias Agopian65ab4712010-07-14 17:59:35 -0700418void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Eric Laurentde070132010-07-13 04:45:46 -0700419 LOGW("binderDied() %p, tid %d, calling tid %d", who.unsafe_get(), gettid(),
420 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700421}
422
423static bool tryLock(Mutex& mutex)
424{
425 bool locked = false;
426 for (int i = 0; i < kDumpLockRetries; ++i) {
427 if (mutex.tryLock() == NO_ERROR) {
428 locked = true;
429 break;
430 }
431 usleep(kDumpLockSleep);
432 }
433 return locked;
434}
435
436status_t AudioPolicyService::dumpInternals(int fd)
437{
438 const size_t SIZE = 256;
439 char buffer[SIZE];
440 String8 result;
441
Dima Zavinfce7a472011-04-19 22:30:36 -0700442 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700443 result.append(buffer);
444 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
445 result.append(buffer);
446 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
447 result.append(buffer);
448
449 write(fd, result.string(), result.size());
450 return NO_ERROR;
451}
452
453status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
454{
455 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
456 dumpPermissionDenial(fd);
457 } else {
458 bool locked = tryLock(mLock);
459 if (!locked) {
460 String8 result(kDeadlockedString);
461 write(fd, result.string(), result.size());
462 }
463
464 dumpInternals(fd);
465 if (mAudioCommandThread != NULL) {
466 mAudioCommandThread->dump(fd);
467 }
468 if (mTonePlaybackThread != NULL) {
469 mTonePlaybackThread->dump(fd);
470 }
471
Dima Zavinfce7a472011-04-19 22:30:36 -0700472 if (mpAudioPolicy) {
473 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700474 }
475
476 if (locked) mLock.unlock();
477 }
478 return NO_ERROR;
479}
480
481status_t AudioPolicyService::dumpPermissionDenial(int fd)
482{
483 const size_t SIZE = 256;
484 char buffer[SIZE];
485 String8 result;
486 snprintf(buffer, SIZE, "Permission Denial: "
487 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
488 IPCThreadState::self()->getCallingPid(),
489 IPCThreadState::self()->getCallingUid());
490 result.append(buffer);
491 write(fd, result.string(), result.size());
492 return NO_ERROR;
493}
494
495status_t AudioPolicyService::onTransact(
496 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
497{
498 return BnAudioPolicyService::onTransact(code, data, reply, flags);
499}
500
501
Mathias Agopian65ab4712010-07-14 17:59:35 -0700502// ----------- AudioPolicyService::AudioCommandThread implementation ----------
503
504AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name)
505 : Thread(false), mName(name)
506{
507 mpToneGenerator = NULL;
508}
509
510
511AudioPolicyService::AudioCommandThread::~AudioCommandThread()
512{
513 if (mName != "" && !mAudioCommands.isEmpty()) {
514 release_wake_lock(mName.string());
515 }
516 mAudioCommands.clear();
517 if (mpToneGenerator != NULL) delete mpToneGenerator;
518}
519
520void AudioPolicyService::AudioCommandThread::onFirstRef()
521{
522 if (mName != "") {
523 run(mName.string(), ANDROID_PRIORITY_AUDIO);
524 } else {
525 run("AudioCommandThread", ANDROID_PRIORITY_AUDIO);
526 }
527}
528
529bool AudioPolicyService::AudioCommandThread::threadLoop()
530{
531 nsecs_t waitTime = INT64_MAX;
532
533 mLock.lock();
534 while (!exitPending())
535 {
536 while(!mAudioCommands.isEmpty()) {
537 nsecs_t curTime = systemTime();
538 // commands are sorted by increasing time stamp: execute them from index 0 and up
539 if (mAudioCommands[0]->mTime <= curTime) {
540 AudioCommand *command = mAudioCommands[0];
541 mAudioCommands.removeAt(0);
542 mLastCommand = *command;
543
544 switch (command->mCommand) {
545 case START_TONE: {
546 mLock.unlock();
547 ToneData *data = (ToneData *)command->mParam;
548 LOGV("AudioCommandThread() processing start tone %d on stream %d",
549 data->mType, data->mStream);
550 if (mpToneGenerator != NULL)
551 delete mpToneGenerator;
552 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
553 mpToneGenerator->startTone(data->mType);
554 delete data;
555 mLock.lock();
556 }break;
557 case STOP_TONE: {
558 mLock.unlock();
559 LOGV("AudioCommandThread() processing stop tone");
560 if (mpToneGenerator != NULL) {
561 mpToneGenerator->stopTone();
562 delete mpToneGenerator;
563 mpToneGenerator = NULL;
564 }
565 mLock.lock();
566 }break;
567 case SET_VOLUME: {
568 VolumeData *data = (VolumeData *)command->mParam;
Eric Laurentde070132010-07-13 04:45:46 -0700569 LOGV("AudioCommandThread() processing set volume stream %d, \
570 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
571 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
572 data->mVolume,
573 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700574 if (command->mWaitStatus) {
575 command->mCond.signal();
576 mWaitWorkCV.wait(mLock);
577 }
578 delete data;
579 }break;
580 case SET_PARAMETERS: {
581 ParametersData *data = (ParametersData *)command->mParam;
Eric Laurentde070132010-07-13 04:45:46 -0700582 LOGV("AudioCommandThread() processing set parameters string %s, io %d",
583 data->mKeyValuePairs.string(), data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700584 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
585 if (command->mWaitStatus) {
586 command->mCond.signal();
587 mWaitWorkCV.wait(mLock);
588 }
589 delete data;
590 }break;
591 case SET_VOICE_VOLUME: {
592 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Eric Laurentde070132010-07-13 04:45:46 -0700593 LOGV("AudioCommandThread() processing set voice volume volume %f",
594 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
596 if (command->mWaitStatus) {
597 command->mCond.signal();
598 mWaitWorkCV.wait(mLock);
599 }
600 delete data;
601 }break;
602 default:
603 LOGW("AudioCommandThread() unknown command %d", command->mCommand);
604 }
605 delete command;
606 waitTime = INT64_MAX;
607 } else {
608 waitTime = mAudioCommands[0]->mTime - curTime;
609 break;
610 }
611 }
612 // release delayed commands wake lock
613 if (mName != "" && mAudioCommands.isEmpty()) {
614 release_wake_lock(mName.string());
615 }
616 LOGV("AudioCommandThread() going to sleep");
617 mWaitWorkCV.waitRelative(mLock, waitTime);
618 LOGV("AudioCommandThread() waking up");
619 }
620 mLock.unlock();
621 return false;
622}
623
624status_t AudioPolicyService::AudioCommandThread::dump(int fd)
625{
626 const size_t SIZE = 256;
627 char buffer[SIZE];
628 String8 result;
629
630 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
631 result.append(buffer);
632 write(fd, result.string(), result.size());
633
634 bool locked = tryLock(mLock);
635 if (!locked) {
636 String8 result2(kCmdDeadlockedString);
637 write(fd, result2.string(), result2.size());
638 }
639
640 snprintf(buffer, SIZE, "- Commands:\n");
641 result = String8(buffer);
642 result.append(" Command Time Wait pParam\n");
643 for (int i = 0; i < (int)mAudioCommands.size(); i++) {
644 mAudioCommands[i]->dump(buffer, SIZE);
645 result.append(buffer);
646 }
647 result.append(" Last Command\n");
648 mLastCommand.dump(buffer, SIZE);
649 result.append(buffer);
650
651 write(fd, result.string(), result.size());
652
653 if (locked) mLock.unlock();
654
655 return NO_ERROR;
656}
657
658void AudioPolicyService::AudioCommandThread::startToneCommand(int type, int stream)
659{
660 AudioCommand *command = new AudioCommand();
661 command->mCommand = START_TONE;
662 ToneData *data = new ToneData();
663 data->mType = type;
664 data->mStream = stream;
665 command->mParam = (void *)data;
666 command->mWaitStatus = false;
667 Mutex::Autolock _l(mLock);
668 insertCommand_l(command);
669 LOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
670 mWaitWorkCV.signal();
671}
672
673void AudioPolicyService::AudioCommandThread::stopToneCommand()
674{
675 AudioCommand *command = new AudioCommand();
676 command->mCommand = STOP_TONE;
677 command->mParam = NULL;
678 command->mWaitStatus = false;
679 Mutex::Autolock _l(mLock);
680 insertCommand_l(command);
681 LOGV("AudioCommandThread() adding tone stop");
682 mWaitWorkCV.signal();
683}
684
Eric Laurentde070132010-07-13 04:45:46 -0700685status_t AudioPolicyService::AudioCommandThread::volumeCommand(int stream,
686 float volume,
687 int output,
688 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700689{
690 status_t status = NO_ERROR;
691
692 AudioCommand *command = new AudioCommand();
693 command->mCommand = SET_VOLUME;
694 VolumeData *data = new VolumeData();
695 data->mStream = stream;
696 data->mVolume = volume;
697 data->mIO = output;
698 command->mParam = data;
699 if (delayMs == 0) {
700 command->mWaitStatus = true;
701 } else {
702 command->mWaitStatus = false;
703 }
704 Mutex::Autolock _l(mLock);
705 insertCommand_l(command, delayMs);
Eric Laurentde070132010-07-13 04:45:46 -0700706 LOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
707 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700708 mWaitWorkCV.signal();
709 if (command->mWaitStatus) {
710 command->mCond.wait(mLock);
711 status = command->mStatus;
712 mWaitWorkCV.signal();
713 }
714 return status;
715}
716
Eric Laurentde070132010-07-13 04:45:46 -0700717status_t AudioPolicyService::AudioCommandThread::parametersCommand(int ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700718 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700719 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700720{
721 status_t status = NO_ERROR;
722
723 AudioCommand *command = new AudioCommand();
724 command->mCommand = SET_PARAMETERS;
725 ParametersData *data = new ParametersData();
726 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700727 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700728 command->mParam = data;
729 if (delayMs == 0) {
730 command->mWaitStatus = true;
731 } else {
732 command->mWaitStatus = false;
733 }
734 Mutex::Autolock _l(mLock);
735 insertCommand_l(command, delayMs);
Eric Laurentde070132010-07-13 04:45:46 -0700736 LOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700737 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700738 mWaitWorkCV.signal();
739 if (command->mWaitStatus) {
740 command->mCond.wait(mLock);
741 status = command->mStatus;
742 mWaitWorkCV.signal();
743 }
744 return status;
745}
746
747status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
748{
749 status_t status = NO_ERROR;
750
751 AudioCommand *command = new AudioCommand();
752 command->mCommand = SET_VOICE_VOLUME;
753 VoiceVolumeData *data = new VoiceVolumeData();
754 data->mVolume = volume;
755 command->mParam = data;
756 if (delayMs == 0) {
757 command->mWaitStatus = true;
758 } else {
759 command->mWaitStatus = false;
760 }
761 Mutex::Autolock _l(mLock);
762 insertCommand_l(command, delayMs);
763 LOGV("AudioCommandThread() adding set voice volume volume %f", volume);
764 mWaitWorkCV.signal();
765 if (command->mWaitStatus) {
766 command->mCond.wait(mLock);
767 status = command->mStatus;
768 mWaitWorkCV.signal();
769 }
770 return status;
771}
772
773// insertCommand_l() must be called with mLock held
774void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
775{
776 ssize_t i;
777 Vector <AudioCommand *> removedCommands;
778
779 command->mTime = systemTime() + milliseconds(delayMs);
780
781 // acquire wake lock to make sure delayed commands are processed
782 if (mName != "" && mAudioCommands.isEmpty()) {
783 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
784 }
785
786 // check same pending commands with later time stamps and eliminate them
787 for (i = mAudioCommands.size()-1; i >= 0; i--) {
788 AudioCommand *command2 = mAudioCommands[i];
789 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
790 if (command2->mTime <= command->mTime) break;
791 if (command2->mCommand != command->mCommand) continue;
792
793 switch (command->mCommand) {
794 case SET_PARAMETERS: {
795 ParametersData *data = (ParametersData *)command->mParam;
796 ParametersData *data2 = (ParametersData *)command2->mParam;
797 if (data->mIO != data2->mIO) break;
Eric Laurentde070132010-07-13 04:45:46 -0700798 LOGV("Comparing parameter command %s to new command %s",
799 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700800 AudioParameter param = AudioParameter(data->mKeyValuePairs);
801 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
802 for (size_t j = 0; j < param.size(); j++) {
803 String8 key;
804 String8 value;
805 param.getAt(j, key, value);
806 for (size_t k = 0; k < param2.size(); k++) {
807 String8 key2;
808 String8 value2;
809 param2.getAt(k, key2, value2);
810 if (key2 == key) {
811 param2.remove(key2);
812 LOGV("Filtering out parameter %s", key2.string());
813 break;
814 }
815 }
816 }
817 // if all keys have been filtered out, remove the command.
818 // otherwise, update the key value pairs
819 if (param2.size() == 0) {
820 removedCommands.add(command2);
821 } else {
822 data2->mKeyValuePairs = param2.toString();
823 }
824 } break;
825
826 case SET_VOLUME: {
827 VolumeData *data = (VolumeData *)command->mParam;
828 VolumeData *data2 = (VolumeData *)command2->mParam;
829 if (data->mIO != data2->mIO) break;
830 if (data->mStream != data2->mStream) break;
Eric Laurentde070132010-07-13 04:45:46 -0700831 LOGV("Filtering out volume command on output %d for stream %d",
832 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700833 removedCommands.add(command2);
834 } break;
835 case START_TONE:
836 case STOP_TONE:
837 default:
838 break;
839 }
840 }
841
842 // remove filtered commands
843 for (size_t j = 0; j < removedCommands.size(); j++) {
844 // removed commands always have time stamps greater than current command
845 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
846 if (mAudioCommands[k] == removedCommands[j]) {
847 LOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
848 mAudioCommands.removeAt(k);
849 break;
850 }
851 }
852 }
853 removedCommands.clear();
854
855 // insert command at the right place according to its time stamp
Eric Laurentde070132010-07-13 04:45:46 -0700856 LOGV("inserting command: %d at index %d, num commands %d",
857 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700858 mAudioCommands.insertAt(command, i + 1);
859}
860
861void AudioPolicyService::AudioCommandThread::exit()
862{
863 LOGV("AudioCommandThread::exit");
864 {
865 AutoMutex _l(mLock);
866 requestExit();
867 mWaitWorkCV.signal();
868 }
869 requestExitAndWait();
870}
871
872void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
873{
874 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
875 mCommand,
876 (int)ns2s(mTime),
877 (int)ns2ms(mTime)%1000,
878 mWaitStatus,
879 mParam);
880}
881
Dima Zavinfce7a472011-04-19 22:30:36 -0700882/******* helpers for the service_ops callbacks defined below *********/
883void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
884 const char *keyValuePairs,
885 int delayMs)
886{
887 mAudioCommandThread->parametersCommand((int)ioHandle, keyValuePairs,
888 delayMs);
889}
890
891int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
892 float volume,
893 audio_io_handle_t output,
894 int delayMs)
895{
896 return (int)mAudioCommandThread->volumeCommand((int)stream, volume,
897 (int)output, delayMs);
898}
899
900int AudioPolicyService::startTone(audio_policy_tone_t tone,
901 audio_stream_type_t stream)
902{
903 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
904 LOGE("startTone: illegal tone requested (%d)", tone);
905 if (stream != AUDIO_STREAM_VOICE_CALL)
906 LOGE("startTone: illegal stream (%d) requested for tone %d", stream,
907 tone);
908 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
909 AUDIO_STREAM_VOICE_CALL);
910 return 0;
911}
912
913int AudioPolicyService::stopTone()
914{
915 mTonePlaybackThread->stopToneCommand();
916 return 0;
917}
918
919int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
920{
921 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
922}
923
924/* implementation of the interface to the policy manager */
925extern "C" {
926
927static audio_io_handle_t aps_open_output(void *service,
928 uint32_t *pDevices,
929 uint32_t *pSamplingRate,
930 uint32_t *pFormat,
931 uint32_t *pChannels,
932 uint32_t *pLatencyMs,
933 audio_policy_output_flags_t flags)
934{
935 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
936 if (af == NULL) {
937 LOGW("%s: could not get AudioFlinger", __func__);
938 return 0;
939 }
940
941 return af->openOutput(pDevices, pSamplingRate, pFormat, pChannels,
942 pLatencyMs, flags);
943}
944
945static audio_io_handle_t aps_open_dup_output(void *service,
946 audio_io_handle_t output1,
947 audio_io_handle_t output2)
948{
949 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
950 if (af == NULL) {
951 LOGW("%s: could not get AudioFlinger", __func__);
952 return 0;
953 }
954 return af->openDuplicateOutput(output1, output2);
955}
956
957static int aps_close_output(void *service, audio_io_handle_t output)
958{
959 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
960 if (af == NULL)
961 return PERMISSION_DENIED;
962
963 return af->closeOutput(output);
964}
965
966static int aps_suspend_output(void *service, audio_io_handle_t output)
967{
968 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
969 if (af == NULL) {
970 LOGW("%s: could not get AudioFlinger", __func__);
971 return PERMISSION_DENIED;
972 }
973
974 return af->suspendOutput(output);
975}
976
977static int aps_restore_output(void *service, audio_io_handle_t output)
978{
979 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
980 if (af == NULL) {
981 LOGW("%s: could not get AudioFlinger", __func__);
982 return PERMISSION_DENIED;
983 }
984
985 return af->restoreOutput(output);
986}
987
988static audio_io_handle_t aps_open_input(void *service,
989 uint32_t *pDevices,
990 uint32_t *pSamplingRate,
991 uint32_t *pFormat,
992 uint32_t *pChannels,
993 uint32_t acoustics)
994{
995 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
996 if (af == NULL) {
997 LOGW("%s: could not get AudioFlinger", __func__);
998 return 0;
999 }
1000
1001 return af->openInput(pDevices, pSamplingRate, pFormat, pChannels,
1002 acoustics);
1003}
1004
1005static int aps_close_input(void *service, audio_io_handle_t input)
1006{
1007 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1008 if (af == NULL)
1009 return PERMISSION_DENIED;
1010
1011 return af->closeInput(input);
1012}
1013
1014static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1015 audio_io_handle_t output)
1016{
1017 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1018 if (af == NULL)
1019 return PERMISSION_DENIED;
1020
1021 return af->setStreamOutput(stream, output);
1022}
1023
1024static int aps_move_effects(void *service, int session,
1025 audio_io_handle_t src_output,
1026 audio_io_handle_t dst_output)
1027{
1028 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1029 if (af == NULL)
1030 return PERMISSION_DENIED;
1031
1032 return af->moveEffects(session, (int)src_output, (int)dst_output);
1033}
1034
1035static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1036 const char *keys)
1037{
1038 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1039 return strdup(result.string());
1040}
1041
1042static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1043 const char *kv_pairs, int delay_ms)
1044{
1045 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1046
1047 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1048}
1049
1050static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1051 float volume, audio_io_handle_t output,
1052 int delay_ms)
1053{
1054 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1055
1056 return audioPolicyService->setStreamVolume(stream, volume, output,
1057 delay_ms);
1058}
1059
1060static int aps_start_tone(void *service, audio_policy_tone_t tone,
1061 audio_stream_type_t stream)
1062{
1063 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1064
1065 return audioPolicyService->startTone(tone, stream);
1066}
1067
1068static int aps_stop_tone(void *service)
1069{
1070 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1071
1072 return audioPolicyService->stopTone();
1073}
1074
1075static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1076{
1077 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1078
1079 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1080}
1081
1082}; // extern "C"
1083
1084namespace {
1085 struct audio_policy_service_ops aps_ops = {
1086 open_output : aps_open_output,
1087 open_duplicate_output : aps_open_dup_output,
1088 close_output : aps_close_output,
1089 suspend_output : aps_suspend_output,
1090 restore_output : aps_restore_output,
1091 open_input : aps_open_input,
1092 close_input : aps_close_input,
1093 set_stream_volume : aps_set_stream_volume,
1094 set_stream_output : aps_set_stream_output,
1095 set_parameters : aps_set_parameters,
1096 get_parameters : aps_get_parameters,
1097 start_tone : aps_start_tone,
1098 stop_tone : aps_stop_tone,
1099 set_voice_volume : aps_set_voice_volume,
1100 move_effects : aps_move_effects,
1101 };
1102}; // namespace <unnamed>
1103
Mathias Agopian65ab4712010-07-14 17:59:35 -07001104}; // namespace android