Home | History | Annotate | Download | only in source
      1 /*
      2  *  Copyright (c) 2011 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_VIDEO_CODING_JITTER_ESTIMATOR_H_
     12 #define WEBRTC_MODULES_VIDEO_CODING_JITTER_ESTIMATOR_H_
     13 
     14 #include "webrtc/modules/video_coding/main/source/rtt_filter.h"
     15 #include "webrtc/typedefs.h"
     16 
     17 namespace webrtc
     18 {
     19 
     20 class VCMJitterEstimator
     21 {
     22 public:
     23     VCMJitterEstimator(int32_t vcmId = 0, int32_t receiverId = 0);
     24 
     25     VCMJitterEstimator& operator=(const VCMJitterEstimator& rhs);
     26 
     27     // Resets the estimate to the initial state
     28     void Reset();
     29     void ResetNackCount();
     30 
     31     // Updates the jitter estimate with the new data.
     32     //
     33     // Input:
     34     //          - frameDelay      : Delay-delta calculated by UTILDelayEstimate in milliseconds
     35     //          - frameSize       : Frame size of the current frame.
     36     //          - incompleteFrame : Flags if the frame is used to update the estimate before it
     37     //                              was complete. Default is false.
     38     void UpdateEstimate(int64_t frameDelayMS,
     39                         uint32_t frameSizeBytes,
     40                         bool incompleteFrame = false);
     41 
     42     // Returns the current jitter estimate in milliseconds and adds
     43     // also adds an RTT dependent term in cases of retransmission.
     44     //  Input:
     45     //          - rttMultiplier  : RTT param multiplier (when applicable).
     46     //
     47     // Return value                   : Jitter estimate in milliseconds
     48     int GetJitterEstimate(double rttMultiplier);
     49 
     50     // Updates the nack counter.
     51     void FrameNacked();
     52 
     53     // Updates the RTT filter.
     54     //
     55     // Input:
     56     //          - rttMs               : RTT in ms
     57     void UpdateRtt(uint32_t rttMs);
     58 
     59     void UpdateMaxFrameSize(uint32_t frameSizeBytes);
     60 
     61     // A constant describing the delay from the jitter buffer
     62     // to the delay on the receiving side which is not accounted
     63     // for by the jitter buffer nor the decoding delay estimate.
     64     static const uint32_t OPERATING_SYSTEM_JITTER = 10;
     65 
     66 protected:
     67     // These are protected for better testing possibilities
     68     double              _theta[2]; // Estimated line parameters (slope, offset)
     69     double              _varNoise; // Variance of the time-deviation from the line
     70 
     71 private:
     72     // Updates the Kalman filter for the line describing
     73     // the frame size dependent jitter.
     74     //
     75     // Input:
     76     //          - frameDelayMS    : Delay-delta calculated by UTILDelayEstimate in milliseconds
     77     //          - deltaFSBytes    : Frame size delta, i.e.
     78     //                            : frame size at time T minus frame size at time T-1
     79     void KalmanEstimateChannel(int64_t frameDelayMS, int32_t deltaFSBytes);
     80 
     81     // Updates the random jitter estimate, i.e. the variance
     82     // of the time deviations from the line given by the Kalman filter.
     83     //
     84     // Input:
     85     //          - d_dT              : The deviation from the kalman estimate
     86     //          - incompleteFrame   : True if the frame used to update the estimate
     87     //                                with was incomplete
     88     void EstimateRandomJitter(double d_dT, bool incompleteFrame);
     89 
     90     double NoiseThreshold() const;
     91 
     92     // Calculates the current jitter estimate.
     93     //
     94     // Return value                 : The current jitter estimate in milliseconds
     95     double CalculateEstimate();
     96 
     97     // Post process the calculated estimate
     98     void PostProcessEstimate();
     99 
    100     // Calculates the difference in delay between a sample and the
    101     // expected delay estimated by the Kalman filter.
    102     //
    103     // Input:
    104     //          - frameDelayMS    : Delay-delta calculated by UTILDelayEstimate in milliseconds
    105     //          - deltaFS         : Frame size delta, i.e. frame size at time
    106     //                              T minus frame size at time T-1
    107     //
    108     // Return value                 : The difference in milliseconds
    109     double DeviationFromExpectedDelay(int64_t frameDelayMS,
    110                                       int32_t deltaFSBytes) const;
    111 
    112     // Constants, filter parameters
    113     int32_t         _vcmId;
    114     int32_t         _receiverId;
    115     const double          _phi;
    116     const double          _psi;
    117     const uint32_t  _alphaCountMax;
    118     const double          _thetaLow;
    119     const uint32_t  _nackLimit;
    120     const int32_t   _numStdDevDelayOutlier;
    121     const int32_t   _numStdDevFrameSizeOutlier;
    122     const double          _noiseStdDevs;
    123     const double          _noiseStdDevOffset;
    124 
    125     double                _thetaCov[2][2]; // Estimate covariance
    126     double                _Qcov[2][2];     // Process noise covariance
    127     double                _avgFrameSize;   // Average frame size
    128     double                _varFrameSize;   // Frame size variance
    129     double                _maxFrameSize;   // Largest frame size received (descending
    130                                            // with a factor _psi)
    131     uint32_t        _fsSum;
    132     uint32_t        _fsCount;
    133 
    134     int64_t         _lastUpdateT;
    135     double                _prevEstimate;         // The previously returned jitter estimate
    136     uint32_t        _prevFrameSize;        // Frame size of the previous frame
    137     double                _avgNoise;             // Average of the random jitter
    138     uint32_t        _alphaCount;
    139     double                _filterJitterEstimate; // The filtered sum of jitter estimates
    140 
    141     uint32_t        _startupCount;
    142 
    143     int64_t         _latestNackTimestamp;  // Timestamp in ms when the latest nack was seen
    144     uint32_t        _nackCount;            // Keeps track of the number of nacks received,
    145                                                  // but never goes above _nackLimit
    146     VCMRttFilter          _rttFilter;
    147 
    148     enum { kStartupDelaySamples = 30 };
    149     enum { kFsAccuStartupSamples = 5 };
    150 };
    151 
    152 }  // namespace webrtc
    153 
    154 #endif // WEBRTC_MODULES_VIDEO_CODING_JITTER_ESTIMATOR_H_
    155