From 87fea24e8bba1bfbee707cdaa6f7979451531acc Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期六, 19 十月 2019 10:23:01 +0800
Subject: [PATCH] add interface rec duration

---
 csrc/wrapper.cpp    |    4 ++++
 csrc/worker/rec.cpp |   14 ++++++++------
 csrc/wrapper.hpp    |    4 +++-
 cffmpeg.h           |    1 +
 csrc/worker/rec.hpp |    8 +++++---
 libcffmpeg.c        |   13 +++++++++++++
 csrc/cffmpeg.cpp    |    5 +++++
 gorec.go            |    5 +++++
 libcffmpeg.h        |    3 +++
 9 files changed, 47 insertions(+), 10 deletions(-)

diff --git a/cffmpeg.h b/cffmpeg.h
index 3128ee1..ef67716 100644
--- a/cffmpeg.h
+++ b/cffmpeg.h
@@ -18,6 +18,7 @@
 void c_ffmpeg_run_gb28181(const cffmpeg h);
 void c_ffmepg_use_cpu(const cffmpeg h);
 /////////passive api
+void c_ffmpeg_set_record_duration(const cffmpeg h, const int min, const int max);
 void c_ffmpeg_build_recorder(const cffmpeg h, const char*id, const char *dir, int mind, int maxd, int audio);
 void c_ffmpeg_fire_recorder(const cffmpeg h, const char*sid, const int64_t id);
 void c_ffmpeg_get_info_recorder(const cffmpeg h, int *index, char** recid, int *recidLen, char **fpath, int *pathLen);
diff --git a/csrc/cffmpeg.cpp b/csrc/cffmpeg.cpp
index 824b2a7..690f061 100644
--- a/csrc/cffmpeg.cpp
+++ b/csrc/cffmpeg.cpp
@@ -45,6 +45,11 @@
 
 
 //////passive api
+void c_ffmpeg_set_record_duration(const cffmpeg h, const int min, const int max){
+    Wrapper *s = (Wrapper*)h;
+    s->SetRecMinCacheTime(min);
+}
+
 void c_ffmpeg_build_recorder(const cffmpeg h, const char* id, const char *dir, int mind, int maxd, int audio){
     Wrapper *s = (Wrapper*)h;
 
diff --git a/csrc/worker/rec.cpp b/csrc/worker/rec.cpp
index 01896b0..14069ae 100644
--- a/csrc/worker/rec.cpp
+++ b/csrc/worker/rec.cpp
@@ -20,8 +20,8 @@
 {
     rec::rec()
     :recRef_(NULL)
-    ,minduration_(250)
-    ,maxduration_(750)
+    ,min_cache_len_(125)
+    ,time_offset_(4)
     {}
 
     rec::~rec()
@@ -141,9 +141,6 @@
         std::string rid(id);
         std::string dir(output);
         
-        minduration_ = mindur * recRef_->getFPS();
-        maxduration_ = maxdur * recRef_->getFPS();
-
         {
             std::lock_guard<std::mutex> l(mtx_rec_);
             if (map_rec_.find(rid) != map_rec_.end()){
@@ -197,10 +194,15 @@
         shrinkCache();
     }
 
+    void rec::SetRecMinCacheTime(const int min){
+        // 鐢变簬鏁翠釜娴佺▼鑰楁椂,琛ュ伩time_offset_,鍥犱负鏄渶灏忕殑涓�鍗�,鍙湁time_offset_/2
+        min_cache_len_ = (min + time_offset_) * recRef_->getFPS();
+    }
+
     int rec::shrinkCache(){
         //瓒呰繃鏈�澶х紦瀛�,涓㈠純gop
         //缂撳瓨鏈�灏忛暱搴︾殑,鐢ㄤ簬璁板綍
-        int md = minduration_ < 201 ? 200 : minduration_;
+        int md = min_cache_len_ < 200 ? 200 : min_cache_len_;
         while (list_pkt_.size() > md/2) {
             list_pkt_.pop_front();
             while(!list_pkt_.empty()){
diff --git a/csrc/worker/rec.hpp b/csrc/worker/rec.hpp
index a19ea29..c802d9a 100644
--- a/csrc/worker/rec.hpp
+++ b/csrc/worker/rec.hpp
@@ -23,9 +23,9 @@
     {
     private:
         ffwrapper::FormatIn *recRef_;
-        int     maxduration_;
-        int     minduration_;
-
+        int min_cache_len_;
+        // 鏁翠釜娴佺▼鑰楁椂琛ュ伩褰曞埗鏃堕棿,2s榛樿
+        const int time_offset_;
         // 褰曞儚鐨勫疄渚�,瀵瑰簲浠诲姟
         std::unordered_map<std::string, std::unique_ptr<buz::Recorder> > map_rec_;
         // 澶氱嚎绋嬫坊鍔犱换鍔″疄渚�,鍦ㄨ娴佺嚎绋嬩娇鐢ㄥ綍鍍�,浣嗘槸娣诲姞鍦ㄥ彟涓�涓嚎绋�
@@ -71,6 +71,8 @@
         // 鑾峰彇褰曞儚鏂囦欢璺緞鍜屽抚id
         void GetRecInfo(std::string &recID, int &index, std::string &path);
         
+        // 璁剧疆褰曞埗鏃堕暱浣滀负缂撳瓨
+        void SetRecMinCacheTime(const int min);
     public:
         rec();
         ~rec();
diff --git a/csrc/wrapper.cpp b/csrc/wrapper.cpp
index ece3ddf..058e499 100644
--- a/csrc/wrapper.cpp
+++ b/csrc/wrapper.cpp
@@ -225,6 +225,10 @@
         }
     }
 
+    void Wrapper::SetRecMinCacheTime(const int mind){
+        rec_->SetRecMinCacheTime(mind);
+    }
+    
     void Wrapper::BuildRecorder(const char* id, const char *output, const int mindur, const int maxdur, const bool audio){
         bool a = audio;
         if (gb_) a = false;
diff --git a/csrc/wrapper.hpp b/csrc/wrapper.hpp
index ca528f9..4ae1a4f 100644
--- a/csrc/wrapper.hpp
+++ b/csrc/wrapper.hpp
@@ -44,10 +44,12 @@
         void BuildRecorder(const char* id,const char *dir, const int mind, const int maxd, const bool audio);
         int FireRecorder(const char* sid,const int64_t &id);
         void GetInfoRecorder(std::string &recID, int &index, std::string &path);
-        
+    
+    public:
         void GB28181(){gb_ = 1;}
         void CPUDec(){cpu_ = 1;}
         void AudioSwitch(const bool a);
+        void SetRecMinCacheTime(const int mind);
     public: //decoder
         void BuildDecoder();
         void GetPicDecoder(unsigned char **data, int *w, int *h, int *format, int *length, int64_t *id);
diff --git a/gorec.go b/gorec.go
index dcf0f5e..17bcfd8 100644
--- a/gorec.go
+++ b/gorec.go
@@ -52,3 +52,8 @@
 
 	return gID, int(i), path
 }
+
+// SetRecDurationForCache cache
+func (h *GoFFMPEG) SetRecDurationForCache(min, max int) {
+	C.wrap_fn_rec_duration(h.ffmpeg, C.int(min), C.int(max))
+}
diff --git a/libcffmpeg.c b/libcffmpeg.c
index 8f9bd3f..0486921 100644
--- a/libcffmpeg.c
+++ b/libcffmpeg.c
@@ -21,26 +21,35 @@
         release_if_err(fn_create, lib);
         fn_create2 = (lib_cffmpeg_create)dlsym(lib, "c_ffmpeg_create2");
         release_if_err(fn_create2, lib);
+
         fn_destroy = (lib_cffmpeg_destroy)dlsym(lib, "c_ffmpeg_destroy");
         release_if_err(fn_destroy, lib);
+
         fn_run = (lib_cffmpeg_run)dlsym(lib, "c_ffmpeg_run");
         release_if_err(fn_run, lib);
         fn_gb28181 = (lib_cffmpeg_gb28181)dlsym(lib, "c_ffmpeg_run_gb28181");
         release_if_err(fn_gb28181, lib);
+
         fn_cpu = (lib_cffmpeg_cpu)dlsym(lib, "c_ffmepg_use_cpu");
         release_if_err(fn_cpu, lib);
+
+        fn_rec_duration = (lib_cffmpeg_rec_duration)dlsym(lib, "c_ffmpeg_set_record_duration");
+        release_if_err(fn_rec_duration, lib);
         fn_recorder = (lib_cffmpeg_recorder)dlsym(lib, "c_ffmpeg_build_recorder");
         release_if_err(fn_recorder, lib);
         fn_fire_recorder = (lib_cffmpeg_fire_recorder)dlsym(lib, "c_ffmpeg_fire_recorder");
         release_if_err(fn_fire_recorder, lib);
         fn_info_recorder = (lib_cffmpeg_info_recorder)dlsym(lib, "c_ffmpeg_get_info_recorder");
         release_if_err(fn_info_recorder, lib);
+
         fn_decoder = (lib_cffmpeg_decoder)dlsym(lib, "c_ffmpeg_build_decoder");
         release_if_err(fn_decoder, lib);
         fn_decoder_pic = (lib_cffmpeg_pic)dlsym(lib, "c_ffmpeg_get_pic_decoder");
         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_decode = (lib_cffmpeg_decode)dlsym(lib, "c_ffmpeg_decode");
         release_if_err(fn_decode, lib);
 
@@ -101,6 +110,10 @@
     fn_recorder(h, id, dir, mind, maxd, audio);
 }
 
+void wrap_fn_rec_duration(const cffmpeg h, const int min, const int max){
+    fn_rec_duration(h, min, max);
+}
+
 void wrap_fn_fire_recorder(const cffmpeg h, const char* sid, const int64_t id){
     fn_fire_recorder(h, sid, id);
 }
diff --git a/libcffmpeg.h b/libcffmpeg.h
index 13be006..231ff64 100644
--- a/libcffmpeg.h
+++ b/libcffmpeg.h
@@ -16,6 +16,7 @@
 typedef void (*lib_cffmpeg_run)(const cffmpeg, const char*);
 typedef void (*lib_cffmpeg_gb28181)(const cffmpeg);
 typedef void (*lib_cffmpeg_cpu)(const cffmpeg);
+typedef void (*lib_cffmpeg_rec_duration)(const cffmpeg, const int, const int);
 typedef void (*lib_cffmpeg_recorder)(const cffmpeg, const char*, const char*, int, int, int);
 typedef void (*lib_cffmpeg_fire_recorder)(const cffmpeg, const char*, const int64_t);
 typedef void (*lib_cffmpeg_info_recorder)(const cffmpeg, int*, char**, int*, char**, int*);
@@ -31,6 +32,7 @@
 static lib_cffmpeg_run                 fn_run = NULL;
 static lib_cffmpeg_gb28181             fn_gb28181 = NULL;
 static lib_cffmpeg_cpu                 fn_cpu = NULL;
+static lib_cffmpeg_rec_duration        fn_rec_duration = NULL;
 static lib_cffmpeg_recorder            fn_recorder = NULL;
 static lib_cffmpeg_fire_recorder       fn_fire_recorder = NULL;
 static lib_cffmpeg_info_recorder       fn_info_recorder = NULL;
@@ -50,6 +52,7 @@
 void wrap_fn_run(const cffmpeg h, const char* input);
 void wrap_fn_run_gb28181(const cffmpeg h);
 void wrap_fn_use_cpu(const cffmpeg h);
+void wrap_fn_rec_duration(const cffmpeg h, const int min, const int max);
 void wrap_fn_recorder(const cffmpeg h, const char* id, const char* dir, int mind, int maxd, int audio);
 void wrap_fn_fire_recorder(const cffmpeg h, const char *sid, const int64_t id);
 void wrap_fn_info_recorder(const cffmpeg, int* index, char** recid, int* recidLen, char** fpath, int* pathLen);

--
Gitblit v1.8.0