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 #ifndef MODULES_RTP_RTCP_INCLUDE_REMOTE_NTP_TIME_ESTIMATOR_H_ 12 #define MODULES_RTP_RTCP_INCLUDE_REMOTE_NTP_TIME_ESTIMATOR_H_ 13 14 #include <stdint.h> 15 16 #include "absl/types/optional.h" 17 #include "rtc_base/constructor_magic.h" 18 #include "rtc_base/numerics/moving_median_filter.h" 19 #include "system_wrappers/include/rtp_to_ntp_estimator.h" 20 21 namespace webrtc { 22 23 class Clock; 24 25 // RemoteNtpTimeEstimator can be used to estimate a given RTP timestamp's NTP 26 // time in local timebase. 27 // Note that it needs to be trained with at least 2 RTCP SR (by calling 28 // |UpdateRtcpTimestamp|) before it can be used. 29 class RemoteNtpTimeEstimator { 30 public: 31 explicit RemoteNtpTimeEstimator(Clock* clock); 32 33 ~RemoteNtpTimeEstimator(); 34 35 // Updates the estimator with round trip time |rtt|, NTP seconds |ntp_secs|, 36 // NTP fraction |ntp_frac| and RTP timestamp |rtp_timestamp|. 37 bool UpdateRtcpTimestamp(int64_t rtt, 38 uint32_t ntp_secs, 39 uint32_t ntp_frac, 40 uint32_t rtp_timestamp); 41 42 // Estimates the NTP timestamp in local timebase from |rtp_timestamp|. 43 // Returns the NTP timestamp in ms when success. -1 if failed. 44 int64_t Estimate(uint32_t rtp_timestamp); 45 46 // Estimates the offset, in milliseconds, between the remote clock and the 47 // local one. This is equal to local NTP clock - remote NTP clock. 48 absl::optional<int64_t> EstimateRemoteToLocalClockOffsetMs(); 49 50 private: 51 Clock* clock_; 52 MovingMedianFilter<int64_t> ntp_clocks_offset_estimator_; 53 RtpToNtpEstimator rtp_to_ntp_; 54 int64_t last_timing_log_ms_; 55 RTC_DISALLOW_COPY_AND_ASSIGN(RemoteNtpTimeEstimator); 56 }; 57 58 } // namespace webrtc 59 60 #endif // MODULES_RTP_RTCP_INCLUDE_REMOTE_NTP_TIME_ESTIMATOR_H_ 61