video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2023-12-26 18a05d269516a5e33d8460291c2f93e73d95adce
csrc/wrapper.hpp
@@ -8,118 +8,96 @@
#include <thread>
#include <atomic>
#include <mutex>
#include <unordered_map>
#include <memory>
#include "common/callback.hpp"
class CPacket;
namespace ffwrapper{
    class FormatIn;
    class FormatOut;
    class VideoProp;
    class CodedData;
    class FrameData;
    class cvbridge;
    class VideoProp;
}
enum WORKER{
    WORKER_RECORDER = 0,
    WORKER_DECODER,
    WORKER_ENCODER,
};
namespace cffmpeg_wrap{
    typedef std::function<int(ffwrapper::FormatIn*)> FUNC_WORKER;
    class stream;
    class decoder;
    class rec;
    typedef struct _pic_bgr24{
        unsigned char *data;
        int w;
        int h;
    }pic_bgr24;
    namespace buz{
        class Recorder;
        struct avpacket;
    }
    class Wrapper{
        public:
            Wrapper();
            ~Wrapper ();
    public:
        Wrapper();
        explicit Wrapper(const char *logfile);
        ~Wrapper ();
    private:
        std::unique_ptr<ffwrapper::FormatIn> init_reader(const char* input);
        private:
            std::unique_ptr<ffwrapper::FormatIn> init_reader(const char* input);
            void init_worker(ffwrapper::FormatIn *in);
            int init_recorder(ffwrapper::FormatIn *in, std::string dir, const int mind, const int maxd);
            void run_worker(ffwrapper::FormatIn *in, buz::avpacket &pkt);
        void init_worker(ffwrapper::FormatIn *in);
        int run_worker(ffwrapper::FormatIn *in, const CPacket &pkt);
        void deinit_worker();
    public:
        int RunStream(const char* input);
    private:
        void run_stream_thread();
    public: //recorder
        void BuildRecorder(const char* id, const char *dir, const int64_t &fid, const int mind, const int maxd, const bool audio);
        int FireRecorder(const char* sid,const int64_t &id);
        int GetInfoRecorder(std::string &recID, int &index, std::string &path);
    public:
        void gb28181(){gb_ = 1;}
        void CPUDec(){cpu_ = 1;}
        void AudioSwitch(const bool a);
        void SetRecMinCacheTime(const int mind, const int maxd);
            void cache_rec_info(int &index, std::string &path);
            void cache_pic(std::shared_ptr<ffwrapper::FrameData> &frame);
        public:
            int RunStream(const char* input);
        private:
            void run_stream_thread();
        int GetFPS(){return fps_;}
    public: //decoder
        void OpenDecoder();
        int GetPicDecoder(unsigned char **data, int *w, int *h, int *format, int *length, int64_t *id);
    public: // push stream
        void CloseStream();
        int GetPacket(unsigned char **pktData, int *size, int *key);
    public: // recorder
        void OpenRecorder();
        public: //recorder
            void BuildRecorder(const char *dir, const int mind, const int maxd);
            int FireRecorder(const int64_t &id);
            void GetInfoRecorder(int &index, std::string &path);
    public:
        // set device id
        int SetDevID(const int devid){ devid_ = devid; return 0;}
            // active api
            void ActiveRecorder(const char *dir, const int mind, const int maxd,
                                FUNC_REC func);
            void ScalePicture(const int w, const int h, const int flags);
        public: //decoder
            void BuildDecoder();
            void GetPicDecoder(unsigned char **data, int *w, int *h);
            //active api
            void ActiveDecoder(FUNC_DEC fn);
        public:
            void BuildEncoder(const char *file, const int w, const int h, const int fps, const int br, const int gi);
            int init_encoder(ffwrapper::FormatIn *in, const char *file, const int w, const int h, const int fps, const int br, const int gi);
        private:
            std::string input_url_;
            buz::Recorder  *recorder_;
            std::unique_ptr<std::thread> thread_;
            std::atomic_bool    stop_stream_;
            ffwrapper::cvbridge *bridge_;
            std::unordered_map<int, FUNC_WORKER> map_workers_;
            //passive api
            struct record_file_info{
                int file_frame_index;
                std::string file_path;
            };
            std::list<struct record_file_info>  list_rec_;
            std::mutex mutex_rec_;
            std::list<pic_bgr24> list_pic_;
            std::mutex mutex_pic_;
            // active api
            FUNC_REC func_rec_;
            FUNC_DEC func_dec_;
            int scale_w_, scale_h_, scale_f_;
        //////////////////test frame to bgr24
        public:
            uint8_t *decodeJPEG(const char *file, int *w, int *h);
        private:
            ffwrapper::FormatOut *encoder_;
    private:
        // stream 参数
        std::string input_url_;
        bool audio_;
        int gb_, cpu_, devid_;
        bool run_dec_;
        bool run_stream_;
        bool run_rec_;
        bool work_start;
        // decoder 参数
        std::unique_ptr<std::thread> thread_;
        std::atomic_bool    stop_stream_;
        // 业务类
        // 推流类
        stream* stream_;
        // 解码类
        decoder* decoder_;
        // 录像类,一直存在
        rec* rec_;
        // 录像请求缓存,等待runstream后添加
        std::function<void()> fn_rec_lazy_;
        bool logit_;
        int fps_;
        // 为兼容文件解码做的补丁
        bool file_finished_;
        int min_duration_;
    };
    void *CreateEncoder(const int w, const int h, const int fps, const int br, const int scale_flag, const int gi);
    void *CreateEncoder(const int w, const int h, const int fps, const int br,
                        const int pix_fmt, const int scale_flag, const int gi);
    void DestroyEncoder(void *h);
    int Encode(void *hdl, uint8_t *in, const int w, const int h, uint8_t **out, int *size, int *key);
    int GetGb28181Pic(const char *filename, char *retData, int *retDataLen, const int tt);
}
#endif