blob: 80f84968eb1c50c13567a1ebabce3b118d4b9644 [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"
18#include "webrtc/base/mod_ops.h"
19#include "webrtc/modules/utility/include/process_thread.h"
20
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;
32} // namespace
33
34NackModule::NackInfo::NackInfo()
35 : seq_num(0), send_at_seq_num(0), sent_at_time(-1), retries(0) {}
36
37NackModule::NackInfo::NackInfo(uint16_t seq_num, uint16_t send_at_seq_num)
38 : seq_num(seq_num),
39 send_at_seq_num(send_at_seq_num),
40 sent_at_time(-1),
41 retries(0) {}
42
43NackModule::NackModule(Clock* clock,
44 NackSender* nack_sender,
45 KeyFrameRequestSender* keyframe_request_sender)
46 : clock_(clock),
47 nack_sender_(nack_sender),
48 keyframe_request_sender_(keyframe_request_sender),
49 reordering_histogram_(kNumReorderingBuckets, kMaxReorderedPackets),
50 running_(true),
51 initialized_(false),
52 rtt_ms_(kDefaultRttMs),
53 last_seq_num_(0),
54 next_process_time_ms_(-1) {
55 RTC_DCHECK(clock_);
56 RTC_DCHECK(nack_sender_);
57 RTC_DCHECK(keyframe_request_sender_);
58}
59
60void NackModule::OnReceivedPacket(const VCMPacket& packet) {
61 rtc::CritScope lock(&crit_);
62 if (!running_)
63 return;
64 uint16_t seq_num = packet.seqNum;
65 // TODO(philipel): When the packet includes information whether it is
66 // retransmitted or not, use that value instead. For
67 // now set it to true, which will cause the reordering
68 // statistics to never be updated.
69 bool is_retransmitted = true;
70 bool is_keyframe = packet.isFirstPacket && packet.frameType == kVideoFrameKey;
71
72 if (!initialized_) {
73 last_seq_num_ = seq_num;
74 if (is_keyframe)
75 keyframe_list_.insert(seq_num);
76 initialized_ = true;
77 return;
78 }
79
80 if (seq_num == last_seq_num_)
81 return;
82
83 if (AheadOf(last_seq_num_, seq_num)) {
84 // An out of order packet has been received.
85 nack_list_.erase(seq_num);
86 if (!is_retransmitted)
87 UpdateReorderingStatistics(seq_num);
88 return;
89 } else {
90 AddPacketsToNack(last_seq_num_ + 1, seq_num);
91 last_seq_num_ = seq_num;
92
93 // Keep track of new keyframes.
94 if (is_keyframe)
95 keyframe_list_.insert(seq_num);
96
97 // And remove old ones so we don't accumulate keyframes.
98 auto it = keyframe_list_.lower_bound(seq_num - kMaxPacketAge);
99 if (it != keyframe_list_.begin())
100 keyframe_list_.erase(keyframe_list_.begin(), it);
101
102 // Are there any nacks that are waiting for this seq_num.
103 std::vector<uint16_t> nack_batch = GetNackBatch(kSeqNumOnly);
104 if (!nack_batch.empty())
105 nack_sender_->SendNack(nack_batch);
106 }
107}
108
109void NackModule::ClearUpTo(uint16_t seq_num) {
110 rtc::CritScope lock(&crit_);
111 nack_list_.erase(nack_list_.begin(), nack_list_.lower_bound(seq_num));
112 keyframe_list_.erase(keyframe_list_.begin(),
113 keyframe_list_.lower_bound(seq_num));
114}
115
116void NackModule::UpdateRtt(int64_t rtt_ms) {
117 rtc::CritScope lock(&crit_);
118 rtt_ms_ = rtt_ms;
119}
120
philipel83f831a2016-03-12 03:30:23 -0800121void NackModule::Clear() {
122 rtc::CritScope lock(&crit_);
123 nack_list_.clear();
124 keyframe_list_.clear();
125}
126
philipel5ab4c6d2016-03-08 03:36:15 -0800127void NackModule::Stop() {
128 rtc::CritScope lock(&crit_);
129 running_ = false;
130}
131
132int64_t NackModule::TimeUntilNextProcess() {
133 rtc::CritScope lock(&crit_);
134 return std::max<int64_t>(next_process_time_ms_ - clock_->TimeInMilliseconds(),
135 0);
136}
137
138void NackModule::Process() {
139 rtc::CritScope lock(&crit_);
140 if (!running_)
141 return;
142
143 // Update the next_process_time_ms_ in intervals to achieve
144 // the targeted frequency over time. Also add multiple intervals
145 // in case of a skip in time as to not make uneccessary
146 // calls to Process in order to catch up.
147 int64_t now_ms = clock_->TimeInMilliseconds();
148 if (next_process_time_ms_ == -1) {
149 next_process_time_ms_ = now_ms + kProcessIntervalMs;
150 } else {
151 next_process_time_ms_ = next_process_time_ms_ + kProcessIntervalMs +
152 (now_ms - next_process_time_ms_) /
153 kProcessIntervalMs * kProcessIntervalMs;
154 }
155
156 std::vector<uint16_t> nack_batch = GetNackBatch(kTimeOnly);
157 if (!nack_batch.empty() && nack_sender_ != nullptr)
158 nack_sender_->SendNack(nack_batch);
159}
160
161bool NackModule::RemovePacketsUntilKeyFrame() {
162 while (!keyframe_list_.empty()) {
163 auto it = nack_list_.lower_bound(*keyframe_list_.begin());
164
165 if (it != nack_list_.begin()) {
166 // We have found a keyframe that actually is newer than at least one
167 // packet in the nack list.
168 RTC_DCHECK(it != nack_list_.end());
169 nack_list_.erase(nack_list_.begin(), it);
170 return true;
171 }
172
173 // If this keyframe is so old it does not remove any packets from the list,
174 // remove it from the list of keyframes and try the next keyframe.
175 keyframe_list_.erase(keyframe_list_.begin());
176 }
177 return false;
178}
179
180void NackModule::AddPacketsToNack(uint16_t seq_num_start,
181 uint16_t seq_num_end) {
182 // Remove old packets.
183 auto it = nack_list_.lower_bound(seq_num_end - kMaxPacketAge);
184 nack_list_.erase(nack_list_.begin(), it);
185
186 // If the nack list is too large, remove packets from the nack list until
187 // the latest first packet of a keyframe. If the list is still too large,
188 // clear it and request a keyframe.
189 uint16_t num_new_nacks = ForwardDiff(seq_num_start, seq_num_end);
190 if (nack_list_.size() + num_new_nacks > kMaxNackPackets) {
191 while (RemovePacketsUntilKeyFrame() &&
192 nack_list_.size() + num_new_nacks > kMaxNackPackets) {
193 }
194
195 if (nack_list_.size() + num_new_nacks > kMaxNackPackets) {
196 nack_list_.clear();
197 LOG(LS_WARNING) << "NACK list full, clearing NACK"
198 " list and requesting keyframe.";
199 keyframe_request_sender_->RequestKeyFrame();
200 return;
201 }
202 }
203
204 for (uint16_t seq_num = seq_num_start; seq_num != seq_num_end; ++seq_num) {
205 NackInfo nack_info(seq_num, seq_num + WaitNumberOfPackets(0.5));
206 RTC_DCHECK(nack_list_.find(seq_num) == nack_list_.end());
207 nack_list_[seq_num] = nack_info;
208 }
209}
210
211std::vector<uint16_t> NackModule::GetNackBatch(NackFilterOptions options) {
212 bool consider_seq_num = options != kTimeOnly;
213 bool consider_timestamp = options != kSeqNumOnly;
214 int64_t now_ms = clock_->TimeInMilliseconds();
215 std::vector<uint16_t> nack_batch;
216 auto it = nack_list_.begin();
217 while (it != nack_list_.end()) {
218 if (consider_seq_num && it->second.sent_at_time == -1 &&
219 AheadOrAt(last_seq_num_, it->second.send_at_seq_num)) {
220 nack_batch.emplace_back(it->second.seq_num);
221 ++it->second.retries;
222 it->second.sent_at_time = now_ms;
223 if (it->second.retries >= kMaxNackRetries) {
224 LOG(LS_WARNING) << "Sequence number " << it->second.seq_num
225 << " removed from NACK list due to max retries.";
226 it = nack_list_.erase(it);
227 } else {
228 ++it;
229 }
230 continue;
231 }
232
233 if (consider_timestamp && it->second.sent_at_time + rtt_ms_ <= now_ms) {
234 nack_batch.emplace_back(it->second.seq_num);
235 ++it->second.retries;
236 it->second.sent_at_time = now_ms;
237 if (it->second.retries >= kMaxNackRetries) {
238 LOG(LS_WARNING) << "Sequence number " << it->second.seq_num
239 << " removed from NACK list due to max retries.";
240 it = nack_list_.erase(it);
241 } else {
242 ++it;
243 }
244 continue;
245 }
246 ++it;
247 }
248 return nack_batch;
249}
250
251void NackModule::UpdateReorderingStatistics(uint16_t seq_num) {
252 RTC_DCHECK(AheadOf(last_seq_num_, seq_num));
253 uint16_t diff = ReverseDiff(last_seq_num_, seq_num);
254 reordering_histogram_.Add(diff);
255}
256
257int NackModule::WaitNumberOfPackets(float probability) const {
258 if (reordering_histogram_.NumValues() == 0)
259 return 0;
260 return reordering_histogram_.InverseCdf(probability);
261}
262
263} // namespace webrtc