OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 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 } // namespace | 24 } // namespace |
25 | 25 |
26 namespace webrtc { | 26 namespace webrtc { |
27 | 27 |
28 TransportFeedbackPacketLossTracker::TransportFeedbackPacketLossTracker( | 28 TransportFeedbackPacketLossTracker::TransportFeedbackPacketLossTracker( |
29 size_t min_window_size, | 29 size_t min_window_size, |
30 size_t max_window_size) | 30 size_t max_window_size, |
31 size_t min_pairs_num_for_rplr) | |
31 : min_window_size_(min_window_size), | 32 : min_window_size_(min_window_size), |
32 max_window_size_(max_window_size), | 33 max_window_size_(max_window_size), |
34 min_pairs_num_for_rplr_(min_pairs_num_for_rplr), | |
33 ref_packet_status_(packet_status_window_.begin()) { | 35 ref_packet_status_(packet_status_window_.begin()) { |
34 RTC_DCHECK_GT(min_window_size, 0u); | 36 RTC_DCHECK_GT(min_window_size, 0u); |
35 RTC_DCHECK_GE(max_window_size_, min_window_size_); | 37 RTC_DCHECK_GE(max_window_size_, min_window_size_); |
36 RTC_DCHECK_LE(max_window_size_, kSeqNumHalf); | 38 RTC_DCHECK_LE(max_window_size_, kSeqNumHalf); |
39 RTC_DCHECK_GT(min_pairs_num_for_rplr, 0); | |
40 RTC_DCHECK_GT(max_window_size, min_pairs_num_for_rplr); | |
37 Reset(); | 41 Reset(); |
38 } | 42 } |
39 | 43 |
40 void TransportFeedbackPacketLossTracker::Reset() { | 44 void TransportFeedbackPacketLossTracker::Reset() { |
41 num_received_packets_ = 0; | 45 num_received_packets_ = 0; |
42 num_lost_packets_ = 0; | 46 num_lost_packets_ = 0; |
43 num_consecutive_losses_ = 0; | 47 num_known_status_pairs_ = 0; |
48 num_loss_followed_by_reception_pairs_ = 0; | |
44 num_consecutive_old_reports_ = 0; | 49 num_consecutive_old_reports_ = 0; |
45 packet_status_window_.clear(); | 50 packet_status_window_.clear(); |
46 ref_packet_status_ = packet_status_window_.begin(); | 51 ref_packet_status_ = packet_status_window_.begin(); |
47 } | 52 } |
48 | 53 |
49 uint16_t TransportFeedbackPacketLossTracker::ReferenceSequenceNumber() const { | 54 uint16_t TransportFeedbackPacketLossTracker::ReferenceSequenceNumber() const { |
50 RTC_DCHECK(!packet_status_window_.empty()); | 55 RTC_DCHECK(!packet_status_window_.empty()); |
51 return ref_packet_status_->first; | 56 return ref_packet_status_->first; |
52 } | 57 } |
53 | 58 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
96 webrtc::rtcp::TransportFeedback::StatusSymbol::kNotReceived; | 101 webrtc::rtcp::TransportFeedback::StatusSymbol::kNotReceived; |
97 InsertPacketStatus(seq_num, received); | 102 InsertPacketStatus(seq_num, received); |
98 | 103 |
99 while (packet_status_window_.size() > max_window_size_) { | 104 while (packet_status_window_.size() > max_window_size_) { |
100 // Make sure that the window holds at most |max_window_size_| items. | 105 // Make sure that the window holds at most |max_window_size_| items. |
101 RemoveOldestPacketStatus(); | 106 RemoveOldestPacketStatus(); |
102 } | 107 } |
103 } | 108 } |
104 } | 109 } |
105 | 110 |
106 bool TransportFeedbackPacketLossTracker::GetPacketLossRates( | 111 rtc::Optional<float> |
107 float* packet_loss_rate, | 112 TransportFeedbackPacketLossTracker::GetPacketLossRate() const { |
minyue-webrtc
2017/01/23 14:04:54
Marginal packet loss rate will not be used, will i
| |
108 float* consecutive_packet_loss_rate) const { | |
109 const size_t total = num_lost_packets_ + num_received_packets_; | 113 const size_t total = num_lost_packets_ + num_received_packets_; |
110 if (total < min_window_size_) | 114 if (total < min_window_size_) |
111 return false; | 115 return rtc::Optional<float>(); |
112 *packet_loss_rate = static_cast<float>(num_lost_packets_) / total; | 116 else |
113 *consecutive_packet_loss_rate = | 117 return rtc::Optional<float>(static_cast<float>(num_lost_packets_) / total); |
114 static_cast<float>(num_consecutive_losses_) / total; | 118 } |
115 return true; | 119 |
120 // Returns the first-order-FEC recoverable packet loss rate, if the window | |
121 // has enough data to reliably compute it. | |
122 rtc::Optional<float> | |
123 TransportFeedbackPacketLossTracker::GetRecoverablePacketLossRate() const { | |
124 if (num_known_status_pairs_ < min_pairs_num_for_rplr_) | |
125 return rtc::Optional<float>(); | |
126 else | |
127 return rtc::Optional<float>( | |
128 static_cast<float>(num_loss_followed_by_reception_pairs_) / | |
129 num_known_status_pairs_); | |
116 } | 130 } |
117 | 131 |
118 void TransportFeedbackPacketLossTracker::InsertPacketStatus(uint16_t seq_num, | 132 void TransportFeedbackPacketLossTracker::InsertPacketStatus(uint16_t seq_num, |
119 bool received) { | 133 bool received) { |
120 const auto& ret = | 134 const auto& ret = |
121 packet_status_window_.insert(std::make_pair(seq_num, received)); | 135 packet_status_window_.insert(std::make_pair(seq_num, received)); |
122 if (!ret.second) { | 136 if (!ret.second) { |
123 if (!ret.first->second && received) { | 137 if (!ret.first->second && received) { |
124 // If older status said that the packet was lost but newer one says it | 138 // If older status said that the packet was lost but newer one says it |
125 // is received, we take the newer one. | 139 // is received, we take the newer one. |
(...skipping 13 matching lines...) Expand all Loading... | |
139 void TransportFeedbackPacketLossTracker::RemoveOldestPacketStatus() { | 153 void TransportFeedbackPacketLossTracker::RemoveOldestPacketStatus() { |
140 UndoPacketStatus(ref_packet_status_); | 154 UndoPacketStatus(ref_packet_status_); |
141 const auto it = ref_packet_status_; | 155 const auto it = ref_packet_status_; |
142 ref_packet_status_ = NextPacketStatus(it); | 156 ref_packet_status_ = NextPacketStatus(it); |
143 packet_status_window_.erase(it); | 157 packet_status_window_.erase(it); |
144 } | 158 } |
145 | 159 |
146 void TransportFeedbackPacketLossTracker::ApplyPacketStatus( | 160 void TransportFeedbackPacketLossTracker::ApplyPacketStatus( |
147 PacketStatusIterator it) { | 161 PacketStatusIterator it) { |
148 RTC_DCHECK(it != packet_status_window_.end()); | 162 RTC_DCHECK(it != packet_status_window_.end()); |
163 | |
164 // Record recetion status of currently handled packet. | |
149 if (it->second) { | 165 if (it->second) { |
150 ++num_received_packets_; | 166 ++num_received_packets_; |
151 } else { | 167 } else { |
152 ++num_lost_packets_; | 168 ++num_lost_packets_; |
153 const auto& next = NextPacketStatus(it); | 169 } |
154 if (next != packet_status_window_.end() && | 170 |
155 next->first == static_cast<uint16_t>(it->first + 1) && !next->second) { | 171 // Previous packet and current packet might compose a pair. |
156 // Feedback shows that the next packet has been lost. Since this | 172 if (it != ref_packet_status_) { |
157 // packet is lost, we increase the consecutive loss counter. | 173 const auto& prev = PreviousPacketStatus(it); |
158 ++num_consecutive_losses_; | 174 if (prev->first == static_cast<uint16_t>(it->first - 1)) { |
175 ++num_known_status_pairs_; | |
176 if (!prev->second && it->second) { | |
177 ++num_loss_followed_by_reception_pairs_; | |
178 } | |
159 } | 179 } |
160 if (it != ref_packet_status_) { | 180 } |
161 const auto& pre = PreviousPacketStatus(it); | 181 |
162 if (pre->first == static_cast<uint16_t>(it->first - 1) && !pre->second) { | 182 // Current packet and next packet might compose a pair. |
163 // Feedback shows that the previous packet has been lost. Since this | 183 const auto& next = NextPacketStatus(it); |
164 // packet is lost, we increase the consecutive loss counter. | 184 if (next != packet_status_window_.end() && |
165 ++num_consecutive_losses_; | 185 next->first == static_cast<uint16_t>(it->first + 1)) { |
166 } | 186 ++num_known_status_pairs_; |
187 if (!it->second && next->second) { | |
188 ++num_loss_followed_by_reception_pairs_; | |
167 } | 189 } |
168 } | 190 } |
169 } | 191 } |
170 | 192 |
171 void TransportFeedbackPacketLossTracker::UndoPacketStatus( | 193 void TransportFeedbackPacketLossTracker::UndoPacketStatus( |
172 PacketStatusIterator it) { | 194 PacketStatusIterator it) { |
173 RTC_DCHECK(it != packet_status_window_.end()); | 195 RTC_DCHECK(it != packet_status_window_.end()); |
196 | |
197 // Undo recetion status of currently handled packet. | |
174 if (it->second) { | 198 if (it->second) { |
175 RTC_DCHECK_GT(num_received_packets_, 0u); | |
176 --num_received_packets_; | 199 --num_received_packets_; |
177 } else { | 200 } else { |
178 RTC_DCHECK_GT(num_lost_packets_, 0u); | |
179 --num_lost_packets_; | 201 --num_lost_packets_; |
180 const auto& next = NextPacketStatus(it); | 202 } |
181 if (next != packet_status_window_.end() && | 203 |
182 next->first == static_cast<uint16_t>(it->first + 1) && !next->second) { | 204 // Previous packet and current packet might have composed a pair. |
183 RTC_DCHECK_GT(num_consecutive_losses_, 0u); | 205 if (it != ref_packet_status_) { |
184 --num_consecutive_losses_; | 206 const auto& prev = PreviousPacketStatus(it); |
207 if (prev->first == static_cast<uint16_t>(it->first - 1)) { | |
208 --num_known_status_pairs_; | |
209 if (!prev->second && it->second) { | |
210 --num_loss_followed_by_reception_pairs_; | |
211 } | |
185 } | 212 } |
186 if (it != ref_packet_status_) { | 213 } |
187 const auto& pre = PreviousPacketStatus(it); | 214 |
188 if (pre->first == static_cast<uint16_t>(it->first - 1) && !pre->second) { | 215 // Current packet and next packet might have composed a pair. |
189 RTC_DCHECK_GT(num_consecutive_losses_, 0u); | 216 const auto& next = NextPacketStatus(it); |
190 --num_consecutive_losses_; | 217 if (next != packet_status_window_.end() && |
191 } | 218 next->first == static_cast<uint16_t>(it->first + 1)) { |
219 --num_known_status_pairs_; | |
220 if (!it->second && next->second) { | |
221 --num_loss_followed_by_reception_pairs_; | |
192 } | 222 } |
193 } | 223 } |
194 } | 224 } |
195 | 225 |
196 TransportFeedbackPacketLossTracker::PacketStatusIterator | 226 TransportFeedbackPacketLossTracker::PacketStatusIterator |
197 TransportFeedbackPacketLossTracker::PreviousPacketStatus( | 227 TransportFeedbackPacketLossTracker::PreviousPacketStatus( |
198 PacketStatusIterator it) { | 228 PacketStatusIterator it) { |
199 RTC_DCHECK(it != ref_packet_status_); | 229 RTC_DCHECK(it != ref_packet_status_); |
200 if (it == packet_status_window_.end()) { | 230 if (it == packet_status_window_.end()) { |
201 // This is to make PreviousPacketStatus(packet_status_window_.end()) point | 231 // This is to make PreviousPacketStatus(packet_status_window_.end()) point |
(...skipping 26 matching lines...) Expand all Loading... | |
228 return it; | 258 return it; |
229 } | 259 } |
230 | 260 |
231 // TODO(minyue): This method checks the states of this class do not misbehave. | 261 // TODO(minyue): This method checks the states of this class do not misbehave. |
232 // The method is used both in unit tests and a fuzzer test. The fuzzer test | 262 // The method is used both in unit tests and a fuzzer test. The fuzzer test |
233 // is present to help finding potential errors. Once the fuzzer test shows no | 263 // is present to help finding potential errors. Once the fuzzer test shows no |
234 // error after long period, we can remove the fuzzer test, and move this method | 264 // error after long period, we can remove the fuzzer test, and move this method |
235 // to unit test. | 265 // to unit test. |
236 void TransportFeedbackPacketLossTracker::Validate() const { // Testing only! | 266 void TransportFeedbackPacketLossTracker::Validate() const { // Testing only! |
237 RTC_CHECK_LE(packet_status_window_.size(), max_window_size_); | 267 RTC_CHECK_LE(packet_status_window_.size(), max_window_size_); |
238 RTC_CHECK_GE(num_lost_packets_, num_consecutive_losses_); | |
239 RTC_CHECK_EQ(packet_status_window_.size(), | 268 RTC_CHECK_EQ(packet_status_window_.size(), |
240 num_lost_packets_ + num_received_packets_); | 269 num_lost_packets_ + num_received_packets_); |
270 RTC_CHECK_LE(num_loss_followed_by_reception_pairs_, num_known_status_pairs_); | |
271 RTC_CHECK_LE(num_known_status_pairs_, packet_status_window_.size() - 1); | |
241 | 272 |
242 size_t received_packets = 0; | 273 size_t received_packets = 0; |
243 size_t lost_packets = 0; | 274 size_t lost_packets = 0; |
244 size_t consecutive_losses = 0; | 275 size_t known_status_pairs = 0; |
276 size_t loss_followed_by_reception_pairs = 0; | |
245 | 277 |
246 if (!packet_status_window_.empty()) { | 278 if (!packet_status_window_.empty()) { |
247 PacketStatusIterator it = ref_packet_status_; | 279 PacketStatusIterator it = ref_packet_status_; |
248 bool pre_lost = false; | |
249 uint16_t pre_seq_num = it->first - 1; | |
250 do { | 280 do { |
251 if (it->second) { | 281 if (it->second) { |
252 ++received_packets; | 282 ++received_packets; |
253 } else { | 283 } else { |
254 ++lost_packets; | 284 ++lost_packets; |
255 if (pre_lost && pre_seq_num == static_cast<uint16_t>(it->first - 1)) | 285 } |
256 ++consecutive_losses; | 286 |
287 auto next = std::next(it); | |
288 if (next == packet_status_window_.end()) | |
289 next = packet_status_window_.begin(); | |
290 | |
291 if (next != ref_packet_status_ && | |
292 static_cast<uint16_t>(it->first + 1) == next->first) { | |
293 ++known_status_pairs; | |
294 if (!it->second && next->second) | |
295 ++loss_followed_by_reception_pairs; | |
257 } | 296 } |
258 | 297 |
259 RTC_CHECK_LT(ForwardDiff(ReferenceSequenceNumber(), it->first), | 298 RTC_CHECK_LT(ForwardDiff(ReferenceSequenceNumber(), it->first), |
260 kSeqNumHalf); | 299 kSeqNumHalf); |
261 | 300 |
262 pre_lost = !it->second; | 301 it = next; |
263 pre_seq_num = it->first; | |
264 | |
265 ++it; | |
266 if (it == packet_status_window_.end()) | |
267 it = packet_status_window_.begin(); | |
268 } while (it != ref_packet_status_); | 302 } while (it != ref_packet_status_); |
269 } | 303 } |
270 | 304 |
271 RTC_CHECK_EQ(num_received_packets_, received_packets); | 305 RTC_CHECK_EQ(num_received_packets_, received_packets); |
272 RTC_CHECK_EQ(num_lost_packets_, lost_packets); | 306 RTC_CHECK_EQ(num_lost_packets_, lost_packets); |
273 RTC_CHECK_EQ(num_consecutive_losses_, consecutive_losses); | 307 RTC_CHECK_EQ(num_known_status_pairs_, known_status_pairs); |
308 RTC_CHECK_EQ(num_loss_followed_by_reception_pairs_, | |
309 loss_followed_by_reception_pairs); | |
274 } | 310 } |
275 | 311 |
276 } // namespace webrtc | 312 } // namespace webrtc |
OLD | NEW |