blob: 80b8c9b0a2377eeb1a08aba32104087de1c3c334 [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
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000013#include <math.h>
14
kwibergfd8be342016-05-14 19:44:11 -070015#include <cstdlib>
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"
19#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
20#include "modules/rtp_rtcp/source/time_util.h"
21#include "rtc_base/logging.h"
22#include "system_wrappers/include/clock.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000023
24namespace webrtc {
25
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000026const int64_t kStatisticsTimeoutMs = 8000;
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000027const int64_t kStatisticsProcessIntervalMs = 1000;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000028
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000029StreamStatistician::~StreamStatistician() {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000030
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000031StreamStatisticianImpl::StreamStatisticianImpl(
danilchapec86be02017-08-14 05:51:02 -070032 uint32_t ssrc,
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000033 Clock* clock,
Niels Möller5304a322018-08-27 13:27:05 +020034 bool enable_retransmit_detection,
sprang@webrtc.org0e932572014-01-23 10:00:39 +000035 RtcpStatisticsCallback* rtcp_callback,
36 StreamDataCountersCallback* rtp_callback)
danilchapec86be02017-08-14 05:51:02 -070037 : ssrc_(ssrc),
38 clock_(clock),
sprangcd349d92016-07-13 09:11:28 -070039 incoming_bitrate_(kStatisticsProcessIntervalMs,
40 RateStatistics::kBpsScale),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000041 max_reordering_threshold_(kDefaultMaxReorderingThreshold),
Niels Möller5304a322018-08-27 13:27:05 +020042 enable_retransmit_detection_(enable_retransmit_detection),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000043 jitter_q4_(0),
Qingsi Wang2370b082018-08-21 14:24:26 -070044 cumulative_loss_(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),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000047 received_seq_first_(0),
48 received_seq_max_(0),
49 received_seq_wraps_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000050 received_packet_overhead_(12),
Qingsi Wang2370b082018-08-21 14:24:26 -070051 last_report_inorder_packets_(0),
52 last_report_old_packets_(0),
53 last_report_seq_max_(0),
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
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000059void StreamStatisticianImpl::IncomingPacket(const RTPHeader& header,
Niels Möller5304a322018-08-27 13:27:05 +020060 size_t packet_length) {
Niels Möllerb615d1a2018-08-27 12:32:21 +020061 StreamDataCounters counters;
62 {
63 rtc::CritScope cs(&stream_lock_);
64
Niels Möller5304a322018-08-27 13:27:05 +020065 bool retransmitted =
66 enable_retransmit_detection_ && IsRetransmitOfOldPacket(header);
Niels Möllerb615d1a2018-08-27 12:32:21 +020067 counters = UpdateCounters(header, packet_length, retransmitted);
68 }
danilchapec86be02017-08-14 05:51:02 -070069 rtp_callback_->DataCountersUpdated(counters, ssrc_);
sprang@webrtc.orga45cac02014-01-27 16:22:08 +000070}
71
danilchapec86be02017-08-14 05:51:02 -070072StreamDataCounters StreamStatisticianImpl::UpdateCounters(
73 const RTPHeader& header,
74 size_t packet_length,
75 bool retransmitted) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000076 bool in_order = InOrderPacketInternal(header.sequenceNumber);
danilchapec86be02017-08-14 05:51:02 -070077 RTC_DCHECK_EQ(ssrc_, header.ssrc);
sprangcd349d92016-07-13 09:11:28 -070078 incoming_bitrate_.Update(packet_length, clock_->TimeInMilliseconds());
asapersson@webrtc.org44149392015-02-04 08:34:47 +000079 receive_counters_.transmitted.AddPacket(packet_length, header);
sprang@webrtc.org0e932572014-01-23 10:00:39 +000080 if (!in_order && retransmitted) {
asapersson@webrtc.org44149392015-02-04 08:34:47 +000081 receive_counters_.retransmitted.AddPacket(packet_length, header);
sprang@webrtc.org0e932572014-01-23 10:00:39 +000082 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +000083
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +000084 if (receive_counters_.transmitted.packets == 1) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000085 received_seq_first_ = header.sequenceNumber;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +000086 receive_counters_.first_packet_time_ms = clock_->TimeInMilliseconds();
wu@webrtc.org822fbd82013-08-15 23:38:54 +000087 }
88
89 // Count only the new packets received. That is, if packets 1, 2, 3, 5, 4, 6
90 // are received, 4 will be ignored.
91 if (in_order) {
92 // Current time in samples.
danilchap37953762017-02-09 11:15:25 -080093 NtpTime receive_time = clock_->CurrentNtpTime();
wu@webrtc.org822fbd82013-08-15 23:38:54 +000094
95 // Wrong if we use RetransmitOfOldPacket.
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +000096 if (receive_counters_.transmitted.packets > 1 &&
sprang@webrtc.org0e932572014-01-23 10:00:39 +000097 received_seq_max_ > header.sequenceNumber) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000098 // Wrap around detected.
99 received_seq_wraps_++;
100 }
101 // New max.
102 received_seq_max_ = header.sequenceNumber;
103
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000104 // If new time stamp and more than one in-order packet received, calculate
105 // new jitter statistics.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000106 if (header.timestamp != last_received_timestamp_ &&
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000107 (receive_counters_.transmitted.packets -
108 receive_counters_.retransmitted.packets) > 1) {
danilchap1227e8b2015-12-21 11:06:50 -0800109 UpdateJitter(header, receive_time);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000110 }
111 last_received_timestamp_ = header.timestamp;
danilchap1227e8b2015-12-21 11:06:50 -0800112 last_receive_time_ntp_ = receive_time;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000113 last_receive_time_ms_ = clock_->TimeInMilliseconds();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000114 }
115
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000116 size_t packet_oh = header.headerLength + header.paddingLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000117
118 // Our measured overhead. Filter from RFC 5104 4.2.1.2:
119 // avg_OH (new) = 15/16*avg_OH (old) + 1/16*pckt_OH,
120 received_packet_overhead_ = (15 * received_packet_overhead_ + packet_oh) >> 4;
danilchapec86be02017-08-14 05:51:02 -0700121 return receive_counters_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000122}
123
124void StreamStatisticianImpl::UpdateJitter(const RTPHeader& header,
danilchap1227e8b2015-12-21 11:06:50 -0800125 NtpTime receive_time) {
126 uint32_t receive_time_rtp =
127 NtpToRtp(receive_time, header.payload_type_frequency);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000128 uint32_t last_receive_time_rtp =
danilchap1227e8b2015-12-21 11:06:50 -0800129 NtpToRtp(last_receive_time_ntp_, header.payload_type_frequency);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000130 int32_t time_diff_samples = (receive_time_rtp - last_receive_time_rtp) -
Yves Gerey665174f2018-06-19 15:03:05 +0200131 (header.timestamp - last_received_timestamp_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000132
kwibergfd8be342016-05-14 19:44:11 -0700133 time_diff_samples = std::abs(time_diff_samples);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000134
135 // lib_jingle sometimes deliver crazy jumps in TS for the same stream.
136 // If this happens, don't update jitter value. Use 5 secs video frequency
137 // as the threshold.
138 if (time_diff_samples < 450000) {
139 // Note we calculate in Q4 to avoid using float.
140 int32_t jitter_diff_q4 = (time_diff_samples << 4) - jitter_q4_;
141 jitter_q4_ += ((jitter_diff_q4 + 8) >> 4);
142 }
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000143}
144
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000145void StreamStatisticianImpl::FecPacketReceived(const RTPHeader& header,
146 size_t packet_length) {
danilchapec86be02017-08-14 05:51:02 -0700147 StreamDataCounters counters;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000148 {
danilchap7c9426c2016-04-14 03:05:31 -0700149 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000150 receive_counters_.fec.AddPacket(packet_length, header);
danilchapec86be02017-08-14 05:51:02 -0700151 counters = receive_counters_;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000152 }
danilchapec86be02017-08-14 05:51:02 -0700153 rtp_callback_->DataCountersUpdated(counters, ssrc_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000154}
155
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000156void StreamStatisticianImpl::SetMaxReorderingThreshold(
157 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700158 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000159 max_reordering_threshold_ = max_reordering_threshold;
160}
161
Niels Möller5304a322018-08-27 13:27:05 +0200162void StreamStatisticianImpl::EnableRetransmitDetection(bool enable) {
163 rtc::CritScope cs(&stream_lock_);
164 enable_retransmit_detection_ = enable;
165}
166
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000167bool StreamStatisticianImpl::GetStatistics(RtcpStatistics* statistics,
Qingsi Wang2370b082018-08-21 14:24:26 -0700168 bool reset) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000169 {
danilchap7c9426c2016-04-14 03:05:31 -0700170 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000171 if (received_seq_first_ == 0 &&
172 receive_counters_.transmitted.payload_bytes == 0) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000173 // We have not received anything.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000174 return false;
175 }
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000176
Qingsi Wang2370b082018-08-21 14:24:26 -0700177 if (!reset) {
178 if (last_report_inorder_packets_ == 0) {
179 // No report.
180 return false;
181 }
182 // Just get last report.
183 *statistics = last_reported_statistics_;
184 return true;
185 }
186
187 *statistics = CalculateRtcpStatistics();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000188 }
189
Qingsi Wang2370b082018-08-21 14:24:26 -0700190 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000191 return true;
192}
193
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200194bool StreamStatisticianImpl::GetActiveStatisticsAndReset(
195 RtcpStatistics* statistics) {
196 {
197 rtc::CritScope cs(&stream_lock_);
198 if (clock_->CurrentNtpInMilliseconds() - last_receive_time_ntp_.ToMs() >=
199 kStatisticsTimeoutMs) {
200 // Not active.
201 return false;
202 }
203 if (received_seq_first_ == 0 &&
204 receive_counters_.transmitted.payload_bytes == 0) {
205 // We have not received anything.
206 return false;
207 }
208
Qingsi Wang2370b082018-08-21 14:24:26 -0700209 *statistics = CalculateRtcpStatistics();
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200210 }
211
212 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
213 return true;
214}
215
Qingsi Wang2370b082018-08-21 14:24:26 -0700216RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics() {
217 RtcpStatistics stats;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000218
Qingsi Wang2370b082018-08-21 14:24:26 -0700219 if (last_report_inorder_packets_ == 0) {
220 // First time we send a report.
221 last_report_seq_max_ = received_seq_first_ - 1;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000222 }
223
Qingsi Wang2370b082018-08-21 14:24:26 -0700224 // Calculate fraction lost.
225 uint16_t exp_since_last = (received_seq_max_ - last_report_seq_max_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000226
Qingsi Wang2370b082018-08-21 14:24:26 -0700227 if (last_report_seq_max_ > received_seq_max_) {
228 // Can we assume that the seq_num can't go decrease over a full RTCP period?
229 exp_since_last = 0;
230 }
231
232 // Number of received RTP packets since last report, counts all packets but
233 // not re-transmissions.
234 uint32_t rec_since_last = (receive_counters_.transmitted.packets -
235 receive_counters_.retransmitted.packets) -
236 last_report_inorder_packets_;
237
238 // With NACK we don't know the expected retransmissions during the last
239 // second. We know how many "old" packets we have received. We just count
240 // the number of old received to estimate the loss, but it still does not
241 // guarantee an exact number since we run this based on time triggered by
242 // sending of an RTP packet. This should have a minimum effect.
243
244 // With NACK we don't count old packets as received since they are
245 // re-transmitted. We use RTT to decide if a packet is re-ordered or
246 // re-transmitted.
247 uint32_t retransmitted_packets =
248 receive_counters_.retransmitted.packets - last_report_old_packets_;
249 rec_since_last += retransmitted_packets;
250
251 int32_t missing = 0;
252 if (exp_since_last > rec_since_last) {
253 missing = (exp_since_last - rec_since_last);
254 }
255 uint8_t local_fraction_lost = 0;
256 if (exp_since_last) {
257 // Scale 0 to 255, where 255 is 100% loss.
258 local_fraction_lost = static_cast<uint8_t>(255 * missing / exp_since_last);
259 }
260 stats.fraction_lost = local_fraction_lost;
261
262 // We need a counter for cumulative loss too.
263 // TODO(danilchap): Ensure cumulative loss is below maximum value of 2^24.
264 cumulative_loss_ += missing;
265 stats.packets_lost = cumulative_loss_;
266 stats.extended_highest_sequence_number =
267 (received_seq_wraps_ << 16) + received_seq_max_;
268 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
269 stats.jitter = jitter_q4_ >> 4;
270
271 // Store this report.
272 last_reported_statistics_ = stats;
273
274 // Only for report blocks in RTCP SR and RR.
275 last_report_inorder_packets_ = receive_counters_.transmitted.packets -
276 receive_counters_.retransmitted.packets;
277 last_report_old_packets_ = receive_counters_.retransmitted.packets;
278 last_report_seq_max_ = received_seq_max_;
gaetano.carlucci61050f62016-09-30 06:29:54 -0700279 BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts",
gaetano.carlucci52a57032016-09-14 05:04:36 -0700280 clock_->TimeInMilliseconds(),
Qingsi Wang2370b082018-08-21 14:24:26 -0700281 cumulative_loss_, ssrc_);
gaetano.carlucci52a57032016-09-14 05:04:36 -0700282 BWE_TEST_LOGGING_PLOT_WITH_SSRC(
gaetano.carlucci61050f62016-09-30 06:29:54 -0700283 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700284 (received_seq_max_ - received_seq_first_), ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000285
Qingsi Wang2370b082018-08-21 14:24:26 -0700286 return stats;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000287}
288
Yves Gerey665174f2018-06-19 15:03:05 +0200289void StreamStatisticianImpl::GetDataCounters(size_t* bytes_received,
290 uint32_t* packets_received) const {
danilchap7c9426c2016-04-14 03:05:31 -0700291 rtc::CritScope cs(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000292 if (bytes_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000293 *bytes_received = receive_counters_.transmitted.payload_bytes +
294 receive_counters_.transmitted.header_bytes +
295 receive_counters_.transmitted.padding_bytes;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000296 }
297 if (packets_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000298 *packets_received = receive_counters_.transmitted.packets;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000299 }
300}
301
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000302void StreamStatisticianImpl::GetReceiveStreamDataCounters(
303 StreamDataCounters* data_counters) const {
danilchap7c9426c2016-04-14 03:05:31 -0700304 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000305 *data_counters = receive_counters_;
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000306}
307
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000308uint32_t StreamStatisticianImpl::BitrateReceived() const {
danilchap7c9426c2016-04-14 03:05:31 -0700309 rtc::CritScope cs(&stream_lock_);
sprangcd349d92016-07-13 09:11:28 -0700310 return incoming_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000311}
312
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000313bool StreamStatisticianImpl::IsRetransmitOfOldPacket(
Niels Möllereda00872018-05-23 13:54:51 +0200314 const RTPHeader& header) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000315 if (InOrderPacketInternal(header.sequenceNumber)) {
316 return false;
317 }
318 uint32_t frequency_khz = header.payload_type_frequency / 1000;
319 assert(frequency_khz > 0);
320
Yves Gerey665174f2018-06-19 15:03:05 +0200321 int64_t time_diff_ms = clock_->TimeInMilliseconds() - last_receive_time_ms_;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000322
323 // Diff in time stamp since last received in order.
324 uint32_t timestamp_diff = header.timestamp - last_received_timestamp_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000325 uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000326
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000327 int64_t max_delay_ms = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000328
Niels Möllereda00872018-05-23 13:54:51 +0200329 // Jitter standard deviation in samples.
330 float jitter_std = sqrt(static_cast<float>(jitter_q4_ >> 4));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000331
Niels Möllereda00872018-05-23 13:54:51 +0200332 // 2 times the standard deviation => 95% confidence.
333 // And transform to milliseconds by dividing by the frequency in kHz.
334 max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz);
335
336 // Min max_delay_ms is 1.
337 if (max_delay_ms == 0) {
338 max_delay_ms = 1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000339 }
340 return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms;
341}
342
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000343bool StreamStatisticianImpl::InOrderPacketInternal(
344 uint16_t sequence_number) const {
345 // First packet is always in order.
Qingsi Wang2370b082018-08-21 14:24:26 -0700346 if (last_receive_time_ms_ == 0)
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000347 return true;
348
349 if (IsNewerSequenceNumber(sequence_number, received_seq_max_)) {
350 return true;
351 } else {
352 // If we have a restart of the remote side this packet is still in order.
Yves Gerey665174f2018-06-19 15:03:05 +0200353 return !IsNewerSequenceNumber(
354 sequence_number, received_seq_max_ - max_reordering_threshold_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000355 }
356}
357
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000358ReceiveStatistics* ReceiveStatistics::Create(Clock* clock) {
359 return new ReceiveStatisticsImpl(clock);
360}
361
362ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock)
363 : clock_(clock),
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100364 last_returned_ssrc_(0),
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000365 rtcp_stats_callback_(NULL),
366 rtp_stats_callback_(NULL) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000367
368ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
369 while (!statisticians_.empty()) {
370 delete statisticians_.begin()->second;
371 statisticians_.erase(statisticians_.begin());
372 }
373}
374
375void ReceiveStatisticsImpl::IncomingPacket(const RTPHeader& header,
Niels Möller5304a322018-08-27 13:27:05 +0200376 size_t packet_length) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000377 StreamStatisticianImpl* impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000378 {
danilchap7c9426c2016-04-14 03:05:31 -0700379 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200380 auto it = statisticians_.find(header.ssrc);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000381 if (it != statisticians_.end()) {
382 impl = it->second;
383 } else {
Niels Möller5304a322018-08-27 13:27:05 +0200384 impl = new StreamStatisticianImpl(
385 header.ssrc, clock_, /* enable_retransmit_detection = */ false, this,
386 this);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000387 statisticians_[header.ssrc] = impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000388 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000389 }
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000390 // StreamStatisticianImpl instance is created once and only destroyed when
391 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
392 // it's own locking so don't hold receive_statistics_lock_ (potential
393 // deadlock).
Niels Möller5304a322018-08-27 13:27:05 +0200394 impl->IncomingPacket(header, packet_length);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000395}
396
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000397void ReceiveStatisticsImpl::FecPacketReceived(const RTPHeader& header,
398 size_t packet_length) {
danilchapec86be02017-08-14 05:51:02 -0700399 StreamStatisticianImpl* impl;
400 {
401 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200402 auto it = statisticians_.find(header.ssrc);
danilchapec86be02017-08-14 05:51:02 -0700403 // Ignore FEC if it is the first packet.
404 if (it == statisticians_.end())
405 return;
406 impl = it->second;
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000407 }
danilchapec86be02017-08-14 05:51:02 -0700408 impl->FecPacketReceived(header, packet_length);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000409}
410
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000411StreamStatistician* ReceiveStatisticsImpl::GetStatistician(
412 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700413 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200414 auto it = statisticians_.find(ssrc);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000415 if (it == statisticians_.end())
416 return NULL;
417 return it->second;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000418}
419
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000420void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
421 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700422 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200423 for (auto& statistician : statisticians_) {
424 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000425 }
426}
427
Niels Möller5304a322018-08-27 13:27:05 +0200428void ReceiveStatisticsImpl::EnableRetransmitDetection(uint32_t ssrc,
429 bool enable) {
430 StreamStatisticianImpl* impl;
431 {
432 rtc::CritScope cs(&receive_statistics_lock_);
433 StreamStatisticianImpl*& impl_ref = statisticians_[ssrc];
434 if (impl_ref == nullptr) { // new element
435 impl_ref = new StreamStatisticianImpl(ssrc, clock_, enable, this, this);
436 return;
437 }
438 impl = impl_ref;
439 }
440 impl->EnableRetransmitDetection(enable);
441}
442
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000443void ReceiveStatisticsImpl::RegisterRtcpStatisticsCallback(
444 RtcpStatisticsCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700445 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000446 if (callback != NULL)
447 assert(rtcp_stats_callback_ == NULL);
448 rtcp_stats_callback_ = callback;
449}
450
451void ReceiveStatisticsImpl::StatisticsUpdated(const RtcpStatistics& statistics,
452 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700453 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000454 if (rtcp_stats_callback_)
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000455 rtcp_stats_callback_->StatisticsUpdated(statistics, ssrc);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000456}
457
458void ReceiveStatisticsImpl::CNameChanged(const char* cname, uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700459 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000460 if (rtcp_stats_callback_)
461 rtcp_stats_callback_->CNameChanged(cname, ssrc);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000462}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000463
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000464void ReceiveStatisticsImpl::RegisterRtpStatisticsCallback(
465 StreamDataCountersCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700466 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000467 if (callback != NULL)
468 assert(rtp_stats_callback_ == NULL);
469 rtp_stats_callback_ = callback;
470}
471
472void ReceiveStatisticsImpl::DataCountersUpdated(const StreamDataCounters& stats,
473 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700474 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000475 if (rtp_stats_callback_) {
476 rtp_stats_callback_->DataCountersUpdated(stats, ssrc);
477 }
478}
479
danilchap0bc84232017-08-11 08:12:54 -0700480std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700481 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200482 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
483 {
484 rtc::CritScope cs(&receive_statistics_lock_);
485 statisticians = statisticians_;
486 }
danilchapf5f793c2017-07-27 04:44:18 -0700487 std::vector<rtcp::ReportBlock> result;
488 result.reserve(std::min(max_blocks, statisticians.size()));
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100489 auto add_report_block = [&result](uint32_t media_ssrc,
490 StreamStatisticianImpl* statistician) {
danilchapf5f793c2017-07-27 04:44:18 -0700491 // Do we have receive statistics to send?
492 RtcpStatistics stats;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100493 if (!statistician->GetActiveStatisticsAndReset(&stats))
494 return;
danilchapf5f793c2017-07-27 04:44:18 -0700495 result.emplace_back();
496 rtcp::ReportBlock& block = result.back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100497 block.SetMediaSsrc(media_ssrc);
danilchapf5f793c2017-07-27 04:44:18 -0700498 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700499 if (!block.SetCumulativeLost(stats.packets_lost)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100500 RTC_LOG(LS_WARNING) << "Cumulative lost is oversized.";
danilchapf5f793c2017-07-27 04:44:18 -0700501 result.pop_back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100502 return;
danilchapf5f793c2017-07-27 04:44:18 -0700503 }
srte186d9c32017-08-04 05:03:53 -0700504 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700505 block.SetJitter(stats.jitter);
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100506 };
507
508 const auto start_it = statisticians.upper_bound(last_returned_ssrc_);
509 for (auto it = start_it;
510 result.size() < max_blocks && it != statisticians.end(); ++it)
511 add_report_block(it->first, it->second);
512 for (auto it = statisticians.begin();
513 result.size() < max_blocks && it != start_it; ++it)
514 add_report_block(it->first, it->second);
515
516 if (!result.empty())
517 last_returned_ssrc_ = result.back().source_ssrc();
danilchapf5f793c2017-07-27 04:44:18 -0700518 return result;
519}
520
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000521} // namespace webrtc