blob: d74f08952617da38ad7a7d75b7f87067021816a5 [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
14#include "webrtc/modules/video_coding/nack_module.h"
15
16#include "webrtc/base/checks.h"
17#include "webrtc/base/logging.h"
philipel5ab4c6d2016-03-08 03:36:15 -080018#include "webrtc/modules/utility/include/process_thread.h"
19
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),
49 running_(true),
50 initialized_(false),
51 rtt_ms_(kDefaultRttMs),
philipel1a830c22016-05-13 11:12:00 +020052 newest_seq_num_(0),
philipel5ab4c6d2016-03-08 03:36:15 -080053 next_process_time_ms_(-1) {
54 RTC_DCHECK(clock_);
55 RTC_DCHECK(nack_sender_);
56 RTC_DCHECK(keyframe_request_sender_);
57}
58
philipel1a830c22016-05-13 11:12:00 +020059int NackModule::OnReceivedPacket(const VCMPacket& packet) {
philipel5ab4c6d2016-03-08 03:36:15 -080060 rtc::CritScope lock(&crit_);
61 if (!running_)
philipel1a830c22016-05-13 11:12:00 +020062 return -1;
philipel5ab4c6d2016-03-08 03:36:15 -080063 uint16_t seq_num = packet.seqNum;
64 // TODO(philipel): When the packet includes information whether it is
65 // retransmitted or not, use that value instead. For
66 // now set it to true, which will cause the reordering
67 // statistics to never be updated.
68 bool is_retransmitted = true;
johanefde9082016-12-19 08:32:24 -080069 bool is_keyframe =
70 packet.is_first_packet_in_frame && packet.frameType == kVideoFrameKey;
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 AddPacketsToNack(newest_seq_num_ + 1, seq_num);
98 newest_seq_num_ = seq_num;
99
100 // Keep track of new keyframes.
101 if (is_keyframe)
102 keyframe_list_.insert(seq_num);
103
104 // And remove old ones so we don't accumulate keyframes.
105 auto it = keyframe_list_.lower_bound(seq_num - kMaxPacketAge);
106 if (it != keyframe_list_.begin())
107 keyframe_list_.erase(keyframe_list_.begin(), it);
108
109 // Are there any nacks that are waiting for this seq_num.
110 std::vector<uint16_t> nack_batch = GetNackBatch(kSeqNumOnly);
111 if (!nack_batch.empty())
112 nack_sender_->SendNack(nack_batch);
113
114 return 0;
philipel5ab4c6d2016-03-08 03:36:15 -0800115}
116
117void 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 -0800135void NackModule::Stop() {
136 rtc::CritScope lock(&crit_);
137 running_ = false;
138}
139
140int64_t NackModule::TimeUntilNextProcess() {
141 rtc::CritScope lock(&crit_);
142 return std::max<int64_t>(next_process_time_ms_ - clock_->TimeInMilliseconds(),
143 0);
144}
145
146void NackModule::Process() {
147 rtc::CritScope lock(&crit_);
148 if (!running_)
149 return;
150
151 // Update the next_process_time_ms_ in intervals to achieve
152 // the targeted frequency over time. Also add multiple intervals
153 // in case of a skip in time as to not make uneccessary
154 // calls to Process in order to catch up.
155 int64_t now_ms = clock_->TimeInMilliseconds();
156 if (next_process_time_ms_ == -1) {
157 next_process_time_ms_ = now_ms + kProcessIntervalMs;
158 } else {
159 next_process_time_ms_ = next_process_time_ms_ + kProcessIntervalMs +
160 (now_ms - next_process_time_ms_) /
161 kProcessIntervalMs * kProcessIntervalMs;
162 }
163
164 std::vector<uint16_t> nack_batch = GetNackBatch(kTimeOnly);
165 if (!nack_batch.empty() && nack_sender_ != nullptr)
166 nack_sender_->SendNack(nack_batch);
167}
168
169bool NackModule::RemovePacketsUntilKeyFrame() {
170 while (!keyframe_list_.empty()) {
171 auto it = nack_list_.lower_bound(*keyframe_list_.begin());
172
173 if (it != nack_list_.begin()) {
174 // We have found a keyframe that actually is newer than at least one
175 // packet in the nack list.
176 RTC_DCHECK(it != nack_list_.end());
177 nack_list_.erase(nack_list_.begin(), it);
178 return true;
179 }
180
181 // If this keyframe is so old it does not remove any packets from the list,
182 // remove it from the list of keyframes and try the next keyframe.
183 keyframe_list_.erase(keyframe_list_.begin());
184 }
185 return false;
186}
187
188void NackModule::AddPacketsToNack(uint16_t seq_num_start,
189 uint16_t seq_num_end) {
190 // Remove old packets.
191 auto it = nack_list_.lower_bound(seq_num_end - kMaxPacketAge);
192 nack_list_.erase(nack_list_.begin(), it);
193
194 // If the nack list is too large, remove packets from the nack list until
195 // the latest first packet of a keyframe. If the list is still too large,
196 // clear it and request a keyframe.
197 uint16_t num_new_nacks = ForwardDiff(seq_num_start, seq_num_end);
198 if (nack_list_.size() + num_new_nacks > kMaxNackPackets) {
199 while (RemovePacketsUntilKeyFrame() &&
200 nack_list_.size() + num_new_nacks > kMaxNackPackets) {
201 }
202
203 if (nack_list_.size() + num_new_nacks > kMaxNackPackets) {
204 nack_list_.clear();
205 LOG(LS_WARNING) << "NACK list full, clearing NACK"
206 " list and requesting keyframe.";
207 keyframe_request_sender_->RequestKeyFrame();
208 return;
209 }
210 }
211
212 for (uint16_t seq_num = seq_num_start; seq_num != seq_num_end; ++seq_num) {
213 NackInfo nack_info(seq_num, seq_num + WaitNumberOfPackets(0.5));
214 RTC_DCHECK(nack_list_.find(seq_num) == nack_list_.end());
215 nack_list_[seq_num] = nack_info;
216 }
217}
218
219std::vector<uint16_t> NackModule::GetNackBatch(NackFilterOptions options) {
220 bool consider_seq_num = options != kTimeOnly;
221 bool consider_timestamp = options != kSeqNumOnly;
222 int64_t now_ms = clock_->TimeInMilliseconds();
223 std::vector<uint16_t> nack_batch;
224 auto it = nack_list_.begin();
225 while (it != nack_list_.end()) {
226 if (consider_seq_num && it->second.sent_at_time == -1 &&
philipel1a830c22016-05-13 11:12:00 +0200227 AheadOrAt(newest_seq_num_, it->second.send_at_seq_num)) {
philipel5ab4c6d2016-03-08 03:36:15 -0800228 nack_batch.emplace_back(it->second.seq_num);
229 ++it->second.retries;
230 it->second.sent_at_time = now_ms;
231 if (it->second.retries >= kMaxNackRetries) {
232 LOG(LS_WARNING) << "Sequence number " << it->second.seq_num
233 << " removed from NACK list due to max retries.";
234 it = nack_list_.erase(it);
235 } else {
236 ++it;
237 }
238 continue;
239 }
240
241 if (consider_timestamp && it->second.sent_at_time + rtt_ms_ <= now_ms) {
242 nack_batch.emplace_back(it->second.seq_num);
243 ++it->second.retries;
244 it->second.sent_at_time = now_ms;
245 if (it->second.retries >= kMaxNackRetries) {
246 LOG(LS_WARNING) << "Sequence number " << it->second.seq_num
247 << " removed from NACK list due to max retries.";
248 it = nack_list_.erase(it);
249 } else {
250 ++it;
251 }
252 continue;
253 }
254 ++it;
255 }
256 return nack_batch;
257}
258
259void NackModule::UpdateReorderingStatistics(uint16_t seq_num) {
philipel1a830c22016-05-13 11:12:00 +0200260 RTC_DCHECK(AheadOf(newest_seq_num_, seq_num));
261 uint16_t diff = ReverseDiff(newest_seq_num_, seq_num);
philipel5ab4c6d2016-03-08 03:36:15 -0800262 reordering_histogram_.Add(diff);
263}
264
265int NackModule::WaitNumberOfPackets(float probability) const {
266 if (reordering_histogram_.NumValues() == 0)
267 return 0;
268 return reordering_histogram_.InverseCdf(probability);
269}
270
271} // namespace webrtc