packet_buffer.h 6.4 KB

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