blob: ca7490827149be583dbf4b727b9cd6e8c96a3a57 [file] [log] [blame]
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001/*
2 * Copyright (c) 2013 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/source/receive_statistics_impl.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000012
Oleh Prypin19929582019-04-23 08:50:04 +020013#include <cmath>
kwibergfd8be342016-05-14 19:44:11 -070014#include <cstdlib>
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +010015#include <memory>
danilchapf5f793c2017-07-27 04:44:18 -070016#include <vector>
kwibergfd8be342016-05-14 19:44:11 -070017
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +010018#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/remote_bitrate_estimator/test/bwe_test_logging.h"
Niels Möller1f3206c2018-09-14 08:26:32 +020020#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
22#include "modules/rtp_rtcp/source/time_util.h"
23#include "rtc_base/logging.h"
24#include "system_wrappers/include/clock.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000025
26namespace webrtc {
27
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000028const int64_t kStatisticsTimeoutMs = 8000;
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000029const int64_t kStatisticsProcessIntervalMs = 1000;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000030
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000031StreamStatistician::~StreamStatistician() {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000032
Niels Möllerd7819652019-08-13 14:43:02 +020033StreamStatisticianImpl::StreamStatisticianImpl(uint32_t ssrc,
34 Clock* clock,
35 int max_reordering_threshold)
danilchapec86be02017-08-14 05:51:02 -070036 : ssrc_(ssrc),
37 clock_(clock),
sprangcd349d92016-07-13 09:11:28 -070038 incoming_bitrate_(kStatisticsProcessIntervalMs,
39 RateStatistics::kBpsScale),
Danil Chapovalovebb50c22018-11-22 14:04:02 +010040 max_reordering_threshold_(max_reordering_threshold),
Niels Möller87da1092019-05-24 14:04:28 +020041 enable_retransmit_detection_(false),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000042 jitter_q4_(0),
Qingsi Wang2370b082018-08-21 14:24:26 -070043 cumulative_loss_(0),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000044 last_receive_time_ms_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000045 last_received_timestamp_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000046 received_seq_first_(0),
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000047 received_seq_max_(-1),
Qingsi Wang2370b082018-08-21 14:24:26 -070048 last_report_inorder_packets_(0),
49 last_report_old_packets_(0),
Niels Möllerd7819652019-08-13 14:43:02 +020050 last_report_seq_max_(-1) {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000051
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010052StreamStatisticianImpl::~StreamStatisticianImpl() = default;
53
Niels Möllerdbb988b2018-11-15 08:05:16 +010054void StreamStatisticianImpl::OnRtpPacket(const RtpPacketReceived& packet) {
Niels Möllerd7819652019-08-13 14:43:02 +020055 UpdateCounters(packet);
sprang@webrtc.orga45cac02014-01-27 16:22:08 +000056}
57
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000058bool StreamStatisticianImpl::UpdateOutOfOrder(const RtpPacketReceived& packet,
59 int64_t sequence_number,
60 int64_t now_ms) {
61 RTC_DCHECK_EQ(sequence_number,
62 seq_unwrapper_.UnwrapWithoutUpdate(packet.SequenceNumber()));
63
64 // Check if |packet| is second packet of a stream restart.
65 if (received_seq_out_of_order_) {
66 uint16_t expected_sequence_number = *received_seq_out_of_order_ + 1;
67 received_seq_out_of_order_ = absl::nullopt;
68 if (packet.SequenceNumber() == expected_sequence_number) {
69 // Ignore sequence number gap caused by stream restart for next packet
70 // loss calculation.
71 last_report_seq_max_ = sequence_number;
72 last_report_inorder_packets_ = receive_counters_.transmitted.packets -
73 receive_counters_.retransmitted.packets;
74 // As final part of stream restart consider |packet| is not out of order.
75 return false;
76 }
77 }
78
79 if (std::abs(sequence_number - received_seq_max_) >
80 max_reordering_threshold_) {
81 // Sequence number gap looks too large, wait until next packet to check
82 // for a stream restart.
83 received_seq_out_of_order_ = packet.SequenceNumber();
84 return true;
85 }
86
87 if (sequence_number > received_seq_max_)
88 return false;
89
90 // Old out of order packet, may be retransmit.
91 if (enable_retransmit_detection_ && IsRetransmitOfOldPacket(packet, now_ms))
92 receive_counters_.retransmitted.AddPacket(packet);
93 return true;
94}
95
danilchapec86be02017-08-14 05:51:02 -070096StreamDataCounters StreamStatisticianImpl::UpdateCounters(
Danil Chapovalov44727b42018-11-22 11:28:45 +010097 const RtpPacketReceived& packet) {
98 rtc::CritScope cs(&stream_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +010099 RTC_DCHECK_EQ(ssrc_, packet.Ssrc());
Danil Chapovalov44727b42018-11-22 11:28:45 +0100100 int64_t now_ms = clock_->TimeInMilliseconds();
101
102 incoming_bitrate_.Update(packet.size(), now_ms);
Henrik Boströmcb755b02019-04-02 15:11:48 +0200103 receive_counters_.last_packet_received_timestamp_ms = now_ms;
Niels Möllerdbb988b2018-11-15 08:05:16 +0100104 receive_counters_.transmitted.AddPacket(packet);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000105
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000106 int64_t sequence_number =
107 seq_unwrapper_.UnwrapWithoutUpdate(packet.SequenceNumber());
108 if (!ReceivedRtpPacket()) {
109 received_seq_first_ = sequence_number;
110 last_report_seq_max_ = sequence_number - 1;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100111 receive_counters_.first_packet_time_ms = now_ms;
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000112 } else if (UpdateOutOfOrder(packet, sequence_number, now_ms)) {
113 return receive_counters_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000114 }
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000115 // In order packet.
116 received_seq_max_ = sequence_number;
117 seq_unwrapper_.UpdateLast(sequence_number);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000118
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000119 // If new time stamp and more than one in-order packet received, calculate
120 // new jitter statistics.
121 if (packet.Timestamp() != last_received_timestamp_ &&
122 (receive_counters_.transmitted.packets -
123 receive_counters_.retransmitted.packets) > 1) {
124 UpdateJitter(packet, now_ms);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000125 }
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000126 last_received_timestamp_ = packet.Timestamp();
127 last_receive_time_ms_ = now_ms;
danilchapec86be02017-08-14 05:51:02 -0700128 return receive_counters_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000129}
130
Niels Möllerdbb988b2018-11-15 08:05:16 +0100131void StreamStatisticianImpl::UpdateJitter(const RtpPacketReceived& packet,
Danil Chapovalov856cf222018-11-26 10:20:01 +0100132 int64_t receive_time_ms) {
133 int64_t receive_diff_ms = receive_time_ms - last_receive_time_ms_;
134 RTC_DCHECK_GE(receive_diff_ms, 0);
135 uint32_t receive_diff_rtp = static_cast<uint32_t>(
136 (receive_diff_ms * packet.payload_type_frequency()) / 1000);
137 int32_t time_diff_samples =
138 receive_diff_rtp - (packet.Timestamp() - last_received_timestamp_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000139
kwibergfd8be342016-05-14 19:44:11 -0700140 time_diff_samples = std::abs(time_diff_samples);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000141
142 // lib_jingle sometimes deliver crazy jumps in TS for the same stream.
143 // If this happens, don't update jitter value. Use 5 secs video frequency
144 // as the threshold.
145 if (time_diff_samples < 450000) {
146 // Note we calculate in Q4 to avoid using float.
147 int32_t jitter_diff_q4 = (time_diff_samples << 4) - jitter_q4_;
148 jitter_q4_ += ((jitter_diff_q4 + 8) >> 4);
149 }
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000150}
151
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000152void StreamStatisticianImpl::SetMaxReorderingThreshold(
153 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700154 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000155 max_reordering_threshold_ = max_reordering_threshold;
156}
157
Niels Möller5304a322018-08-27 13:27:05 +0200158void StreamStatisticianImpl::EnableRetransmitDetection(bool enable) {
159 rtc::CritScope cs(&stream_lock_);
160 enable_retransmit_detection_ = enable;
161}
162
Niels Möllerd77cc242019-08-22 09:40:25 +0200163RtpReceiveStats StreamStatisticianImpl::GetStats() const {
164 rtc::CritScope cs(&stream_lock_);
165 RtpReceiveStats stats;
166 stats.packets_lost = cumulative_loss_;
167 // TODO(nisse): Can we return a float instead?
168 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
169 stats.jitter = jitter_q4_ >> 4;
170 stats.last_packet_received_timestamp_ms =
171 receive_counters_.last_packet_received_timestamp_ms;
172 stats.packet_counter = receive_counters_.transmitted;
173 return stats;
174}
175
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000176bool StreamStatisticianImpl::GetStatistics(RtcpStatistics* statistics,
Qingsi Wang2370b082018-08-21 14:24:26 -0700177 bool reset) {
Niels Möller12ebfa62019-08-06 16:04:12 +0200178 rtc::CritScope cs(&stream_lock_);
179 if (!ReceivedRtpPacket()) {
180 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000181 }
182
Niels Möller12ebfa62019-08-06 16:04:12 +0200183 if (!reset) {
184 if (last_report_inorder_packets_ == 0) {
185 // No report.
186 return false;
187 }
188 // Just get last report.
189 *statistics = last_reported_statistics_;
190 return true;
191 }
192
193 *statistics = CalculateRtcpStatistics();
194
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000195 return true;
196}
197
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200198bool StreamStatisticianImpl::GetActiveStatisticsAndReset(
199 RtcpStatistics* statistics) {
Niels Möller12ebfa62019-08-06 16:04:12 +0200200 rtc::CritScope cs(&stream_lock_);
201 if (clock_->TimeInMilliseconds() - last_receive_time_ms_ >=
202 kStatisticsTimeoutMs) {
203 // Not active.
204 return false;
205 }
206 if (!ReceivedRtpPacket()) {
207 return false;
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200208 }
209
Niels Möller12ebfa62019-08-06 16:04:12 +0200210 *statistics = CalculateRtcpStatistics();
211
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200212 return true;
213}
214
Qingsi Wang2370b082018-08-21 14:24:26 -0700215RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics() {
216 RtcpStatistics stats;
Qingsi Wang2370b082018-08-21 14:24:26 -0700217 // Calculate fraction lost.
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000218 int64_t exp_since_last = received_seq_max_ - last_report_seq_max_;
219 RTC_DCHECK_GE(exp_since_last, 0);
Qingsi Wang2370b082018-08-21 14:24:26 -0700220
221 // Number of received RTP packets since last report, counts all packets but
222 // not re-transmissions.
223 uint32_t rec_since_last = (receive_counters_.transmitted.packets -
224 receive_counters_.retransmitted.packets) -
225 last_report_inorder_packets_;
226
227 // With NACK we don't know the expected retransmissions during the last
228 // second. We know how many "old" packets we have received. We just count
229 // the number of old received to estimate the loss, but it still does not
230 // guarantee an exact number since we run this based on time triggered by
231 // sending of an RTP packet. This should have a minimum effect.
232
233 // With NACK we don't count old packets as received since they are
234 // re-transmitted. We use RTT to decide if a packet is re-ordered or
235 // re-transmitted.
236 uint32_t retransmitted_packets =
237 receive_counters_.retransmitted.packets - last_report_old_packets_;
238 rec_since_last += retransmitted_packets;
239
240 int32_t missing = 0;
241 if (exp_since_last > rec_since_last) {
242 missing = (exp_since_last - rec_since_last);
243 }
244 uint8_t local_fraction_lost = 0;
245 if (exp_since_last) {
246 // Scale 0 to 255, where 255 is 100% loss.
247 local_fraction_lost = static_cast<uint8_t>(255 * missing / exp_since_last);
248 }
249 stats.fraction_lost = local_fraction_lost;
250
251 // We need a counter for cumulative loss too.
252 // TODO(danilchap): Ensure cumulative loss is below maximum value of 2^24.
253 cumulative_loss_ += missing;
254 stats.packets_lost = cumulative_loss_;
255 stats.extended_highest_sequence_number =
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000256 static_cast<uint32_t>(received_seq_max_);
Qingsi Wang2370b082018-08-21 14:24:26 -0700257 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
258 stats.jitter = jitter_q4_ >> 4;
259
260 // Store this report.
261 last_reported_statistics_ = stats;
262
263 // Only for report blocks in RTCP SR and RR.
264 last_report_inorder_packets_ = receive_counters_.transmitted.packets -
265 receive_counters_.retransmitted.packets;
266 last_report_old_packets_ = receive_counters_.retransmitted.packets;
267 last_report_seq_max_ = received_seq_max_;
gaetano.carlucci61050f62016-09-30 06:29:54 -0700268 BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts",
gaetano.carlucci52a57032016-09-14 05:04:36 -0700269 clock_->TimeInMilliseconds(),
Qingsi Wang2370b082018-08-21 14:24:26 -0700270 cumulative_loss_, ssrc_);
gaetano.carlucci52a57032016-09-14 05:04:36 -0700271 BWE_TEST_LOGGING_PLOT_WITH_SSRC(
gaetano.carlucci61050f62016-09-30 06:29:54 -0700272 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700273 (received_seq_max_ - received_seq_first_), ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000274
Qingsi Wang2370b082018-08-21 14:24:26 -0700275 return stats;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000276}
277
Niels Möller9a9f18a2019-08-02 13:52:37 +0200278absl::optional<int> StreamStatisticianImpl::GetFractionLostInPercent() const {
279 rtc::CritScope cs(&stream_lock_);
280 if (received_seq_max_ < 0) {
281 return absl::nullopt;
282 }
283 int64_t expected_packets = 1 + received_seq_max_ - received_seq_first_;
284 if (expected_packets <= 0) {
285 return absl::nullopt;
286 }
287 // Spec allows negative cumulative loss, but implementation uses uint32_t, so
288 // this expression is always non-negative.
289 return 100 * static_cast<int64_t>(cumulative_loss_) / expected_packets;
290}
291
Niels Möller58b496b2019-08-12 12:16:31 +0200292StreamDataCounters StreamStatisticianImpl::GetReceiveStreamDataCounters()
293 const {
danilchap7c9426c2016-04-14 03:05:31 -0700294 rtc::CritScope cs(&stream_lock_);
Niels Möller58b496b2019-08-12 12:16:31 +0200295 return receive_counters_;
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000296}
297
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000298uint32_t StreamStatisticianImpl::BitrateReceived() const {
danilchap7c9426c2016-04-14 03:05:31 -0700299 rtc::CritScope cs(&stream_lock_);
sprangcd349d92016-07-13 09:11:28 -0700300 return incoming_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000301}
302
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000303bool StreamStatisticianImpl::IsRetransmitOfOldPacket(
Danil Chapovalov44727b42018-11-22 11:28:45 +0100304 const RtpPacketReceived& packet,
305 int64_t now_ms) const {
Niels Möllerdbb988b2018-11-15 08:05:16 +0100306 uint32_t frequency_khz = packet.payload_type_frequency() / 1000;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100307 RTC_DCHECK_GT(frequency_khz, 0);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000308
Danil Chapovalov44727b42018-11-22 11:28:45 +0100309 int64_t time_diff_ms = now_ms - last_receive_time_ms_;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000310
311 // Diff in time stamp since last received in order.
Niels Möllerdbb988b2018-11-15 08:05:16 +0100312 uint32_t timestamp_diff = packet.Timestamp() - last_received_timestamp_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000313 uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000314
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000315 int64_t max_delay_ms = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000316
Niels Möllereda00872018-05-23 13:54:51 +0200317 // Jitter standard deviation in samples.
Oleh Prypin19929582019-04-23 08:50:04 +0200318 float jitter_std = std::sqrt(static_cast<float>(jitter_q4_ >> 4));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000319
Niels Möllereda00872018-05-23 13:54:51 +0200320 // 2 times the standard deviation => 95% confidence.
321 // And transform to milliseconds by dividing by the frequency in kHz.
322 max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz);
323
324 // Min max_delay_ms is 1.
325 if (max_delay_ms == 0) {
326 max_delay_ms = 1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000327 }
328 return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms;
329}
330
Niels Möllerd7819652019-08-13 14:43:02 +0200331std::unique_ptr<ReceiveStatistics> ReceiveStatistics::Create(Clock* clock) {
332 return absl::make_unique<ReceiveStatisticsImpl>(clock);
333}
334
Niels Möllerd7819652019-08-13 14:43:02 +0200335ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock)
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000336 : clock_(clock),
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100337 last_returned_ssrc_(0),
Niels Möllerd7819652019-08-13 14:43:02 +0200338 max_reordering_threshold_(kDefaultMaxReorderingThreshold) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000339
340ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
341 while (!statisticians_.empty()) {
342 delete statisticians_.begin()->second;
343 statisticians_.erase(statisticians_.begin());
344 }
345}
346
Niels Möller1f3206c2018-09-14 08:26:32 +0200347void ReceiveStatisticsImpl::OnRtpPacket(const RtpPacketReceived& packet) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000348 // StreamStatisticianImpl instance is created once and only destroyed when
349 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
350 // it's own locking so don't hold receive_statistics_lock_ (potential
351 // deadlock).
Niels Möller87da1092019-05-24 14:04:28 +0200352 GetOrCreateStatistician(packet.Ssrc())->OnRtpPacket(packet);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000353}
354
Niels Möller87da1092019-05-24 14:04:28 +0200355StreamStatisticianImpl* ReceiveStatisticsImpl::GetStatistician(
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000356 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700357 rtc::CritScope cs(&receive_statistics_lock_);
Niels Möller87da1092019-05-24 14:04:28 +0200358 const auto& it = statisticians_.find(ssrc);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000359 if (it == statisticians_.end())
360 return NULL;
361 return it->second;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000362}
363
Niels Möller87da1092019-05-24 14:04:28 +0200364StreamStatisticianImpl* ReceiveStatisticsImpl::GetOrCreateStatistician(
365 uint32_t ssrc) {
366 rtc::CritScope cs(&receive_statistics_lock_);
367 StreamStatisticianImpl*& impl = statisticians_[ssrc];
368 if (impl == nullptr) { // new element
Niels Möllerd7819652019-08-13 14:43:02 +0200369 impl = new StreamStatisticianImpl(ssrc, clock_, max_reordering_threshold_);
Niels Möller87da1092019-05-24 14:04:28 +0200370 }
371 return impl;
372}
373
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000374void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
375 int max_reordering_threshold) {
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100376 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
377 {
378 rtc::CritScope cs(&receive_statistics_lock_);
379 max_reordering_threshold_ = max_reordering_threshold;
380 statisticians = statisticians_;
381 }
382 for (auto& statistician : statisticians) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200383 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000384 }
385}
386
Niels Möller87da1092019-05-24 14:04:28 +0200387void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
388 uint32_t ssrc,
389 int max_reordering_threshold) {
390 GetOrCreateStatistician(ssrc)->SetMaxReorderingThreshold(
391 max_reordering_threshold);
392}
393
Niels Möller5304a322018-08-27 13:27:05 +0200394void ReceiveStatisticsImpl::EnableRetransmitDetection(uint32_t ssrc,
395 bool enable) {
Niels Möller87da1092019-05-24 14:04:28 +0200396 GetOrCreateStatistician(ssrc)->EnableRetransmitDetection(enable);
Niels Möller5304a322018-08-27 13:27:05 +0200397}
398
danilchap0bc84232017-08-11 08:12:54 -0700399std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700400 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200401 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
402 {
403 rtc::CritScope cs(&receive_statistics_lock_);
404 statisticians = statisticians_;
405 }
danilchapf5f793c2017-07-27 04:44:18 -0700406 std::vector<rtcp::ReportBlock> result;
407 result.reserve(std::min(max_blocks, statisticians.size()));
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100408 auto add_report_block = [&result](uint32_t media_ssrc,
409 StreamStatisticianImpl* statistician) {
danilchapf5f793c2017-07-27 04:44:18 -0700410 // Do we have receive statistics to send?
411 RtcpStatistics stats;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100412 if (!statistician->GetActiveStatisticsAndReset(&stats))
413 return;
danilchapf5f793c2017-07-27 04:44:18 -0700414 result.emplace_back();
415 rtcp::ReportBlock& block = result.back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100416 block.SetMediaSsrc(media_ssrc);
danilchapf5f793c2017-07-27 04:44:18 -0700417 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700418 if (!block.SetCumulativeLost(stats.packets_lost)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100419 RTC_LOG(LS_WARNING) << "Cumulative lost is oversized.";
danilchapf5f793c2017-07-27 04:44:18 -0700420 result.pop_back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100421 return;
danilchapf5f793c2017-07-27 04:44:18 -0700422 }
srte186d9c32017-08-04 05:03:53 -0700423 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700424 block.SetJitter(stats.jitter);
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100425 };
426
427 const auto start_it = statisticians.upper_bound(last_returned_ssrc_);
428 for (auto it = start_it;
429 result.size() < max_blocks && it != statisticians.end(); ++it)
430 add_report_block(it->first, it->second);
431 for (auto it = statisticians.begin();
432 result.size() < max_blocks && it != start_it; ++it)
433 add_report_block(it->first, it->second);
434
435 if (!result.empty())
436 last_returned_ssrc_ = result.back().source_ssrc();
danilchapf5f793c2017-07-27 04:44:18 -0700437 return result;
438}
439
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000440} // namespace webrtc