From 0367d8e503ca5df1eae359929b8753050ccd9980 Mon Sep 17 00:00:00 2001
From: chenshijun <csj_sky@126.com>
Date: 星期三, 17 七月 2019 13:37:26 +0800
Subject: [PATCH] 增加国标28181的处理

---
 csrc/ffmpeg/format/FormatIn.cpp    |   31 +++++++++++++--
 csrc/wrapper.cpp                   |   48 +++++++++++++++++++++++
 csrc/wrapper.hpp                   |    1 
 CMakeLists.txt                     |    5 +-
 csrc/ffmpeg/filter/FilterVideo.cpp |    2 
 csrc/ffmpeg/format/FormatIn.hpp    |    3 +
 6 files changed, 81 insertions(+), 9 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 66c98bf..2e84a40 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -21,9 +21,10 @@
     ${CMAKE_SOURCE_DIR}/csrc/thirdparty 
     ${CMAKE_SOURCE_DIR}/csrc/thirdparty/whereami
     ${CMAKE_SOURCE_DIR}/csrc/thirdparty/ffmpeg/include
+    ${CMAKE_SOURCE_DIR}/csrc/thirdparty/gb28181/include
 )
 
-link_directories(/usr/local/cuda/lib64)
+link_directories(/usr/local/cuda/lib64 ${CMAKE_SOURCE_DIR}/csrc/thirdparty/gb28181/lib)
 
 set(CMAKE_LIBRARY_PATH ${CMAKE_SOURCE_DIR}/csrc/thirdparty/ffmpeg/lib ${CMAKE_LIBRARY_PATH})
     find_library(avutil NAMES libavutil.a )
@@ -49,4 +50,4 @@
 
 add_library(${BIN} SHARED ${FFMPEG_LIST} ${CMAKE_SOURCE_DIR}/csrc/cffmpeg.cpp)
 
-target_link_libraries(${BIN} ${LINK_LIB} numa nppig nppicc nppc -lz pthread dl)
+target_link_libraries(${BIN} ${LINK_LIB} numa nppig nppicc nppc -lz pthread dl rtspclient StreamParse)
diff --git a/csrc/ffmpeg/filter/FilterVideo.cpp b/csrc/ffmpeg/filter/FilterVideo.cpp
index 387e0e7..f5123b6 100644
--- a/csrc/ffmpeg/filter/FilterVideo.cpp
+++ b/csrc/ffmpeg/filter/FilterVideo.cpp
@@ -6,7 +6,7 @@
 #include <libavutil/opt.h>
 #include <libavcodec/avcodec.h>
 #include <libavfilter/avfilter.h>
-#include <libavfilter/avfiltergraph.h>
+//#include <libavfilter/avfiltergraph.h>
 #include <libavfilter/buffersink.h>
 #include <libavfilter/buffersrc.h>
 }
diff --git a/csrc/ffmpeg/format/FormatIn.cpp b/csrc/ffmpeg/format/FormatIn.cpp
index 9ebdb3d..81d6c5f 100644
--- a/csrc/ffmpeg/format/FormatIn.cpp
+++ b/csrc/ffmpeg/format/FormatIn.cpp
@@ -21,6 +21,7 @@
 #include "../data/FrameData.hpp"
 
 #include "../../common/gpu/info.h"
+#include "PsToEs.hpp"
 
 using namespace logif;
 
@@ -52,7 +53,7 @@
 	}
 
 ////////////////////////////////////////////////////////////////////////
-	int FormatIn::openWithCustomIO(read_packet fn, AVDictionary **options/*=NULL*/){
+	int FormatIn::openWithCustomIO(void *opaque, read_packet fn, AVDictionary **options/*=NULL*/){
 		ctx_ = avformat_alloc_context();
 		if(!ctx_){
 			logIt("open with custom io create format error");
@@ -64,21 +65,43 @@
 			return -1;
 		}
 
-		io_ctx_ = avio_alloc_context(read_io_buff_, read_io_buff_size_, 0, NULL, fn, NULL, NULL);
+		io_ctx_ = avio_alloc_context(read_io_buff_, read_io_buff_size_, 0, opaque, fn, NULL, NULL);//opaque
 		if(!io_ctx_){
 			logIt("open with custom io create custom avio error");
 			return -1;
 		}
 		ctx_->pb = io_ctx_;
 
-		auto err = av_probe_input_buffer(io_ctx_, &ctx_->iformat, NULL, NULL, 0, read_io_buff_size_);
+		auto err = av_probe_input_buffer(ctx_->pb, &ctx_->iformat, NULL, NULL, 0, read_io_buff_size_);
 		if(err != 0){
-			logIt("open with custom io prob input buffer error");
+			logIt("open with custom io prob input buffer error:%d", err);
+            logIt("failed:%s", getAVErrorDesc(err).c_str());
 			return -1;
 		}
+
 		return 0;
 	}
 
+	int FormatIn::openGb28181(const char *filename, AVDictionary **options){
+
+	    std::string fn = filename;
+		addCamera(fn);
+
+        int ret = openWithCustomIO(NULL, readData, options);
+        if(ret < 0){
+            logIt("do openWithCustomIO failed:%d",ret);
+        }
+
+        ret = avformat_open_input(&ctx_, "", NULL, options);
+        if(ret < 0){
+            logIt("open %s failed:%s",filename,
+                  getAVErrorDesc(ret).c_str());
+
+        }
+
+		return ret;
+	}
+
 /////////////////////////////////////////////////////////////////////////
 	int FormatIn::open(const char *filename, AVDictionary **options){
 
diff --git a/csrc/ffmpeg/format/FormatIn.hpp b/csrc/ffmpeg/format/FormatIn.hpp
index 32c8a81..a19d0fe 100644
--- a/csrc/ffmpeg/format/FormatIn.hpp
+++ b/csrc/ffmpeg/format/FormatIn.hpp
@@ -28,7 +28,8 @@
 		~FormatIn();
 		
 	public:
-		int openWithCustomIO(read_packet fn, AVDictionary **options=NULL);
+		int openWithCustomIO(void *opaque, read_packet fn, AVDictionary **options=NULL);
+        int openGb28181(const char *filename, AVDictionary **options);
 
 		int open(const char *filename, AVDictionary **options);
 		bool findStreamInfo(AVDictionary **options);
diff --git a/csrc/wrapper.cpp b/csrc/wrapper.cpp
index 321a66a..9b4a88a 100644
--- a/csrc/wrapper.cpp
+++ b/csrc/wrapper.cpp
@@ -103,6 +103,50 @@
         return nullptr;
     }
 
+    ffwrapper::FormatIn* Wrapper::init_reader_gb28181(const char* input){
+
+            VideoProp prop;
+            prop.url_ = input;
+            prop.rtsp_tcp_ = true;
+
+//        	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);
+        	if(avdic){
+                logIt("ERROR:in->openGb28181(input, &avdic):flag:%d\n",flag);
+        		av_dict_free(&avdic);
+        	}
+            if(flag == 0){
+                if(!in->findStreamInfo(NULL)){
+                    logIt("yolo can't find video stream\n");
+                    return nullptr;
+                }
+                bool flag = true;
+                if(map_workers_.find(WORKER_DECODER) != map_workers_.end()){
+                    flag = in->openCodec(AVMEDIA_TYPE_VIDEO, NULL);
+                    auto dec_ctx = in->getCodecContext();
+                    if(bridge_)delete bridge_;
+
+                    scale_w_ = scale_w_ == 0 || scale_w_ > dec_ctx->width ? dec_ctx->width : scale_w_;
+                    scale_h_ = scale_h_ == 0 || scale_h_ > dec_ctx->height ? dec_ctx->height : scale_h_;
+
+                    AVPixelFormat pix_fmt = AV_PIX_FMT_BGR24;
+                    bridge_ = new cvbridge(
+                            dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
+                            scale_w_, scale_h_, pix_fmt, scale_f_);
+
+                }
+                if (!flag){
+                    logIt("FormatIn openCodec Failed!");
+                    return nullptr;
+                }
+                return in;
+            }
+
+            return nullptr;
+        }
+
     int Wrapper::init_recorder(FormatIn *in, std::string dir, const int mind, const int maxd){
         if(!in){
             logIt("Init wrapper first");
@@ -137,8 +181,10 @@
 
     void Wrapper::run_stream_thread(){
         while(!stop_stream_.load()){
-            auto in = init_reader(input_url_.c_str());
+            //auto in = init_reader(input_url_.c_str());
+            ffwrapper::FormatIn * in = init_reader_gb28181(input_url_.c_str());
             if (!in) {
+                logIt("ERROR: init_reader_gb28181!\n");
                 usleep(200000);
                 continue;
             }
diff --git a/csrc/wrapper.hpp b/csrc/wrapper.hpp
index 10f699f..f242ee7 100644
--- a/csrc/wrapper.hpp
+++ b/csrc/wrapper.hpp
@@ -49,6 +49,7 @@
 
         private: 
             std::unique_ptr<ffwrapper::FormatIn> init_reader(const char* input);
+            ffwrapper::FormatIn* init_reader_gb28181(const char* input);
             void init_worker(ffwrapper::FormatIn *in);
             int init_recorder(ffwrapper::FormatIn *in, std::string dir, const int mind, const int maxd);
             void run_worker(ffwrapper::FormatIn *in, buz::avpacket &pkt);

--
Gitblit v1.8.0