blob: 40e8c972b62e965a2782ce0a3c6816fe966ae1b3 [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"
12#include "system_wrappers/include/clock.h"
13#include "test/gtest.h"
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000014
15namespace webrtc {
Åsa Persson8368d1a2018-01-05 12:44:45 +010016namespace {
17const int kFps = 25;
18} // namespace
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000019
20TEST(ReceiverTiming, Tests) {
21 SimulatedClock clock(0);
22 VCMTiming timing(&clock);
Åsa Persson8368d1a2018-01-05 12:44:45 +010023 timing.Reset();
24
25 uint32_t timestamp = 0;
26 timing.UpdateCurrentDelay(timestamp);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000027
28 timing.Reset();
29
Åsa Persson8368d1a2018-01-05 12:44:45 +010030 timing.IncomingTimestamp(timestamp, clock.TimeInMilliseconds());
31 uint32_t jitter_delay_ms = 20;
32 timing.SetJitterDelay(jitter_delay_ms);
33 timing.UpdateCurrentDelay(timestamp);
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000034 timing.set_render_delay(0);
Åsa Persson8368d1a2018-01-05 12:44:45 +010035 uint32_t wait_time_ms = timing.MaxWaitingTime(
36 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000037 clock.TimeInMilliseconds());
38 // First update initializes the render time. Since we have no decode delay
Åsa Persson8368d1a2018-01-05 12:44:45 +010039 // we get wait_time_ms = renderTime - now - renderDelay = jitter.
40 EXPECT_EQ(jitter_delay_ms, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000041
Åsa Persson8368d1a2018-01-05 12:44:45 +010042 jitter_delay_ms += VCMTiming::kDelayMaxChangeMsPerS + 10;
43 timestamp += 90000;
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000044 clock.AdvanceTimeMilliseconds(1000);
Åsa Persson8368d1a2018-01-05 12:44:45 +010045 timing.SetJitterDelay(jitter_delay_ms);
46 timing.UpdateCurrentDelay(timestamp);
47 wait_time_ms = timing.MaxWaitingTime(
48 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
philipel5908c712015-12-21 08:23:20 -080049 clock.TimeInMilliseconds());
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000050 // Since we gradually increase the delay we only get 100 ms every second.
Åsa Persson8368d1a2018-01-05 12:44:45 +010051 EXPECT_EQ(jitter_delay_ms - 10, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000052
Åsa Persson8368d1a2018-01-05 12:44:45 +010053 timestamp += 90000;
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000054 clock.AdvanceTimeMilliseconds(1000);
Åsa Persson8368d1a2018-01-05 12:44:45 +010055 timing.UpdateCurrentDelay(timestamp);
56 wait_time_ms = timing.MaxWaitingTime(
57 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000058 clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 12:44:45 +010059 EXPECT_EQ(jitter_delay_ms, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000060
Åsa Persson8368d1a2018-01-05 12:44:45 +010061 // Insert frames without jitter, verify that this gives the exact wait time.
62 const int kNumFrames = 300;
63 for (int i = 0; i < kNumFrames; i++) {
64 clock.AdvanceTimeMilliseconds(1000 / kFps);
65 timestamp += 90000 / kFps;
66 timing.IncomingTimestamp(timestamp, clock.TimeInMilliseconds());
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000067 }
Åsa Persson8368d1a2018-01-05 12:44:45 +010068 timing.UpdateCurrentDelay(timestamp);
69 wait_time_ms = timing.MaxWaitingTime(
70 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000071 clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 12:44:45 +010072 EXPECT_EQ(jitter_delay_ms, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000073
Åsa Persson8368d1a2018-01-05 12:44:45 +010074 // Add decode time estimates for 1 second.
75 const uint32_t kDecodeTimeMs = 10;
76 for (int i = 0; i < kFps; i++) {
77 clock.AdvanceTimeMilliseconds(kDecodeTimeMs);
Johannes Kronbfd343b2019-07-01 10:07:50 +020078 timing.StopDecodeTimer(kDecodeTimeMs, clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 12:44:45 +010079 timestamp += 90000 / kFps;
80 clock.AdvanceTimeMilliseconds(1000 / kFps - kDecodeTimeMs);
81 timing.IncomingTimestamp(timestamp, clock.TimeInMilliseconds());
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000082 }
Åsa Persson8368d1a2018-01-05 12:44:45 +010083 timing.UpdateCurrentDelay(timestamp);
84 wait_time_ms = timing.MaxWaitingTime(
85 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000086 clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 12:44:45 +010087 EXPECT_EQ(jitter_delay_ms, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000088
Åsa Persson8368d1a2018-01-05 12:44:45 +010089 const int kMinTotalDelayMs = 200;
90 timing.set_min_playout_delay(kMinTotalDelayMs);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000091 clock.AdvanceTimeMilliseconds(5000);
Åsa Persson8368d1a2018-01-05 12:44:45 +010092 timestamp += 5 * 90000;
93 timing.UpdateCurrentDelay(timestamp);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000094 const int kRenderDelayMs = 10;
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000095 timing.set_render_delay(kRenderDelayMs);
Åsa Persson8368d1a2018-01-05 12:44:45 +010096 wait_time_ms = timing.MaxWaitingTime(
97 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000098 clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 12:44:45 +010099 // We should at least have kMinTotalDelayMs - decodeTime (10) - renderTime
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000100 // (10) to wait.
Åsa Persson8368d1a2018-01-05 12:44:45 +0100101 EXPECT_EQ(kMinTotalDelayMs - kDecodeTimeMs - kRenderDelayMs, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000102 // The total video delay should be equal to the min total delay.
Åsa Persson8368d1a2018-01-05 12:44:45 +0100103 EXPECT_EQ(kMinTotalDelayMs, timing.TargetVideoDelay());
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000104
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +0000105 // Reset playout delay.
106 timing.set_min_playout_delay(0);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000107 clock.AdvanceTimeMilliseconds(5000);
Åsa Persson8368d1a2018-01-05 12:44:45 +0100108 timestamp += 5 * 90000;
109 timing.UpdateCurrentDelay(timestamp);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000110}
111
112TEST(ReceiverTiming, WrapAround) {
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000113 SimulatedClock clock(0);
114 VCMTiming timing(&clock);
Åsa Persson8368d1a2018-01-05 12:44:45 +0100115 // Provoke a wrap-around. The fifth frame will have wrapped at 25 fps.
116 uint32_t timestamp = 0xFFFFFFFFu - 3 * 90000 / kFps;
117 for (int i = 0; i < 5; ++i) {
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000118 timing.IncomingTimestamp(timestamp, clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 12:44:45 +0100119 clock.AdvanceTimeMilliseconds(1000 / kFps);
120 timestamp += 90000 / kFps;
121 EXPECT_EQ(3 * 1000 / kFps,
122 timing.RenderTimeMs(0xFFFFFFFFu, clock.TimeInMilliseconds()));
123 EXPECT_EQ(3 * 1000 / kFps + 1,
124 timing.RenderTimeMs(89u, // One ms later in 90 kHz.
125 clock.TimeInMilliseconds()));
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000126 }
127}
128
129} // namespace webrtc