jitter_estimator.h 5.7 KB

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