• 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_SENDER_H_
12 #define MODULES_RTP_RTCP_SOURCE_ABSOLUTE_CAPTURE_TIME_SENDER_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 sending the |AbsoluteCaptureTime| header extension.
26 //
27 // Supports the "timestamp interpolation" optimization:
28 //   A sender SHOULD save bandwidth by not sending abs-capture-time with every
29 //   RTP packet. It SHOULD still send them at regular intervals (e.g. every
30 //   second) to help mitigate the impact of clock drift and packet loss. Mixers
31 //   SHOULD always send abs-capture-time with the first RTP packet after
32 //   changing capture system.
33 //
34 //   Timestamp interpolation works fine as long as there’s reasonably low
35 //   NTP/RTP clock drift. This is not always true. Senders that detect “jumps”
36 //   between its NTP and RTP clock mappings SHOULD send abs-capture-time with
37 //   the first RTP packet after such a thing happening.
38 //
39 // See: https://webrtc.org/experiments/rtp-hdrext/abs-capture-time/
40 //
41 class AbsoluteCaptureTimeSender {
42  public:
43   static constexpr TimeDelta kInterpolationMaxInterval =
44       TimeDelta::Millis(1000);
45   static constexpr TimeDelta kInterpolationMaxError = TimeDelta::Millis(1);
46 
47   explicit AbsoluteCaptureTimeSender(Clock* clock);
48 
49   // Returns the source (i.e. SSRC or CSRC) of the capture system.
50   static uint32_t GetSource(uint32_t ssrc,
51                             rtc::ArrayView<const uint32_t> csrcs);
52 
53   // Returns a header extension to be sent, or |absl::nullopt| if the header
54   // extension shouldn't be sent.
55   absl::optional<AbsoluteCaptureTime> OnSendPacket(
56       uint32_t source,
57       uint32_t rtp_timestamp,
58       uint32_t rtp_clock_frequency,
59       uint64_t absolute_capture_timestamp,
60       absl::optional<int64_t> estimated_capture_clock_offset);
61 
62  private:
63   bool ShouldSendExtension(
64       Timestamp send_time,
65       uint32_t source,
66       uint32_t rtp_timestamp,
67       uint32_t rtp_clock_frequency,
68       uint64_t absolute_capture_timestamp,
69       absl::optional<int64_t> estimated_capture_clock_offset) const
70       RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
71 
72   Clock* const clock_;
73 
74   Mutex mutex_;
75 
76   Timestamp last_send_time_ RTC_GUARDED_BY(mutex_);
77 
78   uint32_t last_source_ RTC_GUARDED_BY(mutex_);
79   uint32_t last_rtp_timestamp_ RTC_GUARDED_BY(mutex_);
80   uint32_t last_rtp_clock_frequency_ RTC_GUARDED_BY(mutex_);
81   uint64_t last_absolute_capture_timestamp_ RTC_GUARDED_BY(mutex_);
82   absl::optional<int64_t> last_estimated_capture_clock_offset_
83       RTC_GUARDED_BY(mutex_);
84 };  // AbsoluteCaptureTimeSender
85 
86 }  // namespace webrtc
87 
88 #endif  // MODULES_RTP_RTCP_SOURCE_ABSOLUTE_CAPTURE_TIME_SENDER_H_
89