blob: 4c5a341ac0c47a307b485cfd47c7c30e9e05aa0f [file] [log] [blame]
Erik Språng7daf5502019-08-14 11:04:40 +02001/*
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
15namespace webrtc {
16
17FrameRateEstimator::FrameRateEstimator(TimeDelta averaging_window)
18 : averaging_window_(averaging_window) {}
19
20void FrameRateEstimator::OnFrame(Timestamp time) {
21 CullOld(time);
22 frame_times_.push_back(time);
23}
24
25absl::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();
Danil Chapovalov0c626af2020-02-10 11:16:00 +010030 if (time_span < TimeDelta::Micros(1)) {
Erik Språng7daf5502019-08-14 11:04:40 +020031 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
39absl::optional<double> FrameRateEstimator::GetAverageFps(Timestamp now) {
40 CullOld(now);
41 return GetAverageFps();
42}
43
44void FrameRateEstimator::Reset() {
45 frame_times_.clear();
46}
47
48void 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