video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-08-07 4eab3ca9bf020df2bb53fa2a36aa4f57d684225c
csrc/wrapper.cpp
@@ -12,6 +12,7 @@
#include "ffmpeg/configure/conf.hpp"
#include "ffmpeg/format/FormatIn.hpp"
#include "ffmpeg/format/FormatOut.hpp"
#include "ffmpeg/property/VideoProp.hpp"
#include "ffmpeg/data/CodedData.hpp"
#include "ffmpeg/data/FrameData.hpp"
@@ -29,13 +30,17 @@
    Wrapper::Wrapper()
    :input_url_("")
    ,recorder_(NULL)
    ,thread_(nullptr)
    ,stop_stream_(false)
    ,bridge_(NULL)
    ,scale_w_(0)
    ,scale_h_(0)
    ,scale_f_(SWS_POINT)
    ,gb_(0)
    ,cpu_(0)
    ,use_decoder_(false)
    ,minduration(250)
    ,maxduration(750)
    {
        makeTheWorld();
    }
@@ -43,16 +48,29 @@
    Wrapper::~Wrapper()
    {
        if(thread_){
            stop_stream_.store(true);
            thread_->join();
        try
        {
            if(thread_){
                stop_stream_.store(true);
                thread_->join();
            }
            if(bridge_){
                delete bridge_; bridge_ = NULL;
            }
            map_rec_.clear();
            list_rec_pkt_.clear();
            for(auto &i : list_pic_){
                free(i.data);
            }
        }
        if(bridge_){
            delete bridge_; bridge_ = NULL;
        catch(const std::exception& e)
        {
            logIt("WRAPPER EXCEPTION: ", e.what());
        }
        if(recorder_){
            delete recorder_; recorder_ = NULL;
        }
    }
    void Wrapper::ScalePicture(const int w, const int h, const int flags){
@@ -61,15 +79,29 @@
        scale_h_ = h;
    }
    void Wrapper::UseGB28181(){
        gb_ = 1;
    }
    void Wrapper::UseCPU(){
        cpu_ = 1;
    }
    std::unique_ptr<ffwrapper::FormatIn> Wrapper::init_reader(const char* input){
        VideoProp prop;
        prop.url_ = input;
        prop.rtsp_tcp_ = true;
        prop.gpu_acc_ = !cpu_;
       std::unique_ptr<FormatIn> in(new FormatIn(prop.gpuAccl()));
       AVDictionary *avdic = prop.optsFormat();
       int flag = in->open(input, &avdic);
       int flag = -1;
        if (gb_){
            flag = in->openGb28181(input, NULL);
        }else{
            flag = in->open(input, &avdic);
        }
       if(avdic){
          av_dict_free(&avdic);
       }
@@ -78,92 +110,11 @@
                logIt("yolo can't find video stream\n");
                return nullptr;
            }
            bool flag = true;
            if(map_workers_.find(WORKER_DECODER) != map_workers_.end()){
                flag = in->openCodec(AVMEDIA_TYPE_VIDEO, NULL);
                auto dec_ctx = in->getCodecContext();
                if(bridge_)delete bridge_;
                AVPixelFormat pix_fmt = AV_PIX_FMT_BGR24;
                bridge_ = new cvbridge(
                        dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
                        scale_w_?scale_w_:dec_ctx->width, scale_h_?scale_h_:dec_ctx->height, pix_fmt,
                        scale_f_);
            }
            if (!flag){
                logIt("FormatIn openCodec Failed!");
                return nullptr;
            }
            return in;
        }
        return nullptr;
    }
    int Wrapper::init_recorder(FormatIn *in, std::string dir, const int mind, const int maxd){
        if(!in){
            logIt("Init wrapper first");
            return -1;
        }
        if(recorder_){
            logIt("recorder exist");
            delete recorder_;
        }
        recorder_ = new Recorder(in);
        recorder_->SetCallback([&](int &index, std::string &path){
            cache_rec_info(index, path);
        });
        int trycnt = 0;
        while(trycnt < 100){
            const int ret = recorder_->Run(dir.c_str(), mind, maxd);
            if(ret == 0) break;
            usleep(200000);
        }
        return trycnt == 100;
    }
    void Wrapper::init_worker(ffwrapper::FormatIn *in){
        for(auto &i : map_workers_){
            if(i.second(in) != 0){
                logIt("worker %d init error", i.first);
            }
        }
    }
    void Wrapper::run_stream_thread(){
        while(!stop_stream_.load()){
            auto in = init_reader(input_url_.c_str());
            if (!in) {
                usleep(200000);
                continue;
            }
            init_worker(in.get());
            int64_t id = 0;
            avpacket pkt;
            while(!stop_stream_.load()){
                auto data(std::make_shared<CodedData>());
               if(!in->readPacket(data)){
                    logIt("read packet error");
                    pkt.id = -1; data = nullptr; id = 0;
               }else{
                    pkt.id = id++;
                }
                pkt.data = data;
                run_worker(in.get(), pkt);
                if(!data){
                    break;
                }
                //test
                if(recorder_)
                    if(id % 250 == 0)
                        recorder_->FireRecorder(id);
            }
        }
    }
    int Wrapper::RunStream(const char* input){
@@ -181,35 +132,194 @@
        return 0;
    }
    //////////////recorder
    void Wrapper::BuildRecorder(const char *output, const int mindur, const int maxdur){
        std::string dir(output);
        map_workers_[WORKER_RECORDER] = [=](FormatIn *in){
            return init_recorder(in, dir, mindur, maxdur);
        };
    }
    void Wrapper::run_stream_thread(){
        while(!stop_stream_.load()){
            auto in = init_reader(input_url_.c_str());
    int Wrapper::FireRecorder(const int64_t &id){
        if(recorder_){
            recorder_->FireRecorder(id);
            if (!in) {
                logIt("ERROR: init_reader! url: %s\n", input_url_.c_str());
                usleep(200000);
                continue;
            }
            int64_t id = 0;
            avpacket pkt;
            while(!stop_stream_.load()){
                auto data(std::make_shared<CodedData>());
               if(!in->readPacket(data)){
                    logIt("read packet error");
                    pkt.id = -1; data = nullptr; id = 0;
               }else{
                    pkt.id = id++;
                }
                pkt.data = data;
                if(data != nullptr) {
                    cacheAVPacket(data->getAVPacket());
                }
                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
                // if(recorder_)
                //     if(id % 250 == 0)
                //         recorder_->FireRecorder(id);
            }
        }
    }
    void Wrapper::cache_rec_info(int &index, std::string &path){
        if(func_rec_){ //active api
            func_rec_(path, index);
        }else{                  // passive api
            std::lock_guard<std::mutex> l(mutex_rec_);
            while(list_rec_.size() > 10){
                for(int i = 0; i < 5; i++){
                    list_rec_.pop_front();
    void Wrapper::run_worker(ffwrapper::FormatIn *in, avpacket &pkt){
        if(!pkt.data) return;
        if (use_decoder_) {
            if(in->getCodecContext() == NULL){
                bool flag = true;
                flag = in->openCodec(AVMEDIA_TYPE_VIDEO, NULL);
                auto dec_ctx = in->getCodecContext();
                if(bridge_)delete bridge_;
                scale_w_ = scale_w_ == 0 || scale_w_ > dec_ctx->width ? dec_ctx->width : scale_w_;
                scale_h_ = scale_h_ == 0 || scale_h_ > dec_ctx->height ? dec_ctx->height : scale_h_;
                AVPixelFormat pix_fmt = AV_PIX_FMT_BGR24;
                bridge_ = new cvbridge(
                        dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
                        scale_w_, scale_h_, pix_fmt, scale_f_);
                if (!flag){
                    logIt("FormatIn openCodec Failed!");
                }
            }
            struct record_file_info info;
            info.file_frame_index = index;
            info.file_path = path;
            list_rec_.emplace_back(info);
            auto frame(std::make_shared<FrameData>());
            auto ret = in->decode(frame, pkt.data);
            if(ret == 1){
                //吐出数据
                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){
                    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){
            logIt("Init wrapper first");
            return nullptr;
        }
        auto rec = std::make_shared<Recorder>(in, id.c_str());
        rec->SetCallback([&](std::string &id, int &index, std::string &path){
            cache_rec_info(id, index, path);
        });
        int trycnt = 0;
        while(trycnt < 100){
            const int ret = rec->Run(dir.c_str(), mind, maxd);
            if(ret == 0) break;
            usleep(200000);
        }
        if (trycnt < 100){
            return rec;
        }
        return nullptr;
    }
    void Wrapper::BuildRecorder(const char* id, const char *output, const int mindur, const int maxdur){
        std::string rid(id);
        std::string dir(output);
        auto fn = [=](FormatIn *in){
            return init_recorder(in, rid, dir, mindur, maxdur);
        };
        std::shared_ptr<Recorder> rec(nullptr);
        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){
        auto iter = map_rec_.find(sid);
        if (iter != map_rec_.end()){
            if(iter->second.rec){
                iter->second.rec->FireRecorder(id);
            }
        }
    }
    void Wrapper::cache_rec_info(std::string &id, int &index, std::string &path){
        std::lock_guard<std::mutex> l(mutex_rec_);
        while(list_rec_.size() > 100){
            for(int i = 0; i < 25; i++){
                list_rec_.pop_front();
            }
        }
        struct record_file_info info;
        info.file_frame_index = index;
        info.file_path = path;
        info.rec_id = id;
        list_rec_.emplace_back(info);
        list_rec_map_[path] = id;
        logIt("LIST REC FILES COUNT : %d", list_rec_.size());
    }
    void Wrapper::GetInfoRecorder(int &index, std::string &path){
@@ -223,27 +333,43 @@
        index = info.file_frame_index;
        path = info.file_path;
        list_rec_.pop_front();
        if (map_rec_.find(info.rec_id) != map_rec_.end())
            map_rec_.erase(info.rec_id);
        // logIt("go get info index: %d, file: %s\n", index, path.c_str());
    }
    std::string Wrapper::GetRecorderID(const std::string &path){
        std::string ret("");
        auto iter = list_rec_map_.find(path);
        if (iter != list_rec_map_.end()){
            ret = iter->second;
            list_rec_map_.erase(iter);
        }
        return ret;
    }
    ////////decoder
    void Wrapper::BuildDecoder(){
        map_workers_[WORKER_DECODER] = [&](FormatIn*){return 0;};
        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_){
            AVFrame *frm = frame->getAVFrame();
            unsigned char *data = (unsigned char*)malloc(frm->width * frm->height * 3);
            pic.w = scale_w_;
            pic.h = scale_h_;
            unsigned char *data = (unsigned char*)malloc(pic.w * pic.h * 3);
            bridge_->copyPicture(data, frm);
            pic.data = data;
            pic.w = scale_w_ ? scale_w_ : frm->width;
            pic.h = scale_h_ ? scale_h_ : frm->height;
            pic.id = id;
        }
        if(func_dec_){
            func_dec_(pic.data, pic.w, pic.h);
        }else{
        {
            std::lock_guard<std::mutex> l(mutex_pic_);
            while(list_pic_.size() > 10){
                for(int i = 0; i < 5; i++){
@@ -257,7 +383,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;
@@ -267,35 +393,32 @@
        }
        auto p = list_pic_.front();
        *data = p.data; *w = p.w; *h = p.h;
        *id = p.id;
        list_pic_.pop_front();
    }
    void Wrapper::run_worker(ffwrapper::FormatIn *in, avpacket &pkt){
        if(!pkt.data) return;
        if (map_workers_.find(WORKER_DECODER) != map_workers_.end()) {
            auto frame(std::make_shared<FrameData>());
            auto ret = in->decode(frame, pkt.data);
            if(ret == 1){
                //吐出数据
                cache_pic(frame);
    void Wrapper::GetPacket(unsigned char **pktData, int *size, int *key){
        std::lock_guard<std::mutex> l(mutex_avpkt_);
        if(list_avpkt_.empty()){
            return;
        }
        auto pkt = list_avpkt_.front();
        *key = pkt.flags & AV_PKT_FLAG_KEY;
        *size = pkt.size;
        *pktData = (unsigned char *)malloc(*size);
        memcpy(*pktData, pkt.data, pkt.size);
        list_avpkt_.pop_front();
    }
    void Wrapper::cacheAVPacket(const AVPacket &pkt){
        std::lock_guard<std::mutex> l(mutex_pic_);
        while(list_avpkt_.size() > 10){
//            printf("cacheAVPacket drop packets!!!!!!!!!!\n");
            for(int i = 0; i < 5; i++){
                list_avpkt_.pop_front();
            }
        }
        if(recorder_){
            recorder_->CachePacket(pkt);
        }
    }
    ///// active api
    void Wrapper::ActiveRecorder(const char *dir, const int mind, const int maxd,
                                FUNC_REC func){
        BuildRecorder(dir, mind, maxd);
        func_rec_ = func;
    }
    void Wrapper::ActiveDecoder(FUNC_DEC fn){
        BuildDecoder();
        func_dec_ = fn;
        list_avpkt_.emplace_back(pkt);
    }
    ////// test
@@ -349,4 +472,96 @@
        }
        return NULL;
    }
}
/////// for encoder
    typedef struct _PicEncoder{
        FormatOut *enc;
        int w;
        int h;
        int fps;
        int br;
        int gi;
        int flag;
        cvbridge *bridge;
    } PicEncoder;
    void *CreateEncoder(const int w, const int h, const int fps, const int br, const int scale_flag, const int gi){
        PicEncoder *e = (PicEncoder*)malloc(sizeof(PicEncoder));
        e->enc = NULL;
        e->w = w;
        e->h = h;
        e->fps = fps;
        e->br = br;
        e->gi = gi;
        e->flag = scale_flag;
        e->bridge = NULL;
        VideoProp prop_;
        prop_.width_ = w;
        prop_.height_ = h;
        prop_.fps_ = fps;
        prop_.bit_rate_ = br;
        gi < 0 ? prop_.gpu_acc_ = false : prop_.gpu_acc_ = true;
      FormatOut *enc = new FormatOut(prop_, "./88.mp4");
        e->enc = enc;
        return e;
    }
    void DestroyEncoder(void *h){
        PicEncoder *e = (PicEncoder*)h;
        if (e == NULL){
            return;
        }
        delete e->bridge;
        delete e->enc;
        free(e);
    }
    int Encode(void *hdl, uint8_t *in, const int w, const int h, uint8_t **out, int *size, int *key){
        PicEncoder *e = (PicEncoder*)hdl;
        auto ctx = e->enc->getCodecContext();
        if (e->bridge == NULL){
            AVPixelFormat pix_fmt = AV_PIX_FMT_BGR24;
            e->bridge = new cvbridge(
                    w, h, AV_PIX_FMT_BGR24,
                    e->w, e->h, ctx->pix_fmt, e->flag);
        }
        AVFrame *frame = e->bridge->getAVFrame(in, w, h);
        auto data(std::make_shared<CodedData>());
      const int flag = e->enc->encode(data, frame);
      if(flag > 0){
            auto pkt = data->getAVPacket();
            int extradata_size = ctx->extradata_size;
         uint8_t *extra = ctx->extradata;
            *key = pkt.flags & AV_PKT_FLAG_KEY;
           if(!(*key)){
               extradata_size = 0;
           }
           *size = pkt.size + extradata_size;
           *out = (unsigned char *)malloc(*size);
           memcpy(*out, extra, extradata_size);
           memcpy(*out + extradata_size, pkt.data, pkt.size);
      }else{
         logIt("encode error or need more packet\n");
      }
        av_frame_free(&frame);
        return flag;
    }
}