blob: 3e203afab3bd7bf1ffeb343d9efe65cdfe32a92a [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
11#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ4_PACKET_BUFFER_H_
12#define WEBRTC_MODULES_AUDIO_CODING_NETEQ4_PACKET_BUFFER_H_
13
14#include "webrtc/modules/audio_coding/neteq4/packet.h"
15#include "webrtc/system_wrappers/interface/constructor_magic.h"
16#include "webrtc/typedefs.h"
17
18namespace webrtc {
19
20// Forward declaration.
21class DecoderDatabase;
22
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,
32 kInvalidPointer,
33 kOversizePacket
34 };
35
36 // Constructor creates a buffer which can hold a maximum of
37 // |max_number_of_packets| packets and |max_payload_memory| bytes of payload,
38 // excluding RTP headers.
39 PacketBuffer(size_t max_number_of_packets, size_t max_payload_memory);
40
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.
48 virtual bool Empty() const { return buffer_.empty(); }
49
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.
64 virtual int InsertPacketList(PacketList* packet_list,
65 const DecoderDatabase& decoder_database,
66 uint8_t* current_rtp_payload_type,
67 uint8_t* current_cng_rtp_payload_type);
68
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|.
93 virtual Packet* GetNextPacket(int* discard_count);
94
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
100 // Discards all packets that are (strictly) older than |timestamp_limit|.
101 // Returns number of packets discarded.
102 virtual int DiscardOldPackets(uint32_t timestamp_limit);
103
104 // Returns the number of packets in the buffer, including duplicates and
105 // redundant packets.
106 virtual int NumPacketsInBuffer() const {
107 return static_cast<int>(buffer_.size());
108 }
109
110 // Returns the number of samples in the buffer, including samples carried in
111 // duplicate and redundant packets.
112 virtual int NumSamplesInBuffer(DecoderDatabase* decoder_database,
113 int last_decoded_length) const;
114
115 // Increase the waiting time counter for every packet in the buffer by |inc|.
116 // The default value for |inc| is 1.
117 virtual void IncrementWaitingTimes(int inc = 1);
118
119 virtual int current_memory_bytes() const { return current_memory_bytes_; }
120
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
130 private:
131 size_t max_number_of_packets_;
132 size_t max_memory_bytes_;
133 int current_memory_bytes_;
134 PacketList buffer_;
135 DISALLOW_COPY_AND_ASSIGN(PacketBuffer);
136};
137
138} // namespace webrtc
139#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ4_PACKET_BUFFER_H_