blob: 806ca70c640f93a878e8480dd34a989ecb559896 [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
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000014#include "webrtc/modules/audio_coding/neteq/packet.h"
ossu7a377612016-10-18 04:06:13 -070015#include "webrtc/modules/include/module_common_types.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020016#include "webrtc/rtc_base/constructormagic.h"
17#include "webrtc/rtc_base/optional.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;
minyue-webrtcfae474c2017-07-05 11:17:40 +020023class StatisticsCalculator;
henrik.lundin84f8cd62016-04-26 07:45:16 -070024class TickTimer;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000025
26// This is the actual buffer holding the packets before decoding.
27class PacketBuffer {
28 public:
29 enum BufferReturnCodes {
30 kOK = 0,
31 kFlushed,
32 kNotFound,
33 kBufferEmpty,
34 kInvalidPacket,
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +000035 kInvalidPointer
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000036 };
37
38 // Constructor creates a buffer which can hold a maximum of
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +000039 // |max_number_of_packets| packets.
henrik.lundin84f8cd62016-04-26 07:45:16 -070040 PacketBuffer(size_t max_number_of_packets, const TickTimer* tick_timer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000041
42 // Deletes all packets in the buffer before destroying the buffer.
43 virtual ~PacketBuffer();
44
45 // Flushes the buffer and deletes all packets in it.
46 virtual void Flush();
47
48 // Returns true for an empty buffer.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020049 virtual bool Empty() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000050
51 // Inserts |packet| into the buffer. The buffer will take over ownership of
52 // the packet object.
53 // Returns PacketBuffer::kOK on success, PacketBuffer::kFlushed if the buffer
54 // was flushed due to overfilling.
ossua73f6c92016-10-24 08:25:28 -070055 virtual int InsertPacket(Packet&& packet);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000056
57 // Inserts a list of packets into the buffer. The buffer will take over
58 // ownership of the packet objects.
59 // Returns PacketBuffer::kOK if all packets were inserted successfully.
60 // If the buffer was flushed due to overfilling, only a subset of the list is
61 // inserted, and PacketBuffer::kFlushed is returned.
62 // The last three parameters are included for legacy compatibility.
63 // TODO(hlundin): Redesign to not use current_*_payload_type and
64 // decoder_database.
henrik.lundinda8bbf62016-08-31 03:14:11 -070065 virtual int InsertPacketList(
66 PacketList* packet_list,
67 const DecoderDatabase& decoder_database,
68 rtc::Optional<uint8_t>* current_rtp_payload_type,
69 rtc::Optional<uint8_t>* current_cng_rtp_payload_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000070
71 // Gets the timestamp for the first packet in the buffer and writes it to the
72 // output variable |next_timestamp|.
73 // Returns PacketBuffer::kBufferEmpty if the buffer is empty,
74 // PacketBuffer::kOK otherwise.
75 virtual int NextTimestamp(uint32_t* next_timestamp) const;
76
77 // Gets the timestamp for the first packet in the buffer with a timestamp no
78 // lower than the input limit |timestamp|. The result is written to the output
79 // variable |next_timestamp|.
80 // Returns PacketBuffer::kBufferEmpty if the buffer is empty,
81 // PacketBuffer::kOK otherwise.
82 virtual int NextHigherTimestamp(uint32_t timestamp,
83 uint32_t* next_timestamp) const;
84
ossu7a377612016-10-18 04:06:13 -070085 // Returns a (constant) pointer to the first packet in the buffer. Returns
86 // NULL if the buffer is empty.
87 virtual const Packet* PeekNextPacket() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000088
ossua73f6c92016-10-24 08:25:28 -070089 // Extracts the first packet in the buffer and returns it.
90 // Returns an empty optional if the buffer is empty.
91 virtual rtc::Optional<Packet> GetNextPacket();
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.
minyue-webrtcfae474c2017-07-05 11:17:40 +020096 virtual int DiscardNextPacket(StatisticsCalculator* stats);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000097
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.
minyue-webrtcfae474c2017-07-05 11:17:40 +0200103 virtual void DiscardOldPackets(uint32_t timestamp_limit,
104 uint32_t horizon_samples,
105 StatisticsCalculator* stats);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000106
107 // Discards all packets that are (strictly) older than timestamp_limit.
minyue-webrtcfae474c2017-07-05 11:17:40 +0200108 virtual void DiscardAllOldPackets(uint32_t timestamp_limit,
109 StatisticsCalculator* stats);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000110
ossu61a208b2016-09-20 01:38:00 -0700111 // Removes all packets with a specific payload type from the buffer.
minyue-webrtcfae474c2017-07-05 11:17:40 +0200112 virtual void DiscardPacketsWithPayloadType(uint8_t payload_type,
113 StatisticsCalculator* stats);
ossu61a208b2016-09-20 01:38:00 -0700114
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
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000125 // Static method returning true if |timestamp| is older than |timestamp_limit|
126 // but less than |horizon_samples| behind |timestamp_limit|. For instance,
127 // with timestamp_limit = 100 and horizon_samples = 10, a timestamp in the
128 // range (90, 100) is considered obsolete, and will yield true.
129 // Setting |horizon_samples| to 0 is the same as setting it to 2^31, i.e.,
130 // half the 32-bit timestamp range.
131 static bool IsObsoleteTimestamp(uint32_t timestamp,
132 uint32_t timestamp_limit,
133 uint32_t horizon_samples) {
134 return IsNewerTimestamp(timestamp_limit, timestamp) &&
135 (horizon_samples == 0 ||
136 IsNewerTimestamp(timestamp, timestamp_limit - horizon_samples));
137 }
138
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000139 private:
140 size_t max_number_of_packets_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000141 PacketList buffer_;
henrik.lundin84f8cd62016-04-26 07:45:16 -0700142 const TickTimer* tick_timer_;
henrikg3c089d72015-09-16 05:37:44 -0700143 RTC_DISALLOW_COPY_AND_ASSIGN(PacketBuffer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000144};
145
146} // namespace webrtc
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +0000147#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_