1 /*
2 * Copyright (c) 2019 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 "modules/rtp_rtcp/source/absolute_capture_time_receiver.h"
12
13 #include "system_wrappers/include/ntp_time.h"
14 #include "test/gmock.h"
15 #include "test/gtest.h"
16
17 namespace webrtc {
18
TEST(AbsoluteCaptureTimeReceiverTest,GetSourceWithoutCsrcs)19 TEST(AbsoluteCaptureTimeReceiverTest, GetSourceWithoutCsrcs) {
20 constexpr uint32_t kSsrc = 12;
21
22 EXPECT_EQ(AbsoluteCaptureTimeReceiver::GetSource(kSsrc, nullptr), kSsrc);
23 }
24
TEST(AbsoluteCaptureTimeReceiverTest,GetSourceWithCsrcs)25 TEST(AbsoluteCaptureTimeReceiverTest, GetSourceWithCsrcs) {
26 constexpr uint32_t kSsrc = 12;
27 constexpr uint32_t kCsrcs[] = {34, 56, 78, 90};
28
29 EXPECT_EQ(AbsoluteCaptureTimeReceiver::GetSource(kSsrc, kCsrcs), kCsrcs[0]);
30 }
31
TEST(AbsoluteCaptureTimeReceiverTest,ReceiveExtensionReturnsExtension)32 TEST(AbsoluteCaptureTimeReceiverTest, ReceiveExtensionReturnsExtension) {
33 constexpr uint32_t kSource = 1337;
34 constexpr uint32_t kRtpClockFrequency = 64000;
35 constexpr uint32_t kRtpTimestamp0 = 1020300000;
36 constexpr uint32_t kRtpTimestamp1 = kRtpTimestamp0 + 1280;
37 static const absl::optional<AbsoluteCaptureTime> kExtension0 =
38 AbsoluteCaptureTime{Int64MsToUQ32x32(9000), Int64MsToQ32x32(-350)};
39 static const absl::optional<AbsoluteCaptureTime> kExtension1 =
40 AbsoluteCaptureTime{Int64MsToUQ32x32(9020), absl::nullopt};
41
42 SimulatedClock clock(0);
43 AbsoluteCaptureTimeReceiver receiver(&clock);
44
45 receiver.SetRemoteToLocalClockOffset(0);
46
47 EXPECT_EQ(receiver.OnReceivePacket(kSource, kRtpTimestamp0,
48 kRtpClockFrequency, kExtension0),
49 kExtension0);
50
51 EXPECT_EQ(receiver.OnReceivePacket(kSource, kRtpTimestamp1,
52 kRtpClockFrequency, kExtension1),
53 kExtension1);
54 }
55
TEST(AbsoluteCaptureTimeReceiverTest,ReceiveNoExtensionReturnsNoExtension)56 TEST(AbsoluteCaptureTimeReceiverTest, ReceiveNoExtensionReturnsNoExtension) {
57 constexpr uint32_t kSource = 1337;
58 constexpr uint32_t kRtpClockFrequency = 64000;
59 constexpr uint32_t kRtpTimestamp0 = 1020300000;
60 constexpr uint32_t kRtpTimestamp1 = kRtpTimestamp0 + 1280;
61 static const absl::optional<AbsoluteCaptureTime> kExtension0 = absl::nullopt;
62 static const absl::optional<AbsoluteCaptureTime> kExtension1 = absl::nullopt;
63
64 SimulatedClock clock(0);
65 AbsoluteCaptureTimeReceiver receiver(&clock);
66
67 receiver.SetRemoteToLocalClockOffset(0);
68
69 EXPECT_EQ(receiver.OnReceivePacket(kSource, kRtpTimestamp0,
70 kRtpClockFrequency, kExtension0),
71 absl::nullopt);
72
73 EXPECT_EQ(receiver.OnReceivePacket(kSource, kRtpTimestamp1,
74 kRtpClockFrequency, kExtension1),
75 absl::nullopt);
76 }
77
TEST(AbsoluteCaptureTimeReceiverTest,InterpolateLaterPacketArrivingLater)78 TEST(AbsoluteCaptureTimeReceiverTest, InterpolateLaterPacketArrivingLater) {
79 constexpr uint32_t kSource = 1337;
80 constexpr uint32_t kRtpClockFrequency = 64000;
81 constexpr uint32_t kRtpTimestamp0 = 1020300000;
82 constexpr uint32_t kRtpTimestamp1 = kRtpTimestamp0 + 1280;
83 constexpr uint32_t kRtpTimestamp2 = kRtpTimestamp0 + 2560;
84 static const absl::optional<AbsoluteCaptureTime> kExtension0 =
85 AbsoluteCaptureTime{Int64MsToUQ32x32(9000), Int64MsToQ32x32(-350)};
86 static const absl::optional<AbsoluteCaptureTime> kExtension1 = absl::nullopt;
87 static const absl::optional<AbsoluteCaptureTime> kExtension2 = absl::nullopt;
88
89 SimulatedClock clock(0);
90 AbsoluteCaptureTimeReceiver receiver(&clock);
91
92 receiver.SetRemoteToLocalClockOffset(0);
93
94 EXPECT_EQ(receiver.OnReceivePacket(kSource, kRtpTimestamp0,
95 kRtpClockFrequency, kExtension0),
96 kExtension0);
97
98 absl::optional<AbsoluteCaptureTime> extension = receiver.OnReceivePacket(
99 kSource, kRtpTimestamp1, kRtpClockFrequency, kExtension1);
100 EXPECT_TRUE(extension.has_value());
101 EXPECT_EQ(UQ32x32ToInt64Ms(extension->absolute_capture_timestamp),
102 UQ32x32ToInt64Ms(kExtension0->absolute_capture_timestamp) + 20);
103 EXPECT_EQ(extension->estimated_capture_clock_offset,
104 kExtension0->estimated_capture_clock_offset);
105
106 extension = receiver.OnReceivePacket(kSource, kRtpTimestamp2,
107 kRtpClockFrequency, kExtension2);
108 EXPECT_TRUE(extension.has_value());
109 EXPECT_EQ(UQ32x32ToInt64Ms(extension->absolute_capture_timestamp),
110 UQ32x32ToInt64Ms(kExtension0->absolute_capture_timestamp) + 40);
111 EXPECT_EQ(extension->estimated_capture_clock_offset,
112 kExtension0->estimated_capture_clock_offset);
113 }
114
TEST(AbsoluteCaptureTimeReceiverTest,InterpolateEarlierPacketArrivingLater)115 TEST(AbsoluteCaptureTimeReceiverTest, InterpolateEarlierPacketArrivingLater) {
116 constexpr uint32_t kSource = 1337;
117 constexpr uint32_t kRtpClockFrequency = 64000;
118 constexpr uint32_t kRtpTimestamp0 = 1020300000;
119 constexpr uint32_t kRtpTimestamp1 = kRtpTimestamp0 - 1280;
120 constexpr uint32_t kRtpTimestamp2 = kRtpTimestamp0 - 2560;
121 static const absl::optional<AbsoluteCaptureTime> kExtension0 =
122 AbsoluteCaptureTime{Int64MsToUQ32x32(9000), Int64MsToQ32x32(-350)};
123 static const absl::optional<AbsoluteCaptureTime> kExtension1 = absl::nullopt;
124 static const absl::optional<AbsoluteCaptureTime> kExtension2 = absl::nullopt;
125
126 SimulatedClock clock(0);
127 AbsoluteCaptureTimeReceiver receiver(&clock);
128
129 receiver.SetRemoteToLocalClockOffset(0);
130
131 EXPECT_EQ(receiver.OnReceivePacket(kSource, kRtpTimestamp0,
132 kRtpClockFrequency, kExtension0),
133 kExtension0);
134
135 absl::optional<AbsoluteCaptureTime> extension = receiver.OnReceivePacket(
136 kSource, kRtpTimestamp1, kRtpClockFrequency, kExtension1);
137 EXPECT_TRUE(extension.has_value());
138 EXPECT_EQ(UQ32x32ToInt64Ms(extension->absolute_capture_timestamp),
139 UQ32x32ToInt64Ms(kExtension0->absolute_capture_timestamp) - 20);
140 EXPECT_EQ(extension->estimated_capture_clock_offset,
141 kExtension0->estimated_capture_clock_offset);
142
143 extension = receiver.OnReceivePacket(kSource, kRtpTimestamp2,
144 kRtpClockFrequency, kExtension2);
145 EXPECT_TRUE(extension.has_value());
146 EXPECT_EQ(UQ32x32ToInt64Ms(extension->absolute_capture_timestamp),
147 UQ32x32ToInt64Ms(kExtension0->absolute_capture_timestamp) - 40);
148 EXPECT_EQ(extension->estimated_capture_clock_offset,
149 kExtension0->estimated_capture_clock_offset);
150 }
151
TEST(AbsoluteCaptureTimeReceiverTest,InterpolateLaterPacketArrivingLaterWithRtpTimestampWrapAround)152 TEST(AbsoluteCaptureTimeReceiverTest,
153 InterpolateLaterPacketArrivingLaterWithRtpTimestampWrapAround) {
154 constexpr uint32_t kSource = 1337;
155 constexpr uint32_t kRtpClockFrequency = 64000;
156 constexpr uint32_t kRtpTimestamp0 = ~uint32_t{0} - 79;
157 constexpr uint32_t kRtpTimestamp1 = 1280 - 80;
158 constexpr uint32_t kRtpTimestamp2 = 2560 - 80;
159 static const absl::optional<AbsoluteCaptureTime> kExtension0 =
160 AbsoluteCaptureTime{Int64MsToUQ32x32(9000), Int64MsToQ32x32(-350)};
161 static const absl::optional<AbsoluteCaptureTime> kExtension1 = absl::nullopt;
162 static const absl::optional<AbsoluteCaptureTime> kExtension2 = absl::nullopt;
163
164 SimulatedClock clock(0);
165 AbsoluteCaptureTimeReceiver receiver(&clock);
166
167 receiver.SetRemoteToLocalClockOffset(0);
168
169 EXPECT_EQ(receiver.OnReceivePacket(kSource, kRtpTimestamp0,
170 kRtpClockFrequency, kExtension0),
171 kExtension0);
172
173 absl::optional<AbsoluteCaptureTime> extension = receiver.OnReceivePacket(
174 kSource, kRtpTimestamp1, kRtpClockFrequency, kExtension1);
175 EXPECT_TRUE(extension.has_value());
176 EXPECT_EQ(UQ32x32ToInt64Ms(extension->absolute_capture_timestamp),
177 UQ32x32ToInt64Ms(kExtension0->absolute_capture_timestamp) + 20);
178 EXPECT_EQ(extension->estimated_capture_clock_offset,
179 kExtension0->estimated_capture_clock_offset);
180
181 extension = receiver.OnReceivePacket(kSource, kRtpTimestamp2,
182 kRtpClockFrequency, kExtension2);
183 EXPECT_TRUE(extension.has_value());
184 EXPECT_EQ(UQ32x32ToInt64Ms(extension->absolute_capture_timestamp),
185 UQ32x32ToInt64Ms(kExtension0->absolute_capture_timestamp) + 40);
186 EXPECT_EQ(extension->estimated_capture_clock_offset,
187 kExtension0->estimated_capture_clock_offset);
188 }
189
TEST(AbsoluteCaptureTimeReceiverTest,InterpolateEarlierPacketArrivingLaterWithRtpTimestampWrapAround)190 TEST(AbsoluteCaptureTimeReceiverTest,
191 InterpolateEarlierPacketArrivingLaterWithRtpTimestampWrapAround) {
192 constexpr uint32_t kSource = 1337;
193 constexpr uint32_t kRtpClockFrequency = 64000;
194 constexpr uint32_t kRtpTimestamp0 = 799;
195 constexpr uint32_t kRtpTimestamp1 = kRtpTimestamp0 - 1280;
196 constexpr uint32_t kRtpTimestamp2 = kRtpTimestamp0 - 2560;
197 static const absl::optional<AbsoluteCaptureTime> kExtension0 =
198 AbsoluteCaptureTime{Int64MsToUQ32x32(9000), Int64MsToQ32x32(-350)};
199 static const absl::optional<AbsoluteCaptureTime> kExtension1 = absl::nullopt;
200 static const absl::optional<AbsoluteCaptureTime> kExtension2 = absl::nullopt;
201
202 SimulatedClock clock(0);
203 AbsoluteCaptureTimeReceiver receiver(&clock);
204
205 receiver.SetRemoteToLocalClockOffset(0);
206
207 EXPECT_EQ(receiver.OnReceivePacket(kSource, kRtpTimestamp0,
208 kRtpClockFrequency, kExtension0),
209 kExtension0);
210
211 absl::optional<AbsoluteCaptureTime> extension = receiver.OnReceivePacket(
212 kSource, kRtpTimestamp1, kRtpClockFrequency, kExtension1);
213 EXPECT_TRUE(extension.has_value());
214 EXPECT_EQ(UQ32x32ToInt64Ms(extension->absolute_capture_timestamp),
215 UQ32x32ToInt64Ms(kExtension0->absolute_capture_timestamp) - 20);
216 EXPECT_EQ(extension->estimated_capture_clock_offset,
217 kExtension0->estimated_capture_clock_offset);
218
219 extension = receiver.OnReceivePacket(kSource, kRtpTimestamp2,
220 kRtpClockFrequency, kExtension2);
221 EXPECT_TRUE(extension.has_value());
222 EXPECT_EQ(UQ32x32ToInt64Ms(extension->absolute_capture_timestamp),
223 UQ32x32ToInt64Ms(kExtension0->absolute_capture_timestamp) - 40);
224 EXPECT_EQ(extension->estimated_capture_clock_offset,
225 kExtension0->estimated_capture_clock_offset);
226 }
227
TEST(AbsoluteCaptureTimeReceiverTest,SkipEstimatedCaptureClockOffsetIfRemoteToLocalClockOffsetIsUnknown)228 TEST(AbsoluteCaptureTimeReceiverTest,
229 SkipEstimatedCaptureClockOffsetIfRemoteToLocalClockOffsetIsUnknown) {
230 constexpr uint32_t kSource = 1337;
231 constexpr uint32_t kRtpClockFrequency = 64000;
232 constexpr uint32_t kRtpTimestamp0 = 1020300000;
233 constexpr uint32_t kRtpTimestamp1 = kRtpTimestamp0 + 1280;
234 constexpr uint32_t kRtpTimestamp2 = kRtpTimestamp0 + 2560;
235 static const absl::optional<AbsoluteCaptureTime> kExtension0 =
236 AbsoluteCaptureTime{Int64MsToUQ32x32(9000), Int64MsToQ32x32(-350)};
237 static const absl::optional<AbsoluteCaptureTime> kExtension1 = absl::nullopt;
238 static const absl::optional<AbsoluteCaptureTime> kExtension2 = absl::nullopt;
239 static const absl::optional<int64_t> kRemoteToLocalClockOffset2 =
240 Int64MsToQ32x32(-7000007);
241
242 SimulatedClock clock(0);
243 AbsoluteCaptureTimeReceiver receiver(&clock);
244
245 receiver.SetRemoteToLocalClockOffset(0);
246
247 EXPECT_EQ(receiver.OnReceivePacket(kSource, kRtpTimestamp0,
248 kRtpClockFrequency, kExtension0),
249 kExtension0);
250
251 receiver.SetRemoteToLocalClockOffset(absl::nullopt);
252
253 absl::optional<AbsoluteCaptureTime> extension = receiver.OnReceivePacket(
254 kSource, kRtpTimestamp1, kRtpClockFrequency, kExtension1);
255 EXPECT_TRUE(extension.has_value());
256 EXPECT_EQ(UQ32x32ToInt64Ms(extension->absolute_capture_timestamp),
257 UQ32x32ToInt64Ms(kExtension0->absolute_capture_timestamp) + 20);
258 EXPECT_EQ(extension->estimated_capture_clock_offset, absl::nullopt);
259
260 receiver.SetRemoteToLocalClockOffset(kRemoteToLocalClockOffset2);
261
262 extension = receiver.OnReceivePacket(kSource, kRtpTimestamp2,
263 kRtpClockFrequency, kExtension2);
264 EXPECT_TRUE(extension.has_value());
265 EXPECT_EQ(UQ32x32ToInt64Ms(extension->absolute_capture_timestamp),
266 UQ32x32ToInt64Ms(kExtension0->absolute_capture_timestamp) + 40);
267 EXPECT_EQ(extension->estimated_capture_clock_offset,
268 *kExtension0->estimated_capture_clock_offset +
269 *kRemoteToLocalClockOffset2);
270 }
271
TEST(AbsoluteCaptureTimeReceiverTest,SkipInterpolateIfTooLate)272 TEST(AbsoluteCaptureTimeReceiverTest, SkipInterpolateIfTooLate) {
273 constexpr uint32_t kSource = 1337;
274 constexpr uint32_t kRtpClockFrequency = 64000;
275 constexpr uint32_t kRtpTimestamp0 = 1020300000;
276 constexpr uint32_t kRtpTimestamp1 = kRtpTimestamp0 + 1280;
277 constexpr uint32_t kRtpTimestamp2 = kRtpTimestamp1 + 1280;
278 static const absl::optional<AbsoluteCaptureTime> kExtension0 =
279 AbsoluteCaptureTime{Int64MsToUQ32x32(9000), Int64MsToQ32x32(-350)};
280 static const absl::optional<AbsoluteCaptureTime> kExtension1 = absl::nullopt;
281 static const absl::optional<AbsoluteCaptureTime> kExtension2 = absl::nullopt;
282
283 SimulatedClock clock(0);
284 AbsoluteCaptureTimeReceiver receiver(&clock);
285
286 receiver.SetRemoteToLocalClockOffset(0);
287
288 EXPECT_EQ(receiver.OnReceivePacket(kSource, kRtpTimestamp0,
289 kRtpClockFrequency, kExtension0),
290 kExtension0);
291
292 clock.AdvanceTime(AbsoluteCaptureTimeReceiver::kInterpolationMaxInterval);
293
294 EXPECT_TRUE(receiver
295 .OnReceivePacket(kSource, kRtpTimestamp1, kRtpClockFrequency,
296 kExtension1)
297 .has_value());
298
299 clock.AdvanceTimeMilliseconds(1);
300
301 EXPECT_FALSE(receiver
302 .OnReceivePacket(kSource, kRtpTimestamp2, kRtpClockFrequency,
303 kExtension2)
304 .has_value());
305 }
306
TEST(AbsoluteCaptureTimeReceiverTest,SkipInterpolateIfSourceChanged)307 TEST(AbsoluteCaptureTimeReceiverTest, SkipInterpolateIfSourceChanged) {
308 constexpr uint32_t kSource0 = 1337;
309 constexpr uint32_t kSource1 = 1338;
310 constexpr uint32_t kRtpClockFrequency = 64000;
311 constexpr uint32_t kRtpTimestamp0 = 1020300000;
312 constexpr uint32_t kRtpTimestamp1 = kRtpTimestamp0 + 1280;
313 static const absl::optional<AbsoluteCaptureTime> kExtension0 =
314 AbsoluteCaptureTime{Int64MsToUQ32x32(9000), Int64MsToQ32x32(-350)};
315 static const absl::optional<AbsoluteCaptureTime> kExtension1 = absl::nullopt;
316
317 SimulatedClock clock(0);
318 AbsoluteCaptureTimeReceiver receiver(&clock);
319
320 receiver.SetRemoteToLocalClockOffset(0);
321
322 EXPECT_EQ(receiver.OnReceivePacket(kSource0, kRtpTimestamp0,
323 kRtpClockFrequency, kExtension0),
324 kExtension0);
325
326 EXPECT_FALSE(receiver
327 .OnReceivePacket(kSource1, kRtpTimestamp1,
328 kRtpClockFrequency, kExtension1)
329 .has_value());
330 }
331
TEST(AbsoluteCaptureTimeReceiverTest,SkipInterpolateIfRtpClockFrequencyChanged)332 TEST(AbsoluteCaptureTimeReceiverTest,
333 SkipInterpolateIfRtpClockFrequencyChanged) {
334 constexpr uint32_t kSource = 1337;
335 constexpr uint32_t kRtpClockFrequency0 = 64000;
336 constexpr uint32_t kRtpClockFrequency1 = 32000;
337 constexpr uint32_t kRtpTimestamp0 = 1020300000;
338 constexpr uint32_t kRtpTimestamp1 = kRtpTimestamp0 + 640;
339 static const absl::optional<AbsoluteCaptureTime> kExtension0 =
340 AbsoluteCaptureTime{Int64MsToUQ32x32(9000), Int64MsToQ32x32(-350)};
341 static const absl::optional<AbsoluteCaptureTime> kExtension1 = absl::nullopt;
342
343 SimulatedClock clock(0);
344 AbsoluteCaptureTimeReceiver receiver(&clock);
345
346 receiver.SetRemoteToLocalClockOffset(0);
347
348 EXPECT_EQ(receiver.OnReceivePacket(kSource, kRtpTimestamp0,
349 kRtpClockFrequency0, kExtension0),
350 kExtension0);
351
352 EXPECT_FALSE(receiver
353 .OnReceivePacket(kSource, kRtpTimestamp1,
354 kRtpClockFrequency1, kExtension1)
355 .has_value());
356 }
357
TEST(AbsoluteCaptureTimeReceiverTest,SkipInterpolateIfRtpClockFrequencyIsInvalid)358 TEST(AbsoluteCaptureTimeReceiverTest,
359 SkipInterpolateIfRtpClockFrequencyIsInvalid) {
360 constexpr uint32_t kSource = 1337;
361 constexpr uint32_t kRtpClockFrequency = 0;
362 constexpr uint32_t kRtpTimestamp0 = 1020300000;
363 constexpr uint32_t kRtpTimestamp1 = kRtpTimestamp0 + 640;
364 static const absl::optional<AbsoluteCaptureTime> kExtension0 =
365 AbsoluteCaptureTime{Int64MsToUQ32x32(9000), Int64MsToQ32x32(-350)};
366 static const absl::optional<AbsoluteCaptureTime> kExtension1 = absl::nullopt;
367
368 SimulatedClock clock(0);
369 AbsoluteCaptureTimeReceiver receiver(&clock);
370
371 receiver.SetRemoteToLocalClockOffset(0);
372
373 EXPECT_EQ(receiver.OnReceivePacket(kSource, kRtpTimestamp0,
374 kRtpClockFrequency, kExtension0),
375 kExtension0);
376
377 EXPECT_FALSE(receiver
378 .OnReceivePacket(kSource, kRtpTimestamp1, kRtpClockFrequency,
379 kExtension1)
380 .has_value());
381 }
382
TEST(AbsoluteCaptureTimeReceiverTest,SkipInterpolateIsSticky)383 TEST(AbsoluteCaptureTimeReceiverTest, SkipInterpolateIsSticky) {
384 constexpr uint32_t kSource0 = 1337;
385 constexpr uint32_t kSource1 = 1338;
386 constexpr uint32_t kSource2 = 1337;
387 constexpr uint32_t kRtpClockFrequency = 64000;
388 constexpr uint32_t kRtpTimestamp0 = 1020300000;
389 constexpr uint32_t kRtpTimestamp1 = kRtpTimestamp0 + 1280;
390 constexpr uint32_t kRtpTimestamp2 = kRtpTimestamp1 + 1280;
391 static const absl::optional<AbsoluteCaptureTime> kExtension0 =
392 AbsoluteCaptureTime{Int64MsToUQ32x32(9000), Int64MsToQ32x32(-350)};
393 static const absl::optional<AbsoluteCaptureTime> kExtension1 = absl::nullopt;
394 static const absl::optional<AbsoluteCaptureTime> kExtension2 = absl::nullopt;
395
396 SimulatedClock clock(0);
397 AbsoluteCaptureTimeReceiver receiver(&clock);
398
399 receiver.SetRemoteToLocalClockOffset(0);
400
401 EXPECT_EQ(receiver.OnReceivePacket(kSource0, kRtpTimestamp0,
402 kRtpClockFrequency, kExtension0),
403 kExtension0);
404
405 EXPECT_FALSE(receiver
406 .OnReceivePacket(kSource1, kRtpTimestamp1,
407 kRtpClockFrequency, kExtension1)
408 .has_value());
409
410 EXPECT_FALSE(receiver
411 .OnReceivePacket(kSource2, kRtpTimestamp2,
412 kRtpClockFrequency, kExtension2)
413 .has_value());
414 }
415
416 } // namespace webrtc
417