当前位置:编程学习 > JAVA >>

WebRtc VoiceEngine代码解析

WebRtc中VoiceEngine可以完成大部分的VOIP相关人物,包括采集、自动增益、噪声消除、回声抑制、编解码、RTP传输。下边我们通过代码来解析Voe中处理流程;

创建VoiceEngine和VoEBase


[cpp]
VoiceEngine* _vePtr = VoiceEngine::Create();                    //创建VoiceEngine  
VoEBase* _veBasePtr = VoEBase::GetInterface(_vePtr);            //创建VoeBase  所有Voe相关操作通过这个共有类  
_veBasePtr->Init()                                              //创建整个Voe处理线程 

VoiceEngine* _vePtr = VoiceEngine::Create();                    //创建VoiceEngine
VoEBase* _veBasePtr = VoEBase::GetInterface(_vePtr);            //创建VoeBase  所有Voe相关操作通过这个共有类
_veBasePtr->Init()                                              //创建整个Voe处理线程
重点就在_veBasePtr->Init()  它会创建voe线程,线程负责采集、数字信号处理、编码、rtp传输。


[cpp]
int VoEBaseImpl::Init(AudioDeviceModule* external_adm, 
                      AudioProcessing* audioproc) 

    _shared->process_thread();   //创建voe线程  
    _shared->process_thread()->Start(); 
    _shared->audio_device()->Init(); 
    
 

int VoEBaseImpl::Init(AudioDeviceModule* external_adm,
                      AudioProcessing* audioproc)
{
    _shared->process_thread();   //创建voe线程
    _shared->process_thread()->Start();
    _shared->audio_device()->Init();
  

}audio_device()->Init()重载了int32_t AudioDeviceWindowsWave::Init()(windowns平台),别的平台是别的函数,基本差不多,在这个Init中,创建了ThreadProcess线程,ThreadProcess线程负责所有的音频流程,从设备获取音频数据包。


[cpp]
bool AudioDeviceWindowsWave::ThreadProcess() 

   while ((nRecordedBytes = RecProc(recTime)) > 0); 

bool AudioDeviceWindowsWave::ThreadProcess()
{
   while ((nRecordedBytes = RecProc(recTime)) > 0);
}处理过程在RecProc


[cpp]
int32_t AudioDeviceWindowsWave::RecProc(LONGLONG& consumedTime) 

     _ptrAudioBuffer->DeliverRecordedData(); <SPAN style="FONT-FAMILY: Arial, Helvetica, sans-serif">}</SPAN> 

int32_t AudioDeviceWindowsWave::RecProc(LONGLONG& consumedTime)
{
     _ptrAudioBuffer->DeliverRecordedData(); }
[cpp]
int32_t AudioDeviceBuffer::DeliverRecordedData() 

     _ptrCbAudioTransport->RecordedDataIsAvailable(); 

int32_t AudioDeviceBuffer::DeliverRecordedData()
{
     _ptrCbAudioTransport->RecordedDataIsAvailable();
}


RecordedDataIsAvailable是虚函数,被VoeBase重载

 

[cpp]
int32_t VoEBaseImpl::RecordedDataIsAvailable( 
        const void* audioSamples, 
        uint32_t nSamples, 
        uint8_t nBytesPerSample, 
        uint8_t nChannels, 
        uint32_t samplesPerSec, 
        uint32_t totalDelayMS, 
        int32_t clockDrift, 
        uint32_t currentMicLevel, 
        bool keyPressed, 
        uint32_t& newMicLevel) 

     _shared->transmit_mixer()->DemuxAndMix(); 
    _shared->transmit_mixer()->EncodeAndSend(); 

int32_t VoEBaseImpl::RecordedDataIsAvailable(
        const void* audioSamples,
        uint32_t nSamples,
        uint8_t nBytesPerSample,
        uint8_t nChannels,
        uint32_t samplesPerSec,
        uint32_t totalDelayMS,
        int32_t clockDrift,
        uint32_t currentMicLevel,
        bool keyPressed,
        uint32_t& newMicLevel)
{
     _shared->transmit_mixer()->DemuxAndMix();
    _shared->transmit_mixer()->EncodeAndSend();
}
DemuxAndMix() 从字面意思是分路与混合,这个函数,主要负责AudioProcess的所有过程,包括Aec,Aecm,AGC,DTMF,遍历所有channel;


[cpp]
TransmitMixer::DemuxAndMix() 

    Channel* channelPtr = sc.GetFirstChannel(iterator); 
    while (channelPtr != NULL) 
    { 
        if (channelPtr->InputIsOnHold()) 
        { 
            channelPtr->UpdateLocalTimeStamp(); 
        } else if (channelPtr->Sending()) 
        { 
            // Demultiplex makes a copy of its input.  
            channelPtr->Demultiplex(_audioFrame); 
            channelPtr->PrepareEncodeAndSend(_audioFrame.sample_rate_hz_); 
        } 
        channelPtr = sc.GetNextChannel(iterator); 
    } 
 

TransmitMixer::DemuxAndMix()
{
    Channel* channelPtr = sc.GetFirstChannel(iterator);
    while (channelPtr != NULL)
    {
        if (channelPtr->InputIsOnHold())
        {
            channelPtr->UpdateLocalTimeStamp();
   &

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