| 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 |
| 12 | #ifndef TGCALLS_CUSTOM_SIMULCAST_ENCODER_ADAPTER_H_ |
| 13 | #define TGCALLS_CUSTOM_SIMULCAST_ENCODER_ADAPTER_H_ |
| 14 |
| 15 | #include <atomic> |
| 16 | #include <list> |
| 17 | #include <memory> |
| 18 | #include <stack> |
| 19 | #include <string> |
| 20 | #include <utility> |
| 21 | #include <vector> |
| 22 |
| 23 | #include "absl/types/optional.h" |
| 24 | #include "api/fec_controller_override.h" |
| 25 | #include "api/sequence_checker.h" |
| 26 | #include "api/video_codecs/sdp_video_format.h" |
| 27 | #include "api/video_codecs/video_encoder.h" |
| 28 | #include "api/video_codecs/video_encoder_factory.h" |
| 29 | #include "common_video/framerate_controller.h" |
| 30 | #include "modules/video_coding/include/video_codec_interface.h" |
| 31 | #include "rtc_base/experiments/encoder_info_settings.h" |
| 32 | #include "rtc_base/system/no_unique_address.h" |
| 33 | #include "rtc_base/system/rtc_export.h" |
| 34 | #include "api/field_trials_view.h" |
| 35 |
| 36 | namespace webrtc { |
| 37 |
| 38 | // CustomSimulcastEncoderAdapter implements simulcast support by creating multiple |
| 39 | // webrtc::VideoEncoder instances with the given VideoEncoderFactory. |
| 40 | // The object is created and destroyed on the worker thread, but all public |
| 41 | // interfaces should be called from the encoder task queue. |
| 42 | class RTC_EXPORT CustomSimulcastEncoderAdapter : public VideoEncoder { |
| 43 | public: |
| 44 | // TODO(bugs.webrtc.org/11000): Remove when downstream usage is gone. |
| 45 | CustomSimulcastEncoderAdapter(VideoEncoderFactory* primarty_factory, |
| 46 | const SdpVideoFormat& format); |
| 47 | // `primary_factory` produces the first-choice encoders to use. |
| 48 | // `fallback_factory`, if non-null, is used to create fallback encoder that |
| 49 | // will be used if InitEncode() fails for the primary encoder. |
| 50 | CustomSimulcastEncoderAdapter(VideoEncoderFactory* primary_factory, |
| 51 | VideoEncoderFactory* hardware_factory, |
| 52 | const SdpVideoFormat& format, |
| 53 | const FieldTrialsView& field_trials); |
| 54 | ~CustomSimulcastEncoderAdapter() override; |
| 55 |
| 56 | // Implements VideoEncoder. |
| 57 | void SetFecControllerOverride( |
| 58 | FecControllerOverride* fec_controller_override) override; |
| 59 | int Release() override; |
| 60 | int InitEncode(const VideoCodec* codec_settings, |
| 61 | const VideoEncoder::Settings& settings) override; |
| 62 | int Encode(const VideoFrame& input_image, |
| 63 | const std::vector<VideoFrameType>* frame_types) override; |
| 64 | int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override; |
| 65 | void SetRates(const RateControlParameters& parameters) override; |
| 66 | void OnPacketLossRateUpdate(float packet_loss_rate) override; |
| 67 | void OnRttUpdate(int64_t rtt_ms) override; |
| 68 | void OnLossNotification(const LossNotification& loss_notification) override; |
| 69 |
| 70 | EncoderInfo GetEncoderInfo() const override; |
| 71 |
| 72 | private: |
| 73 | class EncoderContext { |
| 74 | public: |
| 75 | EncoderContext(std::unique_ptr<VideoEncoder> encoder, |
| 76 | bool prefer_temporal_support, |
| 77 | VideoEncoder::EncoderInfo primary_info, |
| 78 | VideoEncoder::EncoderInfo fallback_info); |
| 79 | EncoderContext& operator=(EncoderContext&&) = delete; |
| 80 |
| 81 | VideoEncoder& encoder() { return *encoder_; } |
| 82 | bool prefer_temporal_support() { return prefer_temporal_support_; } |
| 83 | void Release(); |
| 84 |
| 85 | const VideoEncoder::EncoderInfo& PrimaryInfo() { return primary_info_; } |
| 86 |
| 87 | const VideoEncoder::EncoderInfo& FallbackInfo() { return fallback_info_; } |
| 88 |
| 89 | private: |
| 90 | std::unique_ptr<VideoEncoder> encoder_; |
| 91 | bool prefer_temporal_support_; |
| 92 | const VideoEncoder::EncoderInfo primary_info_; |
| 93 | const VideoEncoder::EncoderInfo fallback_info_; |
| 94 | }; |
| 95 |
| 96 | class StreamContext : public EncodedImageCallback { |
| 97 | public: |
| 98 | StreamContext(CustomSimulcastEncoderAdapter* parent, |
| 99 | std::unique_ptr<EncoderContext> encoder_context, |
| 100 | std::unique_ptr<FramerateController> framerate_controller, |
| 101 | int stream_idx, |
| 102 | uint16_t width, |
| 103 | uint16_t height, |
| 104 | bool send_stream); |
| 105 | StreamContext(StreamContext&& rhs); |
| 106 | StreamContext& operator=(StreamContext&&) = delete; |
| 107 | ~StreamContext() override; |
| 108 |
| 109 | Result OnEncodedImage( |
| 110 | const EncodedImage& encoded_image, |
| 111 | const CodecSpecificInfo* codec_specific_info) override; |
| 112 | void OnDroppedFrame(DropReason reason) override; |
| 113 |
| 114 | VideoEncoder& encoder() { return encoder_context_->encoder(); } |
| 115 | const VideoEncoder& encoder() const { return encoder_context_->encoder(); } |
| 116 | int stream_idx() const { return stream_idx_; } |
| 117 | uint16_t width() const { return width_; } |
| 118 | uint16_t height() const { return height_; } |
| 119 | bool is_keyframe_needed() const { |
| 120 | return !is_paused_ && is_keyframe_needed_; |
| 121 | } |
| 122 | void set_is_keyframe_needed() { is_keyframe_needed_ = true; } |
| 123 | bool is_paused() const { return is_paused_; } |
| 124 | void set_is_paused(bool is_paused) { is_paused_ = is_paused; } |
| 125 | absl::optional<double> target_fps() const { |
| 126 | return framerate_controller_ == nullptr |
| 127 | ? absl::nullopt |
| 128 | : absl::optional<double>( |
| 129 | framerate_controller_->GetMaxFramerate()); |
| 130 | } |
| 131 |
| 132 | std::unique_ptr<EncoderContext> ReleaseEncoderContext() &&; |
| 133 | void OnKeyframe(Timestamp timestamp); |
| 134 | bool ShouldDropFrame(Timestamp timestamp); |
| 135 |
| 136 | private: |
| 137 | CustomSimulcastEncoderAdapter* const parent_; |
| 138 | std::unique_ptr<EncoderContext> encoder_context_; |
| 139 | std::unique_ptr<FramerateController> framerate_controller_; |
| 140 | const int stream_idx_; |
| 141 | const uint16_t width_; |
| 142 | const uint16_t height_; |
| 143 | bool is_keyframe_needed_; |
| 144 | bool is_paused_; |
| 145 | }; |
| 146 |
| 147 | bool Initialized() const; |
| 148 |
| 149 | void DestroyStoredEncoders(); |
| 150 |
| 151 | // This method creates encoder. May reuse previously created encoders from |
| 152 | // `cached_encoder_contexts_`. It's const because it's used from |
| 153 | // const GetEncoderInfo(). |
| 154 | std::unique_ptr<EncoderContext> FetchOrCreateEncoderContext( |
| 155 | bool is_lowest_quality_stream, bool is_highest_quality_stream) const; |
| 156 |
| 157 | webrtc::VideoCodec MakeStreamCodec(const webrtc::VideoCodec& codec, |
| 158 | int stream_idx, |
| 159 | uint32_t start_bitrate_kbps, |
| 160 | bool is_lowest_quality_stream, |
| 161 | bool is_highest_quality_stream); |
| 162 |
| 163 | EncodedImageCallback::Result OnEncodedImage( |
| 164 | size_t stream_idx, |
| 165 | const EncodedImage& encoded_image, |
| 166 | const CodecSpecificInfo* codec_specific_info); |
| 167 |
| 168 | void OnDroppedFrame(size_t stream_idx); |
| 169 |
| 170 | void OverrideFromFieldTrial(VideoEncoder::EncoderInfo* info) const; |
| 171 |
| 172 | std::atomic<int> inited_; |
| 173 | VideoEncoderFactory* const primary_encoder_factory_; |
| 174 | VideoEncoderFactory* const hardware_encoder_factory_; |
| 175 | const SdpVideoFormat video_format_; |
| 176 | VideoCodec codec_; |
| 177 | int total_streams_count_; |
| 178 | bool bypass_mode_; |
| 179 | std::vector<StreamContext> stream_contexts_; |
| 180 | EncodedImageCallback* encoded_complete_callback_; |
| 181 |
| 182 | // Used for checking the single-threaded access of the encoder interface. |
| 183 | RTC_NO_UNIQUE_ADDRESS SequenceChecker encoder_queue_; |
| 184 |
| 185 | // Store previously created and released encoders , so they don't have to be |
| 186 | // recreated. Remaining encoders are destroyed by the destructor. |
| 187 | // Marked as `mutable` becuase we may need to temporarily create encoder in |
| 188 | // GetEncoderInfo(), which is const. |
| 189 | mutable std::list<std::unique_ptr<EncoderContext>> cached_encoder_contexts_; |
| 190 |
| 191 | const absl::optional<unsigned int> experimental_boosted_screenshare_qp_; |
| 192 | const bool boost_base_layer_quality_; |
| 193 | const bool prefer_temporal_support_on_base_layer_; |
| 194 |
| 195 | const SimulcastEncoderAdapterEncoderInfoSettings encoder_info_override_; |
| 196 | }; |
| 197 |
| 198 | } // namespace webrtc |
| 199 |
| 200 | #endif // TGCALLS_CUSTOM_SIMULCAST_ENCODER_ADAPTER_H_ |
| 201 |