Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(997)

Side by Side Diff: webrtc/voice_engine/transport_feedback_packet_loss_tracker.cc

Issue 2579613003: Add TransportFeedbackPacketLossTracker. (Closed)
Patch Set: on comments Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
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, 0);
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::TransportFeedback& 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 =
96 fb_vector[i] !=
97 webrtc::rtcp::TransportFeedback::StatusSymbol::kNotReceived;
98 InsertPacketStatus(seq_num, received);
99
100 while (packet_status_window_.size() > max_window_size_) {
101 // Make sure that the window holds at most |max_window_size_| items.
102 RemoveOldestPacketStatus();
103 }
104 }
105 }
106
107 bool TransportFeedbackPacketLossTracker::GetPacketLossRates(
108 float* packet_loss_rate,
109 float* consecutive_packet_loss_rate) const {
110 const size_t total = num_lost_packets_ + num_received_packets_;
111 if (total < min_window_size_)
112 return false;
113 *packet_loss_rate = static_cast<float>(num_lost_packets_) / total;
114 *consecutive_packet_loss_rate =
115 static_cast<float>(num_consecutive_losses_) / total;
116 return true;
117 }
118
119 void TransportFeedbackPacketLossTracker::InsertPacketStatus(uint16_t seq_num,
120 bool received) {
121 const auto& ret =
122 packet_status_window_.insert(std::make_pair(seq_num, received));
123 if (!ret.second) {
124 if (!ret.first->second && received) {
125 // If older status said that the packet was lost but newer one says it
126 // is received, we take the newer one.
127 UndoPacketStatus(ret.first);
128 ret.first->second = received;
129 } else {
130 // If the value is unchanged or if older status said that the packet was
131 // received but the newer one says it is lost, we ignore it.
132 return;
133 }
134 }
135 ApplyPacketStatus(ret.first);
136 if (packet_status_window_.size() == 1)
137 ref_packet_status_ = ret.first;
138 }
139
140 void TransportFeedbackPacketLossTracker::RemoveOldestPacketStatus() {
141 UndoPacketStatus(ref_packet_status_);
142 const auto it = ref_packet_status_;
143 ref_packet_status_ = NextPacketStatus(it);
144 packet_status_window_.erase(it);
145 }
146
147 void TransportFeedbackPacketLossTracker::ApplyPacketStatus(
148 PacketStatusIterator it) {
149 RTC_DCHECK(it != packet_status_window_.end());
150 if (it->second) {
151 ++num_received_packets_;
152 } else {
153 ++num_lost_packets_;
154 const auto& next = NextPacketStatus(it);
155 if (next != packet_status_window_.end() &&
156 next->first == static_cast<uint16_t>(it->first + 1) && !next->second) {
157 // Feedback shows that the next packet has been lost. Since this
158 // packet is lost, we increase the consecutive loss counter.
159 ++num_consecutive_losses_;
160 }
161 if (it != ref_packet_status_) {
162 const auto& pre = PreviousPacketStatus(it);
163 if (pre->first == static_cast<uint16_t>(it->first - 1) && !pre->second) {
164 // Feedback shows that the previous packet has been lost. Since this
165 // packet is lost, we increase the consecutive loss counter.
166 ++num_consecutive_losses_;
167 }
168 }
169 }
170 }
171
172 void TransportFeedbackPacketLossTracker::UndoPacketStatus(
173 PacketStatusIterator it) {
174 RTC_DCHECK(it != packet_status_window_.end());
175 if (it->second) {
176 RTC_DCHECK_GT(num_received_packets_, 0);
177 --num_received_packets_;
178 } else {
179 RTC_DCHECK_GT(num_lost_packets_, 0);
180 --num_lost_packets_;
181 const auto& next = NextPacketStatus(it);
182 if (next != packet_status_window_.end() &&
183 next->first == static_cast<uint16_t>(it->first + 1) && !next->second) {
184 RTC_DCHECK_GT(num_consecutive_losses_, 0);
185 --num_consecutive_losses_;
186 }
187 if (it != ref_packet_status_) {
188 const auto& pre = PreviousPacketStatus(it);
189 if (pre->first == static_cast<uint16_t>(it->first - 1) && !pre->second) {
190 RTC_DCHECK_GT(num_consecutive_losses_, 0);
191 --num_consecutive_losses_;
192 }
193 }
194 }
195 }
196
197 TransportFeedbackPacketLossTracker::PacketStatusIterator
198 TransportFeedbackPacketLossTracker::PreviousPacketStatus(
199 PacketStatusIterator it) {
200 RTC_DCHECK(it != ref_packet_status_);
201 if (it == packet_status_window_.end()) {
202 // This is to make PreviousPacketStatus(packet_status_window_.end()) point
203 // to the last element.
204 it = ref_packet_status_;
205 }
206
207 if (it == packet_status_window_.begin()) {
208 // Due to the circular nature of sequence numbers, we let the iterator
209 // go to the end.
210 it = packet_status_window_.end();
211 }
212 return --it;
213 }
214
215 TransportFeedbackPacketLossTracker::PacketStatusIterator
216 TransportFeedbackPacketLossTracker::NextPacketStatus(PacketStatusIterator it) {
217 RTC_DCHECK(it != packet_status_window_.end());
218 ++it;
219 if (it == packet_status_window_.end()) {
220 // Due to the circular nature of sequence numbers, we let the iterator
221 // goes back to the beginning.
222 it = packet_status_window_.begin();
223 }
224 if (it == ref_packet_status_) {
225 // This is to make the NextPacketStatus of the last element to return the
226 // beyond-the-end iterator.
227 it = packet_status_window_.end();
228 }
229 return it;
230 }
231
232 // TODO(minyue): This method checks the states of this class do not misbehave.
233 // The method is used both in unit tests and a fuzzer test. The fuzzer test
234 // is present to help finding potential errors. Once the fuzzer test shows no
235 // error after long period, we can remove the fuzzer test, and move this method
236 // to unit test.
237 void TransportFeedbackPacketLossTracker::Validate() const { // Testing only!
238 RTC_CHECK_LE(packet_status_window_.size(), max_window_size_);
239 RTC_CHECK_GE(num_lost_packets_, num_consecutive_losses_);
240 RTC_CHECK_EQ(packet_status_window_.size(),
241 num_lost_packets_ + num_received_packets_);
242
243 size_t received_packets = 0;
244 size_t lost_packets = 0;
245 size_t consecutive_losses = 0;
246
247 if (!packet_status_window_.empty()) {
248 PacketStatusIterator it = ref_packet_status_;
249 bool pre_lost = false;
250 uint16_t pre_seq_num = it->first - 1;
251 do {
252 if (it->second) {
253 ++received_packets;
254 } else {
255 ++lost_packets;
256 if (pre_lost && pre_seq_num == static_cast<uint16_t>(it->first - 1))
257 ++consecutive_losses;
258 }
259
260 RTC_CHECK_LT(ForwardDiff(ReferenceSequenceNumber(), it->first),
261 kSeqNumHalf);
262
263 pre_lost = !it->second;
264 pre_seq_num = it->first;
265
266 ++it;
267 if (it == packet_status_window_.end())
268 it = packet_status_window_.begin();
269 } while (it != ref_packet_status_);
270 }
271
272 RTC_CHECK_EQ(num_received_packets_, received_packets);
273 RTC_CHECK_EQ(num_lost_packets_, lost_packets);
274 RTC_CHECK_EQ(num_consecutive_losses_, consecutive_losses);
275 }
276
277 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698