| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 33 | #include <cutils/properties.h> | 
|  | 34 | #include <dlfcn.h> | 
|  | 35 | #include <hardware_legacy/power.h> | 
|  | 36 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 37 | #include <hardware/hardware.h> | 
| Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 38 | #include <system/audio.h> | 
| Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 39 | #include <system/audio_policy.h> | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 40 | #include <hardware/audio_policy.h> | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 41 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 42 | namespace android { | 
|  | 43 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 44 | static const char *kDeadlockedString = "AudioPolicyService may be deadlocked\n"; | 
|  | 45 | static const char *kCmdDeadlockedString = "AudioPolicyService command thread may be deadlocked\n"; | 
|  | 46 |  | 
|  | 47 | static const int kDumpLockRetries = 50; | 
|  | 48 | static const int kDumpLockSleep = 20000; | 
|  | 49 |  | 
|  | 50 | static bool checkPermission() { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 51 | 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 Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 57 | namespace { | 
|  | 58 | extern struct audio_policy_service_ops aps_ops; | 
|  | 59 | }; | 
|  | 60 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 61 | // ---------------------------------------------------------------------------- | 
|  | 62 |  | 
|  | 63 | AudioPolicyService::AudioPolicyService() | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 64 | : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 65 | { | 
|  | 66 | char value[PROPERTY_VALUE_MAX]; | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 67 | const struct hw_module_t *module; | 
|  | 68 | int forced_val; | 
|  | 69 | int rc; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 70 |  | 
| Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 71 | Mutex::Autolock _l(mLock); | 
|  | 72 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 73 | // start tone playback thread | 
|  | 74 | mTonePlaybackThread = new AudioCommandThread(String8("")); | 
|  | 75 | // start audio commands thread | 
|  | 76 | mAudioCommandThread = new AudioCommandThread(String8("ApmCommandThread")); | 
|  | 77 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 78 | /* instantiate the audio policy manager */ | 
|  | 79 | rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module); | 
|  | 80 | if (rc) | 
|  | 81 | return; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 82 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 83 | 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 Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 87 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 88 | 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 Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 104 | } | 
|  | 105 |  | 
|  | 106 | AudioPolicyService::~AudioPolicyService() | 
|  | 107 | { | 
|  | 108 | mTonePlaybackThread->exit(); | 
|  | 109 | mTonePlaybackThread.clear(); | 
|  | 110 | mAudioCommandThread->exit(); | 
|  | 111 | mAudioCommandThread.clear(); | 
|  | 112 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 113 | if (mpAudioPolicy && mpAudioPolicyDev) | 
|  | 114 | mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy); | 
|  | 115 | if (mpAudioPolicyDev) | 
|  | 116 | audio_policy_dev_close(mpAudioPolicyDev); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 119 | status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device, | 
|  | 120 | audio_policy_dev_state_t state, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 121 | const char *device_address) | 
|  | 122 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 123 | if (mpAudioPolicy == NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 124 | return NO_INIT; | 
|  | 125 | } | 
|  | 126 | if (!checkPermission()) { | 
|  | 127 | return PERMISSION_DENIED; | 
|  | 128 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 129 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 130 | return BAD_VALUE; | 
|  | 131 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 132 | if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE && | 
|  | 133 | state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 134 | return BAD_VALUE; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | LOGV("setDeviceConnectionState() tid %d", gettid()); | 
|  | 138 | Mutex::Autolock _l(mLock); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 139 | return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device, | 
|  | 140 | state, device_address); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 141 | } | 
|  | 142 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 143 | audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState( | 
|  | 144 | audio_devices_t device, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 145 | const char *device_address) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 146 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 147 | if (mpAudioPolicy == NULL) { | 
|  | 148 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 149 | } | 
|  | 150 | if (!checkPermission()) { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 151 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 152 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 153 | return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device, | 
|  | 154 | device_address); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 155 | } | 
|  | 156 |  | 
|  | 157 | status_t AudioPolicyService::setPhoneState(int state) | 
|  | 158 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 159 | if (mpAudioPolicy == NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 160 | return NO_INIT; | 
|  | 161 | } | 
|  | 162 | if (!checkPermission()) { | 
|  | 163 | return PERMISSION_DENIED; | 
|  | 164 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 165 | if (state < 0 || state >= AUDIO_MODE_CNT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 166 | 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 Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 175 | mpAudioPolicy->set_phone_state(mpAudioPolicy, state); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 176 | return NO_ERROR; | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | status_t AudioPolicyService::setRingerMode(uint32_t mode, uint32_t mask) | 
|  | 180 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 181 | if (mpAudioPolicy == NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 182 | return NO_INIT; | 
|  | 183 | } | 
|  | 184 | if (!checkPermission()) { | 
|  | 185 | return PERMISSION_DENIED; | 
|  | 186 | } | 
|  | 187 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 188 | mpAudioPolicy->set_ringer_mode(mpAudioPolicy, mode, mask); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 189 | return NO_ERROR; | 
|  | 190 | } | 
|  | 191 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 192 | status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage, | 
|  | 193 | audio_policy_forced_cfg_t config) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 194 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 195 | if (mpAudioPolicy == NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 196 | return NO_INIT; | 
|  | 197 | } | 
|  | 198 | if (!checkPermission()) { | 
|  | 199 | return PERMISSION_DENIED; | 
|  | 200 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 201 | if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 202 | return BAD_VALUE; | 
|  | 203 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 204 | if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 205 | return BAD_VALUE; | 
|  | 206 | } | 
|  | 207 | LOGV("setForceUse() tid %d", gettid()); | 
|  | 208 | Mutex::Autolock _l(mLock); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 209 | mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 210 | return NO_ERROR; | 
|  | 211 | } | 
|  | 212 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 213 | audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 214 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 215 | if (mpAudioPolicy == NULL) { | 
|  | 216 | return AUDIO_POLICY_FORCE_NONE; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 217 | } | 
|  | 218 | if (!checkPermission()) { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 219 | return AUDIO_POLICY_FORCE_NONE; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 220 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 221 | if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { | 
|  | 222 | return AUDIO_POLICY_FORCE_NONE; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 223 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 224 | return mpAudioPolicy->get_force_use(mpAudioPolicy, usage); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 225 | } | 
|  | 226 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 227 | audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 228 | uint32_t samplingRate, | 
|  | 229 | uint32_t format, | 
|  | 230 | uint32_t channels, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 231 | audio_policy_output_flags_t flags) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 232 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 233 | if (mpAudioPolicy == NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 234 | return 0; | 
|  | 235 | } | 
|  | 236 | LOGV("getOutput() tid %d", gettid()); | 
|  | 237 | Mutex::Autolock _l(mLock); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 238 | return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, format, channels, flags); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 239 | } | 
|  | 240 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 241 | status_t AudioPolicyService::startOutput(audio_io_handle_t output, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 242 | audio_stream_type_t stream, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 243 | int session) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 244 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 245 | if (mpAudioPolicy == NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 246 | return NO_INIT; | 
|  | 247 | } | 
|  | 248 | LOGV("startOutput() tid %d", gettid()); | 
|  | 249 | Mutex::Autolock _l(mLock); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 250 | return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 251 | } | 
|  | 252 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 253 | status_t AudioPolicyService::stopOutput(audio_io_handle_t output, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 254 | audio_stream_type_t stream, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 255 | int session) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 256 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 257 | if (mpAudioPolicy == NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 258 | return NO_INIT; | 
|  | 259 | } | 
|  | 260 | LOGV("stopOutput() tid %d", gettid()); | 
|  | 261 | Mutex::Autolock _l(mLock); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 262 | return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 263 | } | 
|  | 264 |  | 
|  | 265 | void AudioPolicyService::releaseOutput(audio_io_handle_t output) | 
|  | 266 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 267 | if (mpAudioPolicy == NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 268 | return; | 
|  | 269 | } | 
|  | 270 | LOGV("releaseOutput() tid %d", gettid()); | 
|  | 271 | Mutex::Autolock _l(mLock); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 272 | mpAudioPolicy->release_output(mpAudioPolicy, output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 273 | } | 
|  | 274 |  | 
|  | 275 | audio_io_handle_t AudioPolicyService::getInput(int inputSource, | 
|  | 276 | uint32_t samplingRate, | 
|  | 277 | uint32_t format, | 
|  | 278 | uint32_t channels, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 279 | audio_in_acoustics_t acoustics) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 280 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 281 | if (mpAudioPolicy == NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 282 | return 0; | 
|  | 283 | } | 
|  | 284 | Mutex::Autolock _l(mLock); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 285 | return mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate, format, channels, acoustics); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 286 | } | 
|  | 287 |  | 
|  | 288 | status_t AudioPolicyService::startInput(audio_io_handle_t input) | 
|  | 289 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 290 | if (mpAudioPolicy == NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 291 | return NO_INIT; | 
|  | 292 | } | 
|  | 293 | Mutex::Autolock _l(mLock); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 294 | return mpAudioPolicy->start_input(mpAudioPolicy, input); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 295 | } | 
|  | 296 |  | 
|  | 297 | status_t AudioPolicyService::stopInput(audio_io_handle_t input) | 
|  | 298 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 299 | if (mpAudioPolicy == NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 300 | return NO_INIT; | 
|  | 301 | } | 
|  | 302 | Mutex::Autolock _l(mLock); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 303 | return mpAudioPolicy->stop_input(mpAudioPolicy, input); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 304 | } | 
|  | 305 |  | 
|  | 306 | void AudioPolicyService::releaseInput(audio_io_handle_t input) | 
|  | 307 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 308 | if (mpAudioPolicy == NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 309 | return; | 
|  | 310 | } | 
|  | 311 | Mutex::Autolock _l(mLock); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 312 | mpAudioPolicy->release_input(mpAudioPolicy, input); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 313 | } | 
|  | 314 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 315 | status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 316 | int indexMin, | 
|  | 317 | int indexMax) | 
|  | 318 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 319 | if (mpAudioPolicy == NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 320 | return NO_INIT; | 
|  | 321 | } | 
|  | 322 | if (!checkPermission()) { | 
|  | 323 | return PERMISSION_DENIED; | 
|  | 324 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 325 | if (stream < 0 || stream >= AUDIO_STREAM_CNT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 326 | return BAD_VALUE; | 
|  | 327 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 328 | mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 329 | return NO_ERROR; | 
|  | 330 | } | 
|  | 331 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 332 | status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream, int index) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 333 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 334 | if (mpAudioPolicy == NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 335 | return NO_INIT; | 
|  | 336 | } | 
|  | 337 | if (!checkPermission()) { | 
|  | 338 | return PERMISSION_DENIED; | 
|  | 339 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 340 | if (stream < 0 || stream >= AUDIO_STREAM_CNT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 341 | return BAD_VALUE; | 
|  | 342 | } | 
|  | 343 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 344 | return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 345 | } | 
|  | 346 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 347 | status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream, int *index) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 348 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 349 | if (mpAudioPolicy == NULL) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 350 | return NO_INIT; | 
|  | 351 | } | 
|  | 352 | if (!checkPermission()) { | 
|  | 353 | return PERMISSION_DENIED; | 
|  | 354 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 355 | if (stream < 0 || stream >= AUDIO_STREAM_CNT) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 356 | return BAD_VALUE; | 
|  | 357 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 358 | return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 359 | } | 
|  | 360 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 361 | uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream) | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 362 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 363 | if (mpAudioPolicy == NULL) { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 364 | return 0; | 
|  | 365 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 366 | return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 367 | } | 
|  | 368 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 369 | uint32_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream) | 
| Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 370 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 371 | if (mpAudioPolicy == NULL) { | 
| Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 372 | return 0; | 
|  | 373 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 374 | return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream); | 
| Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 375 | } | 
|  | 376 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 377 | audio_io_handle_t AudioPolicyService::getOutputForEffect(effect_descriptor_t *desc) | 
|  | 378 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 379 | if (mpAudioPolicy == NULL) { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 380 | return NO_INIT; | 
|  | 381 | } | 
|  | 382 | Mutex::Autolock _l(mLock); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 383 | return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 384 | } | 
|  | 385 |  | 
|  | 386 | status_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 Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 392 | if (mpAudioPolicy == NULL) { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 393 | return NO_INIT; | 
|  | 394 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 395 | return mpAudioPolicy->register_effect(mpAudioPolicy, desc, output, strategy, session, id); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 396 | } | 
|  | 397 |  | 
|  | 398 | status_t AudioPolicyService::unregisterEffect(int id) | 
|  | 399 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 400 | if (mpAudioPolicy == NULL) { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 401 | return NO_INIT; | 
|  | 402 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 403 | return mpAudioPolicy->unregister_effect(mpAudioPolicy, id); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 404 | } | 
|  | 405 |  | 
| Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 406 | bool AudioPolicyService::isStreamActive(int stream, uint32_t inPastMs) const | 
|  | 407 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 408 | if (mpAudioPolicy == NULL) { | 
| Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 409 | return 0; | 
|  | 410 | } | 
|  | 411 | Mutex::Autolock _l(mLock); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 412 | return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs); | 
| Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 413 | } | 
|  | 414 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 415 | void AudioPolicyService::binderDied(const wp<IBinder>& who) { | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 416 | LOGW("binderDied() %p, tid %d, calling tid %d", who.unsafe_get(), gettid(), | 
|  | 417 | IPCThreadState::self()->getCallingPid()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 418 | } | 
|  | 419 |  | 
|  | 420 | static 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 |  | 
|  | 433 | status_t AudioPolicyService::dumpInternals(int fd) | 
|  | 434 | { | 
|  | 435 | const size_t SIZE = 256; | 
|  | 436 | char buffer[SIZE]; | 
|  | 437 | String8 result; | 
|  | 438 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 439 | snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 440 | 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 |  | 
|  | 450 | status_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 Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 469 | if (mpAudioPolicy) { | 
|  | 470 | mpAudioPolicy->dump(mpAudioPolicy, fd); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 471 | } | 
|  | 472 |  | 
|  | 473 | if (locked) mLock.unlock(); | 
|  | 474 | } | 
|  | 475 | return NO_ERROR; | 
|  | 476 | } | 
|  | 477 |  | 
|  | 478 | status_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 |  | 
|  | 492 | status_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 Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 499 | // -----------  AudioPolicyService::AudioCommandThread implementation ---------- | 
|  | 500 |  | 
|  | 501 | AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name) | 
|  | 502 | : Thread(false), mName(name) | 
|  | 503 | { | 
|  | 504 | mpToneGenerator = NULL; | 
|  | 505 | } | 
|  | 506 |  | 
|  | 507 |  | 
|  | 508 | AudioPolicyService::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 |  | 
|  | 517 | void 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 |  | 
|  | 526 | bool 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 566 | 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 Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 571 | 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 579 | LOGV("AudioCommandThread() processing set parameters string %s, io %d", | 
|  | 580 | data->mKeyValuePairs.string(), data->mIO); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 581 | 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 590 | LOGV("AudioCommandThread() processing set voice volume volume %f", | 
|  | 591 | data->mVolume); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 592 | 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 |  | 
|  | 621 | status_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 |  | 
|  | 655 | void 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 |  | 
|  | 670 | void 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 682 | status_t AudioPolicyService::AudioCommandThread::volumeCommand(int stream, | 
|  | 683 | float volume, | 
|  | 684 | int output, | 
|  | 685 | int delayMs) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 686 | { | 
|  | 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 703 | LOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d", | 
|  | 704 | stream, volume, output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 705 | 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 714 | status_t AudioPolicyService::AudioCommandThread::parametersCommand(int ioHandle, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 715 | const char *keyValuePairs, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 716 | int delayMs) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 717 | { | 
|  | 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 Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 724 | data->mKeyValuePairs = String8(keyValuePairs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 725 | 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 733 | LOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d", | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 734 | keyValuePairs, ioHandle, delayMs); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 735 | mWaitWorkCV.signal(); | 
|  | 736 | if (command->mWaitStatus) { | 
|  | 737 | command->mCond.wait(mLock); | 
|  | 738 | status =  command->mStatus; | 
|  | 739 | mWaitWorkCV.signal(); | 
|  | 740 | } | 
|  | 741 | return status; | 
|  | 742 | } | 
|  | 743 |  | 
|  | 744 | status_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 | 
|  | 771 | void 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 795 | LOGV("Comparing parameter command %s to new command %s", | 
|  | 796 | data2->mKeyValuePairs.string(), data->mKeyValuePairs.string()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 797 | 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 828 | LOGV("Filtering out volume command on output %d for stream %d", | 
|  | 829 | data->mIO, data->mStream); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 830 | 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 853 | LOGV("inserting command: %d at index %d, num commands %d", | 
|  | 854 | command->mCommand, (int)i+1, mAudioCommands.size()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 855 | mAudioCommands.insertAt(command, i + 1); | 
|  | 856 | } | 
|  | 857 |  | 
|  | 858 | void AudioPolicyService::AudioCommandThread::exit() | 
|  | 859 | { | 
|  | 860 | LOGV("AudioCommandThread::exit"); | 
|  | 861 | { | 
|  | 862 | AutoMutex _l(mLock); | 
|  | 863 | requestExit(); | 
|  | 864 | mWaitWorkCV.signal(); | 
|  | 865 | } | 
|  | 866 | requestExitAndWait(); | 
|  | 867 | } | 
|  | 868 |  | 
|  | 869 | void 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 Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 879 | /******* helpers for the service_ops callbacks defined below *********/ | 
|  | 880 | void 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 |  | 
|  | 888 | int 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 |  | 
|  | 897 | int 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 |  | 
|  | 910 | int AudioPolicyService::stopTone() | 
|  | 911 | { | 
|  | 912 | mTonePlaybackThread->stopToneCommand(); | 
|  | 913 | return 0; | 
|  | 914 | } | 
|  | 915 |  | 
|  | 916 | int 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 */ | 
|  | 922 | extern "C" { | 
|  | 923 |  | 
|  | 924 | static 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 |  | 
|  | 942 | static 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 |  | 
|  | 954 | static 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 |  | 
|  | 963 | static 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 |  | 
|  | 974 | static 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 |  | 
|  | 985 | static 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 |  | 
|  | 1002 | static 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 |  | 
|  | 1011 | static 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 |  | 
|  | 1021 | static 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 |  | 
|  | 1032 | static 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 |  | 
|  | 1039 | static 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 |  | 
|  | 1047 | static 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 |  | 
|  | 1057 | static 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 |  | 
|  | 1065 | static int aps_stop_tone(void *service) | 
|  | 1066 | { | 
|  | 1067 | AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; | 
|  | 1068 |  | 
|  | 1069 | return audioPolicyService->stopTone(); | 
|  | 1070 | } | 
|  | 1071 |  | 
|  | 1072 | static 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 |  | 
|  | 1081 | namespace { | 
|  | 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 Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1101 | }; // namespace android |