stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/video_coding/timing.h" |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 12 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 13 | #include "api/units/frequency.h" |
| 14 | #include "api/units/time_delta.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "system_wrappers/include/clock.h" |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 16 | #include "test/field_trial.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "test/gtest.h" |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 20 | namespace { |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 21 | |
| 22 | constexpr Frequency k25Fps = Frequency::Hertz(25); |
| 23 | constexpr Frequency k90kHz = Frequency::KiloHertz(90); |
| 24 | |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 25 | } // namespace |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 26 | |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 27 | TEST(ReceiverTimingTest, JitterDelay) { |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 28 | SimulatedClock clock(0); |
| 29 | VCMTiming timing(&clock); |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 30 | timing.Reset(); |
| 31 | |
| 32 | uint32_t timestamp = 0; |
| 33 | timing.UpdateCurrentDelay(timestamp); |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 34 | |
| 35 | timing.Reset(); |
| 36 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 37 | timing.IncomingTimestamp(timestamp, clock.CurrentTime()); |
| 38 | TimeDelta jitter_delay = TimeDelta::Millis(20); |
| 39 | timing.SetJitterDelay(jitter_delay); |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 40 | timing.UpdateCurrentDelay(timestamp); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 41 | timing.set_render_delay(TimeDelta::Zero()); |
| 42 | auto wait_time = timing.MaxWaitingTime( |
| 43 | timing.RenderTime(timestamp, clock.CurrentTime()), clock.CurrentTime(), |
| 44 | /*too_many_frames_queued=*/false); |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 45 | // First update initializes the render time. Since we have no decode delay |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 46 | // we get wait_time = renderTime - now - renderDelay = jitter. |
| 47 | EXPECT_EQ(jitter_delay, wait_time); |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 48 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 49 | jitter_delay += TimeDelta::Millis(VCMTiming::kDelayMaxChangeMsPerS + 10); |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 50 | timestamp += 90000; |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 51 | clock.AdvanceTimeMilliseconds(1000); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 52 | timing.SetJitterDelay(jitter_delay); |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 53 | timing.UpdateCurrentDelay(timestamp); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 54 | wait_time = timing.MaxWaitingTime( |
| 55 | timing.RenderTime(timestamp, clock.CurrentTime()), clock.CurrentTime(), |
| 56 | /*too_many_frames_queued=*/false); |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 57 | // Since we gradually increase the delay we only get 100 ms every second. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 58 | EXPECT_EQ(jitter_delay - TimeDelta::Millis(10), wait_time); |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 59 | |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 60 | timestamp += 90000; |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 61 | clock.AdvanceTimeMilliseconds(1000); |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 62 | timing.UpdateCurrentDelay(timestamp); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 63 | wait_time = timing.MaxWaitingTime( |
| 64 | timing.RenderTime(timestamp, clock.CurrentTime()), clock.CurrentTime(), |
| 65 | /*too_many_frames_queued=*/false); |
| 66 | EXPECT_EQ(jitter_delay, wait_time); |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 67 | |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 68 | // Insert frames without jitter, verify that this gives the exact wait time. |
| 69 | const int kNumFrames = 300; |
| 70 | for (int i = 0; i < kNumFrames; i++) { |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 71 | clock.AdvanceTime(1 / k25Fps); |
| 72 | timestamp += k90kHz / k25Fps; |
| 73 | timing.IncomingTimestamp(timestamp, clock.CurrentTime()); |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 74 | } |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 75 | timing.UpdateCurrentDelay(timestamp); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 76 | wait_time = timing.MaxWaitingTime( |
| 77 | timing.RenderTime(timestamp, clock.CurrentTime()), clock.CurrentTime(), |
| 78 | /*too_many_frames_queued=*/false); |
| 79 | EXPECT_EQ(jitter_delay, wait_time); |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 80 | |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 81 | // Add decode time estimates for 1 second. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 82 | const TimeDelta kDecodeTime = TimeDelta::Millis(10); |
| 83 | for (int i = 0; i < k25Fps.hertz(); i++) { |
| 84 | clock.AdvanceTime(kDecodeTime); |
| 85 | timing.StopDecodeTimer(kDecodeTime, clock.CurrentTime()); |
| 86 | timestamp += k90kHz / k25Fps; |
| 87 | clock.AdvanceTime(1 / k25Fps - kDecodeTime); |
| 88 | timing.IncomingTimestamp(timestamp, clock.CurrentTime()); |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 89 | } |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 90 | timing.UpdateCurrentDelay(timestamp); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 91 | wait_time = timing.MaxWaitingTime( |
| 92 | timing.RenderTime(timestamp, clock.CurrentTime()), clock.CurrentTime(), |
| 93 | /*too_many_frames_queued=*/false); |
| 94 | EXPECT_EQ(jitter_delay, wait_time); |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 95 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 96 | const TimeDelta kMinTotalDelay = TimeDelta::Millis(200); |
| 97 | timing.set_min_playout_delay(kMinTotalDelay); |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 98 | clock.AdvanceTimeMilliseconds(5000); |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 99 | timestamp += 5 * 90000; |
| 100 | timing.UpdateCurrentDelay(timestamp); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 101 | const TimeDelta kRenderDelay = TimeDelta::Millis(10); |
| 102 | timing.set_render_delay(kRenderDelay); |
| 103 | wait_time = timing.MaxWaitingTime( |
| 104 | timing.RenderTime(timestamp, clock.CurrentTime()), clock.CurrentTime(), |
| 105 | /*too_many_frames_queued=*/false); |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 106 | // We should at least have kMinTotalDelayMs - decodeTime (10) - renderTime |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 107 | // (10) to wait. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 108 | EXPECT_EQ(kMinTotalDelay - kDecodeTime - kRenderDelay, wait_time); |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 109 | // The total video delay should be equal to the min total delay. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 110 | EXPECT_EQ(kMinTotalDelay, timing.TargetVideoDelay()); |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 111 | |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 112 | // Reset playout delay. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 113 | timing.set_min_playout_delay(TimeDelta::Zero()); |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 114 | clock.AdvanceTimeMilliseconds(5000); |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 115 | timestamp += 5 * 90000; |
| 116 | timing.UpdateCurrentDelay(timestamp); |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 119 | TEST(ReceiverTimingTest, TimestampWrapAround) { |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 120 | constexpr auto kStartTime = Timestamp::Millis(1337); |
| 121 | SimulatedClock clock(kStartTime); |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 122 | VCMTiming timing(&clock); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 123 | |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 124 | // Provoke a wrap-around. The fifth frame will have wrapped at 25 fps. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 125 | constexpr uint32_t kRtpTicksPerFrame = k90kHz / k25Fps; |
| 126 | uint32_t timestamp = 0xFFFFFFFFu - 3 * kRtpTicksPerFrame; |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame] | 127 | for (int i = 0; i < 5; ++i) { |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 128 | timing.IncomingTimestamp(timestamp, clock.CurrentTime()); |
| 129 | clock.AdvanceTime(1 / k25Fps); |
| 130 | timestamp += kRtpTicksPerFrame; |
| 131 | EXPECT_EQ(kStartTime + 3 / k25Fps, |
| 132 | timing.RenderTime(0xFFFFFFFFu, clock.CurrentTime())); |
| 133 | // One ms later in 90 kHz. |
| 134 | EXPECT_EQ(kStartTime + 3 / k25Fps + TimeDelta::Millis(1), |
| 135 | timing.RenderTime(89u, clock.CurrentTime())); |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 139 | TEST(ReceiverTimingTest, MaxWaitingTimeIsZeroForZeroRenderTime) { |
| 140 | // This is the default path when the RTP playout delay header extension is set |
Johannes Kron | 23bfff3 | 2021-09-28 21:31:46 +0200 | [diff] [blame] | 141 | // to min==0 and max==0. |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 142 | constexpr int64_t kStartTimeUs = 3.15e13; // About one year in us. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 143 | constexpr TimeDelta kTimeDelta = 1 / Frequency::Hertz(60); |
| 144 | constexpr Timestamp kZeroRenderTime = Timestamp::Zero(); |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 145 | SimulatedClock clock(kStartTimeUs); |
| 146 | VCMTiming timing(&clock); |
| 147 | timing.Reset(); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 148 | timing.set_max_playout_delay(TimeDelta::Zero()); |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 149 | for (int i = 0; i < 10; ++i) { |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 150 | clock.AdvanceTime(kTimeDelta); |
| 151 | Timestamp now = clock.CurrentTime(); |
| 152 | EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTime, now, |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 153 | /*too_many_frames_queued=*/false), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 154 | TimeDelta::Zero()); |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 155 | } |
| 156 | // Another frame submitted at the same time also returns a negative max |
| 157 | // waiting time. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 158 | Timestamp now = clock.CurrentTime(); |
| 159 | EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTime, now, |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 160 | /*too_many_frames_queued=*/false), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 161 | TimeDelta::Zero()); |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 162 | // MaxWaitingTime should be less than zero even if there's a burst of frames. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 163 | EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTime, now, |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 164 | /*too_many_frames_queued=*/false), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 165 | TimeDelta::Zero()); |
| 166 | EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTime, now, |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 167 | /*too_many_frames_queued=*/false), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 168 | TimeDelta::Zero()); |
| 169 | EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTime, now, |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 170 | /*too_many_frames_queued=*/false), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 171 | TimeDelta::Zero()); |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | TEST(ReceiverTimingTest, MaxWaitingTimeZeroDelayPacingExperiment) { |
| 175 | // The minimum pacing is enabled by a field trial and active if the RTP |
| 176 | // playout delay header extension is set to min==0. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 177 | constexpr TimeDelta kMinPacing = TimeDelta::Millis(3); |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 178 | test::ScopedFieldTrials override_field_trials( |
| 179 | "WebRTC-ZeroPlayoutDelay/min_pacing:3ms/"); |
| 180 | constexpr int64_t kStartTimeUs = 3.15e13; // About one year in us. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 181 | constexpr TimeDelta kTimeDelta = 1 / Frequency::Hertz(60); |
| 182 | constexpr auto kZeroRenderTime = Timestamp::Zero(); |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 183 | SimulatedClock clock(kStartTimeUs); |
| 184 | VCMTiming timing(&clock); |
| 185 | timing.Reset(); |
| 186 | // MaxWaitingTime() returns zero for evenly spaced video frames. |
| 187 | for (int i = 0; i < 10; ++i) { |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 188 | clock.AdvanceTime(kTimeDelta); |
| 189 | Timestamp now = clock.CurrentTime(); |
| 190 | EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTime, now, |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 191 | /*too_many_frames_queued=*/false), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 192 | TimeDelta::Zero()); |
| 193 | timing.SetLastDecodeScheduledTimestamp(now); |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 194 | } |
| 195 | // Another frame submitted at the same time is paced according to the field |
| 196 | // trial setting. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 197 | auto now = clock.CurrentTime(); |
| 198 | EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTime, now, |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 199 | /*too_many_frames_queued=*/false), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 200 | kMinPacing); |
Rezaul Barbhuiya | 82c2248 | 2021-08-05 17:54:11 -0700 | [diff] [blame] | 201 | // If there's a burst of frames, the wait time is calculated based on next |
| 202 | // decode time. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 203 | EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTime, now, |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 204 | /*too_many_frames_queued=*/false), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 205 | kMinPacing); |
| 206 | EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTime, now, |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 207 | /*too_many_frames_queued=*/false), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 208 | kMinPacing); |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 209 | // Allow a few ms to pass, this should be subtracted from the MaxWaitingTime. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 210 | constexpr TimeDelta kTwoMs = TimeDelta::Millis(2); |
| 211 | clock.AdvanceTime(kTwoMs); |
| 212 | now = clock.CurrentTime(); |
| 213 | EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTime, now, |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 214 | /*too_many_frames_queued=*/false), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 215 | kMinPacing - kTwoMs); |
Rezaul Barbhuiya | 82c2248 | 2021-08-05 17:54:11 -0700 | [diff] [blame] | 216 | // A frame is decoded at the current time, the wait time should be restored to |
| 217 | // pacing delay. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 218 | timing.SetLastDecodeScheduledTimestamp(now); |
| 219 | EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTime, now, |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 220 | /*too_many_frames_queued=*/false), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 221 | kMinPacing); |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | TEST(ReceiverTimingTest, DefaultMaxWaitingTimeUnaffectedByPacingExperiment) { |
| 225 | // The minimum pacing is enabled by a field trial but should not have any |
| 226 | // effect if render_time_ms is greater than 0; |
| 227 | test::ScopedFieldTrials override_field_trials( |
| 228 | "WebRTC-ZeroPlayoutDelay/min_pacing:3ms/"); |
| 229 | constexpr int64_t kStartTimeUs = 3.15e13; // About one year in us. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 230 | const TimeDelta kTimeDelta = TimeDelta::Millis(1000.0 / 60.0); |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 231 | SimulatedClock clock(kStartTimeUs); |
| 232 | VCMTiming timing(&clock); |
| 233 | timing.Reset(); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 234 | clock.AdvanceTime(kTimeDelta); |
| 235 | auto now = clock.CurrentTime(); |
| 236 | Timestamp render_time = now + TimeDelta::Millis(30); |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 237 | // Estimate the internal processing delay from the first frame. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 238 | TimeDelta estimated_processing_delay = |
| 239 | (render_time - now) - |
| 240 | timing.MaxWaitingTime(render_time, now, |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 241 | /*too_many_frames_queued=*/false); |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 242 | EXPECT_GT(estimated_processing_delay, TimeDelta::Zero()); |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 243 | |
| 244 | // Any other frame submitted at the same time should be scheduled according to |
| 245 | // its render time. |
| 246 | for (int i = 0; i < 5; ++i) { |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 247 | render_time += kTimeDelta; |
| 248 | EXPECT_EQ(timing.MaxWaitingTime(render_time, now, |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 249 | /*too_many_frames_queued=*/false), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 250 | render_time - now - estimated_processing_delay); |
Johannes Kron | 985905d | 2021-06-29 11:37:06 +0200 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 254 | TEST(ReceiverTimingTest, MaxWaitingTimeReturnsZeroIfTooManyFramesQueuedIsTrue) { |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 255 | // The minimum pacing is enabled by a field trial and active if the RTP |
| 256 | // playout delay header extension is set to min==0. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 257 | constexpr TimeDelta kMinPacing = TimeDelta::Millis(3); |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 258 | test::ScopedFieldTrials override_field_trials( |
| 259 | "WebRTC-ZeroPlayoutDelay/min_pacing:3ms/"); |
| 260 | constexpr int64_t kStartTimeUs = 3.15e13; // About one year in us. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 261 | const TimeDelta kTimeDelta = TimeDelta::Millis(1000.0 / 60.0); |
| 262 | constexpr auto kZeroRenderTime = Timestamp::Zero(); |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 263 | SimulatedClock clock(kStartTimeUs); |
| 264 | VCMTiming timing(&clock); |
| 265 | timing.Reset(); |
| 266 | // MaxWaitingTime() returns zero for evenly spaced video frames. |
| 267 | for (int i = 0; i < 10; ++i) { |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 268 | clock.AdvanceTime(kTimeDelta); |
| 269 | auto now = clock.CurrentTime(); |
| 270 | EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTime, now, |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 271 | /*too_many_frames_queued=*/false), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 272 | TimeDelta::Zero()); |
| 273 | timing.SetLastDecodeScheduledTimestamp(now); |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 274 | } |
| 275 | // Another frame submitted at the same time is paced according to the field |
| 276 | // trial setting. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 277 | auto now_ms = clock.CurrentTime(); |
| 278 | EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTime, now_ms, |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 279 | /*too_many_frames_queued=*/false), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 280 | kMinPacing); |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 281 | // MaxWaitingTime returns 0 even if there's a burst of frames if |
| 282 | // too_many_frames_queued is set to true. |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 283 | EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTime, now_ms, |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 284 | /*too_many_frames_queued=*/true), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 285 | TimeDelta::Zero()); |
| 286 | EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTime, now_ms, |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 287 | /*too_many_frames_queued=*/true), |
Evan Shrubsole | d6cdf80 | 2022-03-02 15:13:55 +0100 | [diff] [blame^] | 288 | TimeDelta::Zero()); |
Johannes Kron | 2ddc39e | 2021-08-10 16:56:12 +0200 | [diff] [blame] | 289 | } |
| 290 | |
stefan@webrtc.org | 9f557c1 | 2013-05-17 12:55:07 +0000 | [diff] [blame] | 291 | } // namespace webrtc |