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/interface/remote_ntp_time_estimator.h"
     15 #include "webrtc/modules/rtp_rtcp/mocks/mock_rtp_rtcp.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 int kTestRtt = 10;
     25 static const int64_t kLocalClockInitialTimeMs = 123;
     26 static const int64_t kRemoteClockInitialTimeMs = 345;
     27 static const uint32_t kTimestampOffset = 567;
     28 static const int kTestSsrc = 789;
     29 
     30 class RemoteNtpTimeEstimatorTest : public ::testing::Test {
     31  protected:
     32   RemoteNtpTimeEstimatorTest()
     33       : local_clock_(kLocalClockInitialTimeMs * 1000),
     34         remote_clock_(kRemoteClockInitialTimeMs * 1000),
     35         estimator_(&local_clock_) {}
     36   ~RemoteNtpTimeEstimatorTest() {}
     37 
     38   void AdvanceTimeMilliseconds(int64_t ms) {
     39     local_clock_.AdvanceTimeMilliseconds(ms);
     40     remote_clock_.AdvanceTimeMilliseconds(ms);
     41   }
     42 
     43   uint32_t GetRemoteTimestamp() {
     44     return static_cast<uint32_t>(remote_clock_.TimeInMilliseconds()) * 90 +
     45            kTimestampOffset;
     46   }
     47 
     48   void SendRtcpSr() {
     49     uint32_t rtcp_timestamp = GetRemoteTimestamp();
     50     uint32_t ntp_seconds;
     51     uint32_t ntp_fractions;
     52     remote_clock_.CurrentNtp(ntp_seconds, ntp_fractions);
     53 
     54     AdvanceTimeMilliseconds(kTestRtt / 2);
     55     ReceiveRtcpSr(rtcp_timestamp, ntp_seconds, ntp_fractions);
     56   }
     57 
     58   void UpdateRtcpTimestamp(MockRtpRtcp* rtp_rtcp, bool expected_result) {
     59     if (rtp_rtcp) {
     60       EXPECT_CALL(*rtp_rtcp, RTT(_, _, _, _, _))
     61           .WillOnce(DoAll(SetArgPointee<1>(kTestRtt),
     62                           Return(0)));
     63     }
     64     EXPECT_EQ(expected_result,
     65               estimator_.UpdateRtcpTimestamp(kTestSsrc, rtp_rtcp));
     66   }
     67 
     68   void ReceiveRtcpSr(uint32_t rtcp_timestamp,
     69                      uint32_t ntp_seconds,
     70                      uint32_t ntp_fractions) {
     71     EXPECT_CALL(rtp_rtcp_, RemoteNTP(_, _, _, _, _))
     72         .WillOnce(DoAll(SetArgPointee<0>(ntp_seconds),
     73                         SetArgPointee<1>(ntp_fractions),
     74                         SetArgPointee<4>(rtcp_timestamp),
     75                         Return(0)));
     76 
     77     UpdateRtcpTimestamp(&rtp_rtcp_, true);
     78   }
     79 
     80   SimulatedClock local_clock_;
     81   SimulatedClock remote_clock_;
     82   MockRtpRtcp rtp_rtcp_;
     83   RemoteNtpTimeEstimator estimator_;
     84 };
     85 
     86 TEST_F(RemoteNtpTimeEstimatorTest, Estimate) {
     87   // Failed without any RTCP SR, where RemoteNTP returns without valid NTP.
     88   EXPECT_CALL(rtp_rtcp_, RemoteNTP(_, _, _, _, _)).WillOnce(Return(0));
     89   UpdateRtcpTimestamp(&rtp_rtcp_, false);
     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;
    102   EXPECT_EQ(kNotEnoughRtcpSr, estimator_.Estimate(rtp_timestamp));
    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.
    109   EXPECT_EQ(capture_ntp_time_ms, estimator_.Estimate(rtp_timestamp));
    110 }
    111 
    112 }  // namespace webrtc
    113