2015-01-20 21:36:13 +00:00
|
|
|
/*
|
2016-02-07 20:46:45 -08:00
|
|
|
* Copyright (c) 2010 The WebRTC project authors. All Rights Reserved.
|
2015-01-20 21:36:13 +00:00
|
|
|
*
|
2016-02-07 20:46:45 -08:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2015-01-20 21:36:13 +00:00
|
|
|
*/
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
#ifndef MEDIA_BASE_VIDEO_ADAPTER_H_
|
|
|
|
|
#define MEDIA_BASE_VIDEO_ADAPTER_H_
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2018-11-28 16:47:49 +01:00
|
|
|
#include <stdint.h>
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2018-09-06 15:02:55 +02:00
|
|
|
#include <utility>
|
|
|
|
|
|
2018-06-15 15:58:38 +02:00
|
|
|
#include "absl/types/optional.h"
|
2019-11-15 16:56:01 +01:00
|
|
|
#include "api/video/video_source_interface.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "media/base/video_common.h"
|
|
|
|
|
#include "rtc_base/constructor_magic.h"
|
2020-07-09 01:34:42 +02:00
|
|
|
#include "rtc_base/synchronization/mutex.h"
|
2020-09-07 13:42:13 +02:00
|
|
|
#include "rtc_base/system/rtc_export.h"
|
2018-11-28 16:47:49 +01:00
|
|
|
#include "rtc_base/thread_annotations.h"
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
namespace cricket {
|
|
|
|
|
|
|
|
|
|
// VideoAdapter adapts an input video frame to an output frame based on the
|
|
|
|
|
// specified input and output formats. The adaptation includes dropping frames
|
2016-04-05 15:23:49 +02:00
|
|
|
// to reduce frame rate and scaling frames.
|
|
|
|
|
// VideoAdapter is thread safe.
|
2020-09-07 13:42:13 +02:00
|
|
|
class RTC_EXPORT VideoAdapter {
|
2013-07-10 00:45:36 +00:00
|
|
|
public:
|
|
|
|
|
VideoAdapter();
|
2019-12-19 09:47:11 +01:00
|
|
|
// The source requests output frames whose width and height are divisible
|
2021-07-26 13:24:57 +02:00
|
|
|
// by `source_resolution_alignment`.
|
2019-12-19 09:47:11 +01:00
|
|
|
explicit VideoAdapter(int source_resolution_alignment);
|
2013-07-10 00:45:36 +00:00
|
|
|
virtual ~VideoAdapter();
|
|
|
|
|
|
2016-05-25 08:47:01 -07:00
|
|
|
// Return the adapted resolution and cropping parameters given the
|
|
|
|
|
// input resolution. The input frame should first be cropped, then
|
|
|
|
|
// scaled to the final output resolution. Returns true if the frame
|
|
|
|
|
// should be adapted, and false if it should be dropped.
|
|
|
|
|
bool AdaptFrameResolution(int in_width,
|
2016-05-13 10:26:00 -07:00
|
|
|
int in_height,
|
2016-05-19 06:05:40 -07:00
|
|
|
int64_t in_timestamp_ns,
|
2016-05-13 10:26:00 -07:00
|
|
|
int* cropped_width,
|
|
|
|
|
int* cropped_height,
|
|
|
|
|
int* out_width,
|
2020-07-09 01:34:42 +02:00
|
|
|
int* out_height) RTC_LOCKS_EXCLUDED(mutex_);
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2018-09-06 15:02:55 +02:00
|
|
|
// DEPRECATED. Please use OnOutputFormatRequest below.
|
|
|
|
|
// TODO(asapersson): Remove this once it is no longer used.
|
2016-04-05 15:23:49 +02:00
|
|
|
// Requests the output frame size and frame interval from
|
2021-07-26 13:24:57 +02:00
|
|
|
// `AdaptFrameResolution` to not be larger than `format`. Also, the input
|
2016-05-13 10:26:00 -07:00
|
|
|
// frame size will be cropped to match the requested aspect ratio. The
|
|
|
|
|
// requested aspect ratio is orientation agnostic and will be adjusted to
|
|
|
|
|
// maintain the input orientation, so it doesn't matter if e.g. 1280x720 or
|
|
|
|
|
// 720x1280 is requested.
|
2018-09-06 15:02:55 +02:00
|
|
|
// Note: Should be called from the source only.
|
2020-05-14 14:59:55 +02:00
|
|
|
void OnOutputFormatRequest(const absl::optional<VideoFormat>& format)
|
2020-07-09 01:34:42 +02:00
|
|
|
RTC_LOCKS_EXCLUDED(mutex_);
|
2013-08-30 21:24:16 +00:00
|
|
|
|
2021-07-26 13:24:57 +02:00
|
|
|
// Requests output frame size and frame interval from `AdaptFrameResolution`.
|
|
|
|
|
// `target_aspect_ratio`: The input frame size will be cropped to match the
|
2018-09-06 15:02:55 +02:00
|
|
|
// requested aspect ratio. The aspect ratio is orientation agnostic and will
|
|
|
|
|
// be adjusted to maintain the input orientation (i.e. it doesn't matter if
|
|
|
|
|
// e.g. <1280,720> or <720,1280> is requested).
|
2021-07-26 13:24:57 +02:00
|
|
|
// `max_pixel_count`: The maximum output frame size.
|
|
|
|
|
// `max_fps`: The maximum output framerate.
|
2018-09-06 15:02:55 +02:00
|
|
|
// Note: Should be called from the source only.
|
|
|
|
|
void OnOutputFormatRequest(
|
|
|
|
|
const absl::optional<std::pair<int, int>>& target_aspect_ratio,
|
|
|
|
|
const absl::optional<int>& max_pixel_count,
|
2020-07-09 01:34:42 +02:00
|
|
|
const absl::optional<int>& max_fps) RTC_LOCKS_EXCLUDED(mutex_);
|
2018-09-06 15:02:55 +02:00
|
|
|
|
2018-10-26 14:00:18 +02:00
|
|
|
// Same as above, but allows setting two different target aspect ratios
|
|
|
|
|
// depending on incoming frame orientation. This gives more fine-grained
|
|
|
|
|
// control and can e.g. be used to force landscape video to be cropped to
|
|
|
|
|
// portrait video.
|
|
|
|
|
void OnOutputFormatRequest(
|
|
|
|
|
const absl::optional<std::pair<int, int>>& target_landscape_aspect_ratio,
|
|
|
|
|
const absl::optional<int>& max_landscape_pixel_count,
|
|
|
|
|
const absl::optional<std::pair<int, int>>& target_portrait_aspect_ratio,
|
|
|
|
|
const absl::optional<int>& max_portrait_pixel_count,
|
2020-07-09 01:34:42 +02:00
|
|
|
const absl::optional<int>& max_fps) RTC_LOCKS_EXCLUDED(mutex_);
|
2018-10-26 14:00:18 +02:00
|
|
|
|
2021-07-26 13:24:57 +02:00
|
|
|
// Requests the output frame size from `AdaptFrameResolution` to have as close
|
2019-11-15 16:56:01 +01:00
|
|
|
// as possible to |sink_wants.target_pixel_count| pixels (if set)
|
|
|
|
|
// but no more than |sink_wants.max_pixel_count|.
|
|
|
|
|
// |sink_wants.max_framerate_fps| is essentially analogous to
|
|
|
|
|
// |sink_wants.max_pixel_count|, but for framerate rather than resolution.
|
|
|
|
|
// Set |sink_wants.max_pixel_count| and/or |sink_wants.max_framerate_fps| to
|
Reland of Add framerate to VideoSinkWants and ability to signal on overuse (patchset #1 id:1 of https://codereview.webrtc.org/2783183003/ )
Reason for revert:
Seem to be a flaky test rather than an issue with this cl. Creating reland, will add code to reduce flakiness to that test.
Original issue's description:
> Revert of Add framerate to VideoSinkWants and ability to signal on overuse (patchset #8 id:410001 of https://codereview.webrtc.org/2781433002/ )
>
> Reason for revert:
> This has resulted in failure of CallPerfTest.ReceivesCpuOveruseAndUnderuse test on the Win7 build bot https://build.chromium.org/p/client.webrtc.perf/builders/Win7/builds/1780
>
> Original issue's description:
> > Reland of Add framerate to VideoSinkWants and ability to signal on overuse (patchset #1 id:1 of https://codereview.webrtc.org/2764133002/ )
> >
> > Reason for revert:
> > Found issue with test case, will add fix to reland cl.
> >
> > Original issue's description:
> > > Revert of Add framerate to VideoSinkWants and ability to signal on overuse (patchset #14 id:250001 of https://codereview.webrtc.org/2716643002/ )
> > >
> > > Reason for revert:
> > > Breaks perf tests:
> > > https://build.chromium.org/p/client.webrtc.perf/builders/Win7/builds/1679
> > > https://build.chromium.org/p/client.webrtc.perf/builders/Android32%20Tests%20%28L%20Nexus5%29/builds/2325
> > >
> > > Original issue's description:
> > > > Add framerate to VideoSinkWants and ability to signal on overuse
> > > >
> > > > In ViEEncoder, try to reduce framerate instead of resolution if the
> > > > current degradation preference is maintain-resolution rather than
> > > > balanced.
> > > >
> > > > BUG=webrtc:4172
> > > >
> > > > Review-Url: https://codereview.webrtc.org/2716643002
> > > > Cr-Commit-Position: refs/heads/master@{#17327}
> > > > Committed: https://chromium.googlesource.com/external/webrtc/+/72acf2526177bb4dbb5103cd6e165eb4361a5ae6
> > >
> > > TBR=nisse@webrtc.org,magjed@webrtc.org,kthelgason@webrtc.org,ilnik@webrtc.org,stefan@webrtc.org,sprang@webrtc.org
> > > # Skipping CQ checks because original CL landed less than 1 days ago.
> > > NOPRESUBMIT=true
> > > NOTREECHECKS=true
> > > NOTRY=true
> > > BUG=webrtc:4172
> > >
> > > Review-Url: https://codereview.webrtc.org/2764133002
> > > Cr-Commit-Position: refs/heads/master@{#17331}
> > > Committed: https://chromium.googlesource.com/external/webrtc/+/8b45b11144c968b4173215c76f78c710c9a2ed0b
> >
> > TBR=nisse@webrtc.org,magjed@webrtc.org,kthelgason@webrtc.org,ilnik@webrtc.org,stefan@webrtc.org,skvlad@webrtc.org
> > # Not skipping CQ checks because original CL landed more than 1 days ago.
> > BUG=webrtc:4172
> >
> > Review-Url: https://codereview.webrtc.org/2781433002
> > Cr-Commit-Position: refs/heads/master@{#17474}
> > Committed: https://chromium.googlesource.com/external/webrtc/+/3ea3c77e93121b1ab9d5e46641e6764f2cca0d51
>
> TBR=ilnik@webrtc.org,stefan@webrtc.org,asapersson@webrtc.org,sprang@webrtc.org
> # Skipping CQ checks because original CL landed less than 1 days ago.
> NOPRESUBMIT=true
> NOTREECHECKS=true
> NOTRY=true
> BUG=webrtc:4172
>
> Review-Url: https://codereview.webrtc.org/2783183003
> Cr-Commit-Position: refs/heads/master@{#17477}
> Committed: https://chromium.googlesource.com/external/webrtc/+/f9ed235c9b7248694edcb46feb1f29ce7456ab59
R=ilnik@webrtc.org,stefan@webrtc.org
BUG=webrtc:4172
Review-Url: https://codereview.webrtc.org/2789823002
Cr-Commit-Position: refs/heads/master@{#17498}
2017-04-02 23:53:04 -07:00
|
|
|
// std::numeric_limit<int>::max() if no upper limit is desired.
|
2019-12-19 09:47:11 +01:00
|
|
|
// The sink resolution alignment requirement is given by
|
|
|
|
|
// |sink_wants.resolution_alignment|.
|
2018-09-06 15:02:55 +02:00
|
|
|
// Note: Should be called from the sink only.
|
2020-05-14 14:59:55 +02:00
|
|
|
void OnSinkWants(const rtc::VideoSinkWants& sink_wants)
|
2020-07-09 01:34:42 +02:00
|
|
|
RTC_LOCKS_EXCLUDED(mutex_);
|
2014-06-24 07:24:49 +00:00
|
|
|
|
2020-08-26 16:50:44 +02:00
|
|
|
// Returns maximum image area, which shouldn't impose any adaptations.
|
|
|
|
|
// Can return |numeric_limits<int>::max()| if no limit is set.
|
|
|
|
|
int GetTargetPixels() const;
|
|
|
|
|
|
|
|
|
|
// Returns current frame-rate limit.
|
|
|
|
|
// Can return |numeric_limits<float>::infinity()| if no limit is set.
|
|
|
|
|
float GetMaxFramerate() const;
|
|
|
|
|
|
2013-07-10 00:45:36 +00:00
|
|
|
private:
|
2016-05-19 06:05:40 -07:00
|
|
|
// Determine if frame should be dropped based on input fps and requested fps.
|
2020-07-09 01:34:42 +02:00
|
|
|
bool KeepFrame(int64_t in_timestamp_ns) RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
|
|
|
|
|
|
|
|
|
int frames_in_ RTC_GUARDED_BY(mutex_); // Number of input frames.
|
|
|
|
|
int frames_out_ RTC_GUARDED_BY(mutex_); // Number of output frames.
|
|
|
|
|
int frames_scaled_ RTC_GUARDED_BY(mutex_); // Number of frames scaled.
|
2020-05-18 15:09:46 +02:00
|
|
|
int adaption_changes_
|
2020-07-09 01:34:42 +02:00
|
|
|
RTC_GUARDED_BY(mutex_); // Number of changes in scale factor.
|
|
|
|
|
int previous_width_ RTC_GUARDED_BY(mutex_); // Previous adapter output width.
|
2020-05-18 15:09:46 +02:00
|
|
|
int previous_height_
|
2020-07-09 01:34:42 +02:00
|
|
|
RTC_GUARDED_BY(mutex_); // Previous adapter output height.
|
2019-10-18 15:03:13 +02:00
|
|
|
const bool variable_start_scale_factor_;
|
2019-12-19 09:47:11 +01:00
|
|
|
|
|
|
|
|
// The fixed source resolution alignment requirement.
|
|
|
|
|
const int source_resolution_alignment_;
|
|
|
|
|
// The currently applied resolution alignment, as given by the requirements:
|
2021-07-26 13:24:57 +02:00
|
|
|
// - the fixed `source_resolution_alignment_`; and
|
2019-12-19 09:47:11 +01:00
|
|
|
// - the latest |sink_wants.resolution_alignment|.
|
2020-07-09 01:34:42 +02:00
|
|
|
int resolution_alignment_ RTC_GUARDED_BY(mutex_);
|
2019-12-19 09:47:11 +01:00
|
|
|
|
2016-05-19 06:05:40 -07:00
|
|
|
// The target timestamp for the next frame based on requested format.
|
2020-07-09 01:34:42 +02:00
|
|
|
absl::optional<int64_t> next_frame_timestamp_ns_ RTC_GUARDED_BY(mutex_);
|
2016-04-05 15:23:49 +02:00
|
|
|
|
2018-09-06 15:02:55 +02:00
|
|
|
// Max number of pixels/fps requested via calls to OnOutputFormatRequest,
|
|
|
|
|
// OnResolutionFramerateRequest respectively.
|
2016-04-05 15:23:49 +02:00
|
|
|
// The adapted output format is the minimum of these.
|
2018-10-26 14:00:18 +02:00
|
|
|
absl::optional<std::pair<int, int>> target_landscape_aspect_ratio_
|
2020-07-09 01:34:42 +02:00
|
|
|
RTC_GUARDED_BY(mutex_);
|
|
|
|
|
absl::optional<int> max_landscape_pixel_count_ RTC_GUARDED_BY(mutex_);
|
2018-10-26 14:00:18 +02:00
|
|
|
absl::optional<std::pair<int, int>> target_portrait_aspect_ratio_
|
2020-07-09 01:34:42 +02:00
|
|
|
RTC_GUARDED_BY(mutex_);
|
|
|
|
|
absl::optional<int> max_portrait_pixel_count_ RTC_GUARDED_BY(mutex_);
|
|
|
|
|
absl::optional<int> max_fps_ RTC_GUARDED_BY(mutex_);
|
|
|
|
|
int resolution_request_target_pixel_count_ RTC_GUARDED_BY(mutex_);
|
|
|
|
|
int resolution_request_max_pixel_count_ RTC_GUARDED_BY(mutex_);
|
|
|
|
|
int max_framerate_request_ RTC_GUARDED_BY(mutex_);
|
2016-04-05 15:23:49 +02:00
|
|
|
|
2013-07-10 00:45:36 +00:00
|
|
|
// The critical section to protect the above variables.
|
2020-08-26 16:50:44 +02:00
|
|
|
mutable webrtc::Mutex mutex_;
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2015-09-16 05:37:44 -07:00
|
|
|
RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter);
|
2013-07-10 00:45:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace cricket
|
|
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
#endif // MEDIA_BASE_VIDEO_ADAPTER_H_
|