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> |
danilchap | b8b6fbb | 2015-12-10 05:05:27 -0800 | [diff] [blame] | 15 | #include <map> |
danilchap | 0bc8423 | 2017-08-11 08:12:54 -0700 | [diff] [blame] | 16 | #include <vector> |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 17 | |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 +0000 | [diff] [blame] | 18 | #include "absl/types/optional.h" |
| 19 | #include "modules/include/module_common_types_public.h" |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 20 | #include "modules/rtp_rtcp/include/receive_statistics.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "rtc_base/critical_section.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/rate_statistics.h" |
Danil Chapovalov | ebb50c2 | 2018-11-22 14:04:02 +0100 | [diff] [blame] | 23 | #include "rtc_base/thread_annotations.h" |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
Niels Möller | dbb988b | 2018-11-15 08:05:16 +0100 | [diff] [blame] | 27 | class StreamStatisticianImpl : public StreamStatistician, |
| 28 | public RtpPacketSinkInterface { |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 29 | public: |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 30 | StreamStatisticianImpl(uint32_t ssrc, |
| 31 | Clock* clock, |
Danil Chapovalov | ebb50c2 | 2018-11-22 14:04:02 +0100 | [diff] [blame] | 32 | int max_reordering_threshold, |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 33 | StreamDataCountersCallback* rtp_callback); |
Danil Chapovalov | 2a5ce2b | 2018-02-07 09:38:31 +0100 | [diff] [blame] | 34 | ~StreamStatisticianImpl() override; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 35 | |
Qingsi Wang | 2370b08 | 2018-08-21 14:24:26 -0700 | [diff] [blame] | 36 | // |reset| here and in next method restarts calculation of fraction_lost stat. |
| 37 | bool GetStatistics(RtcpStatistics* statistics, bool reset) override; |
Danil Chapovalov | c5267d2 | 2017-09-18 13:57:19 +0200 | [diff] [blame] | 38 | bool GetActiveStatisticsAndReset(RtcpStatistics* statistics); |
Niels Möller | 9a9f18a | 2019-08-02 13:52:37 +0200 | [diff] [blame] | 39 | absl::optional<int> GetFractionLostInPercent() const override; |
Niels Möller | 58b496b | 2019-08-12 12:16:31 +0200 | [diff] [blame] | 40 | StreamDataCounters GetReceiveStreamDataCounters() const override; |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 41 | uint32_t BitrateReceived() const override; |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 42 | |
Niels Möller | dbb988b | 2018-11-15 08:05:16 +0100 | [diff] [blame] | 43 | // Implements RtpPacketSinkInterface |
| 44 | void OnRtpPacket(const RtpPacketReceived& packet) override; |
| 45 | |
| 46 | void FecPacketReceived(const RtpPacketReceived& packet); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 47 | void SetMaxReorderingThreshold(int max_reordering_threshold); |
Niels Möller | 5304a32 | 2018-08-27 13:27:05 +0200 | [diff] [blame] | 48 | void EnableRetransmitDetection(bool enable); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 49 | |
| 50 | private: |
Danil Chapovalov | 44727b4 | 2018-11-22 11:28:45 +0100 | [diff] [blame] | 51 | bool IsRetransmitOfOldPacket(const RtpPacketReceived& packet, |
| 52 | int64_t now_ms) const |
Niels Möller | b615d1a | 2018-08-27 12:32:21 +0200 | [diff] [blame] | 53 | RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_); |
Qingsi Wang | 2370b08 | 2018-08-21 14:24:26 -0700 | [diff] [blame] | 54 | RtcpStatistics CalculateRtcpStatistics() |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 55 | RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_); |
Danil Chapovalov | 856cf22 | 2018-11-26 10:20:01 +0100 | [diff] [blame] | 56 | void UpdateJitter(const RtpPacketReceived& packet, int64_t receive_time_ms) |
Niels Möller | b615d1a | 2018-08-27 12:32:21 +0200 | [diff] [blame] | 57 | RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_); |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 +0000 | [diff] [blame] | 58 | // Updates StreamStatistician for out of order packets. |
| 59 | // Returns true if packet considered to be out of order. |
| 60 | bool UpdateOutOfOrder(const RtpPacketReceived& packet, |
| 61 | int64_t sequence_number, |
| 62 | int64_t now_ms) |
| 63 | RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_); |
| 64 | // Updates StreamStatistician for incoming packets. |
Danil Chapovalov | 44727b4 | 2018-11-22 11:28:45 +0100 | [diff] [blame] | 65 | StreamDataCounters UpdateCounters(const RtpPacketReceived& packet); |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 +0000 | [diff] [blame] | 66 | // Checks if this StreamStatistician received any rtp packets. |
| 67 | bool ReceivedRtpPacket() const RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_) { |
| 68 | return received_seq_max_ >= 0; |
| 69 | } |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 70 | |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 71 | const uint32_t ssrc_; |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 72 | Clock* const clock_; |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 73 | rtc::CriticalSection stream_lock_; |
Niels Möller | b615d1a | 2018-08-27 12:32:21 +0200 | [diff] [blame] | 74 | RateStatistics incoming_bitrate_ RTC_GUARDED_BY(&stream_lock_); |
| 75 | // In number of packets or sequence numbers. |
| 76 | int max_reordering_threshold_ RTC_GUARDED_BY(&stream_lock_); |
Niels Möller | 5304a32 | 2018-08-27 13:27:05 +0200 | [diff] [blame] | 77 | bool enable_retransmit_detection_ RTC_GUARDED_BY(&stream_lock_); |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 78 | |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 79 | // Stats on received RTP packets. |
Niels Möller | b615d1a | 2018-08-27 12:32:21 +0200 | [diff] [blame] | 80 | uint32_t jitter_q4_ RTC_GUARDED_BY(&stream_lock_); |
| 81 | uint32_t cumulative_loss_ RTC_GUARDED_BY(&stream_lock_); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 82 | |
Niels Möller | b615d1a | 2018-08-27 12:32:21 +0200 | [diff] [blame] | 83 | int64_t last_receive_time_ms_ RTC_GUARDED_BY(&stream_lock_); |
Niels Möller | b615d1a | 2018-08-27 12:32:21 +0200 | [diff] [blame] | 84 | uint32_t last_received_timestamp_ RTC_GUARDED_BY(&stream_lock_); |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 +0000 | [diff] [blame] | 85 | SequenceNumberUnwrapper seq_unwrapper_ RTC_GUARDED_BY(&stream_lock_); |
| 86 | int64_t received_seq_first_ RTC_GUARDED_BY(&stream_lock_); |
| 87 | int64_t received_seq_max_ RTC_GUARDED_BY(&stream_lock_); |
| 88 | // Assume that the other side restarted when there are two sequential packets |
| 89 | // with large jump from received_seq_max_. |
| 90 | absl::optional<uint16_t> received_seq_out_of_order_ |
| 91 | RTC_GUARDED_BY(&stream_lock_); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 92 | |
| 93 | // Current counter values. |
Niels Möller | b615d1a | 2018-08-27 12:32:21 +0200 | [diff] [blame] | 94 | StreamDataCounters receive_counters_ RTC_GUARDED_BY(&stream_lock_); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 95 | |
Qingsi Wang | 2370b08 | 2018-08-21 14:24:26 -0700 | [diff] [blame] | 96 | // Counter values when we sent the last report. |
Niels Möller | b615d1a | 2018-08-27 12:32:21 +0200 | [diff] [blame] | 97 | uint32_t last_report_inorder_packets_ RTC_GUARDED_BY(&stream_lock_); |
| 98 | uint32_t last_report_old_packets_ RTC_GUARDED_BY(&stream_lock_); |
Danil Chapovalov | b438b5a | 2018-12-05 14:55:46 +0000 | [diff] [blame] | 99 | int64_t last_report_seq_max_ RTC_GUARDED_BY(&stream_lock_); |
Niels Möller | b615d1a | 2018-08-27 12:32:21 +0200 | [diff] [blame] | 100 | RtcpStatistics last_reported_statistics_ RTC_GUARDED_BY(&stream_lock_); |
sprang@webrtc.org | 54ae4ff | 2013-12-19 13:26:02 +0000 | [diff] [blame] | 101 | |
danilchap | ec86be0 | 2017-08-14 05:51:02 -0700 | [diff] [blame] | 102 | // stream_lock_ shouldn't be held when calling callbacks. |
sprang@webrtc.org | 0e93257 | 2014-01-23 10:00:39 +0000 | [diff] [blame] | 103 | StreamDataCountersCallback* const rtp_callback_; |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
Danil Chapovalov | 8ce0d2b | 2018-11-23 11:03:25 +0100 | [diff] [blame] | 106 | class ReceiveStatisticsImpl : public ReceiveStatistics { |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 107 | public: |
Danil Chapovalov | 8ce0d2b | 2018-11-23 11:03:25 +0100 | [diff] [blame] | 108 | ReceiveStatisticsImpl(Clock* clock, |
Danil Chapovalov | 8ce0d2b | 2018-11-23 11:03:25 +0100 | [diff] [blame] | 109 | StreamDataCountersCallback* rtp_callback); |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 110 | |
Danil Chapovalov | 2a5ce2b | 2018-02-07 09:38:31 +0100 | [diff] [blame] | 111 | ~ReceiveStatisticsImpl() override; |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 112 | |
Niels Möller | dbb988b | 2018-11-15 08:05:16 +0100 | [diff] [blame] | 113 | // Implements ReceiveStatisticsProvider. |
danilchap | 0bc8423 | 2017-08-11 08:12:54 -0700 | [diff] [blame] | 114 | std::vector<rtcp::ReportBlock> RtcpReportBlocks(size_t max_blocks) override; |
| 115 | |
Niels Möller | dbb988b | 2018-11-15 08:05:16 +0100 | [diff] [blame] | 116 | // Implements RtpPacketSinkInterface |
Niels Möller | 1f3206c | 2018-09-14 08:26:32 +0200 | [diff] [blame] | 117 | void OnRtpPacket(const RtpPacketReceived& packet) override; |
| 118 | |
Niels Möller | dbb988b | 2018-11-15 08:05:16 +0100 | [diff] [blame] | 119 | // Implements ReceiveStatistics. |
Niels Möller | 1f3206c | 2018-09-14 08:26:32 +0200 | [diff] [blame] | 120 | void FecPacketReceived(const RtpPacketReceived& packet) override; |
Niels Möller | 87da109 | 2019-05-24 14:04:28 +0200 | [diff] [blame] | 121 | // Note: More specific return type for use in the implementation. |
| 122 | StreamStatisticianImpl* GetStatistician(uint32_t ssrc) const override; |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 123 | void SetMaxReorderingThreshold(int max_reordering_threshold) override; |
Niels Möller | 87da109 | 2019-05-24 14:04:28 +0200 | [diff] [blame] | 124 | void SetMaxReorderingThreshold(uint32_t ssrc, |
| 125 | int max_reordering_threshold) override; |
Niels Möller | 5304a32 | 2018-08-27 13:27:05 +0200 | [diff] [blame] | 126 | void EnableRetransmitDetection(uint32_t ssrc, bool enable) override; |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 127 | |
stefan@webrtc.org | 286fe0b | 2013-08-21 20:58:21 +0000 | [diff] [blame] | 128 | private: |
Niels Möller | 87da109 | 2019-05-24 14:04:28 +0200 | [diff] [blame] | 129 | StreamStatisticianImpl* GetOrCreateStatistician(uint32_t ssrc); |
| 130 | |
sprang | cd349d9 | 2016-07-13 09:11:28 -0700 | [diff] [blame] | 131 | Clock* const clock_; |
danilchap | 7c9426c | 2016-04-14 03:05:31 -0700 | [diff] [blame] | 132 | rtc::CriticalSection receive_statistics_lock_; |
Danil Chapovalov | d1996b7 | 2018-01-16 11:07:18 +0100 | [diff] [blame] | 133 | uint32_t last_returned_ssrc_; |
Danil Chapovalov | ebb50c2 | 2018-11-22 14:04:02 +0100 | [diff] [blame] | 134 | int max_reordering_threshold_ RTC_GUARDED_BY(receive_statistics_lock_); |
| 135 | std::map<uint32_t, StreamStatisticianImpl*> statisticians_ |
| 136 | RTC_GUARDED_BY(receive_statistics_lock_); |
sprang@webrtc.org | 54ae4ff | 2013-12-19 13:26:02 +0000 | [diff] [blame] | 137 | |
Danil Chapovalov | 8ce0d2b | 2018-11-23 11:03:25 +0100 | [diff] [blame] | 138 | StreamDataCountersCallback* const rtp_stats_callback_; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 139 | }; |
| 140 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 141 | #endif // MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_ |