blob: cea6b3dbfbfdfc76164c7c0ccfedb51fff01c340 [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// This is the implementation of the PacketBuffer class. It is mostly based on
12// an STL list. The list is kept sorted at all times so that the next packet to
13// decode is at the beginning of the list.
14
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000015#include "webrtc/modules/audio_coding/neteq/packet_buffer.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016
17#include <algorithm> // find_if()
18
kwiberg087bd342017-02-10 08:15:44 -080019#include "webrtc/api/audio_codecs/audio_decoder.h"
Henrik Kjellanderdca1e092017-07-01 16:42:22 +020020#include "webrtc/base/logging.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000021#include "webrtc/modules/audio_coding/neteq/decoder_database.h"
minyue-webrtcfae474c2017-07-05 11:17:40 +020022#include "webrtc/modules/audio_coding/neteq/statistics_calculator.h"
henrik.lundin84f8cd62016-04-26 07:45:16 -070023#include "webrtc/modules/audio_coding/neteq/tick_timer.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000024
25namespace webrtc {
henrik.lundin067d8552016-09-01 23:19:05 -070026namespace {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000027// Predicate used when inserting packets in the buffer list.
28// Operator() returns true when |packet| goes before |new_packet|.
29class NewTimestampIsLarger {
30 public:
ossua73f6c92016-10-24 08:25:28 -070031 explicit NewTimestampIsLarger(const Packet& new_packet)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000032 : new_packet_(new_packet) {
33 }
ossua73f6c92016-10-24 08:25:28 -070034 bool operator()(const Packet& packet) {
35 return (new_packet_ >= packet);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000036 }
37
38 private:
ossua73f6c92016-10-24 08:25:28 -070039 const Packet& new_packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000040};
41
henrik.lundin067d8552016-09-01 23:19:05 -070042// Returns true if both payload types are known to the decoder database, and
43// have the same sample rate.
44bool EqualSampleRates(uint8_t pt1,
45 uint8_t pt2,
46 const DecoderDatabase& decoder_database) {
kjellander7c856582017-02-26 19:53:40 -080047 auto* di1 = decoder_database.GetDecoderInfo(pt1);
48 auto* di2 = decoder_database.GetDecoderInfo(pt2);
henrik.lundin067d8552016-09-01 23:19:05 -070049 return di1 && di2 && di1->SampleRateHz() == di2->SampleRateHz();
50}
51} // namespace
52
henrik.lundin84f8cd62016-04-26 07:45:16 -070053PacketBuffer::PacketBuffer(size_t max_number_of_packets,
54 const TickTimer* tick_timer)
55 : max_number_of_packets_(max_number_of_packets), tick_timer_(tick_timer) {}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000056
57// Destructor. All packets in the buffer will be destroyed.
58PacketBuffer::~PacketBuffer() {
59 Flush();
60}
61
62// Flush the buffer. All packets in the buffer will be destroyed.
63void PacketBuffer::Flush() {
ossua73f6c92016-10-24 08:25:28 -070064 buffer_.clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000065}
66
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020067bool PacketBuffer::Empty() const {
68 return buffer_.empty();
69}
70
ossua73f6c92016-10-24 08:25:28 -070071int PacketBuffer::InsertPacket(Packet&& packet) {
72 if (packet.empty()) {
Henrik Lundind67a2192015-08-03 12:54:37 +020073 LOG(LS_WARNING) << "InsertPacket invalid packet";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000074 return kInvalidPacket;
75 }
76
ossua73f6c92016-10-24 08:25:28 -070077 RTC_DCHECK_GE(packet.priority.codec_level, 0);
78 RTC_DCHECK_GE(packet.priority.red_level, 0);
ossua70695a2016-09-22 02:06:28 -070079
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080 int return_val = kOK;
81
ossua73f6c92016-10-24 08:25:28 -070082 packet.waiting_time = tick_timer_->GetNewStopwatch();
henrik.lundin84f8cd62016-04-26 07:45:16 -070083
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +000084 if (buffer_.size() >= max_number_of_packets_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000085 // Buffer is full. Flush it.
86 Flush();
Henrik Lundind67a2192015-08-03 12:54:37 +020087 LOG(LS_WARNING) << "Packet buffer flushed";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000088 return_val = kFlushed;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000089 }
90
91 // Get an iterator pointing to the place in the buffer where the new packet
92 // should be inserted. The list is searched from the back, since the most
93 // likely case is that the new packet should be near the end of the list.
94 PacketList::reverse_iterator rit = std::find_if(
95 buffer_.rbegin(), buffer_.rend(),
96 NewTimestampIsLarger(packet));
minyue@webrtc.orgc8039072014-10-09 10:49:54 +000097
98 // The new packet is to be inserted to the right of |rit|. If it has the same
99 // timestamp as |rit|, which has a higher priority, do not insert the new
100 // packet to list.
ossua73f6c92016-10-24 08:25:28 -0700101 if (rit != buffer_.rend() && packet.timestamp == rit->timestamp) {
minyue@webrtc.orgc8039072014-10-09 10:49:54 +0000102 return return_val;
103 }
104
105 // The new packet is to be inserted to the left of |it|. If it has the same
106 // timestamp as |it|, which has a lower priority, replace |it| with the new
107 // packet.
108 PacketList::iterator it = rit.base();
ossua73f6c92016-10-24 08:25:28 -0700109 if (it != buffer_.end() && packet.timestamp == it->timestamp) {
minyue@webrtc.orgc8039072014-10-09 10:49:54 +0000110 it = buffer_.erase(it);
111 }
ossua73f6c92016-10-24 08:25:28 -0700112 buffer_.insert(it, std::move(packet)); // Insert the packet at that position.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113
114 return return_val;
115}
116
henrik.lundinda8bbf62016-08-31 03:14:11 -0700117int PacketBuffer::InsertPacketList(
118 PacketList* packet_list,
119 const DecoderDatabase& decoder_database,
120 rtc::Optional<uint8_t>* current_rtp_payload_type,
121 rtc::Optional<uint8_t>* current_cng_rtp_payload_type) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000122 bool flushed = false;
ossua73f6c92016-10-24 08:25:28 -0700123 for (auto& packet : *packet_list) {
124 if (decoder_database.IsComfortNoise(packet.payload_type)) {
henrik.lundinda8bbf62016-08-31 03:14:11 -0700125 if (*current_cng_rtp_payload_type &&
ossua73f6c92016-10-24 08:25:28 -0700126 **current_cng_rtp_payload_type != packet.payload_type) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000127 // New CNG payload type implies new codec type.
henrik.lundinda8bbf62016-08-31 03:14:11 -0700128 *current_rtp_payload_type = rtc::Optional<uint8_t>();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000129 Flush();
130 flushed = true;
131 }
henrik.lundinda8bbf62016-08-31 03:14:11 -0700132 *current_cng_rtp_payload_type =
ossua73f6c92016-10-24 08:25:28 -0700133 rtc::Optional<uint8_t>(packet.payload_type);
134 } else if (!decoder_database.IsDtmf(packet.payload_type)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000135 // This must be speech.
henrik.lundin067d8552016-09-01 23:19:05 -0700136 if ((*current_rtp_payload_type &&
ossua73f6c92016-10-24 08:25:28 -0700137 **current_rtp_payload_type != packet.payload_type) ||
henrik.lundin067d8552016-09-01 23:19:05 -0700138 (*current_cng_rtp_payload_type &&
ossua73f6c92016-10-24 08:25:28 -0700139 !EqualSampleRates(packet.payload_type,
henrik.lundin067d8552016-09-01 23:19:05 -0700140 **current_cng_rtp_payload_type,
141 decoder_database))) {
henrik.lundinda8bbf62016-08-31 03:14:11 -0700142 *current_cng_rtp_payload_type = rtc::Optional<uint8_t>();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000143 Flush();
144 flushed = true;
145 }
ossua73f6c92016-10-24 08:25:28 -0700146 *current_rtp_payload_type = rtc::Optional<uint8_t>(packet.payload_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000147 }
ossua73f6c92016-10-24 08:25:28 -0700148 int return_val = InsertPacket(std::move(packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000149 if (return_val == kFlushed) {
150 // The buffer flushed, but this is not an error. We can still continue.
151 flushed = true;
152 } else if (return_val != kOK) {
153 // An error occurred. Delete remaining packets in list and return.
ossua73f6c92016-10-24 08:25:28 -0700154 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000155 return return_val;
156 }
157 }
ossua73f6c92016-10-24 08:25:28 -0700158 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000159 return flushed ? kFlushed : kOK;
160}
161
162int PacketBuffer::NextTimestamp(uint32_t* next_timestamp) const {
163 if (Empty()) {
164 return kBufferEmpty;
165 }
166 if (!next_timestamp) {
167 return kInvalidPointer;
168 }
ossua73f6c92016-10-24 08:25:28 -0700169 *next_timestamp = buffer_.front().timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000170 return kOK;
171}
172
173int PacketBuffer::NextHigherTimestamp(uint32_t timestamp,
174 uint32_t* next_timestamp) const {
175 if (Empty()) {
176 return kBufferEmpty;
177 }
178 if (!next_timestamp) {
179 return kInvalidPointer;
180 }
181 PacketList::const_iterator it;
182 for (it = buffer_.begin(); it != buffer_.end(); ++it) {
ossua73f6c92016-10-24 08:25:28 -0700183 if (it->timestamp >= timestamp) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000184 // Found a packet matching the search.
ossua73f6c92016-10-24 08:25:28 -0700185 *next_timestamp = it->timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000186 return kOK;
187 }
188 }
189 return kNotFound;
190}
191
ossu7a377612016-10-18 04:06:13 -0700192const Packet* PacketBuffer::PeekNextPacket() const {
ossua73f6c92016-10-24 08:25:28 -0700193 return buffer_.empty() ? nullptr : &buffer_.front();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000194}
195
ossua73f6c92016-10-24 08:25:28 -0700196rtc::Optional<Packet> PacketBuffer::GetNextPacket() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000197 if (Empty()) {
198 // Buffer is empty.
ossua73f6c92016-10-24 08:25:28 -0700199 return rtc::Optional<Packet>();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000200 }
201
ossua73f6c92016-10-24 08:25:28 -0700202 rtc::Optional<Packet> packet(std::move(buffer_.front()));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000203 // Assert that the packet sanity checks in InsertPacket method works.
ossua73f6c92016-10-24 08:25:28 -0700204 RTC_DCHECK(!packet->empty());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000205 buffer_.pop_front();
minyue@webrtc.orgc8039072014-10-09 10:49:54 +0000206
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000207 return packet;
208}
209
minyue-webrtcfae474c2017-07-05 11:17:40 +0200210int PacketBuffer::DiscardNextPacket(StatisticsCalculator* stats) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000211 if (Empty()) {
212 return kBufferEmpty;
213 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000214 // Assert that the packet sanity checks in InsertPacket method works.
ossua73f6c92016-10-24 08:25:28 -0700215 RTC_DCHECK(!buffer_.front().empty());
216 buffer_.pop_front();
minyue-webrtcfae474c2017-07-05 11:17:40 +0200217 stats->PacketsDiscarded(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000218 return kOK;
219}
220
minyue-webrtcfae474c2017-07-05 11:17:40 +0200221void PacketBuffer::DiscardOldPackets(uint32_t timestamp_limit,
222 uint32_t horizon_samples,
223 StatisticsCalculator* stats) {
224 // TODO(minyue): the following implementation is wrong. It won't discard
225 // old packets if the buffer_.front() is newer than timestamp_limit -
226 // horizon_samples. https://bugs.chromium.org/p/webrtc/issues/detail?id=7937
ossua73f6c92016-10-24 08:25:28 -0700227 while (!Empty() && timestamp_limit != buffer_.front().timestamp &&
228 IsObsoleteTimestamp(buffer_.front().timestamp, timestamp_limit,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000229 horizon_samples)) {
minyue-webrtcfae474c2017-07-05 11:17:40 +0200230 if (DiscardNextPacket(stats) != kOK) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000231 assert(false); // Must be ok by design.
232 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000233 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000234}
235
minyue-webrtcfae474c2017-07-05 11:17:40 +0200236void PacketBuffer::DiscardAllOldPackets(uint32_t timestamp_limit,
237 StatisticsCalculator* stats) {
238 DiscardOldPackets(timestamp_limit, 0, stats);
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200239}
240
minyue-webrtcfae474c2017-07-05 11:17:40 +0200241void PacketBuffer::DiscardPacketsWithPayloadType(uint8_t payload_type,
242 StatisticsCalculator* stats) {
243 int packets_discarded = 0;
ossu61a208b2016-09-20 01:38:00 -0700244 for (auto it = buffer_.begin(); it != buffer_.end(); /* */) {
ossua73f6c92016-10-24 08:25:28 -0700245 const Packet& packet = *it;
246 if (packet.payload_type == payload_type) {
ossu61a208b2016-09-20 01:38:00 -0700247 it = buffer_.erase(it);
minyue-webrtcfae474c2017-07-05 11:17:40 +0200248 ++packets_discarded;
ossu61a208b2016-09-20 01:38:00 -0700249 } else {
250 ++it;
251 }
252 }
minyue-webrtcfae474c2017-07-05 11:17:40 +0200253 if (packets_discarded > 0)
254 stats->PacketsDiscarded(packets_discarded);
ossu61a208b2016-09-20 01:38:00 -0700255}
256
Peter Kastingdce40cf2015-08-24 14:52:23 -0700257size_t PacketBuffer::NumPacketsInBuffer() const {
258 return buffer_.size();
Karl Wiberg7f6c4d42015-04-09 15:44:22 +0200259}
260
ossu61a208b2016-09-20 01:38:00 -0700261size_t PacketBuffer::NumSamplesInBuffer(size_t last_decoded_length) const {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700262 size_t num_samples = 0;
263 size_t last_duration = last_decoded_length;
ossua73f6c92016-10-24 08:25:28 -0700264 for (const Packet& packet : buffer_) {
265 if (packet.frame) {
ossua70695a2016-09-22 02:06:28 -0700266 // TODO(hlundin): Verify that it's fine to count all packets and remove
267 // this check.
ossua73f6c92016-10-24 08:25:28 -0700268 if (packet.priority != Packet::Priority(0, 0)) {
minyue@webrtc.org0aa3ee62014-05-28 07:48:01 +0000269 continue;
minyue@webrtc.orgb28bfa72014-03-21 12:07:40 +0000270 }
ossua73f6c92016-10-24 08:25:28 -0700271 size_t duration = packet.frame->Duration();
ossu61a208b2016-09-20 01:38:00 -0700272 if (duration > 0) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000273 last_duration = duration; // Save the most up-to-date (valid) duration.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000274 }
275 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000276 num_samples += last_duration;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000277 }
278 return num_samples;
279}
280
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +0000281void PacketBuffer::BufferStat(int* num_packets, int* max_num_packets) const {
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000282 *num_packets = static_cast<int>(buffer_.size());
283 *max_num_packets = static_cast<int>(max_number_of_packets_);
turaj@webrtc.org7df97062013-08-02 18:07:13 +0000284}
285
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000286} // namespace webrtc