wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/rtp_rtcp/source/receive_statistics_impl.h" |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 12 | |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 13 | #include <math.h> |
| 14 | |
kwiberg | fd8be34 | 2016-05-14 19:44:11 -0700 | [diff] [blame] | 15 | #include <cstdlib> |
danilchap | f5f793c | 2017-07-27 04:44:18 -0700 | [diff] [blame] | 16 | #include <vector> |
kwiberg | fd8be34 | 2016-05-14 19:44:11 -0700 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #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.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 26 | const int64_t kStatisticsTimeoutMs = 8000; |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 27 | const int64_t kStatisticsProcessIntervalMs = 1000; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 28 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 29 | StreamStatistician::~StreamStatistician() {} |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 30 | |
sprang@webrtc.org | 54ae4ff | 2013-12-19 13:26:02 +0000 | [diff] [blame] | 31 | StreamStatisticianImpl::StreamStatisticianImpl( |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 32 | uint32_t ssrc, |
sprang@webrtc.org | 54ae4ff | 2013-12-19 13:26:02 +0000 | [diff] [blame] | 33 | Clock* clock, |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 34 | RtcpStatisticsCallback* rtcp_callback, |
| 35 | StreamDataCountersCallback* rtp_callback) |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 36 | : ssrc_(ssrc), |
| 37 | clock_(clock), |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 38 | incoming_bitrate_(kStatisticsProcessIntervalMs, |
| 39 | RateStatistics::kBpsScale), |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 40 | max_reordering_threshold_(kDefaultMaxReorderingThreshold), |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 41 | jitter_q4_(0), |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 42 | last_receive_time_ms_(0), |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 43 | last_received_timestamp_(0), |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 44 | received_seq_first_(0), |
| 45 | received_seq_max_(0), |
| 46 | received_seq_wraps_(0), |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 47 | received_packet_overhead_(12), |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 48 | rtcp_callback_(rtcp_callback), |
| 49 | rtp_callback_(rtp_callback) {} |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 50 | |
Danil Chapovalov | 2a5ce2b | 2018-02-07 09:38:31 +0100 | [diff] [blame] | 51 | StreamStatisticianImpl::~StreamStatisticianImpl() = default; |
| 52 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 53 | void StreamStatisticianImpl::IncomingPacket(const RTPHeader& header, |
asapersson@webrtc.org | 97d0489 | 2014-12-09 09:47:53 +0000 | [diff] [blame] | 54 | size_t packet_length, |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 55 | bool retransmitted) { |
Taylor Brandstetter | 8491693 | 2018-06-25 15:50:26 -0700 | [diff] [blame^] | 56 | StreamDataCounters counters; |
| 57 | RtcpStatistics rtcp_stats; |
| 58 | { |
| 59 | rtc::CritScope cs(&stream_lock_); |
| 60 | counters = UpdateCounters(header, packet_length, retransmitted); |
| 61 | // We only want to recalculate |fraction_lost| when sending an RTCP SR or |
| 62 | // RR. |
| 63 | rtcp_stats = CalculateRtcpStatistics(/*update_fraction_lost=*/false); |
| 64 | } |
| 65 | |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 66 | rtp_callback_->DataCountersUpdated(counters, ssrc_); |
Taylor Brandstetter | 8491693 | 2018-06-25 15:50:26 -0700 | [diff] [blame^] | 67 | rtcp_callback_->StatisticsUpdated(rtcp_stats, ssrc_); |
sprang@webrtc.org | a45cac0 | 2014-01-27 16:22:08 +0000 | [diff] [blame] | 68 | } |
| 69 | |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 70 | StreamDataCounters StreamStatisticianImpl::UpdateCounters( |
| 71 | const RTPHeader& header, |
| 72 | size_t packet_length, |
| 73 | bool retransmitted) { |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 74 | bool in_order = InOrderPacketInternal(header.sequenceNumber); |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 75 | RTC_DCHECK_EQ(ssrc_, header.ssrc); |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 76 | incoming_bitrate_.Update(packet_length, clock_->TimeInMilliseconds()); |
asapersson@webrtc.org | 4414939 | 2015-02-04 08:34:47 +0000 | [diff] [blame] | 77 | receive_counters_.transmitted.AddPacket(packet_length, header); |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 78 | if (!in_order && retransmitted) { |
asapersson@webrtc.org | 4414939 | 2015-02-04 08:34:47 +0000 | [diff] [blame] | 79 | receive_counters_.retransmitted.AddPacket(packet_length, header); |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 80 | } |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 81 | |
asapersson@webrtc.org | cfd82df | 2015-01-22 09:39:59 +0000 | [diff] [blame] | 82 | if (receive_counters_.transmitted.packets == 1) { |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 83 | received_seq_first_ = header.sequenceNumber; |
asapersson@webrtc.org | d08d389 | 2014-12-16 12:03:11 +0000 | [diff] [blame] | 84 | receive_counters_.first_packet_time_ms = clock_->TimeInMilliseconds(); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Count only the new packets received. That is, if packets 1, 2, 3, 5, 4, 6 |
| 88 | // are received, 4 will be ignored. |
| 89 | if (in_order) { |
| 90 | // Current time in samples. |
danilchap | 3795376 | 2017-02-09 11:15:25 -0800 | [diff] [blame] | 91 | NtpTime receive_time = clock_->CurrentNtpTime(); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 92 | |
| 93 | // Wrong if we use RetransmitOfOldPacket. |
asapersson@webrtc.org | cfd82df | 2015-01-22 09:39:59 +0000 | [diff] [blame] | 94 | if (receive_counters_.transmitted.packets > 1 && |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 95 | received_seq_max_ > header.sequenceNumber) { |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 96 | // Wrap around detected. |
| 97 | received_seq_wraps_++; |
| 98 | } |
| 99 | // New max. |
| 100 | received_seq_max_ = header.sequenceNumber; |
| 101 | |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 102 | // If new time stamp and more than one in-order packet received, calculate |
| 103 | // new jitter statistics. |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 104 | if (header.timestamp != last_received_timestamp_ && |
asapersson@webrtc.org | cfd82df | 2015-01-22 09:39:59 +0000 | [diff] [blame] | 105 | (receive_counters_.transmitted.packets - |
| 106 | receive_counters_.retransmitted.packets) > 1) { |
danilchap | 1227e8b | 2015-12-21 11:06:50 -0800 | [diff] [blame] | 107 | UpdateJitter(header, receive_time); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 108 | } |
| 109 | last_received_timestamp_ = header.timestamp; |
danilchap | 1227e8b | 2015-12-21 11:06:50 -0800 | [diff] [blame] | 110 | last_receive_time_ntp_ = receive_time; |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 111 | last_receive_time_ms_ = clock_->TimeInMilliseconds(); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 112 | } |
| 113 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 114 | size_t packet_oh = header.headerLength + header.paddingLength; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 115 | |
| 116 | // Our measured overhead. Filter from RFC 5104 4.2.1.2: |
| 117 | // avg_OH (new) = 15/16*avg_OH (old) + 1/16*pckt_OH, |
| 118 | received_packet_overhead_ = (15 * received_packet_overhead_ + packet_oh) >> 4; |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 119 | return receive_counters_; |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void StreamStatisticianImpl::UpdateJitter(const RTPHeader& header, |
danilchap | 1227e8b | 2015-12-21 11:06:50 -0800 | [diff] [blame] | 123 | NtpTime receive_time) { |
| 124 | uint32_t receive_time_rtp = |
| 125 | NtpToRtp(receive_time, header.payload_type_frequency); |
pbos@webrtc.org | 62bafae | 2014-07-08 12:10:51 +0000 | [diff] [blame] | 126 | uint32_t last_receive_time_rtp = |
danilchap | 1227e8b | 2015-12-21 11:06:50 -0800 | [diff] [blame] | 127 | NtpToRtp(last_receive_time_ntp_, header.payload_type_frequency); |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 128 | int32_t time_diff_samples = (receive_time_rtp - last_receive_time_rtp) - |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 129 | (header.timestamp - last_received_timestamp_); |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 130 | |
kwiberg | fd8be34 | 2016-05-14 19:44:11 -0700 | [diff] [blame] | 131 | time_diff_samples = std::abs(time_diff_samples); |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 132 | |
| 133 | // lib_jingle sometimes deliver crazy jumps in TS for the same stream. |
| 134 | // If this happens, don't update jitter value. Use 5 secs video frequency |
| 135 | // as the threshold. |
| 136 | if (time_diff_samples < 450000) { |
| 137 | // Note we calculate in Q4 to avoid using float. |
| 138 | int32_t jitter_diff_q4 = (time_diff_samples << 4) - jitter_q4_; |
| 139 | jitter_q4_ += ((jitter_diff_q4 + 8) >> 4); |
| 140 | } |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 141 | } |
| 142 | |
asapersson@webrtc.org | 273fbbb | 2015-01-27 12:17:29 +0000 | [diff] [blame] | 143 | void StreamStatisticianImpl::FecPacketReceived(const RTPHeader& header, |
| 144 | size_t packet_length) { |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 145 | StreamDataCounters counters; |
sprang@webrtc.org | a45cac0 | 2014-01-27 16:22:08 +0000 | [diff] [blame] | 146 | { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 147 | rtc::CritScope cs(&stream_lock_); |
asapersson@webrtc.org | 4414939 | 2015-02-04 08:34:47 +0000 | [diff] [blame] | 148 | receive_counters_.fec.AddPacket(packet_length, header); |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 149 | counters = receive_counters_; |
sprang@webrtc.org | a45cac0 | 2014-01-27 16:22:08 +0000 | [diff] [blame] | 150 | } |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 151 | rtp_callback_->DataCountersUpdated(counters, ssrc_); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 152 | } |
| 153 | |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 154 | void StreamStatisticianImpl::SetMaxReorderingThreshold( |
| 155 | int max_reordering_threshold) { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 156 | rtc::CritScope cs(&stream_lock_); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 157 | max_reordering_threshold_ = max_reordering_threshold; |
| 158 | } |
| 159 | |
sprang@webrtc.org | 54ae4ff | 2013-12-19 13:26:02 +0000 | [diff] [blame] | 160 | bool StreamStatisticianImpl::GetStatistics(RtcpStatistics* statistics, |
Taylor Brandstetter | 8491693 | 2018-06-25 15:50:26 -0700 | [diff] [blame^] | 161 | bool update_fraction_lost) { |
sprang@webrtc.org | 7dba27c | 2014-01-21 16:33:37 +0000 | [diff] [blame] | 162 | { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 163 | rtc::CritScope cs(&stream_lock_); |
asapersson@webrtc.org | cfd82df | 2015-01-22 09:39:59 +0000 | [diff] [blame] | 164 | if (received_seq_first_ == 0 && |
| 165 | receive_counters_.transmitted.payload_bytes == 0) { |
sprang@webrtc.org | 7dba27c | 2014-01-21 16:33:37 +0000 | [diff] [blame] | 166 | // We have not received anything. |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 167 | return false; |
| 168 | } |
sprang@webrtc.org | 7dba27c | 2014-01-21 16:33:37 +0000 | [diff] [blame] | 169 | |
Taylor Brandstetter | 8491693 | 2018-06-25 15:50:26 -0700 | [diff] [blame^] | 170 | *statistics = CalculateRtcpStatistics(update_fraction_lost); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Taylor Brandstetter | 8491693 | 2018-06-25 15:50:26 -0700 | [diff] [blame^] | 173 | if (update_fraction_lost) { |
| 174 | rtcp_callback_->StatisticsUpdated(*statistics, ssrc_); |
| 175 | } |
sprang@webrtc.org | 7dba27c | 2014-01-21 16:33:37 +0000 | [diff] [blame] | 176 | return true; |
| 177 | } |
| 178 | |
Danil Chapovalov | c5267d2 | 2017-09-18 13:57:19 +0200 | [diff] [blame] | 179 | bool StreamStatisticianImpl::GetActiveStatisticsAndReset( |
| 180 | RtcpStatistics* statistics) { |
| 181 | { |
| 182 | rtc::CritScope cs(&stream_lock_); |
| 183 | if (clock_->CurrentNtpInMilliseconds() - last_receive_time_ntp_.ToMs() >= |
| 184 | kStatisticsTimeoutMs) { |
| 185 | // Not active. |
| 186 | return false; |
| 187 | } |
| 188 | if (received_seq_first_ == 0 && |
| 189 | receive_counters_.transmitted.payload_bytes == 0) { |
| 190 | // We have not received anything. |
| 191 | return false; |
| 192 | } |
| 193 | |
Taylor Brandstetter | 8491693 | 2018-06-25 15:50:26 -0700 | [diff] [blame^] | 194 | *statistics = CalculateRtcpStatistics(/*update_fraction_lost=*/true); |
Danil Chapovalov | c5267d2 | 2017-09-18 13:57:19 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | rtcp_callback_->StatisticsUpdated(*statistics, ssrc_); |
| 198 | return true; |
| 199 | } |
| 200 | |
Taylor Brandstetter | 8491693 | 2018-06-25 15:50:26 -0700 | [diff] [blame^] | 201 | RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics( |
| 202 | bool update_fraction_lost) { |
| 203 | RtcpStatistics statistics; |
sprang@webrtc.org | 7dba27c | 2014-01-21 16:33:37 +0000 | [diff] [blame] | 204 | |
Taylor Brandstetter | 8491693 | 2018-06-25 15:50:26 -0700 | [diff] [blame^] | 205 | uint32_t extended_seq_max = (received_seq_wraps_ << 16) + received_seq_max_; |
| 206 | |
| 207 | if (update_fraction_lost) { |
| 208 | if (last_report_received_packets_ == 0) { |
| 209 | // First time we're calculating fraction lost. |
| 210 | last_report_extended_seq_max_ = received_seq_first_ - 1; |
| 211 | } |
| 212 | |
| 213 | uint32_t exp_since_last = |
| 214 | (extended_seq_max - last_report_extended_seq_max_); |
| 215 | |
| 216 | // Number of received RTP packets since last report; counts all packets |
| 217 | // including retransmissions. |
| 218 | uint32_t rec_since_last = |
| 219 | receive_counters_.transmitted.packets - last_report_received_packets_; |
| 220 | |
| 221 | // Calculate fraction lost according to RFC3550 Appendix A.3. Snap to 0 if |
| 222 | // negative (which is possible with duplicate packets). |
| 223 | uint8_t local_fraction_lost = 0; |
| 224 | if (exp_since_last > rec_since_last) { |
| 225 | // Scale 0 to 255, where 255 is 100% loss. |
| 226 | local_fraction_lost = static_cast<uint8_t>( |
| 227 | 255 * (exp_since_last - rec_since_last) / exp_since_last); |
| 228 | } |
| 229 | |
| 230 | last_fraction_lost_ = local_fraction_lost; |
| 231 | last_report_received_packets_ = receive_counters_.transmitted.packets; |
| 232 | last_report_extended_seq_max_ = extended_seq_max; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Taylor Brandstetter | 8491693 | 2018-06-25 15:50:26 -0700 | [diff] [blame^] | 235 | statistics.fraction_lost = last_fraction_lost_; |
| 236 | // Calculate cumulative loss, according to RFC3550 Appendix A.3. |
| 237 | uint32_t total_expected_packets = extended_seq_max - received_seq_first_ + 1; |
| 238 | statistics.packets_lost = |
| 239 | total_expected_packets - receive_counters_.transmitted.packets; |
| 240 | // Since cumulative loss is carried in a signed 24-bit field in RTCP, we may |
| 241 | // need to clamp it. |
| 242 | statistics.packets_lost = std::min(statistics.packets_lost, 0x7fffff); |
| 243 | statistics.packets_lost = std::max(statistics.packets_lost, -0x800000); |
| 244 | statistics.extended_highest_sequence_number = extended_seq_max; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 245 | // Note: internal jitter value is in Q4 and needs to be scaled by 1/16. |
Taylor Brandstetter | 8491693 | 2018-06-25 15:50:26 -0700 | [diff] [blame^] | 246 | statistics.jitter = jitter_q4_ >> 4; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 247 | |
gaetano.carlucci | 61050f6 | 2016-09-30 06:29:54 -0700 | [diff] [blame] | 248 | BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts", |
gaetano.carlucci | 52a5703 | 2016-09-14 05:04:36 -0700 | [diff] [blame] | 249 | clock_->TimeInMilliseconds(), |
Taylor Brandstetter | 8491693 | 2018-06-25 15:50:26 -0700 | [diff] [blame^] | 250 | statistics.packets_lost, ssrc_); |
gaetano.carlucci | 52a5703 | 2016-09-14 05:04:36 -0700 | [diff] [blame] | 251 | BWE_TEST_LOGGING_PLOT_WITH_SSRC( |
gaetano.carlucci | 61050f6 | 2016-09-30 06:29:54 -0700 | [diff] [blame] | 252 | 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(), |
gaetano.carlucci | 52a5703 | 2016-09-14 05:04:36 -0700 | [diff] [blame] | 253 | (received_seq_max_ - received_seq_first_), ssrc_); |
sprang@webrtc.org | 7dba27c | 2014-01-21 16:33:37 +0000 | [diff] [blame] | 254 | |
Taylor Brandstetter | 8491693 | 2018-06-25 15:50:26 -0700 | [diff] [blame^] | 255 | return statistics; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 258 | void StreamStatisticianImpl::GetDataCounters(size_t* bytes_received, |
| 259 | uint32_t* packets_received) const { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 260 | rtc::CritScope cs(&stream_lock_); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 261 | if (bytes_received) { |
asapersson@webrtc.org | cfd82df | 2015-01-22 09:39:59 +0000 | [diff] [blame] | 262 | *bytes_received = receive_counters_.transmitted.payload_bytes + |
| 263 | receive_counters_.transmitted.header_bytes + |
| 264 | receive_counters_.transmitted.padding_bytes; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 265 | } |
| 266 | if (packets_received) { |
asapersson@webrtc.org | cfd82df | 2015-01-22 09:39:59 +0000 | [diff] [blame] | 267 | *packets_received = receive_counters_.transmitted.packets; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
asapersson@webrtc.org | d952c40 | 2014-11-27 07:38:56 +0000 | [diff] [blame] | 271 | void StreamStatisticianImpl::GetReceiveStreamDataCounters( |
| 272 | StreamDataCounters* data_counters) const { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 273 | rtc::CritScope cs(&stream_lock_); |
asapersson@webrtc.org | d952c40 | 2014-11-27 07:38:56 +0000 | [diff] [blame] | 274 | *data_counters = receive_counters_; |
asapersson@webrtc.org | d952c40 | 2014-11-27 07:38:56 +0000 | [diff] [blame] | 275 | } |
| 276 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 277 | uint32_t StreamStatisticianImpl::BitrateReceived() const { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 278 | rtc::CritScope cs(&stream_lock_); |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 279 | return incoming_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0); |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 280 | } |
| 281 | |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 282 | bool StreamStatisticianImpl::IsRetransmitOfOldPacket( |
Niels Möller | eda0087 | 2018-05-23 13:54:51 +0200 | [diff] [blame] | 283 | const RTPHeader& header) const { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 284 | rtc::CritScope cs(&stream_lock_); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 285 | if (InOrderPacketInternal(header.sequenceNumber)) { |
| 286 | return false; |
| 287 | } |
| 288 | uint32_t frequency_khz = header.payload_type_frequency / 1000; |
| 289 | assert(frequency_khz > 0); |
| 290 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 291 | int64_t time_diff_ms = clock_->TimeInMilliseconds() - last_receive_time_ms_; |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 292 | |
| 293 | // Diff in time stamp since last received in order. |
| 294 | uint32_t timestamp_diff = header.timestamp - last_received_timestamp_; |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 295 | uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz; |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 296 | |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 297 | int64_t max_delay_ms = 0; |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 298 | |
Niels Möller | eda0087 | 2018-05-23 13:54:51 +0200 | [diff] [blame] | 299 | // Jitter standard deviation in samples. |
| 300 | float jitter_std = sqrt(static_cast<float>(jitter_q4_ >> 4)); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 301 | |
Niels Möller | eda0087 | 2018-05-23 13:54:51 +0200 | [diff] [blame] | 302 | // 2 times the standard deviation => 95% confidence. |
| 303 | // And transform to milliseconds by dividing by the frequency in kHz. |
| 304 | max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz); |
| 305 | |
| 306 | // Min max_delay_ms is 1. |
| 307 | if (max_delay_ms == 0) { |
| 308 | max_delay_ms = 1; |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 309 | } |
| 310 | return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms; |
| 311 | } |
| 312 | |
| 313 | bool StreamStatisticianImpl::IsPacketInOrder(uint16_t sequence_number) const { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 314 | rtc::CritScope cs(&stream_lock_); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 315 | return InOrderPacketInternal(sequence_number); |
| 316 | } |
| 317 | |
| 318 | bool StreamStatisticianImpl::InOrderPacketInternal( |
| 319 | uint16_t sequence_number) const { |
| 320 | // First packet is always in order. |
Taylor Brandstetter | 8491693 | 2018-06-25 15:50:26 -0700 | [diff] [blame^] | 321 | if (receive_counters_.transmitted.packets == 0) |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 322 | return true; |
| 323 | |
| 324 | if (IsNewerSequenceNumber(sequence_number, received_seq_max_)) { |
| 325 | return true; |
| 326 | } else { |
| 327 | // If we have a restart of the remote side this packet is still in order. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 328 | return !IsNewerSequenceNumber( |
| 329 | sequence_number, received_seq_max_ - max_reordering_threshold_); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 333 | ReceiveStatistics* ReceiveStatistics::Create(Clock* clock) { |
| 334 | return new ReceiveStatisticsImpl(clock); |
| 335 | } |
| 336 | |
| 337 | ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock) |
| 338 | : clock_(clock), |
Danil Chapovalov | d1996b7 | 2018-01-16 11:07:18 +0100 | [diff] [blame] | 339 | last_returned_ssrc_(0), |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 340 | rtcp_stats_callback_(NULL), |
| 341 | rtp_stats_callback_(NULL) {} |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 342 | |
| 343 | ReceiveStatisticsImpl::~ReceiveStatisticsImpl() { |
| 344 | while (!statisticians_.empty()) { |
| 345 | delete statisticians_.begin()->second; |
| 346 | statisticians_.erase(statisticians_.begin()); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | void ReceiveStatisticsImpl::IncomingPacket(const RTPHeader& header, |
asapersson@webrtc.org | 97d0489 | 2014-12-09 09:47:53 +0000 | [diff] [blame] | 351 | size_t packet_length, |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 352 | bool retransmitted) { |
sprang@webrtc.org | c30e9e2 | 2014-09-08 08:20:18 +0000 | [diff] [blame] | 353 | StreamStatisticianImpl* impl; |
sprang@webrtc.org | 7dba27c | 2014-01-21 16:33:37 +0000 | [diff] [blame] | 354 | { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 355 | rtc::CritScope cs(&receive_statistics_lock_); |
Danil Chapovalov | c5267d2 | 2017-09-18 13:57:19 +0200 | [diff] [blame] | 356 | auto it = statisticians_.find(header.ssrc); |
sprang@webrtc.org | c30e9e2 | 2014-09-08 08:20:18 +0000 | [diff] [blame] | 357 | if (it != statisticians_.end()) { |
| 358 | impl = it->second; |
| 359 | } else { |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 360 | impl = new StreamStatisticianImpl(header.ssrc, clock_, this, this); |
sprang@webrtc.org | c30e9e2 | 2014-09-08 08:20:18 +0000 | [diff] [blame] | 361 | statisticians_[header.ssrc] = impl; |
sprang@webrtc.org | 7dba27c | 2014-01-21 16:33:37 +0000 | [diff] [blame] | 362 | } |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 363 | } |
sprang@webrtc.org | c30e9e2 | 2014-09-08 08:20:18 +0000 | [diff] [blame] | 364 | // StreamStatisticianImpl instance is created once and only destroyed when |
| 365 | // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has |
| 366 | // it's own locking so don't hold receive_statistics_lock_ (potential |
| 367 | // deadlock). |
asapersson@webrtc.org | 97d0489 | 2014-12-09 09:47:53 +0000 | [diff] [blame] | 368 | impl->IncomingPacket(header, packet_length, retransmitted); |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 369 | } |
| 370 | |
asapersson@webrtc.org | 273fbbb | 2015-01-27 12:17:29 +0000 | [diff] [blame] | 371 | void ReceiveStatisticsImpl::FecPacketReceived(const RTPHeader& header, |
| 372 | size_t packet_length) { |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 373 | StreamStatisticianImpl* impl; |
| 374 | { |
| 375 | rtc::CritScope cs(&receive_statistics_lock_); |
Danil Chapovalov | c5267d2 | 2017-09-18 13:57:19 +0200 | [diff] [blame] | 376 | auto it = statisticians_.find(header.ssrc); |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 377 | // Ignore FEC if it is the first packet. |
| 378 | if (it == statisticians_.end()) |
| 379 | return; |
| 380 | impl = it->second; |
sprang@webrtc.org | c30e9e2 | 2014-09-08 08:20:18 +0000 | [diff] [blame] | 381 | } |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 382 | impl->FecPacketReceived(header, packet_length); |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 383 | } |
| 384 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 385 | StreamStatistician* ReceiveStatisticsImpl::GetStatistician( |
| 386 | uint32_t ssrc) const { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 387 | rtc::CritScope cs(&receive_statistics_lock_); |
Danil Chapovalov | c5267d2 | 2017-09-18 13:57:19 +0200 | [diff] [blame] | 388 | auto it = statisticians_.find(ssrc); |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 389 | if (it == statisticians_.end()) |
| 390 | return NULL; |
| 391 | return it->second; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 392 | } |
| 393 | |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 394 | void ReceiveStatisticsImpl::SetMaxReorderingThreshold( |
| 395 | int max_reordering_threshold) { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 396 | rtc::CritScope cs(&receive_statistics_lock_); |
Danil Chapovalov | c5267d2 | 2017-09-18 13:57:19 +0200 | [diff] [blame] | 397 | for (auto& statistician : statisticians_) { |
| 398 | statistician.second->SetMaxReorderingThreshold(max_reordering_threshold); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
sprang@webrtc.org | 54ae4ff | 2013-12-19 13:26:02 +0000 | [diff] [blame] | 402 | void ReceiveStatisticsImpl::RegisterRtcpStatisticsCallback( |
| 403 | RtcpStatisticsCallback* callback) { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 404 | rtc::CritScope cs(&receive_statistics_lock_); |
sprang@webrtc.org | 54ae4ff | 2013-12-19 13:26:02 +0000 | [diff] [blame] | 405 | if (callback != NULL) |
| 406 | assert(rtcp_stats_callback_ == NULL); |
| 407 | rtcp_stats_callback_ = callback; |
| 408 | } |
| 409 | |
| 410 | void ReceiveStatisticsImpl::StatisticsUpdated(const RtcpStatistics& statistics, |
| 411 | uint32_t ssrc) { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 412 | rtc::CritScope cs(&receive_statistics_lock_); |
pbos@webrtc.org | ce4e9a3 | 2014-12-18 13:50:16 +0000 | [diff] [blame] | 413 | if (rtcp_stats_callback_) |
sprang@webrtc.org | 54ae4ff | 2013-12-19 13:26:02 +0000 | [diff] [blame] | 414 | rtcp_stats_callback_->StatisticsUpdated(statistics, ssrc); |
pbos@webrtc.org | ce4e9a3 | 2014-12-18 13:50:16 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | void ReceiveStatisticsImpl::CNameChanged(const char* cname, uint32_t ssrc) { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 418 | rtc::CritScope cs(&receive_statistics_lock_); |
pbos@webrtc.org | ce4e9a3 | 2014-12-18 13:50:16 +0000 | [diff] [blame] | 419 | if (rtcp_stats_callback_) |
| 420 | rtcp_stats_callback_->CNameChanged(cname, ssrc); |
sprang@webrtc.org | 54ae4ff | 2013-12-19 13:26:02 +0000 | [diff] [blame] | 421 | } |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 422 | |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 423 | void ReceiveStatisticsImpl::RegisterRtpStatisticsCallback( |
| 424 | StreamDataCountersCallback* callback) { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 425 | rtc::CritScope cs(&receive_statistics_lock_); |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 426 | if (callback != NULL) |
| 427 | assert(rtp_stats_callback_ == NULL); |
| 428 | rtp_stats_callback_ = callback; |
| 429 | } |
| 430 | |
| 431 | void ReceiveStatisticsImpl::DataCountersUpdated(const StreamDataCounters& stats, |
| 432 | uint32_t ssrc) { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 433 | rtc::CritScope cs(&receive_statistics_lock_); |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 434 | if (rtp_stats_callback_) { |
| 435 | rtp_stats_callback_->DataCountersUpdated(stats, ssrc); |
| 436 | } |
| 437 | } |
| 438 | |
danilchap | 0bc8423 | 2017-08-11 08:12:54 -0700 | [diff] [blame] | 439 | std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks( |
danilchap | f5f793c | 2017-07-27 04:44:18 -0700 | [diff] [blame] | 440 | size_t max_blocks) { |
Danil Chapovalov | c5267d2 | 2017-09-18 13:57:19 +0200 | [diff] [blame] | 441 | std::map<uint32_t, StreamStatisticianImpl*> statisticians; |
| 442 | { |
| 443 | rtc::CritScope cs(&receive_statistics_lock_); |
| 444 | statisticians = statisticians_; |
| 445 | } |
danilchap | f5f793c | 2017-07-27 04:44:18 -0700 | [diff] [blame] | 446 | std::vector<rtcp::ReportBlock> result; |
| 447 | result.reserve(std::min(max_blocks, statisticians.size())); |
Danil Chapovalov | d1996b7 | 2018-01-16 11:07:18 +0100 | [diff] [blame] | 448 | auto add_report_block = [&result](uint32_t media_ssrc, |
| 449 | StreamStatisticianImpl* statistician) { |
danilchap | f5f793c | 2017-07-27 04:44:18 -0700 | [diff] [blame] | 450 | // Do we have receive statistics to send? |
| 451 | RtcpStatistics stats; |
Danil Chapovalov | d1996b7 | 2018-01-16 11:07:18 +0100 | [diff] [blame] | 452 | if (!statistician->GetActiveStatisticsAndReset(&stats)) |
| 453 | return; |
danilchap | f5f793c | 2017-07-27 04:44:18 -0700 | [diff] [blame] | 454 | result.emplace_back(); |
| 455 | rtcp::ReportBlock& block = result.back(); |
Danil Chapovalov | d1996b7 | 2018-01-16 11:07:18 +0100 | [diff] [blame] | 456 | block.SetMediaSsrc(media_ssrc); |
danilchap | f5f793c | 2017-07-27 04:44:18 -0700 | [diff] [blame] | 457 | block.SetFractionLost(stats.fraction_lost); |
srte | 186d9c3 | 2017-08-04 05:03:53 -0700 | [diff] [blame] | 458 | if (!block.SetCumulativeLost(stats.packets_lost)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 459 | RTC_LOG(LS_WARNING) << "Cumulative lost is oversized."; |
danilchap | f5f793c | 2017-07-27 04:44:18 -0700 | [diff] [blame] | 460 | result.pop_back(); |
Danil Chapovalov | d1996b7 | 2018-01-16 11:07:18 +0100 | [diff] [blame] | 461 | return; |
danilchap | f5f793c | 2017-07-27 04:44:18 -0700 | [diff] [blame] | 462 | } |
srte | 186d9c3 | 2017-08-04 05:03:53 -0700 | [diff] [blame] | 463 | block.SetExtHighestSeqNum(stats.extended_highest_sequence_number); |
danilchap | f5f793c | 2017-07-27 04:44:18 -0700 | [diff] [blame] | 464 | block.SetJitter(stats.jitter); |
Danil Chapovalov | d1996b7 | 2018-01-16 11:07:18 +0100 | [diff] [blame] | 465 | }; |
| 466 | |
| 467 | const auto start_it = statisticians.upper_bound(last_returned_ssrc_); |
| 468 | for (auto it = start_it; |
| 469 | result.size() < max_blocks && it != statisticians.end(); ++it) |
| 470 | add_report_block(it->first, it->second); |
| 471 | for (auto it = statisticians.begin(); |
| 472 | result.size() < max_blocks && it != start_it; ++it) |
| 473 | add_report_block(it->first, it->second); |
| 474 | |
| 475 | if (!result.empty()) |
| 476 | last_returned_ssrc_ = result.back().source_ssrc(); |
danilchap | f5f793c | 2017-07-27 04:44:18 -0700 | [diff] [blame] | 477 | return result; |
| 478 | } |
| 479 | |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 480 | } // namespace webrtc |