wu@webrtc.org | 88abf11 | 2014-05-14 16:53:51 +0000 | [diff] [blame] | 1 | /* |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 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 | */ |
wu@webrtc.org | 88abf11 | 2014-05-14 16:53:51 +0000 | [diff] [blame] | 10 | |
Mirko Bonadei | f6703c4 | 2017-11-15 16:14:28 +0000 | [diff] [blame] | 11 | #include "modules/rtp_rtcp/include/remote_ntp_time_estimator.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 12 | #include "system_wrappers/include/clock.h" |
| 13 | #include "test/gmock.h" |
| 14 | #include "test/gtest.h" |
wu@webrtc.org | 88abf11 | 2014-05-14 16:53:51 +0000 | [diff] [blame] | 15 | |
| 16 | using ::testing::_; |
| 17 | using ::testing::DoAll; |
| 18 | using ::testing::Return; |
| 19 | using ::testing::SetArgPointee; |
| 20 | |
| 21 | namespace webrtc { |
| 22 | |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 23 | static const int64_t kTestRtt = 10; |
wu@webrtc.org | 88abf11 | 2014-05-14 16:53:51 +0000 | [diff] [blame] | 24 | static const int64_t kLocalClockInitialTimeMs = 123; |
| 25 | static const int64_t kRemoteClockInitialTimeMs = 345; |
| 26 | static const uint32_t kTimestampOffset = 567; |
wu@webrtc.org | 88abf11 | 2014-05-14 16:53:51 +0000 | [diff] [blame] | 27 | |
| 28 | class RemoteNtpTimeEstimatorTest : public ::testing::Test { |
| 29 | protected: |
| 30 | RemoteNtpTimeEstimatorTest() |
| 31 | : local_clock_(kLocalClockInitialTimeMs * 1000), |
| 32 | remote_clock_(kRemoteClockInitialTimeMs * 1000), |
Ilya Nikolaevskiy | 8b64fd8 | 2017-11-15 16:48:04 +0000 | [diff] [blame] | 33 | estimator_(new RemoteNtpTimeEstimator(&local_clock_)) {} |
Danil Chapovalov | dd7e284 | 2018-03-09 15:37:03 +0000 | [diff] [blame] | 34 | ~RemoteNtpTimeEstimatorTest() override = default; |
wu@webrtc.org | 88abf11 | 2014-05-14 16:53:51 +0000 | [diff] [blame] | 35 | |
| 36 | void AdvanceTimeMilliseconds(int64_t ms) { |
| 37 | local_clock_.AdvanceTimeMilliseconds(ms); |
| 38 | remote_clock_.AdvanceTimeMilliseconds(ms); |
| 39 | } |
| 40 | |
| 41 | uint32_t GetRemoteTimestamp() { |
| 42 | return static_cast<uint32_t>(remote_clock_.TimeInMilliseconds()) * 90 + |
| 43 | kTimestampOffset; |
| 44 | } |
| 45 | |
| 46 | void SendRtcpSr() { |
| 47 | uint32_t rtcp_timestamp = GetRemoteTimestamp(); |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 48 | NtpTime ntp = remote_clock_.CurrentNtpTime(); |
wu@webrtc.org | 88abf11 | 2014-05-14 16:53:51 +0000 | [diff] [blame] | 49 | |
| 50 | AdvanceTimeMilliseconds(kTestRtt / 2); |
danilchap | 21dc189 | 2017-03-07 02:51:09 -0800 | [diff] [blame] | 51 | ReceiveRtcpSr(kTestRtt, rtcp_timestamp, ntp.seconds(), ntp.fractions()); |
wu@webrtc.org | 88abf11 | 2014-05-14 16:53:51 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Ilya Nikolaevskiy | 8b64fd8 | 2017-11-15 16:48:04 +0000 | [diff] [blame] | 54 | void SendRtcpSrInaccurately(int64_t ntp_error_ms, |
| 55 | int64_t networking_delay_ms) { |
| 56 | uint32_t rtcp_timestamp = GetRemoteTimestamp(); |
| 57 | int64_t ntp_error_fractions = |
Ilya Nikolaevskiy | ee45f90 | 2018-11-09 10:52:39 +0100 | [diff] [blame] | 58 | ntp_error_ms * static_cast<int64_t>(NtpTime::kFractionsPerSecond) / |
| 59 | 1000; |
Ilya Nikolaevskiy | 8b64fd8 | 2017-11-15 16:48:04 +0000 | [diff] [blame] | 60 | NtpTime ntp(static_cast<uint64_t>(remote_clock_.CurrentNtpTime()) + |
| 61 | ntp_error_fractions); |
| 62 | AdvanceTimeMilliseconds(kTestRtt / 2 + networking_delay_ms); |
| 63 | ReceiveRtcpSr(kTestRtt, rtcp_timestamp, ntp.seconds(), ntp.fractions()); |
| 64 | } |
| 65 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 66 | void UpdateRtcpTimestamp(int64_t rtt, |
| 67 | uint32_t ntp_secs, |
| 68 | uint32_t ntp_frac, |
| 69 | uint32_t rtp_timestamp, |
| 70 | bool expected_result) { |
Ilya Nikolaevskiy | 8b64fd8 | 2017-11-15 16:48:04 +0000 | [diff] [blame] | 71 | EXPECT_EQ(expected_result, estimator_->UpdateRtcpTimestamp( |
| 72 | rtt, ntp_secs, ntp_frac, rtp_timestamp)); |
wu@webrtc.org | 88abf11 | 2014-05-14 16:53:51 +0000 | [diff] [blame] | 73 | } |
| 74 | |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 75 | void ReceiveRtcpSr(int64_t rtt, |
minyue@webrtc.org | 2c0cdbc | 2014-10-09 10:52:43 +0000 | [diff] [blame] | 76 | uint32_t rtcp_timestamp, |
wu@webrtc.org | 88abf11 | 2014-05-14 16:53:51 +0000 | [diff] [blame] | 77 | uint32_t ntp_seconds, |
| 78 | uint32_t ntp_fractions) { |
minyue@webrtc.org | 2c0cdbc | 2014-10-09 10:52:43 +0000 | [diff] [blame] | 79 | UpdateRtcpTimestamp(rtt, ntp_seconds, ntp_fractions, rtcp_timestamp, true); |
wu@webrtc.org | 88abf11 | 2014-05-14 16:53:51 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | SimulatedClock local_clock_; |
| 83 | SimulatedClock remote_clock_; |
Ilya Nikolaevskiy | 8b64fd8 | 2017-11-15 16:48:04 +0000 | [diff] [blame] | 84 | std::unique_ptr<RemoteNtpTimeEstimator> estimator_; |
wu@webrtc.org | 88abf11 | 2014-05-14 16:53:51 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | TEST_F(RemoteNtpTimeEstimatorTest, Estimate) { |
minyue@webrtc.org | 2c0cdbc | 2014-10-09 10:52:43 +0000 | [diff] [blame] | 88 | // Failed without valid NTP. |
| 89 | UpdateRtcpTimestamp(kTestRtt, 0, 0, 0, false); |
wu@webrtc.org | 88abf11 | 2014-05-14 16:53:51 +0000 | [diff] [blame] | 90 | |
| 91 | AdvanceTimeMilliseconds(1000); |
| 92 | // Remote peer sends first RTCP SR. |
| 93 | SendRtcpSr(); |
| 94 | |
| 95 | // Remote sends a RTP packet. |
| 96 | AdvanceTimeMilliseconds(15); |
| 97 | uint32_t rtp_timestamp = GetRemoteTimestamp(); |
| 98 | int64_t capture_ntp_time_ms = local_clock_.CurrentNtpInMilliseconds(); |
| 99 | |
| 100 | // Local peer needs at least 2 RTCP SR to calculate the capture time. |
| 101 | const int64_t kNotEnoughRtcpSr = -1; |
Ilya Nikolaevskiy | 8b64fd8 | 2017-11-15 16:48:04 +0000 | [diff] [blame] | 102 | EXPECT_EQ(kNotEnoughRtcpSr, estimator_->Estimate(rtp_timestamp)); |
wu@webrtc.org | 88abf11 | 2014-05-14 16:53:51 +0000 | [diff] [blame] | 103 | |
| 104 | AdvanceTimeMilliseconds(800); |
| 105 | // Remote sends second RTCP SR. |
| 106 | SendRtcpSr(); |
| 107 | |
| 108 | // Local peer gets enough RTCP SR to calculate the capture time. |
Ilya Nikolaevskiy | 8b64fd8 | 2017-11-15 16:48:04 +0000 | [diff] [blame] | 109 | EXPECT_EQ(capture_ntp_time_ms, estimator_->Estimate(rtp_timestamp)); |
| 110 | } |
| 111 | |
| 112 | TEST_F(RemoteNtpTimeEstimatorTest, AveragesErrorsOut) { |
Ilya Nikolaevskiy | f1cc3a2 | 2018-11-12 15:03:51 +0100 | [diff] [blame] | 113 | // Remote peer sends first 10 RTCP SR without errors. |
| 114 | AdvanceTimeMilliseconds(1000); |
| 115 | SendRtcpSr(); |
| 116 | AdvanceTimeMilliseconds(1000); |
| 117 | SendRtcpSr(); |
| 118 | AdvanceTimeMilliseconds(1000); |
| 119 | SendRtcpSr(); |
| 120 | AdvanceTimeMilliseconds(1000); |
| 121 | SendRtcpSr(); |
| 122 | AdvanceTimeMilliseconds(1000); |
| 123 | SendRtcpSr(); |
Ilya Nikolaevskiy | 8b64fd8 | 2017-11-15 16:48:04 +0000 | [diff] [blame] | 124 | AdvanceTimeMilliseconds(1000); |
| 125 | SendRtcpSr(); |
| 126 | AdvanceTimeMilliseconds(1000); |
| 127 | SendRtcpSr(); |
| 128 | AdvanceTimeMilliseconds(1000); |
| 129 | SendRtcpSr(); |
| 130 | AdvanceTimeMilliseconds(1000); |
| 131 | SendRtcpSr(); |
| 132 | AdvanceTimeMilliseconds(1000); |
| 133 | SendRtcpSr(); |
| 134 | |
Ilya Nikolaevskiy | f1cc3a2 | 2018-11-12 15:03:51 +0100 | [diff] [blame] | 135 | AdvanceTimeMilliseconds(150); |
Ilya Nikolaevskiy | 8b64fd8 | 2017-11-15 16:48:04 +0000 | [diff] [blame] | 136 | uint32_t rtp_timestamp = GetRemoteTimestamp(); |
| 137 | int64_t capture_ntp_time_ms = local_clock_.CurrentNtpInMilliseconds(); |
Ilya Nikolaevskiy | 8b64fd8 | 2017-11-15 16:48:04 +0000 | [diff] [blame] | 138 | // Local peer gets enough RTCP SR to calculate the capture time. |
| 139 | EXPECT_EQ(capture_ntp_time_ms, estimator_->Estimate(rtp_timestamp)); |
| 140 | |
| 141 | // Remote sends corrupted RTCP SRs |
| 142 | AdvanceTimeMilliseconds(1000); |
Ilya Nikolaevskiy | f1cc3a2 | 2018-11-12 15:03:51 +0100 | [diff] [blame] | 143 | SendRtcpSrInaccurately(/*ntp_error_ms=*/2, /*networking_delay_ms=*/-1); |
Ilya Nikolaevskiy | 8b64fd8 | 2017-11-15 16:48:04 +0000 | [diff] [blame] | 144 | AdvanceTimeMilliseconds(1000); |
Ilya Nikolaevskiy | f1cc3a2 | 2018-11-12 15:03:51 +0100 | [diff] [blame] | 145 | SendRtcpSrInaccurately(/*ntp_error_ms=*/-2, /*networking_delay_ms=*/1); |
Ilya Nikolaevskiy | 8b64fd8 | 2017-11-15 16:48:04 +0000 | [diff] [blame] | 146 | |
| 147 | // New RTP packet to estimate timestamp. |
| 148 | AdvanceTimeMilliseconds(150); |
| 149 | rtp_timestamp = GetRemoteTimestamp(); |
| 150 | capture_ntp_time_ms = local_clock_.CurrentNtpInMilliseconds(); |
| 151 | |
| 152 | // Errors should be averaged out. |
| 153 | EXPECT_EQ(capture_ntp_time_ms, estimator_->Estimate(rtp_timestamp)); |
wu@webrtc.org | 88abf11 | 2014-05-14 16:53:51 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | } // namespace webrtc |