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