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