minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | |
Fredrik Solenberg | a8b7c7f | 2018-01-17 11:18:31 +0100 | [diff] [blame] | 11 | #include "audio/transport_feedback_packet_loss_tracker.h" |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #include <iterator> |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 14 | #include <limits> |
| 15 | #include <utility> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
Karl Wiberg | 65c3922 | 2017-11-22 12:25:14 +0100 | [diff] [blame] | 19 | #include "rtc_base/numerics/mod_ops.h" |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 20 | |
| 21 | namespace { |
| 22 | constexpr uint16_t kSeqNumHalf = 0x8000u; |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 23 | void UpdateCounter(size_t* counter, bool increment) { |
| 24 | if (increment) { |
| 25 | RTC_DCHECK_LT(*counter, std::numeric_limits<std::size_t>::max()); |
| 26 | ++(*counter); |
| 27 | } else { |
| 28 | RTC_DCHECK_GT(*counter, 0); |
| 29 | --(*counter); |
| 30 | } |
| 31 | } |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 32 | } // namespace |
| 33 | |
| 34 | namespace webrtc { |
| 35 | |
| 36 | TransportFeedbackPacketLossTracker::TransportFeedbackPacketLossTracker( |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 37 | int64_t max_window_size_ms, |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 38 | size_t plr_min_num_acked_packets, |
| 39 | size_t rplr_min_num_acked_pairs) |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 40 | : max_window_size_ms_(max_window_size_ms), |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 41 | ref_packet_status_(packet_status_window_.begin()), |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 42 | plr_state_(plr_min_num_acked_packets), |
| 43 | rplr_state_(rplr_min_num_acked_pairs) { |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 44 | RTC_DCHECK_GT(max_window_size_ms, 0); |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 45 | RTC_DCHECK_GT(plr_min_num_acked_packets, 0); |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 46 | RTC_DCHECK_GT(rplr_min_num_acked_pairs, 0); |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 47 | Reset(); |
| 48 | } |
| 49 | |
Mirko Bonadei | 8fdcac3 | 2018-08-28 16:30:18 +0200 | [diff] [blame] | 50 | TransportFeedbackPacketLossTracker::~TransportFeedbackPacketLossTracker() = |
| 51 | default; |
| 52 | |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 53 | void TransportFeedbackPacketLossTracker::Reset() { |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 54 | acked_packets_ = 0; |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 55 | plr_state_.Reset(); |
| 56 | rplr_state_.Reset(); |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 57 | packet_status_window_.clear(); |
| 58 | ref_packet_status_ = packet_status_window_.begin(); |
| 59 | } |
| 60 | |
| 61 | uint16_t TransportFeedbackPacketLossTracker::ReferenceSequenceNumber() const { |
| 62 | RTC_DCHECK(!packet_status_window_.empty()); |
| 63 | return ref_packet_status_->first; |
| 64 | } |
| 65 | |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 66 | uint16_t TransportFeedbackPacketLossTracker::NewestSequenceNumber() const { |
| 67 | RTC_DCHECK(!packet_status_window_.empty()); |
| 68 | return PreviousPacketStatus(packet_status_window_.end())->first; |
| 69 | } |
| 70 | |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 71 | void TransportFeedbackPacketLossTracker::OnPacketAdded(uint16_t seq_num, |
| 72 | int64_t send_time_ms) { |
| 73 | // Sanity - time can't flow backwards. |
| 74 | RTC_DCHECK( |
| 75 | packet_status_window_.empty() || |
| 76 | PreviousPacketStatus(packet_status_window_.end())->second.send_time_ms <= |
| 77 | send_time_ms); |
| 78 | |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 79 | if (packet_status_window_.find(seq_num) != packet_status_window_.end() || |
| 80 | (!packet_status_window_.empty() && |
| 81 | ForwardDiff(seq_num, NewestSequenceNumber()) <= kSeqNumHalf)) { |
| 82 | // The only way for these two to happen is when the stream lies dormant for |
| 83 | // long enough for the sequence numbers to wrap. Everything in the window in |
| 84 | // such a case would be too old to use. |
| 85 | Reset(); |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 86 | } |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 87 | |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 88 | // Maintain a window where the newest sequence number is at most 0x7fff away |
| 89 | // from the oldest, so that would could still distinguish old/new. |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 90 | while (!packet_status_window_.empty() && |
| 91 | ForwardDiff(ref_packet_status_->first, seq_num) >= kSeqNumHalf) { |
| 92 | RemoveOldestPacketStatus(); |
| 93 | } |
| 94 | |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 95 | SentPacket sent_packet(send_time_ms, PacketStatus::Unacked); |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 96 | packet_status_window_.insert(packet_status_window_.end(), |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 97 | std::make_pair(seq_num, sent_packet)); |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 98 | |
| 99 | if (packet_status_window_.size() == 1) { |
| 100 | ref_packet_status_ = packet_status_window_.cbegin(); |
| 101 | } |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 102 | } |
| 103 | |
elad.alon | d12a8e1 | 2017-03-23 11:04:48 -0700 | [diff] [blame] | 104 | void TransportFeedbackPacketLossTracker::OnPacketFeedbackVector( |
elad.alon | 92e448d | 2017-03-21 07:31:35 -0700 | [diff] [blame] | 105 | const std::vector<PacketFeedback>& packet_feedback_vector) { |
| 106 | for (const PacketFeedback& packet : packet_feedback_vector) { |
| 107 | const auto& it = packet_status_window_.find(packet.sequence_number); |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 108 | |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 109 | // Packets which aren't at least marked as unacked either do not belong to |
| 110 | // this media stream, or have been shifted out of window. |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 111 | if (it == packet_status_window_.end()) |
| 112 | continue; |
| 113 | |
elad.alon | 92e448d | 2017-03-21 07:31:35 -0700 | [diff] [blame] | 114 | const bool lost = packet.arrival_time_ms == PacketFeedback::kNotReceived; |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 115 | const PacketStatus packet_status = |
| 116 | lost ? PacketStatus::Lost : PacketStatus::Received; |
| 117 | |
| 118 | UpdatePacketStatus(it, packet_status); |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 122 | absl::optional<float> TransportFeedbackPacketLossTracker::GetPacketLossRate() |
| 123 | const { |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 124 | return plr_state_.GetMetric(); |
| 125 | } |
| 126 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 127 | absl::optional<float> |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 128 | TransportFeedbackPacketLossTracker::GetRecoverablePacketLossRate() const { |
| 129 | return rplr_state_.GetMetric(); |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 130 | } |
| 131 | |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 132 | void TransportFeedbackPacketLossTracker::UpdatePacketStatus( |
| 133 | SentPacketStatusMap::iterator it, |
| 134 | PacketStatus new_status) { |
| 135 | if (it->second.status != PacketStatus::Unacked) { |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 136 | // Normally, packets are sent (inserted into window as "unacked"), then we |
| 137 | // receive one feedback for them. |
| 138 | // But it is possible that a packet would receive two feedbacks. Then: |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 139 | if (it->second.status == PacketStatus::Lost && |
| 140 | new_status == PacketStatus::Received) { |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 141 | // If older status said that the packet was lost but newer one says it |
| 142 | // is received, we take the newer one. |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 143 | UpdateMetrics(it, false); |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 144 | it->second.status = |
| 145 | PacketStatus::Unacked; // For clarity; overwritten shortly. |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 146 | } else { |
| 147 | // If the value is unchanged or if older status said that the packet was |
| 148 | // received but the newer one says it is lost, we ignore it. |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 149 | // The standard allows for previously-reported packets to carry |
| 150 | // no report when the reports overlap, which also looks like the |
| 151 | // packet is being reported as lost. |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | } |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 155 | |
| 156 | // Change from UNACKED to RECEIVED/LOST. |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 157 | it->second.status = new_status; |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 158 | UpdateMetrics(it, true); |
| 159 | |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 160 | // Remove packets from the beginning of the window until we only hold packets, |
| 161 | // be they acked or unacked, which are not more than |max_window_size_ms| |
| 162 | // older from the newest packet. (If the packet we're now inserting into the |
| 163 | // window isn't the newest, it would not trigger any removals; the newest |
| 164 | // already removed all relevant.) |
| 165 | while (ref_packet_status_ != packet_status_window_.end() && |
| 166 | (it->second.send_time_ms - ref_packet_status_->second.send_time_ms) > |
| 167 | max_window_size_ms_) { |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 168 | RemoveOldestPacketStatus(); |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 169 | } |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void TransportFeedbackPacketLossTracker::RemoveOldestPacketStatus() { |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 173 | UpdateMetrics(ref_packet_status_, false); |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 174 | const auto it = ref_packet_status_; |
| 175 | ref_packet_status_ = NextPacketStatus(it); |
| 176 | packet_status_window_.erase(it); |
| 177 | } |
| 178 | |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 179 | void TransportFeedbackPacketLossTracker::UpdateMetrics( |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 180 | ConstPacketStatusIterator it, |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 181 | bool apply /* false = undo */) { |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 182 | RTC_DCHECK(it != packet_status_window_.end()); |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 183 | // Metrics are dependent on feedbacks from the other side. We don't want |
| 184 | // to update the metrics each time a packet is sent, except for the case |
| 185 | // when it shifts old sent-but-unacked-packets out of window. |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 186 | RTC_DCHECK(!apply || it->second.status != PacketStatus::Unacked); |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 187 | |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 188 | if (it->second.status != PacketStatus::Unacked) { |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 189 | UpdateCounter(&acked_packets_, apply); |
| 190 | } |
| 191 | |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 192 | UpdatePlr(it, apply); |
| 193 | UpdateRplr(it, apply); |
| 194 | } |
| 195 | |
| 196 | void TransportFeedbackPacketLossTracker::UpdatePlr( |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 197 | ConstPacketStatusIterator it, |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 198 | bool apply /* false = undo */) { |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 199 | switch (it->second.status) { |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 200 | case PacketStatus::Unacked: |
| 201 | return; |
| 202 | case PacketStatus::Received: |
| 203 | UpdateCounter(&plr_state_.num_received_packets_, apply); |
| 204 | break; |
| 205 | case PacketStatus::Lost: |
| 206 | UpdateCounter(&plr_state_.num_lost_packets_, apply); |
| 207 | break; |
| 208 | default: |
| 209 | RTC_NOTREACHED(); |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 213 | void TransportFeedbackPacketLossTracker::UpdateRplr( |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 214 | ConstPacketStatusIterator it, |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 215 | bool apply /* false = undo */) { |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 216 | if (it->second.status == PacketStatus::Unacked) { |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 217 | // Unacked packets cannot compose a pair. |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | // Previous packet and current packet might compose a pair. |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 222 | if (it != ref_packet_status_) { |
| 223 | const auto& prev = PreviousPacketStatus(it); |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 224 | if (prev->second.status != PacketStatus::Unacked) { |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 225 | UpdateCounter(&rplr_state_.num_acked_pairs_, apply); |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 226 | if (prev->second.status == PacketStatus::Lost && |
| 227 | it->second.status == PacketStatus::Received) { |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 228 | UpdateCounter( |
| 229 | &rplr_state_.num_recoverable_losses_, apply); |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | } |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 233 | |
| 234 | // Current packet and next packet might compose a pair. |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 235 | const auto& next = NextPacketStatus(it); |
| 236 | if (next != packet_status_window_.end() && |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 237 | next->second.status != PacketStatus::Unacked) { |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 238 | UpdateCounter(&rplr_state_.num_acked_pairs_, apply); |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 239 | if (it->second.status == PacketStatus::Lost && |
| 240 | next->second.status == PacketStatus::Received) { |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 241 | UpdateCounter(&rplr_state_.num_recoverable_losses_, apply); |
| 242 | } |
| 243 | } |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 244 | } |
| 245 | |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 246 | TransportFeedbackPacketLossTracker::ConstPacketStatusIterator |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 247 | TransportFeedbackPacketLossTracker::PreviousPacketStatus( |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 248 | ConstPacketStatusIterator it) const { |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 249 | RTC_DCHECK(it != ref_packet_status_); |
| 250 | if (it == packet_status_window_.end()) { |
| 251 | // This is to make PreviousPacketStatus(packet_status_window_.end()) point |
| 252 | // to the last element. |
| 253 | it = ref_packet_status_; |
| 254 | } |
| 255 | |
| 256 | if (it == packet_status_window_.begin()) { |
| 257 | // Due to the circular nature of sequence numbers, we let the iterator |
| 258 | // go to the end. |
| 259 | it = packet_status_window_.end(); |
| 260 | } |
| 261 | return --it; |
| 262 | } |
| 263 | |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 264 | TransportFeedbackPacketLossTracker::ConstPacketStatusIterator |
| 265 | TransportFeedbackPacketLossTracker::NextPacketStatus( |
| 266 | ConstPacketStatusIterator it) const { |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 267 | RTC_DCHECK(it != packet_status_window_.end()); |
| 268 | ++it; |
| 269 | if (it == packet_status_window_.end()) { |
| 270 | // Due to the circular nature of sequence numbers, we let the iterator |
| 271 | // goes back to the beginning. |
| 272 | it = packet_status_window_.begin(); |
| 273 | } |
| 274 | if (it == ref_packet_status_) { |
| 275 | // This is to make the NextPacketStatus of the last element to return the |
| 276 | // beyond-the-end iterator. |
| 277 | it = packet_status_window_.end(); |
| 278 | } |
| 279 | return it; |
| 280 | } |
| 281 | |
| 282 | // TODO(minyue): This method checks the states of this class do not misbehave. |
| 283 | // The method is used both in unit tests and a fuzzer test. The fuzzer test |
| 284 | // is present to help finding potential errors. Once the fuzzer test shows no |
| 285 | // error after long period, we can remove the fuzzer test, and move this method |
| 286 | // to unit test. |
| 287 | void TransportFeedbackPacketLossTracker::Validate() const { // Testing only! |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 288 | RTC_CHECK_EQ(plr_state_.num_received_packets_ + plr_state_.num_lost_packets_, |
| 289 | acked_packets_); |
| 290 | RTC_CHECK_LE(acked_packets_, packet_status_window_.size()); |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 291 | RTC_CHECK_LE(rplr_state_.num_recoverable_losses_, |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 292 | rplr_state_.num_acked_pairs_); |
| 293 | RTC_CHECK_LE(rplr_state_.num_acked_pairs_, acked_packets_ - 1); |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 294 | |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 295 | size_t unacked_packets = 0; |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 296 | size_t received_packets = 0; |
| 297 | size_t lost_packets = 0; |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 298 | size_t acked_pairs = 0; |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 299 | size_t recoverable_losses = 0; |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 300 | |
| 301 | if (!packet_status_window_.empty()) { |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 302 | ConstPacketStatusIterator it = ref_packet_status_; |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 303 | do { |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 304 | switch (it->second.status) { |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 305 | case PacketStatus::Unacked: |
| 306 | ++unacked_packets; |
| 307 | break; |
| 308 | case PacketStatus::Received: |
| 309 | ++received_packets; |
| 310 | break; |
| 311 | case PacketStatus::Lost: |
| 312 | ++lost_packets; |
| 313 | break; |
| 314 | default: |
| 315 | RTC_NOTREACHED(); |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | auto next = std::next(it); |
| 319 | if (next == packet_status_window_.end()) |
| 320 | next = packet_status_window_.begin(); |
| 321 | |
elad.alon | 3f9b12d | 2017-03-15 07:38:13 -0700 | [diff] [blame] | 322 | if (next != ref_packet_status_) { // If we have a next packet... |
| 323 | RTC_CHECK_GE(next->second.send_time_ms, it->second.send_time_ms); |
| 324 | |
| 325 | if (it->second.status != PacketStatus::Unacked && |
| 326 | next->second.status != PacketStatus::Unacked) { |
| 327 | ++acked_pairs; |
| 328 | if (it->second.status == PacketStatus::Lost && |
| 329 | next->second.status == PacketStatus::Received) { |
| 330 | ++recoverable_losses; |
| 331 | } |
| 332 | } |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | RTC_CHECK_LT(ForwardDiff(ReferenceSequenceNumber(), it->first), |
| 336 | kSeqNumHalf); |
| 337 | |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 338 | it = next; |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 339 | } while (it != ref_packet_status_); |
| 340 | } |
| 341 | |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 342 | RTC_CHECK_EQ(plr_state_.num_received_packets_, received_packets); |
| 343 | RTC_CHECK_EQ(plr_state_.num_lost_packets_, lost_packets); |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 344 | RTC_CHECK_EQ(packet_status_window_.size(), |
| 345 | unacked_packets + received_packets + lost_packets); |
| 346 | RTC_CHECK_EQ(rplr_state_.num_acked_pairs_, acked_pairs); |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 347 | RTC_CHECK_EQ(rplr_state_.num_recoverable_losses_, recoverable_losses); |
| 348 | } |
| 349 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 350 | absl::optional<float> TransportFeedbackPacketLossTracker::PlrState::GetMetric() |
| 351 | const { |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 352 | const size_t total = num_lost_packets_ + num_received_packets_; |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 353 | if (total < min_num_acked_packets_) { |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 354 | return absl::nullopt; |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 355 | } else { |
Oskar Sundbom | 606c882 | 2017-11-16 10:57:11 +0100 | [diff] [blame] | 356 | return static_cast<float>(num_lost_packets_) / total; |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 360 | absl::optional<float> TransportFeedbackPacketLossTracker::RplrState::GetMetric() |
| 361 | const { |
elad.alon | 7af9357 | 2017-03-03 10:51:35 -0800 | [diff] [blame] | 362 | if (num_acked_pairs_ < min_num_acked_pairs_) { |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 363 | return absl::nullopt; |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 364 | } else { |
Oskar Sundbom | 606c882 | 2017-11-16 10:57:11 +0100 | [diff] [blame] | 365 | return static_cast<float>(num_recoverable_losses_) / num_acked_pairs_; |
elad.alon | d83b967 | 2017-02-01 08:36:09 -0800 | [diff] [blame] | 366 | } |
minyue | 435ddf9 | 2017-01-23 08:07:05 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | } // namespace webrtc |