blob: 8e16d944356d67ca9e6b746dd4c61eef01aed3a5 [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() {
Mathias Agopian65ab4712010-07-14 17:59:35 -070051 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
52 bool ok = checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS"));
53 if (!ok) LOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS");
54 return ok;
55}
56
Dima Zavinfce7a472011-04-19 22:30:36 -070057namespace {
58 extern struct audio_policy_service_ops aps_ops;
59};
60
Mathias Agopian65ab4712010-07-14 17:59:35 -070061// ----------------------------------------------------------------------------
62
63AudioPolicyService::AudioPolicyService()
Dima Zavinfce7a472011-04-19 22:30:36 -070064 : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL)
Mathias Agopian65ab4712010-07-14 17:59:35 -070065{
66 char value[PROPERTY_VALUE_MAX];
Dima Zavinfce7a472011-04-19 22:30:36 -070067 const struct hw_module_t *module;
68 int forced_val;
69 int rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -070070
Eric Laurent93575202011-01-18 18:39:02 -080071 Mutex::Autolock _l(mLock);
72
Mathias Agopian65ab4712010-07-14 17:59:35 -070073 // start tone playback thread
74 mTonePlaybackThread = new AudioCommandThread(String8(""));
75 // start audio commands thread
76 mAudioCommandThread = new AudioCommandThread(String8("ApmCommandThread"));
77
Dima Zavinfce7a472011-04-19 22:30:36 -070078 /* instantiate the audio policy manager */
79 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
80 if (rc)
81 return;
Mathias Agopian65ab4712010-07-14 17:59:35 -070082
Dima Zavinfce7a472011-04-19 22:30:36 -070083 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
84 LOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
85 if (rc)
86 return;
Eric Laurent93575202011-01-18 18:39:02 -080087
Dima Zavinfce7a472011-04-19 22:30:36 -070088 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
89 &mpAudioPolicy);
90 LOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
91 if (rc)
92 return;
93
94 rc = mpAudioPolicy->init_check(mpAudioPolicy);
95 LOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
96 if (rc)
97 return;
98
99 property_get("ro.camera.sound.forced", value, "0");
100 forced_val = strtol(value, NULL, 0);
101 mpAudioPolicy->set_can_mute_enforced_audible(mpAudioPolicy, !forced_val);
102
103 LOGI("Loaded audio policy from %s (%s)", module->name, module->id);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700104}
105
106AudioPolicyService::~AudioPolicyService()
107{
108 mTonePlaybackThread->exit();
109 mTonePlaybackThread.clear();
110 mAudioCommandThread->exit();
111 mAudioCommandThread.clear();
112
Dima Zavinfce7a472011-04-19 22:30:36 -0700113 if (mpAudioPolicy && mpAudioPolicyDev)
114 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
115 if (mpAudioPolicyDev)
116 audio_policy_dev_close(mpAudioPolicyDev);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700117}
118
Dima Zavinfce7a472011-04-19 22:30:36 -0700119status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
120 audio_policy_dev_state_t state,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700121 const char *device_address)
122{
Dima Zavinfce7a472011-04-19 22:30:36 -0700123 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700124 return NO_INIT;
125 }
126 if (!checkPermission()) {
127 return PERMISSION_DENIED;
128 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700129 if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700130 return BAD_VALUE;
131 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700132 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
133 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700134 return BAD_VALUE;
135 }
136
137 LOGV("setDeviceConnectionState() tid %d", gettid());
138 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700139 return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device,
140 state, device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700141}
142
Dima Zavinfce7a472011-04-19 22:30:36 -0700143audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
144 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -0700145 const char *device_address)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700146{
Dima Zavinfce7a472011-04-19 22:30:36 -0700147 if (mpAudioPolicy == NULL) {
148 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700149 }
150 if (!checkPermission()) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700151 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700152 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700153 return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device,
154 device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700155}
156
157status_t AudioPolicyService::setPhoneState(int state)
158{
Dima Zavinfce7a472011-04-19 22:30:36 -0700159 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700160 return NO_INIT;
161 }
162 if (!checkPermission()) {
163 return PERMISSION_DENIED;
164 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700165 if (state < 0 || state >= AUDIO_MODE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700166 return BAD_VALUE;
167 }
168
169 LOGV("setPhoneState() tid %d", gettid());
170
171 // TODO: check if it is more appropriate to do it in platform specific policy manager
172 AudioSystem::setMode(state);
173
174 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700175 mpAudioPolicy->set_phone_state(mpAudioPolicy, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700176 return NO_ERROR;
177}
178
179status_t AudioPolicyService::setRingerMode(uint32_t mode, uint32_t mask)
180{
Dima Zavinfce7a472011-04-19 22:30:36 -0700181 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700182 return NO_INIT;
183 }
184 if (!checkPermission()) {
185 return PERMISSION_DENIED;
186 }
187
Dima Zavinfce7a472011-04-19 22:30:36 -0700188 mpAudioPolicy->set_ringer_mode(mpAudioPolicy, mode, mask);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700189 return NO_ERROR;
190}
191
Dima Zavinfce7a472011-04-19 22:30:36 -0700192status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
193 audio_policy_forced_cfg_t config)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700194{
Dima Zavinfce7a472011-04-19 22:30:36 -0700195 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700196 return NO_INIT;
197 }
198 if (!checkPermission()) {
199 return PERMISSION_DENIED;
200 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700201 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700202 return BAD_VALUE;
203 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700204 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700205 return BAD_VALUE;
206 }
207 LOGV("setForceUse() tid %d", gettid());
208 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700209 mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700210 return NO_ERROR;
211}
212
Dima Zavinfce7a472011-04-19 22:30:36 -0700213audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700214{
Dima Zavinfce7a472011-04-19 22:30:36 -0700215 if (mpAudioPolicy == NULL) {
216 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700217 }
218 if (!checkPermission()) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700219 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700220 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700221 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
222 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700223 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700224 return mpAudioPolicy->get_force_use(mpAudioPolicy, usage);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700225}
226
Dima Zavinfce7a472011-04-19 22:30:36 -0700227audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700228 uint32_t samplingRate,
229 uint32_t format,
230 uint32_t channels,
Dima Zavinfce7a472011-04-19 22:30:36 -0700231 audio_policy_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700232{
Dima Zavinfce7a472011-04-19 22:30:36 -0700233 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700234 return 0;
235 }
236 LOGV("getOutput() tid %d", gettid());
237 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700238 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, format, channels, flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700239}
240
Eric Laurentde070132010-07-13 04:45:46 -0700241status_t AudioPolicyService::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700242 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700243 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244{
Dima Zavinfce7a472011-04-19 22:30:36 -0700245 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700246 return NO_INIT;
247 }
248 LOGV("startOutput() tid %d", gettid());
249 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700250 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700251}
252
Eric Laurentde070132010-07-13 04:45:46 -0700253status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700254 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700255 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700256{
Dima Zavinfce7a472011-04-19 22:30:36 -0700257 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700258 return NO_INIT;
259 }
260 LOGV("stopOutput() tid %d", gettid());
261 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700262 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700263}
264
265void AudioPolicyService::releaseOutput(audio_io_handle_t output)
266{
Dima Zavinfce7a472011-04-19 22:30:36 -0700267 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700268 return;
269 }
270 LOGV("releaseOutput() tid %d", gettid());
271 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700272 mpAudioPolicy->release_output(mpAudioPolicy, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700273}
274
275audio_io_handle_t AudioPolicyService::getInput(int inputSource,
276 uint32_t samplingRate,
277 uint32_t format,
278 uint32_t channels,
Dima Zavinfce7a472011-04-19 22:30:36 -0700279 audio_in_acoustics_t acoustics)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700280{
Dima Zavinfce7a472011-04-19 22:30:36 -0700281 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700282 return 0;
283 }
284 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700285 return mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate, format, channels, acoustics);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700286}
287
288status_t AudioPolicyService::startInput(audio_io_handle_t input)
289{
Dima Zavinfce7a472011-04-19 22:30:36 -0700290 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700291 return NO_INIT;
292 }
293 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700294 return mpAudioPolicy->start_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700295}
296
297status_t AudioPolicyService::stopInput(audio_io_handle_t input)
298{
Dima Zavinfce7a472011-04-19 22:30:36 -0700299 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300 return NO_INIT;
301 }
302 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700303 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700304}
305
306void AudioPolicyService::releaseInput(audio_io_handle_t input)
307{
Dima Zavinfce7a472011-04-19 22:30:36 -0700308 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700309 return;
310 }
311 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700312 mpAudioPolicy->release_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313}
314
Dima Zavinfce7a472011-04-19 22:30:36 -0700315status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316 int indexMin,
317 int indexMax)
318{
Dima Zavinfce7a472011-04-19 22:30:36 -0700319 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320 return NO_INIT;
321 }
322 if (!checkPermission()) {
323 return PERMISSION_DENIED;
324 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700325 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700326 return BAD_VALUE;
327 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700328 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700329 return NO_ERROR;
330}
331
Dima Zavinfce7a472011-04-19 22:30:36 -0700332status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream, int index)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700333{
Dima Zavinfce7a472011-04-19 22:30:36 -0700334 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335 return NO_INIT;
336 }
337 if (!checkPermission()) {
338 return PERMISSION_DENIED;
339 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700340 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700341 return BAD_VALUE;
342 }
343
Dima Zavinfce7a472011-04-19 22:30:36 -0700344 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345}
346
Dima Zavinfce7a472011-04-19 22:30:36 -0700347status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream, int *index)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700348{
Dima Zavinfce7a472011-04-19 22:30:36 -0700349 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700350 return NO_INIT;
351 }
352 if (!checkPermission()) {
353 return PERMISSION_DENIED;
354 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700355 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700356 return BAD_VALUE;
357 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700358 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700359}
360
Dima Zavinfce7a472011-04-19 22:30:36 -0700361uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700362{
Dima Zavinfce7a472011-04-19 22:30:36 -0700363 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700364 return 0;
365 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700366 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurentde070132010-07-13 04:45:46 -0700367}
368
Dima Zavinfce7a472011-04-19 22:30:36 -0700369uint32_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800370{
Dima Zavinfce7a472011-04-19 22:30:36 -0700371 if (mpAudioPolicy == NULL) {
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800372 return 0;
373 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700374 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800375}
376
Eric Laurentde070132010-07-13 04:45:46 -0700377audio_io_handle_t AudioPolicyService::getOutputForEffect(effect_descriptor_t *desc)
378{
Dima Zavinfce7a472011-04-19 22:30:36 -0700379 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700380 return NO_INIT;
381 }
382 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700383 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700384}
385
386status_t AudioPolicyService::registerEffect(effect_descriptor_t *desc,
387 audio_io_handle_t output,
388 uint32_t strategy,
389 int session,
390 int id)
391{
Dima Zavinfce7a472011-04-19 22:30:36 -0700392 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700393 return NO_INIT;
394 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700395 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, output, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700396}
397
398status_t AudioPolicyService::unregisterEffect(int id)
399{
Dima Zavinfce7a472011-04-19 22:30:36 -0700400 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700401 return NO_INIT;
402 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700403 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700404}
405
Eric Laurenteda6c362011-02-02 09:33:30 -0800406bool AudioPolicyService::isStreamActive(int stream, uint32_t inPastMs) const
407{
Dima Zavinfce7a472011-04-19 22:30:36 -0700408 if (mpAudioPolicy == NULL) {
Eric Laurenteda6c362011-02-02 09:33:30 -0800409 return 0;
410 }
411 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700412 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800413}
414
Mathias Agopian65ab4712010-07-14 17:59:35 -0700415void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Eric Laurentde070132010-07-13 04:45:46 -0700416 LOGW("binderDied() %p, tid %d, calling tid %d", who.unsafe_get(), gettid(),
417 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700418}
419
420static bool tryLock(Mutex& mutex)
421{
422 bool locked = false;
423 for (int i = 0; i < kDumpLockRetries; ++i) {
424 if (mutex.tryLock() == NO_ERROR) {
425 locked = true;
426 break;
427 }
428 usleep(kDumpLockSleep);
429 }
430 return locked;
431}
432
433status_t AudioPolicyService::dumpInternals(int fd)
434{
435 const size_t SIZE = 256;
436 char buffer[SIZE];
437 String8 result;
438
Dima Zavinfce7a472011-04-19 22:30:36 -0700439 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700440 result.append(buffer);
441 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
442 result.append(buffer);
443 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
444 result.append(buffer);
445
446 write(fd, result.string(), result.size());
447 return NO_ERROR;
448}
449
450status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
451{
452 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
453 dumpPermissionDenial(fd);
454 } else {
455 bool locked = tryLock(mLock);
456 if (!locked) {
457 String8 result(kDeadlockedString);
458 write(fd, result.string(), result.size());
459 }
460
461 dumpInternals(fd);
462 if (mAudioCommandThread != NULL) {
463 mAudioCommandThread->dump(fd);
464 }
465 if (mTonePlaybackThread != NULL) {
466 mTonePlaybackThread->dump(fd);
467 }
468
Dima Zavinfce7a472011-04-19 22:30:36 -0700469 if (mpAudioPolicy) {
470 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700471 }
472
473 if (locked) mLock.unlock();
474 }
475 return NO_ERROR;
476}
477
478status_t AudioPolicyService::dumpPermissionDenial(int fd)
479{
480 const size_t SIZE = 256;
481 char buffer[SIZE];
482 String8 result;
483 snprintf(buffer, SIZE, "Permission Denial: "
484 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
485 IPCThreadState::self()->getCallingPid(),
486 IPCThreadState::self()->getCallingUid());
487 result.append(buffer);
488 write(fd, result.string(), result.size());
489 return NO_ERROR;
490}
491
492status_t AudioPolicyService::onTransact(
493 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
494{
495 return BnAudioPolicyService::onTransact(code, data, reply, flags);
496}
497
498
Mathias Agopian65ab4712010-07-14 17:59:35 -0700499// ----------- AudioPolicyService::AudioCommandThread implementation ----------
500
501AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name)
502 : Thread(false), mName(name)
503{
504 mpToneGenerator = NULL;
505}
506
507
508AudioPolicyService::AudioCommandThread::~AudioCommandThread()
509{
510 if (mName != "" && !mAudioCommands.isEmpty()) {
511 release_wake_lock(mName.string());
512 }
513 mAudioCommands.clear();
514 if (mpToneGenerator != NULL) delete mpToneGenerator;
515}
516
517void AudioPolicyService::AudioCommandThread::onFirstRef()
518{
519 if (mName != "") {
520 run(mName.string(), ANDROID_PRIORITY_AUDIO);
521 } else {
522 run("AudioCommandThread", ANDROID_PRIORITY_AUDIO);
523 }
524}
525
526bool AudioPolicyService::AudioCommandThread::threadLoop()
527{
528 nsecs_t waitTime = INT64_MAX;
529
530 mLock.lock();
531 while (!exitPending())
532 {
533 while(!mAudioCommands.isEmpty()) {
534 nsecs_t curTime = systemTime();
535 // commands are sorted by increasing time stamp: execute them from index 0 and up
536 if (mAudioCommands[0]->mTime <= curTime) {
537 AudioCommand *command = mAudioCommands[0];
538 mAudioCommands.removeAt(0);
539 mLastCommand = *command;
540
541 switch (command->mCommand) {
542 case START_TONE: {
543 mLock.unlock();
544 ToneData *data = (ToneData *)command->mParam;
545 LOGV("AudioCommandThread() processing start tone %d on stream %d",
546 data->mType, data->mStream);
547 if (mpToneGenerator != NULL)
548 delete mpToneGenerator;
549 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
550 mpToneGenerator->startTone(data->mType);
551 delete data;
552 mLock.lock();
553 }break;
554 case STOP_TONE: {
555 mLock.unlock();
556 LOGV("AudioCommandThread() processing stop tone");
557 if (mpToneGenerator != NULL) {
558 mpToneGenerator->stopTone();
559 delete mpToneGenerator;
560 mpToneGenerator = NULL;
561 }
562 mLock.lock();
563 }break;
564 case SET_VOLUME: {
565 VolumeData *data = (VolumeData *)command->mParam;
Eric Laurentde070132010-07-13 04:45:46 -0700566 LOGV("AudioCommandThread() processing set volume stream %d, \
567 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
568 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
569 data->mVolume,
570 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700571 if (command->mWaitStatus) {
572 command->mCond.signal();
573 mWaitWorkCV.wait(mLock);
574 }
575 delete data;
576 }break;
577 case SET_PARAMETERS: {
578 ParametersData *data = (ParametersData *)command->mParam;
Eric Laurentde070132010-07-13 04:45:46 -0700579 LOGV("AudioCommandThread() processing set parameters string %s, io %d",
580 data->mKeyValuePairs.string(), data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700581 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
582 if (command->mWaitStatus) {
583 command->mCond.signal();
584 mWaitWorkCV.wait(mLock);
585 }
586 delete data;
587 }break;
588 case SET_VOICE_VOLUME: {
589 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Eric Laurentde070132010-07-13 04:45:46 -0700590 LOGV("AudioCommandThread() processing set voice volume volume %f",
591 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700592 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
593 if (command->mWaitStatus) {
594 command->mCond.signal();
595 mWaitWorkCV.wait(mLock);
596 }
597 delete data;
598 }break;
599 default:
600 LOGW("AudioCommandThread() unknown command %d", command->mCommand);
601 }
602 delete command;
603 waitTime = INT64_MAX;
604 } else {
605 waitTime = mAudioCommands[0]->mTime - curTime;
606 break;
607 }
608 }
609 // release delayed commands wake lock
610 if (mName != "" && mAudioCommands.isEmpty()) {
611 release_wake_lock(mName.string());
612 }
613 LOGV("AudioCommandThread() going to sleep");
614 mWaitWorkCV.waitRelative(mLock, waitTime);
615 LOGV("AudioCommandThread() waking up");
616 }
617 mLock.unlock();
618 return false;
619}
620
621status_t AudioPolicyService::AudioCommandThread::dump(int fd)
622{
623 const size_t SIZE = 256;
624 char buffer[SIZE];
625 String8 result;
626
627 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
628 result.append(buffer);
629 write(fd, result.string(), result.size());
630
631 bool locked = tryLock(mLock);
632 if (!locked) {
633 String8 result2(kCmdDeadlockedString);
634 write(fd, result2.string(), result2.size());
635 }
636
637 snprintf(buffer, SIZE, "- Commands:\n");
638 result = String8(buffer);
639 result.append(" Command Time Wait pParam\n");
640 for (int i = 0; i < (int)mAudioCommands.size(); i++) {
641 mAudioCommands[i]->dump(buffer, SIZE);
642 result.append(buffer);
643 }
644 result.append(" Last Command\n");
645 mLastCommand.dump(buffer, SIZE);
646 result.append(buffer);
647
648 write(fd, result.string(), result.size());
649
650 if (locked) mLock.unlock();
651
652 return NO_ERROR;
653}
654
655void AudioPolicyService::AudioCommandThread::startToneCommand(int type, int stream)
656{
657 AudioCommand *command = new AudioCommand();
658 command->mCommand = START_TONE;
659 ToneData *data = new ToneData();
660 data->mType = type;
661 data->mStream = stream;
662 command->mParam = (void *)data;
663 command->mWaitStatus = false;
664 Mutex::Autolock _l(mLock);
665 insertCommand_l(command);
666 LOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
667 mWaitWorkCV.signal();
668}
669
670void AudioPolicyService::AudioCommandThread::stopToneCommand()
671{
672 AudioCommand *command = new AudioCommand();
673 command->mCommand = STOP_TONE;
674 command->mParam = NULL;
675 command->mWaitStatus = false;
676 Mutex::Autolock _l(mLock);
677 insertCommand_l(command);
678 LOGV("AudioCommandThread() adding tone stop");
679 mWaitWorkCV.signal();
680}
681
Eric Laurentde070132010-07-13 04:45:46 -0700682status_t AudioPolicyService::AudioCommandThread::volumeCommand(int stream,
683 float volume,
684 int output,
685 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700686{
687 status_t status = NO_ERROR;
688
689 AudioCommand *command = new AudioCommand();
690 command->mCommand = SET_VOLUME;
691 VolumeData *data = new VolumeData();
692 data->mStream = stream;
693 data->mVolume = volume;
694 data->mIO = output;
695 command->mParam = data;
696 if (delayMs == 0) {
697 command->mWaitStatus = true;
698 } else {
699 command->mWaitStatus = false;
700 }
701 Mutex::Autolock _l(mLock);
702 insertCommand_l(command, delayMs);
Eric Laurentde070132010-07-13 04:45:46 -0700703 LOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
704 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700705 mWaitWorkCV.signal();
706 if (command->mWaitStatus) {
707 command->mCond.wait(mLock);
708 status = command->mStatus;
709 mWaitWorkCV.signal();
710 }
711 return status;
712}
713
Eric Laurentde070132010-07-13 04:45:46 -0700714status_t AudioPolicyService::AudioCommandThread::parametersCommand(int ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700715 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700716 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700717{
718 status_t status = NO_ERROR;
719
720 AudioCommand *command = new AudioCommand();
721 command->mCommand = SET_PARAMETERS;
722 ParametersData *data = new ParametersData();
723 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700724 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700725 command->mParam = data;
726 if (delayMs == 0) {
727 command->mWaitStatus = true;
728 } else {
729 command->mWaitStatus = false;
730 }
731 Mutex::Autolock _l(mLock);
732 insertCommand_l(command, delayMs);
Eric Laurentde070132010-07-13 04:45:46 -0700733 LOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700734 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735 mWaitWorkCV.signal();
736 if (command->mWaitStatus) {
737 command->mCond.wait(mLock);
738 status = command->mStatus;
739 mWaitWorkCV.signal();
740 }
741 return status;
742}
743
744status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
745{
746 status_t status = NO_ERROR;
747
748 AudioCommand *command = new AudioCommand();
749 command->mCommand = SET_VOICE_VOLUME;
750 VoiceVolumeData *data = new VoiceVolumeData();
751 data->mVolume = volume;
752 command->mParam = data;
753 if (delayMs == 0) {
754 command->mWaitStatus = true;
755 } else {
756 command->mWaitStatus = false;
757 }
758 Mutex::Autolock _l(mLock);
759 insertCommand_l(command, delayMs);
760 LOGV("AudioCommandThread() adding set voice volume volume %f", volume);
761 mWaitWorkCV.signal();
762 if (command->mWaitStatus) {
763 command->mCond.wait(mLock);
764 status = command->mStatus;
765 mWaitWorkCV.signal();
766 }
767 return status;
768}
769
770// insertCommand_l() must be called with mLock held
771void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
772{
773 ssize_t i;
774 Vector <AudioCommand *> removedCommands;
775
776 command->mTime = systemTime() + milliseconds(delayMs);
777
778 // acquire wake lock to make sure delayed commands are processed
779 if (mName != "" && mAudioCommands.isEmpty()) {
780 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
781 }
782
783 // check same pending commands with later time stamps and eliminate them
784 for (i = mAudioCommands.size()-1; i >= 0; i--) {
785 AudioCommand *command2 = mAudioCommands[i];
786 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
787 if (command2->mTime <= command->mTime) break;
788 if (command2->mCommand != command->mCommand) continue;
789
790 switch (command->mCommand) {
791 case SET_PARAMETERS: {
792 ParametersData *data = (ParametersData *)command->mParam;
793 ParametersData *data2 = (ParametersData *)command2->mParam;
794 if (data->mIO != data2->mIO) break;
Eric Laurentde070132010-07-13 04:45:46 -0700795 LOGV("Comparing parameter command %s to new command %s",
796 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700797 AudioParameter param = AudioParameter(data->mKeyValuePairs);
798 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
799 for (size_t j = 0; j < param.size(); j++) {
800 String8 key;
801 String8 value;
802 param.getAt(j, key, value);
803 for (size_t k = 0; k < param2.size(); k++) {
804 String8 key2;
805 String8 value2;
806 param2.getAt(k, key2, value2);
807 if (key2 == key) {
808 param2.remove(key2);
809 LOGV("Filtering out parameter %s", key2.string());
810 break;
811 }
812 }
813 }
814 // if all keys have been filtered out, remove the command.
815 // otherwise, update the key value pairs
816 if (param2.size() == 0) {
817 removedCommands.add(command2);
818 } else {
819 data2->mKeyValuePairs = param2.toString();
820 }
821 } break;
822
823 case SET_VOLUME: {
824 VolumeData *data = (VolumeData *)command->mParam;
825 VolumeData *data2 = (VolumeData *)command2->mParam;
826 if (data->mIO != data2->mIO) break;
827 if (data->mStream != data2->mStream) break;
Eric Laurentde070132010-07-13 04:45:46 -0700828 LOGV("Filtering out volume command on output %d for stream %d",
829 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700830 removedCommands.add(command2);
831 } break;
832 case START_TONE:
833 case STOP_TONE:
834 default:
835 break;
836 }
837 }
838
839 // remove filtered commands
840 for (size_t j = 0; j < removedCommands.size(); j++) {
841 // removed commands always have time stamps greater than current command
842 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
843 if (mAudioCommands[k] == removedCommands[j]) {
844 LOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
845 mAudioCommands.removeAt(k);
846 break;
847 }
848 }
849 }
850 removedCommands.clear();
851
852 // insert command at the right place according to its time stamp
Eric Laurentde070132010-07-13 04:45:46 -0700853 LOGV("inserting command: %d at index %d, num commands %d",
854 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700855 mAudioCommands.insertAt(command, i + 1);
856}
857
858void AudioPolicyService::AudioCommandThread::exit()
859{
860 LOGV("AudioCommandThread::exit");
861 {
862 AutoMutex _l(mLock);
863 requestExit();
864 mWaitWorkCV.signal();
865 }
866 requestExitAndWait();
867}
868
869void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
870{
871 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
872 mCommand,
873 (int)ns2s(mTime),
874 (int)ns2ms(mTime)%1000,
875 mWaitStatus,
876 mParam);
877}
878
Dima Zavinfce7a472011-04-19 22:30:36 -0700879/******* helpers for the service_ops callbacks defined below *********/
880void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
881 const char *keyValuePairs,
882 int delayMs)
883{
884 mAudioCommandThread->parametersCommand((int)ioHandle, keyValuePairs,
885 delayMs);
886}
887
888int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
889 float volume,
890 audio_io_handle_t output,
891 int delayMs)
892{
893 return (int)mAudioCommandThread->volumeCommand((int)stream, volume,
894 (int)output, delayMs);
895}
896
897int AudioPolicyService::startTone(audio_policy_tone_t tone,
898 audio_stream_type_t stream)
899{
900 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
901 LOGE("startTone: illegal tone requested (%d)", tone);
902 if (stream != AUDIO_STREAM_VOICE_CALL)
903 LOGE("startTone: illegal stream (%d) requested for tone %d", stream,
904 tone);
905 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
906 AUDIO_STREAM_VOICE_CALL);
907 return 0;
908}
909
910int AudioPolicyService::stopTone()
911{
912 mTonePlaybackThread->stopToneCommand();
913 return 0;
914}
915
916int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
917{
918 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
919}
920
921/* implementation of the interface to the policy manager */
922extern "C" {
923
924static audio_io_handle_t aps_open_output(void *service,
925 uint32_t *pDevices,
926 uint32_t *pSamplingRate,
927 uint32_t *pFormat,
928 uint32_t *pChannels,
929 uint32_t *pLatencyMs,
930 audio_policy_output_flags_t flags)
931{
932 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
933 if (af == NULL) {
934 LOGW("%s: could not get AudioFlinger", __func__);
935 return 0;
936 }
937
938 return af->openOutput(pDevices, pSamplingRate, pFormat, pChannels,
939 pLatencyMs, flags);
940}
941
942static audio_io_handle_t aps_open_dup_output(void *service,
943 audio_io_handle_t output1,
944 audio_io_handle_t output2)
945{
946 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
947 if (af == NULL) {
948 LOGW("%s: could not get AudioFlinger", __func__);
949 return 0;
950 }
951 return af->openDuplicateOutput(output1, output2);
952}
953
954static int aps_close_output(void *service, audio_io_handle_t output)
955{
956 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
957 if (af == NULL)
958 return PERMISSION_DENIED;
959
960 return af->closeOutput(output);
961}
962
963static int aps_suspend_output(void *service, audio_io_handle_t output)
964{
965 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
966 if (af == NULL) {
967 LOGW("%s: could not get AudioFlinger", __func__);
968 return PERMISSION_DENIED;
969 }
970
971 return af->suspendOutput(output);
972}
973
974static int aps_restore_output(void *service, audio_io_handle_t output)
975{
976 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
977 if (af == NULL) {
978 LOGW("%s: could not get AudioFlinger", __func__);
979 return PERMISSION_DENIED;
980 }
981
982 return af->restoreOutput(output);
983}
984
985static audio_io_handle_t aps_open_input(void *service,
986 uint32_t *pDevices,
987 uint32_t *pSamplingRate,
988 uint32_t *pFormat,
989 uint32_t *pChannels,
990 uint32_t acoustics)
991{
992 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
993 if (af == NULL) {
994 LOGW("%s: could not get AudioFlinger", __func__);
995 return 0;
996 }
997
998 return af->openInput(pDevices, pSamplingRate, pFormat, pChannels,
999 acoustics);
1000}
1001
1002static int aps_close_input(void *service, audio_io_handle_t input)
1003{
1004 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1005 if (af == NULL)
1006 return PERMISSION_DENIED;
1007
1008 return af->closeInput(input);
1009}
1010
1011static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1012 audio_io_handle_t output)
1013{
1014 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1015 if (af == NULL)
1016 return PERMISSION_DENIED;
1017
1018 return af->setStreamOutput(stream, output);
1019}
1020
1021static int aps_move_effects(void *service, int session,
1022 audio_io_handle_t src_output,
1023 audio_io_handle_t dst_output)
1024{
1025 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1026 if (af == NULL)
1027 return PERMISSION_DENIED;
1028
1029 return af->moveEffects(session, (int)src_output, (int)dst_output);
1030}
1031
1032static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1033 const char *keys)
1034{
1035 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1036 return strdup(result.string());
1037}
1038
1039static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1040 const char *kv_pairs, int delay_ms)
1041{
1042 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1043
1044 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1045}
1046
1047static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1048 float volume, audio_io_handle_t output,
1049 int delay_ms)
1050{
1051 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1052
1053 return audioPolicyService->setStreamVolume(stream, volume, output,
1054 delay_ms);
1055}
1056
1057static int aps_start_tone(void *service, audio_policy_tone_t tone,
1058 audio_stream_type_t stream)
1059{
1060 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1061
1062 return audioPolicyService->startTone(tone, stream);
1063}
1064
1065static int aps_stop_tone(void *service)
1066{
1067 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1068
1069 return audioPolicyService->stopTone();
1070}
1071
1072static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1073{
1074 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1075
1076 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1077}
1078
1079}; // extern "C"
1080
1081namespace {
1082 struct audio_policy_service_ops aps_ops = {
1083 open_output : aps_open_output,
1084 open_duplicate_output : aps_open_dup_output,
1085 close_output : aps_close_output,
1086 suspend_output : aps_suspend_output,
1087 restore_output : aps_restore_output,
1088 open_input : aps_open_input,
1089 close_input : aps_close_input,
1090 set_stream_volume : aps_set_stream_volume,
1091 set_stream_output : aps_set_stream_output,
1092 set_parameters : aps_set_parameters,
1093 get_parameters : aps_get_parameters,
1094 start_tone : aps_start_tone,
1095 stop_tone : aps_stop_tone,
1096 set_voice_volume : aps_set_voice_volume,
1097 move_effects : aps_move_effects,
1098 };
1099}; // namespace <unnamed>
1100
Mathias Agopian65ab4712010-07-14 17:59:35 -07001101}; // namespace android