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_RECEIVER_H_ 12 #define MODULES_RTP_RTCP_SOURCE_RTCP_RECEIVER_H_ 13 14 #include <list> 15 #include <map> 16 #include <set> 17 #include <string> 18 #include <vector> 19 20 #include "api/array_view.h" 21 #include "modules/rtp_rtcp/include/report_block_data.h" 22 #include "modules/rtp_rtcp/include/rtcp_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/dlrr.h" 26 #include "modules/rtp_rtcp/source/rtp_rtcp_interface.h" 27 #include "rtc_base/synchronization/mutex.h" 28 #include "rtc_base/thread_annotations.h" 29 #include "system_wrappers/include/ntp_time.h" 30 31 namespace webrtc { 32 class VideoBitrateAllocationObserver; 33 namespace rtcp { 34 class CommonHeader; 35 class ReportBlock; 36 class Rrtr; 37 class TargetBitrate; 38 class TmmbItem; 39 } // namespace rtcp 40 41 class RTCPReceiver final { 42 public: 43 class ModuleRtpRtcp { 44 public: 45 virtual void SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) = 0; 46 virtual void OnRequestSendReport() = 0; 47 virtual void OnReceivedNack( 48 const std::vector<uint16_t>& nack_sequence_numbers) = 0; 49 virtual void OnReceivedRtcpReportBlocks( 50 const ReportBlockList& report_blocks) = 0; 51 52 protected: 53 virtual ~ModuleRtpRtcp() = default; 54 }; 55 56 RTCPReceiver(const RtpRtcpInterface::Configuration& config, 57 ModuleRtpRtcp* owner); 58 ~RTCPReceiver(); 59 IncomingPacket(const uint8_t * packet,size_t packet_size)60 void IncomingPacket(const uint8_t* packet, size_t packet_size) { 61 IncomingPacket(rtc::MakeArrayView(packet, packet_size)); 62 } 63 void IncomingPacket(rtc::ArrayView<const uint8_t> packet); 64 65 int64_t LastReceivedReportBlockMs() const; 66 67 void SetRemoteSSRC(uint32_t ssrc); 68 uint32_t RemoteSSRC() const; 69 70 // Get received NTP. 71 // The types for the arguments below derive from the specification: 72 // - `remote_sender_packet_count`: `RTCSentRtpStreamStats.packetsSent` [1] 73 // - `remote_sender_octet_count`: `RTCSentRtpStreamStats.bytesSent` [1] 74 // - `remote_sender_reports_count`: 75 // `RTCRemoteOutboundRtpStreamStats.reportsSent` [2] 76 // [1] https://www.w3.org/TR/webrtc-stats/#remoteoutboundrtpstats-dict* 77 // [2] https://www.w3.org/TR/webrtc-stats/#dom-rtcsentrtpstreamstats 78 bool NTP(uint32_t* received_ntp_secs, 79 uint32_t* received_ntp_frac, 80 uint32_t* rtcp_arrival_time_secs, 81 uint32_t* rtcp_arrival_time_frac, 82 uint32_t* rtcp_timestamp, 83 uint32_t* remote_sender_packet_count, 84 uint64_t* remote_sender_octet_count, 85 uint64_t* remote_sender_reports_count) const; 86 87 std::vector<rtcp::ReceiveTimeInfo> ConsumeReceivedXrReferenceTimeInfo(); 88 89 // Get rtt. 90 int32_t RTT(uint32_t remote_ssrc, 91 int64_t* last_rtt_ms, 92 int64_t* avg_rtt_ms, 93 int64_t* min_rtt_ms, 94 int64_t* max_rtt_ms) const; 95 96 bool GetAndResetXrRrRtt(int64_t* rtt_ms); 97 98 // Called once per second on the worker thread to do rtt calculations. 99 // Returns an optional rtt value if one is available. 100 absl::optional<TimeDelta> OnPeriodicRttUpdate(Timestamp newer_than, 101 bool sending); 102 103 // A snapshot of Report Blocks with additional data of interest to statistics. 104 // Within this list, the sender-source SSRC pair is unique and per-pair the 105 // ReportBlockData represents the latest Report Block that was received for 106 // that pair. 107 std::vector<ReportBlockData> GetLatestReportBlockData() const; 108 109 // Returns true if we haven't received an RTCP RR for several RTCP 110 // intervals, but only triggers true once. 111 bool RtcpRrTimeout(); 112 113 // Returns true if we haven't received an RTCP RR telling the receive side 114 // has not received RTP packets for too long, i.e. extended highest sequence 115 // number hasn't increased for several RTCP intervals. The function only 116 // returns true once until a new RR is received. 117 bool RtcpRrSequenceNumberTimeout(); 118 119 std::vector<rtcp::TmmbItem> TmmbrReceived(); 120 // Return true if new bandwidth should be set. 121 bool UpdateTmmbrTimers(); 122 std::vector<rtcp::TmmbItem> BoundingSet(bool* tmmbr_owner); 123 // Set new bandwidth and notify remote clients about it. 124 void NotifyTmmbrUpdated(); 125 126 private: 127 struct PacketInformation; 128 struct TmmbrInformation; 129 struct RrtrInformation; 130 struct LastFirStatus; 131 // RTCP report blocks mapped by remote SSRC. 132 using ReportBlockDataMap = std::map<uint32_t, ReportBlockData>; 133 // RTCP report blocks map mapped by source SSRC. 134 using ReportBlockMap = std::map<uint32_t, ReportBlockDataMap>; 135 136 bool ParseCompoundPacket(rtc::ArrayView<const uint8_t> packet, 137 PacketInformation* packet_information); 138 139 void TriggerCallbacksFromRtcpPacket( 140 const PacketInformation& packet_information); 141 142 TmmbrInformation* FindOrCreateTmmbrInfo(uint32_t remote_ssrc) 143 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 144 // Update TmmbrInformation (if present) is alive. 145 void UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc) 146 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 147 TmmbrInformation* GetTmmbrInformation(uint32_t remote_ssrc) 148 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 149 150 void HandleSenderReport(const rtcp::CommonHeader& rtcp_block, 151 PacketInformation* packet_information) 152 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 153 154 void HandleReceiverReport(const rtcp::CommonHeader& rtcp_block, 155 PacketInformation* packet_information) 156 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 157 158 void HandleReportBlock(const rtcp::ReportBlock& report_block, 159 PacketInformation* packet_information, 160 uint32_t remote_ssrc) 161 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 162 163 void HandleSdes(const rtcp::CommonHeader& rtcp_block, 164 PacketInformation* packet_information) 165 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 166 167 void HandleXr(const rtcp::CommonHeader& rtcp_block, 168 PacketInformation* packet_information) 169 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 170 171 void HandleXrReceiveReferenceTime(uint32_t sender_ssrc, 172 const rtcp::Rrtr& rrtr) 173 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 174 175 void HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) 176 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 177 178 void HandleXrTargetBitrate(uint32_t ssrc, 179 const rtcp::TargetBitrate& target_bitrate, 180 PacketInformation* packet_information) 181 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 182 183 void HandleNack(const rtcp::CommonHeader& rtcp_block, 184 PacketInformation* packet_information) 185 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 186 187 void HandleApp(const rtcp::CommonHeader& rtcp_block, 188 PacketInformation* packet_information) 189 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 190 191 void HandleBye(const rtcp::CommonHeader& rtcp_block) 192 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 193 194 void HandlePli(const rtcp::CommonHeader& rtcp_block, 195 PacketInformation* packet_information) 196 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 197 198 void HandlePsfbApp(const rtcp::CommonHeader& rtcp_block, 199 PacketInformation* packet_information) 200 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 201 202 void HandleTmmbr(const rtcp::CommonHeader& rtcp_block, 203 PacketInformation* packet_information) 204 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 205 206 void HandleTmmbn(const rtcp::CommonHeader& rtcp_block, 207 PacketInformation* packet_information) 208 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 209 210 void HandleSrReq(const rtcp::CommonHeader& rtcp_block, 211 PacketInformation* packet_information) 212 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 213 214 void HandleFir(const rtcp::CommonHeader& rtcp_block, 215 PacketInformation* packet_information) 216 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 217 218 void HandleTransportFeedback(const rtcp::CommonHeader& rtcp_block, 219 PacketInformation* packet_information) 220 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 221 222 bool RtcpRrTimeoutLocked(Timestamp now) 223 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 224 225 bool RtcpRrSequenceNumberTimeoutLocked(Timestamp now) 226 RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_); 227 228 Clock* const clock_; 229 const bool receiver_only_; 230 ModuleRtpRtcp* const rtp_rtcp_; 231 const uint32_t main_ssrc_; 232 const std::set<uint32_t> registered_ssrcs_; 233 234 RtcpBandwidthObserver* const rtcp_bandwidth_observer_; 235 RtcpIntraFrameObserver* const rtcp_intra_frame_observer_; 236 RtcpLossNotificationObserver* const rtcp_loss_notification_observer_; 237 NetworkStateEstimateObserver* const network_state_estimate_observer_; 238 TransportFeedbackObserver* const transport_feedback_observer_; 239 VideoBitrateAllocationObserver* const bitrate_allocation_observer_; 240 const TimeDelta report_interval_; 241 242 mutable Mutex rtcp_receiver_lock_; 243 uint32_t remote_ssrc_ RTC_GUARDED_BY(rtcp_receiver_lock_); 244 245 // Received sender report. 246 NtpTime remote_sender_ntp_time_ RTC_GUARDED_BY(rtcp_receiver_lock_); 247 uint32_t remote_sender_rtp_time_ RTC_GUARDED_BY(rtcp_receiver_lock_); 248 // When did we receive the last send report. 249 NtpTime last_received_sr_ntp_ RTC_GUARDED_BY(rtcp_receiver_lock_); 250 uint32_t remote_sender_packet_count_ RTC_GUARDED_BY(rtcp_receiver_lock_); 251 uint64_t remote_sender_octet_count_ RTC_GUARDED_BY(rtcp_receiver_lock_); 252 uint64_t remote_sender_reports_count_ RTC_GUARDED_BY(rtcp_receiver_lock_); 253 254 // Received RRTR information in ascending receive time order. 255 std::list<RrtrInformation> received_rrtrs_ 256 RTC_GUARDED_BY(rtcp_receiver_lock_); 257 // Received RRTR information mapped by remote ssrc. 258 std::map<uint32_t, std::list<RrtrInformation>::iterator> 259 received_rrtrs_ssrc_it_ RTC_GUARDED_BY(rtcp_receiver_lock_); 260 261 // Estimated rtt, zero when there is no valid estimate. 262 const bool xr_rrtr_status_; 263 int64_t xr_rr_rtt_ms_; 264 265 int64_t oldest_tmmbr_info_ms_ RTC_GUARDED_BY(rtcp_receiver_lock_); 266 // Mapped by remote ssrc. 267 std::map<uint32_t, TmmbrInformation> tmmbr_infos_ 268 RTC_GUARDED_BY(rtcp_receiver_lock_); 269 270 ReportBlockMap received_report_blocks_ RTC_GUARDED_BY(rtcp_receiver_lock_); 271 std::map<uint32_t, LastFirStatus> last_fir_ 272 RTC_GUARDED_BY(rtcp_receiver_lock_); 273 274 // The last time we received an RTCP Report block for this module. 275 Timestamp last_received_rb_ RTC_GUARDED_BY(rtcp_receiver_lock_) = 276 Timestamp::PlusInfinity(); 277 278 // The time we last received an RTCP RR telling we have successfully 279 // delivered RTP packet to the remote side. 280 Timestamp last_increased_sequence_number_ = Timestamp::PlusInfinity(); 281 282 RtcpStatisticsCallback* const stats_callback_; 283 RtcpCnameCallback* const cname_callback_; 284 // TODO(hbos): Remove RtcpStatisticsCallback in favor of 285 // ReportBlockDataObserver; the ReportBlockData contains a superset of the 286 // RtcpStatistics data. 287 ReportBlockDataObserver* const report_block_data_observer_; 288 289 RtcpPacketTypeCounterObserver* const packet_type_counter_observer_; 290 RtcpPacketTypeCounter packet_type_counter_; 291 292 RtcpNackStats nack_stats_; 293 294 size_t num_skipped_packets_; 295 int64_t last_skipped_packets_warning_ms_; 296 }; 297 } // namespace webrtc 298 #endif // MODULES_RTP_RTCP_SOURCE_RTCP_RECEIVER_H_ 299