| 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 | const RTPFragmentationHeader* fragmentation); |
| 75 |
| 76 | EncoderInfo GetEncoderInfo() const override; |
| 77 |
| 78 | private: |
| 79 | struct StreamInfo { |
| 80 | StreamInfo(std::unique_ptr<VideoEncoder> encoder, |
| 81 | std::unique_ptr<EncodedImageCallback> callback, |
| 82 | std::unique_ptr<FramerateController> framerate_controller, |
| 83 | uint16_t width, |
| 84 | uint16_t height, |
| 85 | bool send_stream) |
| 86 | : encoder(std::move(encoder)), |
| 87 | callback(std::move(callback)), |
| 88 | framerate_controller(std::move(framerate_controller)), |
| 89 | width(width), |
| 90 | height(height), |
| 91 | key_frame_request(false), |
| 92 | send_stream(send_stream) {} |
| 93 | std::unique_ptr<VideoEncoder> encoder; |
| 94 | std::unique_ptr<EncodedImageCallback> callback; |
| 95 | std::unique_ptr<FramerateController> framerate_controller; |
| 96 | uint16_t width; |
| 97 | uint16_t height; |
| 98 | bool key_frame_request; |
| 99 | bool send_stream; |
| 100 | }; |
| 101 |
| 102 | enum class StreamResolution { |
| 103 | OTHER, |
| 104 | HIGHEST, |
| 105 | LOWEST, |
| 106 | }; |
| 107 |
| 108 | // Populate the codec settings for each simulcast stream. |
| 109 | void PopulateStreamCodec(const webrtc::VideoCodec& inst, |
| 110 | int stream_index, |
| 111 | uint32_t start_bitrate_kbps, |
| 112 | StreamResolution stream_resolution, |
| 113 | webrtc::VideoCodec* stream_codec); |
| 114 |
| 115 | bool Initialized() const; |
| 116 |
| 117 | void DestroyStoredEncoders(); |
| 118 |
| 119 | volatile int inited_; // Accessed atomically. |
| 120 | VideoEncoderFactory* const primary_encoder_factory_; |
| 121 | VideoEncoderFactory* const fallback_encoder_factory_; |
| 122 | const SdpVideoFormat video_format_; |
| 123 | VideoCodec codec_; |
| 124 | std::vector<StreamInfo> streaminfos_; |
| 125 | EncodedImageCallback* encoded_complete_callback_; |
| 126 |
| 127 | // Used for checking the single-threaded access of the encoder interface. |
| 128 | SequenceChecker encoder_queue_; |
| 129 |
| 130 | // Store encoders in between calls to Release and InitEncode, so they don't |
| 131 | // have to be recreated. Remaining encoders are destroyed by the destructor. |
| 132 | std::stack<std::unique_ptr<VideoEncoder>> stored_encoders_; |
| 133 |
| 134 | const absl::optional<unsigned int> experimental_boosted_screenshare_qp_; |
| 135 | const bool boost_base_layer_quality_; |
| 136 | const bool prefer_temporal_support_on_base_layer_; |
| 137 | }; |
| 138 |
| 139 | } // namespace webrtc |
| 140 |
| 141 | #endif // MEDIA_ENGINE_SIMULCAST_ENCODER_ADAPTER_H_ |
| 142 |