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