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