blob: dd3ee5508853aaac347bde1dd555a1925ebdc986 [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),
Qingsi Wang2370b082018-08-21 14:24:26 -070042 cumulative_loss_(0),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000043 last_receive_time_ms_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000044 last_received_timestamp_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000045 received_seq_first_(0),
46 received_seq_max_(0),
47 received_seq_wraps_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000048 received_packet_overhead_(12),
Qingsi Wang2370b082018-08-21 14:24:26 -070049 last_report_inorder_packets_(0),
50 last_report_old_packets_(0),
51 last_report_seq_max_(0),
sprang@webrtc.org0e932572014-01-23 10:00:39 +000052 rtcp_callback_(rtcp_callback),
53 rtp_callback_(rtp_callback) {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000054
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010055StreamStatisticianImpl::~StreamStatisticianImpl() = default;
56
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000057void StreamStatisticianImpl::IncomingPacket(const RTPHeader& header,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +000058 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000059 bool retransmitted) {
Niels Möllerb615d1a2018-08-27 12:32:21 +020060 StreamDataCounters counters;
61 {
62 rtc::CritScope cs(&stream_lock_);
63
64 counters = UpdateCounters(header, packet_length, retransmitted);
65 }
danilchapec86be02017-08-14 05:51:02 -070066 rtp_callback_->DataCountersUpdated(counters, ssrc_);
sprang@webrtc.orga45cac02014-01-27 16:22:08 +000067}
68
danilchapec86be02017-08-14 05:51:02 -070069StreamDataCounters StreamStatisticianImpl::UpdateCounters(
70 const RTPHeader& header,
71 size_t packet_length,
72 bool retransmitted) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000073 bool in_order = InOrderPacketInternal(header.sequenceNumber);
danilchapec86be02017-08-14 05:51:02 -070074 RTC_DCHECK_EQ(ssrc_, header.ssrc);
sprangcd349d92016-07-13 09:11:28 -070075 incoming_bitrate_.Update(packet_length, clock_->TimeInMilliseconds());
asapersson@webrtc.org44149392015-02-04 08:34:47 +000076 receive_counters_.transmitted.AddPacket(packet_length, header);
sprang@webrtc.org0e932572014-01-23 10:00:39 +000077 if (!in_order && retransmitted) {
asapersson@webrtc.org44149392015-02-04 08:34:47 +000078 receive_counters_.retransmitted.AddPacket(packet_length, header);
sprang@webrtc.org0e932572014-01-23 10:00:39 +000079 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +000080
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +000081 if (receive_counters_.transmitted.packets == 1) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000082 received_seq_first_ = header.sequenceNumber;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +000083 receive_counters_.first_packet_time_ms = clock_->TimeInMilliseconds();
wu@webrtc.org822fbd82013-08-15 23:38:54 +000084 }
85
86 // Count only the new packets received. That is, if packets 1, 2, 3, 5, 4, 6
87 // are received, 4 will be ignored.
88 if (in_order) {
89 // Current time in samples.
danilchap37953762017-02-09 11:15:25 -080090 NtpTime receive_time = clock_->CurrentNtpTime();
wu@webrtc.org822fbd82013-08-15 23:38:54 +000091
92 // Wrong if we use RetransmitOfOldPacket.
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +000093 if (receive_counters_.transmitted.packets > 1 &&
sprang@webrtc.org0e932572014-01-23 10:00:39 +000094 received_seq_max_ > header.sequenceNumber) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000095 // Wrap around detected.
96 received_seq_wraps_++;
97 }
98 // New max.
99 received_seq_max_ = header.sequenceNumber;
100
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000101 // If new time stamp and more than one in-order packet received, calculate
102 // new jitter statistics.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000103 if (header.timestamp != last_received_timestamp_ &&
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000104 (receive_counters_.transmitted.packets -
105 receive_counters_.retransmitted.packets) > 1) {
danilchap1227e8b2015-12-21 11:06:50 -0800106 UpdateJitter(header, receive_time);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000107 }
108 last_received_timestamp_ = header.timestamp;
danilchap1227e8b2015-12-21 11:06:50 -0800109 last_receive_time_ntp_ = receive_time;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000110 last_receive_time_ms_ = clock_->TimeInMilliseconds();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000111 }
112
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000113 size_t packet_oh = header.headerLength + header.paddingLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000114
115 // Our measured overhead. Filter from RFC 5104 4.2.1.2:
116 // avg_OH (new) = 15/16*avg_OH (old) + 1/16*pckt_OH,
117 received_packet_overhead_ = (15 * received_packet_overhead_ + packet_oh) >> 4;
danilchapec86be02017-08-14 05:51:02 -0700118 return receive_counters_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000119}
120
121void StreamStatisticianImpl::UpdateJitter(const RTPHeader& header,
danilchap1227e8b2015-12-21 11:06:50 -0800122 NtpTime receive_time) {
123 uint32_t receive_time_rtp =
124 NtpToRtp(receive_time, header.payload_type_frequency);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000125 uint32_t last_receive_time_rtp =
danilchap1227e8b2015-12-21 11:06:50 -0800126 NtpToRtp(last_receive_time_ntp_, header.payload_type_frequency);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000127 int32_t time_diff_samples = (receive_time_rtp - last_receive_time_rtp) -
Yves Gerey665174f2018-06-19 15:03:05 +0200128 (header.timestamp - last_received_timestamp_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000129
kwibergfd8be342016-05-14 19:44:11 -0700130 time_diff_samples = std::abs(time_diff_samples);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000131
132 // lib_jingle sometimes deliver crazy jumps in TS for the same stream.
133 // If this happens, don't update jitter value. Use 5 secs video frequency
134 // as the threshold.
135 if (time_diff_samples < 450000) {
136 // Note we calculate in Q4 to avoid using float.
137 int32_t jitter_diff_q4 = (time_diff_samples << 4) - jitter_q4_;
138 jitter_q4_ += ((jitter_diff_q4 + 8) >> 4);
139 }
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000140}
141
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000142void StreamStatisticianImpl::FecPacketReceived(const RTPHeader& header,
143 size_t packet_length) {
danilchapec86be02017-08-14 05:51:02 -0700144 StreamDataCounters counters;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000145 {
danilchap7c9426c2016-04-14 03:05:31 -0700146 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000147 receive_counters_.fec.AddPacket(packet_length, header);
danilchapec86be02017-08-14 05:51:02 -0700148 counters = receive_counters_;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000149 }
danilchapec86be02017-08-14 05:51:02 -0700150 rtp_callback_->DataCountersUpdated(counters, ssrc_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000151}
152
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000153void StreamStatisticianImpl::SetMaxReorderingThreshold(
154 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700155 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000156 max_reordering_threshold_ = max_reordering_threshold;
157}
158
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000159bool StreamStatisticianImpl::GetStatistics(RtcpStatistics* statistics,
Qingsi Wang2370b082018-08-21 14:24:26 -0700160 bool reset) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000161 {
danilchap7c9426c2016-04-14 03:05:31 -0700162 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000163 if (received_seq_first_ == 0 &&
164 receive_counters_.transmitted.payload_bytes == 0) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000165 // We have not received anything.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000166 return false;
167 }
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000168
Qingsi Wang2370b082018-08-21 14:24:26 -0700169 if (!reset) {
170 if (last_report_inorder_packets_ == 0) {
171 // No report.
172 return false;
173 }
174 // Just get last report.
175 *statistics = last_reported_statistics_;
176 return true;
177 }
178
179 *statistics = CalculateRtcpStatistics();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000180 }
181
Qingsi Wang2370b082018-08-21 14:24:26 -0700182 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000183 return true;
184}
185
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200186bool StreamStatisticianImpl::GetActiveStatisticsAndReset(
187 RtcpStatistics* statistics) {
188 {
189 rtc::CritScope cs(&stream_lock_);
190 if (clock_->CurrentNtpInMilliseconds() - last_receive_time_ntp_.ToMs() >=
191 kStatisticsTimeoutMs) {
192 // Not active.
193 return false;
194 }
195 if (received_seq_first_ == 0 &&
196 receive_counters_.transmitted.payload_bytes == 0) {
197 // We have not received anything.
198 return false;
199 }
200
Qingsi Wang2370b082018-08-21 14:24:26 -0700201 *statistics = CalculateRtcpStatistics();
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200202 }
203
204 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
205 return true;
206}
207
Qingsi Wang2370b082018-08-21 14:24:26 -0700208RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics() {
209 RtcpStatistics stats;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000210
Qingsi Wang2370b082018-08-21 14:24:26 -0700211 if (last_report_inorder_packets_ == 0) {
212 // First time we send a report.
213 last_report_seq_max_ = received_seq_first_ - 1;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000214 }
215
Qingsi Wang2370b082018-08-21 14:24:26 -0700216 // Calculate fraction lost.
217 uint16_t exp_since_last = (received_seq_max_ - last_report_seq_max_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000218
Qingsi Wang2370b082018-08-21 14:24:26 -0700219 if (last_report_seq_max_ > received_seq_max_) {
220 // Can we assume that the seq_num can't go decrease over a full RTCP period?
221 exp_since_last = 0;
222 }
223
224 // Number of received RTP packets since last report, counts all packets but
225 // not re-transmissions.
226 uint32_t rec_since_last = (receive_counters_.transmitted.packets -
227 receive_counters_.retransmitted.packets) -
228 last_report_inorder_packets_;
229
230 // With NACK we don't know the expected retransmissions during the last
231 // second. We know how many "old" packets we have received. We just count
232 // the number of old received to estimate the loss, but it still does not
233 // guarantee an exact number since we run this based on time triggered by
234 // sending of an RTP packet. This should have a minimum effect.
235
236 // With NACK we don't count old packets as received since they are
237 // re-transmitted. We use RTT to decide if a packet is re-ordered or
238 // re-transmitted.
239 uint32_t retransmitted_packets =
240 receive_counters_.retransmitted.packets - last_report_old_packets_;
241 rec_since_last += retransmitted_packets;
242
243 int32_t missing = 0;
244 if (exp_since_last > rec_since_last) {
245 missing = (exp_since_last - rec_since_last);
246 }
247 uint8_t local_fraction_lost = 0;
248 if (exp_since_last) {
249 // Scale 0 to 255, where 255 is 100% loss.
250 local_fraction_lost = static_cast<uint8_t>(255 * missing / exp_since_last);
251 }
252 stats.fraction_lost = local_fraction_lost;
253
254 // We need a counter for cumulative loss too.
255 // TODO(danilchap): Ensure cumulative loss is below maximum value of 2^24.
256 cumulative_loss_ += missing;
257 stats.packets_lost = cumulative_loss_;
258 stats.extended_highest_sequence_number =
259 (received_seq_wraps_ << 16) + received_seq_max_;
260 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
261 stats.jitter = jitter_q4_ >> 4;
262
263 // Store this report.
264 last_reported_statistics_ = stats;
265
266 // Only for report blocks in RTCP SR and RR.
267 last_report_inorder_packets_ = receive_counters_.transmitted.packets -
268 receive_counters_.retransmitted.packets;
269 last_report_old_packets_ = receive_counters_.retransmitted.packets;
270 last_report_seq_max_ = received_seq_max_;
gaetano.carlucci61050f62016-09-30 06:29:54 -0700271 BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts",
gaetano.carlucci52a57032016-09-14 05:04:36 -0700272 clock_->TimeInMilliseconds(),
Qingsi Wang2370b082018-08-21 14:24:26 -0700273 cumulative_loss_, ssrc_);
gaetano.carlucci52a57032016-09-14 05:04:36 -0700274 BWE_TEST_LOGGING_PLOT_WITH_SSRC(
gaetano.carlucci61050f62016-09-30 06:29:54 -0700275 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700276 (received_seq_max_ - received_seq_first_), ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000277
Qingsi Wang2370b082018-08-21 14:24:26 -0700278 return stats;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000279}
280
Yves Gerey665174f2018-06-19 15:03:05 +0200281void StreamStatisticianImpl::GetDataCounters(size_t* bytes_received,
282 uint32_t* packets_received) const {
danilchap7c9426c2016-04-14 03:05:31 -0700283 rtc::CritScope cs(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000284 if (bytes_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000285 *bytes_received = receive_counters_.transmitted.payload_bytes +
286 receive_counters_.transmitted.header_bytes +
287 receive_counters_.transmitted.padding_bytes;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000288 }
289 if (packets_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000290 *packets_received = receive_counters_.transmitted.packets;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000291 }
292}
293
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000294void StreamStatisticianImpl::GetReceiveStreamDataCounters(
295 StreamDataCounters* data_counters) const {
danilchap7c9426c2016-04-14 03:05:31 -0700296 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000297 *data_counters = receive_counters_;
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000298}
299
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000300uint32_t StreamStatisticianImpl::BitrateReceived() const {
danilchap7c9426c2016-04-14 03:05:31 -0700301 rtc::CritScope cs(&stream_lock_);
sprangcd349d92016-07-13 09:11:28 -0700302 return incoming_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000303}
304
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000305bool StreamStatisticianImpl::IsRetransmitOfOldPacket(
Niels Möllereda00872018-05-23 13:54:51 +0200306 const RTPHeader& header) const {
danilchap7c9426c2016-04-14 03:05:31 -0700307 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000308 if (InOrderPacketInternal(header.sequenceNumber)) {
309 return false;
310 }
311 uint32_t frequency_khz = header.payload_type_frequency / 1000;
312 assert(frequency_khz > 0);
313
Yves Gerey665174f2018-06-19 15:03:05 +0200314 int64_t time_diff_ms = clock_->TimeInMilliseconds() - last_receive_time_ms_;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000315
316 // Diff in time stamp since last received in order.
317 uint32_t timestamp_diff = header.timestamp - last_received_timestamp_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000318 uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000319
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000320 int64_t max_delay_ms = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000321
Niels Möllereda00872018-05-23 13:54:51 +0200322 // Jitter standard deviation in samples.
323 float jitter_std = sqrt(static_cast<float>(jitter_q4_ >> 4));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000324
Niels Möllereda00872018-05-23 13:54:51 +0200325 // 2 times the standard deviation => 95% confidence.
326 // And transform to milliseconds by dividing by the frequency in kHz.
327 max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz);
328
329 // Min max_delay_ms is 1.
330 if (max_delay_ms == 0) {
331 max_delay_ms = 1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000332 }
333 return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms;
334}
335
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000336bool StreamStatisticianImpl::InOrderPacketInternal(
337 uint16_t sequence_number) const {
338 // First packet is always in order.
Qingsi Wang2370b082018-08-21 14:24:26 -0700339 if (last_receive_time_ms_ == 0)
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000340 return true;
341
342 if (IsNewerSequenceNumber(sequence_number, received_seq_max_)) {
343 return true;
344 } else {
345 // If we have a restart of the remote side this packet is still in order.
Yves Gerey665174f2018-06-19 15:03:05 +0200346 return !IsNewerSequenceNumber(
347 sequence_number, received_seq_max_ - max_reordering_threshold_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000348 }
349}
350
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000351ReceiveStatistics* ReceiveStatistics::Create(Clock* clock) {
352 return new ReceiveStatisticsImpl(clock);
353}
354
355ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock)
356 : clock_(clock),
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100357 last_returned_ssrc_(0),
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000358 rtcp_stats_callback_(NULL),
359 rtp_stats_callback_(NULL) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000360
361ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
362 while (!statisticians_.empty()) {
363 delete statisticians_.begin()->second;
364 statisticians_.erase(statisticians_.begin());
365 }
366}
367
368void ReceiveStatisticsImpl::IncomingPacket(const RTPHeader& header,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000369 size_t packet_length,
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000370 bool retransmitted) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000371 StreamStatisticianImpl* impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000372 {
danilchap7c9426c2016-04-14 03:05:31 -0700373 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200374 auto it = statisticians_.find(header.ssrc);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000375 if (it != statisticians_.end()) {
376 impl = it->second;
377 } else {
danilchapec86be02017-08-14 05:51:02 -0700378 impl = new StreamStatisticianImpl(header.ssrc, clock_, this, this);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000379 statisticians_[header.ssrc] = impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000380 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000381 }
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000382 // StreamStatisticianImpl instance is created once and only destroyed when
383 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
384 // it's own locking so don't hold receive_statistics_lock_ (potential
385 // deadlock).
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000386 impl->IncomingPacket(header, packet_length, retransmitted);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000387}
388
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000389void ReceiveStatisticsImpl::FecPacketReceived(const RTPHeader& header,
390 size_t packet_length) {
danilchapec86be02017-08-14 05:51:02 -0700391 StreamStatisticianImpl* impl;
392 {
393 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200394 auto it = statisticians_.find(header.ssrc);
danilchapec86be02017-08-14 05:51:02 -0700395 // Ignore FEC if it is the first packet.
396 if (it == statisticians_.end())
397 return;
398 impl = it->second;
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000399 }
danilchapec86be02017-08-14 05:51:02 -0700400 impl->FecPacketReceived(header, packet_length);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000401}
402
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000403StreamStatistician* ReceiveStatisticsImpl::GetStatistician(
404 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700405 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200406 auto it = statisticians_.find(ssrc);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000407 if (it == statisticians_.end())
408 return NULL;
409 return it->second;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000410}
411
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000412void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
413 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700414 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200415 for (auto& statistician : statisticians_) {
416 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000417 }
418}
419
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000420void ReceiveStatisticsImpl::RegisterRtcpStatisticsCallback(
421 RtcpStatisticsCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700422 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000423 if (callback != NULL)
424 assert(rtcp_stats_callback_ == NULL);
425 rtcp_stats_callback_ = callback;
426}
427
428void ReceiveStatisticsImpl::StatisticsUpdated(const RtcpStatistics& statistics,
429 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700430 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000431 if (rtcp_stats_callback_)
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000432 rtcp_stats_callback_->StatisticsUpdated(statistics, ssrc);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000433}
434
435void ReceiveStatisticsImpl::CNameChanged(const char* cname, uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700436 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000437 if (rtcp_stats_callback_)
438 rtcp_stats_callback_->CNameChanged(cname, ssrc);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000439}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000440
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000441void ReceiveStatisticsImpl::RegisterRtpStatisticsCallback(
442 StreamDataCountersCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700443 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000444 if (callback != NULL)
445 assert(rtp_stats_callback_ == NULL);
446 rtp_stats_callback_ = callback;
447}
448
449void ReceiveStatisticsImpl::DataCountersUpdated(const StreamDataCounters& stats,
450 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700451 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000452 if (rtp_stats_callback_) {
453 rtp_stats_callback_->DataCountersUpdated(stats, ssrc);
454 }
455}
456
danilchap0bc84232017-08-11 08:12:54 -0700457std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700458 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200459 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
460 {
461 rtc::CritScope cs(&receive_statistics_lock_);
462 statisticians = statisticians_;
463 }
danilchapf5f793c2017-07-27 04:44:18 -0700464 std::vector<rtcp::ReportBlock> result;
465 result.reserve(std::min(max_blocks, statisticians.size()));
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100466 auto add_report_block = [&result](uint32_t media_ssrc,
467 StreamStatisticianImpl* statistician) {
danilchapf5f793c2017-07-27 04:44:18 -0700468 // Do we have receive statistics to send?
469 RtcpStatistics stats;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100470 if (!statistician->GetActiveStatisticsAndReset(&stats))
471 return;
danilchapf5f793c2017-07-27 04:44:18 -0700472 result.emplace_back();
473 rtcp::ReportBlock& block = result.back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100474 block.SetMediaSsrc(media_ssrc);
danilchapf5f793c2017-07-27 04:44:18 -0700475 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700476 if (!block.SetCumulativeLost(stats.packets_lost)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100477 RTC_LOG(LS_WARNING) << "Cumulative lost is oversized.";
danilchapf5f793c2017-07-27 04:44:18 -0700478 result.pop_back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100479 return;
danilchapf5f793c2017-07-27 04:44:18 -0700480 }
srte186d9c32017-08-04 05:03:53 -0700481 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700482 block.SetJitter(stats.jitter);
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100483 };
484
485 const auto start_it = statisticians.upper_bound(last_returned_ssrc_);
486 for (auto it = start_it;
487 result.size() < max_blocks && it != statisticians.end(); ++it)
488 add_report_block(it->first, it->second);
489 for (auto it = statisticians.begin();
490 result.size() < max_blocks && it != start_it; ++it)
491 add_report_block(it->first, it->second);
492
493 if (!result.empty())
494 last_returned_ssrc_ = result.back().source_ssrc();
danilchapf5f793c2017-07-27 04:44:18 -0700495 return result;
496}
497
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000498} // namespace webrtc