xref: /aosp_15_r20/external/webrtc/modules/video_coding/utility/framerate_controller_deprecated.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
11 #include "modules/video_coding/utility/framerate_controller_deprecated.h"
12 
13 #include <stddef.h>
14 
15 #include <cstdint>
16 
17 namespace webrtc {
18 
FramerateControllerDeprecated(float target_framerate_fps)19 FramerateControllerDeprecated::FramerateControllerDeprecated(
20     float target_framerate_fps)
21     : min_frame_interval_ms_(0), framerate_estimator_(1000.0, 1000.0) {
22   SetTargetRate(target_framerate_fps);
23 }
24 
SetTargetRate(float target_framerate_fps)25 void FramerateControllerDeprecated::SetTargetRate(float target_framerate_fps) {
26   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 
GetTargetRate()38 float FramerateControllerDeprecated::GetTargetRate() {
39   return *target_framerate_fps_;
40 }
41 
Reset()42 void FramerateControllerDeprecated::Reset() {
43   framerate_estimator_.Reset();
44   last_timestamp_ms_.reset();
45 }
46 
DropFrame(uint32_t timestamp_ms) const47 bool FramerateControllerDeprecated::DropFrame(uint32_t timestamp_ms) const {
48   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 
AddFrame(uint32_t timestamp_ms)70 void FramerateControllerDeprecated::AddFrame(uint32_t timestamp_ms) {
71   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 
Rate(uint32_t timestamp_ms) const80 absl::optional<float> FramerateControllerDeprecated::Rate(
81     uint32_t timestamp_ms) const {
82   return framerate_estimator_.Rate(timestamp_ms);
83 }
84 
85 }  // namespace webrtc
86