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 "webrtc/voice_engine/transport_feedback_packet_loss_tracker.h" |
| 12 |
| 13 #include <limits> |
| 14 #include <utility> |
| 15 |
| 16 #include "webrtc/base/checks.h" |
| 17 #include "webrtc/base/mod_ops.h" |
| 18 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h" |
| 19 |
| 20 namespace { |
| 21 constexpr uint16_t kSeqNumHalf = 0x8000u; |
| 22 constexpr uint16_t kSeqNumQuarter = kSeqNumHalf / 2; |
| 23 constexpr size_t kMaxConsecutiveOldReports = 4; |
| 24 } // namespace |
| 25 |
| 26 namespace webrtc { |
| 27 |
| 28 TransportFeedbackPacketLossTracker::TransportFeedbackPacketLossTracker( |
| 29 size_t min_window_size, |
| 30 size_t max_window_size) |
| 31 : min_window_size_(min_window_size), |
| 32 max_window_size_(max_window_size), |
| 33 ref_packet_status_(packet_status_window_.begin()) { |
| 34 RTC_DCHECK_GT(min_window_size, 0u); |
| 35 RTC_DCHECK_GE(max_window_size_, min_window_size_); |
| 36 RTC_DCHECK_LE(max_window_size_, kSeqNumHalf); |
| 37 Reset(); |
| 38 } |
| 39 |
| 40 void TransportFeedbackPacketLossTracker::Reset() { |
| 41 num_received_packets_ = 0; |
| 42 num_lost_packets_ = 0; |
| 43 num_consecutive_losses_ = 0; |
| 44 num_consecutive_old_reports_ = 0; |
| 45 packet_status_window_.clear(); |
| 46 ref_packet_status_ = packet_status_window_.begin(); |
| 47 } |
| 48 |
| 49 uint16_t TransportFeedbackPacketLossTracker::ReferenceSequenceNumber() const { |
| 50 RTC_DCHECK(!packet_status_window_.empty()); |
| 51 return ref_packet_status_->first; |
| 52 } |
| 53 |
| 54 bool TransportFeedbackPacketLossTracker::IsOldSequenceNumber( |
| 55 uint16_t seq_num) const { |
| 56 if (packet_status_window_.empty()) { |
| 57 return false; |
| 58 } |
| 59 const uint16_t diff = ForwardDiff(ReferenceSequenceNumber(), seq_num); |
| 60 return diff >= 3 * kSeqNumQuarter; |
| 61 } |
| 62 |
| 63 void TransportFeedbackPacketLossTracker::OnReceivedTransportFeedback( |
| 64 const rtcp::TransportFeedbackInterface& feedback) { |
| 65 const auto& fb_vector = feedback.GetStatusVector(); |
| 66 const uint16_t base_seq_num = feedback.GetBaseSequence(); |
| 67 |
| 68 if (IsOldSequenceNumber(base_seq_num)) { |
| 69 ++num_consecutive_old_reports_; |
| 70 if (num_consecutive_old_reports_ <= kMaxConsecutiveOldReports) { |
| 71 // If the number consecutive old reports have not exceed a threshold, we |
| 72 // consider this packet as a late arrival. We could consider adding it to |
| 73 // |packet_status_window_|, but in current implementation, we simply |
| 74 // ignore it. |
| 75 return; |
| 76 } |
| 77 // If we see several consecutive older reports, we assume that we've not |
| 78 // received reports for an exceedingly long time, and do a reset. |
| 79 Reset(); |
| 80 RTC_DCHECK(!IsOldSequenceNumber(base_seq_num)); |
| 81 } else { |
| 82 num_consecutive_old_reports_ = 0; |
| 83 } |
| 84 |
| 85 uint16_t seq_num = base_seq_num; |
| 86 for (size_t i = 0; i < fb_vector.size(); ++i, ++seq_num) { |
| 87 // Remove the oldest feedbacks so that the distance between the oldest and |
| 88 // the packet to be added does not exceed or equal to half of total sequence |
| 89 // numbers. |
| 90 while (!packet_status_window_.empty() && |
| 91 ForwardDiff(ReferenceSequenceNumber(), seq_num) >= kSeqNumHalf) { |
| 92 RemoveOldestPacketStatus(); |
| 93 } |
| 94 |
| 95 const bool received = fb_vector[i] != |
| 96 webrtc::rtcp::TransportFeedback::StatusSymbol::kNotReceived; |
| 97 InsertPacketStatus(seq_num, received); |
| 98 |
| 99 while (packet_status_window_.size() > max_window_size_) { |
| 100 // Make sure that the window holds at most |max_window_size_| items. |
| 101 RemoveOldestPacketStatus(); |
| 102 } |
| 103 } |
| 104 } |
| 105 |
| 106 bool TransportFeedbackPacketLossTracker::GetPacketLossRates( |
| 107 float* packet_loss_rate, |
| 108 float* consecutive_packet_loss_rate) const { |
| 109 const size_t total = num_lost_packets_ + num_received_packets_; |
| 110 if (total < min_window_size_) |
| 111 return false; |
| 112 *packet_loss_rate = static_cast<float>(num_lost_packets_) / total; |
| 113 *consecutive_packet_loss_rate = |
| 114 static_cast<float>(num_consecutive_losses_) / total; |
| 115 return true; |
| 116 } |
| 117 |
| 118 void TransportFeedbackPacketLossTracker::InsertPacketStatus(uint16_t seq_num, |
| 119 bool received) { |
| 120 const auto& ret = |
| 121 packet_status_window_.insert(std::make_pair(seq_num, received)); |
| 122 if (!ret.second) { |
| 123 if (!ret.first->second && received) { |
| 124 // If older status said that the packet was lost but newer one says it |
| 125 // is received, we take the newer one. |
| 126 UndoPacketStatus(ret.first); |
| 127 ret.first->second = received; |
| 128 } else { |
| 129 // If the value is unchanged or if older status said that the packet was |
| 130 // received but the newer one says it is lost, we ignore it. |
| 131 return; |
| 132 } |
| 133 } |
| 134 ApplyPacketStatus(ret.first); |
| 135 if (packet_status_window_.size() == 1) |
| 136 ref_packet_status_ = ret.first; |
| 137 } |
| 138 |
| 139 void TransportFeedbackPacketLossTracker::RemoveOldestPacketStatus() { |
| 140 UndoPacketStatus(ref_packet_status_); |
| 141 const auto it = ref_packet_status_; |
| 142 ref_packet_status_ = NextPacketStatus(it); |
| 143 packet_status_window_.erase(it); |
| 144 } |
| 145 |
| 146 void TransportFeedbackPacketLossTracker::ApplyPacketStatus( |
| 147 PacketStatusIterator it) { |
| 148 RTC_DCHECK(it != packet_status_window_.end()); |
| 149 if (it->second) { |
| 150 ++num_received_packets_; |
| 151 } else { |
| 152 ++num_lost_packets_; |
| 153 const auto& next = NextPacketStatus(it); |
| 154 if (next != packet_status_window_.end() && |
| 155 next->first == static_cast<uint16_t>(it->first + 1) && !next->second) { |
| 156 // Feedback shows that the next packet has been lost. Since this |
| 157 // packet is lost, we increase the consecutive loss counter. |
| 158 ++num_consecutive_losses_; |
| 159 } |
| 160 if (it != ref_packet_status_) { |
| 161 const auto& pre = PreviousPacketStatus(it); |
| 162 if (pre->first == static_cast<uint16_t>(it->first - 1) && !pre->second) { |
| 163 // Feedback shows that the previous packet has been lost. Since this |
| 164 // packet is lost, we increase the consecutive loss counter. |
| 165 ++num_consecutive_losses_; |
| 166 } |
| 167 } |
| 168 } |
| 169 } |
| 170 |
| 171 void TransportFeedbackPacketLossTracker::UndoPacketStatus( |
| 172 PacketStatusIterator it) { |
| 173 RTC_DCHECK(it != packet_status_window_.end()); |
| 174 if (it->second) { |
| 175 RTC_DCHECK_GT(num_received_packets_, 0u); |
| 176 --num_received_packets_; |
| 177 } else { |
| 178 RTC_DCHECK_GT(num_lost_packets_, 0u); |
| 179 --num_lost_packets_; |
| 180 const auto& next = NextPacketStatus(it); |
| 181 if (next != packet_status_window_.end() && |
| 182 next->first == static_cast<uint16_t>(it->first + 1) && !next->second) { |
| 183 RTC_DCHECK_GT(num_consecutive_losses_, 0u); |
| 184 --num_consecutive_losses_; |
| 185 } |
| 186 if (it != ref_packet_status_) { |
| 187 const auto& pre = PreviousPacketStatus(it); |
| 188 if (pre->first == static_cast<uint16_t>(it->first - 1) && !pre->second) { |
| 189 RTC_DCHECK_GT(num_consecutive_losses_, 0u); |
| 190 --num_consecutive_losses_; |
| 191 } |
| 192 } |
| 193 } |
| 194 } |
| 195 |
| 196 TransportFeedbackPacketLossTracker::PacketStatusIterator |
| 197 TransportFeedbackPacketLossTracker::PreviousPacketStatus( |
| 198 PacketStatusIterator it) { |
| 199 RTC_DCHECK(it != ref_packet_status_); |
| 200 if (it == packet_status_window_.end()) { |
| 201 // This is to make PreviousPacketStatus(packet_status_window_.end()) point |
| 202 // to the last element. |
| 203 it = ref_packet_status_; |
| 204 } |
| 205 |
| 206 if (it == packet_status_window_.begin()) { |
| 207 // Due to the circular nature of sequence numbers, we let the iterator |
| 208 // go to the end. |
| 209 it = packet_status_window_.end(); |
| 210 } |
| 211 return --it; |
| 212 } |
| 213 |
| 214 TransportFeedbackPacketLossTracker::PacketStatusIterator |
| 215 TransportFeedbackPacketLossTracker::NextPacketStatus(PacketStatusIterator it) { |
| 216 RTC_DCHECK(it != packet_status_window_.end()); |
| 217 ++it; |
| 218 if (it == packet_status_window_.end()) { |
| 219 // Due to the circular nature of sequence numbers, we let the iterator |
| 220 // goes back to the beginning. |
| 221 it = packet_status_window_.begin(); |
| 222 } |
| 223 if (it == ref_packet_status_) { |
| 224 // This is to make the NextPacketStatus of the last element to return the |
| 225 // beyond-the-end iterator. |
| 226 it = packet_status_window_.end(); |
| 227 } |
| 228 return it; |
| 229 } |
| 230 |
| 231 // 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 |
| 233 // 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 |
| 235 // to unit test. |
| 236 void TransportFeedbackPacketLossTracker::Validate() const { // Testing only! |
| 237 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(), |
| 240 num_lost_packets_ + num_received_packets_); |
| 241 |
| 242 size_t received_packets = 0; |
| 243 size_t lost_packets = 0; |
| 244 size_t consecutive_losses = 0; |
| 245 |
| 246 if (!packet_status_window_.empty()) { |
| 247 PacketStatusIterator it = ref_packet_status_; |
| 248 bool pre_lost = false; |
| 249 uint16_t pre_seq_num = it->first - 1; |
| 250 do { |
| 251 if (it->second) { |
| 252 ++received_packets; |
| 253 } else { |
| 254 ++lost_packets; |
| 255 if (pre_lost && pre_seq_num == static_cast<uint16_t>(it->first - 1)) |
| 256 ++consecutive_losses; |
| 257 } |
| 258 |
| 259 RTC_CHECK_LT(ForwardDiff(ReferenceSequenceNumber(), it->first), |
| 260 kSeqNumHalf); |
| 261 |
| 262 pre_lost = !it->second; |
| 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_); |
| 269 } |
| 270 |
| 271 RTC_CHECK_EQ(num_received_packets_, received_packets); |
| 272 RTC_CHECK_EQ(num_lost_packets_, lost_packets); |
| 273 RTC_CHECK_EQ(num_consecutive_losses_, consecutive_losses); |
| 274 } |
| 275 |
| 276 } // namespace webrtc |
OLD | NEW |