blob: 45be4d1d39e35b7c68150f74e86d3166478ef1a9 [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
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000033StreamStatisticianImpl::StreamStatisticianImpl(
danilchapec86be02017-08-14 05:51:02 -070034 uint32_t ssrc,
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000035 Clock* clock,
Danil Chapovalovebb50c22018-11-22 14:04:02 +010036 int max_reordering_threshold,
sprang@webrtc.org0e932572014-01-23 10:00:39 +000037 RtcpStatisticsCallback* rtcp_callback,
38 StreamDataCountersCallback* rtp_callback)
danilchapec86be02017-08-14 05:51:02 -070039 : ssrc_(ssrc),
40 clock_(clock),
sprangcd349d92016-07-13 09:11:28 -070041 incoming_bitrate_(kStatisticsProcessIntervalMs,
42 RateStatistics::kBpsScale),
Danil Chapovalovebb50c22018-11-22 14:04:02 +010043 max_reordering_threshold_(max_reordering_threshold),
Niels Möller87da1092019-05-24 14:04:28 +020044 enable_retransmit_detection_(false),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000045 jitter_q4_(0),
Qingsi Wang2370b082018-08-21 14:24:26 -070046 cumulative_loss_(0),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000047 last_receive_time_ms_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000048 last_received_timestamp_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000049 received_seq_first_(0),
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000050 received_seq_max_(-1),
Qingsi Wang2370b082018-08-21 14:24:26 -070051 last_report_inorder_packets_(0),
52 last_report_old_packets_(0),
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000053 last_report_seq_max_(-1),
sprang@webrtc.org0e932572014-01-23 10:00:39 +000054 rtcp_callback_(rtcp_callback),
55 rtp_callback_(rtp_callback) {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000056
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010057StreamStatisticianImpl::~StreamStatisticianImpl() = default;
58
Niels Möllerdbb988b2018-11-15 08:05:16 +010059void StreamStatisticianImpl::OnRtpPacket(const RtpPacketReceived& packet) {
Danil Chapovalov44727b42018-11-22 11:28:45 +010060 StreamDataCounters counters = UpdateCounters(packet);
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +010061 if (rtp_callback_)
62 rtp_callback_->DataCountersUpdated(counters, ssrc_);
sprang@webrtc.orga45cac02014-01-27 16:22:08 +000063}
64
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000065bool StreamStatisticianImpl::UpdateOutOfOrder(const RtpPacketReceived& packet,
66 int64_t sequence_number,
67 int64_t now_ms) {
68 RTC_DCHECK_EQ(sequence_number,
69 seq_unwrapper_.UnwrapWithoutUpdate(packet.SequenceNumber()));
70
71 // Check if |packet| is second packet of a stream restart.
72 if (received_seq_out_of_order_) {
73 uint16_t expected_sequence_number = *received_seq_out_of_order_ + 1;
74 received_seq_out_of_order_ = absl::nullopt;
75 if (packet.SequenceNumber() == expected_sequence_number) {
76 // Ignore sequence number gap caused by stream restart for next packet
77 // loss calculation.
78 last_report_seq_max_ = sequence_number;
79 last_report_inorder_packets_ = receive_counters_.transmitted.packets -
80 receive_counters_.retransmitted.packets;
81 // As final part of stream restart consider |packet| is not out of order.
82 return false;
83 }
84 }
85
86 if (std::abs(sequence_number - received_seq_max_) >
87 max_reordering_threshold_) {
88 // Sequence number gap looks too large, wait until next packet to check
89 // for a stream restart.
90 received_seq_out_of_order_ = packet.SequenceNumber();
91 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
danilchapec86be02017-08-14 05:51:02 -0700103StreamDataCounters StreamStatisticianImpl::UpdateCounters(
Danil Chapovalov44727b42018-11-22 11:28:45 +0100104 const RtpPacketReceived& packet) {
105 rtc::CritScope cs(&stream_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +0100106 RTC_DCHECK_EQ(ssrc_, packet.Ssrc());
Danil Chapovalov44727b42018-11-22 11:28:45 +0100107 int64_t now_ms = clock_->TimeInMilliseconds();
108
109 incoming_bitrate_.Update(packet.size(), now_ms);
Henrik Boströmcb755b02019-04-02 15:11:48 +0200110 receive_counters_.last_packet_received_timestamp_ms = now_ms;
Niels Möllerdbb988b2018-11-15 08:05:16 +0100111 receive_counters_.transmitted.AddPacket(packet);
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());
115 if (!ReceivedRtpPacket()) {
116 received_seq_first_ = sequence_number;
117 last_report_seq_max_ = sequence_number - 1;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100118 receive_counters_.first_packet_time_ms = now_ms;
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000119 } else if (UpdateOutOfOrder(packet, sequence_number, now_ms)) {
120 return receive_counters_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000121 }
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000122 // In order packet.
123 received_seq_max_ = sequence_number;
124 seq_unwrapper_.UpdateLast(sequence_number);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000125
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000126 // If new time stamp and more than one in-order packet received, calculate
127 // new jitter statistics.
128 if (packet.Timestamp() != last_received_timestamp_ &&
129 (receive_counters_.transmitted.packets -
130 receive_counters_.retransmitted.packets) > 1) {
131 UpdateJitter(packet, now_ms);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000132 }
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000133 last_received_timestamp_ = packet.Timestamp();
134 last_receive_time_ms_ = now_ms;
danilchapec86be02017-08-14 05:51:02 -0700135 return receive_counters_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000136}
137
Niels Möllerdbb988b2018-11-15 08:05:16 +0100138void StreamStatisticianImpl::UpdateJitter(const RtpPacketReceived& packet,
Danil Chapovalov856cf222018-11-26 10:20:01 +0100139 int64_t receive_time_ms) {
140 int64_t receive_diff_ms = receive_time_ms - last_receive_time_ms_;
141 RTC_DCHECK_GE(receive_diff_ms, 0);
142 uint32_t receive_diff_rtp = static_cast<uint32_t>(
143 (receive_diff_ms * packet.payload_type_frequency()) / 1000);
144 int32_t time_diff_samples =
145 receive_diff_rtp - (packet.Timestamp() - last_received_timestamp_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000146
kwibergfd8be342016-05-14 19:44:11 -0700147 time_diff_samples = std::abs(time_diff_samples);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000148
149 // lib_jingle sometimes deliver crazy jumps in TS for the same stream.
150 // If this happens, don't update jitter value. Use 5 secs video frequency
151 // as the threshold.
152 if (time_diff_samples < 450000) {
153 // Note we calculate in Q4 to avoid using float.
154 int32_t jitter_diff_q4 = (time_diff_samples << 4) - jitter_q4_;
155 jitter_q4_ += ((jitter_diff_q4 + 8) >> 4);
156 }
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000157}
158
Niels Möllerdbb988b2018-11-15 08:05:16 +0100159void StreamStatisticianImpl::FecPacketReceived(
160 const RtpPacketReceived& packet) {
danilchapec86be02017-08-14 05:51:02 -0700161 StreamDataCounters counters;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000162 {
danilchap7c9426c2016-04-14 03:05:31 -0700163 rtc::CritScope cs(&stream_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +0100164 receive_counters_.fec.AddPacket(packet);
danilchapec86be02017-08-14 05:51:02 -0700165 counters = receive_counters_;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000166 }
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100167 if (rtp_callback_)
168 rtp_callback_->DataCountersUpdated(counters, ssrc_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000169}
170
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000171void StreamStatisticianImpl::SetMaxReorderingThreshold(
172 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700173 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000174 max_reordering_threshold_ = max_reordering_threshold;
175}
176
Niels Möller5304a322018-08-27 13:27:05 +0200177void StreamStatisticianImpl::EnableRetransmitDetection(bool enable) {
178 rtc::CritScope cs(&stream_lock_);
179 enable_retransmit_detection_ = enable;
180}
181
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000182bool StreamStatisticianImpl::GetStatistics(RtcpStatistics* statistics,
Qingsi Wang2370b082018-08-21 14:24:26 -0700183 bool reset) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000184 {
danilchap7c9426c2016-04-14 03:05:31 -0700185 rtc::CritScope cs(&stream_lock_);
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000186 if (!ReceivedRtpPacket()) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000187 return false;
188 }
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000189
Qingsi Wang2370b082018-08-21 14:24:26 -0700190 if (!reset) {
191 if (last_report_inorder_packets_ == 0) {
192 // No report.
193 return false;
194 }
195 // Just get last report.
196 *statistics = last_reported_statistics_;
197 return true;
198 }
199
200 *statistics = CalculateRtcpStatistics();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000201 }
202
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100203 if (rtcp_callback_)
204 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000205 return true;
206}
207
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200208bool StreamStatisticianImpl::GetActiveStatisticsAndReset(
209 RtcpStatistics* statistics) {
210 {
211 rtc::CritScope cs(&stream_lock_);
Danil Chapovalov856cf222018-11-26 10:20:01 +0100212 if (clock_->TimeInMilliseconds() - last_receive_time_ms_ >=
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200213 kStatisticsTimeoutMs) {
214 // Not active.
215 return false;
216 }
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000217 if (!ReceivedRtpPacket()) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200218 return false;
219 }
220
Qingsi Wang2370b082018-08-21 14:24:26 -0700221 *statistics = CalculateRtcpStatistics();
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200222 }
223
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100224 if (rtcp_callback_)
225 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200226 return true;
227}
228
Qingsi Wang2370b082018-08-21 14:24:26 -0700229RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics() {
230 RtcpStatistics stats;
Qingsi Wang2370b082018-08-21 14:24:26 -0700231 // Calculate fraction lost.
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000232 int64_t exp_since_last = received_seq_max_ - last_report_seq_max_;
233 RTC_DCHECK_GE(exp_since_last, 0);
Qingsi Wang2370b082018-08-21 14:24:26 -0700234
235 // Number of received RTP packets since last report, counts all packets but
236 // not re-transmissions.
237 uint32_t rec_since_last = (receive_counters_.transmitted.packets -
238 receive_counters_.retransmitted.packets) -
239 last_report_inorder_packets_;
240
241 // With NACK we don't know the expected retransmissions during the last
242 // second. We know how many "old" packets we have received. We just count
243 // the number of old received to estimate the loss, but it still does not
244 // guarantee an exact number since we run this based on time triggered by
245 // sending of an RTP packet. This should have a minimum effect.
246
247 // With NACK we don't count old packets as received since they are
248 // re-transmitted. We use RTT to decide if a packet is re-ordered or
249 // re-transmitted.
250 uint32_t retransmitted_packets =
251 receive_counters_.retransmitted.packets - last_report_old_packets_;
252 rec_since_last += retransmitted_packets;
253
254 int32_t missing = 0;
255 if (exp_since_last > rec_since_last) {
256 missing = (exp_since_last - rec_since_last);
257 }
258 uint8_t local_fraction_lost = 0;
259 if (exp_since_last) {
260 // Scale 0 to 255, where 255 is 100% loss.
261 local_fraction_lost = static_cast<uint8_t>(255 * missing / exp_since_last);
262 }
263 stats.fraction_lost = local_fraction_lost;
264
265 // We need a counter for cumulative loss too.
266 // TODO(danilchap): Ensure cumulative loss is below maximum value of 2^24.
267 cumulative_loss_ += missing;
268 stats.packets_lost = cumulative_loss_;
269 stats.extended_highest_sequence_number =
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000270 static_cast<uint32_t>(received_seq_max_);
Qingsi Wang2370b082018-08-21 14:24:26 -0700271 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
272 stats.jitter = jitter_q4_ >> 4;
273
274 // Store this report.
275 last_reported_statistics_ = stats;
276
277 // Only for report blocks in RTCP SR and RR.
278 last_report_inorder_packets_ = receive_counters_.transmitted.packets -
279 receive_counters_.retransmitted.packets;
280 last_report_old_packets_ = receive_counters_.retransmitted.packets;
281 last_report_seq_max_ = received_seq_max_;
gaetano.carlucci61050f62016-09-30 06:29:54 -0700282 BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts",
gaetano.carlucci52a57032016-09-14 05:04:36 -0700283 clock_->TimeInMilliseconds(),
Qingsi Wang2370b082018-08-21 14:24:26 -0700284 cumulative_loss_, ssrc_);
gaetano.carlucci52a57032016-09-14 05:04:36 -0700285 BWE_TEST_LOGGING_PLOT_WITH_SSRC(
gaetano.carlucci61050f62016-09-30 06:29:54 -0700286 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700287 (received_seq_max_ - received_seq_first_), ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000288
Qingsi Wang2370b082018-08-21 14:24:26 -0700289 return stats;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000290}
291
Yves Gerey665174f2018-06-19 15:03:05 +0200292void StreamStatisticianImpl::GetDataCounters(size_t* bytes_received,
293 uint32_t* packets_received) const {
danilchap7c9426c2016-04-14 03:05:31 -0700294 rtc::CritScope cs(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000295 if (bytes_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000296 *bytes_received = receive_counters_.transmitted.payload_bytes +
297 receive_counters_.transmitted.header_bytes +
298 receive_counters_.transmitted.padding_bytes;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000299 }
300 if (packets_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000301 *packets_received = receive_counters_.transmitted.packets;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000302 }
303}
304
Niels Möller9a9f18a2019-08-02 13:52:37 +0200305absl::optional<int> StreamStatisticianImpl::GetFractionLostInPercent() const {
306 rtc::CritScope cs(&stream_lock_);
307 if (received_seq_max_ < 0) {
308 return absl::nullopt;
309 }
310 int64_t expected_packets = 1 + received_seq_max_ - received_seq_first_;
311 if (expected_packets <= 0) {
312 return absl::nullopt;
313 }
314 // Spec allows negative cumulative loss, but implementation uses uint32_t, so
315 // this expression is always non-negative.
316 return 100 * static_cast<int64_t>(cumulative_loss_) / expected_packets;
317}
318
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000319void StreamStatisticianImpl::GetReceiveStreamDataCounters(
320 StreamDataCounters* data_counters) const {
danilchap7c9426c2016-04-14 03:05:31 -0700321 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000322 *data_counters = receive_counters_;
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000323}
324
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000325uint32_t StreamStatisticianImpl::BitrateReceived() const {
danilchap7c9426c2016-04-14 03:05:31 -0700326 rtc::CritScope cs(&stream_lock_);
sprangcd349d92016-07-13 09:11:28 -0700327 return incoming_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000328}
329
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000330bool StreamStatisticianImpl::IsRetransmitOfOldPacket(
Danil Chapovalov44727b42018-11-22 11:28:45 +0100331 const RtpPacketReceived& packet,
332 int64_t now_ms) const {
Niels Möllerdbb988b2018-11-15 08:05:16 +0100333 uint32_t frequency_khz = packet.payload_type_frequency() / 1000;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100334 RTC_DCHECK_GT(frequency_khz, 0);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000335
Danil Chapovalov44727b42018-11-22 11:28:45 +0100336 int64_t time_diff_ms = now_ms - last_receive_time_ms_;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000337
338 // Diff in time stamp since last received in order.
Niels Möllerdbb988b2018-11-15 08:05:16 +0100339 uint32_t timestamp_diff = packet.Timestamp() - last_received_timestamp_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000340 uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000341
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000342 int64_t max_delay_ms = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000343
Niels Möllereda00872018-05-23 13:54:51 +0200344 // Jitter standard deviation in samples.
Oleh Prypin19929582019-04-23 08:50:04 +0200345 float jitter_std = std::sqrt(static_cast<float>(jitter_q4_ >> 4));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000346
Niels Möllereda00872018-05-23 13:54:51 +0200347 // 2 times the standard deviation => 95% confidence.
348 // And transform to milliseconds by dividing by the frequency in kHz.
349 max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz);
350
351 // Min max_delay_ms is 1.
352 if (max_delay_ms == 0) {
353 max_delay_ms = 1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000354 }
355 return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms;
356}
357
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100358std::unique_ptr<ReceiveStatistics> ReceiveStatistics::Create(
359 Clock* clock,
360 RtcpStatisticsCallback* rtcp_callback,
361 StreamDataCountersCallback* rtp_callback) {
362 return absl::make_unique<ReceiveStatisticsImpl>(clock, rtcp_callback,
363 rtp_callback);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000364}
365
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100366ReceiveStatisticsImpl::ReceiveStatisticsImpl(
367 Clock* clock,
368 RtcpStatisticsCallback* rtcp_callback,
369 StreamDataCountersCallback* rtp_callback)
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000370 : clock_(clock),
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100371 last_returned_ssrc_(0),
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100372 max_reordering_threshold_(kDefaultMaxReorderingThreshold),
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100373 rtcp_stats_callback_(rtcp_callback),
374 rtp_stats_callback_(rtp_callback) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000375
376ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
377 while (!statisticians_.empty()) {
378 delete statisticians_.begin()->second;
379 statisticians_.erase(statisticians_.begin());
380 }
381}
382
Niels Möller1f3206c2018-09-14 08:26:32 +0200383void ReceiveStatisticsImpl::OnRtpPacket(const RtpPacketReceived& packet) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000384 // StreamStatisticianImpl instance is created once and only destroyed when
385 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
386 // it's own locking so don't hold receive_statistics_lock_ (potential
387 // deadlock).
Niels Möller87da1092019-05-24 14:04:28 +0200388 GetOrCreateStatistician(packet.Ssrc())->OnRtpPacket(packet);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000389}
390
Niels Möller1f3206c2018-09-14 08:26:32 +0200391void ReceiveStatisticsImpl::FecPacketReceived(const RtpPacketReceived& packet) {
Niels Möller87da1092019-05-24 14:04:28 +0200392 StreamStatisticianImpl* impl = GetStatistician(packet.Ssrc());
393 // Ignore FEC if it is the first packet.
394 if (impl) {
395 impl->FecPacketReceived(packet);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000396 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000397}
398
Niels Möller87da1092019-05-24 14:04:28 +0200399StreamStatisticianImpl* ReceiveStatisticsImpl::GetStatistician(
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000400 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700401 rtc::CritScope cs(&receive_statistics_lock_);
Niels Möller87da1092019-05-24 14:04:28 +0200402 const auto& it = statisticians_.find(ssrc);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000403 if (it == statisticians_.end())
404 return NULL;
405 return it->second;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000406}
407
Niels Möller87da1092019-05-24 14:04:28 +0200408StreamStatisticianImpl* ReceiveStatisticsImpl::GetOrCreateStatistician(
409 uint32_t ssrc) {
410 rtc::CritScope cs(&receive_statistics_lock_);
411 StreamStatisticianImpl*& impl = statisticians_[ssrc];
412 if (impl == nullptr) { // new element
413 impl =
414 new StreamStatisticianImpl(ssrc, clock_, max_reordering_threshold_,
415 rtcp_stats_callback_, rtp_stats_callback_);
416 }
417 return impl;
418}
419
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000420void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
421 int max_reordering_threshold) {
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100422 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
423 {
424 rtc::CritScope cs(&receive_statistics_lock_);
425 max_reordering_threshold_ = max_reordering_threshold;
426 statisticians = statisticians_;
427 }
428 for (auto& statistician : statisticians) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200429 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000430 }
431}
432
Niels Möller87da1092019-05-24 14:04:28 +0200433void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
434 uint32_t ssrc,
435 int max_reordering_threshold) {
436 GetOrCreateStatistician(ssrc)->SetMaxReorderingThreshold(
437 max_reordering_threshold);
438}
439
Niels Möller5304a322018-08-27 13:27:05 +0200440void ReceiveStatisticsImpl::EnableRetransmitDetection(uint32_t ssrc,
441 bool enable) {
Niels Möller87da1092019-05-24 14:04:28 +0200442 GetOrCreateStatistician(ssrc)->EnableRetransmitDetection(enable);
Niels Möller5304a322018-08-27 13:27:05 +0200443}
444
danilchap0bc84232017-08-11 08:12:54 -0700445std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700446 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200447 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
448 {
449 rtc::CritScope cs(&receive_statistics_lock_);
450 statisticians = statisticians_;
451 }
danilchapf5f793c2017-07-27 04:44:18 -0700452 std::vector<rtcp::ReportBlock> result;
453 result.reserve(std::min(max_blocks, statisticians.size()));
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100454 auto add_report_block = [&result](uint32_t media_ssrc,
455 StreamStatisticianImpl* statistician) {
danilchapf5f793c2017-07-27 04:44:18 -0700456 // Do we have receive statistics to send?
457 RtcpStatistics stats;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100458 if (!statistician->GetActiveStatisticsAndReset(&stats))
459 return;
danilchapf5f793c2017-07-27 04:44:18 -0700460 result.emplace_back();
461 rtcp::ReportBlock& block = result.back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100462 block.SetMediaSsrc(media_ssrc);
danilchapf5f793c2017-07-27 04:44:18 -0700463 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700464 if (!block.SetCumulativeLost(stats.packets_lost)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100465 RTC_LOG(LS_WARNING) << "Cumulative lost is oversized.";
danilchapf5f793c2017-07-27 04:44:18 -0700466 result.pop_back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100467 return;
danilchapf5f793c2017-07-27 04:44:18 -0700468 }
srte186d9c32017-08-04 05:03:53 -0700469 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700470 block.SetJitter(stats.jitter);
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100471 };
472
473 const auto start_it = statisticians.upper_bound(last_returned_ssrc_);
474 for (auto it = start_it;
475 result.size() < max_blocks && it != statisticians.end(); ++it)
476 add_report_block(it->first, it->second);
477 for (auto it = statisticians.begin();
478 result.size() < max_blocks && it != start_it; ++it)
479 add_report_block(it->first, it->second);
480
481 if (!result.empty())
482 last_returned_ssrc_ = result.back().source_ssrc();
danilchapf5f793c2017-07-27 04:44:18 -0700483 return result;
484}
485
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000486} // namespace webrtc