xref: /aosp_15_r20/external/webrtc/video/video_source_sink_controller.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_VIDEO_SOURCE_SINK_CONTROLLER_H_
12 #define VIDEO_VIDEO_SOURCE_SINK_CONTROLLER_H_
13 
14 #include <string>
15 #include <vector>
16 
17 #include "absl/types/optional.h"
18 #include "api/sequence_checker.h"
19 #include "api/video/video_frame.h"
20 #include "api/video/video_sink_interface.h"
21 #include "api/video/video_source_interface.h"
22 #include "call/adaptation/video_source_restrictions.h"
23 #include "rtc_base/system/no_unique_address.h"
24 
25 namespace webrtc {
26 
27 // Responsible for configuring source/sink settings, i.e. performing
28 // rtc::VideoSourceInterface<VideoFrame>::AddOrUpdateSink(). It does this by
29 // storing settings internally which are converted to rtc::VideoSinkWants when
30 // PushSourceSinkSettings() is performed.
31 class VideoSourceSinkController {
32  public:
33   VideoSourceSinkController(rtc::VideoSinkInterface<VideoFrame>* sink,
34                             rtc::VideoSourceInterface<VideoFrame>* source);
35 
36   ~VideoSourceSinkController();
37 
38   void SetSource(rtc::VideoSourceInterface<VideoFrame>* source);
39   bool HasSource() const;
40 
41   // Requests a refresh frame from the current source, if set.
42   void RequestRefreshFrame();
43 
44   // Must be called in order for changes to settings to have an effect. This
45   // allows you to modify multiple properties in a single push to the sink.
46   void PushSourceSinkSettings();
47 
48   VideoSourceRestrictions restrictions() const;
49   absl::optional<size_t> pixels_per_frame_upper_limit() const;
50   absl::optional<double> frame_rate_upper_limit() const;
51   bool rotation_applied() const;
52   int resolution_alignment() const;
53   const std::vector<rtc::VideoSinkWants::FrameSize>& resolutions() const;
54   bool active() const;
55   absl::optional<rtc::VideoSinkWants::FrameSize> requested_resolution() const;
56 
57   // Updates the settings stored internally. In order for these settings to be
58   // applied to the sink, PushSourceSinkSettings() must subsequently be called.
59   void SetRestrictions(VideoSourceRestrictions restrictions);
60   void SetPixelsPerFrameUpperLimit(
61       absl::optional<size_t> pixels_per_frame_upper_limit);
62   void SetFrameRateUpperLimit(absl::optional<double> frame_rate_upper_limit);
63   void SetRotationApplied(bool rotation_applied);
64   void SetResolutionAlignment(int resolution_alignment);
65   void SetResolutions(std::vector<rtc::VideoSinkWants::FrameSize> resolutions);
66   void SetActive(bool active);
67   void SetRequestedResolution(
68       absl::optional<rtc::VideoSinkWants::FrameSize> requested_resolution);
69 
70  private:
71   rtc::VideoSinkWants CurrentSettingsToSinkWants() const
72       RTC_EXCLUSIVE_LOCKS_REQUIRED(sequence_checker_);
73 
74   // Used to ensure that this class is called on threads/sequences that it and
75   // downstream implementations were designed for.
76   // In practice, this represent's libjingle's worker thread.
77   RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_;
78 
79   rtc::VideoSinkInterface<VideoFrame>* const sink_;
80   rtc::VideoSourceInterface<VideoFrame>* source_
81       RTC_GUARDED_BY(&sequence_checker_);
82   // Pixel and frame rate restrictions.
83   VideoSourceRestrictions restrictions_ RTC_GUARDED_BY(&sequence_checker_);
84   // Ensures that even if we are not restricted, the sink is never configured
85   // above this limit. Example: We are not CPU limited (no `restrictions_`) but
86   // our encoder is capped at 30 fps (= `frame_rate_upper_limit_`).
87   absl::optional<size_t> pixels_per_frame_upper_limit_
88       RTC_GUARDED_BY(&sequence_checker_);
89   absl::optional<double> frame_rate_upper_limit_
90       RTC_GUARDED_BY(&sequence_checker_);
91   bool rotation_applied_ RTC_GUARDED_BY(&sequence_checker_) = false;
92   int resolution_alignment_ RTC_GUARDED_BY(&sequence_checker_) = 1;
93   std::vector<rtc::VideoSinkWants::FrameSize> resolutions_
94       RTC_GUARDED_BY(&sequence_checker_);
95   bool active_ RTC_GUARDED_BY(&sequence_checker_) = true;
96   absl::optional<rtc::VideoSinkWants::FrameSize> requested_resolution_
97       RTC_GUARDED_BY(&sequence_checker_);
98 };
99 
100 }  // namespace webrtc
101 
102 #endif  // VIDEO_VIDEO_SOURCE_SINK_CONTROLLER_H_
103