Index: webrtc/modules/video_coding/bitrate_adjuster_unittest.cc |
diff --git a/webrtc/modules/video_coding/bitrate_adjuster_unittest.cc b/webrtc/modules/video_coding/bitrate_adjuster_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..34d9ec17a0005e5832ca36c4cec149add921a49e |
--- /dev/null |
+++ b/webrtc/modules/video_coding/bitrate_adjuster_unittest.cc |
@@ -0,0 +1,97 @@ |
+/* |
+ * 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 "testing/gtest/include/gtest/gtest.h" |
+ |
+#include "webrtc/modules/video_coding/include/bitrate_adjuster.h" |
+#include "webrtc/system_wrappers/include/clock.h" |
+ |
+namespace webrtc { |
+ |
+class BitrateAdjusterTest : public ::testing::Test { |
+ public: |
+ BitrateAdjusterTest() |
+ : clock_(0), adjuster_(&clock_) {} |
+ |
+ // Simulate an output bitrate for one update cycle of BitrateAdjuster. |
+ void SimulateBitrate(uint32_t bitrate_in_kbit) { |
+ const uint32_t update_interval_ms = |
+ BitrateAdjuster::kBitrateUpdateIntervalMs; |
+ const uint32_t update_frame_interval = |
+ BitrateAdjuster::kBitrateUpdateFrameInterval; |
+ // Round up frame interval so we get one cycle passes. |
+ const uint32_t frame_interval_ms = |
+ (update_interval_ms + update_frame_interval - 1) / |
+ update_frame_interval; |
+ const size_t frame_size_bytes = (bitrate_in_kbit * frame_interval_ms) / 8; |
+ for (size_t i = 0; i < update_frame_interval; ++i) { |
+ clock_.AdvanceTimeMilliseconds(frame_interval_ms); |
+ adjuster_.Update(frame_size_bytes); |
+ } |
+ } |
+ |
+ void VerifyAdjustment() { |
+ // The adjusted bitrate should be between the estimated bitrate and the |
+ // target bitrate within clamp. |
+ uint32_t target_bitrate = adjuster_.GetTargetBitrate(); |
+ uint32_t adjusted_bitrate = adjuster_.GetAdjustedBitrate(); |
+ uint32_t estimated_bitrate = adjuster_.GetEstimatedBitrate(); |
+ uint32_t adjusted_lower_bound = adjuster_.GetMinAdjustedBitrate(); |
+ uint32_t adjusted_upper_bound = adjuster_.GetMaxAdjustedBitrate(); |
+ EXPECT_LE(adjusted_bitrate, adjusted_upper_bound); |
+ EXPECT_GE(adjusted_bitrate, adjusted_lower_bound); |
+ if (estimated_bitrate > target_bitrate) { |
+ EXPECT_LT(adjusted_bitrate, target_bitrate); |
+ } |
+ } |
+ |
+ protected: |
+ SimulatedClock clock_; |
+ BitrateAdjuster adjuster_; |
+}; |
+ |
+TEST_F(BitrateAdjusterTest, VaryingBitrates) { |
+ const uint32_t target_bitrate = 640; |
+ adjuster_.SetTargetBitrate(target_bitrate); |
+ |
+ // Grossly overshoot for a little while. Adjusted bitrate should decrease. |
+ uint32_t actual_bitrate = 2 * target_bitrate; |
+ uint32_t last_adjusted_bitrate = 0; |
+ uint32_t adjusted_bitrate = 0; |
+ |
+ SimulateBitrate(actual_bitrate); |
+ VerifyAdjustment(); |
+ last_adjusted_bitrate = adjuster_.GetAdjustedBitrate(); |
+ |
+ SimulateBitrate(actual_bitrate); |
+ VerifyAdjustment(); |
+ adjusted_bitrate = adjuster_.GetAdjustedBitrate(); |
+ EXPECT_LT(adjusted_bitrate, last_adjusted_bitrate); |
+ last_adjusted_bitrate = adjusted_bitrate; |
+ // After two cycles we should've stabilized and hit the lower bound. |
+ EXPECT_EQ(adjuster_.GetMinAdjustedBitrate(), adjusted_bitrate); |
+ |
+ // Simulate encoder settling down. Adjusted bitrate should increase. |
+ SimulateBitrate(target_bitrate); |
+ adjusted_bitrate = adjuster_.GetAdjustedBitrate(); |
+ VerifyAdjustment(); |
+ EXPECT_GT(adjusted_bitrate, last_adjusted_bitrate); |
+ last_adjusted_bitrate = adjusted_bitrate; |
+ |
+ SimulateBitrate(target_bitrate); |
+ adjusted_bitrate = adjuster_.GetAdjustedBitrate(); |
+ VerifyAdjustment(); |
+ EXPECT_GT(adjusted_bitrate, last_adjusted_bitrate); |
+ last_adjusted_bitrate = adjusted_bitrate; |
+ // After two cycles we should've stabilized and hit the upper bound. |
+ EXPECT_EQ(adjuster_.GetMaxAdjustedBitrate(), adjusted_bitrate); |
+} |
+ |
+} // namespace webrtc |