jetson_nv_encoder.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #ifndef JETSON_VIDEO_ENCODER_H_
  2. #define JETSON_VIDEO_ENCODER_H_
  3. #include <chrono>
  4. #include <memory>
  5. #include <queue>
  6. #include <mutex>
  7. #include <condition_variable>
  8. // Linux
  9. #include <linux/videodev2.h>
  10. // WebRTC
  11. #include <api/video_codecs/video_encoder.h>
  12. #include <common_video/include/bitrate_adjuster.h>
  13. #include <media/base/codec.h>
  14. #include <modules/video_coding/codecs/h264/include/h264.h>
  15. // #include <modules/video_coding/codecs/vp9/include/vp9_globals.h>
  16. // #include <modules/video_coding/svc/scalable_video_controller.h>
  17. #include <rtc_base/synchronization/mutex.h>
  18. // #include "jetson_jpeg_decoder.h"
  19. #define CONVERTER_CAPTURE_NUM 2
  20. class NvBuffer;
  21. class NvV4l2Element;
  22. class NvVideoEncoder;
  23. struct v4l2_ctrl_videoenc_outputbuf_metadata_;
  24. namespace webrtc {
  25. class JetsonVideoEncoder : public webrtc::VideoEncoder {
  26. public:
  27. // explicit JetsonVideoEncoder(const cricket::Codec& codec);
  28. explicit JetsonVideoEncoder(const cricket::VideoCodec& codec);
  29. ~JetsonVideoEncoder() override;
  30. static bool IsSupported(webrtc::VideoCodecType codec);
  31. int32_t InitEncode(const webrtc::VideoCodec* codec_settings,
  32. int32_t number_of_cores,
  33. size_t max_payload_size) override;
  34. int32_t RegisterEncodeCompleteCallback(
  35. webrtc::EncodedImageCallback* callback) override;
  36. int32_t Release() override;
  37. int32_t Encode(
  38. const webrtc::VideoFrame& frame,
  39. const std::vector<webrtc::VideoFrameType>* frame_types) override;
  40. void SetRates(const RateControlParameters& parameters) override;
  41. webrtc::VideoEncoder::EncoderInfo GetEncoderInfo() const override;
  42. private:
  43. struct FrameParams {
  44. FrameParams(int32_t w,
  45. int32_t h,
  46. int64_t rtms,
  47. int64_t ntpms,
  48. int64_t tsus,
  49. int64_t rtpts,
  50. webrtc::VideoRotation r,
  51. // absl::optional<webrtc::ColorSpace> c,
  52. // std::shared_ptr<JetsonJpegDecoder> d)
  53. absl::optional<webrtc::ColorSpace> c)
  54. : width(w),
  55. height(h),
  56. render_time_ms(rtms),
  57. ntp_time_ms(ntpms),
  58. timestamp_us(tsus),
  59. timestamp_rtp(rtpts),
  60. rotation(r),
  61. color_space(c){}
  62. // color_space(c),
  63. // decoder_(d) {}
  64. int32_t width;
  65. int32_t height;
  66. int64_t render_time_ms;
  67. int64_t ntp_time_ms;
  68. int64_t timestamp_us;
  69. int64_t timestamp_rtp;
  70. webrtc::VideoRotation rotation;
  71. absl::optional<webrtc::ColorSpace> color_space;
  72. // std::shared_ptr<JetsonJpegDecoder> decoder_;
  73. };
  74. int32_t JetsonConfigure();
  75. void JetsonRelease();
  76. void SendEOS();
  77. static bool EncodeFinishedCallbackFunction(struct v4l2_buffer* v4l2_buf,
  78. NvBuffer* buffer,
  79. NvBuffer* shared_buffer,
  80. void* data);
  81. bool EncodeFinishedCallback(struct v4l2_buffer* v4l2_buf,
  82. NvBuffer* buffer,
  83. NvBuffer* shared_buffer);
  84. void SetFramerate(uint32_t framerate);
  85. void SetBitrateBps(uint32_t bitrate_bps);
  86. int32_t SendFrame(unsigned char* buffer,
  87. size_t size,
  88. std::unique_ptr<FrameParams> params,
  89. v4l2_ctrl_videoenc_outputbuf_metadata_* enc_metadata);
  90. webrtc::VideoCodec codec_;
  91. webrtc::EncodedImageCallback* callback_;
  92. NvVideoEncoder* encoder_;
  93. std::unique_ptr<webrtc::BitrateAdjuster> bitrate_adjuster_;
  94. uint32_t framerate_;
  95. int32_t configured_framerate_;
  96. uint32_t target_bitrate_bps_;
  97. uint32_t configured_bitrate_bps_;
  98. int key_frame_interval_;
  99. uint32_t decode_pixfmt_;
  100. uint32_t raw_width_;
  101. uint32_t raw_height_;
  102. int32_t width_;
  103. int32_t height_;
  104. bool use_native_;
  105. bool use_dmabuff_;
  106. int dmabuff_fd_[CONVERTER_CAPTURE_NUM];
  107. // webrtc::GofInfoVP9 gof_;
  108. size_t gof_idx_;
  109. // std::unique_ptr<webrtc::ScalableVideoController> svc_controller_; //注意
  110. webrtc::Mutex frame_params_lock_;
  111. std::queue<std::unique_ptr<FrameParams>> frame_params_;
  112. std::mutex enc0_buffer_mtx_;
  113. std::condition_variable enc0_buffer_cond_;
  114. std::queue<NvBuffer*>* enc0_buffer_queue_;
  115. int output_plane_fd_[32];
  116. webrtc::EncodedImage encoded_image_;
  117. // webrtc::ScalabilityMode scalability_mode_;
  118. std::vector<uint8_t> obu_seq_header_;
  119. };
  120. }
  121. #endif //JETSON_VIDEO_ENCODER_H_