Erik Språng | 7daf550 | 2019-08-14 11:04:40 +0200 | [diff] [blame] | 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 | |
| 17 | FrameRateEstimator::FrameRateEstimator(TimeDelta averaging_window) |
| 18 | : averaging_window_(averaging_window) {} |
| 19 | |
| 20 | void FrameRateEstimator::OnFrame(Timestamp time) { |
| 21 | CullOld(time); |
| 22 | frame_times_.push_back(time); |
| 23 | } |
| 24 | |
| 25 | 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(); |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 30 | if (time_span < TimeDelta::Micros(1)) { |
Erik Språng | 7daf550 | 2019-08-14 11:04:40 +0200 | [diff] [blame] | 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 | |
| 39 | absl::optional<double> FrameRateEstimator::GetAverageFps(Timestamp now) { |
| 40 | CullOld(now); |
| 41 | return GetAverageFps(); |
| 42 | } |
| 43 | |
| 44 | void FrameRateEstimator::Reset() { |
| 45 | frame_times_.clear(); |
| 46 | } |
| 47 | |
| 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 |