From 54f400a6e97ab732f38543e0741b70fa479de634 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Fri, 29 Jul 2022 16:23:35 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A1=AE=E4=BF=9DMediaServer=E4=B8=BB=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=8F=AF=E4=BB=A5=E5=A4=9A=E6=AC=A1=E8=B0=83=E7=94=A8?= =?UTF-8?q?:=20#1838?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/System.cpp | 57 +++++++++++++++++++++++----------------------- server/WebApi.cpp | 10 +++++++- server/WebHook.cpp | 27 ++++++++++++---------- 3 files changed, 53 insertions(+), 41 deletions(-) diff --git a/server/System.cpp b/server/System.cpp index 86f580b0..aae4c792 100644 --- a/server/System.cpp +++ b/server/System.cpp @@ -17,20 +17,19 @@ #endif//!defined(ANDROID) #endif//!defined(_WIN32) -#include "System.h" #include #include #include #include -#include "Util/logger.h" -#include "Util/NoticeCenter.h" -#include "Util/uv_errno.h" -using namespace toolkit; -using namespace std; -const int MAX_STACK_FRAMES = 128; -#define BroadcastOnCrashDumpArgs int &sig,const vector > &stack -const char kBroadcastOnCrashDump[] = "kBroadcastOnCrashDump"; +#include "Util/logger.h" +#include "Util/uv_errno.h" +#include "System.h" +#include "Common/macros.h" + +using namespace std; +using namespace toolkit; +using namespace mediakit; #ifdef _WIN32 #define popen _popen @@ -53,6 +52,9 @@ string System::execute(const string &cmd) { } #if !defined(ANDROID) && !defined(_WIN32) + +static constexpr int MAX_STACK_FRAMES = 128; + static void sig_crash(int sig) { signal(sig, SIG_DFL); void *array[MAX_STACK_FRAMES]; @@ -76,7 +78,24 @@ static void sig_crash(int sig) { #endif//__linux } free(strings); - NoticeCenter::Instance().emitEvent(kBroadcastOnCrashDump,sig,stack); + + stringstream ss; + ss << "## crash date:" << getTimeStr("%Y-%m-%d %H:%M:%S") << endl; + ss << "## exe: " << exeName() << endl; + ss << "## signal: " << sig << endl; + ss << "## version: " << kServerName << endl; + ss << "## stack: " << endl; + for (size_t i = 0; i < stack.size(); ++i) { + ss << "[" << i << "]: "; + for (auto &str : stack[i]){ + ss << str << endl; + } + } + string stack_info = ss.str(); + ofstream out(StrPrinter << exeDir() << "/crash." << getpid(), ios::out | ios::binary | ios::trunc); + out << stack_info; + out.flush(); + cerr << stack_info << endl; } #endif // !defined(ANDROID) && !defined(_WIN32) @@ -149,24 +168,6 @@ void System::systemSetup(){ signal(SIGABRT, sig_crash); //忽略挂起信号 signal(SIGHUP, SIG_IGN); - NoticeCenter::Instance().addListener(nullptr,kBroadcastOnCrashDump,[](BroadcastOnCrashDumpArgs){ - stringstream ss; - ss << "## crash date:" << getTimeStr("%Y-%m-%d %H:%M:%S") << endl; - ss << "## exe: " << exeName() << endl; - ss << "## signal: " << sig << endl; - ss << "## stack: " << endl; - for (size_t i = 0; i < stack.size(); ++i) { - ss << "[" << i << "]: "; - for (auto &str : stack[i]){ - ss << str << endl; - } - } - string stack_info = ss.str(); - ofstream out(StrPrinter << exeDir() << "/crash." << getpid(), ios::out | ios::binary | ios::trunc); - out << stack_info; - out.flush(); - cerr << stack_info << endl; - }); #endif// ANDROID #endif//!defined(_WIN32) } diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 144b5c14..b5863215 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -220,10 +220,12 @@ extern std::vector getBlockTypeSize(); extern uint64_t getTotalMemBlockByType(int type); extern uint64_t getThisThreadMemBlockByType(int type) ; +static void *web_api_tag = nullptr; + static inline void addHttpListener(){ GET_CONFIG(bool, api_debug, API::kApiDebug); //注册监听kBroadcastHttpRequest事件 - NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastHttpRequest, [](BroadcastHttpRequestArgs) { + NoticeCenter::Instance().addListener(&web_api_tag, Broadcast::kBroadcastHttpRequest, [](BroadcastHttpRequestArgs) { auto it = s_map_api.find(parser.Url()); if (it == s_map_api.end()) { return; @@ -1681,6 +1683,11 @@ void unInstallWebApi(){ s_ffmpegMap.clear(); } + { + lock_guard lck(s_proxyPusherMapMtx); + s_proxyPusherMap.clear(); + } + { #if defined(ENABLE_RTPPROXY) RtpSelector::Instance().clear(); @@ -1688,4 +1695,5 @@ void unInstallWebApi(){ s_rtpServerMap.clear(); #endif } + NoticeCenter::Instance().delListener(&web_api_tag); } diff --git a/server/WebHook.cpp b/server/WebHook.cpp index 711ae4fb..fe9a5516 100755 --- a/server/WebHook.cpp +++ b/server/WebHook.cpp @@ -288,11 +288,13 @@ static void pullStreamFromOrigin(const vector& urls, size_t index, size_ }); } +static void *web_hook_tag = nullptr; + void installWebHook(){ GET_CONFIG(bool,hook_enable,Hook::kEnable); GET_CONFIG(string,hook_adminparams,Hook::kAdminParams); - NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastMediaPublish, [](BroadcastMediaPublishArgs) { + NoticeCenter::Instance().addListener(&web_hook_tag, Broadcast::kBroadcastMediaPublish, [](BroadcastMediaPublishArgs) { GET_CONFIG(string,hook_publish,Hook::kOnPublish); if (!hook_enable || args._param_strs == hook_adminparams || hook_publish.empty() || sender.get_peer_ip() == "127.0.0.1") { invoker("", ProtocolOption()); @@ -354,7 +356,7 @@ void installWebHook(){ }); }); - NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastMediaPlayed,[](BroadcastMediaPlayedArgs){ + NoticeCenter::Instance().addListener(&web_hook_tag,Broadcast::kBroadcastMediaPlayed,[](BroadcastMediaPlayedArgs){ GET_CONFIG(string,hook_play,Hook::kOnPlay); if(!hook_enable || args._param_strs == hook_adminparams || hook_play.empty() || sender.get_peer_ip() == "127.0.0.1"){ invoker(""); @@ -370,7 +372,7 @@ void installWebHook(){ }); }); - NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastFlowReport,[](BroadcastFlowReportArgs){ + NoticeCenter::Instance().addListener(&web_hook_tag,Broadcast::kBroadcastFlowReport,[](BroadcastFlowReportArgs){ GET_CONFIG(string,hook_flowreport,Hook::kOnFlowReport); if(!hook_enable || args._param_strs == hook_adminparams || hook_flowreport.empty() || sender.get_peer_ip() == "127.0.0.1"){ return; @@ -390,7 +392,7 @@ void installWebHook(){ static const string unAuthedRealm = "unAuthedRealm"; //监听kBroadcastOnGetRtspRealm事件决定rtsp链接是否需要鉴权(传统的rtsp鉴权方案)才能访问 - NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastOnGetRtspRealm,[](BroadcastOnGetRtspRealmArgs){ + NoticeCenter::Instance().addListener(&web_hook_tag,Broadcast::kBroadcastOnGetRtspRealm,[](BroadcastOnGetRtspRealmArgs){ GET_CONFIG(string,hook_rtsp_realm,Hook::kOnRtspRealm); if(!hook_enable || args._param_strs == hook_adminparams || hook_rtsp_realm.empty() || sender.get_peer_ip() == "127.0.0.1"){ //无需认证 @@ -413,7 +415,7 @@ void installWebHook(){ }); //监听kBroadcastOnRtspAuth事件返回正确的rtsp鉴权用户密码 - NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastOnRtspAuth,[](BroadcastOnRtspAuthArgs){ + NoticeCenter::Instance().addListener(&web_hook_tag,Broadcast::kBroadcastOnRtspAuth,[](BroadcastOnRtspAuthArgs){ GET_CONFIG(string,hook_rtsp_auth,Hook::kOnRtspAuth); if(unAuthedRealm == realm || !hook_enable || hook_rtsp_auth.empty()){ //认证失败 @@ -440,7 +442,7 @@ void installWebHook(){ //监听rtsp、rtmp源注册或注销事件 - NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastMediaChanged,[](BroadcastMediaChangedArgs){ + NoticeCenter::Instance().addListener(&web_hook_tag,Broadcast::kBroadcastMediaChanged,[](BroadcastMediaChangedArgs){ GET_CONFIG(string,hook_stream_chaned,Hook::kOnStreamChanged); if(!hook_enable || hook_stream_chaned.empty()){ return; @@ -472,7 +474,7 @@ void installWebHook(){ }); //监听播放失败(未找到特定的流)事件 - NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastNotFoundStream, [](BroadcastNotFoundStreamArgs) { + NoticeCenter::Instance().addListener(&web_hook_tag, Broadcast::kBroadcastNotFoundStream, [](BroadcastNotFoundStreamArgs) { if (!origin_urls.empty()) { //设置了源站,那么尝试溯源 static atomic s_index { 0 }; @@ -516,7 +518,7 @@ void installWebHook(){ #ifdef ENABLE_MP4 //录制mp4文件成功后广播 - NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastRecordMP4,[](BroadcastRecordMP4Args){ + NoticeCenter::Instance().addListener(&web_hook_tag,Broadcast::kBroadcastRecordMP4,[](BroadcastRecordMP4Args){ GET_CONFIG(string,hook_record_mp4,Hook::kOnRecordMp4); if (!hook_enable || hook_record_mp4.empty()) { return; @@ -526,7 +528,7 @@ void installWebHook(){ }); #endif //ENABLE_MP4 - NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastRecordTs, [](BroadcastRecordTsArgs) { + NoticeCenter::Instance().addListener(&web_hook_tag, Broadcast::kBroadcastRecordTs, [](BroadcastRecordTsArgs) { GET_CONFIG(string,hook_record_ts,Hook::kOnRecordTs); if (!hook_enable || hook_record_ts.empty()) { return; @@ -535,7 +537,7 @@ void installWebHook(){ do_http_hook(hook_record_ts, getRecordInfo(info), nullptr); }); - NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastShellLogin,[](BroadcastShellLoginArgs){ + NoticeCenter::Instance().addListener(&web_hook_tag,Broadcast::kBroadcastShellLogin,[](BroadcastShellLoginArgs){ GET_CONFIG(string,hook_shell_login,Hook::kOnShellLogin); if(!hook_enable || hook_shell_login.empty() || sender.get_peer_ip() == "127.0.0.1"){ invoker(""); @@ -554,7 +556,7 @@ void installWebHook(){ }); }); - NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastStreamNoneReader,[](BroadcastStreamNoneReaderArgs) { + NoticeCenter::Instance().addListener(&web_hook_tag,Broadcast::kBroadcastStreamNoneReader,[](BroadcastStreamNoneReaderArgs) { if (!origin_urls.empty()) { //边沿站无人观看时立即停止溯源 sender.close(false); @@ -599,7 +601,7 @@ void installWebHook(){ //如果用户客户端不支持cookie,那么ZLMediaKit会根据url参数查找cookie并追踪用户, //如果没有url参数,客户端又不支持cookie,那么会根据ip和端口追踪用户 //追踪用户的目的是为了缓存上次鉴权结果,减少鉴权次数,提高性能 - NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastHttpAccess,[](BroadcastHttpAccessArgs){ + NoticeCenter::Instance().addListener(&web_hook_tag,Broadcast::kBroadcastHttpAccess,[](BroadcastHttpAccessArgs){ GET_CONFIG(string,hook_http_access,Hook::kOnHttpAccess); if(sender.get_peer_ip() == "127.0.0.1" || parser.Params() == hook_adminparams){ //如果是本机或超级管理员访问,那么不做访问鉴权;权限有效期1个小时 @@ -646,4 +648,5 @@ void installWebHook(){ void unInstallWebHook(){ g_keepalive_timer.reset(); + NoticeCenter::Instance().delListener(&web_hook_tag); }