1/*
2 *  Copyright (c) 2012 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
11#include "testing/gtest/include/gtest/gtest.h"
12#include "webrtc/system_wrappers/interface/rtp_to_ntp.h"
13
14namespace webrtc {
15
16TEST(WrapAroundTests, NoWrap) {
17  EXPECT_EQ(0, CheckForWrapArounds(0xFFFFFFFF, 0xFFFFFFFE));
18  EXPECT_EQ(0, CheckForWrapArounds(1, 0));
19  EXPECT_EQ(0, CheckForWrapArounds(0x00010000, 0x0000FFFF));
20}
21
22TEST(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));
27}
28
29TEST(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));
34}
35
36TEST(WrapAroundTests, OldRtcpWrapped) {
37  RtcpList rtcp;
38  uint32_t ntp_sec = 0;
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, &timestamp_in_ms));
53}
54
55TEST(WrapAroundTests, NewRtcpWrapped) {
56  RtcpList rtcp;
57  uint32_t ntp_sec = 0;
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, &timestamp_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);
71}
72
73TEST(WrapAroundTests, RtpWrapped) {
74  const uint32_t kOneMsInNtpFrac = 4294967;
75  const uint32_t kTimestampTicksPerMs = 90;
76  RtcpList rtcp;
77  uint32_t ntp_sec = 0;
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, &timestamp_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);
91}
92
93TEST(WrapAroundTests, OldRtp_RtcpsWrapped) {
94  const uint32_t kOneMsInNtpFrac = 4294967;
95  const uint32_t kTimestampTicksPerMs = 90;
96  RtcpList rtcp;
97  uint32_t ntp_sec = 0;
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, &timestamp_in_ms));
108}
109
110TEST(WrapAroundTests, OldRtp_NewRtcpWrapped) {
111  const uint32_t kOneMsInNtpFrac = 4294967;
112  const uint32_t kTimestampTicksPerMs = 90;
113  RtcpList rtcp;
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, &timestamp_in_ms));
125  // Constructed at the same time as the first RTCP and should therefore be
126  // mapped to zero.
127  EXPECT_EQ(0, timestamp_in_ms);
128}
129
130TEST(WrapAroundTests, OldRtp_OldRtcpWrapped) {
131  const uint32_t kOneMsInNtpFrac = 4294967;
132  const uint32_t kTimestampTicksPerMs = 90;
133  RtcpList rtcp;
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, &timestamp_in_ms));
145}
146};  // namespace webrtc
147