rtcp_sender.h (12678B)
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 <cstddef> 15 #include <cstdint> 16 #include <map> 17 #include <memory> 18 #include <optional> 19 #include <set> 20 #include <string> 21 #include <vector> 22 23 #include "absl/functional/any_invocable.h" 24 #include "absl/strings/string_view.h" 25 #include "api/array_view.h" 26 #include "api/call/transport.h" 27 #include "api/environment/environment.h" 28 #include "api/rtp_headers.h" 29 #include "api/units/data_rate.h" 30 #include "api/units/time_delta.h" 31 #include "api/units/timestamp.h" 32 #include "api/video/video_bitrate_allocation.h" 33 #include "modules/rtp_rtcp/include/receive_statistics.h" 34 #include "modules/rtp_rtcp/include/rtcp_statistics.h" 35 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" 36 #include "modules/rtp_rtcp/source/rtcp_nack_stats.h" 37 #include "modules/rtp_rtcp/source/rtcp_packet.h" 38 #include "modules/rtp_rtcp/source/rtcp_packet/dlrr.h" 39 #include "modules/rtp_rtcp/source/rtcp_packet/loss_notification.h" 40 #include "modules/rtp_rtcp/source/rtcp_packet/report_block.h" 41 #include "modules/rtp_rtcp/source/rtcp_packet/tmmb_item.h" 42 #include "modules/rtp_rtcp/source/rtcp_receiver.h" 43 #include "rtc_base/random.h" 44 #include "rtc_base/synchronization/mutex.h" 45 #include "rtc_base/thread_annotations.h" 46 #include "system_wrappers/include/ntp_time.h" 47 48 namespace webrtc { 49 50 class RTCPSender final { 51 public: 52 struct Configuration { 53 // True for a audio version of the RTP/RTCP module object false will create 54 // a video version. 55 bool audio = false; 56 // SSRCs for media and retransmission, respectively. 57 // FlexFec SSRC is fetched from `flexfec_sender`. 58 uint32_t local_media_ssrc = 0; 59 60 // Transport object that will be called when packets are ready to be sent 61 // out on the network. 62 Transport* outgoing_transport = nullptr; 63 // Estimate RTT as non-sender as described in 64 // https://tools.ietf.org/html/rfc3611#section-4.4 and #section-4.5 65 bool non_sender_rtt_measurement = false; 66 67 // Mandatory callback which is used by RTCPSender to schedule the next time 68 // to evaluate if RTCP should be sent by means of 69 // TimeToSendRTCPReport/SendRTCP. The RTCPSender client still needs to call 70 // TimeToSendRTCPReport/SendRTCP to actually get RTCP sent. 71 absl::AnyInvocable<void(TimeDelta)> schedule_next_rtcp_send_evaluation; 72 73 TimeDelta rtcp_report_interval; 74 ReceiveStatisticsProvider* receive_statistics = nullptr; 75 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer = nullptr; 76 }; 77 struct FeedbackState { 78 FeedbackState(); 79 FeedbackState(const FeedbackState&); 80 FeedbackState(FeedbackState&&); 81 82 ~FeedbackState(); 83 84 uint32_t packets_sent; 85 size_t media_bytes_sent; 86 DataRate send_bitrate; 87 88 uint32_t remote_sr; 89 NtpTime last_rr; 90 91 std::vector<rtcp::ReceiveTimeInfo> last_xr_rtis; 92 93 // Used when generating TMMBR. 94 RTCPReceiver* receiver; 95 }; 96 97 RTCPSender(const Environment& env, Configuration config); 98 99 RTCPSender() = delete; 100 RTCPSender(const RTCPSender&) = delete; 101 RTCPSender& operator=(const RTCPSender&) = delete; 102 103 ~RTCPSender(); 104 105 RtcpMode Status() const RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 106 void SetRTCPStatus(RtcpMode method) RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 107 108 bool Sending() const RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 109 void SetSendingStatus(const FeedbackState& feedback_state, 110 bool enabled) 111 RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); // combine the functions 112 113 void SetNonSenderRttMeasurement(bool enabled) 114 RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 115 116 void SetTimestampOffset(uint32_t timestamp_offset) 117 RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 118 119 void SetLastRtpTime(uint32_t rtp_timestamp, 120 std::optional<Timestamp> capture_time, 121 std::optional<int8_t> payload_type) 122 RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 123 124 void SetRtpClockRate(int8_t payload_type, int rtp_clock_rate_hz) 125 RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 126 127 uint32_t SSRC() const; 128 void SetSsrc(uint32_t ssrc); 129 130 void SetRemoteSSRC(uint32_t ssrc) RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 131 132 int32_t SetCNAME(absl::string_view cName) 133 RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 134 135 bool TimeToSendRTCPReport(bool send_keyframe_before_rtp = false) const 136 RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 137 138 int32_t SendRTCP(const FeedbackState& feedback_state, 139 RTCPPacketType packetType, 140 ArrayView<const uint16_t> nacks = {}) 141 RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 142 143 int32_t SendLossNotification(const FeedbackState& feedback_state, 144 uint16_t last_decoded_seq_num, 145 uint16_t last_received_seq_num, 146 bool decodability_flag, 147 bool buffering_allowed) 148 RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 149 150 void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) 151 RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 152 153 void UnsetRemb() RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 154 155 bool TMMBR() const RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 156 157 void SetMaxRtpPacketSize(size_t max_packet_size) 158 RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 159 160 void SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) 161 RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 162 163 void SetCsrcs(const std::vector<uint32_t>& csrcs) 164 RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 165 166 void SetTargetBitrate(unsigned int target_bitrate) 167 RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 168 void SetVideoBitrateAllocation(const VideoBitrateAllocation& bitrate) 169 RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 170 void SendCombinedRtcpPacket( 171 std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) 172 RTC_LOCKS_EXCLUDED(mutex_rtcp_sender_); 173 174 private: 175 class RtcpContext; 176 class PacketSender; 177 178 std::optional<int32_t> ComputeCompoundRTCPPacket( 179 const FeedbackState& feedback_state, 180 RTCPPacketType packet_type, 181 ArrayView<const uint16_t> nacks, 182 PacketSender& sender) RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 183 184 TimeDelta ComputeTimeUntilNextReport(DataRate send_bitrate) 185 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 186 187 // Determine which RTCP messages should be sent and setup flags. 188 void PrepareReport(const FeedbackState& feedback_state) 189 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 190 191 std::vector<rtcp::ReportBlock> CreateReportBlocks( 192 const FeedbackState& feedback_state) 193 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 194 195 void BuildSR(const RtcpContext& context, PacketSender& sender) 196 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 197 void BuildRR(const RtcpContext& context, PacketSender& sender) 198 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 199 void BuildSDES(const RtcpContext& context, PacketSender& sender) 200 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 201 void BuildPLI(const RtcpContext& context, PacketSender& sender) 202 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 203 void BuildREMB(const RtcpContext& context, PacketSender& sender) 204 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 205 void BuildTMMBR(const RtcpContext& context, PacketSender& sender) 206 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 207 void BuildTMMBN(const RtcpContext& context, PacketSender& sender) 208 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 209 void BuildAPP(const RtcpContext& context, PacketSender& sender) 210 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 211 void BuildLossNotification(const RtcpContext& context, PacketSender& sender) 212 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 213 void BuildExtendedReports(const RtcpContext& context, PacketSender& sender) 214 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 215 void BuildBYE(const RtcpContext& context, PacketSender& sender) 216 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 217 void BuildFIR(const RtcpContext& context, PacketSender& sender) 218 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 219 void BuildNACK(const RtcpContext& context, PacketSender& sender) 220 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 221 222 // `duration` being TimeDelta::Zero() means schedule immediately. 223 void SetNextRtcpSendEvaluationDuration(TimeDelta duration) 224 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 225 226 const Environment env_; 227 const bool audio_; 228 // TODO(bugs.webrtc.org/11581): `mutex_rtcp_sender_` shouldn't be required if 229 // we consistently run network related operations on the network thread. 230 // This is currently not possible due to callbacks from the process thread in 231 // ModuleRtpRtcpImpl2. 232 uint32_t ssrc_ RTC_GUARDED_BY(mutex_rtcp_sender_); 233 Random random_ RTC_GUARDED_BY(mutex_rtcp_sender_); 234 RtcpMode method_ RTC_GUARDED_BY(mutex_rtcp_sender_); 235 236 Transport* const transport_; 237 238 const TimeDelta report_interval_; 239 absl::AnyInvocable<void(TimeDelta)> schedule_next_rtcp_send_evaluation_; 240 241 mutable Mutex mutex_rtcp_sender_; 242 bool sending_ RTC_GUARDED_BY(mutex_rtcp_sender_); 243 244 std::optional<Timestamp> next_time_to_send_rtcp_ 245 RTC_GUARDED_BY(mutex_rtcp_sender_); 246 247 uint32_t timestamp_offset_ RTC_GUARDED_BY(mutex_rtcp_sender_); 248 uint32_t last_rtp_timestamp_ RTC_GUARDED_BY(mutex_rtcp_sender_); 249 std::optional<Timestamp> last_frame_capture_time_ 250 RTC_GUARDED_BY(mutex_rtcp_sender_); 251 // SSRC that we receive on our RTP channel 252 uint32_t remote_ssrc_ RTC_GUARDED_BY(mutex_rtcp_sender_); 253 std::string cname_ RTC_GUARDED_BY(mutex_rtcp_sender_); 254 255 ReceiveStatisticsProvider* receive_statistics_ 256 RTC_GUARDED_BY(mutex_rtcp_sender_); 257 258 // send CSRCs 259 std::vector<uint32_t> csrcs_ RTC_GUARDED_BY(mutex_rtcp_sender_); 260 261 // Full intra request 262 uint8_t sequence_number_fir_ RTC_GUARDED_BY(mutex_rtcp_sender_); 263 264 rtcp::LossNotification loss_notification_ RTC_GUARDED_BY(mutex_rtcp_sender_); 265 266 // REMB 267 int64_t remb_bitrate_ RTC_GUARDED_BY(mutex_rtcp_sender_); 268 std::vector<uint32_t> remb_ssrcs_ RTC_GUARDED_BY(mutex_rtcp_sender_); 269 270 std::vector<rtcp::TmmbItem> tmmbn_to_send_ RTC_GUARDED_BY(mutex_rtcp_sender_); 271 uint32_t tmmbr_send_bps_ RTC_GUARDED_BY(mutex_rtcp_sender_); 272 uint32_t packet_oh_send_ RTC_GUARDED_BY(mutex_rtcp_sender_); 273 size_t max_packet_size_ RTC_GUARDED_BY(mutex_rtcp_sender_); 274 275 // True if sending of XR Receiver reference time report is enabled. 276 bool xr_send_receiver_reference_time_enabled_ 277 RTC_GUARDED_BY(mutex_rtcp_sender_); 278 279 RtcpPacketTypeCounterObserver* const packet_type_counter_observer_; 280 RtcpPacketTypeCounter packet_type_counter_ RTC_GUARDED_BY(mutex_rtcp_sender_); 281 282 RtcpNackStats nack_stats_ RTC_GUARDED_BY(mutex_rtcp_sender_); 283 284 VideoBitrateAllocation video_bitrate_allocation_ 285 RTC_GUARDED_BY(mutex_rtcp_sender_); 286 bool send_video_bitrate_allocation_ RTC_GUARDED_BY(mutex_rtcp_sender_); 287 288 std::map<int8_t, int> rtp_clock_rates_khz_ RTC_GUARDED_BY(mutex_rtcp_sender_); 289 int8_t last_payload_type_ RTC_GUARDED_BY(mutex_rtcp_sender_); 290 291 std::optional<VideoBitrateAllocation> CheckAndUpdateLayerStructure( 292 const VideoBitrateAllocation& bitrate) const 293 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 294 295 void SetFlag(uint32_t type, bool is_volatile) 296 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 297 bool IsFlagPresent(uint32_t type) const 298 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 299 bool ConsumeFlag(uint32_t type, bool forced = false) 300 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 301 bool AllVolatileFlagsConsumed() const 302 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_rtcp_sender_); 303 struct ReportFlag { 304 ReportFlag(uint32_t type, bool is_volatile) 305 : type(type), is_volatile(is_volatile) {} 306 bool operator<(const ReportFlag& flag) const { return type < flag.type; } 307 bool operator==(const ReportFlag& flag) const { return type == flag.type; } 308 const uint32_t type; 309 const bool is_volatile; 310 }; 311 312 std::set<ReportFlag> report_flags_ RTC_GUARDED_BY(mutex_rtcp_sender_); 313 314 typedef void (RTCPSender::*BuilderFunc)(const RtcpContext&, PacketSender&); 315 // Map from RTCPPacketType to builder. 316 std::map<uint32_t, BuilderFunc> builders_; 317 }; 318 } // namespace webrtc 319 320 #endif // MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_