Niels Möller | 0327c2d | 2018-05-21 12:09:31 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 API_VIDEO_VIDEO_SOURCE_INTERFACE_H_ |
| 12 | #define API_VIDEO_VIDEO_SOURCE_INTERFACE_H_ |
| 13 | |
| 14 | #include <limits> |
Henrik Boström | 1124ed1 | 2021-02-25 09:30:39 | [diff] [blame] | 15 | #include <vector> |
Niels Möller | 0327c2d | 2018-05-21 12:09:31 | [diff] [blame] | 16 | |
Danil Chapovalov | 0bc58cf | 2018-06-21 11:32:56 | [diff] [blame] | 17 | #include "absl/types/optional.h" |
Niels Möller | 0327c2d | 2018-05-21 12:09:31 | [diff] [blame] | 18 | #include "api/video/video_sink_interface.h" |
Mirko Bonadei | ac19414 | 2018-10-22 15:08:37 | [diff] [blame] | 19 | #include "rtc_base/system/rtc_export.h" |
Niels Möller | 0327c2d | 2018-05-21 12:09:31 | [diff] [blame] | 20 | |
| 21 | namespace rtc { |
| 22 | |
| 23 | // VideoSinkWants is used for notifying the source of properties a video frame |
| 24 | // should have when it is delivered to a certain sink. |
Mirko Bonadei | ac19414 | 2018-10-22 15:08:37 | [diff] [blame] | 25 | struct RTC_EXPORT VideoSinkWants { |
Henrik Boström | 1124ed1 | 2021-02-25 09:30:39 | [diff] [blame] | 26 | struct FrameSize { |
| 27 | FrameSize(int width, int height) : width(width), height(height) {} |
| 28 | FrameSize(const FrameSize&) = default; |
| 29 | ~FrameSize() = default; |
| 30 | |
| 31 | int width; |
| 32 | int height; |
| 33 | }; |
| 34 | |
Niels Möller | 0327c2d | 2018-05-21 12:09:31 | [diff] [blame] | 35 | VideoSinkWants(); |
| 36 | VideoSinkWants(const VideoSinkWants&); |
| 37 | ~VideoSinkWants(); |
Jonas Oreland | 1d3452f | 2023-05-12 14:36:41 | [diff] [blame] | 38 | |
Niels Möller | 0327c2d | 2018-05-21 12:09:31 | [diff] [blame] | 39 | // Tells the source whether the sink wants frames with rotation applied. |
| 40 | // By default, any rotation must be applied by the sink. |
| 41 | bool rotation_applied = false; |
| 42 | |
| 43 | // Tells the source that the sink only wants black frames. |
| 44 | bool black_frames = false; |
| 45 | |
| 46 | // Tells the source the maximum number of pixels the sink wants. |
| 47 | int max_pixel_count = std::numeric_limits<int>::max(); |
| 48 | // Tells the source the desired number of pixels the sinks wants. This will |
| 49 | // typically be used when stepping the resolution up again when conditions |
| 50 | // have improved after an earlier downgrade. The source should select the |
| 51 | // closest resolution to this pixel count, but if max_pixel_count is set, it |
| 52 | // still sets the absolute upper bound. |
Danil Chapovalov | 0bc58cf | 2018-06-21 11:32:56 | [diff] [blame] | 53 | absl::optional<int> target_pixel_count; |
Niels Möller | 0327c2d | 2018-05-21 12:09:31 | [diff] [blame] | 54 | // Tells the source the maximum framerate the sink wants. |
| 55 | int max_framerate_fps = std::numeric_limits<int>::max(); |
Rasmus Brandt | 5cad55b | 2019-12-19 08:47:11 | [diff] [blame] | 56 | |
| 57 | // Tells the source that the sink wants width and height of the video frames |
Artem Titov | 0e61fdd | 2021-07-25 19:50:14 | [diff] [blame] | 58 | // to be divisible by `resolution_alignment`. |
Rasmus Brandt | 5cad55b | 2019-12-19 08:47:11 | [diff] [blame] | 59 | // For example: With I420, this value would be a multiple of 2. |
| 60 | // Note that this field is unrelated to any horizontal or vertical stride |
| 61 | // requirements the encoder has on the incoming video frame buffers. |
| 62 | int resolution_alignment = 1; |
Henrik Boström | 1124ed1 | 2021-02-25 09:30:39 | [diff] [blame] | 63 | |
| 64 | // The resolutions that sink is configured to consume. If the sink is an |
| 65 | // encoder this is what the encoder is configured to encode. In singlecast we |
| 66 | // only encode one resolution, but in simulcast and SVC this can mean multiple |
| 67 | // resolutions per frame. |
| 68 | // |
| 69 | // The sink is always configured to consume a subset of the |
| 70 | // webrtc::VideoFrame's resolution. In the case of encoding, we usually encode |
| 71 | // at webrtc::VideoFrame's resolution but this may not always be the case due |
| 72 | // to scaleResolutionDownBy or turning off simulcast or SVC layers. |
| 73 | // |
| 74 | // For example, we may capture at 720p and due to adaptation (e.g. applying |
Artem Titov | 0e61fdd | 2021-07-25 19:50:14 | [diff] [blame] | 75 | // `max_pixel_count` constraints) create webrtc::VideoFrames of size 480p, but |
Henrik Boström | 1124ed1 | 2021-02-25 09:30:39 | [diff] [blame] | 76 | // if we do scaleResolutionDownBy:2 then the only resolution we end up |
| 77 | // encoding is 240p. In this case we still need to provide webrtc::VideoFrames |
| 78 | // of size 480p but we can optimize internal buffers for 240p, avoiding |
| 79 | // downsampling to 480p if possible. |
| 80 | // |
Artem Titov | 0e61fdd | 2021-07-25 19:50:14 | [diff] [blame] | 81 | // Note that the `resolutions` can change while frames are in flight and |
Henrik Boström | 1124ed1 | 2021-02-25 09:30:39 | [diff] [blame] | 82 | // should only be used as a hint when constructing the webrtc::VideoFrame. |
| 83 | std::vector<FrameSize> resolutions; |
Jonas Oreland | 0deda15 | 2022-09-23 10:08:57 | [diff] [blame] | 84 | |
| 85 | // This is the resolution requested by the user using RtpEncodingParameters. |
| 86 | absl::optional<FrameSize> requested_resolution; |
| 87 | |
Jonas Oreland | 1d3452f | 2023-05-12 14:36:41 | [diff] [blame] | 88 | // `is_active` : Is this VideoSinkWants from an encoder that is encoding any |
| 89 | // layer. IF YES, it will affect how the VideoAdapter will choose to |
| 90 | // prioritize the OnOutputFormatRequest vs. requested_resolution. IF NO, |
| 91 | // VideoAdapter consider this VideoSinkWants as a passive listener (e.g a |
| 92 | // VideoRenderer or a VideoEncoder that is not currently actively encoding). |
| 93 | bool is_active = false; |
Jonas Oreland | 0deda15 | 2022-09-23 10:08:57 | [diff] [blame] | 94 | |
| 95 | // This sub-struct contains information computed by VideoBroadcaster |
| 96 | // that aggregates several VideoSinkWants (and sends them to |
| 97 | // AdaptedVideoTrackSource). |
| 98 | struct Aggregates { |
| 99 | // `active_without_requested_resolution` is set by VideoBroadcaster |
| 100 | // when aggregating sink wants if there exists any sink (encoder) that is |
| 101 | // active but has not set the `requested_resolution`, i.e is relying on |
| 102 | // OnOutputFormatRequest to handle encode resolution. |
| 103 | bool any_active_without_requested_resolution = false; |
| 104 | }; |
| 105 | absl::optional<Aggregates> aggregates; |
Niels Möller | 0327c2d | 2018-05-21 12:09:31 | [diff] [blame] | 106 | }; |
| 107 | |
Henrik Boström | 1124ed1 | 2021-02-25 09:30:39 | [diff] [blame] | 108 | inline bool operator==(const VideoSinkWants::FrameSize& a, |
| 109 | const VideoSinkWants::FrameSize& b) { |
| 110 | return a.width == b.width && a.height == b.height; |
| 111 | } |
| 112 | |
Jonas Oreland | 0deda15 | 2022-09-23 10:08:57 | [diff] [blame] | 113 | inline bool operator!=(const VideoSinkWants::FrameSize& a, |
| 114 | const VideoSinkWants::FrameSize& b) { |
| 115 | return !(a == b); |
| 116 | } |
| 117 | |
Niels Möller | 0327c2d | 2018-05-21 12:09:31 | [diff] [blame] | 118 | template <typename VideoFrameT> |
| 119 | class VideoSourceInterface { |
| 120 | public: |
Niels Möller | 1c931c4 | 2018-12-18 15:08:11 | [diff] [blame] | 121 | virtual ~VideoSourceInterface() = default; |
| 122 | |
Niels Möller | 0327c2d | 2018-05-21 12:09:31 | [diff] [blame] | 123 | virtual void AddOrUpdateSink(VideoSinkInterface<VideoFrameT>* sink, |
| 124 | const VideoSinkWants& wants) = 0; |
| 125 | // RemoveSink must guarantee that at the time the method returns, |
| 126 | // there is no current and no future calls to VideoSinkInterface::OnFrame. |
| 127 | virtual void RemoveSink(VideoSinkInterface<VideoFrameT>* sink) = 0; |
Markus Handell | 2e0f4f0 | 2021-12-21 18:14:58 | [diff] [blame] | 128 | |
| 129 | // Request underlying source to capture a new frame. |
| 130 | // TODO(crbug/1255737): make pure virtual once downstream projects adapt. |
| 131 | virtual void RequestRefreshFrame() {} |
Niels Möller | 0327c2d | 2018-05-21 12:09:31 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | } // namespace rtc |
| 135 | #endif // API_VIDEO_VIDEO_SOURCE_INTERFACE_H_ |