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 | |
Elad Alon | 8f01c4e | 2019-06-28 15:19:43 +0200 | [diff] [blame] | 16 | #include "api/fec_controller_override.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 17 | #include "api/rtp_parameters.h" // For DegradationPreference. |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 18 | #include "api/units/data_rate.h" |
Niels Möller | efd7034 | 2019-01-22 10:35:42 +0100 | [diff] [blame] | 19 | #include "api/video/video_bitrate_allocator.h" |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 20 | #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 | |
| 25 | namespace webrtc { |
| 26 | |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 27 | // 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. |
| 39 | class 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, |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 47 | VideoEncoderConfig::ContentType content_type, |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 48 | int min_transmit_bitrate_bps) = 0; |
| 49 | }; |
| 50 | |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 51 | // Sets the source that will provide video frames to the VideoStreamEncoder's |
| 52 | // OnFrame method. |degradation_preference| control whether or not resolution |
| 53 | // or frame rate may be reduced. The VideoStreamEncoder registers itself with |
| 54 | // |source|, and signals adaptation decisions to the source in the form of |
| 55 | // VideoSinkWants. |
| 56 | // TODO(nisse): When adaptation logic is extracted from this class, |
| 57 | // it no longer needs to know the source. |
| 58 | virtual void SetSource( |
| 59 | rtc::VideoSourceInterface<VideoFrame>* source, |
| 60 | const DegradationPreference& degradation_preference) = 0; |
| 61 | |
| 62 | // Sets the |sink| that gets the encoded frames. |rotation_applied| means |
| 63 | // that the source must support rotation. Only set |rotation_applied| if the |
| 64 | // remote side does not support the rotation extension. |
| 65 | virtual void SetSink(EncoderSink* sink, bool rotation_applied) = 0; |
| 66 | |
| 67 | // Sets an initial bitrate, later overriden by OnBitrateUpdated. Mainly |
| 68 | // affects the resolution of the initial key frame: If incoming frames are |
| 69 | // larger than reasonable for the start bitrate, and scaling is enabled, |
| 70 | // VideoStreamEncoder asks the source to scale down and drops a few initial |
| 71 | // frames. |
| 72 | // TODO(nisse): This is a poor interface, and mixes bandwidth estimation and |
| 73 | // codec configuration in an undesired way. For the actual send bandwidth, we |
| 74 | // should always be somewhat conservative, but we may nevertheless want to let |
| 75 | // the application configure a more optimistic quality for the initial |
| 76 | // resolution. Should be replaced by a construction time setting. |
| 77 | virtual void SetStartBitrate(int start_bitrate_bps) = 0; |
| 78 | |
| 79 | // Request a key frame. Used for signalling from the remote receiver. |
| 80 | virtual void SendKeyFrame() = 0; |
| 81 | |
Elad Alon | b6ef99b | 2019-04-10 16:37:07 +0200 | [diff] [blame] | 82 | // Inform the encoder that a loss has occurred. |
| 83 | virtual void OnLossNotification( |
| 84 | const VideoEncoder::LossNotification& loss_notification) = 0; |
| 85 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 86 | // Set the currently estimated network properties. A |target_bitrate| |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 87 | // of zero pauses the encoder. |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 88 | // |link_allocation| is the bandwidth available for this video stream on the |
| 89 | // network link. It is always at least |target_bitrate| but may be higher |
| 90 | // if we are not network constrained. |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 91 | virtual void OnBitrateUpdated(DataRate target_bitrate, |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 92 | DataRate link_allocation, |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 93 | uint8_t fraction_lost, |
| 94 | int64_t round_trip_time_ms) = 0; |
| 95 | |
| 96 | // Register observer for the bitrate allocation between the temporal |
| 97 | // and spatial layers. |
| 98 | virtual void SetBitrateAllocationObserver( |
| 99 | VideoBitrateAllocationObserver* bitrate_observer) = 0; |
| 100 | |
Elad Alon | 8f01c4e | 2019-06-28 15:19:43 +0200 | [diff] [blame] | 101 | // Set a FecControllerOverride, through which the encoder may override |
| 102 | // decisions made by FecController. |
| 103 | // TODO(bugs.webrtc.org/10769): Update downstream projects and then make this |
| 104 | // pure-virtual. |
| 105 | virtual void SetFecControllerOverride( |
| 106 | FecControllerOverride* fec_controller_override) {} |
| 107 | |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 108 | // Creates and configures an encoder with the given |config|. The |
| 109 | // |max_data_payload_length| is used to support single NAL unit |
| 110 | // packetization for H.264. |
| 111 | virtual void ConfigureEncoder(VideoEncoderConfig config, |
| 112 | size_t max_data_payload_length) = 0; |
| 113 | |
| 114 | // Permanently stop encoding. After this method has returned, it is |
| 115 | // guaranteed that no encoded frames will be delivered to the sink. |
| 116 | virtual void Stop() = 0; |
| 117 | }; |
| 118 | |
| 119 | } // namespace webrtc |
| 120 | |
| 121 | #endif // API_VIDEO_VIDEO_STREAM_ENCODER_INTERFACE_H_ |