blob: bc742d1ea63ff7bd4794cceac3ddbd833d12c499 [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),
Danil Chapovalov7e0299e2018-12-04 17:16:10 +000051 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),
Danil Chapovalov7e0299e2018-12-04 17:16:10 +000055 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 Chapovalov7e0299e2018-12-04 17:16:10 +000071 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_);
Danil Chapovalov44727b42018-11-22 11:28:45 +010079 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 Chapovalov7e0299e2018-12-04 17:16:10 +000083 if (!in_order && enable_retransmit_detection_ &&
84 IsRetransmitOfOldPacket(packet, now_ms)) {
85 receive_counters_.retransmitted.AddPacket(packet);
86 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +000087
Danil Chapovalov7e0299e2018-12-04 17:16:10 +000088 if (receive_counters_.transmitted.packets == 1) {
89 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
Danil Chapovalov7e0299e2018-12-04 17:16:10 +000093 // 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 // Wrong if we use RetransmitOfOldPacket.
97 if (receive_counters_.transmitted.packets > 1 &&
98 received_seq_max_ > packet.SequenceNumber()) {
99 // Wrap around detected.
100 received_seq_wraps_++;
101 }
102 // New max.
103 received_seq_max_ = packet.SequenceNumber();
104
105 // If new time stamp and more than one in-order packet received, calculate
106 // new jitter statistics.
107 if (packet.Timestamp() != last_received_timestamp_ &&
108 (receive_counters_.transmitted.packets -
109 receive_counters_.retransmitted.packets) > 1) {
110 UpdateJitter(packet, now_ms);
111 }
112 last_received_timestamp_ = packet.Timestamp();
113 last_receive_time_ms_ = now_ms;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000114 }
danilchapec86be02017-08-14 05:51:02 -0700115 return receive_counters_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000116}
117
Niels Möllerdbb988b2018-11-15 08:05:16 +0100118void StreamStatisticianImpl::UpdateJitter(const RtpPacketReceived& packet,
Danil Chapovalov856cf222018-11-26 10:20:01 +0100119 int64_t receive_time_ms) {
120 int64_t receive_diff_ms = receive_time_ms - last_receive_time_ms_;
121 RTC_DCHECK_GE(receive_diff_ms, 0);
122 uint32_t receive_diff_rtp = static_cast<uint32_t>(
123 (receive_diff_ms * packet.payload_type_frequency()) / 1000);
124 int32_t time_diff_samples =
125 receive_diff_rtp - (packet.Timestamp() - last_received_timestamp_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000126
kwibergfd8be342016-05-14 19:44:11 -0700127 time_diff_samples = std::abs(time_diff_samples);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000128
129 // lib_jingle sometimes deliver crazy jumps in TS for the same stream.
130 // If this happens, don't update jitter value. Use 5 secs video frequency
131 // as the threshold.
132 if (time_diff_samples < 450000) {
133 // Note we calculate in Q4 to avoid using float.
134 int32_t jitter_diff_q4 = (time_diff_samples << 4) - jitter_q4_;
135 jitter_q4_ += ((jitter_diff_q4 + 8) >> 4);
136 }
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000137}
138
Niels Möllerdbb988b2018-11-15 08:05:16 +0100139void StreamStatisticianImpl::FecPacketReceived(
140 const RtpPacketReceived& packet) {
danilchapec86be02017-08-14 05:51:02 -0700141 StreamDataCounters counters;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000142 {
danilchap7c9426c2016-04-14 03:05:31 -0700143 rtc::CritScope cs(&stream_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +0100144 receive_counters_.fec.AddPacket(packet);
danilchapec86be02017-08-14 05:51:02 -0700145 counters = receive_counters_;
sprang@webrtc.orga45cac02014-01-27 16:22:08 +0000146 }
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100147 if (rtp_callback_)
148 rtp_callback_->DataCountersUpdated(counters, ssrc_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000149}
150
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000151void StreamStatisticianImpl::SetMaxReorderingThreshold(
152 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700153 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000154 max_reordering_threshold_ = max_reordering_threshold;
155}
156
Niels Möller5304a322018-08-27 13:27:05 +0200157void StreamStatisticianImpl::EnableRetransmitDetection(bool enable) {
158 rtc::CritScope cs(&stream_lock_);
159 enable_retransmit_detection_ = enable;
160}
161
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000162bool StreamStatisticianImpl::GetStatistics(RtcpStatistics* statistics,
Qingsi Wang2370b082018-08-21 14:24:26 -0700163 bool reset) {
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000164 {
danilchap7c9426c2016-04-14 03:05:31 -0700165 rtc::CritScope cs(&stream_lock_);
Danil Chapovalov7e0299e2018-12-04 17:16:10 +0000166 if (received_seq_first_ == 0 &&
167 receive_counters_.transmitted.payload_bytes == 0) {
168 // We have not received anything.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000169 return false;
170 }
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000171
Qingsi Wang2370b082018-08-21 14:24:26 -0700172 if (!reset) {
173 if (last_report_inorder_packets_ == 0) {
174 // No report.
175 return false;
176 }
177 // Just get last report.
178 *statistics = last_reported_statistics_;
179 return true;
180 }
181
182 *statistics = CalculateRtcpStatistics();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000183 }
184
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100185 if (rtcp_callback_)
186 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_);
Danil Chapovalov856cf222018-11-26 10:20:01 +0100194 if (clock_->TimeInMilliseconds() - last_receive_time_ms_ >=
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200195 kStatisticsTimeoutMs) {
196 // Not active.
197 return false;
198 }
Danil Chapovalov7e0299e2018-12-04 17:16:10 +0000199 if (received_seq_first_ == 0 &&
200 receive_counters_.transmitted.payload_bytes == 0) {
201 // We have not received anything.
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200202 return false;
203 }
204
Qingsi Wang2370b082018-08-21 14:24:26 -0700205 *statistics = CalculateRtcpStatistics();
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200206 }
207
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100208 if (rtcp_callback_)
209 rtcp_callback_->StatisticsUpdated(*statistics, ssrc_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200210 return true;
211}
212
Qingsi Wang2370b082018-08-21 14:24:26 -0700213RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics() {
214 RtcpStatistics stats;
Danil Chapovalov7e0299e2018-12-04 17:16:10 +0000215
216 if (last_report_inorder_packets_ == 0) {
217 // First time we send a report.
218 last_report_seq_max_ = received_seq_first_ - 1;
219 }
220
Qingsi Wang2370b082018-08-21 14:24:26 -0700221 // Calculate fraction lost.
Danil Chapovalov7e0299e2018-12-04 17:16:10 +0000222 uint16_t exp_since_last = (received_seq_max_ - last_report_seq_max_);
223
224 if (last_report_seq_max_ > received_seq_max_) {
225 // Can we assume that the seq_num can't go decrease over a full RTCP period?
226 exp_since_last = 0;
227 }
Qingsi Wang2370b082018-08-21 14:24:26 -0700228
229 // Number of received RTP packets since last report, counts all packets but
230 // not re-transmissions.
231 uint32_t rec_since_last = (receive_counters_.transmitted.packets -
232 receive_counters_.retransmitted.packets) -
233 last_report_inorder_packets_;
234
235 // With NACK we don't know the expected retransmissions during the last
236 // second. We know how many "old" packets we have received. We just count
237 // the number of old received to estimate the loss, but it still does not
238 // guarantee an exact number since we run this based on time triggered by
239 // sending of an RTP packet. This should have a minimum effect.
240
241 // With NACK we don't count old packets as received since they are
242 // re-transmitted. We use RTT to decide if a packet is re-ordered or
243 // re-transmitted.
244 uint32_t retransmitted_packets =
245 receive_counters_.retransmitted.packets - last_report_old_packets_;
246 rec_since_last += retransmitted_packets;
247
248 int32_t missing = 0;
249 if (exp_since_last > rec_since_last) {
250 missing = (exp_since_last - rec_since_last);
251 }
252 uint8_t local_fraction_lost = 0;
253 if (exp_since_last) {
254 // Scale 0 to 255, where 255 is 100% loss.
255 local_fraction_lost = static_cast<uint8_t>(255 * missing / exp_since_last);
256 }
257 stats.fraction_lost = local_fraction_lost;
258
259 // We need a counter for cumulative loss too.
260 // TODO(danilchap): Ensure cumulative loss is below maximum value of 2^24.
261 cumulative_loss_ += missing;
262 stats.packets_lost = cumulative_loss_;
263 stats.extended_highest_sequence_number =
Danil Chapovalov7e0299e2018-12-04 17:16:10 +0000264 (received_seq_wraps_ << 16) + received_seq_max_;
Qingsi Wang2370b082018-08-21 14:24:26 -0700265 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
266 stats.jitter = jitter_q4_ >> 4;
267
268 // Store this report.
269 last_reported_statistics_ = stats;
270
271 // Only for report blocks in RTCP SR and RR.
272 last_report_inorder_packets_ = receive_counters_.transmitted.packets -
273 receive_counters_.retransmitted.packets;
274 last_report_old_packets_ = receive_counters_.retransmitted.packets;
275 last_report_seq_max_ = received_seq_max_;
gaetano.carlucci61050f62016-09-30 06:29:54 -0700276 BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts",
gaetano.carlucci52a57032016-09-14 05:04:36 -0700277 clock_->TimeInMilliseconds(),
Qingsi Wang2370b082018-08-21 14:24:26 -0700278 cumulative_loss_, ssrc_);
gaetano.carlucci52a57032016-09-14 05:04:36 -0700279 BWE_TEST_LOGGING_PLOT_WITH_SSRC(
gaetano.carlucci61050f62016-09-30 06:29:54 -0700280 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700281 (received_seq_max_ - received_seq_first_), ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000282
Qingsi Wang2370b082018-08-21 14:24:26 -0700283 return stats;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000284}
285
Yves Gerey665174f2018-06-19 15:03:05 +0200286void StreamStatisticianImpl::GetDataCounters(size_t* bytes_received,
287 uint32_t* packets_received) const {
danilchap7c9426c2016-04-14 03:05:31 -0700288 rtc::CritScope cs(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000289 if (bytes_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000290 *bytes_received = receive_counters_.transmitted.payload_bytes +
291 receive_counters_.transmitted.header_bytes +
292 receive_counters_.transmitted.padding_bytes;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000293 }
294 if (packets_received) {
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +0000295 *packets_received = receive_counters_.transmitted.packets;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000296 }
297}
298
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000299void StreamStatisticianImpl::GetReceiveStreamDataCounters(
300 StreamDataCounters* data_counters) const {
danilchap7c9426c2016-04-14 03:05:31 -0700301 rtc::CritScope cs(&stream_lock_);
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000302 *data_counters = receive_counters_;
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000303}
304
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000305uint32_t StreamStatisticianImpl::BitrateReceived() const {
danilchap7c9426c2016-04-14 03:05:31 -0700306 rtc::CritScope cs(&stream_lock_);
sprangcd349d92016-07-13 09:11:28 -0700307 return incoming_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000308}
309
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000310bool StreamStatisticianImpl::IsRetransmitOfOldPacket(
Danil Chapovalov44727b42018-11-22 11:28:45 +0100311 const RtpPacketReceived& packet,
312 int64_t now_ms) const {
Niels Möllerdbb988b2018-11-15 08:05:16 +0100313 uint32_t frequency_khz = packet.payload_type_frequency() / 1000;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100314 RTC_DCHECK_GT(frequency_khz, 0);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000315
Danil Chapovalov44727b42018-11-22 11:28:45 +0100316 int64_t time_diff_ms = now_ms - last_receive_time_ms_;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000317
318 // Diff in time stamp since last received in order.
Niels Möllerdbb988b2018-11-15 08:05:16 +0100319 uint32_t timestamp_diff = packet.Timestamp() - last_received_timestamp_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000320 uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000321
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000322 int64_t max_delay_ms = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000323
Niels Möllereda00872018-05-23 13:54:51 +0200324 // Jitter standard deviation in samples.
325 float jitter_std = sqrt(static_cast<float>(jitter_q4_ >> 4));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000326
Niels Möllereda00872018-05-23 13:54:51 +0200327 // 2 times the standard deviation => 95% confidence.
328 // And transform to milliseconds by dividing by the frequency in kHz.
329 max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz);
330
331 // Min max_delay_ms is 1.
332 if (max_delay_ms == 0) {
333 max_delay_ms = 1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000334 }
335 return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms;
336}
337
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100338std::unique_ptr<ReceiveStatistics> ReceiveStatistics::Create(
339 Clock* clock,
340 RtcpStatisticsCallback* rtcp_callback,
341 StreamDataCountersCallback* rtp_callback) {
342 return absl::make_unique<ReceiveStatisticsImpl>(clock, rtcp_callback,
343 rtp_callback);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000344}
345
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100346ReceiveStatisticsImpl::ReceiveStatisticsImpl(
347 Clock* clock,
348 RtcpStatisticsCallback* rtcp_callback,
349 StreamDataCountersCallback* rtp_callback)
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000350 : clock_(clock),
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100351 last_returned_ssrc_(0),
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100352 max_reordering_threshold_(kDefaultMaxReorderingThreshold),
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100353 rtcp_stats_callback_(rtcp_callback),
354 rtp_stats_callback_(rtp_callback) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000355
356ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
357 while (!statisticians_.empty()) {
358 delete statisticians_.begin()->second;
359 statisticians_.erase(statisticians_.begin());
360 }
361}
362
Niels Möller1f3206c2018-09-14 08:26:32 +0200363void ReceiveStatisticsImpl::OnRtpPacket(const RtpPacketReceived& packet) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000364 StreamStatisticianImpl* impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000365 {
danilchap7c9426c2016-04-14 03:05:31 -0700366 rtc::CritScope cs(&receive_statistics_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +0100367 auto it = statisticians_.find(packet.Ssrc());
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000368 if (it != statisticians_.end()) {
369 impl = it->second;
370 } else {
Niels Möller5304a322018-08-27 13:27:05 +0200371 impl = new StreamStatisticianImpl(
Niels Möllerdbb988b2018-11-15 08:05:16 +0100372 packet.Ssrc(), clock_, /* enable_retransmit_detection = */ false,
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100373 max_reordering_threshold_, rtcp_stats_callback_, rtp_stats_callback_);
Niels Möllerdbb988b2018-11-15 08:05:16 +0100374 statisticians_[packet.Ssrc()] = impl;
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000375 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000376 }
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000377 // StreamStatisticianImpl instance is created once and only destroyed when
378 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
379 // it's own locking so don't hold receive_statistics_lock_ (potential
380 // deadlock).
Niels Möllerdbb988b2018-11-15 08:05:16 +0100381 impl->OnRtpPacket(packet);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000382}
383
Niels Möller1f3206c2018-09-14 08:26:32 +0200384void ReceiveStatisticsImpl::FecPacketReceived(const RtpPacketReceived& packet) {
danilchapec86be02017-08-14 05:51:02 -0700385 StreamStatisticianImpl* impl;
386 {
387 rtc::CritScope cs(&receive_statistics_lock_);
Niels Möller1f3206c2018-09-14 08:26:32 +0200388 auto it = statisticians_.find(packet.Ssrc());
danilchapec86be02017-08-14 05:51:02 -0700389 // Ignore FEC if it is the first packet.
390 if (it == statisticians_.end())
391 return;
392 impl = it->second;
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000393 }
Niels Möllerdbb988b2018-11-15 08:05:16 +0100394 impl->FecPacketReceived(packet);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000395}
396
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000397StreamStatistician* ReceiveStatisticsImpl::GetStatistician(
398 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700399 rtc::CritScope cs(&receive_statistics_lock_);
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200400 auto it = statisticians_.find(ssrc);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000401 if (it == statisticians_.end())
402 return NULL;
403 return it->second;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000404}
405
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000406void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
407 int max_reordering_threshold) {
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100408 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
409 {
410 rtc::CritScope cs(&receive_statistics_lock_);
411 max_reordering_threshold_ = max_reordering_threshold;
412 statisticians = statisticians_;
413 }
414 for (auto& statistician : statisticians) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200415 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000416 }
417}
418
Niels Möller5304a322018-08-27 13:27:05 +0200419void ReceiveStatisticsImpl::EnableRetransmitDetection(uint32_t ssrc,
420 bool enable) {
421 StreamStatisticianImpl* impl;
422 {
423 rtc::CritScope cs(&receive_statistics_lock_);
424 StreamStatisticianImpl*& impl_ref = statisticians_[ssrc];
425 if (impl_ref == nullptr) { // new element
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100426 impl_ref = new StreamStatisticianImpl(
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100427 ssrc, clock_, enable, max_reordering_threshold_, rtcp_stats_callback_,
428 rtp_stats_callback_);
Niels Möller5304a322018-08-27 13:27:05 +0200429 return;
430 }
431 impl = impl_ref;
432 }
433 impl->EnableRetransmitDetection(enable);
434}
435
danilchap0bc84232017-08-11 08:12:54 -0700436std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700437 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200438 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
439 {
440 rtc::CritScope cs(&receive_statistics_lock_);
441 statisticians = statisticians_;
442 }
danilchapf5f793c2017-07-27 04:44:18 -0700443 std::vector<rtcp::ReportBlock> result;
444 result.reserve(std::min(max_blocks, statisticians.size()));
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100445 auto add_report_block = [&result](uint32_t media_ssrc,
446 StreamStatisticianImpl* statistician) {
danilchapf5f793c2017-07-27 04:44:18 -0700447 // Do we have receive statistics to send?
448 RtcpStatistics stats;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100449 if (!statistician->GetActiveStatisticsAndReset(&stats))
450 return;
danilchapf5f793c2017-07-27 04:44:18 -0700451 result.emplace_back();
452 rtcp::ReportBlock& block = result.back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100453 block.SetMediaSsrc(media_ssrc);
danilchapf5f793c2017-07-27 04:44:18 -0700454 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700455 if (!block.SetCumulativeLost(stats.packets_lost)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100456 RTC_LOG(LS_WARNING) << "Cumulative lost is oversized.";
danilchapf5f793c2017-07-27 04:44:18 -0700457 result.pop_back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100458 return;
danilchapf5f793c2017-07-27 04:44:18 -0700459 }
srte186d9c32017-08-04 05:03:53 -0700460 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700461 block.SetJitter(stats.jitter);
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100462 };
463
464 const auto start_it = statisticians.upper_bound(last_returned_ssrc_);
465 for (auto it = start_it;
466 result.size() < max_blocks && it != statisticians.end(); ++it)
467 add_report_block(it->first, it->second);
468 for (auto it = statisticians.begin();
469 result.size() < max_blocks && it != start_it; ++it)
470 add_report_block(it->first, it->second);
471
472 if (!result.empty())
473 last_returned_ssrc_ = result.back().source_ssrc();
danilchapf5f793c2017-07-27 04:44:18 -0700474 return result;
475}
476
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000477} // namespace webrtc