blob: 6bba24ec0ed4a97469b4d2acd3b33542c98062c7 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/rtp_rtcp/include/receive_statistics.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000015
Taylor Brandstetter84916932018-06-25 15:50:26 -070016#include <math.h>
17
wu@webrtc.org822fbd82013-08-15 23:38:54 +000018#include <algorithm>
danilchapb8b6fbb2015-12-10 05:05:27 -080019#include <map>
danilchap0bc84232017-08-11 08:12:54 -070020#include <vector>
wu@webrtc.org822fbd82013-08-15 23:38:54 +000021
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/criticalsection.h"
23#include "rtc_base/rate_statistics.h"
24#include "system_wrappers/include/ntp_time.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000025
26namespace webrtc {
27
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000028class StreamStatisticianImpl : public StreamStatistician {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000029 public:
danilchapec86be02017-08-14 05:51:02 -070030 StreamStatisticianImpl(uint32_t ssrc,
31 Clock* clock,
sprang@webrtc.org0e932572014-01-23 10:00:39 +000032 RtcpStatisticsCallback* rtcp_callback,
33 StreamDataCountersCallback* rtp_callback);
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010034 ~StreamStatisticianImpl() override;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000035
Taylor Brandstetter84916932018-06-25 15:50:26 -070036 bool GetStatistics(RtcpStatistics* statistics,
37 bool update_fraction_lost) 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;
41 void GetReceiveStreamDataCounters(
42 StreamDataCounters* data_counters) const override;
43 uint32_t BitrateReceived() const override;
Niels Möllereda00872018-05-23 13:54:51 +020044 bool IsRetransmitOfOldPacket(const RTPHeader& header) const override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000045 bool IsPacketInOrder(uint16_t sequence_number) const override;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000046
sprang@webrtc.org0e932572014-01-23 10:00:39 +000047 void IncomingPacket(const RTPHeader& rtp_header,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +000048 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000049 bool retransmitted);
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +000050 void FecPacketReceived(const RTPHeader& header, size_t packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000051 void SetMaxReorderingThreshold(int max_reordering_threshold);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000052
53 private:
Taylor Brandstetter84916932018-06-25 15:50:26 -070054 bool InOrderPacketInternal(uint16_t sequence_number) const
55 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
56 RtcpStatistics CalculateRtcpStatistics(bool update_fraction_lost)
danilchap56359be2017-09-07 07:53:45 -070057 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
danilchap1227e8b2015-12-21 11:06:50 -080058 void UpdateJitter(const RTPHeader& header, NtpTime receive_time);
danilchapec86be02017-08-14 05:51:02 -070059 StreamDataCounters UpdateCounters(const RTPHeader& rtp_header,
60 size_t packet_length,
Taylor Brandstetter84916932018-06-25 15:50:26 -070061 bool retransmitted)
62 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000063
danilchapec86be02017-08-14 05:51:02 -070064 const uint32_t ssrc_;
sprangcd349d92016-07-13 09:11:28 -070065 Clock* const clock_;
danilchap7c9426c2016-04-14 03:05:31 -070066 rtc::CriticalSection stream_lock_;
sprangcd349d92016-07-13 09:11:28 -070067 RateStatistics incoming_bitrate_;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000068 int max_reordering_threshold_; // In number of packets or sequence numbers.
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000069
wu@webrtc.org822fbd82013-08-15 23:38:54 +000070 // Stats on received RTP packets.
71 uint32_t jitter_q4_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000072
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000073 int64_t last_receive_time_ms_;
danilchap1227e8b2015-12-21 11:06:50 -080074 NtpTime last_receive_time_ntp_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000075 uint32_t last_received_timestamp_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000076 uint16_t received_seq_first_;
77 uint16_t received_seq_max_;
78 uint16_t received_seq_wraps_;
79
80 // Current counter values.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000081 size_t received_packet_overhead_;
Taylor Brandstetter84916932018-06-25 15:50:26 -070082 StreamDataCounters receive_counters_ RTC_GUARDED_BY(stream_lock_);
wu@webrtc.org822fbd82013-08-15 23:38:54 +000083
Taylor Brandstetter84916932018-06-25 15:50:26 -070084 // Used to calculate fraction_lost between reports.
85 uint32_t last_report_received_packets_ = 0;
86 uint32_t last_report_extended_seq_max_ = 0;
87 uint8_t last_fraction_lost_ = 0;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000088
danilchapec86be02017-08-14 05:51:02 -070089 // stream_lock_ shouldn't be held when calling callbacks.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000090 RtcpStatisticsCallback* const rtcp_callback_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +000091 StreamDataCountersCallback* const rtp_callback_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000092};
93
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000094class ReceiveStatisticsImpl : public ReceiveStatistics,
sprang@webrtc.org0e932572014-01-23 10:00:39 +000095 public RtcpStatisticsCallback,
96 public StreamDataCountersCallback {
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000097 public:
98 explicit ReceiveStatisticsImpl(Clock* clock);
99
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +0100100 ~ReceiveStatisticsImpl() override;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000101
danilchap0bc84232017-08-11 08:12:54 -0700102 // Implement ReceiveStatisticsProvider.
103 std::vector<rtcp::ReportBlock> RtcpReportBlocks(size_t max_blocks) override;
104
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000105 // Implement ReceiveStatistics.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000106 void IncomingPacket(const RTPHeader& header,
107 size_t packet_length,
108 bool retransmitted) override;
109 void FecPacketReceived(const RTPHeader& header,
110 size_t packet_length) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000111 StreamStatistician* GetStatistician(uint32_t ssrc) const override;
112 void SetMaxReorderingThreshold(int max_reordering_threshold) override;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000113
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000114 void RegisterRtcpStatisticsCallback(
115 RtcpStatisticsCallback* callback) override;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000116
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000117 void RegisterRtpStatisticsCallback(
118 StreamDataCountersCallback* callback) override;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000119
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000120 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000121 void StatisticsUpdated(const RtcpStatistics& statistics,
122 uint32_t ssrc) override;
123 void CNameChanged(const char* cname, uint32_t ssrc) override;
124 void DataCountersUpdated(const StreamDataCounters& counters,
125 uint32_t ssrc) override;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000126
sprangcd349d92016-07-13 09:11:28 -0700127 Clock* const clock_;
danilchap7c9426c2016-04-14 03:05:31 -0700128 rtc::CriticalSection receive_statistics_lock_;
Danil Chapovalovd1996b72018-01-16 11:07:18 +0100129 uint32_t last_returned_ssrc_;
Danil Chapovalovc5267d22017-09-18 13:57:19 +0200130 std::map<uint32_t, StreamStatisticianImpl*> statisticians_;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000131
132 RtcpStatisticsCallback* rtcp_stats_callback_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000133 StreamDataCountersCallback* rtp_stats_callback_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000134};
135} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200136#endif // MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_