tor-browser

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

packet_buffer.h (5722B)


      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_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_
     12 #define MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_
     13 
     14 #include <cstddef>
     15 #include <cstdint>
     16 #include <optional>
     17 
     18 #include "modules/audio_coding/neteq/decoder_database.h"
     19 #include "modules/audio_coding/neteq/packet.h"
     20 #include "modules/include/module_common_types_public.h"  // IsNewerTimestamp
     21 
     22 namespace webrtc {
     23 
     24 class DecoderDatabase;
     25 class StatisticsCalculator;
     26 class TickTimer;
     27 
     28 // This is the actual buffer holding the packets before decoding.
     29 class PacketBuffer {
     30 public:
     31  enum BufferReturnCodes {
     32    kOK = 0,
     33    kFlushed,
     34    kNotFound,
     35    kBufferEmpty,
     36    kInvalidPacket,
     37    kInvalidPointer
     38  };
     39 
     40  // Constructor creates a buffer which can hold a maximum of
     41  // `max_number_of_packets` packets.
     42  PacketBuffer(size_t max_number_of_packets,
     43               const TickTimer* tick_timer,
     44               StatisticsCalculator* stats);
     45 
     46  // Deletes all packets in the buffer before destroying the buffer.
     47  virtual ~PacketBuffer();
     48 
     49  PacketBuffer(const PacketBuffer&) = delete;
     50  PacketBuffer& operator=(const PacketBuffer&) = delete;
     51 
     52  // Flushes the buffer and deletes all packets in it.
     53  virtual void Flush();
     54 
     55  // Returns true for an empty buffer.
     56  virtual bool Empty() const;
     57 
     58  // Inserts `packet` into the buffer. The buffer will take over ownership of
     59  // the packet object.
     60  // Returns PacketBuffer::kOK on success, PacketBuffer::kFlushed if the buffer
     61  // was flushed due to overfilling.
     62  virtual int InsertPacket(Packet&& packet);
     63 
     64  // Gets the timestamp for the first packet in the buffer and writes it to the
     65  // output variable `next_timestamp`.
     66  // Returns PacketBuffer::kBufferEmpty if the buffer is empty,
     67  // PacketBuffer::kOK otherwise.
     68  virtual int NextTimestamp(uint32_t* next_timestamp) const;
     69 
     70  // Gets the timestamp for the first packet in the buffer with a timestamp no
     71  // lower than the input limit `timestamp`. The result is written to the output
     72  // variable `next_timestamp`.
     73  // Returns PacketBuffer::kBufferEmpty if the buffer is empty,
     74  // PacketBuffer::kOK otherwise.
     75  virtual int NextHigherTimestamp(uint32_t timestamp,
     76                                  uint32_t* next_timestamp) const;
     77 
     78  // Returns a (constant) pointer to the first packet in the buffer. Returns
     79  // NULL if the buffer is empty.
     80  virtual const Packet* PeekNextPacket() const;
     81 
     82  // Extracts the first packet in the buffer and returns it.
     83  // Returns an empty optional if the buffer is empty.
     84  virtual std::optional<Packet> GetNextPacket();
     85 
     86  // Discards the first packet in the buffer. The packet is deleted.
     87  // Returns PacketBuffer::kBufferEmpty if the buffer is empty,
     88  // PacketBuffer::kOK otherwise.
     89  virtual int DiscardNextPacket();
     90 
     91  // Discards all packets that are (strictly) older than timestamp_limit,
     92  // but newer than timestamp_limit - horizon_samples. Setting horizon_samples
     93  // to zero implies that the horizon is set to half the timestamp range. That
     94  // is, if a packet is more than 2^31 timestamps into the future compared with
     95  // timestamp_limit (including wrap-around), it is considered old.
     96  virtual void DiscardOldPackets(uint32_t timestamp_limit,
     97                                 uint32_t horizon_samples);
     98 
     99  // Discards all packets that are (strictly) older than timestamp_limit.
    100  virtual void DiscardAllOldPackets(uint32_t timestamp_limit);
    101 
    102  // Removes all packets with a specific payload type from the buffer.
    103  virtual void DiscardPacketsWithPayloadType(uint8_t payload_type);
    104 
    105  // Returns the number of packets in the buffer, including duplicates and
    106  // redundant packets.
    107  virtual size_t NumPacketsInBuffer() const;
    108 
    109  // Returns the number of samples in the buffer, including samples carried in
    110  // duplicate and redundant packets.
    111  virtual size_t NumSamplesInBuffer(size_t last_decoded_length) const;
    112 
    113  // Returns the total duration in samples that the packets in the buffer spans
    114  // across.
    115  virtual size_t GetSpanSamples(size_t last_decoded_length,
    116                                size_t sample_rate,
    117                                bool count_waiting_time) const;
    118 
    119  // Returns true if the packet buffer contains any DTX or CNG packets.
    120  virtual bool ContainsDtxOrCngPacket(
    121      const DecoderDatabase* decoder_database) const;
    122 
    123  // Static method returning true if `timestamp` is older than `timestamp_limit`
    124  // but less than `horizon_samples` behind `timestamp_limit`. For instance,
    125  // with timestamp_limit = 100 and horizon_samples = 10, a timestamp in the
    126  // range (90, 100) is considered obsolete, and will yield true.
    127  // Setting `horizon_samples` to 0 is the same as setting it to 2^31, i.e.,
    128  // half the 32-bit timestamp range.
    129  static bool IsObsoleteTimestamp(uint32_t timestamp,
    130                                  uint32_t timestamp_limit,
    131                                  uint32_t horizon_samples) {
    132    return IsNewerTimestamp(timestamp_limit, timestamp) &&
    133           (horizon_samples == 0 ||
    134            IsNewerTimestamp(timestamp, timestamp_limit - horizon_samples));
    135  }
    136 
    137 private:
    138  void LogPacketDiscarded(int codec_level);
    139 
    140  size_t max_number_of_packets_;
    141  PacketList buffer_;
    142  const TickTimer* tick_timer_;
    143  StatisticsCalculator* stats_;
    144 };
    145 
    146 }  // namespace webrtc
    147 #endif  // MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_