From f93ee1a42e8c47e472332287b7350b66a6b0fa11 Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期五, 24 七月 2020 18:28:57 +0800 Subject: [PATCH] 保存触发id之后的视频作为触发视频 --- csrc/ffmpeg/format/FormatIn.cpp | 2 +- csrc/worker/rec.cpp | 11 ++++++++--- cffmpeg.h | 2 ++ csrc/worker/rec.hpp | 2 +- libcffmpeg.c | 7 +++++++ csrc/cffmpeg.cpp | 6 ++++++ CMakeLists.txt | 5 +++-- libcffmpeg.h | 4 ++++ 8 files changed, 32 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 51c0d6c..6068781 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,8 +4,9 @@ project(${BIN}) -# set(CMAKE_C_VISIBILITY_PRESET hidden) -# set(CMAKE_CXX_VISIBILITY_PRESET hidden) +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE "Release") +endif() set(CMAKE_CXX_FLAGS "-DLIB_CFFMPEG -fPIC -Wl,-Bsymbolic ${CMAKE_CXX_FLAGS}") set(CMAKE_C_FLAGS "-DLIB_CFFMPEG -fPIC -Wl,-Bsymbolic ${CMAKE_C_FLAGS}") diff --git a/cffmpeg.h b/cffmpeg.h index bb3d0be..f1eeea7 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); +void c_ffmpeg_release_buf(void* buf); + // pic encoder void *c_ffmpeg_create_encoder(const int w, const int h, const int fps, const int br, const int pix_fmt, const int scale_flag, const int gi); void c_ffmpeg_destroy_encoder(void *h); diff --git a/csrc/cffmpeg.cpp b/csrc/cffmpeg.cpp index 8d872c4..e2afc1a 100644 --- a/csrc/cffmpeg.cpp +++ b/csrc/cffmpeg.cpp @@ -134,6 +134,12 @@ return s->GetPacket(data, size, key); } +void c_ffmpeg_release_buf(void* buf){ + if (buf){ + free(buf); + } +} + // pic encoder void *c_ffmpeg_create_encoder(const int w, const int h, const int fps, const int br, const int pix_fmt, const int scale_flag, const int gi){ return CreateEncoder(w, h, fps, br, pix_fmt, scale_flag, gi); diff --git a/csrc/ffmpeg/format/FormatIn.cpp b/csrc/ffmpeg/format/FormatIn.cpp index 32b9525..28b473b 100644 --- a/csrc/ffmpeg/format/FormatIn.cpp +++ b/csrc/ffmpeg/format/FormatIn.cpp @@ -56,7 +56,7 @@ if(io_ctx_){ av_freep(&io_ctx_->buffer); - avio_context_free(&io_ctx_); + av_freep(&io_ctx_); io_ctx_ = NULL; } diff --git a/csrc/worker/rec.cpp b/csrc/worker/rec.cpp index b5bbd5a..28a7401 100644 --- a/csrc/worker/rec.cpp +++ b/csrc/worker/rec.cpp @@ -45,7 +45,7 @@ list_recInfo_.emplace_back(info); } - void rec::findRecFramesIndex(const int64_t &fired_id, const int duration, int &start, int &end){ + void rec::findRecFramesIndex(int64_t &fired_id, const int duration, int &start, int &end){ start = end = -1; @@ -84,6 +84,9 @@ } + // 濡傛灉fired_id鍦╯tartid涔嬪墠,灏嗗叾鏀惧叆start_id涔嬪悗 + if (start_id > fired_id) fired_id = start_id + duration / 2; + if (end < 0) end = index; } @@ -113,8 +116,10 @@ std::lock_guard<std::mutex> locker(mtx_pkt_); logIt("cache size: %ld", list_pkt_.size()); // 棣栨鑾峰彇褰曞儚淇℃伅,鍏堝瓨涓�涓渶鐭椂闀� - findRecFramesIndex(frameID, duration, start, end); - rec->StartWritePacket(list_pkt_, frameID, start, end); + // maybe fired_id changed by findRecFramesIndex + int64_t fired_id = frameID; + findRecFramesIndex(fired_id, duration, start, end); + rec->StartWritePacket(list_pkt_, fired_id, start, end); return rec; } diff --git a/csrc/worker/rec.hpp b/csrc/worker/rec.hpp index fc6ba0d..320832c 100644 --- a/csrc/worker/rec.hpp +++ b/csrc/worker/rec.hpp @@ -47,7 +47,7 @@ private: // 鏌ユ壘缂撳瓨涓殑褰曞埗甯� - void findRecFramesIndex(const int64_t &fired_id, const int duration, int &start, int &end); + void findRecFramesIndex(int64_t &fired_id, const int duration, int &start, int &end); // 褰曞儚瀹炰緥鐨勫洖璋冨嚱鏁�,褰曞儚瀹屾垚鍚庤缃綍鍍忔枃浠惰矾寰�,id鍜屽抚id void setRecInfo(std::string &id, int &index, std::string &path); diff --git a/libcffmpeg.c b/libcffmpeg.c index 8398ea7..70be86b 100644 --- a/libcffmpeg.c +++ b/libcffmpeg.c @@ -177,6 +177,13 @@ } return fn_get_avpacket2(h, data, size, key); } + +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"); + } + if (fn_release_buf) fn_release_buf(buf); +} // for encoder cencoder wrap_fn_create_encoder(void *lib, const int w, const int h, const int fps, const int br, const int pix_fmt, const int scale_flag, const int gi){ if (!fn_create_encoder){ diff --git a/libcffmpeg.h b/libcffmpeg.h index d15ed21..b1d010c 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 void(*lib_cffmpeg_release_buf) (void*); static lib_cffmpeg_create fn_create = NULL; static lib_cffmpeg_create2 fn_create2 = NULL; @@ -47,6 +48,7 @@ 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_release_buf fn_release_buf = NULL; typedef void* libcffmpeg; libcffmpeg init_libcffmpeg(const char *so_file); @@ -70,6 +72,8 @@ 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); +void wrap_fn_release_buf(void *lib, void *buf); + // for encoder typedef void* cencoder; typedef cencoder (*lib_cffmpeg_create_encoder)(const int w, const int h, const int fps, const int br, const int pix_fmt, const int scale_flag, const int gi); -- Gitblit v1.8.0