From 69c3b24d06a4c3fa91f732ae4ea5425c24ff5562 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Wed, 30 Jun 2021 21:06:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=81=8D=E5=8E=86MediaSource?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/include/mk_events_objects.h | 3 ++- api/source/mk_events_objects.cpp | 9 ++++---- server/WebApi.cpp | 32 ++++---------------------- src/Common/MediaSource.cpp | 39 ++++++++++++++++++++++++++++---- src/Common/MediaSource.h | 6 ++++- src/Shell/ShellCMD.h | 18 +-------------- 6 files changed, 51 insertions(+), 56 deletions(-) diff --git a/api/include/mk_events_objects.h b/api/include/mk_events_objects.h index 485f4704..168d9b35 100644 --- a/api/include/mk_events_objects.h +++ b/api/include/mk_events_objects.h @@ -133,7 +133,8 @@ API_EXPORT void API_CALL mk_media_source_find(const char *schema, void *user_data, on_mk_media_source_find_cb cb); //MediaSource::for_each_media() -API_EXPORT void API_CALL mk_media_source_for_each(void *user_data, on_mk_media_source_find_cb cb); +API_EXPORT void API_CALL mk_media_source_for_each(void *user_data, on_mk_media_source_find_cb cb, const char *schema, + const char *vhost, const char *app, const char *stream); ///////////////////////////////////////////HttpBody///////////////////////////////////////////// //HttpBody对象的C映射 diff --git a/api/source/mk_events_objects.cpp b/api/source/mk_events_objects.cpp index f6f0feee..399f025c 100644 --- a/api/source/mk_events_objects.cpp +++ b/api/source/mk_events_objects.cpp @@ -238,11 +238,12 @@ API_EXPORT void API_CALL mk_media_source_find(const char *schema, cb(user_data, src.get()); } -API_EXPORT void API_CALL mk_media_source_for_each(void *user_data, on_mk_media_source_find_cb cb){ +API_EXPORT void API_CALL mk_media_source_for_each(void *user_data, on_mk_media_source_find_cb cb, const char *schema, + const char *vhost, const char *app, const char *stream) { assert(cb); - MediaSource::for_each_media([&](const MediaSource::Ptr &src){ - cb(user_data,src.get()); - }); + MediaSource::for_each_media([&](const MediaSource::Ptr &src) { + cb(user_data, src.get()); + }, schema ? schema : "", vhost ? vhost : "", app ? app : "", stream ? stream : ""); } ///////////////////////////////////////////HttpBody///////////////////////////////////////////// diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 3666fed1..c8e2ea16 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -483,21 +483,9 @@ void installWebApi() { api_regist("/index/api/getMediaList",[](API_ARGS_MAP){ CHECK_SECRET(); //获取所有MediaSource列表 - MediaSource::for_each_media([&](const MediaSource::Ptr &media){ - if (!allArgs["schema"].empty() && allArgs["schema"] != media->getSchema()) { - return; - } - if (!allArgs["vhost"].empty() && allArgs["vhost"] != media->getVhost()) { - return; - } - if (!allArgs["app"].empty() && allArgs["app"] != media->getApp()) { - return; - } - if (!allArgs["stream"].empty() && allArgs["stream"] != media->getId()) { - return; - } + MediaSource::for_each_media([&](const MediaSource::Ptr &media) { val["data"].append(makeMediaSourceJson(*media)); - }); + }, allArgs["schema"], allArgs["vhost"], allArgs["app"], allArgs["stream"]); }); //测试url http://127.0.0.1/index/api/isMediaOnline?schema=rtsp&vhost=__defaultVhost__&app=live&stream=obs @@ -551,22 +539,10 @@ void installWebApi() { int count_hit = 0; int count_closed = 0; list media_list; - MediaSource::for_each_media([&](const MediaSource::Ptr &media){ - if(!allArgs["schema"].empty() && allArgs["schema"] != media->getSchema()){ - return; - } - if(!allArgs["vhost"].empty() && allArgs["vhost"] != media->getVhost()){ - return; - } - if(!allArgs["app"].empty() && allArgs["app"] != media->getApp()){ - return; - } - if(!allArgs["stream"].empty() && allArgs["stream"] != media->getId()){ - return; - } + MediaSource::for_each_media([&](const MediaSource::Ptr &media) { ++count_hit; media_list.emplace_back(media); - }); + }, allArgs["schema"], allArgs["vhost"], allArgs["app"], allArgs["stream"]); bool force = allArgs["force"].as(); for(auto &media : media_list){ diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index 8d2ff3b4..f9305410 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -206,7 +206,18 @@ bool MediaSource::stopSendRtp(const string &ssrc) { return listener->stopSendRtp(*this, ssrc); } -void MediaSource::for_each_media(const function &cb) { +static void do_for_each(const function &cb, weak_ptr &ptr){ + auto src = ptr.lock(); + if (src) { + cb(src); + } +} + +void MediaSource::for_each_media(const function &cb, + const string &schema, + const string &vhost, + const string &app, + const string &stream) { decltype(s_media_source_map) copy; { //拷贝s_media_source_map后再遍历,考虑到是高频使用的全局单例锁,并且在上锁时会执行回调代码 @@ -216,13 +227,31 @@ void MediaSource::for_each_media(const functionsecond); } + continue; + } + for (auto &pr3 : pr2.second) { + //未指定stream id, 遍历stream + do_for_each(cb, pr3.second); } } } diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index c16acba1..7f417bca 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -273,7 +273,11 @@ public: // 异步查找流 static void findAsync(const MediaInfo &info, const std::shared_ptr &session, const function &cb); // 遍历所有流 - static void for_each_media(const function &cb); + static void for_each_media(const function &cb, + const string &schema = "", + const string &vhost = "", + const string &app = "", + const string &stream = ""); // 从mp4文件生成MediaSource static MediaSource::Ptr createFromMP4(const string &schema, const string &vhost, const string &app, const string &stream, const string &file_path = "", bool check_app = true); diff --git a/src/Shell/ShellCMD.h b/src/Shell/ShellCMD.h index b0697725..0a089f57 100644 --- a/src/Shell/ShellCMD.h +++ b/src/Shell/ShellCMD.h @@ -23,22 +23,6 @@ public: CMD_media(){ _parser.reset(new OptionParser([](const std::shared_ptr &stream,mINI &ini){ MediaSource::for_each_media([&](const MediaSource::Ptr &media){ - if(!ini["schema"].empty() && ini["schema"] != media->getSchema()){ - //筛选协议不匹配 - return; - } - if(!ini["vhost"].empty() && ini["vhost"] != media->getVhost()){ - //筛选虚拟主机不匹配 - return; - } - if(!ini["app"].empty() && ini["app"] != media->getApp()){ - //筛选应用名不匹配 - return; - } - if(!ini["stream"].empty() && ini["stream"] != media->getId()){ - //流id不匹配 - return; - } if(ini.find("list") != ini.end()){ //列出源 (*stream) << "\t" @@ -78,7 +62,7 @@ public: },false); - }); + }, ini["schema"], ini["vhost"], ini["app"], ini["stream"]); })); (*_parser) << Option('k', "kick", Option::ArgNone,nullptr,false, "踢出媒体源", nullptr); (*_parser) << Option('l', "list", Option::ArgNone,nullptr,false, "列出媒体源", nullptr);