1 /*
2 * Copyright (c) 2022 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 "video/video_receive_stream_timeout_tracker.h"
12
13 #include <utility>
14 #include <vector>
15
16 #include "api/task_queue/task_queue_base.h"
17 #include "test/gmock.h"
18 #include "test/gtest.h"
19 #include "test/time_controller/simulated_time_controller.h"
20
21 namespace webrtc {
22
23 namespace {
24
25 constexpr auto kMaxWaitForKeyframe = TimeDelta::Millis(500);
26 constexpr auto kMaxWaitForFrame = TimeDelta::Millis(1500);
27 constexpr VideoReceiveStreamTimeoutTracker::Timeouts config = {
28 kMaxWaitForKeyframe, kMaxWaitForFrame};
29 } // namespace
30
31 class VideoReceiveStreamTimeoutTrackerTest : public ::testing::Test {
32 public:
VideoReceiveStreamTimeoutTrackerTest()33 VideoReceiveStreamTimeoutTrackerTest()
34 : time_controller_(Timestamp::Millis(2000)),
35 timeout_tracker_(time_controller_.GetClock(),
36 time_controller_.GetMainThread(),
37 config,
38 [this](TimeDelta delay) { OnTimeout(delay); }) {}
39
40 protected:
41 GlobalSimulatedTimeController time_controller_;
42 VideoReceiveStreamTimeoutTracker timeout_tracker_;
43 std::vector<TimeDelta> timeouts_;
44
45 private:
OnTimeout(TimeDelta delay)46 void OnTimeout(TimeDelta delay) { timeouts_.push_back(delay); }
47 };
48
TEST_F(VideoReceiveStreamTimeoutTrackerTest,TimeoutAfterInitialPeriod)49 TEST_F(VideoReceiveStreamTimeoutTrackerTest, TimeoutAfterInitialPeriod) {
50 timeout_tracker_.Start(true);
51 time_controller_.AdvanceTime(kMaxWaitForKeyframe);
52 EXPECT_THAT(timeouts_, testing::ElementsAre(kMaxWaitForKeyframe));
53 timeout_tracker_.Stop();
54 }
55
TEST_F(VideoReceiveStreamTimeoutTrackerTest,NoTimeoutAfterStop)56 TEST_F(VideoReceiveStreamTimeoutTrackerTest, NoTimeoutAfterStop) {
57 timeout_tracker_.Start(true);
58 time_controller_.AdvanceTime(kMaxWaitForKeyframe / 2);
59 timeout_tracker_.Stop();
60 time_controller_.AdvanceTime(kMaxWaitForKeyframe);
61 EXPECT_THAT(timeouts_, testing::IsEmpty());
62 }
63
TEST_F(VideoReceiveStreamTimeoutTrackerTest,TimeoutForDeltaFrame)64 TEST_F(VideoReceiveStreamTimeoutTrackerTest, TimeoutForDeltaFrame) {
65 timeout_tracker_.Start(true);
66 time_controller_.AdvanceTime(TimeDelta::Millis(5));
67 timeout_tracker_.OnEncodedFrameReleased();
68 time_controller_.AdvanceTime(kMaxWaitForFrame);
69 EXPECT_THAT(timeouts_, testing::ElementsAre(kMaxWaitForFrame));
70 timeout_tracker_.Stop();
71 }
72
TEST_F(VideoReceiveStreamTimeoutTrackerTest,TimeoutForKeyframeWhenForced)73 TEST_F(VideoReceiveStreamTimeoutTrackerTest, TimeoutForKeyframeWhenForced) {
74 timeout_tracker_.Start(true);
75 time_controller_.AdvanceTime(TimeDelta::Millis(5));
76 timeout_tracker_.OnEncodedFrameReleased();
77 timeout_tracker_.SetWaitingForKeyframe();
78 time_controller_.AdvanceTime(kMaxWaitForKeyframe);
79 EXPECT_THAT(timeouts_, testing::ElementsAre(kMaxWaitForKeyframe));
80 timeout_tracker_.Stop();
81 }
82
TEST_F(VideoReceiveStreamTimeoutTrackerTest,TotalTimeoutUsedInCallback)83 TEST_F(VideoReceiveStreamTimeoutTrackerTest, TotalTimeoutUsedInCallback) {
84 timeout_tracker_.Start(true);
85 time_controller_.AdvanceTime(kMaxWaitForKeyframe * 2);
86 timeout_tracker_.OnEncodedFrameReleased();
87 time_controller_.AdvanceTime(kMaxWaitForFrame * 2);
88 EXPECT_THAT(timeouts_,
89 testing::ElementsAre(kMaxWaitForKeyframe, kMaxWaitForKeyframe * 2,
90 kMaxWaitForFrame, kMaxWaitForFrame * 2));
91 timeout_tracker_.Stop();
92 }
93
94 } // namespace webrtc
95