isheriff | 7620be8 | 2016-03-06 23:22:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 11 | #include "modules/video_coding/utility/frame_dropper.h" |
isheriff | 7620be8 | 2016-03-06 23:22:33 -0800 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 13 | #include "rtc_base/logging.h" |
| 14 | #include "test/gtest.h" |
isheriff | 7620be8 | 2016-03-06 23:22:33 -0800 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | const float kTargetBitRateKbps = 300; |
| 21 | const float kIncomingFrameRate = 30; |
| 22 | const size_t kFrameSizeBytes = 1250; |
| 23 | |
| 24 | const size_t kLargeFrameSizeBytes = 25000; |
| 25 | |
| 26 | const bool kIncludeKeyFrame = true; |
| 27 | const bool kDoNotIncludeKeyFrame = false; |
| 28 | |
| 29 | } // namespace |
| 30 | |
| 31 | class FrameDropperTest : public ::testing::Test { |
| 32 | protected: |
| 33 | void SetUp() override { |
| 34 | frame_dropper_.SetRates(kTargetBitRateKbps, kIncomingFrameRate); |
| 35 | } |
| 36 | |
| 37 | void OverflowLeakyBucket() { |
| 38 | // Overflow bucket in frame dropper. |
| 39 | for (int i = 0; i < kIncomingFrameRate; ++i) { |
| 40 | frame_dropper_.Fill(kFrameSizeBytes, true); |
| 41 | } |
| 42 | frame_dropper_.Leak(kIncomingFrameRate); |
| 43 | } |
| 44 | |
| 45 | void ValidateNoDropsAtTargetBitrate(int large_frame_size_bytes, |
| 46 | int large_frame_rate, |
| 47 | bool is_large_frame_delta) { |
| 48 | // Smaller frame size is computed to meet |kTargetBitRateKbps|. |
| 49 | int small_frame_size_bytes = |
| 50 | kFrameSizeBytes - |
| 51 | (large_frame_size_bytes * large_frame_rate) / kIncomingFrameRate; |
| 52 | |
| 53 | for (int i = 1; i <= 5 * large_frame_rate; ++i) { |
| 54 | // Large frame. First frame is always a key frame. |
| 55 | frame_dropper_.Fill(large_frame_size_bytes, |
| 56 | (i == 1) ? false : is_large_frame_delta); |
| 57 | frame_dropper_.Leak(kIncomingFrameRate); |
| 58 | EXPECT_FALSE(frame_dropper_.DropFrame()); |
| 59 | |
| 60 | // Smaller frames. |
| 61 | for (int j = 1; j < kIncomingFrameRate / large_frame_rate; ++j) { |
| 62 | frame_dropper_.Fill(small_frame_size_bytes, true); |
| 63 | frame_dropper_.Leak(kIncomingFrameRate); |
| 64 | EXPECT_FALSE(frame_dropper_.DropFrame()); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void ValidateThroughputMatchesTargetBitrate(int bitrate_kbps, |
| 70 | bool include_keyframe) { |
| 71 | int delta_frame_size; |
| 72 | int total_bytes = 0; |
| 73 | |
| 74 | if (include_keyframe) { |
| 75 | delta_frame_size = ((1000.0 / 8 * bitrate_kbps) - kLargeFrameSizeBytes) / |
| 76 | (kIncomingFrameRate - 1); |
| 77 | } else { |
| 78 | delta_frame_size = bitrate_kbps * 1000.0 / (8 * kIncomingFrameRate); |
| 79 | } |
| 80 | const int kNumIterations = 1000; |
| 81 | for (int i = 1; i <= kNumIterations; ++i) { |
| 82 | int j = 0; |
| 83 | if (include_keyframe) { |
| 84 | if (!frame_dropper_.DropFrame()) { |
| 85 | frame_dropper_.Fill(kLargeFrameSizeBytes, false); |
| 86 | total_bytes += kLargeFrameSizeBytes; |
| 87 | } |
| 88 | frame_dropper_.Leak(kIncomingFrameRate); |
| 89 | j++; |
| 90 | } |
| 91 | for (; j < kIncomingFrameRate; ++j) { |
| 92 | if (!frame_dropper_.DropFrame()) { |
| 93 | frame_dropper_.Fill(delta_frame_size, true); |
| 94 | total_bytes += delta_frame_size; |
| 95 | } |
| 96 | frame_dropper_.Leak(kIncomingFrameRate); |
| 97 | } |
| 98 | } |
| 99 | float throughput_kbps = total_bytes * 8.0 / (1000 * kNumIterations); |
| 100 | float deviation_from_target = |
| 101 | (throughput_kbps - kTargetBitRateKbps) * 100.0 / kTargetBitRateKbps; |
| 102 | if (deviation_from_target < 0) { |
| 103 | deviation_from_target = -deviation_from_target; |
| 104 | } |
| 105 | |
| 106 | // Variation is < 0.1% |
| 107 | EXPECT_LE(deviation_from_target, 0.1); |
| 108 | } |
| 109 | |
| 110 | FrameDropper frame_dropper_; |
| 111 | }; |
| 112 | |
| 113 | TEST_F(FrameDropperTest, NoDropsWhenDisabled) { |
| 114 | frame_dropper_.Enable(false); |
| 115 | OverflowLeakyBucket(); |
| 116 | EXPECT_FALSE(frame_dropper_.DropFrame()); |
| 117 | } |
| 118 | |
| 119 | TEST_F(FrameDropperTest, DropsByDefaultWhenBucketOverflows) { |
| 120 | OverflowLeakyBucket(); |
| 121 | EXPECT_TRUE(frame_dropper_.DropFrame()); |
| 122 | } |
| 123 | |
| 124 | TEST_F(FrameDropperTest, NoDropsWhenFillRateMatchesLeakRate) { |
| 125 | for (int i = 0; i < 5 * kIncomingFrameRate; ++i) { |
| 126 | frame_dropper_.Fill(kFrameSizeBytes, true); |
| 127 | frame_dropper_.Leak(kIncomingFrameRate); |
| 128 | EXPECT_FALSE(frame_dropper_.DropFrame()); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | TEST_F(FrameDropperTest, LargeKeyFrames) { |
| 133 | ValidateNoDropsAtTargetBitrate(kLargeFrameSizeBytes, 1, false); |
| 134 | frame_dropper_.Reset(); |
| 135 | ValidateNoDropsAtTargetBitrate(kLargeFrameSizeBytes / 2, 2, false); |
| 136 | frame_dropper_.Reset(); |
| 137 | ValidateNoDropsAtTargetBitrate(kLargeFrameSizeBytes / 4, 4, false); |
| 138 | frame_dropper_.Reset(); |
| 139 | ValidateNoDropsAtTargetBitrate(kLargeFrameSizeBytes / 8, 8, false); |
| 140 | } |
| 141 | |
| 142 | TEST_F(FrameDropperTest, LargeDeltaFrames) { |
| 143 | ValidateNoDropsAtTargetBitrate(kLargeFrameSizeBytes, 1, true); |
| 144 | frame_dropper_.Reset(); |
| 145 | ValidateNoDropsAtTargetBitrate(kLargeFrameSizeBytes / 2, 2, true); |
| 146 | frame_dropper_.Reset(); |
| 147 | ValidateNoDropsAtTargetBitrate(kLargeFrameSizeBytes / 4, 4, true); |
| 148 | frame_dropper_.Reset(); |
| 149 | ValidateNoDropsAtTargetBitrate(kLargeFrameSizeBytes / 8, 8, true); |
| 150 | } |
| 151 | |
| 152 | TEST_F(FrameDropperTest, TrafficVolumeAboveAvailableBandwidth) { |
| 153 | ValidateThroughputMatchesTargetBitrate(700, kIncludeKeyFrame); |
| 154 | ValidateThroughputMatchesTargetBitrate(700, kDoNotIncludeKeyFrame); |
| 155 | ValidateThroughputMatchesTargetBitrate(600, kIncludeKeyFrame); |
| 156 | ValidateThroughputMatchesTargetBitrate(600, kDoNotIncludeKeyFrame); |
| 157 | ValidateThroughputMatchesTargetBitrate(500, kIncludeKeyFrame); |
| 158 | ValidateThroughputMatchesTargetBitrate(500, kDoNotIncludeKeyFrame); |
| 159 | } |
| 160 | |
| 161 | } // namespace webrtc |