tor-browser

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

packet_buffer.h (4364B)


      1 /*
      2 *  Copyright (c) 2016 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_VIDEO_CODING_PACKET_BUFFER_H_
     12 #define MODULES_VIDEO_CODING_PACKET_BUFFER_H_
     13 
     14 #include <cstddef>
     15 #include <cstdint>
     16 #include <memory>
     17 #include <optional>
     18 #include <set>
     19 #include <vector>
     20 
     21 #include "absl/base/attributes.h"
     22 #include "api/video/video_codec_type.h"
     23 #include "modules/rtp_rtcp/source/rtp_packet_received.h"
     24 #include "modules/rtp_rtcp/source/rtp_video_header.h"
     25 #include "rtc_base/copy_on_write_buffer.h"
     26 #include "rtc_base/numerics/sequence_number_util.h"
     27 
     28 namespace webrtc {
     29 namespace video_coding {
     30 
     31 class PacketBuffer {
     32 public:
     33  struct Packet {
     34    Packet() = default;
     35    Packet(const RtpPacketReceived& rtp_packet,
     36           int64_t sequence_number,
     37           const RTPVideoHeader& video_header);
     38    Packet(const Packet&) = delete;
     39    Packet(Packet&&) = delete;
     40    Packet& operator=(const Packet&) = delete;
     41    Packet& operator=(Packet&&) = delete;
     42    ~Packet() = default;
     43 
     44    VideoCodecType codec() const { return video_header.codec; }
     45    int width() const { return video_header.width; }
     46    int height() const { return video_header.height; }
     47 
     48    bool is_first_packet_in_frame() const {
     49      return video_header.is_first_packet_in_frame;
     50    }
     51    bool is_last_packet_in_frame() const {
     52      return video_header.is_last_packet_in_frame;
     53    }
     54    uint16_t seq_num() const { return static_cast<uint16_t>(sequence_number); }
     55 
     56    // If all its previous packets have been inserted into the packet buffer.
     57    // Set and used internally by the PacketBuffer.
     58    bool continuous = false;
     59    bool marker_bit = false;
     60    uint8_t payload_type = 0;
     61    int64_t sequence_number = 0;
     62    uint32_t timestamp = 0;
     63    int times_nacked = -1;
     64 
     65    CopyOnWriteBuffer video_payload;
     66    RTPVideoHeader video_header;
     67  };
     68  struct InsertResult {
     69    std::vector<std::unique_ptr<Packet>> packets;
     70    // Indicates if the packet buffer was cleared, which means that a key
     71    // frame request should be sent.
     72    bool buffer_cleared = false;
     73  };
     74 
     75  // Both `start_buffer_size` and `max_buffer_size` must be a power of 2.
     76  PacketBuffer(size_t start_buffer_size, size_t max_buffer_size);
     77  ~PacketBuffer();
     78 
     79  ABSL_MUST_USE_RESULT InsertResult
     80  InsertPacket(std::unique_ptr<Packet> packet);
     81  ABSL_MUST_USE_RESULT InsertResult InsertPadding(uint16_t seq_num);
     82 
     83  // Clear all packets older than |seq_num|. Returns the number of packets
     84  // cleared.
     85  uint32_t ClearTo(uint16_t seq_num);
     86  void Clear();
     87 
     88  void ForceSpsPpsIdrIsH264Keyframe();
     89  void ResetSpsPpsIdrIsH264Keyframe();
     90 
     91 private:
     92  void ClearInternal();
     93 
     94  // Tries to expand the buffer.
     95  bool ExpandBufferSize();
     96 
     97  // Test if all previous packets has arrived for the given sequence number.
     98  bool PotentialNewFrame(uint16_t seq_num) const;
     99 
    100  // Test if all packets of a frame has arrived, and if so, returns packets to
    101  // create frames.
    102  std::vector<std::unique_ptr<Packet>> FindFrames(uint16_t seq_num);
    103 
    104  void UpdateMissingPackets(uint16_t seq_num);
    105 
    106  // buffer_.size() and max_size_ must always be a power of two.
    107  const size_t max_size_;
    108 
    109  // The fist sequence number currently in the buffer.
    110  uint16_t first_seq_num_;
    111 
    112  // If the packet buffer has received its first packet.
    113  bool first_packet_received_;
    114 
    115  // If the buffer is cleared to `first_seq_num_`.
    116  bool is_cleared_to_first_seq_num_;
    117 
    118  // Buffer that holds the the inserted packets and information needed to
    119  // determine continuity between them.
    120  std::vector<std::unique_ptr<Packet>> buffer_;
    121 
    122  std::optional<uint16_t> newest_inserted_seq_num_;
    123  std::set<uint16_t, DescendingSeqNumComp<uint16_t>> missing_packets_;
    124 
    125  std::set<uint16_t, DescendingSeqNumComp<uint16_t>> received_padding_;
    126 
    127  // Indicates if we should require SPS, PPS, and IDR for a particular
    128  // RTP timestamp to treat the corresponding frame as a keyframe.
    129  bool sps_pps_idr_is_h264_keyframe_;
    130 };
    131 
    132 }  // namespace video_coding
    133 }  // namespace webrtc
    134 
    135 #endif  // MODULES_VIDEO_CODING_PACKET_BUFFER_H_