tor-browser

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

rtp_rtcp_impl2.h (12163B)


      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_RTP_RTCP_IMPL2_H_
     12 #define MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_
     13 
     14 #include <stddef.h>
     15 #include <stdint.h>
     16 
     17 #include <memory>
     18 #include <optional>
     19 #include <vector>
     20 
     21 #include "absl/strings/string_view.h"
     22 #include "api/array_view.h"
     23 #include "api/environment/environment.h"
     24 #include "api/rtp_headers.h"
     25 #include "api/sequence_checker.h"
     26 #include "api/task_queue/pending_task_safety_flag.h"
     27 #include "api/task_queue/task_queue_base.h"
     28 #include "api/units/time_delta.h"
     29 #include "api/units/timestamp.h"
     30 #include "api/video/video_bitrate_allocation.h"
     31 #include "modules/include/module_fec_types.h"
     32 #include "modules/rtp_rtcp/include/report_block_data.h"
     33 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"  // RTCPPacketType
     34 #include "modules/rtp_rtcp/source/packet_sequencer.h"
     35 #include "modules/rtp_rtcp/source/rtcp_packet.h"
     36 #include "modules/rtp_rtcp/source/rtcp_packet/tmmb_item.h"
     37 #include "modules/rtp_rtcp/source/rtcp_receiver.h"
     38 #include "modules/rtp_rtcp/source/rtcp_sender.h"
     39 #include "modules/rtp_rtcp/source/rtp_packet_history.h"
     40 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
     41 #include "modules/rtp_rtcp/source/rtp_rtcp_interface.h"
     42 #include "modules/rtp_rtcp/source/rtp_sender.h"
     43 #include "modules/rtp_rtcp/source/rtp_sender_egress.h"
     44 #include "modules/rtp_rtcp/source/rtp_sequence_number_map.h"
     45 #include "rtc_base/gtest_prod_util.h"
     46 #include "rtc_base/synchronization/mutex.h"
     47 #include "rtc_base/system/no_unique_address.h"
     48 #include "rtc_base/task_utils/repeating_task.h"
     49 #include "rtc_base/thread_annotations.h"
     50 
     51 namespace webrtc {
     52 
     53 struct PacedPacketInfo;
     54 struct RTPVideoHeader;
     55 
     56 class ModuleRtpRtcpImpl2 final : public RtpRtcpInterface,
     57                                 public RTCPReceiver::ModuleRtpRtcp {
     58 public:
     59  ModuleRtpRtcpImpl2(const Environment& env,
     60                     const RtpRtcpInterface::Configuration& configuration);
     61  ~ModuleRtpRtcpImpl2() override;
     62 
     63  // Receiver part.
     64 
     65  // Called when we receive an RTCP packet.
     66  void IncomingRtcpPacket(ArrayView<const uint8_t> incoming_packet) override;
     67 
     68  void SetRemoteSSRC(uint32_t ssrc) override;
     69 
     70  void SetLocalSsrc(uint32_t local_ssrc) override;
     71 
     72  // Sender part.
     73  void RegisterSendPayloadFrequency(int payload_type,
     74                                    int payload_frequency) override;
     75 
     76  int32_t DeRegisterSendPayload(int8_t payload_type) override;
     77 
     78  void SetExtmapAllowMixed(bool extmap_allow_mixed) override;
     79 
     80  void RegisterRtpHeaderExtension(absl::string_view uri, int id) override;
     81  void DeregisterSendRtpHeaderExtension(absl::string_view uri) override;
     82 
     83  bool SupportsPadding() const override;
     84  bool SupportsRtxPayloadPadding() const override;
     85 
     86  // Get start timestamp.
     87  uint32_t StartTimestamp() const override;
     88 
     89  // Configure start timestamp, default is a random number.
     90  void SetStartTimestamp(uint32_t timestamp) override;
     91 
     92  uint16_t SequenceNumber() const override;
     93 
     94  // Set SequenceNumber, default is a random number.
     95  void SetSequenceNumber(uint16_t seq) override;
     96 
     97  void SetRtpState(const RtpState& rtp_state) override;
     98  void SetRtxState(const RtpState& rtp_state) override;
     99  RtpState GetRtpState() const override;
    100  RtpState GetRtxState() const override;
    101 
    102  void SetNonSenderRttMeasurement(bool enabled) override;
    103 
    104  uint32_t SSRC() const override { return rtcp_sender_.SSRC(); }
    105 
    106  // Semantically identical to `SSRC()` but must be called on the packet
    107  // delivery thread/tq and returns the ssrc that maps to
    108  // RtpRtcpInterface::Configuration::local_media_ssrc.
    109  uint32_t local_media_ssrc() const;
    110 
    111  void SetMid(absl::string_view mid) override;
    112 
    113  RTCPSender::FeedbackState GetFeedbackState();
    114 
    115  void SetRtxSendStatus(int mode) override;
    116  int RtxSendStatus() const override;
    117  std::optional<uint32_t> RtxSsrc() const override;
    118 
    119  void SetRtxSendPayloadType(int payload_type,
    120                             int associated_payload_type) override;
    121 
    122  std::optional<uint32_t> FlexfecSsrc() const override;
    123 
    124  // Sends kRtcpByeCode when going from true to false.
    125  int32_t SetSendingStatus(bool sending) override;
    126 
    127  bool Sending() const override;
    128 
    129  // Drops or relays media packets.
    130  void SetSendingMediaStatus(bool sending) override;
    131 
    132  bool SendingMedia() const override;
    133 
    134  bool IsAudioConfigured() const override;
    135 
    136  void SetAsPartOfAllocation(bool part_of_allocation) override;
    137 
    138  bool OnSendingRtpFrame(uint32_t timestamp,
    139                         int64_t capture_time_ms,
    140                         int payload_type,
    141                         bool force_sender_report) override;
    142 
    143  bool CanSendPacket(const RtpPacketToSend& packet) const override;
    144 
    145  void AssignSequenceNumber(RtpPacketToSend& packet) override;
    146 
    147  void SendPacket(std::unique_ptr<RtpPacketToSend> packet,
    148                  const PacedPacketInfo& pacing_info) override;
    149 
    150  bool TrySendPacket(std::unique_ptr<RtpPacketToSend> packet,
    151                     const PacedPacketInfo& pacing_info) override;
    152  void OnBatchComplete() override;
    153 
    154  void SetFecProtectionParams(const FecProtectionParams& delta_params,
    155                              const FecProtectionParams& key_params) override;
    156 
    157  std::vector<std::unique_ptr<RtpPacketToSend>> FetchFecPackets() override;
    158 
    159  void OnAbortedRetransmissions(
    160      ArrayView<const uint16_t> sequence_numbers) override;
    161 
    162  void OnPacketsAcknowledged(
    163      ArrayView<const uint16_t> sequence_numbers) override;
    164 
    165  std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
    166      size_t target_size_bytes) override;
    167 
    168  std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos(
    169      ArrayView<const uint16_t> sequence_numbers) const override;
    170 
    171  size_t ExpectedPerPacketOverhead() const override;
    172 
    173  void OnPacketSendingThreadSwitched() override;
    174 
    175  // RTCP part.
    176 
    177  // Get RTCP status.
    178  RtcpMode RTCP() const override;
    179 
    180  // Configure RTCP status i.e on/off.
    181  void SetRTCPStatus(RtcpMode method) override;
    182 
    183  // Set RTCP CName.
    184  int32_t SetCNAME(absl::string_view c_name) override;
    185 
    186  // Get RoundTripTime.
    187  std::optional<TimeDelta> LastRtt() const override;
    188 
    189  TimeDelta ExpectedRetransmissionTime() const override;
    190 
    191  // Force a send of an RTCP packet.
    192  // Normal SR and RR are triggered via the task queue that's current when this
    193  // object is created.
    194  int32_t SendRTCP(RTCPPacketType rtcpPacketType) override;
    195 
    196  void GetSendStreamDataCounters(
    197      StreamDataCounters* rtp_counters,
    198      StreamDataCounters* rtx_counters) const override;
    199 
    200  void RemoteRTCPSenderInfo(uint32_t* packet_count,
    201                            uint32_t* octet_count,
    202                            int64_t* ntp_timestamp_ms,
    203                            int64_t* remote_ntp_timestamp_ms) const override;
    204 
    205  // A snapshot of the most recent Report Block with additional data of
    206  // interest to statistics. Used to implement RTCRemoteInboundRtpStreamStats.
    207  // Within this list, the `ReportBlockData::source_ssrc()`, which is the SSRC
    208  // of the corresponding outbound RTP stream, is unique.
    209  std::vector<ReportBlockData> GetLatestReportBlockData() const override;
    210  std::optional<SenderReportStats> GetSenderReportStats() const override;
    211  std::optional<NonSenderRttStats> GetNonSenderRttStats() const override;
    212 
    213  // (REMB) Receiver Estimated Max Bitrate.
    214  void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) override;
    215  void UnsetRemb() override;
    216 
    217  void SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) override;
    218 
    219  size_t MaxRtpPacketSize() const override;
    220 
    221  void SetMaxRtpPacketSize(size_t max_packet_size) override;
    222 
    223  // (NACK) Negative acknowledgment part.
    224 
    225  // Send a Negative acknowledgment packet.
    226  // TODO(philipel): Deprecate SendNACK and use SendNack instead.
    227  int32_t SendNACK(const uint16_t* nack_list, uint16_t size) override;
    228 
    229  void SendNack(const std::vector<uint16_t>& sequence_numbers) override;
    230 
    231  // Store the sent packets, needed to answer to a negative acknowledgment
    232  // requests.
    233  void SetStorePacketsStatus(bool enable, uint16_t number_to_store) override;
    234 
    235  void SendCombinedRtcpPacket(
    236      std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) override;
    237 
    238  // Video part.
    239  int32_t SendLossNotification(uint16_t last_decoded_seq_num,
    240                               uint16_t last_received_seq_num,
    241                               bool decodability_flag,
    242                               bool buffering_allowed) override;
    243 
    244  RtpSendRates GetSendRates() const override;
    245 
    246  void OnReceivedNack(
    247      const std::vector<uint16_t>& nack_sequence_numbers) override;
    248  void OnReceivedRtcpReportBlocks(
    249      ArrayView<const ReportBlockData> report_blocks) override;
    250  void OnRequestSendReport() override;
    251 
    252  void SetVideoBitrateAllocation(
    253      const VideoBitrateAllocation& bitrate) override;
    254 
    255  RTPSender* RtpSender() override;
    256  const RTPSender* RtpSender() const override;
    257 
    258 private:
    259  FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, Rtt);
    260  FRIEND_TEST_ALL_PREFIXES(RtpRtcpImpl2Test, RttForReceiverOnly);
    261 
    262  struct RtpSenderContext {
    263    explicit RtpSenderContext(const Environment& env,
    264                              TaskQueueBase& worker_queue,
    265                              const RtpRtcpInterface::Configuration& config);
    266    // Storage of packets, for retransmissions and padding, if applicable.
    267    RtpPacketHistory packet_history;
    268    SequenceChecker sequencing_checker;
    269    // Handles sequence number assignment and padding timestamp generation.
    270    PacketSequencer sequencer RTC_GUARDED_BY(sequencing_checker);
    271    // Handles final time timestamping/stats/etc and handover to Transport.
    272    RtpSenderEgress packet_sender;
    273    // If no paced sender configured, this class will be used to pass packets
    274    // from `packet_generator_` to `packet_sender_`.
    275    RtpSenderEgress::NonPacedPacketSender non_paced_sender;
    276    // Handles creation of RTP packets to be sent.
    277    RTPSender packet_generator;
    278  };
    279 
    280  void set_rtt_ms(int64_t rtt_ms);
    281  int64_t rtt_ms() const;
    282 
    283  bool TimeToSendFullNackList(int64_t now) const;
    284 
    285  // Called on a timer, once a second, on the worker_queue_, to update the RTT,
    286  // check if we need to send RTCP report, send TMMBR updates and fire events.
    287  void PeriodicUpdate();
    288 
    289  // Returns true if the module is configured to store packets.
    290  bool StorePackets() const;
    291 
    292  // Used from RtcpSenderMediator to maybe send rtcp.
    293  void MaybeSendRtcp() RTC_RUN_ON(worker_queue_);
    294 
    295  // Called when `rtcp_sender_` informs of the next RTCP instant. The method may
    296  // be called on various sequences, and is called under a RTCPSenderLock.
    297  void ScheduleRtcpSendEvaluation(TimeDelta duration);
    298 
    299  // Helper method combating too early delayed calls from task queues.
    300  // TODO(bugs.webrtc.org/12889): Consider removing this function when the issue
    301  // is resolved.
    302  void MaybeSendRtcpAtOrAfterTimestamp(Timestamp execution_time)
    303      RTC_RUN_ON(worker_queue_);
    304 
    305  // Schedules a call to MaybeSendRtcpAtOrAfterTimestamp delayed by `duration`.
    306  void ScheduleMaybeSendRtcpAtOrAfterTimestamp(Timestamp execution_time,
    307                                               TimeDelta duration);
    308 
    309  const Environment env_;
    310  TaskQueueBase* const worker_queue_;
    311  RTC_NO_UNIQUE_ADDRESS SequenceChecker rtcp_thread_checker_;
    312 
    313  std::unique_ptr<RtpSenderContext> rtp_sender_;
    314  RTCPSender rtcp_sender_;
    315  RTCPReceiver rtcp_receiver_;
    316 
    317  uint16_t packet_overhead_;
    318 
    319  // Send side
    320  int64_t nack_last_time_sent_full_ms_;
    321  uint16_t nack_last_seq_number_sent_;
    322 
    323  RtcpRttStats* const rtt_stats_;
    324  RepeatingTaskHandle rtt_update_task_ RTC_GUARDED_BY(worker_queue_);
    325 
    326  // The processed RTT from RtcpRttStats.
    327  mutable Mutex mutex_rtt_;
    328  int64_t rtt_ms_ RTC_GUARDED_BY(mutex_rtt_);
    329 
    330  RTC_NO_UNIQUE_ADDRESS ScopedTaskSafety task_safety_;
    331 };
    332 
    333 }  // namespace webrtc
    334 
    335 #endif  // MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL2_H_