blob: bb2833f6109463246c4cdad0b067d88f2f6931d1 [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),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000042 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),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000049 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) {
danilchapec86be02017-08-14 05:51:02 -070060 auto counters = UpdateCounters(header, packet_length, retransmitted);
61 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) {
danilchap7c9426c2016-04-14 03:05:31 -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) -
124 (header.timestamp - last_received_timestamp_);
125
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,
156 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
165 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
sprang@webrtc.orgc00adbe2014-01-27 10:42:48 +0000175 *statistics = CalculateRtcpStatistics();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000176 }
177
danilchapec86be02017-08-14 05:51:02 -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
197 *statistics = CalculateRtcpStatistics();
198 }
199
200 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
201 return true;
202}
203
sprang@webrtc.orgc00adbe2014-01-27 10:42:48 +0000204RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics() {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000205 RtcpStatistics stats;
206
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000207 if (last_report_inorder_packets_ == 0) {
208 // First time we send a report.
209 last_report_seq_max_ = received_seq_first_ - 1;
210 }
211
212 // Calculate fraction lost.
213 uint16_t exp_since_last = (received_seq_max_ - last_report_seq_max_);
214
215 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 =
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000223 (receive_counters_.transmitted.packets -
224 receive_counters_.retransmitted.packets) - last_report_inorder_packets_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000225
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 =
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000236 receive_counters_.retransmitted.packets - last_report_old_packets_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000237 rec_since_last += retransmitted_packets;
238
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000239 int32_t missing = 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000240 if (exp_since_last > rec_since_last) {
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000241 missing = (exp_since_last - rec_since_last);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000242 }
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 =
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000247 static_cast<uint8_t>(255 * missing / exp_since_last);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000248 }
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000249 stats.fraction_lost = local_fraction_lost;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000250
251 // We need a counter for cumulative loss too.
danilchapa72e7342015-12-22 08:07:45 -0800252 // TODO(danilchap): Ensure cumulative loss is below maximum value of 2^24.
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000253 cumulative_loss_ += missing;
srte186d9c32017-08-04 05:03:53 -0700254 stats.packets_lost = cumulative_loss_;
255 stats.extended_highest_sequence_number =
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000256 (received_seq_wraps_ << 16) + received_seq_max_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000257 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000258 stats.jitter = jitter_q4_ >> 4;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000259
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000260 // Store this report.
261 last_reported_statistics_ = stats;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000262
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000263 // Only for report blocks in RTCP SR and RR.
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000264 last_report_inorder_packets_ =
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000265 receive_counters_.transmitted.packets -
266 receive_counters_.retransmitted.packets;
267 last_report_old_packets_ = receive_counters_.retransmitted.packets;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000268 last_report_seq_max_ = received_seq_max_;
gaetano.carlucci61050f62016-09-30 06:29:54 -0700269 BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts",
gaetano.carlucci52a57032016-09-14 05:04:36 -0700270 clock_->TimeInMilliseconds(),
271 cumulative_loss_, ssrc_);
272 BWE_TEST_LOGGING_PLOT_WITH_SSRC(
gaetano.carlucci61050f62016-09-30 06:29:54 -0700273 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700274 (received_seq_max_ - received_seq_first_), ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000275
276 return stats;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000277}
278
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000279void StreamStatisticianImpl::GetDataCounters(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000280 size_t* bytes_received, uint32_t* packets_received) const {
danilchap7c9426c2016-04-14 03:05:31 -0700281 rtc::CritScope cs(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000282 if (bytes_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000283 *bytes_received = receive_counters_.transmitted.payload_bytes +
284 receive_counters_.transmitted.header_bytes +
285 receive_counters_.transmitted.padding_bytes;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000286 }
287 if (packets_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000288 *packets_received = receive_counters_.transmitted.packets;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000289 }
290}
291
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000292void StreamStatisticianImpl::GetReceiveStreamDataCounters(
293 StreamDataCounters* data_counters) const {
danilchap7c9426c2016-04-14 03:05:31 -0700294 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000295 *data_counters = receive_counters_;
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000296}
297
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000298uint32_t StreamStatisticianImpl::BitrateReceived() const {
danilchap7c9426c2016-04-14 03:05:31 -0700299 rtc::CritScope cs(&stream_lock_);
sprangcd349d92016-07-13 09:11:28 -0700300 return incoming_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000301}
302
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000303bool StreamStatisticianImpl::IsRetransmitOfOldPacket(
Niels Möllereda00872018-05-23 13:54:51 +0200304 const RTPHeader& header) const {
danilchap7c9426c2016-04-14 03:05:31 -0700305 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000306 if (InOrderPacketInternal(header.sequenceNumber)) {
307 return false;
308 }
309 uint32_t frequency_khz = header.payload_type_frequency / 1000;
310 assert(frequency_khz > 0);
311
312 int64_t time_diff_ms = clock_->TimeInMilliseconds() -
313 last_receive_time_ms_;
314
315 // Diff in time stamp since last received in order.
316 uint32_t timestamp_diff = header.timestamp - last_received_timestamp_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000317 uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000318
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000319 int64_t max_delay_ms = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000320
Niels Möllereda00872018-05-23 13:54:51 +0200321 // Jitter standard deviation in samples.
322 float jitter_std = sqrt(static_cast<float>(jitter_q4_ >> 4));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000323
Niels Möllereda00872018-05-23 13:54:51 +0200324 // 2 times the standard deviation => 95% confidence.
325 // And transform to milliseconds by dividing by the frequency in kHz.
326 max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz);
327
328 // Min max_delay_ms is 1.
329 if (max_delay_ms == 0) {
330 max_delay_ms = 1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000331 }
332 return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms;
333}
334
335bool StreamStatisticianImpl::IsPacketInOrder(uint16_t sequence_number) const {
danilchap7c9426c2016-04-14 03:05:31 -0700336 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000337 return InOrderPacketInternal(sequence_number);
338}
339
340bool StreamStatisticianImpl::InOrderPacketInternal(
341 uint16_t sequence_number) const {
342 // First packet is always in order.
343 if (last_receive_time_ms_ == 0)
344 return true;
345
346 if (IsNewerSequenceNumber(sequence_number, received_seq_max_)) {
347 return true;
348 } else {
349 // If we have a restart of the remote side this packet is still in order.
350 return !IsNewerSequenceNumber(sequence_number, received_seq_max_ -
351 max_reordering_threshold_);
352 }
353}
354
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000355ReceiveStatistics* ReceiveStatistics::Create(Clock* clock) {
356 return new ReceiveStatisticsImpl(clock);
357}
358
359ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock)
360 : clock_(clock),
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100361 last_returned_ssrc_(0),
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000362 rtcp_stats_callback_(NULL),
363 rtp_stats_callback_(NULL) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000364
365ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
366 while (!statisticians_.empty()) {
367 delete statisticians_.begin()->second;
368 statisticians_.erase(statisticians_.begin());
369 }
370}
371
372void ReceiveStatisticsImpl::IncomingPacket(const RTPHeader& header,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000373 size_t packet_length,
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000374 bool retransmitted) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000375 StreamStatisticianImpl* impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000376 {
danilchap7c9426c2016-04-14 03:05:31 -0700377 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200378 auto it = statisticians_.find(header.ssrc);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000379 if (it != statisticians_.end()) {
380 impl = it->second;
381 } else {
danilchapec86be02017-08-14 05:51:02 -0700382 impl = new StreamStatisticianImpl(header.ssrc, clock_, this, this);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000383 statisticians_[header.ssrc] = impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000384 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000385 }
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000386 // StreamStatisticianImpl instance is created once and only destroyed when
387 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
388 // it's own locking so don't hold receive_statistics_lock_ (potential
389 // deadlock).
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000390 impl->IncomingPacket(header, packet_length, retransmitted);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000391}
392
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000393void ReceiveStatisticsImpl::FecPacketReceived(const RTPHeader& header,
394 size_t packet_length) {
danilchapec86be02017-08-14 05:51:02 -0700395 StreamStatisticianImpl* impl;
396 {
397 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200398 auto it = statisticians_.find(header.ssrc);
danilchapec86be02017-08-14 05:51:02 -0700399 // Ignore FEC if it is the first packet.
400 if (it == statisticians_.end())
401 return;
402 impl = it->second;
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000403 }
danilchapec86be02017-08-14 05:51:02 -0700404 impl->FecPacketReceived(header, packet_length);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000405}
406
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000407StreamStatistician* ReceiveStatisticsImpl::GetStatistician(
408 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700409 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200410 auto it = statisticians_.find(ssrc);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000411 if (it == statisticians_.end())
412 return NULL;
413 return it->second;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000414}
415
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000416void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
417 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700418 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200419 for (auto& statistician : statisticians_) {
420 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000421 }
422}
423
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000424void ReceiveStatisticsImpl::RegisterRtcpStatisticsCallback(
425 RtcpStatisticsCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700426 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000427 if (callback != NULL)
428 assert(rtcp_stats_callback_ == NULL);
429 rtcp_stats_callback_ = callback;
430}
431
432void ReceiveStatisticsImpl::StatisticsUpdated(const RtcpStatistics& statistics,
433 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700434 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000435 if (rtcp_stats_callback_)
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000436 rtcp_stats_callback_->StatisticsUpdated(statistics, ssrc);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000437}
438
439void ReceiveStatisticsImpl::CNameChanged(const char* cname, uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700440 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000441 if (rtcp_stats_callback_)
442 rtcp_stats_callback_->CNameChanged(cname, ssrc);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000443}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000444
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000445void ReceiveStatisticsImpl::RegisterRtpStatisticsCallback(
446 StreamDataCountersCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700447 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000448 if (callback != NULL)
449 assert(rtp_stats_callback_ == NULL);
450 rtp_stats_callback_ = callback;
451}
452
453void ReceiveStatisticsImpl::DataCountersUpdated(const StreamDataCounters& stats,
454 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700455 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000456 if (rtp_stats_callback_) {
457 rtp_stats_callback_->DataCountersUpdated(stats, ssrc);
458 }
459}
460
danilchap0bc84232017-08-11 08:12:54 -0700461std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700462 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200463 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
464 {
465 rtc::CritScope cs(&receive_statistics_lock_);
466 statisticians = statisticians_;
467 }
danilchapf5f793c2017-07-27 04:44:18 -0700468 std::vector<rtcp::ReportBlock> result;
469 result.reserve(std::min(max_blocks, statisticians.size()));
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100470 auto add_report_block = [&result](uint32_t media_ssrc,
471 StreamStatisticianImpl* statistician) {
danilchapf5f793c2017-07-27 04:44:18 -0700472 // Do we have receive statistics to send?
473 RtcpStatistics stats;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100474 if (!statistician->GetActiveStatisticsAndReset(&stats))
475 return;
danilchapf5f793c2017-07-27 04:44:18 -0700476 result.emplace_back();
477 rtcp::ReportBlock& block = result.back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100478 block.SetMediaSsrc(media_ssrc);
danilchapf5f793c2017-07-27 04:44:18 -0700479 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700480 if (!block.SetCumulativeLost(stats.packets_lost)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100481 RTC_LOG(LS_WARNING) << "Cumulative lost is oversized.";
danilchapf5f793c2017-07-27 04:44:18 -0700482 result.pop_back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100483 return;
danilchapf5f793c2017-07-27 04:44:18 -0700484 }
srte186d9c32017-08-04 05:03:53 -0700485 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700486 block.SetJitter(stats.jitter);
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100487 };
488
489 const auto start_it = statisticians.upper_bound(last_returned_ssrc_);
490 for (auto it = start_it;
491 result.size() < max_blocks && it != statisticians.end(); ++it)
492 add_report_block(it->first, it->second);
493 for (auto it = statisticians.begin();
494 result.size() < max_blocks && it != start_it; ++it)
495 add_report_block(it->first, it->second);
496
497 if (!result.empty())
498 last_returned_ssrc_ = result.back().source_ssrc();
danilchapf5f793c2017-07-27 04:44:18 -0700499 return result;
500}
501
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000502} // namespace webrtc