You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
7.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2019 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. #include "video/encoder_overshoot_detector.h"
  11. #include <algorithm>
  12. namespace webrtc {
  13. namespace {
  14. // The buffer level for media-rate utilization is allowed to go below zero,
  15. // down to
  16. // -(|kMaxMediaUnderrunFrames| / |target_framerate_fps_|) * |target_bitrate_|.
  17. static constexpr double kMaxMediaUnderrunFrames = 5.0;
  18. } // namespace
  19. EncoderOvershootDetector::EncoderOvershootDetector(int64_t window_size_ms)
  20. : window_size_ms_(window_size_ms),
  21. time_last_update_ms_(-1),
  22. sum_network_utilization_factors_(0.0),
  23. sum_media_utilization_factors_(0.0),
  24. target_bitrate_(DataRate::Zero()),
  25. target_framerate_fps_(0),
  26. network_buffer_level_bits_(0),
  27. media_buffer_level_bits_(0) {}
  28. EncoderOvershootDetector::~EncoderOvershootDetector() = default;
  29. void EncoderOvershootDetector::SetTargetRate(DataRate target_bitrate,
  30. double target_framerate_fps,
  31. int64_t time_ms) {
  32. // First leak bits according to the previous target rate.
  33. if (target_bitrate_ != DataRate::Zero()) {
  34. LeakBits(time_ms);
  35. } else if (target_bitrate != DataRate::Zero()) {
  36. // Stream was just enabled, reset state.
  37. time_last_update_ms_ = time_ms;
  38. utilization_factors_.clear();
  39. sum_network_utilization_factors_ = 0.0;
  40. sum_media_utilization_factors_ = 0.0;
  41. network_buffer_level_bits_ = 0;
  42. media_buffer_level_bits_ = 0;
  43. }
  44. target_bitrate_ = target_bitrate;
  45. target_framerate_fps_ = target_framerate_fps;
  46. }
  47. void EncoderOvershootDetector::OnEncodedFrame(size_t bytes, int64_t time_ms) {
  48. // Leak bits from the virtual pacer buffer, according to the current target
  49. // bitrate.
  50. LeakBits(time_ms);
  51. // Ideal size of a frame given the current rates.
  52. const int64_t ideal_frame_size_bits = IdealFrameSizeBits();
  53. if (ideal_frame_size_bits == 0) {
  54. // Frame without updated bitrate and/or framerate, ignore it.
  55. return;
  56. }
  57. const double network_utilization_factor = HandleEncodedFrame(
  58. bytes * 8, ideal_frame_size_bits, time_ms, &network_buffer_level_bits_);
  59. const double media_utilization_factor = HandleEncodedFrame(
  60. bytes * 8, ideal_frame_size_bits, time_ms, &media_buffer_level_bits_);
  61. sum_network_utilization_factors_ += network_utilization_factor;
  62. sum_media_utilization_factors_ += media_utilization_factor;
  63. utilization_factors_.emplace_back(network_utilization_factor,
  64. media_utilization_factor, time_ms);
  65. }
  66. double EncoderOvershootDetector::HandleEncodedFrame(
  67. size_t frame_size_bits,
  68. int64_t ideal_frame_size_bits,
  69. int64_t time_ms,
  70. int64_t* buffer_level_bits) const {
  71. // Add new frame to the buffer level. If doing so exceeds the ideal buffer
  72. // size, penalize this frame but cap overshoot to current buffer level rather
  73. // than size of this frame. This is done so that a single large frame is not
  74. // penalized if the encoder afterwards compensates by dropping frames and/or
  75. // reducing frame size. If however a large frame is followed by more data,
  76. // we cannot pace that next frame out within one frame space.
  77. const int64_t bitsum = frame_size_bits + *buffer_level_bits;
  78. int64_t overshoot_bits = 0;
  79. if (bitsum > ideal_frame_size_bits) {
  80. overshoot_bits =
  81. std::min(*buffer_level_bits, bitsum - ideal_frame_size_bits);
  82. }
  83. // Add entry for the (over) utilization for this frame. Factor is capped
  84. // at 1.0 so that we don't risk overshooting on sudden changes.
  85. double utilization_factor;
  86. if (utilization_factors_.empty()) {
  87. // First frame, cannot estimate overshoot based on previous one so
  88. // for this particular frame, just like as size vs optimal size.
  89. utilization_factor = std::max(
  90. 1.0, static_cast<double>(frame_size_bits) / ideal_frame_size_bits);
  91. } else {
  92. utilization_factor =
  93. 1.0 + (static_cast<double>(overshoot_bits) / ideal_frame_size_bits);
  94. }
  95. // Remove the overshot bits from the virtual buffer so we don't penalize
  96. // those bits multiple times.
  97. *buffer_level_bits -= overshoot_bits;
  98. *buffer_level_bits += frame_size_bits;
  99. return utilization_factor;
  100. }
  101. absl::optional<double>
  102. EncoderOvershootDetector::GetNetworkRateUtilizationFactor(int64_t time_ms) {
  103. CullOldUpdates(time_ms);
  104. // No data points within window, return.
  105. if (utilization_factors_.empty()) {
  106. return absl::nullopt;
  107. }
  108. // TODO(sprang): Consider changing from arithmetic mean to some other
  109. // function such as 90th percentile.
  110. return sum_network_utilization_factors_ / utilization_factors_.size();
  111. }
  112. absl::optional<double> EncoderOvershootDetector::GetMediaRateUtilizationFactor(
  113. int64_t time_ms) {
  114. CullOldUpdates(time_ms);
  115. // No data points within window, return.
  116. if (utilization_factors_.empty()) {
  117. return absl::nullopt;
  118. }
  119. return sum_media_utilization_factors_ / utilization_factors_.size();
  120. }
  121. void EncoderOvershootDetector::Reset() {
  122. time_last_update_ms_ = -1;
  123. utilization_factors_.clear();
  124. target_bitrate_ = DataRate::Zero();
  125. sum_network_utilization_factors_ = 0.0;
  126. sum_media_utilization_factors_ = 0.0;
  127. target_framerate_fps_ = 0.0;
  128. network_buffer_level_bits_ = 0;
  129. media_buffer_level_bits_ = 0;
  130. }
  131. int64_t EncoderOvershootDetector::IdealFrameSizeBits() const {
  132. if (target_framerate_fps_ <= 0 || target_bitrate_ == DataRate::Zero()) {
  133. return 0;
  134. }
  135. // Current ideal frame size, based on the current target bitrate.
  136. return static_cast<int64_t>(
  137. (target_bitrate_.bps() + target_framerate_fps_ / 2) /
  138. target_framerate_fps_);
  139. }
  140. void EncoderOvershootDetector::LeakBits(int64_t time_ms) {
  141. if (time_last_update_ms_ != -1 && target_bitrate_ > DataRate::Zero()) {
  142. int64_t time_delta_ms = time_ms - time_last_update_ms_;
  143. // Leak bits according to the current target bitrate.
  144. const int64_t leaked_bits = (target_bitrate_.bps() * time_delta_ms) / 1000;
  145. // Network buffer may not go below zero.
  146. network_buffer_level_bits_ =
  147. std::max<int64_t>(0, network_buffer_level_bits_ - leaked_bits);
  148. // Media buffer my go down to minus |kMaxMediaUnderrunFrames| frames worth
  149. // of data.
  150. const double max_underrun_seconds =
  151. std::min(kMaxMediaUnderrunFrames, target_framerate_fps_) /
  152. target_framerate_fps_;
  153. media_buffer_level_bits_ = std::max<int64_t>(
  154. -max_underrun_seconds * target_bitrate_.bps<int64_t>(),
  155. media_buffer_level_bits_ - leaked_bits);
  156. }
  157. time_last_update_ms_ = time_ms;
  158. }
  159. void EncoderOvershootDetector::CullOldUpdates(int64_t time_ms) {
  160. // Cull old data points.
  161. const int64_t cutoff_time_ms = time_ms - window_size_ms_;
  162. while (!utilization_factors_.empty() &&
  163. utilization_factors_.front().update_time_ms < cutoff_time_ms) {
  164. // Make sure sum is never allowed to become negative due rounding errors.
  165. sum_network_utilization_factors_ = std::max(
  166. 0.0, sum_network_utilization_factors_ -
  167. utilization_factors_.front().network_utilization_factor);
  168. sum_media_utilization_factors_ = std::max(
  169. 0.0, sum_media_utilization_factors_ -
  170. utilization_factors_.front().media_utilization_factor);
  171. utilization_factors_.pop_front();
  172. }
  173. }
  174. } // namespace webrtc