xref: /aosp_15_r20/external/webrtc/video/adaptation/encode_usage_resource.h (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 #ifndef VIDEO_ADAPTATION_ENCODE_USAGE_RESOURCE_H_
12 #define VIDEO_ADAPTATION_ENCODE_USAGE_RESOURCE_H_
13 
14 #include <memory>
15 #include <string>
16 
17 #include "absl/types/optional.h"
18 #include "api/scoped_refptr.h"
19 #include "api/video/video_adaptation_reason.h"
20 #include "video/adaptation/overuse_frame_detector.h"
21 #include "video/adaptation/video_stream_encoder_resource.h"
22 
23 namespace webrtc {
24 
25 // Handles interaction with the OveruseDetector.
26 // TODO(hbos): Add unittests specific to this class, it is currently only tested
27 // indirectly by usage in the ResourceAdaptationProcessor (which is only tested
28 // because of its usage in VideoStreamEncoder); all tests are currently in
29 // video_stream_encoder_unittest.cc.
30 class EncodeUsageResource : public VideoStreamEncoderResource,
31                             public OveruseFrameDetectorObserverInterface {
32  public:
33   static rtc::scoped_refptr<EncodeUsageResource> Create(
34       std::unique_ptr<OveruseFrameDetector> overuse_detector);
35 
36   explicit EncodeUsageResource(
37       std::unique_ptr<OveruseFrameDetector> overuse_detector);
38   ~EncodeUsageResource() override;
39 
40   bool is_started() const;
41 
42   void StartCheckForOveruse(CpuOveruseOptions options);
43   void StopCheckForOveruse();
44 
45   void SetTargetFrameRate(absl::optional<double> target_frame_rate);
46   void OnEncodeStarted(const VideoFrame& cropped_frame,
47                        int64_t time_when_first_seen_us);
48   void OnEncodeCompleted(uint32_t timestamp,
49                          int64_t time_sent_in_us,
50                          int64_t capture_time_us,
51                          absl::optional<int> encode_duration_us);
52 
53   // OveruseFrameDetectorObserverInterface implementation.
54   void AdaptUp() override;
55   void AdaptDown() override;
56 
57  private:
58   int TargetFrameRateAsInt();
59 
60   const std::unique_ptr<OveruseFrameDetector> overuse_detector_
61       RTC_GUARDED_BY(encoder_queue());
62   bool is_started_ RTC_GUARDED_BY(encoder_queue());
63   absl::optional<double> target_frame_rate_ RTC_GUARDED_BY(encoder_queue());
64 };
65 
66 }  // namespace webrtc
67 
68 #endif  // VIDEO_ADAPTATION_ENCODE_USAGE_RESOURCE_H_
69