xref: /aosp_15_r20/external/webrtc/modules/rtp_rtcp/source/absolute_capture_time_interpolator.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2021 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_INTERPOLATOR_H_
12 #define MODULES_RTP_RTCP_SOURCE_ABSOLUTE_CAPTURE_TIME_INTERPOLATOR_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 interpolating 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 AbsoluteCaptureTimeInterpolator {
37  public:
38   static constexpr TimeDelta kInterpolationMaxInterval =
39       TimeDelta::Millis(5000);
40 
41   explicit AbsoluteCaptureTimeInterpolator(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   // Returns a received header extension, an interpolated header extension, or
48   // `absl::nullopt` if it's not possible to interpolate a header extension.
49   absl::optional<AbsoluteCaptureTime> OnReceivePacket(
50       uint32_t source,
51       uint32_t rtp_timestamp,
52       uint32_t rtp_clock_frequency,
53       const absl::optional<AbsoluteCaptureTime>& received_extension);
54 
55  private:
56   friend class AbsoluteCaptureTimeSender;
57 
58   static uint64_t InterpolateAbsoluteCaptureTimestamp(
59       uint32_t rtp_timestamp,
60       uint32_t rtp_clock_frequency,
61       uint32_t last_rtp_timestamp,
62       uint64_t last_absolute_capture_timestamp);
63 
64   bool ShouldInterpolateExtension(Timestamp receive_time,
65                                   uint32_t source,
66                                   uint32_t rtp_timestamp,
67                                   uint32_t rtp_clock_frequency) const
68       RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
69 
70   Clock* const clock_;
71 
72   Mutex mutex_;
73 
74   Timestamp last_receive_time_ RTC_GUARDED_BY(mutex_);
75 
76   uint32_t last_source_ RTC_GUARDED_BY(mutex_);
77   uint32_t last_rtp_timestamp_ RTC_GUARDED_BY(mutex_);
78   uint32_t last_rtp_clock_frequency_ RTC_GUARDED_BY(mutex_);
79   uint64_t last_absolute_capture_timestamp_ RTC_GUARDED_BY(mutex_);
80   absl::optional<int64_t> last_estimated_capture_clock_offset_
81       RTC_GUARDED_BY(mutex_);
82 };  // AbsoluteCaptureTimeInterpolator
83 
84 }  // namespace webrtc
85 
86 #endif  // MODULES_RTP_RTCP_SOURCE_ABSOLUTE_CAPTURE_TIME_INTERPOLATOR_H_
87