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