blob: c646626f032df58d9ad75b70d31d6407e52aecff [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_
12#define MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "api/optional.h"
Ivo Creusenc7f09ad2018-05-22 13:21:01 +020015#include "modules/audio_coding/neteq/decoder_database.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/audio_coding/neteq/packet.h"
17#include "modules/include/module_common_types.h"
18#include "rtc_base/constructormagic.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020019#include "typedefs.h" // NOLINT(build/include)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000020
21namespace webrtc {
22
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000023class DecoderDatabase;
minyue-webrtcfae474c2017-07-05 11:17:40 +020024class StatisticsCalculator;
henrik.lundin84f8cd62016-04-26 07:45:16 -070025class TickTimer;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000026
27// This is the actual buffer holding the packets before decoding.
28class PacketBuffer {
29 public:
30 enum BufferReturnCodes {
31 kOK = 0,
32 kFlushed,
33 kNotFound,
34 kBufferEmpty,
35 kInvalidPacket,
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +000036 kInvalidPointer
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000037 };
38
39 // Constructor creates a buffer which can hold a maximum of
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +000040 // |max_number_of_packets| packets.
henrik.lundin84f8cd62016-04-26 07:45:16 -070041 PacketBuffer(size_t max_number_of_packets, const TickTimer* tick_timer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000042
43 // Deletes all packets in the buffer before destroying the buffer.
44 virtual ~PacketBuffer();
45
46 // Flushes the buffer and deletes all packets in it.
47 virtual void Flush();
48
49 // Returns true for an empty buffer.
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020050 virtual bool Empty() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000051
52 // Inserts |packet| into the buffer. The buffer will take over ownership of
53 // the packet object.
54 // Returns PacketBuffer::kOK on success, PacketBuffer::kFlushed if the buffer
55 // was flushed due to overfilling.
minyue-webrtc12d30842017-07-19 11:44:06 +020056 virtual int InsertPacket(Packet&& packet, StatisticsCalculator* stats);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000057
58 // Inserts a list of packets into the buffer. The buffer will take over
59 // ownership of the packet objects.
60 // Returns PacketBuffer::kOK if all packets were inserted successfully.
61 // If the buffer was flushed due to overfilling, only a subset of the list is
62 // inserted, and PacketBuffer::kFlushed is returned.
63 // The last three parameters are included for legacy compatibility.
64 // TODO(hlundin): Redesign to not use current_*_payload_type and
65 // decoder_database.
henrik.lundinda8bbf62016-08-31 03:14:11 -070066 virtual int InsertPacketList(
67 PacketList* packet_list,
68 const DecoderDatabase& decoder_database,
69 rtc::Optional<uint8_t>* current_rtp_payload_type,
minyue-webrtc12d30842017-07-19 11:44:06 +020070 rtc::Optional<uint8_t>* current_cng_rtp_payload_type,
71 StatisticsCalculator* stats);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000072
73 // Gets the timestamp for the first packet in the buffer and writes it to the
74 // output variable |next_timestamp|.
75 // Returns PacketBuffer::kBufferEmpty if the buffer is empty,
76 // PacketBuffer::kOK otherwise.
77 virtual int NextTimestamp(uint32_t* next_timestamp) const;
78
79 // Gets the timestamp for the first packet in the buffer with a timestamp no
80 // lower than the input limit |timestamp|. The result is written to the output
81 // variable |next_timestamp|.
82 // Returns PacketBuffer::kBufferEmpty if the buffer is empty,
83 // PacketBuffer::kOK otherwise.
84 virtual int NextHigherTimestamp(uint32_t timestamp,
85 uint32_t* next_timestamp) const;
86
ossu7a377612016-10-18 04:06:13 -070087 // Returns a (constant) pointer to the first packet in the buffer. Returns
88 // NULL if the buffer is empty.
89 virtual const Packet* PeekNextPacket() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000090
ossua73f6c92016-10-24 08:25:28 -070091 // Extracts the first packet in the buffer and returns it.
92 // Returns an empty optional if the buffer is empty.
93 virtual rtc::Optional<Packet> GetNextPacket();
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.
minyue-webrtcfae474c2017-07-05 11:17:40 +020098 virtual int DiscardNextPacket(StatisticsCalculator* stats);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000099
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.
minyue-webrtcfae474c2017-07-05 11:17:40 +0200105 virtual void DiscardOldPackets(uint32_t timestamp_limit,
106 uint32_t horizon_samples,
107 StatisticsCalculator* stats);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000108
109 // Discards all packets that are (strictly) older than timestamp_limit.
minyue-webrtcfae474c2017-07-05 11:17:40 +0200110 virtual void DiscardAllOldPackets(uint32_t timestamp_limit,
111 StatisticsCalculator* stats);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000112
ossu61a208b2016-09-20 01:38:00 -0700113 // Removes all packets with a specific payload type from the buffer.
minyue-webrtcfae474c2017-07-05 11:17:40 +0200114 virtual void DiscardPacketsWithPayloadType(uint8_t payload_type,
115 StatisticsCalculator* stats);
ossu61a208b2016-09-20 01:38:00 -0700116
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000117 // Returns the number of packets in the buffer, including duplicates and
118 // redundant packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700119 virtual size_t NumPacketsInBuffer() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000120
121 // Returns the number of samples in the buffer, including samples carried in
122 // duplicate and redundant packets.
ossu61a208b2016-09-20 01:38:00 -0700123 virtual size_t NumSamplesInBuffer(size_t last_decoded_length) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000124
Ivo Creusenc7f09ad2018-05-22 13:21:01 +0200125 // Returns true if the packet buffer contains any DTX or CNG packets.
126 virtual bool ContainsDtxOrCngPacket(
127 const DecoderDatabase* decoder_database) const;
128
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +0000129 virtual void BufferStat(int* num_packets, int* max_num_packets) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000130
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000131 // Static method returning true if |timestamp| is older than |timestamp_limit|
132 // but less than |horizon_samples| behind |timestamp_limit|. For instance,
133 // with timestamp_limit = 100 and horizon_samples = 10, a timestamp in the
134 // range (90, 100) is considered obsolete, and will yield true.
135 // Setting |horizon_samples| to 0 is the same as setting it to 2^31, i.e.,
136 // half the 32-bit timestamp range.
137 static bool IsObsoleteTimestamp(uint32_t timestamp,
138 uint32_t timestamp_limit,
139 uint32_t horizon_samples) {
140 return IsNewerTimestamp(timestamp_limit, timestamp) &&
141 (horizon_samples == 0 ||
142 IsNewerTimestamp(timestamp, timestamp_limit - horizon_samples));
143 }
144
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000145 private:
146 size_t max_number_of_packets_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000147 PacketList buffer_;
henrik.lundin84f8cd62016-04-26 07:45:16 -0700148 const TickTimer* tick_timer_;
henrikg3c089d72015-09-16 05:37:44 -0700149 RTC_DISALLOW_COPY_AND_ASSIGN(PacketBuffer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000150};
151
152} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200153#endif // MODULES_AUDIO_CODING_NETEQ_PACKET_BUFFER_H_