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; };