blob: 362a7cfb4d91acbd4c01efd93f034aab5970cc79 [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) {
Qingsi Wang2370b082018-08-21 14:24:26 -070060 auto counters = UpdateCounters(header, packet_length, retransmitted);
danilchapec86be02017-08-14 05:51:02 -070061 rtp_callback_->DataCountersUpdated(counters, ssrc_);
sprang@webrtc.orga45cac02014-01-27 16:22:08 +000062}
63
danilchapec86be02017-08-14 05:51:02 -070064StreamDataCounters StreamStatisticianImpl::UpdateCounters(
65 const RTPHeader& header,
66 size_t packet_length,
67 bool retransmitted) {
Qingsi Wang2370b082018-08-21 14:24:26 -070068 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000069 bool in_order = InOrderPacketInternal(header.sequenceNumber);
danilchapec86be02017-08-14 05:51:02 -070070 RTC_DCHECK_EQ(ssrc_, header.ssrc);
sprangcd349d92016-07-13 09:11:28 -070071 incoming_bitrate_.Update(packet_length, clock_->TimeInMilliseconds());
asapersson@webrtc.org44149392015-02-04 08:34:47 +000072 receive_counters_.transmitted.AddPacket(packet_length, header);
sprang@webrtc.org0e932572014-01-23 10:00:39 +000073 if (!in_order && retransmitted) {
asapersson@webrtc.org44149392015-02-04 08:34:47 +000074 receive_counters_.retransmitted.AddPacket(packet_length, header);
sprang@webrtc.org0e932572014-01-23 10:00:39 +000075 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +000076
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +000077 if (receive_counters_.transmitted.packets == 1) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000078 received_seq_first_ = header.sequenceNumber;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +000079 receive_counters_.first_packet_time_ms = clock_->TimeInMilliseconds();
wu@webrtc.org822fbd82013-08-15 23:38:54 +000080 }
81
82 // Count only the new packets received. That is, if packets 1, 2, 3, 5, 4, 6
83 // are received, 4 will be ignored.
84 if (in_order) {
85 // Current time in samples.
danilchap37953762017-02-09 11:15:25 -080086 NtpTime receive_time = clock_->CurrentNtpTime();
wu@webrtc.org822fbd82013-08-15 23:38:54 +000087
88 // Wrong if we use RetransmitOfOldPacket.
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +000089 if (receive_counters_.transmitted.packets > 1 &&
sprang@webrtc.org0e932572014-01-23 10:00:39 +000090 received_seq_max_ > header.sequenceNumber) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000091 // Wrap around detected.
92 received_seq_wraps_++;
93 }
94 // New max.
95 received_seq_max_ = header.sequenceNumber;
96
sprang@webrtc.org0e932572014-01-23 10:00:39 +000097 // If new time stamp and more than one in-order packet received, calculate
98 // new jitter statistics.
wu@webrtc.org822fbd82013-08-15 23:38:54 +000099 if (header.timestamp != last_received_timestamp_ &&
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000100 (receive_counters_.transmitted.packets -
101 receive_counters_.retransmitted.packets) > 1) {
danilchap1227e8b2015-12-21 11:06:50 -0800102 UpdateJitter(header, receive_time);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000103 }
104 last_received_timestamp_ = header.timestamp;
danilchap1227e8b2015-12-21 11:06:50 -0800105 last_receive_time_ntp_ = receive_time;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000106 last_receive_time_ms_ = clock_->TimeInMilliseconds();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000107 }
108
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000109 size_t packet_oh = header.headerLength + header.paddingLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000110
111 // Our measured overhead. Filter from RFC 5104 4.2.1.2:
112 // avg_OH (new) = 15/16*avg_OH (old) + 1/16*pckt_OH,
113 received_packet_overhead_ = (15 * received_packet_overhead_ + packet_oh) >> 4;
danilchapec86be02017-08-14 05:51:02 -0700114 return receive_counters_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000115}
116
117void StreamStatisticianImpl::UpdateJitter(const RTPHeader& header,
danilchap1227e8b2015-12-21 11:06:50 -0800118 NtpTime receive_time) {
119 uint32_t receive_time_rtp =
120 NtpToRtp(receive_time, header.payload_type_frequency);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000121 uint32_t last_receive_time_rtp =
danilchap1227e8b2015-12-21 11:06:50 -0800122 NtpToRtp(last_receive_time_ntp_, header.payload_type_frequency);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000123 int32_t time_diff_samples = (receive_time_rtp - last_receive_time_rtp) -
Yves Gerey665174f2018-06-19 15:03:05 +0200124 (header.timestamp - last_received_timestamp_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000125
kwibergfd8be342016-05-14 19:44:11 -0700126 time_diff_samples = std::abs(time_diff_samples);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000127
128 // lib_jingle sometimes deliver crazy jumps in TS for the same stream.
129 // If this happens, don't update jitter value. Use 5 secs video frequency
130 // as the threshold.
131 if (time_diff_samples < 450000) {
132 // Note we calculate in Q4 to avoid using float.
133 int32_t jitter_diff_q4 = (time_diff_samples << 4) - jitter_q4_;
134 jitter_q4_ += ((jitter_diff_q4 + 8) >> 4);
135 }
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000136}
137
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000138void StreamStatisticianImpl::FecPacketReceived(const RTPHeader& header,
139 size_t packet_length) {
danilchapec86be02017-08-14 05:51:02 -0700140 StreamDataCounters counters;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000141 {
danilchap7c9426c2016-04-14 03:05:31 -0700142 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000143 receive_counters_.fec.AddPacket(packet_length, header);
danilchapec86be02017-08-14 05:51:02 -0700144 counters = receive_counters_;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000145 }
danilchapec86be02017-08-14 05:51:02 -0700146 rtp_callback_->DataCountersUpdated(counters, ssrc_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000147}
148
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000149void StreamStatisticianImpl::SetMaxReorderingThreshold(
150 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700151 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000152 max_reordering_threshold_ = max_reordering_threshold;
153}
154
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000155bool StreamStatisticianImpl::GetStatistics(RtcpStatistics* statistics,
Qingsi Wang2370b082018-08-21 14:24:26 -0700156 bool reset) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000157 {
danilchap7c9426c2016-04-14 03:05:31 -0700158 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000159 if (received_seq_first_ == 0 &&
160 receive_counters_.transmitted.payload_bytes == 0) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000161 // We have not received anything.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000162 return false;
163 }
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000164
Qingsi Wang2370b082018-08-21 14:24:26 -0700165 if (!reset) {
166 if (last_report_inorder_packets_ == 0) {
167 // No report.
168 return false;
169 }
170 // Just get last report.
171 *statistics = last_reported_statistics_;
172 return true;
173 }
174
175 *statistics = CalculateRtcpStatistics();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000176 }
177
Qingsi Wang2370b082018-08-21 14:24:26 -0700178 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000179 return true;
180}
181
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200182bool StreamStatisticianImpl::GetActiveStatisticsAndReset(
183 RtcpStatistics* statistics) {
184 {
185 rtc::CritScope cs(&stream_lock_);
186 if (clock_->CurrentNtpInMilliseconds() - last_receive_time_ntp_.ToMs() >=
187 kStatisticsTimeoutMs) {
188 // Not active.
189 return false;
190 }
191 if (received_seq_first_ == 0 &&
192 receive_counters_.transmitted.payload_bytes == 0) {
193 // We have not received anything.
194 return false;
195 }
196
Qingsi Wang2370b082018-08-21 14:24:26 -0700197 *statistics = CalculateRtcpStatistics();
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200198 }
199
200 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
201 return true;
202}
203
Qingsi Wang2370b082018-08-21 14:24:26 -0700204RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics() {
205 RtcpStatistics stats;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000206
Qingsi Wang2370b082018-08-21 14:24:26 -0700207 if (last_report_inorder_packets_ == 0) {
208 // First time we send a report.
209 last_report_seq_max_ = received_seq_first_ - 1;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000210 }
211
Qingsi Wang2370b082018-08-21 14:24:26 -0700212 // Calculate fraction lost.
213 uint16_t exp_since_last = (received_seq_max_ - last_report_seq_max_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000214
Qingsi Wang2370b082018-08-21 14:24:26 -0700215 if (last_report_seq_max_ > received_seq_max_) {
216 // Can we assume that the seq_num can't go decrease over a full RTCP period?
217 exp_since_last = 0;
218 }
219
220 // Number of received RTP packets since last report, counts all packets but
221 // not re-transmissions.
222 uint32_t rec_since_last = (receive_counters_.transmitted.packets -
223 receive_counters_.retransmitted.packets) -
224 last_report_inorder_packets_;
225
226 // With NACK we don't know the expected retransmissions during the last
227 // second. We know how many "old" packets we have received. We just count
228 // the number of old received to estimate the loss, but it still does not
229 // guarantee an exact number since we run this based on time triggered by
230 // sending of an RTP packet. This should have a minimum effect.
231
232 // With NACK we don't count old packets as received since they are
233 // re-transmitted. We use RTT to decide if a packet is re-ordered or
234 // re-transmitted.
235 uint32_t retransmitted_packets =
236 receive_counters_.retransmitted.packets - last_report_old_packets_;
237 rec_since_last += retransmitted_packets;
238
239 int32_t missing = 0;
240 if (exp_since_last > rec_since_last) {
241 missing = (exp_since_last - rec_since_last);
242 }
243 uint8_t local_fraction_lost = 0;
244 if (exp_since_last) {
245 // Scale 0 to 255, where 255 is 100% loss.
246 local_fraction_lost = static_cast<uint8_t>(255 * missing / exp_since_last);
247 }
248 stats.fraction_lost = local_fraction_lost;
249
250 // We need a counter for cumulative loss too.
251 // TODO(danilchap): Ensure cumulative loss is below maximum value of 2^24.
252 cumulative_loss_ += missing;
253 stats.packets_lost = cumulative_loss_;
254 stats.extended_highest_sequence_number =
255 (received_seq_wraps_ << 16) + received_seq_max_;
256 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
257 stats.jitter = jitter_q4_ >> 4;
258
259 // Store this report.
260 last_reported_statistics_ = stats;
261
262 // Only for report blocks in RTCP SR and RR.
263 last_report_inorder_packets_ = receive_counters_.transmitted.packets -
264 receive_counters_.retransmitted.packets;
265 last_report_old_packets_ = receive_counters_.retransmitted.packets;
266 last_report_seq_max_ = received_seq_max_;
gaetano.carlucci61050f62016-09-30 06:29:54 -0700267 BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts",
gaetano.carlucci52a57032016-09-14 05:04:36 -0700268 clock_->TimeInMilliseconds(),
Qingsi Wang2370b082018-08-21 14:24:26 -0700269 cumulative_loss_, ssrc_);
gaetano.carlucci52a57032016-09-14 05:04:36 -0700270 BWE_TEST_LOGGING_PLOT_WITH_SSRC(
gaetano.carlucci61050f62016-09-30 06:29:54 -0700271 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700272 (received_seq_max_ - received_seq_first_), ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000273
Qingsi Wang2370b082018-08-21 14:24:26 -0700274 return stats;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000275}
276
Yves Gerey665174f2018-06-19 15:03:05 +0200277void StreamStatisticianImpl::GetDataCounters(size_t* bytes_received,
278 uint32_t* packets_received) const {
danilchap7c9426c2016-04-14 03:05:31 -0700279 rtc::CritScope cs(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000280 if (bytes_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000281 *bytes_received = receive_counters_.transmitted.payload_bytes +
282 receive_counters_.transmitted.header_bytes +
283 receive_counters_.transmitted.padding_bytes;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000284 }
285 if (packets_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000286 *packets_received = receive_counters_.transmitted.packets;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000287 }
288}
289
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000290void StreamStatisticianImpl::GetReceiveStreamDataCounters(
291 StreamDataCounters* data_counters) const {
danilchap7c9426c2016-04-14 03:05:31 -0700292 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000293 *data_counters = receive_counters_;
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000294}
295
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000296uint32_t StreamStatisticianImpl::BitrateReceived() const {
danilchap7c9426c2016-04-14 03:05:31 -0700297 rtc::CritScope cs(&stream_lock_);
sprangcd349d92016-07-13 09:11:28 -0700298 return incoming_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000299}
300
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000301bool StreamStatisticianImpl::IsRetransmitOfOldPacket(
Niels Möllereda00872018-05-23 13:54:51 +0200302 const RTPHeader& header) const {
danilchap7c9426c2016-04-14 03:05:31 -0700303 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000304 if (InOrderPacketInternal(header.sequenceNumber)) {
305 return false;
306 }
307 uint32_t frequency_khz = header.payload_type_frequency / 1000;
308 assert(frequency_khz > 0);
309
Yves Gerey665174f2018-06-19 15:03:05 +0200310 int64_t time_diff_ms = clock_->TimeInMilliseconds() - last_receive_time_ms_;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000311
312 // Diff in time stamp since last received in order.
313 uint32_t timestamp_diff = header.timestamp - last_received_timestamp_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000314 uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000315
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000316 int64_t max_delay_ms = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000317
Niels Möllereda00872018-05-23 13:54:51 +0200318 // Jitter standard deviation in samples.
319 float jitter_std = sqrt(static_cast<float>(jitter_q4_ >> 4));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000320
Niels Möllereda00872018-05-23 13:54:51 +0200321 // 2 times the standard deviation => 95% confidence.
322 // And transform to milliseconds by dividing by the frequency in kHz.
323 max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz);
324
325 // Min max_delay_ms is 1.
326 if (max_delay_ms == 0) {
327 max_delay_ms = 1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000328 }
329 return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms;
330}
331
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000332bool StreamStatisticianImpl::InOrderPacketInternal(
333 uint16_t sequence_number) const {
334 // First packet is always in order.
Qingsi Wang2370b082018-08-21 14:24:26 -0700335 if (last_receive_time_ms_ == 0)
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000336 return true;
337
338 if (IsNewerSequenceNumber(sequence_number, received_seq_max_)) {
339 return true;
340 } else {
341 // If we have a restart of the remote side this packet is still in order.
Yves Gerey665174f2018-06-19 15:03:05 +0200342 return !IsNewerSequenceNumber(
343 sequence_number, received_seq_max_ - max_reordering_threshold_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000344 }
345}
346
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000347ReceiveStatistics* ReceiveStatistics::Create(Clock* clock) {
348 return new ReceiveStatisticsImpl(clock);
349}
350
351ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock)
352 : clock_(clock),
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100353 last_returned_ssrc_(0),
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000354 rtcp_stats_callback_(NULL),
355 rtp_stats_callback_(NULL) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000356
357ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
358 while (!statisticians_.empty()) {
359 delete statisticians_.begin()->second;
360 statisticians_.erase(statisticians_.begin());
361 }
362}
363
364void ReceiveStatisticsImpl::IncomingPacket(const RTPHeader& header,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000365 size_t packet_length,
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000366 bool retransmitted) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000367 StreamStatisticianImpl* impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000368 {
danilchap7c9426c2016-04-14 03:05:31 -0700369 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200370 auto it = statisticians_.find(header.ssrc);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000371 if (it != statisticians_.end()) {
372 impl = it->second;
373 } else {
danilchapec86be02017-08-14 05:51:02 -0700374 impl = new StreamStatisticianImpl(header.ssrc, clock_, this, this);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000375 statisticians_[header.ssrc] = impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000376 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000377 }
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000378 // StreamStatisticianImpl instance is created once and only destroyed when
379 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
380 // it's own locking so don't hold receive_statistics_lock_ (potential
381 // deadlock).
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000382 impl->IncomingPacket(header, packet_length, retransmitted);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000383}
384
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000385void ReceiveStatisticsImpl::FecPacketReceived(const RTPHeader& header,
386 size_t packet_length) {
danilchapec86be02017-08-14 05:51:02 -0700387 StreamStatisticianImpl* impl;
388 {
389 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200390 auto it = statisticians_.find(header.ssrc);
danilchapec86be02017-08-14 05:51:02 -0700391 // Ignore FEC if it is the first packet.
392 if (it == statisticians_.end())
393 return;
394 impl = it->second;
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000395 }
danilchapec86be02017-08-14 05:51:02 -0700396 impl->FecPacketReceived(header, packet_length);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000397}
398
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000399StreamStatistician* ReceiveStatisticsImpl::GetStatistician(
400 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700401 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200402 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
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000408void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
409 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700410 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200411 for (auto& statistician : statisticians_) {
412 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000413 }
414}
415
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000416void ReceiveStatisticsImpl::RegisterRtcpStatisticsCallback(
417 RtcpStatisticsCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700418 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000419 if (callback != NULL)
420 assert(rtcp_stats_callback_ == NULL);
421 rtcp_stats_callback_ = callback;
422}
423
424void ReceiveStatisticsImpl::StatisticsUpdated(const RtcpStatistics& statistics,
425 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700426 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000427 if (rtcp_stats_callback_)
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000428 rtcp_stats_callback_->StatisticsUpdated(statistics, ssrc);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000429}
430
431void ReceiveStatisticsImpl::CNameChanged(const char* cname, uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700432 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000433 if (rtcp_stats_callback_)
434 rtcp_stats_callback_->CNameChanged(cname, ssrc);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000435}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000436
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000437void ReceiveStatisticsImpl::RegisterRtpStatisticsCallback(
438 StreamDataCountersCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700439 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000440 if (callback != NULL)
441 assert(rtp_stats_callback_ == NULL);
442 rtp_stats_callback_ = callback;
443}
444
445void ReceiveStatisticsImpl::DataCountersUpdated(const StreamDataCounters& stats,
446 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700447 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000448 if (rtp_stats_callback_) {
449 rtp_stats_callback_->DataCountersUpdated(stats, ssrc);
450 }
451}
452
danilchap0bc84232017-08-11 08:12:54 -0700453std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700454 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200455 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
456 {
457 rtc::CritScope cs(&receive_statistics_lock_);
458 statisticians = statisticians_;
459 }
danilchapf5f793c2017-07-27 04:44:18 -0700460 std::vector<rtcp::ReportBlock> result;
461 result.reserve(std::min(max_blocks, statisticians.size()));
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100462 auto add_report_block = [&result](uint32_t media_ssrc,
463 StreamStatisticianImpl* statistician) {
danilchapf5f793c2017-07-27 04:44:18 -0700464 // Do we have receive statistics to send?
465 RtcpStatistics stats;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100466 if (!statistician->GetActiveStatisticsAndReset(&stats))
467 return;
danilchapf5f793c2017-07-27 04:44:18 -0700468 result.emplace_back();
469 rtcp::ReportBlock& block = result.back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100470 block.SetMediaSsrc(media_ssrc);
danilchapf5f793c2017-07-27 04:44:18 -0700471 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700472 if (!block.SetCumulativeLost(stats.packets_lost)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100473 RTC_LOG(LS_WARNING) << "Cumulative lost is oversized.";
danilchapf5f793c2017-07-27 04:44:18 -0700474 result.pop_back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100475 return;
danilchapf5f793c2017-07-27 04:44:18 -0700476 }
srte186d9c32017-08-04 05:03:53 -0700477 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700478 block.SetJitter(stats.jitter);
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100479 };
480
481 const auto start_it = statisticians.upper_bound(last_returned_ssrc_);
482 for (auto it = start_it;
483 result.size() < max_blocks && it != statisticians.end(); ++it)
484 add_report_block(it->first, it->second);
485 for (auto it = statisticians.begin();
486 result.size() < max_blocks && it != start_it; ++it)
487 add_report_block(it->first, it->second);
488
489 if (!result.empty())
490 last_returned_ssrc_ = result.back().source_ssrc();
danilchapf5f793c2017-07-27 04:44:18 -0700491 return result;
492}
493
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000494} // namespace webrtc