jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-08 04:46:45 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-08 04:46:45 | [diff] [blame] | 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. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 | [diff] [blame] | 9 | */ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 10 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef MEDIA_BASE_VIDEO_ADAPTER_H_ |
| 12 | #define MEDIA_BASE_VIDEO_ADAPTER_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 15 | |
Åsa Persson | 2e4419e | 2018-09-06 13:02:55 | [diff] [blame] | 16 | #include <utility> |
| 17 | |
Danil Chapovalov | 00c71836 | 2018-06-15 13:58:38 | [diff] [blame] | 18 | #include "absl/types/optional.h" |
Rasmus Brandt | 287e464 | 2019-11-15 15:56:01 | [diff] [blame] | 19 | #include "api/video/video_source_interface.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 20 | #include "media/base/video_common.h" |
| 21 | #include "rtc_base/constructor_magic.h" |
Markus Handell | f7303e6 | 2020-07-08 23:34:42 | [diff] [blame] | 22 | #include "rtc_base/synchronization/mutex.h" |
Ilya Nikolaevskiy | ec622d0 | 2020-09-07 11:42:13 | [diff] [blame] | 23 | #include "rtc_base/system/rtc_export.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 24 | #include "rtc_base/thread_annotations.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 25 | |
| 26 | namespace cricket { |
| 27 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 28 | // VideoAdapter adapts an input video frame to an output frame based on the |
| 29 | // specified input and output formats. The adaptation includes dropping frames |
Per | 766ad3b9 | 2016-04-05 13:23:49 | [diff] [blame] | 30 | // to reduce frame rate and scaling frames. |
| 31 | // VideoAdapter is thread safe. |
Ilya Nikolaevskiy | ec622d0 | 2020-09-07 11:42:13 | [diff] [blame] | 32 | class RTC_EXPORT VideoAdapter { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 33 | public: |
| 34 | VideoAdapter(); |
Rasmus Brandt | 5cad55b | 2019-12-19 08:47:11 | [diff] [blame] | 35 | // The source requests output frames whose width and height are divisible |
Artem Titov | 37f664f | 2021-07-26 11:24:57 | [diff] [blame] | 36 | // by `source_resolution_alignment`. |
Rasmus Brandt | 5cad55b | 2019-12-19 08:47:11 | [diff] [blame] | 37 | explicit VideoAdapter(int source_resolution_alignment); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 38 | virtual ~VideoAdapter(); |
| 39 | |
nisse | 47ac462 | 2016-05-25 15:47:01 | [diff] [blame] | 40 | // Return the adapted resolution and cropping parameters given the |
| 41 | // input resolution. The input frame should first be cropped, then |
| 42 | // scaled to the final output resolution. Returns true if the frame |
| 43 | // should be adapted, and false if it should be dropped. |
| 44 | bool AdaptFrameResolution(int in_width, |
magjed | 709f73c | 2016-05-13 17:26:00 | [diff] [blame] | 45 | int in_height, |
magjed | 604abe0 | 2016-05-19 13:05:40 | [diff] [blame] | 46 | int64_t in_timestamp_ns, |
magjed | 709f73c | 2016-05-13 17:26:00 | [diff] [blame] | 47 | int* cropped_width, |
| 48 | int* cropped_height, |
| 49 | int* out_width, |
Markus Handell | f7303e6 | 2020-07-08 23:34:42 | [diff] [blame] | 50 | int* out_height) RTC_LOCKS_EXCLUDED(mutex_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 51 | |
Åsa Persson | 2e4419e | 2018-09-06 13:02:55 | [diff] [blame] | 52 | // DEPRECATED. Please use OnOutputFormatRequest below. |
| 53 | // TODO(asapersson): Remove this once it is no longer used. |
Per | 766ad3b9 | 2016-04-05 13:23:49 | [diff] [blame] | 54 | // Requests the output frame size and frame interval from |
Artem Titov | 37f664f | 2021-07-26 11:24:57 | [diff] [blame] | 55 | // `AdaptFrameResolution` to not be larger than `format`. Also, the input |
magjed | 709f73c | 2016-05-13 17:26:00 | [diff] [blame] | 56 | // frame size will be cropped to match the requested aspect ratio. The |
| 57 | // requested aspect ratio is orientation agnostic and will be adjusted to |
| 58 | // maintain the input orientation, so it doesn't matter if e.g. 1280x720 or |
| 59 | // 720x1280 is requested. |
Åsa Persson | 2e4419e | 2018-09-06 13:02:55 | [diff] [blame] | 60 | // Note: Should be called from the source only. |
Markus Handell | c5324fb | 2020-05-14 12:59:55 | [diff] [blame] | 61 | void OnOutputFormatRequest(const absl::optional<VideoFormat>& format) |
Markus Handell | f7303e6 | 2020-07-08 23:34:42 | [diff] [blame] | 62 | RTC_LOCKS_EXCLUDED(mutex_); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 | [diff] [blame] | 63 | |
Artem Titov | 37f664f | 2021-07-26 11:24:57 | [diff] [blame] | 64 | // Requests output frame size and frame interval from `AdaptFrameResolution`. |
| 65 | // `target_aspect_ratio`: The input frame size will be cropped to match the |
Åsa Persson | 2e4419e | 2018-09-06 13:02:55 | [diff] [blame] | 66 | // requested aspect ratio. The aspect ratio is orientation agnostic and will |
| 67 | // be adjusted to maintain the input orientation (i.e. it doesn't matter if |
| 68 | // e.g. <1280,720> or <720,1280> is requested). |
Artem Titov | 37f664f | 2021-07-26 11:24:57 | [diff] [blame] | 69 | // `max_pixel_count`: The maximum output frame size. |
| 70 | // `max_fps`: The maximum output framerate. |
Åsa Persson | 2e4419e | 2018-09-06 13:02:55 | [diff] [blame] | 71 | // Note: Should be called from the source only. |
| 72 | void OnOutputFormatRequest( |
| 73 | const absl::optional<std::pair<int, int>>& target_aspect_ratio, |
| 74 | const absl::optional<int>& max_pixel_count, |
Markus Handell | f7303e6 | 2020-07-08 23:34:42 | [diff] [blame] | 75 | const absl::optional<int>& max_fps) RTC_LOCKS_EXCLUDED(mutex_); |
Åsa Persson | 2e4419e | 2018-09-06 13:02:55 | [diff] [blame] | 76 | |
Magnus Jedvert | 06aa209 | 2018-10-26 12:00:18 | [diff] [blame] | 77 | // Same as above, but allows setting two different target aspect ratios |
| 78 | // depending on incoming frame orientation. This gives more fine-grained |
| 79 | // control and can e.g. be used to force landscape video to be cropped to |
| 80 | // portrait video. |
| 81 | void OnOutputFormatRequest( |
| 82 | const absl::optional<std::pair<int, int>>& target_landscape_aspect_ratio, |
| 83 | const absl::optional<int>& max_landscape_pixel_count, |
| 84 | const absl::optional<std::pair<int, int>>& target_portrait_aspect_ratio, |
| 85 | const absl::optional<int>& max_portrait_pixel_count, |
Markus Handell | f7303e6 | 2020-07-08 23:34:42 | [diff] [blame] | 86 | const absl::optional<int>& max_fps) RTC_LOCKS_EXCLUDED(mutex_); |
Magnus Jedvert | 06aa209 | 2018-10-26 12:00:18 | [diff] [blame] | 87 | |
Artem Titov | 37f664f | 2021-07-26 11:24:57 | [diff] [blame] | 88 | // Requests the output frame size from `AdaptFrameResolution` to have as close |
Rasmus Brandt | 287e464 | 2019-11-15 15:56:01 | [diff] [blame] | 89 | // as possible to |sink_wants.target_pixel_count| pixels (if set) |
| 90 | // but no more than |sink_wants.max_pixel_count|. |
| 91 | // |sink_wants.max_framerate_fps| is essentially analogous to |
| 92 | // |sink_wants.max_pixel_count|, but for framerate rather than resolution. |
| 93 | // Set |sink_wants.max_pixel_count| and/or |sink_wants.max_framerate_fps| to |
sprang | c5d62e2 | 2017-04-03 06:53:04 | [diff] [blame] | 94 | // std::numeric_limit<int>::max() if no upper limit is desired. |
Rasmus Brandt | 5cad55b | 2019-12-19 08:47:11 | [diff] [blame] | 95 | // The sink resolution alignment requirement is given by |
| 96 | // |sink_wants.resolution_alignment|. |
Åsa Persson | 2e4419e | 2018-09-06 13:02:55 | [diff] [blame] | 97 | // Note: Should be called from the sink only. |
Markus Handell | c5324fb | 2020-05-14 12:59:55 | [diff] [blame] | 98 | void OnSinkWants(const rtc::VideoSinkWants& sink_wants) |
Markus Handell | f7303e6 | 2020-07-08 23:34:42 | [diff] [blame] | 99 | RTC_LOCKS_EXCLUDED(mutex_); |
buildbot@webrtc.org | 71dffb7 | 2014-06-24 07:24:49 | [diff] [blame] | 100 | |
Ilya Nikolaevskiy | c2cc4d3 | 2020-08-26 14:50:44 | [diff] [blame] | 101 | // Returns maximum image area, which shouldn't impose any adaptations. |
| 102 | // Can return |numeric_limits<int>::max()| if no limit is set. |
| 103 | int GetTargetPixels() const; |
| 104 | |
| 105 | // Returns current frame-rate limit. |
| 106 | // Can return |numeric_limits<float>::infinity()| if no limit is set. |
| 107 | float GetMaxFramerate() const; |
| 108 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 109 | private: |
magjed | 604abe0 | 2016-05-19 13:05:40 | [diff] [blame] | 110 | // Determine if frame should be dropped based on input fps and requested fps. |
Markus Handell | f7303e6 | 2020-07-08 23:34:42 | [diff] [blame] | 111 | bool KeepFrame(int64_t in_timestamp_ns) RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
magjed | 604abe0 | 2016-05-19 13:05:40 | [diff] [blame] | 112 | |
Markus Handell | f7303e6 | 2020-07-08 23:34:42 | [diff] [blame] | 113 | int frames_in_ RTC_GUARDED_BY(mutex_); // Number of input frames. |
| 114 | int frames_out_ RTC_GUARDED_BY(mutex_); // Number of output frames. |
| 115 | int frames_scaled_ RTC_GUARDED_BY(mutex_); // Number of frames scaled. |
Markus Handell | 3fa23ee | 2020-05-18 13:09:46 | [diff] [blame] | 116 | int adaption_changes_ |
Markus Handell | f7303e6 | 2020-07-08 23:34:42 | [diff] [blame] | 117 | RTC_GUARDED_BY(mutex_); // Number of changes in scale factor. |
| 118 | int previous_width_ RTC_GUARDED_BY(mutex_); // Previous adapter output width. |
Markus Handell | 3fa23ee | 2020-05-18 13:09:46 | [diff] [blame] | 119 | int previous_height_ |
Markus Handell | f7303e6 | 2020-07-08 23:34:42 | [diff] [blame] | 120 | RTC_GUARDED_BY(mutex_); // Previous adapter output height. |
Åsa Persson | 3f7e0ed | 2019-10-18 13:03:13 | [diff] [blame] | 121 | const bool variable_start_scale_factor_; |
Rasmus Brandt | 5cad55b | 2019-12-19 08:47:11 | [diff] [blame] | 122 | |
| 123 | // The fixed source resolution alignment requirement. |
| 124 | const int source_resolution_alignment_; |
| 125 | // The currently applied resolution alignment, as given by the requirements: |
Artem Titov | 37f664f | 2021-07-26 11:24:57 | [diff] [blame] | 126 | // - the fixed `source_resolution_alignment_`; and |
Rasmus Brandt | 5cad55b | 2019-12-19 08:47:11 | [diff] [blame] | 127 | // - the latest |sink_wants.resolution_alignment|. |
Markus Handell | f7303e6 | 2020-07-08 23:34:42 | [diff] [blame] | 128 | int resolution_alignment_ RTC_GUARDED_BY(mutex_); |
Rasmus Brandt | 5cad55b | 2019-12-19 08:47:11 | [diff] [blame] | 129 | |
magjed | 604abe0 | 2016-05-19 13:05:40 | [diff] [blame] | 130 | // The target timestamp for the next frame based on requested format. |
Markus Handell | f7303e6 | 2020-07-08 23:34:42 | [diff] [blame] | 131 | absl::optional<int64_t> next_frame_timestamp_ns_ RTC_GUARDED_BY(mutex_); |
Per | 766ad3b9 | 2016-04-05 13:23:49 | [diff] [blame] | 132 | |
Åsa Persson | 2e4419e | 2018-09-06 13:02:55 | [diff] [blame] | 133 | // Max number of pixels/fps requested via calls to OnOutputFormatRequest, |
| 134 | // OnResolutionFramerateRequest respectively. |
Per | 766ad3b9 | 2016-04-05 13:23:49 | [diff] [blame] | 135 | // The adapted output format is the minimum of these. |
Magnus Jedvert | 06aa209 | 2018-10-26 12:00:18 | [diff] [blame] | 136 | absl::optional<std::pair<int, int>> target_landscape_aspect_ratio_ |
Markus Handell | f7303e6 | 2020-07-08 23:34:42 | [diff] [blame] | 137 | RTC_GUARDED_BY(mutex_); |
| 138 | absl::optional<int> max_landscape_pixel_count_ RTC_GUARDED_BY(mutex_); |
Magnus Jedvert | 06aa209 | 2018-10-26 12:00:18 | [diff] [blame] | 139 | absl::optional<std::pair<int, int>> target_portrait_aspect_ratio_ |
Markus Handell | f7303e6 | 2020-07-08 23:34:42 | [diff] [blame] | 140 | RTC_GUARDED_BY(mutex_); |
| 141 | absl::optional<int> max_portrait_pixel_count_ RTC_GUARDED_BY(mutex_); |
| 142 | absl::optional<int> max_fps_ RTC_GUARDED_BY(mutex_); |
| 143 | int resolution_request_target_pixel_count_ RTC_GUARDED_BY(mutex_); |
| 144 | int resolution_request_max_pixel_count_ RTC_GUARDED_BY(mutex_); |
| 145 | int max_framerate_request_ RTC_GUARDED_BY(mutex_); |
Per | 766ad3b9 | 2016-04-05 13:23:49 | [diff] [blame] | 146 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 147 | // The critical section to protect the above variables. |
Ilya Nikolaevskiy | c2cc4d3 | 2020-08-26 14:50:44 | [diff] [blame] | 148 | mutable webrtc::Mutex mutex_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 149 | |
henrikg | 3c089d7 | 2015-09-16 12:37:44 | [diff] [blame] | 150 | RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 151 | }; |
| 152 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 153 | } // namespace cricket |
| 154 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 155 | #endif // MEDIA_BASE_VIDEO_ADAPTER_H_ |