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