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