rtcp_sender.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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_RTCP_SENDER_H_
  11. #define MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_
  12. #include <map>
  13. #include <memory>
  14. #include <set>
  15. #include <string>
  16. #include <vector>
  17. #include "absl/types/optional.h"
  18. #include "api/call/transport.h"
  19. #include "api/video/video_bitrate_allocation.h"
  20. #include "modules/remote_bitrate_estimator/include/bwe_defines.h"
  21. #include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
  22. #include "modules/rtp_rtcp/include/receive_statistics.h"
  23. #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
  24. #include "modules/rtp_rtcp/source/rtcp_nack_stats.h"
  25. #include "modules/rtp_rtcp/source/rtcp_packet.h"
  26. #include "modules/rtp_rtcp/source/rtcp_packet/compound_packet.h"
  27. #include "modules/rtp_rtcp/source/rtcp_packet/dlrr.h"
  28. #include "modules/rtp_rtcp/source/rtcp_packet/report_block.h"
  29. #include "modules/rtp_rtcp/source/rtcp_packet/tmmb_item.h"
  30. #include "modules/rtp_rtcp/source/rtp_rtcp_interface.h"
  31. #include "rtc_base/random.h"
  32. #include "rtc_base/synchronization/mutex.h"
  33. #include "rtc_base/thread_annotations.h"
  34. namespace webrtc {
  35. class RTCPReceiver;
  36. class RtcEventLog;
  37. class RTCPSender final {
  38. public:
  39. struct FeedbackState {
  40. FeedbackState();
  41. FeedbackState(const FeedbackState&);
  42. FeedbackState(FeedbackState&&);
  43. ~FeedbackState();
  44. uint32_t packets_sent;
  45. size_t media_bytes_sent;
  46. uint32_t send_bitrate;
  47. uint32_t last_rr_ntp_secs;
  48. uint32_t last_rr_ntp_frac;
  49. uint32_t remote_sr;
  50. std::vector<rtcp::ReceiveTimeInfo> last_xr_rtis;
  51. // Used when generating TMMBR.
  52. RTCPReceiver* receiver;
  53. };
  54. explicit RTCPSender(const RtpRtcpInterface::Configuration& config);
  55. RTCPSender() = delete;
  56. RTCPSender(const RTCPSender&) = delete;
  57. RTCPSender& operator=(const RTCPSender&) = delete;
  58. virtual ~RTCPSender();
  59. RtcpMode Status() const RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  60. void SetRTCPStatus(RtcpMode method) RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  61. bool Sending() const RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  62. int32_t SetSendingStatus(const FeedbackState& feedback_state,
  63. bool enabled)
  64. RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); // combine the functions
  65. int32_t SetNackStatus(bool enable) RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  66. void SetTimestampOffset(uint32_t timestamp_offset)
  67. RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  68. // TODO(bugs.webrtc.org/6458): Remove default parameter value when all the
  69. // depending projects are updated to correctly set payload type.
  70. void SetLastRtpTime(uint32_t rtp_timestamp,
  71. int64_t capture_time_ms,
  72. int8_t payload_type = -1)
  73. RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  74. void SetRtpClockRate(int8_t payload_type, int rtp_clock_rate_hz)
  75. RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  76. uint32_t SSRC() const { return ssrc_; }
  77. void SetRemoteSSRC(uint32_t ssrc) RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  78. int32_t SetCNAME(const char* cName) RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  79. int32_t AddMixedCNAME(uint32_t SSRC, const char* c_name)
  80. RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  81. int32_t RemoveMixedCNAME(uint32_t SSRC)
  82. RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  83. bool TimeToSendRTCPReport(bool sendKeyframeBeforeRTP = false) const
  84. RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  85. int32_t SendRTCP(const FeedbackState& feedback_state,
  86. RTCPPacketType packetType,
  87. int32_t nackSize = 0,
  88. const uint16_t* nackList = 0)
  89. RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  90. int32_t SendCompoundRTCP(const FeedbackState& feedback_state,
  91. const std::set<RTCPPacketType>& packetTypes,
  92. int32_t nackSize = 0,
  93. const uint16_t* nackList = nullptr)
  94. RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  95. int32_t SendLossNotification(const FeedbackState& feedback_state,
  96. uint16_t last_decoded_seq_num,
  97. uint16_t last_received_seq_num,
  98. bool decodability_flag,
  99. bool buffering_allowed)
  100. RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  101. void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs)
  102. RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  103. void UnsetRemb() RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  104. bool TMMBR() const RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  105. void SetTMMBRStatus(bool enable) RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  106. void SetMaxRtpPacketSize(size_t max_packet_size)
  107. RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  108. void SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set)
  109. RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  110. void SendRtcpXrReceiverReferenceTime(bool enable)
  111. RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  112. bool RtcpXrReceiverReferenceTime() const
  113. RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  114. void SetCsrcs(const std::vector<uint32_t>& csrcs)
  115. RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  116. void SetTargetBitrate(unsigned int target_bitrate)
  117. RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  118. void SetVideoBitrateAllocation(const VideoBitrateAllocation& bitrate)
  119. RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  120. void SendCombinedRtcpPacket(
  121. std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets)
  122. RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_);
  123. private:
  124. class RtcpContext;
  125. int32_t SendCompoundRTCPLocked(const FeedbackState& feedback_state,
  126. const std::set<RTCPPacketType>& packet_types,
  127. int32_t nack_size,
  128. const uint16_t* nack_list)
  129. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  130. absl::optional<int32_t> ComputeCompoundRTCPPacket(
  131. const FeedbackState& feedback_state,
  132. const std::set<RTCPPacketType>& packet_types,
  133. int32_t nack_size,
  134. const uint16_t* nack_list,
  135. rtcp::CompoundPacket* out_packet)
  136. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  137. // Determine which RTCP messages should be sent and setup flags.
  138. void PrepareReport(const FeedbackState& feedback_state)
  139. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  140. std::vector<rtcp::ReportBlock> CreateReportBlocks(
  141. const FeedbackState& feedback_state)
  142. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  143. std::unique_ptr<rtcp::RtcpPacket> BuildSR(const RtcpContext& context)
  144. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  145. std::unique_ptr<rtcp::RtcpPacket> BuildRR(const RtcpContext& context)
  146. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  147. std::unique_ptr<rtcp::RtcpPacket> BuildSDES(const RtcpContext& context)
  148. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  149. std::unique_ptr<rtcp::RtcpPacket> BuildPLI(const RtcpContext& context)
  150. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  151. std::unique_ptr<rtcp::RtcpPacket> BuildREMB(const RtcpContext& context)
  152. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  153. std::unique_ptr<rtcp::RtcpPacket> BuildTMMBR(const RtcpContext& context)
  154. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  155. std::unique_ptr<rtcp::RtcpPacket> BuildTMMBN(const RtcpContext& context)
  156. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  157. std::unique_ptr<rtcp::RtcpPacket> BuildAPP(const RtcpContext& context)
  158. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  159. std::unique_ptr<rtcp::RtcpPacket> BuildLossNotification(
  160. const RtcpContext& context)
  161. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  162. std::unique_ptr<rtcp::RtcpPacket> BuildExtendedReports(
  163. const RtcpContext& context)
  164. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  165. std::unique_ptr<rtcp::RtcpPacket> BuildBYE(const RtcpContext& context)
  166. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  167. std::unique_ptr<rtcp::RtcpPacket> BuildFIR(const RtcpContext& context)
  168. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  169. std::unique_ptr<rtcp::RtcpPacket> BuildNACK(const RtcpContext& context)
  170. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  171. private:
  172. const bool audio_;
  173. const uint32_t ssrc_;
  174. Clock* const clock_;
  175. Random random_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  176. RtcpMode method_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  177. RtcEventLog* const event_log_;
  178. Transport* const transport_;
  179. const int report_interval_ms_;
  180. mutable Mutex mutex_rtcp_sender_;
  181. bool sending_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  182. int64_t next_time_to_send_rtcp_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  183. uint32_t timestamp_offset_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  184. uint32_t last_rtp_timestamp_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  185. int64_t last_frame_capture_time_ms_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  186. // SSRC that we receive on our RTP channel
  187. uint32_t remote_ssrc_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  188. std::string cname_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  189. ReceiveStatisticsProvider* receive_statistics_
  190. RTC_GUARDED_BY(mutex_rtcp_sender_);
  191. std::map<uint32_t, std::string> csrc_cnames_
  192. RTC_GUARDED_BY(mutex_rtcp_sender_);
  193. // send CSRCs
  194. std::vector<uint32_t> csrcs_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  195. // Full intra request
  196. uint8_t sequence_number_fir_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  197. // Loss Notification
  198. struct LossNotificationState {
  199. uint16_t last_decoded_seq_num;
  200. uint16_t last_received_seq_num;
  201. bool decodability_flag;
  202. };
  203. LossNotificationState loss_notification_state_
  204. RTC_GUARDED_BY(mutex_rtcp_sender_);
  205. // REMB
  206. int64_t remb_bitrate_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  207. std::vector<uint32_t> remb_ssrcs_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  208. std::vector<rtcp::TmmbItem> tmmbn_to_send_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  209. uint32_t tmmbr_send_bps_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  210. uint32_t packet_oh_send_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  211. size_t max_packet_size_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  212. // True if sending of XR Receiver reference time report is enabled.
  213. bool xr_send_receiver_reference_time_enabled_
  214. RTC_GUARDED_BY(mutex_rtcp_sender_);
  215. RtcpPacketTypeCounterObserver* const packet_type_counter_observer_;
  216. RtcpPacketTypeCounter packet_type_counter_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  217. RtcpNackStats nack_stats_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  218. VideoBitrateAllocation video_bitrate_allocation_
  219. RTC_GUARDED_BY(mutex_rtcp_sender_);
  220. bool send_video_bitrate_allocation_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  221. std::map<int8_t, int> rtp_clock_rates_khz_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  222. int8_t last_payload_type_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  223. absl::optional<VideoBitrateAllocation> CheckAndUpdateLayerStructure(
  224. const VideoBitrateAllocation& bitrate) const
  225. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  226. void SetFlag(uint32_t type, bool is_volatile)
  227. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  228. void SetFlags(const std::set<RTCPPacketType>& types, bool is_volatile)
  229. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  230. bool IsFlagPresent(uint32_t type) const
  231. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  232. bool ConsumeFlag(uint32_t type, bool forced = false)
  233. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  234. bool AllVolatileFlagsConsumed() const
  235. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_);
  236. struct ReportFlag {
  237. ReportFlag(uint32_t type, bool is_volatile)
  238. : type(type), is_volatile(is_volatile) {}
  239. bool operator<(const ReportFlag& flag) const { return type < flag.type; }
  240. bool operator==(const ReportFlag& flag) const { return type == flag.type; }
  241. const uint32_t type;
  242. const bool is_volatile;
  243. };
  244. std::set<ReportFlag> report_flags_ RTC_GUARDED_BY(mutex_rtcp_sender_);
  245. typedef std::unique_ptr<rtcp::RtcpPacket> (RTCPSender::*BuilderFunc)(
  246. const RtcpContext&);
  247. // Map from RTCPPacketType to builder.
  248. std::map<uint32_t, BuilderFunc> builders_;
  249. };
  250. } // namespace webrtc
  251. #endif // MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_