blob: ee86605fb6feff0454b66470fd3dee153879c9b3 [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"
14#include "test/gtest.h"
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000015
16namespace webrtc {
Åsa Persson8368d1a2018-01-05 12:44:45 +010017namespace {
18const int kFps = 25;
19} // namespace
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000020
21TEST(ReceiverTiming, Tests) {
22 SimulatedClock clock(0);
23 VCMTiming timing(&clock);
Åsa Persson8368d1a2018-01-05 12:44:45 +010024 timing.Reset();
25
26 uint32_t timestamp = 0;
27 timing.UpdateCurrentDelay(timestamp);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000028
29 timing.Reset();
30
Åsa Persson8368d1a2018-01-05 12:44:45 +010031 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:18 +000035 timing.set_render_delay(0);
Åsa Persson8368d1a2018-01-05 12:44:45 +010036 uint32_t wait_time_ms = timing.MaxWaitingTime(
37 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000038 clock.TimeInMilliseconds());
39 // First update initializes the render time. Since we have no decode delay
Åsa Persson8368d1a2018-01-05 12:44:45 +010040 // 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:07 +000042
Åsa Persson8368d1a2018-01-05 12:44:45 +010043 jitter_delay_ms += VCMTiming::kDelayMaxChangeMsPerS + 10;
44 timestamp += 90000;
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000045 clock.AdvanceTimeMilliseconds(1000);
Åsa Persson8368d1a2018-01-05 12:44:45 +010046 timing.SetJitterDelay(jitter_delay_ms);
47 timing.UpdateCurrentDelay(timestamp);
48 wait_time_ms = timing.MaxWaitingTime(
49 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
philipel5908c712015-12-21 08:23:20 -080050 clock.TimeInMilliseconds());
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000051 // Since we gradually increase the delay we only get 100 ms every second.
Åsa Persson8368d1a2018-01-05 12:44:45 +010052 EXPECT_EQ(jitter_delay_ms - 10, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000053
Åsa Persson8368d1a2018-01-05 12:44:45 +010054 timestamp += 90000;
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000055 clock.AdvanceTimeMilliseconds(1000);
Åsa Persson8368d1a2018-01-05 12:44:45 +010056 timing.UpdateCurrentDelay(timestamp);
57 wait_time_ms = timing.MaxWaitingTime(
58 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000059 clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 12:44:45 +010060 EXPECT_EQ(jitter_delay_ms, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000061
Åsa Persson8368d1a2018-01-05 12:44:45 +010062 // 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:07 +000068 }
Åsa Persson8368d1a2018-01-05 12:44:45 +010069 timing.UpdateCurrentDelay(timestamp);
70 wait_time_ms = timing.MaxWaitingTime(
71 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000072 clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 12:44:45 +010073 EXPECT_EQ(jitter_delay_ms, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000074
Åsa Persson8368d1a2018-01-05 12:44:45 +010075 // 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 10:07:50 +020079 timing.StopDecodeTimer(kDecodeTimeMs, clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 12:44:45 +010080 timestamp += 90000 / kFps;
81 clock.AdvanceTimeMilliseconds(1000 / kFps - kDecodeTimeMs);
82 timing.IncomingTimestamp(timestamp, clock.TimeInMilliseconds());
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000083 }
Åsa Persson8368d1a2018-01-05 12:44:45 +010084 timing.UpdateCurrentDelay(timestamp);
85 wait_time_ms = timing.MaxWaitingTime(
86 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000087 clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 12:44:45 +010088 EXPECT_EQ(jitter_delay_ms, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000089
Åsa Persson8368d1a2018-01-05 12:44:45 +010090 const int kMinTotalDelayMs = 200;
91 timing.set_min_playout_delay(kMinTotalDelayMs);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000092 clock.AdvanceTimeMilliseconds(5000);
Åsa Persson8368d1a2018-01-05 12:44:45 +010093 timestamp += 5 * 90000;
94 timing.UpdateCurrentDelay(timestamp);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000095 const int kRenderDelayMs = 10;
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +000096 timing.set_render_delay(kRenderDelayMs);
Åsa Persson8368d1a2018-01-05 12:44:45 +010097 wait_time_ms = timing.MaxWaitingTime(
98 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()),
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000099 clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 12:44:45 +0100100 // We should at least have kMinTotalDelayMs - decodeTime (10) - renderTime
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000101 // (10) to wait.
Åsa Persson8368d1a2018-01-05 12:44:45 +0100102 EXPECT_EQ(kMinTotalDelayMs - kDecodeTimeMs - kRenderDelayMs, wait_time_ms);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000103 // The total video delay should be equal to the min total delay.
Åsa Persson8368d1a2018-01-05 12:44:45 +0100104 EXPECT_EQ(kMinTotalDelayMs, timing.TargetVideoDelay());
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000105
mikhal@webrtc.orgadc64a72013-05-30 16:20:18 +0000106 // Reset playout delay.
107 timing.set_min_playout_delay(0);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000108 clock.AdvanceTimeMilliseconds(5000);
Åsa Persson8368d1a2018-01-05 12:44:45 +0100109 timestamp += 5 * 90000;
110 timing.UpdateCurrentDelay(timestamp);
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000111}
112
113TEST(ReceiverTiming, WrapAround) {
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000114 SimulatedClock clock(0);
115 VCMTiming timing(&clock);
Åsa Persson8368d1a2018-01-05 12:44:45 +0100116 // 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:07 +0000119 timing.IncomingTimestamp(timestamp, clock.TimeInMilliseconds());
Åsa Persson8368d1a2018-01-05 12:44:45 +0100120 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:07 +0000127 }
128}
129
130} // namespace webrtc