blob: 71de1fe3da36ac13cf0e080996534b62d3080c4c [file] [log] [blame]
stefan@webrtc.org9f557c12013-05-17 12:55:07 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/timing.h"
Jonas Olssona4d87372019-07-05 19:08:33 +020012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "system_wrappers/include/clock.h"
Johannes Kron985905d2021-06-29 11:37:06 +020014#include "test/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "test/gtest.h"
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000016
17namespace webrtc {
Åsa Persson8368d1a2018-01-05 12:44:45 +010018namespace {
19const int kFps = 25;
20} // namespace
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000021
Johannes Kron985905d2021-06-29 11:37:06 +020022TEST(ReceiverTimingTest, JitterDelay) {
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000023 SimulatedClock clock(0);
24 VCMTiming timing(&clock);
Åsa Persson8368d1a2018-01-05 12:44:45 +010025 timing.Reset();
26
27 uint32_t timestamp = 0;
28 timing.UpdateCurrentDelay(timestamp);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000029
30 timing.Reset();
31
Åsa Persson8368d1a2018-01-05 12:44:45 +010032 timing.IncomingTimestamp(timestamp, clock.TimeInMilliseconds());
33 uint32_t jitter_delay_ms = 20;
34 timing.SetJitterDelay(jitter_delay_ms);
35 timing.UpdateCurrentDelay(timestamp);
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000036 timing.set_render_delay(0);
Åsa Persson8368d1a2018-01-05 12:44:45 +010037 uint32_t wait_time_ms = timing.MaxWaitingTime(
38 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
Johannes Kron2ddc39e2021-08-10 16:56:12 +020039 clock.TimeInMilliseconds(), /*too_many_frames_queued=*/false);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000040 // First update initializes the render time. Since we have no decode delay
Åsa Persson8368d1a2018-01-05 12:44:45 +010041 // we get wait_time_ms = renderTime - now - renderDelay = jitter.
42 EXPECT_EQ(jitter_delay_ms, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000043
Åsa Persson8368d1a2018-01-05 12:44:45 +010044 jitter_delay_ms += VCMTiming::kDelayMaxChangeMsPerS + 10;
45 timestamp += 90000;
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000046 clock.AdvanceTimeMilliseconds(1000);
Åsa Persson8368d1a2018-01-05 12:44:45 +010047 timing.SetJitterDelay(jitter_delay_ms);
48 timing.UpdateCurrentDelay(timestamp);
49 wait_time_ms = timing.MaxWaitingTime(
50 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
Johannes Kron2ddc39e2021-08-10 16:56:12 +020051 clock.TimeInMilliseconds(), /*too_many_frames_queued=*/false);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000052 // Since we gradually increase the delay we only get 100 ms every second.
Åsa Persson8368d1a2018-01-05 12:44:45 +010053 EXPECT_EQ(jitter_delay_ms - 10, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000054
Åsa Persson8368d1a2018-01-05 12:44:45 +010055 timestamp += 90000;
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000056 clock.AdvanceTimeMilliseconds(1000);
Åsa Persson8368d1a2018-01-05 12:44:45 +010057 timing.UpdateCurrentDelay(timestamp);
58 wait_time_ms = timing.MaxWaitingTime(
59 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
Johannes Kron2ddc39e2021-08-10 16:56:12 +020060 clock.TimeInMilliseconds(), /*too_many_frames_queued=*/false);
Åsa Persson8368d1a2018-01-05 12:44:45 +010061 EXPECT_EQ(jitter_delay_ms, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000062
Åsa Persson8368d1a2018-01-05 12:44:45 +010063 // Insert frames without jitter, verify that this gives the exact wait time.
64 const int kNumFrames = 300;
65 for (int i = 0; i < kNumFrames; i++) {
66 clock.AdvanceTimeMilliseconds(1000 / kFps);
67 timestamp += 90000 / kFps;
68 timing.IncomingTimestamp(timestamp, clock.TimeInMilliseconds());
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000069 }
Åsa Persson8368d1a2018-01-05 12:44:45 +010070 timing.UpdateCurrentDelay(timestamp);
71 wait_time_ms = timing.MaxWaitingTime(
72 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
Johannes Kron2ddc39e2021-08-10 16:56:12 +020073 clock.TimeInMilliseconds(), /*too_many_frames_queued=*/false);
Åsa Persson8368d1a2018-01-05 12:44:45 +010074 EXPECT_EQ(jitter_delay_ms, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000075
Åsa Persson8368d1a2018-01-05 12:44:45 +010076 // Add decode time estimates for 1 second.
77 const uint32_t kDecodeTimeMs = 10;
78 for (int i = 0; i < kFps; i++) {
79 clock.AdvanceTimeMilliseconds(kDecodeTimeMs);
Johannes Kronbfd343b2019-07-01 10:07:50 +020080 timing.StopDecodeTimer(kDecodeTimeMs, clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 12:44:45 +010081 timestamp += 90000 / kFps;
82 clock.AdvanceTimeMilliseconds(1000 / kFps - kDecodeTimeMs);
83 timing.IncomingTimestamp(timestamp, clock.TimeInMilliseconds());
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000084 }
Åsa Persson8368d1a2018-01-05 12:44:45 +010085 timing.UpdateCurrentDelay(timestamp);
86 wait_time_ms = timing.MaxWaitingTime(
87 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
Johannes Kron2ddc39e2021-08-10 16:56:12 +020088 clock.TimeInMilliseconds(), /*too_many_frames_queued=*/false);
Åsa Persson8368d1a2018-01-05 12:44:45 +010089 EXPECT_EQ(jitter_delay_ms, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000090
Åsa Persson8368d1a2018-01-05 12:44:45 +010091 const int kMinTotalDelayMs = 200;
92 timing.set_min_playout_delay(kMinTotalDelayMs);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000093 clock.AdvanceTimeMilliseconds(5000);
Åsa Persson8368d1a2018-01-05 12:44:45 +010094 timestamp += 5 * 90000;
95 timing.UpdateCurrentDelay(timestamp);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000096 const int kRenderDelayMs = 10;
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000097 timing.set_render_delay(kRenderDelayMs);
Åsa Persson8368d1a2018-01-05 12:44:45 +010098 wait_time_ms = timing.MaxWaitingTime(
99 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200100 clock.TimeInMilliseconds(), /*too_many_frames_queued=*/false);
Åsa Persson8368d1a2018-01-05 12:44:45 +0100101 // We should at least have kMinTotalDelayMs - decodeTime (10) - renderTime
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000102 // (10) to wait.
Åsa Persson8368d1a2018-01-05 12:44:45 +0100103 EXPECT_EQ(kMinTotalDelayMs - kDecodeTimeMs - kRenderDelayMs, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000104 // The total video delay should be equal to the min total delay.
Åsa Persson8368d1a2018-01-05 12:44:45 +0100105 EXPECT_EQ(kMinTotalDelayMs, timing.TargetVideoDelay());
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000106
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +0000107 // Reset playout delay.
108 timing.set_min_playout_delay(0);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000109 clock.AdvanceTimeMilliseconds(5000);
Åsa Persson8368d1a2018-01-05 12:44:45 +0100110 timestamp += 5 * 90000;
111 timing.UpdateCurrentDelay(timestamp);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000112}
113
Johannes Kron985905d2021-06-29 11:37:06 +0200114TEST(ReceiverTimingTest, TimestampWrapAround) {
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000115 SimulatedClock clock(0);
116 VCMTiming timing(&clock);
Åsa Persson8368d1a2018-01-05 12:44:45 +0100117 // Provoke a wrap-around. The fifth frame will have wrapped at 25 fps.
118 uint32_t timestamp = 0xFFFFFFFFu - 3 * 90000 / kFps;
119 for (int i = 0; i < 5; ++i) {
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000120 timing.IncomingTimestamp(timestamp, clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 12:44:45 +0100121 clock.AdvanceTimeMilliseconds(1000 / kFps);
122 timestamp += 90000 / kFps;
123 EXPECT_EQ(3 * 1000 / kFps,
124 timing.RenderTimeMs(0xFFFFFFFFu, clock.TimeInMilliseconds()));
125 EXPECT_EQ(3 * 1000 / kFps + 1,
126 timing.RenderTimeMs(89u, // One ms later in 90 kHz.
127 clock.TimeInMilliseconds()));
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000128 }
129}
130
Johannes Kron985905d2021-06-29 11:37:06 +0200131TEST(ReceiverTimingTest, MaxWaitingTimeIsZeroForZeroRenderTime) {
132 // This is the default path when the RTP playout delay header extension is set
Johannes Kron23bfff32021-09-28 21:31:46 +0200133 // to min==0 and max==0.
Johannes Kron985905d2021-06-29 11:37:06 +0200134 constexpr int64_t kStartTimeUs = 3.15e13; // About one year in us.
135 constexpr int64_t kTimeDeltaMs = 1000.0 / 60.0;
136 constexpr int64_t kZeroRenderTimeMs = 0;
137 SimulatedClock clock(kStartTimeUs);
138 VCMTiming timing(&clock);
139 timing.Reset();
Johannes Kron23bfff32021-09-28 21:31:46 +0200140 timing.set_max_playout_delay(0);
Johannes Kron985905d2021-06-29 11:37:06 +0200141 for (int i = 0; i < 10; ++i) {
142 clock.AdvanceTimeMilliseconds(kTimeDeltaMs);
143 int64_t now_ms = clock.TimeInMilliseconds();
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200144 EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
145 /*too_many_frames_queued=*/false),
146 0);
Johannes Kron985905d2021-06-29 11:37:06 +0200147 }
148 // Another frame submitted at the same time also returns a negative max
149 // waiting time.
150 int64_t now_ms = clock.TimeInMilliseconds();
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200151 EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
152 /*too_many_frames_queued=*/false),
153 0);
Johannes Kron985905d2021-06-29 11:37:06 +0200154 // MaxWaitingTime should be less than zero even if there's a burst of frames.
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200155 EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
156 /*too_many_frames_queued=*/false),
157 0);
158 EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
159 /*too_many_frames_queued=*/false),
160 0);
161 EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
162 /*too_many_frames_queued=*/false),
163 0);
Johannes Kron985905d2021-06-29 11:37:06 +0200164}
165
166TEST(ReceiverTimingTest, MaxWaitingTimeZeroDelayPacingExperiment) {
167 // The minimum pacing is enabled by a field trial and active if the RTP
168 // playout delay header extension is set to min==0.
169 constexpr int64_t kMinPacingMs = 3;
170 test::ScopedFieldTrials override_field_trials(
171 "WebRTC-ZeroPlayoutDelay/min_pacing:3ms/");
172 constexpr int64_t kStartTimeUs = 3.15e13; // About one year in us.
173 constexpr int64_t kTimeDeltaMs = 1000.0 / 60.0;
174 constexpr int64_t kZeroRenderTimeMs = 0;
175 SimulatedClock clock(kStartTimeUs);
176 VCMTiming timing(&clock);
177 timing.Reset();
178 // MaxWaitingTime() returns zero for evenly spaced video frames.
179 for (int i = 0; i < 10; ++i) {
180 clock.AdvanceTimeMilliseconds(kTimeDeltaMs);
181 int64_t now_ms = clock.TimeInMilliseconds();
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200182 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
183 /*too_many_frames_queued=*/false),
184 0);
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700185 timing.SetLastDecodeScheduledTimestamp(now_ms);
Johannes Kron985905d2021-06-29 11:37:06 +0200186 }
187 // Another frame submitted at the same time is paced according to the field
188 // trial setting.
189 int64_t now_ms = clock.TimeInMilliseconds();
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200190 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
191 /*too_many_frames_queued=*/false),
192 kMinPacingMs);
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700193 // If there's a burst of frames, the wait time is calculated based on next
194 // decode time.
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200195 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
196 /*too_many_frames_queued=*/false),
197 kMinPacingMs);
198 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
199 /*too_many_frames_queued=*/false),
200 kMinPacingMs);
Johannes Kron985905d2021-06-29 11:37:06 +0200201 // Allow a few ms to pass, this should be subtracted from the MaxWaitingTime.
202 constexpr int64_t kTwoMs = 2;
203 clock.AdvanceTimeMilliseconds(kTwoMs);
204 now_ms = clock.TimeInMilliseconds();
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200205 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
206 /*too_many_frames_queued=*/false),
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700207 kMinPacingMs - kTwoMs);
208 // A frame is decoded at the current time, the wait time should be restored to
209 // pacing delay.
210 timing.SetLastDecodeScheduledTimestamp(now_ms);
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200211 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
212 /*too_many_frames_queued=*/false),
213 kMinPacingMs);
Johannes Kron985905d2021-06-29 11:37:06 +0200214}
215
216TEST(ReceiverTimingTest, DefaultMaxWaitingTimeUnaffectedByPacingExperiment) {
217 // The minimum pacing is enabled by a field trial but should not have any
218 // effect if render_time_ms is greater than 0;
219 test::ScopedFieldTrials override_field_trials(
220 "WebRTC-ZeroPlayoutDelay/min_pacing:3ms/");
221 constexpr int64_t kStartTimeUs = 3.15e13; // About one year in us.
222 constexpr int64_t kTimeDeltaMs = 1000.0 / 60.0;
223 SimulatedClock clock(kStartTimeUs);
224 VCMTiming timing(&clock);
225 timing.Reset();
226 clock.AdvanceTimeMilliseconds(kTimeDeltaMs);
227 int64_t now_ms = clock.TimeInMilliseconds();
228 int64_t render_time_ms = now_ms + 30;
229 // Estimate the internal processing delay from the first frame.
230 int64_t estimated_processing_delay =
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200231 (render_time_ms - now_ms) -
232 timing.MaxWaitingTime(render_time_ms, now_ms,
233 /*too_many_frames_queued=*/false);
Johannes Kron985905d2021-06-29 11:37:06 +0200234 EXPECT_GT(estimated_processing_delay, 0);
235
236 // Any other frame submitted at the same time should be scheduled according to
237 // its render time.
238 for (int i = 0; i < 5; ++i) {
239 render_time_ms += kTimeDeltaMs;
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200240 EXPECT_EQ(timing.MaxWaitingTime(render_time_ms, now_ms,
241 /*too_many_frames_queued=*/false),
Johannes Kron985905d2021-06-29 11:37:06 +0200242 render_time_ms - now_ms - estimated_processing_delay);
243 }
244}
245
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200246TEST(ReceiverTiminTest, MaxWaitingTimeReturnsZeroIfTooManyFramesQueuedIsTrue) {
247 // The minimum pacing is enabled by a field trial and active if the RTP
248 // playout delay header extension is set to min==0.
249 constexpr int64_t kMinPacingMs = 3;
250 test::ScopedFieldTrials override_field_trials(
251 "WebRTC-ZeroPlayoutDelay/min_pacing:3ms/");
252 constexpr int64_t kStartTimeUs = 3.15e13; // About one year in us.
253 constexpr int64_t kTimeDeltaMs = 1000.0 / 60.0;
254 constexpr int64_t kZeroRenderTimeMs = 0;
255 SimulatedClock clock(kStartTimeUs);
256 VCMTiming timing(&clock);
257 timing.Reset();
258 // MaxWaitingTime() returns zero for evenly spaced video frames.
259 for (int i = 0; i < 10; ++i) {
260 clock.AdvanceTimeMilliseconds(kTimeDeltaMs);
261 int64_t now_ms = clock.TimeInMilliseconds();
262 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
263 /*too_many_frames_queued=*/false),
264 0);
265 timing.SetLastDecodeScheduledTimestamp(now_ms);
266 }
267 // Another frame submitted at the same time is paced according to the field
268 // trial setting.
269 int64_t now_ms = clock.TimeInMilliseconds();
270 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
271 /*too_many_frames_queued=*/false),
272 kMinPacingMs);
273 // MaxWaitingTime returns 0 even if there's a burst of frames if
274 // too_many_frames_queued is set to true.
275 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
276 /*too_many_frames_queued=*/true),
277 0);
278 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
279 /*too_many_frames_queued=*/true),
280 0);
281}
282
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000283} // namespace webrtc