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