blob: 0c47e08b1ecb4df89d130d524efea65afd77a138 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/remote_bitrate_estimator/test/bwe_test_logging.h"
Niels Möller1f3206c2018-09-14 08:26:32 +020019#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
21#include "modules/rtp_rtcp/source/time_util.h"
22#include "rtc_base/logging.h"
23#include "system_wrappers/include/clock.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000024
25namespace webrtc {
26
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000027const int64_t kStatisticsTimeoutMs = 8000;
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000028const int64_t kStatisticsProcessIntervalMs = 1000;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000029
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000030StreamStatistician::~StreamStatistician() {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000031
Niels Möllerd7819652019-08-13 14:43:02 +020032StreamStatisticianImpl::StreamStatisticianImpl(uint32_t ssrc,
33 Clock* clock,
34 int max_reordering_threshold)
danilchapec86be02017-08-14 05:51:02 -070035 : ssrc_(ssrc),
36 clock_(clock),
sprangcd349d92016-07-13 09:11:28 -070037 incoming_bitrate_(kStatisticsProcessIntervalMs,
38 RateStatistics::kBpsScale),
Danil Chapovalovebb50c22018-11-22 14:04:02 +010039 max_reordering_threshold_(max_reordering_threshold),
Niels Möller87da1092019-05-24 14:04:28 +020040 enable_retransmit_detection_(false),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000041 jitter_q4_(0),
Qingsi Wang2370b082018-08-21 14:24:26 -070042 cumulative_loss_(0),
Niels Möller1a3859c2019-09-04 09:43:15 +020043 cumulative_loss_rtcp_offset_(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),
Niels Möller1a3859c2019-09-04 09:43:15 +020046 received_seq_first_(-1),
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000047 received_seq_max_(-1),
Niels Möller1a3859c2019-09-04 09:43:15 +020048 last_report_cumulative_loss_(0),
Niels Möllerd7819652019-08-13 14:43:02 +020049 last_report_seq_max_(-1) {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000050
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010051StreamStatisticianImpl::~StreamStatisticianImpl() = default;
52
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000053bool StreamStatisticianImpl::UpdateOutOfOrder(const RtpPacketReceived& packet,
54 int64_t sequence_number,
55 int64_t now_ms) {
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000056 // Check if |packet| is second packet of a stream restart.
57 if (received_seq_out_of_order_) {
Niels Möller1a3859c2019-09-04 09:43:15 +020058 // Count the previous packet as a received; it was postponed below.
59 --cumulative_loss_;
60
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000061 uint16_t expected_sequence_number = *received_seq_out_of_order_ + 1;
62 received_seq_out_of_order_ = absl::nullopt;
63 if (packet.SequenceNumber() == expected_sequence_number) {
Niels Möller1a3859c2019-09-04 09:43:15 +020064 // Ignore sequence number gap caused by stream restart for packet loss
65 // calculation, by setting received_seq_max_ to the sequence number just
66 // before the out-of-order seqno. This gives a net zero change of
67 // |cumulative_loss_|, for the two packets interpreted as a stream reset.
68 //
69 // Fraction loss for the next report may get a bit off, since we don't
70 // update last_report_seq_max_ and last_report_cumulative_loss_ in a
71 // consistent way.
72 last_report_seq_max_ = sequence_number - 2;
73 received_seq_max_ = sequence_number - 2;
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000074 return false;
75 }
76 }
77
78 if (std::abs(sequence_number - received_seq_max_) >
79 max_reordering_threshold_) {
80 // Sequence number gap looks too large, wait until next packet to check
81 // for a stream restart.
82 received_seq_out_of_order_ = packet.SequenceNumber();
Niels Möller1a3859c2019-09-04 09:43:15 +020083 // Postpone counting this as a received packet until we know how to update
84 // |received_seq_max_|, otherwise we temporarily decrement
85 // |cumulative_loss_|. The
86 // ReceiveStatisticsTest.StreamRestartDoesntCountAsLoss test expects
87 // |cumulative_loss_| to be unchanged by the reception of the first packet
88 // after stream reset.
89 ++cumulative_loss_;
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000090 return true;
91 }
92
93 if (sequence_number > received_seq_max_)
94 return false;
95
96 // Old out of order packet, may be retransmit.
97 if (enable_retransmit_detection_ && IsRetransmitOfOldPacket(packet, now_ms))
98 receive_counters_.retransmitted.AddPacket(packet);
99 return true;
100}
101
Niels Möller1a3859c2019-09-04 09:43:15 +0200102void StreamStatisticianImpl::UpdateCounters(const RtpPacketReceived& packet) {
Danil Chapovalov44727b42018-11-22 11:28:45 +0100103 rtc::CritScope cs(&stream_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +0100104 RTC_DCHECK_EQ(ssrc_, packet.Ssrc());
Danil Chapovalov44727b42018-11-22 11:28:45 +0100105 int64_t now_ms = clock_->TimeInMilliseconds();
106
107 incoming_bitrate_.Update(packet.size(), now_ms);
Henrik Boströmcb755b02019-04-02 15:11:48 +0200108 receive_counters_.last_packet_received_timestamp_ms = now_ms;
Niels Möllerdbb988b2018-11-15 08:05:16 +0100109 receive_counters_.transmitted.AddPacket(packet);
Niels Möller1a3859c2019-09-04 09:43:15 +0200110 --cumulative_loss_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000111
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000112 int64_t sequence_number =
113 seq_unwrapper_.UnwrapWithoutUpdate(packet.SequenceNumber());
Niels Möller1a3859c2019-09-04 09:43:15 +0200114
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000115 if (!ReceivedRtpPacket()) {
116 received_seq_first_ = sequence_number;
117 last_report_seq_max_ = sequence_number - 1;
Niels Möller1a3859c2019-09-04 09:43:15 +0200118 received_seq_max_ = sequence_number - 1;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100119 receive_counters_.first_packet_time_ms = now_ms;
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000120 } else if (UpdateOutOfOrder(packet, sequence_number, now_ms)) {
Niels Möller1a3859c2019-09-04 09:43:15 +0200121 return;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000122 }
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000123 // In order packet.
Niels Möller1a3859c2019-09-04 09:43:15 +0200124 cumulative_loss_ += sequence_number - received_seq_max_;
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000125 received_seq_max_ = sequence_number;
126 seq_unwrapper_.UpdateLast(sequence_number);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000127
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000128 // If new time stamp and more than one in-order packet received, calculate
129 // new jitter statistics.
130 if (packet.Timestamp() != last_received_timestamp_ &&
131 (receive_counters_.transmitted.packets -
132 receive_counters_.retransmitted.packets) > 1) {
133 UpdateJitter(packet, now_ms);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000134 }
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000135 last_received_timestamp_ = packet.Timestamp();
136 last_receive_time_ms_ = now_ms;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000137}
138
Niels Möllerdbb988b2018-11-15 08:05:16 +0100139void StreamStatisticianImpl::UpdateJitter(const RtpPacketReceived& packet,
Danil Chapovalov856cf222018-11-26 10:20:01 +0100140 int64_t receive_time_ms) {
141 int64_t receive_diff_ms = receive_time_ms - last_receive_time_ms_;
142 RTC_DCHECK_GE(receive_diff_ms, 0);
143 uint32_t receive_diff_rtp = static_cast<uint32_t>(
144 (receive_diff_ms * packet.payload_type_frequency()) / 1000);
145 int32_t time_diff_samples =
146 receive_diff_rtp - (packet.Timestamp() - last_received_timestamp_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000147
kwibergfd8be342016-05-14 19:44:11 -0700148 time_diff_samples = std::abs(time_diff_samples);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000149
150 // lib_jingle sometimes deliver crazy jumps in TS for the same stream.
151 // If this happens, don't update jitter value. Use 5 secs video frequency
152 // as the threshold.
153 if (time_diff_samples < 450000) {
154 // Note we calculate in Q4 to avoid using float.
155 int32_t jitter_diff_q4 = (time_diff_samples << 4) - jitter_q4_;
156 jitter_q4_ += ((jitter_diff_q4 + 8) >> 4);
157 }
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000158}
159
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000160void StreamStatisticianImpl::SetMaxReorderingThreshold(
161 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700162 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000163 max_reordering_threshold_ = max_reordering_threshold;
164}
165
Niels Möller5304a322018-08-27 13:27:05 +0200166void StreamStatisticianImpl::EnableRetransmitDetection(bool enable) {
167 rtc::CritScope cs(&stream_lock_);
168 enable_retransmit_detection_ = enable;
169}
170
Niels Möllerd77cc242019-08-22 09:40:25 +0200171RtpReceiveStats StreamStatisticianImpl::GetStats() const {
172 rtc::CritScope cs(&stream_lock_);
173 RtpReceiveStats stats;
174 stats.packets_lost = cumulative_loss_;
175 // TODO(nisse): Can we return a float instead?
176 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
177 stats.jitter = jitter_q4_ >> 4;
178 stats.last_packet_received_timestamp_ms =
179 receive_counters_.last_packet_received_timestamp_ms;
180 stats.packet_counter = receive_counters_.transmitted;
181 return stats;
182}
183
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200184bool StreamStatisticianImpl::GetActiveStatisticsAndReset(
185 RtcpStatistics* statistics) {
Niels Möller12ebfa62019-08-06 16:04:12 +0200186 rtc::CritScope cs(&stream_lock_);
187 if (clock_->TimeInMilliseconds() - last_receive_time_ms_ >=
188 kStatisticsTimeoutMs) {
189 // Not active.
190 return false;
191 }
192 if (!ReceivedRtpPacket()) {
193 return false;
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200194 }
195
Niels Möller12ebfa62019-08-06 16:04:12 +0200196 *statistics = CalculateRtcpStatistics();
197
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200198 return true;
199}
200
Qingsi Wang2370b082018-08-21 14:24:26 -0700201RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics() {
202 RtcpStatistics stats;
Qingsi Wang2370b082018-08-21 14:24:26 -0700203 // Calculate fraction lost.
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000204 int64_t exp_since_last = received_seq_max_ - last_report_seq_max_;
205 RTC_DCHECK_GE(exp_since_last, 0);
Qingsi Wang2370b082018-08-21 14:24:26 -0700206
Niels Möller1a3859c2019-09-04 09:43:15 +0200207 int32_t lost_since_last = cumulative_loss_ - last_report_cumulative_loss_;
208 if (exp_since_last > 0 && lost_since_last > 0) {
Qingsi Wang2370b082018-08-21 14:24:26 -0700209 // Scale 0 to 255, where 255 is 100% loss.
Niels Möller1a3859c2019-09-04 09:43:15 +0200210 stats.fraction_lost =
211 static_cast<uint8_t>(255 * lost_since_last / exp_since_last);
212 } else {
213 stats.fraction_lost = 0;
Qingsi Wang2370b082018-08-21 14:24:26 -0700214 }
Qingsi Wang2370b082018-08-21 14:24:26 -0700215
Niels Möller1a3859c2019-09-04 09:43:15 +0200216 // TODO(danilchap): Ensure |stats.packets_lost| is clamped to fit in a signed
217 // 24-bit value.
218 stats.packets_lost = cumulative_loss_ + cumulative_loss_rtcp_offset_;
219 if (stats.packets_lost < 0) {
220 // Clamp to zero. Work around to accomodate for senders that misbehave with
221 // negative cumulative loss.
222 stats.packets_lost = 0;
223 cumulative_loss_rtcp_offset_ = -cumulative_loss_;
224 }
Qingsi Wang2370b082018-08-21 14:24:26 -0700225 stats.extended_highest_sequence_number =
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000226 static_cast<uint32_t>(received_seq_max_);
Qingsi Wang2370b082018-08-21 14:24:26 -0700227 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
228 stats.jitter = jitter_q4_ >> 4;
229
Qingsi Wang2370b082018-08-21 14:24:26 -0700230 // Only for report blocks in RTCP SR and RR.
Niels Möller1a3859c2019-09-04 09:43:15 +0200231 last_report_cumulative_loss_ = cumulative_loss_;
Qingsi Wang2370b082018-08-21 14:24:26 -0700232 last_report_seq_max_ = received_seq_max_;
gaetano.carlucci61050f62016-09-30 06:29:54 -0700233 BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts",
gaetano.carlucci52a57032016-09-14 05:04:36 -0700234 clock_->TimeInMilliseconds(),
Qingsi Wang2370b082018-08-21 14:24:26 -0700235 cumulative_loss_, ssrc_);
gaetano.carlucci52a57032016-09-14 05:04:36 -0700236 BWE_TEST_LOGGING_PLOT_WITH_SSRC(
gaetano.carlucci61050f62016-09-30 06:29:54 -0700237 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700238 (received_seq_max_ - received_seq_first_), ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000239
Qingsi Wang2370b082018-08-21 14:24:26 -0700240 return stats;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000241}
242
Niels Möller9a9f18a2019-08-02 13:52:37 +0200243absl::optional<int> StreamStatisticianImpl::GetFractionLostInPercent() const {
244 rtc::CritScope cs(&stream_lock_);
Niels Möller1a3859c2019-09-04 09:43:15 +0200245 if (!ReceivedRtpPacket()) {
Niels Möller9a9f18a2019-08-02 13:52:37 +0200246 return absl::nullopt;
247 }
248 int64_t expected_packets = 1 + received_seq_max_ - received_seq_first_;
249 if (expected_packets <= 0) {
250 return absl::nullopt;
251 }
Niels Möller1a3859c2019-09-04 09:43:15 +0200252 if (cumulative_loss_ <= 0) {
253 return 0;
254 }
Niels Möller9a9f18a2019-08-02 13:52:37 +0200255 return 100 * static_cast<int64_t>(cumulative_loss_) / expected_packets;
256}
257
Niels Möller58b496b2019-08-12 12:16:31 +0200258StreamDataCounters StreamStatisticianImpl::GetReceiveStreamDataCounters()
259 const {
danilchap7c9426c2016-04-14 03:05:31 -0700260 rtc::CritScope cs(&stream_lock_);
Niels Möller58b496b2019-08-12 12:16:31 +0200261 return receive_counters_;
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000262}
263
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000264uint32_t StreamStatisticianImpl::BitrateReceived() const {
danilchap7c9426c2016-04-14 03:05:31 -0700265 rtc::CritScope cs(&stream_lock_);
sprangcd349d92016-07-13 09:11:28 -0700266 return incoming_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000267}
268
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000269bool StreamStatisticianImpl::IsRetransmitOfOldPacket(
Danil Chapovalov44727b42018-11-22 11:28:45 +0100270 const RtpPacketReceived& packet,
271 int64_t now_ms) const {
Niels Möllerdbb988b2018-11-15 08:05:16 +0100272 uint32_t frequency_khz = packet.payload_type_frequency() / 1000;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100273 RTC_DCHECK_GT(frequency_khz, 0);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000274
Danil Chapovalov44727b42018-11-22 11:28:45 +0100275 int64_t time_diff_ms = now_ms - last_receive_time_ms_;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000276
277 // Diff in time stamp since last received in order.
Niels Möllerdbb988b2018-11-15 08:05:16 +0100278 uint32_t timestamp_diff = packet.Timestamp() - last_received_timestamp_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000279 uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000280
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000281 int64_t max_delay_ms = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000282
Niels Möllereda00872018-05-23 13:54:51 +0200283 // Jitter standard deviation in samples.
Oleh Prypin19929582019-04-23 08:50:04 +0200284 float jitter_std = std::sqrt(static_cast<float>(jitter_q4_ >> 4));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000285
Niels Möllereda00872018-05-23 13:54:51 +0200286 // 2 times the standard deviation => 95% confidence.
287 // And transform to milliseconds by dividing by the frequency in kHz.
288 max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz);
289
290 // Min max_delay_ms is 1.
291 if (max_delay_ms == 0) {
292 max_delay_ms = 1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000293 }
294 return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms;
295}
296
Niels Möllerd7819652019-08-13 14:43:02 +0200297std::unique_ptr<ReceiveStatistics> ReceiveStatistics::Create(Clock* clock) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200298 return std::make_unique<ReceiveStatisticsImpl>(clock);
Niels Möllerd7819652019-08-13 14:43:02 +0200299}
300
Niels Möllerd7819652019-08-13 14:43:02 +0200301ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock)
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000302 : clock_(clock),
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100303 last_returned_ssrc_(0),
Niels Möllerd7819652019-08-13 14:43:02 +0200304 max_reordering_threshold_(kDefaultMaxReorderingThreshold) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000305
306ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
307 while (!statisticians_.empty()) {
308 delete statisticians_.begin()->second;
309 statisticians_.erase(statisticians_.begin());
310 }
311}
312
Niels Möller1f3206c2018-09-14 08:26:32 +0200313void ReceiveStatisticsImpl::OnRtpPacket(const RtpPacketReceived& packet) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000314 // StreamStatisticianImpl instance is created once and only destroyed when
315 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
316 // it's own locking so don't hold receive_statistics_lock_ (potential
317 // deadlock).
Niels Möller1a3859c2019-09-04 09:43:15 +0200318 GetOrCreateStatistician(packet.Ssrc())->UpdateCounters(packet);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000319}
320
Niels Möller87da1092019-05-24 14:04:28 +0200321StreamStatisticianImpl* ReceiveStatisticsImpl::GetStatistician(
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000322 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700323 rtc::CritScope cs(&receive_statistics_lock_);
Niels Möller87da1092019-05-24 14:04:28 +0200324 const auto& it = statisticians_.find(ssrc);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000325 if (it == statisticians_.end())
326 return NULL;
327 return it->second;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000328}
329
Niels Möller87da1092019-05-24 14:04:28 +0200330StreamStatisticianImpl* ReceiveStatisticsImpl::GetOrCreateStatistician(
331 uint32_t ssrc) {
332 rtc::CritScope cs(&receive_statistics_lock_);
333 StreamStatisticianImpl*& impl = statisticians_[ssrc];
334 if (impl == nullptr) { // new element
Niels Möllerd7819652019-08-13 14:43:02 +0200335 impl = new StreamStatisticianImpl(ssrc, clock_, max_reordering_threshold_);
Niels Möller87da1092019-05-24 14:04:28 +0200336 }
337 return impl;
338}
339
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000340void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
341 int max_reordering_threshold) {
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100342 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
343 {
344 rtc::CritScope cs(&receive_statistics_lock_);
345 max_reordering_threshold_ = max_reordering_threshold;
346 statisticians = statisticians_;
347 }
348 for (auto& statistician : statisticians) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200349 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000350 }
351}
352
Niels Möller87da1092019-05-24 14:04:28 +0200353void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
354 uint32_t ssrc,
355 int max_reordering_threshold) {
356 GetOrCreateStatistician(ssrc)->SetMaxReorderingThreshold(
357 max_reordering_threshold);
358}
359
Niels Möller5304a322018-08-27 13:27:05 +0200360void ReceiveStatisticsImpl::EnableRetransmitDetection(uint32_t ssrc,
361 bool enable) {
Niels Möller87da1092019-05-24 14:04:28 +0200362 GetOrCreateStatistician(ssrc)->EnableRetransmitDetection(enable);
Niels Möller5304a322018-08-27 13:27:05 +0200363}
364
danilchap0bc84232017-08-11 08:12:54 -0700365std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700366 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200367 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
368 {
369 rtc::CritScope cs(&receive_statistics_lock_);
370 statisticians = statisticians_;
371 }
danilchapf5f793c2017-07-27 04:44:18 -0700372 std::vector<rtcp::ReportBlock> result;
373 result.reserve(std::min(max_blocks, statisticians.size()));
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100374 auto add_report_block = [&result](uint32_t media_ssrc,
375 StreamStatisticianImpl* statistician) {
danilchapf5f793c2017-07-27 04:44:18 -0700376 // Do we have receive statistics to send?
377 RtcpStatistics stats;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100378 if (!statistician->GetActiveStatisticsAndReset(&stats))
379 return;
danilchapf5f793c2017-07-27 04:44:18 -0700380 result.emplace_back();
381 rtcp::ReportBlock& block = result.back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100382 block.SetMediaSsrc(media_ssrc);
danilchapf5f793c2017-07-27 04:44:18 -0700383 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700384 if (!block.SetCumulativeLost(stats.packets_lost)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100385 RTC_LOG(LS_WARNING) << "Cumulative lost is oversized.";
danilchapf5f793c2017-07-27 04:44:18 -0700386 result.pop_back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100387 return;
danilchapf5f793c2017-07-27 04:44:18 -0700388 }
srte186d9c32017-08-04 05:03:53 -0700389 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700390 block.SetJitter(stats.jitter);
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100391 };
392
393 const auto start_it = statisticians.upper_bound(last_returned_ssrc_);
394 for (auto it = start_it;
395 result.size() < max_blocks && it != statisticians.end(); ++it)
396 add_report_block(it->first, it->second);
397 for (auto it = statisticians.begin();
398 result.size() < max_blocks && it != start_it; ++it)
399 add_report_block(it->first, it->second);
400
401 if (!result.empty())
402 last_returned_ssrc_ = result.back().source_ssrc();
danilchapf5f793c2017-07-27 04:44:18 -0700403 return result;
404}
405
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000406} // namespace webrtc