blob: b9397ea005cbeab548e273834dfccff38e9ea6cc [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);
philipel5908c712015-12-21 08:23:20 -080078 timing.StopDecodeTimer(
Åsa Persson8368d1a2018-01-05 12:44:45 +010079 timestamp, kDecodeTimeMs, clock.TimeInMilliseconds(),
80 timing.RenderTimeMs(timestamp, clock.TimeInMilliseconds()));
81 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()),
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000088 clock.TimeInMilliseconds());
Å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()),
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000100 clock.TimeInMilliseconds());
Å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
114TEST(ReceiverTiming, WrapAround) {
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
131} // namespace webrtc