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