Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1120)

Side by Side Diff: webrtc/modules/congestion_controller/median_slope_estimator.cc

Issue 2512693002: Implement Theil-Sen's method for fitting a line to noisy data (used in bandwidth estimation). (Closed)
Patch Set: Unit test for return value of PercentileFilter::Erase Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "webrtc/modules/congestion_controller/median_slope_estimator.h"
12
13 #include <algorithm>
14 #include <vector>
15
16 #include "webrtc/base/logging.h"
17 #include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h"
18 #include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h"
19
20 namespace webrtc {
21
22 constexpr unsigned int kDeltaCounterMax = 1000;
23
24 MedianSlopeEstimator::MedianSlopeEstimator(size_t window_size,
25 double threshold_gain)
26 : window_size_(window_size),
27 threshold_gain_(threshold_gain),
28 num_of_deltas_(0),
29 accumulated_delay_(0),
30 delay_hist_(),
31 median_filter_(0.5),
32 trendline_(0) {}
33
34 MedianSlopeEstimator::~MedianSlopeEstimator() {}
35
36 void MedianSlopeEstimator::Update(double recv_delta_ms,
37 double send_delta_ms,
38 double now_ms) {
39 const double delta_ms = recv_delta_ms - send_delta_ms;
40 ++num_of_deltas_;
41 if (num_of_deltas_ > kDeltaCounterMax) {
42 num_of_deltas_ = kDeltaCounterMax;
43 }
44
45 accumulated_delay_ += delta_ms;
46 BWE_TEST_LOGGING_PLOT(1, "accumulated_delay_ms", now_ms, accumulated_delay_);
47
48 // If the window is full, remove the |window_size_| - 1 slopes that belong to
49 // the oldest point.
50 if (delay_hist_.size() == window_size_) {
51 bool success = true;
52 for (double slope : delay_hist_.front().slopes) {
53 if (!median_filter_.Erase(slope)) {
54 success = false;
55 break;
56 }
57 }
58 delay_hist_.pop_front();
59 if (!success) {
60 // We're erasing previously inserted elements from a multiset, which
magjed_webrtc 2016/12/08 09:31:15 Since you don't recompute the values, I don't see
terelius 2016/12/08 10:47:13 It is basically a question of whether we prefer to
magjed_webrtc 2016/12/08 11:52:30 I still don't understand why precision or the C++
61 // should always succeed. However, there are some intricacies related
62 // to extended precision of floating point registers, so we still want
63 // to verify that the operation succeeds. See comment at insertion below.
64 LOG(LS_ERROR) << "Failed to erase previously inserted slope. This is "
65 "a bug, likely related to rounding.";
66 RTC_NOTREACHED(); // Terminate the program in debug versions.
67 median_filter_.Clear(); // Reset the filter in production versions.
68 delay_hist_.clear();
69 }
70 }
71 // Add |window_size_| - 1 new slopes.
72 for (auto& old_delay : delay_hist_) {
73 if (now_ms - old_delay.time != 0) {
74 // The C99 standard explicitly states that casts and assignments must
75 // perform the associated conversions. This means that |slope| will be
76 // a 64-bit double even if the division is computed using, e.g., 80-bit
77 // extended precision. I believe this also holds in C++ even though the
78 // C++11 standard isn't as explicit. Furthermore, there are good reasons
79 // to believe that compilers couldn't perform optimizations that break
80 // this assumption even if they wanted to.
81 double slope =
82 (accumulated_delay_ - old_delay.delay) / (now_ms - old_delay.time);
83 median_filter_.Insert(slope);
84 // We want to avoid issues with different rounding mode / precision
85 // which we might get if we recomputed the slope when we remove it.
86 old_delay.slopes.push_back(slope);
87 }
88 }
89 delay_hist_.emplace_back(now_ms, accumulated_delay_, window_size_ - 1);
90 // Recompute the median slope.
91 if (delay_hist_.size() == window_size_)
92 trendline_ = median_filter_.GetPercentileValue();
93
94 BWE_TEST_LOGGING_PLOT(1, "trendline_slope", now_ms, trendline_);
95 }
96
97 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698