blob: 7f3d5bb1ebfe32112669648159c1f9d2f084066b [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),
43 jitter_q4_transmission_time_offset_(0),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000044 last_receive_time_ms_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000045 last_received_timestamp_(0),
46 last_received_transmission_time_offset_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000047 received_seq_first_(0),
48 received_seq_max_(0),
49 received_seq_wraps_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000050 received_packet_overhead_(12),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000051 last_report_inorder_packets_(0),
52 last_report_old_packets_(0),
53 last_report_seq_max_(0),
sprang@webrtc.org0e932572014-01-23 10:00:39 +000054 rtcp_callback_(rtcp_callback),
55 rtp_callback_(rtp_callback) {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000056
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 }
136
137 // Extended jitter report, RFC 5450.
138 // Actual network jitter, excluding the source-introduced jitter.
139 int32_t time_diff_samples_ext =
140 (receive_time_rtp - last_receive_time_rtp) -
141 ((header.timestamp +
142 header.extension.transmissionTimeOffset) -
143 (last_received_timestamp_ +
144 last_received_transmission_time_offset_));
145
kwibergfd8be342016-05-14 19:44:11 -0700146 time_diff_samples_ext = std::abs(time_diff_samples_ext);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000147
148 if (time_diff_samples_ext < 450000) {
149 int32_t jitter_diffQ4TransmissionTimeOffset =
150 (time_diff_samples_ext << 4) - jitter_q4_transmission_time_offset_;
151 jitter_q4_transmission_time_offset_ +=
152 ((jitter_diffQ4TransmissionTimeOffset + 8) >> 4);
153 }
154}
155
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000156void StreamStatisticianImpl::FecPacketReceived(const RTPHeader& header,
157 size_t packet_length) {
danilchapec86be02017-08-14 05:51:02 -0700158 StreamDataCounters counters;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000159 {
danilchap7c9426c2016-04-14 03:05:31 -0700160 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000161 receive_counters_.fec.AddPacket(packet_length, header);
danilchapec86be02017-08-14 05:51:02 -0700162 counters = receive_counters_;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000163 }
danilchapec86be02017-08-14 05:51:02 -0700164 rtp_callback_->DataCountersUpdated(counters, ssrc_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000165}
166
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000167void StreamStatisticianImpl::SetMaxReorderingThreshold(
168 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700169 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000170 max_reordering_threshold_ = max_reordering_threshold;
171}
172
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000173bool StreamStatisticianImpl::GetStatistics(RtcpStatistics* statistics,
174 bool reset) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000175 {
danilchap7c9426c2016-04-14 03:05:31 -0700176 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000177 if (received_seq_first_ == 0 &&
178 receive_counters_.transmitted.payload_bytes == 0) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000179 // We have not received anything.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000180 return false;
181 }
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000182
183 if (!reset) {
184 if (last_report_inorder_packets_ == 0) {
185 // No report.
186 return false;
187 }
188 // Just get last report.
189 *statistics = last_reported_statistics_;
190 return true;
191 }
192
sprang@webrtc.orgc00adbe2014-01-27 10:42:48 +0000193 *statistics = CalculateRtcpStatistics();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000194 }
195
danilchapec86be02017-08-14 05:51:02 -0700196 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000197 return true;
198}
199
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200200bool StreamStatisticianImpl::GetActiveStatisticsAndReset(
201 RtcpStatistics* statistics) {
202 {
203 rtc::CritScope cs(&stream_lock_);
204 if (clock_->CurrentNtpInMilliseconds() - last_receive_time_ntp_.ToMs() >=
205 kStatisticsTimeoutMs) {
206 // Not active.
207 return false;
208 }
209 if (received_seq_first_ == 0 &&
210 receive_counters_.transmitted.payload_bytes == 0) {
211 // We have not received anything.
212 return false;
213 }
214
215 *statistics = CalculateRtcpStatistics();
216 }
217
218 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
219 return true;
220}
221
sprang@webrtc.orgc00adbe2014-01-27 10:42:48 +0000222RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics() {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000223 RtcpStatistics stats;
224
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000225 if (last_report_inorder_packets_ == 0) {
226 // First time we send a report.
227 last_report_seq_max_ = received_seq_first_ - 1;
228 }
229
230 // Calculate fraction lost.
231 uint16_t exp_since_last = (received_seq_max_ - last_report_seq_max_);
232
233 if (last_report_seq_max_ > received_seq_max_) {
234 // Can we assume that the seq_num can't go decrease over a full RTCP period?
235 exp_since_last = 0;
236 }
237
238 // Number of received RTP packets since last report, counts all packets but
239 // not re-transmissions.
240 uint32_t rec_since_last =
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000241 (receive_counters_.transmitted.packets -
242 receive_counters_.retransmitted.packets) - last_report_inorder_packets_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000243
244 // With NACK we don't know the expected retransmissions during the last
245 // second. We know how many "old" packets we have received. We just count
246 // the number of old received to estimate the loss, but it still does not
247 // guarantee an exact number since we run this based on time triggered by
248 // sending of an RTP packet. This should have a minimum effect.
249
250 // With NACK we don't count old packets as received since they are
251 // re-transmitted. We use RTT to decide if a packet is re-ordered or
252 // re-transmitted.
253 uint32_t retransmitted_packets =
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000254 receive_counters_.retransmitted.packets - last_report_old_packets_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000255 rec_since_last += retransmitted_packets;
256
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000257 int32_t missing = 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000258 if (exp_since_last > rec_since_last) {
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000259 missing = (exp_since_last - rec_since_last);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000260 }
261 uint8_t local_fraction_lost = 0;
262 if (exp_since_last) {
263 // Scale 0 to 255, where 255 is 100% loss.
264 local_fraction_lost =
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000265 static_cast<uint8_t>(255 * missing / exp_since_last);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000266 }
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000267 stats.fraction_lost = local_fraction_lost;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000268
269 // We need a counter for cumulative loss too.
danilchapa72e7342015-12-22 08:07:45 -0800270 // TODO(danilchap): Ensure cumulative loss is below maximum value of 2^24.
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000271 cumulative_loss_ += missing;
srte186d9c32017-08-04 05:03:53 -0700272 stats.packets_lost = cumulative_loss_;
273 stats.extended_highest_sequence_number =
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000274 (received_seq_wraps_ << 16) + received_seq_max_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000275 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000276 stats.jitter = jitter_q4_ >> 4;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000277
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000278 // Store this report.
279 last_reported_statistics_ = stats;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000280
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000281 // Only for report blocks in RTCP SR and RR.
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000282 last_report_inorder_packets_ =
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000283 receive_counters_.transmitted.packets -
284 receive_counters_.retransmitted.packets;
285 last_report_old_packets_ = receive_counters_.retransmitted.packets;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000286 last_report_seq_max_ = received_seq_max_;
gaetano.carlucci61050f62016-09-30 06:29:54 -0700287 BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts",
gaetano.carlucci52a57032016-09-14 05:04:36 -0700288 clock_->TimeInMilliseconds(),
289 cumulative_loss_, ssrc_);
290 BWE_TEST_LOGGING_PLOT_WITH_SSRC(
gaetano.carlucci61050f62016-09-30 06:29:54 -0700291 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700292 (received_seq_max_ - received_seq_first_), ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000293
294 return stats;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000295}
296
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000297void StreamStatisticianImpl::GetDataCounters(
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000298 size_t* bytes_received, uint32_t* packets_received) const {
danilchap7c9426c2016-04-14 03:05:31 -0700299 rtc::CritScope cs(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000300 if (bytes_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000301 *bytes_received = receive_counters_.transmitted.payload_bytes +
302 receive_counters_.transmitted.header_bytes +
303 receive_counters_.transmitted.padding_bytes;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000304 }
305 if (packets_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000306 *packets_received = receive_counters_.transmitted.packets;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000307 }
308}
309
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000310void StreamStatisticianImpl::GetReceiveStreamDataCounters(
311 StreamDataCounters* data_counters) const {
danilchap7c9426c2016-04-14 03:05:31 -0700312 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000313 *data_counters = receive_counters_;
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000314}
315
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000316uint32_t StreamStatisticianImpl::BitrateReceived() const {
danilchap7c9426c2016-04-14 03:05:31 -0700317 rtc::CritScope cs(&stream_lock_);
sprangcd349d92016-07-13 09:11:28 -0700318 return incoming_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000319}
320
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000321bool StreamStatisticianImpl::IsRetransmitOfOldPacket(
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000322 const RTPHeader& header, int64_t min_rtt) const {
danilchap7c9426c2016-04-14 03:05:31 -0700323 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000324 if (InOrderPacketInternal(header.sequenceNumber)) {
325 return false;
326 }
327 uint32_t frequency_khz = header.payload_type_frequency / 1000;
328 assert(frequency_khz > 0);
329
330 int64_t time_diff_ms = clock_->TimeInMilliseconds() -
331 last_receive_time_ms_;
332
333 // Diff in time stamp since last received in order.
334 uint32_t timestamp_diff = header.timestamp - last_received_timestamp_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000335 uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000336
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000337 int64_t max_delay_ms = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000338 if (min_rtt == 0) {
339 // Jitter standard deviation in samples.
340 float jitter_std = sqrt(static_cast<float>(jitter_q4_ >> 4));
341
342 // 2 times the standard deviation => 95% confidence.
343 // And transform to milliseconds by dividing by the frequency in kHz.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000344 max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000345
346 // Min max_delay_ms is 1.
347 if (max_delay_ms == 0) {
348 max_delay_ms = 1;
349 }
350 } else {
351 max_delay_ms = (min_rtt / 3) + 1;
352 }
353 return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms;
354}
355
356bool StreamStatisticianImpl::IsPacketInOrder(uint16_t sequence_number) const {
danilchap7c9426c2016-04-14 03:05:31 -0700357 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000358 return InOrderPacketInternal(sequence_number);
359}
360
361bool StreamStatisticianImpl::InOrderPacketInternal(
362 uint16_t sequence_number) const {
363 // First packet is always in order.
364 if (last_receive_time_ms_ == 0)
365 return true;
366
367 if (IsNewerSequenceNumber(sequence_number, received_seq_max_)) {
368 return true;
369 } else {
370 // If we have a restart of the remote side this packet is still in order.
371 return !IsNewerSequenceNumber(sequence_number, received_seq_max_ -
372 max_reordering_threshold_);
373 }
374}
375
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000376ReceiveStatistics* ReceiveStatistics::Create(Clock* clock) {
377 return new ReceiveStatisticsImpl(clock);
378}
379
380ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock)
381 : clock_(clock),
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000382 rtcp_stats_callback_(NULL),
383 rtp_stats_callback_(NULL) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000384
385ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
386 while (!statisticians_.empty()) {
387 delete statisticians_.begin()->second;
388 statisticians_.erase(statisticians_.begin());
389 }
390}
391
392void ReceiveStatisticsImpl::IncomingPacket(const RTPHeader& header,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000393 size_t packet_length,
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000394 bool retransmitted) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000395 StreamStatisticianImpl* impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000396 {
danilchap7c9426c2016-04-14 03:05:31 -0700397 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200398 auto it = statisticians_.find(header.ssrc);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000399 if (it != statisticians_.end()) {
400 impl = it->second;
401 } else {
danilchapec86be02017-08-14 05:51:02 -0700402 impl = new StreamStatisticianImpl(header.ssrc, clock_, this, this);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000403 statisticians_[header.ssrc] = impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000404 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000405 }
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000406 // StreamStatisticianImpl instance is created once and only destroyed when
407 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
408 // it's own locking so don't hold receive_statistics_lock_ (potential
409 // deadlock).
asapersson@webrtc.org97d04892014-12-09 09:47:53 +0000410 impl->IncomingPacket(header, packet_length, retransmitted);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000411}
412
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000413void ReceiveStatisticsImpl::FecPacketReceived(const RTPHeader& header,
414 size_t packet_length) {
danilchapec86be02017-08-14 05:51:02 -0700415 StreamStatisticianImpl* impl;
416 {
417 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200418 auto it = statisticians_.find(header.ssrc);
danilchapec86be02017-08-14 05:51:02 -0700419 // Ignore FEC if it is the first packet.
420 if (it == statisticians_.end())
421 return;
422 impl = it->second;
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000423 }
danilchapec86be02017-08-14 05:51:02 -0700424 impl->FecPacketReceived(header, packet_length);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000425}
426
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000427StreamStatistician* ReceiveStatisticsImpl::GetStatistician(
428 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700429 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200430 auto it = statisticians_.find(ssrc);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000431 if (it == statisticians_.end())
432 return NULL;
433 return it->second;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000434}
435
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000436void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
437 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700438 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200439 for (auto& statistician : statisticians_) {
440 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000441 }
442}
443
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000444void ReceiveStatisticsImpl::RegisterRtcpStatisticsCallback(
445 RtcpStatisticsCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700446 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000447 if (callback != NULL)
448 assert(rtcp_stats_callback_ == NULL);
449 rtcp_stats_callback_ = callback;
450}
451
452void ReceiveStatisticsImpl::StatisticsUpdated(const RtcpStatistics& statistics,
453 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700454 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000455 if (rtcp_stats_callback_)
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000456 rtcp_stats_callback_->StatisticsUpdated(statistics, ssrc);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000457}
458
459void ReceiveStatisticsImpl::CNameChanged(const char* cname, uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700460 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000461 if (rtcp_stats_callback_)
462 rtcp_stats_callback_->CNameChanged(cname, ssrc);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000463}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000464
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000465void ReceiveStatisticsImpl::RegisterRtpStatisticsCallback(
466 StreamDataCountersCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700467 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000468 if (callback != NULL)
469 assert(rtp_stats_callback_ == NULL);
470 rtp_stats_callback_ = callback;
471}
472
473void ReceiveStatisticsImpl::DataCountersUpdated(const StreamDataCounters& stats,
474 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700475 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000476 if (rtp_stats_callback_) {
477 rtp_stats_callback_->DataCountersUpdated(stats, ssrc);
478 }
479}
480
danilchap0bc84232017-08-11 08:12:54 -0700481std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700482 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200483 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
484 {
485 rtc::CritScope cs(&receive_statistics_lock_);
486 statisticians = statisticians_;
487 }
danilchapf5f793c2017-07-27 04:44:18 -0700488 std::vector<rtcp::ReportBlock> result;
489 result.reserve(std::min(max_blocks, statisticians.size()));
490 for (auto& statistician : statisticians) {
491 // TODO(danilchap): Select statistician subset across multiple calls using
492 // round-robin, as described in rfc3550 section 6.4 when single
493 // rtcp_module/receive_statistics will be used for more rtp streams.
494 if (result.size() == max_blocks)
495 break;
496
497 // Do we have receive statistics to send?
498 RtcpStatistics stats;
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200499 if (!statistician.second->GetActiveStatisticsAndReset(&stats))
danilchapf5f793c2017-07-27 04:44:18 -0700500 continue;
501 result.emplace_back();
502 rtcp::ReportBlock& block = result.back();
503 block.SetMediaSsrc(statistician.first);
504 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700505 if (!block.SetCumulativeLost(stats.packets_lost)) {
danilchapf5f793c2017-07-27 04:44:18 -0700506 LOG(LS_WARNING) << "Cumulative lost is oversized.";
507 result.pop_back();
508 continue;
509 }
srte186d9c32017-08-04 05:03:53 -0700510 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700511 block.SetJitter(stats.jitter);
512 }
513 return result;
514}
515
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000516} // namespace webrtc