Index: webrtc/modules/video_coding/bitrate_adjuster.cc |
diff --git a/webrtc/modules/video_coding/bitrate_adjuster.cc b/webrtc/modules/video_coding/bitrate_adjuster.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6d6bd0c2e1cb55e634be4753e45b813866313841 |
--- /dev/null |
+++ b/webrtc/modules/video_coding/bitrate_adjuster.cc |
@@ -0,0 +1,134 @@ |
+/* |
+ * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ */ |
+ |
+#include "webrtc/modules/video_coding/include/bitrate_adjuster.h" |
+ |
+#include "webrtc/base/checks.h" |
+#include "webrtc/base/logging.h" |
+#include "webrtc/system_wrappers/include/clock.h" |
+ |
+namespace webrtc { |
+ |
+// Update bitrate at most once every second. |
+const uint32_t BitrateAdjuster::kBitrateUpdateIntervalMs = 1000; |
+ |
+// Update bitrate at most once every 30 frames. |
+const uint32_t BitrateAdjuster::kBitrateUpdateFrameInterval = 30; |
+ |
+// 5 percent of original. |
+const uint32_t BitrateAdjuster::kBitrateTolerancePct = 5; |
+ |
+BitrateAdjuster::BitrateAdjuster(Clock* clock) |
+ : clock_(clock), |
+ bitrate_tracker_(1.5 * kBitrateUpdateIntervalMs, 8 * 1000) { |
pbos-webrtc
2016/02/22 15:44:18
What#s 8? constant please
tkchin_webrtc
2016/02/22 18:23:18
Done.
|
+ Reset(); |
+} |
+ |
+void BitrateAdjuster::SetTargetBitrateBps(uint32_t bitrate_bps) { |
+ rtc::CritScope cs(&crit_); |
+ target_bitrate_bps_ = bitrate_bps; |
+ // If we haven't had time to determine what adjustment is required yet then |
+ // just use the target. |
+ adjusted_bitrate_bps_ = target_bitrate_bps_; |
+} |
+ |
+uint32_t BitrateAdjuster::GetTargetBitrateBps() const { |
+ rtc::CritScope cs(&crit_); |
+ return target_bitrate_bps_; |
+} |
+ |
+uint32_t BitrateAdjuster::GetAdjustedBitrateBps() const { |
+ rtc::CritScope cs(&crit_); |
+ return adjusted_bitrate_bps_; |
+} |
+ |
+uint32_t BitrateAdjuster::GetEstimatedBitrateBps() { |
+ rtc::CritScope cs(&crit_); |
+ return bitrate_tracker_.Rate(clock_->TimeInMilliseconds()); |
+} |
+ |
+uint32_t BitrateAdjuster::GetMinAdjustedBitrateBps() const { |
+ rtc::CritScope cs(&crit_); |
+ // Half of target bitrate. |
+ return target_bitrate_bps_ / 2; |
+} |
+ |
+uint32_t BitrateAdjuster::GetMaxAdjustedBitrateBps() const { |
+ rtc::CritScope cs(&crit_); |
+ // 95% of target bitrate. In practice we know that the VideoToolbox encoder |
+ // on iOS overshoots regularly. Setting it to 95% seems to reduce |
+ // oscillations. |
+ return .95 * target_bitrate_bps_; |
pbos-webrtc
2016/02/22 15:44:18
Is it intentional that this thing can only operate
tkchin_webrtc
2016/02/22 18:23:18
As we talked about before, we wanted this for the
pbos-webrtc
2016/02/22 18:37:19
But this reads to me like it's allowed to clamp on
tkchin_webrtc
2016/02/23 00:05:27
Done.
|
+} |
+ |
+void BitrateAdjuster::Update(size_t frame_size) { |
+ rtc::CritScope cs(&crit_); |
+ uint32_t current_time_ms = clock_->TimeInMilliseconds(); |
+ bitrate_tracker_.Update(frame_size, current_time_ms); |
+ UpdateBitrate(current_time_ms); |
+} |
+ |
+// Only safe to call this after Update calls have stopped |
+void BitrateAdjuster::Reset() { |
+ rtc::CritScope cs(&crit_); |
+ target_bitrate_bps_ = 0; |
+ adjusted_bitrate_bps_ = 0; |
+ last_bitrate_update_time_ms_ = 0; |
+ frames_since_last_update_ = 0; |
+ bitrate_tracker_.Reset(); |
+} |
+ |
+void BitrateAdjuster::UpdateBitrate(uint32_t current_time_ms) { |
+ rtc::CritScope cs(&crit_); |
+ uint32_t time_since_last_update_ms = |
+ current_time_ms - last_bitrate_update_time_ms_; |
+ // Don't attempt to update bitrate unless enough time and frames have passed. |
+ ++frames_since_last_update_; |
+ if (time_since_last_update_ms < kBitrateUpdateIntervalMs || |
+ frames_since_last_update_ < kBitrateUpdateFrameInterval) { |
+ return; |
+ } |
+ float estimated_bitrate_bps = bitrate_tracker_.Rate(current_time_ms); |
+ float target_bitrate_bps = target_bitrate_bps_; |
+ float error = target_bitrate_bps - estimated_bitrate_bps; |
+ |
+ // Adjust if we've overshot by any amount or if we've undershot too much. |
+ if (estimated_bitrate_bps > target_bitrate_bps || |
+ error * 100 > kBitrateTolerancePct * target_bitrate_bps) { |
pbos-webrtc
2016/02/22 15:44:18
make kBitrateTolerance not be in percent.
tkchin_webrtc
2016/02/22 18:23:18
why? Do you have a particular value in mind?
pbos-webrtc
2016/02/22 18:37:19
Just make it 0.05 instead of 5 to avoid the multip
tkchin_webrtc
2016/02/23 00:05:27
Done.
|
+ // Adjust the bitrate by a fraction of the error. |
+ float adjustment = .5 * error; |
+ float adjusted_bitrate_bps = target_bitrate_bps + adjustment; |
+ |
+ // Clamp the adjustment to [0.5 * target, 0.95 * target]. |
+ float min_bitrate_bps = GetMinAdjustedBitrateBps(); |
pbos-webrtc
2016/02/22 18:37:19
This takes crit_ recursively, can you put EXCLUSIV
tkchin_webrtc
2016/02/23 00:05:27
Done. But now requires friend class for test.
pbos-webrtc
2016/02/23 15:04:40
Can't you just make sure it's clamped from the out
tkchin_webrtc
2016/02/23 17:03:20
Done.
|
+ float max_bitrate_bps = GetMaxAdjustedBitrateBps(); |
+ adjusted_bitrate_bps = std::max(adjusted_bitrate_bps, min_bitrate_bps); |
+ adjusted_bitrate_bps = std::min(adjusted_bitrate_bps, max_bitrate_bps); |
+ |
+ // Set the adjustment if it's not already set. |
+ float last_adjusted_bitrate_bps = adjusted_bitrate_bps_; |
+ if (adjusted_bitrate_bps != last_adjusted_bitrate_bps) { |
+ LOG(LS_INFO) << "Adjusting encoder bitrate:" |
+ << "\n target_bitrate:" |
+ << static_cast<uint32_t>(target_bitrate_bps) |
+ << "\n estimated_bitrate:" |
+ << static_cast<uint32_t>(estimated_bitrate_bps) |
+ << "\n last_adjusted_bitrate:" |
+ << static_cast<uint32_t>(last_adjusted_bitrate_bps) |
+ << "\n adjusted_bitrate:" |
+ << static_cast<uint32_t>(adjusted_bitrate_bps); |
+ adjusted_bitrate_bps_ = adjusted_bitrate_bps; |
+ } |
+ } |
+ last_bitrate_update_time_ms_ = current_time_ms; |
+ frames_since_last_update_ = 0; |
+} |
+ |
+} // namespace webrtc |