blob: ea762395775614e960be4e3fadba41a292f27757 [file] [log] [blame]
terelius5a388362016-12-09 05:50:01 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/congestion_controller/median_slope_estimator.h"
terelius5a388362016-12-09 05:50:01 -080012
13#include <algorithm>
14#include <vector>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/remote_bitrate_estimator/include/bwe_defines.h"
17#include "modules/remote_bitrate_estimator/test/bwe_test_logging.h"
18#include "rtc_base/logging.h"
terelius5a388362016-12-09 05:50:01 -080019
20namespace webrtc {
21
22constexpr unsigned int kDeltaCounterMax = 1000;
23
24MedianSlopeEstimator::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
34MedianSlopeEstimator::~MedianSlopeEstimator() {}
35
nisse76e62b02017-05-31 02:24:52 -070036MedianSlopeEstimator::DelayInfo::DelayInfo(int64_t time,
37 double delay,
38 size_t slope_count)
39 : time(time), delay(delay) {
40 slopes.reserve(slope_count);
41}
42
43MedianSlopeEstimator::DelayInfo::~DelayInfo() = default;
44
terelius5a388362016-12-09 05:50:01 -080045void MedianSlopeEstimator::Update(double recv_delta_ms,
46 double send_delta_ms,
tereliuseb538fd2016-12-16 02:37:10 -080047 int64_t arrival_time_ms) {
terelius5a388362016-12-09 05:50:01 -080048 const double delta_ms = recv_delta_ms - send_delta_ms;
49 ++num_of_deltas_;
tereliuseb538fd2016-12-16 02:37:10 -080050 if (num_of_deltas_ > kDeltaCounterMax)
terelius5a388362016-12-09 05:50:01 -080051 num_of_deltas_ = kDeltaCounterMax;
terelius5a388362016-12-09 05:50:01 -080052
53 accumulated_delay_ += delta_ms;
tereliuseb538fd2016-12-16 02:37:10 -080054 BWE_TEST_LOGGING_PLOT(1, "accumulated_delay_ms", arrival_time_ms,
55 accumulated_delay_);
terelius5a388362016-12-09 05:50:01 -080056
57 // If the window is full, remove the |window_size_| - 1 slopes that belong to
58 // the oldest point.
59 if (delay_hist_.size() == window_size_) {
60 for (double slope : delay_hist_.front().slopes) {
61 const bool success = median_filter_.Erase(slope);
62 RTC_CHECK(success);
63 }
64 delay_hist_.pop_front();
65 }
66 // Add |window_size_| - 1 new slopes.
67 for (auto& old_delay : delay_hist_) {
tereliuseb538fd2016-12-16 02:37:10 -080068 if (arrival_time_ms - old_delay.time != 0) {
terelius5a388362016-12-09 05:50:01 -080069 // The C99 standard explicitly states that casts and assignments must
70 // perform the associated conversions. This means that |slope| will be
71 // a 64-bit double even if the division is computed using, e.g., 80-bit
72 // extended precision. I believe this also holds in C++ even though the
73 // C++11 standard isn't as explicit. Furthermore, there are good reasons
74 // to believe that compilers couldn't perform optimizations that break
75 // this assumption even if they wanted to.
tereliuseb538fd2016-12-16 02:37:10 -080076 double slope = (accumulated_delay_ - old_delay.delay) /
77 static_cast<double>(arrival_time_ms - old_delay.time);
terelius5a388362016-12-09 05:50:01 -080078 median_filter_.Insert(slope);
79 // We want to avoid issues with different rounding mode / precision
80 // which we might get if we recomputed the slope when we remove it.
81 old_delay.slopes.push_back(slope);
82 }
83 }
tereliuseb538fd2016-12-16 02:37:10 -080084 delay_hist_.emplace_back(arrival_time_ms, accumulated_delay_,
85 window_size_ - 1);
terelius5a388362016-12-09 05:50:01 -080086 // Recompute the median slope.
87 if (delay_hist_.size() == window_size_)
88 trendline_ = median_filter_.GetPercentileValue();
89
tereliuseb538fd2016-12-16 02:37:10 -080090 BWE_TEST_LOGGING_PLOT(1, "trendline_slope", arrival_time_ms, trendline_);
terelius5a388362016-12-09 05:50:01 -080091}
92
93} // namespace webrtc