From e6725cb3cf4ced3a6985d4d3db4d5cf1336a30d8 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期五, 19 七月 2019 16:53:35 +0800
Subject: [PATCH] merge so

---
 csrc/wrapper.cpp |   39 ++++++++++++++++---
 csrc/wrapper.hpp |    3 +
 goffmpeg.go      |   34 +++++++++++-----
 cffmpeg.h        |    2 +
 libcffmpeg.c     |   12 ++++++
 csrc/cffmpeg.cpp |   11 +++++
 libcffmpeg.h     |    6 +++
 7 files changed, 89 insertions(+), 18 deletions(-)

diff --git a/cffmpeg.h b/cffmpeg.h
index 1eb6536..c7bf9d1 100644
--- a/cffmpeg.h
+++ b/cffmpeg.h
@@ -17,6 +17,8 @@
 void c_ffmpeg_run(const cffmpeg h, const char *input);
 
 void c_ffmpeg_scale(const cffmpeg h, const int wid, const int hei, const int flags);
+void c_ffmpeg_run_gb28181(const cffmpeg h);
+void c_ffmepg_use_cpu(const cffmpeg h);
 /////////passive api
 void c_ffmpeg_build_recorder(const cffmpeg h, const char *dir, int mind, int maxd);
 void c_ffmpeg_fire_recorder(const cffmpeg h, const int64_t id);
diff --git a/csrc/cffmpeg.cpp b/csrc/cffmpeg.cpp
index 4c5ddb8..b78313e 100644
--- a/csrc/cffmpeg.cpp
+++ b/csrc/cffmpeg.cpp
@@ -36,6 +36,17 @@
     s->ScalePicture(wid, hei, flags);
 }
 
+void c_ffmpeg_run_gb28181(const cffmpeg h){
+    Wrapper *s = (Wrapper*)h;
+    s->UseGB28181();
+}
+
+void c_ffmepg_use_cpu(const cffmpeg h){
+    Wrapper *s = (Wrapper*)h;
+    s->UseCPU();
+}
+
+
 //////passive api
 void c_ffmpeg_build_recorder(const cffmpeg h, const char *dir, int mind, int maxd){
     Wrapper *s = (Wrapper*)h;
diff --git a/csrc/wrapper.cpp b/csrc/wrapper.cpp
index c939599..35137c7 100644
--- a/csrc/wrapper.cpp
+++ b/csrc/wrapper.cpp
@@ -37,7 +37,9 @@
     ,scale_w_(0)
     ,scale_h_(0)
     ,scale_f_(SWS_POINT)
-    ,encoder_(NULL)
+    ,gb_(0)
+    ,cpu_(0)
+    ,encoder_(nullptr)
     {
         makeTheWorld();
     }
@@ -66,11 +68,20 @@
         scale_h_ = h;
     }
 
+    void Wrapper::UseGB28181(){
+        gb_ = 1;
+    }
+
+    void Wrapper::UseCPU(){
+        cpu_ = 1;
+    }
+
     std::unique_ptr<ffwrapper::FormatIn> Wrapper::init_reader(const char* input){
 
         VideoProp prop;
         prop.url_ = input;
         prop.rtsp_tcp_ = true;
+        prop.gpu_acc_ = !cpu_;
 
     	std::unique_ptr<FormatIn> in(new FormatIn(prop.gpuAccl()));
     	AVDictionary *avdic = prop.optsFormat();
@@ -113,8 +124,8 @@
             VideoProp prop;
             prop.url_ = input;
             prop.rtsp_tcp_ = true;
+            prop.gpu_acc_ = !cpu_;
 
-//        	std::unique_ptr<FormatIn> in(new FormatIn(prop.gpuAccl()));
             ffwrapper::FormatIn* in(new FormatIn(prop.gpuAccl()));
         	AVDictionary *avdic = prop.optsFormat();
         	int flag = in->openGb28181(input, &avdic);
@@ -185,15 +196,24 @@
     }
 
     void Wrapper::run_stream_thread(){
+        
+        std::unique_ptr<FormatIn> fi(nullptr);
+
         while(!stop_stream_.load()){
-            //auto in = init_reader(input_url_.c_str());
-            ffwrapper::FormatIn * in = init_reader_gb28181(input_url_.c_str());
+            FormatIn *in = NULL;
+            if (!gb_){
+                fi = init_reader(input_url_.c_str());
+                in = fi.get();
+            }else{
+                in = init_reader_gb28181(input_url_.c_str());
+            }
             if (!in) {
-                logIt("ERROR: init_reader_gb28181!\n");
+                
+                logIt("ERROR: init_reader!\n");
                 usleep(200000);
                 continue;
             }
-            init_worker(in.get());
+            init_worker(in);
 
             int64_t id = 0;
             avpacket pkt;
@@ -207,7 +227,7 @@
                 }
                 pkt.data = data;
 
-                run_worker(in.get(), pkt);
+                run_worker(in, pkt);
                 if(!data){
                     break;
                 }
@@ -216,6 +236,11 @@
                     if(id % 250 == 0)
                         recorder_->FireRecorder(id);
             }
+            if (gb_){
+                delete in;
+            }else{
+                fi.reset(nullptr);
+            }
         }
     }
 
diff --git a/csrc/wrapper.hpp b/csrc/wrapper.hpp
index 1145044..c1b56e0 100644
--- a/csrc/wrapper.hpp
+++ b/csrc/wrapper.hpp
@@ -73,6 +73,8 @@
                                 FUNC_REC func);
 
             void ScalePicture(const int w, const int h, const int flags);
+            void UseGB28181();
+            void UseCPU();
         public: //decoder
             void BuildDecoder();
             void GetPicDecoder(unsigned char **data, int *w, int *h);
@@ -109,6 +111,7 @@
             FUNC_DEC func_dec_;
 
             int scale_w_, scale_h_, scale_f_;
+            int gb_, cpu_;
         //////////////////test frame to bgr24
         public:
             uint8_t *decodeJPEG(const char *file, int *w, int *h);
diff --git a/goffmpeg.go b/goffmpeg.go
index 33869c0..b8739f8 100644
--- a/goffmpeg.go
+++ b/goffmpeg.go
@@ -59,24 +59,36 @@
 	}
 }
 
+// Config config
+type Config struct {
+	Scale  int
+	Width  int
+	Height int
+	GB     bool
+	CPU    bool
+}
+
 // GoFFMPEG handle for c
 type GoFFMPEG struct {
 	ffmpeg C.cffmpeg
 }
 
-// New create handle
-func New() *GoFFMPEG {
-	return &GoFFMPEG{
-		ffmpeg: C.wrap_fn_create(),
-	}
-}
-
-// NewWithScale scale out pic
-func NewWithScale(w, h int, flag int) *GoFFMPEG {
+// New 2nd new
+func New(conf Config) *GoFFMPEG {
 	f := C.wrap_fn_create()
-	if f != nil {
-		C.wrap_fn_scale(f, C.int(w), C.int(h), C.int(flag))
+	if f == nil {
+		return nil
 	}
+	if conf.Scale != 0 && conf.Width != 0 && conf.Height != 0 {
+		C.wrap_fn_scale(f, C.int(conf.Width), C.int(conf.Height), C.int(conf.Scale))
+	}
+	if conf.GB {
+		C.wrap_fn_run_gb28181(f)
+	}
+	if conf.CPU {
+		C.wrap_fn_use_cpu(f)
+	}
+
 	return &GoFFMPEG{
 		ffmpeg: f,
 	}
diff --git a/libcffmpeg.c b/libcffmpeg.c
index 3b3541f..bd66a10 100644
--- a/libcffmpeg.c
+++ b/libcffmpeg.c
@@ -25,6 +25,10 @@
         release_if_err(fn_run, lib);
         fn_scale = (lib_cffmpeg_scale)dlsym(lib, "c_ffmpeg_scale");
         release_if_err(fn_scale, 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_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");
@@ -77,6 +81,14 @@
     fn_scale(h, wid, hei, flags);
 }
 
+void wrap_fn_run_gb28181(const cffmpeg h){
+    fn_gb28181(h);
+}
+
+void wrap_fn_use_cpu(const cffmpeg h){
+    fn_cpu(h);
+}
+
 void wrap_fn_recorder(const cffmpeg h, const char* dir, int mind, int maxd){
     fn_recorder(h, dir, mind, maxd);
 }
diff --git a/libcffmpeg.h b/libcffmpeg.h
index 16cb761..d083536 100644
--- a/libcffmpeg.h
+++ b/libcffmpeg.h
@@ -17,6 +17,8 @@
 typedef void (*lib_cffmpeg_destroy)(const cffmpeg);
 typedef void (*lib_cffmpeg_run)(const cffmpeg, const char*);
 typedef void (*lib_cffmpeg_scale)(const cffmpeg, const int, const int, const int);
+typedef void (*lib_cffmpeg_gb28181)(const cffmpeg);
+typedef void (*lib_cffmpeg_cpu)(const cffmpeg);
 typedef void (*lib_cffmpeg_recorder)(const cffmpeg, const char*, int, int);
 typedef void (*lib_cffmpeg_fire_recorder)(const cffmpeg, const int64_t);
 typedef char*(*lib_cffmpeg_info_recorder)(const cffmpeg, int*, int*);
@@ -30,6 +32,8 @@
 static lib_cffmpeg_destroy             fn_destroy = NULL;
 static lib_cffmpeg_run                 fn_run = NULL;
 static lib_cffmpeg_scale               fn_scale = NULL;
+static lib_cffmpeg_gb28181             fn_gb28181 = NULL;
+static lib_cffmpeg_cpu                 fn_cpu = 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;
@@ -47,6 +51,8 @@
 void wrap_fn_destroy(const cffmpeg h);
 void wrap_fn_run(const cffmpeg h, const char* input);
 void wrap_fn_scale(const cffmpeg h, const int wid, const int hei, const int flags);
+void wrap_fn_run_gb28181(const cffmpeg h);
+void wrap_fn_use_cpu(const cffmpeg h);
 void wrap_fn_recorder(const cffmpeg h, const char* dir, int mind, int maxd);
 void wrap_fn_fire_recorder(const cffmpeg h, const int64_t id);
 char* wrap_fn_info_recorder(const cffmpeg, int*, int*);

--
Gitblit v1.8.0