blob: abe028d670237a8921a63d03e23e00bf7da709f9 [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),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000051 received_packet_overhead_(12),
Qingsi Wang2370b082018-08-21 14:24:26 -070052 last_report_inorder_packets_(0),
53 last_report_old_packets_(0),
54 last_report_seq_max_(0),
sprang@webrtc.org0e932572014-01-23 10:00:39 +000055 rtcp_callback_(rtcp_callback),
56 rtp_callback_(rtp_callback) {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000057
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010058StreamStatisticianImpl::~StreamStatisticianImpl() = default;
59
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000060void StreamStatisticianImpl::IncomingPacket(const RTPHeader& header,
Niels Möller5304a322018-08-27 13:27:05 +020061 size_t packet_length) {
Niels Möllerb615d1a2018-08-27 12:32:21 +020062 StreamDataCounters counters;
63 {
64 rtc::CritScope cs(&stream_lock_);
65
Niels Möller5304a322018-08-27 13:27:05 +020066 bool retransmitted =
67 enable_retransmit_detection_ && IsRetransmitOfOldPacket(header);
Niels Möllerb615d1a2018-08-27 12:32:21 +020068 counters = UpdateCounters(header, packet_length, retransmitted);
69 }
danilchapec86be02017-08-14 05:51:02 -070070 rtp_callback_->DataCountersUpdated(counters, ssrc_);
sprang@webrtc.orga45cac02014-01-27 16:22:08 +000071}
72
danilchapec86be02017-08-14 05:51:02 -070073StreamDataCounters StreamStatisticianImpl::UpdateCounters(
74 const RTPHeader& header,
75 size_t packet_length,
76 bool retransmitted) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000077 bool in_order = InOrderPacketInternal(header.sequenceNumber);
danilchapec86be02017-08-14 05:51:02 -070078 RTC_DCHECK_EQ(ssrc_, header.ssrc);
sprangcd349d92016-07-13 09:11:28 -070079 incoming_bitrate_.Update(packet_length, clock_->TimeInMilliseconds());
asapersson@webrtc.org44149392015-02-04 08:34:47 +000080 receive_counters_.transmitted.AddPacket(packet_length, header);
sprang@webrtc.org0e932572014-01-23 10:00:39 +000081 if (!in_order && retransmitted) {
asapersson@webrtc.org44149392015-02-04 08:34:47 +000082 receive_counters_.retransmitted.AddPacket(packet_length, header);
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) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000086 received_seq_first_ = header.sequenceNumber;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +000087 receive_counters_.first_packet_time_ms = clock_->TimeInMilliseconds();
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 &&
sprang@webrtc.org0e932572014-01-23 10:00:39 +000098 received_seq_max_ > header.sequenceNumber) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000099 // Wrap around detected.
100 received_seq_wraps_++;
101 }
102 // New max.
103 received_seq_max_ = header.sequenceNumber;
104
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.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000107 if (header.timestamp != last_received_timestamp_ &&
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000108 (receive_counters_.transmitted.packets -
109 receive_counters_.retransmitted.packets) > 1) {
danilchap1227e8b2015-12-21 11:06:50 -0800110 UpdateJitter(header, receive_time);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000111 }
112 last_received_timestamp_ = header.timestamp;
danilchap1227e8b2015-12-21 11:06:50 -0800113 last_receive_time_ntp_ = receive_time;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000114 last_receive_time_ms_ = clock_->TimeInMilliseconds();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000115 }
116
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000117 size_t packet_oh = header.headerLength + header.paddingLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000118
119 // Our measured overhead. Filter from RFC 5104 4.2.1.2:
120 // avg_OH (new) = 15/16*avg_OH (old) + 1/16*pckt_OH,
121 received_packet_overhead_ = (15 * received_packet_overhead_ + packet_oh) >> 4;
danilchapec86be02017-08-14 05:51:02 -0700122 return receive_counters_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000123}
124
125void StreamStatisticianImpl::UpdateJitter(const RTPHeader& header,
danilchap1227e8b2015-12-21 11:06:50 -0800126 NtpTime receive_time) {
127 uint32_t receive_time_rtp =
128 NtpToRtp(receive_time, header.payload_type_frequency);
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000129 uint32_t last_receive_time_rtp =
danilchap1227e8b2015-12-21 11:06:50 -0800130 NtpToRtp(last_receive_time_ntp_, header.payload_type_frequency);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000131 int32_t time_diff_samples = (receive_time_rtp - last_receive_time_rtp) -
Yves Gerey665174f2018-06-19 15:03:05 +0200132 (header.timestamp - last_received_timestamp_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000133
kwibergfd8be342016-05-14 19:44:11 -0700134 time_diff_samples = std::abs(time_diff_samples);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000135
136 // lib_jingle sometimes deliver crazy jumps in TS for the same stream.
137 // If this happens, don't update jitter value. Use 5 secs video frequency
138 // as the threshold.
139 if (time_diff_samples < 450000) {
140 // Note we calculate in Q4 to avoid using float.
141 int32_t jitter_diff_q4 = (time_diff_samples << 4) - jitter_q4_;
142 jitter_q4_ += ((jitter_diff_q4 + 8) >> 4);
143 }
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000144}
145
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000146void StreamStatisticianImpl::FecPacketReceived(const RTPHeader& header,
147 size_t packet_length) {
danilchapec86be02017-08-14 05:51:02 -0700148 StreamDataCounters counters;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000149 {
danilchap7c9426c2016-04-14 03:05:31 -0700150 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000151 receive_counters_.fec.AddPacket(packet_length, header);
danilchapec86be02017-08-14 05:51:02 -0700152 counters = receive_counters_;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000153 }
danilchapec86be02017-08-14 05:51:02 -0700154 rtp_callback_->DataCountersUpdated(counters, ssrc_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000155}
156
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000157void StreamStatisticianImpl::SetMaxReorderingThreshold(
158 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700159 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000160 max_reordering_threshold_ = max_reordering_threshold;
161}
162
Niels Möller5304a322018-08-27 13:27:05 +0200163void StreamStatisticianImpl::EnableRetransmitDetection(bool enable) {
164 rtc::CritScope cs(&stream_lock_);
165 enable_retransmit_detection_ = enable;
166}
167
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000168bool StreamStatisticianImpl::GetStatistics(RtcpStatistics* statistics,
Qingsi Wang2370b082018-08-21 14:24:26 -0700169 bool reset) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000170 {
danilchap7c9426c2016-04-14 03:05:31 -0700171 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000172 if (received_seq_first_ == 0 &&
173 receive_counters_.transmitted.payload_bytes == 0) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000174 // We have not received anything.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000175 return false;
176 }
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000177
Qingsi Wang2370b082018-08-21 14:24:26 -0700178 if (!reset) {
179 if (last_report_inorder_packets_ == 0) {
180 // No report.
181 return false;
182 }
183 // Just get last report.
184 *statistics = last_reported_statistics_;
185 return true;
186 }
187
188 *statistics = CalculateRtcpStatistics();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000189 }
190
Qingsi Wang2370b082018-08-21 14:24:26 -0700191 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000192 return true;
193}
194
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200195bool StreamStatisticianImpl::GetActiveStatisticsAndReset(
196 RtcpStatistics* statistics) {
197 {
198 rtc::CritScope cs(&stream_lock_);
199 if (clock_->CurrentNtpInMilliseconds() - last_receive_time_ntp_.ToMs() >=
200 kStatisticsTimeoutMs) {
201 // Not active.
202 return false;
203 }
204 if (received_seq_first_ == 0 &&
205 receive_counters_.transmitted.payload_bytes == 0) {
206 // We have not received anything.
207 return false;
208 }
209
Qingsi Wang2370b082018-08-21 14:24:26 -0700210 *statistics = CalculateRtcpStatistics();
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200211 }
212
213 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
214 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(
Niels Möllereda00872018-05-23 13:54:51 +0200315 const RTPHeader& header) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000316 if (InOrderPacketInternal(header.sequenceNumber)) {
317 return false;
318 }
319 uint32_t frequency_khz = header.payload_type_frequency / 1000;
320 assert(frequency_khz > 0);
321
Yves Gerey665174f2018-06-19 15:03:05 +0200322 int64_t time_diff_ms = clock_->TimeInMilliseconds() - last_receive_time_ms_;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000323
324 // Diff in time stamp since last received in order.
325 uint32_t timestamp_diff = header.timestamp - last_received_timestamp_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000326 uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000327
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000328 int64_t max_delay_ms = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000329
Niels Möllereda00872018-05-23 13:54:51 +0200330 // Jitter standard deviation in samples.
331 float jitter_std = sqrt(static_cast<float>(jitter_q4_ >> 4));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000332
Niels Möllereda00872018-05-23 13:54:51 +0200333 // 2 times the standard deviation => 95% confidence.
334 // And transform to milliseconds by dividing by the frequency in kHz.
335 max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz);
336
337 // Min max_delay_ms is 1.
338 if (max_delay_ms == 0) {
339 max_delay_ms = 1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000340 }
341 return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms;
342}
343
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000344bool StreamStatisticianImpl::InOrderPacketInternal(
345 uint16_t sequence_number) const {
346 // First packet is always in order.
Qingsi Wang2370b082018-08-21 14:24:26 -0700347 if (last_receive_time_ms_ == 0)
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000348 return true;
349
350 if (IsNewerSequenceNumber(sequence_number, received_seq_max_)) {
351 return true;
352 } else {
353 // If we have a restart of the remote side this packet is still in order.
Yves Gerey665174f2018-06-19 15:03:05 +0200354 return !IsNewerSequenceNumber(
355 sequence_number, received_seq_max_ - max_reordering_threshold_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000356 }
357}
358
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000359ReceiveStatistics* ReceiveStatistics::Create(Clock* clock) {
360 return new ReceiveStatisticsImpl(clock);
361}
362
363ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock)
364 : clock_(clock),
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100365 last_returned_ssrc_(0),
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000366 rtcp_stats_callback_(NULL),
367 rtp_stats_callback_(NULL) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000368
369ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
370 while (!statisticians_.empty()) {
371 delete statisticians_.begin()->second;
372 statisticians_.erase(statisticians_.begin());
373 }
374}
375
Niels Möller1f3206c2018-09-14 08:26:32 +0200376void ReceiveStatisticsImpl::OnRtpPacket(const RtpPacketReceived& packet) {
377 RTPHeader header;
378 packet.GetHeader(&header);
379 IncomingPacket(header, packet.size());
380}
381
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000382void ReceiveStatisticsImpl::IncomingPacket(const RTPHeader& header,
Niels Möller5304a322018-08-27 13:27:05 +0200383 size_t packet_length) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000384 StreamStatisticianImpl* impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000385 {
danilchap7c9426c2016-04-14 03:05:31 -0700386 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200387 auto it = statisticians_.find(header.ssrc);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000388 if (it != statisticians_.end()) {
389 impl = it->second;
390 } else {
Niels Möller5304a322018-08-27 13:27:05 +0200391 impl = new StreamStatisticianImpl(
392 header.ssrc, clock_, /* enable_retransmit_detection = */ false, this,
393 this);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000394 statisticians_[header.ssrc] = impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000395 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000396 }
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000397 // StreamStatisticianImpl instance is created once and only destroyed when
398 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
399 // it's own locking so don't hold receive_statistics_lock_ (potential
400 // deadlock).
Niels Möller5304a322018-08-27 13:27:05 +0200401 impl->IncomingPacket(header, packet_length);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000402}
403
Niels Möller1f3206c2018-09-14 08:26:32 +0200404void ReceiveStatisticsImpl::FecPacketReceived(const RtpPacketReceived& packet) {
danilchapec86be02017-08-14 05:51:02 -0700405 StreamStatisticianImpl* impl;
406 {
407 rtc::CritScope cs(&receive_statistics_lock_);
Niels Möller1f3206c2018-09-14 08:26:32 +0200408 auto it = statisticians_.find(packet.Ssrc());
danilchapec86be02017-08-14 05:51:02 -0700409 // Ignore FEC if it is the first packet.
410 if (it == statisticians_.end())
411 return;
412 impl = it->second;
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000413 }
Niels Möller1f3206c2018-09-14 08:26:32 +0200414 RTPHeader header;
415 packet.GetHeader(&header);
416 impl->FecPacketReceived(header, packet.size());
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000417}
418
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000419StreamStatistician* ReceiveStatisticsImpl::GetStatistician(
420 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700421 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200422 auto it = statisticians_.find(ssrc);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000423 if (it == statisticians_.end())
424 return NULL;
425 return it->second;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000426}
427
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000428void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
429 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700430 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200431 for (auto& statistician : statisticians_) {
432 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000433 }
434}
435
Niels Möller5304a322018-08-27 13:27:05 +0200436void ReceiveStatisticsImpl::EnableRetransmitDetection(uint32_t ssrc,
437 bool enable) {
438 StreamStatisticianImpl* impl;
439 {
440 rtc::CritScope cs(&receive_statistics_lock_);
441 StreamStatisticianImpl*& impl_ref = statisticians_[ssrc];
442 if (impl_ref == nullptr) { // new element
443 impl_ref = new StreamStatisticianImpl(ssrc, clock_, enable, this, this);
444 return;
445 }
446 impl = impl_ref;
447 }
448 impl->EnableRetransmitDetection(enable);
449}
450
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000451void ReceiveStatisticsImpl::RegisterRtcpStatisticsCallback(
452 RtcpStatisticsCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700453 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000454 if (callback != NULL)
455 assert(rtcp_stats_callback_ == NULL);
456 rtcp_stats_callback_ = callback;
457}
458
459void ReceiveStatisticsImpl::StatisticsUpdated(const RtcpStatistics& statistics,
460 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700461 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000462 if (rtcp_stats_callback_)
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000463 rtcp_stats_callback_->StatisticsUpdated(statistics, ssrc);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000464}
465
466void ReceiveStatisticsImpl::CNameChanged(const char* cname, uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700467 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000468 if (rtcp_stats_callback_)
469 rtcp_stats_callback_->CNameChanged(cname, ssrc);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000470}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000471
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000472void ReceiveStatisticsImpl::RegisterRtpStatisticsCallback(
473 StreamDataCountersCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700474 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000475 if (callback != NULL)
476 assert(rtp_stats_callback_ == NULL);
477 rtp_stats_callback_ = callback;
478}
479
480void ReceiveStatisticsImpl::DataCountersUpdated(const StreamDataCounters& stats,
481 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700482 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000483 if (rtp_stats_callback_) {
484 rtp_stats_callback_->DataCountersUpdated(stats, ssrc);
485 }
486}
487
danilchap0bc84232017-08-11 08:12:54 -0700488std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700489 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200490 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
491 {
492 rtc::CritScope cs(&receive_statistics_lock_);
493 statisticians = statisticians_;
494 }
danilchapf5f793c2017-07-27 04:44:18 -0700495 std::vector<rtcp::ReportBlock> result;
496 result.reserve(std::min(max_blocks, statisticians.size()));
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100497 auto add_report_block = [&result](uint32_t media_ssrc,
498 StreamStatisticianImpl* statistician) {
danilchapf5f793c2017-07-27 04:44:18 -0700499 // Do we have receive statistics to send?
500 RtcpStatistics stats;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100501 if (!statistician->GetActiveStatisticsAndReset(&stats))
502 return;
danilchapf5f793c2017-07-27 04:44:18 -0700503 result.emplace_back();
504 rtcp::ReportBlock& block = result.back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100505 block.SetMediaSsrc(media_ssrc);
danilchapf5f793c2017-07-27 04:44:18 -0700506 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700507 if (!block.SetCumulativeLost(stats.packets_lost)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100508 RTC_LOG(LS_WARNING) << "Cumulative lost is oversized.";
danilchapf5f793c2017-07-27 04:44:18 -0700509 result.pop_back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100510 return;
danilchapf5f793c2017-07-27 04:44:18 -0700511 }
srte186d9c32017-08-04 05:03:53 -0700512 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700513 block.SetJitter(stats.jitter);
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100514 };
515
516 const auto start_it = statisticians.upper_bound(last_returned_ssrc_);
517 for (auto it = start_it;
518 result.size() < max_blocks && it != statisticians.end(); ++it)
519 add_report_block(it->first, it->second);
520 for (auto it = statisticians.begin();
521 result.size() < max_blocks && it != start_it; ++it)
522 add_report_block(it->first, it->second);
523
524 if (!result.empty())
525 last_returned_ssrc_ = result.back().source_ssrc();
danilchapf5f793c2017-07-27 04:44:18 -0700526 return result;
527}
528
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000529} // namespace webrtc