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 | |
| 14 | #include "absl/types/optional.h" |
| 15 | #include "api/rtp_parameters.h" |
| 16 | #include "api/video/video_frame.h" |
| 17 | #include "api/video/video_sink_interface.h" |
| 18 | #include "api/video/video_source_interface.h" |
| 19 | #include "call/adaptation/resource_adaptation_module_interface.h" |
| 20 | #include "rtc_base/critical_section.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | // Responsible for configuring source/sink settings, i.e. performing |
| 25 | // rtc::VideoSourceInterface<VideoFrame>::AddOrUpdateSink(). It does this by |
| 26 | // storing settings internally which are converted to rtc::VideoSinkWants when |
| 27 | // PushSourceSinkSettings() is performed. |
| 28 | class VideoSourceSinkController { |
| 29 | public: |
| 30 | VideoSourceSinkController(rtc::VideoSinkInterface<VideoFrame>* sink, |
| 31 | rtc::VideoSourceInterface<VideoFrame>* source); |
| 32 | |
| 33 | // TODO(https://crbug.com/webrtc/11222): Remove dependency on |
| 34 | // DegradationPreference! How degradation preference affects |
| 35 | // VideoSourceRestrictions should not be a responsibility of the controller, |
| 36 | // but of the resource adaptation module. |
| 37 | void SetSource(rtc::VideoSourceInterface<VideoFrame>* source, |
| 38 | DegradationPreference degradation_preference); |
| 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 | // TODO(https://crbug.com/webrtc/11222): Outside of testing, this is only used |
| 59 | // by OveruseFrameDetectorResourceAdaptationModule::RefreshTargetFramerate(). |
| 60 | // When the DegradationPreference logic has moved outside of this class, there |
| 61 | // will be no public need for this method other than testing reasons and this |
| 62 | // can be renamed "ForTesting". |
| 63 | rtc::VideoSinkWants CurrentSettingsToSinkWants() const; |
| 64 | |
| 65 | private: |
| 66 | rtc::VideoSinkWants CurrentSettingsToSinkWantsInternal() const |
| 67 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 68 | |
| 69 | // TODO(hbos): If everything is handled on the same sequence (i.e. |
| 70 | // VideoStreamEncoder's encoder queue) then |crit_| can be replaced by |
| 71 | // sequence checker. Investigate if we want to do this. |
| 72 | mutable rtc::CriticalSection crit_; |
| 73 | rtc::VideoSinkInterface<VideoFrame>* const sink_; |
| 74 | rtc::VideoSourceInterface<VideoFrame>* source_ RTC_GUARDED_BY(&crit_); |
| 75 | DegradationPreference degradation_preference_ RTC_GUARDED_BY(&crit_); |
| 76 | // Pixel and frame rate restrictions. |
| 77 | VideoSourceRestrictions restrictions_ RTC_GUARDED_BY(&crit_); |
| 78 | // Ensures that even if we are not restricted, the sink is never configured |
| 79 | // above this limit. Example: We are not CPU limited (no |restrictions_|) but |
| 80 | // our encoder is capped at 30 fps (= |frame_rate_upper_limit_|). |
| 81 | absl::optional<size_t> pixels_per_frame_upper_limit_ RTC_GUARDED_BY(&crit_); |
| 82 | absl::optional<double> frame_rate_upper_limit_ RTC_GUARDED_BY(&crit_); |
| 83 | bool rotation_applied_ RTC_GUARDED_BY(&crit_) = false; |
| 84 | int resolution_alignment_ RTC_GUARDED_BY(&crit_) = 1; |
| 85 | }; |
| 86 | |
| 87 | } // namespace webrtc |
| 88 | |
| 89 | #endif // VIDEO_VIDEO_SOURCE_SINK_CONTROLLER_H_ |