#pragma once #include #include #include #include #include #include #include #include // independent of QT, needs ffmpeg extern "C" { #include #include #include } class FrameReader { public: FrameReader(const std::string &url, int timeout_sec = 0); ~FrameReader(); bool process(); uint8_t *get(int idx); int getRGBSize() const { return width * height * 3; } size_t getFrameCount() const { return frames_.size(); } bool valid() const { return valid_; } int width = 0, height = 0; private: void decodeThread(); uint8_t *decodeFrame(AVPacket *pkt); static int check_interrupt(void *p); struct Frame { AVPacket pkt = {}; uint8_t *data = nullptr; bool failed = false; }; std::vector frames_; AVFormatContext *pFormatCtx_ = nullptr; AVCodecContext *pCodecCtx_ = nullptr; AVFrame *frmRgb_ = nullptr; std::queue buffer_pool; struct SwsContext *sws_ctx_ = nullptr; std::mutex mutex_; std::condition_variable cv_decode_; std::condition_variable cv_frame_; int decode_idx_ = 0; std::atomic exit_ = false; bool valid_ = false; std::string url_; std::thread decode_thread_; int timeout_ = 0; double timeout_ms_ = 0; };