frame_rate_estimator.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (c) 2019 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 COMMON_VIDEO_FRAME_RATE_ESTIMATOR_H_
  11. #define COMMON_VIDEO_FRAME_RATE_ESTIMATOR_H_
  12. #include <deque>
  13. #include "absl/types/optional.h"
  14. #include "api/units/time_delta.h"
  15. #include "api/units/timestamp.h"
  16. namespace webrtc {
  17. // Class used to estimate a frame-rate using inter-frame intervals.
  18. // Some notes on usage:
  19. // This class is intended to accurately estimate the frame rate during a
  20. // continuous stream. Unlike a traditional rate estimator that looks at number
  21. // of data points within a time window, if the input stops this implementation
  22. // will not smoothly fall down towards 0. This is done so that the estimated
  23. // fps is not affected by edge conditions like if we sample just before or just
  24. // after the next frame.
  25. // To avoid problems if a stream is stopped and restarted (where estimated fps
  26. // could look too low), users of this class should explicitly call Reset() on
  27. // restart.
  28. // Also note that this class is not thread safe, it's up to the user to guard
  29. // against concurrent access.
  30. class FrameRateEstimator {
  31. public:
  32. explicit FrameRateEstimator(TimeDelta averaging_window);
  33. // Insert a frame, potentially culling old frames that falls outside the
  34. // averaging window.
  35. void OnFrame(Timestamp time);
  36. // Get the current average FPS, based on the frames currently in the window.
  37. absl::optional<double> GetAverageFps() const;
  38. // Move the window so it ends at |now|, and return the new fps estimate.
  39. absl::optional<double> GetAverageFps(Timestamp now);
  40. // Completely clear the averaging window.
  41. void Reset();
  42. private:
  43. void CullOld(Timestamp now);
  44. const TimeDelta averaging_window_;
  45. std::deque<Timestamp> frame_times_;
  46. };
  47. } // namespace webrtc
  48. #endif // COMMON_VIDEO_FRAME_RATE_ESTIMATOR_H_