rtc校准算法_Webrtc AGC 算法(二)

本文详细介绍了iOS平台上WebRTC的自动增益控制(AGC)算法,包括如何通过AudioComponent和AudioUnit设置硬件AGC,以及WebRTC内部的AGC处理流程。在iOS中,VPIO单元已经支持AGC,但WebRTC提供了ios_force_software_aec_HACK选项以在硬件AGC不可用时强制开启软件AGC。同时,文章展示了如何通过WebRtcVoiceEngine的ApplyOptions方法开启WebRTC的AGC,并分析了AGC的处理流程,包括ProcessCaptureStreamLocked函数中的关键步骤。
摘要由CSDN通过智能技术生成

1. IOS平台相关概念

1.1 IOS平台自带AGC功能

可以通过以下方法查看ios平台是否支持AGC和设置AGC(代码摘自webrtc):

//1. 创建Component,设置开启硬件音频处理

// Create an audio component description to identify the Voice Processing

// I/O audio unit.

AudioComponentDescription vpio_unit_description;

vpio_unit_description.componentType = kAudioUnitType_Output;

vpio_unit_description.componentSubType = kAudioUnitSubType_VoiceProcessingIO;//设置这个 sub type 后,会启用系统的 AEC, AGC, NS 等

vpio_unit_description.componentManufacturer = kAudioUnitManufacturer_Apple;

vpio_unit_description.componentFlags = 0;

vpio_unit_description.componentFlagsMask = 0;

// Obtain an audio unit instance given the description.

AudioComponent found_vpio_unit_ref =

AudioComponentFindNext(nullptr, &vpio_unit_description);

// Create a Voice Processing IO audio unit.

OSStatus result = noErr;

result = AudioComponentInstanceNew(found_vpio_unit_ref, &vpio_unit_);

if (result != noErr) {

vpio_unit_ = nullptr;

RTCLogError(@"AudioComponentInstanceNew failed. Error=%ld.", (long)result);

return false;

}

// 2. 查看是否支持AGC,webRTC中封装在GetAGCState方法中

UInt32 size = sizeof(*enabled);

OSStatus result = AudioUnitGetProperty(vpio_unit_,

kAUVoiceIOProperty_VoiceProcessingEnableAGC,

kAudioUnitScope_Global,

kInputBus,

enabled,

&size);

RTCLog(@"VPIO unit AGC: %u", static_cast(*enabled));

// 设置开启AGC

UInt32 enable_agc = 1;

result =

AudioUnitSetProperty(vpio_unit_,

kAUVoiceIOProperty_VoiceProcessingEnableAGC,

kAudioUnitScope_Global, kInputBus, &enable_agc,

sizeof(enable_agc));

if (result != noErr) {

RTCLogError(@"Failed to enable the built-in AGC. "

"Error=%ld.",

(long)result);

RTC_HISTOGRAM_COUNTS_SPARSE_100000(

"WebRTC.Audio.SetAGCStateErrorCode", (-1) * result);

}

//3. 初始化AudioUnit

AudioUnitInitialize(vpio_unit_);

1.2 设置采集回调

在直播录制中比较关键的一步就是Render Callback Function(webrtc中对应设置的方法为OnDeliverRecordedData)。AudioUnit每次都是处理一段音频数据,每次处理完成一段数据的时候,这个回调函数就会被调用一次。 代码摘自webrtc:

/ Disable AU buffer allocation for the recorder, we allocate our own.

// TODO(henrika): not sure that it actually saves resource to make this call.

UInt32 flag = 0;

result = AudioUnitSetProperty(

vpio_unit_, kAudioUnitProperty_ShouldAllocateBuffer,

kAudioUnitScope_Output, kInputBus, &flag, sizeof(flag));

if (result != noErr) {

DisposeAudioUnit();

RTCLogError(@"Failed to disable buffer allocation on the input bus. "

"Error=%ld.",

(long)result);

return false;

}

// Specify the callback to be called by the I/O thread to us when input audio

// is available. The recorded samples can then be obtained by calling the

// AudioUnitRender() method.

AURenderCallbackStruct input_callback;

input_callback.inputProc = OnDeliverRecordedData;

input_callback.inputProcRefCon = this;

result = AudioUnitSetProperty(vpio_unit_,

kAudioOutputUnitProperty_SetInputCallback,

kAudioUnitScope_Global, kInputBus,

&input_callback, sizeof(input_callback));

if (result != noErr) {

DisposeAudioUnit();

RTCLogError(@"Failed to specify the input callback on the input bus. "

"Error=%ld.",

(long)result);

return false;

}

1.3 获取pcm数据

WebRTC 禁用了 AudioUnit 为采集数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值