blob: 9ae74c7915554e9f9c4b7c2424b7578a622b4a4d [file] [log] [blame]
philipel5ab4c6d2016-03-08 03:36:15 -08001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/video_coding/nack_module.h"
philipel5ab4c6d2016-03-08 03:36:15 -080015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/utility/include/process_thread.h"
17#include "rtc_base/checks.h"
18#include "rtc_base/logging.h"
Ying Wang0367d1a2018-11-02 14:51:15 +010019#include "system_wrappers/include/field_trial.h"
philipel5ab4c6d2016-03-08 03:36:15 -080020
21namespace webrtc {
22
23namespace {
24const int kMaxPacketAge = 10000;
25const int kMaxNackPackets = 1000;
26const int kDefaultRttMs = 100;
27const int kMaxNackRetries = 10;
28const int kProcessFrequency = 50;
29const int kProcessIntervalMs = 1000 / kProcessFrequency;
30const int kMaxReorderedPackets = 128;
31const int kNumReorderingBuckets = 10;
Ying Wang0367d1a2018-11-02 14:51:15 +010032const int kDefaultSendNackDelayMs = 0;
33
34int64_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}
philipel5ab4c6d2016-03-08 03:36:15 -080044} // namespace
45
46NackModule::NackInfo::NackInfo()
47 : seq_num(0), send_at_seq_num(0), sent_at_time(-1), retries(0) {}
48
Ying Wang0367d1a2018-11-02 14:51:15 +010049NackModule::NackInfo::NackInfo(uint16_t seq_num,
50 uint16_t send_at_seq_num,
51 int64_t created_at_time)
philipel5ab4c6d2016-03-08 03:36:15 -080052 : seq_num(seq_num),
53 send_at_seq_num(send_at_seq_num),
Ying Wang0367d1a2018-11-02 14:51:15 +010054 created_at_time(created_at_time),
philipel5ab4c6d2016-03-08 03:36:15 -080055 sent_at_time(-1),
56 retries(0) {}
57
58NackModule::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),
philipel5ab4c6d2016-03-08 03:36:15 -080065 initialized_(false),
66 rtt_ms_(kDefaultRttMs),
philipel1a830c22016-05-13 11:12:00 +020067 newest_seq_num_(0),
Ying Wang0367d1a2018-11-02 14:51:15 +010068 next_process_time_ms_(-1),
69 send_nack_delay_ms_(GetSendNackDelay()) {
philipel5ab4c6d2016-03-08 03:36:15 -080070 RTC_DCHECK(clock_);
71 RTC_DCHECK(nack_sender_);
72 RTC_DCHECK(keyframe_request_sender_);
73}
74
Niels Möllerbc010472018-03-23 13:22:29 +010075int NackModule::OnReceivedPacket(uint16_t seq_num, bool is_keyframe) {
Ying Wangb32bb952018-10-31 10:12:27 +010076 return OnReceivedPacket(seq_num, is_keyframe, false);
77}
78
79int NackModule::OnReceivedPacket(uint16_t seq_num,
80 bool is_keyframe,
81 bool is_recovered) {
philipel5ab4c6d2016-03-08 03:36:15 -080082 rtc::CritScope lock(&crit_);
philipel5ab4c6d2016-03-08 03:36:15 -080083 // 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;
philipel5ab4c6d2016-03-08 03:36:15 -080088
89 if (!initialized_) {
philipel1a830c22016-05-13 11:12:00 +020090 newest_seq_num_ = seq_num;
philipel5ab4c6d2016-03-08 03:36:15 -080091 if (is_keyframe)
92 keyframe_list_.insert(seq_num);
93 initialized_ = true;
philipel1a830c22016-05-13 11:12:00 +020094 return 0;
philipel5ab4c6d2016-03-08 03:36:15 -080095 }
96
philipel1a830c22016-05-13 11:12:00 +020097 // 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;
philipel5ab4c6d2016-03-08 03:36:15 -0800101
philipel1a830c22016-05-13 11:12:00 +0200102 if (AheadOf(newest_seq_num_, seq_num)) {
philipel5ab4c6d2016-03-08 03:36:15 -0800103 // An out of order packet has been received.
philipel1a830c22016-05-13 11:12:00 +0200104 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 }
philipel5ab4c6d2016-03-08 03:36:15 -0800110 if (!is_retransmitted)
111 UpdateReorderingStatistics(seq_num);
philipel1a830c22016-05-13 11:12:00 +0200112 return nacks_sent_for_packet;
philipel5ab4c6d2016-03-08 03:36:15 -0800113 }
philipel1a830c22016-05-13 11:12:00 +0200114
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 Wangb32bb952018-10-31 10:12:27 +0100124 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
philipel1a830c22016-05-13 11:12:00 +0200139 // Are there any nacks that are waiting for this seq_num.
140 std::vector<uint16_t> nack_batch = GetNackBatch(kSeqNumOnly);
Elad Alonef09c5b2019-05-31 13:25:50 +0200141 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 }
philipel1a830c22016-05-13 11:12:00 +0200146
147 return 0;
philipel5ab4c6d2016-03-08 03:36:15 -0800148}
149
150void 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 Wangb32bb952018-10-31 10:12:27 +0100155 recovered_list_.erase(recovered_list_.begin(),
156 recovered_list_.lower_bound(seq_num));
philipel5ab4c6d2016-03-08 03:36:15 -0800157}
158
159void NackModule::UpdateRtt(int64_t rtt_ms) {
160 rtc::CritScope lock(&crit_);
161 rtt_ms_ = rtt_ms;
162}
163
philipel83f831a2016-03-12 03:30:23 -0800164void NackModule::Clear() {
165 rtc::CritScope lock(&crit_);
166 nack_list_.clear();
167 keyframe_list_.clear();
Ying Wangb32bb952018-10-31 10:12:27 +0100168 recovered_list_.clear();
philipel83f831a2016-03-12 03:30:23 -0800169}
170
philipel5ab4c6d2016-03-08 03:36:15 -0800171int64_t NackModule::TimeUntilNextProcess() {
philipel5ab4c6d2016-03-08 03:36:15 -0800172 return std::max<int64_t>(next_process_time_ms_ - clock_->TimeInMilliseconds(),
173 0);
174}
175
176void NackModule::Process() {
tommif284b7f2017-02-27 01:59:36 -0800177 if (nack_sender_) {
178 std::vector<uint16_t> nack_batch;
179 {
180 rtc::CritScope lock(&crit_);
181 nack_batch = GetNackBatch(kTimeOnly);
182 }
183
Elad Alonef09c5b2019-05-31 13:25:50 +0200184 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 }
tommif284b7f2017-02-27 01:59:36 -0800189 }
philipel5ab4c6d2016-03-08 03:36:15 -0800190
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 }
philipel5ab4c6d2016-03-08 03:36:15 -0800203}
204
205bool 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.
philipel5ab4c6d2016-03-08 03:36:15 -0800212 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
223void 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 Bonadei675513b2017-11-09 11:09:25 +0100240 RTC_LOG(LS_WARNING) << "NACK list full, clearing NACK"
241 " list and requesting keyframe.";
philipel5ab4c6d2016-03-08 03:36:15 -0800242 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 Wangb32bb952018-10-31 10:12:27 +0100248 // 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 Wang0367d1a2018-11-02 14:51:15 +0100251 NackInfo nack_info(seq_num, seq_num + WaitNumberOfPackets(0.5),
252 clock_->TimeInMilliseconds());
philipel5ab4c6d2016-03-08 03:36:15 -0800253 RTC_DCHECK(nack_list_.find(seq_num) == nack_list_.end());
254 nack_list_[seq_num] = nack_info;
255 }
256}
257
258std::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 Wang0367d1a2018-11-02 14:51:15 +0100265 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))) {
philipel5ab4c6d2016-03-08 03:36:15 -0800273 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 Bonadei675513b2017-11-09 11:09:25 +0100277 RTC_LOG(LS_WARNING) << "Sequence number " << it->second.seq_num
278 << " removed from NACK list due to max retries.";
philipel5ab4c6d2016-03-08 03:36:15 -0800279 it = nack_list_.erase(it);
280 } else {
281 ++it;
282 }
283 continue;
284 }
285 ++it;
286 }
287 return nack_batch;
288}
289
290void NackModule::UpdateReorderingStatistics(uint16_t seq_num) {
philipel1a830c22016-05-13 11:12:00 +0200291 RTC_DCHECK(AheadOf(newest_seq_num_, seq_num));
292 uint16_t diff = ReverseDiff(newest_seq_num_, seq_num);
philipel5ab4c6d2016-03-08 03:36:15 -0800293 reordering_histogram_.Add(diff);
294}
295
296int 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