absolute_capture_time_sender.h (4308B)
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 <cstdint> 15 #include <optional> 16 17 #include "api/array_view.h" 18 #include "api/rtp_headers.h" 19 #include "api/units/time_delta.h" 20 #include "api/units/timestamp.h" 21 #include "system_wrappers/include/clock.h" 22 #include "system_wrappers/include/ntp_time.h" 23 24 namespace webrtc { 25 26 // 27 // Helper class for sending the `AbsoluteCaptureTime` header extension. 28 // 29 // Supports the "timestamp interpolation" optimization: 30 // A sender SHOULD save bandwidth by not sending abs-capture-time with every 31 // RTP packet. It SHOULD still send them at regular intervals (e.g. every 32 // second) to help mitigate the impact of clock drift and packet loss. Mixers 33 // SHOULD always send abs-capture-time with the first RTP packet after 34 // changing capture system. 35 // 36 // Timestamp interpolation works fine as long as there’s reasonably low 37 // NTP/RTP clock drift. This is not always true. Senders that detect “jumps” 38 // between its NTP and RTP clock mappings SHOULD send abs-capture-time with 39 // the first RTP packet after such a thing happening. 40 // 41 // See: https://webrtc.org/experiments/rtp-hdrext/abs-capture-time/ 42 // 43 class AbsoluteCaptureTimeSender { 44 public: 45 static constexpr TimeDelta kInterpolationMaxInterval = TimeDelta::Seconds(1); 46 static constexpr TimeDelta kInterpolationMaxError = TimeDelta::Millis(1); 47 48 explicit AbsoluteCaptureTimeSender(Clock* clock); 49 50 // Returns the source (i.e. SSRC or CSRC) of the capture system. 51 static uint32_t GetSource(uint32_t ssrc, ArrayView<const uint32_t> csrcs); 52 53 // Returns value to write into AbsoluteCaptureTime RTP header extension to be 54 // sent, or `std::nullopt` if the header extension shouldn't be attached to 55 // the outgoing packet. 56 // 57 // - `source` - id of the capture system. 58 // - `rtp_timestamp` - capture time represented as rtp timestamp in the 59 // outgoing packet 60 // - `rtp_clock_frequency_hz` - description of the `rtp_timestamp` units - 61 // `rtp_timetamp` delta of `rtp_clock_freqnecy_hz` represents 1 second. 62 // - `absolute_capture_time` - time when a frame was captured by the capture 63 // system. 64 // - `estimated_capture_clock_offset` - estimated offset between capture 65 // system clock and local `clock` passed as the AbsoluteCaptureTimeSender 66 // construction paramter. Uses the same units as `absolute_capture_time`, 67 // i.e. delta of 2^32 represents 1 second. See AbsoluteCaptureTime type 68 // comments for more details. 69 // - `force` - when set to true, OnSendPacket is forced to return non-nullopt. 70 std::optional<AbsoluteCaptureTime> OnSendPacket( 71 uint32_t source, 72 uint32_t rtp_timestamp, 73 int rtp_clock_frequency_hz, 74 NtpTime absolute_capture_time, 75 std::optional<int64_t> estimated_capture_clock_offset, 76 bool force = false); 77 78 // Returns a header extension to be sent, or `std::nullopt` if the header 79 // extension shouldn't be sent. 80 [[deprecated]] std::optional<AbsoluteCaptureTime> OnSendPacket( 81 uint32_t source, 82 uint32_t rtp_timestamp, 83 uint32_t rtp_clock_frequency, 84 uint64_t absolute_capture_timestamp, 85 std::optional<int64_t> estimated_capture_clock_offset); 86 87 private: 88 bool ShouldSendExtension( 89 Timestamp send_time, 90 uint32_t source, 91 uint32_t rtp_timestamp, 92 int rtp_clock_frequency_hz, 93 NtpTime absolute_capture_time, 94 std::optional<int64_t> estimated_capture_clock_offset) const; 95 96 Clock* const clock_; 97 98 Timestamp last_send_time_ = Timestamp::MinusInfinity(); 99 100 uint32_t last_source_; 101 uint32_t last_rtp_timestamp_; 102 int last_rtp_clock_frequency_hz_; 103 NtpTime last_absolute_capture_time_; 104 std::optional<int64_t> last_estimated_capture_clock_offset_; 105 }; 106 107 } // namespace webrtc 108 109 #endif // MODULES_RTP_RTCP_SOURCE_ABSOLUTE_CAPTURE_TIME_SENDER_H_