Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: webrtc/modules/congestion_controller/acknowledge_bitrate_estimator.cc

Issue 2917873002: Refactored incoming bitrate estimator. (Closed)
Patch Set: Respons to comments Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/acknowledge_bitrate_estimator.h"
12
13 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
14
15 namespace webrtc {
16
17 namespace {
18 constexpr int kInitialRateWindowMs = 500;
19 constexpr int kRateWindowMs = 150;
20 } // namespace
21
22 AcknowledgedBitrateEstimator::AcknowledgedBitrateEstimator()
23 : sum_(0),
24 current_win_ms_(0),
25 prev_time_ms_(-1),
26 bitrate_estimate_(-1.0f),
27 bitrate_estimate_var_(50.0f) {}
28
29 void AcknowledgedBitrateEstimator::IncomingPacketFeedbackVector(
30 const std::vector<PacketFeedback>& packet_feedback_vector) {
31 RTC_DCHECK(std::is_sorted(packet_feedback_vector.begin(),
32 packet_feedback_vector.end(),
33 PacketFeedbackComparator()));
34 for (const auto& packet : packet_feedback_vector)
35 Update(packet.arrival_time_ms, packet.payload_size);
36 }
37
38 void AcknowledgedBitrateEstimator::Update(int64_t now_ms, int bytes) {
39 int rate_window_ms = kRateWindowMs;
40 // We use a larger window at the beginning to get a more stable sample that
41 // we can use to initialize the estimate.
42 if (bitrate_estimate_ < 0.f)
43 rate_window_ms = kInitialRateWindowMs;
44 float bitrate_sample = UpdateWindow(now_ms, bytes, rate_window_ms);
45 if (bitrate_sample < 0.0f)
46 return;
47 if (bitrate_estimate_ < 0.0f) {
48 // This is the very first sample we get. Use it to initialize the estimate.
49 bitrate_estimate_ = bitrate_sample;
50 return;
51 }
52 // Define the sample uncertainty as a function of how far away it is from the
53 // current estimate.
54 float sample_uncertainty =
55 10.0f * std::abs(bitrate_estimate_ - bitrate_sample) / bitrate_estimate_;
56 float sample_var = sample_uncertainty * sample_uncertainty;
57 // Update a bayesian estimate of the rate, weighting it lower if the sample
58 // uncertainty is large.
59 // The bitrate estimate uncertainty is increased with each update to model
60 // that the bitrate changes over time.
61 float pred_bitrate_estimate_var = bitrate_estimate_var_ + 5.f;
62 bitrate_estimate_ = (sample_var * bitrate_estimate_ +
63 pred_bitrate_estimate_var * bitrate_sample) /
64 (sample_var + pred_bitrate_estimate_var);
65 bitrate_estimate_var_ = sample_var * pred_bitrate_estimate_var /
66 (sample_var + pred_bitrate_estimate_var);
67 }
68
69 float AcknowledgedBitrateEstimator::UpdateWindow(int64_t now_ms,
70 int bytes,
71 int rate_window_ms) {
72 // Reset if time moves backwards.
73 if (now_ms < prev_time_ms_) {
74 prev_time_ms_ = -1;
75 sum_ = 0;
76 current_win_ms_ = 0;
77 }
78 if (prev_time_ms_ >= 0) {
79 current_win_ms_ += now_ms - prev_time_ms_;
80 // Reset if nothing has been received for more than a full window.
81 if (now_ms - prev_time_ms_ > rate_window_ms) {
82 sum_ = 0;
83 current_win_ms_ %= rate_window_ms;
84 }
85 }
86 prev_time_ms_ = now_ms;
87 float bitrate_sample = -1.0f;
88 if (current_win_ms_ >= rate_window_ms) {
89 bitrate_sample = 8.0f * sum_ / static_cast<float>(rate_window_ms);
90 current_win_ms_ -= rate_window_ms;
91 sum_ = 0;
92 }
93 sum_ += bytes;
94 return bitrate_sample;
95 }
96
97 rtc::Optional<uint32_t> AcknowledgedBitrateEstimator::bitrate_bps() const {
98 if (bitrate_estimate_ < 0.f)
99 return rtc::Optional<uint32_t>();
100 return rtc::Optional<uint32_t>(bitrate_estimate_ * 1000);
101 }
102
103 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698