• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 cname.
71   int32_t CNAME(uint32_t remote_ssrc, char cname[RTCP_CNAME_SIZE]) const;
72 
73   // Get received NTP.
74   bool NTP(uint32_t* received_ntp_secs,
75            uint32_t* received_ntp_frac,
76            uint32_t* rtcp_arrival_time_secs,
77            uint32_t* rtcp_arrival_time_frac,
78            uint32_t* rtcp_timestamp) const;
79 
80   std::vector<rtcp::ReceiveTimeInfo> ConsumeReceivedXrReferenceTimeInfo();
81 
82   // Get rtt.
83   int32_t RTT(uint32_t remote_ssrc,
84               int64_t* last_rtt_ms,
85               int64_t* avg_rtt_ms,
86               int64_t* min_rtt_ms,
87               int64_t* max_rtt_ms) const;
88 
89   void SetRtcpXrRrtrStatus(bool enable);
90   bool GetAndResetXrRrRtt(int64_t* rtt_ms);
91 
92   // Called once per second on the worker thread to do rtt calculations.
93   // Returns an optional rtt value if one is available.
94   absl::optional<TimeDelta> OnPeriodicRttUpdate(Timestamp newer_than,
95                                                 bool sending);
96 
97   // Get statistics.
98   int32_t StatisticsReceived(std::vector<RTCPReportBlock>* receiveBlocks) const;
99   // A snapshot of Report Blocks with additional data of interest to statistics.
100   // Within this list, the sender-source SSRC pair is unique and per-pair the
101   // ReportBlockData represents the latest Report Block that was received for
102   // that pair.
103   std::vector<ReportBlockData> GetLatestReportBlockData() const;
104 
105   // Returns true if we haven't received an RTCP RR for several RTCP
106   // intervals, but only triggers true once.
107   bool RtcpRrTimeout();
108 
109   // Returns true if we haven't received an RTCP RR telling the receive side
110   // has not received RTP packets for too long, i.e. extended highest sequence
111   // number hasn't increased for several RTCP intervals. The function only
112   // returns true once until a new RR is received.
113   bool RtcpRrSequenceNumberTimeout();
114 
115   std::vector<rtcp::TmmbItem> TmmbrReceived();
116   // Return true if new bandwidth should be set.
117   bool UpdateTmmbrTimers();
118   std::vector<rtcp::TmmbItem> BoundingSet(bool* tmmbr_owner);
119   // Set new bandwidth and notify remote clients about it.
120   void NotifyTmmbrUpdated();
121 
122  private:
123   struct PacketInformation;
124   struct TmmbrInformation;
125   struct RrtrInformation;
126   struct LastFirStatus;
127   // RTCP report blocks mapped by remote SSRC.
128   using ReportBlockDataMap = std::map<uint32_t, ReportBlockData>;
129   // RTCP report blocks map mapped by source SSRC.
130   using ReportBlockMap = std::map<uint32_t, ReportBlockDataMap>;
131 
132   bool ParseCompoundPacket(rtc::ArrayView<const uint8_t> packet,
133                            PacketInformation* packet_information);
134 
135   void TriggerCallbacksFromRtcpPacket(
136       const PacketInformation& packet_information);
137 
138   TmmbrInformation* FindOrCreateTmmbrInfo(uint32_t remote_ssrc)
139       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
140   // Update TmmbrInformation (if present) is alive.
141   void UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc)
142       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
143   TmmbrInformation* GetTmmbrInformation(uint32_t remote_ssrc)
144       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
145 
146   void HandleSenderReport(const rtcp::CommonHeader& rtcp_block,
147                           PacketInformation* packet_information)
148       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
149 
150   void HandleReceiverReport(const rtcp::CommonHeader& rtcp_block,
151                             PacketInformation* packet_information)
152       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
153 
154   void HandleReportBlock(const rtcp::ReportBlock& report_block,
155                          PacketInformation* packet_information,
156                          uint32_t remote_ssrc)
157       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
158 
159   void HandleSdes(const rtcp::CommonHeader& rtcp_block,
160                   PacketInformation* packet_information)
161       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
162 
163   void HandleXr(const rtcp::CommonHeader& rtcp_block,
164                 PacketInformation* packet_information)
165       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
166 
167   void HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
168                                     const rtcp::Rrtr& rrtr)
169       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
170 
171   void HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti)
172       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
173 
174   void HandleXrTargetBitrate(uint32_t ssrc,
175                              const rtcp::TargetBitrate& target_bitrate,
176                              PacketInformation* packet_information)
177       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
178 
179   void HandleNack(const rtcp::CommonHeader& rtcp_block,
180                   PacketInformation* packet_information)
181       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
182 
183   void HandleApp(const rtcp::CommonHeader& rtcp_block,
184                  PacketInformation* packet_information)
185       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
186 
187   void HandleBye(const rtcp::CommonHeader& rtcp_block)
188       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
189 
190   void HandlePli(const rtcp::CommonHeader& rtcp_block,
191                  PacketInformation* packet_information)
192       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
193 
194   void HandlePsfbApp(const rtcp::CommonHeader& rtcp_block,
195                      PacketInformation* packet_information)
196       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
197 
198   void HandleTmmbr(const rtcp::CommonHeader& rtcp_block,
199                    PacketInformation* packet_information)
200       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
201 
202   void HandleTmmbn(const rtcp::CommonHeader& rtcp_block,
203                    PacketInformation* packet_information)
204       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
205 
206   void HandleSrReq(const rtcp::CommonHeader& rtcp_block,
207                    PacketInformation* packet_information)
208       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
209 
210   void HandleFir(const rtcp::CommonHeader& rtcp_block,
211                  PacketInformation* packet_information)
212       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
213 
214   void HandleTransportFeedback(const rtcp::CommonHeader& rtcp_block,
215                                PacketInformation* packet_information)
216       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
217 
218   bool RtcpRrTimeoutLocked(Timestamp now)
219       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
220 
221   bool RtcpRrSequenceNumberTimeoutLocked(Timestamp now)
222       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
223 
224   Clock* const clock_;
225   const bool receiver_only_;
226   ModuleRtpRtcp* const rtp_rtcp_;
227   const uint32_t main_ssrc_;
228   const std::set<uint32_t> registered_ssrcs_;
229 
230   RtcpBandwidthObserver* const rtcp_bandwidth_observer_;
231   RtcpIntraFrameObserver* const rtcp_intra_frame_observer_;
232   RtcpLossNotificationObserver* const rtcp_loss_notification_observer_;
233   NetworkStateEstimateObserver* const network_state_estimate_observer_;
234   TransportFeedbackObserver* const transport_feedback_observer_;
235   VideoBitrateAllocationObserver* const bitrate_allocation_observer_;
236   const TimeDelta report_interval_;
237 
238   mutable Mutex rtcp_receiver_lock_;
239   uint32_t remote_ssrc_ RTC_GUARDED_BY(rtcp_receiver_lock_);
240 
241   // Received sender report.
242   NtpTime remote_sender_ntp_time_ RTC_GUARDED_BY(rtcp_receiver_lock_);
243   uint32_t remote_sender_rtp_time_ RTC_GUARDED_BY(rtcp_receiver_lock_);
244   // When did we receive the last send report.
245   NtpTime last_received_sr_ntp_ RTC_GUARDED_BY(rtcp_receiver_lock_);
246 
247   // Received RRTR information in ascending receive time order.
248   std::list<RrtrInformation> received_rrtrs_
249       RTC_GUARDED_BY(rtcp_receiver_lock_);
250   // Received RRTR information mapped by remote ssrc.
251   std::map<uint32_t, std::list<RrtrInformation>::iterator>
252       received_rrtrs_ssrc_it_ RTC_GUARDED_BY(rtcp_receiver_lock_);
253 
254   // Estimated rtt, zero when there is no valid estimate.
255   bool xr_rrtr_status_ RTC_GUARDED_BY(rtcp_receiver_lock_);
256   int64_t xr_rr_rtt_ms_;
257 
258   int64_t oldest_tmmbr_info_ms_ RTC_GUARDED_BY(rtcp_receiver_lock_);
259   // Mapped by remote ssrc.
260   std::map<uint32_t, TmmbrInformation> tmmbr_infos_
261       RTC_GUARDED_BY(rtcp_receiver_lock_);
262 
263   ReportBlockMap received_report_blocks_ RTC_GUARDED_BY(rtcp_receiver_lock_);
264   std::map<uint32_t, LastFirStatus> last_fir_
265       RTC_GUARDED_BY(rtcp_receiver_lock_);
266   std::map<uint32_t, std::string> received_cnames_
267       RTC_GUARDED_BY(rtcp_receiver_lock_);
268 
269   // The last time we received an RTCP Report block for this module.
270   Timestamp last_received_rb_ RTC_GUARDED_BY(rtcp_receiver_lock_) =
271       Timestamp::PlusInfinity();
272 
273   // The time we last received an RTCP RR telling we have successfully
274   // delivered RTP packet to the remote side.
275   Timestamp last_increased_sequence_number_ = Timestamp::PlusInfinity();
276 
277   RtcpStatisticsCallback* const stats_callback_;
278   RtcpCnameCallback* const cname_callback_;
279   // TODO(hbos): Remove RtcpStatisticsCallback in favor of
280   // ReportBlockDataObserver; the ReportBlockData contains a superset of the
281   // RtcpStatistics data.
282   ReportBlockDataObserver* const report_block_data_observer_;
283 
284   RtcpPacketTypeCounterObserver* const packet_type_counter_observer_;
285   RtcpPacketTypeCounter packet_type_counter_;
286 
287   RtcpNackStats nack_stats_;
288 
289   size_t num_skipped_packets_;
290   int64_t last_skipped_packets_warning_ms_;
291 };
292 }  // namespace webrtc
293 #endif  // MODULES_RTP_RTCP_SOURCE_RTCP_RECEIVER_H_
294