OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/voice_engine/transport_feedback_packet_loss_tracker.h" | 11 #include "webrtc/voice_engine/transport_feedback_packet_loss_tracker.h" |
12 | 12 |
13 #include <limits> | 13 #include <limits> |
14 #include <utility> | 14 #include <utility> |
15 | 15 |
16 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
17 #include "webrtc/base/mod_ops.h" | 17 #include "webrtc/base/mod_ops.h" |
18 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h" | 18 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h" |
19 | 19 |
20 namespace { | 20 namespace { |
21 constexpr uint16_t kSeqNumHalf = 0x8000u; | 21 constexpr uint16_t kSeqNumHalf = 0x8000u; |
22 constexpr uint16_t kSeqNumQuarter = kSeqNumHalf / 2; | 22 constexpr uint16_t kSeqNumQuarter = kSeqNumHalf / 2; |
23 constexpr size_t kMaxConsecutiveOldReports = 4; | 23 constexpr size_t kMaxConsecutiveOldReports = 4; |
24 | |
25 inline void UpdateCounter(size_t& counter, bool increment) { | |
minyue-webrtc
2017/01/25 09:38:45
remove "inline", it happens automatically.
minyue-webrtc
2017/01/25 09:38:45
& always goes with const, so use
size_t* counter
| |
26 increment ? ++counter : --counter; | |
27 } | |
28 | |
24 } // namespace | 29 } // namespace |
25 | 30 |
26 namespace webrtc { | 31 namespace webrtc { |
27 | 32 |
28 TransportFeedbackPacketLossTracker::TransportFeedbackPacketLossTracker( | 33 TransportFeedbackPacketLossTracker::TransportFeedbackPacketLossTracker( |
29 size_t min_window_size, | 34 size_t min_window_size, |
30 size_t max_window_size) | 35 size_t max_window_size, |
31 : min_window_size_(min_window_size), | 36 size_t min_pairs_num_for_rplr) |
32 max_window_size_(max_window_size), | 37 : max_window_size_(max_window_size), |
33 ref_packet_status_(packet_status_window_.begin()) { | 38 ref_packet_status_(packet_status_window_.begin()), |
34 RTC_DCHECK_GT(min_window_size, 0); | 39 plr_state_(min_window_size), |
35 RTC_DCHECK_GE(max_window_size_, min_window_size_); | 40 rplr_state_(min_pairs_num_for_rplr) { |
36 RTC_DCHECK_LE(max_window_size_, kSeqNumHalf); | 41 RTC_DCHECK_GT(min_window_size, 0u); |
minyue-webrtc
2017/01/25 09:38:45
remove "u" after 0
elad.alon_webrtc.org
2017/01/25 12:47:54
Rebase relic. Done.
| |
42 RTC_DCHECK_GE(max_window_size, min_window_size); | |
43 RTC_DCHECK_LE(max_window_size, kSeqNumHalf); | |
44 RTC_DCHECK_GT(min_pairs_num_for_rplr, 0); | |
45 RTC_DCHECK_GT(max_window_size, min_pairs_num_for_rplr); | |
37 Reset(); | 46 Reset(); |
38 } | 47 } |
39 | 48 |
40 void TransportFeedbackPacketLossTracker::Reset() { | 49 void TransportFeedbackPacketLossTracker::Reset() { |
41 num_received_packets_ = 0; | 50 plr_state_.Reset(); |
42 num_lost_packets_ = 0; | 51 rplr_state_.Reset(); |
43 num_consecutive_losses_ = 0; | |
44 num_consecutive_old_reports_ = 0; | 52 num_consecutive_old_reports_ = 0; |
45 packet_status_window_.clear(); | 53 packet_status_window_.clear(); |
46 ref_packet_status_ = packet_status_window_.begin(); | 54 ref_packet_status_ = packet_status_window_.begin(); |
47 } | 55 } |
48 | 56 |
49 uint16_t TransportFeedbackPacketLossTracker::ReferenceSequenceNumber() const { | 57 uint16_t TransportFeedbackPacketLossTracker::ReferenceSequenceNumber() const { |
50 RTC_DCHECK(!packet_status_window_.empty()); | 58 RTC_DCHECK(!packet_status_window_.empty()); |
51 return ref_packet_status_->first; | 59 return ref_packet_status_->first; |
52 } | 60 } |
53 | 61 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 webrtc::rtcp::TransportFeedback::StatusSymbol::kNotReceived; | 105 webrtc::rtcp::TransportFeedback::StatusSymbol::kNotReceived; |
98 InsertPacketStatus(seq_num, received); | 106 InsertPacketStatus(seq_num, received); |
99 | 107 |
100 while (packet_status_window_.size() > max_window_size_) { | 108 while (packet_status_window_.size() > max_window_size_) { |
101 // Make sure that the window holds at most |max_window_size_| items. | 109 // Make sure that the window holds at most |max_window_size_| items. |
102 RemoveOldestPacketStatus(); | 110 RemoveOldestPacketStatus(); |
103 } | 111 } |
104 } | 112 } |
105 } | 113 } |
106 | 114 |
107 bool TransportFeedbackPacketLossTracker::GetPacketLossRates( | 115 rtc::Optional<float> |
108 float* packet_loss_rate, | 116 TransportFeedbackPacketLossTracker::GetPacketLossRate() const { |
109 float* consecutive_packet_loss_rate) const { | 117 return plr_state_.GetMetric(); |
110 const size_t total = num_lost_packets_ + num_received_packets_; | 118 } |
111 if (total < min_window_size_) | 119 |
112 return false; | 120 // Returns the first-order-FEC recoverable packet loss rate, if the window |
minyue-webrtc
2017/01/25 09:38:45
merge this comment to the header file
elad.alon_webrtc.org
2017/01/25 12:47:54
Done.
| |
113 *packet_loss_rate = static_cast<float>(num_lost_packets_) / total; | 121 // has enough data to reliably compute it. |
114 *consecutive_packet_loss_rate = | 122 rtc::Optional<float> |
115 static_cast<float>(num_consecutive_losses_) / total; | 123 TransportFeedbackPacketLossTracker::GetRecoverablePacketLossRate() const { |
116 return true; | 124 return rplr_state_.GetMetric(); |
117 } | 125 } |
118 | 126 |
119 void TransportFeedbackPacketLossTracker::InsertPacketStatus(uint16_t seq_num, | 127 void TransportFeedbackPacketLossTracker::InsertPacketStatus(uint16_t seq_num, |
120 bool received) { | 128 bool received) { |
121 const auto& ret = | 129 const auto& ret = |
122 packet_status_window_.insert(std::make_pair(seq_num, received)); | 130 packet_status_window_.insert(std::make_pair(seq_num, received)); |
123 if (!ret.second) { | 131 if (!ret.second) { |
124 if (!ret.first->second && received) { | 132 if (!ret.first->second && received) { |
125 // If older status said that the packet was lost but newer one says it | 133 // If older status said that the packet was lost but newer one says it |
126 // is received, we take the newer one. | 134 // is received, we take the newer one. |
127 UndoPacketStatus(ret.first); | 135 UpdateMetrics(ret.first, false); |
128 ret.first->second = received; | 136 ret.first->second = received; |
129 } else { | 137 } else { |
130 // If the value is unchanged or if older status said that the packet was | 138 // If the value is unchanged or if older status said that the packet was |
131 // received but the newer one says it is lost, we ignore it. | 139 // received but the newer one says it is lost, we ignore it. |
132 return; | 140 return; |
133 } | 141 } |
134 } | 142 } |
135 ApplyPacketStatus(ret.first); | 143 UpdateMetrics(ret.first, true); |
136 if (packet_status_window_.size() == 1) | 144 if (packet_status_window_.size() == 1) |
137 ref_packet_status_ = ret.first; | 145 ref_packet_status_ = ret.first; |
138 } | 146 } |
139 | 147 |
140 void TransportFeedbackPacketLossTracker::RemoveOldestPacketStatus() { | 148 void TransportFeedbackPacketLossTracker::RemoveOldestPacketStatus() { |
141 UndoPacketStatus(ref_packet_status_); | 149 UpdateMetrics(ref_packet_status_, false); |
142 const auto it = ref_packet_status_; | 150 const auto it = ref_packet_status_; |
143 ref_packet_status_ = NextPacketStatus(it); | 151 ref_packet_status_ = NextPacketStatus(it); |
144 packet_status_window_.erase(it); | 152 packet_status_window_.erase(it); |
145 } | 153 } |
146 | 154 |
147 void TransportFeedbackPacketLossTracker::ApplyPacketStatus( | 155 void TransportFeedbackPacketLossTracker::UpdateMetrics( |
148 PacketStatusIterator it) { | 156 PacketStatusIterator it, |
157 bool apply /* false = undo */) { | |
149 RTC_DCHECK(it != packet_status_window_.end()); | 158 RTC_DCHECK(it != packet_status_window_.end()); |
159 UpdatePlr(it, apply); | |
160 UpdateRplr(it, apply); | |
161 } | |
162 | |
163 void TransportFeedbackPacketLossTracker::UpdatePlr( | |
164 PacketStatusIterator it, | |
165 bool apply /* false = undo */) { | |
166 // Record or undo recetion status of currently handled packet. | |
minyue-webrtc
2017/01/25 09:38:45
reception
But I think the code is clear enough to
elad.alon_webrtc.org
2017/01/25 12:47:54
I want this comment to indicate the "currently han
| |
150 if (it->second) { | 167 if (it->second) { |
151 ++num_received_packets_; | 168 UpdateCounter(plr_state_.num_received_packets_, apply); |
152 } else { | 169 } else { |
153 ++num_lost_packets_; | 170 UpdateCounter(plr_state_.num_lost_packets_, apply); |
154 const auto& next = NextPacketStatus(it); | 171 } |
155 if (next != packet_status_window_.end() && | 172 } |
156 next->first == static_cast<uint16_t>(it->first + 1) && !next->second) { | 173 |
157 // Feedback shows that the next packet has been lost. Since this | 174 void TransportFeedbackPacketLossTracker::UpdateRplr( |
158 // packet is lost, we increase the consecutive loss counter. | 175 PacketStatusIterator it, |
159 ++num_consecutive_losses_; | 176 bool apply /* false = undo */) { |
160 } | 177 // Previous packet and current packet might compose a pair. |
minyue-webrtc
2017/01/25 09:38:45
preferably, move the comment after 180 and write
elad.alon_webrtc.org
2017/01/25 12:47:54
IMHO, clearer to have it over the entire block, wh
| |
161 if (it != ref_packet_status_) { | 178 if (it != ref_packet_status_) { |
162 const auto& pre = PreviousPacketStatus(it); | 179 const auto& prev = PreviousPacketStatus(it); |
163 if (pre->first == static_cast<uint16_t>(it->first - 1) && !pre->second) { | 180 if (prev->first == static_cast<uint16_t>(it->first - 1)) { |
164 // Feedback shows that the previous packet has been lost. Since this | 181 UpdateCounter(rplr_state_.num_known_status_pairs_, apply); |
165 // packet is lost, we increase the consecutive loss counter. | 182 if (!prev->second && it->second) { |
166 ++num_consecutive_losses_; | 183 UpdateCounter(rplr_state_.num_loss_followed_by_reception_pairs_, apply); |
167 } | 184 } |
168 } | 185 } |
169 } | 186 } |
170 } | |
171 | 187 |
172 void TransportFeedbackPacketLossTracker::UndoPacketStatus( | 188 // Current packet and next packet might compose a pair. |
minyue-webrtc
2017/01/25 09:38:45
preferably, move the comment after 190 and write
elad.alon_webrtc.org
2017/01/25 12:47:54
Similarly.
| |
173 PacketStatusIterator it) { | 189 const auto& next = NextPacketStatus(it); |
174 RTC_DCHECK(it != packet_status_window_.end()); | 190 if (next != packet_status_window_.end() && |
175 if (it->second) { | 191 next->first == static_cast<uint16_t>(it->first + 1)) { |
176 RTC_DCHECK_GT(num_received_packets_, 0); | 192 UpdateCounter(rplr_state_.num_known_status_pairs_, apply); |
177 --num_received_packets_; | 193 if (!it->second && next->second) { |
178 } else { | 194 UpdateCounter(rplr_state_.num_loss_followed_by_reception_pairs_, apply); |
179 RTC_DCHECK_GT(num_lost_packets_, 0); | |
180 --num_lost_packets_; | |
181 const auto& next = NextPacketStatus(it); | |
182 if (next != packet_status_window_.end() && | |
183 next->first == static_cast<uint16_t>(it->first + 1) && !next->second) { | |
184 RTC_DCHECK_GT(num_consecutive_losses_, 0); | |
185 --num_consecutive_losses_; | |
186 } | |
187 if (it != ref_packet_status_) { | |
188 const auto& pre = PreviousPacketStatus(it); | |
189 if (pre->first == static_cast<uint16_t>(it->first - 1) && !pre->second) { | |
190 RTC_DCHECK_GT(num_consecutive_losses_, 0); | |
191 --num_consecutive_losses_; | |
192 } | |
193 } | 195 } |
194 } | 196 } |
195 } | 197 } |
196 | 198 |
197 TransportFeedbackPacketLossTracker::PacketStatusIterator | 199 TransportFeedbackPacketLossTracker::PacketStatusIterator |
198 TransportFeedbackPacketLossTracker::PreviousPacketStatus( | 200 TransportFeedbackPacketLossTracker::PreviousPacketStatus( |
199 PacketStatusIterator it) { | 201 PacketStatusIterator it) { |
200 RTC_DCHECK(it != ref_packet_status_); | 202 RTC_DCHECK(it != ref_packet_status_); |
201 if (it == packet_status_window_.end()) { | 203 if (it == packet_status_window_.end()) { |
202 // This is to make PreviousPacketStatus(packet_status_window_.end()) point | 204 // This is to make PreviousPacketStatus(packet_status_window_.end()) point |
(...skipping 26 matching lines...) Expand all Loading... | |
229 return it; | 231 return it; |
230 } | 232 } |
231 | 233 |
232 // TODO(minyue): This method checks the states of this class do not misbehave. | 234 // TODO(minyue): This method checks the states of this class do not misbehave. |
233 // The method is used both in unit tests and a fuzzer test. The fuzzer test | 235 // The method is used both in unit tests and a fuzzer test. The fuzzer test |
234 // is present to help finding potential errors. Once the fuzzer test shows no | 236 // is present to help finding potential errors. Once the fuzzer test shows no |
235 // error after long period, we can remove the fuzzer test, and move this method | 237 // error after long period, we can remove the fuzzer test, and move this method |
236 // to unit test. | 238 // to unit test. |
237 void TransportFeedbackPacketLossTracker::Validate() const { // Testing only! | 239 void TransportFeedbackPacketLossTracker::Validate() const { // Testing only! |
238 RTC_CHECK_LE(packet_status_window_.size(), max_window_size_); | 240 RTC_CHECK_LE(packet_status_window_.size(), max_window_size_); |
239 RTC_CHECK_GE(num_lost_packets_, num_consecutive_losses_); | |
240 RTC_CHECK_EQ(packet_status_window_.size(), | 241 RTC_CHECK_EQ(packet_status_window_.size(), |
241 num_lost_packets_ + num_received_packets_); | 242 plr_state_.num_lost_packets_ + plr_state_.num_received_packets_); |
243 RTC_CHECK_LE(rplr_state_.num_loss_followed_by_reception_pairs_, | |
244 rplr_state_.num_known_status_pairs_); | |
245 RTC_CHECK_LE(rplr_state_.num_known_status_pairs_, | |
246 packet_status_window_.size() - 1); | |
242 | 247 |
243 size_t received_packets = 0; | 248 size_t received_packets = 0; |
244 size_t lost_packets = 0; | 249 size_t lost_packets = 0; |
245 size_t consecutive_losses = 0; | 250 size_t known_status_pairs = 0; |
minyue-webrtc
2017/01/25 09:38:45
if you accept to simplify the private var to num_p
elad.alon_webrtc.org
2017/01/25 12:47:54
I prefer known_status_pairs, because the next CL i
| |
251 size_t loss_followed_by_reception_pairs = 0; | |
minyue-webrtc
2017/01/25 09:38:45
lost_received_pairs
elad.alon_webrtc.org
2017/01/25 12:47:54
Discussed in .h file comments. More feedback welco
| |
246 | 252 |
247 if (!packet_status_window_.empty()) { | 253 if (!packet_status_window_.empty()) { |
248 PacketStatusIterator it = ref_packet_status_; | 254 PacketStatusIterator it = ref_packet_status_; |
249 bool pre_lost = false; | |
250 uint16_t pre_seq_num = it->first - 1; | |
251 do { | 255 do { |
252 if (it->second) { | 256 if (it->second) { |
253 ++received_packets; | 257 ++received_packets; |
254 } else { | 258 } else { |
255 ++lost_packets; | 259 ++lost_packets; |
256 if (pre_lost && pre_seq_num == static_cast<uint16_t>(it->first - 1)) | 260 } |
257 ++consecutive_losses; | 261 |
262 auto next = std::next(it); | |
263 if (next == packet_status_window_.end()) | |
264 next = packet_status_window_.begin(); | |
265 | |
266 if (next != ref_packet_status_ && | |
267 static_cast<uint16_t>(it->first + 1) == next->first) { | |
minyue-webrtc
2017/01/25 09:38:45
next->first == static_cast<uint16_t>(it->first + 1
elad.alon_webrtc.org
2017/01/25 12:47:54
Done.
| |
268 ++known_status_pairs; | |
269 if (!it->second && next->second) | |
270 ++loss_followed_by_reception_pairs; | |
258 } | 271 } |
259 | 272 |
260 RTC_CHECK_LT(ForwardDiff(ReferenceSequenceNumber(), it->first), | 273 RTC_CHECK_LT(ForwardDiff(ReferenceSequenceNumber(), it->first), |
261 kSeqNumHalf); | 274 kSeqNumHalf); |
262 | 275 |
263 pre_lost = !it->second; | 276 it = next; |
264 pre_seq_num = it->first; | |
265 | |
266 ++it; | |
267 if (it == packet_status_window_.end()) | |
268 it = packet_status_window_.begin(); | |
269 } while (it != ref_packet_status_); | 277 } while (it != ref_packet_status_); |
270 } | 278 } |
271 | 279 |
272 RTC_CHECK_EQ(num_received_packets_, received_packets); | 280 RTC_CHECK_EQ(plr_state_.num_received_packets_, received_packets); |
273 RTC_CHECK_EQ(num_lost_packets_, lost_packets); | 281 RTC_CHECK_EQ(plr_state_.num_lost_packets_, lost_packets); |
274 RTC_CHECK_EQ(num_consecutive_losses_, consecutive_losses); | 282 RTC_CHECK_EQ(rplr_state_.num_known_status_pairs_, known_status_pairs); |
283 RTC_CHECK_EQ(rplr_state_.num_loss_followed_by_reception_pairs_, | |
284 loss_followed_by_reception_pairs); | |
285 } | |
286 | |
287 rtc::Optional<float> | |
288 TransportFeedbackPacketLossTracker::PlrState::GetMetric() const { | |
289 const size_t total = num_lost_packets_ + num_received_packets_; | |
290 if (total < min_window_size_) | |
minyue-webrtc
2017/01/25 09:38:45
have to add {} when there are multiple lines in if
elad.alon_webrtc.org
2017/01/25 12:47:54
There aren't multiple lines here. Or do you mean v
minyue-webrtc
2017/01/25 20:00:59
when else is present, it is considered multiple li
| |
291 return rtc::Optional<float>(); | |
292 else | |
293 return rtc::Optional<float>( | |
294 static_cast<float>(num_lost_packets_) / total); | |
295 } | |
296 | |
297 rtc::Optional<float> | |
298 TransportFeedbackPacketLossTracker::RplrState::GetMetric() const { | |
299 if (num_known_status_pairs_ < min_pairs_) | |
minyue-webrtc
2017/01/25 09:38:45
add {} around if and else
elad.alon_webrtc.org
2017/01/25 12:47:54
Similarly.
| |
300 return rtc::Optional<float>(); | |
301 else | |
302 return rtc::Optional<float>( | |
303 static_cast<float>(num_loss_followed_by_reception_pairs_) / | |
304 num_known_status_pairs_); | |
275 } | 305 } |
276 | 306 |
277 } // namespace webrtc | 307 } // namespace webrtc |
OLD | NEW |