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