Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [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/decode_synchronizer.h" |
| 12 | |
| 13 | #include <stddef.h> |
| 14 | |
| 15 | #include <memory> |
| 16 | #include <utility> |
| 17 | |
Markus Handell | be400e4 | 2022-11-08 11:14:23 | [diff] [blame] | 18 | #include "absl/functional/any_invocable.h" |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 19 | #include "api/metronome/test/fake_metronome.h" |
| 20 | #include "api/units/time_delta.h" |
| 21 | #include "test/gmock.h" |
| 22 | #include "test/gtest.h" |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 23 | #include "test/time_controller/simulated_time_controller.h" |
| 24 | #include "video/frame_decode_scheduler.h" |
| 25 | #include "video/frame_decode_timing.h" |
| 26 | |
| 27 | using ::testing::_; |
| 28 | using ::testing::Eq; |
Markus Handell | be400e4 | 2022-11-08 11:14:23 | [diff] [blame] | 29 | using ::testing::Invoke; |
Markus Handell | aee5b17 | 2023-04-28 09:16:30 | [diff] [blame] | 30 | using ::testing::Mock; |
| 31 | using ::testing::MockFunction; |
Markus Handell | be400e4 | 2022-11-08 11:14:23 | [diff] [blame] | 32 | using ::testing::Return; |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 33 | |
| 34 | namespace webrtc { |
| 35 | |
Markus Handell | be400e4 | 2022-11-08 11:14:23 | [diff] [blame] | 36 | class MockMetronome : public Metronome { |
| 37 | public: |
| 38 | MOCK_METHOD(void, |
| 39 | RequestCallOnNextTick, |
| 40 | (absl::AnyInvocable<void() &&> callback), |
| 41 | (override)); |
| 42 | MOCK_METHOD(TimeDelta, TickPeriod, (), (const override)); |
| 43 | }; |
| 44 | |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 45 | class DecodeSynchronizerTest : public ::testing::Test { |
| 46 | public: |
| 47 | static constexpr TimeDelta kTickPeriod = TimeDelta::Millis(33); |
| 48 | |
| 49 | DecodeSynchronizerTest() |
| 50 | : time_controller_(Timestamp::Millis(1337)), |
| 51 | clock_(time_controller_.GetClock()), |
| 52 | metronome_(kTickPeriod), |
Evan Shrubsole | 7cbd8de | 2022-08-16 08:08:53 | [diff] [blame] | 53 | decode_synchronizer_(clock_, |
| 54 | &metronome_, |
| 55 | time_controller_.GetMainThread()) {} |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 56 | |
| 57 | protected: |
| 58 | GlobalSimulatedTimeController time_controller_; |
| 59 | Clock* clock_; |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 60 | test::ForcedTickMetronome metronome_; |
| 61 | DecodeSynchronizer decode_synchronizer_; |
| 62 | }; |
| 63 | |
| 64 | TEST_F(DecodeSynchronizerTest, AllFramesReadyBeforeNextTickDecoded) { |
Markus Handell | aee5b17 | 2023-04-28 09:16:30 | [diff] [blame] | 65 | MockFunction<void(uint32_t, Timestamp)> mock_callback1; |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 66 | auto scheduler1 = decode_synchronizer_.CreateSynchronizedFrameScheduler(); |
| 67 | |
Markus Handell | aee5b17 | 2023-04-28 09:16:30 | [diff] [blame] | 68 | MockFunction<void(unsigned int, Timestamp)> mock_callback2; |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 69 | auto scheduler2 = decode_synchronizer_.CreateSynchronizedFrameScheduler(); |
| 70 | |
| 71 | { |
| 72 | uint32_t frame_rtp = 90000; |
| 73 | FrameDecodeTiming::FrameSchedule frame_sched{ |
| 74 | .latest_decode_time = |
| 75 | clock_->CurrentTime() + kTickPeriod - TimeDelta::Millis(3), |
| 76 | .render_time = clock_->CurrentTime() + TimeDelta::Millis(60)}; |
| 77 | scheduler1->ScheduleFrame(frame_rtp, frame_sched, |
| 78 | mock_callback1.AsStdFunction()); |
| 79 | EXPECT_CALL(mock_callback1, |
| 80 | Call(Eq(frame_rtp), Eq(frame_sched.render_time))); |
| 81 | } |
| 82 | { |
| 83 | uint32_t frame_rtp = 123456; |
| 84 | FrameDecodeTiming::FrameSchedule frame_sched{ |
| 85 | .latest_decode_time = |
| 86 | clock_->CurrentTime() + kTickPeriod - TimeDelta::Millis(2), |
| 87 | .render_time = clock_->CurrentTime() + TimeDelta::Millis(70)}; |
| 88 | scheduler2->ScheduleFrame(frame_rtp, frame_sched, |
| 89 | mock_callback2.AsStdFunction()); |
| 90 | EXPECT_CALL(mock_callback2, |
| 91 | Call(Eq(frame_rtp), Eq(frame_sched.render_time))); |
| 92 | } |
| 93 | metronome_.Tick(); |
Evan Shrubsole | 7cbd8de | 2022-08-16 08:08:53 | [diff] [blame] | 94 | time_controller_.AdvanceTime(TimeDelta::Zero()); |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 95 | |
| 96 | // Cleanup |
| 97 | scheduler1->Stop(); |
| 98 | scheduler2->Stop(); |
| 99 | } |
| 100 | |
| 101 | TEST_F(DecodeSynchronizerTest, FramesNotDecodedIfDecodeTimeIsInNextInterval) { |
Markus Handell | aee5b17 | 2023-04-28 09:16:30 | [diff] [blame] | 102 | MockFunction<void(unsigned int, Timestamp)> mock_callback; |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 103 | auto scheduler = decode_synchronizer_.CreateSynchronizedFrameScheduler(); |
| 104 | |
| 105 | uint32_t frame_rtp = 90000; |
| 106 | FrameDecodeTiming::FrameSchedule frame_sched{ |
| 107 | .latest_decode_time = |
| 108 | clock_->CurrentTime() + kTickPeriod + TimeDelta::Millis(10), |
| 109 | .render_time = |
| 110 | clock_->CurrentTime() + kTickPeriod + TimeDelta::Millis(30)}; |
| 111 | scheduler->ScheduleFrame(frame_rtp, frame_sched, |
| 112 | mock_callback.AsStdFunction()); |
| 113 | |
| 114 | metronome_.Tick(); |
Evan Shrubsole | 7cbd8de | 2022-08-16 08:08:53 | [diff] [blame] | 115 | time_controller_.AdvanceTime(TimeDelta::Zero()); |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 116 | // No decodes should have happened in this tick. |
| 117 | ::testing::Mock::VerifyAndClearExpectations(&mock_callback); |
| 118 | |
| 119 | // Decode should happen on next tick. |
| 120 | EXPECT_CALL(mock_callback, Call(Eq(frame_rtp), Eq(frame_sched.render_time))); |
| 121 | time_controller_.AdvanceTime(kTickPeriod); |
| 122 | metronome_.Tick(); |
Evan Shrubsole | 7cbd8de | 2022-08-16 08:08:53 | [diff] [blame] | 123 | time_controller_.AdvanceTime(TimeDelta::Zero()); |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 124 | |
| 125 | // Cleanup |
| 126 | scheduler->Stop(); |
| 127 | } |
| 128 | |
| 129 | TEST_F(DecodeSynchronizerTest, FrameDecodedOnce) { |
Markus Handell | aee5b17 | 2023-04-28 09:16:30 | [diff] [blame] | 130 | MockFunction<void(unsigned int, Timestamp)> mock_callback; |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 131 | auto scheduler = decode_synchronizer_.CreateSynchronizedFrameScheduler(); |
| 132 | |
| 133 | uint32_t frame_rtp = 90000; |
| 134 | FrameDecodeTiming::FrameSchedule frame_sched{ |
| 135 | .latest_decode_time = clock_->CurrentTime() + TimeDelta::Millis(30), |
| 136 | .render_time = clock_->CurrentTime() + TimeDelta::Millis(60)}; |
| 137 | scheduler->ScheduleFrame(frame_rtp, frame_sched, |
| 138 | mock_callback.AsStdFunction()); |
| 139 | EXPECT_CALL(mock_callback, Call(_, _)).Times(1); |
| 140 | metronome_.Tick(); |
Evan Shrubsole | 7cbd8de | 2022-08-16 08:08:53 | [diff] [blame] | 141 | time_controller_.AdvanceTime(TimeDelta::Zero()); |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 142 | ::testing::Mock::VerifyAndClearExpectations(&mock_callback); |
| 143 | |
| 144 | // Trigger tick again. No frame should be decoded now. |
| 145 | time_controller_.AdvanceTime(kTickPeriod); |
| 146 | metronome_.Tick(); |
Evan Shrubsole | 7cbd8de | 2022-08-16 08:08:53 | [diff] [blame] | 147 | time_controller_.AdvanceTime(TimeDelta::Zero()); |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 148 | |
| 149 | // Cleanup |
| 150 | scheduler->Stop(); |
| 151 | } |
| 152 | |
| 153 | TEST_F(DecodeSynchronizerTest, FrameWithDecodeTimeInPastDecodedImmediately) { |
Markus Handell | aee5b17 | 2023-04-28 09:16:30 | [diff] [blame] | 154 | MockFunction<void(unsigned int, Timestamp)> mock_callback; |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 155 | auto scheduler = decode_synchronizer_.CreateSynchronizedFrameScheduler(); |
| 156 | |
| 157 | uint32_t frame_rtp = 90000; |
| 158 | FrameDecodeTiming::FrameSchedule frame_sched{ |
| 159 | .latest_decode_time = clock_->CurrentTime() - TimeDelta::Millis(5), |
| 160 | .render_time = clock_->CurrentTime() + TimeDelta::Millis(30)}; |
| 161 | EXPECT_CALL(mock_callback, Call(Eq(90000u), _)).Times(1); |
| 162 | scheduler->ScheduleFrame(frame_rtp, frame_sched, |
| 163 | mock_callback.AsStdFunction()); |
| 164 | // Verify the callback was invoked already. |
| 165 | ::testing::Mock::VerifyAndClearExpectations(&mock_callback); |
| 166 | |
| 167 | metronome_.Tick(); |
Evan Shrubsole | 7cbd8de | 2022-08-16 08:08:53 | [diff] [blame] | 168 | time_controller_.AdvanceTime(TimeDelta::Zero()); |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 169 | |
| 170 | // Cleanup |
| 171 | scheduler->Stop(); |
| 172 | } |
| 173 | |
| 174 | TEST_F(DecodeSynchronizerTest, |
| 175 | FrameWithDecodeTimeFarBeforeNextTickDecodedImmediately) { |
Markus Handell | aee5b17 | 2023-04-28 09:16:30 | [diff] [blame] | 176 | MockFunction<void(unsigned int, Timestamp)> mock_callback; |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 177 | auto scheduler = decode_synchronizer_.CreateSynchronizedFrameScheduler(); |
| 178 | |
| 179 | // Frame which would be behind by more than kMaxAllowedFrameDelay after |
| 180 | // the next tick. |
| 181 | FrameDecodeTiming::FrameSchedule frame_sched{ |
| 182 | .latest_decode_time = clock_->CurrentTime() + kTickPeriod - |
| 183 | FrameDecodeTiming::kMaxAllowedFrameDelay - |
| 184 | TimeDelta::Millis(1), |
| 185 | .render_time = clock_->CurrentTime() + TimeDelta::Millis(30)}; |
| 186 | EXPECT_CALL(mock_callback, Call(Eq(90000u), _)).Times(1); |
| 187 | scheduler->ScheduleFrame(90000, frame_sched, mock_callback.AsStdFunction()); |
| 188 | // Verify the callback was invoked already. |
| 189 | ::testing::Mock::VerifyAndClearExpectations(&mock_callback); |
| 190 | |
| 191 | time_controller_.AdvanceTime(kTickPeriod); |
| 192 | metronome_.Tick(); |
Evan Shrubsole | 7cbd8de | 2022-08-16 08:08:53 | [diff] [blame] | 193 | time_controller_.AdvanceTime(TimeDelta::Zero()); |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 194 | |
| 195 | // A frame that would be behind by exactly kMaxAllowedFrameDelay after next |
| 196 | // tick should decode at the next tick. |
| 197 | FrameDecodeTiming::FrameSchedule queued_frame{ |
| 198 | .latest_decode_time = clock_->CurrentTime() + kTickPeriod - |
| 199 | FrameDecodeTiming::kMaxAllowedFrameDelay, |
| 200 | .render_time = clock_->CurrentTime() + TimeDelta::Millis(30)}; |
| 201 | scheduler->ScheduleFrame(180000, queued_frame, mock_callback.AsStdFunction()); |
| 202 | // Verify the callback was invoked already. |
| 203 | ::testing::Mock::VerifyAndClearExpectations(&mock_callback); |
| 204 | |
| 205 | EXPECT_CALL(mock_callback, Call(Eq(180000u), _)).Times(1); |
| 206 | time_controller_.AdvanceTime(kTickPeriod); |
| 207 | metronome_.Tick(); |
Evan Shrubsole | 7cbd8de | 2022-08-16 08:08:53 | [diff] [blame] | 208 | time_controller_.AdvanceTime(TimeDelta::Zero()); |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 209 | |
| 210 | // Cleanup |
| 211 | scheduler->Stop(); |
| 212 | } |
| 213 | |
| 214 | TEST_F(DecodeSynchronizerTest, FramesNotReleasedAfterStop) { |
Markus Handell | aee5b17 | 2023-04-28 09:16:30 | [diff] [blame] | 215 | MockFunction<void(unsigned int, Timestamp)> mock_callback; |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 216 | auto scheduler = decode_synchronizer_.CreateSynchronizedFrameScheduler(); |
| 217 | |
| 218 | uint32_t frame_rtp = 90000; |
| 219 | FrameDecodeTiming::FrameSchedule frame_sched{ |
| 220 | .latest_decode_time = clock_->CurrentTime() + TimeDelta::Millis(30), |
| 221 | .render_time = clock_->CurrentTime() + TimeDelta::Millis(60)}; |
| 222 | scheduler->ScheduleFrame(frame_rtp, frame_sched, |
| 223 | mock_callback.AsStdFunction()); |
| 224 | // Cleanup |
| 225 | scheduler->Stop(); |
| 226 | |
| 227 | // No callback should occur on this tick since Stop() was called before. |
| 228 | metronome_.Tick(); |
Evan Shrubsole | 7cbd8de | 2022-08-16 08:08:53 | [diff] [blame] | 229 | time_controller_.AdvanceTime(TimeDelta::Zero()); |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 230 | } |
| 231 | |
Markus Handell | be400e4 | 2022-11-08 11:14:23 | [diff] [blame] | 232 | TEST(DecodeSynchronizerStandaloneTest, |
| 233 | MetronomeNotListenedWhenNoStreamsAreActive) { |
| 234 | GlobalSimulatedTimeController time_controller(Timestamp::Millis(4711)); |
| 235 | Clock* clock(time_controller.GetClock()); |
| 236 | MockMetronome metronome; |
| 237 | ON_CALL(metronome, TickPeriod).WillByDefault(Return(TimeDelta::Seconds(1))); |
| 238 | DecodeSynchronizer decode_synchronizer_(clock, &metronome, |
| 239 | time_controller.GetMainThread()); |
| 240 | absl::AnyInvocable<void() &&> callback; |
| 241 | EXPECT_CALL(metronome, RequestCallOnNextTick) |
| 242 | .WillOnce(Invoke([&callback](absl::AnyInvocable<void() &&> cb) { |
| 243 | callback = std::move(cb); |
| 244 | })); |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 245 | auto scheduler = decode_synchronizer_.CreateSynchronizedFrameScheduler(); |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 246 | auto scheduler2 = decode_synchronizer_.CreateSynchronizedFrameScheduler(); |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 247 | scheduler->Stop(); |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 248 | scheduler2->Stop(); |
Markus Handell | be400e4 | 2022-11-08 11:14:23 | [diff] [blame] | 249 | time_controller.AdvanceTime(TimeDelta::Seconds(1)); |
| 250 | ASSERT_TRUE(callback); |
| 251 | (std::move)(callback)(); |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 252 | } |
| 253 | |
Markus Handell | aee5b17 | 2023-04-28 09:16:30 | [diff] [blame] | 254 | TEST(DecodeSynchronizerStandaloneTest, |
| 255 | RegistersCallbackOnceDuringRepeatedRegistrations) { |
| 256 | GlobalSimulatedTimeController time_controller(Timestamp::Millis(4711)); |
| 257 | Clock* clock(time_controller.GetClock()); |
| 258 | MockMetronome metronome; |
| 259 | ON_CALL(metronome, TickPeriod).WillByDefault(Return(TimeDelta::Seconds(1))); |
| 260 | DecodeSynchronizer decode_synchronizer_(clock, &metronome, |
| 261 | time_controller.GetMainThread()); |
| 262 | // Expect at most 1 call to register a callback. |
| 263 | EXPECT_CALL(metronome, RequestCallOnNextTick); |
| 264 | auto scheduler1 = decode_synchronizer_.CreateSynchronizedFrameScheduler(); |
| 265 | scheduler1->Stop(); |
| 266 | auto scheduler2 = decode_synchronizer_.CreateSynchronizedFrameScheduler(); |
| 267 | Mock::VerifyAndClearExpectations(&metronome); |
| 268 | scheduler2->Stop(); |
| 269 | } |
| 270 | |
Evan Shrubsole | 6cd6d8e | 2022-02-11 14:30:26 | [diff] [blame] | 271 | } // namespace webrtc |