From e62936fa4a4b626a359bce8981ebbbaddcd4aada Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期二, 01 九月 2020 13:31:45 +0800 Subject: [PATCH] add devid --- csrc/ffmpeg/format/FormatIn.cpp | 23 +++++++++++ csrc/wrapper.cpp | 3 + csrc/wrapper.hpp | 7 +++ goffmpeg.go | 61 ++++++++++++++++++++++++++++++ cffmpeg.h | 2 + libcffmpeg.c | 8 ++++ csrc/cffmpeg.cpp | 5 ++ libcffmpeg.h | 5 ++ csrc/ffmpeg/format/FormatIn.hpp | 2 + 9 files changed, 113 insertions(+), 3 deletions(-) diff --git a/cffmpeg.h b/cffmpeg.h index f1eeea7..1eb38cb 100644 --- a/cffmpeg.h +++ b/cffmpeg.h @@ -32,6 +32,8 @@ void* c_ffmpeg_get_avpacket(const cffmpeg h, int *size, int *key); int c_ffmpeg_get_avpacket2(const cffmpeg h, unsigned char **data, int *size, int *key); +int c_ffmpeg_set_devid(const cffmpeg h, const int devid); + void c_ffmpeg_release_buf(void* buf); // pic encoder diff --git a/csrc/cffmpeg.cpp b/csrc/cffmpeg.cpp index e2afc1a..5d94f90 100644 --- a/csrc/cffmpeg.cpp +++ b/csrc/cffmpeg.cpp @@ -134,6 +134,11 @@ return s->GetPacket(data, size, key); } +int c_ffmpeg_set_devid(const cffmpeg h, const int devid){ + Wrapper *s = (Wrapper*)h; + return s->SetDevID(devid); +} + void c_ffmpeg_release_buf(void* buf){ if (buf){ free(buf); diff --git a/csrc/ffmpeg/format/FormatIn.cpp b/csrc/ffmpeg/format/FormatIn.cpp index 28b473b..d0efd33 100644 --- a/csrc/ffmpeg/format/FormatIn.cpp +++ b/csrc/ffmpeg/format/FormatIn.cpp @@ -28,6 +28,7 @@ ,dec_ctx_(NULL) ,vs_idx_(-1) ,as_idx_(-1) + ,prop_(NULL) ,hw_accl_(hw) ,io_ctx_(NULL) ,read_io_buff_(NULL) @@ -36,9 +37,28 @@ ,fps_(25.0) {} + FormatIn::FormatIn(const VideoProp &prop, bool hw/*=true*/) + :ctx_(NULL) + ,dec_ctx_(NULL) + ,vs_idx_(-1) + ,as_idx_(-1) + ,prop_(NULL) + ,hw_accl_(hw) + ,io_ctx_(NULL) + ,read_io_buff_(NULL) + ,read_io_buff_size_(32768) + ,handle_gb28181(NULL) + ,fps_(25.0) + { + prop_ = new VideoProp; + *prop_ = prop; + } + FormatIn::~FormatIn() { logIt("free format in"); + if (prop_) delete prop_; + if(dec_ctx_){ avcodec_close(dec_ctx_); avcodec_free_context(&dec_ctx_); @@ -196,6 +216,9 @@ if(hw_accl_){ // idle_gpu = gpu::getGPUPrior(300, 1024, 0); idle_gpu = gpu::getGPU(300); + if (prop_->gpu_index_ > -1){ + idle_gpu = prop_->gpu_index_; + } if(idle_gpu < 0){ logIt("NO GPU RESOURCE TO DECODE"); hw_accl_ = false; diff --git a/csrc/ffmpeg/format/FormatIn.hpp b/csrc/ffmpeg/format/FormatIn.hpp index e904145..cac25c2 100644 --- a/csrc/ffmpeg/format/FormatIn.hpp +++ b/csrc/ffmpeg/format/FormatIn.hpp @@ -24,6 +24,7 @@ { public: explicit FormatIn(bool hw=true); + explicit FormatIn(const VideoProp &prop, bool hw=true); ~FormatIn(); public: @@ -56,6 +57,7 @@ int vs_idx_; int as_idx_; + VideoProp *prop_; bool hw_accl_; double fps_; private: diff --git a/csrc/wrapper.cpp b/csrc/wrapper.cpp index 4964629..0a0e84f 100644 --- a/csrc/wrapper.cpp +++ b/csrc/wrapper.cpp @@ -45,6 +45,7 @@ ,audio_(false) ,gb_(0) ,cpu_(0) + ,devid_(-1) ,run_dec_(false) ,run_stream_(true) ,run_rec_(false) @@ -66,6 +67,7 @@ ,audio_(false) ,gb_(0) ,cpu_(0) + ,devid_(-1) ,run_dec_(false) ,run_stream_(true) ,run_rec_(false) @@ -108,6 +110,7 @@ prop.url_ = input; prop.rtsp_tcp_ = true; prop.gpu_acc_ = !cpu_; + prop.gpu_index_ = devid_; std::unique_ptr<FormatIn> in(new FormatIn(prop.gpuAccl())); int flag = -1; diff --git a/csrc/wrapper.hpp b/csrc/wrapper.hpp index 4802e8e..e2d9a8b 100644 --- a/csrc/wrapper.hpp +++ b/csrc/wrapper.hpp @@ -60,11 +60,16 @@ int GetPacket(unsigned char **pktData, int *size, int *key); public: // recorder void OpenRecorder(); + + public: + // set device id + int SetDevID(const int devid){ devid_ = devid; return 0;} + private: // stream 鍙傛暟 std::string input_url_; bool audio_; - int gb_, cpu_; + int gb_, cpu_, devid_; bool run_dec_; bool run_stream_; bool run_rec_; diff --git a/goffmpeg.go b/goffmpeg.go index bca75d2..136a1ba 100644 --- a/goffmpeg.go +++ b/goffmpeg.go @@ -92,6 +92,65 @@ } } +// NewWithDevID 2nd new +func NewWithDevID(GB, CPU bool, devID int) *GoFFMPEG { + + f := C.wrap_fn_create(unsafe.Pointer(libcffmpeg)) + + if f == nil { + return nil + } + if GB { + C.wrap_fn_run_gb28181(unsafe.Pointer(libcffmpeg), f) + } + if CPU { + C.wrap_fn_use_cpu(unsafe.Pointer(libcffmpeg), f) + } else if devID > -1 { + r := C.wrap_fn_set_devid(unsafe.Pointer(libcffmpeg), f, C.int(devID)) + if r != 0 { + if f != nil { + C.wrap_fn_destroy(unsafe.Pointer(libcffmpeg), f) + } + FreeFFmpeg() + } + return nil + } + + return &GoFFMPEG{ + ffmpeg: f, + } +} + +// NewWithLogAndDevID log +func NewWithLogAndDevID(GB, CPU bool, devID int, ffmpegLog string) *GoFFMPEG { + lf := C.CString(ffmpegLog) + defer C.free(unsafe.Pointer(lf)) + + f := C.wrap_fn_create2(unsafe.Pointer(libcffmpeg), lf) + if f == nil { + return nil + } + if GB { + C.wrap_fn_run_gb28181(unsafe.Pointer(libcffmpeg), f) + } + if CPU { + C.wrap_fn_use_cpu(unsafe.Pointer(libcffmpeg), f) + } else if devID > -1 { + r := C.wrap_fn_set_devid(unsafe.Pointer(libcffmpeg), f, C.int(devID)) + if r != 0 { + if f != nil { + C.wrap_fn_destroy(unsafe.Pointer(libcffmpeg), f) + } + FreeFFmpeg() + } + return nil + } + + return &GoFFMPEG{ + ffmpeg: f, + } +} + // NewWithLog log func NewWithLog(GB, CPU bool, ffmpegLog string) *GoFFMPEG { lf := C.CString(ffmpegLog) @@ -150,7 +209,7 @@ } // GetGBJpg Get GB28181 Jpg -func GetGBJpg(rtspUrl string) []byte { +func GetGBJpg(rtspURL string) []byte { rtsp := C.CString(rtspUrl) defer C.free(unsafe.Pointer(rtsp)) var jpgLen C.int diff --git a/libcffmpeg.c b/libcffmpeg.c index 70be86b..c8298a8 100644 --- a/libcffmpeg.c +++ b/libcffmpeg.c @@ -178,6 +178,14 @@ return fn_get_avpacket2(h, data, size, key); } +int wrap_fn_set_devid(void *lib, const cffmpeg h, const int devid){ + if (!fn_set_devid){ + fn_set_devid = (lib_cffmpeg_devid)dlsym(lib, "c_ffmpeg_set_devid"); + if (!fn_set_devid) return -1; + } + return fn_set_devid(h, devid); +} + void wrap_fn_release_buf(void *lib, void *buf){ if (!fn_release_buf){ fn_release_buf = (lib_cffmpeg_release_buf)dlsym(lib, "c_ffmpeg_release_buf"); diff --git a/libcffmpeg.h b/libcffmpeg.h index b1d010c..d11e658 100644 --- a/libcffmpeg.h +++ b/libcffmpeg.h @@ -28,6 +28,7 @@ typedef void (*lib_cffmpeg_close_stream)(const cffmpeg); typedef void*(*lib_cffmpeg_avpacket)(const cffmpeg, int*, int*); typedef int (*lib_cffmpeg_avpacket2)(const cffmpeg, unsigned char**, int*, int*); +typedef int (*lib_cffmpeg_devid)(const cffmpeg, const int devid); typedef void(*lib_cffmpeg_release_buf) (void*); static lib_cffmpeg_create fn_create = NULL; @@ -47,7 +48,8 @@ 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; -static lib_cffmpeg_avpacket2 fn_get_avpacket2 = NULL; +static lib_cffmpeg_avpacket2 fn_get_avpacket2 = NULL; +static lib_cffmpeg_devid fn_set_devid = NULL; static lib_cffmpeg_release_buf fn_release_buf = NULL; typedef void* libcffmpeg; @@ -72,6 +74,7 @@ void wrap_fn_close_stream(void *lib, const cffmpeg h); void* wrap_fn_get_avpacket(void *lib, const cffmpeg h, int* size, int* key); int wrap_fn_get_avpacket2(void *lib, const cffmpeg h, unsigned char **data, int* size, int* key); +int wrap_fn_set_devid(void *lib, const cffmpeg h, const int devid); void wrap_fn_release_buf(void *lib, void *buf); // for encoder -- Gitblit v1.8.0