From 92bc4fbbb15537cf81b0df3a5aee026ed0986733 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Mon, 23 Dec 2019 14:20:49 +0800 Subject: [PATCH] =?UTF-8?q?c=20api=E6=B5=8B=E8=AF=95=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E9=98=B2=E6=AD=A2=E9=80=80=E5=87=BA=E6=97=B6=E5=B4=A9=E6=BA=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rdpart/ZLToolKit | 2 +- api/include/common.h | 5 +++++ api/source/common.cpp | 8 ++++++++ api/tests/server.cpp | 4 ++-- src/Record/Recorder.cpp | 11 +++++++---- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/3rdpart/ZLToolKit b/3rdpart/ZLToolKit index e1b29b7f..4d88a395 160000 --- a/3rdpart/ZLToolKit +++ b/3rdpart/ZLToolKit @@ -1 +1 @@ -Subproject commit e1b29b7fa22e18bb0976a281268c0431a411e7ce +Subproject commit 4d88a395a25e9265c579aa930acdf60796bd4e4c diff --git a/api/include/common.h b/api/include/common.h index 972188c2..033bc077 100755 --- a/api/include/common.h +++ b/api/include/common.h @@ -71,6 +71,11 @@ typedef struct { */ API_EXPORT void API_CALL mk_env_init(const mk_config *cfg); +/** + * 关闭所有服务器,请在main函数退出时调用 + */ +API_EXPORT void API_CALL mk_stop_all_server(); + /** * 基础类型参数版本的mk_env_init,为了方便其他语言调用 */ diff --git a/api/source/common.cpp b/api/source/common.cpp index 6dfc99fb..78a0f849 100755 --- a/api/source/common.cpp +++ b/api/source/common.cpp @@ -62,6 +62,14 @@ API_EXPORT void API_CALL mk_env_init(const mk_config *cfg) { cfg->ssl_pwd); } +API_EXPORT void API_CALL mk_stop_all_server(){ + CLEAR_ARR(rtsp_server); + CLEAR_ARR(rtmp_server); + CLEAR_ARR(http_server); + udpRtpServer = nullptr; + tcpRtpServer = nullptr; +} + API_EXPORT void API_CALL mk_env_init1( int thread_num, int log_level, int ini_is_path, diff --git a/api/tests/server.cpp b/api/tests/server.cpp index 10bad23e..8a188905 100644 --- a/api/tests/server.cpp +++ b/api/tests/server.cpp @@ -11,11 +11,11 @@ int main(int argc,char *argv[]){ mk_rtsp_server_start(554,false); mk_rtmp_server_start(1935,false); mk_rtp_server_start(10000); - static bool flag = true; signal(SIGINT, [](int) { flag = false; });// 设置退出信号 - while (flag){ sleep(1); } + mk_stop_all_server(); + return 0; } \ No newline at end of file diff --git a/src/Record/Recorder.cpp b/src/Record/Recorder.cpp index 42d145e3..43baa0e4 100644 --- a/src/Record/Recorder.cpp +++ b/src/Record/Recorder.cpp @@ -217,21 +217,23 @@ public: private: MediaSourceWatcher(){ - NoticeCenter::Instance().addListener(this,Broadcast::kBroadcastMediaChanged,[this](BroadcastMediaChangedArgs){ + //保存NoticeCenter的强引用,防止在MediaSourceWatcher单例释放前释放NoticeCenter单例 + _notice_center = NoticeCenter::Instance().shared_from_this(); + _notice_center->addListener(this,Broadcast::kBroadcastMediaChanged,[this](BroadcastMediaChangedArgs){ if(bRegist){ onRegist(schema,vhost,app,stream,sender); }else{ onUnRegist(schema,vhost,app,stream,sender); } }); - NoticeCenter::Instance().addListener(this,Broadcast::kBroadcastMediaResetTracks,[this](BroadcastMediaResetTracksArgs){ + _notice_center->addListener(this,Broadcast::kBroadcastMediaResetTracks,[this](BroadcastMediaResetTracksArgs){ onRegist(schema,vhost,app,stream,sender); }); } ~MediaSourceWatcher(){ - NoticeCenter::Instance().delListener(this,Broadcast::kBroadcastMediaChanged); - NoticeCenter::Instance().delListener(this,Broadcast::kBroadcastMediaResetTracks); + _notice_center->delListener(this,Broadcast::kBroadcastMediaChanged); + _notice_center->delListener(this,Broadcast::kBroadcastMediaResetTracks); } void onRegist(const string &schema,const string &vhost,const string &app,const string &stream,MediaSource &sender){ @@ -320,6 +322,7 @@ private: } private: recursive_mutex _recorder_mtx; + NoticeCenter::Ptr _notice_center; unordered_map _recorder_map; };