tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

rtcp_receiver.h (15499B)


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