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 <media/MediaAnalyticsItem.h> 22 23 #include "AudioPolicyService.h" 24 #include "ServiceUtilities.h" 25 #include "TypeConverter.h" 26 27 namespace android { 28 29 30 // ---------------------------------------------------------------------------- 31 32 status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device, 33 audio_policy_dev_state_t state, 34 const char *device_address, 35 const char *device_name) 36 { 37 if (mAudioPolicyManager == NULL) { 38 return NO_INIT; 39 } 40 if (!settingsAllowed()) { 41 return PERMISSION_DENIED; 42 } 43 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE && 44 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { 45 return BAD_VALUE; 46 } 47 48 ALOGV("setDeviceConnectionState()"); 49 Mutex::Autolock _l(mLock); 50 AutoCallerClear acc; 51 return mAudioPolicyManager->setDeviceConnectionState(device, state, 52 device_address, device_name); 53 } 54 55 audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState( 56 audio_devices_t device, 57 const char *device_address) 58 { 59 if (mAudioPolicyManager == NULL) { 60 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; 61 } 62 AutoCallerClear acc; 63 return mAudioPolicyManager->getDeviceConnectionState(device, 64 device_address); 65 } 66 67 status_t AudioPolicyService::handleDeviceConfigChange(audio_devices_t device, 68 const char *device_address, 69 const char *device_name) 70 { 71 if (mAudioPolicyManager == NULL) { 72 return NO_INIT; 73 } 74 if (!settingsAllowed()) { 75 return PERMISSION_DENIED; 76 } 77 78 ALOGV("handleDeviceConfigChange()"); 79 Mutex::Autolock _l(mLock); 80 AutoCallerClear acc; 81 return mAudioPolicyManager->handleDeviceConfigChange(device, device_address, 82 device_name); 83 } 84 85 status_t AudioPolicyService::setPhoneState(audio_mode_t state) 86 { 87 if (mAudioPolicyManager == NULL) { 88 return NO_INIT; 89 } 90 if (!settingsAllowed()) { 91 return PERMISSION_DENIED; 92 } 93 if (uint32_t(state) >= AUDIO_MODE_CNT) { 94 return BAD_VALUE; 95 } 96 97 ALOGV("setPhoneState()"); 98 99 // acquire lock before calling setMode() so that setMode() + setPhoneState() are an atomic 100 // operation from policy manager standpoint (no other operation (e.g track start or stop) 101 // can be interleaved). 102 Mutex::Autolock _l(mLock); 103 // TODO: check if it is more appropriate to do it in platform specific policy manager 104 AudioSystem::setMode(state); 105 106 AutoCallerClear acc; 107 mAudioPolicyManager->setPhoneState(state); 108 mPhoneState = state; 109 return NO_ERROR; 110 } 111 112 audio_mode_t AudioPolicyService::getPhoneState() 113 { 114 Mutex::Autolock _l(mLock); 115 return mPhoneState; 116 } 117 118 status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage, 119 audio_policy_forced_cfg_t config) 120 { 121 if (mAudioPolicyManager == NULL) { 122 return NO_INIT; 123 } 124 125 if (!modifyAudioRoutingAllowed()) { 126 return PERMISSION_DENIED; 127 } 128 129 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { 130 return BAD_VALUE; 131 } 132 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) { 133 return BAD_VALUE; 134 } 135 ALOGV("setForceUse()"); 136 Mutex::Autolock _l(mLock); 137 AutoCallerClear acc; 138 mAudioPolicyManager->setForceUse(usage, config); 139 return NO_ERROR; 140 } 141 142 audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage) 143 { 144 if (mAudioPolicyManager == NULL) { 145 return AUDIO_POLICY_FORCE_NONE; 146 } 147 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { 148 return AUDIO_POLICY_FORCE_NONE; 149 } 150 AutoCallerClear acc; 151 return mAudioPolicyManager->getForceUse(usage); 152 } 153 154 audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream) 155 { 156 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { 157 return AUDIO_IO_HANDLE_NONE; 158 } 159 if (mAudioPolicyManager == NULL) { 160 return AUDIO_IO_HANDLE_NONE; 161 } 162 ALOGV("getOutput()"); 163 Mutex::Autolock _l(mLock); 164 AutoCallerClear acc; 165 return mAudioPolicyManager->getOutput(stream); 166 } 167 168 status_t AudioPolicyService::getOutputForAttr(const audio_attributes_t *attr, 169 audio_io_handle_t *output, 170 audio_session_t session, 171 audio_stream_type_t *stream, 172 pid_t pid, 173 uid_t uid, 174 const audio_config_t *config, 175 audio_output_flags_t flags, 176 audio_port_handle_t *selectedDeviceId, 177 audio_port_handle_t *portId) 178 { 179 if (mAudioPolicyManager == NULL) { 180 return NO_INIT; 181 } 182 ALOGV("getOutputForAttr()"); 183 Mutex::Autolock _l(mLock); 184 185 const uid_t callingUid = IPCThreadState::self()->getCallingUid(); 186 if (!isTrustedCallingUid(callingUid) || uid == (uid_t)-1) { 187 ALOGW_IF(uid != (uid_t)-1 && uid != callingUid, 188 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid); 189 uid = callingUid; 190 } 191 audio_output_flags_t originalFlags = flags; 192 AutoCallerClear acc; 193 status_t result = mAudioPolicyManager->getOutputForAttr(attr, output, session, stream, uid, 194 config, 195 &flags, selectedDeviceId, portId); 196 197 // FIXME: Introduce a way to check for the the telephony device before opening the output 198 if ((result == NO_ERROR) && 199 (flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) && 200 !modifyPhoneStateAllowed(pid, uid)) { 201 // If the app tries to play music through the telephony device and doesn't have permission 202 // the fallback to the default output device. 203 mAudioPolicyManager->releaseOutput(*output, *stream, session); 204 flags = originalFlags; 205 *selectedDeviceId = AUDIO_PORT_HANDLE_NONE; 206 *portId = AUDIO_PORT_HANDLE_NONE; 207 result = mAudioPolicyManager->getOutputForAttr(attr, output, session, stream, uid, 208 config, 209 &flags, selectedDeviceId, portId); 210 } 211 return result; 212 } 213 214 status_t AudioPolicyService::startOutput(audio_io_handle_t output, 215 audio_stream_type_t stream, 216 audio_session_t session) 217 { 218 if (uint32_t(stream) >= AUDIO_STREAM_CNT) { 219 return BAD_VALUE; 220 } 221 if (mAudioPolicyManager == NULL) { 222 return NO_INIT; 223 } 224 ALOGV("startOutput()"); 225 sp<AudioPolicyEffects>audioPolicyEffects; 226 { 227 Mutex::Autolock _l(mLock); 228 audioPolicyEffects = mAudioPolicyEffects; 229 } 230 if (audioPolicyEffects != 0) { 231 // create audio processors according to stream 232 status_t status = audioPolicyEffects->addOutputSessionEffects(output, stream, session); 233 if (status != NO_ERROR && status != ALREADY_EXISTS) { 234 ALOGW("Failed to add effects on session %d", session); 235 } 236 } 237 Mutex::Autolock _l(mLock); 238 AutoCallerClear acc; 239 return mAudioPolicyManager->startOutput(output, stream, session); 240 } 241 242 status_t AudioPolicyService::stopOutput(audio_io_handle_t output, 243 audio_stream_type_t stream, 244 audio_session_t session) 245 { 246 if (uint32_t(stream) >= AUDIO_STREAM_CNT) { 247 return BAD_VALUE; 248 } 249 if (mAudioPolicyManager == NULL) { 250 return NO_INIT; 251 } 252 ALOGV("stopOutput()"); 253 mOutputCommandThread->stopOutputCommand(output, stream, session); 254 return NO_ERROR; 255 } 256 257 status_t AudioPolicyService::doStopOutput(audio_io_handle_t output, 258 audio_stream_type_t stream, 259 audio_session_t session) 260 { 261 ALOGV("doStopOutput from tid %d", gettid()); 262 sp<AudioPolicyEffects>audioPolicyEffects; 263 { 264 Mutex::Autolock _l(mLock); 265 audioPolicyEffects = mAudioPolicyEffects; 266 } 267 if (audioPolicyEffects != 0) { 268 // release audio processors from the stream 269 status_t status = audioPolicyEffects->releaseOutputSessionEffects(output, stream, session); 270 if (status != NO_ERROR && status != ALREADY_EXISTS) { 271 ALOGW("Failed to release effects on session %d", session); 272 } 273 } 274 Mutex::Autolock _l(mLock); 275 AutoCallerClear acc; 276 return mAudioPolicyManager->stopOutput(output, stream, session); 277 } 278 279 void AudioPolicyService::releaseOutput(audio_io_handle_t output, 280 audio_stream_type_t stream, 281 audio_session_t session) 282 { 283 if (mAudioPolicyManager == NULL) { 284 return; 285 } 286 ALOGV("releaseOutput()"); 287 mOutputCommandThread->releaseOutputCommand(output, stream, session); 288 } 289 290 void AudioPolicyService::doReleaseOutput(audio_io_handle_t output, 291 audio_stream_type_t stream, 292 audio_session_t session) 293 { 294 ALOGV("doReleaseOutput from tid %d", gettid()); 295 Mutex::Autolock _l(mLock); 296 // called from internal thread: no need to clear caller identity 297 mAudioPolicyManager->releaseOutput(output, stream, session); 298 } 299 300 status_t AudioPolicyService::getInputForAttr(const audio_attributes_t *attr, 301 audio_io_handle_t *input, 302 audio_session_t session, 303 pid_t pid, 304 uid_t uid, 305 const String16& opPackageName, 306 const audio_config_base_t *config, 307 audio_input_flags_t flags, 308 audio_port_handle_t *selectedDeviceId, 309 audio_port_handle_t *portId) 310 { 311 if (mAudioPolicyManager == NULL) { 312 return NO_INIT; 313 } 314 315 // already checked by client, but double-check in case the client wrapper is bypassed 316 if (attr->source < AUDIO_SOURCE_DEFAULT && attr->source >= AUDIO_SOURCE_CNT && 317 attr->source != AUDIO_SOURCE_HOTWORD && attr->source != AUDIO_SOURCE_FM_TUNER) { 318 return BAD_VALUE; 319 } 320 321 bool updatePid = (pid == -1); 322 const uid_t callingUid = IPCThreadState::self()->getCallingUid(); 323 if (!isTrustedCallingUid(callingUid)) { 324 ALOGW_IF(uid != (uid_t)-1 && uid != callingUid, 325 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid); 326 uid = callingUid; 327 updatePid = true; 328 } 329 330 if (updatePid) { 331 const pid_t callingPid = IPCThreadState::self()->getCallingPid(); 332 ALOGW_IF(pid != (pid_t)-1 && pid != callingPid, 333 "%s uid %d pid %d tried to pass itself off as pid %d", 334 __func__, callingUid, callingPid, pid); 335 pid = callingPid; 336 } 337 338 // check calling permissions 339 if (!recordingAllowed(opPackageName, pid, uid)) { 340 ALOGE("%s permission denied: recording not allowed for uid %d pid %d", 341 __func__, uid, pid); 342 return PERMISSION_DENIED; 343 } 344 345 if ((attr->source == AUDIO_SOURCE_VOICE_UPLINK || 346 attr->source == AUDIO_SOURCE_VOICE_DOWNLINK || 347 attr->source == AUDIO_SOURCE_VOICE_CALL) && 348 !captureAudioOutputAllowed(pid, uid)) { 349 return PERMISSION_DENIED; 350 } 351 352 if ((attr->source == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed(pid, uid)) { 353 return BAD_VALUE; 354 } 355 356 sp<AudioPolicyEffects>audioPolicyEffects; 357 { 358 status_t status; 359 AudioPolicyInterface::input_type_t inputType; 360 361 Mutex::Autolock _l(mLock); 362 { 363 AutoCallerClear acc; 364 // the audio_in_acoustics_t parameter is ignored by get_input() 365 status = mAudioPolicyManager->getInputForAttr(attr, input, session, uid, 366 config, 367 flags, selectedDeviceId, 368 &inputType, portId); 369 } 370 audioPolicyEffects = mAudioPolicyEffects; 371 372 if (status == NO_ERROR) { 373 // enforce permission (if any) required for each type of input 374 switch (inputType) { 375 case AudioPolicyInterface::API_INPUT_LEGACY: 376 break; 377 case AudioPolicyInterface::API_INPUT_TELEPHONY_RX: 378 // FIXME: use the same permission as for remote submix for now. 379 case AudioPolicyInterface::API_INPUT_MIX_CAPTURE: 380 if (!captureAudioOutputAllowed(pid, uid)) { 381 ALOGE("getInputForAttr() permission denied: capture not allowed"); 382 status = PERMISSION_DENIED; 383 } 384 break; 385 case AudioPolicyInterface::API_INPUT_MIX_EXT_POLICY_REROUTE: 386 if (!modifyAudioRoutingAllowed()) { 387 ALOGE("getInputForAttr() permission denied: modify audio routing not allowed"); 388 status = PERMISSION_DENIED; 389 } 390 break; 391 case AudioPolicyInterface::API_INPUT_INVALID: 392 default: 393 LOG_ALWAYS_FATAL("getInputForAttr() encountered an invalid input type %d", 394 (int)inputType); 395 } 396 } 397 398 if (status != NO_ERROR) { 399 if (status == PERMISSION_DENIED) { 400 AutoCallerClear acc; 401 mAudioPolicyManager->releaseInput(*input, session); 402 } 403 return status; 404 } 405 406 sp<AudioRecordClient> client = 407 new AudioRecordClient(*attr, *input, uid, pid, opPackageName, session); 408 client->active = false; 409 client->isConcurrent = false; 410 client->isVirtualDevice = false; //TODO : update from APM->getInputForAttr() 411 client->deviceId = *selectedDeviceId; 412 mAudioRecordClients.add(*portId, client); 413 } 414 415 if (audioPolicyEffects != 0) { 416 // create audio pre processors according to input source 417 status_t status = audioPolicyEffects->addInputEffects(*input, attr->source, session); 418 if (status != NO_ERROR && status != ALREADY_EXISTS) { 419 ALOGW("Failed to add effects on input %d", *input); 420 } 421 } 422 return NO_ERROR; 423 } 424 425 // this is replicated from frameworks/av/media/libaudioclient/AudioRecord.cpp 426 // XXX -- figure out how to put it into a common, shared location 427 428 static std::string audioSourceString(audio_source_t value) { 429 std::string source; 430 if (SourceTypeConverter::toString(value, source)) { 431 return source; 432 } 433 char rawbuffer[16]; // room for "%d" 434 snprintf(rawbuffer, sizeof(rawbuffer), "%d", value); 435 return rawbuffer; 436 } 437 438 static std::string audioConcurrencyString( 439 AudioPolicyInterface::concurrency_type__mask_t concurrency) 440 { 441 char buffer[64]; // oversized 442 if (concurrency & AudioPolicyInterface::API_INPUT_CONCURRENCY_ALL) { 443 snprintf(buffer, sizeof(buffer), "%s%s%s%s", 444 (concurrency & AudioPolicyInterface::API_INPUT_CONCURRENCY_CALL)? ",call":"", 445 (concurrency & AudioPolicyInterface::API_INPUT_CONCURRENCY_CAPTURE)? ",capture":"", 446 (concurrency & AudioPolicyInterface::API_INPUT_CONCURRENCY_HOTWORD)? ",hotword":"", 447 (concurrency & AudioPolicyInterface::API_INPUT_CONCURRENCY_PREEMPT)? ",preempt":""); 448 } else { 449 snprintf(buffer, sizeof(buffer), ",none"); 450 } 451 452 return &buffer[1]; 453 } 454 455 std::string AudioPolicyService::getDeviceTypeStrForPortId(audio_port_handle_t portId) { 456 std::string typeStr; 457 struct audio_port port = {}; 458 port.id = portId; 459 status_t status = mAudioPolicyManager->getAudioPort(&port); 460 if (status == NO_ERROR && port.type == AUDIO_PORT_TYPE_DEVICE) { 461 deviceToString(port.ext.device.type, typeStr); 462 } 463 return typeStr; 464 } 465 466 status_t AudioPolicyService::startInput(audio_port_handle_t portId, bool *silenced) 467 { 468 if (mAudioPolicyManager == NULL) { 469 return NO_INIT; 470 } 471 sp<AudioRecordClient> client; 472 { 473 Mutex::Autolock _l(mLock); 474 475 ssize_t index = mAudioRecordClients.indexOfKey(portId); 476 if (index < 0) { 477 return INVALID_OPERATION; 478 } 479 client = mAudioRecordClients.valueAt(index); 480 } 481 482 // check calling permissions 483 if (!startRecording(client->opPackageName, client->pid, client->uid)) { 484 ALOGE("%s permission denied: recording not allowed for uid %d pid %d", 485 __func__, client->uid, client->pid); 486 return PERMISSION_DENIED; 487 } 488 489 // If UID inactive it records silence until becoming active 490 *silenced = !mUidPolicy->isUidActive(client->uid) && !client->isVirtualDevice; 491 492 Mutex::Autolock _l(mLock); 493 AudioPolicyInterface::concurrency_type__mask_t concurrency = 494 AudioPolicyInterface::API_INPUT_CONCURRENCY_NONE; 495 496 status_t status; 497 { 498 AutoCallerClear acc; 499 status = mAudioPolicyManager->startInput( 500 client->input, client->session, *silenced, &concurrency); 501 502 } 503 504 // including successes gets very verbose 505 if (status != NO_ERROR) { 506 507 static constexpr char kAudioPolicy[] = "audiopolicy"; 508 509 static constexpr char kAudioPolicyReason[] = "android.media.audiopolicy.reason"; 510 static constexpr char kAudioPolicyStatus[] = "android.media.audiopolicy.status"; 511 static constexpr char kAudioPolicyRqstSrc[] = "android.media.audiopolicy.rqst.src"; 512 static constexpr char kAudioPolicyRqstPkg[] = "android.media.audiopolicy.rqst.pkg"; 513 static constexpr char kAudioPolicyRqstSession[] = "android.media.audiopolicy.rqst.session"; 514 static constexpr char kAudioPolicyRqstDevice[] = 515 "android.media.audiopolicy.rqst.device"; 516 static constexpr char kAudioPolicyActiveSrc[] = "android.media.audiopolicy.active.src"; 517 static constexpr char kAudioPolicyActivePkg[] = "android.media.audiopolicy.active.pkg"; 518 static constexpr char kAudioPolicyActiveSession[] = 519 "android.media.audiopolicy.active.session"; 520 static constexpr char kAudioPolicyActiveDevice[] = 521 "android.media.audiopolicy.active.device"; 522 523 MediaAnalyticsItem *item = new MediaAnalyticsItem(kAudioPolicy); 524 if (item != NULL) { 525 526 item->setCString(kAudioPolicyReason, audioConcurrencyString(concurrency).c_str()); 527 item->setInt32(kAudioPolicyStatus, status); 528 529 item->setCString(kAudioPolicyRqstSrc, 530 audioSourceString(client->attributes.source).c_str()); 531 item->setCString(kAudioPolicyRqstPkg, 532 std::string(String8(client->opPackageName).string()).c_str()); 533 item->setInt32(kAudioPolicyRqstSession, client->session); 534 535 item->setCString( 536 kAudioPolicyRqstDevice, getDeviceTypeStrForPortId(client->deviceId).c_str()); 537 538 // figure out who is active 539 // NB: might the other party have given up the microphone since then? how sure. 540 // perhaps could have given up on it. 541 // we hold mLock, so perhaps we're safe for this looping 542 if (concurrency != AudioPolicyInterface::API_INPUT_CONCURRENCY_NONE) { 543 int count = mAudioRecordClients.size(); 544 for (int i = 0; i<count ; i++) { 545 if (portId == mAudioRecordClients.keyAt(i)) { 546 continue; 547 } 548 sp<AudioRecordClient> other = mAudioRecordClients.valueAt(i); 549 if (other->active) { 550 // keeps the last of the clients marked active 551 item->setCString(kAudioPolicyActiveSrc, 552 audioSourceString(other->attributes.source).c_str()); 553 item->setCString(kAudioPolicyActivePkg, 554 std::string(String8(other->opPackageName).string()).c_str()); 555 item->setInt32(kAudioPolicyActiveSession, other->session); 556 item->setCString(kAudioPolicyActiveDevice, 557 getDeviceTypeStrForPortId(other->deviceId).c_str()); 558 } 559 } 560 } 561 item->selfrecord(); 562 delete item; 563 item = NULL; 564 } 565 } 566 567 if (status == NO_ERROR) { 568 LOG_ALWAYS_FATAL_IF(concurrency & ~AudioPolicyInterface::API_INPUT_CONCURRENCY_ALL, 569 "startInput(): invalid concurrency type %d", (int)concurrency); 570 571 // enforce permission (if any) required for each type of concurrency 572 if (concurrency & AudioPolicyInterface::API_INPUT_CONCURRENCY_CALL) { 573 //TODO: check incall capture permission 574 } 575 if (concurrency & AudioPolicyInterface::API_INPUT_CONCURRENCY_CAPTURE) { 576 //TODO: check concurrent capture permission 577 } 578 579 client->active = true; 580 } else { 581 finishRecording(client->opPackageName, client->uid); 582 } 583 584 return status; 585 } 586 587 status_t AudioPolicyService::stopInput(audio_port_handle_t portId) 588 { 589 if (mAudioPolicyManager == NULL) { 590 return NO_INIT; 591 } 592 Mutex::Autolock _l(mLock); 593 594 ssize_t index = mAudioRecordClients.indexOfKey(portId); 595 if (index < 0) { 596 return INVALID_OPERATION; 597 } 598 sp<AudioRecordClient> client = mAudioRecordClients.valueAt(index); 599 600 client->active = false; 601 602 // finish the recording app op 603 finishRecording(client->opPackageName, client->uid); 604 AutoCallerClear acc; 605 return mAudioPolicyManager->stopInput(client->input, client->session); 606 } 607 608 void AudioPolicyService::releaseInput(audio_port_handle_t portId) 609 { 610 if (mAudioPolicyManager == NULL) { 611 return; 612 } 613 sp<AudioPolicyEffects>audioPolicyEffects; 614 sp<AudioRecordClient> client; 615 { 616 Mutex::Autolock _l(mLock); 617 audioPolicyEffects = mAudioPolicyEffects; 618 ssize_t index = mAudioRecordClients.indexOfKey(portId); 619 if (index < 0) { 620 return; 621 } 622 client = mAudioRecordClients.valueAt(index); 623 mAudioRecordClients.removeItem(portId); 624 } 625 if (client == 0) { 626 return; 627 } 628 if (audioPolicyEffects != 0) { 629 // release audio processors from the input 630 status_t status = audioPolicyEffects->releaseInputEffects(client->input, client->session); 631 if(status != NO_ERROR) { 632 ALOGW("Failed to release effects on input %d", client->input); 633 } 634 } 635 { 636 Mutex::Autolock _l(mLock); 637 AutoCallerClear acc; 638 mAudioPolicyManager->releaseInput(client->input, client->session); 639 } 640 } 641 642 status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream, 643 int indexMin, 644 int indexMax) 645 { 646 if (mAudioPolicyManager == NULL) { 647 return NO_INIT; 648 } 649 if (!settingsAllowed()) { 650 return PERMISSION_DENIED; 651 } 652 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { 653 return BAD_VALUE; 654 } 655 Mutex::Autolock _l(mLock); 656 AutoCallerClear acc; 657 mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax); 658 return NO_ERROR; 659 } 660 661 status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream, 662 int index, 663 audio_devices_t device) 664 { 665 if (mAudioPolicyManager == NULL) { 666 return NO_INIT; 667 } 668 if (!settingsAllowed()) { 669 return PERMISSION_DENIED; 670 } 671 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { 672 return BAD_VALUE; 673 } 674 Mutex::Autolock _l(mLock); 675 AutoCallerClear acc; 676 return mAudioPolicyManager->setStreamVolumeIndex(stream, 677 index, 678 device); 679 } 680 681 status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream, 682 int *index, 683 audio_devices_t device) 684 { 685 if (mAudioPolicyManager == NULL) { 686 return NO_INIT; 687 } 688 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { 689 return BAD_VALUE; 690 } 691 Mutex::Autolock _l(mLock); 692 AutoCallerClear acc; 693 return mAudioPolicyManager->getStreamVolumeIndex(stream, 694 index, 695 device); 696 } 697 698 uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream) 699 { 700 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { 701 return 0; 702 } 703 if (mAudioPolicyManager == NULL) { 704 return 0; 705 } 706 AutoCallerClear acc; 707 return mAudioPolicyManager->getStrategyForStream(stream); 708 } 709 710 //audio policy: use audio_device_t appropriately 711 712 audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream) 713 { 714 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { 715 return AUDIO_DEVICE_NONE; 716 } 717 if (mAudioPolicyManager == NULL) { 718 return AUDIO_DEVICE_NONE; 719 } 720 Mutex::Autolock _l(mLock); 721 AutoCallerClear acc; 722 return mAudioPolicyManager->getDevicesForStream(stream); 723 } 724 725 audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc) 726 { 727 // FIXME change return type to status_t, and return NO_INIT here 728 if (mAudioPolicyManager == NULL) { 729 return 0; 730 } 731 Mutex::Autolock _l(mLock); 732 AutoCallerClear acc; 733 return mAudioPolicyManager->getOutputForEffect(desc); 734 } 735 736 status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc, 737 audio_io_handle_t io, 738 uint32_t strategy, 739 audio_session_t session, 740 int id) 741 { 742 if (mAudioPolicyManager == NULL) { 743 return NO_INIT; 744 } 745 Mutex::Autolock _l(mEffectsLock); 746 AutoCallerClear acc; 747 return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id); 748 } 749 750 status_t AudioPolicyService::unregisterEffect(int id) 751 { 752 if (mAudioPolicyManager == NULL) { 753 return NO_INIT; 754 } 755 Mutex::Autolock _l(mEffectsLock); 756 AutoCallerClear acc; 757 return mAudioPolicyManager->unregisterEffect(id); 758 } 759 760 status_t AudioPolicyService::setEffectEnabled(int id, bool enabled) 761 { 762 if (mAudioPolicyManager == NULL) { 763 return NO_INIT; 764 } 765 Mutex::Autolock _l(mEffectsLock); 766 AutoCallerClear acc; 767 return mAudioPolicyManager->setEffectEnabled(id, enabled); 768 } 769 770 bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const 771 { 772 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { 773 return false; 774 } 775 if (mAudioPolicyManager == NULL) { 776 return false; 777 } 778 Mutex::Autolock _l(mLock); 779 AutoCallerClear acc; 780 return mAudioPolicyManager->isStreamActive(stream, inPastMs); 781 } 782 783 bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const 784 { 785 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { 786 return false; 787 } 788 if (mAudioPolicyManager == NULL) { 789 return false; 790 } 791 Mutex::Autolock _l(mLock); 792 AutoCallerClear acc; 793 return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs); 794 } 795 796 bool AudioPolicyService::isSourceActive(audio_source_t source) const 797 { 798 if (mAudioPolicyManager == NULL) { 799 return false; 800 } 801 Mutex::Autolock _l(mLock); 802 AutoCallerClear acc; 803 return mAudioPolicyManager->isSourceActive(source); 804 } 805 806 status_t AudioPolicyService::queryDefaultPreProcessing(audio_session_t audioSession, 807 effect_descriptor_t *descriptors, 808 uint32_t *count) 809 { 810 if (mAudioPolicyManager == NULL) { 811 *count = 0; 812 return NO_INIT; 813 } 814 sp<AudioPolicyEffects>audioPolicyEffects; 815 { 816 Mutex::Autolock _l(mLock); 817 audioPolicyEffects = mAudioPolicyEffects; 818 } 819 if (audioPolicyEffects == 0) { 820 *count = 0; 821 return NO_INIT; 822 } 823 return audioPolicyEffects->queryDefaultInputEffects( 824 (audio_session_t)audioSession, descriptors, count); 825 } 826 827 bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info) 828 { 829 if (mAudioPolicyManager == NULL) { 830 ALOGV("mAudioPolicyManager == NULL"); 831 return false; 832 } 833 Mutex::Autolock _l(mLock); 834 Mutex::Autolock _le(mEffectsLock); // isOffloadSupported queries for 835 // non-offloadable effects 836 AutoCallerClear acc; 837 return mAudioPolicyManager->isOffloadSupported(info); 838 } 839 840 status_t AudioPolicyService::listAudioPorts(audio_port_role_t role, 841 audio_port_type_t type, 842 unsigned int *num_ports, 843 struct audio_port *ports, 844 unsigned int *generation) 845 { 846 Mutex::Autolock _l(mLock); 847 if (mAudioPolicyManager == NULL) { 848 return NO_INIT; 849 } 850 AutoCallerClear acc; 851 return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation); 852 } 853 854 status_t AudioPolicyService::getAudioPort(struct audio_port *port) 855 { 856 Mutex::Autolock _l(mLock); 857 if (mAudioPolicyManager == NULL) { 858 return NO_INIT; 859 } 860 AutoCallerClear acc; 861 return mAudioPolicyManager->getAudioPort(port); 862 } 863 864 status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch, 865 audio_patch_handle_t *handle) 866 { 867 Mutex::Autolock _l(mLock); 868 if(!modifyAudioRoutingAllowed()) { 869 return PERMISSION_DENIED; 870 } 871 if (mAudioPolicyManager == NULL) { 872 return NO_INIT; 873 } 874 AutoCallerClear acc; 875 return mAudioPolicyManager->createAudioPatch(patch, handle, 876 IPCThreadState::self()->getCallingUid()); 877 } 878 879 status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle) 880 { 881 Mutex::Autolock _l(mLock); 882 if(!modifyAudioRoutingAllowed()) { 883 return PERMISSION_DENIED; 884 } 885 if (mAudioPolicyManager == NULL) { 886 return NO_INIT; 887 } 888 AutoCallerClear acc; 889 return mAudioPolicyManager->releaseAudioPatch(handle, 890 IPCThreadState::self()->getCallingUid()); 891 } 892 893 status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches, 894 struct audio_patch *patches, 895 unsigned int *generation) 896 { 897 Mutex::Autolock _l(mLock); 898 if (mAudioPolicyManager == NULL) { 899 return NO_INIT; 900 } 901 AutoCallerClear acc; 902 return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation); 903 } 904 905 status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config) 906 { 907 Mutex::Autolock _l(mLock); 908 if(!modifyAudioRoutingAllowed()) { 909 return PERMISSION_DENIED; 910 } 911 if (mAudioPolicyManager == NULL) { 912 return NO_INIT; 913 } 914 AutoCallerClear acc; 915 return mAudioPolicyManager->setAudioPortConfig(config); 916 } 917 918 status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session, 919 audio_io_handle_t *ioHandle, 920 audio_devices_t *device) 921 { 922 Mutex::Autolock _l(mLock); 923 if (mAudioPolicyManager == NULL) { 924 return NO_INIT; 925 } 926 AutoCallerClear acc; 927 return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device); 928 } 929 930 status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session) 931 { 932 Mutex::Autolock _l(mLock); 933 if (mAudioPolicyManager == NULL) { 934 return NO_INIT; 935 } 936 AutoCallerClear acc; 937 return mAudioPolicyManager->releaseSoundTriggerSession(session); 938 } 939 940 status_t AudioPolicyService::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration) 941 { 942 Mutex::Autolock _l(mLock); 943 if(!modifyAudioRoutingAllowed()) { 944 return PERMISSION_DENIED; 945 } 946 if (mAudioPolicyManager == NULL) { 947 return NO_INIT; 948 } 949 AutoCallerClear acc; 950 if (registration) { 951 return mAudioPolicyManager->registerPolicyMixes(mixes); 952 } else { 953 return mAudioPolicyManager->unregisterPolicyMixes(mixes); 954 } 955 } 956 957 status_t AudioPolicyService::startAudioSource(const struct audio_port_config *source, 958 const audio_attributes_t *attributes, 959 audio_patch_handle_t *handle) 960 { 961 Mutex::Autolock _l(mLock); 962 if (mAudioPolicyManager == NULL) { 963 return NO_INIT; 964 } 965 AutoCallerClear acc; 966 return mAudioPolicyManager->startAudioSource(source, attributes, handle, 967 IPCThreadState::self()->getCallingUid()); 968 } 969 970 status_t AudioPolicyService::stopAudioSource(audio_patch_handle_t handle) 971 { 972 Mutex::Autolock _l(mLock); 973 if (mAudioPolicyManager == NULL) { 974 return NO_INIT; 975 } 976 AutoCallerClear acc; 977 return mAudioPolicyManager->stopAudioSource(handle); 978 } 979 980 status_t AudioPolicyService::setMasterMono(bool mono) 981 { 982 if (mAudioPolicyManager == NULL) { 983 return NO_INIT; 984 } 985 if (!settingsAllowed()) { 986 return PERMISSION_DENIED; 987 } 988 Mutex::Autolock _l(mLock); 989 AutoCallerClear acc; 990 return mAudioPolicyManager->setMasterMono(mono); 991 } 992 993 status_t AudioPolicyService::getMasterMono(bool *mono) 994 { 995 if (mAudioPolicyManager == NULL) { 996 return NO_INIT; 997 } 998 Mutex::Autolock _l(mLock); 999 AutoCallerClear acc; 1000 return mAudioPolicyManager->getMasterMono(mono); 1001 } 1002 1003 1004 float AudioPolicyService::getStreamVolumeDB( 1005 audio_stream_type_t stream, int index, audio_devices_t device) 1006 { 1007 if (mAudioPolicyManager == NULL) { 1008 return NAN; 1009 } 1010 Mutex::Autolock _l(mLock); 1011 AutoCallerClear acc; 1012 return mAudioPolicyManager->getStreamVolumeDB(stream, index, device); 1013 } 1014 1015 status_t AudioPolicyService::getSurroundFormats(unsigned int *numSurroundFormats, 1016 audio_format_t *surroundFormats, 1017 bool *surroundFormatsEnabled, 1018 bool reported) 1019 { 1020 if (mAudioPolicyManager == NULL) { 1021 return NO_INIT; 1022 } 1023 Mutex::Autolock _l(mLock); 1024 AutoCallerClear acc; 1025 return mAudioPolicyManager->getSurroundFormats(numSurroundFormats, surroundFormats, 1026 surroundFormatsEnabled, reported); 1027 } 1028 1029 status_t AudioPolicyService::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) 1030 { 1031 if (mAudioPolicyManager == NULL) { 1032 return NO_INIT; 1033 } 1034 Mutex::Autolock _l(mLock); 1035 AutoCallerClear acc; 1036 return mAudioPolicyManager->setSurroundFormatEnabled(audioFormat, enabled); 1037 } 1038 1039 } // namespace android 1040