| | |
| | | char* c_ffmpeg_get_rec_id(const cffmpeg h, const char* p, int *length); |
| | | |
| | | 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_pic_decoder(const cffmpeg h, int *wid, int *hei, int64_t *id); |
| | | void* c_ffmpeg_get_avpacket(const cffmpeg h, int *size, int *key); |
| | | |
| | | //////test |
| | |
| | | void Recorder::end_writer(){ |
| | | if(cur_frame == -1) return; |
| | | out_->endWriter(); |
| | | logIt("INDEX %d, FILE %s, CURFrame %d, ENDFrame %d\n", |
| | | file_frame_index_, file_path_.c_str(), cur_frame, end_frame); |
| | | |
| | | //reinit cur_frame clear list pkt |
| | | { |
| | | std::lock_guard<std::mutex> locker(mutex_pkt_); |
| | |
| | | //callback to frame index and path |
| | | if(func_rec_info_){ |
| | | func_rec_info_(id_,file_frame_index_, file_path_); |
| | | // logIt("recoder index %d, file name %s\n", file_frame_index_, file_path_.c_str()); |
| | | }else{ |
| | | // logIt("recorder has no func_rec_info"); |
| | | } |
| | |
| | | end_frame = maxduration; |
| | | } |
| | | } |
| | | logIt("FIRE REC FRAME ID: %lld", id); |
| | | return 0; |
| | | } |
| | | |
| | |
| | | cv_.notify_one(); |
| | | } |
| | | |
| | | logIt("CACHE PACKET FRAME ID %lld", pkt.id); |
| | | return 0; |
| | | } |
| | | |
| | |
| | | s->BuildDecoder(); |
| | | } |
| | | |
| | | void* c_ffmpeg_get_pic_decoder(const cffmpeg h, int *wid, int *hei){ |
| | | void* c_ffmpeg_get_pic_decoder(const cffmpeg h, int *wid, int *hei, int64_t *id){ |
| | | Wrapper *s = (Wrapper*)h; |
| | | unsigned char *data = NULL; |
| | | s->GetPicDecoder(&data, wid, hei); |
| | | s->GetPicDecoder(&data, wid, hei, id); |
| | | return data; |
| | | } |
| | | |
| | |
| | | ,gb_(0) |
| | | ,cpu_(0) |
| | | ,use_decoder_(false) |
| | | ,minduration(250) |
| | | ,maxduration(750) |
| | | { |
| | | makeTheWorld(); |
| | | } |
| | |
| | | |
| | | run_worker(in.get(), pkt); |
| | | if(!data){ |
| | | map_rec_.clear(); |
| | | std::lock_guard<std::mutex> locker(mtx_rec_pkt_); |
| | | list_rec_pkt_.clear(); |
| | | |
| | | break; |
| | | } |
| | | //test |
| | |
| | | auto ret = in->decode(frame, pkt.data); |
| | | if(ret == 1){ |
| | | //吐出数据 |
| | | cache_pic(frame); |
| | | cache_pic(frame, pkt.id); |
| | | } |
| | | } |
| | | |
| | | cache_rec_pkt(pkt); |
| | | for(auto &i : map_rec_){ |
| | | if (!i.second.rec){ |
| | | i.second.rec = i.second.fn_init(in); |
| | | } |
| | | if (i.second.rec){ |
| | | if (i.second.rec){ |
| | | std::lock_guard<std::mutex> locker(mtx_rec_pkt_); |
| | | for(auto &k : list_rec_pkt_){ |
| | | avpacket p = {k.data, k.id}; |
| | | i.second.rec->CachePacket(p); |
| | | } |
| | | logIt("start rec %d frames", list_rec_pkt_.size()); |
| | | } |
| | | }else if (i.second.rec){ |
| | | i.second.rec->CachePacket(pkt); |
| | | } |
| | | } |
| | | } |
| | | |
| | | int Wrapper::cache_rec_pkt(const avpacket &pkt){ |
| | | |
| | | std::lock_guard<std::mutex> locker(mtx_rec_pkt_); |
| | | //wait I |
| | | if (list_rec_pkt_.empty()) { |
| | | AVPacket &avpkt = pkt.data->getAVPacket(); |
| | | if (!(avpkt.flags & AV_PKT_FLAG_KEY)){ |
| | | return -1; |
| | | } |
| | | } |
| | | maybe_dump_rec_pkt(); |
| | | recpkt k = {pkt.data, pkt.id}; |
| | | list_rec_pkt_.push_back(k); |
| | | |
| | | return 0; |
| | | } |
| | | void Wrapper::maybe_dump_rec_pkt(){ |
| | | //超过min/2,丢弃gop |
| | | while (list_rec_pkt_.size() > minduration) { |
| | | list_rec_pkt_.pop_front(); |
| | | while(!list_rec_pkt_.empty()){ |
| | | auto &cache = list_rec_pkt_.front(); |
| | | AVPacket &avpkt = cache.data->getAVPacket(); |
| | | if (!(avpkt.flags & AV_PKT_FLAG_KEY)){ |
| | | list_rec_pkt_.pop_front(); |
| | | }else{ |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | //////////////recorder |
| | | std::shared_ptr<Recorder> Wrapper::init_recorder(FormatIn *in, std::string id,std::string dir, const int mind, const int maxd){ |
| | | if(!in){ |
| | |
| | | |
| | | FnRec r = FnRec{fn, rec}; |
| | | map_rec_[rid] = r; |
| | | |
| | | minduration = mindur * 25; |
| | | maxduration = maxdur * 25; |
| | | } |
| | | |
| | | int Wrapper::FireRecorder(const char* sid,const int64_t &id){ |
| | |
| | | iter->second.rec->FireRecorder(id); |
| | | } |
| | | } |
| | | logIt("FIRE REC %s, FRAME ID: %d", sid, id); |
| | | } |
| | | |
| | | void Wrapper::cache_rec_info(std::string &id, int &index, std::string &path){ |
| | |
| | | use_decoder_ = true; |
| | | } |
| | | |
| | | void Wrapper::cache_pic(std::shared_ptr<ffwrapper::FrameData> &frame){ |
| | | void Wrapper::cache_pic(std::shared_ptr<ffwrapper::FrameData> &frame, int64_t &id){ |
| | | |
| | | pic_bgr24 pic; |
| | | if(bridge_){ |
| | |
| | | unsigned char *data = (unsigned char*)malloc(pic.w * pic.h * 3); |
| | | bridge_->copyPicture(data, frm); |
| | | pic.data = data; |
| | | pic.id = id; |
| | | } |
| | | |
| | | { |
| | |
| | | |
| | | } |
| | | |
| | | void Wrapper::GetPicDecoder(unsigned char **data, int *w, int *h){ |
| | | void Wrapper::GetPicDecoder(unsigned char **data, int *w, int *h, int64_t *id){ |
| | | std::lock_guard<std::mutex> l(mutex_pic_); |
| | | if(list_pic_.empty()){ |
| | | *data = NULL; |
| | |
| | | } |
| | | auto p = list_pic_.front(); |
| | | *data = p.data; *w = p.w; *h = p.h; |
| | | *id = p.id; |
| | | list_pic_.pop_front(); |
| | | } |
| | | |
| | |
| | | unsigned char *data; |
| | | int w; |
| | | int h; |
| | | |
| | | int64_t id; |
| | | }pic_bgr24; |
| | | |
| | | namespace buz{ |
| | |
| | | std::shared_ptr<buz::Recorder> init_recorder(ffwrapper::FormatIn *in, std::string id,std::string dir, const int mind, const int maxd); |
| | | |
| | | void cache_rec_info(std::string &id, int &index, std::string &path); |
| | | void cache_pic(std::shared_ptr<ffwrapper::FrameData> &frame); |
| | | void cache_pic(std::shared_ptr<ffwrapper::FrameData> &frame, int64_t &id); |
| | | |
| | | void cacheAVPacket(const AVPacket &pkt); |
| | | public: |
| | |
| | | void UseCPU(); |
| | | public: //decoder |
| | | void BuildDecoder(); |
| | | void GetPicDecoder(unsigned char **data, int *w, int *h); |
| | | void GetPicDecoder(unsigned char **data, int *w, int *h, int64_t *id); |
| | | void GetPacket(unsigned char **pktData, int *size, int *key); |
| | | //active api |
| | | |
| | |
| | | |
| | | int scale_w_, scale_h_, scale_f_; |
| | | int gb_, cpu_; |
| | | |
| | | typedef struct recpkt{ |
| | | std::shared_ptr<ffwrapper::CodedData> data; |
| | | int64_t id; |
| | | }recpkt; |
| | | std::list<recpkt> list_rec_pkt_; |
| | | std::mutex mtx_rec_pkt_; |
| | | int cache_rec_pkt(const buz::avpacket &pkt); |
| | | void maybe_dump_rec_pkt(); |
| | | int maxduration; |
| | | int minduration; |
| | | |
| | | //////////////////test frame to bgr24 |
| | | public: |
| | | uint8_t *decodeJPEG(const char *file, int *w, int *h); |
| | |
| | | return int(i), path |
| | | } |
| | | |
| | | // GetRecID get rec id |
| | | func (h *GoFFMPEG) GetRecID(p string) string { |
| | | pt := C.CString(p) |
| | | defer C.free(unsafe.Pointer(pt)) |
| | |
| | | } |
| | | |
| | | // GetPicDecoder get pic from decoder |
| | | func (h *GoFFMPEG) GetPicDecoder() ([]byte, int, int) { |
| | | func (h *GoFFMPEG) GetPicDecoder() ([]byte, int, int, int64) { |
| | | var width C.int |
| | | var height C.int |
| | | var fid C.longlong |
| | | |
| | | p := C.wrap_fn_decoder_pic(h.ffmpeg, &width, &height) |
| | | p := C.wrap_fn_decoder_pic(h.ffmpeg, &width, &height, &fid) |
| | | if width == 0 && height == 0 { |
| | | return nil, 0, 0 |
| | | return nil, 0, 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 |
| | | gfid := int64(fid) |
| | | return d, wid, hei, gfid |
| | | } |
| | | |
| | | //GetAVPacket get AVPacket |