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