From 461c08142aa92e8ee121c451fd93d04490caff75 Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期五, 26 七月 2019 15:45:24 +0800 Subject: [PATCH] remove redundant code --- csrc/wrapper.cpp | 156 +++++++++++++++++++++++----------------------------- 1 files changed, 69 insertions(+), 87 deletions(-) diff --git a/csrc/wrapper.cpp b/csrc/wrapper.cpp index d67cb8d..48ed9a0 100644 --- a/csrc/wrapper.cpp +++ b/csrc/wrapper.cpp @@ -100,6 +100,20 @@ return nullptr; } + int Wrapper::RunStream(const char* input){ + if(thread_){ + logIt("wrapper run stream already run"); + return 0; + } + + input_url_ = input; + + thread_.reset(new std::thread([&]{ + run_stream_thread(); + })); + + return 0; + } void Wrapper::run_stream_thread(){ @@ -139,21 +153,46 @@ } } - int Wrapper::RunStream(const char* input){ - if(thread_){ - logIt("wrapper run stream already run"); - return 0; + 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!"); + } + } + + auto frame(std::make_shared<FrameData>()); + auto ret = in->decode(frame, pkt.data); + if(ret == 1){ + //鍚愬嚭鏁版嵁 + cache_pic(frame); + } } - input_url_ = input; - - thread_.reset(new std::thread([&]{ - run_stream_thread(); - })); - - return 0; + for(auto &i : map_rec_){ + if (!i.second.rec){ + i.second.rec = i.second.fn_init(in); + } + if (i.second.rec){ + i.second.rec->CachePacket(pkt); + } + } } - //////////////recorder std::shared_ptr<Recorder> Wrapper::init_recorder(FormatIn *in, std::string id,std::string dir, const int mind, const int maxd){ if(!in){ @@ -200,65 +239,21 @@ } } - 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!"); - } - } - - auto frame(std::make_shared<FrameData>()); - auto ret = in->decode(frame, pkt.data); - if(ret == 1){ - //鍚愬嚭鏁版嵁 - cache_pic(frame); - } - } - - for(auto &i : map_rec_){ - if (!i.second.rec){ - i.second.rec = i.second.fn_init(in); - } - if (i.second.rec){ - i.second.rec->CachePacket(pkt); - } - } - } - void Wrapper::cache_rec_info(std::string &id, 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() > 100){ - for(int i = 0; i < 25; i++){ - list_rec_.pop_front(); - } + + 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; - list_rec_.emplace_back(info); - list_rec_map_[path] = id; - logIt("list rec files count : %d", list_rec_.size()); - map_rec_.erase(id); } + struct record_file_info info; + info.file_frame_index = index; + info.file_path = path; + list_rec_.emplace_back(info); + list_rec_map_[path] = id; + logIt("list rec files count : %d", list_rec_.size()); + map_rec_.erase(id); } void Wrapper::GetInfoRecorder(int &index, std::string &path){ @@ -276,11 +271,13 @@ } std::string Wrapper::GetRecorderID(const std::string &path){ + std::string ret(""); auto iter = list_rec_map_.find(path); if (iter != list_rec_map_.end()){ - return iter->second; + ret = iter->second; + list_rec_map_.erase(iter); } - return ""; + return ret; } ////////decoder void Wrapper::BuildDecoder(){ @@ -299,9 +296,8 @@ bridge_->copyPicture(data, frm); pic.data = data; } - 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++){ @@ -350,20 +346,6 @@ } } list_avpkt_.emplace_back(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; } ////// test -- Gitblit v1.8.0