blob: 6867b4cb37e03fe0b5c3432dc35f9301da5c5a4e [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.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000015#include "webrtc/modules/audio_coding/neteq/packet.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016#include "webrtc/typedefs.h"
17
18namespace webrtc {
19
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000020class DecoderDatabase;
henrik.lundin84f8cd62016-04-26 07:45:16 -070021class TickTimer;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000022
23// This is the actual buffer holding the packets before decoding.
24class PacketBuffer {
25 public:
26 enum BufferReturnCodes {
27 kOK = 0,
28 kFlushed,
29 kNotFound,
30 kBufferEmpty,
31 kInvalidPacket,
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +000032 kInvalidPointer
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000033 };
34
35 // Constructor creates a buffer which can hold a maximum of
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +000036 // |max_number_of_packets| packets.
henrik.lundin84f8cd62016-04-26 07:45:16 -070037 PacketBuffer(size_t max_number_of_packets, const TickTimer* tick_timer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000038
39 // Deletes all packets in the buffer before destroying the buffer.
40 virtual ~PacketBuffer();
41
42 // Flushes the buffer and deletes all packets in it.
43 virtual void Flush();
44
45 // Returns true for an empty buffer.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020046 virtual bool Empty() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000047
48 // Inserts |packet| into the buffer. The buffer will take over ownership of
49 // the packet object.
50 // Returns PacketBuffer::kOK on success, PacketBuffer::kFlushed if the buffer
51 // was flushed due to overfilling.
52 virtual int InsertPacket(Packet* packet);
53
54 // Inserts a list of packets into the buffer. The buffer will take over
55 // ownership of the packet objects.
56 // Returns PacketBuffer::kOK if all packets were inserted successfully.
57 // If the buffer was flushed due to overfilling, only a subset of the list is
58 // inserted, and PacketBuffer::kFlushed is returned.
59 // The last three parameters are included for legacy compatibility.
60 // TODO(hlundin): Redesign to not use current_*_payload_type and
61 // decoder_database.
62 virtual int InsertPacketList(PacketList* packet_list,
63 const DecoderDatabase& decoder_database,
64 uint8_t* current_rtp_payload_type,
65 uint8_t* current_cng_rtp_payload_type);
66
67 // Gets the timestamp for the first packet in the buffer and writes it to the
68 // output variable |next_timestamp|.
69 // Returns PacketBuffer::kBufferEmpty if the buffer is empty,
70 // PacketBuffer::kOK otherwise.
71 virtual int NextTimestamp(uint32_t* next_timestamp) const;
72
73 // Gets the timestamp for the first packet in the buffer with a timestamp no
74 // lower than the input limit |timestamp|. The result is written to the output
75 // variable |next_timestamp|.
76 // Returns PacketBuffer::kBufferEmpty if the buffer is empty,
77 // PacketBuffer::kOK otherwise.
78 virtual int NextHigherTimestamp(uint32_t timestamp,
79 uint32_t* next_timestamp) const;
80
81 // Returns a (constant) pointer the RTP header of the first packet in the
82 // buffer. Returns NULL if the buffer is empty.
83 virtual const RTPHeader* NextRtpHeader() const;
84
85 // Extracts the first packet in the buffer and returns a pointer to it.
86 // Returns NULL if the buffer is empty. The caller is responsible for deleting
87 // the packet.
88 // Subsequent packets with the same timestamp as the one extracted will be
89 // discarded and properly deleted. The number of discarded packets will be
90 // written to the output variable |discard_count|.
Peter Kastingdce40cf2015-08-24 14:52:23 -070091 virtual Packet* GetNextPacket(size_t* discard_count);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000092
93 // Discards the first packet in the buffer. The packet is deleted.
94 // Returns PacketBuffer::kBufferEmpty if the buffer is empty,
95 // PacketBuffer::kOK otherwise.
96 virtual int DiscardNextPacket();
97
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +000098 // Discards all packets that are (strictly) older than timestamp_limit,
99 // but newer than timestamp_limit - horizon_samples. Setting horizon_samples
100 // to zero implies that the horizon is set to half the timestamp range. That
101 // is, if a packet is more than 2^31 timestamps into the future compared with
102 // timestamp_limit (including wrap-around), it is considered old.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000103 // Returns number of packets discarded.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000104 virtual int DiscardOldPackets(uint32_t timestamp_limit,
105 uint32_t horizon_samples);
106
107 // Discards all packets that are (strictly) older than timestamp_limit.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200108 virtual int DiscardAllOldPackets(uint32_t timestamp_limit);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000109
110 // Returns the number of packets in the buffer, including duplicates and
111 // redundant packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700112 virtual size_t NumPacketsInBuffer() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113
114 // Returns the number of samples in the buffer, including samples carried in
115 // duplicate and redundant packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700116 virtual size_t NumSamplesInBuffer(DecoderDatabase* decoder_database,
117 size_t last_decoded_length) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000118
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +0000119 virtual void BufferStat(int* num_packets, int* max_num_packets) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000120
121 // Static method that properly deletes the first packet, and its payload
122 // array, in |packet_list|. Returns false if |packet_list| already was empty,
123 // otherwise true.
124 static bool DeleteFirstPacket(PacketList* packet_list);
125
126 // Static method that properly deletes all packets, and their payload arrays,
127 // in |packet_list|.
128 static void DeleteAllPackets(PacketList* packet_list);
129
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000130 // Static method returning true if |timestamp| is older than |timestamp_limit|
131 // but less than |horizon_samples| behind |timestamp_limit|. For instance,
132 // with timestamp_limit = 100 and horizon_samples = 10, a timestamp in the
133 // range (90, 100) is considered obsolete, and will yield true.
134 // Setting |horizon_samples| to 0 is the same as setting it to 2^31, i.e.,
135 // half the 32-bit timestamp range.
136 static bool IsObsoleteTimestamp(uint32_t timestamp,
137 uint32_t timestamp_limit,
138 uint32_t horizon_samples) {
139 return IsNewerTimestamp(timestamp_limit, timestamp) &&
140 (horizon_samples == 0 ||
141 IsNewerTimestamp(timestamp, timestamp_limit - horizon_samples));
142 }
143
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000144 private:
145 size_t max_number_of_packets_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000146 PacketList buffer_;
henrik.lundin84f8cd62016-04-26 07:45:16 -0700147 const TickTimer* tick_timer_;
henrikg3c089d72015-09-16 05:37:44 -0700148 RTC_DISALLOW_COPY_AND_ASSIGN(PacketBuffer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000149};
150
151} // namespace webrtc
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +0000152#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_