Henrik Boström | ce0ea49 | 2020-01-13 11:27:18 +0100 | [diff] [blame] | 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 | |
Evan Shrubsole | 236e0ed | 2020-05-20 12:24:57 +0200 | [diff] [blame] | 14 | #include <string> |
Henrik Boström | 1124ed1 | 2021-02-25 10:30:39 +0100 | [diff] [blame] | 15 | #include <vector> |
Evan Shrubsole | 236e0ed | 2020-05-20 12:24:57 +0200 | [diff] [blame] | 16 | |
Henrik Boström | ce0ea49 | 2020-01-13 11:27:18 +0100 | [diff] [blame] | 17 | #include "absl/types/optional.h" |
Artem Titov | d15a575 | 2021-02-10 14:31:24 +0100 | [diff] [blame] | 18 | #include "api/sequence_checker.h" |
Henrik Boström | ce0ea49 | 2020-01-13 11:27:18 +0100 | [diff] [blame] | 19 | #include "api/video/video_frame.h" |
| 20 | #include "api/video/video_sink_interface.h" |
| 21 | #include "api/video/video_source_interface.h" |
Henrik Boström | 6205762 | 2020-03-10 19:08:05 +0100 | [diff] [blame] | 22 | #include "call/adaptation/video_source_restrictions.h" |
Mirko Bonadei | 20e4c80 | 2020-11-23 11:07:42 +0100 | [diff] [blame] | 23 | #include "rtc_base/system/no_unique_address.h" |
Henrik Boström | ce0ea49 | 2020-01-13 11:27:18 +0100 | [diff] [blame] | 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 | |
Tomas Gunnarsson | 612445e | 2020-09-21 14:31:23 +0200 | [diff] [blame] | 36 | ~VideoSourceSinkController(); |
| 37 | |
Henrik Boström | 8234b92 | 2020-01-13 17:26:50 +0100 | [diff] [blame] | 38 | void SetSource(rtc::VideoSourceInterface<VideoFrame>* source); |
Tomas Gunnarsson | 612445e | 2020-09-21 14:31:23 +0200 | [diff] [blame] | 39 | bool HasSource() const; |
| 40 | |
Markus Handell | 2e0f4f0 | 2021-12-21 19:14:58 +0100 | [diff] [blame] | 41 | // Requests a refresh frame from the current source, if set. |
| 42 | void RequestRefreshFrame(); |
| 43 | |
Henrik Boström | ce0ea49 | 2020-01-13 11:27:18 +0100 | [diff] [blame] | 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; |
Henrik Boström | 1124ed1 | 2021-02-25 10:30:39 +0100 | [diff] [blame] | 53 | const std::vector<rtc::VideoSinkWants::FrameSize>& resolutions() const; |
Jonas Oreland | 0deda15 | 2022-09-23 12:08:57 +0200 | [diff] [blame] | 54 | bool active() const; |
| 55 | absl::optional<rtc::VideoSinkWants::FrameSize> requested_resolution() const; |
Henrik Boström | ce0ea49 | 2020-01-13 11:27:18 +0100 | [diff] [blame] | 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); |
Henrik Boström | 1124ed1 | 2021-02-25 10:30:39 +0100 | [diff] [blame] | 65 | void SetResolutions(std::vector<rtc::VideoSinkWants::FrameSize> resolutions); |
Jonas Oreland | 0deda15 | 2022-09-23 12:08:57 +0200 | [diff] [blame] | 66 | void SetActive(bool active); |
| 67 | void SetRequestedResolution( |
| 68 | absl::optional<rtc::VideoSinkWants::FrameSize> requested_resolution); |
Henrik Boström | ce0ea49 | 2020-01-13 11:27:18 +0100 | [diff] [blame] | 69 | |
Henrik Boström | ce0ea49 | 2020-01-13 11:27:18 +0100 | [diff] [blame] | 70 | private: |
Henrik Boström | 8234b92 | 2020-01-13 17:26:50 +0100 | [diff] [blame] | 71 | rtc::VideoSinkWants CurrentSettingsToSinkWants() const |
Tomas Gunnarsson | 612445e | 2020-09-21 14:31:23 +0200 | [diff] [blame] | 72 | RTC_EXCLUSIVE_LOCKS_REQUIRED(sequence_checker_); |
Henrik Boström | ce0ea49 | 2020-01-13 11:27:18 +0100 | [diff] [blame] | 73 | |
Tomas Gunnarsson | 612445e | 2020-09-21 14:31:23 +0200 | [diff] [blame] | 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. |
Mirko Bonadei | 20e4c80 | 2020-11-23 11:07:42 +0100 | [diff] [blame] | 77 | RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_; |
Tomas Gunnarsson | 612445e | 2020-09-21 14:31:23 +0200 | [diff] [blame] | 78 | |
Henrik Boström | ce0ea49 | 2020-01-13 11:27:18 +0100 | [diff] [blame] | 79 | rtc::VideoSinkInterface<VideoFrame>* const sink_; |
Tomas Gunnarsson | 612445e | 2020-09-21 14:31:23 +0200 | [diff] [blame] | 80 | rtc::VideoSourceInterface<VideoFrame>* source_ |
| 81 | RTC_GUARDED_BY(&sequence_checker_); |
Henrik Boström | ce0ea49 | 2020-01-13 11:27:18 +0100 | [diff] [blame] | 82 | // Pixel and frame rate restrictions. |
Tomas Gunnarsson | 612445e | 2020-09-21 14:31:23 +0200 | [diff] [blame] | 83 | VideoSourceRestrictions restrictions_ RTC_GUARDED_BY(&sequence_checker_); |
Henrik Boström | ce0ea49 | 2020-01-13 11:27:18 +0100 | [diff] [blame] | 84 | // Ensures that even if we are not restricted, the sink is never configured |
Artem Titov | ab30d72 | 2021-07-27 16:22:11 +0200 | [diff] [blame] | 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_`). |
Tomas Gunnarsson | 612445e | 2020-09-21 14:31:23 +0200 | [diff] [blame] | 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; |
Henrik Boström | 1124ed1 | 2021-02-25 10:30:39 +0100 | [diff] [blame] | 93 | std::vector<rtc::VideoSinkWants::FrameSize> resolutions_ |
| 94 | RTC_GUARDED_BY(&sequence_checker_); |
Jonas Oreland | 0deda15 | 2022-09-23 12:08:57 +0200 | [diff] [blame] | 95 | bool active_ RTC_GUARDED_BY(&sequence_checker_) = true; |
| 96 | absl::optional<rtc::VideoSinkWants::FrameSize> requested_resolution_ |
| 97 | RTC_GUARDED_BY(&sequence_checker_); |
Henrik Boström | ce0ea49 | 2020-01-13 11:27:18 +0100 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | } // namespace webrtc |
| 101 | |
| 102 | #endif // VIDEO_VIDEO_SOURCE_SINK_CONTROLLER_H_ |