blob: cc87a3b4e04067a88b7c3efc61b28285ab422a95 [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
133 // to min==0.
134 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();
140 for (int i = 0; i < 10; ++i) {
141 clock.AdvanceTimeMilliseconds(kTimeDeltaMs);
142 int64_t now_ms = clock.TimeInMilliseconds();
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200143 EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
144 /*too_many_frames_queued=*/false),
145 0);
Johannes Kron985905d2021-06-29 11:37:06 +0200146 }
147 // Another frame submitted at the same time also returns a negative max
148 // waiting time.
149 int64_t now_ms = clock.TimeInMilliseconds();
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200150 EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
151 /*too_many_frames_queued=*/false),
152 0);
Johannes Kron985905d2021-06-29 11:37:06 +0200153 // MaxWaitingTime should be less than zero even if there's a burst of frames.
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200154 EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
155 /*too_many_frames_queued=*/false),
156 0);
157 EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
158 /*too_many_frames_queued=*/false),
159 0);
160 EXPECT_LT(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
161 /*too_many_frames_queued=*/false),
162 0);
Johannes Kron985905d2021-06-29 11:37:06 +0200163}
164
165TEST(ReceiverTimingTest, MaxWaitingTimeZeroDelayPacingExperiment) {
166 // The minimum pacing is enabled by a field trial and active if the RTP
167 // playout delay header extension is set to min==0.
168 constexpr int64_t kMinPacingMs = 3;
169 test::ScopedFieldTrials override_field_trials(
170 "WebRTC-ZeroPlayoutDelay/min_pacing:3ms/");
171 constexpr int64_t kStartTimeUs = 3.15e13; // About one year in us.
172 constexpr int64_t kTimeDeltaMs = 1000.0 / 60.0;
173 constexpr int64_t kZeroRenderTimeMs = 0;
174 SimulatedClock clock(kStartTimeUs);
175 VCMTiming timing(&clock);
176 timing.Reset();
177 // MaxWaitingTime() returns zero for evenly spaced video frames.
178 for (int i = 0; i < 10; ++i) {
179 clock.AdvanceTimeMilliseconds(kTimeDeltaMs);
180 int64_t now_ms = clock.TimeInMilliseconds();
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200181 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
182 /*too_many_frames_queued=*/false),
183 0);
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700184 timing.SetLastDecodeScheduledTimestamp(now_ms);
Johannes Kron985905d2021-06-29 11:37:06 +0200185 }
186 // Another frame submitted at the same time is paced according to the field
187 // trial setting.
188 int64_t now_ms = clock.TimeInMilliseconds();
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200189 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
190 /*too_many_frames_queued=*/false),
191 kMinPacingMs);
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700192 // If there's a burst of frames, the wait time is calculated based on next
193 // decode time.
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200194 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
195 /*too_many_frames_queued=*/false),
196 kMinPacingMs);
197 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
198 /*too_many_frames_queued=*/false),
199 kMinPacingMs);
Johannes Kron985905d2021-06-29 11:37:06 +0200200 // Allow a few ms to pass, this should be subtracted from the MaxWaitingTime.
201 constexpr int64_t kTwoMs = 2;
202 clock.AdvanceTimeMilliseconds(kTwoMs);
203 now_ms = clock.TimeInMilliseconds();
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200204 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
205 /*too_many_frames_queued=*/false),
Rezaul Barbhuiya82c22482021-08-05 17:54:11 -0700206 kMinPacingMs - kTwoMs);
207 // A frame is decoded at the current time, the wait time should be restored to
208 // pacing delay.
209 timing.SetLastDecodeScheduledTimestamp(now_ms);
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200210 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
211 /*too_many_frames_queued=*/false),
212 kMinPacingMs);
Johannes Kron985905d2021-06-29 11:37:06 +0200213}
214
215TEST(ReceiverTimingTest, DefaultMaxWaitingTimeUnaffectedByPacingExperiment) {
216 // The minimum pacing is enabled by a field trial but should not have any
217 // effect if render_time_ms is greater than 0;
218 test::ScopedFieldTrials override_field_trials(
219 "WebRTC-ZeroPlayoutDelay/min_pacing:3ms/");
220 constexpr int64_t kStartTimeUs = 3.15e13; // About one year in us.
221 constexpr int64_t kTimeDeltaMs = 1000.0 / 60.0;
222 SimulatedClock clock(kStartTimeUs);
223 VCMTiming timing(&clock);
224 timing.Reset();
225 clock.AdvanceTimeMilliseconds(kTimeDeltaMs);
226 int64_t now_ms = clock.TimeInMilliseconds();
227 int64_t render_time_ms = now_ms + 30;
228 // Estimate the internal processing delay from the first frame.
229 int64_t estimated_processing_delay =
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200230 (render_time_ms - now_ms) -
231 timing.MaxWaitingTime(render_time_ms, now_ms,
232 /*too_many_frames_queued=*/false);
Johannes Kron985905d2021-06-29 11:37:06 +0200233 EXPECT_GT(estimated_processing_delay, 0);
234
235 // Any other frame submitted at the same time should be scheduled according to
236 // its render time.
237 for (int i = 0; i < 5; ++i) {
238 render_time_ms += kTimeDeltaMs;
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200239 EXPECT_EQ(timing.MaxWaitingTime(render_time_ms, now_ms,
240 /*too_many_frames_queued=*/false),
Johannes Kron985905d2021-06-29 11:37:06 +0200241 render_time_ms - now_ms - estimated_processing_delay);
242 }
243}
244
Johannes Kron2ddc39e2021-08-10 16:56:12 +0200245TEST(ReceiverTiminTest, MaxWaitingTimeReturnsZeroIfTooManyFramesQueuedIsTrue) {
246 // The minimum pacing is enabled by a field trial and active if the RTP
247 // playout delay header extension is set to min==0.
248 constexpr int64_t kMinPacingMs = 3;
249 test::ScopedFieldTrials override_field_trials(
250 "WebRTC-ZeroPlayoutDelay/min_pacing:3ms/");
251 constexpr int64_t kStartTimeUs = 3.15e13; // About one year in us.
252 constexpr int64_t kTimeDeltaMs = 1000.0 / 60.0;
253 constexpr int64_t kZeroRenderTimeMs = 0;
254 SimulatedClock clock(kStartTimeUs);
255 VCMTiming timing(&clock);
256 timing.Reset();
257 // MaxWaitingTime() returns zero for evenly spaced video frames.
258 for (int i = 0; i < 10; ++i) {
259 clock.AdvanceTimeMilliseconds(kTimeDeltaMs);
260 int64_t now_ms = clock.TimeInMilliseconds();
261 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
262 /*too_many_frames_queued=*/false),
263 0);
264 timing.SetLastDecodeScheduledTimestamp(now_ms);
265 }
266 // Another frame submitted at the same time is paced according to the field
267 // trial setting.
268 int64_t now_ms = clock.TimeInMilliseconds();
269 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
270 /*too_many_frames_queued=*/false),
271 kMinPacingMs);
272 // MaxWaitingTime returns 0 even if there's a burst of frames if
273 // too_many_frames_queued is set to true.
274 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
275 /*too_many_frames_queued=*/true),
276 0);
277 EXPECT_EQ(timing.MaxWaitingTime(kZeroRenderTimeMs, now_ms,
278 /*too_many_frames_queued=*/true),
279 0);
280}
281
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000282} // namespace webrtc