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