• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2012 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 SYSTEM_WRAPPERS_INCLUDE_RTP_TO_NTP_ESTIMATOR_H_
12 #define SYSTEM_WRAPPERS_INCLUDE_RTP_TO_NTP_ESTIMATOR_H_
13 
14 #include <stdint.h>
15 
16 #include <list>
17 
18 #include "absl/types/optional.h"
19 #include "modules/include/module_common_types_public.h"
20 #include "rtc_base/checks.h"
21 #include "rtc_base/numerics/moving_median_filter.h"
22 #include "system_wrappers/include/ntp_time.h"
23 
24 namespace webrtc {
25 // Class for converting an RTP timestamp to the NTP domain in milliseconds.
26 // The class needs to be trained with (at least 2) RTP/NTP timestamp pairs from
27 // RTCP sender reports before the convertion can be done.
28 class RtpToNtpEstimator {
29  public:
30   RtpToNtpEstimator();
31   ~RtpToNtpEstimator();
32 
33   // RTP and NTP timestamp pair from a RTCP SR report.
34   struct RtcpMeasurement {
35     RtcpMeasurement(uint32_t ntp_secs,
36                     uint32_t ntp_frac,
37                     int64_t unwrapped_timestamp);
38     bool IsEqual(const RtcpMeasurement& other) const;
39 
40     NtpTime ntp_time;
41     int64_t unwrapped_rtp_timestamp;
42   };
43 
44   // Estimated parameters from RTP and NTP timestamp pairs in |measurements_|.
45   struct Parameters {
ParametersParameters46     Parameters() : frequency_khz(0.0), offset_ms(0.0) {}
47 
ParametersParameters48     Parameters(double frequency_khz, double offset_ms)
49         : frequency_khz(frequency_khz), offset_ms(offset_ms) {}
50 
51     double frequency_khz;
52     double offset_ms;
53   };
54 
55   // Updates measurements with RTP/NTP timestamp pair from a RTCP sender report.
56   // |new_rtcp_sr| is set to true if a new report is added.
57   bool UpdateMeasurements(uint32_t ntp_secs,
58                           uint32_t ntp_frac,
59                           uint32_t rtp_timestamp,
60                           bool* new_rtcp_sr);
61 
62   // Converts an RTP timestamp to the NTP domain in milliseconds.
63   // Returns true on success, false otherwise.
64   bool Estimate(int64_t rtp_timestamp, int64_t* ntp_timestamp_ms) const;
65 
66   // Returns estimated rtp to ntp linear transform parameters.
67   const absl::optional<Parameters> params() const;
68 
69   static const int kMaxInvalidSamples = 3;
70 
71  private:
72   void UpdateParameters();
73 
74   int consecutive_invalid_samples_;
75   std::list<RtcpMeasurement> measurements_;
76   absl::optional<Parameters> params_;
77   mutable TimestampUnwrapper unwrapper_;
78 };
79 }  // namespace webrtc
80 
81 #endif  // SYSTEM_WRAPPERS_INCLUDE_RTP_TO_NTP_ESTIMATOR_H_
82