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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "api/audio_codecs/audio_decoder.h" |
| 20 | #include "modules/audio_coding/neteq/decoder_database.h" |
| 21 | #include "modules/audio_coding/neteq/statistics_calculator.h" |
| 22 | #include "modules/audio_coding/neteq/tick_timer.h" |
| 23 | #include "rtc_base/logging.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
henrik.lundin | 067d855 | 2016-09-01 23:19:05 -0700 | [diff] [blame] | 26 | namespace { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 27 | // Predicate used when inserting packets in the buffer list. |
| 28 | // Operator() returns true when |packet| goes before |new_packet|. |
| 29 | class NewTimestampIsLarger { |
| 30 | public: |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 31 | explicit NewTimestampIsLarger(const Packet& new_packet) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 32 | : new_packet_(new_packet) {} |
| 33 | bool operator()(const Packet& packet) { return (new_packet_ >= packet); } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 34 | |
| 35 | private: |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 36 | const Packet& new_packet_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
henrik.lundin | 067d855 | 2016-09-01 23:19:05 -0700 | [diff] [blame] | 39 | // Returns true if both payload types are known to the decoder database, and |
| 40 | // have the same sample rate. |
| 41 | bool EqualSampleRates(uint8_t pt1, |
| 42 | uint8_t pt2, |
| 43 | const DecoderDatabase& decoder_database) { |
kjellander | 7c85658 | 2017-02-26 19:53:40 -0800 | [diff] [blame] | 44 | auto* di1 = decoder_database.GetDecoderInfo(pt1); |
| 45 | auto* di2 = decoder_database.GetDecoderInfo(pt2); |
henrik.lundin | 067d855 | 2016-09-01 23:19:05 -0700 | [diff] [blame] | 46 | return di1 && di2 && di1->SampleRateHz() == di2->SampleRateHz(); |
| 47 | } |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 48 | |
| 49 | void LogPacketDiscarded(int codec_level, StatisticsCalculator* stats) { |
| 50 | RTC_CHECK(stats); |
| 51 | if (codec_level > 0) { |
| 52 | stats->SecondaryPacketsDiscarded(1); |
| 53 | } else { |
| 54 | stats->PacketsDiscarded(1); |
| 55 | } |
| 56 | } |
| 57 | |
henrik.lundin | 067d855 | 2016-09-01 23:19:05 -0700 | [diff] [blame] | 58 | } // namespace |
| 59 | |
henrik.lundin | 84f8cd6 | 2016-04-26 07:45:16 -0700 | [diff] [blame] | 60 | PacketBuffer::PacketBuffer(size_t max_number_of_packets, |
| 61 | const TickTimer* tick_timer) |
| 62 | : 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] | 63 | |
| 64 | // Destructor. All packets in the buffer will be destroyed. |
| 65 | PacketBuffer::~PacketBuffer() { |
| 66 | Flush(); |
| 67 | } |
| 68 | |
| 69 | // Flush the buffer. All packets in the buffer will be destroyed. |
| 70 | void PacketBuffer::Flush() { |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 71 | buffer_.clear(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 74 | bool PacketBuffer::Empty() const { |
| 75 | return buffer_.empty(); |
| 76 | } |
| 77 | |
minyue-webrtc | 12d3084 | 2017-07-19 11:44:06 +0200 | [diff] [blame] | 78 | int PacketBuffer::InsertPacket(Packet&& packet, StatisticsCalculator* stats) { |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 79 | if (packet.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 80 | RTC_LOG(LS_WARNING) << "InsertPacket invalid packet"; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 81 | return kInvalidPacket; |
| 82 | } |
| 83 | |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 84 | RTC_DCHECK_GE(packet.priority.codec_level, 0); |
| 85 | RTC_DCHECK_GE(packet.priority.red_level, 0); |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 86 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 87 | int return_val = kOK; |
| 88 | |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 89 | packet.waiting_time = tick_timer_->GetNewStopwatch(); |
henrik.lundin | 84f8cd6 | 2016-04-26 07:45:16 -0700 | [diff] [blame] | 90 | |
henrik.lundin@webrtc.org | 116ed1d | 2014-04-28 08:20:04 +0000 | [diff] [blame] | 91 | if (buffer_.size() >= max_number_of_packets_) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 92 | // Buffer is full. Flush it. |
| 93 | Flush(); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 94 | RTC_LOG(LS_WARNING) << "Packet buffer flushed"; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 95 | return_val = kFlushed; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Get an iterator pointing to the place in the buffer where the new packet |
| 99 | // should be inserted. The list is searched from the back, since the most |
| 100 | // likely case is that the new packet should be near the end of the list. |
| 101 | PacketList::reverse_iterator rit = std::find_if( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 102 | buffer_.rbegin(), buffer_.rend(), NewTimestampIsLarger(packet)); |
minyue@webrtc.org | c803907 | 2014-10-09 10:49:54 +0000 | [diff] [blame] | 103 | |
| 104 | // The new packet is to be inserted to the right of |rit|. If it has the same |
| 105 | // timestamp as |rit|, which has a higher priority, do not insert the new |
| 106 | // packet to list. |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 107 | if (rit != buffer_.rend() && packet.timestamp == rit->timestamp) { |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 108 | LogPacketDiscarded(packet.priority.codec_level, stats); |
minyue@webrtc.org | c803907 | 2014-10-09 10:49:54 +0000 | [diff] [blame] | 109 | return return_val; |
| 110 | } |
| 111 | |
| 112 | // The new packet is to be inserted to the left of |it|. If it has the same |
| 113 | // timestamp as |it|, which has a lower priority, replace |it| with the new |
| 114 | // packet. |
| 115 | PacketList::iterator it = rit.base(); |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 116 | if (it != buffer_.end() && packet.timestamp == it->timestamp) { |
Peng Yu | b90e63c6 | 2018-06-07 19:20:55 +0800 | [diff] [blame] | 117 | LogPacketDiscarded(it->priority.codec_level, stats); |
minyue@webrtc.org | c803907 | 2014-10-09 10:49:54 +0000 | [diff] [blame] | 118 | it = buffer_.erase(it); |
| 119 | } |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 120 | buffer_.insert(it, std::move(packet)); // Insert the packet at that position. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 121 | |
| 122 | return return_val; |
| 123 | } |
| 124 | |
henrik.lundin | da8bbf6 | 2016-08-31 03:14:11 -0700 | [diff] [blame] | 125 | int PacketBuffer::InsertPacketList( |
| 126 | PacketList* packet_list, |
| 127 | const DecoderDatabase& decoder_database, |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 128 | absl::optional<uint8_t>* current_rtp_payload_type, |
| 129 | absl::optional<uint8_t>* current_cng_rtp_payload_type, |
minyue-webrtc | 12d3084 | 2017-07-19 11:44:06 +0200 | [diff] [blame] | 130 | StatisticsCalculator* stats) { |
| 131 | RTC_DCHECK(stats); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 132 | bool flushed = false; |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 133 | for (auto& packet : *packet_list) { |
| 134 | if (decoder_database.IsComfortNoise(packet.payload_type)) { |
henrik.lundin | da8bbf6 | 2016-08-31 03:14:11 -0700 | [diff] [blame] | 135 | if (*current_cng_rtp_payload_type && |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 136 | **current_cng_rtp_payload_type != packet.payload_type) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 137 | // New CNG payload type implies new codec type. |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 138 | *current_rtp_payload_type = absl::nullopt; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 139 | Flush(); |
| 140 | flushed = true; |
| 141 | } |
Oskar Sundbom | 12ab00b | 2017-11-16 15:31:38 +0100 | [diff] [blame] | 142 | *current_cng_rtp_payload_type = packet.payload_type; |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 143 | } else if (!decoder_database.IsDtmf(packet.payload_type)) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 144 | // This must be speech. |
henrik.lundin | 067d855 | 2016-09-01 23:19:05 -0700 | [diff] [blame] | 145 | if ((*current_rtp_payload_type && |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 146 | **current_rtp_payload_type != packet.payload_type) || |
henrik.lundin | 067d855 | 2016-09-01 23:19:05 -0700 | [diff] [blame] | 147 | (*current_cng_rtp_payload_type && |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 148 | !EqualSampleRates(packet.payload_type, |
henrik.lundin | 067d855 | 2016-09-01 23:19:05 -0700 | [diff] [blame] | 149 | **current_cng_rtp_payload_type, |
| 150 | decoder_database))) { |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 151 | *current_cng_rtp_payload_type = absl::nullopt; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 152 | Flush(); |
| 153 | flushed = true; |
| 154 | } |
Oskar Sundbom | 12ab00b | 2017-11-16 15:31:38 +0100 | [diff] [blame] | 155 | *current_rtp_payload_type = packet.payload_type; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 156 | } |
minyue-webrtc | 12d3084 | 2017-07-19 11:44:06 +0200 | [diff] [blame] | 157 | int return_val = InsertPacket(std::move(packet), stats); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 158 | if (return_val == kFlushed) { |
| 159 | // The buffer flushed, but this is not an error. We can still continue. |
| 160 | flushed = true; |
| 161 | } else if (return_val != kOK) { |
| 162 | // An error occurred. Delete remaining packets in list and return. |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 163 | packet_list->clear(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 164 | return return_val; |
| 165 | } |
| 166 | } |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 167 | packet_list->clear(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 168 | return flushed ? kFlushed : kOK; |
| 169 | } |
| 170 | |
| 171 | int PacketBuffer::NextTimestamp(uint32_t* next_timestamp) const { |
| 172 | if (Empty()) { |
| 173 | return kBufferEmpty; |
| 174 | } |
| 175 | if (!next_timestamp) { |
| 176 | return kInvalidPointer; |
| 177 | } |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 178 | *next_timestamp = buffer_.front().timestamp; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 179 | return kOK; |
| 180 | } |
| 181 | |
| 182 | int PacketBuffer::NextHigherTimestamp(uint32_t timestamp, |
| 183 | uint32_t* next_timestamp) const { |
| 184 | if (Empty()) { |
| 185 | return kBufferEmpty; |
| 186 | } |
| 187 | if (!next_timestamp) { |
| 188 | return kInvalidPointer; |
| 189 | } |
| 190 | PacketList::const_iterator it; |
| 191 | for (it = buffer_.begin(); it != buffer_.end(); ++it) { |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 192 | if (it->timestamp >= timestamp) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 193 | // Found a packet matching the search. |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 194 | *next_timestamp = it->timestamp; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 195 | return kOK; |
| 196 | } |
| 197 | } |
| 198 | return kNotFound; |
| 199 | } |
| 200 | |
ossu | 7a37761 | 2016-10-18 04:06:13 -0700 | [diff] [blame] | 201 | const Packet* PacketBuffer::PeekNextPacket() const { |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 202 | return buffer_.empty() ? nullptr : &buffer_.front(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 205 | absl::optional<Packet> PacketBuffer::GetNextPacket() { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 206 | if (Empty()) { |
| 207 | // Buffer is empty. |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 208 | return absl::nullopt; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 211 | absl::optional<Packet> packet(std::move(buffer_.front())); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 212 | // Assert that the packet sanity checks in InsertPacket method works. |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 213 | RTC_DCHECK(!packet->empty()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 214 | buffer_.pop_front(); |
minyue@webrtc.org | c803907 | 2014-10-09 10:49:54 +0000 | [diff] [blame] | 215 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 216 | return packet; |
| 217 | } |
| 218 | |
minyue-webrtc | fae474c | 2017-07-05 11:17:40 +0200 | [diff] [blame] | 219 | int PacketBuffer::DiscardNextPacket(StatisticsCalculator* stats) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 220 | if (Empty()) { |
| 221 | return kBufferEmpty; |
| 222 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 223 | // Assert that the packet sanity checks in InsertPacket method works. |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 224 | const Packet& packet = buffer_.front(); |
| 225 | RTC_DCHECK(!packet.empty()); |
| 226 | LogPacketDiscarded(packet.priority.codec_level, stats); |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 227 | buffer_.pop_front(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 228 | return kOK; |
| 229 | } |
| 230 | |
minyue-webrtc | fae474c | 2017-07-05 11:17:40 +0200 | [diff] [blame] | 231 | void PacketBuffer::DiscardOldPackets(uint32_t timestamp_limit, |
| 232 | uint32_t horizon_samples, |
| 233 | StatisticsCalculator* stats) { |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 234 | buffer_.remove_if([timestamp_limit, horizon_samples, stats](const Packet& p) { |
| 235 | if (timestamp_limit == p.timestamp || |
| 236 | !IsObsoleteTimestamp(p.timestamp, timestamp_limit, horizon_samples)) { |
| 237 | return false; |
| 238 | } |
| 239 | LogPacketDiscarded(p.priority.codec_level, stats); |
| 240 | return true; |
henrik.lundin | 63d146b | 2017-07-05 07:03:34 -0700 | [diff] [blame] | 241 | }); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 242 | } |
| 243 | |
minyue-webrtc | fae474c | 2017-07-05 11:17:40 +0200 | [diff] [blame] | 244 | void PacketBuffer::DiscardAllOldPackets(uint32_t timestamp_limit, |
| 245 | StatisticsCalculator* stats) { |
| 246 | DiscardOldPackets(timestamp_limit, 0, stats); |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 247 | } |
| 248 | |
minyue-webrtc | fae474c | 2017-07-05 11:17:40 +0200 | [diff] [blame] | 249 | void PacketBuffer::DiscardPacketsWithPayloadType(uint8_t payload_type, |
| 250 | StatisticsCalculator* stats) { |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 251 | buffer_.remove_if([payload_type, stats](const Packet& p) { |
| 252 | if (p.payload_type != payload_type) { |
| 253 | return false; |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 254 | } |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 255 | LogPacketDiscarded(p.priority.codec_level, stats); |
| 256 | return true; |
| 257 | }); |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 260 | size_t PacketBuffer::NumPacketsInBuffer() const { |
| 261 | return buffer_.size(); |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 262 | } |
| 263 | |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 264 | size_t PacketBuffer::NumSamplesInBuffer(size_t last_decoded_length) const { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 265 | size_t num_samples = 0; |
| 266 | size_t last_duration = last_decoded_length; |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 267 | for (const Packet& packet : buffer_) { |
| 268 | if (packet.frame) { |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 269 | // TODO(hlundin): Verify that it's fine to count all packets and remove |
| 270 | // this check. |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 271 | if (packet.priority != Packet::Priority(0, 0)) { |
minyue@webrtc.org | 0aa3ee6 | 2014-05-28 07:48:01 +0000 | [diff] [blame] | 272 | continue; |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 273 | } |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 274 | size_t duration = packet.frame->Duration(); |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 275 | if (duration > 0) { |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 276 | 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] | 277 | } |
| 278 | } |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 279 | num_samples += last_duration; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 280 | } |
| 281 | return num_samples; |
| 282 | } |
| 283 | |
Ivo Creusen | c7f09ad | 2018-05-22 13:21:01 +0200 | [diff] [blame] | 284 | bool PacketBuffer::ContainsDtxOrCngPacket( |
| 285 | const DecoderDatabase* decoder_database) const { |
| 286 | RTC_DCHECK(decoder_database); |
| 287 | for (const Packet& packet : buffer_) { |
| 288 | if ((packet.frame && packet.frame->IsDtxPacket()) || |
| 289 | decoder_database->IsComfortNoise(packet.payload_type)) { |
| 290 | return true; |
| 291 | } |
| 292 | } |
| 293 | return false; |
| 294 | } |
| 295 | |
henrik.lundin@webrtc.org | 116ed1d | 2014-04-28 08:20:04 +0000 | [diff] [blame] | 296 | void PacketBuffer::BufferStat(int* num_packets, int* max_num_packets) const { |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 297 | *num_packets = static_cast<int>(buffer_.size()); |
| 298 | *max_num_packets = static_cast<int>(max_number_of_packets_); |
turaj@webrtc.org | 7df9706 | 2013-08-02 18:07:13 +0000 | [diff] [blame] | 299 | } |
| 300 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 301 | } // namespace webrtc |