blob: 76fefabf815e10b310618abd1669544240c8fdf8 [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:131/*
kjellander1afca732016-02-08 04:46:452 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved.
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:133 *
kjellander1afca732016-02-08 04:46:454 * 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.org5f93d0a2015-01-20 21:36:139 */
henrike@webrtc.org28e20752013-07-10 00:45:3610
Steve Anton10542f22019-01-11 17:11:0011#ifndef MEDIA_BASE_VIDEO_ADAPTER_H_
12#define MEDIA_BASE_VIDEO_ADAPTER_H_
henrike@webrtc.org28e20752013-07-10 00:45:3613
Yves Gerey3e707812018-11-28 15:47:4914#include <stdint.h>
Jonas Olssona4d87372019-07-05 17:08:3315
Åsa Persson2e4419e2018-09-06 13:02:5516#include <utility>
17
Danil Chapovalov00c718362018-06-15 13:58:3818#include "absl/types/optional.h"
Rasmus Brandt287e4642019-11-15 15:56:0119#include "api/video/video_source_interface.h"
Steve Anton10542f22019-01-11 17:11:0020#include "media/base/video_common.h"
21#include "rtc_base/constructor_magic.h"
Markus Handellf7303e62020-07-08 23:34:4222#include "rtc_base/synchronization/mutex.h"
Ilya Nikolaevskiyec622d02020-09-07 11:42:1323#include "rtc_base/system/rtc_export.h"
Yves Gerey3e707812018-11-28 15:47:4924#include "rtc_base/thread_annotations.h"
henrike@webrtc.org28e20752013-07-10 00:45:3625
26namespace cricket {
27
henrike@webrtc.org28e20752013-07-10 00:45:3628// VideoAdapter adapts an input video frame to an output frame based on the
29// specified input and output formats. The adaptation includes dropping frames
Per766ad3b92016-04-05 13:23:4930// to reduce frame rate and scaling frames.
31// VideoAdapter is thread safe.
Ilya Nikolaevskiyec622d02020-09-07 11:42:1332class RTC_EXPORT VideoAdapter {
henrike@webrtc.org28e20752013-07-10 00:45:3633 public:
34 VideoAdapter();
Rasmus Brandt5cad55b2019-12-19 08:47:1135 // The source requests output frames whose width and height are divisible
Artem Titov37f664f2021-07-26 11:24:5736 // by `source_resolution_alignment`.
Rasmus Brandt5cad55b2019-12-19 08:47:1137 explicit VideoAdapter(int source_resolution_alignment);
henrike@webrtc.org28e20752013-07-10 00:45:3638 virtual ~VideoAdapter();
39
nisse47ac4622016-05-25 15:47:0140 // 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,
magjed709f73c2016-05-13 17:26:0045 int in_height,
magjed604abe02016-05-19 13:05:4046 int64_t in_timestamp_ns,
magjed709f73c2016-05-13 17:26:0047 int* cropped_width,
48 int* cropped_height,
49 int* out_width,
Markus Handellf7303e62020-07-08 23:34:4250 int* out_height) RTC_LOCKS_EXCLUDED(mutex_);
henrike@webrtc.org28e20752013-07-10 00:45:3651
Åsa Persson2e4419e2018-09-06 13:02:5552 // DEPRECATED. Please use OnOutputFormatRequest below.
53 // TODO(asapersson): Remove this once it is no longer used.
Per766ad3b92016-04-05 13:23:4954 // Requests the output frame size and frame interval from
Artem Titov37f664f2021-07-26 11:24:5755 // `AdaptFrameResolution` to not be larger than `format`. Also, the input
magjed709f73c2016-05-13 17:26:0056 // 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 Persson2e4419e2018-09-06 13:02:5560 // Note: Should be called from the source only.
Markus Handellc5324fb2020-05-14 12:59:5561 void OnOutputFormatRequest(const absl::optional<VideoFormat>& format)
Markus Handellf7303e62020-07-08 23:34:4262 RTC_LOCKS_EXCLUDED(mutex_);
wu@webrtc.orgcadf9042013-08-30 21:24:1663
Artem Titov37f664f2021-07-26 11:24:5764 // Requests output frame size and frame interval from `AdaptFrameResolution`.
65 // `target_aspect_ratio`: The input frame size will be cropped to match the
Åsa Persson2e4419e2018-09-06 13:02:5566 // 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 Titov37f664f2021-07-26 11:24:5769 // `max_pixel_count`: The maximum output frame size.
70 // `max_fps`: The maximum output framerate.
Åsa Persson2e4419e2018-09-06 13:02:5571 // 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 Handellf7303e62020-07-08 23:34:4275 const absl::optional<int>& max_fps) RTC_LOCKS_EXCLUDED(mutex_);
Åsa Persson2e4419e2018-09-06 13:02:5576
Magnus Jedvert06aa2092018-10-26 12:00:1877 // 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 Handellf7303e62020-07-08 23:34:4286 const absl::optional<int>& max_fps) RTC_LOCKS_EXCLUDED(mutex_);
Magnus Jedvert06aa2092018-10-26 12:00:1887
Artem Titov37f664f2021-07-26 11:24:5788 // Requests the output frame size from `AdaptFrameResolution` to have as close
Rasmus Brandt287e4642019-11-15 15:56:0189 // 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
sprangc5d62e22017-04-03 06:53:0494 // std::numeric_limit<int>::max() if no upper limit is desired.
Rasmus Brandt5cad55b2019-12-19 08:47:1195 // The sink resolution alignment requirement is given by
96 // |sink_wants.resolution_alignment|.
Åsa Persson2e4419e2018-09-06 13:02:5597 // Note: Should be called from the sink only.
Markus Handellc5324fb2020-05-14 12:59:5598 void OnSinkWants(const rtc::VideoSinkWants& sink_wants)
Markus Handellf7303e62020-07-08 23:34:4299 RTC_LOCKS_EXCLUDED(mutex_);
buildbot@webrtc.org71dffb72014-06-24 07:24:49100
Ilya Nikolaevskiyc2cc4d32020-08-26 14:50:44101 // 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.org28e20752013-07-10 00:45:36109 private:
magjed604abe02016-05-19 13:05:40110 // Determine if frame should be dropped based on input fps and requested fps.
Markus Handellf7303e62020-07-08 23:34:42111 bool KeepFrame(int64_t in_timestamp_ns) RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
magjed604abe02016-05-19 13:05:40112
Markus Handellf7303e62020-07-08 23:34:42113 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 Handell3fa23ee2020-05-18 13:09:46116 int adaption_changes_
Markus Handellf7303e62020-07-08 23:34:42117 RTC_GUARDED_BY(mutex_); // Number of changes in scale factor.
118 int previous_width_ RTC_GUARDED_BY(mutex_); // Previous adapter output width.
Markus Handell3fa23ee2020-05-18 13:09:46119 int previous_height_
Markus Handellf7303e62020-07-08 23:34:42120 RTC_GUARDED_BY(mutex_); // Previous adapter output height.
Åsa Persson3f7e0ed2019-10-18 13:03:13121 const bool variable_start_scale_factor_;
Rasmus Brandt5cad55b2019-12-19 08:47:11122
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 Titov37f664f2021-07-26 11:24:57126 // - the fixed `source_resolution_alignment_`; and
Rasmus Brandt5cad55b2019-12-19 08:47:11127 // - the latest |sink_wants.resolution_alignment|.
Markus Handellf7303e62020-07-08 23:34:42128 int resolution_alignment_ RTC_GUARDED_BY(mutex_);
Rasmus Brandt5cad55b2019-12-19 08:47:11129
magjed604abe02016-05-19 13:05:40130 // The target timestamp for the next frame based on requested format.
Markus Handellf7303e62020-07-08 23:34:42131 absl::optional<int64_t> next_frame_timestamp_ns_ RTC_GUARDED_BY(mutex_);
Per766ad3b92016-04-05 13:23:49132
Åsa Persson2e4419e2018-09-06 13:02:55133 // Max number of pixels/fps requested via calls to OnOutputFormatRequest,
134 // OnResolutionFramerateRequest respectively.
Per766ad3b92016-04-05 13:23:49135 // The adapted output format is the minimum of these.
Magnus Jedvert06aa2092018-10-26 12:00:18136 absl::optional<std::pair<int, int>> target_landscape_aspect_ratio_
Markus Handellf7303e62020-07-08 23:34:42137 RTC_GUARDED_BY(mutex_);
138 absl::optional<int> max_landscape_pixel_count_ RTC_GUARDED_BY(mutex_);
Magnus Jedvert06aa2092018-10-26 12:00:18139 absl::optional<std::pair<int, int>> target_portrait_aspect_ratio_
Markus Handellf7303e62020-07-08 23:34:42140 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_);
Per766ad3b92016-04-05 13:23:49146
henrike@webrtc.org28e20752013-07-10 00:45:36147 // The critical section to protect the above variables.
Ilya Nikolaevskiyc2cc4d32020-08-26 14:50:44148 mutable webrtc::Mutex mutex_;
henrike@webrtc.org28e20752013-07-10 00:45:36149
henrikg3c089d72015-09-16 12:37:44150 RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter);
henrike@webrtc.org28e20752013-07-10 00:45:36151};
152
henrike@webrtc.org28e20752013-07-10 00:45:36153} // namespace cricket
154
Steve Anton10542f22019-01-11 17:11:00155#endif // MEDIA_BASE_VIDEO_ADAPTER_H_