streamNode.hpp 3.0 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. #include <chrono>
  7. #include <thread>
  8. // 日志库
  9. #include "plog/Log.h"
  10. #include "plog/Initializers/RollingFileInitializer.h"
  11. namespace GNode
  12. {
  13. enum class DecodeType
  14. {
  15. CPU = 0,
  16. GPU = 1,
  17. FOLDER = 2
  18. };
  19. enum class StreamStatus{
  20. OPENED = 0,
  21. CLOSED = 1,
  22. OPEN_FAILED = 2,
  23. ERROR = 3
  24. };
  25. class StreamNode : public BaseNode
  26. {
  27. public:
  28. StreamNode() = delete;
  29. // Constructor now only initializes members and tries initial open
  30. StreamNode(const std::string& name, const std::string& url, int gpu_id=0, DecodeType type=DecodeType::GPU, int retry_delay_ms = 5000)
  31. : BaseNode(name, NODE_TYPE::SRC_NODE),
  32. stream_url_(url),
  33. gpu_id_(gpu_id),
  34. decode_type_(type),
  35. retry_delay_ms_(retry_delay_ms)
  36. {
  37. PLOGI.printf("StreamNode [%s]: Created for URL: %s (Decode: %s)",
  38. name_.c_str(), stream_url_.c_str(), (decode_type_ == DecodeType::GPU ? "GPU" : "CPU"));
  39. open_stream();
  40. }
  41. virtual ~StreamNode()
  42. {
  43. stop(); // Ensure stop is called (should set running_ to false)
  44. close_stream(); // Clean up resources
  45. PLOGI.printf("StreamNode [%s] destroyed.", name_.c_str());
  46. };
  47. void set_stream_url(const std::string& stream_url)
  48. {
  49. stream_url_ = stream_url;
  50. PLOGI.printf("StreamNode [%s] URL set to: %s", name_.c_str(), stream_url_.c_str());
  51. }
  52. void set_skip_frame(int skip_frame)
  53. {
  54. if (skip_frame < 1)
  55. {
  56. skip_frame = 1;
  57. }
  58. PLOGI.printf("StreamNode [%s] Skip frame set to: %d", name_.c_str(), skip_frame);
  59. skip_frame_ = skip_frame;
  60. }
  61. void set_retry_delay(int delay_ms) {
  62. retry_delay_ms_ = std::max(100, delay_ms); // Ensure a minimum delay
  63. PLOGI.printf("StreamNode [%s] Retry delay set to: %d ms", name_.c_str(), retry_delay_ms_);
  64. }
  65. StreamStatus get_status() const {
  66. return status_;
  67. }
  68. void work() override; // Main loop with reconnection logic
  69. private:
  70. bool open_stream(); // Attempts to open the stream (CPU or GPU)
  71. void close_stream(); // Closes the stream and resets resources
  72. void process_stream_cpu(); // Renamed processing function
  73. void process_stream_gpu(); // Renamed processing function
  74. void process_stream_folder();
  75. std::string stream_url_;
  76. int skip_frame_ = 1;
  77. int frame_count_ = -1; // Reset when stream (re)opens
  78. int gpu_id_ = 0;
  79. int retry_delay_ms_; // Delay between reconnection attempts
  80. int fps_ = 20;
  81. std::shared_ptr<cv::VideoCapture> cap_ = nullptr;
  82. std::shared_ptr<FFHDDemuxer::FFmpegDemuxer> demuxer_ = nullptr;
  83. std::shared_ptr<FFHDDecoder::CUVIDDecoder> decoder_ = nullptr;
  84. DecodeType decode_type_ = DecodeType::GPU;
  85. StreamStatus status_ = StreamStatus::CLOSED; // Initial state before first open attempt
  86. };
  87. } // namespace GNode
  88. #endif // STREAMNODE_HPP__