blob: 60349c446d380390b0a6ca07ffdcfe4f3caaa5d8 [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
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000185bool StreamStatisticianImpl::GetStatistics(RtcpStatistics* statistics,
Qingsi Wang2370b082018-08-21 14:24:26 -0700186 bool reset) {
Niels Möller12ebfa62019-08-06 16:04:12 +0200187 rtc::CritScope cs(&stream_lock_);
188 if (!ReceivedRtpPacket()) {
189 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000190 }
191
Niels Möller12ebfa62019-08-06 16:04:12 +0200192 if (!reset) {
Niels Möller1a3859c2019-09-04 09:43:15 +0200193 if (!ReceivedRtpPacket()) {
Niels Möller12ebfa62019-08-06 16:04:12 +0200194 // No report.
195 return false;
196 }
197 // Just get last report.
198 *statistics = last_reported_statistics_;
199 return true;
200 }
201
202 *statistics = CalculateRtcpStatistics();
203
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000204 return true;
205}
206
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200207bool StreamStatisticianImpl::GetActiveStatisticsAndReset(
208 RtcpStatistics* statistics) {
Niels Möller12ebfa62019-08-06 16:04:12 +0200209 rtc::CritScope cs(&stream_lock_);
210 if (clock_->TimeInMilliseconds() - last_receive_time_ms_ >=
211 kStatisticsTimeoutMs) {
212 // Not active.
213 return false;
214 }
215 if (!ReceivedRtpPacket()) {
216 return false;
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200217 }
218
Niels Möller12ebfa62019-08-06 16:04:12 +0200219 *statistics = CalculateRtcpStatistics();
220
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200221 return true;
222}
223
Qingsi Wang2370b082018-08-21 14:24:26 -0700224RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics() {
225 RtcpStatistics stats;
Qingsi Wang2370b082018-08-21 14:24:26 -0700226 // Calculate fraction lost.
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000227 int64_t exp_since_last = received_seq_max_ - last_report_seq_max_;
228 RTC_DCHECK_GE(exp_since_last, 0);
Qingsi Wang2370b082018-08-21 14:24:26 -0700229
Niels Möller1a3859c2019-09-04 09:43:15 +0200230 int32_t lost_since_last = cumulative_loss_ - last_report_cumulative_loss_;
231 if (exp_since_last > 0 && lost_since_last > 0) {
Qingsi Wang2370b082018-08-21 14:24:26 -0700232 // Scale 0 to 255, where 255 is 100% loss.
Niels Möller1a3859c2019-09-04 09:43:15 +0200233 stats.fraction_lost =
234 static_cast<uint8_t>(255 * lost_since_last / exp_since_last);
235 } else {
236 stats.fraction_lost = 0;
Qingsi Wang2370b082018-08-21 14:24:26 -0700237 }
Qingsi Wang2370b082018-08-21 14:24:26 -0700238
Niels Möller1a3859c2019-09-04 09:43:15 +0200239 // TODO(danilchap): Ensure |stats.packets_lost| is clamped to fit in a signed
240 // 24-bit value.
241 stats.packets_lost = cumulative_loss_ + cumulative_loss_rtcp_offset_;
242 if (stats.packets_lost < 0) {
243 // Clamp to zero. Work around to accomodate for senders that misbehave with
244 // negative cumulative loss.
245 stats.packets_lost = 0;
246 cumulative_loss_rtcp_offset_ = -cumulative_loss_;
247 }
Qingsi Wang2370b082018-08-21 14:24:26 -0700248 stats.extended_highest_sequence_number =
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000249 static_cast<uint32_t>(received_seq_max_);
Qingsi Wang2370b082018-08-21 14:24:26 -0700250 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
251 stats.jitter = jitter_q4_ >> 4;
252
253 // Store this report.
254 last_reported_statistics_ = stats;
255
256 // Only for report blocks in RTCP SR and RR.
Niels Möller1a3859c2019-09-04 09:43:15 +0200257 last_report_cumulative_loss_ = cumulative_loss_;
Qingsi Wang2370b082018-08-21 14:24:26 -0700258 last_report_seq_max_ = received_seq_max_;
gaetano.carlucci61050f62016-09-30 06:29:54 -0700259 BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts",
gaetano.carlucci52a57032016-09-14 05:04:36 -0700260 clock_->TimeInMilliseconds(),
Qingsi Wang2370b082018-08-21 14:24:26 -0700261 cumulative_loss_, ssrc_);
gaetano.carlucci52a57032016-09-14 05:04:36 -0700262 BWE_TEST_LOGGING_PLOT_WITH_SSRC(
gaetano.carlucci61050f62016-09-30 06:29:54 -0700263 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700264 (received_seq_max_ - received_seq_first_), ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000265
Qingsi Wang2370b082018-08-21 14:24:26 -0700266 return stats;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000267}
268
Niels Möller9a9f18a2019-08-02 13:52:37 +0200269absl::optional<int> StreamStatisticianImpl::GetFractionLostInPercent() const {
270 rtc::CritScope cs(&stream_lock_);
Niels Möller1a3859c2019-09-04 09:43:15 +0200271 if (!ReceivedRtpPacket()) {
Niels Möller9a9f18a2019-08-02 13:52:37 +0200272 return absl::nullopt;
273 }
274 int64_t expected_packets = 1 + received_seq_max_ - received_seq_first_;
275 if (expected_packets <= 0) {
276 return absl::nullopt;
277 }
Niels Möller1a3859c2019-09-04 09:43:15 +0200278 if (cumulative_loss_ <= 0) {
279 return 0;
280 }
Niels Möller9a9f18a2019-08-02 13:52:37 +0200281 return 100 * static_cast<int64_t>(cumulative_loss_) / expected_packets;
282}
283
Niels Möller58b496b2019-08-12 12:16:31 +0200284StreamDataCounters StreamStatisticianImpl::GetReceiveStreamDataCounters()
285 const {
danilchap7c9426c2016-04-14 03:05:31 -0700286 rtc::CritScope cs(&stream_lock_);
Niels Möller58b496b2019-08-12 12:16:31 +0200287 return receive_counters_;
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000288}
289
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000290uint32_t StreamStatisticianImpl::BitrateReceived() const {
danilchap7c9426c2016-04-14 03:05:31 -0700291 rtc::CritScope cs(&stream_lock_);
sprangcd349d92016-07-13 09:11:28 -0700292 return incoming_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000293}
294
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000295bool StreamStatisticianImpl::IsRetransmitOfOldPacket(
Danil Chapovalov44727b42018-11-22 11:28:45 +0100296 const RtpPacketReceived& packet,
297 int64_t now_ms) const {
Niels Möllerdbb988b2018-11-15 08:05:16 +0100298 uint32_t frequency_khz = packet.payload_type_frequency() / 1000;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100299 RTC_DCHECK_GT(frequency_khz, 0);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000300
Danil Chapovalov44727b42018-11-22 11:28:45 +0100301 int64_t time_diff_ms = now_ms - last_receive_time_ms_;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000302
303 // Diff in time stamp since last received in order.
Niels Möllerdbb988b2018-11-15 08:05:16 +0100304 uint32_t timestamp_diff = packet.Timestamp() - last_received_timestamp_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000305 uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000306
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000307 int64_t max_delay_ms = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000308
Niels Möllereda00872018-05-23 13:54:51 +0200309 // Jitter standard deviation in samples.
Oleh Prypin19929582019-04-23 08:50:04 +0200310 float jitter_std = std::sqrt(static_cast<float>(jitter_q4_ >> 4));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000311
Niels Möllereda00872018-05-23 13:54:51 +0200312 // 2 times the standard deviation => 95% confidence.
313 // And transform to milliseconds by dividing by the frequency in kHz.
314 max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz);
315
316 // Min max_delay_ms is 1.
317 if (max_delay_ms == 0) {
318 max_delay_ms = 1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000319 }
320 return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms;
321}
322
Niels Möllerd7819652019-08-13 14:43:02 +0200323std::unique_ptr<ReceiveStatistics> ReceiveStatistics::Create(Clock* clock) {
324 return absl::make_unique<ReceiveStatisticsImpl>(clock);
325}
326
Niels Möllerd7819652019-08-13 14:43:02 +0200327ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock)
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000328 : clock_(clock),
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100329 last_returned_ssrc_(0),
Niels Möllerd7819652019-08-13 14:43:02 +0200330 max_reordering_threshold_(kDefaultMaxReorderingThreshold) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000331
332ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
333 while (!statisticians_.empty()) {
334 delete statisticians_.begin()->second;
335 statisticians_.erase(statisticians_.begin());
336 }
337}
338
Niels Möller1f3206c2018-09-14 08:26:32 +0200339void ReceiveStatisticsImpl::OnRtpPacket(const RtpPacketReceived& packet) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000340 // StreamStatisticianImpl instance is created once and only destroyed when
341 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
342 // it's own locking so don't hold receive_statistics_lock_ (potential
343 // deadlock).
Niels Möller1a3859c2019-09-04 09:43:15 +0200344 GetOrCreateStatistician(packet.Ssrc())->UpdateCounters(packet);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000345}
346
Niels Möller87da1092019-05-24 14:04:28 +0200347StreamStatisticianImpl* ReceiveStatisticsImpl::GetStatistician(
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000348 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700349 rtc::CritScope cs(&receive_statistics_lock_);
Niels Möller87da1092019-05-24 14:04:28 +0200350 const auto& it = statisticians_.find(ssrc);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000351 if (it == statisticians_.end())
352 return NULL;
353 return it->second;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000354}
355
Niels Möller87da1092019-05-24 14:04:28 +0200356StreamStatisticianImpl* ReceiveStatisticsImpl::GetOrCreateStatistician(
357 uint32_t ssrc) {
358 rtc::CritScope cs(&receive_statistics_lock_);
359 StreamStatisticianImpl*& impl = statisticians_[ssrc];
360 if (impl == nullptr) { // new element
Niels Möllerd7819652019-08-13 14:43:02 +0200361 impl = new StreamStatisticianImpl(ssrc, clock_, max_reordering_threshold_);
Niels Möller87da1092019-05-24 14:04:28 +0200362 }
363 return impl;
364}
365
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000366void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
367 int max_reordering_threshold) {
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100368 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
369 {
370 rtc::CritScope cs(&receive_statistics_lock_);
371 max_reordering_threshold_ = max_reordering_threshold;
372 statisticians = statisticians_;
373 }
374 for (auto& statistician : statisticians) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200375 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000376 }
377}
378
Niels Möller87da1092019-05-24 14:04:28 +0200379void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
380 uint32_t ssrc,
381 int max_reordering_threshold) {
382 GetOrCreateStatistician(ssrc)->SetMaxReorderingThreshold(
383 max_reordering_threshold);
384}
385
Niels Möller5304a322018-08-27 13:27:05 +0200386void ReceiveStatisticsImpl::EnableRetransmitDetection(uint32_t ssrc,
387 bool enable) {
Niels Möller87da1092019-05-24 14:04:28 +0200388 GetOrCreateStatistician(ssrc)->EnableRetransmitDetection(enable);
Niels Möller5304a322018-08-27 13:27:05 +0200389}
390
danilchap0bc84232017-08-11 08:12:54 -0700391std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700392 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200393 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
394 {
395 rtc::CritScope cs(&receive_statistics_lock_);
396 statisticians = statisticians_;
397 }
danilchapf5f793c2017-07-27 04:44:18 -0700398 std::vector<rtcp::ReportBlock> result;
399 result.reserve(std::min(max_blocks, statisticians.size()));
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100400 auto add_report_block = [&result](uint32_t media_ssrc,
401 StreamStatisticianImpl* statistician) {
danilchapf5f793c2017-07-27 04:44:18 -0700402 // Do we have receive statistics to send?
403 RtcpStatistics stats;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100404 if (!statistician->GetActiveStatisticsAndReset(&stats))
405 return;
danilchapf5f793c2017-07-27 04:44:18 -0700406 result.emplace_back();
407 rtcp::ReportBlock& block = result.back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100408 block.SetMediaSsrc(media_ssrc);
danilchapf5f793c2017-07-27 04:44:18 -0700409 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700410 if (!block.SetCumulativeLost(stats.packets_lost)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100411 RTC_LOG(LS_WARNING) << "Cumulative lost is oversized.";
danilchapf5f793c2017-07-27 04:44:18 -0700412 result.pop_back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100413 return;
danilchapf5f793c2017-07-27 04:44:18 -0700414 }
srte186d9c32017-08-04 05:03:53 -0700415 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700416 block.SetJitter(stats.jitter);
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100417 };
418
419 const auto start_it = statisticians.upper_bound(last_returned_ssrc_);
420 for (auto it = start_it;
421 result.size() < max_blocks && it != statisticians.end(); ++it)
422 add_report_block(it->first, it->second);
423 for (auto it = statisticians.begin();
424 result.size() < max_blocks && it != start_it; ++it)
425 add_report_block(it->first, it->second);
426
427 if (!result.empty())
428 last_returned_ssrc_ = result.back().source_ssrc();
danilchapf5f793c2017-07-27 04:44:18 -0700429 return result;
430}
431
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000432} // namespace webrtc