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