terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/congestion_controller/trendline_estimator.h" |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "api/optional.h" |
| 16 | #include "modules/remote_bitrate_estimator/test/bwe_test_logging.h" |
| 17 | #include "rtc_base/checks.h" |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | namespace { |
terelius | b3564ad | 2016-12-15 08:20:25 -0800 | [diff] [blame] | 22 | rtc::Optional<double> LinearFitSlope( |
terelius | d3fabe5 | 2017-01-18 01:59:53 -0800 | [diff] [blame] | 23 | const std::deque<std::pair<double, double>>& points) { |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 24 | RTC_DCHECK(points.size() >= 2); |
| 25 | // Compute the "center of mass". |
| 26 | double sum_x = 0; |
| 27 | double sum_y = 0; |
| 28 | for (const auto& point : points) { |
| 29 | sum_x += point.first; |
| 30 | sum_y += point.second; |
| 31 | } |
| 32 | double x_avg = sum_x / points.size(); |
| 33 | double y_avg = sum_y / points.size(); |
| 34 | // Compute the slope k = \sum (x_i-x_avg)(y_i-y_avg) / \sum (x_i-x_avg)^2 |
| 35 | double numerator = 0; |
| 36 | double denominator = 0; |
| 37 | for (const auto& point : points) { |
| 38 | numerator += (point.first - x_avg) * (point.second - y_avg); |
| 39 | denominator += (point.first - x_avg) * (point.first - x_avg); |
| 40 | } |
terelius | b3564ad | 2016-12-15 08:20:25 -0800 | [diff] [blame] | 41 | if (denominator == 0) |
| 42 | return rtc::Optional<double>(); |
| 43 | return rtc::Optional<double>(numerator / denominator); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 44 | } |
| 45 | } // namespace |
| 46 | |
| 47 | enum { kDeltaCounterMax = 1000 }; |
| 48 | |
| 49 | TrendlineEstimator::TrendlineEstimator(size_t window_size, |
| 50 | double smoothing_coef, |
| 51 | double threshold_gain) |
| 52 | : window_size_(window_size), |
| 53 | smoothing_coef_(smoothing_coef), |
| 54 | threshold_gain_(threshold_gain), |
| 55 | num_of_deltas_(0), |
terelius | b3564ad | 2016-12-15 08:20:25 -0800 | [diff] [blame] | 56 | first_arrival_time_ms(-1), |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 57 | accumulated_delay_(0), |
| 58 | smoothed_delay_(0), |
| 59 | delay_hist_(), |
| 60 | trendline_(0) {} |
| 61 | |
| 62 | TrendlineEstimator::~TrendlineEstimator() {} |
| 63 | |
| 64 | void TrendlineEstimator::Update(double recv_delta_ms, |
| 65 | double send_delta_ms, |
terelius | b3564ad | 2016-12-15 08:20:25 -0800 | [diff] [blame] | 66 | int64_t arrival_time_ms) { |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 67 | const double delta_ms = recv_delta_ms - send_delta_ms; |
| 68 | ++num_of_deltas_; |
terelius | b3564ad | 2016-12-15 08:20:25 -0800 | [diff] [blame] | 69 | if (num_of_deltas_ > kDeltaCounterMax) |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 70 | num_of_deltas_ = kDeltaCounterMax; |
terelius | b3564ad | 2016-12-15 08:20:25 -0800 | [diff] [blame] | 71 | if (first_arrival_time_ms == -1) |
| 72 | first_arrival_time_ms = arrival_time_ms; |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 73 | |
| 74 | // Exponential backoff filter. |
| 75 | accumulated_delay_ += delta_ms; |
terelius | b3564ad | 2016-12-15 08:20:25 -0800 | [diff] [blame] | 76 | BWE_TEST_LOGGING_PLOT(1, "accumulated_delay_ms", arrival_time_ms, |
| 77 | accumulated_delay_); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 78 | smoothed_delay_ = smoothing_coef_ * smoothed_delay_ + |
| 79 | (1 - smoothing_coef_) * accumulated_delay_; |
terelius | b3564ad | 2016-12-15 08:20:25 -0800 | [diff] [blame] | 80 | BWE_TEST_LOGGING_PLOT(1, "smoothed_delay_ms", arrival_time_ms, |
| 81 | smoothed_delay_); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 82 | |
| 83 | // Simple linear regression. |
terelius | b3564ad | 2016-12-15 08:20:25 -0800 | [diff] [blame] | 84 | delay_hist_.push_back(std::make_pair( |
| 85 | static_cast<double>(arrival_time_ms - first_arrival_time_ms), |
| 86 | smoothed_delay_)); |
| 87 | if (delay_hist_.size() > window_size_) |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 88 | delay_hist_.pop_front(); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 89 | if (delay_hist_.size() == window_size_) { |
terelius | b3564ad | 2016-12-15 08:20:25 -0800 | [diff] [blame] | 90 | // Only update trendline_ if it is possible to fit a line to the data. |
| 91 | trendline_ = LinearFitSlope(delay_hist_).value_or(trendline_); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 92 | } |
| 93 | |
terelius | b3564ad | 2016-12-15 08:20:25 -0800 | [diff] [blame] | 94 | BWE_TEST_LOGGING_PLOT(1, "trendline_slope", arrival_time_ms, trendline_); |
terelius | afaef8b | 2016-11-17 03:48:18 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | } // namespace webrtc |