blob: 1302dac96982ef66df07b712320886e8dc650118 [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
Oleh Prypin19929582019-04-23 08:50:04 +020013#include <cmath>
kwibergfd8be342016-05-14 19:44:11 -070014#include <cstdlib>
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +010015#include <memory>
danilchapf5f793c2017-07-27 04:44:18 -070016#include <vector>
kwibergfd8be342016-05-14 19:44:11 -070017
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +010018#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/remote_bitrate_estimator/test/bwe_test_logging.h"
Niels Möller1f3206c2018-09-14 08:26:32 +020020#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
22#include "modules/rtp_rtcp/source/time_util.h"
23#include "rtc_base/logging.h"
24#include "system_wrappers/include/clock.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000025
26namespace webrtc {
27
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000028const int64_t kStatisticsTimeoutMs = 8000;
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000029const int64_t kStatisticsProcessIntervalMs = 1000;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000030
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000031StreamStatistician::~StreamStatistician() {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000032
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000033StreamStatisticianImpl::StreamStatisticianImpl(
danilchapec86be02017-08-14 05:51:02 -070034 uint32_t ssrc,
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000035 Clock* clock,
Danil Chapovalovebb50c22018-11-22 14:04:02 +010036 int max_reordering_threshold,
sprang@webrtc.org0e932572014-01-23 10:00:39 +000037 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),
Danil Chapovalovebb50c22018-11-22 14:04:02 +010042 max_reordering_threshold_(max_reordering_threshold),
Niels Möller87da1092019-05-24 14:04:28 +020043 enable_retransmit_detection_(false),
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),
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000049 received_seq_max_(-1),
Qingsi Wang2370b082018-08-21 14:24:26 -070050 last_report_inorder_packets_(0),
51 last_report_old_packets_(0),
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000052 last_report_seq_max_(-1),
sprang@webrtc.org0e932572014-01-23 10:00:39 +000053 rtp_callback_(rtp_callback) {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000054
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010055StreamStatisticianImpl::~StreamStatisticianImpl() = default;
56
Niels Möllerdbb988b2018-11-15 08:05:16 +010057void StreamStatisticianImpl::OnRtpPacket(const RtpPacketReceived& packet) {
Danil Chapovalov44727b42018-11-22 11:28:45 +010058 StreamDataCounters counters = UpdateCounters(packet);
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +010059 if (rtp_callback_)
60 rtp_callback_->DataCountersUpdated(counters, ssrc_);
sprang@webrtc.orga45cac02014-01-27 16:22:08 +000061}
62
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000063bool StreamStatisticianImpl::UpdateOutOfOrder(const RtpPacketReceived& packet,
64 int64_t sequence_number,
65 int64_t now_ms) {
66 RTC_DCHECK_EQ(sequence_number,
67 seq_unwrapper_.UnwrapWithoutUpdate(packet.SequenceNumber()));
68
69 // Check if |packet| is second packet of a stream restart.
70 if (received_seq_out_of_order_) {
71 uint16_t expected_sequence_number = *received_seq_out_of_order_ + 1;
72 received_seq_out_of_order_ = absl::nullopt;
73 if (packet.SequenceNumber() == expected_sequence_number) {
74 // Ignore sequence number gap caused by stream restart for next packet
75 // loss calculation.
76 last_report_seq_max_ = sequence_number;
77 last_report_inorder_packets_ = receive_counters_.transmitted.packets -
78 receive_counters_.retransmitted.packets;
79 // As final part of stream restart consider |packet| is not out of order.
80 return false;
81 }
82 }
83
84 if (std::abs(sequence_number - received_seq_max_) >
85 max_reordering_threshold_) {
86 // Sequence number gap looks too large, wait until next packet to check
87 // for a stream restart.
88 received_seq_out_of_order_ = packet.SequenceNumber();
89 return true;
90 }
91
92 if (sequence_number > received_seq_max_)
93 return false;
94
95 // Old out of order packet, may be retransmit.
96 if (enable_retransmit_detection_ && IsRetransmitOfOldPacket(packet, now_ms))
97 receive_counters_.retransmitted.AddPacket(packet);
98 return true;
99}
100
danilchapec86be02017-08-14 05:51:02 -0700101StreamDataCounters StreamStatisticianImpl::UpdateCounters(
Danil Chapovalov44727b42018-11-22 11:28:45 +0100102 const RtpPacketReceived& packet) {
103 rtc::CritScope cs(&stream_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +0100104 RTC_DCHECK_EQ(ssrc_, packet.Ssrc());
Danil Chapovalov44727b42018-11-22 11:28:45 +0100105 int64_t now_ms = clock_->TimeInMilliseconds();
106
107 incoming_bitrate_.Update(packet.size(), now_ms);
Henrik Boströmcb755b02019-04-02 15:11:48 +0200108 receive_counters_.last_packet_received_timestamp_ms = now_ms;
Niels Möllerdbb988b2018-11-15 08:05:16 +0100109 receive_counters_.transmitted.AddPacket(packet);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000110
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000111 int64_t sequence_number =
112 seq_unwrapper_.UnwrapWithoutUpdate(packet.SequenceNumber());
113 if (!ReceivedRtpPacket()) {
114 received_seq_first_ = sequence_number;
115 last_report_seq_max_ = sequence_number - 1;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100116 receive_counters_.first_packet_time_ms = now_ms;
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000117 } else if (UpdateOutOfOrder(packet, sequence_number, now_ms)) {
118 return receive_counters_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000119 }
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000120 // In order packet.
121 received_seq_max_ = sequence_number;
122 seq_unwrapper_.UpdateLast(sequence_number);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000123
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000124 // If new time stamp and more than one in-order packet received, calculate
125 // new jitter statistics.
126 if (packet.Timestamp() != last_received_timestamp_ &&
127 (receive_counters_.transmitted.packets -
128 receive_counters_.retransmitted.packets) > 1) {
129 UpdateJitter(packet, now_ms);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000130 }
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000131 last_received_timestamp_ = packet.Timestamp();
132 last_receive_time_ms_ = now_ms;
danilchapec86be02017-08-14 05:51:02 -0700133 return receive_counters_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000134}
135
Niels Möllerdbb988b2018-11-15 08:05:16 +0100136void StreamStatisticianImpl::UpdateJitter(const RtpPacketReceived& packet,
Danil Chapovalov856cf222018-11-26 10:20:01 +0100137 int64_t receive_time_ms) {
138 int64_t receive_diff_ms = receive_time_ms - last_receive_time_ms_;
139 RTC_DCHECK_GE(receive_diff_ms, 0);
140 uint32_t receive_diff_rtp = static_cast<uint32_t>(
141 (receive_diff_ms * packet.payload_type_frequency()) / 1000);
142 int32_t time_diff_samples =
143 receive_diff_rtp - (packet.Timestamp() - last_received_timestamp_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000144
kwibergfd8be342016-05-14 19:44:11 -0700145 time_diff_samples = std::abs(time_diff_samples);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000146
147 // lib_jingle sometimes deliver crazy jumps in TS for the same stream.
148 // If this happens, don't update jitter value. Use 5 secs video frequency
149 // as the threshold.
150 if (time_diff_samples < 450000) {
151 // Note we calculate in Q4 to avoid using float.
152 int32_t jitter_diff_q4 = (time_diff_samples << 4) - jitter_q4_;
153 jitter_q4_ += ((jitter_diff_q4 + 8) >> 4);
154 }
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000155}
156
Niels Möllerdbb988b2018-11-15 08:05:16 +0100157void StreamStatisticianImpl::FecPacketReceived(
158 const RtpPacketReceived& packet) {
danilchapec86be02017-08-14 05:51:02 -0700159 StreamDataCounters counters;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000160 {
danilchap7c9426c2016-04-14 03:05:31 -0700161 rtc::CritScope cs(&stream_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +0100162 receive_counters_.fec.AddPacket(packet);
danilchapec86be02017-08-14 05:51:02 -0700163 counters = receive_counters_;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000164 }
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100165 if (rtp_callback_)
166 rtp_callback_->DataCountersUpdated(counters, ssrc_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000167}
168
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000169void StreamStatisticianImpl::SetMaxReorderingThreshold(
170 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700171 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000172 max_reordering_threshold_ = max_reordering_threshold;
173}
174
Niels Möller5304a322018-08-27 13:27:05 +0200175void StreamStatisticianImpl::EnableRetransmitDetection(bool enable) {
176 rtc::CritScope cs(&stream_lock_);
177 enable_retransmit_detection_ = enable;
178}
179
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000180bool StreamStatisticianImpl::GetStatistics(RtcpStatistics* statistics,
Qingsi Wang2370b082018-08-21 14:24:26 -0700181 bool reset) {
Niels Möller12ebfa62019-08-06 16:04:12 +0200182 rtc::CritScope cs(&stream_lock_);
183 if (!ReceivedRtpPacket()) {
184 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000185 }
186
Niels Möller12ebfa62019-08-06 16:04:12 +0200187 if (!reset) {
188 if (last_report_inorder_packets_ == 0) {
189 // No report.
190 return false;
191 }
192 // Just get last report.
193 *statistics = last_reported_statistics_;
194 return true;
195 }
196
197 *statistics = CalculateRtcpStatistics();
198
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000199 return true;
200}
201
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200202bool StreamStatisticianImpl::GetActiveStatisticsAndReset(
203 RtcpStatistics* statistics) {
Niels Möller12ebfa62019-08-06 16:04:12 +0200204 rtc::CritScope cs(&stream_lock_);
205 if (clock_->TimeInMilliseconds() - last_receive_time_ms_ >=
206 kStatisticsTimeoutMs) {
207 // Not active.
208 return false;
209 }
210 if (!ReceivedRtpPacket()) {
211 return false;
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200212 }
213
Niels Möller12ebfa62019-08-06 16:04:12 +0200214 *statistics = CalculateRtcpStatistics();
215
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200216 return true;
217}
218
Qingsi Wang2370b082018-08-21 14:24:26 -0700219RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics() {
220 RtcpStatistics stats;
Qingsi Wang2370b082018-08-21 14:24:26 -0700221 // Calculate fraction lost.
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000222 int64_t exp_since_last = received_seq_max_ - last_report_seq_max_;
223 RTC_DCHECK_GE(exp_since_last, 0);
Qingsi Wang2370b082018-08-21 14:24:26 -0700224
225 // Number of received RTP packets since last report, counts all packets but
226 // not re-transmissions.
227 uint32_t rec_since_last = (receive_counters_.transmitted.packets -
228 receive_counters_.retransmitted.packets) -
229 last_report_inorder_packets_;
230
231 // With NACK we don't know the expected retransmissions during the last
232 // second. We know how many "old" packets we have received. We just count
233 // the number of old received to estimate the loss, but it still does not
234 // guarantee an exact number since we run this based on time triggered by
235 // sending of an RTP packet. This should have a minimum effect.
236
237 // With NACK we don't count old packets as received since they are
238 // re-transmitted. We use RTT to decide if a packet is re-ordered or
239 // re-transmitted.
240 uint32_t retransmitted_packets =
241 receive_counters_.retransmitted.packets - last_report_old_packets_;
242 rec_since_last += retransmitted_packets;
243
244 int32_t missing = 0;
245 if (exp_since_last > rec_since_last) {
246 missing = (exp_since_last - rec_since_last);
247 }
248 uint8_t local_fraction_lost = 0;
249 if (exp_since_last) {
250 // Scale 0 to 255, where 255 is 100% loss.
251 local_fraction_lost = static_cast<uint8_t>(255 * missing / exp_since_last);
252 }
253 stats.fraction_lost = local_fraction_lost;
254
255 // We need a counter for cumulative loss too.
256 // TODO(danilchap): Ensure cumulative loss is below maximum value of 2^24.
257 cumulative_loss_ += missing;
258 stats.packets_lost = cumulative_loss_;
259 stats.extended_highest_sequence_number =
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000260 static_cast<uint32_t>(received_seq_max_);
Qingsi Wang2370b082018-08-21 14:24:26 -0700261 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
262 stats.jitter = jitter_q4_ >> 4;
263
264 // Store this report.
265 last_reported_statistics_ = stats;
266
267 // Only for report blocks in RTCP SR and RR.
268 last_report_inorder_packets_ = receive_counters_.transmitted.packets -
269 receive_counters_.retransmitted.packets;
270 last_report_old_packets_ = receive_counters_.retransmitted.packets;
271 last_report_seq_max_ = received_seq_max_;
gaetano.carlucci61050f62016-09-30 06:29:54 -0700272 BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts",
gaetano.carlucci52a57032016-09-14 05:04:36 -0700273 clock_->TimeInMilliseconds(),
Qingsi Wang2370b082018-08-21 14:24:26 -0700274 cumulative_loss_, ssrc_);
gaetano.carlucci52a57032016-09-14 05:04:36 -0700275 BWE_TEST_LOGGING_PLOT_WITH_SSRC(
gaetano.carlucci61050f62016-09-30 06:29:54 -0700276 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700277 (received_seq_max_ - received_seq_first_), ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000278
Qingsi Wang2370b082018-08-21 14:24:26 -0700279 return stats;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000280}
281
Yves Gerey665174f2018-06-19 15:03:05 +0200282void StreamStatisticianImpl::GetDataCounters(size_t* bytes_received,
283 uint32_t* packets_received) const {
danilchap7c9426c2016-04-14 03:05:31 -0700284 rtc::CritScope cs(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000285 if (bytes_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000286 *bytes_received = receive_counters_.transmitted.payload_bytes +
287 receive_counters_.transmitted.header_bytes +
288 receive_counters_.transmitted.padding_bytes;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000289 }
290 if (packets_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000291 *packets_received = receive_counters_.transmitted.packets;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000292 }
293}
294
Niels Möller9a9f18a2019-08-02 13:52:37 +0200295absl::optional<int> StreamStatisticianImpl::GetFractionLostInPercent() const {
296 rtc::CritScope cs(&stream_lock_);
297 if (received_seq_max_ < 0) {
298 return absl::nullopt;
299 }
300 int64_t expected_packets = 1 + received_seq_max_ - received_seq_first_;
301 if (expected_packets <= 0) {
302 return absl::nullopt;
303 }
304 // Spec allows negative cumulative loss, but implementation uses uint32_t, so
305 // this expression is always non-negative.
306 return 100 * static_cast<int64_t>(cumulative_loss_) / expected_packets;
307}
308
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000309void StreamStatisticianImpl::GetReceiveStreamDataCounters(
310 StreamDataCounters* data_counters) const {
danilchap7c9426c2016-04-14 03:05:31 -0700311 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000312 *data_counters = receive_counters_;
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000313}
314
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000315uint32_t StreamStatisticianImpl::BitrateReceived() const {
danilchap7c9426c2016-04-14 03:05:31 -0700316 rtc::CritScope cs(&stream_lock_);
sprangcd349d92016-07-13 09:11:28 -0700317 return incoming_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000318}
319
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000320bool StreamStatisticianImpl::IsRetransmitOfOldPacket(
Danil Chapovalov44727b42018-11-22 11:28:45 +0100321 const RtpPacketReceived& packet,
322 int64_t now_ms) const {
Niels Möllerdbb988b2018-11-15 08:05:16 +0100323 uint32_t frequency_khz = packet.payload_type_frequency() / 1000;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100324 RTC_DCHECK_GT(frequency_khz, 0);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000325
Danil Chapovalov44727b42018-11-22 11:28:45 +0100326 int64_t time_diff_ms = now_ms - last_receive_time_ms_;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000327
328 // Diff in time stamp since last received in order.
Niels Möllerdbb988b2018-11-15 08:05:16 +0100329 uint32_t timestamp_diff = packet.Timestamp() - last_received_timestamp_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000330 uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000331
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000332 int64_t max_delay_ms = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000333
Niels Möllereda00872018-05-23 13:54:51 +0200334 // Jitter standard deviation in samples.
Oleh Prypin19929582019-04-23 08:50:04 +0200335 float jitter_std = std::sqrt(static_cast<float>(jitter_q4_ >> 4));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000336
Niels Möllereda00872018-05-23 13:54:51 +0200337 // 2 times the standard deviation => 95% confidence.
338 // And transform to milliseconds by dividing by the frequency in kHz.
339 max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz);
340
341 // Min max_delay_ms is 1.
342 if (max_delay_ms == 0) {
343 max_delay_ms = 1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000344 }
345 return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms;
346}
347
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100348std::unique_ptr<ReceiveStatistics> ReceiveStatistics::Create(
349 Clock* clock,
Niels Möller12ebfa62019-08-06 16:04:12 +0200350 StreamDataCountersCallback* rtp_callback) {
351 return absl::make_unique<ReceiveStatisticsImpl>(clock, rtp_callback);
352}
353
354std::unique_ptr<ReceiveStatistics> ReceiveStatistics::Create(
355 Clock* clock,
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100356 RtcpStatisticsCallback* rtcp_callback,
357 StreamDataCountersCallback* rtp_callback) {
Niels Möller12ebfa62019-08-06 16:04:12 +0200358 RTC_CHECK(rtcp_callback == nullptr);
359 return Create(clock, rtp_callback);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000360}
361
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100362ReceiveStatisticsImpl::ReceiveStatisticsImpl(
363 Clock* clock,
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100364 StreamDataCountersCallback* rtp_callback)
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000365 : clock_(clock),
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100366 last_returned_ssrc_(0),
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100367 max_reordering_threshold_(kDefaultMaxReorderingThreshold),
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100368 rtp_stats_callback_(rtp_callback) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000369
370ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
371 while (!statisticians_.empty()) {
372 delete statisticians_.begin()->second;
373 statisticians_.erase(statisticians_.begin());
374 }
375}
376
Niels Möller1f3206c2018-09-14 08:26:32 +0200377void ReceiveStatisticsImpl::OnRtpPacket(const RtpPacketReceived& packet) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000378 // StreamStatisticianImpl instance is created once and only destroyed when
379 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
380 // it's own locking so don't hold receive_statistics_lock_ (potential
381 // deadlock).
Niels Möller87da1092019-05-24 14:04:28 +0200382 GetOrCreateStatistician(packet.Ssrc())->OnRtpPacket(packet);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000383}
384
Niels Möller1f3206c2018-09-14 08:26:32 +0200385void ReceiveStatisticsImpl::FecPacketReceived(const RtpPacketReceived& packet) {
Niels Möller87da1092019-05-24 14:04:28 +0200386 StreamStatisticianImpl* impl = GetStatistician(packet.Ssrc());
387 // Ignore FEC if it is the first packet.
388 if (impl) {
389 impl->FecPacketReceived(packet);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000390 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000391}
392
Niels Möller87da1092019-05-24 14:04:28 +0200393StreamStatisticianImpl* ReceiveStatisticsImpl::GetStatistician(
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000394 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700395 rtc::CritScope cs(&receive_statistics_lock_);
Niels Möller87da1092019-05-24 14:04:28 +0200396 const auto& it = statisticians_.find(ssrc);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000397 if (it == statisticians_.end())
398 return NULL;
399 return it->second;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000400}
401
Niels Möller87da1092019-05-24 14:04:28 +0200402StreamStatisticianImpl* ReceiveStatisticsImpl::GetOrCreateStatistician(
403 uint32_t ssrc) {
404 rtc::CritScope cs(&receive_statistics_lock_);
405 StreamStatisticianImpl*& impl = statisticians_[ssrc];
406 if (impl == nullptr) { // new element
Niels Möller12ebfa62019-08-06 16:04:12 +0200407 impl = new StreamStatisticianImpl(ssrc, clock_, max_reordering_threshold_,
408 rtp_stats_callback_);
Niels Möller87da1092019-05-24 14:04:28 +0200409 }
410 return impl;
411}
412
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000413void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
414 int max_reordering_threshold) {
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100415 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
416 {
417 rtc::CritScope cs(&receive_statistics_lock_);
418 max_reordering_threshold_ = max_reordering_threshold;
419 statisticians = statisticians_;
420 }
421 for (auto& statistician : statisticians) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200422 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000423 }
424}
425
Niels Möller87da1092019-05-24 14:04:28 +0200426void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
427 uint32_t ssrc,
428 int max_reordering_threshold) {
429 GetOrCreateStatistician(ssrc)->SetMaxReorderingThreshold(
430 max_reordering_threshold);
431}
432
Niels Möller5304a322018-08-27 13:27:05 +0200433void ReceiveStatisticsImpl::EnableRetransmitDetection(uint32_t ssrc,
434 bool enable) {
Niels Möller87da1092019-05-24 14:04:28 +0200435 GetOrCreateStatistician(ssrc)->EnableRetransmitDetection(enable);
Niels Möller5304a322018-08-27 13:27:05 +0200436}
437
danilchap0bc84232017-08-11 08:12:54 -0700438std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700439 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200440 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
441 {
442 rtc::CritScope cs(&receive_statistics_lock_);
443 statisticians = statisticians_;
444 }
danilchapf5f793c2017-07-27 04:44:18 -0700445 std::vector<rtcp::ReportBlock> result;
446 result.reserve(std::min(max_blocks, statisticians.size()));
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100447 auto add_report_block = [&result](uint32_t media_ssrc,
448 StreamStatisticianImpl* statistician) {
danilchapf5f793c2017-07-27 04:44:18 -0700449 // Do we have receive statistics to send?
450 RtcpStatistics stats;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100451 if (!statistician->GetActiveStatisticsAndReset(&stats))
452 return;
danilchapf5f793c2017-07-27 04:44:18 -0700453 result.emplace_back();
454 rtcp::ReportBlock& block = result.back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100455 block.SetMediaSsrc(media_ssrc);
danilchapf5f793c2017-07-27 04:44:18 -0700456 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700457 if (!block.SetCumulativeLost(stats.packets_lost)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100458 RTC_LOG(LS_WARNING) << "Cumulative lost is oversized.";
danilchapf5f793c2017-07-27 04:44:18 -0700459 result.pop_back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100460 return;
danilchapf5f793c2017-07-27 04:44:18 -0700461 }
srte186d9c32017-08-04 05:03:53 -0700462 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700463 block.SetJitter(stats.jitter);
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100464 };
465
466 const auto start_it = statisticians.upper_bound(last_returned_ssrc_);
467 for (auto it = start_it;
468 result.size() < max_blocks && it != statisticians.end(); ++it)
469 add_report_block(it->first, it->second);
470 for (auto it = statisticians.begin();
471 result.size() < max_blocks && it != start_it; ++it)
472 add_report_block(it->first, it->second);
473
474 if (!result.empty())
475 last_returned_ssrc_ = result.back().source_ssrc();
danilchapf5f793c2017-07-27 04:44:18 -0700476 return result;
477}
478
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000479} // namespace webrtc