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> |
| 38 | #include <hardware/audio.h> |
| 39 | #include <hardware/audio_policy.h> |
| 40 | #include <hardware/audio_policy_hal.h> |
| 41 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 42 | // ---------------------------------------------------------------------------- |
| 43 | // the sim build doesn't have gettid |
| 44 | |
| 45 | #ifndef HAVE_GETTID |
| 46 | # define gettid getpid |
| 47 | #endif |
| 48 | |
| 49 | namespace android { |
| 50 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 51 | static const char *kDeadlockedString = "AudioPolicyService may be deadlocked\n"; |
| 52 | static const char *kCmdDeadlockedString = "AudioPolicyService command thread may be deadlocked\n"; |
| 53 | |
| 54 | static const int kDumpLockRetries = 50; |
| 55 | static const int kDumpLockSleep = 20000; |
| 56 | |
| 57 | static 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 Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 67 | namespace { |
| 68 | extern struct audio_policy_service_ops aps_ops; |
| 69 | }; |
| 70 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 71 | // ---------------------------------------------------------------------------- |
| 72 | |
| 73 | AudioPolicyService::AudioPolicyService() |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 74 | : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 75 | { |
| 76 | char value[PROPERTY_VALUE_MAX]; |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 77 | const struct hw_module_t *module; |
| 78 | int forced_val; |
| 79 | int rc; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 80 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 81 | Mutex::Autolock _l(mLock); |
| 82 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 83 | // start tone playback thread |
| 84 | mTonePlaybackThread = new AudioCommandThread(String8("")); |
| 85 | // start audio commands thread |
| 86 | mAudioCommandThread = new AudioCommandThread(String8("ApmCommandThread")); |
| 87 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 88 | /* instantiate the audio policy manager */ |
| 89 | rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module); |
| 90 | if (rc) |
| 91 | return; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 92 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 93 | 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 Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 97 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 98 | 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 Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | AudioPolicyService::~AudioPolicyService() |
| 117 | { |
| 118 | mTonePlaybackThread->exit(); |
| 119 | mTonePlaybackThread.clear(); |
| 120 | mAudioCommandThread->exit(); |
| 121 | mAudioCommandThread.clear(); |
| 122 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 123 | if (mpAudioPolicy && mpAudioPolicyDev) |
| 124 | mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy); |
| 125 | if (mpAudioPolicyDev) |
| 126 | audio_policy_dev_close(mpAudioPolicyDev); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 129 | status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device, |
| 130 | audio_policy_dev_state_t state, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 131 | const char *device_address) |
| 132 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 133 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 134 | return NO_INIT; |
| 135 | } |
| 136 | if (!checkPermission()) { |
| 137 | return PERMISSION_DENIED; |
| 138 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 139 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 140 | return BAD_VALUE; |
| 141 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 142 | if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE && |
| 143 | state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 144 | return BAD_VALUE; |
| 145 | } |
| 146 | |
| 147 | LOGV("setDeviceConnectionState() tid %d", gettid()); |
| 148 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 149 | return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device, |
| 150 | state, device_address); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 153 | audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState( |
| 154 | audio_devices_t device, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 155 | const char *device_address) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 156 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 157 | if (mpAudioPolicy == NULL) { |
| 158 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 159 | } |
| 160 | if (!checkPermission()) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 161 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 162 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 163 | return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device, |
| 164 | device_address); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | status_t AudioPolicyService::setPhoneState(int state) |
| 168 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 169 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 170 | return NO_INIT; |
| 171 | } |
| 172 | if (!checkPermission()) { |
| 173 | return PERMISSION_DENIED; |
| 174 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 175 | if (state < 0 || state >= AUDIO_MODE_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 176 | 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 Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 185 | mpAudioPolicy->set_phone_state(mpAudioPolicy, state); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 186 | return NO_ERROR; |
| 187 | } |
| 188 | |
| 189 | status_t AudioPolicyService::setRingerMode(uint32_t mode, uint32_t mask) |
| 190 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 191 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 192 | return NO_INIT; |
| 193 | } |
| 194 | if (!checkPermission()) { |
| 195 | return PERMISSION_DENIED; |
| 196 | } |
| 197 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 198 | mpAudioPolicy->set_ringer_mode(mpAudioPolicy, mode, mask); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 199 | return NO_ERROR; |
| 200 | } |
| 201 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 202 | status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage, |
| 203 | audio_policy_forced_cfg_t config) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 204 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 205 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 206 | return NO_INIT; |
| 207 | } |
| 208 | if (!checkPermission()) { |
| 209 | return PERMISSION_DENIED; |
| 210 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 211 | if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 212 | return BAD_VALUE; |
| 213 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 214 | if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 215 | return BAD_VALUE; |
| 216 | } |
| 217 | LOGV("setForceUse() tid %d", gettid()); |
| 218 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 219 | mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 220 | return NO_ERROR; |
| 221 | } |
| 222 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 223 | 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] | 224 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 225 | if (mpAudioPolicy == NULL) { |
| 226 | return AUDIO_POLICY_FORCE_NONE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 227 | } |
| 228 | if (!checkPermission()) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 229 | return AUDIO_POLICY_FORCE_NONE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 230 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 231 | if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { |
| 232 | return AUDIO_POLICY_FORCE_NONE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 233 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 234 | return mpAudioPolicy->get_force_use(mpAudioPolicy, usage); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 237 | audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 238 | uint32_t samplingRate, |
| 239 | uint32_t format, |
| 240 | uint32_t channels, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 241 | audio_policy_output_flags_t flags) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 242 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 243 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 244 | return 0; |
| 245 | } |
| 246 | LOGV("getOutput() tid %d", gettid()); |
| 247 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 248 | return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, format, channels, flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 251 | status_t AudioPolicyService::startOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 252 | audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 253 | int session) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 254 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 255 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 256 | return NO_INIT; |
| 257 | } |
| 258 | LOGV("startOutput() tid %d", gettid()); |
| 259 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 260 | return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 263 | status_t AudioPolicyService::stopOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 264 | audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 265 | int session) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 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 NO_INIT; |
| 269 | } |
| 270 | LOGV("stopOutput() tid %d", gettid()); |
| 271 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 272 | return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void AudioPolicyService::releaseOutput(audio_io_handle_t output) |
| 276 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 277 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 278 | return; |
| 279 | } |
| 280 | LOGV("releaseOutput() tid %d", gettid()); |
| 281 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 282 | mpAudioPolicy->release_output(mpAudioPolicy, output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | audio_io_handle_t AudioPolicyService::getInput(int inputSource, |
| 286 | uint32_t samplingRate, |
| 287 | uint32_t format, |
| 288 | uint32_t channels, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 289 | audio_in_acoustics_t acoustics) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 290 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 291 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 292 | return 0; |
| 293 | } |
| 294 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 295 | return mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate, format, channels, acoustics); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | status_t AudioPolicyService::startInput(audio_io_handle_t input) |
| 299 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 300 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 301 | return NO_INIT; |
| 302 | } |
| 303 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 304 | return mpAudioPolicy->start_input(mpAudioPolicy, input); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | status_t AudioPolicyService::stopInput(audio_io_handle_t input) |
| 308 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 309 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 310 | return NO_INIT; |
| 311 | } |
| 312 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 313 | return mpAudioPolicy->stop_input(mpAudioPolicy, input); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | void AudioPolicyService::releaseInput(audio_io_handle_t input) |
| 317 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 318 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 319 | return; |
| 320 | } |
| 321 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 322 | mpAudioPolicy->release_input(mpAudioPolicy, input); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 325 | status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 326 | int indexMin, |
| 327 | int indexMax) |
| 328 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 329 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 330 | return NO_INIT; |
| 331 | } |
| 332 | if (!checkPermission()) { |
| 333 | return PERMISSION_DENIED; |
| 334 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 335 | if (stream < 0 || stream >= AUDIO_STREAM_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 336 | return BAD_VALUE; |
| 337 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 338 | mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 339 | return NO_ERROR; |
| 340 | } |
| 341 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 342 | status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream, int index) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 343 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 344 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 345 | return NO_INIT; |
| 346 | } |
| 347 | if (!checkPermission()) { |
| 348 | return PERMISSION_DENIED; |
| 349 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 350 | if (stream < 0 || stream >= AUDIO_STREAM_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 351 | return BAD_VALUE; |
| 352 | } |
| 353 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 354 | return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 357 | status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream, int *index) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 358 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 359 | if (mpAudioPolicy == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 360 | return NO_INIT; |
| 361 | } |
| 362 | if (!checkPermission()) { |
| 363 | return PERMISSION_DENIED; |
| 364 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 365 | if (stream < 0 || stream >= AUDIO_STREAM_CNT) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 366 | return BAD_VALUE; |
| 367 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 368 | return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 371 | uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 372 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 373 | if (mpAudioPolicy == NULL) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 374 | return 0; |
| 375 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 376 | return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 379 | uint32_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream) |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 380 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 381 | if (mpAudioPolicy == NULL) { |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 382 | return 0; |
| 383 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 384 | return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream); |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 387 | audio_io_handle_t AudioPolicyService::getOutputForEffect(effect_descriptor_t *desc) |
| 388 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 389 | if (mpAudioPolicy == NULL) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 390 | return NO_INIT; |
| 391 | } |
| 392 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 393 | return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | status_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 Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 402 | if (mpAudioPolicy == NULL) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 403 | return NO_INIT; |
| 404 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 405 | return mpAudioPolicy->register_effect(mpAudioPolicy, desc, output, strategy, session, id); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | status_t AudioPolicyService::unregisterEffect(int id) |
| 409 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 410 | if (mpAudioPolicy == NULL) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 411 | return NO_INIT; |
| 412 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 413 | return mpAudioPolicy->unregister_effect(mpAudioPolicy, id); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 416 | bool AudioPolicyService::isStreamActive(int stream, uint32_t inPastMs) const |
| 417 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 418 | if (mpAudioPolicy == NULL) { |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | Mutex::Autolock _l(mLock); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 422 | return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs); |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 425 | void AudioPolicyService::binderDied(const wp<IBinder>& who) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 426 | LOGW("binderDied() %p, tid %d, calling tid %d", who.unsafe_get(), gettid(), |
| 427 | IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | static 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 | |
| 443 | status_t AudioPolicyService::dumpInternals(int fd) |
| 444 | { |
| 445 | const size_t SIZE = 256; |
| 446 | char buffer[SIZE]; |
| 447 | String8 result; |
| 448 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 449 | snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 450 | 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 | |
| 460 | status_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 Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 479 | if (mpAudioPolicy) { |
| 480 | mpAudioPolicy->dump(mpAudioPolicy, fd); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | if (locked) mLock.unlock(); |
| 484 | } |
| 485 | return NO_ERROR; |
| 486 | } |
| 487 | |
| 488 | status_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 | |
| 502 | status_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 Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 509 | // ----------- AudioPolicyService::AudioCommandThread implementation ---------- |
| 510 | |
| 511 | AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name) |
| 512 | : Thread(false), mName(name) |
| 513 | { |
| 514 | mpToneGenerator = NULL; |
| 515 | } |
| 516 | |
| 517 | |
| 518 | AudioPolicyService::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 | |
| 527 | void 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 | |
| 536 | bool 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 576 | 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 Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 581 | 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 589 | LOGV("AudioCommandThread() processing set parameters string %s, io %d", |
| 590 | data->mKeyValuePairs.string(), data->mIO); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 591 | 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 600 | LOGV("AudioCommandThread() processing set voice volume volume %f", |
| 601 | data->mVolume); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 602 | 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 | |
| 631 | status_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 | |
| 665 | void 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 | |
| 680 | void 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 692 | status_t AudioPolicyService::AudioCommandThread::volumeCommand(int stream, |
| 693 | float volume, |
| 694 | int output, |
| 695 | int delayMs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 696 | { |
| 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 713 | LOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d", |
| 714 | stream, volume, output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 715 | 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 724 | status_t AudioPolicyService::AudioCommandThread::parametersCommand(int ioHandle, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 725 | const char *keyValuePairs, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 726 | int delayMs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 727 | { |
| 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 Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 734 | data->mKeyValuePairs = String8(keyValuePairs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 735 | 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 743 | LOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d", |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 744 | keyValuePairs, ioHandle, delayMs); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 745 | mWaitWorkCV.signal(); |
| 746 | if (command->mWaitStatus) { |
| 747 | command->mCond.wait(mLock); |
| 748 | status = command->mStatus; |
| 749 | mWaitWorkCV.signal(); |
| 750 | } |
| 751 | return status; |
| 752 | } |
| 753 | |
| 754 | status_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 |
| 781 | void 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 805 | LOGV("Comparing parameter command %s to new command %s", |
| 806 | data2->mKeyValuePairs.string(), data->mKeyValuePairs.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 807 | 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 838 | LOGV("Filtering out volume command on output %d for stream %d", |
| 839 | data->mIO, data->mStream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 840 | 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 Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 863 | LOGV("inserting command: %d at index %d, num commands %d", |
| 864 | command->mCommand, (int)i+1, mAudioCommands.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 865 | mAudioCommands.insertAt(command, i + 1); |
| 866 | } |
| 867 | |
| 868 | void AudioPolicyService::AudioCommandThread::exit() |
| 869 | { |
| 870 | LOGV("AudioCommandThread::exit"); |
| 871 | { |
| 872 | AutoMutex _l(mLock); |
| 873 | requestExit(); |
| 874 | mWaitWorkCV.signal(); |
| 875 | } |
| 876 | requestExitAndWait(); |
| 877 | } |
| 878 | |
| 879 | void 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 Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 889 | /******* helpers for the service_ops callbacks defined below *********/ |
| 890 | void 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 | |
| 898 | int 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 | |
| 907 | int 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 | |
| 920 | int AudioPolicyService::stopTone() |
| 921 | { |
| 922 | mTonePlaybackThread->stopToneCommand(); |
| 923 | return 0; |
| 924 | } |
| 925 | |
| 926 | int 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 */ |
| 932 | extern "C" { |
| 933 | |
| 934 | static 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 | |
| 952 | static 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 | |
| 964 | static 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 | |
| 973 | static 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 | |
| 984 | static 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 | |
| 995 | static 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 | |
| 1012 | static 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 | |
| 1021 | static 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 | |
| 1031 | static 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 | |
| 1042 | static 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 | |
| 1049 | static 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 | |
| 1057 | static 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 | |
| 1067 | static 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 | |
| 1075 | static int aps_stop_tone(void *service) |
| 1076 | { |
| 1077 | AudioPolicyService *audioPolicyService = (AudioPolicyService *)service; |
| 1078 | |
| 1079 | return audioPolicyService->stopTone(); |
| 1080 | } |
| 1081 | |
| 1082 | static 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 | |
| 1091 | namespace { |
| 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 Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1111 | }; // namespace android |