webrtc_m130/webrtc/modules/video_coding/utility/frame_dropper_unittest.cc
isheriff 7620be8492 Frame dropper improvements & cleanup
1. Fix the case of key frame accumulation being incorrect due to the chunk
    size being computed at the time of leak based on input frame rate. The issue
    is that the count is computed based on key frame ratio and the actual chunk
    size computed from current input frame rate. These can be wildly different
    especially at the beginning of the stream (key frame ratio defaults based
    on 30 fps) resulting in incorrect key frame accumulation causing large frame
    drops when the input frame rate is low.

    2. Add large delta frame compensation. The current code accounts for key frames
    but not large delta frames. This is a common occurence in some application
    (remote desktop as an example)

    3. Fixes an issue identified by the unit tests. The accumulation of
    key frames had an issue in the scenario of a high key frame ratio where
    the full key frame was not being accounted for.

    3. Removes fast mode and other methods that are mostly dead code.

    4. Cleans up variable names as per chromium style.

Review URL: https://codereview.webrtc.org/1750493002

Cr-Commit-Position: refs/heads/master@{#11884}
2016-03-07 07:22:42 +00:00

162 lines
5.4 KiB
C++

/*
* 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 "webrtc/modules/video_coding/utility/frame_dropper.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webrtc/base/logging.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