blob: f2ccf22077f50fec68dbb9826198d461052290e2 [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,
sprang@webrtc.org0e932572014-01-23 10:00:39 +000034 RtcpStatisticsCallback* rtcp_callback,
35 StreamDataCountersCallback* rtp_callback)
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),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000040 max_reordering_threshold_(kDefaultMaxReorderingThreshold),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000041 jitter_q4_(0),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000042 last_receive_time_ms_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000043 last_received_timestamp_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000044 received_seq_first_(0),
45 received_seq_max_(0),
46 received_seq_wraps_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000047 received_packet_overhead_(12),
sprang@webrtc.org0e932572014-01-23 10:00:39 +000048 rtcp_callback_(rtcp_callback),
49 rtp_callback_(rtp_callback) {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000050
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010051StreamStatisticianImpl::~StreamStatisticianImpl() = default;
52
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000053void StreamStatisticianImpl::IncomingPacket(const RTPHeader& header,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +000054 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000055 bool retransmitted) {
Taylor Brandstetter84916932018-06-25 15:50:26 -070056 StreamDataCounters counters;
57 RtcpStatistics rtcp_stats;
58 {
59 rtc::CritScope cs(&stream_lock_);
60 counters = UpdateCounters(header, packet_length, retransmitted);
61 // We only want to recalculate |fraction_lost| when sending an RTCP SR or
62 // RR.
63 rtcp_stats = CalculateRtcpStatistics(/*update_fraction_lost=*/false);
64 }
65
danilchapec86be02017-08-14 05:51:02 -070066 rtp_callback_->DataCountersUpdated(counters, ssrc_);
Taylor Brandstetter84916932018-06-25 15:50:26 -070067 rtcp_callback_->StatisticsUpdated(rtcp_stats, ssrc_);
sprang@webrtc.orga45cac02014-01-27 16:22:08 +000068}
69
danilchapec86be02017-08-14 05:51:02 -070070StreamDataCounters StreamStatisticianImpl::UpdateCounters(
71 const RTPHeader& header,
72 size_t packet_length,
73 bool retransmitted) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000074 bool in_order = InOrderPacketInternal(header.sequenceNumber);
danilchapec86be02017-08-14 05:51:02 -070075 RTC_DCHECK_EQ(ssrc_, header.ssrc);
sprangcd349d92016-07-13 09:11:28 -070076 incoming_bitrate_.Update(packet_length, clock_->TimeInMilliseconds());
asapersson@webrtc.org44149392015-02-04 08:34:47 +000077 receive_counters_.transmitted.AddPacket(packet_length, header);
sprang@webrtc.org0e932572014-01-23 10:00:39 +000078 if (!in_order && retransmitted) {
asapersson@webrtc.org44149392015-02-04 08:34:47 +000079 receive_counters_.retransmitted.AddPacket(packet_length, header);
sprang@webrtc.org0e932572014-01-23 10:00:39 +000080 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +000081
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +000082 if (receive_counters_.transmitted.packets == 1) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000083 received_seq_first_ = header.sequenceNumber;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +000084 receive_counters_.first_packet_time_ms = clock_->TimeInMilliseconds();
wu@webrtc.org822fbd82013-08-15 23:38:54 +000085 }
86
87 // Count only the new packets received. That is, if packets 1, 2, 3, 5, 4, 6
88 // are received, 4 will be ignored.
89 if (in_order) {
90 // Current time in samples.
danilchap37953762017-02-09 11:15:25 -080091 NtpTime receive_time = clock_->CurrentNtpTime();
wu@webrtc.org822fbd82013-08-15 23:38:54 +000092
93 // Wrong if we use RetransmitOfOldPacket.
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +000094 if (receive_counters_.transmitted.packets > 1 &&
sprang@webrtc.org0e932572014-01-23 10:00:39 +000095 received_seq_max_ > header.sequenceNumber) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000096 // Wrap around detected.
97 received_seq_wraps_++;
98 }
99 // New max.
100 received_seq_max_ = header.sequenceNumber;
101
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000102 // If new time stamp and more than one in-order packet received, calculate
103 // new jitter statistics.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000104 if (header.timestamp != last_received_timestamp_ &&
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000105 (receive_counters_.transmitted.packets -
106 receive_counters_.retransmitted.packets) > 1) {
danilchap1227e8b2015-12-21 11:06:50 -0800107 UpdateJitter(header, receive_time);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000108 }
109 last_received_timestamp_ = header.timestamp;
danilchap1227e8b2015-12-21 11:06:50 -0800110 last_receive_time_ntp_ = receive_time;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000111 last_receive_time_ms_ = clock_->TimeInMilliseconds();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000112 }
113
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000114 size_t packet_oh = header.headerLength + header.paddingLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000115
116 // Our measured overhead. Filter from RFC 5104 4.2.1.2:
117 // avg_OH (new) = 15/16*avg_OH (old) + 1/16*pckt_OH,
118 received_packet_overhead_ = (15 * received_packet_overhead_ + packet_oh) >> 4;
danilchapec86be02017-08-14 05:51:02 -0700119 return receive_counters_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000120}
121
122void StreamStatisticianImpl::UpdateJitter(const RTPHeader& header,
danilchap1227e8b2015-12-21 11:06:50 -0800123 NtpTime receive_time) {
124 uint32_t receive_time_rtp =
125 NtpToRtp(receive_time, header.payload_type_frequency);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000126 uint32_t last_receive_time_rtp =
danilchap1227e8b2015-12-21 11:06:50 -0800127 NtpToRtp(last_receive_time_ntp_, header.payload_type_frequency);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000128 int32_t time_diff_samples = (receive_time_rtp - last_receive_time_rtp) -
Yves Gerey665174f2018-06-19 15:03:05 +0200129 (header.timestamp - last_received_timestamp_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000130
kwibergfd8be342016-05-14 19:44:11 -0700131 time_diff_samples = std::abs(time_diff_samples);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000132
133 // lib_jingle sometimes deliver crazy jumps in TS for the same stream.
134 // If this happens, don't update jitter value. Use 5 secs video frequency
135 // as the threshold.
136 if (time_diff_samples < 450000) {
137 // Note we calculate in Q4 to avoid using float.
138 int32_t jitter_diff_q4 = (time_diff_samples << 4) - jitter_q4_;
139 jitter_q4_ += ((jitter_diff_q4 + 8) >> 4);
140 }
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000141}
142
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000143void StreamStatisticianImpl::FecPacketReceived(const RTPHeader& header,
144 size_t packet_length) {
danilchapec86be02017-08-14 05:51:02 -0700145 StreamDataCounters counters;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000146 {
danilchap7c9426c2016-04-14 03:05:31 -0700147 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000148 receive_counters_.fec.AddPacket(packet_length, header);
danilchapec86be02017-08-14 05:51:02 -0700149 counters = receive_counters_;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000150 }
danilchapec86be02017-08-14 05:51:02 -0700151 rtp_callback_->DataCountersUpdated(counters, ssrc_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000152}
153
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000154void StreamStatisticianImpl::SetMaxReorderingThreshold(
155 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700156 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000157 max_reordering_threshold_ = max_reordering_threshold;
158}
159
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000160bool StreamStatisticianImpl::GetStatistics(RtcpStatistics* statistics,
Taylor Brandstetter84916932018-06-25 15:50:26 -0700161 bool update_fraction_lost) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000162 {
danilchap7c9426c2016-04-14 03:05:31 -0700163 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000164 if (received_seq_first_ == 0 &&
165 receive_counters_.transmitted.payload_bytes == 0) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000166 // We have not received anything.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000167 return false;
168 }
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000169
Taylor Brandstetter84916932018-06-25 15:50:26 -0700170 *statistics = CalculateRtcpStatistics(update_fraction_lost);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000171 }
172
Taylor Brandstetter84916932018-06-25 15:50:26 -0700173 if (update_fraction_lost) {
174 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
175 }
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000176 return true;
177}
178
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200179bool StreamStatisticianImpl::GetActiveStatisticsAndReset(
180 RtcpStatistics* statistics) {
181 {
182 rtc::CritScope cs(&stream_lock_);
183 if (clock_->CurrentNtpInMilliseconds() - last_receive_time_ntp_.ToMs() >=
184 kStatisticsTimeoutMs) {
185 // Not active.
186 return false;
187 }
188 if (received_seq_first_ == 0 &&
189 receive_counters_.transmitted.payload_bytes == 0) {
190 // We have not received anything.
191 return false;
192 }
193
Taylor Brandstetter84916932018-06-25 15:50:26 -0700194 *statistics = CalculateRtcpStatistics(/*update_fraction_lost=*/true);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200195 }
196
197 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
198 return true;
199}
200
Taylor Brandstetter84916932018-06-25 15:50:26 -0700201RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics(
202 bool update_fraction_lost) {
203 RtcpStatistics statistics;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000204
Taylor Brandstetter84916932018-06-25 15:50:26 -0700205 uint32_t extended_seq_max = (received_seq_wraps_ << 16) + received_seq_max_;
206
207 if (update_fraction_lost) {
208 if (last_report_received_packets_ == 0) {
209 // First time we're calculating fraction lost.
210 last_report_extended_seq_max_ = received_seq_first_ - 1;
211 }
212
213 uint32_t exp_since_last =
214 (extended_seq_max - last_report_extended_seq_max_);
215
216 // Number of received RTP packets since last report; counts all packets
217 // including retransmissions.
218 uint32_t rec_since_last =
219 receive_counters_.transmitted.packets - last_report_received_packets_;
220
221 // Calculate fraction lost according to RFC3550 Appendix A.3. Snap to 0 if
222 // negative (which is possible with duplicate packets).
223 uint8_t local_fraction_lost = 0;
224 if (exp_since_last > rec_since_last) {
225 // Scale 0 to 255, where 255 is 100% loss.
226 local_fraction_lost = static_cast<uint8_t>(
227 255 * (exp_since_last - rec_since_last) / exp_since_last);
228 }
229
230 last_fraction_lost_ = local_fraction_lost;
231 last_report_received_packets_ = receive_counters_.transmitted.packets;
232 last_report_extended_seq_max_ = extended_seq_max;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000233 }
234
Taylor Brandstetter84916932018-06-25 15:50:26 -0700235 statistics.fraction_lost = last_fraction_lost_;
236 // Calculate cumulative loss, according to RFC3550 Appendix A.3.
237 uint32_t total_expected_packets = extended_seq_max - received_seq_first_ + 1;
238 statistics.packets_lost =
239 total_expected_packets - receive_counters_.transmitted.packets;
240 // Since cumulative loss is carried in a signed 24-bit field in RTCP, we may
241 // need to clamp it.
242 statistics.packets_lost = std::min(statistics.packets_lost, 0x7fffff);
243 statistics.packets_lost = std::max(statistics.packets_lost, -0x800000);
244 statistics.extended_highest_sequence_number = extended_seq_max;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000245 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
Taylor Brandstetter84916932018-06-25 15:50:26 -0700246 statistics.jitter = jitter_q4_ >> 4;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000247
gaetano.carlucci61050f62016-09-30 06:29:54 -0700248 BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts",
gaetano.carlucci52a57032016-09-14 05:04:36 -0700249 clock_->TimeInMilliseconds(),
Taylor Brandstetter84916932018-06-25 15:50:26 -0700250 statistics.packets_lost, ssrc_);
gaetano.carlucci52a57032016-09-14 05:04:36 -0700251 BWE_TEST_LOGGING_PLOT_WITH_SSRC(
gaetano.carlucci61050f62016-09-30 06:29:54 -0700252 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700253 (received_seq_max_ - received_seq_first_), ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000254
Taylor Brandstetter84916932018-06-25 15:50:26 -0700255 return statistics;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000256}
257
Yves Gerey665174f2018-06-19 15:03:05 +0200258void StreamStatisticianImpl::GetDataCounters(size_t* bytes_received,
259 uint32_t* packets_received) const {
danilchap7c9426c2016-04-14 03:05:31 -0700260 rtc::CritScope cs(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000261 if (bytes_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000262 *bytes_received = receive_counters_.transmitted.payload_bytes +
263 receive_counters_.transmitted.header_bytes +
264 receive_counters_.transmitted.padding_bytes;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000265 }
266 if (packets_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000267 *packets_received = receive_counters_.transmitted.packets;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000268 }
269}
270
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000271void StreamStatisticianImpl::GetReceiveStreamDataCounters(
272 StreamDataCounters* data_counters) const {
danilchap7c9426c2016-04-14 03:05:31 -0700273 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000274 *data_counters = receive_counters_;
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000275}
276
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000277uint32_t StreamStatisticianImpl::BitrateReceived() const {
danilchap7c9426c2016-04-14 03:05:31 -0700278 rtc::CritScope cs(&stream_lock_);
sprangcd349d92016-07-13 09:11:28 -0700279 return incoming_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000280}
281
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000282bool StreamStatisticianImpl::IsRetransmitOfOldPacket(
Niels Möllereda00872018-05-23 13:54:51 +0200283 const RTPHeader& header) const {
danilchap7c9426c2016-04-14 03:05:31 -0700284 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000285 if (InOrderPacketInternal(header.sequenceNumber)) {
286 return false;
287 }
288 uint32_t frequency_khz = header.payload_type_frequency / 1000;
289 assert(frequency_khz > 0);
290
Yves Gerey665174f2018-06-19 15:03:05 +0200291 int64_t time_diff_ms = clock_->TimeInMilliseconds() - last_receive_time_ms_;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000292
293 // Diff in time stamp since last received in order.
294 uint32_t timestamp_diff = header.timestamp - last_received_timestamp_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000295 uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000296
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000297 int64_t max_delay_ms = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000298
Niels Möllereda00872018-05-23 13:54:51 +0200299 // Jitter standard deviation in samples.
300 float jitter_std = sqrt(static_cast<float>(jitter_q4_ >> 4));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000301
Niels Möllereda00872018-05-23 13:54:51 +0200302 // 2 times the standard deviation => 95% confidence.
303 // And transform to milliseconds by dividing by the frequency in kHz.
304 max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz);
305
306 // Min max_delay_ms is 1.
307 if (max_delay_ms == 0) {
308 max_delay_ms = 1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000309 }
310 return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms;
311}
312
313bool StreamStatisticianImpl::IsPacketInOrder(uint16_t sequence_number) const {
danilchap7c9426c2016-04-14 03:05:31 -0700314 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000315 return InOrderPacketInternal(sequence_number);
316}
317
318bool StreamStatisticianImpl::InOrderPacketInternal(
319 uint16_t sequence_number) const {
320 // First packet is always in order.
Taylor Brandstetter84916932018-06-25 15:50:26 -0700321 if (receive_counters_.transmitted.packets == 0)
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000322 return true;
323
324 if (IsNewerSequenceNumber(sequence_number, received_seq_max_)) {
325 return true;
326 } else {
327 // If we have a restart of the remote side this packet is still in order.
Yves Gerey665174f2018-06-19 15:03:05 +0200328 return !IsNewerSequenceNumber(
329 sequence_number, received_seq_max_ - max_reordering_threshold_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000330 }
331}
332
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000333ReceiveStatistics* ReceiveStatistics::Create(Clock* clock) {
334 return new ReceiveStatisticsImpl(clock);
335}
336
337ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock)
338 : clock_(clock),
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100339 last_returned_ssrc_(0),
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000340 rtcp_stats_callback_(NULL),
341 rtp_stats_callback_(NULL) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000342
343ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
344 while (!statisticians_.empty()) {
345 delete statisticians_.begin()->second;
346 statisticians_.erase(statisticians_.begin());
347 }
348}
349
350void ReceiveStatisticsImpl::IncomingPacket(const RTPHeader& header,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000351 size_t packet_length,
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000352 bool retransmitted) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000353 StreamStatisticianImpl* impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000354 {
danilchap7c9426c2016-04-14 03:05:31 -0700355 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200356 auto it = statisticians_.find(header.ssrc);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000357 if (it != statisticians_.end()) {
358 impl = it->second;
359 } else {
danilchapec86be02017-08-14 05:51:02 -0700360 impl = new StreamStatisticianImpl(header.ssrc, clock_, this, this);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000361 statisticians_[header.ssrc] = impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000362 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000363 }
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000364 // StreamStatisticianImpl instance is created once and only destroyed when
365 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
366 // it's own locking so don't hold receive_statistics_lock_ (potential
367 // deadlock).
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000368 impl->IncomingPacket(header, packet_length, retransmitted);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000369}
370
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000371void ReceiveStatisticsImpl::FecPacketReceived(const RTPHeader& header,
372 size_t packet_length) {
danilchapec86be02017-08-14 05:51:02 -0700373 StreamStatisticianImpl* impl;
374 {
375 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200376 auto it = statisticians_.find(header.ssrc);
danilchapec86be02017-08-14 05:51:02 -0700377 // Ignore FEC if it is the first packet.
378 if (it == statisticians_.end())
379 return;
380 impl = it->second;
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000381 }
danilchapec86be02017-08-14 05:51:02 -0700382 impl->FecPacketReceived(header, packet_length);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000383}
384
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000385StreamStatistician* ReceiveStatisticsImpl::GetStatistician(
386 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700387 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200388 auto it = statisticians_.find(ssrc);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000389 if (it == statisticians_.end())
390 return NULL;
391 return it->second;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000392}
393
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000394void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
395 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700396 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200397 for (auto& statistician : statisticians_) {
398 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000399 }
400}
401
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000402void ReceiveStatisticsImpl::RegisterRtcpStatisticsCallback(
403 RtcpStatisticsCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700404 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000405 if (callback != NULL)
406 assert(rtcp_stats_callback_ == NULL);
407 rtcp_stats_callback_ = callback;
408}
409
410void ReceiveStatisticsImpl::StatisticsUpdated(const RtcpStatistics& statistics,
411 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700412 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000413 if (rtcp_stats_callback_)
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000414 rtcp_stats_callback_->StatisticsUpdated(statistics, ssrc);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000415}
416
417void ReceiveStatisticsImpl::CNameChanged(const char* cname, uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700418 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000419 if (rtcp_stats_callback_)
420 rtcp_stats_callback_->CNameChanged(cname, ssrc);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000421}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000422
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000423void ReceiveStatisticsImpl::RegisterRtpStatisticsCallback(
424 StreamDataCountersCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700425 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000426 if (callback != NULL)
427 assert(rtp_stats_callback_ == NULL);
428 rtp_stats_callback_ = callback;
429}
430
431void ReceiveStatisticsImpl::DataCountersUpdated(const StreamDataCounters& stats,
432 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700433 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000434 if (rtp_stats_callback_) {
435 rtp_stats_callback_->DataCountersUpdated(stats, ssrc);
436 }
437}
438
danilchap0bc84232017-08-11 08:12:54 -0700439std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700440 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200441 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
442 {
443 rtc::CritScope cs(&receive_statistics_lock_);
444 statisticians = statisticians_;
445 }
danilchapf5f793c2017-07-27 04:44:18 -0700446 std::vector<rtcp::ReportBlock> result;
447 result.reserve(std::min(max_blocks, statisticians.size()));
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100448 auto add_report_block = [&result](uint32_t media_ssrc,
449 StreamStatisticianImpl* statistician) {
danilchapf5f793c2017-07-27 04:44:18 -0700450 // Do we have receive statistics to send?
451 RtcpStatistics stats;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100452 if (!statistician->GetActiveStatisticsAndReset(&stats))
453 return;
danilchapf5f793c2017-07-27 04:44:18 -0700454 result.emplace_back();
455 rtcp::ReportBlock& block = result.back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100456 block.SetMediaSsrc(media_ssrc);
danilchapf5f793c2017-07-27 04:44:18 -0700457 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700458 if (!block.SetCumulativeLost(stats.packets_lost)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100459 RTC_LOG(LS_WARNING) << "Cumulative lost is oversized.";
danilchapf5f793c2017-07-27 04:44:18 -0700460 result.pop_back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100461 return;
danilchapf5f793c2017-07-27 04:44:18 -0700462 }
srte186d9c32017-08-04 05:03:53 -0700463 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700464 block.SetJitter(stats.jitter);
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100465 };
466
467 const auto start_it = statisticians.upper_bound(last_returned_ssrc_);
468 for (auto it = start_it;
469 result.size() < max_blocks && it != statisticians.end(); ++it)
470 add_report_block(it->first, it->second);
471 for (auto it = statisticians.begin();
472 result.size() < max_blocks && it != start_it; ++it)
473 add_report_block(it->first, it->second);
474
475 if (!result.empty())
476 last_returned_ssrc_ = result.back().source_ssrc();
danilchapf5f793c2017-07-27 04:44:18 -0700477 return result;
478}
479
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000480} // namespace webrtc