Home | History | Annotate | Download | only in source
      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/interface/receive_statistics.h"
     15 
     16 #include <algorithm>
     17 
     18 #include "webrtc/modules/rtp_rtcp/source/bitrate.h"
     19 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
     20 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
     21 
     22 namespace webrtc {
     23 
     24 class CriticalSectionWrapper;
     25 
     26 class StreamStatisticianImpl : public StreamStatistician {
     27  public:
     28   StreamStatisticianImpl(Clock* clock,
     29                          RtcpStatisticsCallback* rtcp_callback,
     30                          StreamDataCountersCallback* rtp_callback);
     31   virtual ~StreamStatisticianImpl() {}
     32 
     33   virtual bool GetStatistics(RtcpStatistics* statistics, bool reset) OVERRIDE;
     34   virtual void GetDataCounters(uint32_t* bytes_received,
     35                                uint32_t* packets_received) const OVERRIDE;
     36   virtual uint32_t BitrateReceived() const OVERRIDE;
     37   virtual void ResetStatistics() OVERRIDE;
     38   virtual bool IsRetransmitOfOldPacket(const RTPHeader& header,
     39                                        int min_rtt) const OVERRIDE;
     40   virtual bool IsPacketInOrder(uint16_t sequence_number) const OVERRIDE;
     41 
     42   void IncomingPacket(const RTPHeader& rtp_header,
     43                       size_t bytes,
     44                       bool retransmitted);
     45   void FecPacketReceived();
     46   void SetMaxReorderingThreshold(int max_reordering_threshold);
     47   void ProcessBitrate();
     48   virtual void LastReceiveTimeNtp(uint32_t* secs, uint32_t* frac) const;
     49 
     50  private:
     51   bool InOrderPacketInternal(uint16_t sequence_number) const;
     52   RtcpStatistics CalculateRtcpStatistics();
     53   void UpdateJitter(const RTPHeader& header,
     54                     uint32_t receive_time_secs,
     55                     uint32_t receive_time_frac);
     56   void UpdateCounters(const RTPHeader& rtp_header,
     57                       size_t bytes,
     58                       bool retransmitted);
     59   void NotifyRtpCallback() LOCKS_EXCLUDED(stream_lock_.get());
     60   void NotifyRtcpCallback() LOCKS_EXCLUDED(stream_lock_.get());
     61 
     62   Clock* clock_;
     63   scoped_ptr<CriticalSectionWrapper> stream_lock_;
     64   Bitrate incoming_bitrate_;
     65   uint32_t ssrc_;
     66   int max_reordering_threshold_;  // In number of packets or sequence numbers.
     67 
     68   // Stats on received RTP packets.
     69   uint32_t jitter_q4_;
     70   uint32_t cumulative_loss_;
     71   uint32_t jitter_q4_transmission_time_offset_;
     72 
     73   int64_t last_receive_time_ms_;
     74   uint32_t last_receive_time_secs_;
     75   uint32_t last_receive_time_frac_;
     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   uint16_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   virtual void IncomingPacket(const RTPHeader& header,
    106                               size_t bytes,
    107                               bool retransmitted) OVERRIDE;
    108   virtual void FecPacketReceived(uint32_t ssrc) OVERRIDE;
    109   virtual StatisticianMap GetActiveStatisticians() const OVERRIDE;
    110   virtual StreamStatistician* GetStatistician(uint32_t ssrc) const OVERRIDE;
    111   virtual void SetMaxReorderingThreshold(int max_reordering_threshold) OVERRIDE;
    112 
    113   // Implement Module.
    114   virtual int32_t Process() OVERRIDE;
    115   virtual int32_t TimeUntilNextProcess() OVERRIDE;
    116 
    117   virtual void RegisterRtcpStatisticsCallback(RtcpStatisticsCallback* callback)
    118       OVERRIDE;
    119 
    120   virtual void RegisterRtpStatisticsCallback(
    121       StreamDataCountersCallback* callback) OVERRIDE;
    122 
    123  private:
    124   virtual void StatisticsUpdated(const RtcpStatistics& statistics,
    125                                  uint32_t ssrc) OVERRIDE;
    126   virtual void DataCountersUpdated(const StreamDataCounters& counters,
    127                                    uint32_t ssrc) OVERRIDE;
    128 
    129   typedef std::map<uint32_t, StreamStatisticianImpl*> StatisticianImplMap;
    130 
    131   Clock* clock_;
    132   scoped_ptr<CriticalSectionWrapper> receive_statistics_lock_;
    133   int64_t last_rate_update_ms_;
    134   StatisticianImplMap statisticians_;
    135 
    136   RtcpStatisticsCallback* rtcp_stats_callback_;
    137   StreamDataCountersCallback* rtp_stats_callback_;
    138 };
    139 }  // namespace webrtc
    140 #endif  // WEBRTC_MODULES_RTP_RTCP_SOURCE_RECEIVE_STATISTICS_IMPL_H_
    141