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" |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 24 | #include "api/neteq/tick_timer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "modules/audio_coding/neteq/decoder_database.h" |
| 26 | #include "modules/audio_coding/neteq/statistics_calculator.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 27 | #include "rtc_base/checks.h" |
Ivo Creusen | 7b463c5 | 2020-11-25 11:32:40 +0100 | [diff] [blame] | 28 | #include "rtc_base/experiments/struct_parameters_parser.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "rtc_base/logging.h" |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 30 | #include "rtc_base/numerics/safe_conversions.h" |
Ivo Creusen | 7b463c5 | 2020-11-25 11:32:40 +0100 | [diff] [blame] | 31 | #include "system_wrappers/include/field_trial.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 32 | |
| 33 | namespace webrtc { |
henrik.lundin | 067d855 | 2016-09-01 23:19:05 -0700 | [diff] [blame] | 34 | namespace { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 35 | // Predicate used when inserting packets in the buffer list. |
| 36 | // Operator() returns true when |packet| goes before |new_packet|. |
| 37 | class NewTimestampIsLarger { |
| 38 | public: |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 39 | explicit NewTimestampIsLarger(const Packet& new_packet) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 40 | : new_packet_(new_packet) {} |
| 41 | bool operator()(const Packet& packet) { return (new_packet_ >= packet); } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 42 | |
| 43 | private: |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 44 | const Packet& new_packet_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
henrik.lundin | 067d855 | 2016-09-01 23:19:05 -0700 | [diff] [blame] | 47 | // Returns true if both payload types are known to the decoder database, and |
| 48 | // have the same sample rate. |
| 49 | bool EqualSampleRates(uint8_t pt1, |
| 50 | uint8_t pt2, |
| 51 | const DecoderDatabase& decoder_database) { |
kjellander | 7c85658 | 2017-02-26 19:53:40 -0800 | [diff] [blame] | 52 | auto* di1 = decoder_database.GetDecoderInfo(pt1); |
| 53 | auto* di2 = decoder_database.GetDecoderInfo(pt2); |
henrik.lundin | 067d855 | 2016-09-01 23:19:05 -0700 | [diff] [blame] | 54 | return di1 && di2 && di1->SampleRateHz() == di2->SampleRateHz(); |
| 55 | } |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 56 | |
| 57 | void LogPacketDiscarded(int codec_level, StatisticsCalculator* stats) { |
| 58 | RTC_CHECK(stats); |
| 59 | if (codec_level > 0) { |
| 60 | stats->SecondaryPacketsDiscarded(1); |
| 61 | } else { |
| 62 | stats->PacketsDiscarded(1); |
| 63 | } |
| 64 | } |
| 65 | |
Ivo Creusen | 7b463c5 | 2020-11-25 11:32:40 +0100 | [diff] [blame] | 66 | absl::optional<SmartFlushingConfig> GetSmartflushingConfig() { |
| 67 | absl::optional<SmartFlushingConfig> result; |
| 68 | std::string field_trial_string = |
| 69 | field_trial::FindFullName("WebRTC-Audio-NetEqSmartFlushing"); |
| 70 | result = SmartFlushingConfig(); |
| 71 | bool enabled = false; |
| 72 | auto parser = StructParametersParser::Create( |
| 73 | "enabled", &enabled, "target_level_threshold_ms", |
| 74 | &result->target_level_threshold_ms, "target_level_multiplier", |
| 75 | &result->target_level_multiplier); |
| 76 | parser->Parse(field_trial_string); |
| 77 | if (!enabled) { |
| 78 | return absl::nullopt; |
| 79 | } |
| 80 | RTC_LOG(LS_INFO) << "Using smart flushing, target_level_threshold_ms: " |
| 81 | << result->target_level_threshold_ms |
| 82 | << ", target_level_multiplier: " |
| 83 | << result->target_level_multiplier; |
| 84 | return result; |
| 85 | } |
| 86 | |
henrik.lundin | 067d855 | 2016-09-01 23:19:05 -0700 | [diff] [blame] | 87 | } // namespace |
| 88 | |
henrik.lundin | 84f8cd6 | 2016-04-26 07:45:16 -0700 | [diff] [blame] | 89 | PacketBuffer::PacketBuffer(size_t max_number_of_packets, |
| 90 | const TickTimer* tick_timer) |
Ivo Creusen | 7b463c5 | 2020-11-25 11:32:40 +0100 | [diff] [blame] | 91 | : smart_flushing_config_(GetSmartflushingConfig()), |
| 92 | max_number_of_packets_(max_number_of_packets), |
| 93 | tick_timer_(tick_timer) {} |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 94 | |
| 95 | // Destructor. All packets in the buffer will be destroyed. |
| 96 | PacketBuffer::~PacketBuffer() { |
Ivo Creusen | 7b463c5 | 2020-11-25 11:32:40 +0100 | [diff] [blame] | 97 | buffer_.clear(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // Flush the buffer. All packets in the buffer will be destroyed. |
Ivo Creusen | 7b463c5 | 2020-11-25 11:32:40 +0100 | [diff] [blame] | 101 | void PacketBuffer::Flush(StatisticsCalculator* stats) { |
| 102 | for (auto& p : buffer_) { |
| 103 | LogPacketDiscarded(p.priority.codec_level, stats); |
| 104 | } |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 105 | buffer_.clear(); |
Ivo Creusen | 7b463c5 | 2020-11-25 11:32:40 +0100 | [diff] [blame] | 106 | stats->FlushedPacketBuffer(); |
| 107 | } |
| 108 | |
| 109 | void PacketBuffer::PartialFlush(int target_level_ms, |
| 110 | size_t sample_rate, |
| 111 | size_t last_decoded_length, |
| 112 | StatisticsCalculator* stats) { |
| 113 | // Make sure that at least half the packet buffer capacity will be available |
| 114 | // after the flush. This is done to avoid getting stuck if the target level is |
| 115 | // very high. |
| 116 | int target_level_samples = |
| 117 | std::min(target_level_ms * sample_rate / 1000, |
| 118 | max_number_of_packets_ * last_decoded_length / 2); |
| 119 | // We should avoid flushing to very low levels. |
| 120 | target_level_samples = std::max( |
| 121 | target_level_samples, smart_flushing_config_->target_level_threshold_ms); |
| 122 | while (GetSpanSamples(last_decoded_length, sample_rate, true) > |
| 123 | static_cast<size_t>(target_level_samples) || |
| 124 | buffer_.size() > max_number_of_packets_ / 2) { |
| 125 | LogPacketDiscarded(PeekNextPacket()->priority.codec_level, stats); |
| 126 | buffer_.pop_front(); |
| 127 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 130 | bool PacketBuffer::Empty() const { |
| 131 | return buffer_.empty(); |
| 132 | } |
| 133 | |
Ivo Creusen | 7b463c5 | 2020-11-25 11:32:40 +0100 | [diff] [blame] | 134 | int PacketBuffer::InsertPacket(Packet&& packet, |
| 135 | StatisticsCalculator* stats, |
| 136 | size_t last_decoded_length, |
| 137 | size_t sample_rate, |
| 138 | int target_level_ms, |
| 139 | const DecoderDatabase& decoder_database) { |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 140 | if (packet.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 141 | RTC_LOG(LS_WARNING) << "InsertPacket invalid packet"; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 142 | return kInvalidPacket; |
| 143 | } |
| 144 | |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 145 | RTC_DCHECK_GE(packet.priority.codec_level, 0); |
| 146 | RTC_DCHECK_GE(packet.priority.red_level, 0); |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 147 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 148 | int return_val = kOK; |
| 149 | |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 150 | packet.waiting_time = tick_timer_->GetNewStopwatch(); |
henrik.lundin | 84f8cd6 | 2016-04-26 07:45:16 -0700 | [diff] [blame] | 151 | |
Ivo Creusen | 7b463c5 | 2020-11-25 11:32:40 +0100 | [diff] [blame] | 152 | // Perform a smart flush if the buffer size exceeds a multiple of the target |
| 153 | // level. |
| 154 | const size_t span_threshold = |
| 155 | smart_flushing_config_ |
| 156 | ? smart_flushing_config_->target_level_multiplier * |
| 157 | std::max(smart_flushing_config_->target_level_threshold_ms, |
| 158 | target_level_ms) * |
| 159 | sample_rate / 1000 |
| 160 | : 0; |
| 161 | const bool smart_flush = |
| 162 | smart_flushing_config_.has_value() && |
| 163 | GetSpanSamples(last_decoded_length, sample_rate, true) >= span_threshold; |
| 164 | if (buffer_.size() >= max_number_of_packets_ || smart_flush) { |
| 165 | size_t buffer_size_before_flush = buffer_.size(); |
| 166 | if (smart_flushing_config_.has_value()) { |
| 167 | // Flush down to the target level. |
| 168 | PartialFlush(target_level_ms, sample_rate, last_decoded_length, stats); |
| 169 | return_val = kPartialFlush; |
| 170 | } else { |
| 171 | // Buffer is full. |
| 172 | Flush(stats); |
| 173 | return_val = kFlushed; |
| 174 | } |
| 175 | RTC_LOG(LS_WARNING) << "Packet buffer flushed, " |
| 176 | << (buffer_size_before_flush - buffer_.size()) |
| 177 | << " packets discarded."; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // Get an iterator pointing to the place in the buffer where the new packet |
| 181 | // should be inserted. The list is searched from the back, since the most |
| 182 | // likely case is that the new packet should be near the end of the list. |
| 183 | PacketList::reverse_iterator rit = std::find_if( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 184 | buffer_.rbegin(), buffer_.rend(), NewTimestampIsLarger(packet)); |
minyue@webrtc.org | c803907 | 2014-10-09 10:49:54 +0000 | [diff] [blame] | 185 | |
| 186 | // The new packet is to be inserted to the right of |rit|. If it has the same |
| 187 | // timestamp as |rit|, which has a higher priority, do not insert the new |
| 188 | // packet to list. |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 189 | if (rit != buffer_.rend() && packet.timestamp == rit->timestamp) { |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 190 | LogPacketDiscarded(packet.priority.codec_level, stats); |
minyue@webrtc.org | c803907 | 2014-10-09 10:49:54 +0000 | [diff] [blame] | 191 | return return_val; |
| 192 | } |
| 193 | |
| 194 | // The new packet is to be inserted to the left of |it|. If it has the same |
| 195 | // timestamp as |it|, which has a lower priority, replace |it| with the new |
| 196 | // packet. |
| 197 | PacketList::iterator it = rit.base(); |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 198 | if (it != buffer_.end() && packet.timestamp == it->timestamp) { |
Peng Yu | b90e63c6 | 2018-06-07 19:20:55 +0800 | [diff] [blame] | 199 | LogPacketDiscarded(it->priority.codec_level, stats); |
minyue@webrtc.org | c803907 | 2014-10-09 10:49:54 +0000 | [diff] [blame] | 200 | it = buffer_.erase(it); |
| 201 | } |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 202 | 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] | 203 | |
| 204 | return return_val; |
| 205 | } |
| 206 | |
henrik.lundin | da8bbf6 | 2016-08-31 03:14:11 -0700 | [diff] [blame] | 207 | int PacketBuffer::InsertPacketList( |
| 208 | PacketList* packet_list, |
| 209 | const DecoderDatabase& decoder_database, |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 210 | absl::optional<uint8_t>* current_rtp_payload_type, |
| 211 | absl::optional<uint8_t>* current_cng_rtp_payload_type, |
Ivo Creusen | 7b463c5 | 2020-11-25 11:32:40 +0100 | [diff] [blame] | 212 | StatisticsCalculator* stats, |
| 213 | size_t last_decoded_length, |
| 214 | size_t sample_rate, |
| 215 | int target_level_ms) { |
minyue-webrtc | 12d3084 | 2017-07-19 11:44:06 +0200 | [diff] [blame] | 216 | RTC_DCHECK(stats); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 217 | bool flushed = false; |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 218 | for (auto& packet : *packet_list) { |
| 219 | if (decoder_database.IsComfortNoise(packet.payload_type)) { |
henrik.lundin | da8bbf6 | 2016-08-31 03:14:11 -0700 | [diff] [blame] | 220 | if (*current_cng_rtp_payload_type && |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 221 | **current_cng_rtp_payload_type != packet.payload_type) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 222 | // New CNG payload type implies new codec type. |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 223 | *current_rtp_payload_type = absl::nullopt; |
Ivo Creusen | 7b463c5 | 2020-11-25 11:32:40 +0100 | [diff] [blame] | 224 | Flush(stats); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 225 | flushed = true; |
| 226 | } |
Oskar Sundbom | 12ab00b | 2017-11-16 15:31:38 +0100 | [diff] [blame] | 227 | *current_cng_rtp_payload_type = packet.payload_type; |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 228 | } else if (!decoder_database.IsDtmf(packet.payload_type)) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 229 | // This must be speech. |
henrik.lundin | 067d855 | 2016-09-01 23:19:05 -0700 | [diff] [blame] | 230 | if ((*current_rtp_payload_type && |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 231 | **current_rtp_payload_type != packet.payload_type) || |
henrik.lundin | 067d855 | 2016-09-01 23:19:05 -0700 | [diff] [blame] | 232 | (*current_cng_rtp_payload_type && |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 233 | !EqualSampleRates(packet.payload_type, |
henrik.lundin | 067d855 | 2016-09-01 23:19:05 -0700 | [diff] [blame] | 234 | **current_cng_rtp_payload_type, |
| 235 | decoder_database))) { |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 236 | *current_cng_rtp_payload_type = absl::nullopt; |
Ivo Creusen | 7b463c5 | 2020-11-25 11:32:40 +0100 | [diff] [blame] | 237 | Flush(stats); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 238 | flushed = true; |
| 239 | } |
Oskar Sundbom | 12ab00b | 2017-11-16 15:31:38 +0100 | [diff] [blame] | 240 | *current_rtp_payload_type = packet.payload_type; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 241 | } |
Ivo Creusen | 7b463c5 | 2020-11-25 11:32:40 +0100 | [diff] [blame] | 242 | int return_val = |
| 243 | InsertPacket(std::move(packet), stats, last_decoded_length, sample_rate, |
| 244 | target_level_ms, decoder_database); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 245 | if (return_val == kFlushed) { |
| 246 | // The buffer flushed, but this is not an error. We can still continue. |
| 247 | flushed = true; |
| 248 | } else if (return_val != kOK) { |
| 249 | // An error occurred. Delete remaining packets in list and return. |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 250 | packet_list->clear(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 251 | return return_val; |
| 252 | } |
| 253 | } |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 254 | packet_list->clear(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 255 | return flushed ? kFlushed : kOK; |
| 256 | } |
| 257 | |
| 258 | int PacketBuffer::NextTimestamp(uint32_t* next_timestamp) const { |
| 259 | if (Empty()) { |
| 260 | return kBufferEmpty; |
| 261 | } |
| 262 | if (!next_timestamp) { |
| 263 | return kInvalidPointer; |
| 264 | } |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 265 | *next_timestamp = buffer_.front().timestamp; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 266 | return kOK; |
| 267 | } |
| 268 | |
| 269 | int PacketBuffer::NextHigherTimestamp(uint32_t timestamp, |
| 270 | uint32_t* next_timestamp) const { |
| 271 | if (Empty()) { |
| 272 | return kBufferEmpty; |
| 273 | } |
| 274 | if (!next_timestamp) { |
| 275 | return kInvalidPointer; |
| 276 | } |
| 277 | PacketList::const_iterator it; |
| 278 | for (it = buffer_.begin(); it != buffer_.end(); ++it) { |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 279 | if (it->timestamp >= timestamp) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 280 | // Found a packet matching the search. |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 281 | *next_timestamp = it->timestamp; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 282 | return kOK; |
| 283 | } |
| 284 | } |
| 285 | return kNotFound; |
| 286 | } |
| 287 | |
ossu | 7a37761 | 2016-10-18 04:06:13 -0700 | [diff] [blame] | 288 | const Packet* PacketBuffer::PeekNextPacket() const { |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 289 | return buffer_.empty() ? nullptr : &buffer_.front(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 292 | absl::optional<Packet> PacketBuffer::GetNextPacket() { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 293 | if (Empty()) { |
| 294 | // Buffer is empty. |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 295 | return absl::nullopt; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 298 | absl::optional<Packet> packet(std::move(buffer_.front())); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 299 | // Assert that the packet sanity checks in InsertPacket method works. |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 300 | RTC_DCHECK(!packet->empty()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 301 | buffer_.pop_front(); |
minyue@webrtc.org | c803907 | 2014-10-09 10:49:54 +0000 | [diff] [blame] | 302 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 303 | return packet; |
| 304 | } |
| 305 | |
minyue-webrtc | fae474c | 2017-07-05 11:17:40 +0200 | [diff] [blame] | 306 | int PacketBuffer::DiscardNextPacket(StatisticsCalculator* stats) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 307 | if (Empty()) { |
| 308 | return kBufferEmpty; |
| 309 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 310 | // Assert that the packet sanity checks in InsertPacket method works. |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 311 | const Packet& packet = buffer_.front(); |
| 312 | RTC_DCHECK(!packet.empty()); |
| 313 | LogPacketDiscarded(packet.priority.codec_level, stats); |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 314 | buffer_.pop_front(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 315 | return kOK; |
| 316 | } |
| 317 | |
minyue-webrtc | fae474c | 2017-07-05 11:17:40 +0200 | [diff] [blame] | 318 | void PacketBuffer::DiscardOldPackets(uint32_t timestamp_limit, |
| 319 | uint32_t horizon_samples, |
| 320 | StatisticsCalculator* stats) { |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 321 | buffer_.remove_if([timestamp_limit, horizon_samples, stats](const Packet& p) { |
| 322 | if (timestamp_limit == p.timestamp || |
| 323 | !IsObsoleteTimestamp(p.timestamp, timestamp_limit, horizon_samples)) { |
| 324 | return false; |
| 325 | } |
| 326 | LogPacketDiscarded(p.priority.codec_level, stats); |
| 327 | return true; |
henrik.lundin | 63d146b | 2017-07-05 07:03:34 -0700 | [diff] [blame] | 328 | }); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 329 | } |
| 330 | |
minyue-webrtc | fae474c | 2017-07-05 11:17:40 +0200 | [diff] [blame] | 331 | void PacketBuffer::DiscardAllOldPackets(uint32_t timestamp_limit, |
| 332 | StatisticsCalculator* stats) { |
| 333 | DiscardOldPackets(timestamp_limit, 0, stats); |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 334 | } |
| 335 | |
minyue-webrtc | fae474c | 2017-07-05 11:17:40 +0200 | [diff] [blame] | 336 | void PacketBuffer::DiscardPacketsWithPayloadType(uint8_t payload_type, |
| 337 | StatisticsCalculator* stats) { |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 338 | buffer_.remove_if([payload_type, stats](const Packet& p) { |
| 339 | if (p.payload_type != payload_type) { |
| 340 | return false; |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 341 | } |
minyue-webrtc | 0c3ca75 | 2017-08-23 15:59:38 +0200 | [diff] [blame] | 342 | LogPacketDiscarded(p.priority.codec_level, stats); |
| 343 | return true; |
| 344 | }); |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 347 | size_t PacketBuffer::NumPacketsInBuffer() const { |
| 348 | return buffer_.size(); |
Karl Wiberg | 7f6c4d4 | 2015-04-09 15:44:22 +0200 | [diff] [blame] | 349 | } |
| 350 | |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 351 | size_t PacketBuffer::NumSamplesInBuffer(size_t last_decoded_length) const { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 352 | size_t num_samples = 0; |
| 353 | size_t last_duration = last_decoded_length; |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 354 | for (const Packet& packet : buffer_) { |
| 355 | if (packet.frame) { |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 356 | // TODO(hlundin): Verify that it's fine to count all packets and remove |
| 357 | // this check. |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 358 | if (packet.priority != Packet::Priority(0, 0)) { |
minyue@webrtc.org | 0aa3ee6 | 2014-05-28 07:48:01 +0000 | [diff] [blame] | 359 | continue; |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 360 | } |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 361 | size_t duration = packet.frame->Duration(); |
ossu | 61a208b | 2016-09-20 01:38:00 -0700 | [diff] [blame] | 362 | if (duration > 0) { |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 363 | 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] | 364 | } |
| 365 | } |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 366 | num_samples += last_duration; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 367 | } |
| 368 | return num_samples; |
| 369 | } |
| 370 | |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 371 | size_t PacketBuffer::GetSpanSamples(size_t last_decoded_length, |
| 372 | size_t sample_rate, |
| 373 | bool count_dtx_waiting_time) const { |
Jakob Ivarsson | 1b4254a | 2019-03-12 15:12:08 +0100 | [diff] [blame] | 374 | if (buffer_.size() == 0) { |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | size_t span = buffer_.back().timestamp - buffer_.front().timestamp; |
| 379 | if (buffer_.back().frame && buffer_.back().frame->Duration() > 0) { |
Jakob Ivarsson | 46dda83 | 2019-07-03 16:00:30 +0200 | [diff] [blame] | 380 | size_t duration = buffer_.back().frame->Duration(); |
| 381 | if (count_dtx_waiting_time && buffer_.back().frame->IsDtxPacket()) { |
| 382 | size_t waiting_time_samples = rtc::dchecked_cast<size_t>( |
| 383 | buffer_.back().waiting_time->ElapsedMs() * (sample_rate / 1000)); |
| 384 | duration = std::max(duration, waiting_time_samples); |
| 385 | } |
| 386 | span += duration; |
Jakob Ivarsson | 1b4254a | 2019-03-12 15:12:08 +0100 | [diff] [blame] | 387 | } else { |
| 388 | span += last_decoded_length; |
| 389 | } |
| 390 | return span; |
| 391 | } |
| 392 | |
Ivo Creusen | c7f09ad | 2018-05-22 13:21:01 +0200 | [diff] [blame] | 393 | bool PacketBuffer::ContainsDtxOrCngPacket( |
| 394 | const DecoderDatabase* decoder_database) const { |
| 395 | RTC_DCHECK(decoder_database); |
| 396 | for (const Packet& packet : buffer_) { |
| 397 | if ((packet.frame && packet.frame->IsDtxPacket()) || |
| 398 | decoder_database->IsComfortNoise(packet.payload_type)) { |
| 399 | return true; |
| 400 | } |
| 401 | } |
| 402 | return false; |
| 403 | } |
| 404 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 405 | } // namespace webrtc |