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 
11 #ifndef MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_
12 #define MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_
13 
14 #include "modules/rtp_rtcp/include/receive_statistics.h"
15 
16 #include <algorithm>
17 #include <map>
18 #include <vector>
19 
20 #include "rtc_base/criticalsection.h"
21 #include "rtc_base/rate_statistics.h"
22 #include "system_wrappers/include/ntp_time.h"
23 
24 namespace webrtc {
25 
26 class StreamStatisticianImpl : public StreamStatistician {
27  public:
28   StreamStatisticianImpl(uint32_t ssrc,
29                          Clock* clock,
30                          RtcpStatisticsCallback* rtcp_callback,
31                          StreamDataCountersCallback* rtp_callback);
~StreamStatisticianImpl()32   virtual ~StreamStatisticianImpl() {}
33 
34   // |reset| here and in next method restarts calculation of fraction_lost stat.
35   bool GetStatistics(RtcpStatistics* statistics, bool reset) override;
36   bool GetActiveStatisticsAndReset(RtcpStatistics* statistics);
37   void GetDataCounters(size_t* bytes_received,
38                        uint32_t* packets_received) const override;
39   void GetReceiveStreamDataCounters(
40       StreamDataCounters* data_counters) const override;
41   uint32_t BitrateReceived() const override;
42   bool IsRetransmitOfOldPacket(const RTPHeader& header,
43                                int64_t min_rtt) const override;
44   bool IsPacketInOrder(uint16_t sequence_number) const override;
45 
46   void IncomingPacket(const RTPHeader& rtp_header,
47                       size_t packet_length,
48                       bool retransmitted);
49   void FecPacketReceived(const RTPHeader& header, size_t packet_length);
50   void SetMaxReorderingThreshold(int max_reordering_threshold);
51 
52  private:
53   bool InOrderPacketInternal(uint16_t sequence_number) const;
54   RtcpStatistics CalculateRtcpStatistics()
55       RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_lock_);
56   void UpdateJitter(const RTPHeader& header, NtpTime receive_time);
57   StreamDataCounters UpdateCounters(const RTPHeader& rtp_header,
58                                     size_t packet_length,
59                                     bool retransmitted);
60 
61   const uint32_t ssrc_;
62   Clock* const clock_;
63   rtc::CriticalSection stream_lock_;
64   RateStatistics incoming_bitrate_;
65   int max_reordering_threshold_;  // In number of packets or sequence numbers.
66 
67   // Stats on received RTP packets.
68   uint32_t jitter_q4_;
69   uint32_t cumulative_loss_;
70 
71   int64_t last_receive_time_ms_;
72   NtpTime last_receive_time_ntp_;
73   uint32_t last_received_timestamp_;
74   uint16_t received_seq_first_;
75   uint16_t received_seq_max_;
76   uint16_t received_seq_wraps_;
77 
78   // Current counter values.
79   size_t received_packet_overhead_;
80   StreamDataCounters receive_counters_;
81 
82   // Counter values when we sent the last report.
83   uint32_t last_report_inorder_packets_;
84   uint32_t last_report_old_packets_;
85   uint16_t last_report_seq_max_;
86   RtcpStatistics last_reported_statistics_;
87 
88   // stream_lock_ shouldn't be held when calling callbacks.
89   RtcpStatisticsCallback* const rtcp_callback_;
90   StreamDataCountersCallback* const rtp_callback_;
91 };
92 
93 class ReceiveStatisticsImpl : public ReceiveStatistics,
94                               public RtcpStatisticsCallback,
95                               public StreamDataCountersCallback {
96  public:
97   explicit ReceiveStatisticsImpl(Clock* clock);
98 
99   ~ReceiveStatisticsImpl();
100 
101   // Implement ReceiveStatisticsProvider.
102   std::vector<rtcp::ReportBlock> RtcpReportBlocks(size_t max_blocks) override;
103 
104   // Implement ReceiveStatistics.
105   void IncomingPacket(const RTPHeader& header,
106                       size_t packet_length,
107                       bool retransmitted) override;
108   void FecPacketReceived(const RTPHeader& header,
109                          size_t packet_length) override;
110   StreamStatistician* GetStatistician(uint32_t ssrc) const override;
111   void SetMaxReorderingThreshold(int max_reordering_threshold) override;
112 
113   void RegisterRtcpStatisticsCallback(
114       RtcpStatisticsCallback* callback) override;
115 
116   void RegisterRtpStatisticsCallback(
117       StreamDataCountersCallback* callback) override;
118 
119  private:
120   void StatisticsUpdated(const RtcpStatistics& statistics,
121                          uint32_t ssrc) override;
122   void CNameChanged(const char* cname, uint32_t ssrc) override;
123   void DataCountersUpdated(const StreamDataCounters& counters,
124                            uint32_t ssrc) override;
125 
126   Clock* const clock_;
127   rtc::CriticalSection receive_statistics_lock_;
128   std::map<uint32_t, StreamStatisticianImpl*> statisticians_;
129 
130   RtcpStatisticsCallback* rtcp_stats_callback_;
131   StreamDataCountersCallback* rtp_stats_callback_;
132 };
133 }  // namespace webrtc
134 #endif  // MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_
135