From 7363d175bd62697a800a724f91795e87a5c6fd3c Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期五, 29 十一月 2019 17:21:41 +0800
Subject: [PATCH] add close push stream

---
 csrc/wrapper.cpp |   13 +++++++++----
 csrc/wrapper.hpp |    2 ++
 gostream.go      |    5 +++++
 cffmpeg.h        |    1 +
 libcffmpeg.c     |    8 ++++++++
 csrc/cffmpeg.cpp |    5 +++++
 libcffmpeg.h     |    3 +++
 7 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/cffmpeg.h b/cffmpeg.h
index 5abcf23..de0ae35 100644
--- a/cffmpeg.h
+++ b/cffmpeg.h
@@ -26,6 +26,7 @@
 
 void c_ffmpeg_build_decoder(const cffmpeg h);
 void* c_ffmpeg_get_pic_decoder(const cffmpeg h, int *wid, int *hei, int *format, int *length, int64_t *id);
+void c_ffmpeg_close_stream(const cffmpeg h);
 void* c_ffmpeg_get_avpacket(const cffmpeg h, int *size, int *key);
 
 // pic encoder
diff --git a/csrc/cffmpeg.cpp b/csrc/cffmpeg.cpp
index f42d753..293df1d 100644
--- a/csrc/cffmpeg.cpp
+++ b/csrc/cffmpeg.cpp
@@ -102,6 +102,11 @@
     return data;
 }
 
+void c_ffmpeg_close_stream(const cffmpeg h){
+    Wrapper *s = (Wrapper*)h;
+    s->CloseStream();
+}
+
 void* c_ffmpeg_get_avpacket(const cffmpeg h, int *size, int *key){
     Wrapper *s = (Wrapper*)h;
     unsigned char *data = NULL;
diff --git a/csrc/wrapper.cpp b/csrc/wrapper.cpp
index d1cbfbf..19357c5 100644
--- a/csrc/wrapper.cpp
+++ b/csrc/wrapper.cpp
@@ -46,6 +46,7 @@
     ,gb_(0)
     ,cpu_(0)
     ,run_dec_(false)
+    ,run_stream_(true)
     ,thread_(nullptr)
     ,stop_stream_(false)
     ,stream_(nullptr)
@@ -63,6 +64,7 @@
     ,gb_(0)
     ,cpu_(0)
     ,run_dec_(false)
+    ,run_stream_(true)
     ,thread_(nullptr)
     ,stop_stream_(false)
     ,stream_(nullptr)
@@ -150,7 +152,7 @@
 
         stream_ = new stream(in, 3 * in->getFPS());
         // stream_->AudioSwitch(audio_);
-
+        
         decoder_ = new decoder(in);
 
         rec_->Load(in);
@@ -166,8 +168,8 @@
             p.pts = p.dts = AV_NOPTS_VALUE;
         }
         int flag = 0;
-        if (stream_) stream_->SetPacket(pkt);
-        if (decoder_ && run_dec_) flag = decoder_->SetFrame(pkt);
+        if (run_stream_ && stream_) stream_->SetPacket(pkt);
+        if (run_dec_ && decoder_) flag = decoder_->SetFrame(pkt);
         if (rec_->Loaded()) rec_->SetPacket(pkt);
 
         return flag;
@@ -261,7 +263,6 @@
             [=]{rec_->NewRec(rid.c_str(), dir.c_str(), fid, mindur, maxdur, a);};
         }
     }
-
     int Wrapper::FireRecorder(const char* sid,const int64_t &id){
         if (rec_->Loaded()){
             rec_->FireRecSignal(sid, id);
@@ -284,6 +285,10 @@
         }
     }
     
+    void Wrapper::CloseStream(){
+        run_stream_ = false;
+    }
+
     void Wrapper::GetPacket(unsigned char **pktData, int *size, int *key){
         if (stream_){
             stream_->GetPacket(pktData, size, key);
diff --git a/csrc/wrapper.hpp b/csrc/wrapper.hpp
index 9b866a1..60e1ac8 100644
--- a/csrc/wrapper.hpp
+++ b/csrc/wrapper.hpp
@@ -56,6 +56,7 @@
         void BuildDecoder();
         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);
     private:
         // stream 鍙傛暟
@@ -63,6 +64,7 @@
         bool audio_;
         int gb_, cpu_;
         bool run_dec_;
+        bool run_stream_;
         // decoder 鍙傛暟
         std::unique_ptr<std::thread> thread_;
         std::atomic_bool    stop_stream_;
diff --git a/gostream.go b/gostream.go
index 7ae6636..a0af494 100644
--- a/gostream.go
+++ b/gostream.go
@@ -8,6 +8,11 @@
 
 import "unsafe"
 
+// CloseStream close stream
+func (h *GoFFMPEG) CloseStream() {
+	C.wrap_fn_close_stream(unsafe.Pointer(libcffmpeg), h.ffmpeg)
+}
+
 //GetAVPacket get AVPacket
 func (h *GoFFMPEG) GetAVPacket() ([]byte, int, int) {
 	var key C.int
diff --git a/libcffmpeg.c b/libcffmpeg.c
index 3bebfa6..3e9490a 100644
--- a/libcffmpeg.c
+++ b/libcffmpeg.c
@@ -135,6 +135,14 @@
     return fn_decoder_pic(h, wid, hei, format, length, id);
 }
 
+void wrap_fn_close_stream(void *lib, const cffmpeg h){
+    if (!fn_close_stream){
+        fn_close_stream = (lib_cffmpeg_close_stream)dlsym(lib, "c_ffmpeg_close_stream");
+        if (!fn_close_stream) return;
+    }
+    fn_close_stream(h);
+}
+
 void* wrap_fn_get_avpacket(void *lib, const cffmpeg h, int* size, int* key){
     if(!fn_get_avpacket){
         fn_get_avpacket = (lib_cffmpeg_avpacket)dlsym(lib, "c_ffmpeg_get_avpacket");
diff --git a/libcffmpeg.h b/libcffmpeg.h
index 95523e7..dea90ba 100644
--- a/libcffmpeg.h
+++ b/libcffmpeg.h
@@ -23,6 +23,7 @@
 typedef void (*lib_cffmpeg_info_recorder)(const cffmpeg, int*, char**, int*, char**, int*);
 typedef void (*lib_cffmpeg_decoder)(const cffmpeg);
 typedef void*(*lib_cffmpeg_pic)(const cffmpeg, int*, int*, int*, int*, int64_t*);
+typedef void (*lib_cffmpeg_close_stream)(const cffmpeg);
 typedef void*(*lib_cffmpeg_avpacket)(const cffmpeg, int*, int*);
 
 static lib_cffmpeg_create              fn_create = NULL;
@@ -38,6 +39,7 @@
 static lib_cffmpeg_info_recorder       fn_info_recorder = NULL;
 static lib_cffmpeg_decoder             fn_decoder = NULL;
 static lib_cffmpeg_pic                 fn_decoder_pic = NULL;
+static lib_cffmpeg_close_stream        fn_close_stream = NULL;
 static lib_cffmpeg_avpacket            fn_get_avpacket = NULL;
 
 typedef void* libcffmpeg;
@@ -57,6 +59,7 @@
 void wrap_fn_info_recorder(void *lib, const cffmpeg, int* index, char** recid, int* recidLen, char** fpath, int* pathLen);
 void wrap_fn_decoder(void *lib, const cffmpeg h);
 void* wrap_fn_decoder_pic(void *lib, const cffmpeg h, int *wid, int *hei, int *format, int *length, int64_t *id);
+void wrap_fn_close_stream(void *lib, const cffmpeg h);
 void* wrap_fn_get_avpacket(void *lib, const cffmpeg h, int* size, int* key);
 // for encoder
 typedef void* cencoder;

--
Gitblit v1.8.0