OLD | NEW |
| (Empty) |
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 "webrtc/system_wrappers/include/rtp_to_ntp.h" | |
12 #include "webrtc/test/gtest.h" | |
13 | |
14 namespace webrtc { | |
15 namespace { | |
16 const uint32_t kOneMsInNtpFrac = 4294967; | |
17 const uint32_t kTimestampTicksPerMs = 90; | |
18 } // namespace | |
19 | |
20 TEST(WrapAroundTests, NoWrap) { | |
21 EXPECT_EQ(0, CheckForWrapArounds(0xFFFFFFFF, 0xFFFFFFFE)); | |
22 EXPECT_EQ(0, CheckForWrapArounds(1, 0)); | |
23 EXPECT_EQ(0, CheckForWrapArounds(0x00010000, 0x0000FFFF)); | |
24 } | |
25 | |
26 TEST(WrapAroundTests, ForwardWrap) { | |
27 EXPECT_EQ(1, CheckForWrapArounds(0, 0xFFFFFFFF)); | |
28 EXPECT_EQ(1, CheckForWrapArounds(0, 0xFFFF0000)); | |
29 EXPECT_EQ(1, CheckForWrapArounds(0x0000FFFF, 0xFFFFFFFF)); | |
30 EXPECT_EQ(1, CheckForWrapArounds(0x0000FFFF, 0xFFFF0000)); | |
31 } | |
32 | |
33 TEST(WrapAroundTests, BackwardWrap) { | |
34 EXPECT_EQ(-1, CheckForWrapArounds(0xFFFFFFFF, 0)); | |
35 EXPECT_EQ(-1, CheckForWrapArounds(0xFFFF0000, 0)); | |
36 EXPECT_EQ(-1, CheckForWrapArounds(0xFFFFFFFF, 0x0000FFFF)); | |
37 EXPECT_EQ(-1, CheckForWrapArounds(0xFFFF0000, 0x0000FFFF)); | |
38 } | |
39 | |
40 TEST(WrapAroundTests, OldRtcpWrapped_OldRtpTimestamp) { | |
41 RtcpMeasurements rtcp; | |
42 bool new_sr; | |
43 uint32_t ntp_sec = 0; | |
44 uint32_t ntp_frac = 1; | |
45 uint32_t timestamp = 0; | |
46 EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
47 ntp_frac += kOneMsInNtpFrac; | |
48 timestamp -= kTimestampTicksPerMs; | |
49 // Expected to fail since the older RTCP has a smaller RTP timestamp than the | |
50 // newer (old:0, new:4294967206). | |
51 EXPECT_FALSE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
52 } | |
53 | |
54 TEST(WrapAroundTests, NewRtcpWrapped) { | |
55 RtcpMeasurements rtcp; | |
56 bool new_sr; | |
57 uint32_t ntp_sec = 0; | |
58 uint32_t ntp_frac = 1; | |
59 uint32_t timestamp = 0xFFFFFFFF; | |
60 EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
61 ntp_frac += kOneMsInNtpFrac; | |
62 timestamp += kTimestampTicksPerMs; | |
63 EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
64 int64_t timestamp_ms = -1; | |
65 EXPECT_TRUE(RtpToNtpMs(rtcp.list.back().rtp_timestamp, rtcp, ×tamp_ms)); | |
66 // Since this RTP packet has the same timestamp as the RTCP packet constructed | |
67 // at time 0 it should be mapped to 0 as well. | |
68 EXPECT_EQ(0, timestamp_ms); | |
69 } | |
70 | |
71 TEST(WrapAroundTests, RtpWrapped) { | |
72 RtcpMeasurements rtcp; | |
73 bool new_sr; | |
74 uint32_t ntp_sec = 0; | |
75 uint32_t ntp_frac = 1; | |
76 uint32_t timestamp = 0xFFFFFFFF - 2 * kTimestampTicksPerMs; | |
77 EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
78 ntp_frac += kOneMsInNtpFrac; | |
79 timestamp += kTimestampTicksPerMs; | |
80 EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
81 | |
82 int64_t timestamp_ms = -1; | |
83 EXPECT_TRUE(RtpToNtpMs(rtcp.list.back().rtp_timestamp, rtcp, ×tamp_ms)); | |
84 // Since this RTP packet has the same timestamp as the RTCP packet constructed | |
85 // at time 0 it should be mapped to 0 as well. | |
86 EXPECT_EQ(0, timestamp_ms); | |
87 // Two kTimestampTicksPerMs advanced. | |
88 timestamp += kTimestampTicksPerMs; | |
89 EXPECT_TRUE(RtpToNtpMs(timestamp, rtcp, ×tamp_ms)); | |
90 EXPECT_EQ(2, timestamp_ms); | |
91 // Wrapped rtp. | |
92 timestamp += kTimestampTicksPerMs; | |
93 EXPECT_TRUE(RtpToNtpMs(timestamp, rtcp, ×tamp_ms)); | |
94 EXPECT_EQ(3, timestamp_ms); | |
95 } | |
96 | |
97 TEST(WrapAroundTests, OldRtp_RtcpsWrapped) { | |
98 RtcpMeasurements rtcp; | |
99 bool new_sr; | |
100 uint32_t ntp_sec = 0; | |
101 uint32_t ntp_frac = 1; | |
102 uint32_t timestamp = 0; | |
103 EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
104 ntp_frac += kOneMsInNtpFrac; | |
105 timestamp += kTimestampTicksPerMs; | |
106 EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
107 timestamp -= 2*kTimestampTicksPerMs; | |
108 int64_t timestamp_ms = -1; | |
109 EXPECT_FALSE(RtpToNtpMs(timestamp, rtcp, ×tamp_ms)); | |
110 } | |
111 | |
112 TEST(WrapAroundTests, OldRtp_NewRtcpWrapped) { | |
113 RtcpMeasurements rtcp; | |
114 bool new_sr; | |
115 uint32_t ntp_sec = 0; | |
116 uint32_t ntp_frac = 1; | |
117 uint32_t timestamp = 0xFFFFFFFF; | |
118 EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
119 ntp_frac += kOneMsInNtpFrac; | |
120 timestamp += kTimestampTicksPerMs; | |
121 EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
122 timestamp -= kTimestampTicksPerMs; | |
123 int64_t timestamp_ms = -1; | |
124 EXPECT_TRUE(RtpToNtpMs(timestamp, rtcp, ×tamp_ms)); | |
125 // Constructed at the same time as the first RTCP and should therefore be | |
126 // mapped to zero. | |
127 EXPECT_EQ(0, timestamp_ms); | |
128 } | |
129 | |
130 TEST(UpdateRtcpListTests, InjectRtcpSr) { | |
131 const uint32_t kNtpSec = 10; | |
132 const uint32_t kNtpFrac = 12345; | |
133 const uint32_t kTs = 0x12345678; | |
134 bool new_sr; | |
135 RtcpMeasurements rtcp; | |
136 EXPECT_TRUE(UpdateRtcpList(kNtpSec, kNtpFrac, kTs, &rtcp, &new_sr)); | |
137 EXPECT_TRUE(new_sr); | |
138 EXPECT_EQ(1u, rtcp.list.size()); | |
139 EXPECT_EQ(kNtpSec, rtcp.list.front().ntp_time.seconds()); | |
140 EXPECT_EQ(kNtpFrac, rtcp.list.front().ntp_time.fractions()); | |
141 EXPECT_EQ(kTs, rtcp.list.front().rtp_timestamp); | |
142 // Add second report. | |
143 EXPECT_TRUE(UpdateRtcpList(kNtpSec, kNtpFrac + kOneMsInNtpFrac, kTs + 1, | |
144 &rtcp, &new_sr)); | |
145 EXPECT_EQ(2u, rtcp.list.size()); | |
146 EXPECT_EQ(kTs + 1, rtcp.list.front().rtp_timestamp); | |
147 EXPECT_EQ(kTs + 0, rtcp.list.back().rtp_timestamp); | |
148 // List should contain last two reports. | |
149 EXPECT_TRUE(UpdateRtcpList(kNtpSec, kNtpFrac + 2 * kOneMsInNtpFrac, kTs + 2, | |
150 &rtcp, &new_sr)); | |
151 EXPECT_EQ(2u, rtcp.list.size()); | |
152 EXPECT_EQ(kTs + 2, rtcp.list.front().rtp_timestamp); | |
153 EXPECT_EQ(kTs + 1, rtcp.list.back().rtp_timestamp); | |
154 } | |
155 | |
156 TEST(UpdateRtcpListTests, FailsForZeroNtp) { | |
157 RtcpMeasurements rtcp; | |
158 uint32_t ntp_sec = 0; | |
159 uint32_t ntp_frac = 0; | |
160 uint32_t timestamp = 0x12345678; | |
161 bool new_sr; | |
162 EXPECT_FALSE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
163 EXPECT_FALSE(new_sr); | |
164 EXPECT_EQ(0u, rtcp.list.size()); | |
165 } | |
166 | |
167 TEST(UpdateRtcpListTests, FailsForEqualNtp) { | |
168 RtcpMeasurements rtcp; | |
169 uint32_t ntp_sec = 0; | |
170 uint32_t ntp_frac = 699925050; | |
171 uint32_t timestamp = 0x12345678; | |
172 bool new_sr; | |
173 EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
174 EXPECT_TRUE(new_sr); | |
175 EXPECT_EQ(1u, rtcp.list.size()); | |
176 // Ntp time already added, list not updated. | |
177 ++timestamp; | |
178 EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
179 EXPECT_FALSE(new_sr); | |
180 EXPECT_EQ(1u, rtcp.list.size()); | |
181 } | |
182 | |
183 TEST(UpdateRtcpListTests, FailsForOldNtp) { | |
184 RtcpMeasurements rtcp; | |
185 uint32_t ntp_sec = 1; | |
186 uint32_t ntp_frac = 699925050; | |
187 uint32_t timestamp = 0x12345678; | |
188 bool new_sr; | |
189 EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
190 EXPECT_TRUE(new_sr); | |
191 EXPECT_EQ(1u, rtcp.list.size()); | |
192 // Old ntp time, list not updated. | |
193 ntp_frac -= kOneMsInNtpFrac; | |
194 timestamp += kTimestampTicksPerMs; | |
195 EXPECT_FALSE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
196 EXPECT_EQ(1u, rtcp.list.size()); | |
197 } | |
198 | |
199 TEST(UpdateRtcpListTests, FailsForEqualTimestamp) { | |
200 RtcpMeasurements rtcp; | |
201 uint32_t ntp_sec = 0; | |
202 uint32_t ntp_frac = 2; | |
203 uint32_t timestamp = 0x12345678; | |
204 bool new_sr; | |
205 EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
206 EXPECT_TRUE(new_sr); | |
207 EXPECT_EQ(1u, rtcp.list.size()); | |
208 // Timestamp already added, list not updated. | |
209 ++ntp_frac; | |
210 EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
211 EXPECT_FALSE(new_sr); | |
212 EXPECT_EQ(1u, rtcp.list.size()); | |
213 } | |
214 | |
215 TEST(UpdateRtcpListTests, FailsForOldRtpTimestamp) { | |
216 RtcpMeasurements rtcp; | |
217 uint32_t ntp_sec = 0; | |
218 uint32_t ntp_frac = 2; | |
219 uint32_t timestamp = 0x12345678; | |
220 bool new_sr; | |
221 EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
222 EXPECT_TRUE(new_sr); | |
223 EXPECT_EQ(1u, rtcp.list.size()); | |
224 // Old timestamp, list not updated. | |
225 ntp_frac += kOneMsInNtpFrac; | |
226 timestamp -= kTimestampTicksPerMs; | |
227 EXPECT_FALSE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
228 EXPECT_FALSE(new_sr); | |
229 EXPECT_EQ(1u, rtcp.list.size()); | |
230 } | |
231 | |
232 TEST(UpdateRtcpListTests, VerifyParameters) { | |
233 RtcpMeasurements rtcp; | |
234 uint32_t ntp_sec = 1; | |
235 uint32_t ntp_frac = 2; | |
236 uint32_t timestamp = 0x12345678; | |
237 bool new_sr; | |
238 EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
239 EXPECT_TRUE(new_sr); | |
240 EXPECT_FALSE(rtcp.params.calculated); | |
241 // Add second report, parameters should be calculated. | |
242 ntp_frac += kOneMsInNtpFrac; | |
243 timestamp += kTimestampTicksPerMs; | |
244 EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
245 EXPECT_TRUE(rtcp.params.calculated); | |
246 EXPECT_DOUBLE_EQ(90.0, rtcp.params.frequency_khz); | |
247 EXPECT_NE(0.0, rtcp.params.offset_ms); | |
248 } | |
249 | |
250 TEST(RtpToNtpTests, FailsForEmptyList) { | |
251 RtcpMeasurements rtcp; | |
252 rtcp.params.calculated = true; | |
253 // List is empty, conversion of RTP to NTP time should fail. | |
254 EXPECT_EQ(0u, rtcp.list.size()); | |
255 int64_t timestamp_ms = -1; | |
256 EXPECT_FALSE(RtpToNtpMs(0, rtcp, ×tamp_ms)); | |
257 } | |
258 | |
259 TEST(RtpToNtpTests, FailsForNoParameters) { | |
260 RtcpMeasurements rtcp; | |
261 uint32_t ntp_sec = 1; | |
262 uint32_t ntp_frac = 2; | |
263 uint32_t timestamp = 0x12345678; | |
264 bool new_sr; | |
265 EXPECT_TRUE(UpdateRtcpList(ntp_sec, ntp_frac, timestamp, &rtcp, &new_sr)); | |
266 EXPECT_EQ(1u, rtcp.list.size()); | |
267 // Parameters are not calculated, conversion of RTP to NTP time should fail. | |
268 EXPECT_FALSE(rtcp.params.calculated); | |
269 int64_t timestamp_ms = -1; | |
270 EXPECT_FALSE(RtpToNtpMs(timestamp, rtcp, ×tamp_ms)); | |
271 } | |
272 | |
273 }; // namespace webrtc | |
OLD | NEW |