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 | |
Oleh Prypin | 1992958 | 2019-04-23 08:50:04 +0200 | [diff] [blame] | 13 | #include <cmath> |
kwiberg | fd8be34 | 2016-05-14 19:44:11 -0700 | [diff] [blame] | 14 | #include <cstdlib> |
Danil Chapovalov | 8ce0d2b | 2018-11-23 11:03:25 +0100 | [diff] [blame] | 15 | #include <memory> |
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 | |
Danil Chapovalov | 8ce0d2b | 2018-11-23 11:03:25 +0100 | [diff] [blame] | 18 | #include "absl/memory/memory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "modules/remote_bitrate_estimator/test/bwe_test_logging.h" |
Niels Möller | 1f3206c | 2018-09-14 08:26:32 +0200 | [diff] [blame] | 20 | #include "modules/rtp_rtcp/source/rtp_packet_received.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "modules/rtp_rtcp/source/rtp_rtcp_config.h" |
| 22 | #include "modules/rtp_rtcp/source/time_util.h" |
| 23 | #include "rtc_base/logging.h" |
| 24 | #include "system_wrappers/include/clock.h" |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 28 | const int64_t kStatisticsTimeoutMs = 8000; |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 29 | const int64_t kStatisticsProcessIntervalMs = 1000; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 30 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 31 | StreamStatistician::~StreamStatistician() {} |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 32 | |
Niels Möller | d781965 | 2019-08-13 14:43:02 +0200 | [diff] [blame^] | 33 | StreamStatisticianImpl::StreamStatisticianImpl(uint32_t ssrc, |
| 34 | Clock* clock, |
| 35 | int max_reordering_threshold) |
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), |
Danil Chapovalov | ebb50c2 | 2018-11-22 14:04:02 +0100 | [diff] [blame] | 40 | max_reordering_threshold_(max_reordering_threshold), |
Niels Möller | 87da109 | 2019-05-24 14:04:28 +0200 | [diff] [blame] | 41 | enable_retransmit_detection_(false), |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 42 | jitter_q4_(0), |
Qingsi Wang | 2370b08 | 2018-08-21 14:24:26 -0700 | [diff] [blame] | 43 | cumulative_loss_(0), |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 44 | last_receive_time_ms_(0), |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 45 | last_received_timestamp_(0), |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 46 | received_seq_first_(0), |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 +0000 | [diff] [blame] | 47 | received_seq_max_(-1), |
Qingsi Wang | 2370b08 | 2018-08-21 14:24:26 -0700 | [diff] [blame] | 48 | last_report_inorder_packets_(0), |
| 49 | last_report_old_packets_(0), |
Niels Möller | d781965 | 2019-08-13 14:43:02 +0200 | [diff] [blame^] | 50 | last_report_seq_max_(-1) {} |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 51 | |
Danil Chapovalov | 2a5ce2b | 2018-02-07 09:38:31 +0100 | [diff] [blame] | 52 | StreamStatisticianImpl::~StreamStatisticianImpl() = default; |
| 53 | |
Niels Möller | dbb988b | 2018-11-15 08:05:16 +0100 | [diff] [blame] | 54 | void StreamStatisticianImpl::OnRtpPacket(const RtpPacketReceived& packet) { |
Niels Möller | d781965 | 2019-08-13 14:43:02 +0200 | [diff] [blame^] | 55 | UpdateCounters(packet); |
sprang@webrtc.org | a45cac0 | 2014-01-27 16:22:08 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 +0000 | [diff] [blame] | 58 | bool StreamStatisticianImpl::UpdateOutOfOrder(const RtpPacketReceived& packet, |
| 59 | int64_t sequence_number, |
| 60 | int64_t now_ms) { |
| 61 | RTC_DCHECK_EQ(sequence_number, |
| 62 | seq_unwrapper_.UnwrapWithoutUpdate(packet.SequenceNumber())); |
| 63 | |
| 64 | // Check if |packet| is second packet of a stream restart. |
| 65 | if (received_seq_out_of_order_) { |
| 66 | uint16_t expected_sequence_number = *received_seq_out_of_order_ + 1; |
| 67 | received_seq_out_of_order_ = absl::nullopt; |
| 68 | if (packet.SequenceNumber() == expected_sequence_number) { |
| 69 | // Ignore sequence number gap caused by stream restart for next packet |
| 70 | // loss calculation. |
| 71 | last_report_seq_max_ = sequence_number; |
| 72 | last_report_inorder_packets_ = receive_counters_.transmitted.packets - |
| 73 | receive_counters_.retransmitted.packets; |
| 74 | // As final part of stream restart consider |packet| is not out of order. |
| 75 | return false; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (std::abs(sequence_number - received_seq_max_) > |
| 80 | max_reordering_threshold_) { |
| 81 | // Sequence number gap looks too large, wait until next packet to check |
| 82 | // for a stream restart. |
| 83 | received_seq_out_of_order_ = packet.SequenceNumber(); |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | if (sequence_number > received_seq_max_) |
| 88 | return false; |
| 89 | |
| 90 | // Old out of order packet, may be retransmit. |
| 91 | if (enable_retransmit_detection_ && IsRetransmitOfOldPacket(packet, now_ms)) |
| 92 | receive_counters_.retransmitted.AddPacket(packet); |
| 93 | return true; |
| 94 | } |
| 95 | |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 96 | StreamDataCounters StreamStatisticianImpl::UpdateCounters( |
Danil Chapovalov | 44727b4 | 2018-11-22 11:28:45 +0100 | [diff] [blame] | 97 | const RtpPacketReceived& packet) { |
| 98 | rtc::CritScope cs(&stream_lock_); |
Niels Möller | dbb988b | 2018-11-15 08:05:16 +0100 | [diff] [blame] | 99 | RTC_DCHECK_EQ(ssrc_, packet.Ssrc()); |
Danil Chapovalov | 44727b4 | 2018-11-22 11:28:45 +0100 | [diff] [blame] | 100 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 101 | |
| 102 | incoming_bitrate_.Update(packet.size(), now_ms); |
Henrik Boström | cb755b0 | 2019-04-02 15:11:48 +0200 | [diff] [blame] | 103 | receive_counters_.last_packet_received_timestamp_ms = now_ms; |
Niels Möller | dbb988b | 2018-11-15 08:05:16 +0100 | [diff] [blame] | 104 | receive_counters_.transmitted.AddPacket(packet); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 105 | |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 +0000 | [diff] [blame] | 106 | int64_t sequence_number = |
| 107 | seq_unwrapper_.UnwrapWithoutUpdate(packet.SequenceNumber()); |
| 108 | if (!ReceivedRtpPacket()) { |
| 109 | received_seq_first_ = sequence_number; |
| 110 | last_report_seq_max_ = sequence_number - 1; |
Danil Chapovalov | 44727b4 | 2018-11-22 11:28:45 +0100 | [diff] [blame] | 111 | receive_counters_.first_packet_time_ms = now_ms; |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 +0000 | [diff] [blame] | 112 | } else if (UpdateOutOfOrder(packet, sequence_number, now_ms)) { |
| 113 | return receive_counters_; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 114 | } |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 +0000 | [diff] [blame] | 115 | // In order packet. |
| 116 | received_seq_max_ = sequence_number; |
| 117 | seq_unwrapper_.UpdateLast(sequence_number); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 118 | |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 +0000 | [diff] [blame] | 119 | // If new time stamp and more than one in-order packet received, calculate |
| 120 | // new jitter statistics. |
| 121 | if (packet.Timestamp() != last_received_timestamp_ && |
| 122 | (receive_counters_.transmitted.packets - |
| 123 | receive_counters_.retransmitted.packets) > 1) { |
| 124 | UpdateJitter(packet, now_ms); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 125 | } |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 +0000 | [diff] [blame] | 126 | last_received_timestamp_ = packet.Timestamp(); |
| 127 | last_receive_time_ms_ = now_ms; |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 128 | return receive_counters_; |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Niels Möller | dbb988b | 2018-11-15 08:05:16 +0100 | [diff] [blame] | 131 | void StreamStatisticianImpl::UpdateJitter(const RtpPacketReceived& packet, |
Danil Chapovalov | 856cf22 | 2018-11-26 10:20:01 +0100 | [diff] [blame] | 132 | int64_t receive_time_ms) { |
| 133 | int64_t receive_diff_ms = receive_time_ms - last_receive_time_ms_; |
| 134 | RTC_DCHECK_GE(receive_diff_ms, 0); |
| 135 | uint32_t receive_diff_rtp = static_cast<uint32_t>( |
| 136 | (receive_diff_ms * packet.payload_type_frequency()) / 1000); |
| 137 | int32_t time_diff_samples = |
| 138 | receive_diff_rtp - (packet.Timestamp() - last_received_timestamp_); |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 139 | |
kwiberg | fd8be34 | 2016-05-14 19:44:11 -0700 | [diff] [blame] | 140 | time_diff_samples = std::abs(time_diff_samples); |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 141 | |
| 142 | // lib_jingle sometimes deliver crazy jumps in TS for the same stream. |
| 143 | // If this happens, don't update jitter value. Use 5 secs video frequency |
| 144 | // as the threshold. |
| 145 | if (time_diff_samples < 450000) { |
| 146 | // Note we calculate in Q4 to avoid using float. |
| 147 | int32_t jitter_diff_q4 = (time_diff_samples << 4) - jitter_q4_; |
| 148 | jitter_q4_ += ((jitter_diff_q4 + 8) >> 4); |
| 149 | } |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Niels Möller | dbb988b | 2018-11-15 08:05:16 +0100 | [diff] [blame] | 152 | void StreamStatisticianImpl::FecPacketReceived( |
| 153 | const RtpPacketReceived& packet) { |
Niels Möller | d781965 | 2019-08-13 14:43:02 +0200 | [diff] [blame^] | 154 | rtc::CritScope cs(&stream_lock_); |
| 155 | receive_counters_.fec.AddPacket(packet); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 156 | } |
| 157 | |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 158 | void StreamStatisticianImpl::SetMaxReorderingThreshold( |
| 159 | int max_reordering_threshold) { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 160 | rtc::CritScope cs(&stream_lock_); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 161 | max_reordering_threshold_ = max_reordering_threshold; |
| 162 | } |
| 163 | |
Niels Möller | 5304a32 | 2018-08-27 13:27:05 +0200 | [diff] [blame] | 164 | void StreamStatisticianImpl::EnableRetransmitDetection(bool enable) { |
| 165 | rtc::CritScope cs(&stream_lock_); |
| 166 | enable_retransmit_detection_ = enable; |
| 167 | } |
| 168 | |
sprang@webrtc.org | 54ae4ff | 2013-12-19 13:26:02 +0000 | [diff] [blame] | 169 | bool StreamStatisticianImpl::GetStatistics(RtcpStatistics* statistics, |
Qingsi Wang | 2370b08 | 2018-08-21 14:24:26 -0700 | [diff] [blame] | 170 | bool reset) { |
Niels Möller | 12ebfa6 | 2019-08-06 16:04:12 +0200 | [diff] [blame] | 171 | rtc::CritScope cs(&stream_lock_); |
| 172 | if (!ReceivedRtpPacket()) { |
| 173 | return false; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Niels Möller | 12ebfa6 | 2019-08-06 16:04:12 +0200 | [diff] [blame] | 176 | if (!reset) { |
| 177 | if (last_report_inorder_packets_ == 0) { |
| 178 | // No report. |
| 179 | return false; |
| 180 | } |
| 181 | // Just get last report. |
| 182 | *statistics = last_reported_statistics_; |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | *statistics = CalculateRtcpStatistics(); |
| 187 | |
sprang@webrtc.org | 7dba27c | 2014-01-21 16:33:37 +0000 | [diff] [blame] | 188 | return true; |
| 189 | } |
| 190 | |
Danil Chapovalov | c5267d2 | 2017-09-18 13:57:19 +0200 | [diff] [blame] | 191 | bool StreamStatisticianImpl::GetActiveStatisticsAndReset( |
| 192 | RtcpStatistics* statistics) { |
Niels Möller | 12ebfa6 | 2019-08-06 16:04:12 +0200 | [diff] [blame] | 193 | rtc::CritScope cs(&stream_lock_); |
| 194 | if (clock_->TimeInMilliseconds() - last_receive_time_ms_ >= |
| 195 | kStatisticsTimeoutMs) { |
| 196 | // Not active. |
| 197 | return false; |
| 198 | } |
| 199 | if (!ReceivedRtpPacket()) { |
| 200 | return false; |
Danil Chapovalov | c5267d2 | 2017-09-18 13:57:19 +0200 | [diff] [blame] | 201 | } |
| 202 | |
Niels Möller | 12ebfa6 | 2019-08-06 16:04:12 +0200 | [diff] [blame] | 203 | *statistics = CalculateRtcpStatistics(); |
| 204 | |
Danil Chapovalov | c5267d2 | 2017-09-18 13:57:19 +0200 | [diff] [blame] | 205 | return true; |
| 206 | } |
| 207 | |
Qingsi Wang | 2370b08 | 2018-08-21 14:24:26 -0700 | [diff] [blame] | 208 | RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics() { |
| 209 | RtcpStatistics stats; |
Qingsi Wang | 2370b08 | 2018-08-21 14:24:26 -0700 | [diff] [blame] | 210 | // Calculate fraction lost. |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 +0000 | [diff] [blame] | 211 | int64_t exp_since_last = received_seq_max_ - last_report_seq_max_; |
| 212 | RTC_DCHECK_GE(exp_since_last, 0); |
Qingsi Wang | 2370b08 | 2018-08-21 14:24:26 -0700 | [diff] [blame] | 213 | |
| 214 | // Number of received RTP packets since last report, counts all packets but |
| 215 | // not re-transmissions. |
| 216 | uint32_t rec_since_last = (receive_counters_.transmitted.packets - |
| 217 | receive_counters_.retransmitted.packets) - |
| 218 | last_report_inorder_packets_; |
| 219 | |
| 220 | // With NACK we don't know the expected retransmissions during the last |
| 221 | // second. We know how many "old" packets we have received. We just count |
| 222 | // the number of old received to estimate the loss, but it still does not |
| 223 | // guarantee an exact number since we run this based on time triggered by |
| 224 | // sending of an RTP packet. This should have a minimum effect. |
| 225 | |
| 226 | // With NACK we don't count old packets as received since they are |
| 227 | // re-transmitted. We use RTT to decide if a packet is re-ordered or |
| 228 | // re-transmitted. |
| 229 | uint32_t retransmitted_packets = |
| 230 | receive_counters_.retransmitted.packets - last_report_old_packets_; |
| 231 | rec_since_last += retransmitted_packets; |
| 232 | |
| 233 | int32_t missing = 0; |
| 234 | if (exp_since_last > rec_since_last) { |
| 235 | missing = (exp_since_last - rec_since_last); |
| 236 | } |
| 237 | uint8_t local_fraction_lost = 0; |
| 238 | if (exp_since_last) { |
| 239 | // Scale 0 to 255, where 255 is 100% loss. |
| 240 | local_fraction_lost = static_cast<uint8_t>(255 * missing / exp_since_last); |
| 241 | } |
| 242 | stats.fraction_lost = local_fraction_lost; |
| 243 | |
| 244 | // We need a counter for cumulative loss too. |
| 245 | // TODO(danilchap): Ensure cumulative loss is below maximum value of 2^24. |
| 246 | cumulative_loss_ += missing; |
| 247 | stats.packets_lost = cumulative_loss_; |
| 248 | stats.extended_highest_sequence_number = |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 +0000 | [diff] [blame] | 249 | static_cast<uint32_t>(received_seq_max_); |
Qingsi Wang | 2370b08 | 2018-08-21 14:24:26 -0700 | [diff] [blame] | 250 | // Note: internal jitter value is in Q4 and needs to be scaled by 1/16. |
| 251 | stats.jitter = jitter_q4_ >> 4; |
| 252 | |
| 253 | // Store this report. |
| 254 | last_reported_statistics_ = stats; |
| 255 | |
| 256 | // Only for report blocks in RTCP SR and RR. |
| 257 | last_report_inorder_packets_ = receive_counters_.transmitted.packets - |
| 258 | receive_counters_.retransmitted.packets; |
| 259 | last_report_old_packets_ = receive_counters_.retransmitted.packets; |
| 260 | last_report_seq_max_ = received_seq_max_; |
gaetano.carlucci | 61050f6 | 2016-09-30 06:29:54 -0700 | [diff] [blame] | 261 | BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts", |
gaetano.carlucci | 52a5703 | 2016-09-14 05:04:36 -0700 | [diff] [blame] | 262 | clock_->TimeInMilliseconds(), |
Qingsi Wang | 2370b08 | 2018-08-21 14:24:26 -0700 | [diff] [blame] | 263 | cumulative_loss_, ssrc_); |
gaetano.carlucci | 52a5703 | 2016-09-14 05:04:36 -0700 | [diff] [blame] | 264 | BWE_TEST_LOGGING_PLOT_WITH_SSRC( |
gaetano.carlucci | 61050f6 | 2016-09-30 06:29:54 -0700 | [diff] [blame] | 265 | 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(), |
gaetano.carlucci | 52a5703 | 2016-09-14 05:04:36 -0700 | [diff] [blame] | 266 | (received_seq_max_ - received_seq_first_), ssrc_); |
sprang@webrtc.org | 7dba27c | 2014-01-21 16:33:37 +0000 | [diff] [blame] | 267 | |
Qingsi Wang | 2370b08 | 2018-08-21 14:24:26 -0700 | [diff] [blame] | 268 | return stats; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Niels Möller | 9a9f18a | 2019-08-02 13:52:37 +0200 | [diff] [blame] | 271 | absl::optional<int> StreamStatisticianImpl::GetFractionLostInPercent() const { |
| 272 | rtc::CritScope cs(&stream_lock_); |
| 273 | if (received_seq_max_ < 0) { |
| 274 | return absl::nullopt; |
| 275 | } |
| 276 | int64_t expected_packets = 1 + received_seq_max_ - received_seq_first_; |
| 277 | if (expected_packets <= 0) { |
| 278 | return absl::nullopt; |
| 279 | } |
| 280 | // Spec allows negative cumulative loss, but implementation uses uint32_t, so |
| 281 | // this expression is always non-negative. |
| 282 | return 100 * static_cast<int64_t>(cumulative_loss_) / expected_packets; |
| 283 | } |
| 284 | |
Niels Möller | 58b496b | 2019-08-12 12:16:31 +0200 | [diff] [blame] | 285 | StreamDataCounters StreamStatisticianImpl::GetReceiveStreamDataCounters() |
| 286 | const { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 287 | rtc::CritScope cs(&stream_lock_); |
Niels Möller | 58b496b | 2019-08-12 12:16:31 +0200 | [diff] [blame] | 288 | return receive_counters_; |
asapersson@webrtc.org | d952c40 | 2014-11-27 07:38:56 +0000 | [diff] [blame] | 289 | } |
| 290 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 291 | uint32_t StreamStatisticianImpl::BitrateReceived() const { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 292 | rtc::CritScope cs(&stream_lock_); |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 293 | return incoming_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0); |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 294 | } |
| 295 | |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 296 | bool StreamStatisticianImpl::IsRetransmitOfOldPacket( |
Danil Chapovalov | 44727b4 | 2018-11-22 11:28:45 +0100 | [diff] [blame] | 297 | const RtpPacketReceived& packet, |
| 298 | int64_t now_ms) const { |
Niels Möller | dbb988b | 2018-11-15 08:05:16 +0100 | [diff] [blame] | 299 | uint32_t frequency_khz = packet.payload_type_frequency() / 1000; |
Danil Chapovalov | 44727b4 | 2018-11-22 11:28:45 +0100 | [diff] [blame] | 300 | RTC_DCHECK_GT(frequency_khz, 0); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 301 | |
Danil Chapovalov | 44727b4 | 2018-11-22 11:28:45 +0100 | [diff] [blame] | 302 | int64_t time_diff_ms = now_ms - last_receive_time_ms_; |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 303 | |
| 304 | // Diff in time stamp since last received in order. |
Niels Möller | dbb988b | 2018-11-15 08:05:16 +0100 | [diff] [blame] | 305 | uint32_t timestamp_diff = packet.Timestamp() - last_received_timestamp_; |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 306 | uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz; |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 307 | |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 308 | int64_t max_delay_ms = 0; |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 309 | |
Niels Möller | eda0087 | 2018-05-23 13:54:51 +0200 | [diff] [blame] | 310 | // Jitter standard deviation in samples. |
Oleh Prypin | 1992958 | 2019-04-23 08:50:04 +0200 | [diff] [blame] | 311 | float jitter_std = std::sqrt(static_cast<float>(jitter_q4_ >> 4)); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 312 | |
Niels Möller | eda0087 | 2018-05-23 13:54:51 +0200 | [diff] [blame] | 313 | // 2 times the standard deviation => 95% confidence. |
| 314 | // And transform to milliseconds by dividing by the frequency in kHz. |
| 315 | max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz); |
| 316 | |
| 317 | // Min max_delay_ms is 1. |
| 318 | if (max_delay_ms == 0) { |
| 319 | max_delay_ms = 1; |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 320 | } |
| 321 | return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms; |
| 322 | } |
| 323 | |
Niels Möller | d781965 | 2019-08-13 14:43:02 +0200 | [diff] [blame^] | 324 | std::unique_ptr<ReceiveStatistics> ReceiveStatistics::Create(Clock* clock) { |
| 325 | return absl::make_unique<ReceiveStatisticsImpl>(clock); |
| 326 | } |
| 327 | |
Danil Chapovalov | 8ce0d2b | 2018-11-23 11:03:25 +0100 | [diff] [blame] | 328 | std::unique_ptr<ReceiveStatistics> ReceiveStatistics::Create( |
| 329 | Clock* clock, |
Niels Möller | 12ebfa6 | 2019-08-06 16:04:12 +0200 | [diff] [blame] | 330 | StreamDataCountersCallback* rtp_callback) { |
Niels Möller | d781965 | 2019-08-13 14:43:02 +0200 | [diff] [blame^] | 331 | RTC_CHECK(rtp_callback == nullptr); |
| 332 | return Create(clock); |
Niels Möller | 12ebfa6 | 2019-08-06 16:04:12 +0200 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | std::unique_ptr<ReceiveStatistics> ReceiveStatistics::Create( |
| 336 | Clock* clock, |
Danil Chapovalov | 8ce0d2b | 2018-11-23 11:03:25 +0100 | [diff] [blame] | 337 | RtcpStatisticsCallback* rtcp_callback, |
| 338 | StreamDataCountersCallback* rtp_callback) { |
Niels Möller | 12ebfa6 | 2019-08-06 16:04:12 +0200 | [diff] [blame] | 339 | RTC_CHECK(rtcp_callback == nullptr); |
Niels Möller | d781965 | 2019-08-13 14:43:02 +0200 | [diff] [blame^] | 340 | RTC_CHECK(rtp_callback == nullptr); |
| 341 | return Create(clock); |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Niels Möller | d781965 | 2019-08-13 14:43:02 +0200 | [diff] [blame^] | 344 | ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock) |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 345 | : clock_(clock), |
Danil Chapovalov | d1996b7 | 2018-01-16 11:07:18 +0100 | [diff] [blame] | 346 | last_returned_ssrc_(0), |
Niels Möller | d781965 | 2019-08-13 14:43:02 +0200 | [diff] [blame^] | 347 | max_reordering_threshold_(kDefaultMaxReorderingThreshold) {} |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 348 | |
| 349 | ReceiveStatisticsImpl::~ReceiveStatisticsImpl() { |
| 350 | while (!statisticians_.empty()) { |
| 351 | delete statisticians_.begin()->second; |
| 352 | statisticians_.erase(statisticians_.begin()); |
| 353 | } |
| 354 | } |
| 355 | |
Niels Möller | 1f3206c | 2018-09-14 08:26:32 +0200 | [diff] [blame] | 356 | void ReceiveStatisticsImpl::OnRtpPacket(const RtpPacketReceived& packet) { |
sprang@webrtc.org | c30e9e2 | 2014-09-08 08:20:18 +0000 | [diff] [blame] | 357 | // StreamStatisticianImpl instance is created once and only destroyed when |
| 358 | // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has |
| 359 | // it's own locking so don't hold receive_statistics_lock_ (potential |
| 360 | // deadlock). |
Niels Möller | 87da109 | 2019-05-24 14:04:28 +0200 | [diff] [blame] | 361 | GetOrCreateStatistician(packet.Ssrc())->OnRtpPacket(packet); |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Niels Möller | 1f3206c | 2018-09-14 08:26:32 +0200 | [diff] [blame] | 364 | void ReceiveStatisticsImpl::FecPacketReceived(const RtpPacketReceived& packet) { |
Niels Möller | 87da109 | 2019-05-24 14:04:28 +0200 | [diff] [blame] | 365 | StreamStatisticianImpl* impl = GetStatistician(packet.Ssrc()); |
| 366 | // Ignore FEC if it is the first packet. |
| 367 | if (impl) { |
| 368 | impl->FecPacketReceived(packet); |
sprang@webrtc.org | c30e9e2 | 2014-09-08 08:20:18 +0000 | [diff] [blame] | 369 | } |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Niels Möller | 87da109 | 2019-05-24 14:04:28 +0200 | [diff] [blame] | 372 | StreamStatisticianImpl* ReceiveStatisticsImpl::GetStatistician( |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 373 | uint32_t ssrc) const { |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 374 | rtc::CritScope cs(&receive_statistics_lock_); |
Niels Möller | 87da109 | 2019-05-24 14:04:28 +0200 | [diff] [blame] | 375 | const auto& it = statisticians_.find(ssrc); |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 376 | if (it == statisticians_.end()) |
| 377 | return NULL; |
| 378 | return it->second; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Niels Möller | 87da109 | 2019-05-24 14:04:28 +0200 | [diff] [blame] | 381 | StreamStatisticianImpl* ReceiveStatisticsImpl::GetOrCreateStatistician( |
| 382 | uint32_t ssrc) { |
| 383 | rtc::CritScope cs(&receive_statistics_lock_); |
| 384 | StreamStatisticianImpl*& impl = statisticians_[ssrc]; |
| 385 | if (impl == nullptr) { // new element |
Niels Möller | d781965 | 2019-08-13 14:43:02 +0200 | [diff] [blame^] | 386 | impl = new StreamStatisticianImpl(ssrc, clock_, max_reordering_threshold_); |
Niels Möller | 87da109 | 2019-05-24 14:04:28 +0200 | [diff] [blame] | 387 | } |
| 388 | return impl; |
| 389 | } |
| 390 | |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 391 | void ReceiveStatisticsImpl::SetMaxReorderingThreshold( |
| 392 | int max_reordering_threshold) { |
Danil Chapovalov | ebb50c2 | 2018-11-22 14:04:02 +0100 | [diff] [blame] | 393 | std::map<uint32_t, StreamStatisticianImpl*> statisticians; |
| 394 | { |
| 395 | rtc::CritScope cs(&receive_statistics_lock_); |
| 396 | max_reordering_threshold_ = max_reordering_threshold; |
| 397 | statisticians = statisticians_; |
| 398 | } |
| 399 | for (auto& statistician : statisticians) { |
Danil Chapovalov | c5267d2 | 2017-09-18 13:57:19 +0200 | [diff] [blame] | 400 | statistician.second->SetMaxReorderingThreshold(max_reordering_threshold); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
Niels Möller | 87da109 | 2019-05-24 14:04:28 +0200 | [diff] [blame] | 404 | void ReceiveStatisticsImpl::SetMaxReorderingThreshold( |
| 405 | uint32_t ssrc, |
| 406 | int max_reordering_threshold) { |
| 407 | GetOrCreateStatistician(ssrc)->SetMaxReorderingThreshold( |
| 408 | max_reordering_threshold); |
| 409 | } |
| 410 | |
Niels Möller | 5304a32 | 2018-08-27 13:27:05 +0200 | [diff] [blame] | 411 | void ReceiveStatisticsImpl::EnableRetransmitDetection(uint32_t ssrc, |
| 412 | bool enable) { |
Niels Möller | 87da109 | 2019-05-24 14:04:28 +0200 | [diff] [blame] | 413 | GetOrCreateStatistician(ssrc)->EnableRetransmitDetection(enable); |
Niels Möller | 5304a32 | 2018-08-27 13:27:05 +0200 | [diff] [blame] | 414 | } |
| 415 | |
danilchap | 0bc8423 | 2017-08-11 08:12:54 -0700 | [diff] [blame] | 416 | std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks( |
danilchap | f5f793c | 2017-07-27 04:44:18 -0700 | [diff] [blame] | 417 | size_t max_blocks) { |
Danil Chapovalov | c5267d2 | 2017-09-18 13:57:19 +0200 | [diff] [blame] | 418 | std::map<uint32_t, StreamStatisticianImpl*> statisticians; |
| 419 | { |
| 420 | rtc::CritScope cs(&receive_statistics_lock_); |
| 421 | statisticians = statisticians_; |
| 422 | } |
danilchap | f5f793c | 2017-07-27 04:44:18 -0700 | [diff] [blame] | 423 | std::vector<rtcp::ReportBlock> result; |
| 424 | result.reserve(std::min(max_blocks, statisticians.size())); |
Danil Chapovalov | d1996b7 | 2018-01-16 11:07:18 +0100 | [diff] [blame] | 425 | auto add_report_block = [&result](uint32_t media_ssrc, |
| 426 | StreamStatisticianImpl* statistician) { |
danilchap | f5f793c | 2017-07-27 04:44:18 -0700 | [diff] [blame] | 427 | // Do we have receive statistics to send? |
| 428 | RtcpStatistics stats; |
Danil Chapovalov | d1996b7 | 2018-01-16 11:07:18 +0100 | [diff] [blame] | 429 | if (!statistician->GetActiveStatisticsAndReset(&stats)) |
| 430 | return; |
danilchap | f5f793c | 2017-07-27 04:44:18 -0700 | [diff] [blame] | 431 | result.emplace_back(); |
| 432 | rtcp::ReportBlock& block = result.back(); |
Danil Chapovalov | d1996b7 | 2018-01-16 11:07:18 +0100 | [diff] [blame] | 433 | block.SetMediaSsrc(media_ssrc); |
danilchap | f5f793c | 2017-07-27 04:44:18 -0700 | [diff] [blame] | 434 | block.SetFractionLost(stats.fraction_lost); |
srte | 186d9c3 | 2017-08-04 05:03:53 -0700 | [diff] [blame] | 435 | if (!block.SetCumulativeLost(stats.packets_lost)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 436 | RTC_LOG(LS_WARNING) << "Cumulative lost is oversized."; |
danilchap | f5f793c | 2017-07-27 04:44:18 -0700 | [diff] [blame] | 437 | result.pop_back(); |
Danil Chapovalov | d1996b7 | 2018-01-16 11:07:18 +0100 | [diff] [blame] | 438 | return; |
danilchap | f5f793c | 2017-07-27 04:44:18 -0700 | [diff] [blame] | 439 | } |
srte | 186d9c3 | 2017-08-04 05:03:53 -0700 | [diff] [blame] | 440 | block.SetExtHighestSeqNum(stats.extended_highest_sequence_number); |
danilchap | f5f793c | 2017-07-27 04:44:18 -0700 | [diff] [blame] | 441 | block.SetJitter(stats.jitter); |
Danil Chapovalov | d1996b7 | 2018-01-16 11:07:18 +0100 | [diff] [blame] | 442 | }; |
| 443 | |
| 444 | const auto start_it = statisticians.upper_bound(last_returned_ssrc_); |
| 445 | for (auto it = start_it; |
| 446 | result.size() < max_blocks && it != statisticians.end(); ++it) |
| 447 | add_report_block(it->first, it->second); |
| 448 | for (auto it = statisticians.begin(); |
| 449 | result.size() < max_blocks && it != start_it; ++it) |
| 450 | add_report_block(it->first, it->second); |
| 451 | |
| 452 | if (!result.empty()) |
| 453 | last_returned_ssrc_ = result.back().source_ssrc(); |
danilchap | f5f793c | 2017-07-27 04:44:18 -0700 | [diff] [blame] | 454 | return result; |
| 455 | } |
| 456 | |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 457 | } // namespace webrtc |