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