| 1 | #ifndef WEBRTC_NVIDIA_H265_ENCODER_IMPL_H_ |
| 2 | #define WEBRTC_NVIDIA_H265_ENCODER_IMPL_H_ |
| 3 | |
| 4 | #include <cuda.h> |
| 5 | |
| 6 | #include <memory> |
| 7 | #include <vector> |
| 8 | |
| 9 | #include "NvEncoder/NvEncoder.h" |
| 10 | #include "NvEncoder/NvEncoderCuda.h" |
| 11 | |
| 12 | #include "absl/container/inlined_vector.h" |
| 13 | #include "api/environment/environment.h" |
| 14 | #include "api/video_codecs/sdp_video_format.h" |
| 15 | #include "api/transport/rtp/dependency_descriptor.h" |
| 16 | #include "api/video/i420_buffer.h" |
| 17 | #include "api/video/video_codec_constants.h" |
| 18 | #include "api/video_codecs/scalability_mode.h" |
| 19 | #include "api/video_codecs/video_encoder.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | class NvidiaH265EncoderImpl : public VideoEncoder { |
| 24 | public: |
| 25 | struct LayerConfig { |
| 26 | int simulcast_idx = 0; |
| 27 | int width = -1; |
| 28 | int height = -1; |
| 29 | bool sending = true; |
| 30 | bool key_frame_request = false; |
| 31 | float max_frame_rate = 0; |
| 32 | uint32_t target_bps = 0; |
| 33 | uint32_t max_bps = 0; |
| 34 | bool frame_dropping_on = false; |
| 35 | int key_frame_interval = 0; |
| 36 | int num_temporal_layers = 1; |
| 37 | |
| 38 | void SetStreamState(bool send_stream); |
| 39 | }; |
| 40 | |
| 41 | public: |
| 42 | NvidiaH265EncoderImpl(const webrtc::Environment& env, |
| 43 | CUcontext context, |
| 44 | CUmemorytype memory_type, |
| 45 | NV_ENC_BUFFER_FORMAT nv_format, |
| 46 | const SdpVideoFormat& format); |
| 47 | ~NvidiaH265EncoderImpl() override; |
| 48 | |
| 49 | int32_t InitEncode(const VideoCodec* codec_settings, |
| 50 | const Settings& settings) override; |
| 51 | |
| 52 | int32_t RegisterEncodeCompleteCallback( |
| 53 | EncodedImageCallback* callback) override; |
| 54 | |
| 55 | int32_t Release() override; |
| 56 | |
| 57 | int32_t Encode(const VideoFrame& frame, |
| 58 | const std::vector<VideoFrameType>* frame_types) override; |
| 59 | |
| 60 | void SetRates(const RateControlParameters& rc_parameters) override; |
| 61 | |
| 62 | EncoderInfo GetEncoderInfo() const override; |
| 63 | |
| 64 | private: |
| 65 | int32_t ProcessEncodedFrame(std::vector<uint8_t>& packet, |
| 66 | const ::webrtc::VideoFrame& inputFrame); |
| 67 | private: |
| 68 | const webrtc::Environment& env_; |
| 69 | EncodedImageCallback* encoded_image_callback_ = nullptr; |
| 70 | |
| 71 | std::unique_ptr<NvEncoder> encoder_; |
| 72 | CUcontext cu_context_; |
| 73 | CUmemorytype cu_memory_type_; |
| 74 | CUarray cu_scaled_array_; |
| 75 | NV_ENC_BUFFER_FORMAT nv_format_; |
| 76 | NV_ENC_INITIALIZE_PARAMS nv_initialize_params_; |
| 77 | NV_ENC_CONFIG nv_encode_config_; |
| 78 | |
| 79 | LayerConfig configuration_; |
| 80 | EncodedImage encoded_image_; |
| 81 | VideoCodec codec_; |
| 82 | void ReportInit(); |
| 83 | void ReportError(); |
| 84 | bool has_reported_init_ = false; |
| 85 | bool has_reported_error_ = false; |
| 86 | const SdpVideoFormat format_; |
| 87 | bool current_encoding_is_keyframe_ = false; |
| 88 | }; |
| 89 | |
| 90 | } // namespace webrtc |
| 91 | |
| 92 | #endif // WEBRTC_NVIDIA_H265_ENCODER_IMPL_H_ |
| 93 | |
| 94 | |