AudioSystem.cpp revision 7c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745
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/IAudioPolicyService.h> 24#include <math.h> 25 26#include <system/audio.h> 27 28// ---------------------------------------------------------------------------- 29 30namespace android { 31 32// client singleton for AudioFlinger binder interface 33Mutex AudioSystem::gLock; 34sp<IAudioFlinger> AudioSystem::gAudioFlinger; 35sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient; 36audio_error_callback AudioSystem::gAudioErrorCallback = NULL; 37// Cached values 38DefaultKeyedVector<int, audio_io_handle_t> AudioSystem::gStreamOutputMap(0); 39DefaultKeyedVector<audio_io_handle_t, AudioSystem::OutputDescriptor *> AudioSystem::gOutputs(0); 40 41// Cached values for recording queries 42uint32_t AudioSystem::gPrevInSamplingRate = 16000; 43int AudioSystem::gPrevInFormat = AUDIO_FORMAT_PCM_16_BIT; 44int AudioSystem::gPrevInChannelCount = 1; 45size_t AudioSystem::gInBuffSize = 0; 46 47 48// establish binder interface to AudioFlinger service 49const sp<IAudioFlinger>& AudioSystem::get_audio_flinger() 50{ 51 Mutex::Autolock _l(gLock); 52 if (gAudioFlinger.get() == 0) { 53 sp<IServiceManager> sm = defaultServiceManager(); 54 sp<IBinder> binder; 55 do { 56 binder = sm->getService(String16("media.audio_flinger")); 57 if (binder != 0) 58 break; 59 LOGW("AudioFlinger not published, waiting..."); 60 usleep(500000); // 0.5 s 61 } while(true); 62 if (gAudioFlingerClient == NULL) { 63 gAudioFlingerClient = new AudioFlingerClient(); 64 } else { 65 if (gAudioErrorCallback) { 66 gAudioErrorCallback(NO_ERROR); 67 } 68 } 69 binder->linkToDeath(gAudioFlingerClient); 70 gAudioFlinger = interface_cast<IAudioFlinger>(binder); 71 gAudioFlinger->registerClient(gAudioFlingerClient); 72 } 73 LOGE_IF(gAudioFlinger==0, "no AudioFlinger!?"); 74 75 return gAudioFlinger; 76} 77 78status_t AudioSystem::muteMicrophone(bool state) { 79 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 80 if (af == 0) return PERMISSION_DENIED; 81 return af->setMicMute(state); 82} 83 84status_t AudioSystem::isMicrophoneMuted(bool* state) { 85 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 86 if (af == 0) return PERMISSION_DENIED; 87 *state = af->getMicMute(); 88 return NO_ERROR; 89} 90 91status_t AudioSystem::setMasterVolume(float value) 92{ 93 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 94 if (af == 0) return PERMISSION_DENIED; 95 af->setMasterVolume(value); 96 return NO_ERROR; 97} 98 99status_t AudioSystem::setMasterMute(bool mute) 100{ 101 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 102 if (af == 0) return PERMISSION_DENIED; 103 af->setMasterMute(mute); 104 return NO_ERROR; 105} 106 107status_t AudioSystem::getMasterVolume(float* volume) 108{ 109 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 110 if (af == 0) return PERMISSION_DENIED; 111 *volume = af->masterVolume(); 112 return NO_ERROR; 113} 114 115status_t AudioSystem::getMasterMute(bool* mute) 116{ 117 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 118 if (af == 0) return PERMISSION_DENIED; 119 *mute = af->masterMute(); 120 return NO_ERROR; 121} 122 123status_t AudioSystem::setStreamVolume(int stream, float value, int output) 124{ 125 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE; 126 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 127 if (af == 0) return PERMISSION_DENIED; 128 af->setStreamVolume(stream, value, output); 129 return NO_ERROR; 130} 131 132status_t AudioSystem::setStreamMute(int stream, bool mute) 133{ 134 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE; 135 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 136 if (af == 0) return PERMISSION_DENIED; 137 af->setStreamMute(stream, mute); 138 return NO_ERROR; 139} 140 141status_t AudioSystem::getStreamVolume(int stream, float* volume, int output) 142{ 143 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE; 144 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 145 if (af == 0) return PERMISSION_DENIED; 146 *volume = af->streamVolume(stream, output); 147 return NO_ERROR; 148} 149 150status_t AudioSystem::getStreamMute(int stream, bool* mute) 151{ 152 if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE; 153 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 154 if (af == 0) return PERMISSION_DENIED; 155 *mute = af->streamMute(stream); 156 return NO_ERROR; 157} 158 159status_t AudioSystem::setMode(int mode) 160{ 161 if (mode >= AUDIO_MODE_CNT) return BAD_VALUE; 162 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 163 if (af == 0) return PERMISSION_DENIED; 164 return af->setMode(mode); 165} 166 167status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) { 168 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 169 if (af == 0) return PERMISSION_DENIED; 170 return af->setParameters(ioHandle, keyValuePairs); 171} 172 173String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) { 174 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 175 String8 result = String8(""); 176 if (af == 0) return result; 177 178 result = af->getParameters(ioHandle, keys); 179 return result; 180} 181 182// convert volume steps to natural log scale 183 184// change this value to change volume scaling 185static const float dBPerStep = 0.5f; 186// shouldn't need to touch these 187static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f; 188static const float dBConvertInverse = 1.0f / dBConvert; 189 190float AudioSystem::linearToLog(int volume) 191{ 192 // float v = volume ? exp(float(100 - volume) * dBConvert) : 0; 193 // LOGD("linearToLog(%d)=%f", volume, v); 194 // return v; 195 return volume ? exp(float(100 - volume) * dBConvert) : 0; 196} 197 198int AudioSystem::logToLinear(float volume) 199{ 200 // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; 201 // LOGD("logTolinear(%d)=%f", v, volume); 202 // return v; 203 return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; 204} 205 206status_t AudioSystem::getOutputSamplingRate(int* samplingRate, int streamType) 207{ 208 OutputDescriptor *outputDesc; 209 audio_io_handle_t output; 210 211 if (streamType == AUDIO_STREAM_DEFAULT) { 212 streamType = AUDIO_STREAM_MUSIC; 213 } 214 215 output = getOutput((audio_stream_type_t)streamType); 216 if (output == 0) { 217 return PERMISSION_DENIED; 218 } 219 220 gLock.lock(); 221 outputDesc = AudioSystem::gOutputs.valueFor(output); 222 if (outputDesc == 0) { 223 LOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output); 224 gLock.unlock(); 225 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 226 if (af == 0) return PERMISSION_DENIED; 227 *samplingRate = af->sampleRate(output); 228 } else { 229 LOGV("getOutputSamplingRate() reading from output desc"); 230 *samplingRate = outputDesc->samplingRate; 231 gLock.unlock(); 232 } 233 234 LOGV("getOutputSamplingRate() streamType %d, output %d, sampling rate %d", streamType, output, *samplingRate); 235 236 return NO_ERROR; 237} 238 239status_t AudioSystem::getOutputFrameCount(int* frameCount, int streamType) 240{ 241 OutputDescriptor *outputDesc; 242 audio_io_handle_t output; 243 244 if (streamType == AUDIO_STREAM_DEFAULT) { 245 streamType = AUDIO_STREAM_MUSIC; 246 } 247 248 output = getOutput((audio_stream_type_t)streamType); 249 if (output == 0) { 250 return PERMISSION_DENIED; 251 } 252 253 gLock.lock(); 254 outputDesc = AudioSystem::gOutputs.valueFor(output); 255 if (outputDesc == 0) { 256 gLock.unlock(); 257 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 258 if (af == 0) return PERMISSION_DENIED; 259 *frameCount = af->frameCount(output); 260 } else { 261 *frameCount = outputDesc->frameCount; 262 gLock.unlock(); 263 } 264 265 LOGV("getOutputFrameCount() streamType %d, output %d, frameCount %d", streamType, output, *frameCount); 266 267 return NO_ERROR; 268} 269 270status_t AudioSystem::getOutputLatency(uint32_t* latency, int streamType) 271{ 272 OutputDescriptor *outputDesc; 273 audio_io_handle_t output; 274 275 if (streamType == AUDIO_STREAM_DEFAULT) { 276 streamType = AUDIO_STREAM_MUSIC; 277 } 278 279 output = getOutput((audio_stream_type_t)streamType); 280 if (output == 0) { 281 return PERMISSION_DENIED; 282 } 283 284 gLock.lock(); 285 outputDesc = AudioSystem::gOutputs.valueFor(output); 286 if (outputDesc == 0) { 287 gLock.unlock(); 288 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 289 if (af == 0) return PERMISSION_DENIED; 290 *latency = af->latency(output); 291 } else { 292 *latency = outputDesc->latency; 293 gLock.unlock(); 294 } 295 296 LOGV("getOutputLatency() streamType %d, output %d, latency %d", streamType, output, *latency); 297 298 return NO_ERROR; 299} 300 301status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, int format, int channelCount, 302 size_t* buffSize) 303{ 304 // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values 305 if ((gInBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat) 306 || (channelCount != gPrevInChannelCount)) { 307 // save the request params 308 gPrevInSamplingRate = sampleRate; 309 gPrevInFormat = format; 310 gPrevInChannelCount = channelCount; 311 312 gInBuffSize = 0; 313 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 314 if (af == 0) { 315 return PERMISSION_DENIED; 316 } 317 gInBuffSize = af->getInputBufferSize(sampleRate, format, channelCount); 318 } 319 *buffSize = gInBuffSize; 320 321 return NO_ERROR; 322} 323 324status_t AudioSystem::setVoiceVolume(float value) 325{ 326 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 327 if (af == 0) return PERMISSION_DENIED; 328 return af->setVoiceVolume(value); 329} 330 331status_t AudioSystem::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int stream) 332{ 333 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 334 if (af == 0) return PERMISSION_DENIED; 335 336 if (stream == AUDIO_STREAM_DEFAULT) { 337 stream = AUDIO_STREAM_MUSIC; 338 } 339 340 return af->getRenderPosition(halFrames, dspFrames, getOutput((audio_stream_type_t)stream)); 341} 342 343unsigned int AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) { 344 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 345 unsigned int result = 0; 346 if (af == 0) return result; 347 if (ioHandle == 0) return result; 348 349 result = af->getInputFramesLost(ioHandle); 350 return result; 351} 352 353int AudioSystem::newAudioSessionId() { 354 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); 355 if (af == 0) return 0; 356 return af->newAudioSessionId(); 357} 358 359// --------------------------------------------------------------------------- 360 361void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who) { 362 Mutex::Autolock _l(AudioSystem::gLock); 363 364 AudioSystem::gAudioFlinger.clear(); 365 // clear output handles and stream to output map caches 366 AudioSystem::gStreamOutputMap.clear(); 367 AudioSystem::gOutputs.clear(); 368 369 if (gAudioErrorCallback) { 370 gAudioErrorCallback(DEAD_OBJECT); 371 } 372 LOGW("AudioFlinger server died!"); 373} 374 375void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, int ioHandle, void *param2) { 376 LOGV("ioConfigChanged() event %d", event); 377 OutputDescriptor *desc; 378 uint32_t stream; 379 380 if (ioHandle == 0) return; 381 382 Mutex::Autolock _l(AudioSystem::gLock); 383 384 switch (event) { 385 case STREAM_CONFIG_CHANGED: 386 if (param2 == 0) break; 387 stream = *(uint32_t *)param2; 388 LOGV("ioConfigChanged() STREAM_CONFIG_CHANGED stream %d, output %d", stream, ioHandle); 389 if (gStreamOutputMap.indexOfKey(stream) >= 0) { 390 gStreamOutputMap.replaceValueFor(stream, ioHandle); 391 } 392 break; 393 case OUTPUT_OPENED: { 394 if (gOutputs.indexOfKey(ioHandle) >= 0) { 395 LOGV("ioConfigChanged() opening already existing output! %d", ioHandle); 396 break; 397 } 398 if (param2 == 0) break; 399 desc = (OutputDescriptor *)param2; 400 401 OutputDescriptor *outputDesc = new OutputDescriptor(*desc); 402 gOutputs.add(ioHandle, outputDesc); 403 LOGV("ioConfigChanged() new output samplingRate %d, format %d channels %d frameCount %d latency %d", 404 outputDesc->samplingRate, outputDesc->format, outputDesc->channels, outputDesc->frameCount, outputDesc->latency); 405 } break; 406 case OUTPUT_CLOSED: { 407 if (gOutputs.indexOfKey(ioHandle) < 0) { 408 LOGW("ioConfigChanged() closing unknow output! %d", ioHandle); 409 break; 410 } 411 LOGV("ioConfigChanged() output %d closed", ioHandle); 412 413 gOutputs.removeItem(ioHandle); 414 for (int i = gStreamOutputMap.size() - 1; i >= 0 ; i--) { 415 if (gStreamOutputMap.valueAt(i) == ioHandle) { 416 gStreamOutputMap.removeItemsAt(i); 417 } 418 } 419 } break; 420 421 case OUTPUT_CONFIG_CHANGED: { 422 int index = gOutputs.indexOfKey(ioHandle); 423 if (index < 0) { 424 LOGW("ioConfigChanged() modifying unknow output! %d", ioHandle); 425 break; 426 } 427 if (param2 == 0) break; 428 desc = (OutputDescriptor *)param2; 429 430 LOGV("ioConfigChanged() new config for output %d samplingRate %d, format %d channels %d frameCount %d latency %d", 431 ioHandle, desc->samplingRate, desc->format, 432 desc->channels, desc->frameCount, desc->latency); 433 OutputDescriptor *outputDesc = gOutputs.valueAt(index); 434 delete outputDesc; 435 outputDesc = new OutputDescriptor(*desc); 436 gOutputs.replaceValueFor(ioHandle, outputDesc); 437 } break; 438 case INPUT_OPENED: 439 case INPUT_CLOSED: 440 case INPUT_CONFIG_CHANGED: 441 break; 442 443 } 444} 445 446void AudioSystem::setErrorCallback(audio_error_callback cb) { 447 Mutex::Autolock _l(gLock); 448 gAudioErrorCallback = cb; 449} 450 451bool AudioSystem::routedToA2dpOutput(int streamType) { 452 switch(streamType) { 453 case AUDIO_STREAM_MUSIC: 454 case AUDIO_STREAM_VOICE_CALL: 455 case AUDIO_STREAM_BLUETOOTH_SCO: 456 case AUDIO_STREAM_SYSTEM: 457 return true; 458 default: 459 return false; 460 } 461} 462 463 464// client singleton for AudioPolicyService binder interface 465sp<IAudioPolicyService> AudioSystem::gAudioPolicyService; 466sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient; 467 468 469// establish binder interface to AudioFlinger service 470const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service() 471{ 472 gLock.lock(); 473 if (gAudioPolicyService.get() == 0) { 474 sp<IServiceManager> sm = defaultServiceManager(); 475 sp<IBinder> binder; 476 do { 477 binder = sm->getService(String16("media.audio_policy")); 478 if (binder != 0) 479 break; 480 LOGW("AudioPolicyService not published, waiting..."); 481 usleep(500000); // 0.5 s 482 } while(true); 483 if (gAudioPolicyServiceClient == NULL) { 484 gAudioPolicyServiceClient = new AudioPolicyServiceClient(); 485 } 486 binder->linkToDeath(gAudioPolicyServiceClient); 487 gAudioPolicyService = interface_cast<IAudioPolicyService>(binder); 488 gLock.unlock(); 489 } else { 490 gLock.unlock(); 491 } 492 return gAudioPolicyService; 493} 494 495status_t AudioSystem::setDeviceConnectionState(audio_devices_t device, 496 audio_policy_dev_state_t state, 497 const char *device_address) 498{ 499 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 500 if (aps == 0) return PERMISSION_DENIED; 501 502 return aps->setDeviceConnectionState(device, state, device_address); 503} 504 505audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device, 506 const char *device_address) 507{ 508 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 509 if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; 510 511 return aps->getDeviceConnectionState(device, device_address); 512} 513 514status_t AudioSystem::setPhoneState(int state) 515{ 516 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 517 if (aps == 0) return PERMISSION_DENIED; 518 519 return aps->setPhoneState(state); 520} 521 522status_t AudioSystem::setRingerMode(uint32_t mode, uint32_t mask) 523{ 524 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 525 if (aps == 0) return PERMISSION_DENIED; 526 return aps->setRingerMode(mode, mask); 527} 528 529status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) 530{ 531 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 532 if (aps == 0) return PERMISSION_DENIED; 533 return aps->setForceUse(usage, config); 534} 535 536audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage) 537{ 538 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 539 if (aps == 0) return AUDIO_POLICY_FORCE_NONE; 540 return aps->getForceUse(usage); 541} 542 543 544audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream, 545 uint32_t samplingRate, 546 uint32_t format, 547 uint32_t channels, 548 audio_policy_output_flags_t flags) 549{ 550 audio_io_handle_t output = 0; 551 // Do not use stream to output map cache if the direct output 552 // flag is set or if we are likely to use a direct output 553 // (e.g voice call stream @ 8kHz could use BT SCO device and be routed to 554 // a direct output on some platforms). 555 // TODO: the output cache and stream to output mapping implementation needs to 556 // be reworked for proper operation with direct outputs. This code is too specific 557 // to the first use case we want to cover (Voice Recognition and Voice Dialer over 558 // Bluetooth SCO 559 if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) == 0 && 560 ((stream != AUDIO_STREAM_VOICE_CALL && stream != AUDIO_STREAM_BLUETOOTH_SCO) || 561 channels != AUDIO_CHANNEL_OUT_MONO || 562 (samplingRate != 8000 && samplingRate != 16000))) { 563 Mutex::Autolock _l(gLock); 564 output = AudioSystem::gStreamOutputMap.valueFor(stream); 565 LOGV_IF((output != 0), "getOutput() read %d from cache for stream %d", output, stream); 566 } 567 if (output == 0) { 568 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 569 if (aps == 0) return 0; 570 output = aps->getOutput(stream, samplingRate, format, channels, flags); 571 if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) == 0) { 572 Mutex::Autolock _l(gLock); 573 AudioSystem::gStreamOutputMap.add(stream, output); 574 } 575 } 576 return output; 577} 578 579status_t AudioSystem::startOutput(audio_io_handle_t output, 580 audio_stream_type_t stream, 581 int session) 582{ 583 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 584 if (aps == 0) return PERMISSION_DENIED; 585 return aps->startOutput(output, stream, session); 586} 587 588status_t AudioSystem::stopOutput(audio_io_handle_t output, 589 audio_stream_type_t stream, 590 int session) 591{ 592 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 593 if (aps == 0) return PERMISSION_DENIED; 594 return aps->stopOutput(output, stream, session); 595} 596 597void AudioSystem::releaseOutput(audio_io_handle_t output) 598{ 599 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 600 if (aps == 0) return; 601 aps->releaseOutput(output); 602} 603 604audio_io_handle_t AudioSystem::getInput(int inputSource, 605 uint32_t samplingRate, 606 uint32_t format, 607 uint32_t channels, 608 audio_in_acoustics_t acoustics, 609 int sessionId) 610{ 611 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 612 if (aps == 0) return 0; 613 return aps->getInput(inputSource, samplingRate, format, channels, acoustics, sessionId); 614} 615 616status_t AudioSystem::startInput(audio_io_handle_t input) 617{ 618 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 619 if (aps == 0) return PERMISSION_DENIED; 620 return aps->startInput(input); 621} 622 623status_t AudioSystem::stopInput(audio_io_handle_t input) 624{ 625 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 626 if (aps == 0) return PERMISSION_DENIED; 627 return aps->stopInput(input); 628} 629 630void AudioSystem::releaseInput(audio_io_handle_t input) 631{ 632 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 633 if (aps == 0) return; 634 aps->releaseInput(input); 635} 636 637status_t AudioSystem::initStreamVolume(audio_stream_type_t stream, 638 int indexMin, 639 int indexMax) 640{ 641 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 642 if (aps == 0) return PERMISSION_DENIED; 643 return aps->initStreamVolume(stream, indexMin, indexMax); 644} 645 646status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream, int index) 647{ 648 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 649 if (aps == 0) return PERMISSION_DENIED; 650 return aps->setStreamVolumeIndex(stream, index); 651} 652 653status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream, int *index) 654{ 655 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 656 if (aps == 0) return PERMISSION_DENIED; 657 return aps->getStreamVolumeIndex(stream, index); 658} 659 660uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) 661{ 662 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 663 if (aps == 0) return 0; 664 return aps->getStrategyForStream(stream); 665} 666 667uint32_t AudioSystem::getDevicesForStream(audio_stream_type_t stream) 668{ 669 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 670 if (aps == 0) return 0; 671 return aps->getDevicesForStream(stream); 672} 673 674audio_io_handle_t AudioSystem::getOutputForEffect(effect_descriptor_t *desc) 675{ 676 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 677 if (aps == 0) return PERMISSION_DENIED; 678 return aps->getOutputForEffect(desc); 679} 680 681status_t AudioSystem::registerEffect(effect_descriptor_t *desc, 682 audio_io_handle_t io, 683 uint32_t strategy, 684 int session, 685 int id) 686{ 687 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 688 if (aps == 0) return PERMISSION_DENIED; 689 return aps->registerEffect(desc, io, strategy, session, id); 690} 691 692status_t AudioSystem::unregisterEffect(int id) 693{ 694 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 695 if (aps == 0) return PERMISSION_DENIED; 696 return aps->unregisterEffect(id); 697} 698 699status_t AudioSystem::isStreamActive(int stream, bool* state, uint32_t inPastMs) 700{ 701 const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); 702 if (aps == 0) return PERMISSION_DENIED; 703 if (state == NULL) return BAD_VALUE; 704 *state = aps->isStreamActive(stream, inPastMs); 705 return NO_ERROR; 706} 707 708 709// --------------------------------------------------------------------------- 710 711void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who) { 712 Mutex::Autolock _l(AudioSystem::gLock); 713 AudioSystem::gAudioPolicyService.clear(); 714 715 LOGW("AudioPolicyService server died!"); 716} 717 718}; // namespace android 719 720