blob: 888fa929e7b472eb58ceab76479eba4d5f4831a4 [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) -
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,
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.
Yves Gerey665174f2018-06-19 15:03:05 +0200222 uint32_t rec_since_last = (receive_counters_.transmitted.packets -
223 receive_counters_.retransmitted.packets) -
224 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.
Yves Gerey665174f2018-06-19 15:03:05 +0200246 local_fraction_lost = static_cast<uint8_t>(255 * missing / exp_since_last);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000247 }
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000248 stats.fraction_lost = local_fraction_lost;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000249
250 // We need a counter for cumulative loss too.
danilchapa72e7342015-12-22 08:07:45 -0800251 // TODO(danilchap): Ensure cumulative loss is below maximum value of 2^24.
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000252 cumulative_loss_ += missing;
srte186d9c32017-08-04 05:03:53 -0700253 stats.packets_lost = cumulative_loss_;
254 stats.extended_highest_sequence_number =
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000255 (received_seq_wraps_ << 16) + received_seq_max_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000256 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000257 stats.jitter = jitter_q4_ >> 4;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000258
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000259 // Store this report.
260 last_reported_statistics_ = stats;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000261
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000262 // Only for report blocks in RTCP SR and RR.
Yves Gerey665174f2018-06-19 15:03:05 +0200263 last_report_inorder_packets_ = receive_counters_.transmitted.packets -
264 receive_counters_.retransmitted.packets;
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000265 last_report_old_packets_ = receive_counters_.retransmitted.packets;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000266 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(),
269 cumulative_loss_, ssrc_);
270 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
274 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
332bool StreamStatisticianImpl::IsPacketInOrder(uint16_t sequence_number) const {
danilchap7c9426c2016-04-14 03:05:31 -0700333 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000334 return InOrderPacketInternal(sequence_number);
335}
336
337bool StreamStatisticianImpl::InOrderPacketInternal(
338 uint16_t sequence_number) const {
339 // First packet is always in order.
340 if (last_receive_time_ms_ == 0)
341 return true;
342
343 if (IsNewerSequenceNumber(sequence_number, received_seq_max_)) {
344 return true;
345 } else {
346 // If we have a restart of the remote side this packet is still in order.
Yves Gerey665174f2018-06-19 15:03:05 +0200347 return !IsNewerSequenceNumber(
348 sequence_number, received_seq_max_ - max_reordering_threshold_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000349 }
350}
351
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000352ReceiveStatistics* ReceiveStatistics::Create(Clock* clock) {
353 return new ReceiveStatisticsImpl(clock);
354}
355
356ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock)
357 : clock_(clock),
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100358 last_returned_ssrc_(0),
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000359 rtcp_stats_callback_(NULL),
360 rtp_stats_callback_(NULL) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000361
362ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
363 while (!statisticians_.empty()) {
364 delete statisticians_.begin()->second;
365 statisticians_.erase(statisticians_.begin());
366 }
367}
368
369void ReceiveStatisticsImpl::IncomingPacket(const RTPHeader& header,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000370 size_t packet_length,
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000371 bool retransmitted) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000372 StreamStatisticianImpl* impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000373 {
danilchap7c9426c2016-04-14 03:05:31 -0700374 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200375 auto it = statisticians_.find(header.ssrc);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000376 if (it != statisticians_.end()) {
377 impl = it->second;
378 } else {
danilchapec86be02017-08-14 05:51:02 -0700379 impl = new StreamStatisticianImpl(header.ssrc, clock_, this, this);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000380 statisticians_[header.ssrc] = impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000381 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000382 }
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000383 // StreamStatisticianImpl instance is created once and only destroyed when
384 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
385 // it's own locking so don't hold receive_statistics_lock_ (potential
386 // deadlock).
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000387 impl->IncomingPacket(header, packet_length, retransmitted);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000388}
389
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000390void ReceiveStatisticsImpl::FecPacketReceived(const RTPHeader& header,
391 size_t packet_length) {
danilchapec86be02017-08-14 05:51:02 -0700392 StreamStatisticianImpl* impl;
393 {
394 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200395 auto it = statisticians_.find(header.ssrc);
danilchapec86be02017-08-14 05:51:02 -0700396 // Ignore FEC if it is the first packet.
397 if (it == statisticians_.end())
398 return;
399 impl = it->second;
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000400 }
danilchapec86be02017-08-14 05:51:02 -0700401 impl->FecPacketReceived(header, packet_length);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000402}
403
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000404StreamStatistician* ReceiveStatisticsImpl::GetStatistician(
405 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700406 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200407 auto it = statisticians_.find(ssrc);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000408 if (it == statisticians_.end())
409 return NULL;
410 return it->second;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000411}
412
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000413void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
414 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700415 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200416 for (auto& statistician : statisticians_) {
417 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000418 }
419}
420
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000421void ReceiveStatisticsImpl::RegisterRtcpStatisticsCallback(
422 RtcpStatisticsCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700423 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000424 if (callback != NULL)
425 assert(rtcp_stats_callback_ == NULL);
426 rtcp_stats_callback_ = callback;
427}
428
429void ReceiveStatisticsImpl::StatisticsUpdated(const RtcpStatistics& statistics,
430 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700431 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000432 if (rtcp_stats_callback_)
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000433 rtcp_stats_callback_->StatisticsUpdated(statistics, ssrc);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000434}
435
436void ReceiveStatisticsImpl::CNameChanged(const char* cname, uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700437 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000438 if (rtcp_stats_callback_)
439 rtcp_stats_callback_->CNameChanged(cname, ssrc);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000440}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000441
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000442void ReceiveStatisticsImpl::RegisterRtpStatisticsCallback(
443 StreamDataCountersCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700444 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000445 if (callback != NULL)
446 assert(rtp_stats_callback_ == NULL);
447 rtp_stats_callback_ = callback;
448}
449
450void ReceiveStatisticsImpl::DataCountersUpdated(const StreamDataCounters& stats,
451 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700452 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000453 if (rtp_stats_callback_) {
454 rtp_stats_callback_->DataCountersUpdated(stats, ssrc);
455 }
456}
457
danilchap0bc84232017-08-11 08:12:54 -0700458std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700459 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200460 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
461 {
462 rtc::CritScope cs(&receive_statistics_lock_);
463 statisticians = statisticians_;
464 }
danilchapf5f793c2017-07-27 04:44:18 -0700465 std::vector<rtcp::ReportBlock> result;
466 result.reserve(std::min(max_blocks, statisticians.size()));
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100467 auto add_report_block = [&result](uint32_t media_ssrc,
468 StreamStatisticianImpl* statistician) {
danilchapf5f793c2017-07-27 04:44:18 -0700469 // Do we have receive statistics to send?
470 RtcpStatistics stats;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100471 if (!statistician->GetActiveStatisticsAndReset(&stats))
472 return;
danilchapf5f793c2017-07-27 04:44:18 -0700473 result.emplace_back();
474 rtcp::ReportBlock& block = result.back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100475 block.SetMediaSsrc(media_ssrc);
danilchapf5f793c2017-07-27 04:44:18 -0700476 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700477 if (!block.SetCumulativeLost(stats.packets_lost)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100478 RTC_LOG(LS_WARNING) << "Cumulative lost is oversized.";
danilchapf5f793c2017-07-27 04:44:18 -0700479 result.pop_back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100480 return;
danilchapf5f793c2017-07-27 04:44:18 -0700481 }
srte186d9c32017-08-04 05:03:53 -0700482 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700483 block.SetJitter(stats.jitter);
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100484 };
485
486 const auto start_it = statisticians.upper_bound(last_returned_ssrc_);
487 for (auto it = start_it;
488 result.size() < max_blocks && it != statisticians.end(); ++it)
489 add_report_block(it->first, it->second);
490 for (auto it = statisticians.begin();
491 result.size() < max_blocks && it != start_it; ++it)
492 add_report_block(it->first, it->second);
493
494 if (!result.empty())
495 last_returned_ssrc_ = result.back().source_ssrc();
danilchapf5f793c2017-07-27 04:44:18 -0700496 return result;
497}
498
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000499} // namespace webrtc