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