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