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