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 void UpdateCounter(size_t* counter, bool increment) { |
| 26 if (increment) { |
| 27 RTC_DCHECK_LT(*counter, std::numeric_limits<std::size_t>::max()); |
| 28 ++(*counter); |
| 29 } else { |
| 30 RTC_DCHECK_GT(*counter, 0); |
| 31 --(*counter); |
| 32 } |
| 33 } |
| 34 |
24 } // namespace | 35 } // namespace |
25 | 36 |
26 namespace webrtc { | 37 namespace webrtc { |
27 | 38 |
28 TransportFeedbackPacketLossTracker::TransportFeedbackPacketLossTracker( | 39 TransportFeedbackPacketLossTracker::TransportFeedbackPacketLossTracker( |
29 size_t min_window_size, | 40 size_t min_window_size, |
30 size_t max_window_size) | 41 size_t max_window_size, |
31 : min_window_size_(min_window_size), | 42 size_t rplr_min_num_pairs) |
32 max_window_size_(max_window_size), | 43 : max_window_size_(max_window_size), |
33 ref_packet_status_(packet_status_window_.begin()) { | 44 ref_packet_status_(packet_status_window_.begin()), |
| 45 plr_state_(min_window_size), |
| 46 rplr_state_(rplr_min_num_pairs) { |
34 RTC_DCHECK_GT(min_window_size, 0); | 47 RTC_DCHECK_GT(min_window_size, 0); |
35 RTC_DCHECK_GE(max_window_size_, min_window_size_); | 48 RTC_DCHECK_GE(max_window_size, min_window_size); |
36 RTC_DCHECK_LE(max_window_size_, kSeqNumHalf); | 49 RTC_DCHECK_LE(max_window_size, kSeqNumHalf); |
| 50 RTC_DCHECK_GT(rplr_min_num_pairs, 0); |
| 51 RTC_DCHECK_GT(max_window_size, rplr_min_num_pairs); |
37 Reset(); | 52 Reset(); |
38 } | 53 } |
39 | 54 |
40 void TransportFeedbackPacketLossTracker::Reset() { | 55 void TransportFeedbackPacketLossTracker::Reset() { |
41 num_received_packets_ = 0; | 56 plr_state_.Reset(); |
42 num_lost_packets_ = 0; | 57 rplr_state_.Reset(); |
43 num_consecutive_losses_ = 0; | |
44 num_consecutive_old_reports_ = 0; | 58 num_consecutive_old_reports_ = 0; |
45 packet_status_window_.clear(); | 59 packet_status_window_.clear(); |
46 ref_packet_status_ = packet_status_window_.begin(); | 60 ref_packet_status_ = packet_status_window_.begin(); |
47 } | 61 } |
48 | 62 |
49 uint16_t TransportFeedbackPacketLossTracker::ReferenceSequenceNumber() const { | 63 uint16_t TransportFeedbackPacketLossTracker::ReferenceSequenceNumber() const { |
50 RTC_DCHECK(!packet_status_window_.empty()); | 64 RTC_DCHECK(!packet_status_window_.empty()); |
51 return ref_packet_status_->first; | 65 return ref_packet_status_->first; |
52 } | 66 } |
53 | 67 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 webrtc::rtcp::TransportFeedback::StatusSymbol::kNotReceived; | 111 webrtc::rtcp::TransportFeedback::StatusSymbol::kNotReceived; |
98 InsertPacketStatus(seq_num, received); | 112 InsertPacketStatus(seq_num, received); |
99 | 113 |
100 while (packet_status_window_.size() > max_window_size_) { | 114 while (packet_status_window_.size() > max_window_size_) { |
101 // Make sure that the window holds at most |max_window_size_| items. | 115 // Make sure that the window holds at most |max_window_size_| items. |
102 RemoveOldestPacketStatus(); | 116 RemoveOldestPacketStatus(); |
103 } | 117 } |
104 } | 118 } |
105 } | 119 } |
106 | 120 |
107 bool TransportFeedbackPacketLossTracker::GetPacketLossRates( | 121 rtc::Optional<float> |
108 float* packet_loss_rate, | 122 TransportFeedbackPacketLossTracker::GetPacketLossRate() const { |
109 float* consecutive_packet_loss_rate) const { | 123 return plr_state_.GetMetric(); |
110 const size_t total = num_lost_packets_ + num_received_packets_; | 124 } |
111 if (total < min_window_size_) | 125 |
112 return false; | 126 rtc::Optional<float> |
113 *packet_loss_rate = static_cast<float>(num_lost_packets_) / total; | 127 TransportFeedbackPacketLossTracker::GetRecoverablePacketLossRate() const { |
114 *consecutive_packet_loss_rate = | 128 return rplr_state_.GetMetric(); |
115 static_cast<float>(num_consecutive_losses_) / total; | |
116 return true; | |
117 } | 129 } |
118 | 130 |
119 void TransportFeedbackPacketLossTracker::InsertPacketStatus(uint16_t seq_num, | 131 void TransportFeedbackPacketLossTracker::InsertPacketStatus(uint16_t seq_num, |
120 bool received) { | 132 bool received) { |
121 const auto& ret = | 133 const auto& ret = |
122 packet_status_window_.insert(std::make_pair(seq_num, received)); | 134 packet_status_window_.insert(std::make_pair(seq_num, received)); |
123 if (!ret.second) { | 135 if (!ret.second) { |
124 if (!ret.first->second && received) { | 136 if (!ret.first->second && received) { |
125 // If older status said that the packet was lost but newer one says it | 137 // If older status said that the packet was lost but newer one says it |
126 // is received, we take the newer one. | 138 // is received, we take the newer one. |
127 UndoPacketStatus(ret.first); | 139 UpdateMetrics(ret.first, false); |
128 ret.first->second = received; | 140 ret.first->second = received; |
129 } else { | 141 } else { |
130 // If the value is unchanged or if older status said that the packet was | 142 // 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. | 143 // received but the newer one says it is lost, we ignore it. |
132 return; | 144 return; |
133 } | 145 } |
134 } | 146 } |
135 ApplyPacketStatus(ret.first); | 147 UpdateMetrics(ret.first, true); |
136 if (packet_status_window_.size() == 1) | 148 if (packet_status_window_.size() == 1) |
137 ref_packet_status_ = ret.first; | 149 ref_packet_status_ = ret.first; |
138 } | 150 } |
139 | 151 |
140 void TransportFeedbackPacketLossTracker::RemoveOldestPacketStatus() { | 152 void TransportFeedbackPacketLossTracker::RemoveOldestPacketStatus() { |
141 UndoPacketStatus(ref_packet_status_); | 153 UpdateMetrics(ref_packet_status_, false); |
142 const auto it = ref_packet_status_; | 154 const auto it = ref_packet_status_; |
143 ref_packet_status_ = NextPacketStatus(it); | 155 ref_packet_status_ = NextPacketStatus(it); |
144 packet_status_window_.erase(it); | 156 packet_status_window_.erase(it); |
145 } | 157 } |
146 | 158 |
147 void TransportFeedbackPacketLossTracker::ApplyPacketStatus( | 159 void TransportFeedbackPacketLossTracker::UpdateMetrics( |
148 PacketStatusIterator it) { | 160 PacketStatusIterator it, |
| 161 bool apply /* false = undo */) { |
149 RTC_DCHECK(it != packet_status_window_.end()); | 162 RTC_DCHECK(it != packet_status_window_.end()); |
| 163 UpdatePlr(it, apply); |
| 164 UpdateRplr(it, apply); |
| 165 } |
| 166 |
| 167 void TransportFeedbackPacketLossTracker::UpdatePlr( |
| 168 PacketStatusIterator it, |
| 169 bool apply /* false = undo */) { |
| 170 // Record or undo reception status of currently handled packet. |
150 if (it->second) { | 171 if (it->second) { |
151 ++num_received_packets_; | 172 UpdateCounter(&plr_state_.num_received_packets_, apply); |
152 } else { | 173 } else { |
153 ++num_lost_packets_; | 174 UpdateCounter(&plr_state_.num_lost_packets_, apply); |
154 const auto& next = NextPacketStatus(it); | 175 } |
155 if (next != packet_status_window_.end() && | 176 } |
156 next->first == static_cast<uint16_t>(it->first + 1) && !next->second) { | 177 |
157 // Feedback shows that the next packet has been lost. Since this | 178 void TransportFeedbackPacketLossTracker::UpdateRplr( |
158 // packet is lost, we increase the consecutive loss counter. | 179 PacketStatusIterator it, |
159 ++num_consecutive_losses_; | 180 bool apply /* false = undo */) { |
160 } | 181 // Previous packet and current packet might compose a known pair. |
161 if (it != ref_packet_status_) { | 182 // If so, the RPLR state needs to be updated accordingly. |
162 const auto& pre = PreviousPacketStatus(it); | 183 if (it != ref_packet_status_) { |
163 if (pre->first == static_cast<uint16_t>(it->first - 1) && !pre->second) { | 184 const auto& prev = PreviousPacketStatus(it); |
164 // Feedback shows that the previous packet has been lost. Since this | 185 if (prev->first == static_cast<uint16_t>(it->first - 1)) { |
165 // packet is lost, we increase the consecutive loss counter. | 186 UpdateCounter(&rplr_state_.num_known_pairs_, apply); |
166 ++num_consecutive_losses_; | 187 if (!prev->second && it->second) { |
| 188 UpdateCounter( |
| 189 &rplr_state_.num_loss_followed_by_reception_pairs_, apply); |
167 } | 190 } |
168 } | 191 } |
169 } | 192 } |
170 } | |
171 | 193 |
172 void TransportFeedbackPacketLossTracker::UndoPacketStatus( | 194 // Current packet and next packet might compose a pair. |
173 PacketStatusIterator it) { | 195 // If so, the RPLR state needs to be updated accordingly. |
174 RTC_DCHECK(it != packet_status_window_.end()); | 196 const auto& next = NextPacketStatus(it); |
175 if (it->second) { | 197 if (next != packet_status_window_.end() && |
176 RTC_DCHECK_GT(num_received_packets_, 0); | 198 next->first == static_cast<uint16_t>(it->first + 1)) { |
177 --num_received_packets_; | 199 UpdateCounter(&rplr_state_.num_known_pairs_, apply); |
178 } else { | 200 if (!it->second && next->second) { |
179 RTC_DCHECK_GT(num_lost_packets_, 0); | 201 UpdateCounter(&rplr_state_.num_loss_followed_by_reception_pairs_, apply); |
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 } | 202 } |
194 } | 203 } |
195 } | 204 } |
196 | 205 |
197 TransportFeedbackPacketLossTracker::PacketStatusIterator | 206 TransportFeedbackPacketLossTracker::PacketStatusIterator |
198 TransportFeedbackPacketLossTracker::PreviousPacketStatus( | 207 TransportFeedbackPacketLossTracker::PreviousPacketStatus( |
199 PacketStatusIterator it) { | 208 PacketStatusIterator it) { |
200 RTC_DCHECK(it != ref_packet_status_); | 209 RTC_DCHECK(it != ref_packet_status_); |
201 if (it == packet_status_window_.end()) { | 210 if (it == packet_status_window_.end()) { |
202 // This is to make PreviousPacketStatus(packet_status_window_.end()) point | 211 // This is to make PreviousPacketStatus(packet_status_window_.end()) point |
(...skipping 26 matching lines...) Expand all Loading... |
229 return it; | 238 return it; |
230 } | 239 } |
231 | 240 |
232 // TODO(minyue): This method checks the states of this class do not misbehave. | 241 // 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 | 242 // 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 | 243 // 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 | 244 // error after long period, we can remove the fuzzer test, and move this method |
236 // to unit test. | 245 // to unit test. |
237 void TransportFeedbackPacketLossTracker::Validate() const { // Testing only! | 246 void TransportFeedbackPacketLossTracker::Validate() const { // Testing only! |
238 RTC_CHECK_LE(packet_status_window_.size(), max_window_size_); | 247 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(), | 248 RTC_CHECK_EQ(packet_status_window_.size(), |
241 num_lost_packets_ + num_received_packets_); | 249 plr_state_.num_lost_packets_ + plr_state_.num_received_packets_); |
| 250 RTC_CHECK_LE(rplr_state_.num_loss_followed_by_reception_pairs_, |
| 251 rplr_state_.num_known_pairs_); |
| 252 RTC_CHECK_LE(rplr_state_.num_known_pairs_, |
| 253 packet_status_window_.size() - 1); |
242 | 254 |
243 size_t received_packets = 0; | 255 size_t received_packets = 0; |
244 size_t lost_packets = 0; | 256 size_t lost_packets = 0; |
245 size_t consecutive_losses = 0; | 257 size_t known_status_pairs = 0; |
| 258 size_t loss_followed_by_reception_pairs = 0; |
246 | 259 |
247 if (!packet_status_window_.empty()) { | 260 if (!packet_status_window_.empty()) { |
248 PacketStatusIterator it = ref_packet_status_; | 261 PacketStatusIterator it = ref_packet_status_; |
249 bool pre_lost = false; | |
250 uint16_t pre_seq_num = it->first - 1; | |
251 do { | 262 do { |
252 if (it->second) { | 263 if (it->second) { |
253 ++received_packets; | 264 ++received_packets; |
254 } else { | 265 } else { |
255 ++lost_packets; | 266 ++lost_packets; |
256 if (pre_lost && pre_seq_num == static_cast<uint16_t>(it->first - 1)) | 267 } |
257 ++consecutive_losses; | 268 |
| 269 auto next = std::next(it); |
| 270 if (next == packet_status_window_.end()) |
| 271 next = packet_status_window_.begin(); |
| 272 |
| 273 if (next != ref_packet_status_ && |
| 274 next->first == static_cast<uint16_t>(it->first + 1)) { |
| 275 ++known_status_pairs; |
| 276 if (!it->second && next->second) |
| 277 ++loss_followed_by_reception_pairs; |
258 } | 278 } |
259 | 279 |
260 RTC_CHECK_LT(ForwardDiff(ReferenceSequenceNumber(), it->first), | 280 RTC_CHECK_LT(ForwardDiff(ReferenceSequenceNumber(), it->first), |
261 kSeqNumHalf); | 281 kSeqNumHalf); |
262 | 282 |
263 pre_lost = !it->second; | 283 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_); | 284 } while (it != ref_packet_status_); |
270 } | 285 } |
271 | 286 |
272 RTC_CHECK_EQ(num_received_packets_, received_packets); | 287 RTC_CHECK_EQ(plr_state_.num_received_packets_, received_packets); |
273 RTC_CHECK_EQ(num_lost_packets_, lost_packets); | 288 RTC_CHECK_EQ(plr_state_.num_lost_packets_, lost_packets); |
274 RTC_CHECK_EQ(num_consecutive_losses_, consecutive_losses); | 289 RTC_CHECK_EQ(rplr_state_.num_known_pairs_, known_status_pairs); |
| 290 RTC_CHECK_EQ(rplr_state_.num_loss_followed_by_reception_pairs_, |
| 291 loss_followed_by_reception_pairs); |
| 292 } |
| 293 |
| 294 rtc::Optional<float> |
| 295 TransportFeedbackPacketLossTracker::PlrState::GetMetric() const { |
| 296 const size_t total = num_lost_packets_ + num_received_packets_; |
| 297 if (total < min_window_size_) { |
| 298 return rtc::Optional<float>(); |
| 299 } else { |
| 300 return rtc::Optional<float>( |
| 301 static_cast<float>(num_lost_packets_) / total); |
| 302 } |
| 303 } |
| 304 |
| 305 rtc::Optional<float> |
| 306 TransportFeedbackPacketLossTracker::RplrState::GetMetric() const { |
| 307 if (num_known_pairs_ < min_num_pairs_) { |
| 308 return rtc::Optional<float>(); |
| 309 } else { |
| 310 return rtc::Optional<float>( |
| 311 static_cast<float>(num_loss_followed_by_reception_pairs_) / |
| 312 num_known_pairs_); |
| 313 } |
275 } | 314 } |
276 | 315 |
277 } // namespace webrtc | 316 } // namespace webrtc |
OLD | NEW |