blob: f4ea2a096c5a67c5564012b4b46bf85e0dded162 [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
Niels Möllerd77cc242019-08-22 09:40:25 +0200169RtpReceiveStats StreamStatisticianImpl::GetStats() const {
170 rtc::CritScope cs(&stream_lock_);
171 RtpReceiveStats stats;
172 stats.packets_lost = cumulative_loss_;
173 // TODO(nisse): Can we return a float instead?
174 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
175 stats.jitter = jitter_q4_ >> 4;
176 stats.last_packet_received_timestamp_ms =
177 receive_counters_.last_packet_received_timestamp_ms;
178 stats.packet_counter = receive_counters_.transmitted;
179 return stats;
180}
181
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000182bool StreamStatisticianImpl::GetStatistics(RtcpStatistics* statistics,
Qingsi Wang2370b082018-08-21 14:24:26 -0700183 bool reset) {
Niels Möller12ebfa62019-08-06 16:04:12 +0200184 rtc::CritScope cs(&stream_lock_);
185 if (!ReceivedRtpPacket()) {
186 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000187 }
188
Niels Möller12ebfa62019-08-06 16:04:12 +0200189 if (!reset) {
190 if (last_report_inorder_packets_ == 0) {
191 // No report.
192 return false;
193 }
194 // Just get last report.
195 *statistics = last_reported_statistics_;
196 return true;
197 }
198
199 *statistics = CalculateRtcpStatistics();
200
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000201 return true;
202}
203
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200204bool StreamStatisticianImpl::GetActiveStatisticsAndReset(
205 RtcpStatistics* statistics) {
Niels Möller12ebfa62019-08-06 16:04:12 +0200206 rtc::CritScope cs(&stream_lock_);
207 if (clock_->TimeInMilliseconds() - last_receive_time_ms_ >=
208 kStatisticsTimeoutMs) {
209 // Not active.
210 return false;
211 }
212 if (!ReceivedRtpPacket()) {
213 return false;
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200214 }
215
Niels Möller12ebfa62019-08-06 16:04:12 +0200216 *statistics = CalculateRtcpStatistics();
217
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200218 return true;
219}
220
Qingsi Wang2370b082018-08-21 14:24:26 -0700221RtcpStatistics StreamStatisticianImpl::CalculateRtcpStatistics() {
222 RtcpStatistics stats;
Qingsi Wang2370b082018-08-21 14:24:26 -0700223 // Calculate fraction lost.
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000224 int64_t exp_since_last = received_seq_max_ - last_report_seq_max_;
225 RTC_DCHECK_GE(exp_since_last, 0);
Qingsi Wang2370b082018-08-21 14:24:26 -0700226
227 // Number of received RTP packets since last report, counts all packets but
228 // not re-transmissions.
229 uint32_t rec_since_last = (receive_counters_.transmitted.packets -
230 receive_counters_.retransmitted.packets) -
231 last_report_inorder_packets_;
232
233 // With NACK we don't know the expected retransmissions during the last
234 // second. We know how many "old" packets we have received. We just count
235 // the number of old received to estimate the loss, but it still does not
236 // guarantee an exact number since we run this based on time triggered by
237 // sending of an RTP packet. This should have a minimum effect.
238
239 // With NACK we don't count old packets as received since they are
240 // re-transmitted. We use RTT to decide if a packet is re-ordered or
241 // re-transmitted.
242 uint32_t retransmitted_packets =
243 receive_counters_.retransmitted.packets - last_report_old_packets_;
244 rec_since_last += retransmitted_packets;
245
246 int32_t missing = 0;
247 if (exp_since_last > rec_since_last) {
248 missing = (exp_since_last - rec_since_last);
249 }
250 uint8_t local_fraction_lost = 0;
251 if (exp_since_last) {
252 // Scale 0 to 255, where 255 is 100% loss.
253 local_fraction_lost = static_cast<uint8_t>(255 * missing / exp_since_last);
254 }
255 stats.fraction_lost = local_fraction_lost;
256
257 // We need a counter for cumulative loss too.
258 // TODO(danilchap): Ensure cumulative loss is below maximum value of 2^24.
259 cumulative_loss_ += missing;
260 stats.packets_lost = cumulative_loss_;
261 stats.extended_highest_sequence_number =
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000262 static_cast<uint32_t>(received_seq_max_);
Qingsi Wang2370b082018-08-21 14:24:26 -0700263 // Note: internal jitter value is in Q4 and needs to be scaled by 1/16.
264 stats.jitter = jitter_q4_ >> 4;
265
266 // Store this report.
267 last_reported_statistics_ = stats;
268
269 // Only for report blocks in RTCP SR and RR.
270 last_report_inorder_packets_ = receive_counters_.transmitted.packets -
271 receive_counters_.retransmitted.packets;
272 last_report_old_packets_ = receive_counters_.retransmitted.packets;
273 last_report_seq_max_ = received_seq_max_;
gaetano.carlucci61050f62016-09-30 06:29:54 -0700274 BWE_TEST_LOGGING_PLOT_WITH_SSRC(1, "cumulative_loss_pkts",
gaetano.carlucci52a57032016-09-14 05:04:36 -0700275 clock_->TimeInMilliseconds(),
Qingsi Wang2370b082018-08-21 14:24:26 -0700276 cumulative_loss_, ssrc_);
gaetano.carlucci52a57032016-09-14 05:04:36 -0700277 BWE_TEST_LOGGING_PLOT_WITH_SSRC(
gaetano.carlucci61050f62016-09-30 06:29:54 -0700278 1, "received_seq_max_pkts", clock_->TimeInMilliseconds(),
gaetano.carlucci52a57032016-09-14 05:04:36 -0700279 (received_seq_max_ - received_seq_first_), ssrc_);
sprang@webrtc.org7dba27c2014-01-21 16:33:37 +0000280
Qingsi Wang2370b082018-08-21 14:24:26 -0700281 return stats;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000282}
283
Niels Möller9a9f18a2019-08-02 13:52:37 +0200284absl::optional<int> StreamStatisticianImpl::GetFractionLostInPercent() const {
285 rtc::CritScope cs(&stream_lock_);
286 if (received_seq_max_ < 0) {
287 return absl::nullopt;
288 }
289 int64_t expected_packets = 1 + received_seq_max_ - received_seq_first_;
290 if (expected_packets <= 0) {
291 return absl::nullopt;
292 }
293 // Spec allows negative cumulative loss, but implementation uses uint32_t, so
294 // this expression is always non-negative.
295 return 100 * static_cast<int64_t>(cumulative_loss_) / expected_packets;
296}
297
Niels Möller58b496b2019-08-12 12:16:31 +0200298StreamDataCounters StreamStatisticianImpl::GetReceiveStreamDataCounters()
299 const {
danilchap7c9426c2016-04-14 03:05:31 -0700300 rtc::CritScope cs(&stream_lock_);
Niels Möller58b496b2019-08-12 12:16:31 +0200301 return 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.
Oleh Prypin19929582019-04-23 08:50:04 +0200324 float jitter_std = 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
Niels Möllerd7819652019-08-13 14:43:02 +0200337std::unique_ptr<ReceiveStatistics> ReceiveStatistics::Create(Clock* clock) {
338 return absl::make_unique<ReceiveStatisticsImpl>(clock);
339}
340
Niels Möllerd7819652019-08-13 14:43:02 +0200341ReceiveStatisticsImpl::ReceiveStatisticsImpl(Clock* clock)
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000342 : clock_(clock),
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100343 last_returned_ssrc_(0),
Niels Möllerd7819652019-08-13 14:43:02 +0200344 max_reordering_threshold_(kDefaultMaxReorderingThreshold) {}
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000345
346ReceiveStatisticsImpl::~ReceiveStatisticsImpl() {
347 while (!statisticians_.empty()) {
348 delete statisticians_.begin()->second;
349 statisticians_.erase(statisticians_.begin());
350 }
351}
352
Niels Möller1f3206c2018-09-14 08:26:32 +0200353void ReceiveStatisticsImpl::OnRtpPacket(const RtpPacketReceived& packet) {
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000354 // StreamStatisticianImpl instance is created once and only destroyed when
355 // this whole ReceiveStatisticsImpl is destroyed. StreamStatisticianImpl has
356 // it's own locking so don't hold receive_statistics_lock_ (potential
357 // deadlock).
Niels Möller87da1092019-05-24 14:04:28 +0200358 GetOrCreateStatistician(packet.Ssrc())->OnRtpPacket(packet);
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000359}
360
Niels Möller1f3206c2018-09-14 08:26:32 +0200361void ReceiveStatisticsImpl::FecPacketReceived(const RtpPacketReceived& packet) {
Niels Möller87da1092019-05-24 14:04:28 +0200362 StreamStatisticianImpl* impl = GetStatistician(packet.Ssrc());
363 // Ignore FEC if it is the first packet.
364 if (impl) {
365 impl->FecPacketReceived(packet);
sprang@webrtc.orgc30e9e22014-09-08 08:20:18 +0000366 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000367}
368
Niels Möller87da1092019-05-24 14:04:28 +0200369StreamStatisticianImpl* ReceiveStatisticsImpl::GetStatistician(
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000370 uint32_t ssrc) const {
danilchap7c9426c2016-04-14 03:05:31 -0700371 rtc::CritScope cs(&receive_statistics_lock_);
Niels Möller87da1092019-05-24 14:04:28 +0200372 const auto& it = statisticians_.find(ssrc);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000373 if (it == statisticians_.end())
374 return NULL;
375 return it->second;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000376}
377
Niels Möller87da1092019-05-24 14:04:28 +0200378StreamStatisticianImpl* ReceiveStatisticsImpl::GetOrCreateStatistician(
379 uint32_t ssrc) {
380 rtc::CritScope cs(&receive_statistics_lock_);
381 StreamStatisticianImpl*& impl = statisticians_[ssrc];
382 if (impl == nullptr) { // new element
Niels Möllerd7819652019-08-13 14:43:02 +0200383 impl = new StreamStatisticianImpl(ssrc, clock_, max_reordering_threshold_);
Niels Möller87da1092019-05-24 14:04:28 +0200384 }
385 return impl;
386}
387
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000388void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
389 int max_reordering_threshold) {
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100390 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
391 {
392 rtc::CritScope cs(&receive_statistics_lock_);
393 max_reordering_threshold_ = max_reordering_threshold;
394 statisticians = statisticians_;
395 }
396 for (auto& statistician : statisticians) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200397 statistician.second->SetMaxReorderingThreshold(max_reordering_threshold);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000398 }
399}
400
Niels Möller87da1092019-05-24 14:04:28 +0200401void ReceiveStatisticsImpl::SetMaxReorderingThreshold(
402 uint32_t ssrc,
403 int max_reordering_threshold) {
404 GetOrCreateStatistician(ssrc)->SetMaxReorderingThreshold(
405 max_reordering_threshold);
406}
407
Niels Möller5304a322018-08-27 13:27:05 +0200408void ReceiveStatisticsImpl::EnableRetransmitDetection(uint32_t ssrc,
409 bool enable) {
Niels Möller87da1092019-05-24 14:04:28 +0200410 GetOrCreateStatistician(ssrc)->EnableRetransmitDetection(enable);
Niels Möller5304a322018-08-27 13:27:05 +0200411}
412
danilchap0bc84232017-08-11 08:12:54 -0700413std::vector<rtcp::ReportBlock> ReceiveStatisticsImpl::RtcpReportBlocks(
danilchapf5f793c2017-07-27 04:44:18 -0700414 size_t max_blocks) {
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200415 std::map<uint32_t, StreamStatisticianImpl*> statisticians;
416 {
417 rtc::CritScope cs(&receive_statistics_lock_);
418 statisticians = statisticians_;
419 }
danilchapf5f793c2017-07-27 04:44:18 -0700420 std::vector<rtcp::ReportBlock> result;
421 result.reserve(std::min(max_blocks, statisticians.size()));
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100422 auto add_report_block = [&result](uint32_t media_ssrc,
423 StreamStatisticianImpl* statistician) {
danilchapf5f793c2017-07-27 04:44:18 -0700424 // Do we have receive statistics to send?
425 RtcpStatistics stats;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100426 if (!statistician->GetActiveStatisticsAndReset(&stats))
427 return;
danilchapf5f793c2017-07-27 04:44:18 -0700428 result.emplace_back();
429 rtcp::ReportBlock& block = result.back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100430 block.SetMediaSsrc(media_ssrc);
danilchapf5f793c2017-07-27 04:44:18 -0700431 block.SetFractionLost(stats.fraction_lost);
srte186d9c32017-08-04 05:03:53 -0700432 if (!block.SetCumulativeLost(stats.packets_lost)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100433 RTC_LOG(LS_WARNING) << "Cumulative lost is oversized.";
danilchapf5f793c2017-07-27 04:44:18 -0700434 result.pop_back();
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100435 return;
danilchapf5f793c2017-07-27 04:44:18 -0700436 }
srte186d9c32017-08-04 05:03:53 -0700437 block.SetExtHighestSeqNum(stats.extended_highest_sequence_number);
danilchapf5f793c2017-07-27 04:44:18 -0700438 block.SetJitter(stats.jitter);
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100439 };
440
441 const auto start_it = statisticians.upper_bound(last_returned_ssrc_);
442 for (auto it = start_it;
443 result.size() < max_blocks && it != statisticians.end(); ++it)
444 add_report_block(it->first, it->second);
445 for (auto it = statisticians.begin();
446 result.size() < max_blocks && it != start_it; ++it)
447 add_report_block(it->first, it->second);
448
449 if (!result.empty())
450 last_returned_ssrc_ = result.back().source_ssrc();
danilchapf5f793c2017-07-27 04:44:18 -0700451 return result;
452}
453
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000454} // namespace webrtc