video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-07-22 bf0974b21a86e635e37b9523e633def915e43626
update
2个文件已修改
109 ■■■■■ 已修改文件
csrc/wrapper.cpp 107 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/wrapper.hpp 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
csrc/wrapper.cpp
@@ -85,7 +85,12 @@
        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);
        }
@@ -119,49 +124,44 @@
        return nullptr;
    }
    ffwrapper::FormatIn* Wrapper::init_reader_gb28181(const char* input){
    // 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;
        }
    //     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){
@@ -197,23 +197,15 @@
    void Wrapper::run_stream_thread(){
        
        std::unique_ptr<FormatIn> fi(nullptr);
        while(!stop_stream_.load()){
            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) {
            auto in = init_reader(input_url_.c_str());
                
            if (!in) {
                logIt("ERROR: init_reader!\n");
                usleep(200000);
                continue;
            }
            init_worker(in);
            init_worker(in.get());
            int64_t id = 0;
            avpacket pkt;
@@ -227,7 +219,7 @@
                }
                pkt.data = data;
                run_worker(in, pkt);
                run_worker(in.get(), pkt);
                if(!data){
                    break;
                }
@@ -235,11 +227,6 @@
                if(recorder_)
                    if(id % 250 == 0)
                        recorder_->FireRecorder(id);
            }
            if (gb_){
                delete in;
            }else{
                fi.reset(nullptr);
            }
        }
    }
csrc/wrapper.hpp
@@ -51,7 +51,7 @@
        private: 
            std::unique_ptr<ffwrapper::FormatIn> init_reader(const char* input);
            ffwrapper::FormatIn* init_reader_gb28181(const char* input);
            // ffwrapper::FormatIn* init_reader_gb28181(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);