andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | d5d709e | 2013-05-27 12:41:33 +0000 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | #include "webrtc/modules/remote_bitrate_estimator/include/rtp_to_ntp.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 13 | |
| 14 | namespace webrtc { |
| 15 | |
| 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)); |
| 20 | } |
| 21 | |
| 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)); |
| 27 | } |
| 28 | |
| 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)); |
| 34 | } |
| 35 | |
| 36 | TEST(WrapAroundTests, OldRtcpWrapped) { |
| 37 | synchronization::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(synchronization::RtcpMeasurement(ntp_sec, ntp_frac, |
| 44 | timestamp)); |
| 45 | ntp_frac += kOneMsInNtpFrac; |
| 46 | timestamp -= kTimestampTicksPerMs; |
| 47 | rtcp.push_front(synchronization::RtcpMeasurement(ntp_sec, ntp_frac, |
| 48 | timestamp)); |
| 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)); |
| 55 | } |
| 56 | |
| 57 | TEST(WrapAroundTests, NewRtcpWrapped) { |
| 58 | synchronization::RtcpList rtcp; |
| 59 | uint32_t ntp_sec = 0; |
| 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, |
| 65 | timestamp)); |
| 66 | ntp_frac += kOneMsInNtpFrac; |
| 67 | timestamp += kTimestampTicksPerMs; |
| 68 | rtcp.push_front(synchronization::RtcpMeasurement(ntp_sec, ntp_frac, |
| 69 | timestamp)); |
| 70 | int64_t timestamp_in_ms = -1; |
| 71 | EXPECT_TRUE(synchronization::RtpToNtpMs(rtcp.back().rtp_timestamp, rtcp, |
| 72 | ×tamp_in_ms)); |
| 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); |
| 76 | } |
| 77 | |
| 78 | TEST(WrapAroundTests, RtpWrapped) { |
| 79 | const uint32_t kOneMsInNtpFrac = 4294967; |
| 80 | const uint32_t kTimestampTicksPerMs = 90; |
| 81 | synchronization::RtcpList rtcp; |
| 82 | uint32_t ntp_sec = 0; |
| 83 | uint32_t ntp_frac = 0; |
| 84 | uint32_t timestamp = 0xFFFFFFFF - 2 * kTimestampTicksPerMs; |
| 85 | rtcp.push_front(synchronization::RtcpMeasurement(ntp_sec, ntp_frac, |
| 86 | timestamp)); |
| 87 | ntp_frac += kOneMsInNtpFrac; |
| 88 | timestamp += kTimestampTicksPerMs; |
| 89 | rtcp.push_front(synchronization::RtcpMeasurement(ntp_sec, ntp_frac, |
| 90 | timestamp)); |
| 91 | ntp_frac += kOneMsInNtpFrac; |
| 92 | timestamp += kTimestampTicksPerMs; |
| 93 | int64_t timestamp_in_ms = -1; |
| 94 | EXPECT_TRUE(synchronization::RtpToNtpMs(timestamp, rtcp, |
| 95 | ×tamp_in_ms)); |
| 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); |
| 99 | } |
| 100 | |
| 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, |
| 109 | timestamp)); |
| 110 | ntp_frac += kOneMsInNtpFrac; |
| 111 | timestamp += kTimestampTicksPerMs; |
| 112 | rtcp.push_front(synchronization::RtcpMeasurement(ntp_sec, ntp_frac, |
| 113 | timestamp)); |
| 114 | ntp_frac += kOneMsInNtpFrac; |
| 115 | timestamp -= 2*kTimestampTicksPerMs; |
| 116 | int64_t timestamp_in_ms = -1; |
| 117 | EXPECT_FALSE(synchronization::RtpToNtpMs(timestamp, rtcp, |
| 118 | ×tamp_in_ms)); |
| 119 | } |
| 120 | |
| 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, |
| 129 | timestamp)); |
| 130 | ntp_frac += kOneMsInNtpFrac; |
| 131 | timestamp += kTimestampTicksPerMs; |
| 132 | rtcp.push_front(synchronization::RtcpMeasurement(ntp_sec, ntp_frac, |
| 133 | timestamp)); |
| 134 | ntp_frac += kOneMsInNtpFrac; |
| 135 | timestamp -= kTimestampTicksPerMs; |
| 136 | int64_t timestamp_in_ms = -1; |
| 137 | EXPECT_TRUE(synchronization::RtpToNtpMs(timestamp, rtcp, |
| 138 | ×tamp_in_ms)); |
| 139 | // Constructed at the same time as the first RTCP and should therefore be |
| 140 | // mapped to zero. |
| 141 | EXPECT_EQ(0, timestamp_in_ms); |
| 142 | } |
| 143 | |
| 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, |
| 152 | timestamp)); |
| 153 | ntp_frac += kOneMsInNtpFrac; |
| 154 | timestamp -= kTimestampTicksPerMs; |
| 155 | rtcp.push_front(synchronization::RtcpMeasurement(ntp_sec, ntp_frac, |
| 156 | timestamp)); |
| 157 | ntp_frac += kOneMsInNtpFrac; |
| 158 | timestamp += 2*kTimestampTicksPerMs; |
| 159 | int64_t timestamp_in_ms = -1; |
| 160 | EXPECT_FALSE(synchronization::RtpToNtpMs(timestamp, rtcp, |
| 161 | ×tamp_in_ms)); |
| 162 | } |
| 163 | }; // namespace webrtc |