Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: webrtc/system_wrappers/source/rtp_to_ntp_estimator_unittest.cc

Issue 2574133003: Make class of static functions in rtp_to_ntp.h: (Closed)
Patch Set: rebase Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_estimator.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 RtpToNtpEstimator estimator;
42 bool new_sr;
43 uint32_t ntp_sec = 0;
44 uint32_t ntp_frac = 1;
45 uint32_t timestamp = 0;
46 EXPECT_TRUE(
47 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
48 ntp_frac += kOneMsInNtpFrac;
49 timestamp -= kTimestampTicksPerMs;
50 // Expected to fail since the older RTCP has a smaller RTP timestamp than the
51 // newer (old:0, new:4294967206).
52 EXPECT_FALSE(
53 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
54 }
55
56 TEST(WrapAroundTests, NewRtcpWrapped) {
57 RtpToNtpEstimator estimator;
58 bool new_sr;
59 uint32_t ntp_sec = 0;
60 uint32_t ntp_frac = 1;
61 uint32_t timestamp = 0xFFFFFFFF;
62 EXPECT_TRUE(
63 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
64 ntp_frac += kOneMsInNtpFrac;
65 timestamp += kTimestampTicksPerMs;
66 EXPECT_TRUE(
67 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
68 int64_t timestamp_ms = -1;
69 EXPECT_TRUE(estimator.Estimate(estimator.measurements().back().rtp_timestamp,
70 &timestamp_ms));
71 // Since this RTP packet has the same timestamp as the RTCP packet constructed
72 // at time 0 it should be mapped to 0 as well.
73 EXPECT_EQ(0, timestamp_ms);
74 }
75
76 TEST(WrapAroundTests, RtpWrapped) {
77 RtpToNtpEstimator estimator;
78 bool new_sr;
79 uint32_t ntp_sec = 0;
80 uint32_t ntp_frac = 1;
81 uint32_t timestamp = 0xFFFFFFFF - 2 * kTimestampTicksPerMs;
82 EXPECT_TRUE(
83 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
84 ntp_frac += kOneMsInNtpFrac;
85 timestamp += kTimestampTicksPerMs;
86 EXPECT_TRUE(
87 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
88
89 int64_t timestamp_ms = -1;
90 EXPECT_TRUE(estimator.Estimate(estimator.measurements().back().rtp_timestamp,
91 &timestamp_ms));
92 // Since this RTP packet has the same timestamp as the RTCP packet constructed
93 // at time 0 it should be mapped to 0 as well.
94 EXPECT_EQ(0, timestamp_ms);
95 // Two kTimestampTicksPerMs advanced.
96 timestamp += kTimestampTicksPerMs;
97 EXPECT_TRUE(estimator.Estimate(timestamp, &timestamp_ms));
98 EXPECT_EQ(2, timestamp_ms);
99 // Wrapped rtp.
100 timestamp += kTimestampTicksPerMs;
101 EXPECT_TRUE(estimator.Estimate(timestamp, &timestamp_ms));
102 EXPECT_EQ(3, timestamp_ms);
103 }
104
105 TEST(WrapAroundTests, OldRtp_RtcpsWrapped) {
106 RtpToNtpEstimator estimator;
107 bool new_sr;
108 uint32_t ntp_sec = 0;
109 uint32_t ntp_frac = 1;
110 uint32_t timestamp = 0;
111 EXPECT_TRUE(
112 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
113 ntp_frac += kOneMsInNtpFrac;
114 timestamp += kTimestampTicksPerMs;
115 EXPECT_TRUE(
116 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
117 timestamp -= 2 * kTimestampTicksPerMs;
118 int64_t timestamp_ms = -1;
119 EXPECT_FALSE(estimator.Estimate(timestamp, &timestamp_ms));
120 }
121
122 TEST(WrapAroundTests, OldRtp_NewRtcpWrapped) {
123 RtpToNtpEstimator estimator;
124 bool new_sr;
125 uint32_t ntp_sec = 0;
126 uint32_t ntp_frac = 1;
127 uint32_t timestamp = 0xFFFFFFFF;
128 EXPECT_TRUE(
129 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
130 ntp_frac += kOneMsInNtpFrac;
131 timestamp += kTimestampTicksPerMs;
132 EXPECT_TRUE(
133 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
134 timestamp -= kTimestampTicksPerMs;
135 int64_t timestamp_ms = -1;
136 EXPECT_TRUE(estimator.Estimate(timestamp, &timestamp_ms));
137 // Constructed at the same time as the first RTCP and should therefore be
138 // mapped to zero.
139 EXPECT_EQ(0, timestamp_ms);
140 }
141
142 TEST(UpdateRtcpMeasurementTests, InjectRtcpSr) {
143 const uint32_t kNtpSec = 10;
144 const uint32_t kNtpFrac = 12345;
145 const uint32_t kTs = 0x12345678;
146 bool new_sr;
147 RtpToNtpEstimator estimator;
148 EXPECT_TRUE(estimator.UpdateMeasurements(kNtpSec, kNtpFrac, kTs, &new_sr));
149 EXPECT_TRUE(new_sr);
150 EXPECT_EQ(1u, estimator.measurements().size());
151 EXPECT_EQ(kNtpSec, estimator.measurements().front().ntp_time.seconds());
152 EXPECT_EQ(kNtpFrac, estimator.measurements().front().ntp_time.fractions());
153 EXPECT_EQ(kTs, estimator.measurements().front().rtp_timestamp);
154 // Add second report.
155 EXPECT_TRUE(estimator.UpdateMeasurements(kNtpSec, kNtpFrac + kOneMsInNtpFrac,
156 kTs + 1, &new_sr));
157 EXPECT_TRUE(new_sr);
158 EXPECT_EQ(2u, estimator.measurements().size());
159 EXPECT_EQ(kTs + 1, estimator.measurements().front().rtp_timestamp);
160 EXPECT_EQ(kTs + 0, estimator.measurements().back().rtp_timestamp);
161 // List should contain last two reports.
162 EXPECT_TRUE(estimator.UpdateMeasurements(
163 kNtpSec, kNtpFrac + 2 * kOneMsInNtpFrac, kTs + 2, &new_sr));
164 EXPECT_EQ(2u, estimator.measurements().size());
165 EXPECT_EQ(kTs + 2, estimator.measurements().front().rtp_timestamp);
166 EXPECT_EQ(kTs + 1, estimator.measurements().back().rtp_timestamp);
167 }
168
169 TEST(UpdateRtcpMeasurementTests, FailsForZeroNtp) {
170 RtpToNtpEstimator estimator;
171 uint32_t ntp_sec = 0;
172 uint32_t ntp_frac = 0;
173 uint32_t timestamp = 0x12345678;
174 bool new_sr;
175 EXPECT_FALSE(
176 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
177 EXPECT_FALSE(new_sr);
178 EXPECT_EQ(0u, estimator.measurements().size());
179 }
180
181 TEST(UpdateRtcpMeasurementTests, FailsForEqualNtp) {
182 RtpToNtpEstimator estimator;
183 uint32_t ntp_sec = 0;
184 uint32_t ntp_frac = 699925050;
185 uint32_t timestamp = 0x12345678;
186 bool new_sr;
187 EXPECT_TRUE(
188 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
189 EXPECT_TRUE(new_sr);
190 EXPECT_EQ(1u, estimator.measurements().size());
191 // Ntp time already added, list not updated.
192 ++timestamp;
193 EXPECT_TRUE(
194 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
195 EXPECT_FALSE(new_sr);
196 EXPECT_EQ(1u, estimator.measurements().size());
197 }
198
199 TEST(UpdateRtcpMeasurementTests, FailsForOldNtp) {
200 RtpToNtpEstimator estimator;
201 uint32_t ntp_sec = 1;
202 uint32_t ntp_frac = 699925050;
203 uint32_t timestamp = 0x12345678;
204 bool new_sr;
205 EXPECT_TRUE(
206 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
207 EXPECT_TRUE(new_sr);
208 EXPECT_EQ(1u, estimator.measurements().size());
209 // Old ntp time, list not updated.
210 ntp_frac -= kOneMsInNtpFrac;
211 timestamp += kTimestampTicksPerMs;
212 EXPECT_FALSE(
213 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
214 EXPECT_EQ(1u, estimator.measurements().size());
215 }
216
217 TEST(UpdateRtcpMeasurementTests, FailsForEqualTimestamp) {
218 RtpToNtpEstimator estimator;
219 uint32_t ntp_sec = 0;
220 uint32_t ntp_frac = 2;
221 uint32_t timestamp = 0x12345678;
222 bool new_sr;
223 EXPECT_TRUE(
224 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
225 EXPECT_TRUE(new_sr);
226 EXPECT_EQ(1u, estimator.measurements().size());
227 // Timestamp already added, list not updated.
228 ++ntp_frac;
229 EXPECT_TRUE(
230 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
231 EXPECT_FALSE(new_sr);
232 EXPECT_EQ(1u, estimator.measurements().size());
233 }
234
235 TEST(UpdateRtcpMeasurementTests, FailsForOldRtpTimestamp) {
236 RtpToNtpEstimator estimator;
237 uint32_t ntp_sec = 0;
238 uint32_t ntp_frac = 2;
239 uint32_t timestamp = 0x12345678;
240 bool new_sr;
241 EXPECT_TRUE(
242 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
243 EXPECT_TRUE(new_sr);
244 EXPECT_EQ(1u, estimator.measurements().size());
245 // Old timestamp, list not updated.
246 ntp_frac += kOneMsInNtpFrac;
247 timestamp -= kTimestampTicksPerMs;
248 EXPECT_FALSE(
249 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
250 EXPECT_FALSE(new_sr);
251 EXPECT_EQ(1u, estimator.measurements().size());
252 }
253
254 TEST(UpdateRtcpMeasurementTests, VerifyParameters) {
255 RtpToNtpEstimator estimator;
256 uint32_t ntp_sec = 1;
257 uint32_t ntp_frac = 2;
258 uint32_t timestamp = 0x12345678;
259 bool new_sr;
260 EXPECT_TRUE(
261 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
262 EXPECT_TRUE(new_sr);
263 EXPECT_FALSE(estimator.params().calculated);
264 // Add second report, parameters should be calculated.
265 ntp_frac += kOneMsInNtpFrac;
266 timestamp += kTimestampTicksPerMs;
267 EXPECT_TRUE(
268 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
269 EXPECT_TRUE(estimator.params().calculated);
270 EXPECT_DOUBLE_EQ(90.0, estimator.params().frequency_khz);
271 EXPECT_NE(0.0, estimator.params().offset_ms);
272 }
273
274 TEST(RtpToNtpTests, FailsForNoParameters) {
275 RtpToNtpEstimator estimator;
276 uint32_t ntp_sec = 1;
277 uint32_t ntp_frac = 2;
278 uint32_t timestamp = 0x12345678;
279 bool new_sr;
280 EXPECT_TRUE(
281 estimator.UpdateMeasurements(ntp_sec, ntp_frac, timestamp, &new_sr));
282 EXPECT_EQ(1u, estimator.measurements().size());
283 // Parameters are not calculated, conversion of RTP to NTP time should fail.
284 EXPECT_FALSE(estimator.params().calculated);
285 int64_t timestamp_ms = -1;
286 EXPECT_FALSE(estimator.Estimate(timestamp, &timestamp_ms));
287 }
288
289 }; // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698