blob: 538ff7b2b79c07d4c2c5c8cec6baf0b9086afdaa [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
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000055void StreamStatisticianImpl::IncomingPacket(const RTPHeader& header,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +000056 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000057 bool retransmitted) {
danilchapec86be02017-08-14 05:51:02 -070058 auto counters = UpdateCounters(header, packet_length, retransmitted);
59 rtp_callback_->DataCountersUpdated(counters, ssrc_);
sprang@webrtc.orga45cac02014-01-27 16:22:08 +000060}
61
danilchapec86be02017-08-14 05:51:02 -070062StreamDataCounters StreamStatisticianImpl::UpdateCounters(
63 const RTPHeader& header,
64 size_t packet_length,
65 bool retransmitted) {
danilchap7c9426c2016-04-14 03:05:31 -070066 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000067 bool in_order = InOrderPacketInternal(header.sequenceNumber);
danilchapec86be02017-08-14 05:51:02 -070068 RTC_DCHECK_EQ(ssrc_, header.ssrc);
sprangcd349d92016-07-13 09:11:28 -070069 incoming_bitrate_.Update(packet_length, clock_->TimeInMilliseconds());
asapersson@webrtc.org44149392015-02-04 08:34:47 +000070 receive_counters_.transmitted.AddPacket(packet_length, header);
sprang@webrtc.org0e932572014-01-23 10:00:39 +000071 if (!in_order && retransmitted) {
asapersson@webrtc.org44149392015-02-04 08:34:47 +000072 receive_counters_.retransmitted.AddPacket(packet_length, header);
sprang@webrtc.org0e932572014-01-23 10:00:39 +000073 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +000074
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +000075 if (receive_counters_.transmitted.packets == 1) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000076 received_seq_first_ = header.sequenceNumber;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +000077 receive_counters_.first_packet_time_ms = clock_->TimeInMilliseconds();
wu@webrtc.org822fbd82013-08-15 23:38:54 +000078 }
79
80 // Count only the new packets received. That is, if packets 1, 2, 3, 5, 4, 6
81 // are received, 4 will be ignored.
82 if (in_order) {
83 // Current time in samples.
danilchap37953762017-02-09 11:15:25 -080084 NtpTime receive_time = clock_->CurrentNtpTime();
wu@webrtc.org822fbd82013-08-15 23:38:54 +000085
86 // Wrong if we use RetransmitOfOldPacket.
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +000087 if (receive_counters_.transmitted.packets > 1 &&
sprang@webrtc.org0e932572014-01-23 10:00:39 +000088 received_seq_max_ > header.sequenceNumber) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000089 // Wrap around detected.
90 received_seq_wraps_++;
91 }
92 // New max.
93 received_seq_max_ = header.sequenceNumber;
94
sprang@webrtc.org0e932572014-01-23 10:00:39 +000095 // If new time stamp and more than one in-order packet received, calculate
96 // new jitter statistics.
wu@webrtc.org822fbd82013-08-15 23:38:54 +000097 if (header.timestamp != last_received_timestamp_ &&
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +000098 (receive_counters_.transmitted.packets -
99 receive_counters_.retransmitted.packets) > 1) {
danilchap1227e8b2015-12-21 11:06:50 -0800100 UpdateJitter(header, receive_time);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000101 }
102 last_received_timestamp_ = header.timestamp;
danilchap1227e8b2015-12-21 11:06:50 -0800103 last_receive_time_ntp_ = receive_time;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000104 last_receive_time_ms_ = clock_->TimeInMilliseconds();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000105 }
106
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000107 size_t packet_oh = header.headerLength + header.paddingLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000108
109 // Our measured overhead. Filter from RFC 5104 4.2.1.2:
110 // avg_OH (new) = 15/16*avg_OH (old) + 1/16*pckt_OH,
111 received_packet_overhead_ = (15 * received_packet_overhead_ + packet_oh) >> 4;
danilchapec86be02017-08-14 05:51:02 -0700112 return receive_counters_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000113}
114
115void StreamStatisticianImpl::UpdateJitter(const RTPHeader& header,
danilchap1227e8b2015-12-21 11:06:50 -0800116 NtpTime receive_time) {
117 uint32_t receive_time_rtp =
118 NtpToRtp(receive_time, header.payload_type_frequency);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000119 uint32_t last_receive_time_rtp =
danilchap1227e8b2015-12-21 11:06:50 -0800120 NtpToRtp(last_receive_time_ntp_, header.payload_type_frequency);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000121 int32_t time_diff_samples = (receive_time_rtp - last_receive_time_rtp) -
122 (header.timestamp - last_received_timestamp_);
123
kwibergfd8be342016-05-14 19:44:11 -0700124 time_diff_samples = std::abs(time_diff_samples);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000125
126 // lib_jingle sometimes deliver crazy jumps in TS for the same stream.
127 // If this happens, don't update jitter value. Use 5 secs video frequency
128 // as the threshold.
129 if (time_diff_samples < 450000) {
130 // Note we calculate in Q4 to avoid using float.
131 int32_t jitter_diff_q4 = (time_diff_samples << 4) - jitter_q4_;
132 jitter_q4_ += ((jitter_diff_q4 + 8) >> 4);
133 }
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000134}
135
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000136void StreamStatisticianImpl::FecPacketReceived(const RTPHeader& header,
137 size_t packet_length) {
danilchapec86be02017-08-14 05:51:02 -0700138 StreamDataCounters counters;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000139 {
danilchap7c9426c2016-04-14 03:05:31 -0700140 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000141 receive_counters_.fec.AddPacket(packet_length, header);
danilchapec86be02017-08-14 05:51:02 -0700142 counters = receive_counters_;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000143 }
danilchapec86be02017-08-14 05:51:02 -0700144 rtp_callback_->DataCountersUpdated(counters, ssrc_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000145}
146
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000147void StreamStatisticianImpl::SetMaxReorderingThreshold(
148 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700149 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000150 max_reordering_threshold_ = max_reordering_threshold;
151}
152
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000153bool StreamStatisticianImpl::GetStatistics(RtcpStatistics* statistics,
154 bool reset) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000155 {
danilchap7c9426c2016-04-14 03:05:31 -0700156 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000157 if (received_seq_first_ == 0 &&
158 receive_counters_.transmitted.payload_bytes == 0) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000159 // We have not received anything.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000160 return false;
161 }
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000162
163 if (!reset) {
164 if (last_report_inorder_packets_ == 0) {
165 // No report.
166 return false;
167 }
168 // Just get last report.
169 *statistics = last_reported_statistics_;
170 return true;
171 }
172
sprang@webrtc.orgc00adbe2014-01-27 10:42:48 +0000173 *statistics = CalculateRtcpStatistics();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000174 }
175
danilchapec86be02017-08-14 05:51:02 -0700176 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000177 return true;
178}
179
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200180bool StreamStatisticianImpl::GetActiveStatisticsAndReset(
181 RtcpStatistics* statistics) {
182 {
183 rtc::CritScope cs(&stream_lock_);
184 if (clock_->CurrentNtpInMilliseconds() - last_receive_time_ntp_.ToMs() >=
185 kStatisticsTimeoutMs) {
186 // Not active.
187 return false;
188 }
189 if (received_seq_first_ == 0 &&
190 receive_counters_.transmitted.payload_bytes == 0) {
191 // We have not received anything.
192 return false;
193 }
194
195 *statistics = CalculateRtcpStatistics();
196 }
197
198 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
199 return true;
200}
201
sprang@webrtc.orgc00adbe2014-01-27 10:42:48 +0000202RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics() {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000203 RtcpStatistics stats;
204
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000205 if (last_report_inorder_packets_ == 0) {
206 // First time we send a report.
207 last_report_seq_max_ = received_seq_first_ - 1;
208 }
209
210 // Calculate fraction lost.
211 uint16_t exp_since_last = (received_seq_max_ - last_report_seq_max_);
212
213 if (last_report_seq_max_ > received_seq_max_) {
214 // Can we assume that the seq_num can't go decrease over a full RTCP period?
215 exp_since_last = 0;
216 }
217
218 // Number of received RTP packets since last report, counts all packets but
219 // not re-transmissions.
220 uint32_t rec_since_last =
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000221 (receive_counters_.transmitted.packets -
222 receive_counters_.retransmitted.packets) - last_report_inorder_packets_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000223
224 // With NACK we don't know the expected retransmissions during the last
225 // second. We know how many "old" packets we have received. We just count
226 // the number of old received to estimate the loss, but it still does not
227 // guarantee an exact number since we run this based on time triggered by
228 // sending of an RTP packet. This should have a minimum effect.
229
230 // With NACK we don't count old packets as received since they are
231 // re-transmitted. We use RTT to decide if a packet is re-ordered or
232 // re-transmitted.
233 uint32_t retransmitted_packets =
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000234 receive_counters_.retransmitted.packets - last_report_old_packets_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000235 rec_since_last += retransmitted_packets;
236
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000237 int32_t missing = 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000238 if (exp_since_last > rec_since_last) {
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000239 missing = (exp_since_last - rec_since_last);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000240 }
241 uint8_t local_fraction_lost = 0;
242 if (exp_since_last) {
243 // Scale 0 to 255, where 255 is 100% loss.
244 local_fraction_lost =
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000245 static_cast<uint8_t>(255 * missing / exp_since_last);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000246 }
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000247 stats.fraction_lost = local_fraction_lost;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000248
249 // We need a counter for cumulative loss too.
danilchapa72e7342015-12-22 08:07:45 -0800250 // TODO(danilchap): Ensure cumulative loss is below maximum value of 2^24.
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000251 cumulative_loss_ += missing;
srte186d9c32017-08-04 05:03:53 -0700252 stats.packets_lost = cumulative_loss_;
253 stats.extended_highest_sequence_number =
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000254 (received_seq_wraps_ << 16) + received_seq_max_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000255 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000256 stats.jitter = jitter_q4_ >> 4;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000257
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000258 // Store this report.
259 last_reported_statistics_ = stats;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000260
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000261 // Only for report blocks in RTCP SR and RR.
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000262 last_report_inorder_packets_ =
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000263 receive_counters_.transmitted.packets -
264 receive_counters_.retransmitted.packets;
265 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
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000277void StreamStatisticianImpl::GetDataCounters(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000278 size_t* bytes_received, 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(
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000302 const RTPHeader& header, int64_t min_rtt) 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
310 int64_t time_diff_ms = clock_->TimeInMilliseconds() -
311 last_receive_time_ms_;
312
313 // Diff in time stamp since last received in order.
314 uint32_t timestamp_diff = header.timestamp - last_received_timestamp_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000315 uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000316
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000317 int64_t max_delay_ms = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000318 if (min_rtt == 0) {
319 // Jitter standard deviation in samples.
320 float jitter_std = sqrt(static_cast<float>(jitter_q4_ >> 4));
321
322 // 2 times the standard deviation => 95% confidence.
323 // And transform to milliseconds by dividing by the frequency in kHz.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000324 max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000325
326 // Min max_delay_ms is 1.
327 if (max_delay_ms == 0) {
328 max_delay_ms = 1;
329 }
330 } else {
331 max_delay_ms = (min_rtt / 3) + 1;
332 }
333 return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms;
334}
335
336bool StreamStatisticianImpl::IsPacketInOrder(uint16_t sequence_number) const {
danilchap7c9426c2016-04-14 03:05:31 -0700337 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000338 return InOrderPacketInternal(sequence_number);
339}
340
341bool StreamStatisticianImpl::InOrderPacketInternal(
342 uint16_t sequence_number) const {
343 // First packet is always in order.
344 if (last_receive_time_ms_ == 0)
345 return true;
346
347 if (IsNewerSequenceNumber(sequence_number, received_seq_max_)) {
348 return true;
349 } else {
350 // If we have a restart of the remote side this packet is still in order.
351 return !IsNewerSequenceNumber(sequence_number, received_seq_max_ -
352 max_reordering_threshold_);
353 }
354}
355
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000356ReceiveStatistics* ReceiveStatistics::Create(Clock* clock) {
357 return new ReceiveStatisticsImpl(clock);
358}
359
360ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock)
361 : clock_(clock),
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()));
470 for (auto& statistician : statisticians) {
471 // TODO(danilchap): Select statistician subset across multiple calls using
472 // round-robin, as described in rfc3550 section 6.4 when single
473 // rtcp_module/receive_statistics will be used for more rtp streams.
474 if (result.size() == max_blocks)
475 break;
476
477 // Do we have receive statistics to send?
478 RtcpStatistics stats;
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200479 if (!statistician.second->GetActiveStatisticsAndReset(&stats))
danilchapf5f793c2017-07-27 04:44:18 -0700480 continue;
481 result.emplace_back();
482 rtcp::ReportBlock& block = result.back();
483 block.SetMediaSsrc(statistician.first);
484 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700485 if (!block.SetCumulativeLost(stats.packets_lost)) {
danilchapf5f793c2017-07-27 04:44:18 -0700486 LOG(LS_WARNING) << "Cumulative lost is oversized.";
487 result.pop_back();
488 continue;
489 }
srte186d9c32017-08-04 05:03:53 -0700490 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700491 block.SetJitter(stats.jitter);
492 }
493 return result;
494}
495
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000496} // namespace webrtc