henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 11 | #ifndef MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_ |
| 12 | #define MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 14 | #include "api/optional.h" |
| 15 | #include "modules/audio_coding/neteq/packet.h" |
| 16 | #include "modules/include/module_common_types.h" |
| 17 | #include "rtc_base/constructormagic.h" |
| 18 | #include "typedefs.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 22 | class DecoderDatabase; |
minyue-webrtc | fae474c | 2017-07-05 11:17:40 +0200 | [diff] [blame] | 23 | class StatisticsCalculator; |
henrik.lundin | 84f8cd6 | 2016-04-26 07:45:16 -0700 | [diff] [blame] | 24 | class TickTimer; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 25 | |
| 26 | // This is the actual buffer holding the packets before decoding. |
| 27 | class PacketBuffer { |
| 28 | public: |
| 29 | enum BufferReturnCodes { |
| 30 | kOK = 0, |
| 31 | kFlushed, |
| 32 | kNotFound, |
| 33 | kBufferEmpty, |
| 34 | kInvalidPacket, |
henrik.lundin@webrtc.org | 116ed1d | 2014-04-28 08:20:04 +0000 | [diff] [blame] | 35 | kInvalidPointer |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | // Constructor creates a buffer which can hold a maximum of |
henrik.lundin@webrtc.org | 116ed1d | 2014-04-28 08:20:04 +0000 | [diff] [blame] | 39 | // |max_number_of_packets| packets. |
henrik.lundin | 84f8cd6 | 2016-04-26 07:45:16 -0700 | [diff] [blame] | 40 | PacketBuffer(size_t max_number_of_packets, const TickTimer* tick_timer); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 41 | |
| 42 | // Deletes all packets in the buffer before destroying the buffer. |
| 43 | virtual ~PacketBuffer(); |
| 44 | |
| 45 | // Flushes the buffer and deletes all packets in it. |
| 46 | virtual void Flush(); |
| 47 | |
| 48 | // Returns true for an empty buffer. |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 49 | virtual bool Empty() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 50 | |
| 51 | // Inserts |packet| into the buffer. The buffer will take over ownership of |
| 52 | // the packet object. |
| 53 | // Returns PacketBuffer::kOK on success, PacketBuffer::kFlushed if the buffer |
| 54 | // was flushed due to overfilling. |
minyue-webrtc | 12d3084 | 2017-07-19 11:44:06 +0200 | [diff] [blame] | 55 | virtual int InsertPacket(Packet&& packet, StatisticsCalculator* stats); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 56 | |
| 57 | // Inserts a list of packets into the buffer. The buffer will take over |
| 58 | // ownership of the packet objects. |
| 59 | // Returns PacketBuffer::kOK if all packets were inserted successfully. |
| 60 | // If the buffer was flushed due to overfilling, only a subset of the list is |
| 61 | // inserted, and PacketBuffer::kFlushed is returned. |
| 62 | // The last three parameters are included for legacy compatibility. |
| 63 | // TODO(hlundin): Redesign to not use current_*_payload_type and |
| 64 | // decoder_database. |
henrik.lundin | da8bbf6 | 2016-08-31 03:14:11 -0700 | [diff] [blame] | 65 | virtual int InsertPacketList( |
| 66 | PacketList* packet_list, |
| 67 | const DecoderDatabase& decoder_database, |
| 68 | rtc::Optional<uint8_t>* current_rtp_payload_type, |
minyue-webrtc | 12d3084 | 2017-07-19 11:44:06 +0200 | [diff] [blame] | 69 | rtc::Optional<uint8_t>* current_cng_rtp_payload_type, |
| 70 | StatisticsCalculator* stats); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 71 | |
| 72 | // Gets the timestamp for the first packet in the buffer and writes it to the |
| 73 | // output variable |next_timestamp|. |
| 74 | // Returns PacketBuffer::kBufferEmpty if the buffer is empty, |
| 75 | // PacketBuffer::kOK otherwise. |
| 76 | virtual int NextTimestamp(uint32_t* next_timestamp) const; |
| 77 | |
| 78 | // Gets the timestamp for the first packet in the buffer with a timestamp no |
| 79 | // lower than the input limit |timestamp|. The result is written to the output |
| 80 | // variable |next_timestamp|. |
| 81 | // Returns PacketBuffer::kBufferEmpty if the buffer is empty, |
| 82 | // PacketBuffer::kOK otherwise. |
| 83 | virtual int NextHigherTimestamp(uint32_t timestamp, |
| 84 | uint32_t* next_timestamp) const; |
| 85 | |
ossu | 7a37761 | 2016-10-18 04:06:13 -0700 | [diff] [blame] | 86 | // Returns a (constant) pointer to the first packet in the buffer. Returns |
| 87 | // NULL if the buffer is empty. |
| 88 | virtual const Packet* PeekNextPacket() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 89 | |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 90 | // Extracts the first packet in the buffer and returns it. |
| 91 | // Returns an empty optional if the buffer is empty. |
| 92 | virtual rtc::Optional<Packet> GetNextPacket(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 93 | |
| 94 | // Discards the first packet in the buffer. The packet is deleted. |
| 95 | // Returns PacketBuffer::kBufferEmpty if the buffer is empty, |
| 96 | // PacketBuffer::kOK otherwise. |
minyue-webrtc | fae474c | 2017-07-05 11:17:40 +0200 | [diff] [blame] | 97 | virtual int DiscardNextPacket(StatisticsCalculator* stats); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 98 | |
henrik.lundin@webrtc.org | 52b42cb | 2014-11-04 14:03:58 +0000 | [diff] [blame] | 99 | // Discards all packets that are (strictly) older than timestamp_limit, |
| 100 | // but newer than timestamp_limit - horizon_samples. Setting horizon_samples |
| 101 | // to zero implies that the horizon is set to half the timestamp range. That |
| 102 | // is, if a packet is more than 2^31 timestamps into the future compared with |
| 103 | // timestamp_limit (including wrap-around), it is considered old. |
minyue-webrtc | fae474c | 2017-07-05 11:17:40 +0200 | [diff] [blame] | 104 | virtual void DiscardOldPackets(uint32_t timestamp_limit, |
| 105 | uint32_t horizon_samples, |
| 106 | StatisticsCalculator* stats); |
henrik.lundin@webrtc.org | 52b42cb | 2014-11-04 14:03:58 +0000 | [diff] [blame] | 107 | |
| 108 | // Discards all packets that are (strictly) older than timestamp_limit. |
minyue-webrtc | fae474c | 2017-07-05 11:17:40 +0200 | [diff] [blame] | 109 | virtual void DiscardAllOldPackets(uint32_t timestamp_limit, |
| 110 | StatisticsCalculator* stats); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 111 | |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 112 | // Removes all packets with a specific payload type from the buffer. |
minyue-webrtc | fae474c | 2017-07-05 11:17:40 +0200 | [diff] [blame] | 113 | virtual void DiscardPacketsWithPayloadType(uint8_t payload_type, |
| 114 | StatisticsCalculator* stats); |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 115 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 116 | // Returns the number of packets in the buffer, including duplicates and |
| 117 | // redundant packets. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 118 | virtual size_t NumPacketsInBuffer() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 119 | |
| 120 | // Returns the number of samples in the buffer, including samples carried in |
| 121 | // duplicate and redundant packets. |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 122 | virtual size_t NumSamplesInBuffer(size_t last_decoded_length) const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 123 | |
henrik.lundin@webrtc.org | 116ed1d | 2014-04-28 08:20:04 +0000 | [diff] [blame] | 124 | virtual void BufferStat(int* num_packets, int* max_num_packets) const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 125 | |
henrik.lundin@webrtc.org | 52b42cb | 2014-11-04 14:03:58 +0000 | [diff] [blame] | 126 | // Static method returning true if |timestamp| is older than |timestamp_limit| |
| 127 | // but less than |horizon_samples| behind |timestamp_limit|. For instance, |
| 128 | // with timestamp_limit = 100 and horizon_samples = 10, a timestamp in the |
| 129 | // range (90, 100) is considered obsolete, and will yield true. |
| 130 | // Setting |horizon_samples| to 0 is the same as setting it to 2^31, i.e., |
| 131 | // half the 32-bit timestamp range. |
| 132 | static bool IsObsoleteTimestamp(uint32_t timestamp, |
| 133 | uint32_t timestamp_limit, |
| 134 | uint32_t horizon_samples) { |
| 135 | return IsNewerTimestamp(timestamp_limit, timestamp) && |
| 136 | (horizon_samples == 0 || |
| 137 | IsNewerTimestamp(timestamp, timestamp_limit - horizon_samples)); |
| 138 | } |
| 139 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 140 | private: |
| 141 | size_t max_number_of_packets_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 142 | PacketList buffer_; |
henrik.lundin | 84f8cd6 | 2016-04-26 07:45:16 -0700 | [diff] [blame] | 143 | const TickTimer* tick_timer_; |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 144 | RTC_DISALLOW_COPY_AND_ASSIGN(PacketBuffer); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 148 | #endif // MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_ |