tor-browser

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

rtp_rtcp_defines.h (15351B)


      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_INCLUDE_RTP_RTCP_DEFINES_H_
     12 #define MODULES_RTP_RTCP_INCLUDE_RTP_RTCP_DEFINES_H_
     13 
     14 #include <stddef.h>
     15 
     16 #include <array>
     17 #include <cstddef>
     18 #include <cstdint>
     19 #include <memory>
     20 #include <optional>
     21 #include <vector>
     22 
     23 #include "absl/algorithm/container.h"
     24 #include "absl/strings/string_view.h"
     25 #include "api/array_view.h"
     26 #include "api/transport/network_types.h"
     27 #include "api/units/data_rate.h"
     28 #include "api/units/time_delta.h"
     29 #include "api/units/timestamp.h"
     30 #include "modules/rtp_rtcp/include/report_block_data.h"
     31 #include "modules/rtp_rtcp/source/rtcp_packet.h"
     32 #include "modules/rtp_rtcp/source/rtcp_packet/congestion_control_feedback.h"
     33 #include "rtc_base/checks.h"
     34 
     35 #define RTCP_CNAME_SIZE 256  // RFC 3550 page 44, including null termination
     36 #define IP_PACKET_SIZE 1500  // we assume ethernet
     37 
     38 namespace webrtc {
     39 class RtpPacket;
     40 class RtpPacketToSend;
     41 class RtpPacketReceived;
     42 
     43 namespace rtcp {
     44 class TransportFeedback;
     45 }  // namespace rtcp
     46 
     47 const int kVideoPayloadTypeFrequency = 90000;
     48 
     49 // TODO(bugs.webrtc.org/6458): Remove this when all the depending projects are
     50 // updated to correctly set rtp rate for RtcpSender.
     51 const int kBogusRtpRateForAudioRtcp = 8000;
     52 
     53 // Minimum RTP header size in bytes.
     54 const uint8_t kRtpHeaderSize = 12;
     55 
     56 bool IsLegalMidName(absl::string_view name);
     57 bool IsLegalRsidName(absl::string_view name);
     58 
     59 // This enum must not have any gaps, i.e., all integers between
     60 // kRtpExtensionNone and kRtpExtensionNumberOfExtensions must be valid enum
     61 // entries.
     62 enum RTPExtensionType : int {
     63  kRtpExtensionNone,
     64  kRtpExtensionTransmissionTimeOffset,
     65  kRtpExtensionAudioLevel,
     66  kRtpExtensionCsrcAudioLevel,
     67  kRtpExtensionInbandComfortNoise,
     68  kRtpExtensionAbsoluteSendTime,
     69  kRtpExtensionAbsoluteCaptureTime,
     70  kRtpExtensionVideoRotation,
     71  kRtpExtensionTransportSequenceNumber,
     72  kRtpExtensionTransportSequenceNumber02,
     73  kRtpExtensionPlayoutDelay,
     74  kRtpExtensionVideoContentType,
     75  kRtpExtensionVideoLayersAllocation,
     76  kRtpExtensionVideoTiming,
     77  kRtpExtensionRtpStreamId,
     78  kRtpExtensionRepairedRtpStreamId,
     79  kRtpExtensionMid,
     80  kRtpExtensionGenericFrameDescriptor,
     81  kRtpExtensionGenericFrameDescriptor00 [[deprecated]] =
     82      kRtpExtensionGenericFrameDescriptor,
     83  kRtpExtensionDependencyDescriptor,
     84  kRtpExtensionGenericFrameDescriptor02 [[deprecated]] =
     85      kRtpExtensionDependencyDescriptor,
     86  kRtpExtensionColorSpace,
     87  kRtpExtensionVideoFrameTrackingId,
     88  kRtpExtensionCorruptionDetection,
     89  kRtpExtensionNumberOfExtensions  // Must be the last entity in the enum.
     90 };
     91 
     92 enum RTCPAppSubTypes { kAppSubtypeBwe = 0x00 };
     93 
     94 // TODO(sprang): Make this an enum class once rtcp_receiver has been cleaned up.
     95 enum RTCPPacketType : uint32_t {
     96  kRtcpReport = 0x0001,
     97  kRtcpSr = 0x0002,
     98  kRtcpRr = 0x0004,
     99  kRtcpSdes = 0x0008,
    100  kRtcpBye = 0x0010,
    101  kRtcpPli = 0x0020,
    102  kRtcpNack = 0x0040,
    103  kRtcpFir = 0x0080,
    104  kRtcpTmmbr = 0x0100,
    105  kRtcpTmmbn = 0x0200,
    106  kRtcpSrReq = 0x0400,
    107  kRtcpLossNotification = 0x2000,
    108  kRtcpRemb = 0x10000,
    109  kRtcpTransmissionTimeOffset = 0x20000,
    110  kRtcpXrReceiverReferenceTime = 0x40000,
    111  kRtcpXrDlrrReportBlock = 0x80000,
    112  kRtcpTransportFeedback = 0x100000,
    113  kRtcpXrTargetBitrate = 0x200000,
    114 };
    115 
    116 enum class KeyFrameReqMethod : uint8_t {
    117  kNone,     // Don't request keyframes.
    118  kPliRtcp,  // Request keyframes through Picture Loss Indication.
    119  kFirRtcp   // Request keyframes through Full Intra-frame Request.
    120 };
    121 
    122 enum RtxMode {
    123  kRtxOff = 0x0,
    124  kRtxRetransmitted = 0x1,     // Only send retransmissions over RTX.
    125  kRtxRedundantPayloads = 0x2  // Preventively send redundant payloads
    126                               // instead of padding.
    127 };
    128 
    129 const size_t kRtxHeaderSize = 2;
    130 
    131 struct RtpState {
    132  uint16_t sequence_number = 0;
    133  uint32_t start_timestamp = 0;
    134  uint32_t timestamp = 0;
    135  Timestamp capture_time = Timestamp::MinusInfinity();
    136  Timestamp last_timestamp_time = Timestamp::MinusInfinity();
    137  bool ssrc_has_acked = false;
    138 };
    139 
    140 class RtcpIntraFrameObserver {
    141 public:
    142  virtual ~RtcpIntraFrameObserver() {}
    143 
    144  virtual void OnReceivedIntraFrameRequest(uint32_t ssrc) = 0;
    145 };
    146 
    147 // Observer for incoming LossNotification RTCP messages.
    148 // See the documentation of LossNotification for details.
    149 class RtcpLossNotificationObserver {
    150 public:
    151  virtual ~RtcpLossNotificationObserver() = default;
    152 
    153  virtual void OnReceivedLossNotification(uint32_t ssrc,
    154                                          uint16_t seq_num_of_last_decodable,
    155                                          uint16_t seq_num_of_last_received,
    156                                          bool decodability_flag) = 0;
    157 };
    158 
    159 // Interface to watch incoming rtcp packets related to the link in general.
    160 // All message handlers have default empty implementation. This way users only
    161 // need to implement the ones they are interested in.
    162 // All message handles pass `receive_time` parameter, which is receive time
    163 // of the rtcp packet that triggered the update.
    164 class NetworkLinkRtcpObserver {
    165 public:
    166  virtual ~NetworkLinkRtcpObserver() = default;
    167 
    168  virtual void OnTransportFeedback(
    169      Timestamp /* receive_time */,
    170      const rtcp::TransportFeedback& /* feedback */) {}
    171  // RFC 8888 congestion control feedback.
    172  virtual void OnCongestionControlFeedback(
    173      Timestamp /* receive_time */,
    174      const rtcp::CongestionControlFeedback& /* feedback */) {}
    175  virtual void OnReceiverEstimatedMaxBitrate(Timestamp /* receive_time */,
    176                                             DataRate /* bitrate */) {}
    177 
    178  // Called on an RTCP packet with sender or receiver reports with non zero
    179  // report blocks. Report blocks are combined from all reports into one array.
    180  virtual void OnReport(Timestamp /* receive_time */,
    181                        ArrayView<const ReportBlockData> /* report_blocks */) {}
    182  virtual void OnRttUpdate(Timestamp /* receive_time */, TimeDelta /* rtt */) {}
    183 };
    184 
    185 class RtcpEventObserver {
    186 public:
    187  virtual void OnRtcpBye() = 0;
    188  virtual void OnRtcpTimeout() = 0;
    189 
    190  virtual ~RtcpEventObserver() {}
    191 };
    192 
    193 // NOTE! `kNumMediaTypes` must be kept in sync with RtpPacketMediaType!
    194 static constexpr size_t kNumMediaTypes = 5;
    195 enum class RtpPacketMediaType : size_t {
    196  kAudio,                         // Audio media packets.
    197  kVideo,                         // Video media packets.
    198  kRetransmission,                // Retransmisions, sent as response to NACK.
    199  kForwardErrorCorrection,        // FEC packets.
    200  kPadding = kNumMediaTypes - 1,  // RTX or plain padding sent to maintain BWE.
    201  // Again, don't forget to update `kNumMediaTypes` if you add another value!
    202 };
    203 
    204 struct RtpPacketSendInfo {
    205  static RtpPacketSendInfo From(const RtpPacketToSend& rtp_packet_to_send,
    206                                const PacedPacketInfo& pacing_info);
    207 
    208  uint16_t transport_sequence_number = 0;
    209  std::optional<uint32_t> media_ssrc;
    210  uint16_t rtp_sequence_number = 0;  // Only valid if `media_ssrc` is set.
    211  uint32_t rtp_timestamp = 0;
    212  size_t length = 0;
    213  std::optional<RtpPacketMediaType> packet_type;
    214  PacedPacketInfo pacing_info;
    215 };
    216 
    217 class NetworkStateEstimateObserver {
    218 public:
    219  virtual void OnRemoteNetworkEstimate(NetworkStateEstimate estimate) = 0;
    220  virtual ~NetworkStateEstimateObserver() = default;
    221 };
    222 
    223 class TransportFeedbackObserver {
    224 public:
    225  virtual ~TransportFeedbackObserver() = default;
    226 
    227  virtual void OnAddPacket(const RtpPacketSendInfo& packet_info) = 0;
    228 };
    229 
    230 // Interface for PacketRouter to send rtcp feedback on behalf of
    231 // congestion controller.
    232 // TODO(bugs.webrtc.org/8239): Remove and use RtcpTransceiver directly
    233 // when RtcpTransceiver always present in rtp transport.
    234 class RtcpFeedbackSenderInterface {
    235 public:
    236  virtual ~RtcpFeedbackSenderInterface() = default;
    237  virtual void SendCombinedRtcpPacket(
    238      std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) = 0;
    239  virtual void SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) = 0;
    240  virtual void UnsetRemb() = 0;
    241 };
    242 
    243 class StreamFeedbackObserver {
    244 public:
    245  struct StreamPacketInfo {
    246    bool received;
    247 
    248    // `rtp_sequence_number` and `is_retransmission` are only valid if `ssrc`
    249    // is populated.
    250    std::optional<uint32_t> ssrc;
    251    uint16_t rtp_sequence_number;
    252    bool is_retransmission;
    253  };
    254  virtual ~StreamFeedbackObserver() = default;
    255 
    256  virtual void OnPacketFeedbackVector(
    257      std::vector<StreamPacketInfo> packet_feedback_vector) = 0;
    258 };
    259 
    260 class StreamFeedbackProvider {
    261 public:
    262  virtual void RegisterStreamFeedbackObserver(
    263      std::vector<uint32_t> ssrcs,
    264      StreamFeedbackObserver* observer) = 0;
    265  virtual void DeRegisterStreamFeedbackObserver(
    266      StreamFeedbackObserver* observer) = 0;
    267  virtual ~StreamFeedbackProvider() = default;
    268 };
    269 
    270 class RtcpRttStats {
    271 public:
    272  virtual ~RtcpRttStats() = default;
    273  virtual void OnRttUpdate(int64_t rtt) = 0;
    274 };
    275 
    276 struct RtpPacketCounter {
    277  RtpPacketCounter()
    278      : header_bytes(0),
    279        payload_bytes(0),
    280        padding_bytes(0),
    281        packets(0),
    282        packets_with_ect1(0),
    283        packets_with_ce(0) {}
    284 
    285  explicit RtpPacketCounter(const RtpPacket& packet);
    286  explicit RtpPacketCounter(const RtpPacketToSend& packet_to_send);
    287  explicit RtpPacketCounter(const RtpPacketReceived& packet_received);
    288 
    289  void Add(const RtpPacketCounter& other) {
    290    header_bytes += other.header_bytes;
    291    payload_bytes += other.payload_bytes;
    292    padding_bytes += other.padding_bytes;
    293    packets += other.packets;
    294    packets_with_ect1 += other.packets_with_ect1;
    295    packets_with_ce += other.packets_with_ce;
    296    total_packet_delay += other.total_packet_delay;
    297  }
    298 
    299  bool operator==(const RtpPacketCounter& other) const {
    300    return header_bytes == other.header_bytes &&
    301           payload_bytes == other.payload_bytes &&
    302           padding_bytes == other.padding_bytes && packets == other.packets &&
    303           packets_with_ect1 == other.packets_with_ect1 &&
    304           packets_with_ce == other.packets_with_ce &&
    305           total_packet_delay == other.total_packet_delay;
    306  }
    307 
    308  // Not inlined, since use of RtpPacket would result in circular includes.
    309  void AddPacket(const RtpPacket& packet);
    310  void AddPacket(const RtpPacketToSend& packet_to_send);
    311  void AddPacket(const RtpPacketReceived& packet_received);
    312 
    313  size_t TotalBytes() const {
    314    return header_bytes + payload_bytes + padding_bytes;
    315  }
    316 
    317  size_t header_bytes;   // Number of bytes used by RTP headers.
    318  size_t payload_bytes;  // Payload bytes, excluding RTP headers and padding.
    319  size_t padding_bytes;  // Number of padding bytes.
    320  size_t packets;        // Number of packets.
    321  size_t packets_with_ect1;  // Number of packets with ECT1 flag set to true.
    322  size_t packets_with_ce;    // Number of packets with CE flag set to true.
    323  // The total delay of all `packets`. For RtpPacketToSend packets, this is
    324  // `time_in_send_queue()`. For receive packets, this is zero.
    325  TimeDelta total_packet_delay = TimeDelta::Zero();
    326 };
    327 
    328 // Data usage statistics for a (rtp) stream.
    329 struct StreamDataCounters {
    330  StreamDataCounters();
    331 
    332  void Add(const StreamDataCounters& other) {
    333    transmitted.Add(other.transmitted);
    334    retransmitted.Add(other.retransmitted);
    335    fec.Add(other.fec);
    336    if (other.first_packet_time < first_packet_time) {
    337      // Use oldest time (excluding unsed value represented as plus infinity.
    338      first_packet_time = other.first_packet_time;
    339    }
    340  }
    341 
    342  void MaybeSetFirstPacketTime(Timestamp now) {
    343    if (first_packet_time == Timestamp::PlusInfinity()) {
    344      first_packet_time = now;
    345    }
    346  }
    347 
    348  // Return time since first packet is send/received, or zero if such event
    349  // haven't happen.
    350  TimeDelta TimeSinceFirstPacket(Timestamp now) const {
    351    return first_packet_time == Timestamp::PlusInfinity()
    352               ? TimeDelta::Zero()
    353               : now - first_packet_time;
    354  }
    355 
    356  // Returns the number of bytes corresponding to the actual media payload (i.e.
    357  // RTP headers, padding, retransmissions and fec packets are excluded).
    358  // Note this function does not have meaning for an RTX stream.
    359  size_t MediaPayloadBytes() const {
    360    return transmitted.payload_bytes - retransmitted.payload_bytes -
    361           fec.payload_bytes;
    362  }
    363 
    364  // Time when first packet is sent/received.
    365  Timestamp first_packet_time = Timestamp::PlusInfinity();
    366 
    367  RtpPacketCounter transmitted;    // Number of transmitted packets/bytes.
    368  RtpPacketCounter retransmitted;  // Number of retransmitted packets/bytes.
    369  RtpPacketCounter fec;            // Number of redundancy packets/bytes.
    370 };
    371 
    372 class RtpSendRates {
    373 public:
    374  constexpr RtpSendRates() = default;
    375  RtpSendRates(const RtpSendRates& rhs) = default;
    376  RtpSendRates& operator=(const RtpSendRates&) = default;
    377 
    378  DataRate& operator[](RtpPacketMediaType type) {
    379    return send_rates_[static_cast<size_t>(type)];
    380  }
    381  const DataRate& operator[](RtpPacketMediaType type) const {
    382    return send_rates_[static_cast<size_t>(type)];
    383  }
    384  DataRate Sum() const {
    385    return absl::c_accumulate(send_rates_, DataRate::Zero());
    386  }
    387 
    388 private:
    389  std::array<DataRate, kNumMediaTypes> send_rates_;
    390 };
    391 
    392 // Callback, called whenever byte/packet counts have been updated.
    393 class StreamDataCountersCallback {
    394 public:
    395  virtual ~StreamDataCountersCallback() {}
    396 
    397  // TODO: webrtc:40644448 - Make this pure virtual.
    398  virtual StreamDataCounters GetDataCounters(uint32_t ssrc) const {
    399    RTC_CHECK_NOTREACHED();
    400  }
    401  virtual void DataCountersUpdated(const StreamDataCounters& counters,
    402                                   uint32_t ssrc) = 0;
    403 };
    404 
    405 // Information exposed through the GetStats api.
    406 struct RtpReceiveStats {
    407  // `packets_lost` and `jitter` are defined by RFC 3550, and exposed in the
    408  // RTCReceivedRtpStreamStats dictionary, see
    409  // https://w3c.github.io/webrtc-stats/#receivedrtpstats-dict*
    410  int32_t packets_lost = 0;
    411  // Interarrival jitter in samples.
    412  uint32_t jitter = 0;
    413  // Interarrival jitter in time.
    414  TimeDelta interarrival_jitter = TimeDelta::Zero();
    415 
    416  // Time of the last packet received in unix epoch,
    417  // i.e. Timestamp::Zero() represents 1st Jan 1970 00:00
    418  std::optional<Timestamp> last_packet_received;
    419 
    420  // Counters exposed in RTCInboundRtpStreamStats, see
    421  // https://w3c.github.io/webrtc-stats/#inboundrtpstats-dict*
    422  RtpPacketCounter packet_counter;
    423 };
    424 
    425 // Callback, used to notify an observer whenever new rates have been estimated.
    426 class BitrateStatisticsObserver {
    427 public:
    428  virtual ~BitrateStatisticsObserver() {}
    429 
    430  virtual void Notify(uint32_t total_bitrate_bps,
    431                      uint32_t retransmit_bitrate_bps,
    432                      uint32_t ssrc) = 0;
    433 };
    434 
    435 // Callback, used to notify an observer whenever a packet is sent to the
    436 // transport.
    437 class SendPacketObserver {
    438 public:
    439  virtual ~SendPacketObserver() = default;
    440  virtual void OnSendPacket(std::optional<uint16_t> packet_id,
    441                            Timestamp capture_time,
    442                            uint32_t ssrc) = 0;
    443 };
    444 
    445 }  // namespace webrtc
    446 #endif  // MODULES_RTP_RTCP_INCLUDE_RTP_RTCP_DEFINES_H_