1 /* 2 * Copyright (c) 2014 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 12 #ifndef MEDIA_ENGINE_SIMULCAST_ENCODER_ADAPTER_H_ 13 #define MEDIA_ENGINE_SIMULCAST_ENCODER_ADAPTER_H_ 14 15 #include <memory> 16 #include <stack> 17 #include <string> 18 #include <utility> 19 #include <vector> 20 21 #include "absl/types/optional.h" 22 #include "api/fec_controller_override.h" 23 #include "api/video_codecs/sdp_video_format.h" 24 #include "api/video_codecs/video_encoder.h" 25 #include "modules/video_coding/include/video_codec_interface.h" 26 #include "modules/video_coding/utility/framerate_controller.h" 27 #include "rtc_base/atomic_ops.h" 28 #include "rtc_base/synchronization/sequence_checker.h" 29 #include "rtc_base/system/rtc_export.h" 30 31 namespace webrtc { 32 33 class SimulcastRateAllocator; 34 class VideoEncoderFactory; 35 36 // SimulcastEncoderAdapter implements simulcast support by creating multiple 37 // webrtc::VideoEncoder instances with the given VideoEncoderFactory. 38 // The object is created and destroyed on the worker thread, but all public 39 // interfaces should be called from the encoder task queue. 40 class RTC_EXPORT SimulcastEncoderAdapter : public VideoEncoder { 41 public: 42 // TODO(bugs.webrtc.org/11000): Remove when downstream usage is gone. 43 SimulcastEncoderAdapter(VideoEncoderFactory* primarty_factory, 44 const SdpVideoFormat& format); 45 // |primary_factory| produces the first-choice encoders to use. 46 // |fallback_factory|, if non-null, is used to create fallback encoder that 47 // will be used if InitEncode() fails for the primary encoder. 48 SimulcastEncoderAdapter(VideoEncoderFactory* primary_factory, 49 VideoEncoderFactory* fallback_factory, 50 const SdpVideoFormat& format); 51 ~SimulcastEncoderAdapter() override; 52 53 // Implements VideoEncoder. 54 void SetFecControllerOverride( 55 FecControllerOverride* fec_controller_override) override; 56 int Release() override; 57 int InitEncode(const VideoCodec* codec_settings, 58 const VideoEncoder::Settings& settings) override; 59 int Encode(const VideoFrame& input_image, 60 const std::vector<VideoFrameType>* frame_types) override; 61 int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override; 62 void SetRates(const RateControlParameters& parameters) override; 63 void OnPacketLossRateUpdate(float packet_loss_rate) override; 64 void OnRttUpdate(int64_t rtt_ms) override; 65 void OnLossNotification(const LossNotification& loss_notification) override; 66 67 // Eventual handler for the contained encoders' EncodedImageCallbacks, but 68 // called from an internal helper that also knows the correct stream 69 // index. 70 EncodedImageCallback::Result OnEncodedImage( 71 size_t stream_idx, 72 const EncodedImage& encoded_image, 73 const CodecSpecificInfo* codec_specific_info); 74 75 EncoderInfo GetEncoderInfo() const override; 76 77 private: 78 struct StreamInfo { StreamInfoStreamInfo79 StreamInfo(std::unique_ptr<VideoEncoder> encoder, 80 std::unique_ptr<EncodedImageCallback> callback, 81 std::unique_ptr<FramerateController> framerate_controller, 82 uint16_t width, 83 uint16_t height, 84 bool send_stream) 85 : encoder(std::move(encoder)), 86 callback(std::move(callback)), 87 framerate_controller(std::move(framerate_controller)), 88 width(width), 89 height(height), 90 key_frame_request(false), 91 send_stream(send_stream) {} 92 std::unique_ptr<VideoEncoder> encoder; 93 std::unique_ptr<EncodedImageCallback> callback; 94 std::unique_ptr<FramerateController> framerate_controller; 95 uint16_t width; 96 uint16_t height; 97 bool key_frame_request; 98 bool send_stream; 99 }; 100 101 enum class StreamResolution { 102 OTHER, 103 HIGHEST, 104 LOWEST, 105 }; 106 107 // Populate the codec settings for each simulcast stream. 108 void PopulateStreamCodec(const webrtc::VideoCodec& inst, 109 int stream_index, 110 uint32_t start_bitrate_kbps, 111 StreamResolution stream_resolution, 112 webrtc::VideoCodec* stream_codec); 113 114 bool Initialized() const; 115 116 void DestroyStoredEncoders(); 117 118 volatile int inited_; // Accessed atomically. 119 VideoEncoderFactory* const primary_encoder_factory_; 120 VideoEncoderFactory* const fallback_encoder_factory_; 121 const SdpVideoFormat video_format_; 122 VideoCodec codec_; 123 std::vector<StreamInfo> streaminfos_; 124 EncodedImageCallback* encoded_complete_callback_; 125 126 // Used for checking the single-threaded access of the encoder interface. 127 SequenceChecker encoder_queue_; 128 129 // Store encoders in between calls to Release and InitEncode, so they don't 130 // have to be recreated. Remaining encoders are destroyed by the destructor. 131 std::stack<std::unique_ptr<VideoEncoder>> stored_encoders_; 132 133 const absl::optional<unsigned int> experimental_boosted_screenshare_qp_; 134 const bool boost_base_layer_quality_; 135 const bool prefer_temporal_support_on_base_layer_; 136 }; 137 138 } // namespace webrtc 139 140 #endif // MEDIA_ENGINE_SIMULCAST_ENCODER_ADAPTER_H_ 141