blob: 5eb4ebfd756ad02b1e440d60db46a9c070bf6f3b [file] [log] [blame]
Niels Möller0327c2d2018-05-21 14:09:31 +02001/*
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öm1124ed12021-02-25 10:30:39 +010015#include <vector>
Niels Möller0327c2d2018-05-21 14:09:31 +020016
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020017#include "absl/types/optional.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020018#include "api/video/video_sink_interface.h"
Mirko Bonadeiac194142018-10-22 17:08:37 +020019#include "rtc_base/system/rtc_export.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020020
21namespace 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 Bonadeiac194142018-10-22 17:08:37 +020025struct RTC_EXPORT VideoSinkWants {
Henrik Boström1124ed12021-02-25 10:30:39 +010026 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öller0327c2d2018-05-21 14:09:31 +020035 VideoSinkWants();
36 VideoSinkWants(const VideoSinkWants&);
37 ~VideoSinkWants();
38 // Tells the source whether the sink wants frames with rotation applied.
39 // By default, any rotation must be applied by the sink.
40 bool rotation_applied = false;
41
42 // Tells the source that the sink only wants black frames.
43 bool black_frames = false;
44
45 // Tells the source the maximum number of pixels the sink wants.
46 int max_pixel_count = std::numeric_limits<int>::max();
47 // Tells the source the desired number of pixels the sinks wants. This will
48 // typically be used when stepping the resolution up again when conditions
49 // have improved after an earlier downgrade. The source should select the
50 // closest resolution to this pixel count, but if max_pixel_count is set, it
51 // still sets the absolute upper bound.
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020052 absl::optional<int> target_pixel_count;
Niels Möller0327c2d2018-05-21 14:09:31 +020053 // Tells the source the maximum framerate the sink wants.
54 int max_framerate_fps = std::numeric_limits<int>::max();
Rasmus Brandt5cad55b2019-12-19 09:47:11 +010055
56 // Tells the source that the sink wants width and height of the video frames
Artem Titov0e61fdd2021-07-25 21:50:14 +020057 // to be divisible by `resolution_alignment`.
Rasmus Brandt5cad55b2019-12-19 09:47:11 +010058 // For example: With I420, this value would be a multiple of 2.
59 // Note that this field is unrelated to any horizontal or vertical stride
60 // requirements the encoder has on the incoming video frame buffers.
61 int resolution_alignment = 1;
Henrik Boström1124ed12021-02-25 10:30:39 +010062
63 // The resolutions that sink is configured to consume. If the sink is an
64 // encoder this is what the encoder is configured to encode. In singlecast we
65 // only encode one resolution, but in simulcast and SVC this can mean multiple
66 // resolutions per frame.
67 //
68 // The sink is always configured to consume a subset of the
69 // webrtc::VideoFrame's resolution. In the case of encoding, we usually encode
70 // at webrtc::VideoFrame's resolution but this may not always be the case due
71 // to scaleResolutionDownBy or turning off simulcast or SVC layers.
72 //
73 // For example, we may capture at 720p and due to adaptation (e.g. applying
Artem Titov0e61fdd2021-07-25 21:50:14 +020074 // `max_pixel_count` constraints) create webrtc::VideoFrames of size 480p, but
Henrik Boström1124ed12021-02-25 10:30:39 +010075 // if we do scaleResolutionDownBy:2 then the only resolution we end up
76 // encoding is 240p. In this case we still need to provide webrtc::VideoFrames
77 // of size 480p but we can optimize internal buffers for 240p, avoiding
78 // downsampling to 480p if possible.
79 //
Artem Titov0e61fdd2021-07-25 21:50:14 +020080 // Note that the `resolutions` can change while frames are in flight and
Henrik Boström1124ed12021-02-25 10:30:39 +010081 // should only be used as a hint when constructing the webrtc::VideoFrame.
82 std::vector<FrameSize> resolutions;
Niels Möller0327c2d2018-05-21 14:09:31 +020083};
84
Henrik Boström1124ed12021-02-25 10:30:39 +010085inline bool operator==(const VideoSinkWants::FrameSize& a,
86 const VideoSinkWants::FrameSize& b) {
87 return a.width == b.width && a.height == b.height;
88}
89
Niels Möller0327c2d2018-05-21 14:09:31 +020090template <typename VideoFrameT>
91class VideoSourceInterface {
92 public:
Niels Möller1c931c42018-12-18 16:08:11 +010093 virtual ~VideoSourceInterface() = default;
94
Niels Möller0327c2d2018-05-21 14:09:31 +020095 virtual void AddOrUpdateSink(VideoSinkInterface<VideoFrameT>* sink,
96 const VideoSinkWants& wants) = 0;
97 // RemoveSink must guarantee that at the time the method returns,
98 // there is no current and no future calls to VideoSinkInterface::OnFrame.
99 virtual void RemoveSink(VideoSinkInterface<VideoFrameT>* sink) = 0;
Markus Handell2e0f4f02021-12-21 19:14:58 +0100100
101 // Request underlying source to capture a new frame.
102 // TODO(crbug/1255737): make pure virtual once downstream projects adapt.
103 virtual void RequestRefreshFrame() {}
Niels Möller0327c2d2018-05-21 14:09:31 +0200104};
105
106} // namespace rtc
107#endif // API_VIDEO_VIDEO_SOURCE_INTERFACE_H_