xref: /aosp_15_r20/external/webrtc/video/video_stream_encoder_interface.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef VIDEO_VIDEO_STREAM_ENCODER_INTERFACE_H_
12*d9f75844SAndroid Build Coastguard Worker #define VIDEO_VIDEO_STREAM_ENCODER_INTERFACE_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <vector>
15*d9f75844SAndroid Build Coastguard Worker 
16*d9f75844SAndroid Build Coastguard Worker #include "api/adaptation/resource.h"
17*d9f75844SAndroid Build Coastguard Worker #include "api/fec_controller_override.h"
18*d9f75844SAndroid Build Coastguard Worker #include "api/rtc_error.h"
19*d9f75844SAndroid Build Coastguard Worker #include "api/rtp_parameters.h"  // For DegradationPreference.
20*d9f75844SAndroid Build Coastguard Worker #include "api/rtp_sender_interface.h"
21*d9f75844SAndroid Build Coastguard Worker #include "api/scoped_refptr.h"
22*d9f75844SAndroid Build Coastguard Worker #include "api/units/data_rate.h"
23*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_bitrate_allocator.h"
24*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_layers_allocation.h"
25*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_sink_interface.h"
26*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_source_interface.h"
27*d9f75844SAndroid Build Coastguard Worker #include "api/video_codecs/video_encoder.h"
28*d9f75844SAndroid Build Coastguard Worker #include "video/config/video_encoder_config.h"
29*d9f75844SAndroid Build Coastguard Worker 
30*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
31*d9f75844SAndroid Build Coastguard Worker 
32*d9f75844SAndroid Build Coastguard Worker // This interface represents a class responsible for creating and driving the
33*d9f75844SAndroid Build Coastguard Worker // encoder(s) for a single video stream. It is also responsible for adaptation
34*d9f75844SAndroid Build Coastguard Worker // decisions related to video quality, requesting reduced frame rate or
35*d9f75844SAndroid Build Coastguard Worker // resolution from the VideoSource when needed.
36*d9f75844SAndroid Build Coastguard Worker // TODO(bugs.webrtc.org/8830): This interface is under development. Changes
37*d9f75844SAndroid Build Coastguard Worker // under consideration include:
38*d9f75844SAndroid Build Coastguard Worker //
39*d9f75844SAndroid Build Coastguard Worker // 1. Taking out responsibility for adaptation decisions, instead only reporting
40*d9f75844SAndroid Build Coastguard Worker //    per-frame measurements to the decision maker.
41*d9f75844SAndroid Build Coastguard Worker //
42*d9f75844SAndroid Build Coastguard Worker // 2. Moving responsibility for simulcast and for software fallback into this
43*d9f75844SAndroid Build Coastguard Worker //    class.
44*d9f75844SAndroid Build Coastguard Worker class VideoStreamEncoderInterface {
45*d9f75844SAndroid Build Coastguard Worker  public:
46*d9f75844SAndroid Build Coastguard Worker   // Interface for receiving encoded video frames and notifications about
47*d9f75844SAndroid Build Coastguard Worker   // configuration changes.
48*d9f75844SAndroid Build Coastguard Worker   class EncoderSink : public EncodedImageCallback {
49*d9f75844SAndroid Build Coastguard Worker    public:
50*d9f75844SAndroid Build Coastguard Worker     virtual void OnEncoderConfigurationChanged(
51*d9f75844SAndroid Build Coastguard Worker         std::vector<VideoStream> streams,
52*d9f75844SAndroid Build Coastguard Worker         bool is_svc,
53*d9f75844SAndroid Build Coastguard Worker         VideoEncoderConfig::ContentType content_type,
54*d9f75844SAndroid Build Coastguard Worker         int min_transmit_bitrate_bps) = 0;
55*d9f75844SAndroid Build Coastguard Worker 
56*d9f75844SAndroid Build Coastguard Worker     virtual void OnBitrateAllocationUpdated(
57*d9f75844SAndroid Build Coastguard Worker         const VideoBitrateAllocation& allocation) = 0;
58*d9f75844SAndroid Build Coastguard Worker 
59*d9f75844SAndroid Build Coastguard Worker     virtual void OnVideoLayersAllocationUpdated(
60*d9f75844SAndroid Build Coastguard Worker         VideoLayersAllocation allocation) = 0;
61*d9f75844SAndroid Build Coastguard Worker   };
62*d9f75844SAndroid Build Coastguard Worker 
63*d9f75844SAndroid Build Coastguard Worker   virtual ~VideoStreamEncoderInterface() = default;
64*d9f75844SAndroid Build Coastguard Worker 
65*d9f75844SAndroid Build Coastguard Worker   // If the resource is overusing, the VideoStreamEncoder will try to reduce
66*d9f75844SAndroid Build Coastguard Worker   // resolution or frame rate until no resource is overusing.
67*d9f75844SAndroid Build Coastguard Worker   // TODO(https://crbug.com/webrtc/11565): When the ResourceAdaptationProcessor
68*d9f75844SAndroid Build Coastguard Worker   // is moved to Call this method could be deleted altogether in favor of
69*d9f75844SAndroid Build Coastguard Worker   // Call-level APIs only.
70*d9f75844SAndroid Build Coastguard Worker   virtual void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) = 0;
71*d9f75844SAndroid Build Coastguard Worker   virtual std::vector<rtc::scoped_refptr<Resource>>
72*d9f75844SAndroid Build Coastguard Worker   GetAdaptationResources() = 0;
73*d9f75844SAndroid Build Coastguard Worker 
74*d9f75844SAndroid Build Coastguard Worker   // Sets the source that will provide video frames to the VideoStreamEncoder's
75*d9f75844SAndroid Build Coastguard Worker   // OnFrame method. `degradation_preference` control whether or not resolution
76*d9f75844SAndroid Build Coastguard Worker   // or frame rate may be reduced. The VideoStreamEncoder registers itself with
77*d9f75844SAndroid Build Coastguard Worker   // `source`, and signals adaptation decisions to the source in the form of
78*d9f75844SAndroid Build Coastguard Worker   // VideoSinkWants.
79*d9f75844SAndroid Build Coastguard Worker   // TODO(bugs.webrtc.org/14246): When adaptation logic is extracted from this
80*d9f75844SAndroid Build Coastguard Worker   // class, it no longer needs to know the source.
81*d9f75844SAndroid Build Coastguard Worker   virtual void SetSource(
82*d9f75844SAndroid Build Coastguard Worker       rtc::VideoSourceInterface<VideoFrame>* source,
83*d9f75844SAndroid Build Coastguard Worker       const DegradationPreference& degradation_preference) = 0;
84*d9f75844SAndroid Build Coastguard Worker 
85*d9f75844SAndroid Build Coastguard Worker   // Sets the `sink` that gets the encoded frames. `rotation_applied` means
86*d9f75844SAndroid Build Coastguard Worker   // that the source must support rotation. Only set `rotation_applied` if the
87*d9f75844SAndroid Build Coastguard Worker   // remote side does not support the rotation extension.
88*d9f75844SAndroid Build Coastguard Worker   virtual void SetSink(EncoderSink* sink, bool rotation_applied) = 0;
89*d9f75844SAndroid Build Coastguard Worker 
90*d9f75844SAndroid Build Coastguard Worker   // Sets an initial bitrate, later overriden by OnBitrateUpdated. Mainly
91*d9f75844SAndroid Build Coastguard Worker   // affects the resolution of the initial key frame: If incoming frames are
92*d9f75844SAndroid Build Coastguard Worker   // larger than reasonable for the start bitrate, and scaling is enabled,
93*d9f75844SAndroid Build Coastguard Worker   // VideoStreamEncoder asks the source to scale down and drops a few initial
94*d9f75844SAndroid Build Coastguard Worker   // frames.
95*d9f75844SAndroid Build Coastguard Worker   // TODO(nisse): This is a poor interface, and mixes bandwidth estimation and
96*d9f75844SAndroid Build Coastguard Worker   // codec configuration in an undesired way. For the actual send bandwidth, we
97*d9f75844SAndroid Build Coastguard Worker   // should always be somewhat conservative, but we may nevertheless want to let
98*d9f75844SAndroid Build Coastguard Worker   // the application configure a more optimistic quality for the initial
99*d9f75844SAndroid Build Coastguard Worker   // resolution. Should be replaced by a construction time setting.
100*d9f75844SAndroid Build Coastguard Worker   virtual void SetStartBitrate(int start_bitrate_bps) = 0;
101*d9f75844SAndroid Build Coastguard Worker 
102*d9f75844SAndroid Build Coastguard Worker   // Request a key frame. Used for signalling from the remote receiver with
103*d9f75844SAndroid Build Coastguard Worker   // no arguments and for RTCRtpSender.generateKeyFrame with a list of
104*d9f75844SAndroid Build Coastguard Worker   // rids/layers.
105*d9f75844SAndroid Build Coastguard Worker   virtual void SendKeyFrame(const std::vector<VideoFrameType>& layers = {}) = 0;
106*d9f75844SAndroid Build Coastguard Worker 
107*d9f75844SAndroid Build Coastguard Worker   // Inform the encoder that a loss has occurred.
108*d9f75844SAndroid Build Coastguard Worker   virtual void OnLossNotification(
109*d9f75844SAndroid Build Coastguard Worker       const VideoEncoder::LossNotification& loss_notification) = 0;
110*d9f75844SAndroid Build Coastguard Worker 
111*d9f75844SAndroid Build Coastguard Worker   // Set the currently estimated network properties. A `target_bitrate`
112*d9f75844SAndroid Build Coastguard Worker   // of zero pauses the encoder.
113*d9f75844SAndroid Build Coastguard Worker   // `stable_target_bitrate` is a filtered version of `target_bitrate`. It  is
114*d9f75844SAndroid Build Coastguard Worker   // always less or equal to it. It can be used to avoid rapid changes of
115*d9f75844SAndroid Build Coastguard Worker   // expensive encoding settings, such as resolution.
116*d9f75844SAndroid Build Coastguard Worker   // `link_allocation` is the bandwidth available for this video stream on the
117*d9f75844SAndroid Build Coastguard Worker   // network link. It is always at least `target_bitrate` but may be higher
118*d9f75844SAndroid Build Coastguard Worker   // if we are not network constrained.
119*d9f75844SAndroid Build Coastguard Worker   virtual void OnBitrateUpdated(DataRate target_bitrate,
120*d9f75844SAndroid Build Coastguard Worker                                 DataRate stable_target_bitrate,
121*d9f75844SAndroid Build Coastguard Worker                                 DataRate link_allocation,
122*d9f75844SAndroid Build Coastguard Worker                                 uint8_t fraction_lost,
123*d9f75844SAndroid Build Coastguard Worker                                 int64_t round_trip_time_ms,
124*d9f75844SAndroid Build Coastguard Worker                                 double cwnd_reduce_ratio) = 0;
125*d9f75844SAndroid Build Coastguard Worker 
126*d9f75844SAndroid Build Coastguard Worker   // Set a FecControllerOverride, through which the encoder may override
127*d9f75844SAndroid Build Coastguard Worker   // decisions made by FecController.
128*d9f75844SAndroid Build Coastguard Worker   virtual void SetFecControllerOverride(
129*d9f75844SAndroid Build Coastguard Worker       FecControllerOverride* fec_controller_override) = 0;
130*d9f75844SAndroid Build Coastguard Worker 
131*d9f75844SAndroid Build Coastguard Worker   // Creates and configures an encoder with the given `config`. The
132*d9f75844SAndroid Build Coastguard Worker   // `max_data_payload_length` is used to support single NAL unit
133*d9f75844SAndroid Build Coastguard Worker   // packetization for H.264.
134*d9f75844SAndroid Build Coastguard Worker   virtual void ConfigureEncoder(VideoEncoderConfig config,
135*d9f75844SAndroid Build Coastguard Worker                                 size_t max_data_payload_length) = 0;
136*d9f75844SAndroid Build Coastguard Worker   virtual void ConfigureEncoder(VideoEncoderConfig config,
137*d9f75844SAndroid Build Coastguard Worker                                 size_t max_data_payload_length,
138*d9f75844SAndroid Build Coastguard Worker                                 SetParametersCallback callback) = 0;
139*d9f75844SAndroid Build Coastguard Worker 
140*d9f75844SAndroid Build Coastguard Worker   // Permanently stop encoding. After this method has returned, it is
141*d9f75844SAndroid Build Coastguard Worker   // guaranteed that no encoded frames will be delivered to the sink.
142*d9f75844SAndroid Build Coastguard Worker   virtual void Stop() = 0;
143*d9f75844SAndroid Build Coastguard Worker };
144*d9f75844SAndroid Build Coastguard Worker 
145*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
146*d9f75844SAndroid Build Coastguard Worker 
147*d9f75844SAndroid Build Coastguard Worker #endif  // VIDEO_VIDEO_STREAM_ENCODER_INTERFACE_H_
148