video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-07-30 a6f5c0ca80b2fdd53351d5957a659a939f530fc2
csrc/wrapper.cpp
@@ -39,6 +39,8 @@
    ,gb_(0)
    ,cpu_(0)
    ,use_decoder_(false)
    ,minduration(250)
    ,maxduration(750)
    {
        makeTheWorld();
    }
@@ -143,6 +145,10 @@
                run_worker(in.get(), pkt);
                if(!data){
                    map_rec_.clear();
                    std::lock_guard<std::mutex> locker(mtx_rec_pkt_);
                    list_rec_pkt_.clear();
                    break;
                }
                //test
@@ -180,19 +186,59 @@
            auto ret = in->decode(frame, pkt.data);
            if(ret == 1){
                //吐出数据
                cache_pic(frame);
                cache_pic(frame, pkt.id);
            }
        }
        cache_rec_pkt(pkt);
        for(auto &i : map_rec_){
            if (!i.second.rec){
                i.second.rec = i.second.fn_init(in);
            }
            if (i.second.rec){
                if (i.second.rec){
                    std::lock_guard<std::mutex> locker(mtx_rec_pkt_);
                    for(auto &k : list_rec_pkt_){
                        avpacket p = {k.data, k.id};
                        i.second.rec->CachePacket(p);
                    }
                    logIt("start rec %d frames", list_rec_pkt_.size());
                }
            }else if (i.second.rec){
                i.second.rec->CachePacket(pkt);
            }
        }
    }
    int Wrapper::cache_rec_pkt(const avpacket &pkt){
        std::lock_guard<std::mutex> locker(mtx_rec_pkt_);
        //wait I
        if (list_rec_pkt_.empty()) {
            AVPacket &avpkt = pkt.data->getAVPacket();
            if (!(avpkt.flags & AV_PKT_FLAG_KEY)){
                return -1;
            }
        }
        maybe_dump_rec_pkt();
        recpkt k = {pkt.data, pkt.id};
        list_rec_pkt_.push_back(k);
        return 0;
    }
    void Wrapper::maybe_dump_rec_pkt(){
        //超过min/2,丢弃gop
        while (list_rec_pkt_.size() > minduration) {
            list_rec_pkt_.pop_front();
            while(!list_rec_pkt_.empty()){
                auto &cache = list_rec_pkt_.front();
                AVPacket &avpkt = cache.data->getAVPacket();
                if (!(avpkt.flags & AV_PKT_FLAG_KEY)){
                    list_rec_pkt_.pop_front();
                }else{
                    break;
                }
            }
        }
    }
    //////////////recorder
    std::shared_ptr<Recorder> Wrapper::init_recorder(FormatIn *in, std::string id,std::string dir, const int mind, const int maxd){
        if(!in){
@@ -228,6 +274,9 @@
        
        FnRec r = FnRec{fn, rec};
        map_rec_[rid] = r;
        minduration = mindur * 25;
        maxduration = maxdur * 25;
    }
    int Wrapper::FireRecorder(const char* sid,const int64_t &id){
@@ -237,6 +286,7 @@
                iter->second.rec->FireRecorder(id);
            }
        }
        logIt("FIRE REC %s, FRAME ID: %d", sid, id);
    }
    void Wrapper::cache_rec_info(std::string &id, int &index, std::string &path){
@@ -284,7 +334,7 @@
        use_decoder_ = true;
    }
    void Wrapper::cache_pic(std::shared_ptr<ffwrapper::FrameData> &frame){
    void Wrapper::cache_pic(std::shared_ptr<ffwrapper::FrameData> &frame, int64_t &id){
        pic_bgr24 pic;
        if(bridge_){
@@ -295,6 +345,7 @@
            unsigned char *data = (unsigned char*)malloc(pic.w * pic.h * 3);
            bridge_->copyPicture(data, frm);
            pic.data = data;
            pic.id = id;
        }
        
        {
@@ -311,7 +362,7 @@
    }
    void Wrapper::GetPicDecoder(unsigned char **data, int *w, int *h){
    void Wrapper::GetPicDecoder(unsigned char **data, int *w, int *h, int64_t *id){
        std::lock_guard<std::mutex> l(mutex_pic_);
        if(list_pic_.empty()){
            *data = NULL;
@@ -321,6 +372,7 @@
        }
        auto p = list_pic_.front();
        *data = p.data; *w = p.w; *h = p.h;
        *id = p.id;
        list_pic_.pop_front();
    }