blob: e3eeb84d9f9a9ac073b8ea9c7e0917afed08bd79 [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"
philipel5ab4c6d2016-03-08 03:36:15 -080019
20namespace webrtc {
21
22namespace {
23const int kMaxPacketAge = 10000;
24const int kMaxNackPackets = 1000;
25const int kDefaultRttMs = 100;
26const int kMaxNackRetries = 10;
27const int kProcessFrequency = 50;
28const int kProcessIntervalMs = 1000 / kProcessFrequency;
29const int kMaxReorderedPackets = 128;
30const int kNumReorderingBuckets = 10;
31} // namespace
32
33NackModule::NackInfo::NackInfo()
34 : seq_num(0), send_at_seq_num(0), sent_at_time(-1), retries(0) {}
35
36NackModule::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
42NackModule::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),
philipel5ab4c6d2016-03-08 03:36:15 -080049 initialized_(false),
50 rtt_ms_(kDefaultRttMs),
philipel1a830c22016-05-13 11:12:00 +020051 newest_seq_num_(0),
philipel5ab4c6d2016-03-08 03:36:15 -080052 next_process_time_ms_(-1) {
53 RTC_DCHECK(clock_);
54 RTC_DCHECK(nack_sender_);
55 RTC_DCHECK(keyframe_request_sender_);
56}
57
Niels Möllerbc010472018-03-23 13:22:29 +010058int NackModule::OnReceivedPacket(uint16_t seq_num, bool is_keyframe) {
Ying Wangb32bb952018-10-31 10:12:27 +010059 return OnReceivedPacket(seq_num, is_keyframe, false);
60}
61
62int NackModule::OnReceivedPacket(uint16_t seq_num,
63 bool is_keyframe,
64 bool is_recovered) {
philipel5ab4c6d2016-03-08 03:36:15 -080065 rtc::CritScope lock(&crit_);
philipel5ab4c6d2016-03-08 03:36:15 -080066 // TODO(philipel): When the packet includes information whether it is
67 // retransmitted or not, use that value instead. For
68 // now set it to true, which will cause the reordering
69 // statistics to never be updated.
70 bool is_retransmitted = true;
philipel5ab4c6d2016-03-08 03:36:15 -080071
72 if (!initialized_) {
philipel1a830c22016-05-13 11:12:00 +020073 newest_seq_num_ = seq_num;
philipel5ab4c6d2016-03-08 03:36:15 -080074 if (is_keyframe)
75 keyframe_list_.insert(seq_num);
76 initialized_ = true;
philipel1a830c22016-05-13 11:12:00 +020077 return 0;
philipel5ab4c6d2016-03-08 03:36:15 -080078 }
79
philipel1a830c22016-05-13 11:12:00 +020080 // Since the |newest_seq_num_| is a packet we have actually received we know
81 // that packet has never been Nacked.
82 if (seq_num == newest_seq_num_)
83 return 0;
philipel5ab4c6d2016-03-08 03:36:15 -080084
philipel1a830c22016-05-13 11:12:00 +020085 if (AheadOf(newest_seq_num_, seq_num)) {
philipel5ab4c6d2016-03-08 03:36:15 -080086 // An out of order packet has been received.
philipel1a830c22016-05-13 11:12:00 +020087 auto nack_list_it = nack_list_.find(seq_num);
88 int nacks_sent_for_packet = 0;
89 if (nack_list_it != nack_list_.end()) {
90 nacks_sent_for_packet = nack_list_it->second.retries;
91 nack_list_.erase(nack_list_it);
92 }
philipel5ab4c6d2016-03-08 03:36:15 -080093 if (!is_retransmitted)
94 UpdateReorderingStatistics(seq_num);
philipel1a830c22016-05-13 11:12:00 +020095 return nacks_sent_for_packet;
philipel5ab4c6d2016-03-08 03:36:15 -080096 }
philipel1a830c22016-05-13 11:12:00 +020097
98 // Keep track of new keyframes.
99 if (is_keyframe)
100 keyframe_list_.insert(seq_num);
101
102 // And remove old ones so we don't accumulate keyframes.
103 auto it = keyframe_list_.lower_bound(seq_num - kMaxPacketAge);
104 if (it != keyframe_list_.begin())
105 keyframe_list_.erase(keyframe_list_.begin(), it);
106
Ying Wangb32bb952018-10-31 10:12:27 +0100107 if (is_recovered) {
108 recovered_list_.insert(seq_num);
109
110 // Remove old ones so we don't accumulate recovered packets.
111 auto it = recovered_list_.lower_bound(seq_num - kMaxPacketAge);
112 if (it != recovered_list_.begin())
113 recovered_list_.erase(recovered_list_.begin(), it);
114
115 // Do not send nack for packets recovered by FEC or RTX.
116 return 0;
117 }
118
119 AddPacketsToNack(newest_seq_num_ + 1, seq_num);
120 newest_seq_num_ = seq_num;
121
philipel1a830c22016-05-13 11:12:00 +0200122 // Are there any nacks that are waiting for this seq_num.
123 std::vector<uint16_t> nack_batch = GetNackBatch(kSeqNumOnly);
124 if (!nack_batch.empty())
125 nack_sender_->SendNack(nack_batch);
126
127 return 0;
philipel5ab4c6d2016-03-08 03:36:15 -0800128}
129
130void NackModule::ClearUpTo(uint16_t seq_num) {
131 rtc::CritScope lock(&crit_);
132 nack_list_.erase(nack_list_.begin(), nack_list_.lower_bound(seq_num));
133 keyframe_list_.erase(keyframe_list_.begin(),
134 keyframe_list_.lower_bound(seq_num));
Ying Wangb32bb952018-10-31 10:12:27 +0100135 recovered_list_.erase(recovered_list_.begin(),
136 recovered_list_.lower_bound(seq_num));
philipel5ab4c6d2016-03-08 03:36:15 -0800137}
138
139void NackModule::UpdateRtt(int64_t rtt_ms) {
140 rtc::CritScope lock(&crit_);
141 rtt_ms_ = rtt_ms;
142}
143
philipel83f831a2016-03-12 03:30:23 -0800144void NackModule::Clear() {
145 rtc::CritScope lock(&crit_);
146 nack_list_.clear();
147 keyframe_list_.clear();
Ying Wangb32bb952018-10-31 10:12:27 +0100148 recovered_list_.clear();
philipel83f831a2016-03-12 03:30:23 -0800149}
150
philipel5ab4c6d2016-03-08 03:36:15 -0800151int64_t NackModule::TimeUntilNextProcess() {
philipel5ab4c6d2016-03-08 03:36:15 -0800152 return std::max<int64_t>(next_process_time_ms_ - clock_->TimeInMilliseconds(),
153 0);
154}
155
156void NackModule::Process() {
tommif284b7f2017-02-27 01:59:36 -0800157 if (nack_sender_) {
158 std::vector<uint16_t> nack_batch;
159 {
160 rtc::CritScope lock(&crit_);
161 nack_batch = GetNackBatch(kTimeOnly);
162 }
163
164 if (!nack_batch.empty())
165 nack_sender_->SendNack(nack_batch);
166 }
philipel5ab4c6d2016-03-08 03:36:15 -0800167
168 // Update the next_process_time_ms_ in intervals to achieve
169 // the targeted frequency over time. Also add multiple intervals
170 // in case of a skip in time as to not make uneccessary
171 // calls to Process in order to catch up.
172 int64_t now_ms = clock_->TimeInMilliseconds();
173 if (next_process_time_ms_ == -1) {
174 next_process_time_ms_ = now_ms + kProcessIntervalMs;
175 } else {
176 next_process_time_ms_ = next_process_time_ms_ + kProcessIntervalMs +
177 (now_ms - next_process_time_ms_) /
178 kProcessIntervalMs * kProcessIntervalMs;
179 }
philipel5ab4c6d2016-03-08 03:36:15 -0800180}
181
182bool NackModule::RemovePacketsUntilKeyFrame() {
183 while (!keyframe_list_.empty()) {
184 auto it = nack_list_.lower_bound(*keyframe_list_.begin());
185
186 if (it != nack_list_.begin()) {
187 // We have found a keyframe that actually is newer than at least one
188 // packet in the nack list.
philipel5ab4c6d2016-03-08 03:36:15 -0800189 nack_list_.erase(nack_list_.begin(), it);
190 return true;
191 }
192
193 // If this keyframe is so old it does not remove any packets from the list,
194 // remove it from the list of keyframes and try the next keyframe.
195 keyframe_list_.erase(keyframe_list_.begin());
196 }
197 return false;
198}
199
200void NackModule::AddPacketsToNack(uint16_t seq_num_start,
201 uint16_t seq_num_end) {
202 // Remove old packets.
203 auto it = nack_list_.lower_bound(seq_num_end - kMaxPacketAge);
204 nack_list_.erase(nack_list_.begin(), it);
205
206 // If the nack list is too large, remove packets from the nack list until
207 // the latest first packet of a keyframe. If the list is still too large,
208 // clear it and request a keyframe.
209 uint16_t num_new_nacks = ForwardDiff(seq_num_start, seq_num_end);
210 if (nack_list_.size() + num_new_nacks > kMaxNackPackets) {
211 while (RemovePacketsUntilKeyFrame() &&
212 nack_list_.size() + num_new_nacks > kMaxNackPackets) {
213 }
214
215 if (nack_list_.size() + num_new_nacks > kMaxNackPackets) {
216 nack_list_.clear();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100217 RTC_LOG(LS_WARNING) << "NACK list full, clearing NACK"
218 " list and requesting keyframe.";
philipel5ab4c6d2016-03-08 03:36:15 -0800219 keyframe_request_sender_->RequestKeyFrame();
220 return;
221 }
222 }
223
224 for (uint16_t seq_num = seq_num_start; seq_num != seq_num_end; ++seq_num) {
Ying Wangb32bb952018-10-31 10:12:27 +0100225 // Do not send nack for packets that are already recovered by FEC or RTX
226 if (recovered_list_.find(seq_num) != recovered_list_.end())
227 continue;
philipel5ab4c6d2016-03-08 03:36:15 -0800228 NackInfo nack_info(seq_num, seq_num + WaitNumberOfPackets(0.5));
229 RTC_DCHECK(nack_list_.find(seq_num) == nack_list_.end());
230 nack_list_[seq_num] = nack_info;
231 }
232}
233
234std::vector<uint16_t> NackModule::GetNackBatch(NackFilterOptions options) {
235 bool consider_seq_num = options != kTimeOnly;
236 bool consider_timestamp = options != kSeqNumOnly;
237 int64_t now_ms = clock_->TimeInMilliseconds();
238 std::vector<uint16_t> nack_batch;
239 auto it = nack_list_.begin();
240 while (it != nack_list_.end()) {
241 if (consider_seq_num && it->second.sent_at_time == -1 &&
philipel1a830c22016-05-13 11:12:00 +0200242 AheadOrAt(newest_seq_num_, it->second.send_at_seq_num)) {
philipel5ab4c6d2016-03-08 03:36:15 -0800243 nack_batch.emplace_back(it->second.seq_num);
244 ++it->second.retries;
245 it->second.sent_at_time = now_ms;
246 if (it->second.retries >= kMaxNackRetries) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100247 RTC_LOG(LS_WARNING) << "Sequence number " << it->second.seq_num
248 << " removed from NACK list due to max retries.";
philipel5ab4c6d2016-03-08 03:36:15 -0800249 it = nack_list_.erase(it);
250 } else {
251 ++it;
252 }
253 continue;
254 }
255
256 if (consider_timestamp && it->second.sent_at_time + rtt_ms_ <= now_ms) {
257 nack_batch.emplace_back(it->second.seq_num);
258 ++it->second.retries;
259 it->second.sent_at_time = now_ms;
260 if (it->second.retries >= kMaxNackRetries) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100261 RTC_LOG(LS_WARNING) << "Sequence number " << it->second.seq_num
262 << " removed from NACK list due to max retries.";
philipel5ab4c6d2016-03-08 03:36:15 -0800263 it = nack_list_.erase(it);
264 } else {
265 ++it;
266 }
267 continue;
268 }
269 ++it;
270 }
271 return nack_batch;
272}
273
274void NackModule::UpdateReorderingStatistics(uint16_t seq_num) {
philipel1a830c22016-05-13 11:12:00 +0200275 RTC_DCHECK(AheadOf(newest_seq_num_, seq_num));
276 uint16_t diff = ReverseDiff(newest_seq_num_, seq_num);
philipel5ab4c6d2016-03-08 03:36:15 -0800277 reordering_histogram_.Add(diff);
278}
279
280int NackModule::WaitNumberOfPackets(float probability) const {
281 if (reordering_histogram_.NumValues() == 0)
282 return 0;
283 return reordering_histogram_.InverseCdf(probability);
284}
285
286} // namespace webrtc