blob: 63e006c1d545dc05e81d6171bbf2c13625d74565 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000011#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_
12#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000014#include "webrtc/base/constructormagic.h"
henrik.lundinda8bbf62016-08-31 03:14:11 -070015#include "webrtc/base/optional.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000016#include "webrtc/modules/audio_coding/neteq/packet.h"
ossu7a377612016-10-18 04:06:13 -070017#include "webrtc/modules/include/module_common_types.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000018#include "webrtc/typedefs.h"
19
20namespace webrtc {
21
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000022class DecoderDatabase;
henrik.lundin84f8cd62016-04-26 07:45:16 -070023class TickTimer;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000024
25// This is the actual buffer holding the packets before decoding.
26class PacketBuffer {
27 public:
28 enum BufferReturnCodes {
29 kOK = 0,
30 kFlushed,
31 kNotFound,
32 kBufferEmpty,
33 kInvalidPacket,
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +000034 kInvalidPointer
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035 };
36
37 // Constructor creates a buffer which can hold a maximum of
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +000038 // |max_number_of_packets| packets.
henrik.lundin84f8cd62016-04-26 07:45:16 -070039 PacketBuffer(size_t max_number_of_packets, const TickTimer* tick_timer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000040
41 // Deletes all packets in the buffer before destroying the buffer.
42 virtual ~PacketBuffer();
43
44 // Flushes the buffer and deletes all packets in it.
45 virtual void Flush();
46
47 // Returns true for an empty buffer.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020048 virtual bool Empty() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000049
50 // Inserts |packet| into the buffer. The buffer will take over ownership of
51 // the packet object.
52 // Returns PacketBuffer::kOK on success, PacketBuffer::kFlushed if the buffer
53 // was flushed due to overfilling.
54 virtual int InsertPacket(Packet* packet);
55
56 // Inserts a list of packets into the buffer. The buffer will take over
57 // ownership of the packet objects.
58 // Returns PacketBuffer::kOK if all packets were inserted successfully.
59 // If the buffer was flushed due to overfilling, only a subset of the list is
60 // inserted, and PacketBuffer::kFlushed is returned.
61 // The last three parameters are included for legacy compatibility.
62 // TODO(hlundin): Redesign to not use current_*_payload_type and
63 // decoder_database.
henrik.lundinda8bbf62016-08-31 03:14:11 -070064 virtual int InsertPacketList(
65 PacketList* packet_list,
66 const DecoderDatabase& decoder_database,
67 rtc::Optional<uint8_t>* current_rtp_payload_type,
68 rtc::Optional<uint8_t>* current_cng_rtp_payload_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000069
70 // Gets the timestamp for the first packet in the buffer and writes it to the
71 // output variable |next_timestamp|.
72 // Returns PacketBuffer::kBufferEmpty if the buffer is empty,
73 // PacketBuffer::kOK otherwise.
74 virtual int NextTimestamp(uint32_t* next_timestamp) const;
75
76 // Gets the timestamp for the first packet in the buffer with a timestamp no
77 // lower than the input limit |timestamp|. The result is written to the output
78 // variable |next_timestamp|.
79 // Returns PacketBuffer::kBufferEmpty if the buffer is empty,
80 // PacketBuffer::kOK otherwise.
81 virtual int NextHigherTimestamp(uint32_t timestamp,
82 uint32_t* next_timestamp) const;
83
ossu7a377612016-10-18 04:06:13 -070084 // Returns a (constant) pointer to the first packet in the buffer. Returns
85 // NULL if the buffer is empty.
86 virtual const Packet* PeekNextPacket() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000087
88 // Extracts the first packet in the buffer and returns a pointer to it.
89 // Returns NULL if the buffer is empty. The caller is responsible for deleting
90 // the packet.
91 // Subsequent packets with the same timestamp as the one extracted will be
92 // discarded and properly deleted. The number of discarded packets will be
93 // written to the output variable |discard_count|.
Peter Kastingdce40cf2015-08-24 14:52:23 -070094 virtual Packet* GetNextPacket(size_t* discard_count);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000095
96 // Discards the first packet in the buffer. The packet is deleted.
97 // Returns PacketBuffer::kBufferEmpty if the buffer is empty,
98 // PacketBuffer::kOK otherwise.
99 virtual int DiscardNextPacket();
100
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000101 // Discards all packets that are (strictly) older than timestamp_limit,
102 // but newer than timestamp_limit - horizon_samples. Setting horizon_samples
103 // to zero implies that the horizon is set to half the timestamp range. That
104 // is, if a packet is more than 2^31 timestamps into the future compared with
105 // timestamp_limit (including wrap-around), it is considered old.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000106 // Returns number of packets discarded.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000107 virtual int DiscardOldPackets(uint32_t timestamp_limit,
108 uint32_t horizon_samples);
109
110 // Discards all packets that are (strictly) older than timestamp_limit.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200111 virtual int DiscardAllOldPackets(uint32_t timestamp_limit);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000112
ossu61a208b2016-09-20 01:38:00 -0700113 // Removes all packets with a specific payload type from the buffer.
114 virtual void DiscardPacketsWithPayloadType(uint8_t payload_type);
115
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000116 // Returns the number of packets in the buffer, including duplicates and
117 // redundant packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700118 virtual size_t NumPacketsInBuffer() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000119
120 // Returns the number of samples in the buffer, including samples carried in
121 // duplicate and redundant packets.
ossu61a208b2016-09-20 01:38:00 -0700122 virtual size_t NumSamplesInBuffer(size_t last_decoded_length) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000123
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +0000124 virtual void BufferStat(int* num_packets, int* max_num_packets) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000125
126 // Static method that properly deletes the first packet, and its payload
127 // array, in |packet_list|. Returns false if |packet_list| already was empty,
128 // otherwise true.
129 static bool DeleteFirstPacket(PacketList* packet_list);
130
131 // Static method that properly deletes all packets, and their payload arrays,
132 // in |packet_list|.
133 static void DeleteAllPackets(PacketList* packet_list);
134
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000135 // Static method returning true if |timestamp| is older than |timestamp_limit|
136 // but less than |horizon_samples| behind |timestamp_limit|. For instance,
137 // with timestamp_limit = 100 and horizon_samples = 10, a timestamp in the
138 // range (90, 100) is considered obsolete, and will yield true.
139 // Setting |horizon_samples| to 0 is the same as setting it to 2^31, i.e.,
140 // half the 32-bit timestamp range.
141 static bool IsObsoleteTimestamp(uint32_t timestamp,
142 uint32_t timestamp_limit,
143 uint32_t horizon_samples) {
144 return IsNewerTimestamp(timestamp_limit, timestamp) &&
145 (horizon_samples == 0 ||
146 IsNewerTimestamp(timestamp, timestamp_limit - horizon_samples));
147 }
148
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000149 private:
150 size_t max_number_of_packets_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000151 PacketList buffer_;
henrik.lundin84f8cd62016-04-26 07:45:16 -0700152 const TickTimer* tick_timer_;
henrikg3c089d72015-09-16 05:37:44 -0700153 RTC_DISALLOW_COPY_AND_ASSIGN(PacketBuffer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000154};
155
156} // namespace webrtc
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +0000157#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_