blob: e352ae878737d509bb4caaf0d751dd34f2cd64f2 [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#ifndef MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_
12#define MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_
wu@webrtc.org822fbd82013-08-15 23:38:54 +000013
wu@webrtc.org822fbd82013-08-15 23:38:54 +000014#include <algorithm>
danilchapb8b6fbb2015-12-10 05:05:27 -080015#include <map>
danilchap0bc84232017-08-11 08:12:54 -070016#include <vector>
wu@webrtc.org822fbd82013-08-15 23:38:54 +000017
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000018#include "absl/types/optional.h"
19#include "modules/include/module_common_types_public.h"
Jonas Olssona4d87372019-07-05 19:08:33 +020020#include "modules/rtp_rtcp/include/receive_statistics.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/rate_statistics.h"
Danil Chapovalovebb50c22018-11-22 14:04:02 +010023#include "rtc_base/thread_annotations.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000024
25namespace webrtc {
26
Niels Möller1a3859c2019-09-04 09:43:15 +020027class StreamStatisticianImpl : public StreamStatistician {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000028 public:
danilchapec86be02017-08-14 05:51:02 -070029 StreamStatisticianImpl(uint32_t ssrc,
30 Clock* clock,
Niels Möllerd7819652019-08-13 14:43:02 +020031 int max_reordering_threshold);
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010032 ~StreamStatisticianImpl() override;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000033
Niels Möllerd77cc242019-08-22 09:40:25 +020034 RtpReceiveStats GetStats() const override;
35
Danil Chapovalovc5267d22017-09-18 13:57:19 +020036 bool GetActiveStatisticsAndReset(RtcpStatistics* statistics);
Niels Möller9a9f18a2019-08-02 13:52:37 +020037 absl::optional<int> GetFractionLostInPercent() const override;
Niels Möller58b496b2019-08-12 12:16:31 +020038 StreamDataCounters GetReceiveStreamDataCounters() const override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000039 uint32_t BitrateReceived() const override;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000040
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000041 void SetMaxReorderingThreshold(int max_reordering_threshold);
Niels Möller5304a322018-08-27 13:27:05 +020042 void EnableRetransmitDetection(bool enable);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000043
Niels Möller1a3859c2019-09-04 09:43:15 +020044 // Updates StreamStatistician for incoming packets.
45 void UpdateCounters(const RtpPacketReceived& packet);
46
wu@webrtc.org822fbd82013-08-15 23:38:54 +000047 private:
Danil Chapovalov44727b42018-11-22 11:28:45 +010048 bool IsRetransmitOfOldPacket(const RtpPacketReceived& packet,
49 int64_t now_ms) const
Niels Möllerb615d1a2018-08-27 12:32:21 +020050 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
Qingsi Wang2370b082018-08-21 14:24:26 -070051 RtcpStatistics CalculateRtcpStatistics()
danilchap56359be2017-09-07 07:53:45 -070052 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
Danil Chapovalov856cf222018-11-26 10:20:01 +010053 void UpdateJitter(const RtpPacketReceived& packet, int64_t receive_time_ms)
Niels Möllerb615d1a2018-08-27 12:32:21 +020054 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000055 // Updates StreamStatistician for out of order packets.
56 // Returns true if packet considered to be out of order.
57 bool UpdateOutOfOrder(const RtpPacketReceived& packet,
58 int64_t sequence_number,
59 int64_t now_ms)
60 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000061 // Checks if this StreamStatistician received any rtp packets.
62 bool ReceivedRtpPacket() const RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_) {
Niels Möller1a3859c2019-09-04 09:43:15 +020063 return received_seq_first_ >= 0;
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000064 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000065
danilchapec86be02017-08-14 05:51:02 -070066 const uint32_t ssrc_;
sprangcd349d92016-07-13 09:11:28 -070067 Clock* const clock_;
danilchap7c9426c2016-04-14 03:05:31 -070068 rtc::CriticalSection stream_lock_;
Niels Möllerb615d1a2018-08-27 12:32:21 +020069 RateStatistics incoming_bitrate_ RTC_GUARDED_BY(&stream_lock_);
70 // In number of packets or sequence numbers.
71 int max_reordering_threshold_ RTC_GUARDED_BY(&stream_lock_);
Niels Möller5304a322018-08-27 13:27:05 +020072 bool enable_retransmit_detection_ RTC_GUARDED_BY(&stream_lock_);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000073
wu@webrtc.org822fbd82013-08-15 23:38:54 +000074 // Stats on received RTP packets.
Niels Möllerb615d1a2018-08-27 12:32:21 +020075 uint32_t jitter_q4_ RTC_GUARDED_BY(&stream_lock_);
Niels Möller1a3859c2019-09-04 09:43:15 +020076 // Cumulative loss according to RFC 3550, which may be negative (and often is,
77 // if packets are reordered and there are non-RTX retransmissions).
78 int32_t cumulative_loss_ RTC_GUARDED_BY(&stream_lock_);
79 // Offset added to outgoing rtcp reports, to make ensure that the reported
80 // cumulative loss is non-negative. Reports with negative values confuse some
81 // senders, in particular, our own loss-based bandwidth estimator.
82 int32_t cumulative_loss_rtcp_offset_ RTC_GUARDED_BY(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000083
Niels Möllerb615d1a2018-08-27 12:32:21 +020084 int64_t last_receive_time_ms_ RTC_GUARDED_BY(&stream_lock_);
Niels Möllerb615d1a2018-08-27 12:32:21 +020085 uint32_t last_received_timestamp_ RTC_GUARDED_BY(&stream_lock_);
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000086 SequenceNumberUnwrapper seq_unwrapper_ RTC_GUARDED_BY(&stream_lock_);
87 int64_t received_seq_first_ RTC_GUARDED_BY(&stream_lock_);
88 int64_t received_seq_max_ RTC_GUARDED_BY(&stream_lock_);
89 // Assume that the other side restarted when there are two sequential packets
90 // with large jump from received_seq_max_.
91 absl::optional<uint16_t> received_seq_out_of_order_
92 RTC_GUARDED_BY(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000093
94 // Current counter values.
Niels Möllerb615d1a2018-08-27 12:32:21 +020095 StreamDataCounters receive_counters_ RTC_GUARDED_BY(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000096
Qingsi Wang2370b082018-08-21 14:24:26 -070097 // Counter values when we sent the last report.
Niels Möller1a3859c2019-09-04 09:43:15 +020098 int32_t last_report_cumulative_loss_ RTC_GUARDED_BY(&stream_lock_);
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000099 int64_t last_report_seq_max_ RTC_GUARDED_BY(&stream_lock_);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000100};
101
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100102class ReceiveStatisticsImpl : public ReceiveStatistics {
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000103 public:
Niels Möllerd7819652019-08-13 14:43:02 +0200104 explicit ReceiveStatisticsImpl(Clock* clock);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000105
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100106 ~ReceiveStatisticsImpl() override;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000107
Niels Möllerdbb988b2018-11-15 08:05:16 +0100108 // Implements ReceiveStatisticsProvider.
danilchap0bc84232017-08-11 08:12:54 -0700109 std::vector<rtcp::ReportBlock> RtcpReportBlocks(size_t max_blocks) override;
110
Niels Möllerdbb988b2018-11-15 08:05:16 +0100111 // Implements RtpPacketSinkInterface
Niels Möller1f3206c2018-09-14 08:26:32 +0200112 void OnRtpPacket(const RtpPacketReceived& packet) override;
113
Niels Möllerdbb988b2018-11-15 08:05:16 +0100114 // Implements ReceiveStatistics.
Niels Möller87da1092019-05-24 14:04:28 +0200115 // Note: More specific return type for use in the implementation.
116 StreamStatisticianImpl* GetStatistician(uint32_t ssrc) const override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000117 void SetMaxReorderingThreshold(int max_reordering_threshold) override;
Niels Möller87da1092019-05-24 14:04:28 +0200118 void SetMaxReorderingThreshold(uint32_t ssrc,
119 int max_reordering_threshold) override;
Niels Möller5304a322018-08-27 13:27:05 +0200120 void EnableRetransmitDetection(uint32_t ssrc, bool enable) override;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000121
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000122 private:
Niels Möller87da1092019-05-24 14:04:28 +0200123 StreamStatisticianImpl* GetOrCreateStatistician(uint32_t ssrc);
124
sprangcd349d92016-07-13 09:11:28 -0700125 Clock* const clock_;
danilchap7c9426c2016-04-14 03:05:31 -0700126 rtc::CriticalSection receive_statistics_lock_;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100127 uint32_t last_returned_ssrc_;
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100128 int max_reordering_threshold_ RTC_GUARDED_BY(receive_statistics_lock_);
129 std::map<uint32_t, StreamStatisticianImpl*> statisticians_
130 RTC_GUARDED_BY(receive_statistics_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000131};
132} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200133#endif // MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_