Home | History | Annotate | Download | only in libmedia
      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 
     30 namespace android {
     31 
     32 // client singleton for AudioFlinger binder interface
     33 Mutex AudioSystem::gLock;
     34 sp<IAudioFlinger> AudioSystem::gAudioFlinger;
     35 sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient;
     36 audio_error_callback AudioSystem::gAudioErrorCallback = NULL;
     37 // Cached values
     38 
     39 DefaultKeyedVector<audio_io_handle_t, AudioSystem::OutputDescriptor *> AudioSystem::gOutputs(0);
     40 
     41 // Cached values for recording queries, all protected by gLock
     42 uint32_t AudioSystem::gPrevInSamplingRate = 16000;
     43 audio_format_t AudioSystem::gPrevInFormat = AUDIO_FORMAT_PCM_16_BIT;
     44 audio_channel_mask_t AudioSystem::gPrevInChannelMask = AUDIO_CHANNEL_IN_MONO;
     45 size_t AudioSystem::gInBuffSize = 0;
     46 
     47 
     48 // establish binder interface to AudioFlinger service
     49 const sp<IAudioFlinger>& AudioSystem::get_audio_flinger()
     50 {
     51     Mutex::Autolock _l(gLock);
     52     if (gAudioFlinger == 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             ALOGW("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     ALOGE_IF(gAudioFlinger==0, "no AudioFlinger!?");
     74 
     75     return gAudioFlinger;
     76 }
     77 
     78 status_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 
     84 status_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 
     91 status_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 
     99 status_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 
    107 status_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 
    115 status_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 
    123 status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value,
    124         audio_io_handle_t output)
    125 {
    126     if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
    127     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
    128     if (af == 0) return PERMISSION_DENIED;
    129     af->setStreamVolume(stream, value, output);
    130     return NO_ERROR;
    131 }
    132 
    133 status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute)
    134 {
    135     if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
    136     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
    137     if (af == 0) return PERMISSION_DENIED;
    138     af->setStreamMute(stream, mute);
    139     return NO_ERROR;
    140 }
    141 
    142 status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume,
    143         audio_io_handle_t output)
    144 {
    145     if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
    146     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
    147     if (af == 0) return PERMISSION_DENIED;
    148     *volume = af->streamVolume(stream, output);
    149     return NO_ERROR;
    150 }
    151 
    152 status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute)
    153 {
    154     if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE;
    155     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
    156     if (af == 0) return PERMISSION_DENIED;
    157     *mute = af->streamMute(stream);
    158     return NO_ERROR;
    159 }
    160 
    161 status_t AudioSystem::setMode(audio_mode_t mode)
    162 {
    163     if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE;
    164     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
    165     if (af == 0) return PERMISSION_DENIED;
    166     return af->setMode(mode);
    167 }
    168 
    169 status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {
    170     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
    171     if (af == 0) return PERMISSION_DENIED;
    172     return af->setParameters(ioHandle, keyValuePairs);
    173 }
    174 
    175 String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) {
    176     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
    177     String8 result = String8("");
    178     if (af == 0) return result;
    179 
    180     result = af->getParameters(ioHandle, keys);
    181     return result;
    182 }
    183 
    184 // convert volume steps to natural log scale
    185 
    186 // change this value to change volume scaling
    187 static const float dBPerStep = 0.5f;
    188 // shouldn't need to touch these
    189 static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f;
    190 static const float dBConvertInverse = 1.0f / dBConvert;
    191 
    192 float AudioSystem::linearToLog(int volume)
    193 {
    194     // float v = volume ? exp(float(100 - volume) * dBConvert) : 0;
    195     // ALOGD("linearToLog(%d)=%f", volume, v);
    196     // return v;
    197     return volume ? exp(float(100 - volume) * dBConvert) : 0;
    198 }
    199 
    200 int AudioSystem::logToLinear(float volume)
    201 {
    202     // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
    203     // ALOGD("logTolinear(%d)=%f", v, volume);
    204     // return v;
    205     return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0;
    206 }
    207 
    208 // DEPRECATED
    209 status_t AudioSystem::getOutputSamplingRate(int* samplingRate, int streamType) {
    210     return getOutputSamplingRate(samplingRate, (audio_stream_type_t)streamType);
    211 }
    212 
    213 status_t AudioSystem::getOutputSamplingRate(int* samplingRate, audio_stream_type_t streamType)
    214 {
    215     audio_io_handle_t output;
    216 
    217     if (streamType == AUDIO_STREAM_DEFAULT) {
    218         streamType = AUDIO_STREAM_MUSIC;
    219     }
    220 
    221     output = getOutput(streamType);
    222     if (output == 0) {
    223         return PERMISSION_DENIED;
    224     }
    225 
    226     return getSamplingRate(output, streamType, samplingRate);
    227 }
    228 
    229 status_t AudioSystem::getSamplingRate(audio_io_handle_t output,
    230                                       audio_stream_type_t streamType,
    231                                       int* samplingRate)
    232 {
    233     OutputDescriptor *outputDesc;
    234 
    235     gLock.lock();
    236     outputDesc = AudioSystem::gOutputs.valueFor(output);
    237     if (outputDesc == NULL) {
    238         ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);
    239         gLock.unlock();
    240         const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
    241         if (af == 0) return PERMISSION_DENIED;
    242         *samplingRate = af->sampleRate(output);
    243     } else {
    244         ALOGV("getOutputSamplingRate() reading from output desc");
    245         *samplingRate = outputDesc->samplingRate;
    246         gLock.unlock();
    247     }
    248 
    249     ALOGV("getSamplingRate() streamType %d, output %d, sampling rate %d", streamType, output, *samplingRate);
    250 
    251     return NO_ERROR;
    252 }
    253 
    254 // DEPRECATED
    255 status_t AudioSystem::getOutputFrameCount(int* frameCount, int streamType) {
    256     return getOutputFrameCount(frameCount, (audio_stream_type_t)streamType);
    257 }
    258 
    259 status_t AudioSystem::getOutputFrameCount(int* frameCount, audio_stream_type_t streamType)
    260 {
    261     audio_io_handle_t output;
    262 
    263     if (streamType == AUDIO_STREAM_DEFAULT) {
    264         streamType = AUDIO_STREAM_MUSIC;
    265     }
    266 
    267     output = getOutput(streamType);
    268     if (output == 0) {
    269         return PERMISSION_DENIED;
    270     }
    271 
    272     return getFrameCount(output, streamType, frameCount);
    273 }
    274 
    275 status_t AudioSystem::getFrameCount(audio_io_handle_t output,
    276                                     audio_stream_type_t streamType,
    277                                     int* frameCount)
    278 {
    279     OutputDescriptor *outputDesc;
    280 
    281     gLock.lock();
    282     outputDesc = AudioSystem::gOutputs.valueFor(output);
    283     if (outputDesc == NULL) {
    284         gLock.unlock();
    285         const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
    286         if (af == 0) return PERMISSION_DENIED;
    287         *frameCount = af->frameCount(output);
    288     } else {
    289         *frameCount = outputDesc->frameCount;
    290         gLock.unlock();
    291     }
    292 
    293     ALOGV("getFrameCount() streamType %d, output %d, frameCount %d", streamType, output, *frameCount);
    294 
    295     return NO_ERROR;
    296 }
    297 
    298 status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType)
    299 {
    300     audio_io_handle_t output;
    301 
    302     if (streamType == AUDIO_STREAM_DEFAULT) {
    303         streamType = AUDIO_STREAM_MUSIC;
    304     }
    305 
    306     output = getOutput(streamType);
    307     if (output == 0) {
    308         return PERMISSION_DENIED;
    309     }
    310 
    311     return getLatency(output, streamType, latency);
    312 }
    313 
    314 status_t AudioSystem::getLatency(audio_io_handle_t output,
    315                                  audio_stream_type_t streamType,
    316                                  uint32_t* latency)
    317 {
    318     OutputDescriptor *outputDesc;
    319 
    320     gLock.lock();
    321     outputDesc = AudioSystem::gOutputs.valueFor(output);
    322     if (outputDesc == NULL) {
    323         gLock.unlock();
    324         const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
    325         if (af == 0) return PERMISSION_DENIED;
    326         *latency = af->latency(output);
    327     } else {
    328         *latency = outputDesc->latency;
    329         gLock.unlock();
    330     }
    331 
    332     ALOGV("getLatency() streamType %d, output %d, latency %d", streamType, output, *latency);
    333 
    334     return NO_ERROR;
    335 }
    336 
    337 status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format,
    338         audio_channel_mask_t channelMask, size_t* buffSize)
    339 {
    340     gLock.lock();
    341     // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values
    342     size_t inBuffSize = gInBuffSize;
    343     if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat)
    344         || (channelMask != gPrevInChannelMask)) {
    345         gLock.unlock();
    346         const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
    347         if (af == 0) {
    348             return PERMISSION_DENIED;
    349         }
    350         inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask);
    351         gLock.lock();
    352         // save the request params
    353         gPrevInSamplingRate = sampleRate;
    354         gPrevInFormat = format;
    355         gPrevInChannelMask = channelMask;
    356 
    357         gInBuffSize = inBuffSize;
    358     }
    359     gLock.unlock();
    360     *buffSize = inBuffSize;
    361 
    362     return NO_ERROR;
    363 }
    364 
    365 status_t AudioSystem::setVoiceVolume(float value)
    366 {
    367     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
    368     if (af == 0) return PERMISSION_DENIED;
    369     return af->setVoiceVolume(value);
    370 }
    371 
    372 status_t AudioSystem::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, audio_stream_type_t stream)
    373 {
    374     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
    375     if (af == 0) return PERMISSION_DENIED;
    376 
    377     if (stream == AUDIO_STREAM_DEFAULT) {
    378         stream = AUDIO_STREAM_MUSIC;
    379     }
    380 
    381     return af->getRenderPosition(halFrames, dspFrames, getOutput(stream));
    382 }
    383 
    384 unsigned int AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) {
    385     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
    386     unsigned int result = 0;
    387     if (af == 0) return result;
    388     if (ioHandle == 0) return result;
    389 
    390     result = af->getInputFramesLost(ioHandle);
    391     return result;
    392 }
    393 
    394 int AudioSystem::newAudioSessionId() {
    395     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
    396     if (af == 0) return 0;
    397     return af->newAudioSessionId();
    398 }
    399 
    400 void AudioSystem::acquireAudioSessionId(int audioSession) {
    401     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
    402     if (af != 0) {
    403         af->acquireAudioSessionId(audioSession);
    404     }
    405 }
    406 
    407 void AudioSystem::releaseAudioSessionId(int audioSession) {
    408     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
    409     if (af != 0) {
    410         af->releaseAudioSessionId(audioSession);
    411     }
    412 }
    413 
    414 // ---------------------------------------------------------------------------
    415 
    416 void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who) {
    417     Mutex::Autolock _l(AudioSystem::gLock);
    418 
    419     AudioSystem::gAudioFlinger.clear();
    420     // clear output handles and stream to output map caches
    421     AudioSystem::gOutputs.clear();
    422 
    423     if (gAudioErrorCallback) {
    424         gAudioErrorCallback(DEAD_OBJECT);
    425     }
    426     ALOGW("AudioFlinger server died!");
    427 }
    428 
    429 void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, audio_io_handle_t ioHandle,
    430         const void *param2) {
    431     ALOGV("ioConfigChanged() event %d", event);
    432     const OutputDescriptor *desc;
    433     audio_stream_type_t stream;
    434 
    435     if (ioHandle == 0) return;
    436 
    437     Mutex::Autolock _l(AudioSystem::gLock);
    438 
    439     switch (event) {
    440     case STREAM_CONFIG_CHANGED:
    441         break;
    442     case OUTPUT_OPENED: {
    443         if (gOutputs.indexOfKey(ioHandle) >= 0) {
    444             ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle);
    445             break;
    446         }
    447         if (param2 == NULL) break;
    448         desc = (const OutputDescriptor *)param2;
    449 
    450         OutputDescriptor *outputDesc =  new OutputDescriptor(*desc);
    451         gOutputs.add(ioHandle, outputDesc);
    452         ALOGV("ioConfigChanged() new output samplingRate %d, format %d channels %#x frameCount %d latency %d",
    453                 outputDesc->samplingRate, outputDesc->format, outputDesc->channels, outputDesc->frameCount, outputDesc->latency);
    454         } break;
    455     case OUTPUT_CLOSED: {
    456         if (gOutputs.indexOfKey(ioHandle) < 0) {
    457             ALOGW("ioConfigChanged() closing unknow output! %d", ioHandle);
    458             break;
    459         }
    460         ALOGV("ioConfigChanged() output %d closed", ioHandle);
    461 
    462         gOutputs.removeItem(ioHandle);
    463         } break;
    464 
    465     case OUTPUT_CONFIG_CHANGED: {
    466         int index = gOutputs.indexOfKey(ioHandle);
    467         if (index < 0) {
    468             ALOGW("ioConfigChanged() modifying unknow output! %d", ioHandle);
    469             break;
    470         }
    471         if (param2 == NULL) break;
    472         desc = (const OutputDescriptor *)param2;
    473 
    474         ALOGV("ioConfigChanged() new config for output %d samplingRate %d, format %d channels %#x frameCount %d latency %d",
    475                 ioHandle, desc->samplingRate, desc->format,
    476                 desc->channels, desc->frameCount, desc->latency);
    477         OutputDescriptor *outputDesc = gOutputs.valueAt(index);
    478         delete outputDesc;
    479         outputDesc =  new OutputDescriptor(*desc);
    480         gOutputs.replaceValueFor(ioHandle, outputDesc);
    481     } break;
    482     case INPUT_OPENED:
    483     case INPUT_CLOSED:
    484     case INPUT_CONFIG_CHANGED:
    485         break;
    486 
    487     }
    488 }
    489 
    490 void AudioSystem::setErrorCallback(audio_error_callback cb) {
    491     Mutex::Autolock _l(gLock);
    492     gAudioErrorCallback = cb;
    493 }
    494 
    495 bool AudioSystem::routedToA2dpOutput(audio_stream_type_t streamType) {
    496     switch (streamType) {
    497     case AUDIO_STREAM_MUSIC:
    498     case AUDIO_STREAM_VOICE_CALL:
    499     case AUDIO_STREAM_BLUETOOTH_SCO:
    500     case AUDIO_STREAM_SYSTEM:
    501         return true;
    502     default:
    503         return false;
    504     }
    505 }
    506 
    507 
    508 // client singleton for AudioPolicyService binder interface
    509 sp<IAudioPolicyService> AudioSystem::gAudioPolicyService;
    510 sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient;
    511 
    512 
    513 // establish binder interface to AudioFlinger service
    514 const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()
    515 {
    516     gLock.lock();
    517     if (gAudioPolicyService == 0) {
    518         sp<IServiceManager> sm = defaultServiceManager();
    519         sp<IBinder> binder;
    520         do {
    521             binder = sm->getService(String16("media.audio_policy"));
    522             if (binder != 0)
    523                 break;
    524             ALOGW("AudioPolicyService not published, waiting...");
    525             usleep(500000); // 0.5 s
    526         } while (true);
    527         if (gAudioPolicyServiceClient == NULL) {
    528             gAudioPolicyServiceClient = new AudioPolicyServiceClient();
    529         }
    530         binder->linkToDeath(gAudioPolicyServiceClient);
    531         gAudioPolicyService = interface_cast<IAudioPolicyService>(binder);
    532         gLock.unlock();
    533     } else {
    534         gLock.unlock();
    535     }
    536     return gAudioPolicyService;
    537 }
    538 
    539 status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
    540                                                audio_policy_dev_state_t state,
    541                                                const char *device_address)
    542 {
    543     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    544     const char *address = "";
    545 
    546     if (aps == 0) return PERMISSION_DENIED;
    547 
    548     if (device_address != NULL) {
    549         address = device_address;
    550     }
    551 
    552     return aps->setDeviceConnectionState(device, state, address);
    553 }
    554 
    555 audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device,
    556                                                   const char *device_address)
    557 {
    558     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    559     if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
    560 
    561     return aps->getDeviceConnectionState(device, device_address);
    562 }
    563 
    564 status_t AudioSystem::setPhoneState(audio_mode_t state)
    565 {
    566     if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE;
    567     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    568     if (aps == 0) return PERMISSION_DENIED;
    569 
    570     return aps->setPhoneState(state);
    571 }
    572 
    573 status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
    574 {
    575     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    576     if (aps == 0) return PERMISSION_DENIED;
    577     return aps->setForceUse(usage, config);
    578 }
    579 
    580 audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage)
    581 {
    582     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    583     if (aps == 0) return AUDIO_POLICY_FORCE_NONE;
    584     return aps->getForceUse(usage);
    585 }
    586 
    587 
    588 audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream,
    589                                     uint32_t samplingRate,
    590                                     audio_format_t format,
    591                                     audio_channel_mask_t channelMask,
    592                                     audio_output_flags_t flags)
    593 {
    594     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    595     if (aps == 0) return 0;
    596     return aps->getOutput(stream, samplingRate, format, channelMask, flags);
    597 }
    598 
    599 status_t AudioSystem::startOutput(audio_io_handle_t output,
    600                                   audio_stream_type_t stream,
    601                                   int session)
    602 {
    603     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    604     if (aps == 0) return PERMISSION_DENIED;
    605     return aps->startOutput(output, stream, session);
    606 }
    607 
    608 status_t AudioSystem::stopOutput(audio_io_handle_t output,
    609                                  audio_stream_type_t stream,
    610                                  int session)
    611 {
    612     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    613     if (aps == 0) return PERMISSION_DENIED;
    614     return aps->stopOutput(output, stream, session);
    615 }
    616 
    617 void AudioSystem::releaseOutput(audio_io_handle_t output)
    618 {
    619     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    620     if (aps == 0) return;
    621     aps->releaseOutput(output);
    622 }
    623 
    624 audio_io_handle_t AudioSystem::getInput(audio_source_t inputSource,
    625                                     uint32_t samplingRate,
    626                                     audio_format_t format,
    627                                     audio_channel_mask_t channelMask,
    628                                     int sessionId)
    629 {
    630     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    631     if (aps == 0) return 0;
    632     return aps->getInput(inputSource, samplingRate, format, channelMask, sessionId);
    633 }
    634 
    635 status_t AudioSystem::startInput(audio_io_handle_t input)
    636 {
    637     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    638     if (aps == 0) return PERMISSION_DENIED;
    639     return aps->startInput(input);
    640 }
    641 
    642 status_t AudioSystem::stopInput(audio_io_handle_t input)
    643 {
    644     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    645     if (aps == 0) return PERMISSION_DENIED;
    646     return aps->stopInput(input);
    647 }
    648 
    649 void AudioSystem::releaseInput(audio_io_handle_t input)
    650 {
    651     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    652     if (aps == 0) return;
    653     aps->releaseInput(input);
    654 }
    655 
    656 status_t AudioSystem::initStreamVolume(audio_stream_type_t stream,
    657                                     int indexMin,
    658                                     int indexMax)
    659 {
    660     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    661     if (aps == 0) return PERMISSION_DENIED;
    662     return aps->initStreamVolume(stream, indexMin, indexMax);
    663 }
    664 
    665 status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream,
    666                                            int index,
    667                                            audio_devices_t device)
    668 {
    669     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    670     if (aps == 0) return PERMISSION_DENIED;
    671     return aps->setStreamVolumeIndex(stream, index, device);
    672 }
    673 
    674 status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream,
    675                                            int *index,
    676                                            audio_devices_t device)
    677 {
    678     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    679     if (aps == 0) return PERMISSION_DENIED;
    680     return aps->getStreamVolumeIndex(stream, index, device);
    681 }
    682 
    683 uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream)
    684 {
    685     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    686     if (aps == 0) return 0;
    687     return aps->getStrategyForStream(stream);
    688 }
    689 
    690 audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream)
    691 {
    692     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    693     if (aps == 0) return (audio_devices_t)0;
    694     return aps->getDevicesForStream(stream);
    695 }
    696 
    697 audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc)
    698 {
    699     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    700     if (aps == 0) return PERMISSION_DENIED;
    701     return aps->getOutputForEffect(desc);
    702 }
    703 
    704 status_t AudioSystem::registerEffect(const effect_descriptor_t *desc,
    705                                 audio_io_handle_t io,
    706                                 uint32_t strategy,
    707                                 int session,
    708                                 int id)
    709 {
    710     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    711     if (aps == 0) return PERMISSION_DENIED;
    712     return aps->registerEffect(desc, io, strategy, session, id);
    713 }
    714 
    715 status_t AudioSystem::unregisterEffect(int id)
    716 {
    717     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    718     if (aps == 0) return PERMISSION_DENIED;
    719     return aps->unregisterEffect(id);
    720 }
    721 
    722 status_t AudioSystem::setEffectEnabled(int id, bool enabled)
    723 {
    724     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    725     if (aps == 0) return PERMISSION_DENIED;
    726     return aps->setEffectEnabled(id, enabled);
    727 }
    728 
    729 status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs)
    730 {
    731     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    732     if (aps == 0) return PERMISSION_DENIED;
    733     if (state == NULL) return BAD_VALUE;
    734     *state = aps->isStreamActive(stream, inPastMs);
    735     return NO_ERROR;
    736 }
    737 
    738 status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state)
    739 {
    740     const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    741     if (aps == 0) return PERMISSION_DENIED;
    742     if (state == NULL) return BAD_VALUE;
    743     *state = aps->isSourceActive(stream);
    744     return NO_ERROR;
    745 }
    746 
    747 int32_t AudioSystem::getPrimaryOutputSamplingRate()
    748 {
    749     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
    750     if (af == 0) return 0;
    751     return af->getPrimaryOutputSamplingRate();
    752 }
    753 
    754 int32_t AudioSystem::getPrimaryOutputFrameCount()
    755 {
    756     const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
    757     if (af == 0) return 0;
    758     return af->getPrimaryOutputFrameCount();
    759 }
    760 
    761 void AudioSystem::clearAudioConfigCache()
    762 {
    763     Mutex::Autolock _l(gLock);
    764     ALOGV("clearAudioConfigCache()");
    765     gOutputs.clear();
    766 }
    767 
    768 // ---------------------------------------------------------------------------
    769 
    770 void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who) {
    771     Mutex::Autolock _l(AudioSystem::gLock);
    772     AudioSystem::gAudioPolicyService.clear();
    773 
    774     ALOGW("AudioPolicyService server died!");
    775 }
    776 
    777 }; // namespace android
    778