blob: 0c69b57749349ffafce1d0877e5fd50704a71071 [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);
141 if (!nack_batch.empty())
142 nack_sender_->SendNack(nack_batch);
143
144 return 0;
philipel5ab4c6d2016-03-08 03:36:15 -0800145}
146
147void NackModule::ClearUpTo(uint16_t seq_num) {
148 rtc::CritScope lock(&crit_);
149 nack_list_.erase(nack_list_.begin(), nack_list_.lower_bound(seq_num));
150 keyframe_list_.erase(keyframe_list_.begin(),
151 keyframe_list_.lower_bound(seq_num));
Ying Wangb32bb952018-10-31 10:12:27 +0100152 recovered_list_.erase(recovered_list_.begin(),
153 recovered_list_.lower_bound(seq_num));
philipel5ab4c6d2016-03-08 03:36:15 -0800154}
155
156void NackModule::UpdateRtt(int64_t rtt_ms) {
157 rtc::CritScope lock(&crit_);
158 rtt_ms_ = rtt_ms;
159}
160
philipel83f831a2016-03-12 03:30:23 -0800161void NackModule::Clear() {
162 rtc::CritScope lock(&crit_);
163 nack_list_.clear();
164 keyframe_list_.clear();
Ying Wangb32bb952018-10-31 10:12:27 +0100165 recovered_list_.clear();
philipel83f831a2016-03-12 03:30:23 -0800166}
167
philipel5ab4c6d2016-03-08 03:36:15 -0800168int64_t NackModule::TimeUntilNextProcess() {
philipel5ab4c6d2016-03-08 03:36:15 -0800169 return std::max<int64_t>(next_process_time_ms_ - clock_->TimeInMilliseconds(),
170 0);
171}
172
173void NackModule::Process() {
tommif284b7f2017-02-27 01:59:36 -0800174 if (nack_sender_) {
175 std::vector<uint16_t> nack_batch;
176 {
177 rtc::CritScope lock(&crit_);
178 nack_batch = GetNackBatch(kTimeOnly);
179 }
180
181 if (!nack_batch.empty())
182 nack_sender_->SendNack(nack_batch);
183 }
philipel5ab4c6d2016-03-08 03:36:15 -0800184
185 // Update the next_process_time_ms_ in intervals to achieve
186 // the targeted frequency over time. Also add multiple intervals
187 // in case of a skip in time as to not make uneccessary
188 // calls to Process in order to catch up.
189 int64_t now_ms = clock_->TimeInMilliseconds();
190 if (next_process_time_ms_ == -1) {
191 next_process_time_ms_ = now_ms + kProcessIntervalMs;
192 } else {
193 next_process_time_ms_ = next_process_time_ms_ + kProcessIntervalMs +
194 (now_ms - next_process_time_ms_) /
195 kProcessIntervalMs * kProcessIntervalMs;
196 }
philipel5ab4c6d2016-03-08 03:36:15 -0800197}
198
199bool NackModule::RemovePacketsUntilKeyFrame() {
200 while (!keyframe_list_.empty()) {
201 auto it = nack_list_.lower_bound(*keyframe_list_.begin());
202
203 if (it != nack_list_.begin()) {
204 // We have found a keyframe that actually is newer than at least one
205 // packet in the nack list.
philipel5ab4c6d2016-03-08 03:36:15 -0800206 nack_list_.erase(nack_list_.begin(), it);
207 return true;
208 }
209
210 // If this keyframe is so old it does not remove any packets from the list,
211 // remove it from the list of keyframes and try the next keyframe.
212 keyframe_list_.erase(keyframe_list_.begin());
213 }
214 return false;
215}
216
217void NackModule::AddPacketsToNack(uint16_t seq_num_start,
218 uint16_t seq_num_end) {
219 // Remove old packets.
220 auto it = nack_list_.lower_bound(seq_num_end - kMaxPacketAge);
221 nack_list_.erase(nack_list_.begin(), it);
222
223 // If the nack list is too large, remove packets from the nack list until
224 // the latest first packet of a keyframe. If the list is still too large,
225 // clear it and request a keyframe.
226 uint16_t num_new_nacks = ForwardDiff(seq_num_start, seq_num_end);
227 if (nack_list_.size() + num_new_nacks > kMaxNackPackets) {
228 while (RemovePacketsUntilKeyFrame() &&
229 nack_list_.size() + num_new_nacks > kMaxNackPackets) {
230 }
231
232 if (nack_list_.size() + num_new_nacks > kMaxNackPackets) {
233 nack_list_.clear();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100234 RTC_LOG(LS_WARNING) << "NACK list full, clearing NACK"
235 " list and requesting keyframe.";
philipel5ab4c6d2016-03-08 03:36:15 -0800236 keyframe_request_sender_->RequestKeyFrame();
237 return;
238 }
239 }
240
241 for (uint16_t seq_num = seq_num_start; seq_num != seq_num_end; ++seq_num) {
Ying Wangb32bb952018-10-31 10:12:27 +0100242 // Do not send nack for packets that are already recovered by FEC or RTX
243 if (recovered_list_.find(seq_num) != recovered_list_.end())
244 continue;
Ying Wang0367d1a2018-11-02 14:51:15 +0100245 NackInfo nack_info(seq_num, seq_num + WaitNumberOfPackets(0.5),
246 clock_->TimeInMilliseconds());
philipel5ab4c6d2016-03-08 03:36:15 -0800247 RTC_DCHECK(nack_list_.find(seq_num) == nack_list_.end());
248 nack_list_[seq_num] = nack_info;
249 }
250}
251
252std::vector<uint16_t> NackModule::GetNackBatch(NackFilterOptions options) {
253 bool consider_seq_num = options != kTimeOnly;
254 bool consider_timestamp = options != kSeqNumOnly;
255 int64_t now_ms = clock_->TimeInMilliseconds();
256 std::vector<uint16_t> nack_batch;
257 auto it = nack_list_.begin();
258 while (it != nack_list_.end()) {
Ying Wang0367d1a2018-11-02 14:51:15 +0100259 bool delay_timed_out =
260 now_ms - it->second.created_at_time >= send_nack_delay_ms_;
261 bool nack_on_rtt_passed = now_ms - it->second.sent_at_time >= rtt_ms_;
262 bool nack_on_seq_num_passed =
263 it->second.sent_at_time == -1 &&
264 AheadOrAt(newest_seq_num_, it->second.send_at_seq_num);
265 if (delay_timed_out && ((consider_seq_num && nack_on_seq_num_passed) ||
266 (consider_timestamp && nack_on_rtt_passed))) {
philipel5ab4c6d2016-03-08 03:36:15 -0800267 nack_batch.emplace_back(it->second.seq_num);
268 ++it->second.retries;
269 it->second.sent_at_time = now_ms;
270 if (it->second.retries >= kMaxNackRetries) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100271 RTC_LOG(LS_WARNING) << "Sequence number " << it->second.seq_num
272 << " removed from NACK list due to max retries.";
philipel5ab4c6d2016-03-08 03:36:15 -0800273 it = nack_list_.erase(it);
274 } else {
275 ++it;
276 }
277 continue;
278 }
279 ++it;
280 }
281 return nack_batch;
282}
283
284void NackModule::UpdateReorderingStatistics(uint16_t seq_num) {
philipel1a830c22016-05-13 11:12:00 +0200285 RTC_DCHECK(AheadOf(newest_seq_num_, seq_num));
286 uint16_t diff = ReverseDiff(newest_seq_num_, seq_num);
philipel5ab4c6d2016-03-08 03:36:15 -0800287 reordering_histogram_.Add(diff);
288}
289
290int NackModule::WaitNumberOfPackets(float probability) const {
291 if (reordering_histogram_.NumValues() == 0)
292 return 0;
293 return reordering_histogram_.InverseCdf(probability);
294}
295
296} // namespace webrtc