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