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