xref: /aosp_15_r20/external/webrtc/modules/rtp_rtcp/source/remote_ntp_time_estimator_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2014 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/include/remote_ntp_time_estimator.h"
12 
13 #include "absl/types/optional.h"
14 #include "modules/rtp_rtcp/source/time_util.h"
15 #include "system_wrappers/include/clock.h"
16 #include "system_wrappers/include/ntp_time.h"
17 #include "test/gmock.h"
18 #include "test/gtest.h"
19 
20 namespace webrtc {
21 namespace {
22 
23 constexpr TimeDelta kTestRtt = TimeDelta::Millis(10);
24 constexpr Timestamp kLocalClockInitialTime = Timestamp::Millis(123);
25 constexpr Timestamp kRemoteClockInitialTime = Timestamp::Millis(373);
26 constexpr uint32_t kTimestampOffset = 567;
27 constexpr int64_t kRemoteToLocalClockOffsetNtp =
28     ToNtpUnits(kLocalClockInitialTime - kRemoteClockInitialTime);
29 
30 class RemoteNtpTimeEstimatorTest : public ::testing::Test {
31  protected:
AdvanceTime(TimeDelta delta)32   void AdvanceTime(TimeDelta delta) {
33     local_clock_.AdvanceTime(delta);
34     remote_clock_.AdvanceTime(delta);
35   }
36 
GetRemoteTimestamp()37   uint32_t GetRemoteTimestamp() {
38     return static_cast<uint32_t>(remote_clock_.TimeInMilliseconds()) * 90 +
39            kTimestampOffset;
40   }
41 
SendRtcpSr()42   void SendRtcpSr() {
43     uint32_t rtcp_timestamp = GetRemoteTimestamp();
44     NtpTime ntp = remote_clock_.CurrentNtpTime();
45 
46     AdvanceTime(kTestRtt / 2);
47     RTC_DCHECK(estimator_.UpdateRtcpTimestamp(kTestRtt, ntp, rtcp_timestamp));
48   }
49 
SendRtcpSrInaccurately(TimeDelta ntp_error,TimeDelta networking_delay)50   void SendRtcpSrInaccurately(TimeDelta ntp_error, TimeDelta networking_delay) {
51     uint32_t rtcp_timestamp = GetRemoteTimestamp();
52     int64_t ntp_error_fractions = ToNtpUnits(ntp_error);
53     NtpTime ntp(static_cast<uint64_t>(remote_clock_.CurrentNtpTime()) +
54                 ntp_error_fractions);
55     AdvanceTime(kTestRtt / 2 + networking_delay);
56     RTC_DCHECK(estimator_.UpdateRtcpTimestamp(kTestRtt, ntp, rtcp_timestamp));
57   }
58 
59   SimulatedClock local_clock_{kLocalClockInitialTime};
60   SimulatedClock remote_clock_{kRemoteClockInitialTime};
61   RemoteNtpTimeEstimator estimator_{&local_clock_};
62 };
63 
TEST_F(RemoteNtpTimeEstimatorTest,FailsWithoutValidNtpTime)64 TEST_F(RemoteNtpTimeEstimatorTest, FailsWithoutValidNtpTime) {
65   EXPECT_FALSE(
66       estimator_.UpdateRtcpTimestamp(kTestRtt, NtpTime(), /*rtp_timestamp=*/0));
67 }
68 
TEST_F(RemoteNtpTimeEstimatorTest,Estimate)69 TEST_F(RemoteNtpTimeEstimatorTest, Estimate) {
70   // Remote peer sends first RTCP SR.
71   SendRtcpSr();
72 
73   // Remote sends a RTP packet.
74   AdvanceTime(TimeDelta::Millis(15));
75   uint32_t rtp_timestamp = GetRemoteTimestamp();
76   int64_t capture_ntp_time_ms = local_clock_.CurrentNtpInMilliseconds();
77 
78   // Local peer needs at least 2 RTCP SR to calculate the capture time.
79   const int64_t kNotEnoughRtcpSr = -1;
80   EXPECT_EQ(kNotEnoughRtcpSr, estimator_.Estimate(rtp_timestamp));
81   EXPECT_EQ(estimator_.EstimateRemoteToLocalClockOffset(), absl::nullopt);
82 
83   AdvanceTime(TimeDelta::Millis(800));
84   // Remote sends second RTCP SR.
85   SendRtcpSr();
86 
87   // Local peer gets enough RTCP SR to calculate the capture time.
88   EXPECT_EQ(capture_ntp_time_ms, estimator_.Estimate(rtp_timestamp));
89   EXPECT_EQ(estimator_.EstimateRemoteToLocalClockOffset(),
90             kRemoteToLocalClockOffsetNtp);
91 }
92 
TEST_F(RemoteNtpTimeEstimatorTest,AveragesErrorsOut)93 TEST_F(RemoteNtpTimeEstimatorTest, AveragesErrorsOut) {
94   // Remote peer sends first 10 RTCP SR without errors.
95   for (int i = 0; i < 10; ++i) {
96     AdvanceTime(TimeDelta::Seconds(1));
97     SendRtcpSr();
98   }
99 
100   AdvanceTime(TimeDelta::Millis(150));
101   uint32_t rtp_timestamp = GetRemoteTimestamp();
102   int64_t capture_ntp_time_ms = local_clock_.CurrentNtpInMilliseconds();
103   // Local peer gets enough RTCP SR to calculate the capture time.
104   EXPECT_EQ(capture_ntp_time_ms, estimator_.Estimate(rtp_timestamp));
105   EXPECT_EQ(kRemoteToLocalClockOffsetNtp,
106             estimator_.EstimateRemoteToLocalClockOffset());
107 
108   // Remote sends corrupted RTCP SRs
109   AdvanceTime(TimeDelta::Seconds(1));
110   SendRtcpSrInaccurately(/*ntp_error=*/TimeDelta::Millis(2),
111                          /*networking_delay=*/TimeDelta::Millis(-1));
112   AdvanceTime(TimeDelta::Seconds(1));
113   SendRtcpSrInaccurately(/*ntp_error=*/TimeDelta::Millis(-2),
114                          /*networking_delay=*/TimeDelta::Millis(1));
115 
116   // New RTP packet to estimate timestamp.
117   AdvanceTime(TimeDelta::Millis(150));
118   rtp_timestamp = GetRemoteTimestamp();
119   capture_ntp_time_ms = local_clock_.CurrentNtpInMilliseconds();
120 
121   // Errors should be averaged out.
122   EXPECT_EQ(capture_ntp_time_ms, estimator_.Estimate(rtp_timestamp));
123   EXPECT_EQ(kRemoteToLocalClockOffsetNtp,
124             estimator_.EstimateRemoteToLocalClockOffset());
125 }
126 
127 }  // namespace
128 }  // namespace webrtc
129