OLD | NEW |
---|---|
(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/mod_ops.h" | |
18 #include "webrtc/modules/utility/include/process_thread.h" | |
19 | |
20 namespace webrtc { | |
21 | |
22 namespace { | |
23 const int kMaxPacketAge = 10000; | |
24 const int kMaxNackPackets = 1000; | |
25 const int kDefaultRttMs = 100; | |
26 const int kMaxNackRetries = 10; | |
27 const int kProcessFrequency = 50; | |
28 const int kProcessIntervalMs = 1000 / kProcessFrequency; | |
29 const int kMaxReorderedPackets = 128; | |
30 const int kNumReorderingBuckets = 10; | |
31 } // namespace | |
32 | |
33 NackModule::NackInfo::NackInfo() | |
34 : seq_num(0), send_at_seq_num(0), sent_at_time(-1), retries(0) {} | |
35 | |
36 NackModule::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 | |
42 NackModule::NackModule(Clock* clock, | |
43 VCMNackSender* nack_sender, | |
44 VCMKeyFrameRequestSender* keyframe_request_sender) | |
45 : clock_(clock), | |
46 nack_sender_(nack_sender), | |
47 keyframe_request_sender_(keyframe_request_sender), | |
48 reordering_distribution_(kNumReorderingBuckets, kMaxReorderedPackets), | |
49 running_(true), | |
50 initialized_(false), | |
51 rtt_ms_(kDefaultRttMs), | |
52 last_seq_num_(0), | |
53 next_process_time_ms_(-1) { | |
54 RTC_DCHECK(clock_); | |
55 RTC_DCHECK(nack_sender_); | |
56 RTC_DCHECK(keyframe_request_sender_); | |
57 } | |
58 | |
59 void NackModule::OnReceivedPacket(const VCMPacket& packet) { | |
60 rtc::CritScope lock(&crit_); | |
61 if (!running_) | |
62 return; | |
63 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; | |
69 bool is_keyframe = packet.isFirstPacket && packet.frameType == kVideoFrameKey; | |
70 | |
71 if (!initialized_) { | |
72 last_seq_num_ = seq_num; | |
73 if (is_keyframe) keyframe_list_.emplace(seq_num); | |
stefan-webrtc
2016/03/01 08:52:10
Is there a reason for doing emplace rather than in
philipel
2016/03/01 10:27:06
Good habit since emplace does the same thing as in
stefan-webrtc
2016/03/01 12:16:34
Acknowledged.
| |
74 initialized_ = true; | |
75 return; | |
76 } | |
77 | |
78 if (seq_num == last_seq_num_) | |
79 return; | |
80 | |
81 if (AheadOf(last_seq_num_, seq_num)) { | |
82 // An out of order packet has been received. | |
83 RemovePacketFromNack(seq_num); | |
84 if (!is_retransmitted) | |
85 UpdateReorderingStatistics(seq_num); | |
86 return; | |
87 } else { | |
88 AddPacketsToNack(last_seq_num_ + 1, seq_num); | |
89 last_seq_num_ = seq_num; | |
90 | |
91 // Keep track of new keyframes. | |
92 if (is_keyframe) | |
93 keyframe_list_.emplace(seq_num); | |
94 | |
95 // And remove old ones so we don't accumulate keyframes. | |
96 auto it = keyframe_list_.lower_bound(seq_num - kMaxPacketAge); | |
97 if (it != keyframe_list_.begin()) | |
98 keyframe_list_.erase(keyframe_list_.begin(), it); | |
99 | |
100 // Are there any nacks that are waiting for this seq_num. | |
101 std::vector<uint16_t> nack_batch = GetNackBatch(kSeqNumOnly); | |
102 if (!nack_batch.empty()) | |
103 nack_sender_->SendNack(nack_batch.data(), nack_batch.size()); | |
104 } | |
105 } | |
106 | |
107 void NackModule::ClearUpTo(uint16_t seq_num) { | |
108 rtc::CritScope lock(&crit_); | |
109 nack_list_.erase(nack_list_.begin(), | |
110 nack_list_.lower_bound(seq_num)); | |
111 keyframe_list_.erase(keyframe_list_.begin(), | |
112 keyframe_list_.lower_bound(seq_num)); | |
113 } | |
114 | |
115 void NackModule::UpdateRtt(int64_t rtt_ms) { | |
116 rtc::CritScope lock(&crit_); | |
117 rtt_ms_ = rtt_ms; | |
118 } | |
119 | |
120 void NackModule::Stop() { | |
121 rtc::CritScope lock(&crit_); | |
122 running_ = false; | |
123 } | |
124 | |
125 int64_t NackModule::TimeUntilNextProcess() { | |
126 rtc::CritScope lock(&crit_); | |
127 return std::max(next_process_time_ms_ - clock_->TimeInMilliseconds(), 0l); | |
128 } | |
129 | |
130 int32_t NackModule::Process() { | |
131 rtc::CritScope lock(&crit_); | |
132 if (!running_) | |
133 return 0; | |
134 | |
135 // Update the next_process_time_ms_ in intervals to achieve | |
136 // the targeted frequency over time. Also add multiple intervals | |
137 // in case of a skip in time as to not make uneccessary | |
138 // calls to Process in order to catch up. | |
139 int64_t now_ms = clock_->TimeInMilliseconds(); | |
140 if (next_process_time_ms_ == -1) { | |
141 next_process_time_ms_ = now_ms + kProcessIntervalMs; | |
142 } else { | |
143 next_process_time_ms_ = next_process_time_ms_ + | |
144 kProcessIntervalMs + | |
145 (now_ms - next_process_time_ms_) / | |
146 kProcessIntervalMs * kProcessIntervalMs; | |
147 } | |
148 | |
149 std::vector<uint16_t> nack_batch = GetNackBatch(kTimeOnly); | |
150 if (!nack_batch.empty() && nack_sender_ != nullptr) | |
151 nack_sender_->SendNack(nack_batch.data(), nack_batch.size()); | |
152 | |
153 return 0; | |
154 } | |
155 | |
156 bool NackModule::RemovePacketsUntilKeyframe() { | |
157 while (!keyframe_list_.empty()) { | |
158 auto it = nack_list_.lower_bound(*keyframe_list_.begin()); | |
159 | |
160 // If this keyframe is so old it does not remove any | |
161 // packets from the list, remove if from the list of | |
stefan-webrtc
2016/03/01 08:52:10
"remove *it* from the list"
philipel
2016/03/01 10:27:06
Done.
| |
162 // keyframes and try the next keyframe. | |
163 if (it == nack_list_.begin()) { | |
164 keyframe_list_.erase(keyframe_list_.begin()); | |
165 continue; | |
166 } | |
167 | |
168 // We have found a keyframe that actually is newer than | |
169 // atleast one packet in the nack list. | |
stefan-webrtc
2016/03/01 08:52:10
at least
philipel
2016/03/01 10:27:06
Done.
| |
170 nack_list_.erase(nack_list_.begin(), it); | |
171 return true; | |
stefan-webrtc
2016/03/01 08:52:10
I'd prefer to change the structure of this loop a
philipel
2016/03/01 10:27:06
Done.
| |
172 } | |
173 return false; | |
174 } | |
175 | |
176 void NackModule::AddPacketsToNack(uint16_t seq_num_start, | |
177 uint16_t seq_num_end) { | |
178 // Remove old packets. | |
179 auto it = nack_list_.lower_bound(seq_num_end - kMaxPacketAge); | |
stefan-webrtc
2016/03/01 08:52:10
We've been talking about adding support for a time
philipel
2016/03/01 10:27:06
It is something we can implement later on, I don't
stefan-webrtc
2016/03/01 12:16:34
Sounds good.
| |
180 nack_list_.erase(nack_list_.begin(), it); | |
181 | |
182 // If the nack list is too large, remove packets from the nack list until | |
183 // the latest first packet of a keyframe. If the list is still too large, | |
184 // clear it and request a keyframe. | |
185 uint16_t num_new_nacks = ForwardDiff(seq_num_start, seq_num_end); | |
186 if (nack_list_.size() + num_new_nacks > kMaxNackPackets) { | |
187 while (RemovePacketsUntilKeyframe() && | |
stefan-webrtc
2016/03/01 08:52:10
Search and replace Keyframe -> KeyFrame
philipel
2016/03/01 10:27:06
Done.
| |
188 nack_list_.size() + num_new_nacks > kMaxNackPackets) {} | |
189 | |
190 if (nack_list_.size() + num_new_nacks > kMaxNackPackets) { | |
191 nack_list_.clear(); | |
192 keyframe_request_sender_->RequestKeyFrame(); | |
193 return; | |
194 } | |
195 } | |
196 | |
197 for (uint16_t seq_num = seq_num_start; seq_num != seq_num_end; ++seq_num) { | |
198 NackInfo nack_info(seq_num, seq_num + WaitNumberOfPackets(0.5)); | |
199 RTC_DCHECK(nack_list_.find(seq_num) == nack_list_.end()); | |
200 nack_list_[seq_num] = nack_info; | |
201 } | |
202 } | |
203 | |
204 void NackModule::RemovePacketFromNack(uint16_t seq_num) { | |
stefan-webrtc
2016/03/01 08:52:10
Maybe remove this method now that it's trivial?
philipel
2016/03/01 10:27:06
Done.
| |
205 nack_list_.erase(seq_num); | |
206 } | |
207 | |
208 std::vector<uint16_t> NackModule::GetNackBatch(NackFilterOptions options) { | |
209 bool consider_seq_num = options != kTimeOnly; | |
210 bool consider_timestamp = options != kSeqNumOnly; | |
211 int64_t now_ms = clock_->TimeInMilliseconds(); | |
212 std::vector<uint16_t> nack_batch; | |
213 auto it = nack_list_.begin(); | |
214 while (it != nack_list_.end()) { | |
215 if (consider_seq_num && | |
216 it->second.sent_at_time == -1 && | |
217 AheadOrAt(last_seq_num_, it->second.send_at_seq_num)) { | |
218 nack_batch.emplace_back(it->second.seq_num); | |
219 ++it->second.retries; | |
220 it->second.sent_at_time = now_ms; | |
221 if (it->second.retries >= kMaxNackRetries) { | |
222 it = nack_list_.erase(it); | |
223 } else { | |
224 ++it; | |
225 } | |
226 continue; | |
227 } | |
228 | |
229 if (consider_timestamp && | |
230 it->second.sent_at_time + rtt_ms_ <= now_ms) { | |
231 nack_batch.emplace_back(it->second.seq_num); | |
232 ++it->second.retries; | |
233 it->second.sent_at_time = now_ms; | |
234 if (it->second.retries >= kMaxNackRetries) { | |
235 it = nack_list_.erase(it); | |
236 } else { | |
237 ++it; | |
238 } | |
239 continue; | |
240 } | |
241 ++it; | |
242 } | |
243 return nack_batch; | |
244 } | |
245 | |
246 void NackModule::UpdateReorderingStatistics(uint16_t seq_num) { | |
247 RTC_DCHECK(AheadOf(last_seq_num_, seq_num)); | |
248 uint16_t diff = ReverseDiff(last_seq_num_, seq_num); | |
249 reordering_distribution_.AddValue(diff); | |
250 } | |
251 | |
252 int NackModule::WaitNumberOfPackets(float probability) const { | |
253 if (reordering_distribution_.NumValues() == 0) | |
254 return 0; | |
255 return reordering_distribution_.InverseCDF(probability); | |
256 } | |
257 | |
258 } // namespace webrtc | |
OLD | NEW |