// Updates history of min bitrates.
// After this method returns min_bitrate_history_.front().second contains the
// min bitrate used during last kBweIncreaseIntervalMs.
// 主要结合这个函数解释下变量min_bitrate_history_
// 这个变量的两个维度,front记录的是离当前最远的时间,
// 每个速率都是按照时间先后顺序逐渐push到尾部。
// 因此更新的时候,需要先将超时的元素从列表头剔除。
// 后一个维度是最小速率值,
// 在相同的时间区间内,保留最小的速率值。
// |-------Interval 1---------|----------Interval 2------|
// | | |
// |--t1 < t2 < t3 < t4 < t5--|--t1 < t2 < t3 < t4 < t5--|
// 这样的操作较为简单,不用在每次插入元素时去判断对应的时间区域,再找到对应时间区间的最小值,用部分冗余的内存换取操作的快捷。
void SendSideBandwidthEstimation::UpdateMinHistory(int64_t now_ms) {
// Remove old data points from history.
// Since history precision is in ms, add one so it is able to increase
// bitrate if it is off by as little as 0.5ms.
while (!min_bitrate_history_.empty() &&
now_ms - min_bitrate_history_.front().first + 1 >
kBweIncreaseIntervalMs) {
min_bitrate_history_.pop_front();
}
// Typical minimum sliding-window algorithm: Pop values higher than current
// bitrate before pushing it.
while (!min_bitrate_history_.empty() &&
bitrate_ <= min_bitrate_history_.back().second) {
min_bitrate_history_.pop_back();
}
min_bitrate_history_.push_back(std::make_pair(now_ms, bitrate_));
}