Line data Source code
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 :
11 : #ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_
12 : #define WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_
13 :
14 : #include "webrtc/base/constructormagic.h"
15 : #include "webrtc/base/optional.h"
16 : #include "webrtc/modules/audio_coding/neteq/packet.h"
17 : #include "webrtc/modules/include/module_common_types.h"
18 : #include "webrtc/typedefs.h"
19 :
20 : namespace webrtc {
21 :
22 : class DecoderDatabase;
23 : class TickTimer;
24 :
25 : // This is the actual buffer holding the packets before decoding.
26 : class PacketBuffer {
27 : public:
28 : enum BufferReturnCodes {
29 : kOK = 0,
30 : kFlushed,
31 : kNotFound,
32 : kBufferEmpty,
33 : kInvalidPacket,
34 : kInvalidPointer
35 : };
36 :
37 : // Constructor creates a buffer which can hold a maximum of
38 : // |max_number_of_packets| packets.
39 : PacketBuffer(size_t max_number_of_packets, const TickTimer* tick_timer);
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;
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(
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);
69 :
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 :
84 : // 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;
87 :
88 : // Extracts the first packet in the buffer and returns it.
89 : // Returns an empty optional if the buffer is empty.
90 : virtual rtc::Optional<Packet> GetNextPacket();
91 :
92 : // Discards the first packet in the buffer. The packet is deleted.
93 : // Returns PacketBuffer::kBufferEmpty if the buffer is empty,
94 : // PacketBuffer::kOK otherwise.
95 : virtual int DiscardNextPacket();
96 :
97 : // Discards all packets that are (strictly) older than timestamp_limit,
98 : // but newer than timestamp_limit - horizon_samples. Setting horizon_samples
99 : // to zero implies that the horizon is set to half the timestamp range. That
100 : // is, if a packet is more than 2^31 timestamps into the future compared with
101 : // timestamp_limit (including wrap-around), it is considered old.
102 : // Returns number of packets discarded.
103 : virtual int DiscardOldPackets(uint32_t timestamp_limit,
104 : uint32_t horizon_samples);
105 :
106 : // Discards all packets that are (strictly) older than timestamp_limit.
107 : virtual int DiscardAllOldPackets(uint32_t timestamp_limit);
108 :
109 : // Removes all packets with a specific payload type from the buffer.
110 : virtual void DiscardPacketsWithPayloadType(uint8_t payload_type);
111 :
112 : // Returns the number of packets in the buffer, including duplicates and
113 : // redundant packets.
114 : virtual size_t NumPacketsInBuffer() const;
115 :
116 : // Returns the number of samples in the buffer, including samples carried in
117 : // duplicate and redundant packets.
118 : virtual size_t NumSamplesInBuffer(size_t last_decoded_length) const;
119 :
120 : virtual void BufferStat(int* num_packets, int* max_num_packets) const;
121 :
122 : // Static method returning true if |timestamp| is older than |timestamp_limit|
123 : // but less than |horizon_samples| behind |timestamp_limit|. For instance,
124 : // with timestamp_limit = 100 and horizon_samples = 10, a timestamp in the
125 : // range (90, 100) is considered obsolete, and will yield true.
126 : // Setting |horizon_samples| to 0 is the same as setting it to 2^31, i.e.,
127 : // half the 32-bit timestamp range.
128 0 : static bool IsObsoleteTimestamp(uint32_t timestamp,
129 : uint32_t timestamp_limit,
130 : uint32_t horizon_samples) {
131 0 : return IsNewerTimestamp(timestamp_limit, timestamp) &&
132 0 : (horizon_samples == 0 ||
133 0 : IsNewerTimestamp(timestamp, timestamp_limit - horizon_samples));
134 : }
135 :
136 : private:
137 : size_t max_number_of_packets_;
138 : PacketList buffer_;
139 : const TickTimer* tick_timer_;
140 : RTC_DISALLOW_COPY_AND_ASSIGN(PacketBuffer);
141 : };
142 :
143 : } // namespace webrtc
144 : #endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_
|