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