Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
Henrik Boström | f4a9991 | 2020-06-11 12:07:14 +0200 | [diff] [blame] | 16 | #include "api/adaptation/resource.h" |
Elad Alon | 8f01c4e | 2019-06-28 15:19:43 +0200 | [diff] [blame] | 17 | #include "api/fec_controller_override.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "api/rtp_parameters.h" // For DegradationPreference. |
Henrik Boström | f4a9991 | 2020-06-11 12:07:14 +0200 | [diff] [blame] | 19 | #include "api/scoped_refptr.h" |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 20 | #include "api/units/data_rate.h" |
Niels Möller | efd7034 | 2019-01-22 10:35:42 +0100 | [diff] [blame] | 21 | #include "api/video/video_bitrate_allocator.h" |
Per Kjellander | a943484 | 2020-10-15 17:53:22 +0200 | [diff] [blame] | 22 | #include "api/video/video_layers_allocation.h" |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 23 | #include "api/video/video_sink_interface.h" |
| 24 | #include "api/video/video_source_interface.h" |
| 25 | #include "api/video_codecs/video_encoder.h" |
| 26 | #include "api/video_codecs/video_encoder_config.h" |
| 27 | |
| 28 | namespace webrtc { |
| 29 | |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 30 | // This interface represents a class responsible for creating and driving the |
| 31 | // encoder(s) for a single video stream. It is also responsible for adaptation |
| 32 | // decisions related to video quality, requesting reduced frame rate or |
| 33 | // resolution from the VideoSource when needed. |
| 34 | // TODO(bugs.webrtc.org/8830): This interface is under development. Changes |
| 35 | // under consideration include: |
| 36 | // |
| 37 | // 1. Taking out responsibility for adaptation decisions, instead only reporting |
| 38 | // per-frame measurements to the decision maker. |
| 39 | // |
| 40 | // 2. Moving responsibility for simulcast and for software fallback into this |
| 41 | // class. |
Markus Handell | b4e96d4 | 2021-11-05 12:00:55 +0100 | [diff] [blame^] | 42 | class VideoStreamEncoderInterface { |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 43 | public: |
| 44 | // Interface for receiving encoded video frames and notifications about |
| 45 | // configuration changes. |
| 46 | class EncoderSink : public EncodedImageCallback { |
| 47 | public: |
| 48 | virtual void OnEncoderConfigurationChanged( |
| 49 | std::vector<VideoStream> streams, |
Ilya Nikolaevskiy | 93be66c | 2020-04-02 14:10:27 +0200 | [diff] [blame] | 50 | bool is_svc, |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 51 | VideoEncoderConfig::ContentType content_type, |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 52 | int min_transmit_bitrate_bps) = 0; |
Per Kjellander | dcef641 | 2020-10-07 15:09:05 +0200 | [diff] [blame] | 53 | |
| 54 | virtual void OnBitrateAllocationUpdated( |
| 55 | const VideoBitrateAllocation& allocation) = 0; |
Per Kjellander | a943484 | 2020-10-15 17:53:22 +0200 | [diff] [blame] | 56 | |
| 57 | virtual void OnVideoLayersAllocationUpdated( |
| 58 | VideoLayersAllocation allocation) = 0; |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
Markus Handell | b4e96d4 | 2021-11-05 12:00:55 +0100 | [diff] [blame^] | 61 | virtual ~VideoStreamEncoderInterface() = default; |
| 62 | |
Henrik Boström | f4a9991 | 2020-06-11 12:07:14 +0200 | [diff] [blame] | 63 | // If the resource is overusing, the VideoStreamEncoder will try to reduce |
| 64 | // resolution or frame rate until no resource is overusing. |
| 65 | // TODO(https://crbug.com/webrtc/11565): When the ResourceAdaptationProcessor |
| 66 | // is moved to Call this method could be deleted altogether in favor of |
| 67 | // Call-level APIs only. |
| 68 | virtual void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) = 0; |
| 69 | virtual std::vector<rtc::scoped_refptr<Resource>> |
| 70 | GetAdaptationResources() = 0; |
| 71 | |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 72 | // Sets the source that will provide video frames to the VideoStreamEncoder's |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 73 | // OnFrame method. `degradation_preference` control whether or not resolution |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 74 | // or frame rate may be reduced. The VideoStreamEncoder registers itself with |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 75 | // `source`, and signals adaptation decisions to the source in the form of |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 76 | // VideoSinkWants. |
| 77 | // TODO(nisse): When adaptation logic is extracted from this class, |
| 78 | // it no longer needs to know the source. |
| 79 | virtual void SetSource( |
| 80 | rtc::VideoSourceInterface<VideoFrame>* source, |
| 81 | const DegradationPreference& degradation_preference) = 0; |
| 82 | |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 83 | // Sets the `sink` that gets the encoded frames. `rotation_applied` means |
| 84 | // that the source must support rotation. Only set `rotation_applied` if the |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 85 | // remote side does not support the rotation extension. |
| 86 | virtual void SetSink(EncoderSink* sink, bool rotation_applied) = 0; |
| 87 | |
| 88 | // Sets an initial bitrate, later overriden by OnBitrateUpdated. Mainly |
| 89 | // affects the resolution of the initial key frame: If incoming frames are |
| 90 | // larger than reasonable for the start bitrate, and scaling is enabled, |
| 91 | // VideoStreamEncoder asks the source to scale down and drops a few initial |
| 92 | // frames. |
| 93 | // TODO(nisse): This is a poor interface, and mixes bandwidth estimation and |
| 94 | // codec configuration in an undesired way. For the actual send bandwidth, we |
| 95 | // should always be somewhat conservative, but we may nevertheless want to let |
| 96 | // the application configure a more optimistic quality for the initial |
| 97 | // resolution. Should be replaced by a construction time setting. |
| 98 | virtual void SetStartBitrate(int start_bitrate_bps) = 0; |
| 99 | |
| 100 | // Request a key frame. Used for signalling from the remote receiver. |
| 101 | virtual void SendKeyFrame() = 0; |
| 102 | |
Elad Alon | b6ef99b | 2019-04-10 16:37:07 +0200 | [diff] [blame] | 103 | // Inform the encoder that a loss has occurred. |
| 104 | virtual void OnLossNotification( |
| 105 | const VideoEncoder::LossNotification& loss_notification) = 0; |
| 106 | |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 107 | // Set the currently estimated network properties. A `target_bitrate` |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 108 | // of zero pauses the encoder. |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 109 | // `stable_target_bitrate` is a filtered version of `target_bitrate`. It is |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 110 | // always less or equal to it. It can be used to avoid rapid changes of |
| 111 | // expensive encoding settings, such as resolution. |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 112 | // `link_allocation` is the bandwidth available for this video stream on the |
| 113 | // network link. It is always at least `target_bitrate` but may be higher |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 114 | // if we are not network constrained. |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 115 | virtual void OnBitrateUpdated(DataRate target_bitrate, |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 116 | DataRate stable_target_bitrate, |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 117 | DataRate link_allocation, |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 118 | uint8_t fraction_lost, |
Ying Wang | 9b881ab | 2020-02-07 14:29:32 +0100 | [diff] [blame] | 119 | int64_t round_trip_time_ms, |
| 120 | double cwnd_reduce_ratio) = 0; |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 121 | |
Elad Alon | 8f01c4e | 2019-06-28 15:19:43 +0200 | [diff] [blame] | 122 | // Set a FecControllerOverride, through which the encoder may override |
| 123 | // decisions made by FecController. |
Elad Alon | 8f01c4e | 2019-06-28 15:19:43 +0200 | [diff] [blame] | 124 | virtual void SetFecControllerOverride( |
Elad Alon | a63aede | 2019-06-28 18:20:58 +0200 | [diff] [blame] | 125 | FecControllerOverride* fec_controller_override) = 0; |
Elad Alon | 8f01c4e | 2019-06-28 15:19:43 +0200 | [diff] [blame] | 126 | |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 127 | // Creates and configures an encoder with the given `config`. The |
| 128 | // `max_data_payload_length` is used to support single NAL unit |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 129 | // packetization for H.264. |
| 130 | virtual void ConfigureEncoder(VideoEncoderConfig config, |
| 131 | size_t max_data_payload_length) = 0; |
| 132 | |
| 133 | // Permanently stop encoding. After this method has returned, it is |
| 134 | // guaranteed that no encoded frames will be delivered to the sink. |
| 135 | virtual void Stop() = 0; |
| 136 | }; |
| 137 | |
| 138 | } // namespace webrtc |
| 139 | |
| 140 | #endif // API_VIDEO_VIDEO_STREAM_ENCODER_INTERFACE_H_ |