blob: 5978adc3c48fe4d3dcc46b26644acd99dbda7a47 [file] [log] [blame]
Sergey Silkinae3144c2018-08-29 01:05:26 +02001/*
2 * Copyright (c) 2018 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
Åsa Persson062acd92021-08-16 09:33:13 +020011#include "modules/video_coding/utility/framerate_controller_deprecated.h"
Sergey Silkinae3144c2018-08-29 01:05:26 +020012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Yves Gerey3e707812018-11-28 16:47:49 +010015#include <cstdint>
Sergey Silkinae3144c2018-08-29 01:05:26 +020016
17namespace webrtc {
18
Åsa Persson062acd92021-08-16 09:33:13 +020019FramerateControllerDeprecated::FramerateControllerDeprecated(
20 float target_framerate_fps)
Sergey Silkinae3144c2018-08-29 01:05:26 +020021 : min_frame_interval_ms_(0), framerate_estimator_(1000.0, 1000.0) {
22 SetTargetRate(target_framerate_fps);
23}
24
Åsa Persson062acd92021-08-16 09:33:13 +020025void FramerateControllerDeprecated::SetTargetRate(float target_framerate_fps) {
Sergey Silkinae3144c2018-08-29 01:05:26 +020026 if (target_framerate_fps_ != target_framerate_fps) {
27 framerate_estimator_.Reset();
28 if (last_timestamp_ms_) {
29 framerate_estimator_.Update(1, *last_timestamp_ms_);
30 }
31
32 const size_t target_frame_interval_ms = 1000 / target_framerate_fps;
33 target_framerate_fps_ = target_framerate_fps;
34 min_frame_interval_ms_ = 85 * target_frame_interval_ms / 100;
35 }
36}
37
Åsa Persson062acd92021-08-16 09:33:13 +020038float FramerateControllerDeprecated::GetTargetRate() {
Sergey Silkinae3144c2018-08-29 01:05:26 +020039 return *target_framerate_fps_;
40}
41
Åsa Persson062acd92021-08-16 09:33:13 +020042void FramerateControllerDeprecated::Reset() {
Sergey Silkinae3144c2018-08-29 01:05:26 +020043 framerate_estimator_.Reset();
44 last_timestamp_ms_.reset();
45}
46
Åsa Persson062acd92021-08-16 09:33:13 +020047bool FramerateControllerDeprecated::DropFrame(uint32_t timestamp_ms) const {
Sergey Silkinae3144c2018-08-29 01:05:26 +020048 if (timestamp_ms < last_timestamp_ms_) {
49 // Timestamp jumps backward. We can't make adequate drop decision. Don't
50 // drop this frame. Stats will be reset in AddFrame().
51 return false;
52 }
53
54 if (Rate(timestamp_ms).value_or(*target_framerate_fps_) >
55 target_framerate_fps_) {
56 return true;
57 }
58
59 if (last_timestamp_ms_) {
60 const int64_t diff_ms =
61 static_cast<int64_t>(timestamp_ms) - *last_timestamp_ms_;
62 if (diff_ms < min_frame_interval_ms_) {
63 return true;
64 }
65 }
66
67 return false;
68}
69
Åsa Persson062acd92021-08-16 09:33:13 +020070void FramerateControllerDeprecated::AddFrame(uint32_t timestamp_ms) {
Sergey Silkinae3144c2018-08-29 01:05:26 +020071 if (timestamp_ms < last_timestamp_ms_) {
72 // Timestamp jumps backward.
73 Reset();
74 }
75
76 framerate_estimator_.Update(1, timestamp_ms);
77 last_timestamp_ms_ = timestamp_ms;
78}
79
Åsa Persson062acd92021-08-16 09:33:13 +020080absl::optional<float> FramerateControllerDeprecated::Rate(
81 uint32_t timestamp_ms) const {
Sergey Silkinae3144c2018-08-29 01:05:26 +020082 return framerate_estimator_.Rate(timestamp_ms);
83}
84
85} // namespace webrtc