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