Line data Source code
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 0 : MedianSlopeEstimator::MedianSlopeEstimator(size_t window_size,
25 0 : 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 0 : trendline_(0) {}
33 :
34 0 : MedianSlopeEstimator::~MedianSlopeEstimator() {}
35 :
36 0 : void MedianSlopeEstimator::Update(double recv_delta_ms,
37 : double send_delta_ms,
38 : int64_t arrival_time_ms) {
39 0 : const double delta_ms = recv_delta_ms - send_delta_ms;
40 0 : ++num_of_deltas_;
41 0 : if (num_of_deltas_ > kDeltaCounterMax)
42 0 : num_of_deltas_ = kDeltaCounterMax;
43 :
44 0 : accumulated_delay_ += delta_ms;
45 : BWE_TEST_LOGGING_PLOT(1, "accumulated_delay_ms", arrival_time_ms,
46 : accumulated_delay_);
47 :
48 : // If the window is full, remove the |window_size_| - 1 slopes that belong to
49 : // the oldest point.
50 0 : if (delay_hist_.size() == window_size_) {
51 0 : for (double slope : delay_hist_.front().slopes) {
52 0 : const bool success = median_filter_.Erase(slope);
53 0 : RTC_CHECK(success);
54 : }
55 0 : delay_hist_.pop_front();
56 : }
57 : // Add |window_size_| - 1 new slopes.
58 0 : for (auto& old_delay : delay_hist_) {
59 0 : if (arrival_time_ms - old_delay.time != 0) {
60 : // The C99 standard explicitly states that casts and assignments must
61 : // perform the associated conversions. This means that |slope| will be
62 : // a 64-bit double even if the division is computed using, e.g., 80-bit
63 : // extended precision. I believe this also holds in C++ even though the
64 : // C++11 standard isn't as explicit. Furthermore, there are good reasons
65 : // to believe that compilers couldn't perform optimizations that break
66 : // this assumption even if they wanted to.
67 0 : double slope = (accumulated_delay_ - old_delay.delay) /
68 0 : static_cast<double>(arrival_time_ms - old_delay.time);
69 0 : median_filter_.Insert(slope);
70 : // We want to avoid issues with different rounding mode / precision
71 : // which we might get if we recomputed the slope when we remove it.
72 0 : old_delay.slopes.push_back(slope);
73 : }
74 : }
75 0 : delay_hist_.emplace_back(arrival_time_ms, accumulated_delay_,
76 0 : window_size_ - 1);
77 : // Recompute the median slope.
78 0 : if (delay_hist_.size() == window_size_)
79 0 : trendline_ = median_filter_.GetPercentileValue();
80 :
81 : BWE_TEST_LOGGING_PLOT(1, "trendline_slope", arrival_time_ms, trendline_);
82 0 : }
83 :
84 : } // namespace webrtc
|