From 783b80757681c1cc08eeb7aac55f446244d7bc46 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期二, 03 十二月 2019 10:50:21 +0800
Subject: [PATCH] add rec switch

---
 csrc/wrapper.cpp |   10 ++++++++--
 csrc/wrapper.hpp |    5 ++++-
 cffmpeg.h        |    1 +
 libcffmpeg.c     |    8 ++++++++
 csrc/cffmpeg.cpp |    6 +++++-
 gorec.go         |    5 +++++
 libcffmpeg.h     |    3 +++
 7 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/cffmpeg.h b/cffmpeg.h
index de0ae35..e5f7c59 100644
--- a/cffmpeg.h
+++ b/cffmpeg.h
@@ -19,6 +19,7 @@
 void c_ffmpeg_run_gb28181(const cffmpeg h);
 void c_ffmepg_use_cpu(const cffmpeg h);
 /////////passive api
+void c_ffmpeg_open_recorder(const cffmpeg h);
 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, const int64_t fid, int mind, int maxd, int audio);
 void c_ffmpeg_fire_recorder(const cffmpeg h, const char*sid, const int64_t id);
diff --git a/csrc/cffmpeg.cpp b/csrc/cffmpeg.cpp
index 293df1d..3e84f94 100644
--- a/csrc/cffmpeg.cpp
+++ b/csrc/cffmpeg.cpp
@@ -50,6 +50,10 @@
 
 
 //////passive api
+void c_ffmpeg_open_recorder(const cffmpeg h){
+    Wrapper *s = (Wrapper*)h;
+    s->OpenRecorder();
+}
 void c_ffmpeg_set_record_duration(const cffmpeg h, const int min, const int max){
     Wrapper *s = (Wrapper*)h;
     s->SetRecMinCacheTime(min);
@@ -92,7 +96,7 @@
 
 void c_ffmpeg_build_decoder(const cffmpeg h){
     Wrapper *s = (Wrapper*)h;
-    s->BuildDecoder();
+    s->OpenDecoder();
 }
 
 void* c_ffmpeg_get_pic_decoder(const cffmpeg h, int *wid, int *hei, int *format, int *length, int64_t *id){
diff --git a/csrc/wrapper.cpp b/csrc/wrapper.cpp
index 19357c5..9706071 100644
--- a/csrc/wrapper.cpp
+++ b/csrc/wrapper.cpp
@@ -47,6 +47,7 @@
     ,cpu_(0)
     ,run_dec_(false)
     ,run_stream_(true)
+    ,run_rec_(false)
     ,thread_(nullptr)
     ,stop_stream_(false)
     ,stream_(nullptr)
@@ -65,6 +66,7 @@
     ,cpu_(0)
     ,run_dec_(false)
     ,run_stream_(true)
+    ,run_rec_(false)
     ,thread_(nullptr)
     ,stop_stream_(false)
     ,stream_(nullptr)
@@ -170,7 +172,7 @@
         int flag = 0;
         if (run_stream_ && stream_) stream_->SetPacket(pkt);
         if (run_dec_ && decoder_) flag = decoder_->SetFrame(pkt);
-        if (rec_->Loaded()) rec_->SetPacket(pkt);
+        if (run_rec_ && rec_->Loaded()) rec_->SetPacket(pkt);
 
         return flag;
     }
@@ -251,6 +253,10 @@
         rec_->SetRecMinCacheTime(mind);
     }
 
+    void Wrapper::OpenRecorder(){
+        run_rec_ = true;
+    }
+    
     void Wrapper::BuildRecorder(const char* id, const char *output, const int64_t &fid, const int mindur, const int maxdur, const bool audio){
         bool a = audio;
         if (gb_) a = false;
@@ -275,7 +281,7 @@
         }
     }
     ////////decoder
-    void Wrapper::BuildDecoder(){
+    void Wrapper::OpenDecoder(){
         run_dec_ = true;
     }
 
diff --git a/csrc/wrapper.hpp b/csrc/wrapper.hpp
index 60e1ac8..c08eecf 100644
--- a/csrc/wrapper.hpp
+++ b/csrc/wrapper.hpp
@@ -53,11 +53,13 @@
 
         int GetFPS(){return fps_;}
     public: //decoder
-        void BuildDecoder();
+        void OpenDecoder();
         void GetPicDecoder(unsigned char **data, int *w, int *h, int *format, int *length, int64_t *id);
     public: // push stream
         void CloseStream();
         void GetPacket(unsigned char **pktData, int *size, int *key);
+    public: // recorder
+        void OpenRecorder();
     private:
         // stream 鍙傛暟
         std::string input_url_;
@@ -65,6 +67,7 @@
         int gb_, cpu_;
         bool run_dec_;
         bool run_stream_;
+        bool run_rec_;
         // decoder 鍙傛暟
         std::unique_ptr<std::thread> thread_;
         std::atomic_bool    stop_stream_;
diff --git a/gorec.go b/gorec.go
index eb44575..2f17b20 100644
--- a/gorec.go
+++ b/gorec.go
@@ -8,6 +8,11 @@
 
 import "unsafe"
 
+// OpenRecorder rec func open
+func (h *GoFFMPEG) OpenRecorder() {
+	C.wrap_fn_open_rec(unsafe.Pointer(libcffmpeg), h.ffmpeg)
+}
+
 // FireRecorder fire recorder
 func (h *GoFFMPEG) FireRecorder(sid string, id int64) {
 	csid := C.CString(sid)
diff --git a/libcffmpeg.c b/libcffmpeg.c
index 3e9490a..d4f6d7d 100644
--- a/libcffmpeg.c
+++ b/libcffmpeg.c
@@ -87,6 +87,14 @@
     fn_cpu(h);
 }
 
+void wrap_fn_open_rec(void *lib, const cffmpeg h){
+    if (!fn_open_recorder){
+        fn_open_recorder = (lib_cffmpeg_open_recorder)dlsym(lib, "c_ffmpeg_open_recorder");
+        if (!fn_open_recorder) return;
+    }
+    fn_open_recorder(h);
+}
+
 void wrap_fn_recorder(void *lib, const cffmpeg h, const char* id, const char* dir, const int64_t fid, int mind, int maxd, int audio){
     if (!fn_recorder){
         fn_recorder = (lib_cffmpeg_recorder)dlsym(lib, "c_ffmpeg_build_recorder");
diff --git a/libcffmpeg.h b/libcffmpeg.h
index dea90ba..36c09bc 100644
--- a/libcffmpeg.h
+++ b/libcffmpeg.h
@@ -17,6 +17,7 @@
 typedef int (*lib_cffmpeg_fps)(const cffmpeg);
 typedef void (*lib_cffmpeg_gb28181)(const cffmpeg);
 typedef void (*lib_cffmpeg_cpu)(const cffmpeg);
+typedef void (*lib_cffmpeg_open_recorder)(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*, const int64_t, int, int, int);
 typedef void (*lib_cffmpeg_fire_recorder)(const cffmpeg, const char*, const int64_t);
@@ -33,6 +34,7 @@
 static lib_cffmpeg_fps                 fn_fps = NULL;
 static lib_cffmpeg_gb28181             fn_gb28181 = NULL;
 static lib_cffmpeg_cpu                 fn_cpu = NULL;
+static lib_cffmpeg_open_recorder       fn_open_recorder = 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;
@@ -53,6 +55,7 @@
 int wrap_fn_fps(void *lib, const cffmpeg h);
 void wrap_fn_run_gb28181(void *lib, const cffmpeg h);
 void wrap_fn_use_cpu(void *lib, const cffmpeg h);
+void wrap_fn_open_rec(void *lib, const cffmpeg h);
 void wrap_fn_rec_duration(void *lib, const cffmpeg h, const int min, const int max);
 void wrap_fn_recorder(void *lib, const cffmpeg h, const char* id, const char* dir, const int64_t fid, int mind, int maxd, int audio);
 void wrap_fn_fire_recorder(void *lib, const cffmpeg h, const char *sid, const int64_t id);

--
Gitblit v1.8.0