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