blob: 8e1df0f858a5c1974ef4b441d3ceb2f72f73e3ae [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
11#ifndef API_VIDEO_VIDEO_STREAM_ENCODER_INTERFACE_H_
12#define API_VIDEO_VIDEO_STREAM_ENCODER_INTERFACE_H_
13
14#include <vector>
15
Elad Alon8f01c4e2019-06-28 15:19:43 +020016#include "api/fec_controller_override.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "api/rtp_parameters.h" // For DegradationPreference.
Erik Språng610c7632019-03-06 15:37:33 +010018#include "api/units/data_rate.h"
Niels Möllerefd70342019-01-22 10:35:42 +010019#include "api/video/video_bitrate_allocator.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020020#include "api/video/video_sink_interface.h"
21#include "api/video/video_source_interface.h"
22#include "api/video_codecs/video_encoder.h"
23#include "api/video_codecs/video_encoder_config.h"
24
25namespace webrtc {
26
Niels Möller0327c2d2018-05-21 14:09:31 +020027// This interface represents a class responsible for creating and driving the
28// encoder(s) for a single video stream. It is also responsible for adaptation
29// decisions related to video quality, requesting reduced frame rate or
30// resolution from the VideoSource when needed.
31// TODO(bugs.webrtc.org/8830): This interface is under development. Changes
32// under consideration include:
33//
34// 1. Taking out responsibility for adaptation decisions, instead only reporting
35// per-frame measurements to the decision maker.
36//
37// 2. Moving responsibility for simulcast and for software fallback into this
38// class.
39class VideoStreamEncoderInterface : public rtc::VideoSinkInterface<VideoFrame> {
40 public:
41 // Interface for receiving encoded video frames and notifications about
42 // configuration changes.
43 class EncoderSink : public EncodedImageCallback {
44 public:
45 virtual void OnEncoderConfigurationChanged(
46 std::vector<VideoStream> streams,
Ilya Nikolaevskiy93be66c2020-04-02 14:10:27 +020047 bool is_svc,
Rasmus Brandtc402dbe2019-02-04 11:09:46 +010048 VideoEncoderConfig::ContentType content_type,
Niels Möller0327c2d2018-05-21 14:09:31 +020049 int min_transmit_bitrate_bps) = 0;
50 };
51
Niels Möller0327c2d2018-05-21 14:09:31 +020052 // Sets the source that will provide video frames to the VideoStreamEncoder's
53 // OnFrame method. |degradation_preference| control whether or not resolution
54 // or frame rate may be reduced. The VideoStreamEncoder registers itself with
55 // |source|, and signals adaptation decisions to the source in the form of
56 // VideoSinkWants.
57 // TODO(nisse): When adaptation logic is extracted from this class,
58 // it no longer needs to know the source.
59 virtual void SetSource(
60 rtc::VideoSourceInterface<VideoFrame>* source,
61 const DegradationPreference& degradation_preference) = 0;
62
63 // Sets the |sink| that gets the encoded frames. |rotation_applied| means
64 // that the source must support rotation. Only set |rotation_applied| if the
65 // remote side does not support the rotation extension.
66 virtual void SetSink(EncoderSink* sink, bool rotation_applied) = 0;
67
68 // Sets an initial bitrate, later overriden by OnBitrateUpdated. Mainly
69 // affects the resolution of the initial key frame: If incoming frames are
70 // larger than reasonable for the start bitrate, and scaling is enabled,
71 // VideoStreamEncoder asks the source to scale down and drops a few initial
72 // frames.
73 // TODO(nisse): This is a poor interface, and mixes bandwidth estimation and
74 // codec configuration in an undesired way. For the actual send bandwidth, we
75 // should always be somewhat conservative, but we may nevertheless want to let
76 // the application configure a more optimistic quality for the initial
77 // resolution. Should be replaced by a construction time setting.
78 virtual void SetStartBitrate(int start_bitrate_bps) = 0;
79
80 // Request a key frame. Used for signalling from the remote receiver.
81 virtual void SendKeyFrame() = 0;
82
Elad Alonb6ef99b2019-04-10 16:37:07 +020083 // Inform the encoder that a loss has occurred.
84 virtual void OnLossNotification(
85 const VideoEncoder::LossNotification& loss_notification) = 0;
86
Erik Språng4c6ca302019-04-08 15:14:01 +020087 // Set the currently estimated network properties. A |target_bitrate|
Niels Möller0327c2d2018-05-21 14:09:31 +020088 // of zero pauses the encoder.
Florent Castellia8336d32019-09-09 13:36:55 +020089 // |stable_target_bitrate| is a filtered version of |target_bitrate|. It is
90 // always less or equal to it. It can be used to avoid rapid changes of
91 // expensive encoding settings, such as resolution.
Erik Språng4c6ca302019-04-08 15:14:01 +020092 // |link_allocation| is the bandwidth available for this video stream on the
93 // network link. It is always at least |target_bitrate| but may be higher
94 // if we are not network constrained.
Erik Språng610c7632019-03-06 15:37:33 +010095 virtual void OnBitrateUpdated(DataRate target_bitrate,
Florent Castellia8336d32019-09-09 13:36:55 +020096 DataRate stable_target_bitrate,
Erik Språng4c6ca302019-04-08 15:14:01 +020097 DataRate link_allocation,
Niels Möller0327c2d2018-05-21 14:09:31 +020098 uint8_t fraction_lost,
Ying Wang9b881ab2020-02-07 14:29:32 +010099 int64_t round_trip_time_ms,
100 double cwnd_reduce_ratio) = 0;
Niels Möller0327c2d2018-05-21 14:09:31 +0200101
102 // Register observer for the bitrate allocation between the temporal
103 // and spatial layers.
104 virtual void SetBitrateAllocationObserver(
105 VideoBitrateAllocationObserver* bitrate_observer) = 0;
106
Elad Alon8f01c4e2019-06-28 15:19:43 +0200107 // Set a FecControllerOverride, through which the encoder may override
108 // decisions made by FecController.
Elad Alon8f01c4e2019-06-28 15:19:43 +0200109 virtual void SetFecControllerOverride(
Elad Alona63aede2019-06-28 18:20:58 +0200110 FecControllerOverride* fec_controller_override) = 0;
Elad Alon8f01c4e2019-06-28 15:19:43 +0200111
Niels Möller0327c2d2018-05-21 14:09:31 +0200112 // Creates and configures an encoder with the given |config|. The
113 // |max_data_payload_length| is used to support single NAL unit
114 // packetization for H.264.
115 virtual void ConfigureEncoder(VideoEncoderConfig config,
116 size_t max_data_payload_length) = 0;
117
118 // Permanently stop encoding. After this method has returned, it is
119 // guaranteed that no encoded frames will be delivered to the sink.
120 virtual void Stop() = 0;
121};
122
123} // namespace webrtc
124
125#endif // API_VIDEO_VIDEO_STREAM_ENCODER_INTERFACE_H_