blob: b80a91a72e5b9e294439833054295c39e3fb1c81 [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
11#ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_
12#define WEBRTC_MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_
13
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010014#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000015
16#include <algorithm>
danilchapb8b6fbb2015-12-10 05:05:27 -080017#include <map>
danilchap0bc84232017-08-11 08:12:54 -070018#include <vector>
wu@webrtc.org822fbd82013-08-15 23:38:54 +000019
Edward Lemurc20978e2017-07-06 19:44:34 +020020#include "webrtc/rtc_base/criticalsection.h"
21#include "webrtc/rtc_base/rate_statistics.h"
danilchap1227e8b2015-12-21 11:06:50 -080022#include "webrtc/system_wrappers/include/ntp_time.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000023
24namespace webrtc {
25
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000026class StreamStatisticianImpl : public StreamStatistician {
wu@webrtc.org822fbd82013-08-15 23:38:54 +000027 public:
danilchapec86be02017-08-14 05:51:02 -070028 StreamStatisticianImpl(uint32_t ssrc,
29 Clock* clock,
sprang@webrtc.org0e932572014-01-23 10:00:39 +000030 RtcpStatisticsCallback* rtcp_callback,
31 StreamDataCountersCallback* rtp_callback);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000032 virtual ~StreamStatisticianImpl() {}
wu@webrtc.org822fbd82013-08-15 23:38:54 +000033
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000034 bool GetStatistics(RtcpStatistics* statistics, bool reset) override;
35 void GetDataCounters(size_t* bytes_received,
36 uint32_t* packets_received) const override;
37 void GetReceiveStreamDataCounters(
38 StreamDataCounters* data_counters) const override;
39 uint32_t BitrateReceived() const override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000040 bool IsRetransmitOfOldPacket(const RTPHeader& header,
41 int64_t min_rtt) const override;
42 bool IsPacketInOrder(uint16_t sequence_number) const override;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000043
sprang@webrtc.org0e932572014-01-23 10:00:39 +000044 void IncomingPacket(const RTPHeader& rtp_header,
asapersson@webrtc.org97d04892014-12-09 09:47:53 +000045 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000046 bool retransmitted);
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +000047 void FecPacketReceived(const RTPHeader& header, size_t packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000048 void SetMaxReorderingThreshold(int max_reordering_threshold);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000049 virtual void LastReceiveTimeNtp(uint32_t* secs, uint32_t* frac) const;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000050
51 private:
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000052 bool InOrderPacketInternal(uint16_t sequence_number) const;
danilchapec86be02017-08-14 05:51:02 -070053 RtcpStatistics CalculateRtcpStatistics()
danilchap56359be2017-09-07 07:53:45 -070054 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
danilchap1227e8b2015-12-21 11:06:50 -080055 void UpdateJitter(const RTPHeader& header, NtpTime receive_time);
danilchapec86be02017-08-14 05:51:02 -070056 StreamDataCounters UpdateCounters(const RTPHeader& rtp_header,
57 size_t packet_length,
58 bool retransmitted);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000059
danilchapec86be02017-08-14 05:51:02 -070060 const uint32_t ssrc_;
sprangcd349d92016-07-13 09:11:28 -070061 Clock* const clock_;
danilchap7c9426c2016-04-14 03:05:31 -070062 rtc::CriticalSection stream_lock_;
sprangcd349d92016-07-13 09:11:28 -070063 RateStatistics incoming_bitrate_;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000064 int max_reordering_threshold_; // In number of packets or sequence numbers.
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000065
wu@webrtc.org822fbd82013-08-15 23:38:54 +000066 // Stats on received RTP packets.
67 uint32_t jitter_q4_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000068 uint32_t cumulative_loss_;
69 uint32_t jitter_q4_transmission_time_offset_;
70
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +000071 int64_t last_receive_time_ms_;
danilchap1227e8b2015-12-21 11:06:50 -080072 NtpTime last_receive_time_ntp_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000073 uint32_t last_received_timestamp_;
74 int32_t last_received_transmission_time_offset_;
75 uint16_t received_seq_first_;
76 uint16_t received_seq_max_;
77 uint16_t received_seq_wraps_;
78
79 // Current counter values.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000080 size_t received_packet_overhead_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +000081 StreamDataCounters receive_counters_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000082
83 // Counter values when we sent the last report.
84 uint32_t last_report_inorder_packets_;
85 uint32_t last_report_old_packets_;
86 uint16_t last_report_seq_max_;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000087 RtcpStatistics last_reported_statistics_;
88
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
100 ~ReceiveStatisticsImpl();
101
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;
111 StatisticianMap GetActiveStatisticians() const override;
112 StreamStatistician* GetStatistician(uint32_t ssrc) const override;
113 void SetMaxReorderingThreshold(int max_reordering_threshold) override;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000114
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000115 void RegisterRtcpStatisticsCallback(
116 RtcpStatisticsCallback* callback) override;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000117
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000118 void RegisterRtpStatisticsCallback(
119 StreamDataCountersCallback* callback) override;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000120
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000121 private:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000122 void StatisticsUpdated(const RtcpStatistics& statistics,
123 uint32_t ssrc) override;
124 void CNameChanged(const char* cname, uint32_t ssrc) override;
125 void DataCountersUpdated(const StreamDataCounters& counters,
126 uint32_t ssrc) override;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000127
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000128 typedef std::map<uint32_t, StreamStatisticianImpl*> StatisticianImplMap;
129
sprangcd349d92016-07-13 09:11:28 -0700130 Clock* const clock_;
danilchap7c9426c2016-04-14 03:05:31 -0700131 rtc::CriticalSection receive_statistics_lock_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000132 StatisticianImplMap statisticians_;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000133
134 RtcpStatisticsCallback* rtcp_stats_callback_;
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000135 StreamDataCountersCallback* rtp_stats_callback_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000136};
137} // namespace webrtc
138#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_