tor-browser

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

rtp_rtcp_interface.h (20093B)


      1 /*
      2 *  Copyright (c) 2020 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_INTERFACE_H_
     12 #define MODULES_RTP_RTCP_SOURCE_RTP_RTCP_INTERFACE_H_
     13 
     14 #include <cstddef>
     15 #include <cstdint>
     16 #include <memory>
     17 #include <optional>
     18 #include <string>
     19 #include <vector>
     20 
     21 #include "absl/strings/string_view.h"
     22 #include "api/array_view.h"
     23 #include "api/frame_transformer_interface.h"
     24 #include "api/rtp_headers.h"
     25 #include "api/rtp_packet_sender.h"
     26 #include "api/scoped_refptr.h"
     27 #include "api/transport/network_types.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/receive_statistics.h"
     33 #include "modules/rtp_rtcp/include/report_block_data.h"
     34 #include "modules/rtp_rtcp/include/rtcp_statistics.h"
     35 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
     36 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
     37 #include "modules/rtp_rtcp/source/rtp_sequence_number_map.h"
     38 #include "modules/rtp_rtcp/source/video_fec_generator.h"
     39 #include "system_wrappers/include/ntp_time.h"
     40 
     41 namespace webrtc {
     42 
     43 // Forward declarations.
     44 class FrameEncryptorInterface;
     45 class RateLimiter;
     46 class RTPSender;
     47 class Transport;
     48 class VideoBitrateAllocationObserver;
     49 
     50 class RtpRtcpInterface : public RtcpFeedbackSenderInterface {
     51 public:
     52  struct Configuration {
     53    // True for a audio version of the RTP/RTCP module object false will create
     54    // a video version.
     55    bool audio = false;
     56    bool receiver_only = false;
     57 
     58    ReceiveStatisticsProvider* receive_statistics = nullptr;
     59 
     60    // Transport object that will be called when packets are ready to be sent
     61    // out on the network.
     62    Transport* outgoing_transport = nullptr;
     63 
     64    // Called when the receiver requests an intra frame.
     65    RtcpIntraFrameObserver* intra_frame_callback = nullptr;
     66 
     67    // Called when the receiver sends a loss notification.
     68    RtcpLossNotificationObserver* rtcp_loss_notification_observer = nullptr;
     69 
     70    // Called when receive an RTCP message related to the link in general, e.g.
     71    // bandwidth estimation related message.
     72    NetworkLinkRtcpObserver* network_link_rtcp_observer = nullptr;
     73 
     74    // Called when we receive a RTCP bye or timeout
     75    RtcpEventObserver* rtcp_event_observer = nullptr;
     76 
     77    NetworkStateEstimateObserver* network_state_estimate_observer = nullptr;
     78 
     79    VideoBitrateAllocationObserver* bitrate_allocation_observer = nullptr;
     80    RtcpRttStats* rtt_stats = nullptr;
     81    RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer = nullptr;
     82    // Called on receipt of RTCP report block from remote side.
     83    // TODO(bugs.webrtc.org/10679): Consider whether we want to use
     84    // only getters or only callbacks. If we decide on getters, the
     85    // ReportBlockDataObserver should also be removed in favor of
     86    // GetLatestReportBlockData().
     87    RtcpCnameCallback* rtcp_cname_callback = nullptr;
     88    ReportBlockDataObserver* report_block_data_observer = nullptr;
     89 
     90    // Spread any bursts of packets into smaller bursts to minimize packet loss.
     91    RtpPacketSender* paced_sender = nullptr;
     92 
     93    // Generates FEC packets.
     94    // TODO(sprang): Wire up to RtpSenderEgress.
     95    VideoFecGenerator* fec_generator = nullptr;
     96 
     97    BitrateStatisticsObserver* send_bitrate_observer = nullptr;
     98    SendPacketObserver* send_packet_observer = nullptr;
     99    RateLimiter* retransmission_rate_limiter = nullptr;
    100    StreamDataCountersCallback* rtp_stats_callback = nullptr;
    101 
    102    int rtcp_report_interval_ms = 0;
    103 
    104    // Update network2 instead of pacer_exit field of video timing extension.
    105    bool populate_network2_timestamp = false;
    106 
    107    scoped_refptr<FrameTransformerInterface> frame_transformer;
    108 
    109    // E2EE Custom Video Frame Encryption
    110    FrameEncryptorInterface* frame_encryptor = nullptr;
    111    // Require all outgoing frames to be encrypted with a FrameEncryptor.
    112    bool require_frame_encryption = false;
    113 
    114    // Corresponds to extmap-allow-mixed in SDP negotiation.
    115    bool extmap_allow_mixed = false;
    116 
    117    // If true, the RTP sender will always annotate outgoing packets with
    118    // MID and RID header extensions, if provided and negotiated.
    119    // If false, the RTP sender will stop sending MID and RID header extensions,
    120    // when it knows that the receiver is ready to demux based on SSRC. This is
    121    // done by RTCP RR acking.
    122    bool always_send_mid_and_rid = false;
    123 
    124    // SSRCs for media and retransmission, respectively.
    125    // FlexFec SSRC is fetched from `flexfec_sender`.
    126    uint32_t local_media_ssrc = 0;
    127    std::optional<uint32_t> rtx_send_ssrc;
    128 
    129    bool need_rtp_packet_infos = false;
    130 
    131    // Estimate RTT as non-sender as described in
    132    // https://tools.ietf.org/html/rfc3611#section-4.4 and #section-4.5
    133    bool non_sender_rtt_measurement = false;
    134 
    135    // If non-empty, sets the value for sending in the RID (and Repaired) RTP
    136    // header extension. RIDs are used to identify an RTP stream if SSRCs are
    137    // not negotiated. If the RID and Repaired RID extensions are not
    138    // registered, the RID will not be sent.
    139    std::string rid;
    140 
    141    // Enables send packet batching from the egress RTP sender.
    142    bool enable_send_packet_batching = false;
    143  };
    144 
    145  // Stats for RTCP sender reports (SR) for a specific SSRC.
    146  // Refer to https://tools.ietf.org/html/rfc3550#section-6.4.1.
    147  struct SenderReportStats {
    148    // Arrival timestamp (enviroment clock) for the last received RTCP SR.
    149    Timestamp last_arrival_timestamp = Timestamp::Zero();
    150    // Arrival NTP timestamp for the last received RTCP SR.
    151    // TODO: bugs.webrtc.org/370535296 - Remove the ntp arrival timestamp when
    152    // linked issue is fixed.
    153    NtpTime last_arrival_ntp_timestamp;
    154    // Received (a.k.a., remote) NTP timestamp for the last received RTCP SR.
    155    NtpTime last_remote_ntp_timestamp;
    156    // Received (a.k.a., remote) RTP timestamp from the last received RTCP SR.
    157    uint32_t last_remote_rtp_timestamp = 0;
    158    // Total number of RTP data packets transmitted by the sender since starting
    159    // transmission up until the time this SR packet was generated. The count
    160    // should be reset if the sender changes its SSRC identifier.
    161    uint32_t packets_sent = 0;
    162    // Total number of payload octets (i.e., not including header or padding)
    163    // transmitted in RTP data packets by the sender since starting transmission
    164    // up until the time this SR packet was generated. The count should be reset
    165    // if the sender changes its SSRC identifier.
    166    uint64_t bytes_sent = 0;
    167    // Total number of RTCP SR blocks received.
    168    // https://www.w3.org/TR/webrtc-stats/#dom-rtcremoteoutboundrtpstreamstats-reportssent.
    169    uint64_t reports_count = 0;
    170  };
    171  // Stats about the non-sender SSRC, based on RTCP extended reports (XR).
    172  // Refer to https://datatracker.ietf.org/doc/html/rfc3611#section-2.
    173  struct NonSenderRttStats {
    174    // https://www.w3.org/TR/webrtc-stats/#dom-rtcremoteoutboundrtpstreamstats-roundtriptime
    175    std::optional<TimeDelta> round_trip_time;
    176    // https://www.w3.org/TR/webrtc-stats/#dom-rtcremoteoutboundrtpstreamstats-totalroundtriptime
    177    TimeDelta total_round_trip_time = TimeDelta::Zero();
    178    // https://www.w3.org/TR/webrtc-stats/#dom-rtcremoteoutboundrtpstreamstats-roundtriptimemeasurements
    179    int round_trip_time_measurements = 0;
    180  };
    181 
    182  // **************************************************************************
    183  // Receiver functions
    184  // **************************************************************************
    185 
    186  virtual void IncomingRtcpPacket(ArrayView<const uint8_t> incoming_packet) = 0;
    187 
    188  virtual void SetRemoteSSRC(uint32_t ssrc) = 0;
    189 
    190  // Called when the local ssrc changes (post initialization) for receive
    191  // streams to match with send. Called on the packet receive thread/tq.
    192  virtual void SetLocalSsrc(uint32_t ssrc) = 0;
    193 
    194  // **************************************************************************
    195  // Sender
    196  // **************************************************************************
    197 
    198  // Sets the maximum size of an RTP packet, including RTP headers.
    199  virtual void SetMaxRtpPacketSize(size_t size) = 0;
    200 
    201  // Returns max RTP packet size. Takes into account RTP headers and
    202  // FEC/ULP/RED overhead (when FEC is enabled).
    203  virtual size_t MaxRtpPacketSize() const = 0;
    204 
    205  virtual void RegisterSendPayloadFrequency(int payload_type,
    206                                            int payload_frequency) = 0;
    207 
    208  // Unregisters a send payload.
    209  // `payload_type` - payload type of codec
    210  // Returns -1 on failure else 0.
    211  virtual int32_t DeRegisterSendPayload(int8_t payload_type) = 0;
    212 
    213  virtual void SetExtmapAllowMixed(bool extmap_allow_mixed) = 0;
    214 
    215  // Register extension by uri, triggers CHECK on falure.
    216  virtual void RegisterRtpHeaderExtension(absl::string_view uri, int id) = 0;
    217 
    218  virtual void DeregisterSendRtpHeaderExtension(absl::string_view uri) = 0;
    219 
    220  // Returns true if RTP module is send media, and any of the extensions
    221  // required for bandwidth estimation is registered.
    222  virtual bool SupportsPadding() const = 0;
    223  // Same as SupportsPadding(), but additionally requires that
    224  // SetRtxSendStatus() has been called with the kRtxRedundantPayloads option
    225  // enabled.
    226  virtual bool SupportsRtxPayloadPadding() const = 0;
    227 
    228  // Returns start timestamp.
    229  virtual uint32_t StartTimestamp() const = 0;
    230 
    231  // Sets start timestamp. Start timestamp is set to a random value if this
    232  // function is never called.
    233  virtual void SetStartTimestamp(uint32_t timestamp) = 0;
    234 
    235  // Returns SequenceNumber.
    236  virtual uint16_t SequenceNumber() const = 0;
    237 
    238  // Sets SequenceNumber, default is a random number.
    239  virtual void SetSequenceNumber(uint16_t seq) = 0;
    240 
    241  virtual void SetRtpState(const RtpState& rtp_state) = 0;
    242  virtual void SetRtxState(const RtpState& rtp_state) = 0;
    243  virtual RtpState GetRtpState() const = 0;
    244  virtual RtpState GetRtxState() const = 0;
    245 
    246  // This can be used to enable/disable receive-side RTT.
    247  virtual void SetNonSenderRttMeasurement(bool enabled) = 0;
    248 
    249  // Returns SSRC.
    250  virtual uint32_t SSRC() const = 0;
    251 
    252  // Sets the value for sending in the MID RTP header extension.
    253  // The MID RTP header extension should be registered for this to do anything.
    254  // Once set, this value can not be changed or removed.
    255  virtual void SetMid(absl::string_view mid) = 0;
    256 
    257  // Turns on/off sending RTX (RFC 4588). The modes can be set as a combination
    258  // of values of the enumerator RtxMode.
    259  virtual void SetRtxSendStatus(int modes) = 0;
    260 
    261  // Returns status of sending RTX (RFC 4588). The returned value can be
    262  // a combination of values of the enumerator RtxMode.
    263  virtual int RtxSendStatus() const = 0;
    264 
    265  // Returns the SSRC used for RTX if set, otherwise a nullopt.
    266  virtual std::optional<uint32_t> RtxSsrc() const = 0;
    267 
    268  // Sets the payload type to use when sending RTX packets. Note that this
    269  // doesn't enable RTX, only the payload type is set.
    270  virtual void SetRtxSendPayloadType(int payload_type,
    271                                     int associated_payload_type) = 0;
    272 
    273  // Returns the FlexFEC SSRC, if there is one.
    274  virtual std::optional<uint32_t> FlexfecSsrc() const = 0;
    275 
    276  // Sets sending status. Sends kRtcpByeCode when going from true to false.
    277  // Returns -1 on failure else 0.
    278  virtual int32_t SetSendingStatus(bool sending) = 0;
    279 
    280  // Returns current sending status.
    281  virtual bool Sending() const = 0;
    282 
    283  // Starts/Stops media packets. On by default.
    284  virtual void SetSendingMediaStatus(bool sending) = 0;
    285 
    286  // Returns current media sending status.
    287  virtual bool SendingMedia() const = 0;
    288 
    289  // Returns whether audio is configured (i.e. Configuration::audio = true).
    290  virtual bool IsAudioConfigured() const = 0;
    291 
    292  // Indicate that the packets sent by this module should be counted towards the
    293  // bitrate estimate since the stream participates in the bitrate allocation.
    294  virtual void SetAsPartOfAllocation(bool part_of_allocation) = 0;
    295 
    296  // Returns bitrate sent (post-pacing) per packet type.
    297  virtual RtpSendRates GetSendRates() const = 0;
    298 
    299  virtual RTPSender* RtpSender() = 0;
    300  virtual const RTPSender* RtpSender() const = 0;
    301 
    302  // Record that a frame is about to be sent. Returns true on success, and false
    303  // if the module isn't ready to send.
    304  virtual bool OnSendingRtpFrame(uint32_t timestamp,
    305                                 int64_t capture_time_ms,
    306                                 int payload_type,
    307                                 bool force_sender_report) = 0;
    308 
    309  // Try to send the provided packet. Returns true iff packet matches any of
    310  // the SSRCs for this module (media/rtx/fec etc) and was forwarded to the
    311  // transport.
    312  virtual bool TrySendPacket(std::unique_ptr<RtpPacketToSend> packet,
    313                             const PacedPacketInfo& pacing_info) = 0;
    314 
    315  //  Returns true if the module can send media packets and the module is ready
    316  //  so send `packet` A RTP Sequence numbers may or may not have been assigned
    317  //  to the packet.
    318  virtual bool CanSendPacket(const RtpPacketToSend& packet) const = 0;
    319 
    320  //  Assigns continuous RTP sequence number to packet.
    321  virtual void AssignSequenceNumber(RtpPacketToSend& packet) = 0;
    322 
    323  // Send the packet to transport. Before using this method, a caller must
    324  // ensure the packet can be sent by first checking if the packet can be sent
    325  // using CanSendPacket and the packet must be assigned a sequence number using
    326  // AssignSequenceNumber.
    327  virtual void SendPacket(std::unique_ptr<RtpPacketToSend> packet,
    328                          const PacedPacketInfo& pacing_info) = 0;
    329 
    330  // Notifies that a batch of packet sends is completed. The implementation
    331  // can use this to optimize packet sending.
    332  virtual void OnBatchComplete() = 0;
    333 
    334  // Update the FEC protection parameters to use for delta- and key-frames.
    335  // Only used when deferred FEC is active.
    336  virtual void SetFecProtectionParams(
    337      const FecProtectionParams& delta_params,
    338      const FecProtectionParams& key_params) = 0;
    339 
    340  // If deferred FEC generation is enabled, this method should be called after
    341  // calling TrySendPacket(). Any generated FEC packets will be removed and
    342  // returned from the FEC generator.
    343  virtual std::vector<std::unique_ptr<RtpPacketToSend>> FetchFecPackets() = 0;
    344 
    345  virtual void OnAbortedRetransmissions(
    346      ArrayView<const uint16_t> sequence_numbers) = 0;
    347 
    348  virtual void OnPacketsAcknowledged(
    349      ArrayView<const uint16_t> sequence_numbers) = 0;
    350 
    351  virtual std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
    352      size_t target_size_bytes) = 0;
    353 
    354  virtual std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos(
    355      ArrayView<const uint16_t> sequence_numbers) const = 0;
    356 
    357  // Returns an expected per packet overhead representing the main RTP header,
    358  // any CSRCs, and the registered header extensions that are expected on all
    359  // packets (i.e. disregarding things like abs capture time which is only
    360  // populated on a subset of packets, but counting MID/RID type extensions
    361  // when we expect to send them).
    362  virtual size_t ExpectedPerPacketOverhead() const = 0;
    363 
    364  // Access to packet state (e.g. sequence numbering) must only be access by
    365  // one thread at a time. It may be only one thread, or a construction thread
    366  // that calls SetRtpState() - handing over to a pacer thread that calls
    367  // TrySendPacket() - and at teardown ownership is handed to a destruciton
    368  // thread that calls GetRtpState().
    369  // This method is used to signal that "ownership" of the rtp state is being
    370  // transferred to another thread.
    371  virtual void OnPacketSendingThreadSwitched() = 0;
    372 
    373  // **************************************************************************
    374  // RTCP
    375  // **************************************************************************
    376 
    377  // Returns RTCP status.
    378  virtual RtcpMode RTCP() const = 0;
    379 
    380  // Sets RTCP status i.e on(compound or non-compound)/off.
    381  // `method` - RTCP method to use.
    382  virtual void SetRTCPStatus(RtcpMode method) = 0;
    383 
    384  // Sets RTCP CName (i.e unique identifier).
    385  // Returns -1 on failure else 0.
    386  virtual int32_t SetCNAME(absl::string_view cname) = 0;
    387 
    388  // Returns current RTT (round-trip time) estimate.
    389  virtual std::optional<TimeDelta> LastRtt() const = 0;
    390 
    391  // Returns the estimated RTT, with fallback to a default value.
    392  virtual TimeDelta ExpectedRetransmissionTime() const = 0;
    393 
    394  // Forces a send of a RTCP packet. Periodic SR and RR are triggered via the
    395  // process function.
    396  // Returns -1 on failure else 0.
    397  virtual int32_t SendRTCP(RTCPPacketType rtcp_packet_type) = 0;
    398 
    399  // Returns send statistics for the RTP and RTX stream.
    400  virtual void GetSendStreamDataCounters(
    401      StreamDataCounters* rtp_counters,
    402      StreamDataCounters* rtx_counters) const = 0;
    403 
    404 
    405  // Returns packet count, octet count, and timestamps from RTCP sender report.
    406  virtual void RemoteRTCPSenderInfo(uint32_t* packet_count,
    407                                    uint32_t* octet_count,
    408                                    int64_t* ntp_timestamp_ms,
    409                                    int64_t* remote_ntp_timestamp_ms) const = 0;
    410  // A snapshot of Report Blocks with additional data of interest to statistics.
    411  // Within this list, the sender-source SSRC pair is unique and per-pair the
    412  // ReportBlockData represents the latest Report Block that was received for
    413  // that pair.
    414  virtual std::vector<ReportBlockData> GetLatestReportBlockData() const = 0;
    415  // Returns stats based on the received RTCP SRs.
    416  virtual std::optional<SenderReportStats> GetSenderReportStats() const = 0;
    417  // Returns non-sender RTT stats, based on DLRR.
    418  virtual std::optional<NonSenderRttStats> GetNonSenderRttStats() const = 0;
    419 
    420  // (REMB) Receiver Estimated Max Bitrate.
    421  // Schedules sending REMB on next and following sender/receiver reports.
    422  void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) override = 0;
    423  // Stops sending REMB on next and following sender/receiver reports.
    424  void UnsetRemb() override = 0;
    425 
    426  // (NACK)
    427 
    428  // Sends a Negative acknowledgement packet.
    429  // Returns -1 on failure else 0.
    430  // TODO(philipel): Deprecate this and start using SendNack instead, mostly
    431  // because we want a function that actually send NACK for the specified
    432  // packets.
    433  virtual int32_t SendNACK(const uint16_t* nack_list, uint16_t size) = 0;
    434 
    435  // Sends NACK for the packets specified.
    436  // Note: This assumes the caller keeps track of timing and doesn't rely on
    437  // the RTP module to do this.
    438  virtual void SendNack(const std::vector<uint16_t>& sequence_numbers) = 0;
    439 
    440  // Store the sent packets, needed to answer to a Negative acknowledgment
    441  // requests.
    442  virtual void SetStorePacketsStatus(bool enable, uint16_t numberToStore) = 0;
    443 
    444  virtual void SetVideoBitrateAllocation(
    445      const VideoBitrateAllocation& bitrate) = 0;
    446 
    447  // **************************************************************************
    448  // Video
    449  // **************************************************************************
    450 
    451  // Requests new key frame.
    452  // using PLI, https://tools.ietf.org/html/rfc4585#section-6.3.1.1
    453  void SendPictureLossIndication() { SendRTCP(kRtcpPli); }
    454  // using FIR, https://tools.ietf.org/html/rfc5104#section-4.3.1.2
    455  void SendFullIntraRequest() { SendRTCP(kRtcpFir); }
    456 
    457  // Sends a LossNotification RTCP message.
    458  // Returns -1 on failure else 0.
    459  virtual int32_t SendLossNotification(uint16_t last_decoded_seq_num,
    460                                       uint16_t last_received_seq_num,
    461                                       bool decodability_flag,
    462                                       bool buffering_allowed) = 0;
    463 };
    464 
    465 }  // namespace webrtc
    466 
    467 #endif  // MODULES_RTP_RTCP_SOURCE_RTP_RTCP_INTERFACE_H_