blob: ab66b2b562f5218a8e98b43e1333ca118dac779b [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) {
philipel5ab4c6d2016-03-08 03:36:15 -080059 rtc::CritScope lock(&crit_);
philipel5ab4c6d2016-03-08 03:36:15 -080060 // 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;
philipel5ab4c6d2016-03-08 03:36:15 -080065
66 if (!initialized_) {
philipel1a830c22016-05-13 11:12:00 +020067 newest_seq_num_ = seq_num;
philipel5ab4c6d2016-03-08 03:36:15 -080068 if (is_keyframe)
69 keyframe_list_.insert(seq_num);
70 initialized_ = true;
philipel1a830c22016-05-13 11:12:00 +020071 return 0;
philipel5ab4c6d2016-03-08 03:36:15 -080072 }
73
philipel1a830c22016-05-13 11:12:00 +020074 // 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;
philipel5ab4c6d2016-03-08 03:36:15 -080078
philipel1a830c22016-05-13 11:12:00 +020079 if (AheadOf(newest_seq_num_, seq_num)) {
philipel5ab4c6d2016-03-08 03:36:15 -080080 // An out of order packet has been received.
philipel1a830c22016-05-13 11:12:00 +020081 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 }
philipel5ab4c6d2016-03-08 03:36:15 -080087 if (!is_retransmitted)
88 UpdateReorderingStatistics(seq_num);
philipel1a830c22016-05-13 11:12:00 +020089 return nacks_sent_for_packet;
philipel5ab4c6d2016-03-08 03:36:15 -080090 }
philipel1a830c22016-05-13 11:12:00 +020091 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;
philipel5ab4c6d2016-03-08 03:36:15 -0800109}
110
Niels Möllerbc010472018-03-23 13:22:29 +0100111int NackModule::OnReceivedPacket(const VCMPacket& packet) {
112 return OnReceivedPacket(
113 packet.seqNum,
114 packet.is_first_packet_in_frame && packet.frameType == kVideoFrameKey);
115}
116
philipel5ab4c6d2016-03-08 03:36:15 -0800117void NackModule::ClearUpTo(uint16_t seq_num) {
118 rtc::CritScope lock(&crit_);
119 nack_list_.erase(nack_list_.begin(), nack_list_.lower_bound(seq_num));
120 keyframe_list_.erase(keyframe_list_.begin(),
121 keyframe_list_.lower_bound(seq_num));
122}
123
124void NackModule::UpdateRtt(int64_t rtt_ms) {
125 rtc::CritScope lock(&crit_);
126 rtt_ms_ = rtt_ms;
127}
128
philipel83f831a2016-03-12 03:30:23 -0800129void NackModule::Clear() {
130 rtc::CritScope lock(&crit_);
131 nack_list_.clear();
132 keyframe_list_.clear();
133}
134
philipel5ab4c6d2016-03-08 03:36:15 -0800135int64_t NackModule::TimeUntilNextProcess() {
philipel5ab4c6d2016-03-08 03:36:15 -0800136 return std::max<int64_t>(next_process_time_ms_ - clock_->TimeInMilliseconds(),
137 0);
138}
139
140void NackModule::Process() {
tommif284b7f2017-02-27 01:59:36 -0800141 if (nack_sender_) {
142 std::vector<uint16_t> nack_batch;
143 {
144 rtc::CritScope lock(&crit_);
145 nack_batch = GetNackBatch(kTimeOnly);
146 }
147
148 if (!nack_batch.empty())
149 nack_sender_->SendNack(nack_batch);
150 }
philipel5ab4c6d2016-03-08 03:36:15 -0800151
152 // Update the next_process_time_ms_ in intervals to achieve
153 // the targeted frequency over time. Also add multiple intervals
154 // in case of a skip in time as to not make uneccessary
155 // calls to Process in order to catch up.
156 int64_t now_ms = clock_->TimeInMilliseconds();
157 if (next_process_time_ms_ == -1) {
158 next_process_time_ms_ = now_ms + kProcessIntervalMs;
159 } else {
160 next_process_time_ms_ = next_process_time_ms_ + kProcessIntervalMs +
161 (now_ms - next_process_time_ms_) /
162 kProcessIntervalMs * kProcessIntervalMs;
163 }
philipel5ab4c6d2016-03-08 03:36:15 -0800164}
165
166bool NackModule::RemovePacketsUntilKeyFrame() {
167 while (!keyframe_list_.empty()) {
168 auto it = nack_list_.lower_bound(*keyframe_list_.begin());
169
170 if (it != nack_list_.begin()) {
171 // We have found a keyframe that actually is newer than at least one
172 // packet in the nack list.
173 RTC_DCHECK(it != nack_list_.end());
174 nack_list_.erase(nack_list_.begin(), it);
175 return true;
176 }
177
178 // If this keyframe is so old it does not remove any packets from the list,
179 // remove it from the list of keyframes and try the next keyframe.
180 keyframe_list_.erase(keyframe_list_.begin());
181 }
182 return false;
183}
184
185void NackModule::AddPacketsToNack(uint16_t seq_num_start,
186 uint16_t seq_num_end) {
187 // Remove old packets.
188 auto it = nack_list_.lower_bound(seq_num_end - kMaxPacketAge);
189 nack_list_.erase(nack_list_.begin(), it);
190
191 // If the nack list is too large, remove packets from the nack list until
192 // the latest first packet of a keyframe. If the list is still too large,
193 // clear it and request a keyframe.
194 uint16_t num_new_nacks = ForwardDiff(seq_num_start, seq_num_end);
195 if (nack_list_.size() + num_new_nacks > kMaxNackPackets) {
196 while (RemovePacketsUntilKeyFrame() &&
197 nack_list_.size() + num_new_nacks > kMaxNackPackets) {
198 }
199
200 if (nack_list_.size() + num_new_nacks > kMaxNackPackets) {
201 nack_list_.clear();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100202 RTC_LOG(LS_WARNING) << "NACK list full, clearing NACK"
203 " list and requesting keyframe.";
philipel5ab4c6d2016-03-08 03:36:15 -0800204 keyframe_request_sender_->RequestKeyFrame();
205 return;
206 }
207 }
208
209 for (uint16_t seq_num = seq_num_start; seq_num != seq_num_end; ++seq_num) {
210 NackInfo nack_info(seq_num, seq_num + WaitNumberOfPackets(0.5));
211 RTC_DCHECK(nack_list_.find(seq_num) == nack_list_.end());
212 nack_list_[seq_num] = nack_info;
213 }
214}
215
216std::vector<uint16_t> NackModule::GetNackBatch(NackFilterOptions options) {
217 bool consider_seq_num = options != kTimeOnly;
218 bool consider_timestamp = options != kSeqNumOnly;
219 int64_t now_ms = clock_->TimeInMilliseconds();
220 std::vector<uint16_t> nack_batch;
221 auto it = nack_list_.begin();
222 while (it != nack_list_.end()) {
223 if (consider_seq_num && it->second.sent_at_time == -1 &&
philipel1a830c22016-05-13 11:12:00 +0200224 AheadOrAt(newest_seq_num_, it->second.send_at_seq_num)) {
philipel5ab4c6d2016-03-08 03:36:15 -0800225 nack_batch.emplace_back(it->second.seq_num);
226 ++it->second.retries;
227 it->second.sent_at_time = now_ms;
228 if (it->second.retries >= kMaxNackRetries) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100229 RTC_LOG(LS_WARNING) << "Sequence number " << it->second.seq_num
230 << " removed from NACK list due to max retries.";
philipel5ab4c6d2016-03-08 03:36:15 -0800231 it = nack_list_.erase(it);
232 } else {
233 ++it;
234 }
235 continue;
236 }
237
238 if (consider_timestamp && it->second.sent_at_time + rtt_ms_ <= now_ms) {
239 nack_batch.emplace_back(it->second.seq_num);
240 ++it->second.retries;
241 it->second.sent_at_time = now_ms;
242 if (it->second.retries >= kMaxNackRetries) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100243 RTC_LOG(LS_WARNING) << "Sequence number " << it->second.seq_num
244 << " removed from NACK list due to max retries.";
philipel5ab4c6d2016-03-08 03:36:15 -0800245 it = nack_list_.erase(it);
246 } else {
247 ++it;
248 }
249 continue;
250 }
251 ++it;
252 }
253 return nack_batch;
254}
255
256void NackModule::UpdateReorderingStatistics(uint16_t seq_num) {
philipel1a830c22016-05-13 11:12:00 +0200257 RTC_DCHECK(AheadOf(newest_seq_num_, seq_num));
258 uint16_t diff = ReverseDiff(newest_seq_num_, seq_num);
philipel5ab4c6d2016-03-08 03:36:15 -0800259 reordering_histogram_.Add(diff);
260}
261
262int NackModule::WaitNumberOfPackets(float probability) const {
263 if (reordering_histogram_.NumValues() == 0)
264 return 0;
265 return reordering_histogram_.InverseCdf(probability);
266}
267
268} // namespace webrtc