rtp_sender.h 9.1 KB

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