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