streamNode.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #ifndef STREAMNODE_HPP__
  2. #define STREAMNODE_HPP__
  3. #include "nodes/base/base.hpp"
  4. #include "opencv2/opencv.hpp"
  5. #include "stream/stream.hpp"
  6. namespace Node
  7. {
  8. enum class DecodeType
  9. {
  10. CPU = 0,
  11. GPU = 1
  12. };
  13. enum class StreamStatus{
  14. OPENED = 0,
  15. CLOSED = 1,
  16. OPEN_FAILED = 2
  17. };
  18. class StreamNode : public BaseNode
  19. {
  20. public:
  21. StreamNode() = delete;
  22. StreamNode(const std::string& name) : BaseNode(name, NODE_TYPE::SRC_NODE) {}
  23. StreamNode(const std::string& name, const std::string& url, int gpu_id=0, DecodeType type=DecodeType::GPU)
  24. : BaseNode(name, NODE_TYPE::SRC_NODE), stream_url_(url), gpu_id_(gpu_id), decode_type_(type)
  25. {
  26. if (decode_type_ == DecodeType::GPU)
  27. {
  28. demuxer_ = FFHDDemuxer::create_ffmpeg_demuxer(stream_url_);
  29. if (demuxer_ == nullptr)
  30. {
  31. printf("demuxer create failed\n");
  32. status_ = StreamStatus::OPEN_FAILED;
  33. return;
  34. }
  35. decoder_ = FFHDDecoder::create_cuvid_decoder(
  36. false, FFHDDecoder::ffmpeg2NvCodecId(demuxer_->get_video_codec()), -1, gpu_id, nullptr, nullptr, true
  37. );
  38. if (decoder_ == nullptr)
  39. {
  40. printf("decoder create failed\n");
  41. status_ = StreamStatus::OPEN_FAILED;
  42. return;
  43. }
  44. status_ = StreamStatus::OPENED;
  45. }
  46. else
  47. {
  48. cap_ = std::make_shared<cv::VideoCapture>(stream_url_);
  49. if (!cap_->isOpened())
  50. {
  51. printf("cap open failed\n");
  52. status_ = StreamStatus::OPEN_FAILED;
  53. return;
  54. }
  55. status_ = StreamStatus::OPENED;
  56. }
  57. }
  58. virtual ~StreamNode()
  59. {
  60. if (demuxer_)
  61. {
  62. printf("error : demuxer 未被工作线程释放\n");
  63. }
  64. };
  65. void set_stream_url(const std::string& stream_url)
  66. {
  67. stream_url_ = stream_url;
  68. }
  69. void set_skip_frame(int skip_frame)
  70. {
  71. if (skip_frame < 1)
  72. {
  73. skip_frame = 1;
  74. }
  75. skip_frame_ = skip_frame;
  76. }
  77. void work() override;
  78. void work_cpu();
  79. void work_gpu();
  80. private:
  81. std::string stream_url_;
  82. int skip_frame_ = 1;
  83. int frame_count_ = -1;
  84. int gpu_id_ = 0;
  85. std::shared_ptr<cv::VideoCapture> cap_ = nullptr;
  86. std::shared_ptr<FFHDDemuxer::FFmpegDemuxer> demuxer_ = nullptr;
  87. std::shared_ptr<FFHDDecoder::CUVIDDecoder> decoder_ = nullptr;
  88. DecodeType decode_type_ = DecodeType::GPU;
  89. StreamStatus status_ = StreamStatus::CLOSED;
  90. };
  91. }
  92. #endif // STREAMNODE_HPP__