From d1fd674c36e57dc087438cbe48b91ced7fbfe248 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=A5=9A?= <771730766@qq.com> Date: Wed, 13 Nov 2019 10:43:46 +0800 Subject: [PATCH 01/19] Update README.md --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index becaaf31..d02ad8e3 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,12 @@ - Apple OSX(Darwin), both 32 and 64bits. - All hardware with x86/x86_64/arm/mips cpu. - Windows. + +## How to build + +It is recommended to compile on Ubuntu or MacOS,compiling on windows is cumbersome, and some features are not compiled by default. + +### Before build - **You must use git to clone the complete code. Do not download the source code by downloading zip package. Otherwise, the sub-module code will not be downloaded by default.You can do it like this:** ``` git clone https://github.com/zlmediakit/ZLMediaKit.git @@ -124,12 +130,6 @@ cd ZLMediaKit git submodule update --init ``` - - -## How to build - -It is recommended to compile on Ubuntu or MacOS,compiling on windows is cumbersome, and some features are not compiled by default. - ### Build on linux - My environment From 94eebdadf1709cd9df52f85defb396e6b12473c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=A5=9A?= <771730766@qq.com> Date: Wed, 13 Nov 2019 10:45:52 +0800 Subject: [PATCH 02/19] Update README_CN.md --- README_CN.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README_CN.md b/README_CN.md index 54199461..a2eba340 100644 --- a/README_CN.md +++ b/README_CN.md @@ -128,6 +128,9 @@ ## 编译要求 - 编译器支持C++11,GCC4.8/Clang3.3/VC2015或以上 - cmake3.2或以上 + +## 编译前必看!!! + - **必须使用git下载完整的代码,不要使用下载zip包的方式下载源码,否则子模块代码默认不下载!你可以像以下这样操作:** ``` git clone https://github.com/zlmediakit/ZLMediaKit.git From 88a5de00b2d1498b16237088d0ca9ee6c4510f82 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Wed, 13 Nov 2019 14:05:15 +0800 Subject: [PATCH 03/19] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=8B=BC=E5=86=99?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 453db5aa..a33d9bb0 100644 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -554,7 +554,7 @@ void installWebApi() { }); #if !defined(_WIN32) - static auto addFFmepgSource = [](const string &src_url, + static auto addFFmpegSource = [](const string &src_url, const string &dst_url, int timeout_ms, const function &cb){ @@ -591,7 +591,7 @@ void installWebApi() { auto dst_url = allArgs["dst_url"]; int timeout_ms = allArgs["timeout_ms"]; - addFFmepgSource(src_url,dst_url,timeout_ms,[invoker,val,headerOut](const SockException &ex,const string &key){ + addFFmpegSource(src_url,dst_url,timeout_ms,[invoker,val,headerOut](const SockException &ex,const string &key){ if(ex){ const_cast(val)["code"] = API::OtherFailed; const_cast(val)["msg"] = ex.what(); @@ -604,7 +604,7 @@ void installWebApi() { //关闭拉流代理 //测试url http://127.0.0.1/index/api/delFFmepgSource?key=key - API_REGIST(api,delFFmepgSource,{ + API_REGIST(api,delFFmpegSource,{ CHECK_SECRET(); CHECK_ARGS("key"); lock_guard lck(s_ffmpegMapMtx); @@ -677,7 +677,7 @@ void installWebApi() { << allArgs["stream"] << "?vhost=" << allArgs["vhost"]; - addFFmepgSource("http://live.hkstv.hk.lxdns.com/live/hks2/playlist.m3u8",/** ffmpeg拉流支持任意编码格式任意协议 **/ + addFFmpegSource("http://live.hkstv.hk.lxdns.com/live/hks2/playlist.m3u8",/** ffmpeg拉流支持任意编码格式任意协议 **/ dst_url, (1000 * timeout_sec) - 500, [invoker,val,headerOut](const SockException &ex,const string &key){ From 9665d557ae89a902cafad62d5a2417da858b0705 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Wed, 13 Nov 2019 14:11:49 +0800 Subject: [PATCH 04/19] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=8B=BC=E5=86=99?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index a33d9bb0..587697a8 100644 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -72,6 +72,8 @@ typedef map ApiArgsType; invoker("200 OK", headerOut, val.toStyledString()); \ }); +#define API_ARGS_VALUE sender,headerIn,headerOut,allArgs,val,invoker + #define API_REGIST_INVOKER(field, name, ...) \ s_map_api.emplace("/index/"#field"/"#name,[](API_ARGS,const HttpSession::HttpResponseInvoker &invoker) __VA_ARGS__); @@ -602,13 +604,23 @@ void installWebApi() { }); }); - //关闭拉流代理 - //测试url http://127.0.0.1/index/api/delFFmepgSource?key=key - API_REGIST(api,delFFmpegSource,{ + + static auto api_delFFmpegSource = [](API_ARGS,const HttpSession::HttpResponseInvoker &invoker){ CHECK_SECRET(); CHECK_ARGS("key"); lock_guard lck(s_ffmpegMapMtx); val["data"]["flag"] = s_ffmpegMap.erase(allArgs["key"]) == 1; + }; + + //关闭拉流代理 + //测试url http://127.0.0.1/index/api/delFFmepgSource?key=key + API_REGIST(api,delFFmpegSource,{ + api_delFFmpegSource(API_ARGS_VALUE); + }); + + //此处为了兼容之前的拼写错误 + API_REGIST(api,delFFmepgSource,{ + api_delFFmpegSource(API_ARGS_VALUE); }); #endif From aef8fb8849d1416395761ebf2bec28254f1c32c7 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Wed, 13 Nov 2019 14:26:12 +0800 Subject: [PATCH 05/19] =?UTF-8?q?=E6=B7=BB=E5=8A=A0isMediaOnline=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=88=A4=E6=96=AD=E6=98=AF=E5=90=A6=E5=AA=92=E4=BD=93?= =?UTF-8?q?=E5=9C=A8=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 587697a8..0f53cd7c 100644 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -418,6 +418,13 @@ void installWebApi() { }); }); + //测试url http://127.0.0.1/index/api/isMediaOnline?schema=rtsp&vhost=__defaultVhost__&app=live&stream=obs + API_REGIST(api,isMediaOnline,{ + CHECK_SECRET(); + CHECK_ARGS("schema","vhost","app","stream"); + val["online"] = (bool) (MediaSource::find(allArgs["schema"],allArgs["vhost"],allArgs["app"],allArgs["stream"],false)); + }); + //主动关断流,包括关断拉流、推流 //测试url http://127.0.0.1/index/api/close_stream?schema=rtsp&vhost=__defaultVhost__&app=live&stream=obs&force=1 API_REGIST(api,close_stream,{ From d440c424c4273050ffa7cdf1475ab4a4729615ff Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Wed, 13 Nov 2019 14:33:19 +0800 Subject: [PATCH 06/19] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E8=B4=9F=E8=BD=BD=E6=8E=A5=E5=8F=A3=E8=BF=94=E5=9B=9Ecode?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=20=E7=AE=80=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 0f53cd7c..1c7eab29 100644 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -296,6 +296,7 @@ void installWebApi() { obj["delay"] = vecDelay[i++]; val["data"].append(obj); } + val["code"] = API::Success; invoker("200 OK", headerOut, val.toStyledString()); }); }); @@ -313,6 +314,7 @@ void installWebApi() { obj["delay"] = vecDelay[i++]; val["data"].append(obj); } + val["code"] = API::Success; invoker("200 OK", headerOut, val.toStyledString()); }); }); @@ -393,8 +395,6 @@ void installWebApi() { API_REGIST(api,getMediaList,{ CHECK_SECRET(); //获取所有MediaSource列表 - val["code"] = API::Success; - val["msg"] = "success"; MediaSource::for_each_media([&](const string &schema, const string &vhost, const string &app, @@ -479,13 +479,9 @@ void installWebApi() { //踢掉tcp会话 auto session = SessionMap::Instance().get(allArgs["id"]); if(!session){ - val["code"] = API::OtherFailed; - val["msg"] = "can not find the target"; - return; + throw ApiRetException("can not find the target",API::OtherFailed); } session->safeShutdown(); - val["code"] = API::Success; - val["msg"] = "success"; }); static auto addStreamProxy = [](const string &vhost, From c1f3b8ffbc9b393687c5da00176f891b76f96eef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=A5=9A?= <771730766@qq.com> Date: Thu, 14 Nov 2019 14:45:12 +0800 Subject: [PATCH 07/19] Create ccpp.yml --- .github/workflows/ccpp.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 .github/workflows/ccpp.yml diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml new file mode 100644 index 00000000..89cea2fd --- /dev/null +++ b/.github/workflows/ccpp.yml @@ -0,0 +1,16 @@ +name: C/C++ CI + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v1 + - name: build + run: ./build_for_linux.sh + - name: run + run: cd ZLMediaKit/release/linux/Debug && ./MediaServer -d + From c8ba6fd73fedf15cd218185a2dd317d969a7a721 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Thu, 14 Nov 2019 14:48:27 +0800 Subject: [PATCH 08/19] =?UTF-8?q?=E6=9B=B4=E6=96=B0ZLToolKit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3rdpart/ZLToolKit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3rdpart/ZLToolKit b/3rdpart/ZLToolKit index 73eb9077..628d3b25 160000 --- a/3rdpart/ZLToolKit +++ b/3rdpart/ZLToolKit @@ -1 +1 @@ -Subproject commit 73eb9077e19e473c643708cf586517bacaa16302 +Subproject commit 628d3b2527f63b54a5eb38b9e9973254d4a2192b From a3e67a13064c88452aee107575284cbecddb8c22 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Thu, 14 Nov 2019 15:14:25 +0800 Subject: [PATCH 09/19] =?UTF-8?q?=E5=AE=8C=E6=88=90workflows=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ccpp.yml | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml index 89cea2fd..17674cc2 100644 --- a/.github/workflows/ccpp.yml +++ b/.github/workflows/ccpp.yml @@ -9,8 +9,16 @@ jobs: steps: - uses: actions/checkout@v1 - - name: build - run: ./build_for_linux.sh - - name: run - run: cd ZLMediaKit/release/linux/Debug && ./MediaServer -d + + - name: 下载submodule源码 + run: git submodule update --init + + - name: apt-get安装依赖库(非必选) + run: sudo apt-get install -y cmake libmysqlclient-dev libssl-dev libx264-dev libfaac-dev libmp4v2-dev libsdl-dev libavcodec-dev libavutil-dev + + - name: 编译 + run: mkdir -p linux_build && cd linux_build && cmake .. && make -j4 + + - name: 运行MediaServer + run: pwd && cd release/linux/Debug && ./MediaServer -h From 2543ee01dc25217f0539934ae5c2df6a0ee92f55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=A5=9A?= <771730766@qq.com> Date: Thu, 14 Nov 2019 15:22:15 +0800 Subject: [PATCH 10/19] Update ccpp.yml --- .github/workflows/ccpp.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml index 17674cc2..fc130fbf 100644 --- a/.github/workflows/ccpp.yml +++ b/.github/workflows/ccpp.yml @@ -20,5 +20,8 @@ jobs: run: mkdir -p linux_build && cd linux_build && cmake .. && make -j4 - name: 运行MediaServer - run: pwd && cd release/linux/Debug && ./MediaServer -h + run: pwd && cd release/linux/Debug && sudo ./MediaServer -d & + + - name: 关闭MediaServer + run: sleep 3 && killall -2 MediaServer From 08ad39d0056aad18893db33091e63df9d8f58fc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=A5=9A?= <771730766@qq.com> Date: Thu, 14 Nov 2019 15:26:35 +0800 Subject: [PATCH 11/19] Update ccpp.yml --- .github/workflows/ccpp.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml index fc130fbf..dc2660e2 100644 --- a/.github/workflows/ccpp.yml +++ b/.github/workflows/ccpp.yml @@ -22,6 +22,4 @@ jobs: - name: 运行MediaServer run: pwd && cd release/linux/Debug && sudo ./MediaServer -d & - - name: 关闭MediaServer - run: sleep 3 && killall -2 MediaServer From a993132a595485031aa1cfccdca4f301403e6b68 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Fri, 15 Nov 2019 13:50:42 +0800 Subject: [PATCH 12/19] =?UTF-8?q?=E4=BF=AE=E5=A4=8DFFmpeg=E6=8B=89?= =?UTF-8?q?=E6=B5=81=E4=BB=A3=E7=90=86=E5=8D=A1=E9=A1=BF=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/config.ini | 2 +- server/FFmpegSource.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/conf/config.ini b/conf/config.ini index 830acbb3..1ef7c145 100644 --- a/conf/config.ini +++ b/conf/config.ini @@ -9,7 +9,7 @@ secret=035c73f7-bb6b-4889-a715-d9eb2d1925cc #FFmpeg可执行程序绝对路径 bin=/usr/local/bin/ffmpeg #FFmpeg拉流再推流的命令模板,通过该模板可以设置再编码的一些参数 -cmd=%s -i %s -c:a aac -strict -2 -ar 44100 -ab 48k -c:v libx264 -f flv %s +cmd=%s -re -i %s -c:a aac -strict -2 -ar 44100 -ab 48k -c:v libx264 -f flv %s #FFmpeg日志的路径,如果置空则不生成FFmpeg日志 #可以为相对(相对于本可执行程序目录)或绝对路径 log=./ffmpeg/ffmpeg.log diff --git a/server/FFmpegSource.cpp b/server/FFmpegSource.cpp index e55e6dbb..359ed2be 100644 --- a/server/FFmpegSource.cpp +++ b/server/FFmpegSource.cpp @@ -38,7 +38,7 @@ const char kLog[] = FFmpeg_FIELD"log"; onceToken token([]() { mINI::Instance()[kBin] = trim(System::execute("which ffmpeg")); - mINI::Instance()[kCmd] = "%s -i %s -c:a aac -strict -2 -ar 44100 -ab 48k -c:v libx264 -f flv %s"; + mINI::Instance()[kCmd] = "%s -re -i %s -c:a aac -strict -2 -ar 44100 -ab 48k -c:v libx264 -f flv %s"; mINI::Instance()[kLog] = "./ffmpeg/ffmpeg.log"; }); } From ddf351d35008899896075b72e6542c9d1d3a8c43 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Mon, 18 Nov 2019 10:46:59 +0800 Subject: [PATCH 13/19] =?UTF-8?q?=E6=B7=BB=E5=8A=A0close=5Fstreams?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 1c7eab29..f4a998ca 100644 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -445,6 +445,39 @@ void installWebApi() { } }); + //批量主动关断流,包括关断拉流、推流 + //测试url http://127.0.0.1/index/api/close_streams?schema=rtsp&vhost=__defaultVhost__&app=live&stream=obs&force=1 + API_REGIST(api,close_streams,{ + CHECK_SECRET(); + //筛选命中个数 + int count_hit = 0; + int count_closed = 0; + MediaSource::for_each_media([&](const string &schema, + const string &vhost, + const string &app, + const string &stream, + const MediaSource::Ptr &media){ + if(!allArgs["schema"].empty() && allArgs["schema"] != schema){ + return; + } + if(!allArgs["vhost"].empty() && allArgs["vhost"] != vhost){ + return; + } + if(!allArgs["app"].empty() && allArgs["app"] != app){ + return; + } + if(!allArgs["stream"].empty() && allArgs["stream"] != stream){ + return; + } + ++count_hit; + if(media->close(allArgs["force"].as())){ + ++count_closed; + } + }); + val["count_hit"] = count_hit; + val["count_closed"] = count_closed; + }); + //获取所有TcpSession列表信息 //可以根据本地端口和远端ip来筛选 //测试url(筛选某端口下的tcp会话) http://127.0.0.1/index/api/getAllSession?local_port=1935 From 0d42aab9e7370f14b798548eb10355303d067445 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Mon, 18 Nov 2019 12:07:11 +0800 Subject: [PATCH 14/19] =?UTF-8?q?=E4=BF=AE=E5=A4=8DFFmpeg=E6=8B=89?= =?UTF-8?q?=E6=B5=81=E4=BB=A3=E7=90=86=E6=97=A0=E6=B3=95=E9=80=9A=E8=BF=87?= =?UTF-8?q?close=5Fstream=E6=8E=A5=E5=8F=A3=E5=85=B3=E9=97=AD=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/FFmpegSource.cpp | 55 ++++++++++++++++++++++------------------ server/FFmpegSource.h | 7 ++++- src/Common/MediaSource.h | 5 ++++ 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/server/FFmpegSource.cpp b/server/FFmpegSource.cpp index 359ed2be..81d98bd5 100644 --- a/server/FFmpegSource.cpp +++ b/server/FFmpegSource.cpp @@ -48,7 +48,6 @@ FFmpegSource::FFmpegSource() { } FFmpegSource::~FFmpegSource() { - NoticeCenter::Instance().delListener(this, Broadcast::kBroadcastStreamNoneReader); DebugL; } @@ -83,6 +82,7 @@ void FFmpegSource::play(const string &src_url,const string &dst_url,int timeout_ if(src){ //推流给自己成功 cb(SockException()); + strongSelf->onGetMediaSource(src); strongSelf->startTimer(timeout_ms); return; } @@ -192,8 +192,7 @@ void FFmpegSource::startTimer(int timeout_ms) { //同步查找流 if (!src) { //流不在线,重新拉流 - strongSelf->play(strongSelf->_src_url, strongSelf->_dst_url, timeout_ms, - [](const SockException &) {}); + strongSelf->play(strongSelf->_src_url, strongSelf->_dst_url, timeout_ms, [](const SockException &) {}); } }); } else { @@ -205,29 +204,35 @@ void FFmpegSource::startTimer(int timeout_ms) { } return true; }, _poller); - - NoticeCenter::Instance().delListener(this, Broadcast::kBroadcastStreamNoneReader); - NoticeCenter::Instance().addListener(this, Broadcast::kBroadcastStreamNoneReader,[weakSelf](BroadcastStreamNoneReaderArgs) { - auto strongSelf = weakSelf.lock(); - if (!strongSelf) { - //自身已经销毁 - return; - } - - if(sender.getVhost() != strongSelf->_media_info._vhost || - sender.getApp() != strongSelf->_media_info._app || - sender.getId() != strongSelf->_media_info._streamid){ - //不是自己感兴趣的事件,忽略之 - return; - } - - //该流无人观看,我们停止吧 - if(strongSelf->_onClose){ - strongSelf->_onClose(); - } - }); } void FFmpegSource::setOnClose(const function &cb){ _onClose = cb; -} \ No newline at end of file +} + +bool FFmpegSource::close(MediaSource &sender, bool force) { + auto listener = _listener.lock(); + if(listener && !listener->close(sender,force)){ + //关闭失败 + return false; + } + //该流无人观看,我们停止吧 + if(_onClose){ + _onClose(); + } + return true; +} + +void FFmpegSource::onNoneReader(MediaSource &sender) { + auto listener = _listener.lock(); + if(listener){ + listener->onNoneReader(sender); + }else{ + MediaSourceEvent::onNoneReader(sender); + } +} + +void FFmpegSource::onGetMediaSource(const MediaSource::Ptr &src) { + _listener = src->getListener(); + src->setListener(shared_from_this()); +} diff --git a/server/FFmpegSource.h b/server/FFmpegSource.h index 6be92fc3..5cf2c4f7 100644 --- a/server/FFmpegSource.h +++ b/server/FFmpegSource.h @@ -39,7 +39,7 @@ using namespace std; using namespace toolkit; using namespace mediakit; -class FFmpegSource : public std::enable_shared_from_this{ +class FFmpegSource : public std::enable_shared_from_this , public MediaSourceEvent{ public: typedef shared_ptr Ptr; typedef function onPlay; @@ -55,6 +55,10 @@ public: private: void findAsync(int maxWaitMS ,const function &cb); void startTimer(int timeout_ms); + void onGetMediaSource(const MediaSource::Ptr &src); + + bool close(MediaSource &sender,bool force) override; + void onNoneReader(MediaSource &sender) override ; private: Process _process; Timer::Ptr _timer; @@ -63,6 +67,7 @@ private: string _src_url; string _dst_url; function _onClose; + std::weak_ptr _listener; }; diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index a54bc8d8..5cfe597d 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -167,10 +167,15 @@ public: } listener->onNoneReader(*this); } + virtual void setListener(const std::weak_ptr &listener){ _listener = listener; } + std::weak_ptr getListener(){ + return _listener; + } + template static void for_each_media(FUN && fun){ lock_guard lock(g_mtxMediaSrc); From f628985037fbab822b986e9456fd666bd36f5a62 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Mon, 18 Nov 2019 16:16:56 +0800 Subject: [PATCH 15/19] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index f4a998ca..417a4d0d 100644 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -488,7 +488,7 @@ void installWebApi() { string &peer_ip = allArgs["peer_ip"]; SessionMap::Instance().for_each_session([&](const string &id,const TcpSession::Ptr &session){ - if(local_port != API::Success && local_port != session->get_local_port()){ + if(local_port != 0 && local_port != session->get_local_port()){ return; } if(!peer_ip.empty() && peer_ip != session->get_peer_ip()){ From bcc5c839a583627231639c11002af64ca84d18e0 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Mon, 18 Nov 2019 16:28:56 +0800 Subject: [PATCH 16/19] =?UTF-8?q?close=5Fstream=E6=8E=A5=E5=8F=A3=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E8=BF=94=E5=9B=9E=E5=AD=97=E6=AE=B5=E6=94=B9=E6=88=90?= =?UTF-8?q?result?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 417a4d0d..e1278c9c 100644 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -437,10 +437,10 @@ void installWebApi() { allArgs["stream"]); if(src){ bool flag = src->close(allArgs["force"].as()); - val["code"] = flag ? 0 : -1; + val["result"] = flag ? 0 : -1; val["msg"] = flag ? "success" : "close failed"; }else{ - val["code"] = -2; + val["result"] = -2; val["msg"] = "can not find the stream"; } }); From 517f3c229c151ef3a4cfed98c7c17db19afaa34b Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Mon, 18 Nov 2019 16:34:39 +0800 Subject: [PATCH 17/19] =?UTF-8?q?=E6=B7=BB=E5=8A=A0kick=5Fsessions?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index e1278c9c..f1263e70 100644 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -517,6 +517,28 @@ void installWebApi() { session->safeShutdown(); }); + + //批量断开tcp连接,比如说可以断开rtsp、rtmp播放器等 + //测试url http://127.0.0.1/index/api/kick_sessions?local_port=1935 + API_REGIST(api,kick_sessions,{ + CHECK_SECRET(); + uint16_t local_port = allArgs["local_port"].as(); + string &peer_ip = allArgs["peer_ip"]; + uint64_t count_hit = 0; + + SessionMap::Instance().for_each_session([&](const string &id,const TcpSession::Ptr &session){ + if(local_port != 0 && local_port != session->get_local_port()){ + return; + } + if(!peer_ip.empty() && peer_ip != session->get_peer_ip()){ + return; + } + session->safeShutdown(); + ++count_hit; + }); + val["count_hit"] = (Json::UInt64)count_hit; + }); + static auto addStreamProxy = [](const string &vhost, const string &app, const string &stream, From 2eb64d2c39cde19ebca8d45426e46c7d8c6124dd Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Tue, 19 Nov 2019 10:20:57 +0800 Subject: [PATCH 18/19] =?UTF-8?q?=E7=A1=AE=E4=BF=9Drtp=E7=AB=AF=E5=8F=A3?= =?UTF-8?q?=E4=B8=BA=E5=81=B6=E6=95=B0=EF=BC=8Crtcp=E7=AB=AF=E5=8F=A3?= =?UTF-8?q?=E4=B8=BA=E5=A5=87=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Rtsp/RtspPlayer.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Rtsp/RtspPlayer.cpp b/src/Rtsp/RtspPlayer.cpp index be1424d6..aa7dfce3 100644 --- a/src/Rtsp/RtspPlayer.cpp +++ b/src/Rtsp/RtspPlayer.cpp @@ -277,6 +277,13 @@ void RtspPlayer::createUdpSockIfNecessary(int track_idx){ throw std::runtime_error("open rtcp sock failed"); } } + + if(rtpSockRef->get_local_port() % 2 != 0){ + //如果rtp端口不是偶数,那么与rtcp端口互换,目的是兼容一些要求严格的服务器 + Socket::Ptr tmp = rtpSockRef; + rtpSockRef = rtcpSockRef; + rtcpSockRef = tmp; + } } From 85cd4a7d0234869faa4fc66c2129b5d54f71f413 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Tue, 19 Nov 2019 10:55:44 +0800 Subject: [PATCH 19/19] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=81=8D=E5=8E=86?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=8F=AF=E8=83=BD=E5=B4=A9=E6=BA=83=E7=9A=84?= =?UTF-8?q?bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index f1263e70..9a23da09 100644 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -452,6 +452,7 @@ void installWebApi() { //筛选命中个数 int count_hit = 0; int count_closed = 0; + list media_list; MediaSource::for_each_media([&](const string &schema, const string &vhost, const string &app, @@ -470,10 +471,15 @@ void installWebApi() { return; } ++count_hit; - if(media->close(allArgs["force"].as())){ + media_list.emplace_back(media); + }); + + bool force = allArgs["force"].as(); + for(auto &media : media_list){ + if(media->close(force)){ ++count_closed; } - }); + } val["count_hit"] = count_hit; val["count_closed"] = count_closed; }); @@ -526,6 +532,7 @@ void installWebApi() { string &peer_ip = allArgs["peer_ip"]; uint64_t count_hit = 0; + list session_list; SessionMap::Instance().for_each_session([&](const string &id,const TcpSession::Ptr &session){ if(local_port != 0 && local_port != session->get_local_port()){ return; @@ -533,9 +540,13 @@ void installWebApi() { if(!peer_ip.empty() && peer_ip != session->get_peer_ip()){ return; } - session->safeShutdown(); + session_list.emplace_back(session); ++count_hit; }); + + for(auto &session : session_list){ + session->safeShutdown(); + } val["count_hit"] = (Json::UInt64)count_hit; });