AudioPolicyInterfaceImpl.cpp revision baac183f3808d7b68f184647a97bd381620415fe
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 mPhoneState = state; 84 return NO_ERROR; 85} 86 87audio_mode_t AudioPolicyService::getPhoneState() 88{ 89 Mutex::Autolock _l(mLock); 90 return mPhoneState; 91} 92 93status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage, 94 audio_policy_forced_cfg_t config) 95{ 96 if (mAudioPolicyManager == NULL) { 97 return NO_INIT; 98 } 99 if (!settingsAllowed()) { 100 return PERMISSION_DENIED; 101 } 102 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { 103 return BAD_VALUE; 104 } 105 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) { 106 return BAD_VALUE; 107 } 108 ALOGV("setForceUse()"); 109 Mutex::Autolock _l(mLock); 110 mAudioPolicyManager->setForceUse(usage, config); 111 return NO_ERROR; 112} 113 114audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage) 115{ 116 if (mAudioPolicyManager == NULL) { 117 return AUDIO_POLICY_FORCE_NONE; 118 } 119 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { 120 return AUDIO_POLICY_FORCE_NONE; 121 } 122 return mAudioPolicyManager->getForceUse(usage); 123} 124 125audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream, 126 uint32_t samplingRate, 127 audio_format_t format, 128 audio_channel_mask_t channelMask, 129 audio_output_flags_t flags, 130 const audio_offload_info_t *offloadInfo) 131{ 132 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { 133 return AUDIO_IO_HANDLE_NONE; 134 } 135 if (mAudioPolicyManager == NULL) { 136 return AUDIO_IO_HANDLE_NONE; 137 } 138 ALOGV("getOutput()"); 139 Mutex::Autolock _l(mLock); 140 return mAudioPolicyManager->getOutput(stream, samplingRate, 141 format, channelMask, flags, offloadInfo); 142} 143 144status_t AudioPolicyService::getOutputForAttr(const audio_attributes_t *attr, 145 audio_io_handle_t *output, 146 audio_session_t session, 147 audio_stream_type_t *stream, 148 uint32_t samplingRate, 149 audio_format_t format, 150 audio_channel_mask_t channelMask, 151 audio_output_flags_t flags, 152 const audio_offload_info_t *offloadInfo) 153{ 154 if (mAudioPolicyManager == NULL) { 155 return NO_INIT; 156 } 157 ALOGV("getOutput()"); 158 Mutex::Autolock _l(mLock); 159 return mAudioPolicyManager->getOutputForAttr(attr, output, session, stream, samplingRate, 160 format, channelMask, flags, offloadInfo); 161} 162 163status_t AudioPolicyService::startOutput(audio_io_handle_t output, 164 audio_stream_type_t stream, 165 audio_session_t session) 166{ 167 if (uint32_t(stream) >= AUDIO_STREAM_CNT) { 168 return BAD_VALUE; 169 } 170 if (mAudioPolicyManager == NULL) { 171 return NO_INIT; 172 } 173 ALOGV("startOutput()"); 174 sp<AudioPolicyEffects>audioPolicyEffects; 175 { 176 Mutex::Autolock _l(mLock); 177 audioPolicyEffects = mAudioPolicyEffects; 178 } 179 if (audioPolicyEffects != 0) { 180 // create audio processors according to stream 181 status_t status = audioPolicyEffects->addOutputSessionEffects(output, stream, session); 182 if (status != NO_ERROR && status != ALREADY_EXISTS) { 183 ALOGW("Failed to add effects on session %d", session); 184 } 185 } 186 Mutex::Autolock _l(mLock); 187 return mAudioPolicyManager->startOutput(output, stream, session); 188} 189 190status_t AudioPolicyService::stopOutput(audio_io_handle_t output, 191 audio_stream_type_t stream, 192 audio_session_t session) 193{ 194 if (uint32_t(stream) >= AUDIO_STREAM_CNT) { 195 return BAD_VALUE; 196 } 197 if (mAudioPolicyManager == NULL) { 198 return NO_INIT; 199 } 200 ALOGV("stopOutput()"); 201 mOutputCommandThread->stopOutputCommand(output, stream, session); 202 return NO_ERROR; 203} 204 205status_t AudioPolicyService::doStopOutput(audio_io_handle_t output, 206 audio_stream_type_t stream, 207 audio_session_t session) 208{ 209 ALOGV("doStopOutput from tid %d", gettid()); 210 sp<AudioPolicyEffects>audioPolicyEffects; 211 { 212 Mutex::Autolock _l(mLock); 213 audioPolicyEffects = mAudioPolicyEffects; 214 } 215 if (audioPolicyEffects != 0) { 216 // release audio processors from the stream 217 status_t status = audioPolicyEffects->releaseOutputSessionEffects(output, stream, session); 218 if (status != NO_ERROR && status != ALREADY_EXISTS) { 219 ALOGW("Failed to release effects on session %d", session); 220 } 221 } 222 Mutex::Autolock _l(mLock); 223 return mAudioPolicyManager->stopOutput(output, stream, session); 224} 225 226void AudioPolicyService::releaseOutput(audio_io_handle_t output, 227 audio_stream_type_t stream, 228 audio_session_t session) 229{ 230 if (mAudioPolicyManager == NULL) { 231 return; 232 } 233 ALOGV("releaseOutput()"); 234 mOutputCommandThread->releaseOutputCommand(output, stream, session); 235} 236 237void AudioPolicyService::doReleaseOutput(audio_io_handle_t output, 238 audio_stream_type_t stream, 239 audio_session_t session) 240{ 241 ALOGV("doReleaseOutput from tid %d", gettid()); 242 Mutex::Autolock _l(mLock); 243 mAudioPolicyManager->releaseOutput(output, stream, session); 244} 245 246status_t AudioPolicyService::getInputForAttr(const audio_attributes_t *attr, 247 audio_io_handle_t *input, 248 audio_session_t session, 249 uint32_t samplingRate, 250 audio_format_t format, 251 audio_channel_mask_t channelMask, 252 audio_input_flags_t flags) 253{ 254 if (mAudioPolicyManager == NULL) { 255 return NO_INIT; 256 } 257 // already checked by client, but double-check in case the client wrapper is bypassed 258 if (attr->source >= AUDIO_SOURCE_CNT && attr->source != AUDIO_SOURCE_HOTWORD && 259 attr->source != AUDIO_SOURCE_FM_TUNER) { 260 return BAD_VALUE; 261 } 262 263 if (((attr->source == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) || 264 ((attr->source == AUDIO_SOURCE_FM_TUNER) && !captureFmTunerAllowed())) { 265 return BAD_VALUE; 266 } 267 sp<AudioPolicyEffects>audioPolicyEffects; 268 status_t status; 269 { 270 Mutex::Autolock _l(mLock); 271 // the audio_in_acoustics_t parameter is ignored by get_input() 272 status = mAudioPolicyManager->getInputForAttr(attr, input, session, 273 samplingRate, format, channelMask, 274 flags); 275 audioPolicyEffects = mAudioPolicyEffects; 276 } 277 if (status != NO_ERROR) { 278 return status; 279 } 280 if (audioPolicyEffects != 0) { 281 // create audio pre processors according to input source 282 status_t status = audioPolicyEffects->addInputEffects(*input, attr->source, session); 283 if (status != NO_ERROR && status != ALREADY_EXISTS) { 284 ALOGW("Failed to add effects on input %d", *input); 285 } 286 } 287 return NO_ERROR; 288} 289 290status_t AudioPolicyService::startInput(audio_io_handle_t input, 291 audio_session_t session) 292{ 293 if (mAudioPolicyManager == NULL) { 294 return NO_INIT; 295 } 296 Mutex::Autolock _l(mLock); 297 298 return mAudioPolicyManager->startInput(input, session); 299} 300 301status_t AudioPolicyService::stopInput(audio_io_handle_t input, 302 audio_session_t session) 303{ 304 if (mAudioPolicyManager == NULL) { 305 return NO_INIT; 306 } 307 Mutex::Autolock _l(mLock); 308 309 return mAudioPolicyManager->stopInput(input, session); 310} 311 312void AudioPolicyService::releaseInput(audio_io_handle_t input, 313 audio_session_t session) 314{ 315 if (mAudioPolicyManager == NULL) { 316 return; 317 } 318 sp<AudioPolicyEffects>audioPolicyEffects; 319 { 320 Mutex::Autolock _l(mLock); 321 mAudioPolicyManager->releaseInput(input, session); 322 audioPolicyEffects = mAudioPolicyEffects; 323 } 324 if (audioPolicyEffects != 0) { 325 // release audio processors from the input 326 status_t status = audioPolicyEffects->releaseInputEffects(input); 327 if(status != NO_ERROR) { 328 ALOGW("Failed to release effects on input %d", input); 329 } 330 } 331} 332 333status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream, 334 int indexMin, 335 int indexMax) 336{ 337 if (mAudioPolicyManager == NULL) { 338 return NO_INIT; 339 } 340 if (!settingsAllowed()) { 341 return PERMISSION_DENIED; 342 } 343 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { 344 return BAD_VALUE; 345 } 346 Mutex::Autolock _l(mLock); 347 mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax); 348 return NO_ERROR; 349} 350 351status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream, 352 int index, 353 audio_devices_t device) 354{ 355 if (mAudioPolicyManager == NULL) { 356 return NO_INIT; 357 } 358 if (!settingsAllowed()) { 359 return PERMISSION_DENIED; 360 } 361 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { 362 return BAD_VALUE; 363 } 364 Mutex::Autolock _l(mLock); 365 return mAudioPolicyManager->setStreamVolumeIndex(stream, 366 index, 367 device); 368} 369 370status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream, 371 int *index, 372 audio_devices_t device) 373{ 374 if (mAudioPolicyManager == NULL) { 375 return NO_INIT; 376 } 377 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { 378 return BAD_VALUE; 379 } 380 Mutex::Autolock _l(mLock); 381 return mAudioPolicyManager->getStreamVolumeIndex(stream, 382 index, 383 device); 384} 385 386uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream) 387{ 388 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { 389 return 0; 390 } 391 if (mAudioPolicyManager == NULL) { 392 return 0; 393 } 394 return mAudioPolicyManager->getStrategyForStream(stream); 395} 396 397//audio policy: use audio_device_t appropriately 398 399audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream) 400{ 401 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { 402 return AUDIO_DEVICE_NONE; 403 } 404 if (mAudioPolicyManager == NULL) { 405 return AUDIO_DEVICE_NONE; 406 } 407 return mAudioPolicyManager->getDevicesForStream(stream); 408} 409 410audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc) 411{ 412 // FIXME change return type to status_t, and return NO_INIT here 413 if (mAudioPolicyManager == NULL) { 414 return 0; 415 } 416 Mutex::Autolock _l(mLock); 417 return mAudioPolicyManager->getOutputForEffect(desc); 418} 419 420status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc, 421 audio_io_handle_t io, 422 uint32_t strategy, 423 int session, 424 int id) 425{ 426 if (mAudioPolicyManager == NULL) { 427 return NO_INIT; 428 } 429 return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id); 430} 431 432status_t AudioPolicyService::unregisterEffect(int id) 433{ 434 if (mAudioPolicyManager == NULL) { 435 return NO_INIT; 436 } 437 return mAudioPolicyManager->unregisterEffect(id); 438} 439 440status_t AudioPolicyService::setEffectEnabled(int id, bool enabled) 441{ 442 if (mAudioPolicyManager == NULL) { 443 return NO_INIT; 444 } 445 return mAudioPolicyManager->setEffectEnabled(id, enabled); 446} 447 448bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const 449{ 450 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { 451 return false; 452 } 453 if (mAudioPolicyManager == NULL) { 454 return false; 455 } 456 Mutex::Autolock _l(mLock); 457 return mAudioPolicyManager->isStreamActive(stream, inPastMs); 458} 459 460bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const 461{ 462 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { 463 return false; 464 } 465 if (mAudioPolicyManager == NULL) { 466 return false; 467 } 468 Mutex::Autolock _l(mLock); 469 return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs); 470} 471 472bool AudioPolicyService::isSourceActive(audio_source_t source) const 473{ 474 if (mAudioPolicyManager == NULL) { 475 return false; 476 } 477 Mutex::Autolock _l(mLock); 478 return mAudioPolicyManager->isSourceActive(source); 479} 480 481status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession, 482 effect_descriptor_t *descriptors, 483 uint32_t *count) 484{ 485 if (mAudioPolicyManager == NULL) { 486 *count = 0; 487 return NO_INIT; 488 } 489 sp<AudioPolicyEffects>audioPolicyEffects; 490 { 491 Mutex::Autolock _l(mLock); 492 audioPolicyEffects = mAudioPolicyEffects; 493 } 494 if (audioPolicyEffects == 0) { 495 *count = 0; 496 return NO_INIT; 497 } 498 return audioPolicyEffects->queryDefaultInputEffects(audioSession, descriptors, count); 499} 500 501bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info) 502{ 503 if (mAudioPolicyManager == NULL) { 504 ALOGV("mAudioPolicyManager == NULL"); 505 return false; 506 } 507 508 return mAudioPolicyManager->isOffloadSupported(info); 509} 510 511status_t AudioPolicyService::listAudioPorts(audio_port_role_t role, 512 audio_port_type_t type, 513 unsigned int *num_ports, 514 struct audio_port *ports, 515 unsigned int *generation) 516{ 517 Mutex::Autolock _l(mLock); 518 if(!modifyAudioRoutingAllowed()) { 519 return PERMISSION_DENIED; 520 } 521 if (mAudioPolicyManager == NULL) { 522 return NO_INIT; 523 } 524 525 return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation); 526} 527 528status_t AudioPolicyService::getAudioPort(struct audio_port *port) 529{ 530 Mutex::Autolock _l(mLock); 531 if(!modifyAudioRoutingAllowed()) { 532 return PERMISSION_DENIED; 533 } 534 if (mAudioPolicyManager == NULL) { 535 return NO_INIT; 536 } 537 538 return mAudioPolicyManager->getAudioPort(port); 539} 540 541status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch, 542 audio_patch_handle_t *handle) 543{ 544 Mutex::Autolock _l(mLock); 545 if(!modifyAudioRoutingAllowed()) { 546 return PERMISSION_DENIED; 547 } 548 if (mAudioPolicyManager == NULL) { 549 return NO_INIT; 550 } 551 return mAudioPolicyManager->createAudioPatch(patch, handle, 552 IPCThreadState::self()->getCallingUid()); 553} 554 555status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle) 556{ 557 Mutex::Autolock _l(mLock); 558 if(!modifyAudioRoutingAllowed()) { 559 return PERMISSION_DENIED; 560 } 561 if (mAudioPolicyManager == NULL) { 562 return NO_INIT; 563 } 564 565 return mAudioPolicyManager->releaseAudioPatch(handle, 566 IPCThreadState::self()->getCallingUid()); 567} 568 569status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches, 570 struct audio_patch *patches, 571 unsigned int *generation) 572{ 573 Mutex::Autolock _l(mLock); 574 if(!modifyAudioRoutingAllowed()) { 575 return PERMISSION_DENIED; 576 } 577 if (mAudioPolicyManager == NULL) { 578 return NO_INIT; 579 } 580 581 return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation); 582} 583 584status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config) 585{ 586 Mutex::Autolock _l(mLock); 587 if(!modifyAudioRoutingAllowed()) { 588 return PERMISSION_DENIED; 589 } 590 if (mAudioPolicyManager == NULL) { 591 return NO_INIT; 592 } 593 594 return mAudioPolicyManager->setAudioPortConfig(config); 595} 596 597status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session, 598 audio_io_handle_t *ioHandle, 599 audio_devices_t *device) 600{ 601 if (mAudioPolicyManager == NULL) { 602 return NO_INIT; 603 } 604 605 return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device); 606} 607 608status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session) 609{ 610 if (mAudioPolicyManager == NULL) { 611 return NO_INIT; 612 } 613 614 return mAudioPolicyManager->releaseSoundTriggerSession(session); 615} 616 617status_t AudioPolicyService::registerPolicyMixes(Vector<AudioMix> mixes, bool registration) 618{ 619 Mutex::Autolock _l(mLock); 620 if(!modifyAudioRoutingAllowed()) { 621 return PERMISSION_DENIED; 622 } 623 if (mAudioPolicyManager == NULL) { 624 return NO_INIT; 625 } 626 if (registration) { 627 return mAudioPolicyManager->registerPolicyMixes(mixes); 628 } else { 629 return mAudioPolicyManager->unregisterPolicyMixes(mixes); 630 } 631} 632 633}; // namespace android 634