2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "webrtc/modules/remote_bitrate_estimator/include/rtp_to_ntp.h"
16 TEST(WrapAroundTests, NoWrap) {
17 EXPECT_EQ(0, synchronization::CheckForWrapArounds(0xFFFFFFFF, 0xFFFFFFFE));
18 EXPECT_EQ(0, synchronization::CheckForWrapArounds(1, 0));
19 EXPECT_EQ(0, synchronization::CheckForWrapArounds(0x00010000, 0x0000FFFF));
22 TEST(WrapAroundTests, ForwardWrap) {
23 EXPECT_EQ(1, synchronization::CheckForWrapArounds(0, 0xFFFFFFFF));
24 EXPECT_EQ(1, synchronization::CheckForWrapArounds(0, 0xFFFF0000));
25 EXPECT_EQ(1, synchronization::CheckForWrapArounds(0x0000FFFF, 0xFFFFFFFF));
26 EXPECT_EQ(1, synchronization::CheckForWrapArounds(0x0000FFFF, 0xFFFF0000));
29 TEST(WrapAroundTests, BackwardWrap) {
30 EXPECT_EQ(-1, synchronization::CheckForWrapArounds(0xFFFFFFFF, 0));
31 EXPECT_EQ(-1, synchronization::CheckForWrapArounds(0xFFFF0000, 0));
32 EXPECT_EQ(-1, synchronization::CheckForWrapArounds(0xFFFFFFFF, 0x0000FFFF));
33 EXPECT_EQ(-1, synchronization::CheckForWrapArounds(0xFFFF0000, 0x0000FFFF));
36 TEST(WrapAroundTests, OldRtcpWrapped) {
37 synchronization::RtcpList rtcp;
39 uint32_t ntp_frac = 0;
40 uint32_t timestamp = 0;
41 const uint32_t kOneMsInNtpFrac = 4294967;
42 const uint32_t kTimestampTicksPerMs = 90;
43 rtcp.push_front(synchronization::RtcpMeasurement(ntp_sec, ntp_frac,
45 ntp_frac += kOneMsInNtpFrac;
46 timestamp -= kTimestampTicksPerMs;
47 rtcp.push_front(synchronization::RtcpMeasurement(ntp_sec, ntp_frac,
49 ntp_frac += kOneMsInNtpFrac;
50 timestamp -= kTimestampTicksPerMs;
51 int64_t timestamp_in_ms = -1;
52 // This expected to fail since it's highly unlikely that the older RTCP
53 // has a much smaller RTP timestamp than the newer.
54 EXPECT_FALSE(synchronization::RtpToNtpMs(timestamp, rtcp, ×tamp_in_ms));
57 TEST(WrapAroundTests, NewRtcpWrapped) {
58 synchronization::RtcpList rtcp;
60 uint32_t ntp_frac = 0;
61 uint32_t timestamp = 0xFFFFFFFF;
62 const uint32_t kOneMsInNtpFrac = 4294967;
63 const uint32_t kTimestampTicksPerMs = 90;
64 rtcp.push_front(synchronization::RtcpMeasurement(ntp_sec, ntp_frac,
66 ntp_frac += kOneMsInNtpFrac;
67 timestamp += kTimestampTicksPerMs;
68 rtcp.push_front(synchronization::RtcpMeasurement(ntp_sec, ntp_frac,
70 int64_t timestamp_in_ms = -1;
71 EXPECT_TRUE(synchronization::RtpToNtpMs(rtcp.back().rtp_timestamp, rtcp,
73 // Since this RTP packet has the same timestamp as the RTCP packet constructed
74 // at time 0 it should be mapped to 0 as well.
75 EXPECT_EQ(0, timestamp_in_ms);
78 TEST(WrapAroundTests, RtpWrapped) {
79 const uint32_t kOneMsInNtpFrac = 4294967;
80 const uint32_t kTimestampTicksPerMs = 90;
81 synchronization::RtcpList rtcp;
83 uint32_t ntp_frac = 0;
84 uint32_t timestamp = 0xFFFFFFFF - 2 * kTimestampTicksPerMs;
85 rtcp.push_front(synchronization::RtcpMeasurement(ntp_sec, ntp_frac,
87 ntp_frac += kOneMsInNtpFrac;
88 timestamp += kTimestampTicksPerMs;
89 rtcp.push_front(synchronization::RtcpMeasurement(ntp_sec, ntp_frac,
91 ntp_frac += kOneMsInNtpFrac;
92 timestamp += kTimestampTicksPerMs;
93 int64_t timestamp_in_ms = -1;
94 EXPECT_TRUE(synchronization::RtpToNtpMs(timestamp, rtcp,
96 // Since this RTP packet has the same timestamp as the RTCP packet constructed
97 // at time 0 it should be mapped to 0 as well.
98 EXPECT_EQ(2, timestamp_in_ms);
101 TEST(WrapAroundTests, OldRtp_RtcpsWrapped) {
102 const uint32_t kOneMsInNtpFrac = 4294967;
103 const uint32_t kTimestampTicksPerMs = 90;
104 synchronization::RtcpList rtcp;
105 uint32_t ntp_sec = 0;
106 uint32_t ntp_frac = 0;
107 uint32_t timestamp = 0;
108 rtcp.push_front(synchronization::RtcpMeasurement(ntp_sec, ntp_frac,
110 ntp_frac += kOneMsInNtpFrac;
111 timestamp += kTimestampTicksPerMs;
112 rtcp.push_front(synchronization::RtcpMeasurement(ntp_sec, ntp_frac,
114 ntp_frac += kOneMsInNtpFrac;
115 timestamp -= 2*kTimestampTicksPerMs;
116 int64_t timestamp_in_ms = -1;
117 EXPECT_FALSE(synchronization::RtpToNtpMs(timestamp, rtcp,
121 TEST(WrapAroundTests, OldRtp_NewRtcpWrapped) {
122 const uint32_t kOneMsInNtpFrac = 4294967;
123 const uint32_t kTimestampTicksPerMs = 90;
124 synchronization::RtcpList rtcp;
125 uint32_t ntp_sec = 0;
126 uint32_t ntp_frac = 0;
127 uint32_t timestamp = 0xFFFFFFFF;
128 rtcp.push_front(synchronization::RtcpMeasurement(ntp_sec, ntp_frac,
130 ntp_frac += kOneMsInNtpFrac;
131 timestamp += kTimestampTicksPerMs;
132 rtcp.push_front(synchronization::RtcpMeasurement(ntp_sec, ntp_frac,
134 ntp_frac += kOneMsInNtpFrac;
135 timestamp -= kTimestampTicksPerMs;
136 int64_t timestamp_in_ms = -1;
137 EXPECT_TRUE(synchronization::RtpToNtpMs(timestamp, rtcp,
139 // Constructed at the same time as the first RTCP and should therefore be
141 EXPECT_EQ(0, timestamp_in_ms);
144 TEST(WrapAroundTests, OldRtp_OldRtcpWrapped) {
145 const uint32_t kOneMsInNtpFrac = 4294967;
146 const uint32_t kTimestampTicksPerMs = 90;
147 synchronization::RtcpList rtcp;
148 uint32_t ntp_sec = 0;
149 uint32_t ntp_frac = 0;
150 uint32_t timestamp = 0;
151 rtcp.push_front(synchronization::RtcpMeasurement(ntp_sec, ntp_frac,
153 ntp_frac += kOneMsInNtpFrac;
154 timestamp -= kTimestampTicksPerMs;
155 rtcp.push_front(synchronization::RtcpMeasurement(ntp_sec, ntp_frac,
157 ntp_frac += kOneMsInNtpFrac;
158 timestamp += 2*kTimestampTicksPerMs;
159 int64_t timestamp_in_ms = -1;
160 EXPECT_FALSE(synchronization::RtpToNtpMs(timestamp, rtcp,
163 }; // namespace webrtc