rtp_sender.h (8113B)
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 MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_ 12 #define MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_ 13 14 #include <cstddef> 15 #include <cstdint> 16 #include <map> 17 #include <memory> 18 #include <optional> 19 #include <string> 20 #include <vector> 21 22 #include "absl/strings/string_view.h" 23 #include "api/array_view.h" 24 #include "api/environment/environment.h" 25 #include "api/rtp_packet_sender.h" 26 #include "modules/rtp_rtcp/include/flexfec_sender.h" 27 #include "modules/rtp_rtcp/include/rtp_header_extension_map.h" 28 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" 29 #include "modules/rtp_rtcp/source/rtp_header_extension_size.h" 30 #include "modules/rtp_rtcp/source/rtp_packet_history.h" 31 #include "modules/rtp_rtcp/source/rtp_rtcp_interface.h" 32 #include "rtc_base/random.h" 33 #include "rtc_base/synchronization/mutex.h" 34 #include "rtc_base/thread_annotations.h" 35 36 namespace webrtc { 37 38 class FrameEncryptorInterface; 39 class RateLimiter; 40 class RtpPacketToSend; 41 42 // Maximum amount of padding in RFC 3550 is 255 bytes. 43 constexpr size_t kMaxPaddingLength = 255; 44 45 class RTPSender { 46 public: 47 RTPSender(const Environment& env, 48 const RtpRtcpInterface::Configuration& config, 49 RtpPacketHistory* packet_history, 50 RtpPacketSender* packet_sender); 51 52 RTPSender(const RTPSender&) = delete; 53 RTPSender& operator=(const RTPSender&) = delete; 54 55 ~RTPSender(); 56 57 void SetSendingMediaStatus(bool enabled) RTC_LOCKS_EXCLUDED(send_mutex_); 58 bool SendingMedia() const RTC_LOCKS_EXCLUDED(send_mutex_); 59 bool IsAudioConfigured() const RTC_LOCKS_EXCLUDED(send_mutex_); 60 61 uint32_t TimestampOffset() const RTC_LOCKS_EXCLUDED(send_mutex_); 62 void SetTimestampOffset(uint32_t timestamp) RTC_LOCKS_EXCLUDED(send_mutex_); 63 64 void SetMid(absl::string_view mid) RTC_LOCKS_EXCLUDED(send_mutex_); 65 66 uint16_t SequenceNumber() const RTC_LOCKS_EXCLUDED(send_mutex_); 67 void SetSequenceNumber(uint16_t seq) RTC_LOCKS_EXCLUDED(send_mutex_); 68 69 void SetMaxRtpPacketSize(size_t max_packet_size) 70 RTC_LOCKS_EXCLUDED(send_mutex_); 71 72 void SetExtmapAllowMixed(bool extmap_allow_mixed) 73 RTC_LOCKS_EXCLUDED(send_mutex_); 74 75 // RTP header extension 76 bool RegisterRtpHeaderExtension(absl::string_view uri, int id) 77 RTC_LOCKS_EXCLUDED(send_mutex_); 78 bool IsRtpHeaderExtensionRegistered(RTPExtensionType type) const 79 RTC_LOCKS_EXCLUDED(send_mutex_); 80 void DeregisterRtpHeaderExtension(absl::string_view uri) 81 RTC_LOCKS_EXCLUDED(send_mutex_); 82 83 bool SupportsPadding() const RTC_LOCKS_EXCLUDED(send_mutex_); 84 bool SupportsRtxPayloadPadding() const RTC_LOCKS_EXCLUDED(send_mutex_); 85 86 std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding( 87 size_t target_size_bytes, 88 bool media_has_been_sent, 89 bool can_send_padding_on_media_ssrc) RTC_LOCKS_EXCLUDED(send_mutex_); 90 91 // NACK. 92 void OnReceivedNack(const std::vector<uint16_t>& nack_sequence_numbers, 93 int64_t avg_rtt) RTC_LOCKS_EXCLUDED(send_mutex_); 94 95 int32_t ReSendPacket(uint16_t packet_id) RTC_LOCKS_EXCLUDED(send_mutex_); 96 97 // ACK. 98 void OnReceivedAckOnSsrc(int64_t extended_highest_sequence_number) 99 RTC_LOCKS_EXCLUDED(send_mutex_); 100 void OnReceivedAckOnRtxSsrc(int64_t extended_highest_sequence_number) 101 RTC_LOCKS_EXCLUDED(send_mutex_); 102 103 // RTX. 104 void SetRtxStatus(int mode) RTC_LOCKS_EXCLUDED(send_mutex_); 105 int RtxStatus() const RTC_LOCKS_EXCLUDED(send_mutex_); 106 std::optional<uint32_t> RtxSsrc() const RTC_LOCKS_EXCLUDED(send_mutex_) { 107 return rtx_ssrc_; 108 } 109 // Returns expected size difference between an RTX packet and media packet 110 // that RTX packet is created from. Returns 0 if RTX is disabled. 111 size_t RtxPacketOverhead() const; 112 113 void SetRtxPayloadType(int payload_type, int associated_payload_type) 114 RTC_LOCKS_EXCLUDED(send_mutex_); 115 116 // Size info for header extensions used by FEC packets. 117 static ArrayView<const RtpExtensionSize> FecExtensionSizes() 118 RTC_LOCKS_EXCLUDED(send_mutex_); 119 120 // Size info for header extensions used by video packets. 121 static ArrayView<const RtpExtensionSize> VideoExtensionSizes() 122 RTC_LOCKS_EXCLUDED(send_mutex_); 123 124 // Size info for header extensions used by audio packets. 125 static ArrayView<const RtpExtensionSize> AudioExtensionSizes() 126 RTC_LOCKS_EXCLUDED(send_mutex_); 127 128 // Create empty packet, fills ssrc, csrcs and reserve place for header 129 // extensions RtpSender updates before sending. 130 std::unique_ptr<RtpPacketToSend> AllocatePacket( 131 ArrayView<const uint32_t> csrcs = {}) RTC_LOCKS_EXCLUDED(send_mutex_); 132 133 // Maximum header overhead per fec/padding packet. 134 size_t FecOrPaddingPacketMaxRtpHeaderLength() const 135 RTC_LOCKS_EXCLUDED(send_mutex_); 136 // Expected header overhead per media packet. 137 size_t ExpectedPerPacketOverhead() const RTC_LOCKS_EXCLUDED(send_mutex_); 138 // Including RTP headers. 139 size_t MaxRtpPacketSize() const RTC_LOCKS_EXCLUDED(send_mutex_); 140 141 uint32_t SSRC() const RTC_LOCKS_EXCLUDED(send_mutex_) { return ssrc_; } 142 143 std::string Rid() const RTC_LOCKS_EXCLUDED(send_mutex_) { return rid_; } 144 145 std::optional<uint32_t> FlexfecSsrc() const RTC_LOCKS_EXCLUDED(send_mutex_) { 146 return flexfec_ssrc_; 147 } 148 149 // Pass a set of packets to RtpPacketSender instance, for paced or immediate 150 // sending to the network. 151 void EnqueuePackets(std::vector<std::unique_ptr<RtpPacketToSend>> packets) 152 RTC_LOCKS_EXCLUDED(send_mutex_); 153 154 void SetRtpState(const RtpState& rtp_state) RTC_LOCKS_EXCLUDED(send_mutex_); 155 RtpState GetRtpState() const RTC_LOCKS_EXCLUDED(send_mutex_); 156 void SetRtxRtpState(const RtpState& rtp_state) 157 RTC_LOCKS_EXCLUDED(send_mutex_); 158 RtpState GetRtxRtpState() const RTC_LOCKS_EXCLUDED(send_mutex_); 159 160 private: 161 std::unique_ptr<RtpPacketToSend> BuildRtxPacket( 162 const RtpPacketToSend& packet); 163 164 bool IsFecPacket(const RtpPacketToSend& packet) const; 165 166 void UpdateHeaderSizes() RTC_EXCLUSIVE_LOCKS_REQUIRED(send_mutex_); 167 168 void UpdateLastPacketState(const RtpPacketToSend& packet) 169 RTC_EXCLUSIVE_LOCKS_REQUIRED(send_mutex_); 170 171 Clock* const clock_; 172 Random random_ RTC_GUARDED_BY(send_mutex_); 173 174 const bool audio_configured_; 175 176 const uint32_t ssrc_; 177 const std::optional<uint32_t> rtx_ssrc_; 178 const std::optional<uint32_t> flexfec_ssrc_; 179 180 RtpPacketHistory* const packet_history_; 181 RtpPacketSender* const paced_sender_; 182 183 mutable Mutex send_mutex_; 184 185 bool sending_media_ RTC_GUARDED_BY(send_mutex_); 186 size_t max_packet_size_; 187 188 RtpHeaderExtensionMap rtp_header_extension_map_ RTC_GUARDED_BY(send_mutex_); 189 size_t max_media_packet_header_ RTC_GUARDED_BY(send_mutex_); 190 size_t max_padding_fec_packet_header_ RTC_GUARDED_BY(send_mutex_); 191 192 // RTP variables 193 uint32_t timestamp_offset_ RTC_GUARDED_BY(send_mutex_); 194 // RID value to send in the RID or RepairedRID header extension. 195 const std::string rid_; 196 // MID value to send in the MID header extension. 197 std::string mid_ RTC_GUARDED_BY(send_mutex_); 198 // Should we send MID/RID even when ACKed? (see below). 199 const bool always_send_mid_and_rid_; 200 // Track if any ACK has been received on the SSRC and RTX SSRC to indicate 201 // when to stop sending the MID and RID header extensions. 202 bool ssrc_has_acked_ RTC_GUARDED_BY(send_mutex_); 203 bool rtx_ssrc_has_acked_ RTC_GUARDED_BY(send_mutex_); 204 // Maximum number of csrcs this sender is used with. 205 size_t max_num_csrcs_ RTC_GUARDED_BY(send_mutex_) = 0; 206 int rtx_ RTC_GUARDED_BY(send_mutex_); 207 // Mapping rtx_payload_type_map_[associated] = rtx. 208 std::map<int8_t, int8_t> rtx_payload_type_map_ RTC_GUARDED_BY(send_mutex_); 209 bool supports_bwe_extension_ RTC_GUARDED_BY(send_mutex_); 210 211 RateLimiter* const retransmission_rate_limiter_; 212 }; 213 214 } // namespace webrtc 215 216 #endif // MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_