From 7fe46306ac577db11ba8a8bbf20653861fcb1a1a Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期二, 22 十月 2019 15:31:29 +0800 Subject: [PATCH] rec bug fix --- csrc/worker/rec.cpp | 96 ++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 76 insertions(+), 20 deletions(-) diff --git a/csrc/worker/rec.cpp b/csrc/worker/rec.cpp index 36d837c..abb8da6 100644 --- a/csrc/worker/rec.cpp +++ b/csrc/worker/rec.cpp @@ -16,11 +16,13 @@ using namespace ffwrapper; using namespace cffmpeg_wrap::buz; +static const int cache_time = 30 * 60; + namespace cffmpeg_wrap { rec::rec() :recRef_(NULL) - ,min_cache_len_(10 * 60 * 25) // 鏈�灏忕紦瀛�?鍒嗛挓鐨勮棰�,鍥犱负鏁翠釜娴佺▼浼氭湁寤惰繜,鏆傚畾?鍒嗛挓 + ,min_cache_len_(cache_time * 25) // 鏈�灏忕紦瀛�?鍒嗛挓鐨勮棰�,鍥犱负鏁翠釜娴佺▼浼氭湁寤惰繜,鏆傚畾?鍒嗛挓 {} rec::~rec() @@ -43,6 +45,48 @@ list_recInfo_.emplace_back(info); } + void rec::findRecFramesIndex(const int64_t &fired_id, const int duration, int &start, int &end){ + + start = end = -1; + + if (list_pkt_.empty()){ + return; + } + + // 褰曞儚寮�濮媔d鍦ㄨЕ鍙慽d涔嬪墠1/2鏃堕暱,淇濊瘉鍦ㄤ腑闂� + int64_t start_id = fired_id - duration/2; + // 瀵绘壘鍏抽敭甯т綔涓哄綍鍍忓紑濮媔d + int offset = recRef_ ? recRef_->getFPS() : 25; + + int64_t index = -1; + + for(auto &i : list_pkt_){ + index++; + // 璺宠繃闊抽 + if(!recRef_->isVideoPkt(&i.data->getAVPacket())){ + continue; + } + // 瀵绘壘鍏抽敭甯т綔涓鸿捣濮� + if (start < 0){ + if (i.data->getAVPacket().flags & AV_PKT_FLAG_KEY){ + // 褰撳墠甯d > 寮�濮媔d鎴栧紑濮媔d鍦╫ffset鍐�,浣滀负璧峰褰曞儚甯� + if (i.v_id >= start_id || start_id - i.v_id < offset){ + start = index; + start_id = i.v_id; + } + } + }else if (recRef_->isVideoPkt(&i.data->getAVPacket())){ + // 瑙嗛甯�,鐪嬫槸鍚︾紦瀛樹腑鏈夋墍鏈夌殑duration鏁版嵁 + if (i.v_id - start_id == duration){ + end = index; + } + } + + } + + if (end < 0) end = index; + } + std::unique_ptr<buz::Recorder> rec::startRec(std::string id, std::string dir, const int64_t &frameID, const int mind, const int maxd, const bool audio){ if(!recRef_){ logIt("Init wrapper first"); @@ -63,8 +107,15 @@ } if (trycnt < 100){ + int duration = mind * recRef_->getFPS(); + int start=0, end=0; + std::lock_guard<std::mutex> locker(mtx_pkt_); - rec->PushPackets(list_pkt_); + logIt("cache size: %ld", list_pkt_.size()); + // 棣栨鑾峰彇褰曞儚淇℃伅,鍏堝瓨涓�涓渶鐭椂闀� + findRecFramesIndex(frameID, duration, start, end); + rec->StartWritePacket(list_pkt_, frameID, start, end); + return rec; } @@ -126,6 +177,9 @@ void rec::Load(ffwrapper::FormatIn *in){ recRef_ = in; + if (in){ + min_cache_len_ = in->getFPS() * cache_time; + } } void rec::Unload(){ @@ -165,30 +219,35 @@ // logIt("recorders count: %d", map_rec_.size()); } - void rec::SetPacket(std::shared_ptr<ffwrapper::CodedData> data, int64_t &id){ - if (!data) return; + void rec::SetPacket(const CPacket &pkt){ + if (!pkt.data) return; + cachePacket(pkt); std::lock_guard<std::mutex> l(mtx_rec_); for(auto &i : map_rec_){ if (i.second){ - i.second->PushPacket({data, id}); + std::lock_guard<std::mutex> pl(mtx_pkt_); + i.second->PushPacket(list_pkt_); } } - cachePacket(data, id); } - void rec::cachePacket(std::shared_ptr<ffwrapper::CodedData> data, int64_t &id){ + void rec::cachePacket(const CPacket &pkt){ + std::lock_guard<std::mutex> l(mtx_pkt_); //wait I if (list_pkt_.empty()) { + if (!recRef_->isVideoPkt(&pkt.data->getAVPacket())){ + return; + } - if (!(data->getAVPacket().flags & AV_PKT_FLAG_KEY)){ + if (!(pkt.data->getAVPacket().flags & AV_PKT_FLAG_KEY)){ return; } } - list_pkt_.push_back({data, id}); + list_pkt_.push_back(pkt); // 瓒呰繃缂撳瓨鏈�澶ч暱搴�,鍒犻櫎涓�涓猤op shrinkCache(); @@ -204,20 +263,17 @@ } int rec::shrinkCache(){ - //瓒呰繃鏈�澶х紦瀛�,涓㈠純gop - //缂撳瓨鏈�灏忛暱搴︾殑,鐢ㄤ簬璁板綍 - int fps = 25; - if (recRef_){ - fps = recRef_->getFPS(); - } - // 鏈�灏�5绉掗暱搴� - int mincache = fps * 5; - int md = min_cache_len_ < mincache ? mincache : min_cache_len_; - while (list_pkt_.size() > md) { + //瓒呰繃鏈�澶х紦瀛�,涓㈠純gop + + while (list_pkt_.size() > min_cache_len_) { list_pkt_.pop_front(); while(!list_pkt_.empty()){ auto &i = list_pkt_.front(); - if (!(i.data->getAVPacket().flags & AV_PKT_FLAG_KEY)){ + // 闊抽涓㈠純 + if (!recRef_->isVideoPkt(&i.data->getAVPacket())){ + list_pkt_.pop_front(); + }else if (!(i.data->getAVPacket().flags & AV_PKT_FLAG_KEY)){ + // 闈炲叧閿抚涓㈠純 list_pkt_.pop_front(); }else{ break; -- Gitblit v1.8.0