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 WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_ 12 #define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_ 13 14 #include <map> 15 #include <set> 16 #include <sstream> 17 #include <string> 18 #include <vector> 19 20 #include "webrtc/base/random.h" 21 #include "webrtc/base/scoped_ptr.h" 22 #include "webrtc/base/thread_annotations.h" 23 #include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h" 24 #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h" 25 #include "webrtc/modules/rtp_rtcp/include/receive_statistics.h" 26 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" 27 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h" 28 #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h" 29 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h" 30 #include "webrtc/modules/rtp_rtcp/source/tmmbr_help.h" 31 #include "webrtc/transport.h" 32 #include "webrtc/typedefs.h" 33 34 namespace webrtc { 35 36 class ModuleRtpRtcpImpl; 37 class RTCPReceiver; 38 39 class NACKStringBuilder { 40 public: 41 NACKStringBuilder(); 42 ~NACKStringBuilder(); 43 44 void PushNACK(uint16_t nack); 45 std::string GetResult(); 46 47 private: 48 std::ostringstream stream_; 49 int count_; 50 uint16_t prevNack_; 51 bool consecutive_; 52 }; 53 54 class RTCPSender { 55 public: 56 struct FeedbackState { 57 FeedbackState(); 58 59 uint8_t send_payload_type; 60 uint32_t frequency_hz; 61 uint32_t packets_sent; 62 size_t media_bytes_sent; 63 uint32_t send_bitrate; 64 65 uint32_t last_rr_ntp_secs; 66 uint32_t last_rr_ntp_frac; 67 uint32_t remote_sr; 68 69 bool has_last_xr_rr; 70 RtcpReceiveTimeInfo last_xr_rr; 71 72 // Used when generating TMMBR. 73 ModuleRtpRtcpImpl* module; 74 }; 75 76 RTCPSender(bool audio, 77 Clock* clock, 78 ReceiveStatistics* receive_statistics, 79 RtcpPacketTypeCounterObserver* packet_type_counter_observer, 80 Transport* outgoing_transport); 81 virtual ~RTCPSender(); 82 83 RtcpMode Status() const; 84 void SetRTCPStatus(RtcpMode method); 85 86 bool Sending() const; 87 int32_t SetSendingStatus(const FeedbackState& feedback_state, 88 bool enabled); // combine the functions 89 90 int32_t SetNackStatus(bool enable); 91 92 void SetStartTimestamp(uint32_t start_timestamp); 93 94 void SetLastRtpTime(uint32_t rtp_timestamp, int64_t capture_time_ms); 95 96 void SetSSRC(uint32_t ssrc); 97 98 void SetRemoteSSRC(uint32_t ssrc); 99 100 int32_t SetCNAME(const char* cName); 101 102 int32_t AddMixedCNAME(uint32_t SSRC, const char* c_name); 103 104 int32_t RemoveMixedCNAME(uint32_t SSRC); 105 106 int64_t SendTimeOfSendReport(uint32_t sendReport); 107 108 bool SendTimeOfXrRrReport(uint32_t mid_ntp, int64_t* time_ms) const; 109 110 bool TimeToSendRTCPReport(bool sendKeyframeBeforeRTP = false) const; 111 112 int32_t SendRTCP(const FeedbackState& feedback_state, 113 RTCPPacketType packetType, 114 int32_t nackSize = 0, 115 const uint16_t* nackList = 0, 116 bool repeat = false, 117 uint64_t pictureID = 0); 118 119 int32_t SendCompoundRTCP(const FeedbackState& feedback_state, 120 const std::set<RTCPPacketType>& packetTypes, 121 int32_t nackSize = 0, 122 const uint16_t* nackList = 0, 123 bool repeat = false, 124 uint64_t pictureID = 0); 125 126 bool REMB() const; 127 128 void SetREMBStatus(bool enable); 129 130 void SetREMBData(uint32_t bitrate, const std::vector<uint32_t>& ssrcs); 131 132 bool TMMBR() const; 133 134 void SetTMMBRStatus(bool enable); 135 136 int32_t SetTMMBN(const TMMBRSet* boundingSet, uint32_t maxBitrateKbit); 137 138 int32_t SetApplicationSpecificData(uint8_t subType, 139 uint32_t name, 140 const uint8_t* data, 141 uint16_t length); 142 int32_t SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric); 143 144 void SendRtcpXrReceiverReferenceTime(bool enable); 145 146 bool RtcpXrReceiverReferenceTime() const; 147 148 void SetCsrcs(const std::vector<uint32_t>& csrcs); 149 150 void SetTargetBitrate(unsigned int target_bitrate); 151 bool SendFeedbackPacket(const rtcp::TransportFeedback& packet); 152 153 private: 154 class RtcpContext; 155 156 // Determine which RTCP messages should be sent and setup flags. 157 void PrepareReport(const std::set<RTCPPacketType>& packetTypes, 158 const FeedbackState& feedback_state) 159 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 160 161 bool AddReportBlock(const FeedbackState& feedback_state, 162 uint32_t ssrc, 163 StreamStatistician* statistician) 164 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 165 166 rtc::scoped_ptr<rtcp::RtcpPacket> BuildSR(const RtcpContext& context) 167 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 168 rtc::scoped_ptr<rtcp::RtcpPacket> BuildRR(const RtcpContext& context) 169 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 170 rtc::scoped_ptr<rtcp::RtcpPacket> BuildSDES(const RtcpContext& context) 171 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 172 rtc::scoped_ptr<rtcp::RtcpPacket> BuildPLI(const RtcpContext& context) 173 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 174 rtc::scoped_ptr<rtcp::RtcpPacket> BuildREMB(const RtcpContext& context) 175 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 176 rtc::scoped_ptr<rtcp::RtcpPacket> BuildTMMBR(const RtcpContext& context) 177 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 178 rtc::scoped_ptr<rtcp::RtcpPacket> BuildTMMBN(const RtcpContext& context) 179 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 180 rtc::scoped_ptr<rtcp::RtcpPacket> BuildAPP(const RtcpContext& context) 181 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 182 rtc::scoped_ptr<rtcp::RtcpPacket> BuildVoIPMetric(const RtcpContext& context) 183 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 184 rtc::scoped_ptr<rtcp::RtcpPacket> BuildBYE(const RtcpContext& context) 185 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 186 rtc::scoped_ptr<rtcp::RtcpPacket> BuildFIR(const RtcpContext& context) 187 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 188 rtc::scoped_ptr<rtcp::RtcpPacket> BuildSLI(const RtcpContext& context) 189 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 190 rtc::scoped_ptr<rtcp::RtcpPacket> BuildRPSI(const RtcpContext& context) 191 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 192 rtc::scoped_ptr<rtcp::RtcpPacket> BuildNACK(const RtcpContext& context) 193 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 194 rtc::scoped_ptr<rtcp::RtcpPacket> BuildReceiverReferenceTime( 195 const RtcpContext& context) 196 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 197 rtc::scoped_ptr<rtcp::RtcpPacket> BuildDlrr(const RtcpContext& context) 198 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 199 200 private: 201 const bool audio_; 202 Clock* const clock_; 203 Random random_ GUARDED_BY(critical_section_rtcp_sender_); 204 RtcpMode method_ GUARDED_BY(critical_section_rtcp_sender_); 205 206 Transport* const transport_; 207 208 rtc::scoped_ptr<CriticalSectionWrapper> critical_section_rtcp_sender_; 209 bool using_nack_ GUARDED_BY(critical_section_rtcp_sender_); 210 bool sending_ GUARDED_BY(critical_section_rtcp_sender_); 211 bool remb_enabled_ GUARDED_BY(critical_section_rtcp_sender_); 212 213 int64_t next_time_to_send_rtcp_ GUARDED_BY(critical_section_rtcp_sender_); 214 215 uint32_t start_timestamp_ GUARDED_BY(critical_section_rtcp_sender_); 216 uint32_t last_rtp_timestamp_ GUARDED_BY(critical_section_rtcp_sender_); 217 int64_t last_frame_capture_time_ms_ GUARDED_BY(critical_section_rtcp_sender_); 218 uint32_t ssrc_ GUARDED_BY(critical_section_rtcp_sender_); 219 // SSRC that we receive on our RTP channel 220 uint32_t remote_ssrc_ GUARDED_BY(critical_section_rtcp_sender_); 221 std::string cname_ GUARDED_BY(critical_section_rtcp_sender_); 222 223 ReceiveStatistics* receive_statistics_ 224 GUARDED_BY(critical_section_rtcp_sender_); 225 std::map<uint32_t, rtcp::ReportBlock> report_blocks_ 226 GUARDED_BY(critical_section_rtcp_sender_); 227 std::map<uint32_t, std::string> csrc_cnames_ 228 GUARDED_BY(critical_section_rtcp_sender_); 229 230 // Sent 231 uint32_t last_send_report_[RTCP_NUMBER_OF_SR] GUARDED_BY( 232 critical_section_rtcp_sender_); // allow packet loss and RTT above 1 sec 233 int64_t last_rtcp_time_[RTCP_NUMBER_OF_SR] GUARDED_BY( 234 critical_section_rtcp_sender_); 235 236 // Sent XR receiver reference time report. 237 // <mid ntp (mid 32 bits of the 64 bits NTP timestamp), send time in ms>. 238 std::map<uint32_t, int64_t> last_xr_rr_ 239 GUARDED_BY(critical_section_rtcp_sender_); 240 241 // send CSRCs 242 std::vector<uint32_t> csrcs_ GUARDED_BY(critical_section_rtcp_sender_); 243 244 // Full intra request 245 uint8_t sequence_number_fir_ GUARDED_BY(critical_section_rtcp_sender_); 246 247 // REMB 248 uint32_t remb_bitrate_ GUARDED_BY(critical_section_rtcp_sender_); 249 std::vector<uint32_t> remb_ssrcs_ GUARDED_BY(critical_section_rtcp_sender_); 250 251 TMMBRHelp tmmbr_help_ GUARDED_BY(critical_section_rtcp_sender_); 252 uint32_t tmmbr_send_ GUARDED_BY(critical_section_rtcp_sender_); 253 uint32_t packet_oh_send_ GUARDED_BY(critical_section_rtcp_sender_); 254 255 // APP 256 uint8_t app_sub_type_ GUARDED_BY(critical_section_rtcp_sender_); 257 uint32_t app_name_ GUARDED_BY(critical_section_rtcp_sender_); 258 rtc::scoped_ptr<uint8_t[]> app_data_ 259 GUARDED_BY(critical_section_rtcp_sender_); 260 uint16_t app_length_ GUARDED_BY(critical_section_rtcp_sender_); 261 262 // True if sending of XR Receiver reference time report is enabled. 263 bool xr_send_receiver_reference_time_enabled_ 264 GUARDED_BY(critical_section_rtcp_sender_); 265 266 // XR VoIP metric 267 RTCPVoIPMetric xr_voip_metric_ GUARDED_BY(critical_section_rtcp_sender_); 268 269 RtcpPacketTypeCounterObserver* const packet_type_counter_observer_; 270 RtcpPacketTypeCounter packet_type_counter_ 271 GUARDED_BY(critical_section_rtcp_sender_); 272 273 RTCPUtility::NackStats nack_stats_ GUARDED_BY(critical_section_rtcp_sender_); 274 275 void SetFlag(RTCPPacketType type, bool is_volatile) 276 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 277 void SetFlags(const std::set<RTCPPacketType>& types, bool is_volatile) 278 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 279 bool IsFlagPresent(RTCPPacketType type) const 280 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 281 bool ConsumeFlag(RTCPPacketType type, bool forced = false) 282 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 283 bool AllVolatileFlagsConsumed() const 284 EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_); 285 struct ReportFlag { 286 ReportFlag(RTCPPacketType type, bool is_volatile) 287 : type(type), is_volatile(is_volatile) {} 288 bool operator<(const ReportFlag& flag) const { return type < flag.type; } 289 bool operator==(const ReportFlag& flag) const { return type == flag.type; } 290 const RTCPPacketType type; 291 const bool is_volatile; 292 }; 293 294 std::set<ReportFlag> report_flags_ GUARDED_BY(critical_section_rtcp_sender_); 295 296 typedef rtc::scoped_ptr<rtcp::RtcpPacket> (RTCPSender::*BuilderFunc)( 297 const RtcpContext&); 298 std::map<RTCPPacketType, BuilderFunc> builders_; 299 }; 300 } // namespace webrtc 301 302 #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_ 303