• 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 <string>
17 #include <vector>
18 
19 #include "absl/types/optional.h"
20 #include "api/array_view.h"
21 #include "api/sequence_checker.h"
22 #include "api/units/time_delta.h"
23 #include "modules/rtp_rtcp/include/report_block_data.h"
24 #include "modules/rtp_rtcp/include/rtcp_statistics.h"
25 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
26 #include "modules/rtp_rtcp/source/rtcp_nack_stats.h"
27 #include "modules/rtp_rtcp/source/rtcp_packet/dlrr.h"
28 #include "modules/rtp_rtcp/source/rtcp_packet/tmmb_item.h"
29 #include "modules/rtp_rtcp/source/rtp_rtcp_interface.h"
30 #include "rtc_base/containers/flat_map.h"
31 #include "rtc_base/synchronization/mutex.h"
32 #include "rtc_base/system/no_unique_address.h"
33 #include "rtc_base/thread_annotations.h"
34 #include "system_wrappers/include/ntp_time.h"
35 
36 namespace webrtc {
37 
38 class ModuleRtpRtcpImpl2;
39 class VideoBitrateAllocationObserver;
40 
41 namespace rtcp {
42 class CommonHeader;
43 class ReportBlock;
44 class Rrtr;
45 class TargetBitrate;
46 class TmmbItem;
47 }  // namespace rtcp
48 
49 class RTCPReceiver final {
50  public:
51   class ModuleRtpRtcp {
52    public:
53     virtual void SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) = 0;
54     virtual void OnRequestSendReport() = 0;
55     virtual void OnReceivedNack(
56         const std::vector<uint16_t>& nack_sequence_numbers) = 0;
57     virtual void OnReceivedRtcpReportBlocks(
58         const ReportBlockList& report_blocks) = 0;
59 
60    protected:
61     virtual ~ModuleRtpRtcp() = default;
62   };
63   // Standardized stats derived from the non-sender RTT.
64   class NonSenderRttStats {
65    public:
66     NonSenderRttStats() = default;
67     NonSenderRttStats(const NonSenderRttStats&) = default;
68     NonSenderRttStats& operator=(const NonSenderRttStats&) = default;
69     ~NonSenderRttStats() = default;
Update(TimeDelta non_sender_rtt_seconds)70     void Update(TimeDelta non_sender_rtt_seconds) {
71       round_trip_time_ = non_sender_rtt_seconds;
72       total_round_trip_time_ += non_sender_rtt_seconds;
73       round_trip_time_measurements_++;
74     }
Invalidate()75     void Invalidate() { round_trip_time_.reset(); }
76     // https://www.w3.org/TR/webrtc-stats/#dom-rtcremoteoutboundrtpstreamstats-roundtriptime
round_trip_time()77     absl::optional<TimeDelta> round_trip_time() const {
78       return round_trip_time_;
79     }
80     // https://www.w3.org/TR/webrtc-stats/#dom-rtcremoteoutboundrtpstreamstats-totalroundtriptime
total_round_trip_time()81     TimeDelta total_round_trip_time() const { return total_round_trip_time_; }
82     // https://www.w3.org/TR/webrtc-stats/#dom-rtcremoteoutboundrtpstreamstats-roundtriptimemeasurements
round_trip_time_measurements()83     int round_trip_time_measurements() const {
84       return round_trip_time_measurements_;
85     }
86 
87    private:
88     absl::optional<TimeDelta> round_trip_time_;
89     TimeDelta total_round_trip_time_ = TimeDelta::Zero();
90     int round_trip_time_measurements_ = 0;
91   };
92 
93   RTCPReceiver(const RtpRtcpInterface::Configuration& config,
94                ModuleRtpRtcp* owner);
95 
96   RTCPReceiver(const RtpRtcpInterface::Configuration& config,
97                ModuleRtpRtcpImpl2* owner);
98 
99   ~RTCPReceiver();
100 
IncomingPacket(const uint8_t * packet,size_t packet_size)101   void IncomingPacket(const uint8_t* packet, size_t packet_size) {
102     IncomingPacket(rtc::MakeArrayView(packet, packet_size));
103   }
104   void IncomingPacket(rtc::ArrayView<const uint8_t> packet);
105 
106   int64_t LastReceivedReportBlockMs() const;
107 
108   void set_local_media_ssrc(uint32_t ssrc);
109   uint32_t local_media_ssrc() const;
110 
111   void SetRemoteSSRC(uint32_t ssrc);
112   uint32_t RemoteSSRC() const;
113 
receiver_only()114   bool receiver_only() const { return receiver_only_; }
115 
116   // Get received NTP.
117   // The types for the arguments below derive from the specification:
118   // - `remote_sender_packet_count`: `RTCSentRtpStreamStats.packetsSent` [1]
119   // - `remote_sender_octet_count`: `RTCSentRtpStreamStats.bytesSent` [1]
120   // - `remote_sender_reports_count`:
121   //   `RTCRemoteOutboundRtpStreamStats.reportsSent` [2]
122   // [1] https://www.w3.org/TR/webrtc-stats/#remoteoutboundrtpstats-dict*
123   // [2] https://www.w3.org/TR/webrtc-stats/#dom-rtcsentrtpstreamstats
124   bool NTP(uint32_t* received_ntp_secs,
125            uint32_t* received_ntp_frac,
126            uint32_t* rtcp_arrival_time_secs,
127            uint32_t* rtcp_arrival_time_frac,
128            uint32_t* rtcp_timestamp,
129            uint32_t* remote_sender_packet_count,
130            uint64_t* remote_sender_octet_count,
131            uint64_t* remote_sender_reports_count) const;
132 
133   std::vector<rtcp::ReceiveTimeInfo> ConsumeReceivedXrReferenceTimeInfo();
134 
135   // Get rtt.
136   int32_t RTT(uint32_t remote_ssrc,
137               int64_t* last_rtt_ms,
138               int64_t* avg_rtt_ms,
139               int64_t* min_rtt_ms,
140               int64_t* max_rtt_ms) const;
141 
142   // Returns non-sender RTT metrics for the remote SSRC.
143   NonSenderRttStats GetNonSenderRTT() const;
144 
145   void SetNonSenderRttMeasurement(bool enabled);
146   bool GetAndResetXrRrRtt(int64_t* rtt_ms);
147 
148   // Called once per second on the worker thread to do rtt calculations.
149   // Returns an optional rtt value if one is available.
150   absl::optional<TimeDelta> OnPeriodicRttUpdate(Timestamp newer_than,
151                                                 bool sending);
152 
153   // A snapshot of Report Blocks with additional data of interest to statistics.
154   // Within this list, the source SSRC is unique and ReportBlockData represents
155   // the latest Report Block that was received for that SSRC.
156   std::vector<ReportBlockData> GetLatestReportBlockData() const;
157 
158   // Returns true if we haven't received an RTCP RR for several RTCP
159   // intervals, but only triggers true once.
160   bool RtcpRrTimeout();
161 
162   // Returns true if we haven't received an RTCP RR telling the receive side
163   // has not received RTP packets for too long, i.e. extended highest sequence
164   // number hasn't increased for several RTCP intervals. The function only
165   // returns true once until a new RR is received.
166   bool RtcpRrSequenceNumberTimeout();
167 
168   std::vector<rtcp::TmmbItem> TmmbrReceived();
169   // Return true if new bandwidth should be set.
170   bool UpdateTmmbrTimers();
171   std::vector<rtcp::TmmbItem> BoundingSet(bool* tmmbr_owner);
172   // Set new bandwidth and notify remote clients about it.
173   void NotifyTmmbrUpdated();
174 
175  private:
176 #if RTC_DCHECK_IS_ON
177   class CustomSequenceChecker : public SequenceChecker {
178    public:
CustomSequenceChecker(bool disable_checks)179     explicit CustomSequenceChecker(bool disable_checks)
180         : disable_checks_(disable_checks) {}
IsCurrent()181     bool IsCurrent() const {
182       if (disable_checks_)
183         return true;
184       return SequenceChecker::IsCurrent();
185     }
186 
187    private:
188     const bool disable_checks_;
189   };
190 #else
191   class CustomSequenceChecker : public SequenceChecker {
192    public:
193     explicit CustomSequenceChecker(bool) {}
194   };
195 #endif
196 
197   // A lightweight inlined set of local SSRCs.
198   class RegisteredSsrcs {
199    public:
200     static constexpr size_t kMediaSsrcIndex = 0;
201     static constexpr size_t kMaxSsrcs = 3;
202     // Initializes the set of registered local SSRCS by extracting them from the
203     // provided `config`. The `disable_sequence_checker` flag is a workaround
204     // to be able to use a sequence checker without breaking downstream
205     // code that currently doesn't follow the same threading rules as webrtc.
206     RegisteredSsrcs(bool disable_sequence_checker,
207                     const RtpRtcpInterface::Configuration& config);
208 
209     // Indicates if `ssrc` is in the set of registered local SSRCs.
210     bool contains(uint32_t ssrc) const;
211     uint32_t media_ssrc() const;
212     void set_media_ssrc(uint32_t ssrc);
213 
214    private:
215     RTC_NO_UNIQUE_ADDRESS CustomSequenceChecker packet_sequence_checker_;
216     absl::InlinedVector<uint32_t, kMaxSsrcs> ssrcs_
217         RTC_GUARDED_BY(packet_sequence_checker_);
218   };
219 
220   struct PacketInformation;
221 
222   // Structure for handing TMMBR and TMMBN rtcp messages (RFC5104,
223   // section 3.5.4).
224   struct TmmbrInformation {
225     struct TimedTmmbrItem {
226       rtcp::TmmbItem tmmbr_item;
227       int64_t last_updated_ms;
228     };
229 
230     int64_t last_time_received_ms = 0;
231 
232     bool ready_for_delete = false;
233 
234     std::vector<rtcp::TmmbItem> tmmbn;
235     std::map<uint32_t, TimedTmmbrItem> tmmbr;
236   };
237 
238   // Structure for storing received RRTR RTCP messages (RFC3611, section 4.4).
239   struct RrtrInformation {
RrtrInformationRrtrInformation240     RrtrInformation(uint32_t ssrc,
241                     uint32_t received_remote_mid_ntp_time,
242                     uint32_t local_receive_mid_ntp_time)
243         : ssrc(ssrc),
244           received_remote_mid_ntp_time(received_remote_mid_ntp_time),
245           local_receive_mid_ntp_time(local_receive_mid_ntp_time) {}
246 
247     uint32_t ssrc;
248     // Received NTP timestamp in compact representation.
249     uint32_t received_remote_mid_ntp_time;
250     // NTP time when the report was received in compact representation.
251     uint32_t local_receive_mid_ntp_time;
252   };
253 
254   struct LastFirStatus {
LastFirStatusLastFirStatus255     LastFirStatus(int64_t now_ms, uint8_t sequence_number)
256         : request_ms(now_ms), sequence_number(sequence_number) {}
257     int64_t request_ms;
258     uint8_t sequence_number;
259   };
260 
261   class RttStats {
262    public:
263     RttStats() = default;
264     RttStats(const RttStats&) = default;
265     RttStats& operator=(const RttStats&) = default;
266 
267     void AddRtt(TimeDelta rtt);
268 
last_rtt()269     TimeDelta last_rtt() const { return last_rtt_; }
min_rtt()270     TimeDelta min_rtt() const { return min_rtt_; }
max_rtt()271     TimeDelta max_rtt() const { return max_rtt_; }
average_rtt()272     TimeDelta average_rtt() const { return sum_rtt_ / num_rtts_; }
273 
274    private:
275     TimeDelta last_rtt_ = TimeDelta::Zero();
276     TimeDelta min_rtt_ = TimeDelta::PlusInfinity();
277     TimeDelta max_rtt_ = TimeDelta::MinusInfinity();
278     TimeDelta sum_rtt_ = TimeDelta::Zero();
279     size_t num_rtts_ = 0;
280   };
281 
282   bool ParseCompoundPacket(rtc::ArrayView<const uint8_t> packet,
283                            PacketInformation* packet_information);
284 
285   void TriggerCallbacksFromRtcpPacket(
286       const PacketInformation& packet_information);
287 
288   TmmbrInformation* FindOrCreateTmmbrInfo(uint32_t remote_ssrc)
289       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
290   // Update TmmbrInformation (if present) is alive.
291   void UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc)
292       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
293   TmmbrInformation* GetTmmbrInformation(uint32_t remote_ssrc)
294       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
295 
296   void HandleSenderReport(const rtcp::CommonHeader& rtcp_block,
297                           PacketInformation* packet_information)
298       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
299 
300   void HandleReceiverReport(const rtcp::CommonHeader& rtcp_block,
301                             PacketInformation* packet_information)
302       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
303 
304   void HandleReportBlock(const rtcp::ReportBlock& report_block,
305                          PacketInformation* packet_information,
306                          uint32_t remote_ssrc)
307       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
308 
309   void HandleSdes(const rtcp::CommonHeader& rtcp_block,
310                   PacketInformation* packet_information)
311       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
312 
313   void HandleXr(const rtcp::CommonHeader& rtcp_block,
314                 PacketInformation* packet_information,
315                 bool& contains_dlrr,
316                 uint32_t& ssrc)
317       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
318 
319   void HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
320                                     const rtcp::Rrtr& rrtr)
321       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
322 
323   void HandleXrDlrrReportBlock(uint32_t ssrc, const rtcp::ReceiveTimeInfo& rti)
324       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
325 
326   void HandleXrTargetBitrate(uint32_t ssrc,
327                              const rtcp::TargetBitrate& target_bitrate,
328                              PacketInformation* packet_information)
329       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
330 
331   void HandleNack(const rtcp::CommonHeader& rtcp_block,
332                   PacketInformation* packet_information)
333       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
334 
335   void HandleApp(const rtcp::CommonHeader& rtcp_block,
336                  PacketInformation* packet_information)
337       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
338 
339   void HandleBye(const rtcp::CommonHeader& rtcp_block)
340       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
341 
342   void HandlePli(const rtcp::CommonHeader& rtcp_block,
343                  PacketInformation* packet_information)
344       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
345 
346   void HandlePsfbApp(const rtcp::CommonHeader& rtcp_block,
347                      PacketInformation* packet_information)
348       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
349 
350   void HandleTmmbr(const rtcp::CommonHeader& rtcp_block,
351                    PacketInformation* packet_information)
352       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
353 
354   void HandleTmmbn(const rtcp::CommonHeader& rtcp_block,
355                    PacketInformation* packet_information)
356       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
357 
358   void HandleSrReq(const rtcp::CommonHeader& rtcp_block,
359                    PacketInformation* packet_information)
360       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
361 
362   void HandleFir(const rtcp::CommonHeader& rtcp_block,
363                  PacketInformation* packet_information)
364       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
365 
366   void HandleTransportFeedback(const rtcp::CommonHeader& rtcp_block,
367                                PacketInformation* packet_information)
368       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
369 
370   bool RtcpRrTimeoutLocked(Timestamp now)
371       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
372 
373   bool RtcpRrSequenceNumberTimeoutLocked(Timestamp now)
374       RTC_EXCLUSIVE_LOCKS_REQUIRED(rtcp_receiver_lock_);
375 
376   Clock* const clock_;
377   const bool receiver_only_;
378   ModuleRtpRtcp* const rtp_rtcp_;
379   // The set of registered local SSRCs.
380   RegisteredSsrcs registered_ssrcs_;
381 
382   RtcpBandwidthObserver* const rtcp_bandwidth_observer_;
383   RtcpIntraFrameObserver* const rtcp_intra_frame_observer_;
384   RtcpLossNotificationObserver* const rtcp_loss_notification_observer_;
385   NetworkStateEstimateObserver* const network_state_estimate_observer_;
386   TransportFeedbackObserver* const transport_feedback_observer_;
387   VideoBitrateAllocationObserver* const bitrate_allocation_observer_;
388   const TimeDelta report_interval_;
389 
390   mutable Mutex rtcp_receiver_lock_;
391   uint32_t remote_ssrc_ RTC_GUARDED_BY(rtcp_receiver_lock_);
392 
393   // Received sender report.
394   NtpTime remote_sender_ntp_time_ RTC_GUARDED_BY(rtcp_receiver_lock_);
395   uint32_t remote_sender_rtp_time_ RTC_GUARDED_BY(rtcp_receiver_lock_);
396   // When did we receive the last send report.
397   NtpTime last_received_sr_ntp_ RTC_GUARDED_BY(rtcp_receiver_lock_);
398   uint32_t remote_sender_packet_count_ RTC_GUARDED_BY(rtcp_receiver_lock_);
399   uint64_t remote_sender_octet_count_ RTC_GUARDED_BY(rtcp_receiver_lock_);
400   uint64_t remote_sender_reports_count_ RTC_GUARDED_BY(rtcp_receiver_lock_);
401 
402   // Received RRTR information in ascending receive time order.
403   std::list<RrtrInformation> received_rrtrs_
404       RTC_GUARDED_BY(rtcp_receiver_lock_);
405   // Received RRTR information mapped by remote ssrc.
406   flat_map<uint32_t, std::list<RrtrInformation>::iterator>
407       received_rrtrs_ssrc_it_ RTC_GUARDED_BY(rtcp_receiver_lock_);
408 
409   // Estimated rtt, zero when there is no valid estimate.
410   bool xr_rrtr_status_ RTC_GUARDED_BY(rtcp_receiver_lock_);
411   int64_t xr_rr_rtt_ms_;
412 
413   int64_t oldest_tmmbr_info_ms_ RTC_GUARDED_BY(rtcp_receiver_lock_);
414   // Mapped by remote ssrc.
415   flat_map<uint32_t, TmmbrInformation> tmmbr_infos_
416       RTC_GUARDED_BY(rtcp_receiver_lock_);
417 
418   // Round-Trip Time per remote sender ssrc.
419   flat_map<uint32_t, RttStats> rtts_ RTC_GUARDED_BY(rtcp_receiver_lock_);
420   // Non-sender Round-trip time per remote ssrc.
421   flat_map<uint32_t, NonSenderRttStats> non_sender_rtts_
422       RTC_GUARDED_BY(rtcp_receiver_lock_);
423 
424   // Report blocks per local source ssrc.
425   flat_map<uint32_t, ReportBlockData> received_report_blocks_
426       RTC_GUARDED_BY(rtcp_receiver_lock_);
427   flat_map<uint32_t, LastFirStatus> last_fir_
428       RTC_GUARDED_BY(rtcp_receiver_lock_);
429 
430   // The last time we received an RTCP Report block for this module.
431   Timestamp last_received_rb_ RTC_GUARDED_BY(rtcp_receiver_lock_) =
432       Timestamp::PlusInfinity();
433 
434   // The time we last received an RTCP RR telling we have successfully
435   // delivered RTP packet to the remote side.
436   Timestamp last_increased_sequence_number_ = Timestamp::PlusInfinity();
437 
438   RtcpCnameCallback* const cname_callback_;
439   ReportBlockDataObserver* const report_block_data_observer_;
440 
441   RtcpPacketTypeCounterObserver* const packet_type_counter_observer_;
442   RtcpPacketTypeCounter packet_type_counter_;
443 
444   RtcpNackStats nack_stats_;
445 
446   size_t num_skipped_packets_;
447   int64_t last_skipped_packets_warning_ms_;
448 };
449 }  // namespace webrtc
450 #endif  // MODULES_RTP_RTCP_SOURCE_RTCP_RECEIVER_H_
451