• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2019 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_SOURCE_ABSOLUTE_CAPTURE_TIME_RECEIVER_H_
12 #define MODULES_RTP_RTCP_SOURCE_ABSOLUTE_CAPTURE_TIME_RECEIVER_H_
13 
14 #include "api/array_view.h"
15 #include "api/rtp_headers.h"
16 #include "api/units/time_delta.h"
17 #include "api/units/timestamp.h"
18 #include "rtc_base/synchronization/mutex.h"
19 #include "rtc_base/thread_annotations.h"
20 #include "system_wrappers/include/clock.h"
21 
22 namespace webrtc {
23 
24 //
25 // Helper class for receiving the |AbsoluteCaptureTime| header extension.
26 //
27 // Supports the "timestamp interpolation" optimization:
28 //   A receiver SHOULD memorize the capture system (i.e. CSRC/SSRC), capture
29 //   timestamp, and RTP timestamp of the most recently received abs-capture-time
30 //   packet on each received stream. It can then use that information, in
31 //   combination with RTP timestamps of packets without abs-capture-time, to
32 //   extrapolate missing capture timestamps.
33 //
34 // See: https://webrtc.org/experiments/rtp-hdrext/abs-capture-time/
35 //
36 class AbsoluteCaptureTimeReceiver {
37  public:
38   static constexpr TimeDelta kInterpolationMaxInterval =
39       TimeDelta::Millis(5000);
40 
41   explicit AbsoluteCaptureTimeReceiver(Clock* clock);
42 
43   // Returns the source (i.e. SSRC or CSRC) of the capture system.
44   static uint32_t GetSource(uint32_t ssrc,
45                             rtc::ArrayView<const uint32_t> csrcs);
46 
47   // Sets the NTP clock offset between the sender system (which may be different
48   // from the capture system) and the local system. This information is normally
49   // provided by passing half the value of the Round-Trip Time estimation given
50   // by RTCP sender reports (see DLSR/DLRR).
51   //
52   // Note that the value must be in Q32.32-formatted fixed-point seconds.
53   void SetRemoteToLocalClockOffset(absl::optional<int64_t> value_q32x32);
54 
55   // Returns a received header extension, an interpolated header extension, or
56   // |absl::nullopt| if it's not possible to interpolate a header extension.
57   absl::optional<AbsoluteCaptureTime> OnReceivePacket(
58       uint32_t source,
59       uint32_t rtp_timestamp,
60       uint32_t rtp_clock_frequency,
61       const absl::optional<AbsoluteCaptureTime>& received_extension);
62 
63  private:
64   friend class AbsoluteCaptureTimeSender;
65 
66   static uint64_t InterpolateAbsoluteCaptureTimestamp(
67       uint32_t rtp_timestamp,
68       uint32_t rtp_clock_frequency,
69       uint32_t last_rtp_timestamp,
70       uint64_t last_absolute_capture_timestamp);
71 
72   bool ShouldInterpolateExtension(Timestamp receive_time,
73                                   uint32_t source,
74                                   uint32_t rtp_timestamp,
75                                   uint32_t rtp_clock_frequency) const
76       RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
77 
78   absl::optional<int64_t> AdjustEstimatedCaptureClockOffset(
79       absl::optional<int64_t> received_value) const
80       RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
81 
82   Clock* const clock_;
83 
84   Mutex mutex_;
85 
86   absl::optional<int64_t> remote_to_local_clock_offset_ RTC_GUARDED_BY(mutex_);
87 
88   Timestamp last_receive_time_ RTC_GUARDED_BY(mutex_);
89 
90   uint32_t last_source_ RTC_GUARDED_BY(mutex_);
91   uint32_t last_rtp_timestamp_ RTC_GUARDED_BY(mutex_);
92   uint32_t last_rtp_clock_frequency_ RTC_GUARDED_BY(mutex_);
93   uint64_t last_absolute_capture_timestamp_ RTC_GUARDED_BY(mutex_);
94   absl::optional<int64_t> last_estimated_capture_clock_offset_
95       RTC_GUARDED_BY(mutex_);
96 };  // AbsoluteCaptureTimeReceiver
97 
98 }  // namespace webrtc
99 
100 #endif  // MODULES_RTP_RTCP_SOURCE_ABSOLUTE_CAPTURE_TIME_RECEIVER_H_
101