blob: ee8c3789db1e9b04f43dab987071dfd6a4857a0d [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"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000017#include "webrtc/typedefs.h"
18
19namespace webrtc {
20
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000021class DecoderDatabase;
henrik.lundin84f8cd62016-04-26 07:45:16 -070022class TickTimer;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000023
24// This is the actual buffer holding the packets before decoding.
25class PacketBuffer {
26 public:
27 enum BufferReturnCodes {
28 kOK = 0,
29 kFlushed,
30 kNotFound,
31 kBufferEmpty,
32 kInvalidPacket,
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +000033 kInvalidPointer
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000034 };
35
36 // Constructor creates a buffer which can hold a maximum of
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +000037 // |max_number_of_packets| packets.
henrik.lundin84f8cd62016-04-26 07:45:16 -070038 PacketBuffer(size_t max_number_of_packets, const TickTimer* tick_timer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000039
40 // Deletes all packets in the buffer before destroying the buffer.
41 virtual ~PacketBuffer();
42
43 // Flushes the buffer and deletes all packets in it.
44 virtual void Flush();
45
46 // Returns true for an empty buffer.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020047 virtual bool Empty() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000048
49 // Inserts |packet| into the buffer. The buffer will take over ownership of
50 // the packet object.
51 // Returns PacketBuffer::kOK on success, PacketBuffer::kFlushed if the buffer
52 // was flushed due to overfilling.
53 virtual int InsertPacket(Packet* packet);
54
55 // Inserts a list of packets into the buffer. The buffer will take over
56 // ownership of the packet objects.
57 // Returns PacketBuffer::kOK if all packets were inserted successfully.
58 // If the buffer was flushed due to overfilling, only a subset of the list is
59 // inserted, and PacketBuffer::kFlushed is returned.
60 // The last three parameters are included for legacy compatibility.
61 // TODO(hlundin): Redesign to not use current_*_payload_type and
62 // decoder_database.
henrik.lundinda8bbf62016-08-31 03:14:11 -070063 virtual int InsertPacketList(
64 PacketList* packet_list,
65 const DecoderDatabase& decoder_database,
66 rtc::Optional<uint8_t>* current_rtp_payload_type,
67 rtc::Optional<uint8_t>* current_cng_rtp_payload_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000068
69 // Gets the timestamp for the first packet in the buffer and writes it to the
70 // output variable |next_timestamp|.
71 // Returns PacketBuffer::kBufferEmpty if the buffer is empty,
72 // PacketBuffer::kOK otherwise.
73 virtual int NextTimestamp(uint32_t* next_timestamp) const;
74
75 // Gets the timestamp for the first packet in the buffer with a timestamp no
76 // lower than the input limit |timestamp|. The result is written to the output
77 // variable |next_timestamp|.
78 // Returns PacketBuffer::kBufferEmpty if the buffer is empty,
79 // PacketBuffer::kOK otherwise.
80 virtual int NextHigherTimestamp(uint32_t timestamp,
81 uint32_t* next_timestamp) const;
82
83 // Returns a (constant) pointer the RTP header of the first packet in the
84 // buffer. Returns NULL if the buffer is empty.
85 virtual const RTPHeader* NextRtpHeader() const;
86
87 // Extracts the first packet in the buffer and returns a pointer to it.
88 // Returns NULL if the buffer is empty. The caller is responsible for deleting
89 // the packet.
90 // Subsequent packets with the same timestamp as the one extracted will be
91 // discarded and properly deleted. The number of discarded packets will be
92 // written to the output variable |discard_count|.
Peter Kastingdce40cf2015-08-24 14:52:23 -070093 virtual Packet* GetNextPacket(size_t* discard_count);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000094
95 // Discards the first packet in the buffer. The packet is deleted.
96 // Returns PacketBuffer::kBufferEmpty if the buffer is empty,
97 // PacketBuffer::kOK otherwise.
98 virtual int DiscardNextPacket();
99
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000100 // Discards all packets that are (strictly) older than timestamp_limit,
101 // but newer than timestamp_limit - horizon_samples. Setting horizon_samples
102 // to zero implies that the horizon is set to half the timestamp range. That
103 // is, if a packet is more than 2^31 timestamps into the future compared with
104 // timestamp_limit (including wrap-around), it is considered old.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000105 // Returns number of packets discarded.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000106 virtual int DiscardOldPackets(uint32_t timestamp_limit,
107 uint32_t horizon_samples);
108
109 // Discards all packets that are (strictly) older than timestamp_limit.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200110 virtual int DiscardAllOldPackets(uint32_t timestamp_limit);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000111
ossu61a208b2016-09-20 01:38:00 -0700112 // Removes all packets with a specific payload type from the buffer.
113 virtual void DiscardPacketsWithPayloadType(uint8_t payload_type);
114
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000115 // Returns the number of packets in the buffer, including duplicates and
116 // redundant packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700117 virtual size_t NumPacketsInBuffer() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000118
119 // Returns the number of samples in the buffer, including samples carried in
120 // duplicate and redundant packets.
ossu61a208b2016-09-20 01:38:00 -0700121 virtual size_t NumSamplesInBuffer(size_t last_decoded_length) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000122
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +0000123 virtual void BufferStat(int* num_packets, int* max_num_packets) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000124
125 // Static method that properly deletes the first packet, and its payload
126 // array, in |packet_list|. Returns false if |packet_list| already was empty,
127 // otherwise true.
128 static bool DeleteFirstPacket(PacketList* packet_list);
129
130 // Static method that properly deletes all packets, and their payload arrays,
131 // in |packet_list|.
132 static void DeleteAllPackets(PacketList* packet_list);
133
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000134 // Static method returning true if |timestamp| is older than |timestamp_limit|
135 // but less than |horizon_samples| behind |timestamp_limit|. For instance,
136 // with timestamp_limit = 100 and horizon_samples = 10, a timestamp in the
137 // range (90, 100) is considered obsolete, and will yield true.
138 // Setting |horizon_samples| to 0 is the same as setting it to 2^31, i.e.,
139 // half the 32-bit timestamp range.
140 static bool IsObsoleteTimestamp(uint32_t timestamp,
141 uint32_t timestamp_limit,
142 uint32_t horizon_samples) {
143 return IsNewerTimestamp(timestamp_limit, timestamp) &&
144 (horizon_samples == 0 ||
145 IsNewerTimestamp(timestamp, timestamp_limit - horizon_samples));
146 }
147
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000148 private:
149 size_t max_number_of_packets_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000150 PacketList buffer_;
henrik.lundin84f8cd62016-04-26 07:45:16 -0700151 const TickTimer* tick_timer_;
henrikg3c089d72015-09-16 05:37:44 -0700152 RTC_DISALLOW_COPY_AND_ASSIGN(PacketBuffer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000153};
154
155} // namespace webrtc
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +0000156#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_