Home | History | Annotate | Download | only in source
      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 "testing/gmock/include/gmock/gmock.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 #include "webrtc/common_types.h"
     14 #include "webrtc/modules/rtp_rtcp/include/remote_ntp_time_estimator.h"
     15 #include "webrtc/system_wrappers/include/clock.h"
     16 
     17 using ::testing::_;
     18 using ::testing::DoAll;
     19 using ::testing::Return;
     20 using ::testing::SetArgPointee;
     21 
     22 namespace webrtc {
     23 
     24 static const int64_t kTestRtt = 10;
     25 static const int64_t kLocalClockInitialTimeMs = 123;
     26 static const int64_t kRemoteClockInitialTimeMs = 345;
     27 static const uint32_t kTimestampOffset = 567;
     28 
     29 class RemoteNtpTimeEstimatorTest : public ::testing::Test {
     30  protected:
     31   RemoteNtpTimeEstimatorTest()
     32       : local_clock_(kLocalClockInitialTimeMs * 1000),
     33         remote_clock_(kRemoteClockInitialTimeMs * 1000),
     34         estimator_(&local_clock_) {}
     35   ~RemoteNtpTimeEstimatorTest() {}
     36 
     37   void AdvanceTimeMilliseconds(int64_t ms) {
     38     local_clock_.AdvanceTimeMilliseconds(ms);
     39     remote_clock_.AdvanceTimeMilliseconds(ms);
     40   }
     41 
     42   uint32_t GetRemoteTimestamp() {
     43     return static_cast<uint32_t>(remote_clock_.TimeInMilliseconds()) * 90 +
     44            kTimestampOffset;
     45   }
     46 
     47   void SendRtcpSr() {
     48     uint32_t rtcp_timestamp = GetRemoteTimestamp();
     49     uint32_t ntp_seconds;
     50     uint32_t ntp_fractions;
     51     remote_clock_.CurrentNtp(ntp_seconds, ntp_fractions);
     52 
     53     AdvanceTimeMilliseconds(kTestRtt / 2);
     54     ReceiveRtcpSr(kTestRtt, rtcp_timestamp, ntp_seconds, ntp_fractions);
     55   }
     56 
     57   void UpdateRtcpTimestamp(int64_t rtt, uint32_t ntp_secs, uint32_t ntp_frac,
     58                            uint32_t rtp_timestamp, bool expected_result) {
     59     EXPECT_EQ(expected_result,
     60               estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac,
     61                                              rtp_timestamp));
     62   }
     63 
     64   void ReceiveRtcpSr(int64_t rtt,
     65                      uint32_t rtcp_timestamp,
     66                      uint32_t ntp_seconds,
     67                      uint32_t ntp_fractions) {
     68     UpdateRtcpTimestamp(rtt, ntp_seconds, ntp_fractions, rtcp_timestamp, true);
     69   }
     70 
     71   SimulatedClock local_clock_;
     72   SimulatedClock remote_clock_;
     73   RemoteNtpTimeEstimator estimator_;
     74 };
     75 
     76 TEST_F(RemoteNtpTimeEstimatorTest, Estimate) {
     77   // Failed without valid NTP.
     78   UpdateRtcpTimestamp(kTestRtt, 0, 0, 0, false);
     79 
     80   AdvanceTimeMilliseconds(1000);
     81   // Remote peer sends first RTCP SR.
     82   SendRtcpSr();
     83 
     84   // Remote sends a RTP packet.
     85   AdvanceTimeMilliseconds(15);
     86   uint32_t rtp_timestamp = GetRemoteTimestamp();
     87   int64_t capture_ntp_time_ms = local_clock_.CurrentNtpInMilliseconds();
     88 
     89   // Local peer needs at least 2 RTCP SR to calculate the capture time.
     90   const int64_t kNotEnoughRtcpSr = -1;
     91   EXPECT_EQ(kNotEnoughRtcpSr, estimator_.Estimate(rtp_timestamp));
     92 
     93   AdvanceTimeMilliseconds(800);
     94   // Remote sends second RTCP SR.
     95   SendRtcpSr();
     96 
     97   // Local peer gets enough RTCP SR to calculate the capture time.
     98   EXPECT_EQ(capture_ntp_time_ms, estimator_.Estimate(rtp_timestamp));
     99 }
    100 
    101 }  // namespace webrtc
    102