video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-07-19 e6725cb3cf4ced3a6985d4d3db4d5cf1336a30d8
csrc/wrapper.cpp
@@ -37,7 +37,9 @@
    ,scale_w_(0)
    ,scale_h_(0)
    ,scale_f_(SWS_POINT)
    ,encoder_(NULL)
    ,gb_(0)
    ,cpu_(0)
    ,encoder_(nullptr)
    {
        makeTheWorld();
    }
@@ -66,11 +68,20 @@
        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();
@@ -108,6 +119,50 @@
        return nullptr;
    }
    ffwrapper::FormatIn* Wrapper::init_reader_gb28181(const char* input){
            VideoProp prop;
            prop.url_ = input;
            prop.rtsp_tcp_ = true;
            prop.gpu_acc_ = !cpu_;
            ffwrapper::FormatIn* in(new FormatIn(prop.gpuAccl()));
           AVDictionary *avdic = prop.optsFormat();
           int flag = in->openGb28181(input, &avdic);
           if(avdic){
                logIt("ERROR:in->openGb28181(input, &avdic):flag:%d\n",flag);
              av_dict_free(&avdic);
           }
            if(flag == 0){
                if(!in->findStreamInfo(NULL)){
                    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_;
                    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!");
                    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");
@@ -141,13 +196,24 @@
    }
    void Wrapper::run_stream_thread(){
        std::unique_ptr<FormatIn> fi(nullptr);
        while(!stop_stream_.load()){
            auto in = init_reader(input_url_.c_str());
            FormatIn *in = NULL;
            if (!gb_){
                fi = init_reader(input_url_.c_str());
                in = fi.get();
            }else{
                in = init_reader_gb28181(input_url_.c_str());
            }
            if (!in) {
                logIt("ERROR: init_reader!\n");
                usleep(200000);
                continue;
            }
            init_worker(in.get());
            init_worker(in);
            int64_t id = 0;
            avpacket pkt;
@@ -161,7 +227,7 @@
                }
                pkt.data = data;
                run_worker(in.get(), pkt);
                run_worker(in, pkt);
                if(!data){
                    break;
                }
@@ -170,6 +236,11 @@
                    if(id % 250 == 0)
                        recorder_->FireRecorder(id);
            }
            if (gb_){
                delete in;
            }else{
                fi.reset(nullptr);
            }
        }
    }