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