blob: 4720776d819bc875e0135c51fde962cb1a02b4fb [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),
Niels Möller1a3859c2019-09-04 09:43:15 +020044 cumulative_loss_rtcp_offset_(0),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000045 last_receive_time_ms_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000046 last_received_timestamp_(0),
Niels Möller1a3859c2019-09-04 09:43:15 +020047 received_seq_first_(-1),
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000048 received_seq_max_(-1),
Niels Möller1a3859c2019-09-04 09:43:15 +020049 last_report_cumulative_loss_(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
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000054bool StreamStatisticianImpl::UpdateOutOfOrder(const RtpPacketReceived& packet,
55 int64_t sequence_number,
56 int64_t now_ms) {
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000057 // Check if |packet| is second packet of a stream restart.
58 if (received_seq_out_of_order_) {
Niels Möller1a3859c2019-09-04 09:43:15 +020059 // Count the previous packet as a received; it was postponed below.
60 --cumulative_loss_;
61
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000062 uint16_t expected_sequence_number = *received_seq_out_of_order_ + 1;
63 received_seq_out_of_order_ = absl::nullopt;
64 if (packet.SequenceNumber() == expected_sequence_number) {
Niels Möller1a3859c2019-09-04 09:43:15 +020065 // Ignore sequence number gap caused by stream restart for packet loss
66 // calculation, by setting received_seq_max_ to the sequence number just
67 // before the out-of-order seqno. This gives a net zero change of
68 // |cumulative_loss_|, for the two packets interpreted as a stream reset.
69 //
70 // Fraction loss for the next report may get a bit off, since we don't
71 // update last_report_seq_max_ and last_report_cumulative_loss_ in a
72 // consistent way.
73 last_report_seq_max_ = sequence_number - 2;
74 received_seq_max_ = sequence_number - 2;
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000075 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();
Niels Möller1a3859c2019-09-04 09:43:15 +020084 // Postpone counting this as a received packet until we know how to update
85 // |received_seq_max_|, otherwise we temporarily decrement
86 // |cumulative_loss_|. The
87 // ReceiveStatisticsTest.StreamRestartDoesntCountAsLoss test expects
88 // |cumulative_loss_| to be unchanged by the reception of the first packet
89 // after stream reset.
90 ++cumulative_loss_;
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000091 return true;
92 }
93
94 if (sequence_number > received_seq_max_)
95 return false;
96
97 // Old out of order packet, may be retransmit.
98 if (enable_retransmit_detection_ && IsRetransmitOfOldPacket(packet, now_ms))
99 receive_counters_.retransmitted.AddPacket(packet);
100 return true;
101}
102
Niels Möller1a3859c2019-09-04 09:43:15 +0200103void StreamStatisticianImpl::UpdateCounters(const RtpPacketReceived& packet) {
Danil Chapovalov44727b42018-11-22 11:28:45 +0100104 rtc::CritScope cs(&stream_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +0100105 RTC_DCHECK_EQ(ssrc_, packet.Ssrc());
Danil Chapovalov44727b42018-11-22 11:28:45 +0100106 int64_t now_ms = clock_->TimeInMilliseconds();
107
108 incoming_bitrate_.Update(packet.size(), now_ms);
Henrik Boströmcb755b02019-04-02 15:11:48 +0200109 receive_counters_.last_packet_received_timestamp_ms = now_ms;
Niels Möllerdbb988b2018-11-15 08:05:16 +0100110 receive_counters_.transmitted.AddPacket(packet);
Niels Möller1a3859c2019-09-04 09:43:15 +0200111 --cumulative_loss_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000112
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000113 int64_t sequence_number =
114 seq_unwrapper_.UnwrapWithoutUpdate(packet.SequenceNumber());
Niels Möller1a3859c2019-09-04 09:43:15 +0200115
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000116 if (!ReceivedRtpPacket()) {
117 received_seq_first_ = sequence_number;
118 last_report_seq_max_ = sequence_number - 1;
Niels Möller1a3859c2019-09-04 09:43:15 +0200119 received_seq_max_ = sequence_number - 1;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100120 receive_counters_.first_packet_time_ms = now_ms;
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000121 } else if (UpdateOutOfOrder(packet, sequence_number, now_ms)) {
Niels Möller1a3859c2019-09-04 09:43:15 +0200122 return;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000123 }
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000124 // In order packet.
Niels Möller1a3859c2019-09-04 09:43:15 +0200125 cumulative_loss_ += sequence_number - received_seq_max_;
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000126 received_seq_max_ = sequence_number;
127 seq_unwrapper_.UpdateLast(sequence_number);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000128
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000129 // If new time stamp and more than one in-order packet received, calculate
130 // new jitter statistics.
131 if (packet.Timestamp() != last_received_timestamp_ &&
132 (receive_counters_.transmitted.packets -
133 receive_counters_.retransmitted.packets) > 1) {
134 UpdateJitter(packet, now_ms);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000135 }
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000136 last_received_timestamp_ = packet.Timestamp();
137 last_receive_time_ms_ = now_ms;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000138}
139
Niels Möllerdbb988b2018-11-15 08:05:16 +0100140void StreamStatisticianImpl::UpdateJitter(const RtpPacketReceived& packet,
Danil Chapovalov856cf222018-11-26 10:20:01 +0100141 int64_t receive_time_ms) {
142 int64_t receive_diff_ms = receive_time_ms - last_receive_time_ms_;
143 RTC_DCHECK_GE(receive_diff_ms, 0);
144 uint32_t receive_diff_rtp = static_cast<uint32_t>(
145 (receive_diff_ms * packet.payload_type_frequency()) / 1000);
146 int32_t time_diff_samples =
147 receive_diff_rtp - (packet.Timestamp() - last_received_timestamp_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000148
kwibergfd8be342016-05-14 19:44:11 -0700149 time_diff_samples = std::abs(time_diff_samples);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000150
151 // lib_jingle sometimes deliver crazy jumps in TS for the same stream.
152 // If this happens, don't update jitter value. Use 5 secs video frequency
153 // as the threshold.
154 if (time_diff_samples < 450000) {
155 // Note we calculate in Q4 to avoid using float.
156 int32_t jitter_diff_q4 = (time_diff_samples << 4) - jitter_q4_;
157 jitter_q4_ += ((jitter_diff_q4 + 8) >> 4);
158 }
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000159}
160
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000161void StreamStatisticianImpl::SetMaxReorderingThreshold(
162 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700163 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000164 max_reordering_threshold_ = max_reordering_threshold;
165}
166
Niels Möller5304a322018-08-27 13:27:05 +0200167void StreamStatisticianImpl::EnableRetransmitDetection(bool enable) {
168 rtc::CritScope cs(&stream_lock_);
169 enable_retransmit_detection_ = enable;
170}
171
Niels Möllerd77cc242019-08-22 09:40:25 +0200172RtpReceiveStats StreamStatisticianImpl::GetStats() const {
173 rtc::CritScope cs(&stream_lock_);
174 RtpReceiveStats stats;
175 stats.packets_lost = cumulative_loss_;
176 // TODO(nisse): Can we return a float instead?
177 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
178 stats.jitter = jitter_q4_ >> 4;
179 stats.last_packet_received_timestamp_ms =
180 receive_counters_.last_packet_received_timestamp_ms;
181 stats.packet_counter = receive_counters_.transmitted;
182 return stats;
183}
184
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200185bool StreamStatisticianImpl::GetActiveStatisticsAndReset(
186 RtcpStatistics* statistics) {
Niels Möller12ebfa62019-08-06 16:04:12 +0200187 rtc::CritScope cs(&stream_lock_);
188 if (clock_->TimeInMilliseconds() - last_receive_time_ms_ >=
189 kStatisticsTimeoutMs) {
190 // Not active.
191 return false;
192 }
193 if (!ReceivedRtpPacket()) {
194 return false;
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200195 }
196
Niels Möller12ebfa62019-08-06 16:04:12 +0200197 *statistics = CalculateRtcpStatistics();
198
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200199 return true;
200}
201
Qingsi Wang2370b082018-08-21 14:24:26 -0700202RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics() {
203 RtcpStatistics stats;
Qingsi Wang2370b082018-08-21 14:24:26 -0700204 // Calculate fraction lost.
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000205 int64_t exp_since_last = received_seq_max_ - last_report_seq_max_;
206 RTC_DCHECK_GE(exp_since_last, 0);
Qingsi Wang2370b082018-08-21 14:24:26 -0700207
Niels Möller1a3859c2019-09-04 09:43:15 +0200208 int32_t lost_since_last = cumulative_loss_ - last_report_cumulative_loss_;
209 if (exp_since_last > 0 && lost_since_last > 0) {
Qingsi Wang2370b082018-08-21 14:24:26 -0700210 // Scale 0 to 255, where 255 is 100% loss.
Niels Möller1a3859c2019-09-04 09:43:15 +0200211 stats.fraction_lost =
212 static_cast<uint8_t>(255 * lost_since_last / exp_since_last);
213 } else {
214 stats.fraction_lost = 0;
Qingsi Wang2370b082018-08-21 14:24:26 -0700215 }
Qingsi Wang2370b082018-08-21 14:24:26 -0700216
Niels Möller1a3859c2019-09-04 09:43:15 +0200217 // TODO(danilchap): Ensure |stats.packets_lost| is clamped to fit in a signed
218 // 24-bit value.
219 stats.packets_lost = cumulative_loss_ + cumulative_loss_rtcp_offset_;
220 if (stats.packets_lost < 0) {
221 // Clamp to zero. Work around to accomodate for senders that misbehave with
222 // negative cumulative loss.
223 stats.packets_lost = 0;
224 cumulative_loss_rtcp_offset_ = -cumulative_loss_;
225 }
Qingsi Wang2370b082018-08-21 14:24:26 -0700226 stats.extended_highest_sequence_number =
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000227 static_cast<uint32_t>(received_seq_max_);
Qingsi Wang2370b082018-08-21 14:24:26 -0700228 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
229 stats.jitter = jitter_q4_ >> 4;
230
Qingsi Wang2370b082018-08-21 14:24:26 -0700231 // Only for report blocks in RTCP SR and RR.
Niels Möller1a3859c2019-09-04 09:43:15 +0200232 last_report_cumulative_loss_ = cumulative_loss_;
Qingsi Wang2370b082018-08-21 14:24:26 -0700233 last_report_seq_max_ = received_seq_max_;
gaetano.carlucci61050f62016-09-30 06:29:54 -0700234 BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts",
gaetano.carlucci52a57032016-09-14 05:04:36 -0700235 clock_->TimeInMilliseconds(),
Qingsi Wang2370b082018-08-21 14:24:26 -0700236 cumulative_loss_, ssrc_);
gaetano.carlucci52a57032016-09-14 05:04:36 -0700237 BWE_TEST_LOGGING_PLOT_WITH_SSRC(
gaetano.carlucci61050f62016-09-30 06:29:54 -0700238 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700239 (received_seq_max_ - received_seq_first_), ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000240
Qingsi Wang2370b082018-08-21 14:24:26 -0700241 return stats;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000242}
243
Niels Möller9a9f18a2019-08-02 13:52:37 +0200244absl::optional<int> StreamStatisticianImpl::GetFractionLostInPercent() const {
245 rtc::CritScope cs(&stream_lock_);
Niels Möller1a3859c2019-09-04 09:43:15 +0200246 if (!ReceivedRtpPacket()) {
Niels Möller9a9f18a2019-08-02 13:52:37 +0200247 return absl::nullopt;
248 }
249 int64_t expected_packets = 1 + received_seq_max_ - received_seq_first_;
250 if (expected_packets <= 0) {
251 return absl::nullopt;
252 }
Niels Möller1a3859c2019-09-04 09:43:15 +0200253 if (cumulative_loss_ <= 0) {
254 return 0;
255 }
Niels Möller9a9f18a2019-08-02 13:52:37 +0200256 return 100 * static_cast<int64_t>(cumulative_loss_) / expected_packets;
257}
258
Niels Möller58b496b2019-08-12 12:16:31 +0200259StreamDataCounters StreamStatisticianImpl::GetReceiveStreamDataCounters()
260 const {
danilchap7c9426c2016-04-14 03:05:31 -0700261 rtc::CritScope cs(&stream_lock_);
Niels Möller58b496b2019-08-12 12:16:31 +0200262 return receive_counters_;
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000263}
264
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000265uint32_t StreamStatisticianImpl::BitrateReceived() const {
danilchap7c9426c2016-04-14 03:05:31 -0700266 rtc::CritScope cs(&stream_lock_);
sprangcd349d92016-07-13 09:11:28 -0700267 return incoming_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000268}
269
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000270bool StreamStatisticianImpl::IsRetransmitOfOldPacket(
Danil Chapovalov44727b42018-11-22 11:28:45 +0100271 const RtpPacketReceived& packet,
272 int64_t now_ms) const {
Niels Möllerdbb988b2018-11-15 08:05:16 +0100273 uint32_t frequency_khz = packet.payload_type_frequency() / 1000;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100274 RTC_DCHECK_GT(frequency_khz, 0);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000275
Danil Chapovalov44727b42018-11-22 11:28:45 +0100276 int64_t time_diff_ms = now_ms - last_receive_time_ms_;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000277
278 // Diff in time stamp since last received in order.
Niels Möllerdbb988b2018-11-15 08:05:16 +0100279 uint32_t timestamp_diff = packet.Timestamp() - last_received_timestamp_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000280 uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000281
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000282 int64_t max_delay_ms = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000283
Niels Möllereda00872018-05-23 13:54:51 +0200284 // Jitter standard deviation in samples.
Oleh Prypin19929582019-04-23 08:50:04 +0200285 float jitter_std = std::sqrt(static_cast<float>(jitter_q4_ >> 4));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000286
Niels Möllereda00872018-05-23 13:54:51 +0200287 // 2 times the standard deviation => 95% confidence.
288 // And transform to milliseconds by dividing by the frequency in kHz.
289 max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz);
290
291 // Min max_delay_ms is 1.
292 if (max_delay_ms == 0) {
293 max_delay_ms = 1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000294 }
295 return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms;
296}
297
Niels Möllerd7819652019-08-13 14:43:02 +0200298std::unique_ptr<ReceiveStatistics> ReceiveStatistics::Create(Clock* clock) {
299 return absl::make_unique<ReceiveStatisticsImpl>(clock);
300}
301
Niels Möllerd7819652019-08-13 14:43:02 +0200302ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock)
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000303 : clock_(clock),
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100304 last_returned_ssrc_(0),
Niels Möllerd7819652019-08-13 14:43:02 +0200305 max_reordering_threshold_(kDefaultMaxReorderingThreshold) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000306
307ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
308 while (!statisticians_.empty()) {
309 delete statisticians_.begin()->second;
310 statisticians_.erase(statisticians_.begin());
311 }
312}
313
Niels Möller1f3206c2018-09-14 08:26:32 +0200314void ReceiveStatisticsImpl::OnRtpPacket(const RtpPacketReceived& packet) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000315 // StreamStatisticianImpl instance is created once and only destroyed when
316 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
317 // it's own locking so don't hold receive_statistics_lock_ (potential
318 // deadlock).
Niels Möller1a3859c2019-09-04 09:43:15 +0200319 GetOrCreateStatistician(packet.Ssrc())->UpdateCounters(packet);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000320}
321
Niels Möller87da1092019-05-24 14:04:28 +0200322StreamStatisticianImpl* ReceiveStatisticsImpl::GetStatistician(
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000323 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700324 rtc::CritScope cs(&receive_statistics_lock_);
Niels Möller87da1092019-05-24 14:04:28 +0200325 const auto& it = statisticians_.find(ssrc);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000326 if (it == statisticians_.end())
327 return NULL;
328 return it->second;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000329}
330
Niels Möller87da1092019-05-24 14:04:28 +0200331StreamStatisticianImpl* ReceiveStatisticsImpl::GetOrCreateStatistician(
332 uint32_t ssrc) {
333 rtc::CritScope cs(&receive_statistics_lock_);
334 StreamStatisticianImpl*& impl = statisticians_[ssrc];
335 if (impl == nullptr) { // new element
Niels Möllerd7819652019-08-13 14:43:02 +0200336 impl = new StreamStatisticianImpl(ssrc, clock_, max_reordering_threshold_);
Niels Möller87da1092019-05-24 14:04:28 +0200337 }
338 return impl;
339}
340
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000341void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
342 int max_reordering_threshold) {
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100343 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
344 {
345 rtc::CritScope cs(&receive_statistics_lock_);
346 max_reordering_threshold_ = max_reordering_threshold;
347 statisticians = statisticians_;
348 }
349 for (auto& statistician : statisticians) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200350 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000351 }
352}
353
Niels Möller87da1092019-05-24 14:04:28 +0200354void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
355 uint32_t ssrc,
356 int max_reordering_threshold) {
357 GetOrCreateStatistician(ssrc)->SetMaxReorderingThreshold(
358 max_reordering_threshold);
359}
360
Niels Möller5304a322018-08-27 13:27:05 +0200361void ReceiveStatisticsImpl::EnableRetransmitDetection(uint32_t ssrc,
362 bool enable) {
Niels Möller87da1092019-05-24 14:04:28 +0200363 GetOrCreateStatistician(ssrc)->EnableRetransmitDetection(enable);
Niels Möller5304a322018-08-27 13:27:05 +0200364}
365
danilchap0bc84232017-08-11 08:12:54 -0700366std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700367 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200368 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
369 {
370 rtc::CritScope cs(&receive_statistics_lock_);
371 statisticians = statisticians_;
372 }
danilchapf5f793c2017-07-27 04:44:18 -0700373 std::vector<rtcp::ReportBlock> result;
374 result.reserve(std::min(max_blocks, statisticians.size()));
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100375 auto add_report_block = [&result](uint32_t media_ssrc,
376 StreamStatisticianImpl* statistician) {
danilchapf5f793c2017-07-27 04:44:18 -0700377 // Do we have receive statistics to send?
378 RtcpStatistics stats;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100379 if (!statistician->GetActiveStatisticsAndReset(&stats))
380 return;
danilchapf5f793c2017-07-27 04:44:18 -0700381 result.emplace_back();
382 rtcp::ReportBlock& block = result.back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100383 block.SetMediaSsrc(media_ssrc);
danilchapf5f793c2017-07-27 04:44:18 -0700384 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700385 if (!block.SetCumulativeLost(stats.packets_lost)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100386 RTC_LOG(LS_WARNING) << "Cumulative lost is oversized.";
danilchapf5f793c2017-07-27 04:44:18 -0700387 result.pop_back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100388 return;
danilchapf5f793c2017-07-27 04:44:18 -0700389 }
srte186d9c32017-08-04 05:03:53 -0700390 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700391 block.SetJitter(stats.jitter);
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100392 };
393
394 const auto start_it = statisticians.upper_bound(last_returned_ssrc_);
395 for (auto it = start_it;
396 result.size() < max_blocks && it != statisticians.end(); ++it)
397 add_report_block(it->first, it->second);
398 for (auto it = statisticians.begin();
399 result.size() < max_blocks && it != start_it; ++it)
400 add_report_block(it->first, it->second);
401
402 if (!result.empty())
403 last_returned_ssrc_ = result.back().source_ssrc();
danilchapf5f793c2017-07-27 04:44:18 -0700404 return result;
405}
406
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000407} // namespace webrtc