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/system_wrappers/interface/rtp_to_ntp.h"
16 TEST(WrapAroundTests, NoWrap) {
17 EXPECT_EQ(0, CheckForWrapArounds(0xFFFFFFFF, 0xFFFFFFFE));
18 EXPECT_EQ(0, CheckForWrapArounds(1, 0));
19 EXPECT_EQ(0, CheckForWrapArounds(0x00010000, 0x0000FFFF));
22 TEST(WrapAroundTests, ForwardWrap) {
23 EXPECT_EQ(1, CheckForWrapArounds(0, 0xFFFFFFFF));
24 EXPECT_EQ(1, CheckForWrapArounds(0, 0xFFFF0000));
25 EXPECT_EQ(1, CheckForWrapArounds(0x0000FFFF, 0xFFFFFFFF));
26 EXPECT_EQ(1, CheckForWrapArounds(0x0000FFFF, 0xFFFF0000));
29 TEST(WrapAroundTests, BackwardWrap) {
30 EXPECT_EQ(-1, CheckForWrapArounds(0xFFFFFFFF, 0));
31 EXPECT_EQ(-1, CheckForWrapArounds(0xFFFF0000, 0));
32 EXPECT_EQ(-1, CheckForWrapArounds(0xFFFFFFFF, 0x0000FFFF));
33 EXPECT_EQ(-1, CheckForWrapArounds(0xFFFF0000, 0x0000FFFF));
36 TEST(WrapAroundTests, OldRtcpWrapped) {
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(RtcpMeasurement(ntp_sec, ntp_frac, timestamp));
44 ntp_frac += kOneMsInNtpFrac;
45 timestamp -= kTimestampTicksPerMs;
46 rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp));
47 ntp_frac += kOneMsInNtpFrac;
48 timestamp -= kTimestampTicksPerMs;
49 int64_t timestamp_in_ms = -1;
50 // This expected to fail since it's highly unlikely that the older RTCP
51 // has a much smaller RTP timestamp than the newer.
52 EXPECT_FALSE(RtpToNtpMs(timestamp, rtcp, ×tamp_in_ms));
55 TEST(WrapAroundTests, NewRtcpWrapped) {
58 uint32_t ntp_frac = 0;
59 uint32_t timestamp = 0xFFFFFFFF;
60 const uint32_t kOneMsInNtpFrac = 4294967;
61 const uint32_t kTimestampTicksPerMs = 90;
62 rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp));
63 ntp_frac += kOneMsInNtpFrac;
64 timestamp += kTimestampTicksPerMs;
65 rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp));
66 int64_t timestamp_in_ms = -1;
67 EXPECT_TRUE(RtpToNtpMs(rtcp.back().rtp_timestamp, rtcp, ×tamp_in_ms));
68 // Since this RTP packet has the same timestamp as the RTCP packet constructed
69 // at time 0 it should be mapped to 0 as well.
70 EXPECT_EQ(0, timestamp_in_ms);
73 TEST(WrapAroundTests, RtpWrapped) {
74 const uint32_t kOneMsInNtpFrac = 4294967;
75 const uint32_t kTimestampTicksPerMs = 90;
78 uint32_t ntp_frac = 0;
79 uint32_t timestamp = 0xFFFFFFFF - 2 * kTimestampTicksPerMs;
80 rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp));
81 ntp_frac += kOneMsInNtpFrac;
82 timestamp += kTimestampTicksPerMs;
83 rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp));
84 ntp_frac += kOneMsInNtpFrac;
85 timestamp += kTimestampTicksPerMs;
86 int64_t timestamp_in_ms = -1;
87 EXPECT_TRUE(RtpToNtpMs(timestamp, rtcp, ×tamp_in_ms));
88 // Since this RTP packet has the same timestamp as the RTCP packet constructed
89 // at time 0 it should be mapped to 0 as well.
90 EXPECT_EQ(2, timestamp_in_ms);
93 TEST(WrapAroundTests, OldRtp_RtcpsWrapped) {
94 const uint32_t kOneMsInNtpFrac = 4294967;
95 const uint32_t kTimestampTicksPerMs = 90;
98 uint32_t ntp_frac = 0;
99 uint32_t timestamp = 0;
100 rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp));
101 ntp_frac += kOneMsInNtpFrac;
102 timestamp += kTimestampTicksPerMs;
103 rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp));
104 ntp_frac += kOneMsInNtpFrac;
105 timestamp -= 2*kTimestampTicksPerMs;
106 int64_t timestamp_in_ms = -1;
107 EXPECT_FALSE(RtpToNtpMs(timestamp, rtcp, ×tamp_in_ms));
110 TEST(WrapAroundTests, OldRtp_NewRtcpWrapped) {
111 const uint32_t kOneMsInNtpFrac = 4294967;
112 const uint32_t kTimestampTicksPerMs = 90;
114 uint32_t ntp_sec = 0;
115 uint32_t ntp_frac = 0;
116 uint32_t timestamp = 0xFFFFFFFF;
117 rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp));
118 ntp_frac += kOneMsInNtpFrac;
119 timestamp += kTimestampTicksPerMs;
120 rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp));
121 ntp_frac += kOneMsInNtpFrac;
122 timestamp -= kTimestampTicksPerMs;
123 int64_t timestamp_in_ms = -1;
124 EXPECT_TRUE(RtpToNtpMs(timestamp, rtcp, ×tamp_in_ms));
125 // Constructed at the same time as the first RTCP and should therefore be
127 EXPECT_EQ(0, timestamp_in_ms);
130 TEST(WrapAroundTests, OldRtp_OldRtcpWrapped) {
131 const uint32_t kOneMsInNtpFrac = 4294967;
132 const uint32_t kTimestampTicksPerMs = 90;
134 uint32_t ntp_sec = 0;
135 uint32_t ntp_frac = 0;
136 uint32_t timestamp = 0;
137 rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp));
138 ntp_frac += kOneMsInNtpFrac;
139 timestamp -= kTimestampTicksPerMs;
140 rtcp.push_front(RtcpMeasurement(ntp_sec, ntp_frac, timestamp));
141 ntp_frac += kOneMsInNtpFrac;
142 timestamp += 2*kTimestampTicksPerMs;
143 int64_t timestamp_in_ms = -1;
144 EXPECT_FALSE(RtpToNtpMs(timestamp, rtcp, ×tamp_in_ms));
146 }; // namespace webrtc