Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(318)

Side by Side Diff: webrtc/modules/video_coding/nack_module.cc

Issue 1715673002: Implement the NackModule as part of the new jitter buffer. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Format Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
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
21 namespace webrtc {
22
23 namespace {
24 const int kMaxPacketAge = 10000;
25 const int kMaxNackPackets = 1000;
26 const int kDefaultRttMs = 100;
27 const int kMaxNackRetries = 10;
28 const int kProcessFrequency = 50;
29 const int kProcessIntervalMs = 1000 / kProcessFrequency;
30 const int kMaxReorderedPackets = 128;
31 const int kNumReorderingBuckets = 10;
32 } // namespace
33
34 NackModule::NackInfo::NackInfo()
35 : seq_num(0), send_at_seq_num(0), sent_at_time(-1), retries(0) {}
36
37 NackModule::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
43 NackModule::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
60 void 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
109 void 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
116 void NackModule::UpdateRtt(int64_t rtt_ms) {
117 rtc::CritScope lock(&crit_);
118 rtt_ms_ = rtt_ms;
119 }
120
121 void NackModule::Stop() {
122 rtc::CritScope lock(&crit_);
123 running_ = false;
124 }
125
126 int64_t NackModule::TimeUntilNextProcess() {
127 rtc::CritScope lock(&crit_);
128 return std::max<int64_t>(next_process_time_ms_ - clock_->TimeInMilliseconds(),
129 0);
130 }
131
132 void NackModule::Process() {
133 rtc::CritScope lock(&crit_);
134 if (!running_)
135 return;
136
137 // Update the next_process_time_ms_ in intervals to achieve
138 // the targeted frequency over time. Also add multiple intervals
139 // in case of a skip in time as to not make uneccessary
140 // calls to Process in order to catch up.
141 int64_t now_ms = clock_->TimeInMilliseconds();
142 if (next_process_time_ms_ == -1) {
143 next_process_time_ms_ = now_ms + kProcessIntervalMs;
144 } else {
145 next_process_time_ms_ = next_process_time_ms_ + kProcessIntervalMs +
146 (now_ms - next_process_time_ms_) /
147 kProcessIntervalMs * kProcessIntervalMs;
148 }
149
150 std::vector<uint16_t> nack_batch = GetNackBatch(kTimeOnly);
151 if (!nack_batch.empty() && nack_sender_ != nullptr)
152 nack_sender_->SendNack(nack_batch);
153 }
154
155 bool NackModule::RemovePacketsUntilKeyFrame() {
156 while (!keyframe_list_.empty()) {
157 auto it = nack_list_.lower_bound(*keyframe_list_.begin());
158
159 if (it != nack_list_.begin()) {
160 // We have found a keyframe that actually is newer than at least one
161 // packet in the nack list.
162 RTC_DCHECK(it != nack_list_.end());
163 nack_list_.erase(nack_list_.begin(), it);
164 return true;
165 }
166
167 // If this keyframe is so old it does not remove any packets from the list,
168 // remove it from the list of keyframes and try the next keyframe.
169 keyframe_list_.erase(keyframe_list_.begin());
170 }
171 return false;
172 }
173
174 void NackModule::AddPacketsToNack(uint16_t seq_num_start,
175 uint16_t seq_num_end) {
176 // Remove old packets.
177 auto it = nack_list_.lower_bound(seq_num_end - kMaxPacketAge);
178 nack_list_.erase(nack_list_.begin(), it);
179
180 // If the nack list is too large, remove packets from the nack list until
181 // the latest first packet of a keyframe. If the list is still too large,
182 // clear it and request a keyframe.
183 uint16_t num_new_nacks = ForwardDiff(seq_num_start, seq_num_end);
184 if (nack_list_.size() + num_new_nacks > kMaxNackPackets) {
185 while (RemovePacketsUntilKeyFrame() &&
186 nack_list_.size() + num_new_nacks > kMaxNackPackets) {
187 }
188
189 if (nack_list_.size() + num_new_nacks > kMaxNackPackets) {
190 nack_list_.clear();
191 LOG(LS_WARNING) << "NACK list full, clearing NACK"
192 " list and requesting keyframe.";
193 keyframe_request_sender_->RequestKeyFrame();
194 return;
195 }
196 }
197
198 for (uint16_t seq_num = seq_num_start; seq_num != seq_num_end; ++seq_num) {
199 NackInfo nack_info(seq_num, seq_num + WaitNumberOfPackets(0.5));
200 RTC_DCHECK(nack_list_.find(seq_num) == nack_list_.end());
201 nack_list_[seq_num] = nack_info;
202 }
203 }
204
205 std::vector<uint16_t> NackModule::GetNackBatch(NackFilterOptions options) {
206 bool consider_seq_num = options != kTimeOnly;
207 bool consider_timestamp = options != kSeqNumOnly;
208 int64_t now_ms = clock_->TimeInMilliseconds();
209 std::vector<uint16_t> nack_batch;
210 auto it = nack_list_.begin();
211 while (it != nack_list_.end()) {
212 if (consider_seq_num && it->second.sent_at_time == -1 &&
213 AheadOrAt(last_seq_num_, it->second.send_at_seq_num)) {
214 nack_batch.emplace_back(it->second.seq_num);
215 ++it->second.retries;
216 it->second.sent_at_time = now_ms;
217 if (it->second.retries >= kMaxNackRetries) {
218 LOG(LS_WARNING) << "Sequence number " << it->second.seq_num
219 << " removed from NACK list due to max retries.";
220 it = nack_list_.erase(it);
221 } else {
222 ++it;
223 }
224 continue;
225 }
226
227 if (consider_timestamp && it->second.sent_at_time + rtt_ms_ <= now_ms) {
228 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 ++it;
241 }
242 return nack_batch;
243 }
244
245 void NackModule::UpdateReorderingStatistics(uint16_t seq_num) {
246 RTC_DCHECK(AheadOf(last_seq_num_, seq_num));
247 uint16_t diff = ReverseDiff(last_seq_num_, seq_num);
248 reordering_histogram_.Add(diff);
249 }
250
251 int NackModule::WaitNumberOfPackets(float probability) const {
252 if (reordering_histogram_.NumValues() == 0)
253 return 0;
254 return reordering_histogram_.InverseCdf(probability);
255 }
256
257 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698