tschumim | 3fae628 | 2017-06-11 23:57:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | |
tschumim | 9d11764 | 2017-07-17 01:41:41 -0700 | [diff] [blame] | 11 | #include "webrtc/modules/congestion_controller/bitrate_estimator.h" |
tschumim | 3fae628 | 2017-06-11 23:57:17 -0700 | [diff] [blame] | 12 | |
| 13 | #include <cmath> |
| 14 | |
terelius | 6c4ba9f | 2017-06-14 02:41:59 -0700 | [diff] [blame] | 15 | #include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h" |
tschumim | 3fae628 | 2017-06-11 23:57:17 -0700 | [diff] [blame] | 16 | #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | namespace { |
| 21 | constexpr int kInitialRateWindowMs = 500; |
| 22 | constexpr int kRateWindowMs = 150; |
tschumim | 3fae628 | 2017-06-11 23:57:17 -0700 | [diff] [blame] | 23 | } // namespace |
| 24 | |
tschumim | 9d11764 | 2017-07-17 01:41:41 -0700 | [diff] [blame] | 25 | BitrateEstimator::BitrateEstimator() |
tschumim | 3fae628 | 2017-06-11 23:57:17 -0700 | [diff] [blame] | 26 | : sum_(0), |
| 27 | current_win_ms_(0), |
| 28 | prev_time_ms_(-1), |
| 29 | bitrate_estimate_(-1.0f), |
| 30 | bitrate_estimate_var_(50.0f) {} |
| 31 | |
tschumim | 9d11764 | 2017-07-17 01:41:41 -0700 | [diff] [blame] | 32 | BitrateEstimator::~BitrateEstimator() = default; |
tschumim | 3fae628 | 2017-06-11 23:57:17 -0700 | [diff] [blame] | 33 | |
tschumim | 9d11764 | 2017-07-17 01:41:41 -0700 | [diff] [blame] | 34 | void BitrateEstimator::Update(int64_t now_ms, int bytes) { |
tschumim | 3fae628 | 2017-06-11 23:57:17 -0700 | [diff] [blame] | 35 | int rate_window_ms = kRateWindowMs; |
| 36 | // We use a larger window at the beginning to get a more stable sample that |
| 37 | // we can use to initialize the estimate. |
| 38 | if (bitrate_estimate_ < 0.f) |
| 39 | rate_window_ms = kInitialRateWindowMs; |
| 40 | float bitrate_sample = UpdateWindow(now_ms, bytes, rate_window_ms); |
| 41 | if (bitrate_sample < 0.0f) |
| 42 | return; |
| 43 | if (bitrate_estimate_ < 0.0f) { |
| 44 | // This is the very first sample we get. Use it to initialize the estimate. |
| 45 | bitrate_estimate_ = bitrate_sample; |
| 46 | return; |
| 47 | } |
| 48 | // Define the sample uncertainty as a function of how far away it is from the |
| 49 | // current estimate. |
| 50 | float sample_uncertainty = |
| 51 | 10.0f * std::abs(bitrate_estimate_ - bitrate_sample) / bitrate_estimate_; |
| 52 | float sample_var = sample_uncertainty * sample_uncertainty; |
| 53 | // Update a bayesian estimate of the rate, weighting it lower if the sample |
| 54 | // uncertainty is large. |
| 55 | // The bitrate estimate uncertainty is increased with each update to model |
| 56 | // that the bitrate changes over time. |
| 57 | float pred_bitrate_estimate_var = bitrate_estimate_var_ + 5.f; |
| 58 | bitrate_estimate_ = (sample_var * bitrate_estimate_ + |
| 59 | pred_bitrate_estimate_var * bitrate_sample) / |
| 60 | (sample_var + pred_bitrate_estimate_var); |
| 61 | bitrate_estimate_var_ = sample_var * pred_bitrate_estimate_var / |
| 62 | (sample_var + pred_bitrate_estimate_var); |
terelius | 6c4ba9f | 2017-06-14 02:41:59 -0700 | [diff] [blame] | 63 | BWE_TEST_LOGGING_PLOT(1, "acknowledged_bitrate", now_ms, |
| 64 | bitrate_estimate_ * 1000); |
tschumim | 3fae628 | 2017-06-11 23:57:17 -0700 | [diff] [blame] | 65 | } |
| 66 | |
tschumim | 9d11764 | 2017-07-17 01:41:41 -0700 | [diff] [blame] | 67 | float BitrateEstimator::UpdateWindow(int64_t now_ms, |
| 68 | int bytes, |
| 69 | int rate_window_ms) { |
tschumim | 3fae628 | 2017-06-11 23:57:17 -0700 | [diff] [blame] | 70 | // Reset if time moves backwards. |
| 71 | if (now_ms < prev_time_ms_) { |
| 72 | prev_time_ms_ = -1; |
| 73 | sum_ = 0; |
| 74 | current_win_ms_ = 0; |
| 75 | } |
| 76 | if (prev_time_ms_ >= 0) { |
| 77 | current_win_ms_ += now_ms - prev_time_ms_; |
| 78 | // Reset if nothing has been received for more than a full window. |
| 79 | if (now_ms - prev_time_ms_ > rate_window_ms) { |
| 80 | sum_ = 0; |
| 81 | current_win_ms_ %= rate_window_ms; |
| 82 | } |
| 83 | } |
| 84 | prev_time_ms_ = now_ms; |
| 85 | float bitrate_sample = -1.0f; |
| 86 | if (current_win_ms_ >= rate_window_ms) { |
| 87 | bitrate_sample = 8.0f * sum_ / static_cast<float>(rate_window_ms); |
| 88 | current_win_ms_ -= rate_window_ms; |
| 89 | sum_ = 0; |
| 90 | } |
| 91 | sum_ += bytes; |
| 92 | return bitrate_sample; |
| 93 | } |
| 94 | |
tschumim | 9d11764 | 2017-07-17 01:41:41 -0700 | [diff] [blame] | 95 | rtc::Optional<uint32_t> BitrateEstimator::bitrate_bps() const { |
tschumim | 3fae628 | 2017-06-11 23:57:17 -0700 | [diff] [blame] | 96 | if (bitrate_estimate_ < 0.f) |
| 97 | return rtc::Optional<uint32_t>(); |
| 98 | return rtc::Optional<uint32_t>(bitrate_estimate_ * 1000); |
| 99 | } |
| 100 | |
tschumim | 9d11764 | 2017-07-17 01:41:41 -0700 | [diff] [blame] | 101 | void BitrateEstimator::ExpectFastRateChange() { |
| 102 | // By setting the bitrate-estimate variance to a higher value we allow the |
| 103 | // bitrate to change fast for the next few samples. |
| 104 | bitrate_estimate_var_ += 200; |
| 105 | } |
| 106 | |
tschumim | 3fae628 | 2017-06-11 23:57:17 -0700 | [diff] [blame] | 107 | } // namespace webrtc |