Eric Laurent | a553c25 | 2009-07-17 12:17:14 -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 |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 19 | |
| 20 | #undef __STRICT_ANSI__ |
| 21 | #define __STDINT_LIMITS |
| 22 | #define __STDC_LIMIT_MACROS |
| 23 | #include <stdint.h> |
| 24 | |
| 25 | #include <sys/time.h> |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 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" |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 33 | #include <hardware_legacy/AudioPolicyManagerBase.h> |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 34 | #include <cutils/properties.h> |
| 35 | #include <dlfcn.h> |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 36 | #include <hardware_legacy/power.h> |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 37 | |
| 38 | // ---------------------------------------------------------------------------- |
| 39 | // the sim build doesn't have gettid |
| 40 | |
| 41 | #ifndef HAVE_GETTID |
| 42 | # define gettid getpid |
| 43 | #endif |
| 44 | |
| 45 | namespace android { |
| 46 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 47 | |
| 48 | static const char *kDeadlockedString = "AudioPolicyService may be deadlocked\n"; |
| 49 | static const char *kCmdDeadlockedString = "AudioPolicyService command thread may be deadlocked\n"; |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 50 | |
| 51 | static const int kDumpLockRetries = 50; |
| 52 | static const int kDumpLockSleep = 20000; |
| 53 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 54 | static bool checkPermission() { |
| 55 | #ifndef HAVE_ANDROID_OS |
| 56 | return true; |
| 57 | #endif |
| 58 | if (getpid() == IPCThreadState::self()->getCallingPid()) return true; |
| 59 | bool ok = checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS")); |
| 60 | if (!ok) LOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS"); |
| 61 | return ok; |
| 62 | } |
| 63 | |
| 64 | // ---------------------------------------------------------------------------- |
| 65 | |
| 66 | AudioPolicyService::AudioPolicyService() |
Eric Laurent | 4192cce | 2009-07-24 06:58:44 -0700 | [diff] [blame] | 67 | : BnAudioPolicyService() , mpPolicyManager(NULL) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 68 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 69 | char value[PROPERTY_VALUE_MAX]; |
| 70 | |
Eric Laurent | 0163594 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 71 | Mutex::Autolock _l(mLock); |
| 72 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 73 | // start tone playback thread |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 74 | mTonePlaybackThread = new AudioCommandThread(String8("")); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 75 | // start audio commands thread |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 76 | mAudioCommandThread = new AudioCommandThread(String8("ApmCommandThread")); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 77 | |
Eric Laurent | 4192cce | 2009-07-24 06:58:44 -0700 | [diff] [blame] | 78 | #if (defined GENERIC_AUDIO) || (defined AUDIO_POLICY_TEST) |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 79 | mpPolicyManager = new AudioPolicyManagerBase(this); |
Eric Laurent | 4192cce | 2009-07-24 06:58:44 -0700 | [diff] [blame] | 80 | LOGV("build for GENERIC_AUDIO - using generic audio policy"); |
| 81 | #else |
| 82 | // if running in emulation - use the emulator driver |
| 83 | if (property_get("ro.kernel.qemu", value, 0)) { |
| 84 | LOGV("Running in emulation - using generic audio policy"); |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 85 | mpPolicyManager = new AudioPolicyManagerBase(this); |
Eric Laurent | 4192cce | 2009-07-24 06:58:44 -0700 | [diff] [blame] | 86 | } |
| 87 | else { |
| 88 | LOGV("Using hardware specific audio policy"); |
| 89 | mpPolicyManager = createAudioPolicyManager(this); |
| 90 | } |
| 91 | #endif |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 92 | |
Eric Laurent | 0163594 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 93 | if ((mpPolicyManager != NULL) && (mpPolicyManager->initCheck() != NO_ERROR)) { |
| 94 | delete mpPolicyManager; |
| 95 | mpPolicyManager = NULL; |
| 96 | } |
| 97 | |
| 98 | if (mpPolicyManager == NULL) { |
| 99 | LOGE("Could not create AudioPolicyManager"); |
| 100 | } else { |
| 101 | // load properties |
| 102 | property_get("ro.camera.sound.forced", value, "0"); |
| 103 | mpPolicyManager->setSystemProperty("ro.camera.sound.forced", value); |
| 104 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | AudioPolicyService::~AudioPolicyService() |
| 108 | { |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 109 | mTonePlaybackThread->exit(); |
| 110 | mTonePlaybackThread.clear(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 111 | mAudioCommandThread->exit(); |
| 112 | mAudioCommandThread.clear(); |
| 113 | |
| 114 | if (mpPolicyManager) { |
Eric Laurent | 4192cce | 2009-07-24 06:58:44 -0700 | [diff] [blame] | 115 | delete mpPolicyManager; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | |
| 120 | status_t AudioPolicyService::setDeviceConnectionState(AudioSystem::audio_devices device, |
| 121 | AudioSystem::device_connection_state state, |
| 122 | const char *device_address) |
| 123 | { |
| 124 | if (mpPolicyManager == NULL) { |
| 125 | return NO_INIT; |
| 126 | } |
| 127 | if (!checkPermission()) { |
| 128 | return PERMISSION_DENIED; |
| 129 | } |
| 130 | if (!AudioSystem::isOutputDevice(device) && !AudioSystem::isInputDevice(device)) { |
| 131 | return BAD_VALUE; |
| 132 | } |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 133 | if (state != AudioSystem::DEVICE_STATE_AVAILABLE && |
| 134 | state != AudioSystem::DEVICE_STATE_UNAVAILABLE) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 135 | return BAD_VALUE; |
| 136 | } |
| 137 | |
| 138 | LOGV("setDeviceConnectionState() tid %d", gettid()); |
| 139 | Mutex::Autolock _l(mLock); |
| 140 | return mpPolicyManager->setDeviceConnectionState(device, state, device_address); |
| 141 | } |
| 142 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 143 | AudioSystem::device_connection_state AudioPolicyService::getDeviceConnectionState( |
| 144 | AudioSystem::audio_devices device, |
| 145 | const char *device_address) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 146 | { |
| 147 | if (mpPolicyManager == NULL) { |
| 148 | return AudioSystem::DEVICE_STATE_UNAVAILABLE; |
| 149 | } |
| 150 | if (!checkPermission()) { |
| 151 | return AudioSystem::DEVICE_STATE_UNAVAILABLE; |
| 152 | } |
| 153 | return mpPolicyManager->getDeviceConnectionState(device, device_address); |
| 154 | } |
| 155 | |
| 156 | status_t AudioPolicyService::setPhoneState(int state) |
| 157 | { |
| 158 | if (mpPolicyManager == NULL) { |
| 159 | return NO_INIT; |
| 160 | } |
| 161 | if (!checkPermission()) { |
| 162 | return PERMISSION_DENIED; |
| 163 | } |
| 164 | if (state < 0 || state >= AudioSystem::NUM_MODES) { |
| 165 | return BAD_VALUE; |
| 166 | } |
| 167 | |
| 168 | LOGV("setPhoneState() tid %d", gettid()); |
| 169 | |
| 170 | // TODO: check if it is more appropriate to do it in platform specific policy manager |
| 171 | AudioSystem::setMode(state); |
| 172 | |
| 173 | Mutex::Autolock _l(mLock); |
| 174 | mpPolicyManager->setPhoneState(state); |
| 175 | return NO_ERROR; |
| 176 | } |
| 177 | |
| 178 | status_t AudioPolicyService::setRingerMode(uint32_t mode, uint32_t mask) |
| 179 | { |
| 180 | if (mpPolicyManager == NULL) { |
| 181 | return NO_INIT; |
| 182 | } |
| 183 | if (!checkPermission()) { |
| 184 | return PERMISSION_DENIED; |
| 185 | } |
| 186 | |
| 187 | mpPolicyManager->setRingerMode(mode, mask); |
| 188 | return NO_ERROR; |
| 189 | } |
| 190 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 191 | status_t AudioPolicyService::setForceUse(AudioSystem::force_use usage, |
| 192 | AudioSystem::forced_config config) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 193 | { |
| 194 | if (mpPolicyManager == NULL) { |
| 195 | return NO_INIT; |
| 196 | } |
| 197 | if (!checkPermission()) { |
| 198 | return PERMISSION_DENIED; |
| 199 | } |
| 200 | if (usage < 0 || usage >= AudioSystem::NUM_FORCE_USE) { |
| 201 | return BAD_VALUE; |
| 202 | } |
| 203 | if (config < 0 || config >= AudioSystem::NUM_FORCE_CONFIG) { |
| 204 | return BAD_VALUE; |
| 205 | } |
| 206 | LOGV("setForceUse() tid %d", gettid()); |
| 207 | Mutex::Autolock _l(mLock); |
| 208 | mpPolicyManager->setForceUse(usage, config); |
| 209 | return NO_ERROR; |
| 210 | } |
| 211 | |
| 212 | AudioSystem::forced_config AudioPolicyService::getForceUse(AudioSystem::force_use usage) |
| 213 | { |
| 214 | if (mpPolicyManager == NULL) { |
| 215 | return AudioSystem::FORCE_NONE; |
| 216 | } |
| 217 | if (!checkPermission()) { |
| 218 | return AudioSystem::FORCE_NONE; |
| 219 | } |
| 220 | if (usage < 0 || usage >= AudioSystem::NUM_FORCE_USE) { |
| 221 | return AudioSystem::FORCE_NONE; |
| 222 | } |
| 223 | return mpPolicyManager->getForceUse(usage); |
| 224 | } |
| 225 | |
| 226 | audio_io_handle_t AudioPolicyService::getOutput(AudioSystem::stream_type stream, |
| 227 | uint32_t samplingRate, |
| 228 | uint32_t format, |
| 229 | uint32_t channels, |
| 230 | AudioSystem::output_flags flags) |
| 231 | { |
| 232 | if (mpPolicyManager == NULL) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 233 | return 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 234 | } |
| 235 | LOGV("getOutput() tid %d", gettid()); |
| 236 | Mutex::Autolock _l(mLock); |
| 237 | return mpPolicyManager->getOutput(stream, samplingRate, format, channels, flags); |
| 238 | } |
| 239 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 240 | status_t AudioPolicyService::startOutput(audio_io_handle_t output, |
| 241 | AudioSystem::stream_type stream, |
| 242 | int session) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 243 | { |
| 244 | if (mpPolicyManager == NULL) { |
| 245 | return NO_INIT; |
| 246 | } |
| 247 | LOGV("startOutput() tid %d", gettid()); |
| 248 | Mutex::Autolock _l(mLock); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 249 | return mpPolicyManager->startOutput(output, stream, session); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 252 | status_t AudioPolicyService::stopOutput(audio_io_handle_t output, |
| 253 | AudioSystem::stream_type stream, |
| 254 | int session) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 255 | { |
| 256 | if (mpPolicyManager == NULL) { |
| 257 | return NO_INIT; |
| 258 | } |
| 259 | LOGV("stopOutput() tid %d", gettid()); |
| 260 | Mutex::Autolock _l(mLock); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 261 | return mpPolicyManager->stopOutput(output, stream, session); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | void AudioPolicyService::releaseOutput(audio_io_handle_t output) |
| 265 | { |
| 266 | if (mpPolicyManager == NULL) { |
| 267 | return; |
| 268 | } |
| 269 | LOGV("releaseOutput() tid %d", gettid()); |
| 270 | Mutex::Autolock _l(mLock); |
| 271 | mpPolicyManager->releaseOutput(output); |
| 272 | } |
| 273 | |
| 274 | audio_io_handle_t AudioPolicyService::getInput(int inputSource, |
| 275 | uint32_t samplingRate, |
| 276 | uint32_t format, |
| 277 | uint32_t channels, |
| 278 | AudioSystem::audio_in_acoustics acoustics) |
| 279 | { |
| 280 | if (mpPolicyManager == NULL) { |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 281 | return 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 282 | } |
| 283 | Mutex::Autolock _l(mLock); |
| 284 | return mpPolicyManager->getInput(inputSource, samplingRate, format, channels, acoustics); |
| 285 | } |
| 286 | |
| 287 | status_t AudioPolicyService::startInput(audio_io_handle_t input) |
| 288 | { |
| 289 | if (mpPolicyManager == NULL) { |
| 290 | return NO_INIT; |
| 291 | } |
| 292 | Mutex::Autolock _l(mLock); |
| 293 | return mpPolicyManager->startInput(input); |
| 294 | } |
| 295 | |
| 296 | status_t AudioPolicyService::stopInput(audio_io_handle_t input) |
| 297 | { |
| 298 | if (mpPolicyManager == NULL) { |
| 299 | return NO_INIT; |
| 300 | } |
| 301 | Mutex::Autolock _l(mLock); |
| 302 | return mpPolicyManager->stopInput(input); |
| 303 | } |
| 304 | |
| 305 | void AudioPolicyService::releaseInput(audio_io_handle_t input) |
| 306 | { |
| 307 | if (mpPolicyManager == NULL) { |
| 308 | return; |
| 309 | } |
| 310 | Mutex::Autolock _l(mLock); |
| 311 | mpPolicyManager->releaseInput(input); |
| 312 | } |
| 313 | |
| 314 | status_t AudioPolicyService::initStreamVolume(AudioSystem::stream_type stream, |
| 315 | int indexMin, |
| 316 | int indexMax) |
| 317 | { |
| 318 | if (mpPolicyManager == NULL) { |
| 319 | return NO_INIT; |
| 320 | } |
| 321 | if (!checkPermission()) { |
| 322 | return PERMISSION_DENIED; |
| 323 | } |
| 324 | if (stream < 0 || stream >= AudioSystem::NUM_STREAM_TYPES) { |
| 325 | return BAD_VALUE; |
| 326 | } |
| 327 | mpPolicyManager->initStreamVolume(stream, indexMin, indexMax); |
| 328 | return NO_ERROR; |
| 329 | } |
| 330 | |
| 331 | status_t AudioPolicyService::setStreamVolumeIndex(AudioSystem::stream_type stream, int index) |
| 332 | { |
| 333 | if (mpPolicyManager == NULL) { |
| 334 | return NO_INIT; |
| 335 | } |
| 336 | if (!checkPermission()) { |
| 337 | return PERMISSION_DENIED; |
| 338 | } |
| 339 | if (stream < 0 || stream >= AudioSystem::NUM_STREAM_TYPES) { |
| 340 | return BAD_VALUE; |
| 341 | } |
| 342 | |
| 343 | return mpPolicyManager->setStreamVolumeIndex(stream, index); |
| 344 | } |
| 345 | |
| 346 | status_t AudioPolicyService::getStreamVolumeIndex(AudioSystem::stream_type stream, int *index) |
| 347 | { |
| 348 | if (mpPolicyManager == NULL) { |
| 349 | return NO_INIT; |
| 350 | } |
| 351 | if (!checkPermission()) { |
| 352 | return PERMISSION_DENIED; |
| 353 | } |
| 354 | if (stream < 0 || stream >= AudioSystem::NUM_STREAM_TYPES) { |
| 355 | return BAD_VALUE; |
| 356 | } |
| 357 | return mpPolicyManager->getStreamVolumeIndex(stream, index); |
| 358 | } |
| 359 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 360 | uint32_t AudioPolicyService::getStrategyForStream(AudioSystem::stream_type stream) |
| 361 | { |
| 362 | if (mpPolicyManager == NULL) { |
| 363 | return 0; |
| 364 | } |
| 365 | return mpPolicyManager->getStrategyForStream(stream); |
| 366 | } |
| 367 | |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 368 | uint32_t AudioPolicyService::getDevicesForStream(AudioSystem::stream_type stream) |
| 369 | { |
| 370 | if (mpPolicyManager == NULL) { |
| 371 | return 0; |
| 372 | } |
| 373 | return mpPolicyManager->getDevicesForStream(stream); |
| 374 | } |
| 375 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 376 | audio_io_handle_t AudioPolicyService::getOutputForEffect(effect_descriptor_t *desc) |
| 377 | { |
| 378 | if (mpPolicyManager == NULL) { |
| 379 | return NO_INIT; |
| 380 | } |
| 381 | Mutex::Autolock _l(mLock); |
| 382 | return mpPolicyManager->getOutputForEffect(desc); |
| 383 | } |
| 384 | |
| 385 | status_t AudioPolicyService::registerEffect(effect_descriptor_t *desc, |
| 386 | audio_io_handle_t output, |
| 387 | uint32_t strategy, |
| 388 | int session, |
| 389 | int id) |
| 390 | { |
| 391 | if (mpPolicyManager == NULL) { |
| 392 | return NO_INIT; |
| 393 | } |
| 394 | return mpPolicyManager->registerEffect(desc, output, strategy, session, id); |
| 395 | } |
| 396 | |
| 397 | status_t AudioPolicyService::unregisterEffect(int id) |
| 398 | { |
| 399 | if (mpPolicyManager == NULL) { |
| 400 | return NO_INIT; |
| 401 | } |
| 402 | return mpPolicyManager->unregisterEffect(id); |
| 403 | } |
| 404 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 405 | bool AudioPolicyService::isStreamActive(int stream, uint32_t inPastMs) const |
| 406 | { |
| 407 | if (mpPolicyManager == NULL) { |
| 408 | return 0; |
| 409 | } |
| 410 | Mutex::Autolock _l(mLock); |
| 411 | return mpPolicyManager->isStreamActive(stream, inPastMs); |
| 412 | } |
| 413 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 414 | void AudioPolicyService::binderDied(const wp<IBinder>& who) { |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 415 | LOGW("binderDied() %p, tid %d, calling tid %d", who.unsafe_get(), gettid(), |
| 416 | IPCThreadState::self()->getCallingPid()); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 419 | static bool tryLock(Mutex& mutex) |
| 420 | { |
| 421 | bool locked = false; |
| 422 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 423 | if (mutex.tryLock() == NO_ERROR) { |
| 424 | locked = true; |
| 425 | break; |
| 426 | } |
| 427 | usleep(kDumpLockSleep); |
| 428 | } |
| 429 | return locked; |
| 430 | } |
| 431 | |
| 432 | status_t AudioPolicyService::dumpInternals(int fd) |
| 433 | { |
| 434 | const size_t SIZE = 256; |
| 435 | char buffer[SIZE]; |
| 436 | String8 result; |
| 437 | |
| 438 | snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpPolicyManager); |
| 439 | result.append(buffer); |
| 440 | snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get()); |
| 441 | result.append(buffer); |
| 442 | snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get()); |
| 443 | result.append(buffer); |
| 444 | |
| 445 | write(fd, result.string(), result.size()); |
| 446 | return NO_ERROR; |
| 447 | } |
| 448 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 449 | status_t AudioPolicyService::dump(int fd, const Vector<String16>& args) |
| 450 | { |
| 451 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 452 | dumpPermissionDenial(fd); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 453 | } else { |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 454 | bool locked = tryLock(mLock); |
| 455 | if (!locked) { |
| 456 | String8 result(kDeadlockedString); |
| 457 | write(fd, result.string(), result.size()); |
| 458 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 459 | |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 460 | dumpInternals(fd); |
| 461 | if (mAudioCommandThread != NULL) { |
| 462 | mAudioCommandThread->dump(fd); |
| 463 | } |
| 464 | if (mTonePlaybackThread != NULL) { |
| 465 | mTonePlaybackThread->dump(fd); |
| 466 | } |
| 467 | |
| 468 | if (mpPolicyManager) { |
| 469 | mpPolicyManager->dump(fd); |
| 470 | } |
| 471 | |
| 472 | if (locked) mLock.unlock(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 473 | } |
| 474 | return NO_ERROR; |
| 475 | } |
| 476 | |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 477 | status_t AudioPolicyService::dumpPermissionDenial(int fd) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 478 | { |
| 479 | const size_t SIZE = 256; |
| 480 | char buffer[SIZE]; |
| 481 | String8 result; |
| 482 | snprintf(buffer, SIZE, "Permission Denial: " |
| 483 | "can't dump AudioPolicyService from pid=%d, uid=%d\n", |
| 484 | IPCThreadState::self()->getCallingPid(), |
| 485 | IPCThreadState::self()->getCallingUid()); |
| 486 | result.append(buffer); |
| 487 | write(fd, result.string(), result.size()); |
| 488 | return NO_ERROR; |
| 489 | } |
| 490 | |
| 491 | status_t AudioPolicyService::onTransact( |
| 492 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 493 | { |
| 494 | return BnAudioPolicyService::onTransact(code, data, reply, flags); |
| 495 | } |
| 496 | |
| 497 | |
| 498 | // ---------------------------------------------------------------------------- |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 499 | // AudioPolicyClientInterface implementation |
| 500 | // ---------------------------------------------------------------------------- |
| 501 | |
| 502 | |
| 503 | audio_io_handle_t AudioPolicyService::openOutput(uint32_t *pDevices, |
| 504 | uint32_t *pSamplingRate, |
| 505 | uint32_t *pFormat, |
| 506 | uint32_t *pChannels, |
| 507 | uint32_t *pLatencyMs, |
| 508 | AudioSystem::output_flags flags) |
| 509 | { |
| 510 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 511 | if (af == 0) { |
| 512 | LOGW("openOutput() could not get AudioFlinger"); |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 513 | return 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 516 | return af->openOutput(pDevices, |
| 517 | pSamplingRate, |
| 518 | (uint32_t *)pFormat, |
| 519 | pChannels, |
| 520 | pLatencyMs, |
| 521 | flags); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 524 | audio_io_handle_t AudioPolicyService::openDuplicateOutput(audio_io_handle_t output1, |
| 525 | audio_io_handle_t output2) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 526 | { |
| 527 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 528 | if (af == 0) { |
| 529 | LOGW("openDuplicateOutput() could not get AudioFlinger"); |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 530 | return 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 531 | } |
| 532 | return af->openDuplicateOutput(output1, output2); |
| 533 | } |
| 534 | |
| 535 | status_t AudioPolicyService::closeOutput(audio_io_handle_t output) |
| 536 | { |
| 537 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 538 | if (af == 0) return PERMISSION_DENIED; |
| 539 | |
| 540 | return af->closeOutput(output); |
| 541 | } |
| 542 | |
| 543 | |
| 544 | status_t AudioPolicyService::suspendOutput(audio_io_handle_t output) |
| 545 | { |
| 546 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 547 | if (af == 0) { |
| 548 | LOGW("suspendOutput() could not get AudioFlinger"); |
| 549 | return PERMISSION_DENIED; |
| 550 | } |
| 551 | |
| 552 | return af->suspendOutput(output); |
| 553 | } |
| 554 | |
| 555 | status_t AudioPolicyService::restoreOutput(audio_io_handle_t output) |
| 556 | { |
| 557 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 558 | if (af == 0) { |
| 559 | LOGW("restoreOutput() could not get AudioFlinger"); |
| 560 | return PERMISSION_DENIED; |
| 561 | } |
| 562 | |
| 563 | return af->restoreOutput(output); |
| 564 | } |
| 565 | |
| 566 | audio_io_handle_t AudioPolicyService::openInput(uint32_t *pDevices, |
| 567 | uint32_t *pSamplingRate, |
| 568 | uint32_t *pFormat, |
| 569 | uint32_t *pChannels, |
| 570 | uint32_t acoustics) |
| 571 | { |
| 572 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 573 | if (af == 0) { |
| 574 | LOGW("openInput() could not get AudioFlinger"); |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 575 | return 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | return af->openInput(pDevices, pSamplingRate, (uint32_t *)pFormat, pChannels, acoustics); |
| 579 | } |
| 580 | |
| 581 | status_t AudioPolicyService::closeInput(audio_io_handle_t input) |
| 582 | { |
| 583 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 584 | if (af == 0) return PERMISSION_DENIED; |
| 585 | |
| 586 | return af->closeInput(input); |
| 587 | } |
| 588 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 589 | status_t AudioPolicyService::setStreamVolume(AudioSystem::stream_type stream, |
| 590 | float volume, |
| 591 | audio_io_handle_t output, |
| 592 | int delayMs) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 593 | { |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 594 | return mAudioCommandThread->volumeCommand((int)stream, volume, (int)output, delayMs); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 597 | status_t AudioPolicyService::setStreamOutput(AudioSystem::stream_type stream, |
| 598 | audio_io_handle_t output) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 599 | { |
| 600 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 601 | if (af == 0) return PERMISSION_DENIED; |
| 602 | |
| 603 | return af->setStreamOutput(stream, output); |
| 604 | } |
| 605 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 606 | status_t AudioPolicyService::moveEffects(int session, audio_io_handle_t srcOutput, |
| 607 | audio_io_handle_t dstOutput) |
| 608 | { |
| 609 | sp<IAudioFlinger> af = AudioSystem::get_audio_flinger(); |
| 610 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 611 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 612 | return af->moveEffects(session, (int)srcOutput, (int)dstOutput); |
| 613 | } |
| 614 | |
| 615 | void AudioPolicyService::setParameters(audio_io_handle_t ioHandle, |
| 616 | const String8& keyValuePairs, |
| 617 | int delayMs) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 618 | { |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 619 | mAudioCommandThread->parametersCommand((int)ioHandle, keyValuePairs, delayMs); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | String8 AudioPolicyService::getParameters(audio_io_handle_t ioHandle, const String8& keys) |
| 623 | { |
| 624 | String8 result = AudioSystem::getParameters(ioHandle, keys); |
| 625 | return result; |
| 626 | } |
| 627 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 628 | status_t AudioPolicyService::startTone(ToneGenerator::tone_type tone, |
| 629 | AudioSystem::stream_type stream) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 630 | { |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 631 | mTonePlaybackThread->startToneCommand(tone, stream); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 632 | return NO_ERROR; |
| 633 | } |
| 634 | |
| 635 | status_t AudioPolicyService::stopTone() |
| 636 | { |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 637 | mTonePlaybackThread->stopToneCommand(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 638 | return NO_ERROR; |
| 639 | } |
| 640 | |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 641 | status_t AudioPolicyService::setVoiceVolume(float volume, int delayMs) |
| 642 | { |
| 643 | return mAudioCommandThread->voiceVolumeCommand(volume, delayMs); |
| 644 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 645 | |
| 646 | // ----------- AudioPolicyService::AudioCommandThread implementation ---------- |
| 647 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 648 | AudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name) |
| 649 | : Thread(false), mName(name) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 650 | { |
| 651 | mpToneGenerator = NULL; |
| 652 | } |
| 653 | |
| 654 | |
| 655 | AudioPolicyService::AudioCommandThread::~AudioCommandThread() |
| 656 | { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 657 | if (mName != "" && !mAudioCommands.isEmpty()) { |
| 658 | release_wake_lock(mName.string()); |
| 659 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 660 | mAudioCommands.clear(); |
| 661 | if (mpToneGenerator != NULL) delete mpToneGenerator; |
| 662 | } |
| 663 | |
| 664 | void AudioPolicyService::AudioCommandThread::onFirstRef() |
| 665 | { |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 666 | if (mName != "") { |
| 667 | run(mName.string(), ANDROID_PRIORITY_AUDIO); |
| 668 | } else { |
| 669 | run("AudioCommandThread", ANDROID_PRIORITY_AUDIO); |
| 670 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | bool AudioPolicyService::AudioCommandThread::threadLoop() |
| 674 | { |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 675 | nsecs_t waitTime = INT64_MAX; |
| 676 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 677 | mLock.lock(); |
| 678 | while (!exitPending()) |
| 679 | { |
| 680 | while(!mAudioCommands.isEmpty()) { |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 681 | nsecs_t curTime = systemTime(); |
| 682 | // commands are sorted by increasing time stamp: execute them from index 0 and up |
| 683 | if (mAudioCommands[0]->mTime <= curTime) { |
| 684 | AudioCommand *command = mAudioCommands[0]; |
| 685 | mAudioCommands.removeAt(0); |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 686 | mLastCommand = *command; |
| 687 | |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 688 | switch (command->mCommand) { |
| 689 | case START_TONE: { |
| 690 | mLock.unlock(); |
| 691 | ToneData *data = (ToneData *)command->mParam; |
| 692 | LOGV("AudioCommandThread() processing start tone %d on stream %d", |
| 693 | data->mType, data->mStream); |
| 694 | if (mpToneGenerator != NULL) |
| 695 | delete mpToneGenerator; |
| 696 | mpToneGenerator = new ToneGenerator(data->mStream, 1.0); |
| 697 | mpToneGenerator->startTone(data->mType); |
| 698 | delete data; |
| 699 | mLock.lock(); |
| 700 | }break; |
| 701 | case STOP_TONE: { |
| 702 | mLock.unlock(); |
| 703 | LOGV("AudioCommandThread() processing stop tone"); |
| 704 | if (mpToneGenerator != NULL) { |
| 705 | mpToneGenerator->stopTone(); |
| 706 | delete mpToneGenerator; |
| 707 | mpToneGenerator = NULL; |
| 708 | } |
| 709 | mLock.lock(); |
| 710 | }break; |
| 711 | case SET_VOLUME: { |
| 712 | VolumeData *data = (VolumeData *)command->mParam; |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 713 | LOGV("AudioCommandThread() processing set volume stream %d, \ |
| 714 | volume %f, output %d", data->mStream, data->mVolume, data->mIO); |
| 715 | command->mStatus = AudioSystem::setStreamVolume(data->mStream, |
| 716 | data->mVolume, |
| 717 | data->mIO); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 718 | if (command->mWaitStatus) { |
| 719 | command->mCond.signal(); |
| 720 | mWaitWorkCV.wait(mLock); |
| 721 | } |
| 722 | delete data; |
| 723 | }break; |
| 724 | case SET_PARAMETERS: { |
| 725 | ParametersData *data = (ParametersData *)command->mParam; |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 726 | LOGV("AudioCommandThread() processing set parameters string %s, io %d", |
| 727 | data->mKeyValuePairs.string(), data->mIO); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 728 | command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs); |
| 729 | if (command->mWaitStatus) { |
| 730 | command->mCond.signal(); |
| 731 | mWaitWorkCV.wait(mLock); |
| 732 | } |
| 733 | delete data; |
| 734 | }break; |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 735 | case SET_VOICE_VOLUME: { |
| 736 | VoiceVolumeData *data = (VoiceVolumeData *)command->mParam; |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 737 | LOGV("AudioCommandThread() processing set voice volume volume %f", |
| 738 | data->mVolume); |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 739 | command->mStatus = AudioSystem::setVoiceVolume(data->mVolume); |
| 740 | if (command->mWaitStatus) { |
| 741 | command->mCond.signal(); |
| 742 | mWaitWorkCV.wait(mLock); |
| 743 | } |
| 744 | delete data; |
| 745 | }break; |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 746 | default: |
| 747 | LOGW("AudioCommandThread() unknown command %d", command->mCommand); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 748 | } |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 749 | delete command; |
| 750 | waitTime = INT64_MAX; |
| 751 | } else { |
| 752 | waitTime = mAudioCommands[0]->mTime - curTime; |
| 753 | break; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 754 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 755 | } |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 756 | // release delayed commands wake lock |
| 757 | if (mName != "" && mAudioCommands.isEmpty()) { |
| 758 | release_wake_lock(mName.string()); |
| 759 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 760 | LOGV("AudioCommandThread() going to sleep"); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 761 | mWaitWorkCV.waitRelative(mLock, waitTime); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 762 | LOGV("AudioCommandThread() waking up"); |
| 763 | } |
| 764 | mLock.unlock(); |
| 765 | return false; |
| 766 | } |
| 767 | |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 768 | status_t AudioPolicyService::AudioCommandThread::dump(int fd) |
| 769 | { |
| 770 | const size_t SIZE = 256; |
| 771 | char buffer[SIZE]; |
| 772 | String8 result; |
| 773 | |
| 774 | snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this); |
| 775 | result.append(buffer); |
| 776 | write(fd, result.string(), result.size()); |
| 777 | |
| 778 | bool locked = tryLock(mLock); |
| 779 | if (!locked) { |
| 780 | String8 result2(kCmdDeadlockedString); |
| 781 | write(fd, result2.string(), result2.size()); |
| 782 | } |
| 783 | |
| 784 | snprintf(buffer, SIZE, "- Commands:\n"); |
| 785 | result = String8(buffer); |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 786 | result.append(" Command Time Wait pParam\n"); |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 787 | for (int i = 0; i < (int)mAudioCommands.size(); i++) { |
| 788 | mAudioCommands[i]->dump(buffer, SIZE); |
| 789 | result.append(buffer); |
| 790 | } |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 791 | result.append(" Last Command\n"); |
| 792 | mLastCommand.dump(buffer, SIZE); |
| 793 | result.append(buffer); |
| 794 | |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 795 | write(fd, result.string(), result.size()); |
| 796 | |
| 797 | if (locked) mLock.unlock(); |
| 798 | |
| 799 | return NO_ERROR; |
| 800 | } |
| 801 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 802 | void AudioPolicyService::AudioCommandThread::startToneCommand(int type, int stream) |
| 803 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 804 | AudioCommand *command = new AudioCommand(); |
| 805 | command->mCommand = START_TONE; |
| 806 | ToneData *data = new ToneData(); |
| 807 | data->mType = type; |
| 808 | data->mStream = stream; |
| 809 | command->mParam = (void *)data; |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 810 | command->mWaitStatus = false; |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 811 | Mutex::Autolock _l(mLock); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 812 | insertCommand_l(command); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 813 | LOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream); |
| 814 | mWaitWorkCV.signal(); |
| 815 | } |
| 816 | |
| 817 | void AudioPolicyService::AudioCommandThread::stopToneCommand() |
| 818 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 819 | AudioCommand *command = new AudioCommand(); |
| 820 | command->mCommand = STOP_TONE; |
| 821 | command->mParam = NULL; |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 822 | command->mWaitStatus = false; |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 823 | Mutex::Autolock _l(mLock); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 824 | insertCommand_l(command); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 825 | LOGV("AudioCommandThread() adding tone stop"); |
| 826 | mWaitWorkCV.signal(); |
| 827 | } |
| 828 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 829 | status_t AudioPolicyService::AudioCommandThread::volumeCommand(int stream, |
| 830 | float volume, |
| 831 | int output, |
| 832 | int delayMs) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 833 | { |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 834 | status_t status = NO_ERROR; |
| 835 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 836 | AudioCommand *command = new AudioCommand(); |
| 837 | command->mCommand = SET_VOLUME; |
| 838 | VolumeData *data = new VolumeData(); |
| 839 | data->mStream = stream; |
| 840 | data->mVolume = volume; |
| 841 | data->mIO = output; |
| 842 | command->mParam = data; |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 843 | if (delayMs == 0) { |
| 844 | command->mWaitStatus = true; |
| 845 | } else { |
| 846 | command->mWaitStatus = false; |
| 847 | } |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 848 | Mutex::Autolock _l(mLock); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 849 | insertCommand_l(command, delayMs); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 850 | LOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d", |
| 851 | stream, volume, output); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 852 | mWaitWorkCV.signal(); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 853 | if (command->mWaitStatus) { |
| 854 | command->mCond.wait(mLock); |
| 855 | status = command->mStatus; |
| 856 | mWaitWorkCV.signal(); |
| 857 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 858 | return status; |
| 859 | } |
| 860 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 861 | status_t AudioPolicyService::AudioCommandThread::parametersCommand(int ioHandle, |
| 862 | const String8& keyValuePairs, |
| 863 | int delayMs) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 864 | { |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 865 | status_t status = NO_ERROR; |
| 866 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 867 | AudioCommand *command = new AudioCommand(); |
| 868 | command->mCommand = SET_PARAMETERS; |
| 869 | ParametersData *data = new ParametersData(); |
| 870 | data->mIO = ioHandle; |
| 871 | data->mKeyValuePairs = keyValuePairs; |
| 872 | command->mParam = data; |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 873 | if (delayMs == 0) { |
| 874 | command->mWaitStatus = true; |
| 875 | } else { |
| 876 | command->mWaitStatus = false; |
| 877 | } |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 878 | Mutex::Autolock _l(mLock); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 879 | insertCommand_l(command, delayMs); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 880 | LOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d", |
| 881 | keyValuePairs.string(), ioHandle, delayMs); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 882 | mWaitWorkCV.signal(); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 883 | if (command->mWaitStatus) { |
| 884 | command->mCond.wait(mLock); |
| 885 | status = command->mStatus; |
| 886 | mWaitWorkCV.signal(); |
| 887 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 888 | return status; |
| 889 | } |
| 890 | |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 891 | status_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs) |
| 892 | { |
| 893 | status_t status = NO_ERROR; |
| 894 | |
| 895 | AudioCommand *command = new AudioCommand(); |
| 896 | command->mCommand = SET_VOICE_VOLUME; |
| 897 | VoiceVolumeData *data = new VoiceVolumeData(); |
| 898 | data->mVolume = volume; |
| 899 | command->mParam = data; |
| 900 | if (delayMs == 0) { |
| 901 | command->mWaitStatus = true; |
| 902 | } else { |
| 903 | command->mWaitStatus = false; |
| 904 | } |
| 905 | Mutex::Autolock _l(mLock); |
| 906 | insertCommand_l(command, delayMs); |
| 907 | LOGV("AudioCommandThread() adding set voice volume volume %f", volume); |
| 908 | mWaitWorkCV.signal(); |
| 909 | if (command->mWaitStatus) { |
| 910 | command->mCond.wait(mLock); |
| 911 | status = command->mStatus; |
| 912 | mWaitWorkCV.signal(); |
| 913 | } |
| 914 | return status; |
| 915 | } |
| 916 | |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 917 | // insertCommand_l() must be called with mLock held |
| 918 | void AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs) |
| 919 | { |
| 920 | ssize_t i; |
| 921 | Vector <AudioCommand *> removedCommands; |
| 922 | |
| 923 | command->mTime = systemTime() + milliseconds(delayMs); |
| 924 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 925 | // acquire wake lock to make sure delayed commands are processed |
| 926 | if (mName != "" && mAudioCommands.isEmpty()) { |
| 927 | acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string()); |
| 928 | } |
| 929 | |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 930 | // check same pending commands with later time stamps and eliminate them |
| 931 | for (i = mAudioCommands.size()-1; i >= 0; i--) { |
| 932 | AudioCommand *command2 = mAudioCommands[i]; |
| 933 | // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands |
| 934 | if (command2->mTime <= command->mTime) break; |
| 935 | if (command2->mCommand != command->mCommand) continue; |
| 936 | |
| 937 | switch (command->mCommand) { |
| 938 | case SET_PARAMETERS: { |
| 939 | ParametersData *data = (ParametersData *)command->mParam; |
| 940 | ParametersData *data2 = (ParametersData *)command2->mParam; |
| 941 | if (data->mIO != data2->mIO) break; |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 942 | LOGV("Comparing parameter command %s to new command %s", |
| 943 | data2->mKeyValuePairs.string(), data->mKeyValuePairs.string()); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 944 | AudioParameter param = AudioParameter(data->mKeyValuePairs); |
| 945 | AudioParameter param2 = AudioParameter(data2->mKeyValuePairs); |
| 946 | for (size_t j = 0; j < param.size(); j++) { |
| 947 | String8 key; |
| 948 | String8 value; |
| 949 | param.getAt(j, key, value); |
| 950 | for (size_t k = 0; k < param2.size(); k++) { |
| 951 | String8 key2; |
| 952 | String8 value2; |
| 953 | param2.getAt(k, key2, value2); |
| 954 | if (key2 == key) { |
| 955 | param2.remove(key2); |
| 956 | LOGV("Filtering out parameter %s", key2.string()); |
| 957 | break; |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | // if all keys have been filtered out, remove the command. |
| 962 | // otherwise, update the key value pairs |
| 963 | if (param2.size() == 0) { |
| 964 | removedCommands.add(command2); |
| 965 | } else { |
| 966 | data2->mKeyValuePairs = param2.toString(); |
| 967 | } |
| 968 | } break; |
| 969 | |
| 970 | case SET_VOLUME: { |
| 971 | VolumeData *data = (VolumeData *)command->mParam; |
| 972 | VolumeData *data2 = (VolumeData *)command2->mParam; |
| 973 | if (data->mIO != data2->mIO) break; |
| 974 | if (data->mStream != data2->mStream) break; |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 975 | LOGV("Filtering out volume command on output %d for stream %d", |
| 976 | data->mIO, data->mStream); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 977 | removedCommands.add(command2); |
| 978 | } break; |
| 979 | case START_TONE: |
| 980 | case STOP_TONE: |
| 981 | default: |
| 982 | break; |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | // remove filtered commands |
| 987 | for (size_t j = 0; j < removedCommands.size(); j++) { |
| 988 | // removed commands always have time stamps greater than current command |
| 989 | for (size_t k = i + 1; k < mAudioCommands.size(); k++) { |
| 990 | if (mAudioCommands[k] == removedCommands[j]) { |
| 991 | LOGV("suppressing command: %d", mAudioCommands[k]->mCommand); |
| 992 | mAudioCommands.removeAt(k); |
| 993 | break; |
| 994 | } |
| 995 | } |
| 996 | } |
| 997 | removedCommands.clear(); |
| 998 | |
| 999 | // insert command at the right place according to its time stamp |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1000 | LOGV("inserting command: %d at index %d, num commands %d", |
| 1001 | command->mCommand, (int)i+1, mAudioCommands.size()); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 1002 | mAudioCommands.insertAt(command, i + 1); |
| 1003 | } |
| 1004 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1005 | void AudioPolicyService::AudioCommandThread::exit() |
| 1006 | { |
| 1007 | LOGV("AudioCommandThread::exit"); |
| 1008 | { |
| 1009 | AutoMutex _l(mLock); |
| 1010 | requestExit(); |
| 1011 | mWaitWorkCV.signal(); |
| 1012 | } |
| 1013 | requestExitAndWait(); |
| 1014 | } |
| 1015 | |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 1016 | void AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size) |
| 1017 | { |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 1018 | snprintf(buffer, size, " %02d %06d.%03d %01u %p\n", |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 1019 | mCommand, |
| 1020 | (int)ns2s(mTime), |
| 1021 | (int)ns2ms(mTime)%1000, |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 1022 | mWaitStatus, |
| 1023 | mParam); |
| 1024 | } |
| 1025 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1026 | }; // namespace android |