Index: webrtc/modules/remote_bitrate_estimator/overuse_estimator.cc |
diff --git a/webrtc/modules/remote_bitrate_estimator/overuse_estimator.cc b/webrtc/modules/remote_bitrate_estimator/overuse_estimator.cc |
index 4be7b7493b845945937765979797c250e53f229d..a9d3bfe609d2c69b05a8ae4f0e5393ae6a3404b0 100644 |
--- a/webrtc/modules/remote_bitrate_estimator/overuse_estimator.cc |
+++ b/webrtc/modules/remote_bitrate_estimator/overuse_estimator.cc |
@@ -16,6 +16,7 @@ |
#include <stdlib.h> |
#include <string.h> |
+#include "webrtc/base/checks.h" |
#include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h" |
#include "webrtc/system_wrappers/include/logging.h" |
@@ -24,33 +25,25 @@ namespace webrtc { |
enum { kMinFramePeriodHistoryLength = 60 }; |
enum { kDeltaCounterMax = 1000 }; |
-OveruseEstimator::OveruseEstimator(const OverUseDetectorOptions& options) |
- : options_(options), |
- num_of_deltas_(0), |
- slope_(options_.initial_slope), |
- offset_(options_.initial_offset), |
- prev_offset_(options_.initial_offset), |
- E_(), |
- process_noise_(), |
- avg_noise_(options_.initial_avg_noise), |
- var_noise_(options_.initial_var_noise), |
- ts_delta_hist_() { |
- memcpy(E_, options_.initial_e, sizeof(E_)); |
- memcpy(process_noise_, options_.initial_process_noise, |
- sizeof(process_noise_)); |
-} |
+OveruseEstimator::OveruseEstimator() |
+ : num_of_deltas_(0), |
+ offset_(0), |
+ prev_offset_(offset_), |
+ e_(0.1), |
+ process_noise_(1e-2), |
+ avg_noise_(0), |
+ var_noise_(50), |
+ send_delta_history_() {} |
OveruseEstimator::~OveruseEstimator() { |
- ts_delta_hist_.clear(); |
+ send_delta_history_.clear(); |
} |
-void OveruseEstimator::Update(int64_t t_delta, |
- double ts_delta, |
- int size_delta, |
+void OveruseEstimator::Update(double recv_delta_ms, |
+ double send_delta_ms, |
BandwidthUsage current_hypothesis) { |
- const double min_frame_period = UpdateMinFramePeriod(ts_delta); |
- const double t_ts_delta = t_delta - ts_delta; |
- double fs_delta = size_delta; |
+ const double min_frame_period = UpdateMinFramePeriod(send_delta_ms); |
+ const double delta_ms = recv_delta_ms - send_delta_ms; |
++num_of_deltas_; |
if (num_of_deltas_ > kDeltaCounterMax) { |
@@ -58,19 +51,14 @@ void OveruseEstimator::Update(int64_t t_delta, |
} |
// Update the Kalman filter. |
- E_[0][0] += process_noise_[0]; |
- E_[1][1] += process_noise_[1]; |
+ e_ += process_noise_; |
if ((current_hypothesis == kBwOverusing && offset_ < prev_offset_) || |
(current_hypothesis == kBwUnderusing && offset_ > prev_offset_)) { |
- E_[1][1] += 10 * process_noise_[1]; |
+ e_ += 10 * process_noise_; |
} |
- const double h[2] = {fs_delta, 1.0}; |
- const double Eh[2] = {E_[0][0]*h[0] + E_[0][1]*h[1], |
- E_[1][0]*h[0] + E_[1][1]*h[1]}; |
- |
- const double residual = t_ts_delta - slope_*h[0] - offset_; |
+ const double residual = delta_ms - offset_; |
const bool in_stable_state = (current_hypothesis == kBwNormal); |
const double max_residual = 3.0 * sqrt(var_noise_); |
@@ -82,66 +70,47 @@ void OveruseEstimator::Update(int64_t t_delta, |
UpdateNoiseEstimate(residual < 0 ? -max_residual : max_residual, |
min_frame_period, in_stable_state); |
} |
- |
- const double denom = var_noise_ + h[0]*Eh[0] + h[1]*Eh[1]; |
- |
- const double K[2] = {Eh[0] / denom, |
- Eh[1] / denom}; |
- |
- const double IKh[2][2] = {{1.0 - K[0]*h[0], -K[0]*h[1]}, |
- {-K[1]*h[0], 1.0 - K[1]*h[1]}}; |
- const double e00 = E_[0][0]; |
- const double e01 = E_[0][1]; |
+ const double k = e_ / (var_noise_ + e_); |
// Update state. |
- E_[0][0] = e00 * IKh[0][0] + E_[1][0] * IKh[0][1]; |
- E_[0][1] = e01 * IKh[0][0] + E_[1][1] * IKh[0][1]; |
- E_[1][0] = e00 * IKh[1][0] + E_[1][0] * IKh[1][1]; |
- E_[1][1] = e01 * IKh[1][0] + E_[1][1] * IKh[1][1]; |
- |
- // The covariance matrix must be positive semi-definite. |
- bool positive_semi_definite = E_[0][0] + E_[1][1] >= 0 && |
- E_[0][0] * E_[1][1] - E_[0][1] * E_[1][0] >= 0 && E_[0][0] >= 0; |
- assert(positive_semi_definite); |
- if (!positive_semi_definite) { |
- LOG(LS_ERROR) << "The over-use estimator's covariance matrix is no longer " |
- "semi-definite."; |
- } |
+ e_ = e_ * (1.0 - k); |
+ |
+ // The covariance matrix must be positive. |
+ RTC_DCHECK(e_ >= 0.0); |
+ if (e_ < 0) |
+ LOG(LS_ERROR) << "The over-use estimator's covariance is negative!"; |
- slope_ = slope_ + K[0] * residual; |
- prev_offset_ = offset_; |
- offset_ = offset_ + K[1] * residual; |
+ offset_ = offset_ + k * residual; |
} |
-double OveruseEstimator::UpdateMinFramePeriod(double ts_delta) { |
- double min_frame_period = ts_delta; |
- if (ts_delta_hist_.size() >= kMinFramePeriodHistoryLength) { |
- ts_delta_hist_.pop_front(); |
+double OveruseEstimator::UpdateMinFramePeriod(double send_delta_ms) { |
+ double min_frame_period = send_delta_ms; |
+ if (send_delta_history_.size() >= kMinFramePeriodHistoryLength) { |
+ send_delta_history_.pop_front(); |
} |
- std::list<double>::iterator it = ts_delta_hist_.begin(); |
- for (; it != ts_delta_hist_.end(); it++) { |
- min_frame_period = std::min(*it, min_frame_period); |
+ for (double delta_ms : send_delta_history_) { |
+ min_frame_period = std::min(delta_ms, min_frame_period); |
} |
- ts_delta_hist_.push_back(ts_delta); |
+ send_delta_history_.push_back(send_delta_ms); |
return min_frame_period; |
} |
void OveruseEstimator::UpdateNoiseEstimate(double residual, |
- double ts_delta, |
+ double send_delta_ms, |
bool stable_state) { |
if (!stable_state) { |
return; |
} |
// Faster filter during startup to faster adapt to the jitter level |
// of the network. |alpha| is tuned for 30 frames per second, but is scaled |
- // according to |ts_delta|. |
+ // according to |send_delta_ms|. |
double alpha = 0.01; |
if (num_of_deltas_ > 10*30) { |
alpha = 0.002; |
} |
// Only update the noise estimate if we're not over-using. |beta| is a |
// function of alpha and the time delta since the previous update. |
- const double beta = pow(1 - alpha, ts_delta * 30.0 / 1000.0); |
+ const double beta = pow(1 - alpha, send_delta_ms * 30.0 / 1000.0); |
avg_noise_ = beta * avg_noise_ |
+ (1 - beta) * residual; |
var_noise_ = beta * var_noise_ |