philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 | #include <algorithm> |
| 12 | #include <limits> |
| 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "modules/video_coding/nack_module.h" |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "modules/utility/include/process_thread.h" |
| 17 | #include "rtc_base/checks.h" |
| 18 | #include "rtc_base/logging.h" |
Ying Wang | 0367d1a | 2018-11-02 14:51:15 +0100 | [diff] [blame] | 19 | #include "system_wrappers/include/field_trial.h" |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | namespace { |
| 24 | const int kMaxPacketAge = 10000; |
| 25 | const int kMaxNackPackets = 1000; |
| 26 | const int kDefaultRttMs = 100; |
| 27 | const int kMaxNackRetries = 10; |
| 28 | const int kProcessFrequency = 50; |
| 29 | const int kProcessIntervalMs = 1000 / kProcessFrequency; |
| 30 | const int kMaxReorderedPackets = 128; |
| 31 | const int kNumReorderingBuckets = 10; |
Ying Wang | 0367d1a | 2018-11-02 14:51:15 +0100 | [diff] [blame] | 32 | const int kDefaultSendNackDelayMs = 0; |
| 33 | |
| 34 | int64_t GetSendNackDelay() { |
| 35 | int64_t delay_ms = strtol( |
| 36 | webrtc::field_trial::FindFullName("WebRTC-SendNackDelayMs").c_str(), |
| 37 | nullptr, 10); |
| 38 | if (delay_ms > 0 && delay_ms <= 20) { |
| 39 | RTC_LOG(LS_INFO) << "SendNackDelay is set to " << delay_ms; |
| 40 | return delay_ms; |
| 41 | } |
| 42 | return kDefaultSendNackDelayMs; |
| 43 | } |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 44 | } // namespace |
| 45 | |
| 46 | NackModule::NackInfo::NackInfo() |
| 47 | : seq_num(0), send_at_seq_num(0), sent_at_time(-1), retries(0) {} |
| 48 | |
Ying Wang | 0367d1a | 2018-11-02 14:51:15 +0100 | [diff] [blame] | 49 | NackModule::NackInfo::NackInfo(uint16_t seq_num, |
| 50 | uint16_t send_at_seq_num, |
| 51 | int64_t created_at_time) |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 52 | : seq_num(seq_num), |
| 53 | send_at_seq_num(send_at_seq_num), |
Ying Wang | 0367d1a | 2018-11-02 14:51:15 +0100 | [diff] [blame] | 54 | created_at_time(created_at_time), |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 55 | sent_at_time(-1), |
| 56 | retries(0) {} |
| 57 | |
| 58 | NackModule::NackModule(Clock* clock, |
| 59 | NackSender* nack_sender, |
| 60 | KeyFrameRequestSender* keyframe_request_sender) |
| 61 | : clock_(clock), |
| 62 | nack_sender_(nack_sender), |
| 63 | keyframe_request_sender_(keyframe_request_sender), |
| 64 | reordering_histogram_(kNumReorderingBuckets, kMaxReorderedPackets), |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 65 | initialized_(false), |
| 66 | rtt_ms_(kDefaultRttMs), |
philipel | 1a830c2 | 2016-05-13 11:12:00 +0200 | [diff] [blame] | 67 | newest_seq_num_(0), |
Ying Wang | 0367d1a | 2018-11-02 14:51:15 +0100 | [diff] [blame] | 68 | next_process_time_ms_(-1), |
| 69 | send_nack_delay_ms_(GetSendNackDelay()) { |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 70 | RTC_DCHECK(clock_); |
| 71 | RTC_DCHECK(nack_sender_); |
| 72 | RTC_DCHECK(keyframe_request_sender_); |
| 73 | } |
| 74 | |
Niels Möller | bc01047 | 2018-03-23 13:22:29 +0100 | [diff] [blame] | 75 | int NackModule::OnReceivedPacket(uint16_t seq_num, bool is_keyframe) { |
Ying Wang | b32bb95 | 2018-10-31 10:12:27 +0100 | [diff] [blame] | 76 | return OnReceivedPacket(seq_num, is_keyframe, false); |
| 77 | } |
| 78 | |
| 79 | int NackModule::OnReceivedPacket(uint16_t seq_num, |
| 80 | bool is_keyframe, |
| 81 | bool is_recovered) { |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 82 | rtc::CritScope lock(&crit_); |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 83 | // TODO(philipel): When the packet includes information whether it is |
| 84 | // retransmitted or not, use that value instead. For |
| 85 | // now set it to true, which will cause the reordering |
| 86 | // statistics to never be updated. |
| 87 | bool is_retransmitted = true; |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 88 | |
| 89 | if (!initialized_) { |
philipel | 1a830c2 | 2016-05-13 11:12:00 +0200 | [diff] [blame] | 90 | newest_seq_num_ = seq_num; |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 91 | if (is_keyframe) |
| 92 | keyframe_list_.insert(seq_num); |
| 93 | initialized_ = true; |
philipel | 1a830c2 | 2016-05-13 11:12:00 +0200 | [diff] [blame] | 94 | return 0; |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 95 | } |
| 96 | |
philipel | 1a830c2 | 2016-05-13 11:12:00 +0200 | [diff] [blame] | 97 | // Since the |newest_seq_num_| is a packet we have actually received we know |
| 98 | // that packet has never been Nacked. |
| 99 | if (seq_num == newest_seq_num_) |
| 100 | return 0; |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 101 | |
philipel | 1a830c2 | 2016-05-13 11:12:00 +0200 | [diff] [blame] | 102 | if (AheadOf(newest_seq_num_, seq_num)) { |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 103 | // An out of order packet has been received. |
philipel | 1a830c2 | 2016-05-13 11:12:00 +0200 | [diff] [blame] | 104 | auto nack_list_it = nack_list_.find(seq_num); |
| 105 | int nacks_sent_for_packet = 0; |
| 106 | if (nack_list_it != nack_list_.end()) { |
| 107 | nacks_sent_for_packet = nack_list_it->second.retries; |
| 108 | nack_list_.erase(nack_list_it); |
| 109 | } |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 110 | if (!is_retransmitted) |
| 111 | UpdateReorderingStatistics(seq_num); |
philipel | 1a830c2 | 2016-05-13 11:12:00 +0200 | [diff] [blame] | 112 | return nacks_sent_for_packet; |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 113 | } |
philipel | 1a830c2 | 2016-05-13 11:12:00 +0200 | [diff] [blame] | 114 | |
| 115 | // Keep track of new keyframes. |
| 116 | if (is_keyframe) |
| 117 | keyframe_list_.insert(seq_num); |
| 118 | |
| 119 | // And remove old ones so we don't accumulate keyframes. |
| 120 | auto it = keyframe_list_.lower_bound(seq_num - kMaxPacketAge); |
| 121 | if (it != keyframe_list_.begin()) |
| 122 | keyframe_list_.erase(keyframe_list_.begin(), it); |
| 123 | |
Ying Wang | b32bb95 | 2018-10-31 10:12:27 +0100 | [diff] [blame] | 124 | if (is_recovered) { |
| 125 | recovered_list_.insert(seq_num); |
| 126 | |
| 127 | // Remove old ones so we don't accumulate recovered packets. |
| 128 | auto it = recovered_list_.lower_bound(seq_num - kMaxPacketAge); |
| 129 | if (it != recovered_list_.begin()) |
| 130 | recovered_list_.erase(recovered_list_.begin(), it); |
| 131 | |
| 132 | // Do not send nack for packets recovered by FEC or RTX. |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | AddPacketsToNack(newest_seq_num_ + 1, seq_num); |
| 137 | newest_seq_num_ = seq_num; |
| 138 | |
philipel | 1a830c2 | 2016-05-13 11:12:00 +0200 | [diff] [blame] | 139 | // Are there any nacks that are waiting for this seq_num. |
| 140 | std::vector<uint16_t> nack_batch = GetNackBatch(kSeqNumOnly); |
Elad Alon | ef09c5b | 2019-05-31 13:25:50 +0200 | [diff] [blame^] | 141 | if (!nack_batch.empty()) { |
| 142 | // This batch of NACKs is triggered externally; the initiator can |
| 143 | // batch them with other feedback messages. |
| 144 | nack_sender_->SendNack(nack_batch, /*buffering_allowed=*/true); |
| 145 | } |
philipel | 1a830c2 | 2016-05-13 11:12:00 +0200 | [diff] [blame] | 146 | |
| 147 | return 0; |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void NackModule::ClearUpTo(uint16_t seq_num) { |
| 151 | rtc::CritScope lock(&crit_); |
| 152 | nack_list_.erase(nack_list_.begin(), nack_list_.lower_bound(seq_num)); |
| 153 | keyframe_list_.erase(keyframe_list_.begin(), |
| 154 | keyframe_list_.lower_bound(seq_num)); |
Ying Wang | b32bb95 | 2018-10-31 10:12:27 +0100 | [diff] [blame] | 155 | recovered_list_.erase(recovered_list_.begin(), |
| 156 | recovered_list_.lower_bound(seq_num)); |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void NackModule::UpdateRtt(int64_t rtt_ms) { |
| 160 | rtc::CritScope lock(&crit_); |
| 161 | rtt_ms_ = rtt_ms; |
| 162 | } |
| 163 | |
philipel | 83f831a | 2016-03-12 03:30:23 -0800 | [diff] [blame] | 164 | void NackModule::Clear() { |
| 165 | rtc::CritScope lock(&crit_); |
| 166 | nack_list_.clear(); |
| 167 | keyframe_list_.clear(); |
Ying Wang | b32bb95 | 2018-10-31 10:12:27 +0100 | [diff] [blame] | 168 | recovered_list_.clear(); |
philipel | 83f831a | 2016-03-12 03:30:23 -0800 | [diff] [blame] | 169 | } |
| 170 | |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 171 | int64_t NackModule::TimeUntilNextProcess() { |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 172 | return std::max<int64_t>(next_process_time_ms_ - clock_->TimeInMilliseconds(), |
| 173 | 0); |
| 174 | } |
| 175 | |
| 176 | void NackModule::Process() { |
tommi | f284b7f | 2017-02-27 01:59:36 -0800 | [diff] [blame] | 177 | if (nack_sender_) { |
| 178 | std::vector<uint16_t> nack_batch; |
| 179 | { |
| 180 | rtc::CritScope lock(&crit_); |
| 181 | nack_batch = GetNackBatch(kTimeOnly); |
| 182 | } |
| 183 | |
Elad Alon | ef09c5b | 2019-05-31 13:25:50 +0200 | [diff] [blame^] | 184 | if (!nack_batch.empty()) { |
| 185 | // This batch of NACKs is triggered externally; there is no external |
| 186 | // initiator who can batch them with other feedback messages. |
| 187 | nack_sender_->SendNack(nack_batch, /*buffering_allowed=*/false); |
| 188 | } |
tommi | f284b7f | 2017-02-27 01:59:36 -0800 | [diff] [blame] | 189 | } |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 190 | |
| 191 | // Update the next_process_time_ms_ in intervals to achieve |
| 192 | // the targeted frequency over time. Also add multiple intervals |
| 193 | // in case of a skip in time as to not make uneccessary |
| 194 | // calls to Process in order to catch up. |
| 195 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 196 | if (next_process_time_ms_ == -1) { |
| 197 | next_process_time_ms_ = now_ms + kProcessIntervalMs; |
| 198 | } else { |
| 199 | next_process_time_ms_ = next_process_time_ms_ + kProcessIntervalMs + |
| 200 | (now_ms - next_process_time_ms_) / |
| 201 | kProcessIntervalMs * kProcessIntervalMs; |
| 202 | } |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | bool NackModule::RemovePacketsUntilKeyFrame() { |
| 206 | while (!keyframe_list_.empty()) { |
| 207 | auto it = nack_list_.lower_bound(*keyframe_list_.begin()); |
| 208 | |
| 209 | if (it != nack_list_.begin()) { |
| 210 | // We have found a keyframe that actually is newer than at least one |
| 211 | // packet in the nack list. |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 212 | nack_list_.erase(nack_list_.begin(), it); |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | // If this keyframe is so old it does not remove any packets from the list, |
| 217 | // remove it from the list of keyframes and try the next keyframe. |
| 218 | keyframe_list_.erase(keyframe_list_.begin()); |
| 219 | } |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | void NackModule::AddPacketsToNack(uint16_t seq_num_start, |
| 224 | uint16_t seq_num_end) { |
| 225 | // Remove old packets. |
| 226 | auto it = nack_list_.lower_bound(seq_num_end - kMaxPacketAge); |
| 227 | nack_list_.erase(nack_list_.begin(), it); |
| 228 | |
| 229 | // If the nack list is too large, remove packets from the nack list until |
| 230 | // the latest first packet of a keyframe. If the list is still too large, |
| 231 | // clear it and request a keyframe. |
| 232 | uint16_t num_new_nacks = ForwardDiff(seq_num_start, seq_num_end); |
| 233 | if (nack_list_.size() + num_new_nacks > kMaxNackPackets) { |
| 234 | while (RemovePacketsUntilKeyFrame() && |
| 235 | nack_list_.size() + num_new_nacks > kMaxNackPackets) { |
| 236 | } |
| 237 | |
| 238 | if (nack_list_.size() + num_new_nacks > kMaxNackPackets) { |
| 239 | nack_list_.clear(); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 240 | RTC_LOG(LS_WARNING) << "NACK list full, clearing NACK" |
| 241 | " list and requesting keyframe."; |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 242 | keyframe_request_sender_->RequestKeyFrame(); |
| 243 | return; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | for (uint16_t seq_num = seq_num_start; seq_num != seq_num_end; ++seq_num) { |
Ying Wang | b32bb95 | 2018-10-31 10:12:27 +0100 | [diff] [blame] | 248 | // Do not send nack for packets that are already recovered by FEC or RTX |
| 249 | if (recovered_list_.find(seq_num) != recovered_list_.end()) |
| 250 | continue; |
Ying Wang | 0367d1a | 2018-11-02 14:51:15 +0100 | [diff] [blame] | 251 | NackInfo nack_info(seq_num, seq_num + WaitNumberOfPackets(0.5), |
| 252 | clock_->TimeInMilliseconds()); |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 253 | RTC_DCHECK(nack_list_.find(seq_num) == nack_list_.end()); |
| 254 | nack_list_[seq_num] = nack_info; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | std::vector<uint16_t> NackModule::GetNackBatch(NackFilterOptions options) { |
| 259 | bool consider_seq_num = options != kTimeOnly; |
| 260 | bool consider_timestamp = options != kSeqNumOnly; |
| 261 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 262 | std::vector<uint16_t> nack_batch; |
| 263 | auto it = nack_list_.begin(); |
| 264 | while (it != nack_list_.end()) { |
Ying Wang | 0367d1a | 2018-11-02 14:51:15 +0100 | [diff] [blame] | 265 | bool delay_timed_out = |
| 266 | now_ms - it->second.created_at_time >= send_nack_delay_ms_; |
| 267 | bool nack_on_rtt_passed = now_ms - it->second.sent_at_time >= rtt_ms_; |
| 268 | bool nack_on_seq_num_passed = |
| 269 | it->second.sent_at_time == -1 && |
| 270 | AheadOrAt(newest_seq_num_, it->second.send_at_seq_num); |
| 271 | if (delay_timed_out && ((consider_seq_num && nack_on_seq_num_passed) || |
| 272 | (consider_timestamp && nack_on_rtt_passed))) { |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 273 | nack_batch.emplace_back(it->second.seq_num); |
| 274 | ++it->second.retries; |
| 275 | it->second.sent_at_time = now_ms; |
| 276 | if (it->second.retries >= kMaxNackRetries) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 277 | RTC_LOG(LS_WARNING) << "Sequence number " << it->second.seq_num |
| 278 | << " removed from NACK list due to max retries."; |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 279 | it = nack_list_.erase(it); |
| 280 | } else { |
| 281 | ++it; |
| 282 | } |
| 283 | continue; |
| 284 | } |
| 285 | ++it; |
| 286 | } |
| 287 | return nack_batch; |
| 288 | } |
| 289 | |
| 290 | void NackModule::UpdateReorderingStatistics(uint16_t seq_num) { |
philipel | 1a830c2 | 2016-05-13 11:12:00 +0200 | [diff] [blame] | 291 | RTC_DCHECK(AheadOf(newest_seq_num_, seq_num)); |
| 292 | uint16_t diff = ReverseDiff(newest_seq_num_, seq_num); |
philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 293 | reordering_histogram_.Add(diff); |
| 294 | } |
| 295 | |
| 296 | int NackModule::WaitNumberOfPackets(float probability) const { |
| 297 | if (reordering_histogram_.NumValues() == 0) |
| 298 | return 0; |
| 299 | return reordering_histogram_.InverseCdf(probability); |
| 300 | } |
| 301 | |
| 302 | } // namespace webrtc |