当前位置:操作系统 > 安卓/Android >>

Android Audio代码分析4 - AudioSystem::getOutputSamplingRate

 

前面看过的代码中,经常会调用到AudioSystem类中的getOutputSamplingRate函数,getOutputFrameCount函数,getOutputLatency函数等。

这些函数的实现基本类似,今天就细细品味下AudioSystem::getOutputSamplingRate函数。

 

*****************************************源码*************************************************

status_t AudioSystem::getOutputSamplingRate(int* samplingRate, int streamType)

{

    OutputDescriptor *outputDesc;

    audio_io_handle_t output;

 

    if (streamType == DEFAULT) {

        streamType = MUSIC;

    }

 

    output = getOutput((stream_type)streamType);

    if (output == 0) {

        return PERMISSION_DENIED;

    }

 

    gLock.lock();

    outputDesc = AudioSystem::gOutputs.valueFor(output);

    if (outputDesc == 0) {

        LOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output);

        gLock.unlock();

        const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();

        if (af == 0) return PERMISSION_DENIED;

        *samplingRate = af->sampleRate(output);

    } else {

        LOGV("getOutputSamplingRate() reading from output desc");

        *samplingRate = outputDesc->samplingRate;

        gLock.unlock();

    }

 

    LOGV("getOutputSamplingRate() streamType %d, output %d, sampling rate %d", streamType, output, *samplingRate);

 

    return NO_ERROR;

}

**********************************************************************************************

源码路径:

frameworks\base\media\libmedia\AudioSystem.cpp

 

###########################################说明################################################

status_t AudioSystem::getOutputSamplingRate(int* samplingRate, int streamType)

{

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    // audio output descritor used to cache output configurations in client process to avoid frequent calls

    // through IAudioFlinger

    class OutputDescriptor {

    public:

        OutputDescriptor()

        : samplingRate(0), format(0), channels(0), frameCount(0), latency(0)  {}

 

        uint32_t samplingRate;

        int32_t format;

        int32_t channels;

        size_t frameCount;

        uint32_t latency;

    };

----------------------------------------------------------------

    OutputDescriptor *outputDesc;

// typedef int audio_io_handle_t;

    audio_io_handle_t output;

 

// Default类型的stream,强制转为music

    if (streamType == DEFAULT) {

        streamType = MUSIC;

    }

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

audio_io_handle_t AudioSystem::getOutput(stream_type stream,

                                    uint32_t samplingRate,

                                    uint32_t format,

                                    uint32_t channels,

                                    output_flags flags)

{

    audio_io_handle_t output = 0;

// 如果是direct模式,或者可能会使用direct模式,就不能使用cache中保存的output

    // Do not use stream to output map cache if the direct output

    // flag is set or if we are likely to use a direct output

    // (e.g voice call stream @ 8kHz could use BT SCO device and be routed to

    // a direct output on some platforms).

    // TODO: the output cache and stream to output mapping implementation needs to

    // be reworked for proper operation with direct outputs. This code is too specific

    // to the first use case we want to cover (Voice Recognition and Voice Dialer over

    // Bluetooth SCO

    if ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) == 0 &&

        ((stream != AudioSystem::VOICE_CALL && stream != AudioSystem::BLUETOOTH_SCO) ||

         channels != AudioSystem::CHANNEL_OUT_MONO ||

         (samplingRate != 8000 && samplingRate != 16000))) {

        Mutex::Autolock _l(gLock);

  // gStreamOutputMap中保存的是stream与output之间的对应关系

  // 调用getOutput时,如果获取不到output,就会调用AudioPolicyService的getOutput函数获取output,

  // 并将stream与output的对应关系添加到gStreamOutputMap。(也就是该函数下面的处理)

        output = AudioSystem::gStreamOutputMap.valueFor(stream);

        LOGV_IF((output != 0), "getOutput() read %d from cache for stream %d", output, stream);

    }

 // 如果Cache中找不到stream对应的output

    if (output == 0) {

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// establish binder interface to AudioFlinger service

const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()

{

    gLock.lock();

 // 如果gAudioPolicyService尚未创建,则创建一个,否则直接将gAudioPolicyService返回

    if (gAudioPolicyService.get() == 0) {

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

ServiceManager是一个管理所有service的一个东东

sp<IServiceManager> defaultServiceManager()

{

// 如果gDefaultServiceManager不为空,直接将其返回

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,