AudioSystem.cpp revision f6778fd0c72ab54328f0e9f5ecf0017b73e99dd8
1/* 2 * Copyright (C) 2006-2007 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 "AudioSystem" 18//#define LOG_NDEBUG 0 19 20#include <utils/Log.h> 21#include <binder/IServiceManager.h> 22#include <media/AudioSystem.h> 23#include <media/IAudioFlinger.h> 24#include <media/IAudioPolicyService.h> 25#include <math.h> 26 27#include <system/audio.h> 28 29// ---------------------------------------------------------------------------- 30 31namespace android { 32 33// client singleton for AudioFlinger binder interface 34Mutex AudioSystem::gLock; 35Mutex AudioSystem::gLockCache; 36Mutex AudioSystem::gLockAPS; 37Mutex AudioSystem::gLockAPC; 38sp<IAudioFlinger> AudioSystem::gAudioFlinger; 39sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient; 40audio_error_callback AudioSystem::gAudioErrorCallback = NULL; 41 42// Cached values for output handles 43DefaultKeyedVector<audio_io_handle_t, AudioSystem::OutputDescriptor *> AudioSystem::gOutputs(NULL); 44 45// Cached values for recording queries, all protected by gLock 46uint32_t AudioSystem::gPrevInSamplingRate; 47audio_format_t AudioSystem::gPrevInFormat; 48audio_channel_mask_t AudioSystem::gPrevInChannelMask; 49size_t AudioSystem::gInBuffSize = 0; // zero indicates cache is invalid 50 51sp<AudioSystem::AudioPortCallback> AudioSystem::gAudioPortCallback; 52 53// establish binder interface to AudioFlinger service 54const sp<IAudioFlinger>& AudioSystem::get_audio_flinger() 55{ 56 Mutex::Autolock _l(gLock); 57 if (gAudioFlinger == 0) { 58 sp<IServiceManager> sm = defaultServiceManager(); 59 sp<IBinder> binder; 60 do { 61 binder = sm->getService(String16("media.audio_flinger")); 62 if (binder != 0) 63 break; 64 ALOGW("AudioFlinger not published, waiting..."); 65 usleep(500000); // 0.5 s 66 } while (true); 67 if (gAudioFlingerClient == NULL) { 68 gAudioFlingerClient = new AudioFlingerClient(); 69 } else { 70 if (gAudioErrorCallback) { 71 gAudioErrorCallback(NO_ERROR); 72 } 73 } 74 binder->linkToDeath(gAudioFlingerClient); 75 gAudioFlinger = interface_cast<IAudioFlinger>(binder); 76 LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0); 77 gAudioFlinger->registerClient(gAudioFlingerClient); 78 } 79 80 return gAudioFlinger; 81} 82 83/* static */ status_t AudioSystem::checkAudioFlinger() 84{ 85 if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) { 86 return NO_ERROR; 87 } 88 return DEAD_OBJECT; 89} 90 91status_t AudioSystem::muteMicrophone(bool state) 92{ 93 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 94 if (af == 0) return PERMISSION_DENIED; 95 return af->setMicMute(state); 96} 97 98status_t AudioSystem::isMicrophoneMuted(bool* state) 99{ 100 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 101 if (af == 0) return PERMISSION_DENIED; 102 *state = af->getMicMute(); 103 return NO_ERROR; 104} 105 106status_t AudioSystem::setMasterVolume(float value) 107{ 108 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 109 if (af == 0) return PERMISSION_DENIED; 110 af->setMasterVolume(value); 111 return NO_ERROR; 112} 113 114status_t AudioSystem::setMasterMute(bool mute) 115{ 116 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 117 if (af == 0) return PERMISSION_DENIED; 118 af->setMasterMute(mute); 119 return NO_ERROR; 120} 121 122status_t AudioSystem::getMasterVolume(float* volume) 123{ 124 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 125 if (af == 0) return PERMISSION_DENIED; 126 *volume = af->masterVolume(); 127 return NO_ERROR; 128} 129 130status_t AudioSystem::getMasterMute(bool* mute) 131{ 132 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 133 if (af == 0) return PERMISSION_DENIED; 134 *mute = af->masterMute(); 135 return NO_ERROR; 136} 137 138status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value, 139 audio_io_handle_t output) 140{ 141 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE; 142 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 143 if (af == 0) return PERMISSION_DENIED; 144 af->setStreamVolume(stream, value, output); 145 return NO_ERROR; 146} 147 148status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute) 149{ 150 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE; 151 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 152 if (af == 0) return PERMISSION_DENIED; 153 af->setStreamMute(stream, mute); 154 return NO_ERROR; 155} 156 157status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume, 158 audio_io_handle_t output) 159{ 160 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE; 161 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 162 if (af == 0) return PERMISSION_DENIED; 163 *volume = af->streamVolume(stream, output); 164 return NO_ERROR; 165} 166 167status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute) 168{ 169 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE; 170 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 171 if (af == 0) return PERMISSION_DENIED; 172 *mute = af->streamMute(stream); 173 return NO_ERROR; 174} 175 176status_t AudioSystem::setMode(audio_mode_t mode) 177{ 178 if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE; 179 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 180 if (af == 0) return PERMISSION_DENIED; 181 return af->setMode(mode); 182} 183 184status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) 185{ 186 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 187 if (af == 0) return PERMISSION_DENIED; 188 return af->setParameters(ioHandle, keyValuePairs); 189} 190 191String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) 192{ 193 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 194 String8 result = String8(""); 195 if (af == 0) return result; 196 197 result = af->getParameters(ioHandle, keys); 198 return result; 199} 200 201status_t AudioSystem::setParameters(const String8& keyValuePairs) 202{ 203 return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs); 204} 205 206String8 AudioSystem::getParameters(const String8& keys) 207{ 208 return getParameters(AUDIO_IO_HANDLE_NONE, keys); 209} 210 211// convert volume steps to natural log scale 212 213// change this value to change volume scaling 214static const float dBPerStep = 0.5f; 215// shouldn't need to touch these 216static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f; 217static const float dBConvertInverse = 1.0f / dBConvert; 218 219float AudioSystem::linearToLog(int volume) 220{ 221 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0; 222 // ALOGD("linearToLog(%d)=%f", volume, v); 223 // return v; 224 return volume ? exp(float(100 - volume) * dBConvert) : 0; 225} 226 227int AudioSystem::logToLinear(float volume) 228{ 229 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; 230 // ALOGD("logTolinear(%d)=%f", v, volume); 231 // return v; 232 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; 233} 234 235status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType) 236{ 237 audio_io_handle_t output; 238 239 if (streamType == AUDIO_STREAM_DEFAULT) { 240 streamType = AUDIO_STREAM_MUSIC; 241 } 242 243 output = getOutput(streamType); 244 if (output == 0) { 245 return PERMISSION_DENIED; 246 } 247 248 return getSamplingRate(output, samplingRate); 249} 250 251status_t AudioSystem::getSamplingRate(audio_io_handle_t output, 252 uint32_t* samplingRate) 253{ 254 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 255 if (af == 0) return PERMISSION_DENIED; 256 257 Mutex::Autolock _l(gLockCache); 258 259 OutputDescriptor *outputDesc = AudioSystem::gOutputs.valueFor(output); 260 if (outputDesc == NULL) { 261 ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output); 262 gLockCache.unlock(); 263 *samplingRate = af->sampleRate(output); 264 gLockCache.lock(); 265 } else { 266 ALOGV("getOutputSamplingRate() reading from output desc"); 267 *samplingRate = outputDesc->samplingRate; 268 } 269 if (*samplingRate == 0) { 270 ALOGE("AudioSystem::getSamplingRate failed for output %d", output); 271 return BAD_VALUE; 272 } 273 274 ALOGV("getSamplingRate() output %d, sampling rate %u", output, *samplingRate); 275 276 return NO_ERROR; 277} 278 279status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType) 280{ 281 audio_io_handle_t output; 282 283 if (streamType == AUDIO_STREAM_DEFAULT) { 284 streamType = AUDIO_STREAM_MUSIC; 285 } 286 287 output = getOutput(streamType); 288 if (output == AUDIO_IO_HANDLE_NONE) { 289 return PERMISSION_DENIED; 290 } 291 292 return getFrameCount(output, frameCount); 293} 294 295status_t AudioSystem::getFrameCount(audio_io_handle_t output, 296 size_t* frameCount) 297{ 298 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 299 if (af == 0) return PERMISSION_DENIED; 300 301 Mutex::Autolock _l(gLockCache); 302 303 OutputDescriptor *outputDesc = AudioSystem::gOutputs.valueFor(output); 304 if (outputDesc == NULL) { 305 gLockCache.unlock(); 306 *frameCount = af->frameCount(output); 307 gLockCache.lock(); 308 } else { 309 *frameCount = outputDesc->frameCount; 310 } 311 if (*frameCount == 0) { 312 ALOGE("AudioSystem::getFrameCount failed for output %d", output); 313 return BAD_VALUE; 314 } 315 316 ALOGV("getFrameCount() output %d, frameCount %zu", output, *frameCount); 317 318 return NO_ERROR; 319} 320 321status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType) 322{ 323 audio_io_handle_t output; 324 325 if (streamType == AUDIO_STREAM_DEFAULT) { 326 streamType = AUDIO_STREAM_MUSIC; 327 } 328 329 output = getOutput(streamType); 330 if (output == AUDIO_IO_HANDLE_NONE) { 331 return PERMISSION_DENIED; 332 } 333 334 return getLatency(output, latency); 335} 336 337status_t AudioSystem::getLatency(audio_io_handle_t output, 338 uint32_t* latency) 339{ 340 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 341 if (af == 0) return PERMISSION_DENIED; 342 343 Mutex::Autolock _l(gLockCache); 344 345 OutputDescriptor *outputDesc = AudioSystem::gOutputs.valueFor(output); 346 if (outputDesc == NULL) { 347 gLockCache.unlock(); 348 *latency = af->latency(output); 349 gLockCache.lock(); 350 } else { 351 *latency = outputDesc->latency; 352 } 353 354 ALOGV("getLatency() output %d, latency %d", output, *latency); 355 356 return NO_ERROR; 357} 358 359status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format, 360 audio_channel_mask_t channelMask, size_t* buffSize) 361{ 362 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 363 if (af == 0) { 364 return PERMISSION_DENIED; 365 } 366 Mutex::Autolock _l(gLockCache); 367 // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values 368 size_t inBuffSize = gInBuffSize; 369 if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat) 370 || (channelMask != gPrevInChannelMask)) { 371 gLockCache.unlock(); 372 inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask); 373 gLockCache.lock(); 374 if (inBuffSize == 0) { 375 ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x", 376 sampleRate, format, channelMask); 377 return BAD_VALUE; 378 } 379 // A benign race is possible here: we could overwrite a fresher cache entry 380 // save the request params 381 gPrevInSamplingRate = sampleRate; 382 gPrevInFormat = format; 383 gPrevInChannelMask = channelMask; 384 385 gInBuffSize = inBuffSize; 386 } 387 *buffSize = inBuffSize; 388 389 return NO_ERROR; 390} 391 392status_t AudioSystem::setVoiceVolume(float value) 393{ 394 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 395 if (af == 0) return PERMISSION_DENIED; 396 return af->setVoiceVolume(value); 397} 398 399status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames, 400 uint32_t *dspFrames) 401{ 402 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 403 if (af == 0) return PERMISSION_DENIED; 404 405 return af->getRenderPosition(halFrames, dspFrames, output); 406} 407 408uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) 409{ 410 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 411 uint32_t result = 0; 412 if (af == 0) return result; 413 if (ioHandle == AUDIO_IO_HANDLE_NONE) return result; 414 415 result = af->getInputFramesLost(ioHandle); 416 return result; 417} 418 419audio_unique_id_t AudioSystem::newAudioUniqueId() 420{ 421 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 422 if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE; 423 return af->newAudioUniqueId(); 424} 425 426void AudioSystem::acquireAudioSessionId(int audioSession, pid_t pid) 427{ 428 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 429 if (af != 0) { 430 af->acquireAudioSessionId(audioSession, pid); 431 } 432} 433 434void AudioSystem::releaseAudioSessionId(int audioSession, pid_t pid) 435{ 436 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 437 if (af != 0) { 438 af->releaseAudioSessionId(audioSession, pid); 439 } 440} 441 442audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId) 443{ 444 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 445 if (af == 0) return AUDIO_HW_SYNC_INVALID; 446 return af->getAudioHwSyncForSession(sessionId); 447} 448 449// --------------------------------------------------------------------------- 450 451void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused) 452{ 453 audio_error_callback cb = NULL; 454 { 455 Mutex::Autolock _l(AudioSystem::gLock); 456 AudioSystem::gAudioFlinger.clear(); 457 cb = gAudioErrorCallback; 458 } 459 460 { 461 // clear output handles and stream to output map caches 462 Mutex::Autolock _l(gLockCache); 463 AudioSystem::gOutputs.clear(); 464 } 465 466 if (cb) { 467 cb(DEAD_OBJECT); 468 } 469 ALOGW("AudioFlinger server died!"); 470} 471 472void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, audio_io_handle_t ioHandle, 473 const void *param2) { 474 ALOGV("ioConfigChanged() event %d", event); 475 const OutputDescriptor *desc; 476 audio_stream_type_t stream; 477 478 if (ioHandle == AUDIO_IO_HANDLE_NONE) return; 479 480 Mutex::Autolock _l(AudioSystem::gLockCache); 481 482 switch (event) { 483 case STREAM_CONFIG_CHANGED: 484 break; 485 case OUTPUT_OPENED: { 486 if (gOutputs.indexOfKey(ioHandle) >= 0) { 487 ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle); 488 break; 489 } 490 if (param2 == NULL) break; 491 desc = (const OutputDescriptor *)param2; 492 493 OutputDescriptor *outputDesc = new OutputDescriptor(*desc); 494 gOutputs.add(ioHandle, outputDesc); 495 ALOGV("ioConfigChanged() new output samplingRate %u, format %#x channel mask %#x frameCount %zu " 496 "latency %d", 497 outputDesc->samplingRate, outputDesc->format, outputDesc->channelMask, 498 outputDesc->frameCount, outputDesc->latency); 499 } break; 500 case OUTPUT_CLOSED: { 501 if (gOutputs.indexOfKey(ioHandle) < 0) { 502 ALOGW("ioConfigChanged() closing unknown output! %d", ioHandle); 503 break; 504 } 505 ALOGV("ioConfigChanged() output %d closed", ioHandle); 506 507 gOutputs.removeItem(ioHandle); 508 } break; 509 510 case OUTPUT_CONFIG_CHANGED: { 511 int index = gOutputs.indexOfKey(ioHandle); 512 if (index < 0) { 513 ALOGW("ioConfigChanged() modifying unknown output! %d", ioHandle); 514 break; 515 } 516 if (param2 == NULL) break; 517 desc = (const OutputDescriptor *)param2; 518 519 ALOGV("ioConfigChanged() new config for output %d samplingRate %u, format %#x channel mask %#x " 520 "frameCount %zu latency %d", 521 ioHandle, desc->samplingRate, desc->format, 522 desc->channelMask, desc->frameCount, desc->latency); 523 OutputDescriptor *outputDesc = gOutputs.valueAt(index); 524 delete outputDesc; 525 outputDesc = new OutputDescriptor(*desc); 526 gOutputs.replaceValueFor(ioHandle, outputDesc); 527 } break; 528 case INPUT_OPENED: 529 case INPUT_CLOSED: 530 case INPUT_CONFIG_CHANGED: 531 break; 532 533 } 534} 535 536void AudioSystem::setErrorCallback(audio_error_callback cb) 537{ 538 Mutex::Autolock _l(gLock); 539 gAudioErrorCallback = cb; 540} 541 542// client singleton for AudioPolicyService binder interface 543// protected by gLockAPS 544sp<IAudioPolicyService> AudioSystem::gAudioPolicyService; 545sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient; 546 547 548// establish binder interface to AudioPolicy service 549const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service() 550{ 551 Mutex::Autolock _l(gLockAPS); 552 if (gAudioPolicyService == 0) { 553 sp<IServiceManager> sm = defaultServiceManager(); 554 sp<IBinder> binder; 555 do { 556 binder = sm->getService(String16("media.audio_policy")); 557 if (binder != 0) 558 break; 559 ALOGW("AudioPolicyService not published, waiting..."); 560 usleep(500000); // 0.5 s 561 } while (true); 562 if (gAudioPolicyServiceClient == NULL) { 563 gAudioPolicyServiceClient = new AudioPolicyServiceClient(); 564 } 565 binder->linkToDeath(gAudioPolicyServiceClient); 566 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder); 567 LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0); 568 gAudioPolicyService->registerClient(gAudioPolicyServiceClient); 569 } 570 571 return gAudioPolicyService; 572} 573 574// --------------------------------------------------------------------------- 575 576status_t AudioSystem::setDeviceConnectionState(audio_devices_t device, 577 audio_policy_dev_state_t state, 578 const char *device_address) 579{ 580 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 581 const char *address = ""; 582 583 if (aps == 0) return PERMISSION_DENIED; 584 585 if (device_address != NULL) { 586 address = device_address; 587 } 588 589 return aps->setDeviceConnectionState(device, state, address); 590} 591 592audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device, 593 const char *device_address) 594{ 595 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 596 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; 597 598 return aps->getDeviceConnectionState(device, device_address); 599} 600 601status_t AudioSystem::setPhoneState(audio_mode_t state) 602{ 603 if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE; 604 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 605 if (aps == 0) return PERMISSION_DENIED; 606 607 return aps->setPhoneState(state); 608} 609 610status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) 611{ 612 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 613 if (aps == 0) return PERMISSION_DENIED; 614 return aps->setForceUse(usage, config); 615} 616 617audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage) 618{ 619 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 620 if (aps == 0) return AUDIO_POLICY_FORCE_NONE; 621 return aps->getForceUse(usage); 622} 623 624 625audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream, 626 uint32_t samplingRate, 627 audio_format_t format, 628 audio_channel_mask_t channelMask, 629 audio_output_flags_t flags, 630 const audio_offload_info_t *offloadInfo) 631{ 632 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 633 if (aps == 0) return 0; 634 return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo); 635} 636 637audio_io_handle_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr, 638 uint32_t samplingRate, 639 audio_format_t format, 640 audio_channel_mask_t channelMask, 641 audio_output_flags_t flags, 642 const audio_offload_info_t *offloadInfo) 643{ 644 if (attr == NULL) return 0; 645 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 646 if (aps == 0) return 0; 647 return aps->getOutputForAttr(attr, samplingRate, format, channelMask, flags, offloadInfo); 648} 649 650status_t AudioSystem::startOutput(audio_io_handle_t output, 651 audio_stream_type_t stream, 652 int session) 653{ 654 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 655 if (aps == 0) return PERMISSION_DENIED; 656 return aps->startOutput(output, stream, session); 657} 658 659status_t AudioSystem::stopOutput(audio_io_handle_t output, 660 audio_stream_type_t stream, 661 int session) 662{ 663 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 664 if (aps == 0) return PERMISSION_DENIED; 665 return aps->stopOutput(output, stream, session); 666} 667 668void AudioSystem::releaseOutput(audio_io_handle_t output) 669{ 670 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 671 if (aps == 0) return; 672 aps->releaseOutput(output); 673} 674 675audio_io_handle_t AudioSystem::getInput(audio_source_t inputSource, 676 uint32_t samplingRate, 677 audio_format_t format, 678 audio_channel_mask_t channelMask, 679 int sessionId, 680 audio_input_flags_t flags) 681{ 682 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 683 if (aps == 0) return 0; 684 return aps->getInput(inputSource, samplingRate, format, channelMask, sessionId, flags); 685} 686 687status_t AudioSystem::startInput(audio_io_handle_t input, 688 audio_session_t session) 689{ 690 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 691 if (aps == 0) return PERMISSION_DENIED; 692 return aps->startInput(input, session); 693} 694 695status_t AudioSystem::stopInput(audio_io_handle_t input, 696 audio_session_t session) 697{ 698 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 699 if (aps == 0) return PERMISSION_DENIED; 700 return aps->stopInput(input, session); 701} 702 703void AudioSystem::releaseInput(audio_io_handle_t input, 704 audio_session_t session) 705{ 706 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 707 if (aps == 0) return; 708 aps->releaseInput(input, session); 709} 710 711status_t AudioSystem::initStreamVolume(audio_stream_type_t stream, 712 int indexMin, 713 int indexMax) 714{ 715 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 716 if (aps == 0) return PERMISSION_DENIED; 717 return aps->initStreamVolume(stream, indexMin, indexMax); 718} 719 720status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream, 721 int index, 722 audio_devices_t device) 723{ 724 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 725 if (aps == 0) return PERMISSION_DENIED; 726 return aps->setStreamVolumeIndex(stream, index, device); 727} 728 729status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream, 730 int *index, 731 audio_devices_t device) 732{ 733 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 734 if (aps == 0) return PERMISSION_DENIED; 735 return aps->getStreamVolumeIndex(stream, index, device); 736} 737 738uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) 739{ 740 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 741 if (aps == 0) return 0; 742 return aps->getStrategyForStream(stream); 743} 744 745audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream) 746{ 747 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 748 if (aps == 0) return AUDIO_DEVICE_NONE; 749 return aps->getDevicesForStream(stream); 750} 751 752audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc) 753{ 754 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 755 // FIXME change return type to status_t, and return PERMISSION_DENIED here 756 if (aps == 0) return AUDIO_IO_HANDLE_NONE; 757 return aps->getOutputForEffect(desc); 758} 759 760status_t AudioSystem::registerEffect(const effect_descriptor_t *desc, 761 audio_io_handle_t io, 762 uint32_t strategy, 763 int session, 764 int id) 765{ 766 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 767 if (aps == 0) return PERMISSION_DENIED; 768 return aps->registerEffect(desc, io, strategy, session, id); 769} 770 771status_t AudioSystem::unregisterEffect(int id) 772{ 773 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 774 if (aps == 0) return PERMISSION_DENIED; 775 return aps->unregisterEffect(id); 776} 777 778status_t AudioSystem::setEffectEnabled(int id, bool enabled) 779{ 780 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 781 if (aps == 0) return PERMISSION_DENIED; 782 return aps->setEffectEnabled(id, enabled); 783} 784 785status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs) 786{ 787 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 788 if (aps == 0) return PERMISSION_DENIED; 789 if (state == NULL) return BAD_VALUE; 790 *state = aps->isStreamActive(stream, inPastMs); 791 return NO_ERROR; 792} 793 794status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state, 795 uint32_t inPastMs) 796{ 797 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 798 if (aps == 0) return PERMISSION_DENIED; 799 if (state == NULL) return BAD_VALUE; 800 *state = aps->isStreamActiveRemotely(stream, inPastMs); 801 return NO_ERROR; 802} 803 804status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) 805{ 806 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 807 if (aps == 0) return PERMISSION_DENIED; 808 if (state == NULL) return BAD_VALUE; 809 *state = aps->isSourceActive(stream); 810 return NO_ERROR; 811} 812 813uint32_t AudioSystem::getPrimaryOutputSamplingRate() 814{ 815 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 816 if (af == 0) return 0; 817 return af->getPrimaryOutputSamplingRate(); 818} 819 820size_t AudioSystem::getPrimaryOutputFrameCount() 821{ 822 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 823 if (af == 0) return 0; 824 return af->getPrimaryOutputFrameCount(); 825} 826 827status_t AudioSystem::setLowRamDevice(bool isLowRamDevice) 828{ 829 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 830 if (af == 0) return PERMISSION_DENIED; 831 return af->setLowRamDevice(isLowRamDevice); 832} 833 834void AudioSystem::clearAudioConfigCache() 835{ 836 // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances 837 ALOGV("clearAudioConfigCache()"); 838 { 839 Mutex::Autolock _l(gLockCache); 840 gOutputs.clear(); 841 } 842 { 843 Mutex::Autolock _l(gLock); 844 gAudioFlinger.clear(); 845 } 846 { 847 Mutex::Autolock _l(gLockAPS); 848 gAudioPolicyService.clear(); 849 } 850 // Do not clear gAudioPortCallback 851} 852 853bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info) 854{ 855 ALOGV("isOffloadSupported()"); 856 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 857 if (aps == 0) return false; 858 return aps->isOffloadSupported(info); 859} 860 861status_t AudioSystem::listAudioPorts(audio_port_role_t role, 862 audio_port_type_t type, 863 unsigned int *num_ports, 864 struct audio_port *ports, 865 unsigned int *generation) 866{ 867 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 868 if (aps == 0) return PERMISSION_DENIED; 869 return aps->listAudioPorts(role, type, num_ports, ports, generation); 870} 871 872status_t AudioSystem::getAudioPort(struct audio_port *port) 873{ 874 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 875 if (aps == 0) return PERMISSION_DENIED; 876 return aps->getAudioPort(port); 877} 878 879status_t AudioSystem::createAudioPatch(const struct audio_patch *patch, 880 audio_patch_handle_t *handle) 881{ 882 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 883 if (aps == 0) return PERMISSION_DENIED; 884 return aps->createAudioPatch(patch, handle); 885} 886 887status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle) 888{ 889 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 890 if (aps == 0) return PERMISSION_DENIED; 891 return aps->releaseAudioPatch(handle); 892} 893 894status_t AudioSystem::listAudioPatches(unsigned int *num_patches, 895 struct audio_patch *patches, 896 unsigned int *generation) 897{ 898 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 899 if (aps == 0) return PERMISSION_DENIED; 900 return aps->listAudioPatches(num_patches, patches, generation); 901} 902 903status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config) 904{ 905 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 906 if (aps == 0) return PERMISSION_DENIED; 907 return aps->setAudioPortConfig(config); 908} 909 910void AudioSystem::setAudioPortCallback(sp<AudioPortCallback> callBack) 911{ 912 Mutex::Autolock _l(gLockAPC); 913 gAudioPortCallback = callBack; 914} 915 916status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session, 917 audio_io_handle_t *ioHandle, 918 audio_devices_t *device) 919{ 920 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 921 if (aps == 0) return PERMISSION_DENIED; 922 return aps->acquireSoundTriggerSession(session, ioHandle, device); 923} 924 925status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session) 926{ 927 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 928 if (aps == 0) return PERMISSION_DENIED; 929 return aps->releaseSoundTriggerSession(session); 930} 931 932audio_mode_t AudioSystem::getPhoneState() 933{ 934 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 935 if (aps == 0) return AUDIO_MODE_INVALID; 936 return aps->getPhoneState(); 937} 938 939 940// --------------------------------------------------------------------------- 941 942void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused) 943{ 944 { 945 Mutex::Autolock _l(gLockAPC); 946 if (gAudioPortCallback != 0) { 947 gAudioPortCallback->onServiceDied(); 948 } 949 } 950 { 951 Mutex::Autolock _l(gLockAPS); 952 AudioSystem::gAudioPolicyService.clear(); 953 } 954 955 ALOGW("AudioPolicyService server died!"); 956} 957 958void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate() 959{ 960 Mutex::Autolock _l(gLockAPC); 961 if (gAudioPortCallback != 0) { 962 gAudioPortCallback->onAudioPortListUpdate(); 963 } 964} 965 966void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate() 967{ 968 Mutex::Autolock _l(gLockAPC); 969 if (gAudioPortCallback != 0) { 970 gAudioPortCallback->onAudioPatchListUpdate(); 971 } 972} 973 974}; // namespace android 975