video analysis2.0拆分,ffmpeg封装go接口库
zhangmeng
2019-07-26 e84510254860e916cbb48ec3de4557b14a99d76c
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,15 +68,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);
       }
@@ -107,6 +123,45 @@
        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){
@@ -141,9 +196,12 @@
    }
    void Wrapper::run_stream_thread(){
        while(!stop_stream_.load()){
            auto in = init_reader(input_url_.c_str());
            if (!in) {
                logIt("ERROR: init_reader! url: %s\n", input_url_.c_str());
                usleep(200000);
                continue;
            }
@@ -160,15 +218,18 @@
                    pkt.id = id++;
                }
                pkt.data = data;
                if(data != nullptr) {
                    cacheAVPacket(data->getAVPacket());
                }
                run_worker(in.get(), pkt);
                if(!data){
                    break;
                }
                //test
                if(recorder_)
                    if(id % 250 == 0)
                        recorder_->FireRecorder(id);
                // if(recorder_)
                //     if(id % 250 == 0)
                //         recorder_->FireRecorder(id);
            }
        }
    }
@@ -216,6 +277,7 @@
            info.file_frame_index = index;
            info.file_path = path;
            list_rec_.emplace_back(info);
            logIt("list rec files count : %d", list_rec_.size());
        }
    }
@@ -230,6 +292,7 @@
        index = info.file_frame_index;
        path = info.file_path;
        list_rec_.pop_front();
        logIt("go get info index: %d, file: %s\n", index, path.c_str());
    }
    ////////decoder
@@ -276,6 +339,30 @@
        auto p = list_pic_.front();
        *data = p.data; *w = p.w; *h = p.h;
        list_pic_.pop_front();
    }
    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();
            }
        }
        list_avpkt_.emplace_back(pkt);
    }
    void Wrapper::run_worker(ffwrapper::FormatIn *in, avpacket &pkt){
@@ -465,7 +552,7 @@
           }
           *size = pkt.size + extradata_size;
           *out = (unsigned char *)malloc(*size);
           memcpy(*out, extra, extradata_size);
           memcpy(*out + extradata_size, pkt.data, pkt.size);