| | |
| | | |
| | | 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); |
| | |
| | | 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); |
| | |
| | | ,minduration(10 * 25) |
| | | ,end_frame(minduration) |
| | | ,cur_frame(-1) |
| | | ,thread_(nullptr) |
| | | ,stop_recorder_(false) |
| | | ,id_(id) |
| | | ,id_frame_(0) |
| | |
| | | {} |
| | | |
| | | 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(){ |
| | |
| | | } |
| | | } |
| | | } |
| | | 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(); |
| | |
| | | |
| | | logIt("min %d max %d endcount %d", minduration, maxduration, end_frame); |
| | | |
| | | thread_.reset(new std::thread([&]{ |
| | | std::thread([&]{ |
| | | run_thread(); |
| | | })); |
| | | }).detach(); |
| | | |
| | | return 0; |
| | | } |
| | |
| | | |
| | | std::list<avpacket> list_pkt_; |
| | | |
| | | std::unique_ptr<std::thread> thread_; |
| | | std::atomic_bool stop_recorder_; |
| | | std::mutex mutex_pkt_; |
| | | std::condition_variable cv_; |
| | |
| | | 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){ |
| | |
| | | #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 */ |
| | |
| | | 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(){ |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | 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){ |
| | |
| | | } |
| | | } |
| | | |
| | | 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){ |
| | |
| | | } |
| | | |
| | | 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(){ |
| | |
| | | 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++){ |
| | |
| | | } |
| | | } |
| | | 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 |
| | |
| | | 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(); |
| | |
| | | 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: |
| | | |
| | |
| | | |
| | | 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_; |
| | |
| | | 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 |
| | |
| | | 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); |
| | | |
| | |
| | | |
| | | 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){ |
| | |
| | | |
| | | 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*); |
| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | 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); |
| | | |
| | | |