OLD | NEW |
---|---|
(Empty) | |
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 | |
11 #include "webrtc/modules/congestion_controller/incoming_bitrate_estimator.h" | |
12 | |
13 namespace webrtc { | |
14 | |
15 namespace { | |
16 constexpr int kInitialRateWindowMs = 500; | |
17 constexpr int kRateWindowMs = 150; | |
18 } // namespace | |
19 | |
20 IncomingBitrateEstimator::IncomingBitrateEstimator() | |
21 : sum_(0), | |
22 current_win_ms_(0), | |
23 prev_time_ms_(-1), | |
24 bitrate_estimate_(-1.0f), | |
25 bitrate_estimate_var_(50.0f) {} | |
26 | |
27 void IncomingBitrateEstimator::Update(int64_t now_ms, int bytes) { | |
terelius
2017/06/01 15:15:04
Has any part of this function changed?
tschumi
2017/06/02 11:48:44
Nope.
That is just a pure copy.
| |
28 int rate_window_ms = kRateWindowMs; | |
29 // We use a larger window at the beginning to get a more stable sample that | |
30 // we can use to initialize the estimate. | |
31 if (bitrate_estimate_ < 0.f) | |
32 rate_window_ms = kInitialRateWindowMs; | |
33 float bitrate_sample = UpdateWindow(now_ms, bytes, rate_window_ms); | |
34 if (bitrate_sample < 0.0f) | |
35 return; | |
36 if (bitrate_estimate_ < 0.0f) { | |
37 // This is the very first sample we get. Use it to initialize the estimate. | |
38 bitrate_estimate_ = bitrate_sample; | |
39 return; | |
40 } | |
41 // Define the sample uncertainty as a function of how far away it is from the | |
42 // current estimate. | |
43 float sample_uncertainty = | |
44 10.0f * std::abs(bitrate_estimate_ - bitrate_sample) / bitrate_estimate_; | |
45 float sample_var = sample_uncertainty * sample_uncertainty; | |
46 // Update a bayesian estimate of the rate, weighting it lower if the sample | |
47 // uncertainty is large. | |
48 // The bitrate estimate uncertainty is increased with each update to model | |
49 // that the bitrate changes over time. | |
50 float pred_bitrate_estimate_var = bitrate_estimate_var_ + 5.f; | |
51 bitrate_estimate_ = (sample_var * bitrate_estimate_ + | |
52 pred_bitrate_estimate_var * bitrate_sample) / | |
53 (sample_var + pred_bitrate_estimate_var); | |
54 bitrate_estimate_var_ = sample_var * pred_bitrate_estimate_var / | |
55 (sample_var + pred_bitrate_estimate_var); | |
56 } | |
57 | |
58 float IncomingBitrateEstimator::UpdateWindow(int64_t now_ms, | |
59 int bytes, | |
60 int rate_window_ms) { | |
61 // Reset if time moves backwards. | |
terelius
2017/06/01 15:15:04
Has any part of this function changed?
tschumi
2017/06/02 11:48:43
Nope.
That is just a pure copy.
| |
62 if (now_ms < prev_time_ms_) { | |
63 prev_time_ms_ = -1; | |
64 sum_ = 0; | |
65 current_win_ms_ = 0; | |
66 } | |
67 if (prev_time_ms_ >= 0) { | |
68 current_win_ms_ += now_ms - prev_time_ms_; | |
69 // Reset if nothing has been received for more than a full window. | |
70 if (now_ms - prev_time_ms_ > rate_window_ms) { | |
71 sum_ = 0; | |
72 current_win_ms_ %= rate_window_ms; | |
73 } | |
74 } | |
75 prev_time_ms_ = now_ms; | |
76 float bitrate_sample = -1.0f; | |
77 if (current_win_ms_ >= rate_window_ms) { | |
78 bitrate_sample = 8.0f * sum_ / static_cast<float>(rate_window_ms); | |
79 current_win_ms_ -= rate_window_ms; | |
80 sum_ = 0; | |
81 } | |
82 sum_ += bytes; | |
83 return bitrate_sample; | |
84 } | |
85 | |
86 rtc::Optional<uint32_t> IncomingBitrateEstimator::bitrate_bps() const { | |
87 if (bitrate_estimate_ < 0.f) | |
88 return rtc::Optional<uint32_t>(); | |
89 return rtc::Optional<uint32_t>(bitrate_estimate_ * 1000); | |
90 } | |
91 | |
92 } // namespace webrtc | |
OLD | NEW |