blob: 101b6b48817c17a1c7962501d3e91e4361fa03fb [file] [log] [blame]
minyue435ddf92017-01-23 08:07:05 -08001/*
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 Solenberga8b7c7f2018-01-17 11:18:31 +010011#include "audio/transport_feedback_packet_loss_tracker.h"
minyue435ddf92017-01-23 08:07:05 -080012
13#include <limits>
14#include <utility>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
17#include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
18#include "rtc_base/checks.h"
Karl Wiberg65c39222017-11-22 12:25:14 +010019#include "rtc_base/numerics/mod_ops.h"
minyue435ddf92017-01-23 08:07:05 -080020
21namespace {
22constexpr uint16_t kSeqNumHalf = 0x8000u;
elad.alond83b9672017-02-01 08:36:09 -080023void 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}
minyue435ddf92017-01-23 08:07:05 -080032} // namespace
33
34namespace webrtc {
35
36TransportFeedbackPacketLossTracker::TransportFeedbackPacketLossTracker(
elad.alon3f9b12d2017-03-15 07:38:13 -070037 int64_t max_window_size_ms,
elad.alon7af93572017-03-03 10:51:35 -080038 size_t plr_min_num_acked_packets,
39 size_t rplr_min_num_acked_pairs)
elad.alon3f9b12d2017-03-15 07:38:13 -070040 : max_window_size_ms_(max_window_size_ms),
elad.alond83b9672017-02-01 08:36:09 -080041 ref_packet_status_(packet_status_window_.begin()),
elad.alon7af93572017-03-03 10:51:35 -080042 plr_state_(plr_min_num_acked_packets),
43 rplr_state_(rplr_min_num_acked_pairs) {
elad.alon3f9b12d2017-03-15 07:38:13 -070044 RTC_DCHECK_GT(max_window_size_ms, 0);
elad.alon7af93572017-03-03 10:51:35 -080045 RTC_DCHECK_GT(plr_min_num_acked_packets, 0);
elad.alon7af93572017-03-03 10:51:35 -080046 RTC_DCHECK_GT(rplr_min_num_acked_pairs, 0);
minyue435ddf92017-01-23 08:07:05 -080047 Reset();
48}
49
50void TransportFeedbackPacketLossTracker::Reset() {
elad.alon7af93572017-03-03 10:51:35 -080051 acked_packets_ = 0;
elad.alond83b9672017-02-01 08:36:09 -080052 plr_state_.Reset();
53 rplr_state_.Reset();
minyue435ddf92017-01-23 08:07:05 -080054 packet_status_window_.clear();
55 ref_packet_status_ = packet_status_window_.begin();
56}
57
58uint16_t TransportFeedbackPacketLossTracker::ReferenceSequenceNumber() const {
59 RTC_DCHECK(!packet_status_window_.empty());
60 return ref_packet_status_->first;
61}
62
elad.alon7af93572017-03-03 10:51:35 -080063uint16_t TransportFeedbackPacketLossTracker::NewestSequenceNumber() const {
64 RTC_DCHECK(!packet_status_window_.empty());
65 return PreviousPacketStatus(packet_status_window_.end())->first;
66}
67
elad.alon3f9b12d2017-03-15 07:38:13 -070068void TransportFeedbackPacketLossTracker::OnPacketAdded(uint16_t seq_num,
69 int64_t send_time_ms) {
70 // Sanity - time can't flow backwards.
71 RTC_DCHECK(
72 packet_status_window_.empty() ||
73 PreviousPacketStatus(packet_status_window_.end())->second.send_time_ms <=
74 send_time_ms);
75
elad.alon7af93572017-03-03 10:51:35 -080076 if (packet_status_window_.find(seq_num) != packet_status_window_.end() ||
77 (!packet_status_window_.empty() &&
78 ForwardDiff(seq_num, NewestSequenceNumber()) <= kSeqNumHalf)) {
79 // The only way for these two to happen is when the stream lies dormant for
80 // long enough for the sequence numbers to wrap. Everything in the window in
81 // such a case would be too old to use.
82 Reset();
minyue435ddf92017-01-23 08:07:05 -080083 }
elad.alon7af93572017-03-03 10:51:35 -080084
elad.alon3f9b12d2017-03-15 07:38:13 -070085 // Maintain a window where the newest sequence number is at most 0x7fff away
86 // from the oldest, so that would could still distinguish old/new.
elad.alon7af93572017-03-03 10:51:35 -080087 while (!packet_status_window_.empty() &&
88 ForwardDiff(ref_packet_status_->first, seq_num) >= kSeqNumHalf) {
89 RemoveOldestPacketStatus();
90 }
91
elad.alon3f9b12d2017-03-15 07:38:13 -070092 SentPacket sent_packet(send_time_ms, PacketStatus::Unacked);
elad.alon7af93572017-03-03 10:51:35 -080093 packet_status_window_.insert(packet_status_window_.end(),
elad.alon3f9b12d2017-03-15 07:38:13 -070094 std::make_pair(seq_num, sent_packet));
elad.alon7af93572017-03-03 10:51:35 -080095
96 if (packet_status_window_.size() == 1) {
97 ref_packet_status_ = packet_status_window_.cbegin();
98 }
minyue435ddf92017-01-23 08:07:05 -080099}
100
elad.alond12a8e12017-03-23 11:04:48 -0700101void TransportFeedbackPacketLossTracker::OnPacketFeedbackVector(
elad.alon92e448d2017-03-21 07:31:35 -0700102 const std::vector<PacketFeedback>& packet_feedback_vector) {
103 for (const PacketFeedback& packet : packet_feedback_vector) {
104 const auto& it = packet_status_window_.find(packet.sequence_number);
minyue435ddf92017-01-23 08:07:05 -0800105
elad.alon7af93572017-03-03 10:51:35 -0800106 // Packets which aren't at least marked as unacked either do not belong to
107 // this media stream, or have been shifted out of window.
elad.alon3f9b12d2017-03-15 07:38:13 -0700108 if (it == packet_status_window_.end())
109 continue;
110
elad.alon92e448d2017-03-21 07:31:35 -0700111 const bool lost = packet.arrival_time_ms == PacketFeedback::kNotReceived;
elad.alon3f9b12d2017-03-15 07:38:13 -0700112 const PacketStatus packet_status =
113 lost ? PacketStatus::Lost : PacketStatus::Received;
114
115 UpdatePacketStatus(it, packet_status);
minyue435ddf92017-01-23 08:07:05 -0800116 }
117}
118
elad.alond83b9672017-02-01 08:36:09 -0800119rtc::Optional<float>
120TransportFeedbackPacketLossTracker::GetPacketLossRate() const {
121 return plr_state_.GetMetric();
122}
123
124rtc::Optional<float>
125TransportFeedbackPacketLossTracker::GetRecoverablePacketLossRate() const {
126 return rplr_state_.GetMetric();
minyue435ddf92017-01-23 08:07:05 -0800127}
128
elad.alon3f9b12d2017-03-15 07:38:13 -0700129void TransportFeedbackPacketLossTracker::UpdatePacketStatus(
130 SentPacketStatusMap::iterator it,
131 PacketStatus new_status) {
132 if (it->second.status != PacketStatus::Unacked) {
elad.alon7af93572017-03-03 10:51:35 -0800133 // Normally, packets are sent (inserted into window as "unacked"), then we
134 // receive one feedback for them.
135 // But it is possible that a packet would receive two feedbacks. Then:
elad.alon3f9b12d2017-03-15 07:38:13 -0700136 if (it->second.status == PacketStatus::Lost &&
137 new_status == PacketStatus::Received) {
minyue435ddf92017-01-23 08:07:05 -0800138 // If older status said that the packet was lost but newer one says it
139 // is received, we take the newer one.
elad.alon7af93572017-03-03 10:51:35 -0800140 UpdateMetrics(it, false);
elad.alon3f9b12d2017-03-15 07:38:13 -0700141 it->second.status =
142 PacketStatus::Unacked; // For clarity; overwritten shortly.
minyue435ddf92017-01-23 08:07:05 -0800143 } else {
144 // If the value is unchanged or if older status said that the packet was
145 // received but the newer one says it is lost, we ignore it.
elad.alon7af93572017-03-03 10:51:35 -0800146 // The standard allows for previously-reported packets to carry
147 // no report when the reports overlap, which also looks like the
148 // packet is being reported as lost.
minyue435ddf92017-01-23 08:07:05 -0800149 return;
150 }
151 }
elad.alon7af93572017-03-03 10:51:35 -0800152
153 // Change from UNACKED to RECEIVED/LOST.
elad.alon3f9b12d2017-03-15 07:38:13 -0700154 it->second.status = new_status;
elad.alon7af93572017-03-03 10:51:35 -0800155 UpdateMetrics(it, true);
156
elad.alon3f9b12d2017-03-15 07:38:13 -0700157 // Remove packets from the beginning of the window until we only hold packets,
158 // be they acked or unacked, which are not more than |max_window_size_ms|
159 // older from the newest packet. (If the packet we're now inserting into the
160 // window isn't the newest, it would not trigger any removals; the newest
161 // already removed all relevant.)
162 while (ref_packet_status_ != packet_status_window_.end() &&
163 (it->second.send_time_ms - ref_packet_status_->second.send_time_ms) >
164 max_window_size_ms_) {
elad.alon7af93572017-03-03 10:51:35 -0800165 RemoveOldestPacketStatus();
elad.alon3f9b12d2017-03-15 07:38:13 -0700166 }
minyue435ddf92017-01-23 08:07:05 -0800167}
168
169void TransportFeedbackPacketLossTracker::RemoveOldestPacketStatus() {
elad.alond83b9672017-02-01 08:36:09 -0800170 UpdateMetrics(ref_packet_status_, false);
minyue435ddf92017-01-23 08:07:05 -0800171 const auto it = ref_packet_status_;
172 ref_packet_status_ = NextPacketStatus(it);
173 packet_status_window_.erase(it);
174}
175
elad.alond83b9672017-02-01 08:36:09 -0800176void TransportFeedbackPacketLossTracker::UpdateMetrics(
elad.alon7af93572017-03-03 10:51:35 -0800177 ConstPacketStatusIterator it,
elad.alond83b9672017-02-01 08:36:09 -0800178 bool apply /* false = undo */) {
minyue435ddf92017-01-23 08:07:05 -0800179 RTC_DCHECK(it != packet_status_window_.end());
elad.alon7af93572017-03-03 10:51:35 -0800180 // Metrics are dependent on feedbacks from the other side. We don't want
181 // to update the metrics each time a packet is sent, except for the case
182 // when it shifts old sent-but-unacked-packets out of window.
elad.alon3f9b12d2017-03-15 07:38:13 -0700183 RTC_DCHECK(!apply || it->second.status != PacketStatus::Unacked);
elad.alon7af93572017-03-03 10:51:35 -0800184
elad.alon3f9b12d2017-03-15 07:38:13 -0700185 if (it->second.status != PacketStatus::Unacked) {
elad.alon7af93572017-03-03 10:51:35 -0800186 UpdateCounter(&acked_packets_, apply);
187 }
188
elad.alond83b9672017-02-01 08:36:09 -0800189 UpdatePlr(it, apply);
190 UpdateRplr(it, apply);
191}
192
193void TransportFeedbackPacketLossTracker::UpdatePlr(
elad.alon7af93572017-03-03 10:51:35 -0800194 ConstPacketStatusIterator it,
elad.alond83b9672017-02-01 08:36:09 -0800195 bool apply /* false = undo */) {
elad.alon3f9b12d2017-03-15 07:38:13 -0700196 switch (it->second.status) {
elad.alon7af93572017-03-03 10:51:35 -0800197 case PacketStatus::Unacked:
198 return;
199 case PacketStatus::Received:
200 UpdateCounter(&plr_state_.num_received_packets_, apply);
201 break;
202 case PacketStatus::Lost:
203 UpdateCounter(&plr_state_.num_lost_packets_, apply);
204 break;
205 default:
206 RTC_NOTREACHED();
minyue435ddf92017-01-23 08:07:05 -0800207 }
208}
209
elad.alond83b9672017-02-01 08:36:09 -0800210void TransportFeedbackPacketLossTracker::UpdateRplr(
elad.alon7af93572017-03-03 10:51:35 -0800211 ConstPacketStatusIterator it,
elad.alond83b9672017-02-01 08:36:09 -0800212 bool apply /* false = undo */) {
elad.alon3f9b12d2017-03-15 07:38:13 -0700213 if (it->second.status == PacketStatus::Unacked) {
elad.alon7af93572017-03-03 10:51:35 -0800214 // Unacked packets cannot compose a pair.
215 return;
216 }
217
218 // Previous packet and current packet might compose a pair.
elad.alond83b9672017-02-01 08:36:09 -0800219 if (it != ref_packet_status_) {
220 const auto& prev = PreviousPacketStatus(it);
elad.alon3f9b12d2017-03-15 07:38:13 -0700221 if (prev->second.status != PacketStatus::Unacked) {
elad.alon7af93572017-03-03 10:51:35 -0800222 UpdateCounter(&rplr_state_.num_acked_pairs_, apply);
elad.alon3f9b12d2017-03-15 07:38:13 -0700223 if (prev->second.status == PacketStatus::Lost &&
224 it->second.status == PacketStatus::Received) {
elad.alond83b9672017-02-01 08:36:09 -0800225 UpdateCounter(
226 &rplr_state_.num_recoverable_losses_, apply);
minyue435ddf92017-01-23 08:07:05 -0800227 }
228 }
229 }
elad.alond83b9672017-02-01 08:36:09 -0800230
231 // Current packet and next packet might compose a pair.
elad.alond83b9672017-02-01 08:36:09 -0800232 const auto& next = NextPacketStatus(it);
233 if (next != packet_status_window_.end() &&
elad.alon3f9b12d2017-03-15 07:38:13 -0700234 next->second.status != PacketStatus::Unacked) {
elad.alon7af93572017-03-03 10:51:35 -0800235 UpdateCounter(&rplr_state_.num_acked_pairs_, apply);
elad.alon3f9b12d2017-03-15 07:38:13 -0700236 if (it->second.status == PacketStatus::Lost &&
237 next->second.status == PacketStatus::Received) {
elad.alond83b9672017-02-01 08:36:09 -0800238 UpdateCounter(&rplr_state_.num_recoverable_losses_, apply);
239 }
240 }
minyue435ddf92017-01-23 08:07:05 -0800241}
242
elad.alon7af93572017-03-03 10:51:35 -0800243TransportFeedbackPacketLossTracker::ConstPacketStatusIterator
minyue435ddf92017-01-23 08:07:05 -0800244TransportFeedbackPacketLossTracker::PreviousPacketStatus(
elad.alon7af93572017-03-03 10:51:35 -0800245 ConstPacketStatusIterator it) const {
minyue435ddf92017-01-23 08:07:05 -0800246 RTC_DCHECK(it != ref_packet_status_);
247 if (it == packet_status_window_.end()) {
248 // This is to make PreviousPacketStatus(packet_status_window_.end()) point
249 // to the last element.
250 it = ref_packet_status_;
251 }
252
253 if (it == packet_status_window_.begin()) {
254 // Due to the circular nature of sequence numbers, we let the iterator
255 // go to the end.
256 it = packet_status_window_.end();
257 }
258 return --it;
259}
260
elad.alon7af93572017-03-03 10:51:35 -0800261TransportFeedbackPacketLossTracker::ConstPacketStatusIterator
262TransportFeedbackPacketLossTracker::NextPacketStatus(
263 ConstPacketStatusIterator it) const {
minyue435ddf92017-01-23 08:07:05 -0800264 RTC_DCHECK(it != packet_status_window_.end());
265 ++it;
266 if (it == packet_status_window_.end()) {
267 // Due to the circular nature of sequence numbers, we let the iterator
268 // goes back to the beginning.
269 it = packet_status_window_.begin();
270 }
271 if (it == ref_packet_status_) {
272 // This is to make the NextPacketStatus of the last element to return the
273 // beyond-the-end iterator.
274 it = packet_status_window_.end();
275 }
276 return it;
277}
278
279// TODO(minyue): This method checks the states of this class do not misbehave.
280// The method is used both in unit tests and a fuzzer test. The fuzzer test
281// is present to help finding potential errors. Once the fuzzer test shows no
282// error after long period, we can remove the fuzzer test, and move this method
283// to unit test.
284void TransportFeedbackPacketLossTracker::Validate() const { // Testing only!
elad.alon7af93572017-03-03 10:51:35 -0800285 RTC_CHECK_EQ(plr_state_.num_received_packets_ + plr_state_.num_lost_packets_,
286 acked_packets_);
287 RTC_CHECK_LE(acked_packets_, packet_status_window_.size());
elad.alond83b9672017-02-01 08:36:09 -0800288 RTC_CHECK_LE(rplr_state_.num_recoverable_losses_,
elad.alon7af93572017-03-03 10:51:35 -0800289 rplr_state_.num_acked_pairs_);
290 RTC_CHECK_LE(rplr_state_.num_acked_pairs_, acked_packets_ - 1);
minyue435ddf92017-01-23 08:07:05 -0800291
elad.alon7af93572017-03-03 10:51:35 -0800292 size_t unacked_packets = 0;
minyue435ddf92017-01-23 08:07:05 -0800293 size_t received_packets = 0;
294 size_t lost_packets = 0;
elad.alon7af93572017-03-03 10:51:35 -0800295 size_t acked_pairs = 0;
elad.alond83b9672017-02-01 08:36:09 -0800296 size_t recoverable_losses = 0;
minyue435ddf92017-01-23 08:07:05 -0800297
298 if (!packet_status_window_.empty()) {
elad.alon7af93572017-03-03 10:51:35 -0800299 ConstPacketStatusIterator it = ref_packet_status_;
minyue435ddf92017-01-23 08:07:05 -0800300 do {
elad.alon3f9b12d2017-03-15 07:38:13 -0700301 switch (it->second.status) {
elad.alon7af93572017-03-03 10:51:35 -0800302 case PacketStatus::Unacked:
303 ++unacked_packets;
304 break;
305 case PacketStatus::Received:
306 ++received_packets;
307 break;
308 case PacketStatus::Lost:
309 ++lost_packets;
310 break;
311 default:
312 RTC_NOTREACHED();
elad.alond83b9672017-02-01 08:36:09 -0800313 }
314
315 auto next = std::next(it);
316 if (next == packet_status_window_.end())
317 next = packet_status_window_.begin();
318
elad.alon3f9b12d2017-03-15 07:38:13 -0700319 if (next != ref_packet_status_) { // If we have a next packet...
320 RTC_CHECK_GE(next->second.send_time_ms, it->second.send_time_ms);
321
322 if (it->second.status != PacketStatus::Unacked &&
323 next->second.status != PacketStatus::Unacked) {
324 ++acked_pairs;
325 if (it->second.status == PacketStatus::Lost &&
326 next->second.status == PacketStatus::Received) {
327 ++recoverable_losses;
328 }
329 }
minyue435ddf92017-01-23 08:07:05 -0800330 }
331
332 RTC_CHECK_LT(ForwardDiff(ReferenceSequenceNumber(), it->first),
333 kSeqNumHalf);
334
elad.alond83b9672017-02-01 08:36:09 -0800335 it = next;
minyue435ddf92017-01-23 08:07:05 -0800336 } while (it != ref_packet_status_);
337 }
338
elad.alond83b9672017-02-01 08:36:09 -0800339 RTC_CHECK_EQ(plr_state_.num_received_packets_, received_packets);
340 RTC_CHECK_EQ(plr_state_.num_lost_packets_, lost_packets);
elad.alon7af93572017-03-03 10:51:35 -0800341 RTC_CHECK_EQ(packet_status_window_.size(),
342 unacked_packets + received_packets + lost_packets);
343 RTC_CHECK_EQ(rplr_state_.num_acked_pairs_, acked_pairs);
elad.alond83b9672017-02-01 08:36:09 -0800344 RTC_CHECK_EQ(rplr_state_.num_recoverable_losses_, recoverable_losses);
345}
346
347rtc::Optional<float>
348TransportFeedbackPacketLossTracker::PlrState::GetMetric() const {
349 const size_t total = num_lost_packets_ + num_received_packets_;
elad.alon7af93572017-03-03 10:51:35 -0800350 if (total < min_num_acked_packets_) {
Oskar Sundbom606c8822017-11-16 10:57:11 +0100351 return rtc::nullopt;
elad.alond83b9672017-02-01 08:36:09 -0800352 } else {
Oskar Sundbom606c8822017-11-16 10:57:11 +0100353 return static_cast<float>(num_lost_packets_) / total;
elad.alond83b9672017-02-01 08:36:09 -0800354 }
355}
356
357rtc::Optional<float>
358TransportFeedbackPacketLossTracker::RplrState::GetMetric() const {
elad.alon7af93572017-03-03 10:51:35 -0800359 if (num_acked_pairs_ < min_num_acked_pairs_) {
Oskar Sundbom606c8822017-11-16 10:57:11 +0100360 return rtc::nullopt;
elad.alond83b9672017-02-01 08:36:09 -0800361 } else {
Oskar Sundbom606c8822017-11-16 10:57:11 +0100362 return static_cast<float>(num_recoverable_losses_) / num_acked_pairs_;
elad.alond83b9672017-02-01 08:36:09 -0800363 }
minyue435ddf92017-01-23 08:07:05 -0800364}
365
366} // namespace webrtc