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