blob: 25190aa474a90efceda2c3fc23483a4c1ecd48ac [file] [log] [blame]
Niels Möller0327c2d2018-05-21 14:09:31 +02001/*
2 * Copyright (c) 2018 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
Jonas Oreland6c2dae22022-09-29 10:28:24 +020011#ifndef VIDEO_VIDEO_STREAM_ENCODER_INTERFACE_H_
12#define VIDEO_VIDEO_STREAM_ENCODER_INTERFACE_H_
Niels Möller0327c2d2018-05-21 14:09:31 +020013
14#include <vector>
15
Henrik Boströmf4a99912020-06-11 12:07:14 +020016#include "api/adaptation/resource.h"
Elad Alon8f01c4e2019-06-28 15:19:43 +020017#include "api/fec_controller_override.h"
Florent Castelliacabb362022-10-18 17:05:16 +020018#include "api/rtc_error.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "api/rtp_parameters.h" // For DegradationPreference.
Florent Castelliacabb362022-10-18 17:05:16 +020020#include "api/rtp_sender_interface.h"
Henrik Boströmf4a99912020-06-11 12:07:14 +020021#include "api/scoped_refptr.h"
Erik Språng610c7632019-03-06 15:37:33 +010022#include "api/units/data_rate.h"
Niels Möllerefd70342019-01-22 10:35:42 +010023#include "api/video/video_bitrate_allocator.h"
Per Kjellandera9434842020-10-15 17:53:22 +020024#include "api/video/video_layers_allocation.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020025#include "api/video/video_sink_interface.h"
26#include "api/video/video_source_interface.h"
27#include "api/video_codecs/video_encoder.h"
Jonas Oreland6c2dae22022-09-29 10:28:24 +020028#include "video/config/video_encoder_config.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020029
30namespace webrtc {
31
Niels Möller0327c2d2018-05-21 14:09:31 +020032// This interface represents a class responsible for creating and driving the
33// encoder(s) for a single video stream. It is also responsible for adaptation
34// decisions related to video quality, requesting reduced frame rate or
35// resolution from the VideoSource when needed.
36// TODO(bugs.webrtc.org/8830): This interface is under development. Changes
37// under consideration include:
38//
39// 1. Taking out responsibility for adaptation decisions, instead only reporting
40// per-frame measurements to the decision maker.
41//
42// 2. Moving responsibility for simulcast and for software fallback into this
43// class.
Markus Handellb4e96d42021-11-05 12:00:55 +010044class VideoStreamEncoderInterface {
Niels Möller0327c2d2018-05-21 14:09:31 +020045 public:
46 // Interface for receiving encoded video frames and notifications about
47 // configuration changes.
48 class EncoderSink : public EncodedImageCallback {
49 public:
50 virtual void OnEncoderConfigurationChanged(
51 std::vector<VideoStream> streams,
Ilya Nikolaevskiy93be66c2020-04-02 14:10:27 +020052 bool is_svc,
Rasmus Brandtc402dbe2019-02-04 11:09:46 +010053 VideoEncoderConfig::ContentType content_type,
Niels Möller0327c2d2018-05-21 14:09:31 +020054 int min_transmit_bitrate_bps) = 0;
Per Kjellanderdcef6412020-10-07 15:09:05 +020055
56 virtual void OnBitrateAllocationUpdated(
57 const VideoBitrateAllocation& allocation) = 0;
Per Kjellandera9434842020-10-15 17:53:22 +020058
59 virtual void OnVideoLayersAllocationUpdated(
60 VideoLayersAllocation allocation) = 0;
Niels Möller0327c2d2018-05-21 14:09:31 +020061 };
62
Markus Handellb4e96d42021-11-05 12:00:55 +010063 virtual ~VideoStreamEncoderInterface() = default;
64
Henrik Boströmf4a99912020-06-11 12:07:14 +020065 // If the resource is overusing, the VideoStreamEncoder will try to reduce
66 // resolution or frame rate until no resource is overusing.
67 // TODO(https://crbug.com/webrtc/11565): When the ResourceAdaptationProcessor
68 // is moved to Call this method could be deleted altogether in favor of
69 // Call-level APIs only.
70 virtual void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) = 0;
71 virtual std::vector<rtc::scoped_refptr<Resource>>
72 GetAdaptationResources() = 0;
73
Niels Möller0327c2d2018-05-21 14:09:31 +020074 // Sets the source that will provide video frames to the VideoStreamEncoder's
Artem Titov0e61fdd2021-07-25 21:50:14 +020075 // OnFrame method. `degradation_preference` control whether or not resolution
Niels Möller0327c2d2018-05-21 14:09:31 +020076 // or frame rate may be reduced. The VideoStreamEncoder registers itself with
Artem Titov0e61fdd2021-07-25 21:50:14 +020077 // `source`, and signals adaptation decisions to the source in the form of
Niels Möller0327c2d2018-05-21 14:09:31 +020078 // VideoSinkWants.
Niels Möller6939f632022-07-05 08:55:19 +020079 // TODO(bugs.webrtc.org/14246): When adaptation logic is extracted from this
80 // class, it no longer needs to know the source.
Niels Möller0327c2d2018-05-21 14:09:31 +020081 virtual void SetSource(
82 rtc::VideoSourceInterface<VideoFrame>* source,
83 const DegradationPreference& degradation_preference) = 0;
84
Artem Titov0e61fdd2021-07-25 21:50:14 +020085 // Sets the `sink` that gets the encoded frames. `rotation_applied` means
86 // that the source must support rotation. Only set `rotation_applied` if the
Niels Möller0327c2d2018-05-21 14:09:31 +020087 // remote side does not support the rotation extension.
88 virtual void SetSink(EncoderSink* sink, bool rotation_applied) = 0;
89
90 // Sets an initial bitrate, later overriden by OnBitrateUpdated. Mainly
91 // affects the resolution of the initial key frame: If incoming frames are
92 // larger than reasonable for the start bitrate, and scaling is enabled,
93 // VideoStreamEncoder asks the source to scale down and drops a few initial
94 // frames.
95 // TODO(nisse): This is a poor interface, and mixes bandwidth estimation and
96 // codec configuration in an undesired way. For the actual send bandwidth, we
97 // should always be somewhat conservative, but we may nevertheless want to let
98 // the application configure a more optimistic quality for the initial
99 // resolution. Should be replaced by a construction time setting.
100 virtual void SetStartBitrate(int start_bitrate_bps) = 0;
101
Philipp Hanckea1b4eb22022-11-04 14:45:23 +0100102 // Request a key frame. Used for signalling from the remote receiver with
103 // no arguments and for RTCRtpSender.generateKeyFrame with a list of
104 // rids/layers.
105 virtual void SendKeyFrame(const std::vector<VideoFrameType>& layers = {}) = 0;
Niels Möller0327c2d2018-05-21 14:09:31 +0200106
Elad Alonb6ef99b2019-04-10 16:37:07 +0200107 // Inform the encoder that a loss has occurred.
108 virtual void OnLossNotification(
109 const VideoEncoder::LossNotification& loss_notification) = 0;
110
Artem Titov0e61fdd2021-07-25 21:50:14 +0200111 // Set the currently estimated network properties. A `target_bitrate`
Niels Möller0327c2d2018-05-21 14:09:31 +0200112 // of zero pauses the encoder.
Artem Titov0e61fdd2021-07-25 21:50:14 +0200113 // `stable_target_bitrate` is a filtered version of `target_bitrate`. It is
Florent Castellia8336d32019-09-09 13:36:55 +0200114 // always less or equal to it. It can be used to avoid rapid changes of
115 // expensive encoding settings, such as resolution.
Artem Titov0e61fdd2021-07-25 21:50:14 +0200116 // `link_allocation` is the bandwidth available for this video stream on the
117 // network link. It is always at least `target_bitrate` but may be higher
Erik Språng4c6ca302019-04-08 15:14:01 +0200118 // if we are not network constrained.
Erik Språng610c7632019-03-06 15:37:33 +0100119 virtual void OnBitrateUpdated(DataRate target_bitrate,
Florent Castellia8336d32019-09-09 13:36:55 +0200120 DataRate stable_target_bitrate,
Erik Språng4c6ca302019-04-08 15:14:01 +0200121 DataRate link_allocation,
Niels Möller0327c2d2018-05-21 14:09:31 +0200122 uint8_t fraction_lost,
Ying Wang9b881ab2020-02-07 14:29:32 +0100123 int64_t round_trip_time_ms,
124 double cwnd_reduce_ratio) = 0;
Niels Möller0327c2d2018-05-21 14:09:31 +0200125
Elad Alon8f01c4e2019-06-28 15:19:43 +0200126 // Set a FecControllerOverride, through which the encoder may override
127 // decisions made by FecController.
Elad Alon8f01c4e2019-06-28 15:19:43 +0200128 virtual void SetFecControllerOverride(
Elad Alona63aede2019-06-28 18:20:58 +0200129 FecControllerOverride* fec_controller_override) = 0;
Elad Alon8f01c4e2019-06-28 15:19:43 +0200130
Artem Titov0e61fdd2021-07-25 21:50:14 +0200131 // Creates and configures an encoder with the given `config`. The
132 // `max_data_payload_length` is used to support single NAL unit
Niels Möller0327c2d2018-05-21 14:09:31 +0200133 // packetization for H.264.
134 virtual void ConfigureEncoder(VideoEncoderConfig config,
135 size_t max_data_payload_length) = 0;
Florent Castelliacabb362022-10-18 17:05:16 +0200136 virtual void ConfigureEncoder(VideoEncoderConfig config,
137 size_t max_data_payload_length,
138 SetParametersCallback callback) = 0;
Niels Möller0327c2d2018-05-21 14:09:31 +0200139
140 // Permanently stop encoding. After this method has returned, it is
141 // guaranteed that no encoded frames will be delivered to the sink.
142 virtual void Stop() = 0;
143};
144
145} // namespace webrtc
146
Jonas Oreland6c2dae22022-09-29 10:28:24 +0200147#endif // VIDEO_VIDEO_STREAM_ENCODER_INTERFACE_H_