From c60d61c48c7a1e7b693d4c3f6427e3b616d1f471 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期一, 21 十月 2019 12:29:33 +0800
Subject: [PATCH] update ffmpeg

---
 csrc/worker/rec.cpp |   46 ++++++++++++++++++++++++++++++++--------------
 1 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/csrc/worker/rec.cpp b/csrc/worker/rec.cpp
index 797e047..36d837c 100644
--- a/csrc/worker/rec.cpp
+++ b/csrc/worker/rec.cpp
@@ -3,6 +3,10 @@
 #include <unistd.h>
 #include <sys/time.h>
 
+extern "C"{
+#include <libavcodec/avcodec.h>
+}
+
 #include "../ffmpeg/format/FormatIn.hpp"
 #include "../ffmpeg/data/CodedData.hpp"
 #include "../ffmpeg/log/log.hpp"
@@ -16,8 +20,7 @@
 {
     rec::rec()
     :recRef_(NULL)
-    ,minduration_(250)
-    ,maxduration_(750)
+    ,min_cache_len_(10 * 60 * 25) // 鏈�灏忕紦瀛�?鍒嗛挓鐨勮棰�,鍥犱负鏁翠釜娴佺▼浼氭湁寤惰繜,鏆傚畾?鍒嗛挓
     {}
 
     rec::~rec()
@@ -40,7 +43,7 @@
         list_recInfo_.emplace_back(info);
     }
 
-    std::unique_ptr<buz::Recorder> rec::startRec(std::string id, std::string dir, const int mind, const int maxd, const bool audio){
+    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");
             return nullptr;
@@ -58,6 +61,7 @@
             if(ret == 0) break;
             usleep(200000);
         }
+        
         if (trycnt < 100){
             std::lock_guard<std::mutex> locker(mtx_pkt_);
             rec->PushPackets(list_pkt_);
@@ -133,19 +137,16 @@
         return recRef_ != NULL;
     }
     
-    void rec::NewRec(const char* id, const char *output, const int mindur, const int maxdur, const bool audio){
+    void rec::NewRec(const char* id, const char *output, const int64_t &frameID, const int mindur, const int maxdur, const bool audio){
         std::string rid(id);
         std::string dir(output);
         
-        minduration_ = mindur * 25;
-        maxduration_ = maxdur * 25;
-
         {
             std::lock_guard<std::mutex> l(mtx_rec_);
             if (map_rec_.find(rid) != map_rec_.end()){
                 map_rec_.erase(rid);
             }
-            map_rec_[rid] = startRec(rid, dir, mindur, maxdur, audio);
+            map_rec_[rid] = startRec(rid, dir, frameID, mindur, maxdur, audio);
         }
         
     }
@@ -181,25 +182,42 @@
         std::lock_guard<std::mutex> l(mtx_pkt_);
         //wait I 
         if (list_pkt_.empty()) {
-            AVPacket &avpkt = data->getAVPacket();
-            if (!(avpkt.flags & AV_PKT_FLAG_KEY)){
+
+            if (!(data->getAVPacket().flags & AV_PKT_FLAG_KEY)){
                 return;
             }
         }
+        
         list_pkt_.push_back({data, id});
+
         // 瓒呰繃缂撳瓨鏈�澶ч暱搴�,鍒犻櫎涓�涓猤op
         shrinkCache();
+    }
+
+    void rec::SetRecMinCacheTime(const int min){
+        // 鐢变簬鏁翠釜娴佺▼鑰楁椂,琛ュ伩time_offset_
+        int fps = 25;
+        if (recRef_){
+            fps = recRef_->getFPS();
+        }
+        min_cache_len_ += min * fps;
     }
 
     int rec::shrinkCache(){
         //瓒呰繃鏈�澶х紦瀛�,涓㈠純gop
         //缂撳瓨鏈�灏忛暱搴︾殑,鐢ㄤ簬璁板綍
-        while (list_pkt_.size() > minduration_/2) {
+        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) {
             list_pkt_.pop_front();
             while(!list_pkt_.empty()){
-                auto &cache = list_pkt_.front();
-                AVPacket &avpkt = cache.data->getAVPacket();
-                if (!(avpkt.flags & AV_PKT_FLAG_KEY)){
+                auto &i = list_pkt_.front();
+                if (!(i.data->getAVPacket().flags & AV_PKT_FLAG_KEY)){
                     list_pkt_.pop_front();
                 }else{
                     break;

--
Gitblit v1.8.0