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