blob: 43244321ea0ed5d883a5aadf9e7fa8ed070995b8 [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;
danilchap0ad21112016-12-19 09:36:33 -080069 bool is_keyframe = packet.isFirstPacket && packet.frameType == kVideoFrameKey;
philipel5ab4c6d2016-03-08 03:36:15 -080070
71 if (!initialized_) {
philipel1a830c22016-05-13 11:12:00 +020072 newest_seq_num_ = seq_num;
philipel5ab4c6d2016-03-08 03:36:15 -080073 if (is_keyframe)
74 keyframe_list_.insert(seq_num);
75 initialized_ = true;
philipel1a830c22016-05-13 11:12:00 +020076 return 0;
philipel5ab4c6d2016-03-08 03:36:15 -080077 }
78
philipel1a830c22016-05-13 11:12:00 +020079 // Since the |newest_seq_num_| is a packet we have actually received we know
80 // that packet has never been Nacked.
81 if (seq_num == newest_seq_num_)
82 return 0;
philipel5ab4c6d2016-03-08 03:36:15 -080083
philipel1a830c22016-05-13 11:12:00 +020084 if (AheadOf(newest_seq_num_, seq_num)) {
philipel5ab4c6d2016-03-08 03:36:15 -080085 // An out of order packet has been received.
philipel1a830c22016-05-13 11:12:00 +020086 auto nack_list_it = nack_list_.find(seq_num);
87 int nacks_sent_for_packet = 0;
88 if (nack_list_it != nack_list_.end()) {
89 nacks_sent_for_packet = nack_list_it->second.retries;
90 nack_list_.erase(nack_list_it);
91 }
philipel5ab4c6d2016-03-08 03:36:15 -080092 if (!is_retransmitted)
93 UpdateReorderingStatistics(seq_num);
philipel1a830c22016-05-13 11:12:00 +020094 return nacks_sent_for_packet;
philipel5ab4c6d2016-03-08 03:36:15 -080095 }
philipel1a830c22016-05-13 11:12:00 +020096 AddPacketsToNack(newest_seq_num_ + 1, seq_num);
97 newest_seq_num_ = seq_num;
98
99 // Keep track of new keyframes.
100 if (is_keyframe)
101 keyframe_list_.insert(seq_num);
102
103 // And remove old ones so we don't accumulate keyframes.
104 auto it = keyframe_list_.lower_bound(seq_num - kMaxPacketAge);
105 if (it != keyframe_list_.begin())
106 keyframe_list_.erase(keyframe_list_.begin(), it);
107
108 // Are there any nacks that are waiting for this seq_num.
109 std::vector<uint16_t> nack_batch = GetNackBatch(kSeqNumOnly);
110 if (!nack_batch.empty())
111 nack_sender_->SendNack(nack_batch);
112
113 return 0;
philipel5ab4c6d2016-03-08 03:36:15 -0800114}
115
116void NackModule::ClearUpTo(uint16_t seq_num) {
117 rtc::CritScope lock(&crit_);
118 nack_list_.erase(nack_list_.begin(), nack_list_.lower_bound(seq_num));
119 keyframe_list_.erase(keyframe_list_.begin(),
120 keyframe_list_.lower_bound(seq_num));
121}
122
123void NackModule::UpdateRtt(int64_t rtt_ms) {
124 rtc::CritScope lock(&crit_);
125 rtt_ms_ = rtt_ms;
126}
127
philipel83f831a2016-03-12 03:30:23 -0800128void NackModule::Clear() {
129 rtc::CritScope lock(&crit_);
130 nack_list_.clear();
131 keyframe_list_.clear();
132}
133
philipel5ab4c6d2016-03-08 03:36:15 -0800134void NackModule::Stop() {
135 rtc::CritScope lock(&crit_);
136 running_ = false;
137}
138
139int64_t NackModule::TimeUntilNextProcess() {
140 rtc::CritScope lock(&crit_);
141 return std::max<int64_t>(next_process_time_ms_ - clock_->TimeInMilliseconds(),
142 0);
143}
144
145void NackModule::Process() {
146 rtc::CritScope lock(&crit_);
147 if (!running_)
148 return;
149
150 // Update the next_process_time_ms_ in intervals to achieve
151 // the targeted frequency over time. Also add multiple intervals
152 // in case of a skip in time as to not make uneccessary
153 // calls to Process in order to catch up.
154 int64_t now_ms = clock_->TimeInMilliseconds();
155 if (next_process_time_ms_ == -1) {
156 next_process_time_ms_ = now_ms + kProcessIntervalMs;
157 } else {
158 next_process_time_ms_ = next_process_time_ms_ + kProcessIntervalMs +
159 (now_ms - next_process_time_ms_) /
160 kProcessIntervalMs * kProcessIntervalMs;
161 }
162
163 std::vector<uint16_t> nack_batch = GetNackBatch(kTimeOnly);
164 if (!nack_batch.empty() && nack_sender_ != nullptr)
165 nack_sender_->SendNack(nack_batch);
166}
167
168bool NackModule::RemovePacketsUntilKeyFrame() {
169 while (!keyframe_list_.empty()) {
170 auto it = nack_list_.lower_bound(*keyframe_list_.begin());
171
172 if (it != nack_list_.begin()) {
173 // We have found a keyframe that actually is newer than at least one
174 // packet in the nack list.
175 RTC_DCHECK(it != nack_list_.end());
176 nack_list_.erase(nack_list_.begin(), it);
177 return true;
178 }
179
180 // If this keyframe is so old it does not remove any packets from the list,
181 // remove it from the list of keyframes and try the next keyframe.
182 keyframe_list_.erase(keyframe_list_.begin());
183 }
184 return false;
185}
186
187void NackModule::AddPacketsToNack(uint16_t seq_num_start,
188 uint16_t seq_num_end) {
189 // Remove old packets.
190 auto it = nack_list_.lower_bound(seq_num_end - kMaxPacketAge);
191 nack_list_.erase(nack_list_.begin(), it);
192
193 // If the nack list is too large, remove packets from the nack list until
194 // the latest first packet of a keyframe. If the list is still too large,
195 // clear it and request a keyframe.
196 uint16_t num_new_nacks = ForwardDiff(seq_num_start, seq_num_end);
197 if (nack_list_.size() + num_new_nacks > kMaxNackPackets) {
198 while (RemovePacketsUntilKeyFrame() &&
199 nack_list_.size() + num_new_nacks > kMaxNackPackets) {
200 }
201
202 if (nack_list_.size() + num_new_nacks > kMaxNackPackets) {
203 nack_list_.clear();
204 LOG(LS_WARNING) << "NACK list full, clearing NACK"
205 " list and requesting keyframe.";
206 keyframe_request_sender_->RequestKeyFrame();
207 return;
208 }
209 }
210
211 for (uint16_t seq_num = seq_num_start; seq_num != seq_num_end; ++seq_num) {
212 NackInfo nack_info(seq_num, seq_num + WaitNumberOfPackets(0.5));
213 RTC_DCHECK(nack_list_.find(seq_num) == nack_list_.end());
214 nack_list_[seq_num] = nack_info;
215 }
216}
217
218std::vector<uint16_t> NackModule::GetNackBatch(NackFilterOptions options) {
219 bool consider_seq_num = options != kTimeOnly;
220 bool consider_timestamp = options != kSeqNumOnly;
221 int64_t now_ms = clock_->TimeInMilliseconds();
222 std::vector<uint16_t> nack_batch;
223 auto it = nack_list_.begin();
224 while (it != nack_list_.end()) {
225 if (consider_seq_num && it->second.sent_at_time == -1 &&
philipel1a830c22016-05-13 11:12:00 +0200226 AheadOrAt(newest_seq_num_, it->second.send_at_seq_num)) {
philipel5ab4c6d2016-03-08 03:36:15 -0800227 nack_batch.emplace_back(it->second.seq_num);
228 ++it->second.retries;
229 it->second.sent_at_time = now_ms;
230 if (it->second.retries >= kMaxNackRetries) {
231 LOG(LS_WARNING) << "Sequence number " << it->second.seq_num
232 << " removed from NACK list due to max retries.";
233 it = nack_list_.erase(it);
234 } else {
235 ++it;
236 }
237 continue;
238 }
239
240 if (consider_timestamp && it->second.sent_at_time + rtt_ms_ <= now_ms) {
241 nack_batch.emplace_back(it->second.seq_num);
242 ++it->second.retries;
243 it->second.sent_at_time = now_ms;
244 if (it->second.retries >= kMaxNackRetries) {
245 LOG(LS_WARNING) << "Sequence number " << it->second.seq_num
246 << " removed from NACK list due to max retries.";
247 it = nack_list_.erase(it);
248 } else {
249 ++it;
250 }
251 continue;
252 }
253 ++it;
254 }
255 return nack_batch;
256}
257
258void NackModule::UpdateReorderingStatistics(uint16_t seq_num) {
philipel1a830c22016-05-13 11:12:00 +0200259 RTC_DCHECK(AheadOf(newest_seq_num_, seq_num));
260 uint16_t diff = ReverseDiff(newest_seq_num_, seq_num);
philipel5ab4c6d2016-03-08 03:36:15 -0800261 reordering_histogram_.Add(diff);
262}
263
264int NackModule::WaitNumberOfPackets(float probability) const {
265 if (reordering_histogram_.NumValues() == 0)
266 return 0;
267 return reordering_histogram_.InverseCdf(probability);
268}
269
270} // namespace webrtc