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 WEBRTC_MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_
12 #define WEBRTC_MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_
13 
14 #include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
15 
16 #include <algorithm>
17 #include <map>
18 
19 #include "webrtc/base/scoped_ptr.h"
20 #include "webrtc/modules/rtp_rtcp/source/bitrate.h"
21 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
22 #include "webrtc/system_wrappers/include/ntp_time.h"
23 
24 namespace webrtc {
25 
26 class CriticalSectionWrapper;
27 
28 class StreamStatisticianImpl : public StreamStatistician {
29  public:
30   StreamStatisticianImpl(Clock* clock,
31                          RtcpStatisticsCallback* rtcp_callback,
32                          StreamDataCountersCallback* rtp_callback);
~StreamStatisticianImpl()33   virtual ~StreamStatisticianImpl() {}
34 
35   bool GetStatistics(RtcpStatistics* statistics, bool reset) override;
36   void GetDataCounters(size_t* bytes_received,
37                        uint32_t* packets_received) const override;
38   void GetReceiveStreamDataCounters(
39       StreamDataCounters* data_counters) const override;
40   uint32_t BitrateReceived() const override;
41   bool IsRetransmitOfOldPacket(const RTPHeader& header,
42                                int64_t min_rtt) const override;
43   bool IsPacketInOrder(uint16_t sequence_number) const override;
44 
45   void IncomingPacket(const RTPHeader& rtp_header,
46                       size_t packet_length,
47                       bool retransmitted);
48   void FecPacketReceived(const RTPHeader& header, size_t packet_length);
49   void SetMaxReorderingThreshold(int max_reordering_threshold);
50   void ProcessBitrate();
51   virtual void LastReceiveTimeNtp(uint32_t* secs, uint32_t* frac) const;
52 
53  private:
54   bool InOrderPacketInternal(uint16_t sequence_number) const;
55   RtcpStatistics CalculateRtcpStatistics();
56   void UpdateJitter(const RTPHeader& header, NtpTime receive_time);
57   void UpdateCounters(const RTPHeader& rtp_header,
58                       size_t packet_length,
59                       bool retransmitted);
60   void NotifyRtpCallback() LOCKS_EXCLUDED(stream_lock_.get());
61   void NotifyRtcpCallback() LOCKS_EXCLUDED(stream_lock_.get());
62 
63   Clock* clock_;
64   rtc::scoped_ptr<CriticalSectionWrapper> stream_lock_;
65   Bitrate incoming_bitrate_;
66   uint32_t ssrc_;
67   int max_reordering_threshold_;  // In number of packets or sequence numbers.
68 
69   // Stats on received RTP packets.
70   uint32_t jitter_q4_;
71   uint32_t cumulative_loss_;
72   uint32_t jitter_q4_transmission_time_offset_;
73 
74   int64_t last_receive_time_ms_;
75   NtpTime last_receive_time_ntp_;
76   uint32_t last_received_timestamp_;
77   int32_t last_received_transmission_time_offset_;
78   uint16_t received_seq_first_;
79   uint16_t received_seq_max_;
80   uint16_t received_seq_wraps_;
81 
82   // Current counter values.
83   size_t received_packet_overhead_;
84   StreamDataCounters receive_counters_;
85 
86   // Counter values when we sent the last report.
87   uint32_t last_report_inorder_packets_;
88   uint32_t last_report_old_packets_;
89   uint16_t last_report_seq_max_;
90   RtcpStatistics last_reported_statistics_;
91 
92   RtcpStatisticsCallback* const rtcp_callback_;
93   StreamDataCountersCallback* const rtp_callback_;
94 };
95 
96 class ReceiveStatisticsImpl : public ReceiveStatistics,
97                               public RtcpStatisticsCallback,
98                               public StreamDataCountersCallback {
99  public:
100   explicit ReceiveStatisticsImpl(Clock* clock);
101 
102   ~ReceiveStatisticsImpl();
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   StatisticianMap GetActiveStatisticians() const override;
111   StreamStatistician* GetStatistician(uint32_t ssrc) const override;
112   void SetMaxReorderingThreshold(int max_reordering_threshold) override;
113 
114   // Implement Module.
115   int32_t Process() override;
116   int64_t TimeUntilNextProcess() override;
117 
118   void RegisterRtcpStatisticsCallback(
119       RtcpStatisticsCallback* callback) override;
120 
121   void RegisterRtpStatisticsCallback(
122       StreamDataCountersCallback* callback) override;
123 
124  private:
125   void StatisticsUpdated(const RtcpStatistics& statistics,
126                          uint32_t ssrc) override;
127   void CNameChanged(const char* cname, uint32_t ssrc) override;
128   void DataCountersUpdated(const StreamDataCounters& counters,
129                            uint32_t ssrc) override;
130 
131   typedef std::map<uint32_t, StreamStatisticianImpl*> StatisticianImplMap;
132 
133   Clock* clock_;
134   rtc::scoped_ptr<CriticalSectionWrapper> receive_statistics_lock_;
135   int64_t last_rate_update_ms_;
136   StatisticianImplMap statisticians_;
137 
138   RtcpStatisticsCallback* rtcp_stats_callback_;
139   StreamDataCountersCallback* rtp_stats_callback_;
140 };
141 }  // namespace webrtc
142 #endif  // WEBRTC_MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_
143