Index: webrtc/modules/congestion_controller/theil_sen_estimator.cc |
diff --git a/webrtc/modules/congestion_controller/theil_sen_estimator.cc b/webrtc/modules/congestion_controller/theil_sen_estimator.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3405cd82642c292b0157719b0109ac425107e5e1 |
--- /dev/null |
+++ b/webrtc/modules/congestion_controller/theil_sen_estimator.cc |
@@ -0,0 +1,76 @@ |
+/* |
+ * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ */ |
+ |
+#include "webrtc/modules/congestion_controller/theil_sen_estimator.h" |
+ |
+#include <algorithm> |
+#include <vector> |
+ |
+#include "webrtc/base/logging.h" |
+#include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h" |
+#include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h" |
+ |
+namespace webrtc { |
+ |
+constexpr unsigned int kDeltaCounterMax = 1000; |
+ |
+TheilSenEstimator::TheilSenEstimator(size_t window_size, double threshold_gain) |
+ : window_size_(window_size), |
+ threshold_gain_(threshold_gain), |
+ num_of_deltas_(0), |
+ accumulated_delay_(0), |
+ delay_hist_(), |
+ median_filter_(0.5), |
+ trendline_(0) {} |
+ |
+TheilSenEstimator::~TheilSenEstimator() {} |
+ |
+void TheilSenEstimator::Update(double recv_delta_ms, |
+ double send_delta_ms, |
+ double now_ms) { |
+ const double delta_ms = recv_delta_ms - send_delta_ms; |
+ ++num_of_deltas_; |
+ if (num_of_deltas_ > kDeltaCounterMax) { |
+ num_of_deltas_ = kDeltaCounterMax; |
+ } |
+ |
+ accumulated_delay_ += delta_ms; |
+ BWE_TEST_LOGGING_PLOT(1, "accumulated_delay_ms", now_ms, accumulated_delay_); |
+ |
+ // If the window is full, remove the |window_size_| - 1 slopes that belong to |
+ // the oldest point. |
+ if (delay_hist_.size() == window_size_) { |
+ for (double slope : delay_hist_.front().slopes) { |
+ if (!median_filter_.Erase(slope)) { |
+ LOG(LS_ERROR) << "Failed to erase previously inserted slope. This is " |
+ "a bug, likely related to rounding."; |
brandtr
2016/12/05 12:34:45
Remove or update "likely related to rounding."
|
+ RTC_NOTREACHED(); |
+ } |
+ } |
+ delay_hist_.pop_front(); |
+ } |
+ // Add |window_size_| - 1 new slopes. |
+ for (auto& old_delay : delay_hist_) { |
+ if (now_ms - old_delay.time != 0) { |
+ double slope = |
+ (accumulated_delay_ - old_delay.delay) / (now_ms - old_delay.time); |
+ median_filter_.Insert(slope); |
+ old_delay.slopes.push_back(slope); |
+ } |
+ } |
+ delay_hist_.emplace_back(now_ms, accumulated_delay_, window_size_ - 1); |
+ // Recompute the median slope. |
+ if (delay_hist_.size() == window_size_) |
+ trendline_ = median_filter_.GetPercentileValue(); |
+ |
+ BWE_TEST_LOGGING_PLOT(1, "trendline_slope", now_ms, trendline_); |
+} |
+ |
+} // namespace webrtc |