OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/congestion_controller/acknowledge_bitrate_estimator.h" | 11 #include "webrtc/modules/congestion_controller/bitrate_estimator.h" |
12 | 12 |
13 #include <cmath> | 13 #include <cmath> |
14 | 14 |
15 #include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h" | 15 #include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h" |
16 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" | 16 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
17 | 17 |
18 namespace webrtc { | 18 namespace webrtc { |
19 | 19 |
20 namespace { | 20 namespace { |
21 constexpr int kInitialRateWindowMs = 500; | 21 constexpr int kInitialRateWindowMs = 500; |
22 constexpr int kRateWindowMs = 150; | 22 constexpr int kRateWindowMs = 150; |
23 | |
24 bool IsInSendTimeHistory(const PacketFeedback& packet) { | |
25 return packet.send_time_ms >= 0; | |
26 } | |
27 | |
28 } // namespace | 23 } // namespace |
29 | 24 |
30 AcknowledgedBitrateEstimator::AcknowledgedBitrateEstimator() | 25 BitrateEstimator::BitrateEstimator() |
31 : sum_(0), | 26 : sum_(0), |
32 current_win_ms_(0), | 27 current_win_ms_(0), |
33 prev_time_ms_(-1), | 28 prev_time_ms_(-1), |
34 bitrate_estimate_(-1.0f), | 29 bitrate_estimate_(-1.0f), |
35 bitrate_estimate_var_(50.0f) {} | 30 bitrate_estimate_var_(50.0f) {} |
36 | 31 |
37 void AcknowledgedBitrateEstimator::IncomingPacketFeedbackVector( | 32 BitrateEstimator::~BitrateEstimator() = default; |
38 const std::vector<PacketFeedback>& packet_feedback_vector) { | |
39 RTC_DCHECK(std::is_sorted(packet_feedback_vector.begin(), | |
40 packet_feedback_vector.end(), | |
41 PacketFeedbackComparator())); | |
42 for (const auto& packet : packet_feedback_vector) { | |
43 if (IsInSendTimeHistory(packet)) | |
44 Update(packet.arrival_time_ms, packet.payload_size); | |
45 } | |
46 } | |
47 | 33 |
48 void AcknowledgedBitrateEstimator::Update(int64_t now_ms, int bytes) { | 34 void BitrateEstimator::Update(int64_t now_ms, int bytes) { |
49 int rate_window_ms = kRateWindowMs; | 35 int rate_window_ms = kRateWindowMs; |
50 // We use a larger window at the beginning to get a more stable sample that | 36 // We use a larger window at the beginning to get a more stable sample that |
51 // we can use to initialize the estimate. | 37 // we can use to initialize the estimate. |
52 if (bitrate_estimate_ < 0.f) | 38 if (bitrate_estimate_ < 0.f) |
53 rate_window_ms = kInitialRateWindowMs; | 39 rate_window_ms = kInitialRateWindowMs; |
54 float bitrate_sample = UpdateWindow(now_ms, bytes, rate_window_ms); | 40 float bitrate_sample = UpdateWindow(now_ms, bytes, rate_window_ms); |
55 if (bitrate_sample < 0.0f) | 41 if (bitrate_sample < 0.0f) |
56 return; | 42 return; |
57 if (bitrate_estimate_ < 0.0f) { | 43 if (bitrate_estimate_ < 0.0f) { |
58 // This is the very first sample we get. Use it to initialize the estimate. | 44 // This is the very first sample we get. Use it to initialize the estimate. |
(...skipping 12 matching lines...) Expand all Loading... |
71 float pred_bitrate_estimate_var = bitrate_estimate_var_ + 5.f; | 57 float pred_bitrate_estimate_var = bitrate_estimate_var_ + 5.f; |
72 bitrate_estimate_ = (sample_var * bitrate_estimate_ + | 58 bitrate_estimate_ = (sample_var * bitrate_estimate_ + |
73 pred_bitrate_estimate_var * bitrate_sample) / | 59 pred_bitrate_estimate_var * bitrate_sample) / |
74 (sample_var + pred_bitrate_estimate_var); | 60 (sample_var + pred_bitrate_estimate_var); |
75 bitrate_estimate_var_ = sample_var * pred_bitrate_estimate_var / | 61 bitrate_estimate_var_ = sample_var * pred_bitrate_estimate_var / |
76 (sample_var + pred_bitrate_estimate_var); | 62 (sample_var + pred_bitrate_estimate_var); |
77 BWE_TEST_LOGGING_PLOT(1, "acknowledged_bitrate", now_ms, | 63 BWE_TEST_LOGGING_PLOT(1, "acknowledged_bitrate", now_ms, |
78 bitrate_estimate_ * 1000); | 64 bitrate_estimate_ * 1000); |
79 } | 65 } |
80 | 66 |
81 float AcknowledgedBitrateEstimator::UpdateWindow(int64_t now_ms, | 67 float BitrateEstimator::UpdateWindow(int64_t now_ms, |
82 int bytes, | 68 int bytes, |
83 int rate_window_ms) { | 69 int rate_window_ms) { |
84 // Reset if time moves backwards. | 70 // Reset if time moves backwards. |
85 if (now_ms < prev_time_ms_) { | 71 if (now_ms < prev_time_ms_) { |
86 prev_time_ms_ = -1; | 72 prev_time_ms_ = -1; |
87 sum_ = 0; | 73 sum_ = 0; |
88 current_win_ms_ = 0; | 74 current_win_ms_ = 0; |
89 } | 75 } |
90 if (prev_time_ms_ >= 0) { | 76 if (prev_time_ms_ >= 0) { |
91 current_win_ms_ += now_ms - prev_time_ms_; | 77 current_win_ms_ += now_ms - prev_time_ms_; |
92 // Reset if nothing has been received for more than a full window. | 78 // Reset if nothing has been received for more than a full window. |
93 if (now_ms - prev_time_ms_ > rate_window_ms) { | 79 if (now_ms - prev_time_ms_ > rate_window_ms) { |
94 sum_ = 0; | 80 sum_ = 0; |
95 current_win_ms_ %= rate_window_ms; | 81 current_win_ms_ %= rate_window_ms; |
96 } | 82 } |
97 } | 83 } |
98 prev_time_ms_ = now_ms; | 84 prev_time_ms_ = now_ms; |
99 float bitrate_sample = -1.0f; | 85 float bitrate_sample = -1.0f; |
100 if (current_win_ms_ >= rate_window_ms) { | 86 if (current_win_ms_ >= rate_window_ms) { |
101 bitrate_sample = 8.0f * sum_ / static_cast<float>(rate_window_ms); | 87 bitrate_sample = 8.0f * sum_ / static_cast<float>(rate_window_ms); |
102 current_win_ms_ -= rate_window_ms; | 88 current_win_ms_ -= rate_window_ms; |
103 sum_ = 0; | 89 sum_ = 0; |
104 } | 90 } |
105 sum_ += bytes; | 91 sum_ += bytes; |
106 return bitrate_sample; | 92 return bitrate_sample; |
107 } | 93 } |
108 | 94 |
109 rtc::Optional<uint32_t> AcknowledgedBitrateEstimator::bitrate_bps() const { | 95 rtc::Optional<uint32_t> BitrateEstimator::bitrate_bps() const { |
110 if (bitrate_estimate_ < 0.f) | 96 if (bitrate_estimate_ < 0.f) |
111 return rtc::Optional<uint32_t>(); | 97 return rtc::Optional<uint32_t>(); |
112 return rtc::Optional<uint32_t>(bitrate_estimate_ * 1000); | 98 return rtc::Optional<uint32_t>(bitrate_estimate_ * 1000); |
113 } | 99 } |
114 | 100 |
115 } // namespace webrtc | 101 } // namespace webrtc |
OLD | NEW |