webrtc网络排队趋势推测器(TrendlineEstimator),用于推测网络的延迟趋势,延迟值。
用于计算直线 y=ax+b
-
创建
// window_size: 窗口大小,数据点数组的大小,如果数据过多则删除老的数据
// smoothing_coef :平滑输入y的因子 , (使用滑动平滑算法,相当把 1/smoothing_coef 个数据点进行 行平均 )
// threshold_gain:
TrendlineEstimator(size_t window_size, double smoothing_coef, double threshold_gain);
- 更新窗口数据,计算趋势
recv_delta_ms: 接收端2帧数据的相对延迟
send_delta_ms:发送端2帧数据的相对延迟
arrival_time_ms:接收的本地时间
void Update(double recv_delta_ms, double send_delta_ms,int64_t arrival_time_ms){ //延迟值 const double delta_ms = recv_delta_ms - send_delta_ms; ++num_of_deltas_; if (num_of_deltas_ > kDeltaCounterMax) num_of_deltas_ = kDeltaCounterMax; if (first_arrival_time_ms == -1) first_arrival_time_ms = arrival_time_ms; // Exponential backoff filter. //平滑accumulated_delay_数据到smoothed_delay_ //防止输入一些大的波动点,无效值等 accumulated_delay_ += delta_ms; //滑动平均平滑算法 smoothed_delay_ = smoothing_coef_ * smoothed_delay_ +(1 - smoothing_coef_) *accumulated_delay_; //保存所有点 (x,y) // x :本地时间 // y :延迟值 // delay_hist_.push_back(std::make_pair( static_cast<double>(arrival_time_ms - first_arrival_time_ms), smoothed_delay_)); if (delay_hist_.size() > window_size_) delay_hist_.pop_front(); if (delay_hist_.size() == window_size_) { //通过线性回归计算趋势 (斜率) // y=ax+b // trendline_:就是我们需要计算的a trendline_ = LinearFitSlope(delay_hist_).value_or(trendline_); } }
- 通过最小二乘法进行线性回归,返回直线斜率
y=ax+b 返回a
rtc::Optional LinearFitSlope(const std::deque<std::pair<double, double>>& points) {
//计算x,y的均值
double sum_x = 0;
double sum_y = 0;
for (const auto& point : points) {
sum_x += point.first;
sum_y += point.second;
}
double x_avg = sum_x / points.size();
double y_avg = sum_y / points.size();// Compute the slope k = \sum (x_i-x_avg)(y_i-y_avg) / \sum (x_i-x_avg)^2 //最小二乘法 线性回归 // //计算斜率a sum (x_i-x_avg)(y_i-y_avg) / sum (x_i-x_avg)^2 // // x,y,xy,x^2 double numerator = 0; double denominator = 0; for (const auto& point : points) { numerator += (point.first - x_avg) * (point.second - y_avg); denominator += (point.first - x_avg) * (point.first - x_avg); } if (denominator == 0) return rtc::Optional<double>(); return rtc::Optional<double>(numerator / denominator); }
-
获取 斜率*threshold_gain_
我的理解threshold_gain_:相当于数据点间的理想间隔trendline_: 便是网络延迟的变化趋势
0 表示网络延迟在增加
=0 网络络延迟保持不变
<0 网络延迟在减少
double trendline_slope() const {
return trendline_ * threshold_gain_;
}
- y=ax y表示延迟 ,x表示时间点
在4函数中,返回了 a*threshold_gain_ ,我的理解是一个时间点延迟的大小 , 假设了理想的时间点输入间隔threshold_gain_毫秒