wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [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 04:47:31 | [diff] [blame] | 11 | #include "modules/rtp_rtcp/source/receive_statistics_impl.h" |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [diff] [blame] | 12 | |
Oleh Prypin | 1992958 | 2019-04-23 06:50:04 | [diff] [blame] | 13 | #include <cmath> |
kwiberg | fd8be34 | 2016-05-15 02:44:11 | [diff] [blame] | 14 | #include <cstdlib> |
Danil Chapovalov | 8ce0d2b | 2018-11-23 10:03:25 | [diff] [blame] | 15 | #include <memory> |
Per Kjellander | ee8cd20 | 2021-03-10 11:31:38 | [diff] [blame] | 16 | #include <utility> |
danilchap | f5f793c | 2017-07-27 11:44:18 | [diff] [blame] | 17 | #include <vector> |
kwiberg | fd8be34 | 2016-05-15 02:44:11 | [diff] [blame] | 18 | |
Tomas Lundqvist | b50599b | 2022-10-17 14:44:52 | [diff] [blame] | 19 | #include "api/units/time_delta.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 20 | #include "modules/remote_bitrate_estimator/test/bwe_test_logging.h" |
Danil Chapovalov | b27a9f9 | 2021-05-17 12:22:02 | [diff] [blame] | 21 | #include "modules/rtp_rtcp/source/rtcp_packet/report_block.h" |
Niels Möller | 1f3206c | 2018-09-14 06:26:32 | [diff] [blame] | 22 | #include "modules/rtp_rtcp/source/rtp_packet_received.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 23 | #include "modules/rtp_rtcp/source/rtp_rtcp_config.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 24 | #include "rtc_base/logging.h" |
Nico Grunbaum | 7eac6ca | 2022-01-24 21:49:58 | [diff] [blame] | 25 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 26 | #include "system_wrappers/include/clock.h" |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
Alessio Bazzica | 5cf8c2c | 2021-03-24 07:51:26 | [diff] [blame] | 29 | namespace { |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 30 | constexpr TimeDelta kStatisticsTimeout = TimeDelta::Seconds(8); |
| 31 | constexpr TimeDelta kStatisticsProcessInterval = TimeDelta::Seconds(1); |
| 32 | |
| 33 | TimeDelta UnixEpochDelta(Clock& clock) { |
| 34 | Timestamp now = clock.CurrentTime(); |
| 35 | NtpTime ntp_now = clock.ConvertTimestampToNtpTime(now); |
| 36 | return TimeDelta::Millis(ntp_now.ToMs() - now.ms() - |
| 37 | rtc::kNtpJan1970Millisecs); |
| 38 | } |
| 39 | |
Alessio Bazzica | 5cf8c2c | 2021-03-24 07:51:26 | [diff] [blame] | 40 | } // namespace |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [diff] [blame] | 41 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 | [diff] [blame] | 42 | StreamStatistician::~StreamStatistician() {} |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [diff] [blame] | 43 | |
Jared Siskin | c018bae | 2023-04-19 23:24:03 | [diff] [blame] | 44 | StreamStatisticianImpl::StreamStatisticianImpl(uint32_t ssrc, |
| 45 | Clock* clock, |
Niels Möller | d781965 | 2019-08-13 12:43:02 | [diff] [blame] | 46 | int max_reordering_threshold) |
danilchap | ec86be0 | 2017-08-14 12:51:02 | [diff] [blame] | 47 | : ssrc_(ssrc), |
| 48 | clock_(clock), |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 49 | delta_internal_unix_epoch_(UnixEpochDelta(*clock_)), |
Danil Chapovalov | 950e231 | 2023-07-24 10:44:05 | [diff] [blame] | 50 | incoming_bitrate_(/*max_window_size=*/kStatisticsProcessInterval), |
Danil Chapovalov | ebb50c2 | 2018-11-22 13:04:02 | [diff] [blame] | 51 | max_reordering_threshold_(max_reordering_threshold), |
Niels Möller | 87da109 | 2019-05-24 12:04:28 | [diff] [blame] | 52 | enable_retransmit_detection_(false), |
Danil Chapovalov | b27a9f9 | 2021-05-17 12:22:02 | [diff] [blame] | 53 | cumulative_loss_is_capped_(false), |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [diff] [blame] | 54 | jitter_q4_(0), |
Qingsi Wang | 2370b08 | 2018-08-21 21:24:26 | [diff] [blame] | 55 | cumulative_loss_(0), |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 56 | cumulative_loss_rtcp_offset_(0), |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [diff] [blame] | 57 | last_received_timestamp_(0), |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 58 | received_seq_first_(-1), |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 | [diff] [blame] | 59 | received_seq_max_(-1), |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 60 | last_report_cumulative_loss_(0), |
Tomas Lundqvist | b50599b | 2022-10-17 14:44:52 | [diff] [blame] | 61 | last_report_seq_max_(-1), |
| 62 | last_payload_type_frequency_(0) {} |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [diff] [blame] | 63 | |
Danil Chapovalov | 2a5ce2b | 2018-02-07 08:38:31 | [diff] [blame] | 64 | StreamStatisticianImpl::~StreamStatisticianImpl() = default; |
| 65 | |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 | [diff] [blame] | 66 | bool StreamStatisticianImpl::UpdateOutOfOrder(const RtpPacketReceived& packet, |
| 67 | int64_t sequence_number, |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 68 | Timestamp now) { |
Artem Titov | 913cfa7 | 2021-07-28 21:57:33 | [diff] [blame] | 69 | // Check if `packet` is second packet of a stream restart. |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 | [diff] [blame] | 70 | if (received_seq_out_of_order_) { |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 71 | // Count the previous packet as a received; it was postponed below. |
| 72 | --cumulative_loss_; |
| 73 | |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 | [diff] [blame] | 74 | uint16_t expected_sequence_number = *received_seq_out_of_order_ + 1; |
| 75 | received_seq_out_of_order_ = absl::nullopt; |
| 76 | if (packet.SequenceNumber() == expected_sequence_number) { |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 77 | // Ignore sequence number gap caused by stream restart for packet loss |
| 78 | // calculation, by setting received_seq_max_ to the sequence number just |
| 79 | // before the out-of-order seqno. This gives a net zero change of |
Artem Titov | 913cfa7 | 2021-07-28 21:57:33 | [diff] [blame] | 80 | // `cumulative_loss_`, for the two packets interpreted as a stream reset. |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 81 | // |
| 82 | // Fraction loss for the next report may get a bit off, since we don't |
| 83 | // update last_report_seq_max_ and last_report_cumulative_loss_ in a |
| 84 | // consistent way. |
| 85 | last_report_seq_max_ = sequence_number - 2; |
| 86 | received_seq_max_ = sequence_number - 2; |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 | [diff] [blame] | 87 | return false; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if (std::abs(sequence_number - received_seq_max_) > |
| 92 | max_reordering_threshold_) { |
| 93 | // Sequence number gap looks too large, wait until next packet to check |
| 94 | // for a stream restart. |
| 95 | received_seq_out_of_order_ = packet.SequenceNumber(); |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 96 | // Postpone counting this as a received packet until we know how to update |
Artem Titov | 913cfa7 | 2021-07-28 21:57:33 | [diff] [blame] | 97 | // `received_seq_max_`, otherwise we temporarily decrement |
| 98 | // `cumulative_loss_`. The |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 99 | // ReceiveStatisticsTest.StreamRestartDoesntCountAsLoss test expects |
Artem Titov | 913cfa7 | 2021-07-28 21:57:33 | [diff] [blame] | 100 | // `cumulative_loss_` to be unchanged by the reception of the first packet |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 101 | // after stream reset. |
| 102 | ++cumulative_loss_; |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
| 106 | if (sequence_number > received_seq_max_) |
| 107 | return false; |
| 108 | |
| 109 | // Old out of order packet, may be retransmit. |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 110 | if (enable_retransmit_detection_ && IsRetransmitOfOldPacket(packet, now)) |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 | [diff] [blame] | 111 | receive_counters_.retransmitted.AddPacket(packet); |
| 112 | return true; |
| 113 | } |
| 114 | |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 115 | void StreamStatisticianImpl::UpdateCounters(const RtpPacketReceived& packet) { |
Niels Möller | dbb988b | 2018-11-15 07:05:16 | [diff] [blame] | 116 | RTC_DCHECK_EQ(ssrc_, packet.Ssrc()); |
Danil Chapovalov | 0f1a2c5 | 2023-05-26 09:30:26 | [diff] [blame] | 117 | Timestamp now = clock_->CurrentTime(); |
Danil Chapovalov | 44727b4 | 2018-11-22 10:28:45 | [diff] [blame] | 118 | |
Danil Chapovalov | 950e231 | 2023-07-24 10:44:05 | [diff] [blame] | 119 | incoming_bitrate_.Update(packet.size(), now); |
Niels Möller | dbb988b | 2018-11-15 07:05:16 | [diff] [blame] | 120 | receive_counters_.transmitted.AddPacket(packet); |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 121 | --cumulative_loss_; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [diff] [blame] | 122 | |
Evan Shrubsole | 7b4c8ad | 2023-01-09 14:34:34 | [diff] [blame] | 123 | // Use PeekUnwrap and later update the state to avoid updating the state for |
| 124 | // out of order packets. |
| 125 | int64_t sequence_number = seq_unwrapper_.PeekUnwrap(packet.SequenceNumber()); |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 126 | |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 | [diff] [blame] | 127 | if (!ReceivedRtpPacket()) { |
| 128 | received_seq_first_ = sequence_number; |
| 129 | last_report_seq_max_ = sequence_number - 1; |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 130 | received_seq_max_ = sequence_number - 1; |
Danil Chapovalov | 0f1a2c5 | 2023-05-26 09:30:26 | [diff] [blame] | 131 | receive_counters_.first_packet_time = now; |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 132 | } else if (UpdateOutOfOrder(packet, sequence_number, now)) { |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 133 | return; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [diff] [blame] | 134 | } |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 | [diff] [blame] | 135 | // In order packet. |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 136 | cumulative_loss_ += sequence_number - received_seq_max_; |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 | [diff] [blame] | 137 | received_seq_max_ = sequence_number; |
Evan Shrubsole | 7b4c8ad | 2023-01-09 14:34:34 | [diff] [blame] | 138 | // Update the internal state of `seq_unwrapper_`. |
| 139 | seq_unwrapper_.Unwrap(packet.SequenceNumber()); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [diff] [blame] | 140 | |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 | [diff] [blame] | 141 | // If new time stamp and more than one in-order packet received, calculate |
| 142 | // new jitter statistics. |
| 143 | if (packet.Timestamp() != last_received_timestamp_ && |
| 144 | (receive_counters_.transmitted.packets - |
| 145 | receive_counters_.retransmitted.packets) > 1) { |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 146 | UpdateJitter(packet, now); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [diff] [blame] | 147 | } |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 | [diff] [blame] | 148 | last_received_timestamp_ = packet.Timestamp(); |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 149 | last_receive_time_ = now; |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 | [diff] [blame] | 150 | } |
| 151 | |
Niels Möller | dbb988b | 2018-11-15 07:05:16 | [diff] [blame] | 152 | void StreamStatisticianImpl::UpdateJitter(const RtpPacketReceived& packet, |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 153 | Timestamp receive_time) { |
| 154 | RTC_DCHECK(last_receive_time_.has_value()); |
| 155 | TimeDelta receive_diff = receive_time - *last_receive_time_; |
| 156 | RTC_DCHECK_GE(receive_diff, TimeDelta::Zero()); |
| 157 | uint32_t receive_diff_rtp = |
| 158 | (receive_diff * packet.payload_type_frequency()).seconds<uint32_t>(); |
Danil Chapovalov | 856cf22 | 2018-11-26 09:20:01 | [diff] [blame] | 159 | int32_t time_diff_samples = |
| 160 | receive_diff_rtp - (packet.Timestamp() - last_received_timestamp_); |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 | [diff] [blame] | 161 | |
kwiberg | fd8be34 | 2016-05-15 02:44:11 | [diff] [blame] | 162 | time_diff_samples = std::abs(time_diff_samples); |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 | [diff] [blame] | 163 | |
Anton Podavalov | ea40563 | 2022-10-19 16:58:22 | [diff] [blame] | 164 | ReviseFrequencyAndJitter(packet.payload_type_frequency()); |
| 165 | |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 | [diff] [blame] | 166 | // lib_jingle sometimes deliver crazy jumps in TS for the same stream. |
| 167 | // If this happens, don't update jitter value. Use 5 secs video frequency |
| 168 | // as the threshold. |
| 169 | if (time_diff_samples < 450000) { |
| 170 | // Note we calculate in Q4 to avoid using float. |
| 171 | int32_t jitter_diff_q4 = (time_diff_samples << 4) - jitter_q4_; |
| 172 | jitter_q4_ += ((jitter_diff_q4 + 8) >> 4); |
| 173 | } |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 | [diff] [blame] | 174 | } |
| 175 | |
Anton Podavalov | ea40563 | 2022-10-19 16:58:22 | [diff] [blame] | 176 | void StreamStatisticianImpl::ReviseFrequencyAndJitter( |
| 177 | int payload_type_frequency) { |
| 178 | if (payload_type_frequency == last_payload_type_frequency_) { |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | if (payload_type_frequency != 0) { |
| 183 | if (last_payload_type_frequency_ != 0) { |
| 184 | // Value in "jitter_q4_" variable is a number of samples. |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 185 | // I.e. jitter = timestamp (s) * frequency (Hz). |
Anton Podavalov | ea40563 | 2022-10-19 16:58:22 | [diff] [blame] | 186 | // Since the frequency has changed we have to update the number of samples |
| 187 | // accordingly. The new value should rely on a new frequency. |
| 188 | |
| 189 | // If we don't do such procedure we end up with the number of samples that |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 190 | // cannot be converted into TimeDelta correctly |
| 191 | // (i.e. jitter = jitter_q4_ >> 4 / payload_type_frequency). |
Anton Podavalov | ea40563 | 2022-10-19 16:58:22 | [diff] [blame] | 192 | // In such case, the number of samples has a "mix". |
| 193 | |
| 194 | // Doing so we pretend that everything prior and including the current |
| 195 | // packet were computed on packet's frequency. |
| 196 | jitter_q4_ = static_cast<int>(static_cast<uint64_t>(jitter_q4_) * |
| 197 | payload_type_frequency / |
| 198 | last_payload_type_frequency_); |
| 199 | } |
| 200 | // If last_payload_type_frequency_ is not present, the jitter_q4_ |
| 201 | // variable has its initial value. |
| 202 | |
| 203 | // Keep last_payload_type_frequency_ up to date and non-zero (set). |
| 204 | last_payload_type_frequency_ = payload_type_frequency; |
| 205 | } |
| 206 | } |
| 207 | |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 | [diff] [blame] | 208 | void StreamStatisticianImpl::SetMaxReorderingThreshold( |
| 209 | int max_reordering_threshold) { |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 | [diff] [blame] | 210 | max_reordering_threshold_ = max_reordering_threshold; |
| 211 | } |
| 212 | |
Niels Möller | 5304a32 | 2018-08-27 11:27:05 | [diff] [blame] | 213 | void StreamStatisticianImpl::EnableRetransmitDetection(bool enable) { |
Niels Möller | 5304a32 | 2018-08-27 11:27:05 | [diff] [blame] | 214 | enable_retransmit_detection_ = enable; |
| 215 | } |
| 216 | |
Niels Möller | d77cc24 | 2019-08-22 07:40:25 | [diff] [blame] | 217 | RtpReceiveStats StreamStatisticianImpl::GetStats() const { |
Niels Möller | d77cc24 | 2019-08-22 07:40:25 | [diff] [blame] | 218 | RtpReceiveStats stats; |
| 219 | stats.packets_lost = cumulative_loss_; |
Niels Möller | d77cc24 | 2019-08-22 07:40:25 | [diff] [blame] | 220 | // Note: internal jitter value is in Q4 and needs to be scaled by 1/16. |
| 221 | stats.jitter = jitter_q4_ >> 4; |
Tomas Lundqvist | b50599b | 2022-10-17 14:44:52 | [diff] [blame] | 222 | if (last_payload_type_frequency_ > 0) { |
| 223 | // Divide value in fractional seconds by frequency to get jitter in |
| 224 | // fractional seconds. |
| 225 | stats.interarrival_jitter = |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 226 | TimeDelta::Seconds(stats.jitter) / last_payload_type_frequency_; |
Tomas Lundqvist | b50599b | 2022-10-17 14:44:52 | [diff] [blame] | 227 | } |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 228 | if (last_receive_time_.has_value()) { |
Danil Chapovalov | 54e95bc | 2023-06-02 12:54:45 | [diff] [blame] | 229 | stats.last_packet_received = |
| 230 | *last_receive_time_ + delta_internal_unix_epoch_; |
Alessio Bazzica | 5cf8c2c | 2021-03-24 07:51:26 | [diff] [blame] | 231 | } |
Niels Möller | d77cc24 | 2019-08-22 07:40:25 | [diff] [blame] | 232 | stats.packet_counter = receive_counters_.transmitted; |
| 233 | return stats; |
| 234 | } |
| 235 | |
Danil Chapovalov | b27a9f9 | 2021-05-17 12:22:02 | [diff] [blame] | 236 | void StreamStatisticianImpl::MaybeAppendReportBlockAndReset( |
| 237 | std::vector<rtcp::ReportBlock>& report_blocks) { |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 238 | if (!ReceivedRtpPacket()) { |
Danil Chapovalov | b27a9f9 | 2021-05-17 12:22:02 | [diff] [blame] | 239 | return; |
Niels Möller | 12ebfa6 | 2019-08-06 14:04:12 | [diff] [blame] | 240 | } |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 241 | Timestamp now = clock_->CurrentTime(); |
| 242 | if (now - *last_receive_time_ >= kStatisticsTimeout) { |
| 243 | // Not active. |
Danil Chapovalov | b27a9f9 | 2021-05-17 12:22:02 | [diff] [blame] | 244 | return; |
Danil Chapovalov | c5267d2 | 2017-09-18 11:57:19 | [diff] [blame] | 245 | } |
Danil Chapovalov | c5267d2 | 2017-09-18 11:57:19 | [diff] [blame] | 246 | |
Danil Chapovalov | b27a9f9 | 2021-05-17 12:22:02 | [diff] [blame] | 247 | report_blocks.emplace_back(); |
| 248 | rtcp::ReportBlock& stats = report_blocks.back(); |
| 249 | stats.SetMediaSsrc(ssrc_); |
Qingsi Wang | 2370b08 | 2018-08-21 21:24:26 | [diff] [blame] | 250 | // Calculate fraction lost. |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 | [diff] [blame] | 251 | int64_t exp_since_last = received_seq_max_ - last_report_seq_max_; |
| 252 | RTC_DCHECK_GE(exp_since_last, 0); |
Qingsi Wang | 2370b08 | 2018-08-21 21:24:26 | [diff] [blame] | 253 | |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 254 | int32_t lost_since_last = cumulative_loss_ - last_report_cumulative_loss_; |
| 255 | if (exp_since_last > 0 && lost_since_last > 0) { |
Qingsi Wang | 2370b08 | 2018-08-21 21:24:26 | [diff] [blame] | 256 | // Scale 0 to 255, where 255 is 100% loss. |
Danil Chapovalov | b27a9f9 | 2021-05-17 12:22:02 | [diff] [blame] | 257 | stats.SetFractionLost(255 * lost_since_last / exp_since_last); |
Qingsi Wang | 2370b08 | 2018-08-21 21:24:26 | [diff] [blame] | 258 | } |
Qingsi Wang | 2370b08 | 2018-08-21 21:24:26 | [diff] [blame] | 259 | |
Danil Chapovalov | b27a9f9 | 2021-05-17 12:22:02 | [diff] [blame] | 260 | int packets_lost = cumulative_loss_ + cumulative_loss_rtcp_offset_; |
| 261 | if (packets_lost < 0) { |
Alfred E. Heggestad | be90237 | 2023-06-23 08:22:25 | [diff] [blame] | 262 | // Clamp to zero. Work around to accommodate for senders that misbehave with |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 263 | // negative cumulative loss. |
Danil Chapovalov | b27a9f9 | 2021-05-17 12:22:02 | [diff] [blame] | 264 | packets_lost = 0; |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 265 | cumulative_loss_rtcp_offset_ = -cumulative_loss_; |
| 266 | } |
Danil Chapovalov | b27a9f9 | 2021-05-17 12:22:02 | [diff] [blame] | 267 | if (packets_lost > 0x7fffff) { |
| 268 | // Packets lost is a 24 bit signed field, and thus should be clamped, as |
| 269 | // described in https://datatracker.ietf.org/doc/html/rfc3550#appendix-A.3 |
| 270 | if (!cumulative_loss_is_capped_) { |
| 271 | cumulative_loss_is_capped_ = true; |
| 272 | RTC_LOG(LS_WARNING) << "Cumulative loss reached maximum value for ssrc " |
| 273 | << ssrc_; |
| 274 | } |
| 275 | packets_lost = 0x7fffff; |
| 276 | } |
| 277 | stats.SetCumulativeLost(packets_lost); |
| 278 | stats.SetExtHighestSeqNum(received_seq_max_); |
Qingsi Wang | 2370b08 | 2018-08-21 21:24:26 | [diff] [blame] | 279 | // Note: internal jitter value is in Q4 and needs to be scaled by 1/16. |
Danil Chapovalov | b27a9f9 | 2021-05-17 12:22:02 | [diff] [blame] | 280 | stats.SetJitter(jitter_q4_ >> 4); |
Qingsi Wang | 2370b08 | 2018-08-21 21:24:26 | [diff] [blame] | 281 | |
Qingsi Wang | 2370b08 | 2018-08-21 21:24:26 | [diff] [blame] | 282 | // Only for report blocks in RTCP SR and RR. |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 283 | last_report_cumulative_loss_ = cumulative_loss_; |
Qingsi Wang | 2370b08 | 2018-08-21 21:24:26 | [diff] [blame] | 284 | last_report_seq_max_ = received_seq_max_; |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 285 | BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts", now.ms(), |
Qingsi Wang | 2370b08 | 2018-08-21 21:24:26 | [diff] [blame] | 286 | cumulative_loss_, ssrc_); |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 287 | BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "received_seq_max_pkts", now.ms(), |
Danil Chapovalov | b27a9f9 | 2021-05-17 12:22:02 | [diff] [blame] | 288 | (received_seq_max_ - received_seq_first_), |
| 289 | ssrc_); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [diff] [blame] | 290 | } |
| 291 | |
Niels Möller | 9a9f18a | 2019-08-02 11:52:37 | [diff] [blame] | 292 | absl::optional<int> StreamStatisticianImpl::GetFractionLostInPercent() const { |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 293 | if (!ReceivedRtpPacket()) { |
Niels Möller | 9a9f18a | 2019-08-02 11:52:37 | [diff] [blame] | 294 | return absl::nullopt; |
| 295 | } |
| 296 | int64_t expected_packets = 1 + received_seq_max_ - received_seq_first_; |
| 297 | if (expected_packets <= 0) { |
| 298 | return absl::nullopt; |
| 299 | } |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 300 | if (cumulative_loss_ <= 0) { |
| 301 | return 0; |
| 302 | } |
Niels Möller | 9a9f18a | 2019-08-02 11:52:37 | [diff] [blame] | 303 | return 100 * static_cast<int64_t>(cumulative_loss_) / expected_packets; |
| 304 | } |
| 305 | |
Niels Möller | 58b496b | 2019-08-12 10:16:31 | [diff] [blame] | 306 | StreamDataCounters StreamStatisticianImpl::GetReceiveStreamDataCounters() |
| 307 | const { |
Niels Möller | 58b496b | 2019-08-12 10:16:31 | [diff] [blame] | 308 | return receive_counters_; |
asapersson@webrtc.org | d952c40 | 2014-11-27 07:38:56 | [diff] [blame] | 309 | } |
| 310 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 | [diff] [blame] | 311 | uint32_t StreamStatisticianImpl::BitrateReceived() const { |
Danil Chapovalov | 950e231 | 2023-07-24 10:44:05 | [diff] [blame] | 312 | return incoming_bitrate_.Rate(clock_->CurrentTime()) |
| 313 | .value_or(DataRate::Zero()) |
| 314 | .bps<uint32_t>(); |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 | [diff] [blame] | 315 | } |
| 316 | |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 | [diff] [blame] | 317 | bool StreamStatisticianImpl::IsRetransmitOfOldPacket( |
Danil Chapovalov | 44727b4 | 2018-11-22 10:28:45 | [diff] [blame] | 318 | const RtpPacketReceived& packet, |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 319 | Timestamp now) const { |
| 320 | int frequency_hz = packet.payload_type_frequency(); |
| 321 | RTC_DCHECK(last_receive_time_.has_value()); |
Harald Alvestrand | 7dbf554 | 2023-07-24 14:25:47 | [diff] [blame] | 322 | RTC_CHECK_GT(frequency_hz, 0); |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 323 | TimeDelta time_diff = now - *last_receive_time_; |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 | [diff] [blame] | 324 | |
| 325 | // Diff in time stamp since last received in order. |
Niels Möller | dbb988b | 2018-11-15 07:05:16 | [diff] [blame] | 326 | uint32_t timestamp_diff = packet.Timestamp() - last_received_timestamp_; |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 327 | TimeDelta rtp_time_stamp_diff = |
| 328 | TimeDelta::Seconds(timestamp_diff) / frequency_hz; |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 | [diff] [blame] | 329 | |
Niels Möller | eda0087 | 2018-05-23 11:54:51 | [diff] [blame] | 330 | // Jitter standard deviation in samples. |
Oleh Prypin | 1992958 | 2019-04-23 06:50:04 | [diff] [blame] | 331 | float jitter_std = std::sqrt(static_cast<float>(jitter_q4_ >> 4)); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 | [diff] [blame] | 332 | |
Niels Möller | eda0087 | 2018-05-23 11:54:51 | [diff] [blame] | 333 | // 2 times the standard deviation => 95% confidence. |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 334 | // Min max_delay is 1ms. |
| 335 | TimeDelta max_delay = std::max( |
| 336 | TimeDelta::Seconds(2 * jitter_std / frequency_hz), TimeDelta::Millis(1)); |
Niels Möller | eda0087 | 2018-05-23 11:54:51 | [diff] [blame] | 337 | |
Danil Chapovalov | 2197300 | 2023-05-31 11:21:01 | [diff] [blame] | 338 | return time_diff > rtp_time_stamp_diff + max_delay; |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 | [diff] [blame] | 339 | } |
| 340 | |
Niels Möller | d781965 | 2019-08-13 12:43:02 | [diff] [blame] | 341 | std::unique_ptr<ReceiveStatistics> ReceiveStatistics::Create(Clock* clock) { |
Per Kjellander | ee8cd20 | 2021-03-10 11:31:38 | [diff] [blame] | 342 | return std::make_unique<ReceiveStatisticsLocked>( |
| 343 | clock, [](uint32_t ssrc, Clock* clock, int max_reordering_threshold) { |
| 344 | return std::make_unique<StreamStatisticianLocked>( |
| 345 | ssrc, clock, max_reordering_threshold); |
| 346 | }); |
Niels Möller | d781965 | 2019-08-13 12:43:02 | [diff] [blame] | 347 | } |
| 348 | |
Per Kjellander | ee8cd20 | 2021-03-10 11:31:38 | [diff] [blame] | 349 | std::unique_ptr<ReceiveStatistics> ReceiveStatistics::CreateThreadCompatible( |
| 350 | Clock* clock) { |
| 351 | return std::make_unique<ReceiveStatisticsImpl>( |
| 352 | clock, [](uint32_t ssrc, Clock* clock, int max_reordering_threshold) { |
| 353 | return std::make_unique<StreamStatisticianImpl>( |
| 354 | ssrc, clock, max_reordering_threshold); |
| 355 | }); |
| 356 | } |
| 357 | |
| 358 | ReceiveStatisticsImpl::ReceiveStatisticsImpl( |
| 359 | Clock* clock, |
| 360 | std::function<std::unique_ptr<StreamStatisticianImplInterface>( |
| 361 | uint32_t ssrc, |
| 362 | Clock* clock, |
| 363 | int max_reordering_threshold)> stream_statistician_factory) |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 | [diff] [blame] | 364 | : clock_(clock), |
Per Kjellander | ee8cd20 | 2021-03-10 11:31:38 | [diff] [blame] | 365 | stream_statistician_factory_(std::move(stream_statistician_factory)), |
Victor Boivie | b2f8c16 | 2021-04-28 10:02:37 | [diff] [blame] | 366 | last_returned_ssrc_idx_(0), |
Niels Möller | d781965 | 2019-08-13 12:43:02 | [diff] [blame] | 367 | max_reordering_threshold_(kDefaultMaxReorderingThreshold) {} |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 | [diff] [blame] | 368 | |
Niels Möller | 1f3206c | 2018-09-14 06:26:32 | [diff] [blame] | 369 | void ReceiveStatisticsImpl::OnRtpPacket(const RtpPacketReceived& packet) { |
sprang@webrtc.org | c30e9e2 | 2014-09-08 08:20:18 | [diff] [blame] | 370 | // StreamStatisticianImpl instance is created once and only destroyed when |
| 371 | // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has |
| 372 | // it's own locking so don't hold receive_statistics_lock_ (potential |
| 373 | // deadlock). |
Niels Möller | 1a3859c | 2019-09-04 07:43:15 | [diff] [blame] | 374 | GetOrCreateStatistician(packet.Ssrc())->UpdateCounters(packet); |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 | [diff] [blame] | 375 | } |
| 376 | |
Per Kjellander | ee8cd20 | 2021-03-10 11:31:38 | [diff] [blame] | 377 | StreamStatistician* ReceiveStatisticsImpl::GetStatistician( |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 | [diff] [blame] | 378 | uint32_t ssrc) const { |
Niels Möller | 87da109 | 2019-05-24 12:04:28 | [diff] [blame] | 379 | const auto& it = statisticians_.find(ssrc); |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 | [diff] [blame] | 380 | if (it == statisticians_.end()) |
Per Kjellander | ee8cd20 | 2021-03-10 11:31:38 | [diff] [blame] | 381 | return nullptr; |
| 382 | return it->second.get(); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [diff] [blame] | 383 | } |
| 384 | |
Per Kjellander | ee8cd20 | 2021-03-10 11:31:38 | [diff] [blame] | 385 | StreamStatisticianImplInterface* ReceiveStatisticsImpl::GetOrCreateStatistician( |
Niels Möller | 87da109 | 2019-05-24 12:04:28 | [diff] [blame] | 386 | uint32_t ssrc) { |
Per Kjellander | ee8cd20 | 2021-03-10 11:31:38 | [diff] [blame] | 387 | std::unique_ptr<StreamStatisticianImplInterface>& impl = statisticians_[ssrc]; |
Niels Möller | 87da109 | 2019-05-24 12:04:28 | [diff] [blame] | 388 | if (impl == nullptr) { // new element |
Per Kjellander | ee8cd20 | 2021-03-10 11:31:38 | [diff] [blame] | 389 | impl = |
| 390 | stream_statistician_factory_(ssrc, clock_, max_reordering_threshold_); |
Victor Boivie | b2f8c16 | 2021-04-28 10:02:37 | [diff] [blame] | 391 | all_ssrcs_.push_back(ssrc); |
Niels Möller | 87da109 | 2019-05-24 12:04:28 | [diff] [blame] | 392 | } |
Per Kjellander | ee8cd20 | 2021-03-10 11:31:38 | [diff] [blame] | 393 | return impl.get(); |
Niels Möller | 87da109 | 2019-05-24 12:04:28 | [diff] [blame] | 394 | } |
| 395 | |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 | [diff] [blame] | 396 | void ReceiveStatisticsImpl::SetMaxReorderingThreshold( |
| 397 | int max_reordering_threshold) { |
Per Kjellander | ee8cd20 | 2021-03-10 11:31:38 | [diff] [blame] | 398 | max_reordering_threshold_ = max_reordering_threshold; |
| 399 | for (auto& statistician : statisticians_) { |
Danil Chapovalov | c5267d2 | 2017-09-18 11:57:19 | [diff] [blame] | 400 | statistician.second->SetMaxReorderingThreshold(max_reordering_threshold); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
Niels Möller | 87da109 | 2019-05-24 12:04:28 | [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 11:27:05 | [diff] [blame] | 411 | void ReceiveStatisticsImpl::EnableRetransmitDetection(uint32_t ssrc, |
| 412 | bool enable) { |
Niels Möller | 87da109 | 2019-05-24 12:04:28 | [diff] [blame] | 413 | GetOrCreateStatistician(ssrc)->EnableRetransmitDetection(enable); |
Niels Möller | 5304a32 | 2018-08-27 11:27:05 | [diff] [blame] | 414 | } |
| 415 | |
danilchap | 0bc8423 | 2017-08-11 15:12:54 | [diff] [blame] | 416 | std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks( |
danilchap | f5f793c | 2017-07-27 11:44:18 | [diff] [blame] | 417 | size_t max_blocks) { |
danilchap | f5f793c | 2017-07-27 11:44:18 | [diff] [blame] | 418 | std::vector<rtcp::ReportBlock> result; |
Victor Boivie | b2f8c16 | 2021-04-28 10:02:37 | [diff] [blame] | 419 | result.reserve(std::min(max_blocks, all_ssrcs_.size())); |
| 420 | |
| 421 | size_t ssrc_idx = 0; |
| 422 | for (size_t i = 0; i < all_ssrcs_.size() && result.size() < max_blocks; ++i) { |
| 423 | ssrc_idx = (last_returned_ssrc_idx_ + i + 1) % all_ssrcs_.size(); |
| 424 | const uint32_t media_ssrc = all_ssrcs_[ssrc_idx]; |
| 425 | auto statistician_it = statisticians_.find(media_ssrc); |
| 426 | RTC_DCHECK(statistician_it != statisticians_.end()); |
Danil Chapovalov | b27a9f9 | 2021-05-17 12:22:02 | [diff] [blame] | 427 | statistician_it->second->MaybeAppendReportBlockAndReset(result); |
Victor Boivie | b2f8c16 | 2021-04-28 10:02:37 | [diff] [blame] | 428 | } |
| 429 | last_returned_ssrc_idx_ = ssrc_idx; |
danilchap | f5f793c | 2017-07-27 11:44:18 | [diff] [blame] | 430 | return result; |
| 431 | } |
| 432 | |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 | [diff] [blame] | 433 | } // namespace webrtc |