| | |
| | | ,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(); |
| | | } |
| | | |