blob: 30a0f360f680f8b58c855af95e2a19e1f4c32250 [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,
Danil Chapovalovebb50c22018-11-22 14:04:02 +010036 int max_reordering_threshold,
sprang@webrtc.org0e932572014-01-23 10:00:39 +000037 RtcpStatisticsCallback* rtcp_callback,
38 StreamDataCountersCallback* rtp_callback)
danilchapec86be02017-08-14 05:51:02 -070039 : ssrc_(ssrc),
40 clock_(clock),
sprangcd349d92016-07-13 09:11:28 -070041 incoming_bitrate_(kStatisticsProcessIntervalMs,
42 RateStatistics::kBpsScale),
Danil Chapovalovebb50c22018-11-22 14:04:02 +010043 max_reordering_threshold_(max_reordering_threshold),
Niels Möller5304a322018-08-27 13:27:05 +020044 enable_retransmit_detection_(enable_retransmit_detection),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000045 jitter_q4_(0),
Qingsi Wang2370b082018-08-21 14:24:26 -070046 cumulative_loss_(0),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000047 last_receive_time_ms_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000048 last_received_timestamp_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000049 received_seq_first_(0),
50 received_seq_max_(0),
51 received_seq_wraps_(0),
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
Niels Möllerdbb988b2018-11-15 08:05:16 +010060void StreamStatisticianImpl::OnRtpPacket(const RtpPacketReceived& packet) {
Danil Chapovalov44727b42018-11-22 11:28:45 +010061 StreamDataCounters counters = UpdateCounters(packet);
danilchapec86be02017-08-14 05:51:02 -070062 rtp_callback_->DataCountersUpdated(counters, ssrc_);
sprang@webrtc.orga45cac02014-01-27 16:22:08 +000063}
64
danilchapec86be02017-08-14 05:51:02 -070065StreamDataCounters StreamStatisticianImpl::UpdateCounters(
Danil Chapovalov44727b42018-11-22 11:28:45 +010066 const RtpPacketReceived& packet) {
67 rtc::CritScope cs(&stream_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +010068 RTC_DCHECK_EQ(ssrc_, packet.Ssrc());
Danil Chapovalov44727b42018-11-22 11:28:45 +010069 uint16_t sequence_number = packet.SequenceNumber();
70 bool in_order =
71 // First packet is always in order.
72 last_receive_time_ms_ == 0 ||
73 IsNewerSequenceNumber(sequence_number, received_seq_max_) ||
74 // If we have a restart of the remote side this packet is still in order.
75 !IsNewerSequenceNumber(sequence_number,
76 received_seq_max_ - max_reordering_threshold_);
77 int64_t now_ms = clock_->TimeInMilliseconds();
78
79 incoming_bitrate_.Update(packet.size(), now_ms);
Niels Möllerdbb988b2018-11-15 08:05:16 +010080 receive_counters_.transmitted.AddPacket(packet);
Danil Chapovalov44727b42018-11-22 11:28:45 +010081 if (!in_order && enable_retransmit_detection_ &&
82 IsRetransmitOfOldPacket(packet, now_ms)) {
Niels Möllerdbb988b2018-11-15 08:05:16 +010083 receive_counters_.retransmitted.AddPacket(packet);
sprang@webrtc.org0e932572014-01-23 10:00:39 +000084 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +000085
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +000086 if (receive_counters_.transmitted.packets == 1) {
Niels Möllerdbb988b2018-11-15 08:05:16 +010087 received_seq_first_ = packet.SequenceNumber();
Danil Chapovalov44727b42018-11-22 11:28:45 +010088 receive_counters_.first_packet_time_ms = now_ms;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000089 }
90
91 // Count only the new packets received. That is, if packets 1, 2, 3, 5, 4, 6
92 // are received, 4 will be ignored.
93 if (in_order) {
94 // Current time in samples.
danilchap37953762017-02-09 11:15:25 -080095 NtpTime receive_time = clock_->CurrentNtpTime();
wu@webrtc.org822fbd82013-08-15 23:38:54 +000096
97 // Wrong if we use RetransmitOfOldPacket.
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +000098 if (receive_counters_.transmitted.packets > 1 &&
Niels Möllerdbb988b2018-11-15 08:05:16 +010099 received_seq_max_ > packet.SequenceNumber()) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000100 // Wrap around detected.
101 received_seq_wraps_++;
102 }
103 // New max.
Niels Möllerdbb988b2018-11-15 08:05:16 +0100104 received_seq_max_ = packet.SequenceNumber();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000105
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000106 // If new time stamp and more than one in-order packet received, calculate
107 // new jitter statistics.
Niels Möllerdbb988b2018-11-15 08:05:16 +0100108 if (packet.Timestamp() != last_received_timestamp_ &&
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000109 (receive_counters_.transmitted.packets -
110 receive_counters_.retransmitted.packets) > 1) {
Niels Möllerdbb988b2018-11-15 08:05:16 +0100111 UpdateJitter(packet, receive_time);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000112 }
Niels Möllerdbb988b2018-11-15 08:05:16 +0100113 last_received_timestamp_ = packet.Timestamp();
danilchap1227e8b2015-12-21 11:06:50 -0800114 last_receive_time_ntp_ = receive_time;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100115 last_receive_time_ms_ = now_ms;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000116 }
danilchapec86be02017-08-14 05:51:02 -0700117 return receive_counters_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000118}
119
Niels Möllerdbb988b2018-11-15 08:05:16 +0100120void StreamStatisticianImpl::UpdateJitter(const RtpPacketReceived& packet,
danilchap1227e8b2015-12-21 11:06:50 -0800121 NtpTime receive_time) {
122 uint32_t receive_time_rtp =
Niels Möllerdbb988b2018-11-15 08:05:16 +0100123 NtpToRtp(receive_time, packet.payload_type_frequency());
pbos@webrtc.org62bafae2014-07-08 12:10:51 +0000124 uint32_t last_receive_time_rtp =
Niels Möllerdbb988b2018-11-15 08:05:16 +0100125 NtpToRtp(last_receive_time_ntp_, packet.payload_type_frequency());
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000126 int32_t time_diff_samples = (receive_time_rtp - last_receive_time_rtp) -
Niels Möllerdbb988b2018-11-15 08:05:16 +0100127 (packet.Timestamp() - last_received_timestamp_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000128
kwibergfd8be342016-05-14 19:44:11 -0700129 time_diff_samples = std::abs(time_diff_samples);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000130
131 // lib_jingle sometimes deliver crazy jumps in TS for the same stream.
132 // If this happens, don't update jitter value. Use 5 secs video frequency
133 // as the threshold.
134 if (time_diff_samples < 450000) {
135 // Note we calculate in Q4 to avoid using float.
136 int32_t jitter_diff_q4 = (time_diff_samples << 4) - jitter_q4_;
137 jitter_q4_ += ((jitter_diff_q4 + 8) >> 4);
138 }
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000139}
140
Niels Möllerdbb988b2018-11-15 08:05:16 +0100141void StreamStatisticianImpl::FecPacketReceived(
142 const RtpPacketReceived& packet) {
danilchapec86be02017-08-14 05:51:02 -0700143 StreamDataCounters counters;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000144 {
danilchap7c9426c2016-04-14 03:05:31 -0700145 rtc::CritScope cs(&stream_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +0100146 receive_counters_.fec.AddPacket(packet);
danilchapec86be02017-08-14 05:51:02 -0700147 counters = receive_counters_;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000148 }
danilchapec86be02017-08-14 05:51:02 -0700149 rtp_callback_->DataCountersUpdated(counters, ssrc_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000150}
151
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000152void StreamStatisticianImpl::SetMaxReorderingThreshold(
153 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700154 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000155 max_reordering_threshold_ = max_reordering_threshold;
156}
157
Niels Möller5304a322018-08-27 13:27:05 +0200158void StreamStatisticianImpl::EnableRetransmitDetection(bool enable) {
159 rtc::CritScope cs(&stream_lock_);
160 enable_retransmit_detection_ = enable;
161}
162
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000163bool StreamStatisticianImpl::GetStatistics(RtcpStatistics* statistics,
Qingsi Wang2370b082018-08-21 14:24:26 -0700164 bool reset) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000165 {
danilchap7c9426c2016-04-14 03:05:31 -0700166 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000167 if (received_seq_first_ == 0 &&
168 receive_counters_.transmitted.payload_bytes == 0) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000169 // We have not received anything.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000170 return false;
171 }
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000172
Qingsi Wang2370b082018-08-21 14:24:26 -0700173 if (!reset) {
174 if (last_report_inorder_packets_ == 0) {
175 // No report.
176 return false;
177 }
178 // Just get last report.
179 *statistics = last_reported_statistics_;
180 return true;
181 }
182
183 *statistics = CalculateRtcpStatistics();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000184 }
185
Qingsi Wang2370b082018-08-21 14:24:26 -0700186 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000187 return true;
188}
189
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200190bool StreamStatisticianImpl::GetActiveStatisticsAndReset(
191 RtcpStatistics* statistics) {
192 {
193 rtc::CritScope cs(&stream_lock_);
194 if (clock_->CurrentNtpInMilliseconds() - last_receive_time_ntp_.ToMs() >=
195 kStatisticsTimeoutMs) {
196 // Not active.
197 return false;
198 }
199 if (received_seq_first_ == 0 &&
200 receive_counters_.transmitted.payload_bytes == 0) {
201 // We have not received anything.
202 return false;
203 }
204
Qingsi Wang2370b082018-08-21 14:24:26 -0700205 *statistics = CalculateRtcpStatistics();
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200206 }
207
208 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
209 return true;
210}
211
Qingsi Wang2370b082018-08-21 14:24:26 -0700212RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics() {
213 RtcpStatistics stats;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000214
Qingsi Wang2370b082018-08-21 14:24:26 -0700215 if (last_report_inorder_packets_ == 0) {
216 // First time we send a report.
217 last_report_seq_max_ = received_seq_first_ - 1;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000218 }
219
Qingsi Wang2370b082018-08-21 14:24:26 -0700220 // Calculate fraction lost.
221 uint16_t exp_since_last = (received_seq_max_ - last_report_seq_max_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000222
Qingsi Wang2370b082018-08-21 14:24:26 -0700223 if (last_report_seq_max_ > received_seq_max_) {
224 // Can we assume that the seq_num can't go decrease over a full RTCP period?
225 exp_since_last = 0;
226 }
227
228 // Number of received RTP packets since last report, counts all packets but
229 // not re-transmissions.
230 uint32_t rec_since_last = (receive_counters_.transmitted.packets -
231 receive_counters_.retransmitted.packets) -
232 last_report_inorder_packets_;
233
234 // With NACK we don't know the expected retransmissions during the last
235 // second. We know how many "old" packets we have received. We just count
236 // the number of old received to estimate the loss, but it still does not
237 // guarantee an exact number since we run this based on time triggered by
238 // sending of an RTP packet. This should have a minimum effect.
239
240 // With NACK we don't count old packets as received since they are
241 // re-transmitted. We use RTT to decide if a packet is re-ordered or
242 // re-transmitted.
243 uint32_t retransmitted_packets =
244 receive_counters_.retransmitted.packets - last_report_old_packets_;
245 rec_since_last += retransmitted_packets;
246
247 int32_t missing = 0;
248 if (exp_since_last > rec_since_last) {
249 missing = (exp_since_last - rec_since_last);
250 }
251 uint8_t local_fraction_lost = 0;
252 if (exp_since_last) {
253 // Scale 0 to 255, where 255 is 100% loss.
254 local_fraction_lost = static_cast<uint8_t>(255 * missing / exp_since_last);
255 }
256 stats.fraction_lost = local_fraction_lost;
257
258 // We need a counter for cumulative loss too.
259 // TODO(danilchap): Ensure cumulative loss is below maximum value of 2^24.
260 cumulative_loss_ += missing;
261 stats.packets_lost = cumulative_loss_;
262 stats.extended_highest_sequence_number =
263 (received_seq_wraps_ << 16) + received_seq_max_;
264 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
265 stats.jitter = jitter_q4_ >> 4;
266
267 // Store this report.
268 last_reported_statistics_ = stats;
269
270 // Only for report blocks in RTCP SR and RR.
271 last_report_inorder_packets_ = receive_counters_.transmitted.packets -
272 receive_counters_.retransmitted.packets;
273 last_report_old_packets_ = receive_counters_.retransmitted.packets;
274 last_report_seq_max_ = received_seq_max_;
gaetano.carlucci61050f62016-09-30 06:29:54 -0700275 BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts",
gaetano.carlucci52a57032016-09-14 05:04:36 -0700276 clock_->TimeInMilliseconds(),
Qingsi Wang2370b082018-08-21 14:24:26 -0700277 cumulative_loss_, ssrc_);
gaetano.carlucci52a57032016-09-14 05:04:36 -0700278 BWE_TEST_LOGGING_PLOT_WITH_SSRC(
gaetano.carlucci61050f62016-09-30 06:29:54 -0700279 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700280 (received_seq_max_ - received_seq_first_), ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000281
Qingsi Wang2370b082018-08-21 14:24:26 -0700282 return stats;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000283}
284
Yves Gerey665174f2018-06-19 15:03:05 +0200285void StreamStatisticianImpl::GetDataCounters(size_t* bytes_received,
286 uint32_t* packets_received) const {
danilchap7c9426c2016-04-14 03:05:31 -0700287 rtc::CritScope cs(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000288 if (bytes_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000289 *bytes_received = receive_counters_.transmitted.payload_bytes +
290 receive_counters_.transmitted.header_bytes +
291 receive_counters_.transmitted.padding_bytes;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000292 }
293 if (packets_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000294 *packets_received = receive_counters_.transmitted.packets;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000295 }
296}
297
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000298void StreamStatisticianImpl::GetReceiveStreamDataCounters(
299 StreamDataCounters* data_counters) const {
danilchap7c9426c2016-04-14 03:05:31 -0700300 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000301 *data_counters = receive_counters_;
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000302}
303
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000304uint32_t StreamStatisticianImpl::BitrateReceived() const {
danilchap7c9426c2016-04-14 03:05:31 -0700305 rtc::CritScope cs(&stream_lock_);
sprangcd349d92016-07-13 09:11:28 -0700306 return incoming_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000307}
308
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000309bool StreamStatisticianImpl::IsRetransmitOfOldPacket(
Danil Chapovalov44727b42018-11-22 11:28:45 +0100310 const RtpPacketReceived& packet,
311 int64_t now_ms) const {
Niels Möllerdbb988b2018-11-15 08:05:16 +0100312 uint32_t frequency_khz = packet.payload_type_frequency() / 1000;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100313 RTC_DCHECK_GT(frequency_khz, 0);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000314
Danil Chapovalov44727b42018-11-22 11:28:45 +0100315 int64_t time_diff_ms = now_ms - last_receive_time_ms_;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000316
317 // Diff in time stamp since last received in order.
Niels Möllerdbb988b2018-11-15 08:05:16 +0100318 uint32_t timestamp_diff = packet.Timestamp() - last_received_timestamp_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000319 uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000320
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000321 int64_t max_delay_ms = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000322
Niels Möllereda00872018-05-23 13:54:51 +0200323 // Jitter standard deviation in samples.
324 float jitter_std = sqrt(static_cast<float>(jitter_q4_ >> 4));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000325
Niels Möllereda00872018-05-23 13:54:51 +0200326 // 2 times the standard deviation => 95% confidence.
327 // And transform to milliseconds by dividing by the frequency in kHz.
328 max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz);
329
330 // Min max_delay_ms is 1.
331 if (max_delay_ms == 0) {
332 max_delay_ms = 1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000333 }
334 return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms;
335}
336
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000337ReceiveStatistics* ReceiveStatistics::Create(Clock* clock) {
338 return new ReceiveStatisticsImpl(clock);
339}
340
341ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock)
342 : clock_(clock),
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100343 last_returned_ssrc_(0),
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100344 max_reordering_threshold_(kDefaultMaxReorderingThreshold),
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000345 rtcp_stats_callback_(NULL),
346 rtp_stats_callback_(NULL) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000347
348ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
349 while (!statisticians_.empty()) {
350 delete statisticians_.begin()->second;
351 statisticians_.erase(statisticians_.begin());
352 }
353}
354
Niels Möller1f3206c2018-09-14 08:26:32 +0200355void ReceiveStatisticsImpl::OnRtpPacket(const RtpPacketReceived& packet) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000356 StreamStatisticianImpl* impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000357 {
danilchap7c9426c2016-04-14 03:05:31 -0700358 rtc::CritScope cs(&receive_statistics_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +0100359 auto it = statisticians_.find(packet.Ssrc());
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000360 if (it != statisticians_.end()) {
361 impl = it->second;
362 } else {
Niels Möller5304a322018-08-27 13:27:05 +0200363 impl = new StreamStatisticianImpl(
Niels Möllerdbb988b2018-11-15 08:05:16 +0100364 packet.Ssrc(), clock_, /* enable_retransmit_detection = */ false,
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100365 max_reordering_threshold_, this, this);
Niels Möllerdbb988b2018-11-15 08:05:16 +0100366 statisticians_[packet.Ssrc()] = impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000367 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000368 }
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000369 // StreamStatisticianImpl instance is created once and only destroyed when
370 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
371 // it's own locking so don't hold receive_statistics_lock_ (potential
372 // deadlock).
Niels Möllerdbb988b2018-11-15 08:05:16 +0100373 impl->OnRtpPacket(packet);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000374}
375
Niels Möller1f3206c2018-09-14 08:26:32 +0200376void ReceiveStatisticsImpl::FecPacketReceived(const RtpPacketReceived& packet) {
danilchapec86be02017-08-14 05:51:02 -0700377 StreamStatisticianImpl* impl;
378 {
379 rtc::CritScope cs(&receive_statistics_lock_);
Niels Möller1f3206c2018-09-14 08:26:32 +0200380 auto it = statisticians_.find(packet.Ssrc());
danilchapec86be02017-08-14 05:51:02 -0700381 // Ignore FEC if it is the first packet.
382 if (it == statisticians_.end())
383 return;
384 impl = it->second;
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000385 }
Niels Möllerdbb988b2018-11-15 08:05:16 +0100386 impl->FecPacketReceived(packet);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000387}
388
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000389StreamStatistician* ReceiveStatisticsImpl::GetStatistician(
390 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700391 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200392 auto it = statisticians_.find(ssrc);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000393 if (it == statisticians_.end())
394 return NULL;
395 return it->second;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000396}
397
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000398void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
399 int max_reordering_threshold) {
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100400 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
401 {
402 rtc::CritScope cs(&receive_statistics_lock_);
403 max_reordering_threshold_ = max_reordering_threshold;
404 statisticians = statisticians_;
405 }
406 for (auto& statistician : statisticians) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200407 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000408 }
409}
410
Niels Möller5304a322018-08-27 13:27:05 +0200411void ReceiveStatisticsImpl::EnableRetransmitDetection(uint32_t ssrc,
412 bool enable) {
413 StreamStatisticianImpl* impl;
414 {
415 rtc::CritScope cs(&receive_statistics_lock_);
416 StreamStatisticianImpl*& impl_ref = statisticians_[ssrc];
417 if (impl_ref == nullptr) { // new element
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100418 impl_ref = new StreamStatisticianImpl(
419 ssrc, clock_, enable, max_reordering_threshold_, this, this);
Niels Möller5304a322018-08-27 13:27:05 +0200420 return;
421 }
422 impl = impl_ref;
423 }
424 impl->EnableRetransmitDetection(enable);
425}
426
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000427void ReceiveStatisticsImpl::RegisterRtcpStatisticsCallback(
428 RtcpStatisticsCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700429 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000430 if (callback != NULL)
431 assert(rtcp_stats_callback_ == NULL);
432 rtcp_stats_callback_ = callback;
433}
434
435void ReceiveStatisticsImpl::StatisticsUpdated(const RtcpStatistics& statistics,
436 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700437 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000438 if (rtcp_stats_callback_)
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000439 rtcp_stats_callback_->StatisticsUpdated(statistics, ssrc);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000440}
441
442void ReceiveStatisticsImpl::CNameChanged(const char* cname, uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700443 rtc::CritScope cs(&receive_statistics_lock_);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000444 if (rtcp_stats_callback_)
445 rtcp_stats_callback_->CNameChanged(cname, ssrc);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000446}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000447
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000448void ReceiveStatisticsImpl::RegisterRtpStatisticsCallback(
449 StreamDataCountersCallback* callback) {
danilchap7c9426c2016-04-14 03:05:31 -0700450 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000451 if (callback != NULL)
452 assert(rtp_stats_callback_ == NULL);
453 rtp_stats_callback_ = callback;
454}
455
456void ReceiveStatisticsImpl::DataCountersUpdated(const StreamDataCounters& stats,
457 uint32_t ssrc) {
danilchap7c9426c2016-04-14 03:05:31 -0700458 rtc::CritScope cs(&receive_statistics_lock_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000459 if (rtp_stats_callback_) {
460 rtp_stats_callback_->DataCountersUpdated(stats, ssrc);
461 }
462}
463
danilchap0bc84232017-08-11 08:12:54 -0700464std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700465 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200466 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
467 {
468 rtc::CritScope cs(&receive_statistics_lock_);
469 statisticians = statisticians_;
470 }
danilchapf5f793c2017-07-27 04:44:18 -0700471 std::vector<rtcp::ReportBlock> result;
472 result.reserve(std::min(max_blocks, statisticians.size()));
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100473 auto add_report_block = [&result](uint32_t media_ssrc,
474 StreamStatisticianImpl* statistician) {
danilchapf5f793c2017-07-27 04:44:18 -0700475 // Do we have receive statistics to send?
476 RtcpStatistics stats;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100477 if (!statistician->GetActiveStatisticsAndReset(&stats))
478 return;
danilchapf5f793c2017-07-27 04:44:18 -0700479 result.emplace_back();
480 rtcp::ReportBlock& block = result.back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100481 block.SetMediaSsrc(media_ssrc);
danilchapf5f793c2017-07-27 04:44:18 -0700482 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700483 if (!block.SetCumulativeLost(stats.packets_lost)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100484 RTC_LOG(LS_WARNING) << "Cumulative lost is oversized.";
danilchapf5f793c2017-07-27 04:44:18 -0700485 result.pop_back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100486 return;
danilchapf5f793c2017-07-27 04:44:18 -0700487 }
srte186d9c32017-08-04 05:03:53 -0700488 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700489 block.SetJitter(stats.jitter);
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100490 };
491
492 const auto start_it = statisticians.upper_bound(last_returned_ssrc_);
493 for (auto it = start_it;
494 result.size() < max_blocks && it != statisticians.end(); ++it)
495 add_report_block(it->first, it->second);
496 for (auto it = statisticians.begin();
497 result.size() < max_blocks && it != start_it; ++it)
498 add_report_block(it->first, it->second);
499
500 if (!result.empty())
501 last_returned_ssrc_ = result.back().source_ssrc();
danilchapf5f793c2017-07-27 04:44:18 -0700502 return result;
503}
504
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000505} // namespace webrtc