rtcp_receiver.h 11 KB

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