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

Android Audio代码分析8 - AudioHardwareALSA::openOutputStream函数

发现以前写的东西,对调用函数的展开放在了函数的前面,导致不方便找到原来代码及设置的函数参数。
以后打算稍作改动,把对被调函数的展开放在原代码的后面,这样看起来应该方便些。


闲言少叙,跳入代码。


前两天看AudioTrack创建的时候,我们看到了AudioHardwareALSA::openOutputStream,并没有继续往下看。
今天就看看函数AudioHardwareALSA::openOutputStream的实现。


*****************************************源码*************************************************
AudioStreamOut *
AudioHardwareALSA::openOutputStream(uint32_t devices,
                                    int *format,
                                    uint32_t *channels,
                                    uint32_t *sampleRate,
                                    status_t *status)
{
    AutoMutex lock(mLock);


    LOGD("openOutputStream called for devices: 0x%08x", devices);


    status_t err = BAD_VALUE;
    AudioStreamOutALSA *out = 0;


    if (devices & (devices - 1)) {
        if (status) *status = err;
        LOGD("openOutputStream called with bad devices");
        return out;
    }


    // Find the appropriate alsa device
    for(ALSAHandleList::iterator it = mDeviceList.begin();
        it != mDeviceList.end(); ++it)
        if (it->devices & devices) {
            err = mALSADevice->open(&(*it), devices, mode());
            if (err) break;
            if (devices & AudioSystem::DEVICE_OUT_WIRED_HDMI){
                strcpy(mCurCard ,SPDIF);
                mMixer = mMixerSpdif;
            } else {
                strcpy(mCurCard,SGTL5000);
                mMixer = mMixerSgtl5000;
            }


            out = new AudioStreamOutALSA(this, &(*it));
            err = out->set(format, channels, sampleRate);
            break;
        }


    if (status) *status = err;
    return out;
}
**********************************************************************************************
源码路径:
hardware\alsa_sound\AudioHardwareALSA.cpp


###########################################说明################################################
AudioStreamOut *
AudioHardwareALSA::openOutputStream(uint32_t devices,
                                    int *format,
                                    uint32_t *channels,
                                    uint32_t *sampleRate,
                                    status_t *status)
{
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
先回忆一下各个参数都是嘛意思。
devices
设备编号。也就是说打开的是第几个ALSA声卡。该参数是在函数AudioPolicyManagerBase::getOutput中产生的。
至于如何调到了函数AudioHardwareALSA::openOutputStream,可以参考文章:Android Audio代码分析4 - AudioSystem::getOutputSamplingRate。
函数AudioPolicyManagerBase::getOutput中直接调用的是AudioPolicyService::openOutput函数。调用代码如下:
        output = mpClientInte易做图ce->openOutput(&outputDesc->mDevice,
                                        &outputDesc->mSamplingRate,
                                        &outputDesc->mFormat,
                                        &outputDesc->mChannels,
                                        &outputDesc->mLatency,
                                        outputDesc->mFlags);
其中,outputDesc->mDevice就是devices。
outputDesc->mDevice是怎么来的呢?
继续往前看,outputDesc->mDevice = device;
device的出处在哪?
uint32_t device = getDeviceForStrategy(strategy);
strategy的来头是:
routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);


看看这两个函数吧。
先看看getStrategy。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AudioPolicyManagerBase::routing_strategy AudioPolicyManagerBase::getStrategy(
        AudioSystem::stream_type stream) {
    // stream to strategy mapping
    switc

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,