@(webrtc)[webrtc, congestion control]
Webrtc delay-base-bwe代码分析(5): AimdRateControl模块
0. 简介
这个模块是根据OveruseDetector模块计算出来的状态来维护码率控制模块的自动状态机,并更新估算出来的对端发送速率,提供给REMB进行反馈。
1. 原理
一共维持三个状态,增长、保持、衰减,状态转换根据OveruseDetector的三个状态(Normal, Overuse, Underuse)来进行判断。
- 当Overuse发生时,无论什么状态都进入衰减。
- 当Underuse发生时,无论什么状态都进入保持状态。
- 在保持和增长阶段,Normal状态将保持继续增长。
- 在衰减阶段,Normal状态会将状态拉回保持状态。
2. 代码
核心函数为ChangeBitrate,其他部分代码比较简单这里不贴了。
uint32_t AimdRateControl::ChangeBitrate(uint32_t current_bitrate_bps,
uint32_t incoming_bitrate_bps,
int64_t now_ms) {
// 在调用函数update更新对应的链路状态估计,累积码率,噪声值后
// 会将updated置位,如果没置位则不会去更新码率。
if (!updated_) {
return current_bitrate_bps_;
}
// An over-use should always trigger us to reduce the bitrate, even though
// we have not yet established our first estimate. By acting on the over-use,
// we will end up with a valid estimate.
// 初始化未完成,如果不是一开始就Overuse,直接返回初始的码率即可。
if (!bitrate_is_initialized_ && current_input_.bw_state != kBwOverusing)
return current_bitrate_bps_;
updated_ = false;
// 这里对状态进行转换,这个函数是状态机状态转换函数
// 1. Underuse总是进入Hold状态。
// 2. Overuse总是进入Dec状态。
// 3. Normal状态维持,除非当前在Hold状态,此时会进入Inc状态。
ChangeState(current_input_, now_ms);
// Calculated here because it's used in multiple places.
const float incoming_bitrate_kbps = incoming_bitrate_bps / 1000.0f;
// Calculate the max bit rate std dev given the normalized
// variance and the current incoming bit rate.
const float std_max_bit_rate =