Sergey Silkin | ae3144c | 2018-08-29 01:05:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | |
| 11 | #include "modules/video_coding/utility/framerate_controller.h" |
| 12 | |
| 13 | #include "test/gtest.h" |
| 14 | |
| 15 | namespace webrtc { |
| 16 | |
| 17 | TEST(FramerateController, KeepTargetFramerate) { |
| 18 | const float input_framerate_fps = 20; |
| 19 | const float target_framerate_fps = 5; |
| 20 | const float max_abs_framerate_error_fps = target_framerate_fps * 0.1f; |
| 21 | const size_t input_duration_secs = 3; |
| 22 | const size_t num_input_frames = input_duration_secs * input_framerate_fps; |
| 23 | |
| 24 | FramerateController framerate_controller(target_framerate_fps); |
| 25 | size_t num_dropped_frames = 0; |
| 26 | for (size_t frame_num = 0; frame_num < num_input_frames; ++frame_num) { |
| 27 | const uint32_t timestamp_ms = |
| 28 | static_cast<uint32_t>(1000 * frame_num / input_framerate_fps); |
| 29 | if (framerate_controller.DropFrame(timestamp_ms)) { |
| 30 | ++num_dropped_frames; |
| 31 | } else { |
| 32 | framerate_controller.AddFrame(timestamp_ms); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | const float output_framerate_fps = |
| 37 | static_cast<float>(num_input_frames - num_dropped_frames) / |
| 38 | input_duration_secs; |
| 39 | EXPECT_NEAR(output_framerate_fps, target_framerate_fps, |
| 40 | max_abs_framerate_error_fps); |
| 41 | } |
| 42 | |
| 43 | TEST(FramerateController, DoNotDropAnyFramesIfTargerEqualsInput) { |
| 44 | const float input_framerate_fps = 30; |
| 45 | const size_t input_duration_secs = 3; |
| 46 | const size_t num_input_frames = input_duration_secs * input_framerate_fps; |
| 47 | |
| 48 | FramerateController framerate_controller(input_framerate_fps); |
| 49 | size_t num_dropped_frames = 0; |
| 50 | for (size_t frame_num = 0; frame_num < num_input_frames; ++frame_num) { |
| 51 | const uint32_t timestamp_ms = |
| 52 | static_cast<uint32_t>(1000 * frame_num / input_framerate_fps); |
| 53 | if (framerate_controller.DropFrame(timestamp_ms)) { |
| 54 | ++num_dropped_frames; |
| 55 | } else { |
| 56 | framerate_controller.AddFrame(timestamp_ms); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | EXPECT_EQ(num_dropped_frames, 0U); |
| 61 | } |
| 62 | |
| 63 | TEST(FramerateController, DoNotDropFrameWhenTimestampJumpsBackward) { |
| 64 | FramerateController framerate_controller(30); |
| 65 | ASSERT_FALSE(framerate_controller.DropFrame(66)); |
| 66 | framerate_controller.AddFrame(66); |
| 67 | EXPECT_FALSE(framerate_controller.DropFrame(33)); |
| 68 | } |
| 69 | |
| 70 | TEST(FramerateController, DropFrameIfItIsTooCloseToPreviousFrame) { |
| 71 | FramerateController framerate_controller(30); |
| 72 | ASSERT_FALSE(framerate_controller.DropFrame(33)); |
| 73 | framerate_controller.AddFrame(33); |
| 74 | EXPECT_TRUE(framerate_controller.DropFrame(34)); |
| 75 | } |
| 76 | |
| 77 | TEST(FramerateController, FrameDroppingStartsFromSecondInputFrame) { |
| 78 | const float input_framerate_fps = 23; |
| 79 | const float target_framerate_fps = 19; |
| 80 | const uint32_t input_frame_duration_ms = |
| 81 | static_cast<uint32_t>(1000 / input_framerate_fps); |
| 82 | FramerateController framerate_controller(target_framerate_fps); |
| 83 | ASSERT_FALSE(framerate_controller.DropFrame(1 * input_frame_duration_ms)); |
| 84 | framerate_controller.AddFrame(1 * input_frame_duration_ms); |
| 85 | EXPECT_TRUE(framerate_controller.DropFrame(2 * input_frame_duration_ms)); |
| 86 | } |
| 87 | |
| 88 | } // namespace webrtc |