blob: 0869d88ebb43c917c4b52e631874181c05e54bc1 [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>
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,
Niels Möller5304a322018-08-27 13:27:05 +020036 bool enable_retransmit_detection,
Danil Chapovalovebb50c22018-11-22 14:04:02 +010037 int max_reordering_threshold,
sprang@webrtc.org0e932572014-01-23 10:00:39 +000038 RtcpStatisticsCallback* rtcp_callback,
39 StreamDataCountersCallback* rtp_callback)
danilchapec86be02017-08-14 05:51:02 -070040 : ssrc_(ssrc),
41 clock_(clock),
sprangcd349d92016-07-13 09:11:28 -070042 incoming_bitrate_(kStatisticsProcessIntervalMs,
43 RateStatistics::kBpsScale),
Danil Chapovalovebb50c22018-11-22 14:04:02 +010044 max_reordering_threshold_(max_reordering_threshold),
Niels Möller5304a322018-08-27 13:27:05 +020045 enable_retransmit_detection_(enable_retransmit_detection),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000046 jitter_q4_(0),
Qingsi Wang2370b082018-08-21 14:24:26 -070047 cumulative_loss_(0),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000048 last_receive_time_ms_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000049 last_received_timestamp_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000050 received_seq_first_(0),
51 received_seq_max_(0),
52 received_seq_wraps_(0),
Qingsi Wang2370b082018-08-21 14:24:26 -070053 last_report_inorder_packets_(0),
54 last_report_old_packets_(0),
55 last_report_seq_max_(0),
sprang@webrtc.org0e932572014-01-23 10:00:39 +000056 rtcp_callback_(rtcp_callback),
57 rtp_callback_(rtp_callback) {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000058
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010059StreamStatisticianImpl::~StreamStatisticianImpl() = default;
60
Niels Möllerdbb988b2018-11-15 08:05:16 +010061void StreamStatisticianImpl::OnRtpPacket(const RtpPacketReceived& packet) {
Danil Chapovalov44727b42018-11-22 11:28:45 +010062 StreamDataCounters counters = UpdateCounters(packet);
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +010063 if (rtp_callback_)
64 rtp_callback_->DataCountersUpdated(counters, ssrc_);
sprang@webrtc.orga45cac02014-01-27 16:22:08 +000065}
66
danilchapec86be02017-08-14 05:51:02 -070067StreamDataCounters StreamStatisticianImpl::UpdateCounters(
Danil Chapovalov44727b42018-11-22 11:28:45 +010068 const RtpPacketReceived& packet) {
69 rtc::CritScope cs(&stream_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +010070 RTC_DCHECK_EQ(ssrc_, packet.Ssrc());
Danil Chapovalov44727b42018-11-22 11:28:45 +010071 uint16_t sequence_number = packet.SequenceNumber();
72 bool in_order =
73 // First packet is always in order.
74 last_receive_time_ms_ == 0 ||
75 IsNewerSequenceNumber(sequence_number, received_seq_max_) ||
76 // If we have a restart of the remote side this packet is still in order.
77 !IsNewerSequenceNumber(sequence_number,
78 received_seq_max_ - max_reordering_threshold_);
79 int64_t now_ms = clock_->TimeInMilliseconds();
80
81 incoming_bitrate_.Update(packet.size(), now_ms);
Niels Möllerdbb988b2018-11-15 08:05:16 +010082 receive_counters_.transmitted.AddPacket(packet);
Danil Chapovalov44727b42018-11-22 11:28:45 +010083 if (!in_order && enable_retransmit_detection_ &&
84 IsRetransmitOfOldPacket(packet, now_ms)) {
Niels Möllerdbb988b2018-11-15 08:05:16 +010085 receive_counters_.retransmitted.AddPacket(packet);
sprang@webrtc.org0e932572014-01-23 10:00:39 +000086 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +000087
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +000088 if (receive_counters_.transmitted.packets == 1) {
Niels Möllerdbb988b2018-11-15 08:05:16 +010089 received_seq_first_ = packet.SequenceNumber();
Danil Chapovalov44727b42018-11-22 11:28:45 +010090 receive_counters_.first_packet_time_ms = now_ms;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000091 }
92
93 // Count only the new packets received. That is, if packets 1, 2, 3, 5, 4, 6
94 // are received, 4 will be ignored.
95 if (in_order) {
96 // Current time in samples.
danilchap37953762017-02-09 11:15:25 -080097 NtpTime receive_time = clock_->CurrentNtpTime();
wu@webrtc.org822fbd82013-08-15 23:38:54 +000098
99 // Wrong if we use RetransmitOfOldPacket.
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000100 if (receive_counters_.transmitted.packets > 1 &&
Niels Möllerdbb988b2018-11-15 08:05:16 +0100101 received_seq_max_ > packet.SequenceNumber()) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000102 // Wrap around detected.
103 received_seq_wraps_++;
104 }
105 // New max.
Niels Möllerdbb988b2018-11-15 08:05:16 +0100106 received_seq_max_ = packet.SequenceNumber();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000107
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000108 // If new time stamp and more than one in-order packet received, calculate
109 // new jitter statistics.
Niels Möllerdbb988b2018-11-15 08:05:16 +0100110 if (packet.Timestamp() != last_received_timestamp_ &&
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000111 (receive_counters_.transmitted.packets -
112 receive_counters_.retransmitted.packets) > 1) {
Niels Möllerdbb988b2018-11-15 08:05:16 +0100113 UpdateJitter(packet, receive_time);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000114 }
Niels Möllerdbb988b2018-11-15 08:05:16 +0100115 last_received_timestamp_ = packet.Timestamp();
danilchap1227e8b2015-12-21 11:06:50 -0800116 last_receive_time_ntp_ = receive_time;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100117 last_receive_time_ms_ = now_ms;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000118 }
danilchapec86be02017-08-14 05:51:02 -0700119 return receive_counters_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000120}
121
Niels Möllerdbb988b2018-11-15 08:05:16 +0100122void StreamStatisticianImpl::UpdateJitter(const RtpPacketReceived& packet,
danilchap1227e8b2015-12-21 11:06:50 -0800123 NtpTime receive_time) {
124 uint32_t receive_time_rtp =
Niels Möllerdbb988b2018-11-15 08:05:16 +0100125 NtpToRtp(receive_time, packet.payload_type_frequency());
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000126 uint32_t last_receive_time_rtp =
Niels Möllerdbb988b2018-11-15 08:05:16 +0100127 NtpToRtp(last_receive_time_ntp_, packet.payload_type_frequency());
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000128 int32_t time_diff_samples = (receive_time_rtp - last_receive_time_rtp) -
Niels Möllerdbb988b2018-11-15 08:05:16 +0100129 (packet.Timestamp() - last_received_timestamp_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000130
kwibergfd8be342016-05-14 19:44:11 -0700131 time_diff_samples = std::abs(time_diff_samples);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000132
133 // lib_jingle sometimes deliver crazy jumps in TS for the same stream.
134 // If this happens, don't update jitter value. Use 5 secs video frequency
135 // as the threshold.
136 if (time_diff_samples < 450000) {
137 // Note we calculate in Q4 to avoid using float.
138 int32_t jitter_diff_q4 = (time_diff_samples << 4) - jitter_q4_;
139 jitter_q4_ += ((jitter_diff_q4 + 8) >> 4);
140 }
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000141}
142
Niels Möllerdbb988b2018-11-15 08:05:16 +0100143void StreamStatisticianImpl::FecPacketReceived(
144 const RtpPacketReceived& packet) {
danilchapec86be02017-08-14 05:51:02 -0700145 StreamDataCounters counters;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000146 {
danilchap7c9426c2016-04-14 03:05:31 -0700147 rtc::CritScope cs(&stream_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +0100148 receive_counters_.fec.AddPacket(packet);
danilchapec86be02017-08-14 05:51:02 -0700149 counters = receive_counters_;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000150 }
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100151 if (rtp_callback_)
152 rtp_callback_->DataCountersUpdated(counters, ssrc_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000153}
154
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000155void StreamStatisticianImpl::SetMaxReorderingThreshold(
156 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700157 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000158 max_reordering_threshold_ = max_reordering_threshold;
159}
160
Niels Möller5304a322018-08-27 13:27:05 +0200161void StreamStatisticianImpl::EnableRetransmitDetection(bool enable) {
162 rtc::CritScope cs(&stream_lock_);
163 enable_retransmit_detection_ = enable;
164}
165
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000166bool StreamStatisticianImpl::GetStatistics(RtcpStatistics* statistics,
Qingsi Wang2370b082018-08-21 14:24:26 -0700167 bool reset) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000168 {
danilchap7c9426c2016-04-14 03:05:31 -0700169 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000170 if (received_seq_first_ == 0 &&
171 receive_counters_.transmitted.payload_bytes == 0) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000172 // We have not received anything.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000173 return false;
174 }
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000175
Qingsi Wang2370b082018-08-21 14:24:26 -0700176 if (!reset) {
177 if (last_report_inorder_packets_ == 0) {
178 // No report.
179 return false;
180 }
181 // Just get last report.
182 *statistics = last_reported_statistics_;
183 return true;
184 }
185
186 *statistics = CalculateRtcpStatistics();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000187 }
188
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100189 if (rtcp_callback_)
190 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000191 return true;
192}
193
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200194bool StreamStatisticianImpl::GetActiveStatisticsAndReset(
195 RtcpStatistics* statistics) {
196 {
197 rtc::CritScope cs(&stream_lock_);
198 if (clock_->CurrentNtpInMilliseconds() - last_receive_time_ntp_.ToMs() >=
199 kStatisticsTimeoutMs) {
200 // Not active.
201 return false;
202 }
203 if (received_seq_first_ == 0 &&
204 receive_counters_.transmitted.payload_bytes == 0) {
205 // We have not received anything.
206 return false;
207 }
208
Qingsi Wang2370b082018-08-21 14:24:26 -0700209 *statistics = CalculateRtcpStatistics();
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200210 }
211
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100212 if (rtcp_callback_)
213 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200214 return true;
215}
216
Qingsi Wang2370b082018-08-21 14:24:26 -0700217RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics() {
218 RtcpStatistics stats;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000219
Qingsi Wang2370b082018-08-21 14:24:26 -0700220 if (last_report_inorder_packets_ == 0) {
221 // First time we send a report.
222 last_report_seq_max_ = received_seq_first_ - 1;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000223 }
224
Qingsi Wang2370b082018-08-21 14:24:26 -0700225 // Calculate fraction lost.
226 uint16_t exp_since_last = (received_seq_max_ - last_report_seq_max_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000227
Qingsi Wang2370b082018-08-21 14:24:26 -0700228 if (last_report_seq_max_ > received_seq_max_) {
229 // Can we assume that the seq_num can't go decrease over a full RTCP period?
230 exp_since_last = 0;
231 }
232
233 // Number of received RTP packets since last report, counts all packets but
234 // not re-transmissions.
235 uint32_t rec_since_last = (receive_counters_.transmitted.packets -
236 receive_counters_.retransmitted.packets) -
237 last_report_inorder_packets_;
238
239 // With NACK we don't know the expected retransmissions during the last
240 // second. We know how many "old" packets we have received. We just count
241 // the number of old received to estimate the loss, but it still does not
242 // guarantee an exact number since we run this based on time triggered by
243 // sending of an RTP packet. This should have a minimum effect.
244
245 // With NACK we don't count old packets as received since they are
246 // re-transmitted. We use RTT to decide if a packet is re-ordered or
247 // re-transmitted.
248 uint32_t retransmitted_packets =
249 receive_counters_.retransmitted.packets - last_report_old_packets_;
250 rec_since_last += retransmitted_packets;
251
252 int32_t missing = 0;
253 if (exp_since_last > rec_since_last) {
254 missing = (exp_since_last - rec_since_last);
255 }
256 uint8_t local_fraction_lost = 0;
257 if (exp_since_last) {
258 // Scale 0 to 255, where 255 is 100% loss.
259 local_fraction_lost = static_cast<uint8_t>(255 * missing / exp_since_last);
260 }
261 stats.fraction_lost = local_fraction_lost;
262
263 // We need a counter for cumulative loss too.
264 // TODO(danilchap): Ensure cumulative loss is below maximum value of 2^24.
265 cumulative_loss_ += missing;
266 stats.packets_lost = cumulative_loss_;
267 stats.extended_highest_sequence_number =
268 (received_seq_wraps_ << 16) + received_seq_max_;
269 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
270 stats.jitter = jitter_q4_ >> 4;
271
272 // Store this report.
273 last_reported_statistics_ = stats;
274
275 // Only for report blocks in RTCP SR and RR.
276 last_report_inorder_packets_ = receive_counters_.transmitted.packets -
277 receive_counters_.retransmitted.packets;
278 last_report_old_packets_ = receive_counters_.retransmitted.packets;
279 last_report_seq_max_ = received_seq_max_;
gaetano.carlucci61050f62016-09-30 06:29:54 -0700280 BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts",
gaetano.carlucci52a57032016-09-14 05:04:36 -0700281 clock_->TimeInMilliseconds(),
Qingsi Wang2370b082018-08-21 14:24:26 -0700282 cumulative_loss_, ssrc_);
gaetano.carlucci52a57032016-09-14 05:04:36 -0700283 BWE_TEST_LOGGING_PLOT_WITH_SSRC(
gaetano.carlucci61050f62016-09-30 06:29:54 -0700284 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700285 (received_seq_max_ - received_seq_first_), ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000286
Qingsi Wang2370b082018-08-21 14:24:26 -0700287 return stats;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000288}
289
Yves Gerey665174f2018-06-19 15:03:05 +0200290void StreamStatisticianImpl::GetDataCounters(size_t* bytes_received,
291 uint32_t* packets_received) const {
danilchap7c9426c2016-04-14 03:05:31 -0700292 rtc::CritScope cs(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000293 if (bytes_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000294 *bytes_received = receive_counters_.transmitted.payload_bytes +
295 receive_counters_.transmitted.header_bytes +
296 receive_counters_.transmitted.padding_bytes;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000297 }
298 if (packets_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000299 *packets_received = receive_counters_.transmitted.packets;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000300 }
301}
302
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000303void StreamStatisticianImpl::GetReceiveStreamDataCounters(
304 StreamDataCounters* data_counters) const {
danilchap7c9426c2016-04-14 03:05:31 -0700305 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000306 *data_counters = receive_counters_;
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000307}
308
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000309uint32_t StreamStatisticianImpl::BitrateReceived() const {
danilchap7c9426c2016-04-14 03:05:31 -0700310 rtc::CritScope cs(&stream_lock_);
sprangcd349d92016-07-13 09:11:28 -0700311 return incoming_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000312}
313
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000314bool StreamStatisticianImpl::IsRetransmitOfOldPacket(
Danil Chapovalov44727b42018-11-22 11:28:45 +0100315 const RtpPacketReceived& packet,
316 int64_t now_ms) const {
Niels Möllerdbb988b2018-11-15 08:05:16 +0100317 uint32_t frequency_khz = packet.payload_type_frequency() / 1000;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100318 RTC_DCHECK_GT(frequency_khz, 0);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000319
Danil Chapovalov44727b42018-11-22 11:28:45 +0100320 int64_t time_diff_ms = now_ms - last_receive_time_ms_;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000321
322 // Diff in time stamp since last received in order.
Niels Möllerdbb988b2018-11-15 08:05:16 +0100323 uint32_t timestamp_diff = packet.Timestamp() - last_received_timestamp_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000324 uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000325
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000326 int64_t max_delay_ms = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000327
Niels Möllereda00872018-05-23 13:54:51 +0200328 // Jitter standard deviation in samples.
329 float jitter_std = sqrt(static_cast<float>(jitter_q4_ >> 4));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000330
Niels Möllereda00872018-05-23 13:54:51 +0200331 // 2 times the standard deviation => 95% confidence.
332 // And transform to milliseconds by dividing by the frequency in kHz.
333 max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz);
334
335 // Min max_delay_ms is 1.
336 if (max_delay_ms == 0) {
337 max_delay_ms = 1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000338 }
339 return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms;
340}
341
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100342std::unique_ptr<ReceiveStatistics> ReceiveStatistics::Create(
343 Clock* clock,
344 RtcpStatisticsCallback* rtcp_callback,
345 StreamDataCountersCallback* rtp_callback) {
346 return absl::make_unique<ReceiveStatisticsImpl>(clock, rtcp_callback,
347 rtp_callback);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000348}
349
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100350ReceiveStatisticsImpl::ReceiveStatisticsImpl(
351 Clock* clock,
352 RtcpStatisticsCallback* rtcp_callback,
353 StreamDataCountersCallback* rtp_callback)
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000354 : clock_(clock),
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100355 last_returned_ssrc_(0),
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100356 max_reordering_threshold_(kDefaultMaxReorderingThreshold),
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100357 rtcp_stats_callback_(rtcp_callback),
358 rtp_stats_callback_(rtp_callback) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000359
360ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
361 while (!statisticians_.empty()) {
362 delete statisticians_.begin()->second;
363 statisticians_.erase(statisticians_.begin());
364 }
365}
366
Niels Möller1f3206c2018-09-14 08:26:32 +0200367void ReceiveStatisticsImpl::OnRtpPacket(const RtpPacketReceived& packet) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000368 StreamStatisticianImpl* impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000369 {
danilchap7c9426c2016-04-14 03:05:31 -0700370 rtc::CritScope cs(&receive_statistics_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +0100371 auto it = statisticians_.find(packet.Ssrc());
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000372 if (it != statisticians_.end()) {
373 impl = it->second;
374 } else {
Niels Möller5304a322018-08-27 13:27:05 +0200375 impl = new StreamStatisticianImpl(
Niels Möllerdbb988b2018-11-15 08:05:16 +0100376 packet.Ssrc(), clock_, /* enable_retransmit_detection = */ false,
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100377 max_reordering_threshold_, rtcp_stats_callback_, rtp_stats_callback_);
Niels Möllerdbb988b2018-11-15 08:05:16 +0100378 statisticians_[packet.Ssrc()] = impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000379 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000380 }
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000381 // StreamStatisticianImpl instance is created once and only destroyed when
382 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
383 // it's own locking so don't hold receive_statistics_lock_ (potential
384 // deadlock).
Niels Möllerdbb988b2018-11-15 08:05:16 +0100385 impl->OnRtpPacket(packet);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000386}
387
Niels Möller1f3206c2018-09-14 08:26:32 +0200388void ReceiveStatisticsImpl::FecPacketReceived(const RtpPacketReceived& packet) {
danilchapec86be02017-08-14 05:51:02 -0700389 StreamStatisticianImpl* impl;
390 {
391 rtc::CritScope cs(&receive_statistics_lock_);
Niels Möller1f3206c2018-09-14 08:26:32 +0200392 auto it = statisticians_.find(packet.Ssrc());
danilchapec86be02017-08-14 05:51:02 -0700393 // Ignore FEC if it is the first packet.
394 if (it == statisticians_.end())
395 return;
396 impl = it->second;
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000397 }
Niels Möllerdbb988b2018-11-15 08:05:16 +0100398 impl->FecPacketReceived(packet);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000399}
400
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000401StreamStatistician* ReceiveStatisticsImpl::GetStatistician(
402 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700403 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200404 auto it = statisticians_.find(ssrc);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000405 if (it == statisticians_.end())
406 return NULL;
407 return it->second;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000408}
409
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000410void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
411 int max_reordering_threshold) {
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100412 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
413 {
414 rtc::CritScope cs(&receive_statistics_lock_);
415 max_reordering_threshold_ = max_reordering_threshold;
416 statisticians = statisticians_;
417 }
418 for (auto& statistician : statisticians) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200419 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000420 }
421}
422
Niels Möller5304a322018-08-27 13:27:05 +0200423void ReceiveStatisticsImpl::EnableRetransmitDetection(uint32_t ssrc,
424 bool enable) {
425 StreamStatisticianImpl* impl;
426 {
427 rtc::CritScope cs(&receive_statistics_lock_);
428 StreamStatisticianImpl*& impl_ref = statisticians_[ssrc];
429 if (impl_ref == nullptr) { // new element
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100430 impl_ref = new StreamStatisticianImpl(
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100431 ssrc, clock_, enable, max_reordering_threshold_, rtcp_stats_callback_,
432 rtp_stats_callback_);
Niels Möller5304a322018-08-27 13:27:05 +0200433 return;
434 }
435 impl = impl_ref;
436 }
437 impl->EnableRetransmitDetection(enable);
438}
439
danilchap0bc84232017-08-11 08:12:54 -0700440std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700441 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200442 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
443 {
444 rtc::CritScope cs(&receive_statistics_lock_);
445 statisticians = statisticians_;
446 }
danilchapf5f793c2017-07-27 04:44:18 -0700447 std::vector<rtcp::ReportBlock> result;
448 result.reserve(std::min(max_blocks, statisticians.size()));
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100449 auto add_report_block = [&result](uint32_t media_ssrc,
450 StreamStatisticianImpl* statistician) {
danilchapf5f793c2017-07-27 04:44:18 -0700451 // Do we have receive statistics to send?
452 RtcpStatistics stats;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100453 if (!statistician->GetActiveStatisticsAndReset(&stats))
454 return;
danilchapf5f793c2017-07-27 04:44:18 -0700455 result.emplace_back();
456 rtcp::ReportBlock& block = result.back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100457 block.SetMediaSsrc(media_ssrc);
danilchapf5f793c2017-07-27 04:44:18 -0700458 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700459 if (!block.SetCumulativeLost(stats.packets_lost)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100460 RTC_LOG(LS_WARNING) << "Cumulative lost is oversized.";
danilchapf5f793c2017-07-27 04:44:18 -0700461 result.pop_back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100462 return;
danilchapf5f793c2017-07-27 04:44:18 -0700463 }
srte186d9c32017-08-04 05:03:53 -0700464 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700465 block.SetJitter(stats.jitter);
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100466 };
467
468 const auto start_it = statisticians.upper_bound(last_returned_ssrc_);
469 for (auto it = start_it;
470 result.size() < max_blocks && it != statisticians.end(); ++it)
471 add_report_block(it->first, it->second);
472 for (auto it = statisticians.begin();
473 result.size() < max_blocks && it != start_it; ++it)
474 add_report_block(it->first, it->second);
475
476 if (!result.empty())
477 last_returned_ssrc_ = result.back().source_ssrc();
danilchapf5f793c2017-07-27 04:44:18 -0700478 return result;
479}
480
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000481} // namespace webrtc