blob: eebc1b3464012e7a762470aafc5e9c79ecabcd8d [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>
38#include <hardware/audio.h>
39#include <hardware/audio_policy.h>
40#include <hardware/audio_policy_hal.h>
41
Mathias Agopian65ab4712010-07-14 17:59:35 -070042// ----------------------------------------------------------------------------
43// the sim build doesn't have gettid
44
45#ifndef HAVE_GETTID
46# define gettid getpid
47#endif
48
49namespace android {
50
Mathias Agopian65ab4712010-07-14 17:59:35 -070051static const char *kDeadlockedString = "AudioPolicyService may be deadlocked\n";
52static const char *kCmdDeadlockedString = "AudioPolicyService command thread may be deadlocked\n";
53
54static const int kDumpLockRetries = 50;
55static const int kDumpLockSleep = 20000;
56
57static bool checkPermission() {
58#ifndef HAVE_ANDROID_OS
59 return true;
60#endif
61 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
62 bool ok = checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS"));
63 if (!ok) LOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS");
64 return ok;
65}
66
Dima Zavinfce7a472011-04-19 22:30:36 -070067namespace {
68 extern struct audio_policy_service_ops aps_ops;
69};
70
Mathias Agopian65ab4712010-07-14 17:59:35 -070071// ----------------------------------------------------------------------------
72
73AudioPolicyService::AudioPolicyService()
Dima Zavinfce7a472011-04-19 22:30:36 -070074 : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL)
Mathias Agopian65ab4712010-07-14 17:59:35 -070075{
76 char value[PROPERTY_VALUE_MAX];
Dima Zavinfce7a472011-04-19 22:30:36 -070077 const struct hw_module_t *module;
78 int forced_val;
79 int rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -070080
Eric Laurent93575202011-01-18 18:39:02 -080081 Mutex::Autolock _l(mLock);
82
Mathias Agopian65ab4712010-07-14 17:59:35 -070083 // start tone playback thread
84 mTonePlaybackThread = new AudioCommandThread(String8(""));
85 // start audio commands thread
86 mAudioCommandThread = new AudioCommandThread(String8("ApmCommandThread"));
87
Dima Zavinfce7a472011-04-19 22:30:36 -070088 /* instantiate the audio policy manager */
89 rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
90 if (rc)
91 return;
Mathias Agopian65ab4712010-07-14 17:59:35 -070092
Dima Zavinfce7a472011-04-19 22:30:36 -070093 rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
94 LOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
95 if (rc)
96 return;
Eric Laurent93575202011-01-18 18:39:02 -080097
Dima Zavinfce7a472011-04-19 22:30:36 -070098 rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
99 &mpAudioPolicy);
100 LOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
101 if (rc)
102 return;
103
104 rc = mpAudioPolicy->init_check(mpAudioPolicy);
105 LOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
106 if (rc)
107 return;
108
109 property_get("ro.camera.sound.forced", value, "0");
110 forced_val = strtol(value, NULL, 0);
111 mpAudioPolicy->set_can_mute_enforced_audible(mpAudioPolicy, !forced_val);
112
113 LOGI("Loaded audio policy from %s (%s)", module->name, module->id);
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
Dima Zavinfce7a472011-04-19 22:30:36 -0700123 if (mpAudioPolicy && mpAudioPolicyDev)
124 mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
125 if (mpAudioPolicyDev)
126 audio_policy_dev_close(mpAudioPolicyDev);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700127}
128
Dima Zavinfce7a472011-04-19 22:30:36 -0700129status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
130 audio_policy_dev_state_t state,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700131 const char *device_address)
132{
Dima Zavinfce7a472011-04-19 22:30:36 -0700133 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700134 return NO_INIT;
135 }
136 if (!checkPermission()) {
137 return PERMISSION_DENIED;
138 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700139 if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700140 return BAD_VALUE;
141 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700142 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
143 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700144 return BAD_VALUE;
145 }
146
147 LOGV("setDeviceConnectionState() tid %d", gettid());
148 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700149 return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device,
150 state, device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700151}
152
Dima Zavinfce7a472011-04-19 22:30:36 -0700153audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
154 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -0700155 const char *device_address)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700156{
Dima Zavinfce7a472011-04-19 22:30:36 -0700157 if (mpAudioPolicy == NULL) {
158 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700159 }
160 if (!checkPermission()) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700161 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700162 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700163 return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device,
164 device_address);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700165}
166
167status_t AudioPolicyService::setPhoneState(int state)
168{
Dima Zavinfce7a472011-04-19 22:30:36 -0700169 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700170 return NO_INIT;
171 }
172 if (!checkPermission()) {
173 return PERMISSION_DENIED;
174 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700175 if (state < 0 || state >= AUDIO_MODE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700176 return BAD_VALUE;
177 }
178
179 LOGV("setPhoneState() tid %d", gettid());
180
181 // TODO: check if it is more appropriate to do it in platform specific policy manager
182 AudioSystem::setMode(state);
183
184 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700185 mpAudioPolicy->set_phone_state(mpAudioPolicy, state);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700186 return NO_ERROR;
187}
188
189status_t AudioPolicyService::setRingerMode(uint32_t mode, uint32_t mask)
190{
Dima Zavinfce7a472011-04-19 22:30:36 -0700191 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700192 return NO_INIT;
193 }
194 if (!checkPermission()) {
195 return PERMISSION_DENIED;
196 }
197
Dima Zavinfce7a472011-04-19 22:30:36 -0700198 mpAudioPolicy->set_ringer_mode(mpAudioPolicy, mode, mask);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700199 return NO_ERROR;
200}
201
Dima Zavinfce7a472011-04-19 22:30:36 -0700202status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
203 audio_policy_forced_cfg_t config)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700204{
Dima Zavinfce7a472011-04-19 22:30:36 -0700205 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700206 return NO_INIT;
207 }
208 if (!checkPermission()) {
209 return PERMISSION_DENIED;
210 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700211 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700212 return BAD_VALUE;
213 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700214 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700215 return BAD_VALUE;
216 }
217 LOGV("setForceUse() tid %d", gettid());
218 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700219 mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700220 return NO_ERROR;
221}
222
Dima Zavinfce7a472011-04-19 22:30:36 -0700223audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700224{
Dima Zavinfce7a472011-04-19 22:30:36 -0700225 if (mpAudioPolicy == NULL) {
226 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700227 }
228 if (!checkPermission()) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700229 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700230 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700231 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
232 return AUDIO_POLICY_FORCE_NONE;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700233 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700234 return mpAudioPolicy->get_force_use(mpAudioPolicy, usage);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700235}
236
Dima Zavinfce7a472011-04-19 22:30:36 -0700237audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700238 uint32_t samplingRate,
239 uint32_t format,
240 uint32_t channels,
Dima Zavinfce7a472011-04-19 22:30:36 -0700241 audio_policy_output_flags_t flags)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242{
Dima Zavinfce7a472011-04-19 22:30:36 -0700243 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244 return 0;
245 }
246 LOGV("getOutput() tid %d", gettid());
247 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700248 return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, format, channels, flags);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700249}
250
Eric Laurentde070132010-07-13 04:45:46 -0700251status_t AudioPolicyService::startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700252 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700253 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700254{
Dima Zavinfce7a472011-04-19 22:30:36 -0700255 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700256 return NO_INIT;
257 }
258 LOGV("startOutput() tid %d", gettid());
259 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700260 return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700261}
262
Eric Laurentde070132010-07-13 04:45:46 -0700263status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700264 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700265 int session)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700266{
Dima Zavinfce7a472011-04-19 22:30:36 -0700267 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700268 return NO_INIT;
269 }
270 LOGV("stopOutput() tid %d", gettid());
271 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700272 return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700273}
274
275void AudioPolicyService::releaseOutput(audio_io_handle_t output)
276{
Dima Zavinfce7a472011-04-19 22:30:36 -0700277 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700278 return;
279 }
280 LOGV("releaseOutput() tid %d", gettid());
281 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700282 mpAudioPolicy->release_output(mpAudioPolicy, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700283}
284
285audio_io_handle_t AudioPolicyService::getInput(int inputSource,
286 uint32_t samplingRate,
287 uint32_t format,
288 uint32_t channels,
Dima Zavinfce7a472011-04-19 22:30:36 -0700289 audio_in_acoustics_t acoustics)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700290{
Dima Zavinfce7a472011-04-19 22:30:36 -0700291 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700292 return 0;
293 }
294 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700295 return mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate, format, channels, acoustics);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700296}
297
298status_t AudioPolicyService::startInput(audio_io_handle_t input)
299{
Dima Zavinfce7a472011-04-19 22:30:36 -0700300 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700301 return NO_INIT;
302 }
303 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700304 return mpAudioPolicy->start_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305}
306
307status_t AudioPolicyService::stopInput(audio_io_handle_t input)
308{
Dima Zavinfce7a472011-04-19 22:30:36 -0700309 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310 return NO_INIT;
311 }
312 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700313 return mpAudioPolicy->stop_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700314}
315
316void AudioPolicyService::releaseInput(audio_io_handle_t input)
317{
Dima Zavinfce7a472011-04-19 22:30:36 -0700318 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700319 return;
320 }
321 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700322 mpAudioPolicy->release_input(mpAudioPolicy, input);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700323}
324
Dima Zavinfce7a472011-04-19 22:30:36 -0700325status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700326 int indexMin,
327 int indexMax)
328{
Dima Zavinfce7a472011-04-19 22:30:36 -0700329 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700330 return NO_INIT;
331 }
332 if (!checkPermission()) {
333 return PERMISSION_DENIED;
334 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700335 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336 return BAD_VALUE;
337 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700338 mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700339 return NO_ERROR;
340}
341
Dima Zavinfce7a472011-04-19 22:30:36 -0700342status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream, int index)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700343{
Dima Zavinfce7a472011-04-19 22:30:36 -0700344 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345 return NO_INIT;
346 }
347 if (!checkPermission()) {
348 return PERMISSION_DENIED;
349 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700350 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351 return BAD_VALUE;
352 }
353
Dima Zavinfce7a472011-04-19 22:30:36 -0700354 return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700355}
356
Dima Zavinfce7a472011-04-19 22:30:36 -0700357status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream, int *index)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700358{
Dima Zavinfce7a472011-04-19 22:30:36 -0700359 if (mpAudioPolicy == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360 return NO_INIT;
361 }
362 if (!checkPermission()) {
363 return PERMISSION_DENIED;
364 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700365 if (stream < 0 || stream >= AUDIO_STREAM_CNT) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700366 return BAD_VALUE;
367 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700368 return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700369}
370
Dima Zavinfce7a472011-04-19 22:30:36 -0700371uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700372{
Dima Zavinfce7a472011-04-19 22:30:36 -0700373 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700374 return 0;
375 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700376 return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
Eric Laurentde070132010-07-13 04:45:46 -0700377}
378
Dima Zavinfce7a472011-04-19 22:30:36 -0700379uint32_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800380{
Dima Zavinfce7a472011-04-19 22:30:36 -0700381 if (mpAudioPolicy == NULL) {
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800382 return 0;
383 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700384 return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800385}
386
Eric Laurentde070132010-07-13 04:45:46 -0700387audio_io_handle_t AudioPolicyService::getOutputForEffect(effect_descriptor_t *desc)
388{
Dima Zavinfce7a472011-04-19 22:30:36 -0700389 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700390 return NO_INIT;
391 }
392 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700393 return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
Eric Laurentde070132010-07-13 04:45:46 -0700394}
395
396status_t AudioPolicyService::registerEffect(effect_descriptor_t *desc,
397 audio_io_handle_t output,
398 uint32_t strategy,
399 int session,
400 int id)
401{
Dima Zavinfce7a472011-04-19 22:30:36 -0700402 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700403 return NO_INIT;
404 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700405 return mpAudioPolicy->register_effect(mpAudioPolicy, desc, output, strategy, session, id);
Eric Laurentde070132010-07-13 04:45:46 -0700406}
407
408status_t AudioPolicyService::unregisterEffect(int id)
409{
Dima Zavinfce7a472011-04-19 22:30:36 -0700410 if (mpAudioPolicy == NULL) {
Eric Laurentde070132010-07-13 04:45:46 -0700411 return NO_INIT;
412 }
Dima Zavinfce7a472011-04-19 22:30:36 -0700413 return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
Eric Laurentde070132010-07-13 04:45:46 -0700414}
415
Eric Laurenteda6c362011-02-02 09:33:30 -0800416bool AudioPolicyService::isStreamActive(int stream, uint32_t inPastMs) const
417{
Dima Zavinfce7a472011-04-19 22:30:36 -0700418 if (mpAudioPolicy == NULL) {
Eric Laurenteda6c362011-02-02 09:33:30 -0800419 return 0;
420 }
421 Mutex::Autolock _l(mLock);
Dima Zavinfce7a472011-04-19 22:30:36 -0700422 return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
Eric Laurenteda6c362011-02-02 09:33:30 -0800423}
424
Mathias Agopian65ab4712010-07-14 17:59:35 -0700425void AudioPolicyService::binderDied(const wp<IBinder>& who) {
Eric Laurentde070132010-07-13 04:45:46 -0700426 LOGW("binderDied() %p, tid %d, calling tid %d", who.unsafe_get(), gettid(),
427 IPCThreadState::self()->getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700428}
429
430static bool tryLock(Mutex& mutex)
431{
432 bool locked = false;
433 for (int i = 0; i < kDumpLockRetries; ++i) {
434 if (mutex.tryLock() == NO_ERROR) {
435 locked = true;
436 break;
437 }
438 usleep(kDumpLockSleep);
439 }
440 return locked;
441}
442
443status_t AudioPolicyService::dumpInternals(int fd)
444{
445 const size_t SIZE = 256;
446 char buffer[SIZE];
447 String8 result;
448
Dima Zavinfce7a472011-04-19 22:30:36 -0700449 snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700450 result.append(buffer);
451 snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
452 result.append(buffer);
453 snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
454 result.append(buffer);
455
456 write(fd, result.string(), result.size());
457 return NO_ERROR;
458}
459
460status_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
461{
462 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
463 dumpPermissionDenial(fd);
464 } else {
465 bool locked = tryLock(mLock);
466 if (!locked) {
467 String8 result(kDeadlockedString);
468 write(fd, result.string(), result.size());
469 }
470
471 dumpInternals(fd);
472 if (mAudioCommandThread != NULL) {
473 mAudioCommandThread->dump(fd);
474 }
475 if (mTonePlaybackThread != NULL) {
476 mTonePlaybackThread->dump(fd);
477 }
478
Dima Zavinfce7a472011-04-19 22:30:36 -0700479 if (mpAudioPolicy) {
480 mpAudioPolicy->dump(mpAudioPolicy, fd);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700481 }
482
483 if (locked) mLock.unlock();
484 }
485 return NO_ERROR;
486}
487
488status_t AudioPolicyService::dumpPermissionDenial(int fd)
489{
490 const size_t SIZE = 256;
491 char buffer[SIZE];
492 String8 result;
493 snprintf(buffer, SIZE, "Permission Denial: "
494 "can't dump AudioPolicyService from pid=%d, uid=%d\n",
495 IPCThreadState::self()->getCallingPid(),
496 IPCThreadState::self()->getCallingUid());
497 result.append(buffer);
498 write(fd, result.string(), result.size());
499 return NO_ERROR;
500}
501
502status_t AudioPolicyService::onTransact(
503 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
504{
505 return BnAudioPolicyService::onTransact(code, data, reply, flags);
506}
507
508
Mathias Agopian65ab4712010-07-14 17:59:35 -0700509// ----------- AudioPolicyService::AudioCommandThread implementation ----------
510
511AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name)
512 : Thread(false), mName(name)
513{
514 mpToneGenerator = NULL;
515}
516
517
518AudioPolicyService::AudioCommandThread::~AudioCommandThread()
519{
520 if (mName != "" && !mAudioCommands.isEmpty()) {
521 release_wake_lock(mName.string());
522 }
523 mAudioCommands.clear();
524 if (mpToneGenerator != NULL) delete mpToneGenerator;
525}
526
527void AudioPolicyService::AudioCommandThread::onFirstRef()
528{
529 if (mName != "") {
530 run(mName.string(), ANDROID_PRIORITY_AUDIO);
531 } else {
532 run("AudioCommandThread", ANDROID_PRIORITY_AUDIO);
533 }
534}
535
536bool AudioPolicyService::AudioCommandThread::threadLoop()
537{
538 nsecs_t waitTime = INT64_MAX;
539
540 mLock.lock();
541 while (!exitPending())
542 {
543 while(!mAudioCommands.isEmpty()) {
544 nsecs_t curTime = systemTime();
545 // commands are sorted by increasing time stamp: execute them from index 0 and up
546 if (mAudioCommands[0]->mTime <= curTime) {
547 AudioCommand *command = mAudioCommands[0];
548 mAudioCommands.removeAt(0);
549 mLastCommand = *command;
550
551 switch (command->mCommand) {
552 case START_TONE: {
553 mLock.unlock();
554 ToneData *data = (ToneData *)command->mParam;
555 LOGV("AudioCommandThread() processing start tone %d on stream %d",
556 data->mType, data->mStream);
557 if (mpToneGenerator != NULL)
558 delete mpToneGenerator;
559 mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
560 mpToneGenerator->startTone(data->mType);
561 delete data;
562 mLock.lock();
563 }break;
564 case STOP_TONE: {
565 mLock.unlock();
566 LOGV("AudioCommandThread() processing stop tone");
567 if (mpToneGenerator != NULL) {
568 mpToneGenerator->stopTone();
569 delete mpToneGenerator;
570 mpToneGenerator = NULL;
571 }
572 mLock.lock();
573 }break;
574 case SET_VOLUME: {
575 VolumeData *data = (VolumeData *)command->mParam;
Eric Laurentde070132010-07-13 04:45:46 -0700576 LOGV("AudioCommandThread() processing set volume stream %d, \
577 volume %f, output %d", data->mStream, data->mVolume, data->mIO);
578 command->mStatus = AudioSystem::setStreamVolume(data->mStream,
579 data->mVolume,
580 data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700581 if (command->mWaitStatus) {
582 command->mCond.signal();
583 mWaitWorkCV.wait(mLock);
584 }
585 delete data;
586 }break;
587 case SET_PARAMETERS: {
588 ParametersData *data = (ParametersData *)command->mParam;
Eric Laurentde070132010-07-13 04:45:46 -0700589 LOGV("AudioCommandThread() processing set parameters string %s, io %d",
590 data->mKeyValuePairs.string(), data->mIO);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700591 command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
592 if (command->mWaitStatus) {
593 command->mCond.signal();
594 mWaitWorkCV.wait(mLock);
595 }
596 delete data;
597 }break;
598 case SET_VOICE_VOLUME: {
599 VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
Eric Laurentde070132010-07-13 04:45:46 -0700600 LOGV("AudioCommandThread() processing set voice volume volume %f",
601 data->mVolume);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700602 command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
603 if (command->mWaitStatus) {
604 command->mCond.signal();
605 mWaitWorkCV.wait(mLock);
606 }
607 delete data;
608 }break;
609 default:
610 LOGW("AudioCommandThread() unknown command %d", command->mCommand);
611 }
612 delete command;
613 waitTime = INT64_MAX;
614 } else {
615 waitTime = mAudioCommands[0]->mTime - curTime;
616 break;
617 }
618 }
619 // release delayed commands wake lock
620 if (mName != "" && mAudioCommands.isEmpty()) {
621 release_wake_lock(mName.string());
622 }
623 LOGV("AudioCommandThread() going to sleep");
624 mWaitWorkCV.waitRelative(mLock, waitTime);
625 LOGV("AudioCommandThread() waking up");
626 }
627 mLock.unlock();
628 return false;
629}
630
631status_t AudioPolicyService::AudioCommandThread::dump(int fd)
632{
633 const size_t SIZE = 256;
634 char buffer[SIZE];
635 String8 result;
636
637 snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
638 result.append(buffer);
639 write(fd, result.string(), result.size());
640
641 bool locked = tryLock(mLock);
642 if (!locked) {
643 String8 result2(kCmdDeadlockedString);
644 write(fd, result2.string(), result2.size());
645 }
646
647 snprintf(buffer, SIZE, "- Commands:\n");
648 result = String8(buffer);
649 result.append(" Command Time Wait pParam\n");
650 for (int i = 0; i < (int)mAudioCommands.size(); i++) {
651 mAudioCommands[i]->dump(buffer, SIZE);
652 result.append(buffer);
653 }
654 result.append(" Last Command\n");
655 mLastCommand.dump(buffer, SIZE);
656 result.append(buffer);
657
658 write(fd, result.string(), result.size());
659
660 if (locked) mLock.unlock();
661
662 return NO_ERROR;
663}
664
665void AudioPolicyService::AudioCommandThread::startToneCommand(int type, int stream)
666{
667 AudioCommand *command = new AudioCommand();
668 command->mCommand = START_TONE;
669 ToneData *data = new ToneData();
670 data->mType = type;
671 data->mStream = stream;
672 command->mParam = (void *)data;
673 command->mWaitStatus = false;
674 Mutex::Autolock _l(mLock);
675 insertCommand_l(command);
676 LOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
677 mWaitWorkCV.signal();
678}
679
680void AudioPolicyService::AudioCommandThread::stopToneCommand()
681{
682 AudioCommand *command = new AudioCommand();
683 command->mCommand = STOP_TONE;
684 command->mParam = NULL;
685 command->mWaitStatus = false;
686 Mutex::Autolock _l(mLock);
687 insertCommand_l(command);
688 LOGV("AudioCommandThread() adding tone stop");
689 mWaitWorkCV.signal();
690}
691
Eric Laurentde070132010-07-13 04:45:46 -0700692status_t AudioPolicyService::AudioCommandThread::volumeCommand(int stream,
693 float volume,
694 int output,
695 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700696{
697 status_t status = NO_ERROR;
698
699 AudioCommand *command = new AudioCommand();
700 command->mCommand = SET_VOLUME;
701 VolumeData *data = new VolumeData();
702 data->mStream = stream;
703 data->mVolume = volume;
704 data->mIO = output;
705 command->mParam = data;
706 if (delayMs == 0) {
707 command->mWaitStatus = true;
708 } else {
709 command->mWaitStatus = false;
710 }
711 Mutex::Autolock _l(mLock);
712 insertCommand_l(command, delayMs);
Eric Laurentde070132010-07-13 04:45:46 -0700713 LOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
714 stream, volume, output);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700715 mWaitWorkCV.signal();
716 if (command->mWaitStatus) {
717 command->mCond.wait(mLock);
718 status = command->mStatus;
719 mWaitWorkCV.signal();
720 }
721 return status;
722}
723
Eric Laurentde070132010-07-13 04:45:46 -0700724status_t AudioPolicyService::AudioCommandThread::parametersCommand(int ioHandle,
Dima Zavinfce7a472011-04-19 22:30:36 -0700725 const char *keyValuePairs,
Eric Laurentde070132010-07-13 04:45:46 -0700726 int delayMs)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700727{
728 status_t status = NO_ERROR;
729
730 AudioCommand *command = new AudioCommand();
731 command->mCommand = SET_PARAMETERS;
732 ParametersData *data = new ParametersData();
733 data->mIO = ioHandle;
Dima Zavinfce7a472011-04-19 22:30:36 -0700734 data->mKeyValuePairs = String8(keyValuePairs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735 command->mParam = data;
736 if (delayMs == 0) {
737 command->mWaitStatus = true;
738 } else {
739 command->mWaitStatus = false;
740 }
741 Mutex::Autolock _l(mLock);
742 insertCommand_l(command, delayMs);
Eric Laurentde070132010-07-13 04:45:46 -0700743 LOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
Dima Zavinfce7a472011-04-19 22:30:36 -0700744 keyValuePairs, ioHandle, delayMs);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700745 mWaitWorkCV.signal();
746 if (command->mWaitStatus) {
747 command->mCond.wait(mLock);
748 status = command->mStatus;
749 mWaitWorkCV.signal();
750 }
751 return status;
752}
753
754status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
755{
756 status_t status = NO_ERROR;
757
758 AudioCommand *command = new AudioCommand();
759 command->mCommand = SET_VOICE_VOLUME;
760 VoiceVolumeData *data = new VoiceVolumeData();
761 data->mVolume = volume;
762 command->mParam = data;
763 if (delayMs == 0) {
764 command->mWaitStatus = true;
765 } else {
766 command->mWaitStatus = false;
767 }
768 Mutex::Autolock _l(mLock);
769 insertCommand_l(command, delayMs);
770 LOGV("AudioCommandThread() adding set voice volume volume %f", volume);
771 mWaitWorkCV.signal();
772 if (command->mWaitStatus) {
773 command->mCond.wait(mLock);
774 status = command->mStatus;
775 mWaitWorkCV.signal();
776 }
777 return status;
778}
779
780// insertCommand_l() must be called with mLock held
781void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
782{
783 ssize_t i;
784 Vector <AudioCommand *> removedCommands;
785
786 command->mTime = systemTime() + milliseconds(delayMs);
787
788 // acquire wake lock to make sure delayed commands are processed
789 if (mName != "" && mAudioCommands.isEmpty()) {
790 acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
791 }
792
793 // check same pending commands with later time stamps and eliminate them
794 for (i = mAudioCommands.size()-1; i >= 0; i--) {
795 AudioCommand *command2 = mAudioCommands[i];
796 // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
797 if (command2->mTime <= command->mTime) break;
798 if (command2->mCommand != command->mCommand) continue;
799
800 switch (command->mCommand) {
801 case SET_PARAMETERS: {
802 ParametersData *data = (ParametersData *)command->mParam;
803 ParametersData *data2 = (ParametersData *)command2->mParam;
804 if (data->mIO != data2->mIO) break;
Eric Laurentde070132010-07-13 04:45:46 -0700805 LOGV("Comparing parameter command %s to new command %s",
806 data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700807 AudioParameter param = AudioParameter(data->mKeyValuePairs);
808 AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
809 for (size_t j = 0; j < param.size(); j++) {
810 String8 key;
811 String8 value;
812 param.getAt(j, key, value);
813 for (size_t k = 0; k < param2.size(); k++) {
814 String8 key2;
815 String8 value2;
816 param2.getAt(k, key2, value2);
817 if (key2 == key) {
818 param2.remove(key2);
819 LOGV("Filtering out parameter %s", key2.string());
820 break;
821 }
822 }
823 }
824 // if all keys have been filtered out, remove the command.
825 // otherwise, update the key value pairs
826 if (param2.size() == 0) {
827 removedCommands.add(command2);
828 } else {
829 data2->mKeyValuePairs = param2.toString();
830 }
831 } break;
832
833 case SET_VOLUME: {
834 VolumeData *data = (VolumeData *)command->mParam;
835 VolumeData *data2 = (VolumeData *)command2->mParam;
836 if (data->mIO != data2->mIO) break;
837 if (data->mStream != data2->mStream) break;
Eric Laurentde070132010-07-13 04:45:46 -0700838 LOGV("Filtering out volume command on output %d for stream %d",
839 data->mIO, data->mStream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700840 removedCommands.add(command2);
841 } break;
842 case START_TONE:
843 case STOP_TONE:
844 default:
845 break;
846 }
847 }
848
849 // remove filtered commands
850 for (size_t j = 0; j < removedCommands.size(); j++) {
851 // removed commands always have time stamps greater than current command
852 for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
853 if (mAudioCommands[k] == removedCommands[j]) {
854 LOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
855 mAudioCommands.removeAt(k);
856 break;
857 }
858 }
859 }
860 removedCommands.clear();
861
862 // insert command at the right place according to its time stamp
Eric Laurentde070132010-07-13 04:45:46 -0700863 LOGV("inserting command: %d at index %d, num commands %d",
864 command->mCommand, (int)i+1, mAudioCommands.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700865 mAudioCommands.insertAt(command, i + 1);
866}
867
868void AudioPolicyService::AudioCommandThread::exit()
869{
870 LOGV("AudioCommandThread::exit");
871 {
872 AutoMutex _l(mLock);
873 requestExit();
874 mWaitWorkCV.signal();
875 }
876 requestExitAndWait();
877}
878
879void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
880{
881 snprintf(buffer, size, " %02d %06d.%03d %01u %p\n",
882 mCommand,
883 (int)ns2s(mTime),
884 (int)ns2ms(mTime)%1000,
885 mWaitStatus,
886 mParam);
887}
888
Dima Zavinfce7a472011-04-19 22:30:36 -0700889/******* helpers for the service_ops callbacks defined below *********/
890void AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
891 const char *keyValuePairs,
892 int delayMs)
893{
894 mAudioCommandThread->parametersCommand((int)ioHandle, keyValuePairs,
895 delayMs);
896}
897
898int AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
899 float volume,
900 audio_io_handle_t output,
901 int delayMs)
902{
903 return (int)mAudioCommandThread->volumeCommand((int)stream, volume,
904 (int)output, delayMs);
905}
906
907int AudioPolicyService::startTone(audio_policy_tone_t tone,
908 audio_stream_type_t stream)
909{
910 if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
911 LOGE("startTone: illegal tone requested (%d)", tone);
912 if (stream != AUDIO_STREAM_VOICE_CALL)
913 LOGE("startTone: illegal stream (%d) requested for tone %d", stream,
914 tone);
915 mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
916 AUDIO_STREAM_VOICE_CALL);
917 return 0;
918}
919
920int AudioPolicyService::stopTone()
921{
922 mTonePlaybackThread->stopToneCommand();
923 return 0;
924}
925
926int AudioPolicyService::setVoiceVolume(float volume, int delayMs)
927{
928 return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
929}
930
931/* implementation of the interface to the policy manager */
932extern "C" {
933
934static audio_io_handle_t aps_open_output(void *service,
935 uint32_t *pDevices,
936 uint32_t *pSamplingRate,
937 uint32_t *pFormat,
938 uint32_t *pChannels,
939 uint32_t *pLatencyMs,
940 audio_policy_output_flags_t flags)
941{
942 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
943 if (af == NULL) {
944 LOGW("%s: could not get AudioFlinger", __func__);
945 return 0;
946 }
947
948 return af->openOutput(pDevices, pSamplingRate, pFormat, pChannels,
949 pLatencyMs, flags);
950}
951
952static audio_io_handle_t aps_open_dup_output(void *service,
953 audio_io_handle_t output1,
954 audio_io_handle_t output2)
955{
956 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
957 if (af == NULL) {
958 LOGW("%s: could not get AudioFlinger", __func__);
959 return 0;
960 }
961 return af->openDuplicateOutput(output1, output2);
962}
963
964static int aps_close_output(void *service, audio_io_handle_t output)
965{
966 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
967 if (af == NULL)
968 return PERMISSION_DENIED;
969
970 return af->closeOutput(output);
971}
972
973static int aps_suspend_output(void *service, audio_io_handle_t output)
974{
975 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
976 if (af == NULL) {
977 LOGW("%s: could not get AudioFlinger", __func__);
978 return PERMISSION_DENIED;
979 }
980
981 return af->suspendOutput(output);
982}
983
984static int aps_restore_output(void *service, audio_io_handle_t output)
985{
986 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
987 if (af == NULL) {
988 LOGW("%s: could not get AudioFlinger", __func__);
989 return PERMISSION_DENIED;
990 }
991
992 return af->restoreOutput(output);
993}
994
995static audio_io_handle_t aps_open_input(void *service,
996 uint32_t *pDevices,
997 uint32_t *pSamplingRate,
998 uint32_t *pFormat,
999 uint32_t *pChannels,
1000 uint32_t acoustics)
1001{
1002 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1003 if (af == NULL) {
1004 LOGW("%s: could not get AudioFlinger", __func__);
1005 return 0;
1006 }
1007
1008 return af->openInput(pDevices, pSamplingRate, pFormat, pChannels,
1009 acoustics);
1010}
1011
1012static int aps_close_input(void *service, audio_io_handle_t input)
1013{
1014 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1015 if (af == NULL)
1016 return PERMISSION_DENIED;
1017
1018 return af->closeInput(input);
1019}
1020
1021static int aps_set_stream_output(void *service, audio_stream_type_t stream,
1022 audio_io_handle_t output)
1023{
1024 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1025 if (af == NULL)
1026 return PERMISSION_DENIED;
1027
1028 return af->setStreamOutput(stream, output);
1029}
1030
1031static int aps_move_effects(void *service, int session,
1032 audio_io_handle_t src_output,
1033 audio_io_handle_t dst_output)
1034{
1035 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
1036 if (af == NULL)
1037 return PERMISSION_DENIED;
1038
1039 return af->moveEffects(session, (int)src_output, (int)dst_output);
1040}
1041
1042static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1043 const char *keys)
1044{
1045 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1046 return strdup(result.string());
1047}
1048
1049static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1050 const char *kv_pairs, int delay_ms)
1051{
1052 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1053
1054 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1055}
1056
1057static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1058 float volume, audio_io_handle_t output,
1059 int delay_ms)
1060{
1061 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1062
1063 return audioPolicyService->setStreamVolume(stream, volume, output,
1064 delay_ms);
1065}
1066
1067static int aps_start_tone(void *service, audio_policy_tone_t tone,
1068 audio_stream_type_t stream)
1069{
1070 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1071
1072 return audioPolicyService->startTone(tone, stream);
1073}
1074
1075static int aps_stop_tone(void *service)
1076{
1077 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1078
1079 return audioPolicyService->stopTone();
1080}
1081
1082static int aps_set_voice_volume(void *service, float volume, int delay_ms)
1083{
1084 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1085
1086 return audioPolicyService->setVoiceVolume(volume, delay_ms);
1087}
1088
1089}; // extern "C"
1090
1091namespace {
1092 struct audio_policy_service_ops aps_ops = {
1093 open_output : aps_open_output,
1094 open_duplicate_output : aps_open_dup_output,
1095 close_output : aps_close_output,
1096 suspend_output : aps_suspend_output,
1097 restore_output : aps_restore_output,
1098 open_input : aps_open_input,
1099 close_input : aps_close_input,
1100 set_stream_volume : aps_set_stream_volume,
1101 set_stream_output : aps_set_stream_output,
1102 set_parameters : aps_set_parameters,
1103 get_parameters : aps_get_parameters,
1104 start_tone : aps_start_tone,
1105 stop_tone : aps_stop_tone,
1106 set_voice_volume : aps_set_voice_volume,
1107 move_effects : aps_move_effects,
1108 };
1109}; // namespace <unnamed>
1110
Mathias Agopian65ab4712010-07-14 17:59:35 -07001111}; // namespace android