wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_ |
| 12 | #define MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_ |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 13 | |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 14 | #include <algorithm> |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 15 | #include <functional> |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 16 | #include <memory> |
| 17 | #include <utility> |
danilchap | 0bc8423 | 2017-08-11 08:12:54 -0700 | [diff] [blame] | 18 | #include <vector> |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 19 | |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 +0000 | [diff] [blame] | 20 | #include "absl/types/optional.h" |
| 21 | #include "modules/include/module_common_types_public.h" |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 22 | #include "modules/rtp_rtcp/include/receive_statistics.h" |
Danil Chapovalov | b27a9f9 | 2021-05-17 14:22:02 +0200 | [diff] [blame] | 23 | #include "modules/rtp_rtcp/source/rtcp_packet/report_block.h" |
Victor Boivie | 1864997 | 2021-07-06 22:43:47 +0200 | [diff] [blame] | 24 | #include "rtc_base/containers/flat_map.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/rate_statistics.h" |
Markus Handell | e7c015e | 2020-07-07 11:44:28 +0200 | [diff] [blame] | 26 | #include "rtc_base/synchronization/mutex.h" |
Danil Chapovalov | ebb50c2 | 2018-11-22 14:04:02 +0100 | [diff] [blame] | 27 | #include "rtc_base/thread_annotations.h" |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
| 30 | |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 31 | // Extends StreamStatistician with methods needed by the implementation. |
| 32 | class StreamStatisticianImplInterface : public StreamStatistician { |
| 33 | public: |
| 34 | virtual ~StreamStatisticianImplInterface() = default; |
Danil Chapovalov | b27a9f9 | 2021-05-17 14:22:02 +0200 | [diff] [blame] | 35 | virtual void MaybeAppendReportBlockAndReset( |
| 36 | std::vector<rtcp::ReportBlock>& report_blocks) = 0; |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 37 | virtual void SetMaxReorderingThreshold(int max_reordering_threshold) = 0; |
| 38 | virtual void EnableRetransmitDetection(bool enable) = 0; |
| 39 | virtual void UpdateCounters(const RtpPacketReceived& packet) = 0; |
| 40 | }; |
| 41 | |
| 42 | // Thread-compatible implementation of StreamStatisticianImplInterface. |
| 43 | class StreamStatisticianImpl : public StreamStatisticianImplInterface { |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 44 | public: |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 45 | StreamStatisticianImpl(uint32_t ssrc, |
| 46 | Clock* clock, |
Niels Möller | d781965 | 2019-08-13 14:43:02 +0200 | [diff] [blame] | 47 | int max_reordering_threshold); |
Danil Chapovalov | 2a5ce2b | 2018-02-07 09:38:31 +0100 | [diff] [blame] | 48 | ~StreamStatisticianImpl() override; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 49 | |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 50 | // Implements StreamStatistician |
Niels Möller | d77cc24 | 2019-08-22 09:40:25 +0200 | [diff] [blame] | 51 | RtpReceiveStats GetStats() const override; |
Niels Möller | 9a9f18a | 2019-08-02 13:52:37 +0200 | [diff] [blame] | 52 | absl::optional<int> GetFractionLostInPercent() const override; |
Niels Möller | 58b496b | 2019-08-12 12:16:31 +0200 | [diff] [blame] | 53 | StreamDataCounters GetReceiveStreamDataCounters() const override; |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 54 | uint32_t BitrateReceived() const override; |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 55 | |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 56 | // Implements StreamStatisticianImplInterface |
Danil Chapovalov | b27a9f9 | 2021-05-17 14:22:02 +0200 | [diff] [blame] | 57 | void MaybeAppendReportBlockAndReset( |
| 58 | std::vector<rtcp::ReportBlock>& report_blocks) override; |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 59 | void SetMaxReorderingThreshold(int max_reordering_threshold) override; |
| 60 | void EnableRetransmitDetection(bool enable) override; |
Niels Möller | 1a3859c | 2019-09-04 09:43:15 +0200 | [diff] [blame] | 61 | // Updates StreamStatistician for incoming packets. |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 62 | void UpdateCounters(const RtpPacketReceived& packet) override; |
Niels Möller | 1a3859c | 2019-09-04 09:43:15 +0200 | [diff] [blame] | 63 | |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 64 | private: |
Danil Chapovalov | 44727b4 | 2018-11-22 11:28:45 +0100 | [diff] [blame] | 65 | bool IsRetransmitOfOldPacket(const RtpPacketReceived& packet, |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 66 | int64_t now_ms) const; |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 67 | void UpdateJitter(const RtpPacketReceived& packet, int64_t receive_time_ms); |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 +0000 | [diff] [blame] | 68 | // Updates StreamStatistician for out of order packets. |
| 69 | // Returns true if packet considered to be out of order. |
| 70 | bool UpdateOutOfOrder(const RtpPacketReceived& packet, |
| 71 | int64_t sequence_number, |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 72 | int64_t now_ms); |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 +0000 | [diff] [blame] | 73 | // Checks if this StreamStatistician received any rtp packets. |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 74 | bool ReceivedRtpPacket() const { return received_seq_first_ >= 0; } |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 75 | |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 76 | const uint32_t ssrc_; |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 77 | Clock* const clock_; |
Alessio Bazzica | 5cf8c2c | 2021-03-24 08:51:26 +0100 | [diff] [blame] | 78 | // Delta used to map internal timestamps to Unix epoch ones. |
| 79 | const int64_t delta_internal_unix_epoch_ms_; |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 80 | RateStatistics incoming_bitrate_; |
Niels Möller | b615d1a | 2018-08-27 12:32:21 +0200 | [diff] [blame] | 81 | // In number of packets or sequence numbers. |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 82 | int max_reordering_threshold_; |
| 83 | bool enable_retransmit_detection_; |
Danil Chapovalov | b27a9f9 | 2021-05-17 14:22:02 +0200 | [diff] [blame] | 84 | bool cumulative_loss_is_capped_; |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 85 | |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 86 | // Stats on received RTP packets. |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 87 | uint32_t jitter_q4_; |
Niels Möller | 1a3859c | 2019-09-04 09:43:15 +0200 | [diff] [blame] | 88 | // Cumulative loss according to RFC 3550, which may be negative (and often is, |
| 89 | // if packets are reordered and there are non-RTX retransmissions). |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 90 | int32_t cumulative_loss_; |
Niels Möller | 1a3859c | 2019-09-04 09:43:15 +0200 | [diff] [blame] | 91 | // Offset added to outgoing rtcp reports, to make ensure that the reported |
| 92 | // cumulative loss is non-negative. Reports with negative values confuse some |
| 93 | // senders, in particular, our own loss-based bandwidth estimator. |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 94 | int32_t cumulative_loss_rtcp_offset_; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 95 | |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 96 | int64_t last_receive_time_ms_; |
| 97 | uint32_t last_received_timestamp_; |
| 98 | SequenceNumberUnwrapper seq_unwrapper_; |
| 99 | int64_t received_seq_first_; |
| 100 | int64_t received_seq_max_; |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 +0000 | [diff] [blame] | 101 | // Assume that the other side restarted when there are two sequential packets |
| 102 | // with large jump from received_seq_max_. |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 103 | absl::optional<uint16_t> received_seq_out_of_order_; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 104 | |
| 105 | // Current counter values. |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 106 | StreamDataCounters receive_counters_; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 107 | |
Qingsi Wang | 2370b08 | 2018-08-21 14:24:26 -0700 | [diff] [blame] | 108 | // Counter values when we sent the last report. |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 109 | int32_t last_report_cumulative_loss_; |
| 110 | int64_t last_report_seq_max_; |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 113 | // Thread-safe implementation of StreamStatisticianImplInterface. |
| 114 | class StreamStatisticianLocked : public StreamStatisticianImplInterface { |
| 115 | public: |
| 116 | StreamStatisticianLocked(uint32_t ssrc, |
| 117 | Clock* clock, |
| 118 | int max_reordering_threshold) |
| 119 | : impl_(ssrc, clock, max_reordering_threshold) {} |
| 120 | ~StreamStatisticianLocked() override = default; |
| 121 | |
| 122 | RtpReceiveStats GetStats() const override { |
| 123 | MutexLock lock(&stream_lock_); |
| 124 | return impl_.GetStats(); |
| 125 | } |
| 126 | absl::optional<int> GetFractionLostInPercent() const override { |
| 127 | MutexLock lock(&stream_lock_); |
| 128 | return impl_.GetFractionLostInPercent(); |
| 129 | } |
| 130 | StreamDataCounters GetReceiveStreamDataCounters() const override { |
| 131 | MutexLock lock(&stream_lock_); |
| 132 | return impl_.GetReceiveStreamDataCounters(); |
| 133 | } |
| 134 | uint32_t BitrateReceived() const override { |
| 135 | MutexLock lock(&stream_lock_); |
| 136 | return impl_.BitrateReceived(); |
| 137 | } |
Danil Chapovalov | b27a9f9 | 2021-05-17 14:22:02 +0200 | [diff] [blame] | 138 | void MaybeAppendReportBlockAndReset( |
| 139 | std::vector<rtcp::ReportBlock>& report_blocks) override { |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 140 | MutexLock lock(&stream_lock_); |
Danil Chapovalov | b27a9f9 | 2021-05-17 14:22:02 +0200 | [diff] [blame] | 141 | impl_.MaybeAppendReportBlockAndReset(report_blocks); |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 142 | } |
| 143 | void SetMaxReorderingThreshold(int max_reordering_threshold) override { |
| 144 | MutexLock lock(&stream_lock_); |
| 145 | return impl_.SetMaxReorderingThreshold(max_reordering_threshold); |
| 146 | } |
| 147 | void EnableRetransmitDetection(bool enable) override { |
| 148 | MutexLock lock(&stream_lock_); |
| 149 | return impl_.EnableRetransmitDetection(enable); |
| 150 | } |
| 151 | void UpdateCounters(const RtpPacketReceived& packet) override { |
| 152 | MutexLock lock(&stream_lock_); |
| 153 | return impl_.UpdateCounters(packet); |
| 154 | } |
| 155 | |
| 156 | private: |
| 157 | mutable Mutex stream_lock_; |
| 158 | StreamStatisticianImpl impl_ RTC_GUARDED_BY(&stream_lock_); |
| 159 | }; |
| 160 | |
| 161 | // Thread-compatible implementation. |
Danil Chapovalov | 8ce0d2b | 2018-11-23 11:03:25 +0100 | [diff] [blame] | 162 | class ReceiveStatisticsImpl : public ReceiveStatistics { |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 163 | public: |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 164 | ReceiveStatisticsImpl( |
| 165 | Clock* clock, |
| 166 | std::function<std::unique_ptr<StreamStatisticianImplInterface>( |
| 167 | uint32_t ssrc, |
| 168 | Clock* clock, |
| 169 | int max_reordering_threshold)> stream_statistician_factory); |
| 170 | ~ReceiveStatisticsImpl() override = default; |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 171 | |
Niels Möller | dbb988b | 2018-11-15 08:05:16 +0100 | [diff] [blame] | 172 | // Implements ReceiveStatisticsProvider. |
danilchap | 0bc8423 | 2017-08-11 08:12:54 -0700 | [diff] [blame] | 173 | std::vector<rtcp::ReportBlock> RtcpReportBlocks(size_t max_blocks) override; |
| 174 | |
Niels Möller | dbb988b | 2018-11-15 08:05:16 +0100 | [diff] [blame] | 175 | // Implements RtpPacketSinkInterface |
Niels Möller | 1f3206c | 2018-09-14 08:26:32 +0200 | [diff] [blame] | 176 | void OnRtpPacket(const RtpPacketReceived& packet) override; |
| 177 | |
Niels Möller | dbb988b | 2018-11-15 08:05:16 +0100 | [diff] [blame] | 178 | // Implements ReceiveStatistics. |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 179 | StreamStatistician* GetStatistician(uint32_t ssrc) const override; |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 180 | void SetMaxReorderingThreshold(int max_reordering_threshold) override; |
Niels Möller | 87da109 | 2019-05-24 14:04:28 +0200 | [diff] [blame] | 181 | void SetMaxReorderingThreshold(uint32_t ssrc, |
| 182 | int max_reordering_threshold) override; |
Niels Möller | 5304a32 | 2018-08-27 13:27:05 +0200 | [diff] [blame] | 183 | void EnableRetransmitDetection(uint32_t ssrc, bool enable) override; |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 184 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 185 | private: |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 186 | StreamStatisticianImplInterface* GetOrCreateStatistician(uint32_t ssrc); |
Niels Möller | 87da109 | 2019-05-24 14:04:28 +0200 | [diff] [blame] | 187 | |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 188 | Clock* const clock_; |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 189 | std::function<std::unique_ptr<StreamStatisticianImplInterface>( |
| 190 | uint32_t ssrc, |
| 191 | Clock* clock, |
| 192 | int max_reordering_threshold)> |
| 193 | stream_statistician_factory_; |
Victor Boivie | b2f8c16 | 2021-04-28 12:02:37 +0200 | [diff] [blame] | 194 | // The index within `all_ssrcs_` that was last returned. |
| 195 | size_t last_returned_ssrc_idx_; |
| 196 | std::vector<uint32_t> all_ssrcs_; |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 197 | int max_reordering_threshold_; |
Victor Boivie | 1864997 | 2021-07-06 22:43:47 +0200 | [diff] [blame] | 198 | flat_map<uint32_t /*ssrc*/, std::unique_ptr<StreamStatisticianImplInterface>> |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 199 | statisticians_; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 200 | }; |
Per Kjellander | ee8cd20 | 2021-03-10 12:31:38 +0100 | [diff] [blame] | 201 | |
| 202 | // Thread-safe implementation wrapping access to ReceiveStatisticsImpl with a |
| 203 | // mutex. |
| 204 | class ReceiveStatisticsLocked : public ReceiveStatistics { |
| 205 | public: |
| 206 | explicit ReceiveStatisticsLocked( |
| 207 | Clock* clock, |
| 208 | std::function<std::unique_ptr<StreamStatisticianImplInterface>( |
| 209 | uint32_t ssrc, |
| 210 | Clock* clock, |
| 211 | int max_reordering_threshold)> stream_statitician_factory) |
| 212 | : impl_(clock, std::move(stream_statitician_factory)) {} |
| 213 | ~ReceiveStatisticsLocked() override = default; |
| 214 | std::vector<rtcp::ReportBlock> RtcpReportBlocks(size_t max_blocks) override { |
| 215 | MutexLock lock(&receive_statistics_lock_); |
| 216 | return impl_.RtcpReportBlocks(max_blocks); |
| 217 | } |
| 218 | void OnRtpPacket(const RtpPacketReceived& packet) override { |
| 219 | MutexLock lock(&receive_statistics_lock_); |
| 220 | return impl_.OnRtpPacket(packet); |
| 221 | } |
| 222 | StreamStatistician* GetStatistician(uint32_t ssrc) const override { |
| 223 | MutexLock lock(&receive_statistics_lock_); |
| 224 | return impl_.GetStatistician(ssrc); |
| 225 | } |
| 226 | void SetMaxReorderingThreshold(int max_reordering_threshold) override { |
| 227 | MutexLock lock(&receive_statistics_lock_); |
| 228 | return impl_.SetMaxReorderingThreshold(max_reordering_threshold); |
| 229 | } |
| 230 | void SetMaxReorderingThreshold(uint32_t ssrc, |
| 231 | int max_reordering_threshold) override { |
| 232 | MutexLock lock(&receive_statistics_lock_); |
| 233 | return impl_.SetMaxReorderingThreshold(ssrc, max_reordering_threshold); |
| 234 | } |
| 235 | void EnableRetransmitDetection(uint32_t ssrc, bool enable) override { |
| 236 | MutexLock lock(&receive_statistics_lock_); |
| 237 | return impl_.EnableRetransmitDetection(ssrc, enable); |
| 238 | } |
| 239 | |
| 240 | private: |
| 241 | mutable Mutex receive_statistics_lock_; |
| 242 | ReceiveStatisticsImpl impl_ RTC_GUARDED_BY(&receive_statistics_lock_); |
| 243 | }; |
| 244 | |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 245 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 246 | #endif // MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_ |