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