blob: a4eab7b6a2639f1f6c9db493e458b28e06d672c2 [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öllerdbb988b2018-11-15 08:05:16 +010027class StreamStatisticianImpl : public StreamStatistician,
28 public RtpPacketSinkInterface {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000029 public:
danilchapec86be02017-08-14 05:51:02 -070030 StreamStatisticianImpl(uint32_t ssrc,
31 Clock* clock,
Danil Chapovalovebb50c22018-11-22 14:04:02 +010032 int max_reordering_threshold,
sprang@webrtc.org0e932572014-01-23 10:00:39 +000033 StreamDataCountersCallback* rtp_callback);
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010034 ~StreamStatisticianImpl() override;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000035
Qingsi Wang2370b082018-08-21 14:24:26 -070036 // |reset| here and in next method restarts calculation of fraction_lost stat.
37 bool GetStatistics(RtcpStatistics* statistics, bool reset) override;
Danil Chapovalovc5267d22017-09-18 13:57:19 +020038 bool GetActiveStatisticsAndReset(RtcpStatistics* statistics);
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000039 void GetDataCounters(size_t* bytes_received,
40 uint32_t* packets_received) const override;
Niels Möller9a9f18a2019-08-02 13:52:37 +020041 absl::optional<int> GetFractionLostInPercent() const override;
Niels Möller58b496b2019-08-12 12:16:31 +020042 StreamDataCounters GetReceiveStreamDataCounters() const override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000043 uint32_t BitrateReceived() const override;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000044
Niels Möllerdbb988b2018-11-15 08:05:16 +010045 // Implements RtpPacketSinkInterface
46 void OnRtpPacket(const RtpPacketReceived& packet) override;
47
48 void FecPacketReceived(const RtpPacketReceived& packet);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000049 void SetMaxReorderingThreshold(int max_reordering_threshold);
Niels Möller5304a322018-08-27 13:27:05 +020050 void EnableRetransmitDetection(bool enable);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000051
52 private:
Danil Chapovalov44727b42018-11-22 11:28:45 +010053 bool IsRetransmitOfOldPacket(const RtpPacketReceived& packet,
54 int64_t now_ms) const
Niels Möllerb615d1a2018-08-27 12:32:21 +020055 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
Qingsi Wang2370b082018-08-21 14:24:26 -070056 RtcpStatistics CalculateRtcpStatistics()
danilchap56359be2017-09-07 07:53:45 -070057 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
Danil Chapovalov856cf222018-11-26 10:20:01 +010058 void UpdateJitter(const RtpPacketReceived& packet, int64_t receive_time_ms)
Niels Möllerb615d1a2018-08-27 12:32:21 +020059 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000060 // Updates StreamStatistician for out of order packets.
61 // Returns true if packet considered to be out of order.
62 bool UpdateOutOfOrder(const RtpPacketReceived& packet,
63 int64_t sequence_number,
64 int64_t now_ms)
65 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
66 // Updates StreamStatistician for incoming packets.
Danil Chapovalov44727b42018-11-22 11:28:45 +010067 StreamDataCounters UpdateCounters(const RtpPacketReceived& packet);
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000068 // Checks if this StreamStatistician received any rtp packets.
69 bool ReceivedRtpPacket() const RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_) {
70 return received_seq_max_ >= 0;
71 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000072
danilchapec86be02017-08-14 05:51:02 -070073 const uint32_t ssrc_;
sprangcd349d92016-07-13 09:11:28 -070074 Clock* const clock_;
danilchap7c9426c2016-04-14 03:05:31 -070075 rtc::CriticalSection stream_lock_;
Niels Möllerb615d1a2018-08-27 12:32:21 +020076 RateStatistics incoming_bitrate_ RTC_GUARDED_BY(&stream_lock_);
77 // In number of packets or sequence numbers.
78 int max_reordering_threshold_ RTC_GUARDED_BY(&stream_lock_);
Niels Möller5304a322018-08-27 13:27:05 +020079 bool enable_retransmit_detection_ RTC_GUARDED_BY(&stream_lock_);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000080
wu@webrtc.org822fbd82013-08-15 23:38:54 +000081 // Stats on received RTP packets.
Niels Möllerb615d1a2018-08-27 12:32:21 +020082 uint32_t jitter_q4_ RTC_GUARDED_BY(&stream_lock_);
83 uint32_t cumulative_loss_ RTC_GUARDED_BY(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000084
Niels Möllerb615d1a2018-08-27 12:32:21 +020085 int64_t last_receive_time_ms_ RTC_GUARDED_BY(&stream_lock_);
Niels Möllerb615d1a2018-08-27 12:32:21 +020086 uint32_t last_received_timestamp_ RTC_GUARDED_BY(&stream_lock_);
Danil Chapovalovb438b5a2018-12-05 14:55:46 +000087 SequenceNumberUnwrapper seq_unwrapper_ RTC_GUARDED_BY(&stream_lock_);
88 int64_t received_seq_first_ RTC_GUARDED_BY(&stream_lock_);
89 int64_t received_seq_max_ RTC_GUARDED_BY(&stream_lock_);
90 // Assume that the other side restarted when there are two sequential packets
91 // with large jump from received_seq_max_.
92 absl::optional<uint16_t> received_seq_out_of_order_
93 RTC_GUARDED_BY(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000094
95 // Current counter values.
Niels Möllerb615d1a2018-08-27 12:32:21 +020096 StreamDataCounters receive_counters_ RTC_GUARDED_BY(&stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000097
Qingsi Wang2370b082018-08-21 14:24:26 -070098 // Counter values when we sent the last report.
Niels Möllerb615d1a2018-08-27 12:32:21 +020099 uint32_t last_report_inorder_packets_ RTC_GUARDED_BY(&stream_lock_);
100 uint32_t last_report_old_packets_ RTC_GUARDED_BY(&stream_lock_);
Danil Chapovalovb438b5a2018-12-05 14:55:46 +0000101 int64_t last_report_seq_max_ RTC_GUARDED_BY(&stream_lock_);
Niels Möllerb615d1a2018-08-27 12:32:21 +0200102 RtcpStatistics last_reported_statistics_ RTC_GUARDED_BY(&stream_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000103
danilchapec86be02017-08-14 05:51:02 -0700104 // stream_lock_ shouldn't be held when calling callbacks.
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000105 StreamDataCountersCallback* const rtp_callback_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000106};
107
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100108class ReceiveStatisticsImpl : public ReceiveStatistics {
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000109 public:
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100110 ReceiveStatisticsImpl(Clock* clock,
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100111 StreamDataCountersCallback* rtp_callback);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000112
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100113 ~ReceiveStatisticsImpl() override;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000114
Niels Möllerdbb988b2018-11-15 08:05:16 +0100115 // Implements ReceiveStatisticsProvider.
danilchap0bc84232017-08-11 08:12:54 -0700116 std::vector<rtcp::ReportBlock> RtcpReportBlocks(size_t max_blocks) override;
117
Niels Möllerdbb988b2018-11-15 08:05:16 +0100118 // Implements RtpPacketSinkInterface
Niels Möller1f3206c2018-09-14 08:26:32 +0200119 void OnRtpPacket(const RtpPacketReceived& packet) override;
120
Niels Möllerdbb988b2018-11-15 08:05:16 +0100121 // Implements ReceiveStatistics.
Niels Möller1f3206c2018-09-14 08:26:32 +0200122 void FecPacketReceived(const RtpPacketReceived& packet) override;
Niels Möller87da1092019-05-24 14:04:28 +0200123 // Note: More specific return type for use in the implementation.
124 StreamStatisticianImpl* GetStatistician(uint32_t ssrc) const override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000125 void SetMaxReorderingThreshold(int max_reordering_threshold) override;
Niels Möller87da1092019-05-24 14:04:28 +0200126 void SetMaxReorderingThreshold(uint32_t ssrc,
127 int max_reordering_threshold) override;
Niels Möller5304a322018-08-27 13:27:05 +0200128 void EnableRetransmitDetection(uint32_t ssrc, bool enable) override;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000129
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000130 private:
Niels Möller87da1092019-05-24 14:04:28 +0200131 StreamStatisticianImpl* GetOrCreateStatistician(uint32_t ssrc);
132
sprangcd349d92016-07-13 09:11:28 -0700133 Clock* const clock_;
danilchap7c9426c2016-04-14 03:05:31 -0700134 rtc::CriticalSection receive_statistics_lock_;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100135 uint32_t last_returned_ssrc_;
Danil Chapovalovebb50c22018-11-22 14:04:02 +0100136 int max_reordering_threshold_ RTC_GUARDED_BY(receive_statistics_lock_);
137 std::map<uint32_t, StreamStatisticianImpl*> statisticians_
138 RTC_GUARDED_BY(receive_statistics_lock_);
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000139
Danil Chapovalov8ce0d2b2018-11-23 11:03:25 +0100140 StreamDataCountersCallback* const rtp_stats_callback_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000141};
142} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200143#endif // MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_