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/field_trials_view.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/random.h" 33 #include "rtc_base/rate_statistics.h" 34 #include "rtc_base/synchronization/mutex.h" 35 #include "rtc_base/thread_annotations.h" 36 37 namespace webrtc { 38 39 class FrameEncryptorInterface; 40 class RateLimiter; 41 class RtcEventLog; 42 class RtpPacketToSend; 43 44 class RTPSender { 45 public: 46 RTPSender(const RtpRtcpInterface::Configuration& config, 47 RtpPacketHistory* packet_history, 48 RtpPacketSender* packet_sender); 49 RTPSender(const RTPSender&) = delete; 50 RTPSender& operator=(const RTPSender&) = delete; 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 SetMid(absl::string_view mid) RTC_LOCKS_EXCLUDED(send_mutex_); 62 63 uint16_t SequenceNumber() const RTC_LOCKS_EXCLUDED(send_mutex_); 64 void SetSequenceNumber(uint16_t seq) RTC_LOCKS_EXCLUDED(send_mutex_); 65 66 void SetCsrcs(const std::vector<uint32_t>& csrcs) 67 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_); RtxSsrc()106 absl::optional<uint32_t> RtxSsrc() const RTC_LOCKS_EXCLUDED(send_mutex_) { 107 return rtx_ssrc_; 108 } 109 110 void SetRtxPayloadType(int payload_type, int associated_payload_type) 111 RTC_LOCKS_EXCLUDED(send_mutex_); 112 113 // Size info for header extensions used by FEC packets. 114 static rtc::ArrayView<const RtpExtensionSize> FecExtensionSizes() 115 RTC_LOCKS_EXCLUDED(send_mutex_); 116 117 // Size info for header extensions used by video packets. 118 static rtc::ArrayView<const RtpExtensionSize> VideoExtensionSizes() 119 RTC_LOCKS_EXCLUDED(send_mutex_); 120 121 // Size info for header extensions used by audio packets. 122 static rtc::ArrayView<const RtpExtensionSize> AudioExtensionSizes() 123 RTC_LOCKS_EXCLUDED(send_mutex_); 124 125 // Create empty packet, fills ssrc, csrcs and reserve place for header 126 // extensions RtpSender updates before sending. 127 std::unique_ptr<RtpPacketToSend> AllocatePacket() const 128 RTC_LOCKS_EXCLUDED(send_mutex_); 129 // Maximum header overhead per fec/padding packet. 130 size_t FecOrPaddingPacketMaxRtpHeaderLength() const 131 RTC_LOCKS_EXCLUDED(send_mutex_); 132 // Expected header overhead per media packet. 133 size_t ExpectedPerPacketOverhead() const RTC_LOCKS_EXCLUDED(send_mutex_); 134 // Including RTP headers. 135 size_t MaxRtpPacketSize() const RTC_LOCKS_EXCLUDED(send_mutex_); 136 SSRC()137 uint32_t SSRC() const RTC_LOCKS_EXCLUDED(send_mutex_) { return ssrc_; } 138 FlexfecSsrc()139 absl::optional<uint32_t> FlexfecSsrc() const RTC_LOCKS_EXCLUDED(send_mutex_) { 140 return flexfec_ssrc_; 141 } 142 143 // Sends packet to `transport_` or to the pacer, depending on configuration. 144 // TODO(bugs.webrtc.org/XXX): Remove in favor of EnqueuePackets(). 145 bool SendToNetwork(std::unique_ptr<RtpPacketToSend> packet) 146 RTC_LOCKS_EXCLUDED(send_mutex_); 147 148 // Pass a set of packets to RtpPacketSender instance, for paced or immediate 149 // sending to the network. 150 void EnqueuePackets(std::vector<std::unique_ptr<RtpPacketToSend>> packets) 151 RTC_LOCKS_EXCLUDED(send_mutex_); 152 153 void SetRtpState(const RtpState& rtp_state) RTC_LOCKS_EXCLUDED(send_mutex_); 154 RtpState GetRtpState() const RTC_LOCKS_EXCLUDED(send_mutex_); 155 void SetRtxRtpState(const RtpState& rtp_state) 156 RTC_LOCKS_EXCLUDED(send_mutex_); 157 RtpState GetRtxRtpState() const RTC_LOCKS_EXCLUDED(send_mutex_); 158 159 private: 160 std::unique_ptr<RtpPacketToSend> BuildRtxPacket( 161 const RtpPacketToSend& packet); 162 163 bool IsFecPacket(const RtpPacketToSend& packet) const; 164 165 void UpdateHeaderSizes() RTC_EXCLUSIVE_LOCKS_REQUIRED(send_mutex_); 166 167 void UpdateLastPacketState(const RtpPacketToSend& packet) 168 RTC_EXCLUSIVE_LOCKS_REQUIRED(send_mutex_); 169 170 Clock* const clock_; 171 Random random_ RTC_GUARDED_BY(send_mutex_); 172 173 const bool audio_configured_; 174 175 const uint32_t ssrc_; 176 const absl::optional<uint32_t> rtx_ssrc_; 177 const absl::optional<uint32_t> flexfec_ssrc_; 178 179 RtpPacketHistory* const packet_history_; 180 RtpPacketSender* const paced_sender_; 181 182 mutable Mutex send_mutex_; 183 184 bool sending_media_ RTC_GUARDED_BY(send_mutex_); 185 size_t max_packet_size_; 186 187 RtpHeaderExtensionMap rtp_header_extension_map_ RTC_GUARDED_BY(send_mutex_); 188 size_t max_media_packet_header_ RTC_GUARDED_BY(send_mutex_); 189 size_t max_padding_fec_packet_header_ RTC_GUARDED_BY(send_mutex_); 190 191 // RTP variables 192 uint32_t timestamp_offset_ RTC_GUARDED_BY(send_mutex_); 193 // RID value to send in the RID or RepairedRID header extension. 194 const std::string rid_; 195 // MID value to send in the MID header extension. 196 std::string mid_ RTC_GUARDED_BY(send_mutex_); 197 // Should we send MID/RID even when ACKed? (see below). 198 const bool always_send_mid_and_rid_; 199 // Track if any ACK has been received on the SSRC and RTX SSRC to indicate 200 // when to stop sending the MID and RID header extensions. 201 bool ssrc_has_acked_ RTC_GUARDED_BY(send_mutex_); 202 bool rtx_ssrc_has_acked_ RTC_GUARDED_BY(send_mutex_); 203 std::vector<uint32_t> csrcs_ RTC_GUARDED_BY(send_mutex_); 204 int rtx_ RTC_GUARDED_BY(send_mutex_); 205 // Mapping rtx_payload_type_map_[associated] = rtx. 206 std::map<int8_t, int8_t> rtx_payload_type_map_ RTC_GUARDED_BY(send_mutex_); 207 bool supports_bwe_extension_ RTC_GUARDED_BY(send_mutex_); 208 209 RateLimiter* const retransmission_rate_limiter_; 210 }; 211 212 } // namespace webrtc 213 214 #endif // MODULES_RTP_RTCP_SOURCE_RTP_SENDER_H_ 215