AudioPolicyInterfaceImpl.cpp revision ba2b43990a7b4f0f2c425cf6cdfc63376a45772c
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 "AudioPolicyIntefaceImpl" 18//#define LOG_NDEBUG 0 19 20#include <utils/Log.h> 21#include "AudioPolicyService.h" 22#include "ServiceUtilities.h" 23 24namespace android { 25 26 27// ---------------------------------------------------------------------------- 28 29status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device, 30 audio_policy_dev_state_t state, 31 const char *device_address) 32{ 33 if (mAudioPolicyManager == NULL) { 34 return NO_INIT; 35 } 36 if (!settingsAllowed()) { 37 return PERMISSION_DENIED; 38 } 39 if (!audio_is_output_device(device) && !audio_is_input_device(device)) { 40 return BAD_VALUE; 41 } 42 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE && 43 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { 44 return BAD_VALUE; 45 } 46 47 ALOGV("setDeviceConnectionState()"); 48 Mutex::Autolock _l(mLock); 49 return mAudioPolicyManager->setDeviceConnectionState(device, 50 state, device_address); 51} 52 53audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState( 54 audio_devices_t device, 55 const char *device_address) 56{ 57 if (mAudioPolicyManager == NULL) { 58 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; 59 } 60 return mAudioPolicyManager->getDeviceConnectionState(device, 61 device_address); 62} 63 64status_t AudioPolicyService::setPhoneState(audio_mode_t state) 65{ 66 if (mAudioPolicyManager == NULL) { 67 return NO_INIT; 68 } 69 if (!settingsAllowed()) { 70 return PERMISSION_DENIED; 71 } 72 if (uint32_t(state) >= AUDIO_MODE_CNT) { 73 return BAD_VALUE; 74 } 75 76 ALOGV("setPhoneState()"); 77 78 // TODO: check if it is more appropriate to do it in platform specific policy manager 79 AudioSystem::setMode(state); 80 81 Mutex::Autolock _l(mLock); 82 mAudioPolicyManager->setPhoneState(state); 83 return NO_ERROR; 84} 85 86status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage, 87 audio_policy_forced_cfg_t config) 88{ 89 if (mAudioPolicyManager == NULL) { 90 return NO_INIT; 91 } 92 if (!settingsAllowed()) { 93 return PERMISSION_DENIED; 94 } 95 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { 96 return BAD_VALUE; 97 } 98 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) { 99 return BAD_VALUE; 100 } 101 ALOGV("setForceUse()"); 102 Mutex::Autolock _l(mLock); 103 mAudioPolicyManager->setForceUse(usage, config); 104 return NO_ERROR; 105} 106 107audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage) 108{ 109 if (mAudioPolicyManager == NULL) { 110 return AUDIO_POLICY_FORCE_NONE; 111 } 112 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { 113 return AUDIO_POLICY_FORCE_NONE; 114 } 115 return mAudioPolicyManager->getForceUse(usage); 116} 117 118audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream, 119 uint32_t samplingRate, 120 audio_format_t format, 121 audio_channel_mask_t channelMask, 122 audio_output_flags_t flags, 123 const audio_offload_info_t *offloadInfo) 124{ 125 if (mAudioPolicyManager == NULL) { 126 return 0; 127 } 128 ALOGV("getOutput()"); 129 Mutex::Autolock _l(mLock); 130 return mAudioPolicyManager->getOutput(stream, samplingRate, 131 format, channelMask, flags, offloadInfo); 132} 133 134audio_io_handle_t AudioPolicyService::getOutputForAttr(const audio_attributes_t *attr, 135 uint32_t samplingRate, 136 audio_format_t format, 137 audio_channel_mask_t channelMask, 138 audio_output_flags_t flags, 139 const audio_offload_info_t *offloadInfo) 140{ 141 if (mAudioPolicyManager == NULL) { 142 return 0; 143 } 144 ALOGV("getOutput()"); 145 Mutex::Autolock _l(mLock); 146 return mAudioPolicyManager->getOutputForAttr(attr, samplingRate, 147 format, channelMask, flags, offloadInfo); 148} 149 150status_t AudioPolicyService::startOutput(audio_io_handle_t output, 151 audio_stream_type_t stream, 152 int session) 153{ 154 if (mAudioPolicyManager == NULL) { 155 return NO_INIT; 156 } 157 ALOGV("startOutput()"); 158 Mutex::Autolock _l(mLock); 159 160 // create audio processors according to stream 161 status_t status = mAudioPolicyEffects->addOutputSessionEffects(output, stream, session); 162 if (status != NO_ERROR && status != ALREADY_EXISTS) { 163 ALOGW("Failed to add effects on session %d", session); 164 } 165 166 return mAudioPolicyManager->startOutput(output, stream, session); 167} 168 169status_t AudioPolicyService::stopOutput(audio_io_handle_t output, 170 audio_stream_type_t stream, 171 int session) 172{ 173 if (mAudioPolicyManager == NULL) { 174 return NO_INIT; 175 } 176 ALOGV("stopOutput()"); 177 mOutputCommandThread->stopOutputCommand(output, stream, session); 178 return NO_ERROR; 179} 180 181status_t AudioPolicyService::doStopOutput(audio_io_handle_t output, 182 audio_stream_type_t stream, 183 int session) 184{ 185 ALOGV("doStopOutput from tid %d", gettid()); 186 Mutex::Autolock _l(mLock); 187 188 // release audio processors from the stream 189 status_t status = mAudioPolicyEffects->releaseOutputSessionEffects(output, stream, session); 190 if (status != NO_ERROR && status != ALREADY_EXISTS) { 191 ALOGW("Failed to release effects on session %d", session); 192 } 193 194 return mAudioPolicyManager->stopOutput(output, stream, session); 195} 196 197void AudioPolicyService::releaseOutput(audio_io_handle_t output) 198{ 199 if (mAudioPolicyManager == NULL) { 200 return; 201 } 202 ALOGV("releaseOutput()"); 203 mOutputCommandThread->releaseOutputCommand(output); 204} 205 206void AudioPolicyService::doReleaseOutput(audio_io_handle_t output) 207{ 208 ALOGV("doReleaseOutput from tid %d", gettid()); 209 Mutex::Autolock _l(mLock); 210 mAudioPolicyManager->releaseOutput(output); 211} 212 213audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource, 214 uint32_t samplingRate, 215 audio_format_t format, 216 audio_channel_mask_t channelMask, 217 int audioSession) 218{ 219 if (mAudioPolicyManager == NULL) { 220 return 0; 221 } 222 // already checked by client, but double-check in case the client wrapper is bypassed 223 if (inputSource >= AUDIO_SOURCE_CNT && inputSource != AUDIO_SOURCE_HOTWORD) { 224 return 0; 225 } 226 227 if ((inputSource == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) { 228 return 0; 229 } 230 231 Mutex::Autolock _l(mLock); 232 // the audio_in_acoustics_t parameter is ignored by get_input() 233 audio_io_handle_t input = mAudioPolicyManager->getInput(inputSource, samplingRate, 234 format, channelMask, (audio_in_acoustics_t) 0); 235 236 if (input == 0) { 237 return input; 238 } 239 240 // create audio pre processors according to input source 241 status_t status = mAudioPolicyEffects->addInputEffects(input, inputSource, audioSession); 242 if (status != NO_ERROR && status != ALREADY_EXISTS) { 243 ALOGW("Failed to add effects on input %d", input); 244 } 245 246 return input; 247} 248 249status_t AudioPolicyService::startInput(audio_io_handle_t input) 250{ 251 if (mAudioPolicyManager == NULL) { 252 return NO_INIT; 253 } 254 Mutex::Autolock _l(mLock); 255 256 return mAudioPolicyManager->startInput(input); 257} 258 259status_t AudioPolicyService::stopInput(audio_io_handle_t input) 260{ 261 if (mAudioPolicyManager == NULL) { 262 return NO_INIT; 263 } 264 Mutex::Autolock _l(mLock); 265 266 return mAudioPolicyManager->stopInput(input); 267} 268 269void AudioPolicyService::releaseInput(audio_io_handle_t input) 270{ 271 if (mAudioPolicyManager == NULL) { 272 return; 273 } 274 Mutex::Autolock _l(mLock); 275 mAudioPolicyManager->releaseInput(input); 276 277 // release audio processors from the input 278 status_t status = mAudioPolicyEffects->releaseInputEffects(input); 279 if(status != NO_ERROR) { 280 ALOGW("Failed to release effects on input %d", input); 281 } 282} 283 284status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream, 285 int indexMin, 286 int indexMax) 287{ 288 if (mAudioPolicyManager == NULL) { 289 return NO_INIT; 290 } 291 if (!settingsAllowed()) { 292 return PERMISSION_DENIED; 293 } 294 if (uint32_t(stream) >= AUDIO_STREAM_CNT) { 295 return BAD_VALUE; 296 } 297 Mutex::Autolock _l(mLock); 298 mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax); 299 return NO_ERROR; 300} 301 302status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream, 303 int index, 304 audio_devices_t device) 305{ 306 if (mAudioPolicyManager == NULL) { 307 return NO_INIT; 308 } 309 if (!settingsAllowed()) { 310 return PERMISSION_DENIED; 311 } 312 if (uint32_t(stream) >= AUDIO_STREAM_CNT) { 313 return BAD_VALUE; 314 } 315 Mutex::Autolock _l(mLock); 316 return mAudioPolicyManager->setStreamVolumeIndex(stream, 317 index, 318 device); 319} 320 321status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream, 322 int *index, 323 audio_devices_t device) 324{ 325 if (mAudioPolicyManager == NULL) { 326 return NO_INIT; 327 } 328 if (uint32_t(stream) >= AUDIO_STREAM_CNT) { 329 return BAD_VALUE; 330 } 331 Mutex::Autolock _l(mLock); 332 return mAudioPolicyManager->getStreamVolumeIndex(stream, 333 index, 334 device); 335} 336 337uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream) 338{ 339 if (mAudioPolicyManager == NULL) { 340 return 0; 341 } 342 return mAudioPolicyManager->getStrategyForStream(stream); 343} 344 345//audio policy: use audio_device_t appropriately 346 347audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream) 348{ 349 if (mAudioPolicyManager == NULL) { 350 return (audio_devices_t)0; 351 } 352 return mAudioPolicyManager->getDevicesForStream(stream); 353} 354 355audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc) 356{ 357 // FIXME change return type to status_t, and return NO_INIT here 358 if (mAudioPolicyManager == NULL) { 359 return 0; 360 } 361 Mutex::Autolock _l(mLock); 362 return mAudioPolicyManager->getOutputForEffect(desc); 363} 364 365status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc, 366 audio_io_handle_t io, 367 uint32_t strategy, 368 int session, 369 int id) 370{ 371 if (mAudioPolicyManager == NULL) { 372 return NO_INIT; 373 } 374 return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id); 375} 376 377status_t AudioPolicyService::unregisterEffect(int id) 378{ 379 if (mAudioPolicyManager == NULL) { 380 return NO_INIT; 381 } 382 return mAudioPolicyManager->unregisterEffect(id); 383} 384 385status_t AudioPolicyService::setEffectEnabled(int id, bool enabled) 386{ 387 if (mAudioPolicyManager == NULL) { 388 return NO_INIT; 389 } 390 return mAudioPolicyManager->setEffectEnabled(id, enabled); 391} 392 393bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const 394{ 395 if (mAudioPolicyManager == NULL) { 396 return 0; 397 } 398 Mutex::Autolock _l(mLock); 399 return mAudioPolicyManager->isStreamActive(stream, inPastMs); 400} 401 402bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const 403{ 404 if (mAudioPolicyManager == NULL) { 405 return 0; 406 } 407 Mutex::Autolock _l(mLock); 408 return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs); 409} 410 411bool AudioPolicyService::isSourceActive(audio_source_t source) const 412{ 413 if (mAudioPolicyManager == NULL) { 414 return false; 415 } 416 Mutex::Autolock _l(mLock); 417 return mAudioPolicyManager->isSourceActive(source); 418} 419 420status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession, 421 effect_descriptor_t *descriptors, 422 uint32_t *count) 423{ 424 if (mAudioPolicyManager == NULL) { 425 *count = 0; 426 return NO_INIT; 427 } 428 Mutex::Autolock _l(mLock); 429 430 return mAudioPolicyEffects->queryDefaultInputEffects(audioSession, descriptors, count); 431} 432 433bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info) 434{ 435 if (mAudioPolicyManager == NULL) { 436 ALOGV("mAudioPolicyManager == NULL"); 437 return false; 438 } 439 440 return mAudioPolicyManager->isOffloadSupported(info); 441} 442 443status_t AudioPolicyService::listAudioPorts(audio_port_role_t role, 444 audio_port_type_t type, 445 unsigned int *num_ports, 446 struct audio_port *ports, 447 unsigned int *generation) 448{ 449 Mutex::Autolock _l(mLock); 450 if(!modifyAudioRoutingAllowed()) { 451 return PERMISSION_DENIED; 452 } 453 if (mAudioPolicyManager == NULL) { 454 return NO_INIT; 455 } 456 457 return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation); 458} 459 460status_t AudioPolicyService::getAudioPort(struct audio_port *port) 461{ 462 Mutex::Autolock _l(mLock); 463 if(!modifyAudioRoutingAllowed()) { 464 return PERMISSION_DENIED; 465 } 466 if (mAudioPolicyManager == NULL) { 467 return NO_INIT; 468 } 469 470 return mAudioPolicyManager->getAudioPort(port); 471} 472 473status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch, 474 audio_patch_handle_t *handle) 475{ 476 Mutex::Autolock _l(mLock); 477 if(!modifyAudioRoutingAllowed()) { 478 return PERMISSION_DENIED; 479 } 480 if (mAudioPolicyManager == NULL) { 481 return NO_INIT; 482 } 483 return mAudioPolicyManager->createAudioPatch(patch, handle, 484 IPCThreadState::self()->getCallingUid()); 485} 486 487status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle) 488{ 489 Mutex::Autolock _l(mLock); 490 if(!modifyAudioRoutingAllowed()) { 491 return PERMISSION_DENIED; 492 } 493 if (mAudioPolicyManager == NULL) { 494 return NO_INIT; 495 } 496 497 return mAudioPolicyManager->releaseAudioPatch(handle, 498 IPCThreadState::self()->getCallingUid()); 499} 500 501status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches, 502 struct audio_patch *patches, 503 unsigned int *generation) 504{ 505 Mutex::Autolock _l(mLock); 506 if(!modifyAudioRoutingAllowed()) { 507 return PERMISSION_DENIED; 508 } 509 if (mAudioPolicyManager == NULL) { 510 return NO_INIT; 511 } 512 513 return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation); 514} 515 516status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config) 517{ 518 Mutex::Autolock _l(mLock); 519 if(!modifyAudioRoutingAllowed()) { 520 return PERMISSION_DENIED; 521 } 522 if (mAudioPolicyManager == NULL) { 523 return NO_INIT; 524 } 525 526 return mAudioPolicyManager->setAudioPortConfig(config); 527} 528 529}; // namespace android 530