video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-10-19 87fea24e8bba1bfbee707cdaa6f7979451531acc
add interface rec duration
9个文件已修改
57 ■■■■ 已修改文件
cffmpeg.h 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/cffmpeg.cpp 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/worker/rec.cpp 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/worker/rec.hpp 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/wrapper.cpp 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/wrapper.hpp 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
gorec.go 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
libcffmpeg.c 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
libcffmpeg.h 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
cffmpeg.h
@@ -18,6 +18,7 @@
void c_ffmpeg_run_gb28181(const cffmpeg h);
void c_ffmepg_use_cpu(const cffmpeg h);
/////////passive api
void c_ffmpeg_set_record_duration(const cffmpeg h, const int min, const int max);
void c_ffmpeg_build_recorder(const cffmpeg h, const char*id, const char *dir, int mind, int maxd, int audio);
void c_ffmpeg_fire_recorder(const cffmpeg h, const char*sid, const int64_t id);
void c_ffmpeg_get_info_recorder(const cffmpeg h, int *index, char** recid, int *recidLen, char **fpath, int *pathLen);
csrc/cffmpeg.cpp
@@ -45,6 +45,11 @@
//////passive api
void c_ffmpeg_set_record_duration(const cffmpeg h, const int min, const int max){
    Wrapper *s = (Wrapper*)h;
    s->SetRecMinCacheTime(min);
}
void c_ffmpeg_build_recorder(const cffmpeg h, const char* id, const char *dir, int mind, int maxd, int audio){
    Wrapper *s = (Wrapper*)h;
csrc/worker/rec.cpp
@@ -20,8 +20,8 @@
{
    rec::rec()
    :recRef_(NULL)
    ,minduration_(250)
    ,maxduration_(750)
    ,min_cache_len_(125)
    ,time_offset_(4)
    {}
    rec::~rec()
@@ -141,9 +141,6 @@
        std::string rid(id);
        std::string dir(output);
        
        minduration_ = mindur * recRef_->getFPS();
        maxduration_ = maxdur * recRef_->getFPS();
        {
            std::lock_guard<std::mutex> l(mtx_rec_);
            if (map_rec_.find(rid) != map_rec_.end()){
@@ -197,10 +194,15 @@
        shrinkCache();
    }
    void rec::SetRecMinCacheTime(const int min){
        // 由于整个流程耗时,补偿time_offset_,因为是最小的一半,只有time_offset_/2
        min_cache_len_ = (min + time_offset_) * recRef_->getFPS();
    }
    int rec::shrinkCache(){
        //超过最大缓存,丢弃gop
        //缓存最小长度的,用于记录
        int md = minduration_ < 201 ? 200 : minduration_;
        int md = min_cache_len_ < 200 ? 200 : min_cache_len_;
        while (list_pkt_.size() > md/2) {
            list_pkt_.pop_front();
            while(!list_pkt_.empty()){
csrc/worker/rec.hpp
@@ -23,9 +23,9 @@
    {
    private:
        ffwrapper::FormatIn *recRef_;
        int     maxduration_;
        int     minduration_;
        int min_cache_len_;
        // 整个流程耗时补偿录制时间,2s默认
        const int time_offset_;
        // 录像的实例,对应任务
        std::unordered_map<std::string, std::unique_ptr<buz::Recorder> > map_rec_;
        // 多线程添加任务实例,在读流线程使用录像,但是添加在另一个线程
@@ -71,6 +71,8 @@
        // 获取录像文件路径和帧id
        void GetRecInfo(std::string &recID, int &index, std::string &path);
        
        // 设置录制时长作为缓存
        void SetRecMinCacheTime(const int min);
    public:
        rec();
        ~rec();
csrc/wrapper.cpp
@@ -225,6 +225,10 @@
        }
    }
    void Wrapper::SetRecMinCacheTime(const int mind){
        rec_->SetRecMinCacheTime(mind);
    }
    void Wrapper::BuildRecorder(const char* id, const char *output, const int mindur, const int maxdur, const bool audio){
        bool a = audio;
        if (gb_) a = false;
csrc/wrapper.hpp
@@ -44,10 +44,12 @@
        void BuildRecorder(const char* id,const char *dir, const int mind, const int maxd, const bool audio);
        int FireRecorder(const char* sid,const int64_t &id);
        void 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);
    public: //decoder
        void BuildDecoder();
        void GetPicDecoder(unsigned char **data, int *w, int *h, int *format, int *length, int64_t *id);
gorec.go
@@ -52,3 +52,8 @@
    return gID, int(i), path
}
// SetRecDurationForCache cache
func (h *GoFFMPEG) SetRecDurationForCache(min, max int) {
    C.wrap_fn_rec_duration(h.ffmpeg, C.int(min), C.int(max))
}
libcffmpeg.c
@@ -21,26 +21,35 @@
        release_if_err(fn_create, lib);
        fn_create2 = (lib_cffmpeg_create)dlsym(lib, "c_ffmpeg_create2");
        release_if_err(fn_create2, lib);
        fn_destroy = (lib_cffmpeg_destroy)dlsym(lib, "c_ffmpeg_destroy");
        release_if_err(fn_destroy, lib);
        fn_run = (lib_cffmpeg_run)dlsym(lib, "c_ffmpeg_run");
        release_if_err(fn_run, lib);
        fn_gb28181 = (lib_cffmpeg_gb28181)dlsym(lib, "c_ffmpeg_run_gb28181");
        release_if_err(fn_gb28181, lib);
        fn_cpu = (lib_cffmpeg_cpu)dlsym(lib, "c_ffmepg_use_cpu");
        release_if_err(fn_cpu, lib);
        fn_rec_duration = (lib_cffmpeg_rec_duration)dlsym(lib, "c_ffmpeg_set_record_duration");
        release_if_err(fn_rec_duration, lib);
        fn_recorder = (lib_cffmpeg_recorder)dlsym(lib, "c_ffmpeg_build_recorder");
        release_if_err(fn_recorder, lib);
        fn_fire_recorder = (lib_cffmpeg_fire_recorder)dlsym(lib, "c_ffmpeg_fire_recorder");
        release_if_err(fn_fire_recorder, lib);
        fn_info_recorder = (lib_cffmpeg_info_recorder)dlsym(lib, "c_ffmpeg_get_info_recorder");
        release_if_err(fn_info_recorder, lib);
        fn_decoder = (lib_cffmpeg_decoder)dlsym(lib, "c_ffmpeg_build_decoder");
        release_if_err(fn_decoder, lib);
        fn_decoder_pic = (lib_cffmpeg_pic)dlsym(lib, "c_ffmpeg_get_pic_decoder");
        release_if_err(fn_decoder_pic, lib);
        fn_get_avpacket = (lib_cffmpeg_avpacket)dlsym(lib, "c_ffmpeg_get_avpacket");
        release_if_err(fn_get_avpacket, lib);
        fn_decode = (lib_cffmpeg_decode)dlsym(lib, "c_ffmpeg_decode");
        release_if_err(fn_decode, lib);
@@ -101,6 +110,10 @@
    fn_recorder(h, id, dir, mind, maxd, audio);
}
void wrap_fn_rec_duration(const cffmpeg h, const int min, const int max){
    fn_rec_duration(h, min, max);
}
void wrap_fn_fire_recorder(const cffmpeg h, const char* sid, const int64_t id){
    fn_fire_recorder(h, sid, id);
}
libcffmpeg.h
@@ -16,6 +16,7 @@
typedef void (*lib_cffmpeg_run)(const cffmpeg, const char*);
typedef void (*lib_cffmpeg_gb28181)(const cffmpeg);
typedef void (*lib_cffmpeg_cpu)(const cffmpeg);
typedef void (*lib_cffmpeg_rec_duration)(const cffmpeg, const int, const int);
typedef void (*lib_cffmpeg_recorder)(const cffmpeg, const char*, const char*, int, int, int);
typedef void (*lib_cffmpeg_fire_recorder)(const cffmpeg, const char*, const int64_t);
typedef void (*lib_cffmpeg_info_recorder)(const cffmpeg, int*, char**, int*, char**, int*);
@@ -31,6 +32,7 @@
static lib_cffmpeg_run                 fn_run = NULL;
static lib_cffmpeg_gb28181             fn_gb28181 = NULL;
static lib_cffmpeg_cpu                 fn_cpu = NULL;
static lib_cffmpeg_rec_duration        fn_rec_duration = NULL;
static lib_cffmpeg_recorder            fn_recorder = NULL;
static lib_cffmpeg_fire_recorder       fn_fire_recorder = NULL;
static lib_cffmpeg_info_recorder       fn_info_recorder = NULL;
@@ -50,6 +52,7 @@
void wrap_fn_run(const cffmpeg h, const char* input);
void wrap_fn_run_gb28181(const cffmpeg h);
void wrap_fn_use_cpu(const cffmpeg h);
void wrap_fn_rec_duration(const cffmpeg h, const int min, const int max);
void wrap_fn_recorder(const cffmpeg h, const char* id, const char* dir, int mind, int maxd, int audio);
void wrap_fn_fire_recorder(const cffmpeg h, const char *sid, const int64_t id);
void wrap_fn_info_recorder(const cffmpeg, int* index, char** recid, int* recidLen, char** fpath, int* pathLen);