blob: e61719728ea52caa5c575e4e659fab77514366a3 [file] [log] [blame]
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/source/receive_statistics_impl.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000012
Oleh Prypin19929582019-04-23 08:50:04 +020013#include <cmath>
kwibergfd8be342016-05-14 19:44:11 -070014#include <cstdlib>
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +010015#include <memory>
danilchapf5f793c2017-07-27 04:44:18 -070016#include <vector>
kwibergfd8be342016-05-14 19:44:11 -070017
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +010018#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/remote_bitrate_estimator/test/bwe_test_logging.h"
Niels Möller1f3206c2018-09-14 08:26:32 +020020#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
22#include "modules/rtp_rtcp/source/time_util.h"
23#include "rtc_base/logging.h"
24#include "system_wrappers/include/clock.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000025
26namespace webrtc {
27
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000028const int64_t kStatisticsTimeoutMs = 8000;
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000029const int64_t kStatisticsProcessIntervalMs = 1000;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000030
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000031StreamStatistician::~StreamStatistician() {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000032
Niels Möllerd7819652019-08-13 14:43:02 +020033StreamStatisticianImpl::StreamStatisticianImpl(uint32_t ssrc,
34 Clock* clock,
35 int max_reordering_threshold)
danilchapec86be02017-08-14 05:51:02 -070036 : ssrc_(ssrc),
37 clock_(clock),
sprangcd349d92016-07-13 09:11:28 -070038 incoming_bitrate_(kStatisticsProcessIntervalMs,
39 RateStatistics::kBpsScale),
Danil Chapovalovebb50c22018-11-22 14:04:02 +010040 max_reordering_threshold_(max_reordering_threshold),
Niels Möller87da1092019-05-24 14:04:28 +020041 enable_retransmit_detection_(false),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000042 jitter_q4_(0),
Qingsi Wang2370b082018-08-21 14:24:26 -070043 cumulative_loss_(0),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000044 last_receive_time_ms_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000045 last_received_timestamp_(0),
wu@webrtc.org822fbd82013-08-15 23:38:54 +000046 received_seq_first_(0),
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000047 received_seq_max_(-1),
Qingsi Wang2370b082018-08-21 14:24:26 -070048 last_report_inorder_packets_(0),
49 last_report_old_packets_(0),
Niels Möllerd7819652019-08-13 14:43:02 +020050 last_report_seq_max_(-1) {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000051
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010052StreamStatisticianImpl::~StreamStatisticianImpl() = default;
53
Niels Möllerdbb988b2018-11-15 08:05:16 +010054void StreamStatisticianImpl::OnRtpPacket(const RtpPacketReceived& packet) {
Niels Möllerd7819652019-08-13 14:43:02 +020055 UpdateCounters(packet);
sprang@webrtc.orga45cac02014-01-27 16:22:08 +000056}
57
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000058bool StreamStatisticianImpl::UpdateOutOfOrder(const RtpPacketReceived& packet,
59 int64_t sequence_number,
60 int64_t now_ms) {
61 RTC_DCHECK_EQ(sequence_number,
62 seq_unwrapper_.UnwrapWithoutUpdate(packet.SequenceNumber()));
63
64 // Check if |packet| is second packet of a stream restart.
65 if (received_seq_out_of_order_) {
66 uint16_t expected_sequence_number = *received_seq_out_of_order_ + 1;
67 received_seq_out_of_order_ = absl::nullopt;
68 if (packet.SequenceNumber() == expected_sequence_number) {
69 // Ignore sequence number gap caused by stream restart for next packet
70 // loss calculation.
71 last_report_seq_max_ = sequence_number;
72 last_report_inorder_packets_ = receive_counters_.transmitted.packets -
73 receive_counters_.retransmitted.packets;
74 // As final part of stream restart consider |packet| is not out of order.
75 return false;
76 }
77 }
78
79 if (std::abs(sequence_number - received_seq_max_) >
80 max_reordering_threshold_) {
81 // Sequence number gap looks too large, wait until next packet to check
82 // for a stream restart.
83 received_seq_out_of_order_ = packet.SequenceNumber();
84 return true;
85 }
86
87 if (sequence_number > received_seq_max_)
88 return false;
89
90 // Old out of order packet, may be retransmit.
91 if (enable_retransmit_detection_ && IsRetransmitOfOldPacket(packet, now_ms))
92 receive_counters_.retransmitted.AddPacket(packet);
93 return true;
94}
95
danilchapec86be02017-08-14 05:51:02 -070096StreamDataCounters StreamStatisticianImpl::UpdateCounters(
Danil Chapovalov44727b42018-11-22 11:28:45 +010097 const RtpPacketReceived& packet) {
98 rtc::CritScope cs(&stream_lock_);
Niels Möllerdbb988b2018-11-15 08:05:16 +010099 RTC_DCHECK_EQ(ssrc_, packet.Ssrc());
Danil Chapovalov44727b42018-11-22 11:28:45 +0100100 int64_t now_ms = clock_->TimeInMilliseconds();
101
102 incoming_bitrate_.Update(packet.size(), now_ms);
Henrik Boströmcb755b02019-04-02 15:11:48 +0200103 receive_counters_.last_packet_received_timestamp_ms = now_ms;
Niels Möllerdbb988b2018-11-15 08:05:16 +0100104 receive_counters_.transmitted.AddPacket(packet);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000105
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000106 int64_t sequence_number =
107 seq_unwrapper_.UnwrapWithoutUpdate(packet.SequenceNumber());
108 if (!ReceivedRtpPacket()) {
109 received_seq_first_ = sequence_number;
110 last_report_seq_max_ = sequence_number - 1;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100111 receive_counters_.first_packet_time_ms = now_ms;
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000112 } else if (UpdateOutOfOrder(packet, sequence_number, now_ms)) {
113 return receive_counters_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000114 }
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000115 // In order packet.
116 received_seq_max_ = sequence_number;
117 seq_unwrapper_.UpdateLast(sequence_number);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000118
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000119 // If new time stamp and more than one in-order packet received, calculate
120 // new jitter statistics.
121 if (packet.Timestamp() != last_received_timestamp_ &&
122 (receive_counters_.transmitted.packets -
123 receive_counters_.retransmitted.packets) > 1) {
124 UpdateJitter(packet, now_ms);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000125 }
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000126 last_received_timestamp_ = packet.Timestamp();
127 last_receive_time_ms_ = now_ms;
danilchapec86be02017-08-14 05:51:02 -0700128 return receive_counters_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000129}
130
Niels Möllerdbb988b2018-11-15 08:05:16 +0100131void StreamStatisticianImpl::UpdateJitter(const RtpPacketReceived& packet,
Danil Chapovalov856cf222018-11-26 10:20:01 +0100132 int64_t receive_time_ms) {
133 int64_t receive_diff_ms = receive_time_ms - last_receive_time_ms_;
134 RTC_DCHECK_GE(receive_diff_ms, 0);
135 uint32_t receive_diff_rtp = static_cast<uint32_t>(
136 (receive_diff_ms * packet.payload_type_frequency()) / 1000);
137 int32_t time_diff_samples =
138 receive_diff_rtp - (packet.Timestamp() - last_received_timestamp_);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000139
kwibergfd8be342016-05-14 19:44:11 -0700140 time_diff_samples = std::abs(time_diff_samples);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000141
142 // lib_jingle sometimes deliver crazy jumps in TS for the same stream.
143 // If this happens, don't update jitter value. Use 5 secs video frequency
144 // as the threshold.
145 if (time_diff_samples < 450000) {
146 // Note we calculate in Q4 to avoid using float.
147 int32_t jitter_diff_q4 = (time_diff_samples << 4) - jitter_q4_;
148 jitter_q4_ += ((jitter_diff_q4 + 8) >> 4);
149 }
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000150}
151
Niels Möllerdbb988b2018-11-15 08:05:16 +0100152void StreamStatisticianImpl::FecPacketReceived(
153 const RtpPacketReceived& packet) {
Niels Möllerd7819652019-08-13 14:43:02 +0200154 rtc::CritScope cs(&stream_lock_);
155 receive_counters_.fec.AddPacket(packet);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000156}
157
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000158void StreamStatisticianImpl::SetMaxReorderingThreshold(
159 int max_reordering_threshold) {
danilchap7c9426c2016-04-14 03:05:31 -0700160 rtc::CritScope cs(&stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000161 max_reordering_threshold_ = max_reordering_threshold;
162}
163
Niels Möller5304a322018-08-27 13:27:05 +0200164void StreamStatisticianImpl::EnableRetransmitDetection(bool enable) {
165 rtc::CritScope cs(&stream_lock_);
166 enable_retransmit_detection_ = enable;
167}
168
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000169bool StreamStatisticianImpl::GetStatistics(RtcpStatistics* statistics,
Qingsi Wang2370b082018-08-21 14:24:26 -0700170 bool reset) {
Niels Möller12ebfa62019-08-06 16:04:12 +0200171 rtc::CritScope cs(&stream_lock_);
172 if (!ReceivedRtpPacket()) {
173 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000174 }
175
Niels Möller12ebfa62019-08-06 16:04:12 +0200176 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();
187
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000188 return true;
189}
190
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200191bool StreamStatisticianImpl::GetActiveStatisticsAndReset(
192 RtcpStatistics* statistics) {
Niels Möller12ebfa62019-08-06 16:04:12 +0200193 rtc::CritScope cs(&stream_lock_);
194 if (clock_->TimeInMilliseconds() - last_receive_time_ms_ >=
195 kStatisticsTimeoutMs) {
196 // Not active.
197 return false;
198 }
199 if (!ReceivedRtpPacket()) {
200 return false;
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200201 }
202
Niels Möller12ebfa62019-08-06 16:04:12 +0200203 *statistics = CalculateRtcpStatistics();
204
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200205 return true;
206}
207
Qingsi Wang2370b082018-08-21 14:24:26 -0700208RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics() {
209 RtcpStatistics stats;
Qingsi Wang2370b082018-08-21 14:24:26 -0700210 // Calculate fraction lost.
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000211 int64_t exp_since_last = received_seq_max_ - last_report_seq_max_;
212 RTC_DCHECK_GE(exp_since_last, 0);
Qingsi Wang2370b082018-08-21 14:24:26 -0700213
214 // Number of received RTP packets since last report, counts all packets but
215 // not re-transmissions.
216 uint32_t rec_since_last = (receive_counters_.transmitted.packets -
217 receive_counters_.retransmitted.packets) -
218 last_report_inorder_packets_;
219
220 // With NACK we don't know the expected retransmissions during the last
221 // second. We know how many "old" packets we have received. We just count
222 // the number of old received to estimate the loss, but it still does not
223 // guarantee an exact number since we run this based on time triggered by
224 // sending of an RTP packet. This should have a minimum effect.
225
226 // With NACK we don't count old packets as received since they are
227 // re-transmitted. We use RTT to decide if a packet is re-ordered or
228 // re-transmitted.
229 uint32_t retransmitted_packets =
230 receive_counters_.retransmitted.packets - last_report_old_packets_;
231 rec_since_last += retransmitted_packets;
232
233 int32_t missing = 0;
234 if (exp_since_last > rec_since_last) {
235 missing = (exp_since_last - rec_since_last);
236 }
237 uint8_t local_fraction_lost = 0;
238 if (exp_since_last) {
239 // Scale 0 to 255, where 255 is 100% loss.
240 local_fraction_lost = static_cast<uint8_t>(255 * missing / exp_since_last);
241 }
242 stats.fraction_lost = local_fraction_lost;
243
244 // We need a counter for cumulative loss too.
245 // TODO(danilchap): Ensure cumulative loss is below maximum value of 2^24.
246 cumulative_loss_ += missing;
247 stats.packets_lost = cumulative_loss_;
248 stats.extended_highest_sequence_number =
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000249 static_cast<uint32_t>(received_seq_max_);
Qingsi Wang2370b082018-08-21 14:24:26 -0700250 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
251 stats.jitter = jitter_q4_ >> 4;
252
253 // Store this report.
254 last_reported_statistics_ = stats;
255
256 // Only for report blocks in RTCP SR and RR.
257 last_report_inorder_packets_ = receive_counters_.transmitted.packets -
258 receive_counters_.retransmitted.packets;
259 last_report_old_packets_ = receive_counters_.retransmitted.packets;
260 last_report_seq_max_ = received_seq_max_;
gaetano.carlucci61050f62016-09-30 06:29:54 -0700261 BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts",
gaetano.carlucci52a57032016-09-14 05:04:36 -0700262 clock_->TimeInMilliseconds(),
Qingsi Wang2370b082018-08-21 14:24:26 -0700263 cumulative_loss_, ssrc_);
gaetano.carlucci52a57032016-09-14 05:04:36 -0700264 BWE_TEST_LOGGING_PLOT_WITH_SSRC(
gaetano.carlucci61050f62016-09-30 06:29:54 -0700265 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700266 (received_seq_max_ - received_seq_first_), ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000267
Qingsi Wang2370b082018-08-21 14:24:26 -0700268 return stats;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000269}
270
Niels Möller9a9f18a2019-08-02 13:52:37 +0200271absl::optional<int> StreamStatisticianImpl::GetFractionLostInPercent() const {
272 rtc::CritScope cs(&stream_lock_);
273 if (received_seq_max_ < 0) {
274 return absl::nullopt;
275 }
276 int64_t expected_packets = 1 + received_seq_max_ - received_seq_first_;
277 if (expected_packets <= 0) {
278 return absl::nullopt;
279 }
280 // Spec allows negative cumulative loss, but implementation uses uint32_t, so
281 // this expression is always non-negative.
282 return 100 * static_cast<int64_t>(cumulative_loss_) / expected_packets;
283}
284
Niels Möller58b496b2019-08-12 12:16:31 +0200285StreamDataCounters StreamStatisticianImpl::GetReceiveStreamDataCounters()
286 const {
danilchap7c9426c2016-04-14 03:05:31 -0700287 rtc::CritScope cs(&stream_lock_);
Niels Möller58b496b2019-08-12 12:16:31 +0200288 return receive_counters_;
asapersson@webrtc.orgd952c402014-11-27 07:38:56 +0000289}
290
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000291uint32_t StreamStatisticianImpl::BitrateReceived() const {
danilchap7c9426c2016-04-14 03:05:31 -0700292 rtc::CritScope cs(&stream_lock_);
sprangcd349d92016-07-13 09:11:28 -0700293 return incoming_bitrate_.Rate(clock_->TimeInMilliseconds()).value_or(0);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000294}
295
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000296bool StreamStatisticianImpl::IsRetransmitOfOldPacket(
Danil Chapovalov44727b42018-11-22 11:28:45 +0100297 const RtpPacketReceived& packet,
298 int64_t now_ms) const {
Niels Möllerdbb988b2018-11-15 08:05:16 +0100299 uint32_t frequency_khz = packet.payload_type_frequency() / 1000;
Danil Chapovalov44727b42018-11-22 11:28:45 +0100300 RTC_DCHECK_GT(frequency_khz, 0);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000301
Danil Chapovalov44727b42018-11-22 11:28:45 +0100302 int64_t time_diff_ms = now_ms - last_receive_time_ms_;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000303
304 // Diff in time stamp since last received in order.
Niels Möllerdbb988b2018-11-15 08:05:16 +0100305 uint32_t timestamp_diff = packet.Timestamp() - last_received_timestamp_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000306 uint32_t rtp_time_stamp_diff_ms = timestamp_diff / frequency_khz;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000307
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000308 int64_t max_delay_ms = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000309
Niels Möllereda00872018-05-23 13:54:51 +0200310 // Jitter standard deviation in samples.
Oleh Prypin19929582019-04-23 08:50:04 +0200311 float jitter_std = std::sqrt(static_cast<float>(jitter_q4_ >> 4));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000312
Niels Möllereda00872018-05-23 13:54:51 +0200313 // 2 times the standard deviation => 95% confidence.
314 // And transform to milliseconds by dividing by the frequency in kHz.
315 max_delay_ms = static_cast<int64_t>((2 * jitter_std) / frequency_khz);
316
317 // Min max_delay_ms is 1.
318 if (max_delay_ms == 0) {
319 max_delay_ms = 1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000320 }
321 return time_diff_ms > rtp_time_stamp_diff_ms + max_delay_ms;
322}
323
Niels Möllerd7819652019-08-13 14:43:02 +0200324std::unique_ptr<ReceiveStatistics> ReceiveStatistics::Create(Clock* clock) {
325 return absl::make_unique<ReceiveStatisticsImpl>(clock);
326}
327
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100328std::unique_ptr<ReceiveStatistics> ReceiveStatistics::Create(
329 Clock* clock,
Niels Möller12ebfa62019-08-06 16:04:12 +0200330 StreamDataCountersCallback* rtp_callback) {
Niels Möllerd7819652019-08-13 14:43:02 +0200331 RTC_CHECK(rtp_callback == nullptr);
332 return Create(clock);
Niels Möller12ebfa62019-08-06 16:04:12 +0200333}
334
335std::unique_ptr<ReceiveStatistics> ReceiveStatistics::Create(
336 Clock* clock,
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100337 RtcpStatisticsCallback* rtcp_callback,
338 StreamDataCountersCallback* rtp_callback) {
Niels Möller12ebfa62019-08-06 16:04:12 +0200339 RTC_CHECK(rtcp_callback == nullptr);
Niels Möllerd7819652019-08-13 14:43:02 +0200340 RTC_CHECK(rtp_callback == nullptr);
341 return Create(clock);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000342}
343
Niels Möllerd7819652019-08-13 14:43:02 +0200344ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock)
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000345 : clock_(clock),
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100346 last_returned_ssrc_(0),
Niels Möllerd7819652019-08-13 14:43:02 +0200347 max_reordering_threshold_(kDefaultMaxReorderingThreshold) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000348
349ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
350 while (!statisticians_.empty()) {
351 delete statisticians_.begin()->second;
352 statisticians_.erase(statisticians_.begin());
353 }
354}
355
Niels Möller1f3206c2018-09-14 08:26:32 +0200356void ReceiveStatisticsImpl::OnRtpPacket(const RtpPacketReceived& packet) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000357 // StreamStatisticianImpl instance is created once and only destroyed when
358 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
359 // it's own locking so don't hold receive_statistics_lock_ (potential
360 // deadlock).
Niels Möller87da1092019-05-24 14:04:28 +0200361 GetOrCreateStatistician(packet.Ssrc())->OnRtpPacket(packet);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000362}
363
Niels Möller1f3206c2018-09-14 08:26:32 +0200364void ReceiveStatisticsImpl::FecPacketReceived(const RtpPacketReceived& packet) {
Niels Möller87da1092019-05-24 14:04:28 +0200365 StreamStatisticianImpl* impl = GetStatistician(packet.Ssrc());
366 // Ignore FEC if it is the first packet.
367 if (impl) {
368 impl->FecPacketReceived(packet);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000369 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000370}
371
Niels Möller87da1092019-05-24 14:04:28 +0200372StreamStatisticianImpl* ReceiveStatisticsImpl::GetStatistician(
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000373 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700374 rtc::CritScope cs(&receive_statistics_lock_);
Niels Möller87da1092019-05-24 14:04:28 +0200375 const auto& it = statisticians_.find(ssrc);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000376 if (it == statisticians_.end())
377 return NULL;
378 return it->second;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000379}
380
Niels Möller87da1092019-05-24 14:04:28 +0200381StreamStatisticianImpl* ReceiveStatisticsImpl::GetOrCreateStatistician(
382 uint32_t ssrc) {
383 rtc::CritScope cs(&receive_statistics_lock_);
384 StreamStatisticianImpl*& impl = statisticians_[ssrc];
385 if (impl == nullptr) { // new element
Niels Möllerd7819652019-08-13 14:43:02 +0200386 impl = new StreamStatisticianImpl(ssrc, clock_, max_reordering_threshold_);
Niels Möller87da1092019-05-24 14:04:28 +0200387 }
388 return impl;
389}
390
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000391void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
392 int max_reordering_threshold) {
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100393 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
394 {
395 rtc::CritScope cs(&receive_statistics_lock_);
396 max_reordering_threshold_ = max_reordering_threshold;
397 statisticians = statisticians_;
398 }
399 for (auto& statistician : statisticians) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200400 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000401 }
402}
403
Niels Möller87da1092019-05-24 14:04:28 +0200404void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
405 uint32_t ssrc,
406 int max_reordering_threshold) {
407 GetOrCreateStatistician(ssrc)->SetMaxReorderingThreshold(
408 max_reordering_threshold);
409}
410
Niels Möller5304a322018-08-27 13:27:05 +0200411void ReceiveStatisticsImpl::EnableRetransmitDetection(uint32_t ssrc,
412 bool enable) {
Niels Möller87da1092019-05-24 14:04:28 +0200413 GetOrCreateStatistician(ssrc)->EnableRetransmitDetection(enable);
Niels Möller5304a322018-08-27 13:27:05 +0200414}
415
danilchap0bc84232017-08-11 08:12:54 -0700416std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700417 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200418 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
419 {
420 rtc::CritScope cs(&receive_statistics_lock_);
421 statisticians = statisticians_;
422 }
danilchapf5f793c2017-07-27 04:44:18 -0700423 std::vector<rtcp::ReportBlock> result;
424 result.reserve(std::min(max_blocks, statisticians.size()));
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100425 auto add_report_block = [&result](uint32_t media_ssrc,
426 StreamStatisticianImpl* statistician) {
danilchapf5f793c2017-07-27 04:44:18 -0700427 // Do we have receive statistics to send?
428 RtcpStatistics stats;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100429 if (!statistician->GetActiveStatisticsAndReset(&stats))
430 return;
danilchapf5f793c2017-07-27 04:44:18 -0700431 result.emplace_back();
432 rtcp::ReportBlock& block = result.back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100433 block.SetMediaSsrc(media_ssrc);
danilchapf5f793c2017-07-27 04:44:18 -0700434 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700435 if (!block.SetCumulativeLost(stats.packets_lost)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100436 RTC_LOG(LS_WARNING) << "Cumulative lost is oversized.";
danilchapf5f793c2017-07-27 04:44:18 -0700437 result.pop_back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100438 return;
danilchapf5f793c2017-07-27 04:44:18 -0700439 }
srte186d9c32017-08-04 05:03:53 -0700440 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700441 block.SetJitter(stats.jitter);
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100442 };
443
444 const auto start_it = statisticians.upper_bound(last_returned_ssrc_);
445 for (auto it = start_it;
446 result.size() < max_blocks && it != statisticians.end(); ++it)
447 add_report_block(it->first, it->second);
448 for (auto it = statisticians.begin();
449 result.size() < max_blocks && it != start_it; ++it)
450 add_report_block(it->first, it->second);
451
452 if (!result.empty())
453 last_returned_ssrc_ = result.back().source_ssrc();
danilchapf5f793c2017-07-27 04:44:18 -0700454 return result;
455}
456
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000457} // namespace webrtc