From e8b0993493983e830136e2e1311f0e45b08190e3 Mon Sep 17 00:00:00 2001 From: wangcker Date: Fri, 14 Jan 2022 21:53:01 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=99openrtpserver=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0enable=5Freuse=E5=8F=82=E6=95=B0=20(#1372)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修改rtpopen逻辑 * 给openrtpserver接口添加enable_reuse端口 --- server/WebApi.cpp | 11 +++++++++-- src/Rtp/RtpServer.cpp | 6 +++--- src/Rtp/RtpServer.h | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index e9fce6f9..c1dfec29 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -1025,7 +1025,10 @@ void installWebApi() { api_regist("/index/api/openRtpServer",[](API_ARGS_MAP){ CHECK_SECRET(); CHECK_ARGS("port", "enable_tcp", "stream_id"); - + bool enable_reuse=true; + if(!allArgs["enable_reuse"].empty()){ + enable_reuse = allArgs["enable_reuse"].as(); + } auto stream_id = allArgs["stream_id"]; lock_guard lck(s_rtpServerMapMtx); @@ -1034,8 +1037,12 @@ void installWebApi() { throw InvalidArgsException("该stream_id已存在"); } + + + + RtpServer::Ptr server = std::make_shared(); - server->start(allArgs["port"], stream_id, allArgs["enable_tcp"].as()); + server->start(allArgs["port"], stream_id, allArgs["enable_tcp"].as(),"0.0.0.0",enable_reuse); server->setOnDetach([stream_id]() { //设置rtp超时移除事件 lock_guard lck(s_rtpServerMapMtx); diff --git a/src/Rtp/RtpServer.cpp b/src/Rtp/RtpServer.cpp index 53dd5b30..29e2341d 100644 --- a/src/Rtp/RtpServer.cpp +++ b/src/Rtp/RtpServer.cpp @@ -84,7 +84,7 @@ private: std::shared_ptr _rtcp_addr; }; -void RtpServer::start(uint16_t local_port, const string &stream_id, bool enable_tcp, const char *local_ip) { +void RtpServer::start(uint16_t local_port, const string &stream_id, bool enable_tcp, const char *local_ip,bool enable_reuse) { //创建udp服务器 Socket::Ptr rtp_socket = Socket::createSocket(nullptr, true); Socket::Ptr rtcp_socket = Socket::createSocket(nullptr, true); @@ -95,10 +95,10 @@ void RtpServer::start(uint16_t local_port, const string &stream_id, bool enable //取偶数端口 rtp_socket = pair.first; rtcp_socket = pair.second; - } else if (!rtp_socket->bindUdpSock(local_port, local_ip)) { + } else if (!rtp_socket->bindUdpSock(local_port, local_ip,enable_reuse)) { //用户指定端口 throw std::runtime_error(StrPrinter << "创建rtp端口 " << local_ip << ":" << local_port << " 失败:" << get_uv_errmsg(true)); - } else if(!rtcp_socket->bindUdpSock(rtp_socket->get_local_port() + 1, local_ip)) { + } else if(!rtcp_socket->bindUdpSock(rtp_socket->get_local_port() + 1, local_ip,enable_reuse)) { // rtcp端口 throw std::runtime_error(StrPrinter << "创建rtcp端口 " << local_ip << ":" << local_port << " 失败:" << get_uv_errmsg(true)); } diff --git a/src/Rtp/RtpServer.h b/src/Rtp/RtpServer.h index 14b04b7d..61dfb825 100644 --- a/src/Rtp/RtpServer.h +++ b/src/Rtp/RtpServer.h @@ -41,7 +41,7 @@ public: * @param enable_tcp 是否启用tcp服务器 * @param local_ip 绑定的本地网卡ip */ - void start(uint16_t local_port, const string &stream_id = "", bool enable_tcp = true, const char *local_ip = "0.0.0.0"); + void start(uint16_t local_port, const string &stream_id = "", bool enable_tcp = true, const char *local_ip = "0.0.0.0",bool enable_reuse = true); /** * 获取绑定的本地端口