blob: 34356e3455511d143c47327396ca6e55d27b7ca0 [file] [log] [blame]
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/source/receive_statistics_impl.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000012
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000013#include <math.h>
14
kwibergfd8be342016-05-14 19:44:11 -070015#include <cstdlib>
danilchapf5f793c2017-07-27 04:44:18 -070016#include <vector>
kwibergfd8be342016-05-14 19:44:11 -070017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/remote_bitrate_estimator/test/bwe_test_logging.h"
Niels Möller1f3206c2018-09-14 08:26:32 +020019#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
21#include "modules/rtp_rtcp/source/time_util.h"
22#include "rtc_base/logging.h"
23#include "system_wrappers/include/clock.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000024
25namespace webrtc {
26
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000027const int64_t kStatisticsTimeoutMs = 8000;
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000028const int64_t kStatisticsProcessIntervalMs = 1000;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000029
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000030StreamStatistician::~StreamStatistician() {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000031
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000032StreamStatisticianImpl::StreamStatisticianImpl(
danilchapec86be02017-08-14 05:51:02 -070033 uint32_t ssrc,
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000034 Clock* clock,
Niels Möller5304a322018-08-27 13:27:05 +020035 bool enable_retransmit_detection,
sprang@webrtc.org0e932572014-01-23 10:00:39 +000036 RtcpStatisticsCallback* rtcp_callback,
37 StreamDataCountersCallback* rtp_callback)
danilchapec86be02017-08-14 05:51:02 -070038 : ssrc_(ssrc),
39 clock_(clock),
sprangcd349d92016-07-13 09:11:28 -070040 incoming_bitrate_(kStatisticsProcessIntervalMs,
41 RateStatistics::kBpsScale),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000042 max_reordering_threshold_(kDefaultMaxReorderingThreshold),
Niels Möller5304a322018-08-27 13:27:05 +020043 enable_retransmit_detection_(enable_retransmit_detection),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000044 jitter_q4_(0),
Qingsi Wang2370b082018-08-21 14:24:26 -070045 cumulative_loss_(0),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000046 last_receive_time_ms_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000047 last_received_timestamp_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000048 received_seq_first_(0),
49 received_seq_max_(0),
50 received_seq_wraps_(0),
Qingsi Wang2370b082018-08-21 14:24:26 -070051 last_report_inorder_packets_(0),
52 last_report_old_packets_(0),
53 last_report_seq_max_(0),
sprang@webrtc.org0e932572014-01-23 10:00:39 +000054 rtcp_callback_(rtcp_callback),
55 rtp_callback_(rtp_callback) {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000056
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010057StreamStatisticianImpl::~StreamStatisticianImpl() = default;
58
Niels Möllerdbb988b2018-11-15 08:05:16 +010059void StreamStatisticianImpl::OnRtpPacket(const RtpPacketReceived& packet) {
Danil Chapovalov44727b42018-11-22 11:28:45 +010060 StreamDataCounters counters = UpdateCounters(packet);
danilchapec86be02017-08-14 05:51:02 -070061 rtp_callback_->DataCountersUpdated(counters, ssrc_);
sprang@webrtc.orga45cac02014-01-27 16:22:08 +000062}
63
danilchapec86be02017-08-14 05:51:02 -070064StreamDataCounters StreamStatisticianImpl::UpdateCounters(
Danil Chapovalov44727b42018-11-22 11:28:45 +010065 const RtpPacketReceived& packet) {
66 rtc::CritScope cs(&stream_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +010067 RTC_DCHECK_EQ(ssrc_, packet.Ssrc());
Danil Chapovalov44727b42018-11-22 11:28:45 +010068 uint16_t sequence_number = packet.SequenceNumber();
69 bool in_order =
70 // First packet is always in order.
71 last_receive_time_ms_ == 0 ||
72 IsNewerSequenceNumber(sequence_number, received_seq_max_) ||
73 // If we have a restart of the remote side this packet is still in order.
74 !IsNewerSequenceNumber(sequence_number,
75 received_seq_max_ - max_reordering_threshold_);
76 int64_t now_ms = clock_->TimeInMilliseconds();
77
78 incoming_bitrate_.Update(packet.size(), now_ms);
Niels Möllerdbb988b2018-11-15 08:05:16 +010079 receive_counters_.transmitted.AddPacket(packet);
Danil Chapovalov44727b42018-11-22 11:28:45 +010080 if (!in_order && enable_retransmit_detection_ &&
81 IsRetransmitOfOldPacket(packet, now_ms)) {
Niels Möllerdbb988b2018-11-15 08:05:16 +010082 receive_counters_.retransmitted.AddPacket(packet);
sprang@webrtc.org0e932572014-01-23 10:00:39 +000083 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +000084
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +000085 if (receive_counters_.transmitted.packets == 1) {
Niels Möllerdbb988b2018-11-15 08:05:16 +010086 received_seq_first_ = packet.SequenceNumber();
Danil Chapovalov44727b42018-11-22 11:28:45 +010087 receive_counters_.first_packet_time_ms = now_ms;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000088 }
89
90 // Count only the new packets received. That is, if packets 1, 2, 3, 5, 4, 6
91 // are received, 4 will be ignored.
92 if (in_order) {
93 // Current time in samples.
danilchap37953762017-02-09 11:15:25 -080094 NtpTime receive_time = clock_->CurrentNtpTime();
wu@webrtc.org822fbd82013-08-15 23:38:54 +000095
96 // Wrong if we use RetransmitOfOldPacket.
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +000097 if (receive_counters_.transmitted.packets > 1 &&
Niels Möllerdbb988b2018-11-15 08:05:16 +010098 received_seq_max_ > packet.SequenceNumber()) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000099 // Wrap around detected.
100 received_seq_wraps_++;
101 }
102 // New max.
Niels Möllerdbb988b2018-11-15 08:05:16 +0100103 received_seq_max_ = packet.SequenceNumber();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000104
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000105 // If new time stamp and more than one in-order packet received, calculate
106 // new jitter statistics.
Niels Möllerdbb988b2018-11-15 08:05:16 +0100107 if (packet.Timestamp() != last_received_timestamp_ &&
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000108 (receive_counters_.transmitted.packets -
109 receive_counters_.retransmitted.packets) > 1) {
Niels Möllerdbb988b2018-11-15 08:05:16 +0100110 UpdateJitter(packet, receive_time);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000111 }
Niels Möllerdbb988b2018-11-15 08:05:16 +0100112 last_received_timestamp_ = packet.Timestamp();
danilchap1227e8b2015-12-21 11:06:50 -0800113 last_receive_time_ntp_ = receive_time;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100114 last_receive_time_ms_ = now_ms;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000115 }
danilchapec86be02017-08-14 05:51:02 -0700116 return receive_counters_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000117}
118
Niels Möllerdbb988b2018-11-15 08:05:16 +0100119void StreamStatisticianImpl::UpdateJitter(const RtpPacketReceived& packet,
danilchap1227e8b2015-12-21 11:06:50 -0800120 NtpTime receive_time) {
121 uint32_t receive_time_rtp =
Niels Möllerdbb988b2018-11-15 08:05:16 +0100122 NtpToRtp(receive_time, packet.payload_type_frequency());
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000123 uint32_t last_receive_time_rtp =
Niels Möllerdbb988b2018-11-15 08:05:16 +0100124 NtpToRtp(last_receive_time_ntp_, packet.payload_type_frequency());
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000125 int32_t time_diff_samples = (receive_time_rtp - last_receive_time_rtp) -
Niels Möllerdbb988b2018-11-15 08:05:16 +0100126 (packet.Timestamp() - last_received_timestamp_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000127
kwibergfd8be342016-05-14 19:44:11 -0700128 time_diff_samples = std::abs(time_diff_samples);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000129
130 // lib_jingle sometimes deliver crazy jumps in TS for the same stream.
131 // If this happens, don't update jitter value. Use 5 secs video frequency
132 // as the threshold.
133 if (time_diff_samples < 450000) {
134 // Note we calculate in Q4 to avoid using float.
135 int32_t jitter_diff_q4 = (time_diff_samples << 4) - jitter_q4_;
136 jitter_q4_ += ((jitter_diff_q4 + 8) >> 4);
137 }
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000138}
139
Niels Möllerdbb988b2018-11-15 08:05:16 +0100140void StreamStatisticianImpl::FecPacketReceived(
141 const RtpPacketReceived& packet) {
danilchapec86be02017-08-14 05:51:02 -0700142 StreamDataCounters counters;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000143 {
danilchap7c9426c2016-04-14 03:05:31 -0700144 rtc::CritScope cs(&stream_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +0100145 receive_counters_.fec.AddPacket(packet);
danilchapec86be02017-08-14 05:51:02 -0700146 counters = receive_counters_;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000147 }
danilchapec86be02017-08-14 05:51:02 -0700148 rtp_callback_->DataCountersUpdated(counters, ssrc_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000149}
150
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000151void StreamStatisticianImpl::SetMaxReorderingThreshold(
152 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700153 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000154 max_reordering_threshold_ = max_reordering_threshold;
155}
156
Niels Möller5304a322018-08-27 13:27:05 +0200157void StreamStatisticianImpl::EnableRetransmitDetection(bool enable) {
158 rtc::CritScope cs(&stream_lock_);
159 enable_retransmit_detection_ = enable;
160}
161
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000162bool StreamStatisticianImpl::GetStatistics(RtcpStatistics* statistics,
Qingsi Wang2370b082018-08-21 14:24:26 -0700163 bool reset) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000164 {
danilchap7c9426c2016-04-14 03:05:31 -0700165 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000166 if (received_seq_first_ == 0 &&
167 receive_counters_.transmitted.payload_bytes == 0) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000168 // We have not received anything.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000169 return false;
170 }
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000171
Qingsi Wang2370b082018-08-21 14:24:26 -0700172 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.org822fbd82013-08-15 23:38:54 +0000183 }
184
Qingsi Wang2370b082018-08-21 14:24:26 -0700185 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000186 return true;
187}
188
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200189bool StreamStatisticianImpl::GetActiveStatisticsAndReset(
190 RtcpStatistics* statistics) {
191 {
192 rtc::CritScope cs(&stream_lock_);
193 if (clock_->CurrentNtpInMilliseconds() - last_receive_time_ntp_.ToMs() >=
194 kStatisticsTimeoutMs) {
195 // Not active.
196 return false;
197 }
198 if (received_seq_first_ == 0 &&
199 receive_counters_.transmitted.payload_bytes == 0) {
200 // We have not received anything.
201 return false;
202 }
203
Qingsi Wang2370b082018-08-21 14:24:26 -0700204 *statistics = CalculateRtcpStatistics();
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200205 }
206
207 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
208 return true;
209}
210
Qingsi Wang2370b082018-08-21 14:24:26 -0700211RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics() {
212 RtcpStatistics stats;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000213
Qingsi Wang2370b082018-08-21 14:24:26 -0700214 if (last_report_inorder_packets_ == 0) {
215 // First time we send a report.
216 last_report_seq_max_ = received_seq_first_ - 1;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000217 }
218
Qingsi Wang2370b082018-08-21 14:24:26 -0700219 // Calculate fraction lost.
220 uint16_t exp_since_last = (received_seq_max_ - last_report_seq_max_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000221
Qingsi Wang2370b082018-08-21 14:24:26 -0700222 if (last_report_seq_max_ > received_seq_max_) {
223 // Can we assume that the seq_num can't go decrease over a full RTCP period?
224 exp_since_last = 0;
225 }
226
227 // Number of received RTP packets since last report, counts all packets but
228 // not re-transmissions.
229 uint32_t rec_since_last = (receive_counters_.transmitted.packets -
230 receive_counters_.retransmitted.packets) -
231 last_report_inorder_packets_;
232
233 // With NACK we don't know the expected retransmissions during the last
234 // second. We know how many "old" packets we have received. We just count
235 // the number of old received to estimate the loss, but it still does not
236 // guarantee an exact number since we run this based on time triggered by
237 // sending of an RTP packet. This should have a minimum effect.
238
239 // With NACK we don't count old packets as received since they are
240 // re-transmitted. We use RTT to decide if a packet is re-ordered or
241 // re-transmitted.
242 uint32_t retransmitted_packets =
243 receive_counters_.retransmitted.packets - last_report_old_packets_;
244 rec_since_last += retransmitted_packets;
245
246 int32_t missing = 0;
247 if (exp_since_last > rec_since_last) {
248 missing = (exp_since_last - rec_since_last);
249 }
250 uint8_t local_fraction_lost = 0;
251 if (exp_since_last) {
252 // Scale 0 to 255, where 255 is 100% loss.
253 local_fraction_lost = static_cast<uint8_t>(255 * missing / exp_since_last);
254 }
255 stats.fraction_lost = local_fraction_lost;
256
257 // We need a counter for cumulative loss too.
258 // TODO(danilchap): Ensure cumulative loss is below maximum value of 2^24.
259 cumulative_loss_ += missing;
260 stats.packets_lost = cumulative_loss_;
261 stats.extended_highest_sequence_number =
262 (received_seq_wraps_ << 16) + received_seq_max_;
263 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
264 stats.jitter = jitter_q4_ >> 4;
265
266 // Store this report.
267 last_reported_statistics_ = stats;
268
269 // Only for report blocks in RTCP SR and RR.
270 last_report_inorder_packets_ = receive_counters_.transmitted.packets -
271 receive_counters_.retransmitted.packets;
272 last_report_old_packets_ = receive_counters_.retransmitted.packets;
273 last_report_seq_max_ = received_seq_max_;
gaetano.carlucci61050f62016-09-30 06:29:54 -0700274 BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts",
gaetano.carlucci52a57032016-09-14 05:04:36 -0700275 clock_->TimeInMilliseconds(),
Qingsi Wang2370b082018-08-21 14:24:26 -0700276 cumulative_loss_, ssrc_);
gaetano.carlucci52a57032016-09-14 05:04:36 -0700277 BWE_TEST_LOGGING_PLOT_WITH_SSRC(
gaetano.carlucci61050f62016-09-30 06:29:54 -0700278 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700279 (received_seq_max_ - received_seq_first_), ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000280
Qingsi Wang2370b082018-08-21 14:24:26 -0700281 return stats;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000282}
283
Yves Gerey665174f2018-06-19 15:03:05 +0200284void StreamStatisticianImpl::GetDataCounters(size_t* bytes_received,
285 uint32_t* packets_received) const {
danilchap7c9426c2016-04-14 03:05:31 -0700286 rtc::CritScope cs(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000287 if (bytes_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000288 *bytes_received = receive_counters_.transmitted.payload_bytes +
289 receive_counters_.transmitted.header_bytes +
290 receive_counters_.transmitted.padding_bytes;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000291 }
292 if (packets_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000293 *packets_received = receive_counters_.transmitted.packets;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000294 }
295}
296
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000297void StreamStatisticianImpl::GetReceiveStreamDataCounters(
298 StreamDataCounters* data_counters) const {
danilchap7c9426c2016-04-14 03:05:31 -0700299 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000300 *data_counters = receive_counters_;
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000301}
302
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000303uint32_t StreamStatisticianImpl::BitrateReceived() const {
danilchap7c9426c2016-04-14 03:05:31 -0700304 rtc::CritScope cs(&stream_lock_);
sprangcd349d92016-07-13 09:11:28 -0700305 return incoming_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000306}
307
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000308bool StreamStatisticianImpl::IsRetransmitOfOldPacket(
Danil Chapovalov44727b42018-11-22 11:28:45 +0100309 const RtpPacketReceived& packet,
310 int64_t now_ms) const {
Niels Möllerdbb988b2018-11-15 08:05:16 +0100311 uint32_t frequency_khz = packet.payload_type_frequency() / 1000;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100312 RTC_DCHECK_GT(frequency_khz, 0);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000313
Danil Chapovalov44727b42018-11-22 11:28:45 +0100314 int64_t time_diff_ms = now_ms - last_receive_time_ms_;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000315
316 // Diff in time stamp since last received in order.
Niels Möllerdbb988b2018-11-15 08:05:16 +0100317 uint32_t timestamp_diff = packet.Timestamp() - last_received_timestamp_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000318 uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000319
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000320 int64_t max_delay_ms = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000321
Niels Möllereda00872018-05-23 13:54:51 +0200322 // Jitter standard deviation in samples.
323 float jitter_std = sqrt(static_cast<float>(jitter_q4_ >> 4));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000324
Niels Möllereda00872018-05-23 13:54:51 +0200325 // 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.org7bb8f022013-09-06 13:40:11 +0000332 }
333 return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms;
334}
335
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000336ReceiveStatistics* ReceiveStatistics::Create(Clock* clock) {
337 return new ReceiveStatisticsImpl(clock);
338}
339
340ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock)
341 : clock_(clock),
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100342 last_returned_ssrc_(0),
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000343 rtcp_stats_callback_(NULL),
344 rtp_stats_callback_(NULL) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000345
346ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
347 while (!statisticians_.empty()) {
348 delete statisticians_.begin()->second;
349 statisticians_.erase(statisticians_.begin());
350 }
351}
352
Niels Möller1f3206c2018-09-14 08:26:32 +0200353void ReceiveStatisticsImpl::OnRtpPacket(const RtpPacketReceived& packet) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000354 StreamStatisticianImpl* impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000355 {
danilchap7c9426c2016-04-14 03:05:31 -0700356 rtc::CritScope cs(&receive_statistics_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +0100357 auto it = statisticians_.find(packet.Ssrc());
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000358 if (it != statisticians_.end()) {
359 impl = it->second;
360 } else {
Niels Möller5304a322018-08-27 13:27:05 +0200361 impl = new StreamStatisticianImpl(
Niels Möllerdbb988b2018-11-15 08:05:16 +0100362 packet.Ssrc(), clock_, /* enable_retransmit_detection = */ false,
363 this, this);
364 statisticians_[packet.Ssrc()] = impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000365 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000366 }
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000367 // StreamStatisticianImpl instance is created once and only destroyed when
368 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
369 // it's own locking so don't hold receive_statistics_lock_ (potential
370 // deadlock).
Niels Möllerdbb988b2018-11-15 08:05:16 +0100371 impl->OnRtpPacket(packet);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000372}
373
Niels Möller1f3206c2018-09-14 08:26:32 +0200374void ReceiveStatisticsImpl::FecPacketReceived(const RtpPacketReceived& packet) {
danilchapec86be02017-08-14 05:51:02 -0700375 StreamStatisticianImpl* impl;
376 {
377 rtc::CritScope cs(&receive_statistics_lock_);
Niels Möller1f3206c2018-09-14 08:26:32 +0200378 auto it = statisticians_.find(packet.Ssrc());
danilchapec86be02017-08-14 05:51:02 -0700379 // Ignore FEC if it is the first packet.
380 if (it == statisticians_.end())
381 return;
382 impl = it->second;
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000383 }
Niels Möllerdbb988b2018-11-15 08:05:16 +0100384 impl->FecPacketReceived(packet);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000385}
386
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000387StreamStatistician* ReceiveStatisticsImpl::GetStatistician(
388 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700389 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200390 auto it = statisticians_.find(ssrc);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000391 if (it == statisticians_.end())
392 return NULL;
393 return it->second;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000394}
395
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000396void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
397 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700398 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200399 for (auto& statistician : statisticians_) {
400 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000401 }
402}
403
Niels Möller5304a322018-08-27 13:27:05 +0200404void ReceiveStatisticsImpl::EnableRetransmitDetection(uint32_t ssrc,
405 bool enable) {
406 StreamStatisticianImpl* impl;
407 {
408 rtc::CritScope cs(&receive_statistics_lock_);
409 StreamStatisticianImpl*& impl_ref = statisticians_[ssrc];
410 if (impl_ref == nullptr) { // new element
411 impl_ref = new StreamStatisticianImpl(ssrc, clock_, enable, this, this);
412 return;
413 }
414 impl = impl_ref;
415 }
416 impl->EnableRetransmitDetection(enable);
417}
418
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000419void ReceiveStatisticsImpl::RegisterRtcpStatisticsCallback(
420 RtcpStatisticsCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700421 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000422 if (callback != NULL)
423 assert(rtcp_stats_callback_ == NULL);
424 rtcp_stats_callback_ = callback;
425}
426
427void ReceiveStatisticsImpl::StatisticsUpdated(const RtcpStatistics& statistics,
428 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700429 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000430 if (rtcp_stats_callback_)
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000431 rtcp_stats_callback_->StatisticsUpdated(statistics, ssrc);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000432}
433
434void ReceiveStatisticsImpl::CNameChanged(const char* cname, uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700435 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000436 if (rtcp_stats_callback_)
437 rtcp_stats_callback_->CNameChanged(cname, ssrc);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000438}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000439
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000440void ReceiveStatisticsImpl::RegisterRtpStatisticsCallback(
441 StreamDataCountersCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700442 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000443 if (callback != NULL)
444 assert(rtp_stats_callback_ == NULL);
445 rtp_stats_callback_ = callback;
446}
447
448void ReceiveStatisticsImpl::DataCountersUpdated(const StreamDataCounters& stats,
449 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700450 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000451 if (rtp_stats_callback_) {
452 rtp_stats_callback_->DataCountersUpdated(stats, ssrc);
453 }
454}
455
danilchap0bc84232017-08-11 08:12:54 -0700456std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700457 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200458 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
459 {
460 rtc::CritScope cs(&receive_statistics_lock_);
461 statisticians = statisticians_;
462 }
danilchapf5f793c2017-07-27 04:44:18 -0700463 std::vector<rtcp::ReportBlock> result;
464 result.reserve(std::min(max_blocks, statisticians.size()));
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100465 auto add_report_block = [&result](uint32_t media_ssrc,
466 StreamStatisticianImpl* statistician) {
danilchapf5f793c2017-07-27 04:44:18 -0700467 // Do we have receive statistics to send?
468 RtcpStatistics stats;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100469 if (!statistician->GetActiveStatisticsAndReset(&stats))
470 return;
danilchapf5f793c2017-07-27 04:44:18 -0700471 result.emplace_back();
472 rtcp::ReportBlock& block = result.back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100473 block.SetMediaSsrc(media_ssrc);
danilchapf5f793c2017-07-27 04:44:18 -0700474 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700475 if (!block.SetCumulativeLost(stats.packets_lost)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100476 RTC_LOG(LS_WARNING) << "Cumulative lost is oversized.";
danilchapf5f793c2017-07-27 04:44:18 -0700477 result.pop_back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100478 return;
danilchapf5f793c2017-07-27 04:44:18 -0700479 }
srte186d9c32017-08-04 05:03:53 -0700480 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700481 block.SetJitter(stats.jitter);
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100482 };
483
484 const auto start_it = statisticians.upper_bound(last_returned_ssrc_);
485 for (auto it = start_it;
486 result.size() < max_blocks && it != statisticians.end(); ++it)
487 add_report_block(it->first, it->second);
488 for (auto it = statisticians.begin();
489 result.size() < max_blocks && it != start_it; ++it)
490 add_report_block(it->first, it->second);
491
492 if (!result.empty())
493 last_returned_ssrc_ = result.back().source_ssrc();
danilchapf5f793c2017-07-27 04:44:18 -0700494 return result;
495}
496
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000497} // namespace webrtc