henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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 | // 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.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 15 | #include "webrtc/modules/audio_coding/neteq/packet_buffer.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 16 | |
| 17 | #include <algorithm> // find_if() |
| 18 | |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 19 | #include "webrtc/base/logging.h" |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 20 | #include "webrtc/modules/audio_coding/codecs/audio_decoder.h" |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 21 | #include "webrtc/modules/audio_coding/neteq/decoder_database.h" |
henrik.lundin | 84f8cd6 | 2016-04-26 07:45:16 -0700 | [diff] [blame] | 22 | #include "webrtc/modules/audio_coding/neteq/tick_timer.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
henrik.lundin | 067d855 | 2016-09-01 23:19:05 -0700 | [diff] [blame^] | 25 | namespace { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 26 | // Predicate used when inserting packets in the buffer list. |
| 27 | // Operator() returns true when |packet| goes before |new_packet|. |
| 28 | class NewTimestampIsLarger { |
| 29 | public: |
| 30 | explicit NewTimestampIsLarger(const Packet* new_packet) |
| 31 | : new_packet_(new_packet) { |
| 32 | } |
| 33 | bool operator()(Packet* packet) { |
| 34 | return (*new_packet_ >= *packet); |
| 35 | } |
| 36 | |
| 37 | private: |
| 38 | const Packet* new_packet_; |
| 39 | }; |
| 40 | |
henrik.lundin | 067d855 | 2016-09-01 23:19:05 -0700 | [diff] [blame^] | 41 | // Returns true if both payload types are known to the decoder database, and |
| 42 | // have the same sample rate. |
| 43 | bool EqualSampleRates(uint8_t pt1, |
| 44 | uint8_t pt2, |
| 45 | const DecoderDatabase& decoder_database) { |
| 46 | auto di1 = decoder_database.GetDecoderInfo(pt1); |
| 47 | auto di2 = decoder_database.GetDecoderInfo(pt2); |
| 48 | return di1 && di2 && di1->SampleRateHz() == di2->SampleRateHz(); |
| 49 | } |
| 50 | } // namespace |
| 51 | |
henrik.lundin | 84f8cd6 | 2016-04-26 07:45:16 -0700 | [diff] [blame] | 52 | PacketBuffer::PacketBuffer(size_t max_number_of_packets, |
| 53 | const TickTimer* tick_timer) |
| 54 | : max_number_of_packets_(max_number_of_packets), tick_timer_(tick_timer) {} |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 55 | |
| 56 | // Destructor. All packets in the buffer will be destroyed. |
| 57 | PacketBuffer::~PacketBuffer() { |
| 58 | Flush(); |
| 59 | } |
| 60 | |
| 61 | // Flush the buffer. All packets in the buffer will be destroyed. |
| 62 | void PacketBuffer::Flush() { |
| 63 | DeleteAllPackets(&buffer_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 66 | bool PacketBuffer::Empty() const { |
| 67 | return buffer_.empty(); |
| 68 | } |
| 69 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 70 | int PacketBuffer::InsertPacket(Packet* packet) { |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 71 | if (!packet || packet->payload.empty()) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 72 | if (packet) { |
| 73 | delete packet; |
| 74 | } |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 75 | LOG(LS_WARNING) << "InsertPacket invalid packet"; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 76 | return kInvalidPacket; |
| 77 | } |
| 78 | |
| 79 | int return_val = kOK; |
| 80 | |
henrik.lundin | 84f8cd6 | 2016-04-26 07:45:16 -0700 | [diff] [blame] | 81 | packet->waiting_time = tick_timer_->GetNewStopwatch(); |
| 82 | |
henrik.lundin@webrtc.org | 116ed1d | 2014-04-28 08:20:04 +0000 | [diff] [blame] | 83 | if (buffer_.size() >= max_number_of_packets_) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 84 | // Buffer is full. Flush it. |
| 85 | Flush(); |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 86 | LOG(LS_WARNING) << "Packet buffer flushed"; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 87 | return_val = kFlushed; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // Get an iterator pointing to the place in the buffer where the new packet |
| 91 | // should be inserted. The list is searched from the back, since the most |
| 92 | // likely case is that the new packet should be near the end of the list. |
| 93 | PacketList::reverse_iterator rit = std::find_if( |
| 94 | buffer_.rbegin(), buffer_.rend(), |
| 95 | NewTimestampIsLarger(packet)); |
minyue@webrtc.org | c803907 | 2014-10-09 10:49:54 +0000 | [diff] [blame] | 96 | |
| 97 | // The new packet is to be inserted to the right of |rit|. If it has the same |
| 98 | // timestamp as |rit|, which has a higher priority, do not insert the new |
| 99 | // packet to list. |
| 100 | if (rit != buffer_.rend() && |
| 101 | packet->header.timestamp == (*rit)->header.timestamp) { |
minyue@webrtc.org | c803907 | 2014-10-09 10:49:54 +0000 | [diff] [blame] | 102 | delete packet; |
| 103 | return return_val; |
| 104 | } |
| 105 | |
| 106 | // The new packet is to be inserted to the left of |it|. If it has the same |
| 107 | // timestamp as |it|, which has a lower priority, replace |it| with the new |
| 108 | // packet. |
| 109 | PacketList::iterator it = rit.base(); |
| 110 | if (it != buffer_.end() && |
| 111 | packet->header.timestamp == (*it)->header.timestamp) { |
minyue@webrtc.org | c803907 | 2014-10-09 10:49:54 +0000 | [diff] [blame] | 112 | delete *it; |
| 113 | it = buffer_.erase(it); |
| 114 | } |
| 115 | buffer_.insert(it, packet); // Insert the packet at that position. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 116 | |
| 117 | return return_val; |
| 118 | } |
| 119 | |
henrik.lundin | da8bbf6 | 2016-08-31 03:14:11 -0700 | [diff] [blame] | 120 | int PacketBuffer::InsertPacketList( |
| 121 | PacketList* packet_list, |
| 122 | const DecoderDatabase& decoder_database, |
| 123 | rtc::Optional<uint8_t>* current_rtp_payload_type, |
| 124 | rtc::Optional<uint8_t>* current_cng_rtp_payload_type) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 125 | bool flushed = false; |
| 126 | while (!packet_list->empty()) { |
| 127 | Packet* packet = packet_list->front(); |
| 128 | if (decoder_database.IsComfortNoise(packet->header.payloadType)) { |
henrik.lundin | da8bbf6 | 2016-08-31 03:14:11 -0700 | [diff] [blame] | 129 | if (*current_cng_rtp_payload_type && |
| 130 | **current_cng_rtp_payload_type != packet->header.payloadType) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 131 | // New CNG payload type implies new codec type. |
henrik.lundin | da8bbf6 | 2016-08-31 03:14:11 -0700 | [diff] [blame] | 132 | *current_rtp_payload_type = rtc::Optional<uint8_t>(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 133 | Flush(); |
| 134 | flushed = true; |
| 135 | } |
henrik.lundin | da8bbf6 | 2016-08-31 03:14:11 -0700 | [diff] [blame] | 136 | *current_cng_rtp_payload_type = |
| 137 | rtc::Optional<uint8_t>(packet->header.payloadType); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 138 | } else if (!decoder_database.IsDtmf(packet->header.payloadType)) { |
| 139 | // This must be speech. |
henrik.lundin | 067d855 | 2016-09-01 23:19:05 -0700 | [diff] [blame^] | 140 | if ((*current_rtp_payload_type && |
| 141 | **current_rtp_payload_type != packet->header.payloadType) || |
| 142 | (*current_cng_rtp_payload_type && |
| 143 | !EqualSampleRates(packet->header.payloadType, |
| 144 | **current_cng_rtp_payload_type, |
| 145 | decoder_database))) { |
henrik.lundin | da8bbf6 | 2016-08-31 03:14:11 -0700 | [diff] [blame] | 146 | *current_cng_rtp_payload_type = rtc::Optional<uint8_t>(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 147 | Flush(); |
| 148 | flushed = true; |
| 149 | } |
henrik.lundin | da8bbf6 | 2016-08-31 03:14:11 -0700 | [diff] [blame] | 150 | *current_rtp_payload_type = |
| 151 | rtc::Optional<uint8_t>(packet->header.payloadType); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 152 | } |
| 153 | int return_val = InsertPacket(packet); |
| 154 | packet_list->pop_front(); |
| 155 | if (return_val == kFlushed) { |
| 156 | // The buffer flushed, but this is not an error. We can still continue. |
| 157 | flushed = true; |
| 158 | } else if (return_val != kOK) { |
| 159 | // An error occurred. Delete remaining packets in list and return. |
| 160 | DeleteAllPackets(packet_list); |
| 161 | return return_val; |
| 162 | } |
| 163 | } |
| 164 | return flushed ? kFlushed : kOK; |
| 165 | } |
| 166 | |
| 167 | int PacketBuffer::NextTimestamp(uint32_t* next_timestamp) const { |
| 168 | if (Empty()) { |
| 169 | return kBufferEmpty; |
| 170 | } |
| 171 | if (!next_timestamp) { |
| 172 | return kInvalidPointer; |
| 173 | } |
| 174 | *next_timestamp = buffer_.front()->header.timestamp; |
| 175 | return kOK; |
| 176 | } |
| 177 | |
| 178 | int PacketBuffer::NextHigherTimestamp(uint32_t timestamp, |
| 179 | uint32_t* next_timestamp) const { |
| 180 | if (Empty()) { |
| 181 | return kBufferEmpty; |
| 182 | } |
| 183 | if (!next_timestamp) { |
| 184 | return kInvalidPointer; |
| 185 | } |
| 186 | PacketList::const_iterator it; |
| 187 | for (it = buffer_.begin(); it != buffer_.end(); ++it) { |
| 188 | if ((*it)->header.timestamp >= timestamp) { |
| 189 | // Found a packet matching the search. |
| 190 | *next_timestamp = (*it)->header.timestamp; |
| 191 | return kOK; |
| 192 | } |
| 193 | } |
| 194 | return kNotFound; |
| 195 | } |
| 196 | |
| 197 | const RTPHeader* PacketBuffer::NextRtpHeader() const { |
| 198 | if (Empty()) { |
| 199 | return NULL; |
| 200 | } |
| 201 | return const_cast<const RTPHeader*>(&(buffer_.front()->header)); |
| 202 | } |
| 203 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 204 | Packet* PacketBuffer::GetNextPacket(size_t* discard_count) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 205 | if (Empty()) { |
| 206 | // Buffer is empty. |
| 207 | return NULL; |
| 208 | } |
| 209 | |
| 210 | Packet* packet = buffer_.front(); |
| 211 | // Assert that the packet sanity checks in InsertPacket method works. |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 212 | assert(packet && !packet->payload.empty()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 213 | buffer_.pop_front(); |
minyue@webrtc.org | c803907 | 2014-10-09 10:49:54 +0000 | [diff] [blame] | 214 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 215 | // Discard other packets with the same timestamp. These are duplicates or |
| 216 | // redundant payloads that should not be used. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 217 | size_t discards = 0; |
minyue@webrtc.org | c803907 | 2014-10-09 10:49:54 +0000 | [diff] [blame] | 218 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 219 | while (!Empty() && |
| 220 | buffer_.front()->header.timestamp == packet->header.timestamp) { |
| 221 | if (DiscardNextPacket() != kOK) { |
| 222 | assert(false); // Must be ok by design. |
| 223 | } |
minyue@webrtc.org | c803907 | 2014-10-09 10:49:54 +0000 | [diff] [blame] | 224 | ++discards; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 225 | } |
minyue@webrtc.org | c803907 | 2014-10-09 10:49:54 +0000 | [diff] [blame] | 226 | // The way of inserting packet should not cause any packet discarding here. |
| 227 | // TODO(minyue): remove |discard_count|. |
| 228 | assert(discards == 0); |
| 229 | if (discard_count) |
| 230 | *discard_count = discards; |
| 231 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 232 | return packet; |
| 233 | } |
| 234 | |
| 235 | int PacketBuffer::DiscardNextPacket() { |
| 236 | if (Empty()) { |
| 237 | return kBufferEmpty; |
| 238 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 239 | // Assert that the packet sanity checks in InsertPacket method works. |
henrik.lundin@webrtc.org | 12a3424 | 2014-04-28 08:36:35 +0000 | [diff] [blame] | 240 | assert(buffer_.front()); |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 241 | assert(!buffer_.front()->payload.empty()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 242 | DeleteFirstPacket(&buffer_); |
| 243 | return kOK; |
| 244 | } |
| 245 | |
henrik.lundin@webrtc.org | 52b42cb | 2014-11-04 14:03:58 +0000 | [diff] [blame] | 246 | int PacketBuffer::DiscardOldPackets(uint32_t timestamp_limit, |
| 247 | uint32_t horizon_samples) { |
| 248 | while (!Empty() && timestamp_limit != buffer_.front()->header.timestamp && |
| 249 | IsObsoleteTimestamp(buffer_.front()->header.timestamp, |
| 250 | timestamp_limit, |
| 251 | horizon_samples)) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 252 | if (DiscardNextPacket() != kOK) { |
| 253 | assert(false); // Must be ok by design. |
| 254 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 255 | } |
| 256 | return 0; |
| 257 | } |
| 258 | |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 259 | int PacketBuffer::DiscardAllOldPackets(uint32_t timestamp_limit) { |
| 260 | return DiscardOldPackets(timestamp_limit, 0); |
| 261 | } |
| 262 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 263 | size_t PacketBuffer::NumPacketsInBuffer() const { |
| 264 | return buffer_.size(); |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 265 | } |
| 266 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 267 | size_t PacketBuffer::NumSamplesInBuffer(DecoderDatabase* decoder_database, |
| 268 | size_t last_decoded_length) const { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 269 | PacketList::const_iterator it; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 270 | size_t num_samples = 0; |
| 271 | size_t last_duration = last_decoded_length; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 272 | for (it = buffer_.begin(); it != buffer_.end(); ++it) { |
| 273 | Packet* packet = (*it); |
| 274 | AudioDecoder* decoder = |
| 275 | decoder_database->GetDecoder(packet->header.payloadType); |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 276 | if (decoder && !packet->sync_packet) { |
| 277 | if (!packet->primary) { |
minyue@webrtc.org | 0aa3ee6 | 2014-05-28 07:48:01 +0000 | [diff] [blame] | 278 | continue; |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 279 | } |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 280 | int duration = decoder->PacketDuration(packet->payload.data(), |
| 281 | packet->payload.size()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 282 | if (duration >= 0) { |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 283 | last_duration = duration; // Save the most up-to-date (valid) duration. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 286 | num_samples += last_duration; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 287 | } |
| 288 | return num_samples; |
| 289 | } |
| 290 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 291 | bool PacketBuffer::DeleteFirstPacket(PacketList* packet_list) { |
| 292 | if (packet_list->empty()) { |
| 293 | return false; |
| 294 | } |
| 295 | Packet* first_packet = packet_list->front(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 296 | delete first_packet; |
| 297 | packet_list->pop_front(); |
| 298 | return true; |
| 299 | } |
| 300 | |
| 301 | void PacketBuffer::DeleteAllPackets(PacketList* packet_list) { |
| 302 | while (DeleteFirstPacket(packet_list)) { |
| 303 | // Continue while the list is not empty. |
| 304 | } |
| 305 | } |
| 306 | |
henrik.lundin@webrtc.org | 116ed1d | 2014-04-28 08:20:04 +0000 | [diff] [blame] | 307 | void PacketBuffer::BufferStat(int* num_packets, int* max_num_packets) const { |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 308 | *num_packets = static_cast<int>(buffer_.size()); |
| 309 | *max_num_packets = static_cast<int>(max_number_of_packets_); |
turaj@webrtc.org | 7df9706 | 2013-08-02 18:07:13 +0000 | [diff] [blame] | 310 | } |
| 311 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 312 | } // namespace webrtc |