• 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 #include "common_video/frame_rate_estimator.h"
12 
13 #include "rtc_base/time_utils.h"
14 
15 namespace webrtc {
16 
FrameRateEstimator(TimeDelta averaging_window)17 FrameRateEstimator::FrameRateEstimator(TimeDelta averaging_window)
18     : averaging_window_(averaging_window) {}
19 
OnFrame(Timestamp time)20 void FrameRateEstimator::OnFrame(Timestamp time) {
21   CullOld(time);
22   frame_times_.push_back(time);
23 }
24 
GetAverageFps() const25 absl::optional<double> FrameRateEstimator::GetAverageFps() const {
26   if (frame_times_.size() < 2) {
27     return absl::nullopt;
28   }
29   TimeDelta time_span = frame_times_.back() - frame_times_.front();
30   if (time_span < TimeDelta::Micros(1)) {
31     return absl::nullopt;
32   }
33   TimeDelta avg_frame_interval = time_span / (frame_times_.size() - 1);
34 
35   return static_cast<double>(rtc::kNumMicrosecsPerSec) /
36          avg_frame_interval.us();
37 }
38 
GetAverageFps(Timestamp now)39 absl::optional<double> FrameRateEstimator::GetAverageFps(Timestamp now) {
40   CullOld(now);
41   return GetAverageFps();
42 }
43 
Reset()44 void FrameRateEstimator::Reset() {
45   frame_times_.clear();
46 }
47 
CullOld(Timestamp now)48 void FrameRateEstimator::CullOld(Timestamp now) {
49   while (!frame_times_.empty() &&
50          frame_times_.front() + averaging_window_ < now) {
51     frame_times_.pop_front();
52   }
53 }
54 
55 }  // namespace webrtc
56