Unverified 提交 abfd942f authored 作者: Gustavo Garcia's avatar Gustavo Garcia 提交者: GitHub

Upgrade trendline estimator to improve low bandwidth conditions (#1055)

* Uprade trendline estimator to improve low bandwidth conditions

* Missing constant change
上级 937bb6c8
...@@ -61,7 +61,7 @@ DelayBasedBwe::DelayBasedBwe(const WebRtcKeyValueConfig* key_value_config, ...@@ -61,7 +61,7 @@ DelayBasedBwe::DelayBasedBwe(const WebRtcKeyValueConfig* key_value_config,
network_state_predictor_(network_state_predictor), network_state_predictor_(network_state_predictor),
inter_arrival_(), inter_arrival_(),
delay_detector_( delay_detector_(
new TrendlineEstimator(key_value_config_, network_state_predictor_)), new TrendlineEstimator(network_state_predictor_)),
last_seen_packet_(Timestamp::MinusInfinity()), last_seen_packet_(Timestamp::MinusInfinity()),
uma_recorded_(false), uma_recorded_(false),
rate_control_(key_value_config, /*send_side=*/true), rate_control_(key_value_config, /*send_side=*/true),
...@@ -127,7 +127,7 @@ void DelayBasedBwe::IncomingPacketFeedback(const PacketResult& packet_feedback, ...@@ -127,7 +127,7 @@ void DelayBasedBwe::IncomingPacketFeedback(const PacketResult& packet_feedback,
new InterArrival((kTimestampGroupLengthMs << kInterArrivalShift) / 1000, new InterArrival((kTimestampGroupLengthMs << kInterArrivalShift) / 1000,
kTimestampToMs, true)); kTimestampToMs, true));
delay_detector_.reset( delay_detector_.reset(
new TrendlineEstimator(key_value_config_, network_state_predictor_)); new TrendlineEstimator(network_state_predictor_));
} }
last_seen_packet_ = at_time; last_seen_packet_ = at_time;
......
...@@ -13,96 +13,92 @@ ...@@ -13,96 +13,92 @@
#include "modules/congestion_controller/goog_cc/trendline_estimator.h" #include "modules/congestion_controller/goog_cc/trendline_estimator.h"
#include "modules/remote_bitrate_estimator/include/bwe_defines.h"
#include "rtc_base/numerics/safe_minmax.h"
#include "Logger.hpp"
#include <absl/types/optional.h>
#include <math.h> #include <math.h>
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include "absl/strings/match.h"
#include "absl/types/optional.h"
#include "api/network_state_predictor.h"
#include "rtc_base/numerics/safe_minmax.h"
#include "api/transport/webrtc_key_value_config.h"
#include "Logger.hpp"
namespace webrtc { namespace webrtc {
namespace { namespace {
// Parameters for linear least squares fit of regression line to noisy data. // Parameters for linear least squares fit of regression line to noisy data.
constexpr size_t kDefaultTrendlineWindowSize = 20; constexpr double kDefaultTrendlineSmoothingCoeff = 0.9;
constexpr double kDefaultTrendlineSmoothingCoeff = 0.6;
constexpr double kDefaultTrendlineThresholdGain = 4.0; constexpr double kDefaultTrendlineThresholdGain = 4.0;
const char kBweWindowSizeInPacketsExperiment[] =
"WebRTC-BweWindowSizeInPackets";
size_t ReadTrendlineFilterWindowSize(
const WebRtcKeyValueConfig* key_value_config) {
std::string experiment_string =
key_value_config->Lookup(kBweWindowSizeInPacketsExperiment);
size_t window_size;
int parsed_values =
sscanf(experiment_string.c_str(), "Enabled-%zu", &window_size);
if (parsed_values == 1) {
if (window_size > 1)
return window_size;
MS_WARN_DEV("window size must be greater than 1");
}
MS_WARN_DEV(
"failed to parse parameters for BweWindowSizeInPackets"
" experiment from field trial string, using default");
return kDefaultTrendlineWindowSize;
}
absl::optional<double> LinearFitSlope( absl::optional<double> LinearFitSlope(
const std::deque<std::pair<double, double>>& points) { const std::deque<TrendlineEstimator::PacketTiming>& packets) {
//RTC_DCHECK(points.size() >= 2); // RTC_DCHECK(packets.size() >= 2);
// Compute the "center of mass". // Compute the "center of mass".
double sum_x = 0; double sum_x = 0;
double sum_y = 0; double sum_y = 0;
for (const auto& point : points) { for (const auto& packet : packets) {
sum_x += point.first; sum_x += packet.arrival_time_ms;
sum_y += point.second; sum_y += packet.smoothed_delay_ms;
} }
double x_avg = sum_x / points.size(); double x_avg = sum_x / packets.size();
double y_avg = sum_y / points.size(); double y_avg = sum_y / packets.size();
// Compute the slope k = \sum (x_i-x_avg)(y_i-y_avg) / \sum (x_i-x_avg)^2 // Compute the slope k = \sum (x_i-x_avg)(y_i-y_avg) / \sum (x_i-x_avg)^2
double numerator = 0; double numerator = 0;
double denominator = 0; double denominator = 0;
for (const auto& point : points) { for (const auto& packet : packets) {
numerator += (point.first - x_avg) * (point.second - y_avg); double x = packet.arrival_time_ms;
denominator += (point.first - x_avg) * (point.first - x_avg); double y = packet.smoothed_delay_ms;
numerator += (x - x_avg) * (y - y_avg);
denominator += (x - x_avg) * (x - x_avg);
} }
if (denominator == 0) if (denominator == 0)
return absl::nullopt; return absl::nullopt;
return numerator / denominator; return numerator / denominator;
} }
absl::optional<double> ComputeSlopeCap(
const std::deque<TrendlineEstimator::PacketTiming>& packets,
const TrendlineEstimatorSettings& settings) {
// RTC_DCHECK(1 <= settings.beginning_packets &&
// settings.beginning_packets < packets.size());
// RTC_DCHECK(1 <= settings.end_packets &&
// settings.end_packets < packets.size());
// RTC_DCHECK(settings.beginning_packets + settings.end_packets <=
// packets.size());
TrendlineEstimator::PacketTiming early = packets[0];
for (size_t i = 1; i < settings.beginning_packets; ++i) {
if (packets[i].raw_delay_ms < early.raw_delay_ms)
early = packets[i];
}
size_t late_start = packets.size() - settings.end_packets;
TrendlineEstimator::PacketTiming late = packets[late_start];
for (size_t i = late_start + 1; i < packets.size(); ++i) {
if (packets[i].raw_delay_ms < late.raw_delay_ms)
late = packets[i];
}
if (late.arrival_time_ms - early.arrival_time_ms < 1) {
return absl::nullopt;
}
return (late.raw_delay_ms - early.raw_delay_ms) /
(late.arrival_time_ms - early.arrival_time_ms) +
settings.cap_uncertainty;
}
constexpr double kMaxAdaptOffsetMs = 15.0; constexpr double kMaxAdaptOffsetMs = 15.0;
constexpr double kOverUsingTimeThreshold = 30; constexpr double kOverUsingTimeThreshold = 10;
constexpr int kMinNumDeltas = 60; constexpr int kMinNumDeltas = 60;
constexpr int kDeltaCounterMax = 1000; constexpr int kDeltaCounterMax = 1000;
} // namespace } // namespace
TrendlineEstimator::TrendlineEstimator( TrendlineEstimator::TrendlineEstimator(
const WebRtcKeyValueConfig* key_value_config,
NetworkStatePredictor* network_state_predictor) NetworkStatePredictor* network_state_predictor)
: TrendlineEstimator( : smoothing_coef_(kDefaultTrendlineSmoothingCoeff),
key_value_config->Lookup(kBweWindowSizeInPacketsExperiment) threshold_gain_(kDefaultTrendlineThresholdGain),
.find("Enabled") == 0
? ReadTrendlineFilterWindowSize(key_value_config)
: kDefaultTrendlineWindowSize,
kDefaultTrendlineSmoothingCoeff,
kDefaultTrendlineThresholdGain,
network_state_predictor) {}
TrendlineEstimator::TrendlineEstimator(
size_t window_size,
double smoothing_coef,
double threshold_gain,
NetworkStatePredictor* network_state_predictor)
: window_size_(window_size),
smoothing_coef_(smoothing_coef),
threshold_gain_(threshold_gain),
num_of_deltas_(0), num_of_deltas_(0),
first_arrival_time_ms_(-1), first_arrival_time_ms_(-1),
accumulated_delay_(0), accumulated_delay_(0),
...@@ -120,63 +116,75 @@ TrendlineEstimator::TrendlineEstimator( ...@@ -120,63 +116,75 @@ TrendlineEstimator::TrendlineEstimator(
hypothesis_(BandwidthUsage::kBwNormal), hypothesis_(BandwidthUsage::kBwNormal),
hypothesis_predicted_(BandwidthUsage::kBwNormal), hypothesis_predicted_(BandwidthUsage::kBwNormal),
network_state_predictor_(network_state_predictor) { network_state_predictor_(network_state_predictor) {
MS_DEBUG_DEV(
"using Trendline filter for delay change estimation with window size: %zu",
window_size_);
} }
TrendlineEstimator::~TrendlineEstimator() {} TrendlineEstimator::~TrendlineEstimator() {}
void TrendlineEstimator::UpdateTrendline(double recv_delta_ms,
double send_delta_ms,
int64_t send_time_ms,
int64_t arrival_time_ms) {
const double delta_ms = recv_delta_ms - send_delta_ms;
++num_of_deltas_;
num_of_deltas_ = std::min(num_of_deltas_, kDeltaCounterMax);
if (first_arrival_time_ms_ == -1)
first_arrival_time_ms_ = arrival_time_ms;
// Exponential backoff filter.
accumulated_delay_ += delta_ms;
// BWE_TEST_LOGGING_PLOT(1, "accumulated_delay_ms", arrival_time_ms,
// accumulated_delay_);
smoothed_delay_ = smoothing_coef_ * smoothed_delay_ +
(1 - smoothing_coef_) * accumulated_delay_;
// BWE_TEST_LOGGING_PLOT(1, "smoothed_delay_ms", arrival_time_ms,
// smoothed_delay_);
// Maintain packet window
delay_hist_.emplace_back(
static_cast<double>(arrival_time_ms - first_arrival_time_ms_),
smoothed_delay_, accumulated_delay_);
if (settings_.enable_sort) {
for (size_t i = delay_hist_.size() - 1;
i > 0 &&
delay_hist_[i].arrival_time_ms < delay_hist_[i - 1].arrival_time_ms;
--i) {
std::swap(delay_hist_[i], delay_hist_[i - 1]);
}
}
if (delay_hist_.size() > settings_.window_size)
delay_hist_.pop_front();
// Simple linear regression.
double trend = prev_trend_;
if (delay_hist_.size() == settings_.window_size) {
// Update trend_ if it is possible to fit a line to the data. The delay
// trend can be seen as an estimate of (send_rate - capacity)/capacity.
// 0 < trend < 1 -> the delay increases, queues are filling up
// trend == 0 -> the delay does not change
// trend < 0 -> the delay decreases, queues are being emptied
trend = LinearFitSlope(delay_hist_).value_or(trend);
if (settings_.enable_cap) {
absl::optional<double> cap = ComputeSlopeCap(delay_hist_, settings_);
// We only use the cap to filter out overuse detections, not
// to detect additional underuses.
if (trend >= 0 && cap.has_value() && trend > cap.value()) {
trend = cap.value();
}
}
}
// BWE_TEST_LOGGING_PLOT(1, "trendline_slope", arrival_time_ms, trend);
Detect(trend, send_delta_ms, arrival_time_ms);
}
void TrendlineEstimator::Update(double recv_delta_ms, void TrendlineEstimator::Update(double recv_delta_ms,
double send_delta_ms, double send_delta_ms,
int64_t send_time_ms, int64_t send_time_ms,
int64_t arrival_time_ms, int64_t arrival_time_ms,
bool calculated_deltas) { bool calculated_deltas) {
if (calculated_deltas) { if (calculated_deltas) {
const double delta_ms = recv_delta_ms - send_delta_ms; UpdateTrendline(recv_delta_ms, send_delta_ms, send_time_ms, arrival_time_ms);
++num_of_deltas_;
num_of_deltas_ = std::min(num_of_deltas_, kDeltaCounterMax);
if (first_arrival_time_ms_ == -1)
first_arrival_time_ms_ = arrival_time_ms;
// Exponential backoff filter.
accumulated_delay_ += delta_ms;
// BWE_TEST_LOGGING_PLOT(1, "accumulated_delay_ms", arrival_time_ms,
// accumulated_delay_);
// smoothed_delay_ = smoothing_coef_ * smoothed_delay_ +
// (1 - smoothing_coef_) * accumulated_delay_;
// MS_NOTE: Apply WEMA to the current delta_ms. Don't consider the
// accumulated delay. Tests show it behaves more robustly upon delta peaks.
smoothed_delay_ = smoothing_coef_ * delta_ms +
(1 - smoothing_coef_) * smoothed_delay_;
// BWE_TEST_LOGGING_PLOT(1, "smoothed_delay_ms", arrival_time_ms,
// smoothed_delay_);
// Simple linear regression.
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();
double trend = prev_trend_;
if (delay_hist_.size() == window_size_) {
// Update trend_ if it is possible to fit a line to the data. The delay
// trend can be seen as an estimate of (send_rate - capacity)/capacity.
// 0 < trend < 1 -> the delay increases, queues are filling up
// trend == 0 -> the delay does not change
// trend < 0 -> the delay decreases, queues are being emptied
trend = LinearFitSlope(delay_hist_).value_or(trend);
}
// BWE_TEST_LOGGING_PLOT(1, "trendline_slope", arrival_time_ms, trend);
MS_DEBUG_DEV("trend:%f, send_delta_ms:%f, recv_delta_ms:%f, delta_ms:%f arrival_time_ms:%" PRIi64 ", accumulated_delay_:%f, smoothed_delay_:%f", trend, send_delta_ms, recv_delta_ms, delta_ms, arrival_time_ms, accumulated_delay_, smoothed_delay_);
Detect(trend, send_delta_ms, arrival_time_ms);
}
else {
MS_DEBUG_DEV("no calculated deltas");
} }
if (network_state_predictor_) { if (network_state_predictor_) {
hypothesis_predicted_ = network_state_predictor_->Update( hypothesis_predicted_ = network_state_predictor_->Update(
send_time_ms, arrival_time_ms, hypothesis_); send_time_ms, arrival_time_ms, hypothesis_);
...@@ -212,6 +220,7 @@ void TrendlineEstimator::Detect(double trend, double ts_delta, int64_t now_ms) { ...@@ -212,6 +220,7 @@ void TrendlineEstimator::Detect(double trend, double ts_delta, int64_t now_ms) {
if (trend >= prev_trend_) { if (trend >= prev_trend_) {
time_over_using_ = 0; time_over_using_ = 0;
overuse_counter_ = 0; overuse_counter_ = 0;
hypothesis_ = BandwidthUsage::kBwOverusing;
MS_DEBUG_DEV("hypothesis_: BandwidthUsage::kBwOverusing"); MS_DEBUG_DEV("hypothesis_: BandwidthUsage::kBwOverusing");
#if MS_LOG_DEV_LEVEL == 3 #if MS_LOG_DEV_LEVEL == 3
...@@ -219,8 +228,6 @@ void TrendlineEstimator::Detect(double trend, double ts_delta, int64_t now_ms) { ...@@ -219,8 +228,6 @@ void TrendlineEstimator::Detect(double trend, double ts_delta, int64_t now_ms) {
MS_DEBUG_DEV("arrival_time_ms - first_arrival_time_ms_:%f, smoothed_delay_:%f", kv.first, kv.second); MS_DEBUG_DEV("arrival_time_ms - first_arrival_time_ms_:%f, smoothed_delay_:%f", kv.first, kv.second);
} }
#endif #endif
hypothesis_ = BandwidthUsage::kBwOverusing;
} }
} }
} else if (modified_trend < -threshold_) { } else if (modified_trend < -threshold_) {
...@@ -231,8 +238,8 @@ void TrendlineEstimator::Detect(double trend, double ts_delta, int64_t now_ms) { ...@@ -231,8 +238,8 @@ void TrendlineEstimator::Detect(double trend, double ts_delta, int64_t now_ms) {
} else { } else {
time_over_using_ = -1; time_over_using_ = -1;
overuse_counter_ = 0; overuse_counter_ = 0;
MS_DEBUG_DEV("---- BandwidthUsage::kBwNormal ---");
hypothesis_ = BandwidthUsage::kBwNormal; hypothesis_ = BandwidthUsage::kBwNormal;
MS_DEBUG_DEV("---- BandwidthUsage::kBwNormal ---");
} }
prev_trend_ = trend; prev_trend_ = trend;
UpdateThreshold(modified_trend, now_ms); UpdateThreshold(modified_trend, now_ms);
......
...@@ -10,37 +10,48 @@ ...@@ -10,37 +10,48 @@
#ifndef MODULES_CONGESTION_CONTROLLER_GOOG_CC_TRENDLINE_ESTIMATOR_H_ #ifndef MODULES_CONGESTION_CONTROLLER_GOOG_CC_TRENDLINE_ESTIMATOR_H_
#define MODULES_CONGESTION_CONTROLLER_GOOG_CC_TRENDLINE_ESTIMATOR_H_ #define MODULES_CONGESTION_CONTROLLER_GOOG_CC_TRENDLINE_ESTIMATOR_H_
#include <stddef.h>
#include <stdint.h>
#include <deque>
#include <memory>
#include <utility>
#include "api/network_state_predictor.h" #include "api/network_state_predictor.h"
#include "api/transport/webrtc_key_value_config.h" #include "api/transport/webrtc_key_value_config.h"
#include "modules/congestion_controller/goog_cc/delay_increase_detector_interface.h" #include "modules/congestion_controller/goog_cc/delay_increase_detector_interface.h"
#include "modules/remote_bitrate_estimator/include/bwe_defines.h" #include "modules/remote_bitrate_estimator/include/bwe_defines.h"
#include "rtc_base/constructor_magic.h" #include "rtc_base/constructor_magic.h"
#include <stddef.h>
#include <stdint.h>
#include <deque>
#include <utility>
namespace webrtc { namespace webrtc {
struct TrendlineEstimatorSettings {
static constexpr unsigned kDefaultTrendlineWindowSize = 20;
// Sort the packets in the window. Should be redundant,
// but then almost no cost.
bool enable_sort = false;
// Cap the trendline slope based on the minimum delay seen
// in the beginning_packets and end_packets respectively.
bool enable_cap = false;
unsigned beginning_packets = 7;
unsigned end_packets = 7;
double cap_uncertainty = 0.0;
// Size (in packets) of the window.
unsigned window_size = kDefaultTrendlineWindowSize;
};
class TrendlineEstimator : public DelayIncreaseDetectorInterface { class TrendlineEstimator : public DelayIncreaseDetectorInterface {
public: public:
TrendlineEstimator(const WebRtcKeyValueConfig* key_value_config, TrendlineEstimator(NetworkStatePredictor* network_state_predictor);
NetworkStatePredictor* network_state_predictor);
// |window_size| is the number of points required to compute a trend line.
// |smoothing_coef| controls how much we smooth out the delay before fitting
// the trend line. |threshold_gain| is used to scale the trendline slope for
// comparison to the old threshold. Once the old estimator has been removed
// (or the thresholds been merged into the estimators), we can just set the
// threshold instead of setting a gain.|network_state_predictor| is used to
// bettter predict network state.
TrendlineEstimator(size_t window_size,
double smoothing_coef,
double threshold_gain,
NetworkStatePredictor* network_state_predictor);
~TrendlineEstimator() override; ~TrendlineEstimator() override;
TrendlineEstimator(const TrendlineEstimator&) = delete;
TrendlineEstimator& operator=(const TrendlineEstimator&) = delete;
// Update the estimator with a new sample. The deltas should represent deltas // Update the estimator with a new sample. The deltas should represent deltas
// between timestamp groups as defined by the InterArrival class. // between timestamp groups as defined by the InterArrival class.
void Update(double recv_delta_ms, void Update(double recv_delta_ms,
...@@ -49,21 +60,33 @@ class TrendlineEstimator : public DelayIncreaseDetectorInterface { ...@@ -49,21 +60,33 @@ class TrendlineEstimator : public DelayIncreaseDetectorInterface {
int64_t arrival_time_ms, int64_t arrival_time_ms,
bool calculated_deltas) override; bool calculated_deltas) override;
void UpdateTrendline(double recv_delta_ms,
double send_delta_ms,
int64_t send_time_ms,
int64_t arrival_time_ms);
BandwidthUsage State() const override; BandwidthUsage State() const override;
protected: struct PacketTiming {
// Used in unit tests. PacketTiming(double arrival_time_ms,
double modified_trend() const { return prev_trend_ * threshold_gain_; } double smoothed_delay_ms,
double raw_delay_ms)
: arrival_time_ms(arrival_time_ms),
smoothed_delay_ms(smoothed_delay_ms),
raw_delay_ms(raw_delay_ms) {}
double arrival_time_ms;
double smoothed_delay_ms;
double raw_delay_ms;
};
private: private:
friend class GoogCcStatePrinter; friend class GoogCcStatePrinter;
void Detect(double trend, double ts_delta, int64_t now_ms); void Detect(double trend, double ts_delta, int64_t now_ms);
void UpdateThreshold(double modified_offset, int64_t now_ms); void UpdateThreshold(double modified_offset, int64_t now_ms);
// Parameters. // Parameters.
const size_t window_size_; TrendlineEstimatorSettings settings_;
const double smoothing_coef_; const double smoothing_coef_;
const double threshold_gain_; const double threshold_gain_;
// Used by the existing threshold. // Used by the existing threshold.
...@@ -74,7 +97,7 @@ class TrendlineEstimator : public DelayIncreaseDetectorInterface { ...@@ -74,7 +97,7 @@ class TrendlineEstimator : public DelayIncreaseDetectorInterface {
double accumulated_delay_; double accumulated_delay_;
double smoothed_delay_; double smoothed_delay_;
// Linear least squares regression. // Linear least squares regression.
std::deque<std::pair<double, double>> delay_hist_; std::deque<PacketTiming> delay_hist_;
const double k_up_; const double k_up_;
const double k_down_; const double k_down_;
...@@ -88,8 +111,6 @@ class TrendlineEstimator : public DelayIncreaseDetectorInterface { ...@@ -88,8 +111,6 @@ class TrendlineEstimator : public DelayIncreaseDetectorInterface {
BandwidthUsage hypothesis_; BandwidthUsage hypothesis_;
BandwidthUsage hypothesis_predicted_; BandwidthUsage hypothesis_predicted_;
NetworkStatePredictor* network_state_predictor_; NetworkStatePredictor* network_state_predictor_;
RTC_DISALLOW_COPY_AND_ASSIGN(TrendlineEstimator);
}; };
} // namespace webrtc } // namespace webrtc
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论