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 <map> 15 #include <memory> 16 #include <string> 17 #include <utility> 18 #include <vector> 19 20 #include "absl/strings/string_view.h" 21 #include "absl/types/optional.h" 22 #include "api/array_view.h" 23 #include "api/call/transport.h" 24 #include "api/transport/webrtc_key_value_config.h" 25 #include "modules/rtp_rtcp/include/flexfec_sender.h" 26 #include "modules/rtp_rtcp/include/rtp_header_extension_map.h" 27 #include "modules/rtp_rtcp/include/rtp_packet_sender.h" 28 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" 29 #include "modules/rtp_rtcp/source/rtp_packet_history.h" 30 #include "modules/rtp_rtcp/source/rtp_rtcp_config.h" 31 #include "modules/rtp_rtcp/source/rtp_rtcp_interface.h" 32 #include "rtc_base/constructor_magic.h" 33 #include "rtc_base/deprecation.h" 34 #include "rtc_base/random.h" 35 #include "rtc_base/rate_statistics.h" 36 #include "rtc_base/synchronization/mutex.h" 37 #include "rtc_base/thread_annotations.h" 38 39 namespace webrtc { 40 41 class FrameEncryptorInterface; 42 class RateLimiter; 43 class RtcEventLog; 44 class RtpPacketToSend; 45 46 class RTPSender { 47 public: 48 RTPSender(const RtpRtcpInterface::Configuration& config, 49 RtpPacketHistory* packet_history, 50 RtpPacketSender* packet_sender); 51 52 ~RTPSender(); 53 54 void SetSendingMediaStatus(bool enabled) RTC_LOCKS_EXCLUDED(send_mutex_); 55 bool SendingMedia() const RTC_LOCKS_EXCLUDED(send_mutex_); 56 bool IsAudioConfigured() const RTC_LOCKS_EXCLUDED(send_mutex_); 57 58 uint32_t TimestampOffset() const RTC_LOCKS_EXCLUDED(send_mutex_); 59 void SetTimestampOffset(uint32_t timestamp) RTC_LOCKS_EXCLUDED(send_mutex_); 60 61 void SetRid(const std::string& rid) RTC_LOCKS_EXCLUDED(send_mutex_); 62 63 void SetMid(const std::string& mid) RTC_LOCKS_EXCLUDED(send_mutex_); 64 65 uint16_t SequenceNumber() const RTC_LOCKS_EXCLUDED(send_mutex_); 66 void SetSequenceNumber(uint16_t seq) RTC_LOCKS_EXCLUDED(send_mutex_); 67 68 void SetCsrcs(const std::vector<uint32_t>& csrcs) 69 RTC_LOCKS_EXCLUDED(send_mutex_); 70 71 void SetMaxRtpPacketSize(size_t max_packet_size) 72 RTC_LOCKS_EXCLUDED(send_mutex_); 73 74 void SetExtmapAllowMixed(bool extmap_allow_mixed) 75 RTC_LOCKS_EXCLUDED(send_mutex_); 76 77 // RTP header extension 78 int32_t RegisterRtpHeaderExtension(RTPExtensionType type, uint8_t id) 79 RTC_LOCKS_EXCLUDED(send_mutex_); 80 bool RegisterRtpHeaderExtension(absl::string_view uri, int id) 81 RTC_LOCKS_EXCLUDED(send_mutex_); 82 bool IsRtpHeaderExtensionRegistered(RTPExtensionType type) const 83 RTC_LOCKS_EXCLUDED(send_mutex_); 84 int32_t DeregisterRtpHeaderExtension(RTPExtensionType type) 85 RTC_LOCKS_EXCLUDED(send_mutex_); 86 void DeregisterRtpHeaderExtension(absl::string_view uri) 87 RTC_LOCKS_EXCLUDED(send_mutex_); 88 89 bool SupportsPadding() const RTC_LOCKS_EXCLUDED(send_mutex_); 90 bool SupportsRtxPayloadPadding() const RTC_LOCKS_EXCLUDED(send_mutex_); 91 92 std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding( 93 size_t target_size_bytes, 94 bool media_has_been_sent) RTC_LOCKS_EXCLUDED(send_mutex_); 95 96 // NACK. 97 void OnReceivedNack(const std::vector<uint16_t>& nack_sequence_numbers, 98 int64_t avg_rtt) RTC_LOCKS_EXCLUDED(send_mutex_); 99 100 int32_t ReSendPacket(uint16_t packet_id) RTC_LOCKS_EXCLUDED(send_mutex_); 101 102 // ACK. 103 void OnReceivedAckOnSsrc(int64_t extended_highest_sequence_number) 104 RTC_LOCKS_EXCLUDED(send_mutex_); 105 void OnReceivedAckOnRtxSsrc(int64_t extended_highest_sequence_number) 106 RTC_LOCKS_EXCLUDED(send_mutex_); 107 108 // RTX. 109 void SetRtxStatus(int mode) RTC_LOCKS_EXCLUDED(send_mutex_); 110 int RtxStatus() const RTC_LOCKS_EXCLUDED(send_mutex_); RtxSsrc()111 absl::optional<uint32_t> RtxSsrc() const RTC_LOCKS_EXCLUDED(send_mutex_) { 112 return rtx_ssrc_; 113 } 114 115 void SetRtxPayloadType(int payload_type, int associated_payload_type) 116 RTC_LOCKS_EXCLUDED(send_mutex_); 117 118 // Size info for header extensions used by FEC packets. 119 static rtc::ArrayView<const RtpExtensionSize> FecExtensionSizes() 120 RTC_LOCKS_EXCLUDED(send_mutex_); 121 122 // Size info for header extensions used by video packets. 123 static rtc::ArrayView<const RtpExtensionSize> VideoExtensionSizes() 124 RTC_LOCKS_EXCLUDED(send_mutex_); 125 126 // Size info for header extensions used by audio packets. 127 static rtc::ArrayView<const RtpExtensionSize> AudioExtensionSizes() 128 RTC_LOCKS_EXCLUDED(send_mutex_); 129 130 // Create empty packet, fills ssrc, csrcs and reserve place for header 131 // extensions RtpSender updates before sending. 132 std::unique_ptr<RtpPacketToSend> AllocatePacket() const 133 RTC_LOCKS_EXCLUDED(send_mutex_); 134 // Allocate sequence number for provided packet. 135 // Save packet's fields to generate padding that doesn't break media stream. 136 // Return false if sending was turned off. 137 bool AssignSequenceNumber(RtpPacketToSend* packet) 138 RTC_LOCKS_EXCLUDED(send_mutex_); 139 // Maximum header overhead per fec/padding packet. 140 size_t FecOrPaddingPacketMaxRtpHeaderLength() const 141 RTC_LOCKS_EXCLUDED(send_mutex_); 142 // Expected header overhead per media packet. 143 size_t ExpectedPerPacketOverhead() const RTC_LOCKS_EXCLUDED(send_mutex_); 144 uint16_t AllocateSequenceNumber(uint16_t packets_to_send) 145 RTC_LOCKS_EXCLUDED(send_mutex_); 146 // Including RTP headers. 147 size_t MaxRtpPacketSize() const RTC_LOCKS_EXCLUDED(send_mutex_); 148 SSRC()149 uint32_t SSRC() const RTC_LOCKS_EXCLUDED(send_mutex_) { return ssrc_; } 150 FlexfecSsrc()151 absl::optional<uint32_t> FlexfecSsrc() const RTC_LOCKS_EXCLUDED(send_mutex_) { 152 return flexfec_ssrc_; 153 } 154 155 // Sends packet to |transport_| or to the pacer, depending on configuration. 156 // TODO(bugs.webrtc.org/XXX): Remove in favor of EnqueuePackets(). 157 bool SendToNetwork(std::unique_ptr<RtpPacketToSend> packet) 158 RTC_LOCKS_EXCLUDED(send_mutex_); 159 160 // Pass a set of packets to RtpPacketSender instance, for paced or immediate 161 // sending to the network. 162 void EnqueuePackets(std::vector<std::unique_ptr<RtpPacketToSend>> packets) 163 RTC_LOCKS_EXCLUDED(send_mutex_); 164 165 void SetRtpState(const RtpState& rtp_state) RTC_LOCKS_EXCLUDED(send_mutex_); 166 RtpState GetRtpState() const RTC_LOCKS_EXCLUDED(send_mutex_); 167 void SetRtxRtpState(const RtpState& rtp_state) 168 RTC_LOCKS_EXCLUDED(send_mutex_); 169 RtpState GetRtxRtpState() const RTC_LOCKS_EXCLUDED(send_mutex_); 170 171 int64_t LastTimestampTimeMs() const RTC_LOCKS_EXCLUDED(send_mutex_); 172 173 private: 174 std::unique_ptr<RtpPacketToSend> BuildRtxPacket( 175 const RtpPacketToSend& packet); 176 177 bool IsFecPacket(const RtpPacketToSend& packet) const; 178 179 void UpdateHeaderSizes() RTC_EXCLUSIVE_LOCKS_REQUIRED(send_mutex_); 180 181 Clock* const clock_; 182 Random random_ RTC_GUARDED_BY(send_mutex_); 183 184 const bool audio_configured_; 185 186 const uint32_t ssrc_; 187 const absl::optional<uint32_t> rtx_ssrc_; 188 const absl::optional<uint32_t> flexfec_ssrc_; 189 // Limits GeneratePadding() outcome to <= 190 // |max_padding_size_factor_| * |target_size_bytes| 191 const double max_padding_size_factor_; 192 193 RtpPacketHistory* const packet_history_; 194 RtpPacketSender* const paced_sender_; 195 196 mutable Mutex send_mutex_; 197 198 bool sending_media_ RTC_GUARDED_BY(send_mutex_); 199 size_t max_packet_size_; 200 201 int8_t last_payload_type_ RTC_GUARDED_BY(send_mutex_); 202 203 RtpHeaderExtensionMap rtp_header_extension_map_ RTC_GUARDED_BY(send_mutex_); 204 size_t max_media_packet_header_ RTC_GUARDED_BY(send_mutex_); 205 size_t max_padding_fec_packet_header_ RTC_GUARDED_BY(send_mutex_); 206 207 // RTP variables 208 uint32_t timestamp_offset_ RTC_GUARDED_BY(send_mutex_); 209 bool sequence_number_forced_ RTC_GUARDED_BY(send_mutex_); 210 uint16_t sequence_number_ RTC_GUARDED_BY(send_mutex_); 211 uint16_t sequence_number_rtx_ RTC_GUARDED_BY(send_mutex_); 212 // RID value to send in the RID or RepairedRID header extension. 213 std::string rid_ RTC_GUARDED_BY(send_mutex_); 214 // MID value to send in the MID header extension. 215 std::string mid_ RTC_GUARDED_BY(send_mutex_); 216 // Should we send MID/RID even when ACKed? (see below). 217 const bool always_send_mid_and_rid_; 218 // Track if any ACK has been received on the SSRC and RTX SSRC to indicate 219 // when to stop sending the MID and RID header extensions. 220 bool ssrc_has_acked_ RTC_GUARDED_BY(send_mutex_); 221 bool rtx_ssrc_has_acked_ RTC_GUARDED_BY(send_mutex_); 222 uint32_t last_rtp_timestamp_ RTC_GUARDED_BY(send_mutex_); 223 int64_t capture_time_ms_ RTC_GUARDED_BY(send_mutex_); 224 int64_t last_timestamp_time_ms_ RTC_GUARDED_BY(send_mutex_); 225 bool last_packet_marker_bit_ RTC_GUARDED_BY(send_mutex_); 226 std::vector<uint32_t> csrcs_ RTC_GUARDED_BY(send_mutex_); 227 int rtx_ RTC_GUARDED_BY(send_mutex_); 228 // Mapping rtx_payload_type_map_[associated] = rtx. 229 std::map<int8_t, int8_t> rtx_payload_type_map_ RTC_GUARDED_BY(send_mutex_); 230 bool supports_bwe_extension_ RTC_GUARDED_BY(send_mutex_); 231 232 RateLimiter* const retransmission_rate_limiter_; 233 234 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RTPSender); 235 }; 236 237 } // namespace webrtc 238 239 #endif // MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_ 240