Evan Shrubsole | 9a99905 | 2021-12-12 14:27:00 | [diff] [blame] | 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/frame_decode_timing.h" |
| 12 | |
| 13 | #include <stdint.h> |
| 14 | |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 15 | #include <optional> |
| 16 | |
Evan Shrubsole | 9a99905 | 2021-12-12 14:27:00 | [diff] [blame] | 17 | #include "api/units/time_delta.h" |
Rasmus Brandt | c4d253c | 2022-05-25 10:03:35 | [diff] [blame] | 18 | #include "modules/video_coding/timing/timing.h" |
Evan Shrubsole | 9a99905 | 2021-12-12 14:27:00 | [diff] [blame] | 19 | #include "rtc_base/containers/flat_map.h" |
| 20 | #include "test/gmock.h" |
| 21 | #include "test/gtest.h" |
Jonas Oreland | e02f9ee | 2022-03-25 11:43:14 | [diff] [blame] | 22 | #include "test/scoped_key_value_config.h" |
Evan Shrubsole | 3fa9a66 | 2022-06-13 14:19:10 | [diff] [blame] | 23 | #include "video/video_receive_stream2.h" |
Evan Shrubsole | 9a99905 | 2021-12-12 14:27:00 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
| 27 | using ::testing::AllOf; |
| 28 | using ::testing::Eq; |
| 29 | using ::testing::Field; |
| 30 | using ::testing::Optional; |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | class FakeVCMTiming : public webrtc::VCMTiming { |
| 35 | public: |
Jonas Oreland | e62c2f2 | 2022-03-29 09:04:48 | [diff] [blame] | 36 | explicit FakeVCMTiming(Clock* clock, const FieldTrialsView& field_trials) |
Jonas Oreland | e02f9ee | 2022-03-25 11:43:14 | [diff] [blame] | 37 | : webrtc::VCMTiming(clock, field_trials) {} |
Evan Shrubsole | 9a99905 | 2021-12-12 14:27:00 | [diff] [blame] | 38 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 14:13:55 | [diff] [blame] | 39 | Timestamp RenderTime(uint32_t frame_timestamp, Timestamp now) const override { |
Evan Shrubsole | 9a99905 | 2021-12-12 14:27:00 | [diff] [blame] | 40 | RTC_DCHECK(render_time_map_.contains(frame_timestamp)); |
| 41 | auto it = render_time_map_.find(frame_timestamp); |
Evan Shrubsole | d6cdf80 | 2022-03-02 14:13:55 | [diff] [blame] | 42 | return it->second; |
Evan Shrubsole | 9a99905 | 2021-12-12 14:27:00 | [diff] [blame] | 43 | } |
| 44 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 14:13:55 | [diff] [blame] | 45 | TimeDelta MaxWaitingTime(Timestamp render_time, |
| 46 | Timestamp now, |
| 47 | bool too_many_frames_queued) const override { |
Evan Shrubsole | 9a99905 | 2021-12-12 14:27:00 | [diff] [blame] | 48 | RTC_DCHECK(wait_time_map_.contains(render_time)); |
| 49 | auto it = wait_time_map_.find(render_time); |
Evan Shrubsole | d6cdf80 | 2022-03-02 14:13:55 | [diff] [blame] | 50 | return it->second; |
Evan Shrubsole | 9a99905 | 2021-12-12 14:27:00 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void SetTimes(uint32_t frame_timestamp, |
| 54 | Timestamp render_time, |
| 55 | TimeDelta max_decode_wait) { |
| 56 | render_time_map_.insert_or_assign(frame_timestamp, render_time); |
| 57 | wait_time_map_.insert_or_assign(render_time, max_decode_wait); |
| 58 | } |
| 59 | |
| 60 | protected: |
| 61 | flat_map<uint32_t, Timestamp> render_time_map_; |
| 62 | flat_map<Timestamp, TimeDelta> wait_time_map_; |
| 63 | }; |
| 64 | } // namespace |
| 65 | |
| 66 | class FrameDecodeTimingTest : public ::testing::Test { |
| 67 | public: |
| 68 | FrameDecodeTimingTest() |
| 69 | : clock_(Timestamp::Millis(1000)), |
Jonas Oreland | e02f9ee | 2022-03-25 11:43:14 | [diff] [blame] | 70 | timing_(&clock_, field_trials_), |
Evan Shrubsole | 9a99905 | 2021-12-12 14:27:00 | [diff] [blame] | 71 | frame_decode_scheduler_(&clock_, &timing_) {} |
| 72 | |
| 73 | protected: |
Jonas Oreland | e02f9ee | 2022-03-25 11:43:14 | [diff] [blame] | 74 | test::ScopedKeyValueConfig field_trials_; |
Evan Shrubsole | 9a99905 | 2021-12-12 14:27:00 | [diff] [blame] | 75 | SimulatedClock clock_; |
| 76 | FakeVCMTiming timing_; |
| 77 | FrameDecodeTiming frame_decode_scheduler_; |
| 78 | }; |
| 79 | |
| 80 | TEST_F(FrameDecodeTimingTest, ReturnsWaitTimesWhenValid) { |
| 81 | const TimeDelta decode_delay = TimeDelta::Millis(42); |
| 82 | const Timestamp render_time = clock_.CurrentTime() + TimeDelta::Millis(60); |
| 83 | timing_.SetTimes(90000, render_time, decode_delay); |
| 84 | |
Evan Shrubsole | 3fa9a66 | 2022-06-13 14:19:10 | [diff] [blame] | 85 | EXPECT_THAT(frame_decode_scheduler_.OnFrameBufferUpdated( |
| 86 | 90000, 180000, kMaxWaitForFrame, false), |
| 87 | Optional(AllOf( |
| 88 | Field(&FrameDecodeTiming::FrameSchedule::latest_decode_time, |
| 89 | Eq(clock_.CurrentTime() + decode_delay)), |
| 90 | Field(&FrameDecodeTiming::FrameSchedule::render_time, |
| 91 | Eq(render_time))))); |
Evan Shrubsole | 9a99905 | 2021-12-12 14:27:00 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | TEST_F(FrameDecodeTimingTest, FastForwardsFrameTooFarInThePast) { |
Evan Shrubsole | f7a1937 | 2022-02-14 13:05:10 | [diff] [blame] | 95 | const TimeDelta decode_delay = |
| 96 | -FrameDecodeTiming::kMaxAllowedFrameDelay - TimeDelta::Millis(1); |
Evan Shrubsole | 9a99905 | 2021-12-12 14:27:00 | [diff] [blame] | 97 | const Timestamp render_time = clock_.CurrentTime(); |
| 98 | timing_.SetTimes(90000, render_time, decode_delay); |
| 99 | |
Evan Shrubsole | 3fa9a66 | 2022-06-13 14:19:10 | [diff] [blame] | 100 | EXPECT_THAT(frame_decode_scheduler_.OnFrameBufferUpdated( |
| 101 | 90000, 180000, kMaxWaitForFrame, false), |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 102 | Eq(std::nullopt)); |
Evan Shrubsole | 9a99905 | 2021-12-12 14:27:00 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | TEST_F(FrameDecodeTimingTest, NoFastForwardIfOnlyFrameToDecode) { |
Evan Shrubsole | f7a1937 | 2022-02-14 13:05:10 | [diff] [blame] | 106 | const TimeDelta decode_delay = |
| 107 | -FrameDecodeTiming::kMaxAllowedFrameDelay - TimeDelta::Millis(1); |
Evan Shrubsole | 9a99905 | 2021-12-12 14:27:00 | [diff] [blame] | 108 | const Timestamp render_time = clock_.CurrentTime(); |
| 109 | timing_.SetTimes(90000, render_time, decode_delay); |
| 110 | |
Evan Shrubsole | f7a1937 | 2022-02-14 13:05:10 | [diff] [blame] | 111 | // Negative `decode_delay` means that `latest_decode_time` is now. |
Evan Shrubsole | 3fa9a66 | 2022-06-13 14:19:10 | [diff] [blame] | 112 | EXPECT_THAT(frame_decode_scheduler_.OnFrameBufferUpdated( |
| 113 | 90000, 90000, kMaxWaitForFrame, false), |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 114 | Optional(AllOf( |
| 115 | Field(&FrameDecodeTiming::FrameSchedule::latest_decode_time, |
Evan Shrubsole | f7a1937 | 2022-02-14 13:05:10 | [diff] [blame] | 116 | Eq(clock_.CurrentTime())), |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 117 | Field(&FrameDecodeTiming::FrameSchedule::render_time, |
| 118 | Eq(render_time))))); |
Evan Shrubsole | 9a99905 | 2021-12-12 14:27:00 | [diff] [blame] | 119 | } |
| 120 | |
Evan Shrubsole | 3fa9a66 | 2022-06-13 14:19:10 | [diff] [blame] | 121 | TEST_F(FrameDecodeTimingTest, MaxWaitCapped) { |
| 122 | TimeDelta frame_delay = TimeDelta::Millis(30); |
| 123 | const TimeDelta decode_delay = TimeDelta::Seconds(3); |
| 124 | const Timestamp render_time = clock_.CurrentTime() + TimeDelta::Seconds(3); |
| 125 | timing_.SetTimes(90000, render_time, decode_delay); |
| 126 | timing_.SetTimes(180000, render_time + frame_delay, |
| 127 | decode_delay + frame_delay); |
| 128 | |
| 129 | EXPECT_THAT(frame_decode_scheduler_.OnFrameBufferUpdated( |
| 130 | 90000, 270000, kMaxWaitForFrame, false), |
| 131 | Optional(AllOf( |
| 132 | Field(&FrameDecodeTiming::FrameSchedule::latest_decode_time, |
| 133 | Eq(clock_.CurrentTime() + kMaxWaitForFrame)), |
| 134 | Field(&FrameDecodeTiming::FrameSchedule::render_time, |
| 135 | Eq(render_time))))); |
| 136 | |
| 137 | // Test cap keyframe. |
| 138 | clock_.AdvanceTime(frame_delay); |
| 139 | EXPECT_THAT(frame_decode_scheduler_.OnFrameBufferUpdated( |
| 140 | 180000, 270000, kMaxWaitForKeyFrame, false), |
| 141 | Optional(AllOf( |
| 142 | Field(&FrameDecodeTiming::FrameSchedule::latest_decode_time, |
| 143 | Eq(clock_.CurrentTime() + kMaxWaitForKeyFrame)), |
| 144 | Field(&FrameDecodeTiming::FrameSchedule::render_time, |
| 145 | Eq(render_time + frame_delay))))); |
| 146 | } |
| 147 | |
Evan Shrubsole | 9a99905 | 2021-12-12 14:27:00 | [diff] [blame] | 148 | } // namespace webrtc |