Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [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 "modules/video_coding/nack_module2.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | #include <limits> |
| 15 | |
| 16 | #include "api/units/timestamp.h" |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
| 18 | #include "rtc_base/experiments/field_trial_parser.h" |
| 19 | #include "rtc_base/logging.h" |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 20 | #include "rtc_base/task_queue.h" |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 21 | #include "system_wrappers/include/field_trial.h" |
| 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | namespace { |
| 26 | const int kMaxPacketAge = 10000; |
| 27 | const int kMaxNackPackets = 1000; |
| 28 | const int kDefaultRttMs = 100; |
| 29 | const int kMaxNackRetries = 10; |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 30 | const int kMaxReorderedPackets = 128; |
| 31 | const int kNumReorderingBuckets = 10; |
| 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 | } |
| 44 | } // namespace |
| 45 | |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 46 | constexpr TimeDelta NackModule2::kUpdateInterval; |
| 47 | |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 48 | NackModule2::NackInfo::NackInfo() |
| 49 | : seq_num(0), send_at_seq_num(0), sent_at_time(-1), retries(0) {} |
| 50 | |
| 51 | NackModule2::NackInfo::NackInfo(uint16_t seq_num, |
| 52 | uint16_t send_at_seq_num, |
| 53 | int64_t created_at_time) |
| 54 | : seq_num(seq_num), |
| 55 | send_at_seq_num(send_at_seq_num), |
| 56 | created_at_time(created_at_time), |
| 57 | sent_at_time(-1), |
| 58 | retries(0) {} |
| 59 | |
| 60 | NackModule2::BackoffSettings::BackoffSettings(TimeDelta min_retry, |
| 61 | TimeDelta max_rtt, |
| 62 | double base) |
| 63 | : min_retry_interval(min_retry), max_rtt(max_rtt), base(base) {} |
| 64 | |
| 65 | absl::optional<NackModule2::BackoffSettings> |
| 66 | NackModule2::BackoffSettings::ParseFromFieldTrials() { |
| 67 | // Matches magic number in RTPSender::OnReceivedNack(). |
| 68 | const TimeDelta kDefaultMinRetryInterval = TimeDelta::Millis(5); |
| 69 | // Upper bound on link-delay considered for exponential backoff. |
| 70 | // Selected so that cumulative delay with 1.25 base and 10 retries ends up |
| 71 | // below 3s, since above that there will be a FIR generated instead. |
| 72 | const TimeDelta kDefaultMaxRtt = TimeDelta::Millis(160); |
| 73 | // Default base for exponential backoff, adds 25% RTT delay for each retry. |
| 74 | const double kDefaultBase = 1.25; |
| 75 | |
| 76 | FieldTrialParameter<bool> enabled("enabled", false); |
| 77 | FieldTrialParameter<TimeDelta> min_retry("min_retry", |
| 78 | kDefaultMinRetryInterval); |
| 79 | FieldTrialParameter<TimeDelta> max_rtt("max_rtt", kDefaultMaxRtt); |
| 80 | FieldTrialParameter<double> base("base", kDefaultBase); |
| 81 | ParseFieldTrial({&enabled, &min_retry, &max_rtt, &base}, |
| 82 | field_trial::FindFullName("WebRTC-ExponentialNackBackoff")); |
| 83 | |
| 84 | if (enabled) { |
| 85 | return NackModule2::BackoffSettings(min_retry.Get(), max_rtt.Get(), |
| 86 | base.Get()); |
| 87 | } |
| 88 | return absl::nullopt; |
| 89 | } |
| 90 | |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 91 | NackModule2::NackModule2(TaskQueueBase* current_queue, |
| 92 | Clock* clock, |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 93 | NackSender* nack_sender, |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 94 | KeyFrameRequestSender* keyframe_request_sender, |
| 95 | TimeDelta update_interval /*= kUpdateInterval*/) |
| 96 | : worker_thread_(current_queue), |
| 97 | update_interval_(update_interval), |
| 98 | clock_(clock), |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 99 | nack_sender_(nack_sender), |
| 100 | keyframe_request_sender_(keyframe_request_sender), |
| 101 | reordering_histogram_(kNumReorderingBuckets, kMaxReorderedPackets), |
| 102 | initialized_(false), |
| 103 | rtt_ms_(kDefaultRttMs), |
| 104 | newest_seq_num_(0), |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 105 | send_nack_delay_ms_(GetSendNackDelay()), |
| 106 | backoff_settings_(BackoffSettings::ParseFromFieldTrials()) { |
| 107 | RTC_DCHECK(clock_); |
| 108 | RTC_DCHECK(nack_sender_); |
| 109 | RTC_DCHECK(keyframe_request_sender_); |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 110 | RTC_DCHECK_GT(update_interval.ms(), 0); |
| 111 | RTC_DCHECK(worker_thread_); |
| 112 | RTC_DCHECK(worker_thread_->IsCurrent()); |
| 113 | |
| 114 | repeating_task_ = RepeatingTaskHandle::DelayedStart( |
| 115 | TaskQueueBase::Current(), update_interval_, |
| 116 | [this]() { |
| 117 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 118 | std::vector<uint16_t> nack_batch = GetNackBatch(kTimeOnly); |
| 119 | if (!nack_batch.empty()) { |
| 120 | // This batch of NACKs is triggered externally; there is no external |
| 121 | // initiator who can batch them with other feedback messages. |
| 122 | nack_sender_->SendNack(nack_batch, /*buffering_allowed=*/false); |
| 123 | } |
| 124 | return update_interval_; |
| 125 | }, |
| 126 | clock_); |
| 127 | } |
| 128 | |
| 129 | NackModule2::~NackModule2() { |
| 130 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 131 | repeating_task_.Stop(); |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | int NackModule2::OnReceivedPacket(uint16_t seq_num, bool is_keyframe) { |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 135 | RTC_DCHECK_RUN_ON(worker_thread_); |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 136 | return OnReceivedPacket(seq_num, is_keyframe, false); |
| 137 | } |
| 138 | |
| 139 | int NackModule2::OnReceivedPacket(uint16_t seq_num, |
| 140 | bool is_keyframe, |
| 141 | bool is_recovered) { |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 142 | RTC_DCHECK_RUN_ON(worker_thread_); |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 143 | // TODO(philipel): When the packet includes information whether it is |
| 144 | // retransmitted or not, use that value instead. For |
| 145 | // now set it to true, which will cause the reordering |
| 146 | // statistics to never be updated. |
| 147 | bool is_retransmitted = true; |
| 148 | |
| 149 | if (!initialized_) { |
| 150 | newest_seq_num_ = seq_num; |
| 151 | if (is_keyframe) |
| 152 | keyframe_list_.insert(seq_num); |
| 153 | initialized_ = true; |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | // Since the |newest_seq_num_| is a packet we have actually received we know |
| 158 | // that packet has never been Nacked. |
| 159 | if (seq_num == newest_seq_num_) |
| 160 | return 0; |
| 161 | |
| 162 | if (AheadOf(newest_seq_num_, seq_num)) { |
| 163 | // An out of order packet has been received. |
| 164 | auto nack_list_it = nack_list_.find(seq_num); |
| 165 | int nacks_sent_for_packet = 0; |
| 166 | if (nack_list_it != nack_list_.end()) { |
| 167 | nacks_sent_for_packet = nack_list_it->second.retries; |
| 168 | nack_list_.erase(nack_list_it); |
| 169 | } |
| 170 | if (!is_retransmitted) |
| 171 | UpdateReorderingStatistics(seq_num); |
| 172 | return nacks_sent_for_packet; |
| 173 | } |
| 174 | |
| 175 | // Keep track of new keyframes. |
| 176 | if (is_keyframe) |
| 177 | keyframe_list_.insert(seq_num); |
| 178 | |
| 179 | // And remove old ones so we don't accumulate keyframes. |
| 180 | auto it = keyframe_list_.lower_bound(seq_num - kMaxPacketAge); |
| 181 | if (it != keyframe_list_.begin()) |
| 182 | keyframe_list_.erase(keyframe_list_.begin(), it); |
| 183 | |
| 184 | if (is_recovered) { |
| 185 | recovered_list_.insert(seq_num); |
| 186 | |
| 187 | // Remove old ones so we don't accumulate recovered packets. |
| 188 | auto it = recovered_list_.lower_bound(seq_num - kMaxPacketAge); |
| 189 | if (it != recovered_list_.begin()) |
| 190 | recovered_list_.erase(recovered_list_.begin(), it); |
| 191 | |
| 192 | // Do not send nack for packets recovered by FEC or RTX. |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | AddPacketsToNack(newest_seq_num_ + 1, seq_num); |
| 197 | newest_seq_num_ = seq_num; |
| 198 | |
| 199 | // Are there any nacks that are waiting for this seq_num. |
| 200 | std::vector<uint16_t> nack_batch = GetNackBatch(kSeqNumOnly); |
| 201 | if (!nack_batch.empty()) { |
| 202 | // This batch of NACKs is triggered externally; the initiator can |
| 203 | // batch them with other feedback messages. |
| 204 | nack_sender_->SendNack(nack_batch, /*buffering_allowed=*/true); |
| 205 | } |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | void NackModule2::ClearUpTo(uint16_t seq_num) { |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 211 | // Called via RtpVideoStreamReceiver2::FrameContinuous on the network thread. |
| 212 | worker_thread_->PostTask(ToQueuedTask(task_safety_, [seq_num, this]() { |
| 213 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 214 | nack_list_.erase(nack_list_.begin(), nack_list_.lower_bound(seq_num)); |
| 215 | keyframe_list_.erase(keyframe_list_.begin(), |
| 216 | keyframe_list_.lower_bound(seq_num)); |
| 217 | recovered_list_.erase(recovered_list_.begin(), |
| 218 | recovered_list_.lower_bound(seq_num)); |
| 219 | })); |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void NackModule2::UpdateRtt(int64_t rtt_ms) { |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 223 | RTC_DCHECK_RUN_ON(worker_thread_); |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 224 | rtt_ms_ = rtt_ms; |
| 225 | } |
| 226 | |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 227 | bool NackModule2::RemovePacketsUntilKeyFrame() { |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 228 | // Called on worker_thread_. |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 229 | while (!keyframe_list_.empty()) { |
| 230 | auto it = nack_list_.lower_bound(*keyframe_list_.begin()); |
| 231 | |
| 232 | if (it != nack_list_.begin()) { |
| 233 | // We have found a keyframe that actually is newer than at least one |
| 234 | // packet in the nack list. |
| 235 | nack_list_.erase(nack_list_.begin(), it); |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | // If this keyframe is so old it does not remove any packets from the list, |
| 240 | // remove it from the list of keyframes and try the next keyframe. |
| 241 | keyframe_list_.erase(keyframe_list_.begin()); |
| 242 | } |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | void NackModule2::AddPacketsToNack(uint16_t seq_num_start, |
| 247 | uint16_t seq_num_end) { |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 248 | // Called on worker_thread_. |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 249 | // Remove old packets. |
| 250 | auto it = nack_list_.lower_bound(seq_num_end - kMaxPacketAge); |
| 251 | nack_list_.erase(nack_list_.begin(), it); |
| 252 | |
| 253 | // If the nack list is too large, remove packets from the nack list until |
| 254 | // the latest first packet of a keyframe. If the list is still too large, |
| 255 | // clear it and request a keyframe. |
| 256 | uint16_t num_new_nacks = ForwardDiff(seq_num_start, seq_num_end); |
| 257 | if (nack_list_.size() + num_new_nacks > kMaxNackPackets) { |
| 258 | while (RemovePacketsUntilKeyFrame() && |
| 259 | nack_list_.size() + num_new_nacks > kMaxNackPackets) { |
| 260 | } |
| 261 | |
| 262 | if (nack_list_.size() + num_new_nacks > kMaxNackPackets) { |
| 263 | nack_list_.clear(); |
| 264 | RTC_LOG(LS_WARNING) << "NACK list full, clearing NACK" |
| 265 | " list and requesting keyframe."; |
| 266 | keyframe_request_sender_->RequestKeyFrame(); |
| 267 | return; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | for (uint16_t seq_num = seq_num_start; seq_num != seq_num_end; ++seq_num) { |
| 272 | // Do not send nack for packets that are already recovered by FEC or RTX |
| 273 | if (recovered_list_.find(seq_num) != recovered_list_.end()) |
| 274 | continue; |
| 275 | NackInfo nack_info(seq_num, seq_num + WaitNumberOfPackets(0.5), |
| 276 | clock_->TimeInMilliseconds()); |
| 277 | RTC_DCHECK(nack_list_.find(seq_num) == nack_list_.end()); |
| 278 | nack_list_[seq_num] = nack_info; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | std::vector<uint16_t> NackModule2::GetNackBatch(NackFilterOptions options) { |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 283 | // Called on worker_thread_. |
| 284 | |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 285 | bool consider_seq_num = options != kTimeOnly; |
| 286 | bool consider_timestamp = options != kSeqNumOnly; |
| 287 | Timestamp now = clock_->CurrentTime(); |
| 288 | std::vector<uint16_t> nack_batch; |
| 289 | auto it = nack_list_.begin(); |
| 290 | while (it != nack_list_.end()) { |
| 291 | TimeDelta resend_delay = TimeDelta::Millis(rtt_ms_); |
| 292 | if (backoff_settings_) { |
| 293 | resend_delay = |
| 294 | std::max(resend_delay, backoff_settings_->min_retry_interval); |
| 295 | if (it->second.retries > 1) { |
| 296 | TimeDelta exponential_backoff = |
| 297 | std::min(TimeDelta::Millis(rtt_ms_), backoff_settings_->max_rtt) * |
| 298 | std::pow(backoff_settings_->base, it->second.retries - 1); |
| 299 | resend_delay = std::max(resend_delay, exponential_backoff); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | bool delay_timed_out = |
| 304 | now.ms() - it->second.created_at_time >= send_nack_delay_ms_; |
| 305 | bool nack_on_rtt_passed = |
| 306 | now.ms() - it->second.sent_at_time >= resend_delay.ms(); |
| 307 | bool nack_on_seq_num_passed = |
| 308 | it->second.sent_at_time == -1 && |
| 309 | AheadOrAt(newest_seq_num_, it->second.send_at_seq_num); |
| 310 | if (delay_timed_out && ((consider_seq_num && nack_on_seq_num_passed) || |
| 311 | (consider_timestamp && nack_on_rtt_passed))) { |
| 312 | nack_batch.emplace_back(it->second.seq_num); |
| 313 | ++it->second.retries; |
| 314 | it->second.sent_at_time = now.ms(); |
| 315 | if (it->second.retries >= kMaxNackRetries) { |
| 316 | RTC_LOG(LS_WARNING) << "Sequence number " << it->second.seq_num |
| 317 | << " removed from NACK list due to max retries."; |
| 318 | it = nack_list_.erase(it); |
| 319 | } else { |
| 320 | ++it; |
| 321 | } |
| 322 | continue; |
| 323 | } |
| 324 | ++it; |
| 325 | } |
| 326 | return nack_batch; |
| 327 | } |
| 328 | |
| 329 | void NackModule2::UpdateReorderingStatistics(uint16_t seq_num) { |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 330 | // Running on worker_thread_. |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 331 | RTC_DCHECK(AheadOf(newest_seq_num_, seq_num)); |
| 332 | uint16_t diff = ReverseDiff(newest_seq_num_, seq_num); |
| 333 | reordering_histogram_.Add(diff); |
| 334 | } |
| 335 | |
| 336 | int NackModule2::WaitNumberOfPackets(float probability) const { |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 337 | // Called on worker_thread_; |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 338 | if (reordering_histogram_.NumValues() == 0) |
| 339 | return 0; |
| 340 | return reordering_histogram_.InverseCdf(probability); |
| 341 | } |
| 342 | |
| 343 | } // namespace webrtc |