blob: ee86605fb6feff0454b66470fd3dee153879c9b3 [file] [log] [blame]
stefan@webrtc.org9f557c12013-05-17 12:55:071/*
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 04:47:3111#include "modules/video_coding/timing.h"
Jonas Olssona4d87372019-07-05 17:08:3312
Mirko Bonadei92ea95e2017-09-15 04:47:3113#include "system_wrappers/include/clock.h"
14#include "test/gtest.h"
stefan@webrtc.org9f557c12013-05-17 12:55:0715
16namespace webrtc {
Åsa Persson8368d1a2018-01-05 11:44:4517namespace {
18const int kFps = 25;
19} // namespace
stefan@webrtc.org9f557c12013-05-17 12:55:0720
21TEST(ReceiverTiming, Tests) {
22 SimulatedClock clock(0);
23 VCMTiming timing(&clock);
Åsa Persson8368d1a2018-01-05 11:44:4524 timing.Reset();
25
26 uint32_t timestamp = 0;
27 timing.UpdateCurrentDelay(timestamp);
stefan@webrtc.org9f557c12013-05-17 12:55:0728
29 timing.Reset();
30
Åsa Persson8368d1a2018-01-05 11:44:4531 timing.IncomingTimestamp(timestamp, clock.TimeInMilliseconds());
32 uint32_t jitter_delay_ms = 20;
33 timing.SetJitterDelay(jitter_delay_ms);
34 timing.UpdateCurrentDelay(timestamp);
mikhal@webrtc.orgadc64a72013-05-30 16:20:1835 timing.set_render_delay(0);
Åsa Persson8368d1a2018-01-05 11:44:4536 uint32_t wait_time_ms = timing.MaxWaitingTime(
37 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
stefan@webrtc.org9f557c12013-05-17 12:55:0738 clock.TimeInMilliseconds());
39 // First update initializes the render time. Since we have no decode delay
Åsa Persson8368d1a2018-01-05 11:44:4540 // we get wait_time_ms = renderTime - now - renderDelay = jitter.
41 EXPECT_EQ(jitter_delay_ms, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:0742
Åsa Persson8368d1a2018-01-05 11:44:4543 jitter_delay_ms += VCMTiming::kDelayMaxChangeMsPerS + 10;
44 timestamp += 90000;
stefan@webrtc.org9f557c12013-05-17 12:55:0745 clock.AdvanceTimeMilliseconds(1000);
Åsa Persson8368d1a2018-01-05 11:44:4546 timing.SetJitterDelay(jitter_delay_ms);
47 timing.UpdateCurrentDelay(timestamp);
48 wait_time_ms = timing.MaxWaitingTime(
49 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
philipel5908c712015-12-21 16:23:2050 clock.TimeInMilliseconds());
stefan@webrtc.org9f557c12013-05-17 12:55:0751 // Since we gradually increase the delay we only get 100 ms every second.
Åsa Persson8368d1a2018-01-05 11:44:4552 EXPECT_EQ(jitter_delay_ms - 10, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:0753
Åsa Persson8368d1a2018-01-05 11:44:4554 timestamp += 90000;
stefan@webrtc.org9f557c12013-05-17 12:55:0755 clock.AdvanceTimeMilliseconds(1000);
Åsa Persson8368d1a2018-01-05 11:44:4556 timing.UpdateCurrentDelay(timestamp);
57 wait_time_ms = timing.MaxWaitingTime(
58 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
stefan@webrtc.org9f557c12013-05-17 12:55:0759 clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 11:44:4560 EXPECT_EQ(jitter_delay_ms, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:0761
Åsa Persson8368d1a2018-01-05 11:44:4562 // Insert frames without jitter, verify that this gives the exact wait time.
63 const int kNumFrames = 300;
64 for (int i = 0; i < kNumFrames; i++) {
65 clock.AdvanceTimeMilliseconds(1000 / kFps);
66 timestamp += 90000 / kFps;
67 timing.IncomingTimestamp(timestamp, clock.TimeInMilliseconds());
stefan@webrtc.org9f557c12013-05-17 12:55:0768 }
Åsa Persson8368d1a2018-01-05 11:44:4569 timing.UpdateCurrentDelay(timestamp);
70 wait_time_ms = timing.MaxWaitingTime(
71 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
stefan@webrtc.org9f557c12013-05-17 12:55:0772 clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 11:44:4573 EXPECT_EQ(jitter_delay_ms, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:0774
Åsa Persson8368d1a2018-01-05 11:44:4575 // Add decode time estimates for 1 second.
76 const uint32_t kDecodeTimeMs = 10;
77 for (int i = 0; i < kFps; i++) {
78 clock.AdvanceTimeMilliseconds(kDecodeTimeMs);
Johannes Kronbfd343b2019-07-01 08:07:5079 timing.StopDecodeTimer(kDecodeTimeMs, clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 11:44:4580 timestamp += 90000 / kFps;
81 clock.AdvanceTimeMilliseconds(1000 / kFps - kDecodeTimeMs);
82 timing.IncomingTimestamp(timestamp, clock.TimeInMilliseconds());
stefan@webrtc.org9f557c12013-05-17 12:55:0783 }
Åsa Persson8368d1a2018-01-05 11:44:4584 timing.UpdateCurrentDelay(timestamp);
85 wait_time_ms = timing.MaxWaitingTime(
86 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
stefan@webrtc.org9f557c12013-05-17 12:55:0787 clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 11:44:4588 EXPECT_EQ(jitter_delay_ms, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:0789
Åsa Persson8368d1a2018-01-05 11:44:4590 const int kMinTotalDelayMs = 200;
91 timing.set_min_playout_delay(kMinTotalDelayMs);
stefan@webrtc.org9f557c12013-05-17 12:55:0792 clock.AdvanceTimeMilliseconds(5000);
Åsa Persson8368d1a2018-01-05 11:44:4593 timestamp += 5 * 90000;
94 timing.UpdateCurrentDelay(timestamp);
stefan@webrtc.org9f557c12013-05-17 12:55:0795 const int kRenderDelayMs = 10;
mikhal@webrtc.orgadc64a72013-05-30 16:20:1896 timing.set_render_delay(kRenderDelayMs);
Åsa Persson8368d1a2018-01-05 11:44:4597 wait_time_ms = timing.MaxWaitingTime(
98 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
stefan@webrtc.org9f557c12013-05-17 12:55:0799 clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 11:44:45100 // We should at least have kMinTotalDelayMs - decodeTime (10) - renderTime
stefan@webrtc.org9f557c12013-05-17 12:55:07101 // (10) to wait.
Åsa Persson8368d1a2018-01-05 11:44:45102 EXPECT_EQ(kMinTotalDelayMs - kDecodeTimeMs - kRenderDelayMs, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:07103 // The total video delay should be equal to the min total delay.
Åsa Persson8368d1a2018-01-05 11:44:45104 EXPECT_EQ(kMinTotalDelayMs, timing.TargetVideoDelay());
stefan@webrtc.org9f557c12013-05-17 12:55:07105
mikhal@webrtc.orgadc64a72013-05-30 16:20:18106 // Reset playout delay.
107 timing.set_min_playout_delay(0);
stefan@webrtc.org9f557c12013-05-17 12:55:07108 clock.AdvanceTimeMilliseconds(5000);
Åsa Persson8368d1a2018-01-05 11:44:45109 timestamp += 5 * 90000;
110 timing.UpdateCurrentDelay(timestamp);
stefan@webrtc.org9f557c12013-05-17 12:55:07111}
112
113TEST(ReceiverTiming, WrapAround) {
stefan@webrtc.org9f557c12013-05-17 12:55:07114 SimulatedClock clock(0);
115 VCMTiming timing(&clock);
Åsa Persson8368d1a2018-01-05 11:44:45116 // Provoke a wrap-around. The fifth frame will have wrapped at 25 fps.
117 uint32_t timestamp = 0xFFFFFFFFu - 3 * 90000 / kFps;
118 for (int i = 0; i < 5; ++i) {
stefan@webrtc.org9f557c12013-05-17 12:55:07119 timing.IncomingTimestamp(timestamp, clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 11:44:45120 clock.AdvanceTimeMilliseconds(1000 / kFps);
121 timestamp += 90000 / kFps;
122 EXPECT_EQ(3 * 1000 / kFps,
123 timing.RenderTimeMs(0xFFFFFFFFu, clock.TimeInMilliseconds()));
124 EXPECT_EQ(3 * 1000 / kFps + 1,
125 timing.RenderTimeMs(89u, // One ms later in 90 kHz.
126 clock.TimeInMilliseconds()));
stefan@webrtc.org9f557c12013-05-17 12:55:07127 }
128}
129
130} // namespace webrtc