blob: c7acd766c4485925b4da7a9c028137d35def9fca [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
Mirko Bonadei8fdcac32018-08-28 16:30:18 +020050TransportFeedbackPacketLossTracker::~TransportFeedbackPacketLossTracker() =
51 default;
52
minyue435ddf92017-01-23 08:07:05 -080053void TransportFeedbackPacketLossTracker::Reset() {
elad.alon7af93572017-03-03 10:51:35 -080054 acked_packets_ = 0;
elad.alond83b9672017-02-01 08:36:09 -080055 plr_state_.Reset();
56 rplr_state_.Reset();
minyue435ddf92017-01-23 08:07:05 -080057 packet_status_window_.clear();
58 ref_packet_status_ = packet_status_window_.begin();
59}
60
61uint16_t TransportFeedbackPacketLossTracker::ReferenceSequenceNumber() const {
62 RTC_DCHECK(!packet_status_window_.empty());
63 return ref_packet_status_->first;
64}
65
elad.alon7af93572017-03-03 10:51:35 -080066uint16_t TransportFeedbackPacketLossTracker::NewestSequenceNumber() const {
67 RTC_DCHECK(!packet_status_window_.empty());
68 return PreviousPacketStatus(packet_status_window_.end())->first;
69}
70
elad.alon3f9b12d2017-03-15 07:38:13 -070071void 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.alon7af93572017-03-03 10:51:35 -080079 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();
minyue435ddf92017-01-23 08:07:05 -080086 }
elad.alon7af93572017-03-03 10:51:35 -080087
elad.alon3f9b12d2017-03-15 07:38:13 -070088 // 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.alon7af93572017-03-03 10:51:35 -080090 while (!packet_status_window_.empty() &&
91 ForwardDiff(ref_packet_status_->first, seq_num) >= kSeqNumHalf) {
92 RemoveOldestPacketStatus();
93 }
94
elad.alon3f9b12d2017-03-15 07:38:13 -070095 SentPacket sent_packet(send_time_ms, PacketStatus::Unacked);
elad.alon7af93572017-03-03 10:51:35 -080096 packet_status_window_.insert(packet_status_window_.end(),
elad.alon3f9b12d2017-03-15 07:38:13 -070097 std::make_pair(seq_num, sent_packet));
elad.alon7af93572017-03-03 10:51:35 -080098
99 if (packet_status_window_.size() == 1) {
100 ref_packet_status_ = packet_status_window_.cbegin();
101 }
minyue435ddf92017-01-23 08:07:05 -0800102}
103
elad.alond12a8e12017-03-23 11:04:48 -0700104void TransportFeedbackPacketLossTracker::OnPacketFeedbackVector(
elad.alon92e448d2017-03-21 07:31:35 -0700105 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);
minyue435ddf92017-01-23 08:07:05 -0800108
elad.alon7af93572017-03-03 10:51:35 -0800109 // 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.alon3f9b12d2017-03-15 07:38:13 -0700111 if (it == packet_status_window_.end())
112 continue;
113
elad.alon92e448d2017-03-21 07:31:35 -0700114 const bool lost = packet.arrival_time_ms == PacketFeedback::kNotReceived;
elad.alon3f9b12d2017-03-15 07:38:13 -0700115 const PacketStatus packet_status =
116 lost ? PacketStatus::Lost : PacketStatus::Received;
117
118 UpdatePacketStatus(it, packet_status);
minyue435ddf92017-01-23 08:07:05 -0800119 }
120}
121
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200122absl::optional<float> TransportFeedbackPacketLossTracker::GetPacketLossRate()
123 const {
elad.alond83b9672017-02-01 08:36:09 -0800124 return plr_state_.GetMetric();
125}
126
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200127absl::optional<float>
elad.alond83b9672017-02-01 08:36:09 -0800128TransportFeedbackPacketLossTracker::GetRecoverablePacketLossRate() const {
129 return rplr_state_.GetMetric();
minyue435ddf92017-01-23 08:07:05 -0800130}
131
elad.alon3f9b12d2017-03-15 07:38:13 -0700132void TransportFeedbackPacketLossTracker::UpdatePacketStatus(
133 SentPacketStatusMap::iterator it,
134 PacketStatus new_status) {
135 if (it->second.status != PacketStatus::Unacked) {
elad.alon7af93572017-03-03 10:51:35 -0800136 // 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.alon3f9b12d2017-03-15 07:38:13 -0700139 if (it->second.status == PacketStatus::Lost &&
140 new_status == PacketStatus::Received) {
minyue435ddf92017-01-23 08:07:05 -0800141 // If older status said that the packet was lost but newer one says it
142 // is received, we take the newer one.
elad.alon7af93572017-03-03 10:51:35 -0800143 UpdateMetrics(it, false);
elad.alon3f9b12d2017-03-15 07:38:13 -0700144 it->second.status =
145 PacketStatus::Unacked; // For clarity; overwritten shortly.
minyue435ddf92017-01-23 08:07:05 -0800146 } 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.alon7af93572017-03-03 10:51:35 -0800149 // 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.
minyue435ddf92017-01-23 08:07:05 -0800152 return;
153 }
154 }
elad.alon7af93572017-03-03 10:51:35 -0800155
156 // Change from UNACKED to RECEIVED/LOST.
elad.alon3f9b12d2017-03-15 07:38:13 -0700157 it->second.status = new_status;
elad.alon7af93572017-03-03 10:51:35 -0800158 UpdateMetrics(it, true);
159
elad.alon3f9b12d2017-03-15 07:38:13 -0700160 // 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.alon7af93572017-03-03 10:51:35 -0800168 RemoveOldestPacketStatus();
elad.alon3f9b12d2017-03-15 07:38:13 -0700169 }
minyue435ddf92017-01-23 08:07:05 -0800170}
171
172void TransportFeedbackPacketLossTracker::RemoveOldestPacketStatus() {
elad.alond83b9672017-02-01 08:36:09 -0800173 UpdateMetrics(ref_packet_status_, false);
minyue435ddf92017-01-23 08:07:05 -0800174 const auto it = ref_packet_status_;
175 ref_packet_status_ = NextPacketStatus(it);
176 packet_status_window_.erase(it);
177}
178
elad.alond83b9672017-02-01 08:36:09 -0800179void TransportFeedbackPacketLossTracker::UpdateMetrics(
elad.alon7af93572017-03-03 10:51:35 -0800180 ConstPacketStatusIterator it,
elad.alond83b9672017-02-01 08:36:09 -0800181 bool apply /* false = undo */) {
minyue435ddf92017-01-23 08:07:05 -0800182 RTC_DCHECK(it != packet_status_window_.end());
elad.alon7af93572017-03-03 10:51:35 -0800183 // 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.alon3f9b12d2017-03-15 07:38:13 -0700186 RTC_DCHECK(!apply || it->second.status != PacketStatus::Unacked);
elad.alon7af93572017-03-03 10:51:35 -0800187
elad.alon3f9b12d2017-03-15 07:38:13 -0700188 if (it->second.status != PacketStatus::Unacked) {
elad.alon7af93572017-03-03 10:51:35 -0800189 UpdateCounter(&acked_packets_, apply);
190 }
191
elad.alond83b9672017-02-01 08:36:09 -0800192 UpdatePlr(it, apply);
193 UpdateRplr(it, apply);
194}
195
196void TransportFeedbackPacketLossTracker::UpdatePlr(
elad.alon7af93572017-03-03 10:51:35 -0800197 ConstPacketStatusIterator it,
elad.alond83b9672017-02-01 08:36:09 -0800198 bool apply /* false = undo */) {
elad.alon3f9b12d2017-03-15 07:38:13 -0700199 switch (it->second.status) {
elad.alon7af93572017-03-03 10:51:35 -0800200 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();
minyue435ddf92017-01-23 08:07:05 -0800210 }
211}
212
elad.alond83b9672017-02-01 08:36:09 -0800213void TransportFeedbackPacketLossTracker::UpdateRplr(
elad.alon7af93572017-03-03 10:51:35 -0800214 ConstPacketStatusIterator it,
elad.alond83b9672017-02-01 08:36:09 -0800215 bool apply /* false = undo */) {
elad.alon3f9b12d2017-03-15 07:38:13 -0700216 if (it->second.status == PacketStatus::Unacked) {
elad.alon7af93572017-03-03 10:51:35 -0800217 // Unacked packets cannot compose a pair.
218 return;
219 }
220
221 // Previous packet and current packet might compose a pair.
elad.alond83b9672017-02-01 08:36:09 -0800222 if (it != ref_packet_status_) {
223 const auto& prev = PreviousPacketStatus(it);
elad.alon3f9b12d2017-03-15 07:38:13 -0700224 if (prev->second.status != PacketStatus::Unacked) {
elad.alon7af93572017-03-03 10:51:35 -0800225 UpdateCounter(&rplr_state_.num_acked_pairs_, apply);
elad.alon3f9b12d2017-03-15 07:38:13 -0700226 if (prev->second.status == PacketStatus::Lost &&
227 it->second.status == PacketStatus::Received) {
elad.alond83b9672017-02-01 08:36:09 -0800228 UpdateCounter(
229 &rplr_state_.num_recoverable_losses_, apply);
minyue435ddf92017-01-23 08:07:05 -0800230 }
231 }
232 }
elad.alond83b9672017-02-01 08:36:09 -0800233
234 // Current packet and next packet might compose a pair.
elad.alond83b9672017-02-01 08:36:09 -0800235 const auto& next = NextPacketStatus(it);
236 if (next != packet_status_window_.end() &&
elad.alon3f9b12d2017-03-15 07:38:13 -0700237 next->second.status != PacketStatus::Unacked) {
elad.alon7af93572017-03-03 10:51:35 -0800238 UpdateCounter(&rplr_state_.num_acked_pairs_, apply);
elad.alon3f9b12d2017-03-15 07:38:13 -0700239 if (it->second.status == PacketStatus::Lost &&
240 next->second.status == PacketStatus::Received) {
elad.alond83b9672017-02-01 08:36:09 -0800241 UpdateCounter(&rplr_state_.num_recoverable_losses_, apply);
242 }
243 }
minyue435ddf92017-01-23 08:07:05 -0800244}
245
elad.alon7af93572017-03-03 10:51:35 -0800246TransportFeedbackPacketLossTracker::ConstPacketStatusIterator
minyue435ddf92017-01-23 08:07:05 -0800247TransportFeedbackPacketLossTracker::PreviousPacketStatus(
elad.alon7af93572017-03-03 10:51:35 -0800248 ConstPacketStatusIterator it) const {
minyue435ddf92017-01-23 08:07:05 -0800249 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.alon7af93572017-03-03 10:51:35 -0800264TransportFeedbackPacketLossTracker::ConstPacketStatusIterator
265TransportFeedbackPacketLossTracker::NextPacketStatus(
266 ConstPacketStatusIterator it) const {
minyue435ddf92017-01-23 08:07:05 -0800267 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.
287void TransportFeedbackPacketLossTracker::Validate() const { // Testing only!
elad.alon7af93572017-03-03 10:51:35 -0800288 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.alond83b9672017-02-01 08:36:09 -0800291 RTC_CHECK_LE(rplr_state_.num_recoverable_losses_,
elad.alon7af93572017-03-03 10:51:35 -0800292 rplr_state_.num_acked_pairs_);
293 RTC_CHECK_LE(rplr_state_.num_acked_pairs_, acked_packets_ - 1);
minyue435ddf92017-01-23 08:07:05 -0800294
elad.alon7af93572017-03-03 10:51:35 -0800295 size_t unacked_packets = 0;
minyue435ddf92017-01-23 08:07:05 -0800296 size_t received_packets = 0;
297 size_t lost_packets = 0;
elad.alon7af93572017-03-03 10:51:35 -0800298 size_t acked_pairs = 0;
elad.alond83b9672017-02-01 08:36:09 -0800299 size_t recoverable_losses = 0;
minyue435ddf92017-01-23 08:07:05 -0800300
301 if (!packet_status_window_.empty()) {
elad.alon7af93572017-03-03 10:51:35 -0800302 ConstPacketStatusIterator it = ref_packet_status_;
minyue435ddf92017-01-23 08:07:05 -0800303 do {
elad.alon3f9b12d2017-03-15 07:38:13 -0700304 switch (it->second.status) {
elad.alon7af93572017-03-03 10:51:35 -0800305 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.alond83b9672017-02-01 08:36:09 -0800316 }
317
318 auto next = std::next(it);
319 if (next == packet_status_window_.end())
320 next = packet_status_window_.begin();
321
elad.alon3f9b12d2017-03-15 07:38:13 -0700322 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 }
minyue435ddf92017-01-23 08:07:05 -0800333 }
334
335 RTC_CHECK_LT(ForwardDiff(ReferenceSequenceNumber(), it->first),
336 kSeqNumHalf);
337
elad.alond83b9672017-02-01 08:36:09 -0800338 it = next;
minyue435ddf92017-01-23 08:07:05 -0800339 } while (it != ref_packet_status_);
340 }
341
elad.alond83b9672017-02-01 08:36:09 -0800342 RTC_CHECK_EQ(plr_state_.num_received_packets_, received_packets);
343 RTC_CHECK_EQ(plr_state_.num_lost_packets_, lost_packets);
elad.alon7af93572017-03-03 10:51:35 -0800344 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.alond83b9672017-02-01 08:36:09 -0800347 RTC_CHECK_EQ(rplr_state_.num_recoverable_losses_, recoverable_losses);
348}
349
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200350absl::optional<float> TransportFeedbackPacketLossTracker::PlrState::GetMetric()
351 const {
elad.alond83b9672017-02-01 08:36:09 -0800352 const size_t total = num_lost_packets_ + num_received_packets_;
elad.alon7af93572017-03-03 10:51:35 -0800353 if (total < min_num_acked_packets_) {
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200354 return absl::nullopt;
elad.alond83b9672017-02-01 08:36:09 -0800355 } else {
Oskar Sundbom606c8822017-11-16 10:57:11 +0100356 return static_cast<float>(num_lost_packets_) / total;
elad.alond83b9672017-02-01 08:36:09 -0800357 }
358}
359
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200360absl::optional<float> TransportFeedbackPacketLossTracker::RplrState::GetMetric()
361 const {
elad.alon7af93572017-03-03 10:51:35 -0800362 if (num_acked_pairs_ < min_num_acked_pairs_) {
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200363 return absl::nullopt;
elad.alond83b9672017-02-01 08:36:09 -0800364 } else {
Oskar Sundbom606c8822017-11-16 10:57:11 +0100365 return static_cast<float>(num_recoverable_losses_) / num_acked_pairs_;
elad.alond83b9672017-02-01 08:36:09 -0800366 }
minyue435ddf92017-01-23 08:07:05 -0800367}
368
369} // namespace webrtc