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