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 --- /dev/null | 96 ------------- csrc/wrapper.cpp | 156 +++++++++------------ csrc/wrapper.hpp | 8 - goffmpeg.go | 86 ++++++++++++ cffmpeg.h | 7 - csrc/common/callback.hpp | 3 libcffmpeg.c | 12 - csrc/buz/recorder.hpp | 1 csrc/cffmpeg.cpp | 17 -- libcffmpeg.h | 9 - csrc/buz/recorder.cpp | 22 +-- 11 files changed, 163 insertions(+), 254 deletions(-) diff --git a/apiactive.go b/apiactive.go deleted file mode 100644 index 41b8ee1..0000000 --- a/apiactive.go +++ /dev/null @@ -1,38 +0,0 @@ -package goffmpeg - -/* -#include <stdlib.h> -#include "libcffmpeg.h" - -extern void cb_rec_proxy(char*, int); -void cb_rec(char* path, int i){ - cb_rec_proxy(path, i); -} -extern void cb_dec_proxy(void *data, int w, int h); -void cb_dec(void*data, int w, int h){ - cb_dec_proxy(data, w, h); -} -*/ -import "C" -import ( - "unsafe" -) - -// ActiveRecorder active recorer -func (h *GoFFMPEG) ActiveRecorder(output string, mind, maxd int, fn RecorderFunc) { - out := C.CString(output) - defer C.free(unsafe.Pointer(out)) - - funcRecorder = fn - - cFn := (C.rec_func)(unsafe.Pointer(C.cb_rec)) - C.wrap_fn_active_recorder(h.ffmpeg, out, C.int(mind), C.int(maxd), cFn) -} - -// ActiveDecoder active decoder -func (h *GoFFMPEG) ActiveDecoder(fn DecoderFunc) { - funcDecoder = fn - - cFn := (C.dec_func)(unsafe.Pointer(C.cb_dec)) - C.wrap_fn_active_decoder(h.ffmpeg, cFn) -} diff --git a/apiactivecallback.go b/apiactivecallback.go deleted file mode 100644 index c23feca..0000000 --- a/apiactivecallback.go +++ /dev/null @@ -1,40 +0,0 @@ -package goffmpeg - -/* -#include <stdlib.h> -*/ -import "C" -import ( - "unsafe" -) - -// RecorderFunc C function pointer GO -type RecorderFunc func(*string, *int) - -// DecoderFunc C func pointer go -type DecoderFunc func(*[]byte, *int, *int) - -var ( - funcRecorder RecorderFunc - funcDecoder DecoderFunc -) - -//export cb_rec_proxy -func cb_rec_proxy(path *C.char, index C.int) { - if funcRecorder != nil { - p := C.GoString(path) - i := int(index) - funcRecorder(&p, &i) - } -} - -//export cb_dec_proxy -func cb_dec_proxy(data *C.uchar, w, h C.int) { - if funcDecoder != nil { - d := C.GoBytes(unsafe.Pointer(data), w*h*3) - C.free(unsafe.Pointer(data)) - wid := int(w) - hei := int(h) - funcDecoder(&d, &wid, &hei) - } -} diff --git a/apipassive.go b/apipassive.go deleted file mode 100644 index b88c9f1..0000000 --- a/apipassive.go +++ /dev/null @@ -1,96 +0,0 @@ -package goffmpeg - -/* -#include <stdlib.h> -#include "libcffmpeg.h" -*/ -import "C" -import ( - "unsafe" -) - -// FireRecorder fire recorder -func (h *GoFFMPEG) FireRecorder(sid string, id int64) { - csid := C.CString(sid) - defer C.free(unsafe.Pointer(csid)) - C.wrap_fn_fire_recorder(h.ffmpeg, csid, C.long(id)) -} - -// BuildRecorder build recorder -func (h *GoFFMPEG) BuildRecorder(sid, output string, mind, maxd int) { - out := C.CString(output) - defer C.free(unsafe.Pointer(out)) - csid := C.CString(sid) - defer C.free(unsafe.Pointer(csid)) - - C.wrap_fn_recorder(h.ffmpeg, csid, out, C.int(mind), C.int(maxd)) -} - -// GetInfoRecorder info -func (h *GoFFMPEG) GetInfoRecorder() (int, string) { - var i C.int = -1 - var l C.int - - p := C.wrap_fn_info_recorder(h.ffmpeg, &i, &l) - // if p == nil { - // return -1, "" - // } - path := C.GoString(p) - C.free(unsafe.Pointer(p)) - - // fmt.Println("Go get info : ", path, " len: ", l) - - return int(i), path -} - -func (h *GoFFMPEG) GetRecID(p string) string { - pt := C.CString(p) - defer C.free(unsafe.Pointer(pt)) - var i C.int - - cid := C.wrap_fn_rec_id(h.ffmpeg, pt, &i) - - id := C.GoString(cid) - C.free(unsafe.Pointer(cid)) - - return id -} - -// BuildDecoder build decoder -func (h *GoFFMPEG) BuildDecoder() { - C.wrap_fn_decoder(h.ffmpeg) -} - -// GetPicDecoder get pic from decoder -func (h *GoFFMPEG) GetPicDecoder() ([]byte, int, int) { - var width C.int - var height C.int - - p := C.wrap_fn_decoder_pic(h.ffmpeg, &width, &height) - if width == 0 && height == 0 { - return nil, 0, 0 - } - defer C.free(unsafe.Pointer(p)) - d := C.GoBytes(p, width*height*3) - wid := int(width) - hei := int(height) - - return d, wid, hei -} - -//GetAVPacket get AVPacket -func (h *GoFFMPEG) GetAVPacket() ([]byte, int, int) { - var key C.int - var size C.int - - p := C.wrap_fn_get_avpacket(h.ffmpeg, &size, &key) - if size <= 0 { - return nil, 0, -1 - } - defer C.free(unsafe.Pointer(p)) - d := C.GoBytes(p, size) - s := int(size) - k := int(key) - - return d, s, k -} diff --git a/cffmpeg.h b/cffmpeg.h index 38d99e7..ef4ed72 100644 --- a/cffmpeg.h +++ b/cffmpeg.h @@ -9,9 +9,6 @@ typedef void* cffmpeg; -typedef void(*rec_func)(char*, int); -typedef void(*dec_func)(void*,int,int); - cffmpeg c_ffmpeg_create(); void c_ffmpeg_destroy(const cffmpeg h); void c_ffmpeg_run(const cffmpeg h, const char *input); @@ -28,10 +25,6 @@ void c_ffmpeg_build_decoder(const cffmpeg h); void* c_ffmpeg_get_pic_decoder(const cffmpeg h, int *wid, int *hei); void* c_ffmpeg_get_avpacket(const cffmpeg h, int *size, int *key); -////////////active api -void c_ffmpeg_active_recorder(const cffmpeg h, const char *dir, int mind, int maxd, rec_func fn); -void c_ffmpeg_active_decoder(const cffmpeg h, dec_func fn); - //////test void* c_ffmpeg_decode_jpeg(const cffmpeg h, const char *file, int *wid, int *hei); diff --git a/csrc/buz/recorder.cpp b/csrc/buz/recorder.cpp index 2267bf3..f83a596 100644 --- a/csrc/buz/recorder.cpp +++ b/csrc/buz/recorder.cpp @@ -24,7 +24,6 @@ ,minduration(10 * 25) ,end_frame(minduration) ,cur_frame(-1) - ,thread_(nullptr) ,stop_recorder_(false) ,id_(id) ,id_frame_(0) @@ -34,13 +33,8 @@ {} Recorder::~Recorder(){ - if(thread_){ - stop_recorder_.store(true); - cv_.notify_one(); - thread_->join(); - } - if(out_) - delete out_; + stop_recorder_.store(true); + cv_.notify_one(); } int Recorder::init_writer(){ @@ -149,13 +143,13 @@ } } } + if (out_){ + delete out_; + out_ = NULL; + } } int Recorder::Run(const char* output, const int mind, const int maxd){ - if(thread_){ - logIt("recorder already run"); - return 0; - } dir_ = output; int ret = init_writer(); @@ -173,9 +167,9 @@ logIt("min %d max %d endcount %d", minduration, maxduration, end_frame); - thread_.reset(new std::thread([&]{ + std::thread([&]{ run_thread(); - })); + }).detach(); return 0; } diff --git a/csrc/buz/recorder.hpp b/csrc/buz/recorder.hpp index 70c32fe..483404e 100644 --- a/csrc/buz/recorder.hpp +++ b/csrc/buz/recorder.hpp @@ -60,7 +60,6 @@ std::list<avpacket> list_pkt_; - std::unique_ptr<std::thread> thread_; std::atomic_bool stop_recorder_; std::mutex mutex_pkt_; std::condition_variable cv_; diff --git a/csrc/cffmpeg.cpp b/csrc/cffmpeg.cpp index f804054..cdbd2f0 100644 --- a/csrc/cffmpeg.cpp +++ b/csrc/cffmpeg.cpp @@ -103,23 +103,6 @@ s->GetPacket(&data, size, key); return data; } -/////////////////////active api -void c_ffmpeg_active_recorder(const cffmpeg h, const char *dir, int mind, int maxd, - rec_func fn){ - Wrapper *s = (Wrapper*)h; - - s->ActiveRecorder(dir, mind, maxd,[fn](std::string &p, int &i){ - fn((char*)p.c_str(), i); - }); -} - -void c_ffmpeg_active_decoder(const cffmpeg h, dec_func fn){ - Wrapper *s = (Wrapper*)h; - s->ActiveDecoder([fn](void* d, int wid, int hei){ - fn(d, wid, hei); - }); -} - /////////////////////test void* c_ffmpeg_decode_jpeg(const cffmpeg h, const char *file, int *wid, int *hei){ diff --git a/csrc/common/callback.hpp b/csrc/common/callback.hpp index c65b4d4..f1a867a 100644 --- a/csrc/common/callback.hpp +++ b/csrc/common/callback.hpp @@ -4,9 +4,6 @@ #include <string> #include <functional> -typedef std::function<void(std::string&, int&)> FUNC_REC; -typedef std::function<void(void*,int,int)> FUNC_DEC; - typedef std::function<void(std::string &id, int &id_frame, std::string &file_path)> FUNC_REC_INFO; #endif /* callback_h */ 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 diff --git a/csrc/wrapper.hpp b/csrc/wrapper.hpp index 8bbb157..943936f 100644 --- a/csrc/wrapper.hpp +++ b/csrc/wrapper.hpp @@ -74,9 +74,6 @@ int FireRecorder(const char* sid,const int64_t &id); void GetInfoRecorder(int &index, std::string &path); std::string GetRecorderID(const std::string &path); - // active api - void ActiveRecorder(const char *dir, const int mind, const int maxd, - FUNC_REC func); void ScalePicture(const int w, const int h, const int flags); void UseGB28181(); @@ -86,7 +83,6 @@ void GetPicDecoder(unsigned char **data, int *w, int *h); void GetPacket(unsigned char **pktData, int *size, int *key); //active api - void ActiveDecoder(FUNC_DEC fn); private: @@ -115,10 +111,6 @@ std::list<AVPacket> list_avpkt_; std::mutex mutex_avpkt_; - - // active api - FUNC_REC func_rec_; - FUNC_DEC func_dec_; int scale_w_, scale_h_, scale_f_; int gb_, cpu_; diff --git a/goffmpeg.go b/goffmpeg.go index b8739f8..8569084 100644 --- a/goffmpeg.go +++ b/goffmpeg.go @@ -126,6 +126,92 @@ return nil, 0, 0 } +// FireRecorder fire recorder +func (h *GoFFMPEG) FireRecorder(sid string, id int64) { + csid := C.CString(sid) + defer C.free(unsafe.Pointer(csid)) + C.wrap_fn_fire_recorder(h.ffmpeg, csid, C.long(id)) +} + +// BuildRecorder build recorder +func (h *GoFFMPEG) BuildRecorder(sid, output string, mind, maxd int) { + out := C.CString(output) + defer C.free(unsafe.Pointer(out)) + csid := C.CString(sid) + defer C.free(unsafe.Pointer(csid)) + + C.wrap_fn_recorder(h.ffmpeg, csid, out, C.int(mind), C.int(maxd)) +} + +// GetInfoRecorder info +func (h *GoFFMPEG) GetInfoRecorder() (int, string) { + var i C.int = -1 + var l C.int + + p := C.wrap_fn_info_recorder(h.ffmpeg, &i, &l) + // if p == nil { + // return -1, "" + // } + path := C.GoString(p) + C.free(unsafe.Pointer(p)) + + // fmt.Println("Go get info : ", path, " len: ", l) + + return int(i), path +} + +func (h *GoFFMPEG) GetRecID(p string) string { + pt := C.CString(p) + defer C.free(unsafe.Pointer(pt)) + var i C.int + + cid := C.wrap_fn_rec_id(h.ffmpeg, pt, &i) + + id := C.GoString(cid) + C.free(unsafe.Pointer(cid)) + + return id +} + +// BuildDecoder build decoder +func (h *GoFFMPEG) BuildDecoder() { + C.wrap_fn_decoder(h.ffmpeg) +} + +// GetPicDecoder get pic from decoder +func (h *GoFFMPEG) GetPicDecoder() ([]byte, int, int) { + var width C.int + var height C.int + + p := C.wrap_fn_decoder_pic(h.ffmpeg, &width, &height) + if width == 0 && height == 0 { + return nil, 0, 0 + } + defer C.free(unsafe.Pointer(p)) + d := C.GoBytes(p, width*height*3) + wid := int(width) + hei := int(height) + + return d, wid, hei +} + +//GetAVPacket get AVPacket +func (h *GoFFMPEG) GetAVPacket() ([]byte, int, int) { + var key C.int + var size C.int + + p := C.wrap_fn_get_avpacket(h.ffmpeg, &size, &key) + if size <= 0 { + return nil, 0, -1 + } + defer C.free(unsafe.Pointer(p)) + d := C.GoBytes(p, size) + s := int(size) + k := int(key) + + return d, s, k +} + ///////////////for encoder // GoEncoder encoder diff --git a/libcffmpeg.c b/libcffmpeg.c index 5066001..a380de4 100644 --- a/libcffmpeg.c +++ b/libcffmpeg.c @@ -43,10 +43,6 @@ release_if_err(fn_decoder_pic, lib); fn_get_avpacket = (lib_cffmpeg_avpacket)dlsym(lib, "c_ffmpeg_get_avpacket"); release_if_err(fn_get_avpacket, lib); - fn_active_recorder = (lib_cffmpeg_active_recorder)dlsym(lib, "c_ffmpeg_active_recorder"); - release_if_err(fn_active_recorder, lib); - fn_active_decoder = (lib_cffmpeg_active_decoder)dlsym(lib, "c_ffmpeg_active_decoder"); - release_if_err(fn_active_decoder, lib); fn_dec_jpeg = (lib_cffmpeg_decode_jpeg)dlsym(lib, "c_ffmpeg_decode_jpeg"); release_if_err(fn_dec_jpeg, lib); @@ -119,14 +115,6 @@ void* wrap_fn_get_avpacket(const cffmpeg h, int* size, int* key){ return fn_get_avpacket(h, size, key); -} - -void wrap_fn_active_recorder(const cffmpeg h, const char* dir, int mind, int maxd, rec_func fn){ - fn_active_recorder(h, dir, mind, maxd, fn); -} - -void wrap_fn_active_decoder(const cffmpeg h, dec_func fn){ - fn_active_decoder(h, fn); } void* wrap_fn_decode_jpeg(const cffmpeg h, const char* file, int* wid, int* hei){ diff --git a/libcffmpeg.h b/libcffmpeg.h index 9867d44..7c31723 100644 --- a/libcffmpeg.h +++ b/libcffmpeg.h @@ -10,9 +10,6 @@ typedef void* cffmpeg; -typedef void(*rec_func)(char*, int); -typedef void(*dec_func)(void*,int,int); - typedef cffmpeg(*lib_cffmpeg_create)(); typedef void (*lib_cffmpeg_destroy)(const cffmpeg); typedef void (*lib_cffmpeg_run)(const cffmpeg, const char*); @@ -26,8 +23,6 @@ typedef void (*lib_cffmpeg_decoder)(const cffmpeg); typedef void*(*lib_cffmpeg_pic)(const cffmpeg, int*, int*); typedef void*(*lib_cffmpeg_avpacket)(const cffmpeg, int*, int*); -typedef void (*lib_cffmpeg_active_recorder)(const cffmpeg, const char*, int, int, rec_func); -typedef void (*lib_cffmpeg_active_decoder)(const cffmpeg, dec_func); typedef void*(*lib_cffmpeg_decode_jpeg)(const cffmpeg, const char*, int*, int*); static lib_cffmpeg_create fn_create = NULL; @@ -43,8 +38,6 @@ static lib_cffmpeg_decoder fn_decoder = NULL; static lib_cffmpeg_pic fn_decoder_pic = NULL; static lib_cffmpeg_avpacket fn_get_avpacket = NULL; -static lib_cffmpeg_active_recorder fn_active_recorder = NULL; -static lib_cffmpeg_active_decoder fn_active_decoder = NULL; static lib_cffmpeg_decode_jpeg fn_dec_jpeg = NULL; typedef void* libcffmpeg; @@ -64,8 +57,6 @@ void wrap_fn_decoder(const cffmpeg h); void* wrap_fn_decoder_pic(const cffmpeg h, int* wid, int* hei); void* wrap_fn_get_avpacket(const cffmpeg h, int* size, int* key); -void wrap_fn_active_recorder(const cffmpeg h, const char* dir, int mind, int maxd, rec_func fn); -void wrap_fn_active_decoder(const cffmpeg h, dec_func fn); void* wrap_fn_decode_jpeg(const cffmpeg h, const char* file, int* wid, int* hei); -- Gitblit v1.8.0