#pragma once #include #include #include #include #include #include #include "frame_queue.hpp" namespace pipeline { class VideoReader { public: struct Config { int buffer_size; // 缓冲区大小 float target_fps; // 目标帧率(0表示使用视频原始帧率) int frame_timeout_ms; // 读取帧超时时间(毫秒) bool loop_playback; // 是否循环播放 bool async_reading; // 是否使用异步读取 Config() : buffer_size(30) , target_fps(0.0f) , frame_timeout_ms(5000) , loop_playback(false) , async_reading(true) // 默认使用异步读取 {} }; explicit VideoReader(const Config& config = Config{}); ~VideoReader(); // 禁用拷贝 VideoReader(const VideoReader&) = delete; VideoReader& operator=(const VideoReader&) = delete; // 打开视频文件 bool open(const std::string& filepath); // 关闭视频文件 void close(); // 读取一帧(带超时和帧率控制) bool read(cv::Mat& frame); // 获取视频信息 bool isOpened() const { return is_opened_; } std::string getFilePath() const { return filepath_; } float getCurrentFps() const { return current_fps_; } double getOriginalFps() const { return original_fps_; } int getTotalFrames() const { return total_frames_; } int getCurrentFrame() const { return current_frame_; } // 视频控制 bool seek(int frame_number); void restart(); // 帧队列状态 bool hasFrames() const; size_t getQueueSize() const; size_t getQueueCapacity() const; private: cv::VideoCapture cap_; // OpenCV视频捕获器 std::string filepath_; // 视频文件路径 std::atomic is_opened_{false}; // 打开状态 Config config_; // 配置参数 // 视频信息 double original_fps_{0.0}; // 原始帧率 int total_frames_{0}; // 总帧数 int current_frame_{0}; // 当前帧号 float current_fps_{0.0f}; // 当前实际帧率 // 帧率控制相关 std::chrono::steady_clock::time_point last_frame_time_; // 上一帧时间 void updateFrameRate(); // 更新帧率 void controlFrameRate(); // 控制帧率 // 视频格式检查 bool checkVideoFormat(); // 检查视频格式是否支持 // 异步读取相关 std::unique_ptr frame_queue_; // 帧队列 std::unique_ptr read_thread_; // 读取线程 std::atomic stop_flag_{false}; // 停止标志 void readLoop(); // 读取循环 bool readNextFrame(cv::Mat& frame); // 读取下一帧 }; } // namespace pipeline