xref: /aosp_15_r20/external/webrtc/video/adaptation/encode_usage_resource.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2020 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 "video/adaptation/encode_usage_resource.h"
12 
13 #include <limits>
14 #include <utility>
15 
16 #include "rtc_base/checks.h"
17 
18 namespace webrtc {
19 
20 // static
Create(std::unique_ptr<OveruseFrameDetector> overuse_detector)21 rtc::scoped_refptr<EncodeUsageResource> EncodeUsageResource::Create(
22     std::unique_ptr<OveruseFrameDetector> overuse_detector) {
23   return rtc::make_ref_counted<EncodeUsageResource>(
24       std::move(overuse_detector));
25 }
26 
EncodeUsageResource(std::unique_ptr<OveruseFrameDetector> overuse_detector)27 EncodeUsageResource::EncodeUsageResource(
28     std::unique_ptr<OveruseFrameDetector> overuse_detector)
29     : VideoStreamEncoderResource("EncoderUsageResource"),
30       overuse_detector_(std::move(overuse_detector)),
31       is_started_(false),
32       target_frame_rate_(absl::nullopt) {
33   RTC_DCHECK(overuse_detector_);
34 }
35 
~EncodeUsageResource()36 EncodeUsageResource::~EncodeUsageResource() {}
37 
is_started() const38 bool EncodeUsageResource::is_started() const {
39   RTC_DCHECK_RUN_ON(encoder_queue());
40   return is_started_;
41 }
42 
StartCheckForOveruse(CpuOveruseOptions options)43 void EncodeUsageResource::StartCheckForOveruse(CpuOveruseOptions options) {
44   RTC_DCHECK_RUN_ON(encoder_queue());
45   RTC_DCHECK(!is_started_);
46   overuse_detector_->StartCheckForOveruse(TaskQueueBase::Current(),
47                                           std::move(options), this);
48   is_started_ = true;
49   overuse_detector_->OnTargetFramerateUpdated(TargetFrameRateAsInt());
50 }
51 
StopCheckForOveruse()52 void EncodeUsageResource::StopCheckForOveruse() {
53   RTC_DCHECK_RUN_ON(encoder_queue());
54   overuse_detector_->StopCheckForOveruse();
55   is_started_ = false;
56 }
57 
SetTargetFrameRate(absl::optional<double> target_frame_rate)58 void EncodeUsageResource::SetTargetFrameRate(
59     absl::optional<double> target_frame_rate) {
60   RTC_DCHECK_RUN_ON(encoder_queue());
61   if (target_frame_rate == target_frame_rate_)
62     return;
63   target_frame_rate_ = target_frame_rate;
64   if (is_started_)
65     overuse_detector_->OnTargetFramerateUpdated(TargetFrameRateAsInt());
66 }
67 
OnEncodeStarted(const VideoFrame & cropped_frame,int64_t time_when_first_seen_us)68 void EncodeUsageResource::OnEncodeStarted(const VideoFrame& cropped_frame,
69                                           int64_t time_when_first_seen_us) {
70   RTC_DCHECK_RUN_ON(encoder_queue());
71   // TODO(hbos): Rename FrameCaptured() to something more appropriate (e.g.
72   // "OnEncodeStarted"?) or revise usage.
73   overuse_detector_->FrameCaptured(cropped_frame, time_when_first_seen_us);
74 }
75 
OnEncodeCompleted(uint32_t timestamp,int64_t time_sent_in_us,int64_t capture_time_us,absl::optional<int> encode_duration_us)76 void EncodeUsageResource::OnEncodeCompleted(
77     uint32_t timestamp,
78     int64_t time_sent_in_us,
79     int64_t capture_time_us,
80     absl::optional<int> encode_duration_us) {
81   RTC_DCHECK_RUN_ON(encoder_queue());
82   // TODO(hbos): Rename FrameSent() to something more appropriate (e.g.
83   // "OnEncodeCompleted"?).
84   overuse_detector_->FrameSent(timestamp, time_sent_in_us, capture_time_us,
85                                encode_duration_us);
86 }
87 
AdaptUp()88 void EncodeUsageResource::AdaptUp() {
89   RTC_DCHECK_RUN_ON(encoder_queue());
90   OnResourceUsageStateMeasured(ResourceUsageState::kUnderuse);
91 }
92 
AdaptDown()93 void EncodeUsageResource::AdaptDown() {
94   RTC_DCHECK_RUN_ON(encoder_queue());
95   OnResourceUsageStateMeasured(ResourceUsageState::kOveruse);
96 }
97 
TargetFrameRateAsInt()98 int EncodeUsageResource::TargetFrameRateAsInt() {
99   RTC_DCHECK_RUN_ON(encoder_queue());
100   return target_frame_rate_.has_value()
101              ? static_cast<int>(target_frame_rate_.value())
102              : std::numeric_limits<int>::max();
103 }
104 
105 }  // namespace webrtc
106