From e3cad7f8faf7cba7ab746e3715b7a5abc91d059f Mon Sep 17 00:00:00 2001 From: u7ko4 <1868690+u7ko4@users.noreply.github.com> Date: Thu, 1 Aug 2024 11:03:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=BB=91=E5=AE=9A=E5=88=B0?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E7=BD=91=E5=8D=A1=20(#3760)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/source/mk_rtp_server.cpp | 6 ++++-- conf/config.ini | 2 ++ server/WebApi.cpp | 4 ++-- server/main.cpp | 23 ++++++++++++----------- src/Common/config.cpp | 2 ++ src/Common/config.h | 2 ++ src/Rtp/RtpServer.cpp | 2 +- src/Rtp/RtpServer.h | 6 +++--- 8 files changed, 28 insertions(+), 19 deletions(-) diff --git a/api/source/mk_rtp_server.cpp b/api/source/mk_rtp_server.cpp index 4eca8844..316150e7 100644 --- a/api/source/mk_rtp_server.cpp +++ b/api/source/mk_rtp_server.cpp @@ -18,13 +18,15 @@ using namespace mediakit; API_EXPORT mk_rtp_server API_CALL mk_rtp_server_create(uint16_t port, int tcp_mode, const char *stream_id) { RtpServer::Ptr *server = new RtpServer::Ptr(new RtpServer); - (*server)->start(port, MediaTuple { DEFAULT_VHOST, kRtpAppName, stream_id, "" }, (RtpServer::TcpMode)tcp_mode); + GET_CONFIG(std::string, local_ip, General::kListenIP) + (*server)->start(port, local_ip.c_str(), MediaTuple { DEFAULT_VHOST, kRtpAppName, stream_id, "" }, (RtpServer::TcpMode)tcp_mode); return (mk_rtp_server)server; } API_EXPORT mk_rtp_server API_CALL mk_rtp_server_create2(uint16_t port, int tcp_mode, const char *vhost, const char *app, const char *stream_id) { RtpServer::Ptr *server = new RtpServer::Ptr(new RtpServer); - (*server)->start(port, MediaTuple { vhost, app, stream_id, "" }, (RtpServer::TcpMode)tcp_mode); + GET_CONFIG(std::string, local_ip, General::kListenIP) + (*server)->start(port, local_ip.c_str(), MediaTuple { vhost, app, stream_id, "" }, (RtpServer::TcpMode)tcp_mode); return (mk_rtp_server)server; } diff --git a/conf/config.ini b/conf/config.ini index 3fd805c4..6ffeb2e0 100644 --- a/conf/config.ini +++ b/conf/config.ini @@ -128,6 +128,8 @@ wait_add_track_ms=3000 unready_frame_cache=100 #是否启用观看人数变化事件广播,置1则启用,置0则关闭 broadcast_player_count_changed=0 +#绑定的本地网卡ip +listen_ip=:: [hls] #hls写文件的buf大小,调整参数可以提高文件io性能 diff --git a/server/WebApi.cpp b/server/WebApi.cpp index e086fe6c..3f7e18fd 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -482,7 +482,7 @@ uint16_t openRtpServer(uint16_t local_port, const mediakit::MediaTuple &tuple, i } auto server = s_rtp_server.makeWithAction(key, [&](RtpServer::Ptr server) { - server->start(local_port, tuple, (RtpServer::TcpMode)tcp_mode, local_ip.c_str(), re_use_port, ssrc, only_track, multiplex); + server->start(local_port, local_ip.c_str(), tuple, (RtpServer::TcpMode)tcp_mode, re_use_port, ssrc, only_track, multiplex); }); server->setOnDetach([key](const SockException &ex) { //设置rtp超时移除事件 @@ -1242,7 +1242,7 @@ void installWebApi() { // 兼容老版本请求,新版本去除only_audio参数并新增only_track参数 only_track = 1; } - std::string local_ip = "::"; + GET_CONFIG(std::string, local_ip, General::kListenIP) if (!allArgs["local_ip"].empty()) { local_ip = allArgs["local_ip"]; } diff --git a/server/main.cpp b/server/main.cpp index f418f2f8..92ca10ca 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -281,6 +281,7 @@ int start_main(int argc,char *argv[]) { }); } + std::string listen_ip = mINI::Instance()[General::kListenIP]; uint16_t shellPort = mINI::Instance()[Shell::kPort]; uint16_t rtspPort = mINI::Instance()[Rtsp::kPort]; uint16_t rtspsPort = mINI::Instance()[Rtsp::kSSLPort]; @@ -362,39 +363,39 @@ int start_main(int argc,char *argv[]) { try { //rtsp服务器,端口默认554 - if (rtspPort) { rtspSrv->start(rtspPort); } + if (rtspPort) { rtspSrv->start(rtspPort, listen_ip); } //rtsps服务器,端口默认322 - if (rtspsPort) { rtspSSLSrv->start(rtspsPort); } + if (rtspsPort) { rtspSSLSrv->start(rtspsPort, listen_ip); } //rtmp服务器,端口默认1935 - if (rtmpPort) { rtmpSrv->start(rtmpPort); } + if (rtmpPort) { rtmpSrv->start(rtmpPort, listen_ip); } //rtmps服务器,端口默认19350 - if (rtmpsPort) { rtmpsSrv->start(rtmpsPort); } + if (rtmpsPort) { rtmpsSrv->start(rtmpsPort, listen_ip); } //http服务器,端口默认80 - if (httpPort) { httpSrv->start(httpPort); } + if (httpPort) { httpSrv->start(httpPort, listen_ip); } //https服务器,端口默认443 - if (httpsPort) { httpsSrv->start(httpsPort); } + if (httpsPort) { httpsSrv->start(httpsPort, listen_ip); } //telnet远程调试服务器 - if (shellPort) { shellSrv->start(shellPort); } + if (shellPort) { shellSrv->start(shellPort, listen_ip); } #if defined(ENABLE_RTPPROXY) //创建rtp服务器 - if (rtpPort) { rtpServer->start(rtpPort); } + if (rtpPort) { rtpServer->start(rtpPort, listen_ip.c_str()); } #endif//defined(ENABLE_RTPPROXY) #if defined(ENABLE_WEBRTC) //webrtc udp服务器 - if (rtcPort) { rtcSrv_udp->start(rtcPort);} + if (rtcPort) { rtcSrv_udp->start(rtcPort, listen_ip);} - if (rtcTcpPort) { rtcSrv_tcp->start(rtcTcpPort);} + if (rtcTcpPort) { rtcSrv_tcp->start(rtcTcpPort, listen_ip);} #endif//defined(ENABLE_WEBRTC) #if defined(ENABLE_SRT) // srt udp服务器 - if (srtPort) { srtSrv->start(srtPort); } + if (srtPort) { srtSrv->start(srtPort, listen_ip); } #endif//defined(ENABLE_SRT) } catch (std::exception &ex) { diff --git a/src/Common/config.cpp b/src/Common/config.cpp index bbb97c39..a8df9bfe 100644 --- a/src/Common/config.cpp +++ b/src/Common/config.cpp @@ -84,6 +84,7 @@ const string kWaitTrackReadyMS = GENERAL_FIELD "wait_track_ready_ms"; const string kWaitAddTrackMS = GENERAL_FIELD "wait_add_track_ms"; const string kUnreadyFrameCache = GENERAL_FIELD "unready_frame_cache"; const string kBroadcastPlayerCountChanged = GENERAL_FIELD "broadcast_player_count_changed"; +const string kListenIP = GENERAL_FIELD "listen_ip"; static onceToken token([]() { mINI::Instance()[kFlowThreshold] = 1024; @@ -99,6 +100,7 @@ static onceToken token([]() { mINI::Instance()[kWaitAddTrackMS] = 3000; mINI::Instance()[kUnreadyFrameCache] = 100; mINI::Instance()[kBroadcastPlayerCountChanged] = 0; + mINI::Instance()[kListenIP] = "::"; }); } // namespace General diff --git a/src/Common/config.h b/src/Common/config.h index e456bdab..7846a65c 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -202,6 +202,8 @@ extern const std::string kWaitAddTrackMS; extern const std::string kUnreadyFrameCache; // 是否启用观看人数变化事件广播,置1则启用,置0则关闭 extern const std::string kBroadcastPlayerCountChanged; +// 绑定的本地网卡ip +extern const std::string kListenIP; } // namespace General namespace Protocol { diff --git a/src/Rtp/RtpServer.cpp b/src/Rtp/RtpServer.cpp index 513c3e56..5c657d19 100644 --- a/src/Rtp/RtpServer.cpp +++ b/src/Rtp/RtpServer.cpp @@ -122,7 +122,7 @@ private: std::shared_ptr _rtcp_addr; }; -void RtpServer::start(uint16_t local_port, const MediaTuple &tuple, TcpMode tcp_mode, const char *local_ip, bool re_use_port, uint32_t ssrc, int only_track, bool multiplex) { +void RtpServer::start(uint16_t local_port, const char *local_ip, const MediaTuple &tuple, TcpMode tcp_mode, bool re_use_port, uint32_t ssrc, int only_track, bool multiplex) { //创建udp服务器 auto poller = EventPollerPool::Instance().getPoller(); Socket::Ptr rtp_socket = Socket::createSocket(poller, true); diff --git a/src/Rtp/RtpServer.h b/src/Rtp/RtpServer.h index 70af06de..f98db95b 100644 --- a/src/Rtp/RtpServer.h +++ b/src/Rtp/RtpServer.h @@ -36,15 +36,15 @@ public: /** * 开启服务器,可能抛异常 * @param local_port 本地端口,0时为随机端口 + * @param local_ip 绑定的本地网卡ip * @param stream_id 流id,置空则使用ssrc * @param tcp_mode tcp服务模式 - * @param local_ip 绑定的本地网卡ip * @param re_use_port 是否设置socket为re_use属性 * @param ssrc 指定的ssrc * @param multiplex 多路复用 */ - void start(uint16_t local_port, const MediaTuple &tuple = MediaTuple{DEFAULT_VHOST, kRtpAppName, "", ""}, TcpMode tcp_mode = PASSIVE, - const char *local_ip = "::", bool re_use_port = true, uint32_t ssrc = 0, int only_track = 0, bool multiplex = false); + void start(uint16_t local_port, const char *local_ip = "::", const MediaTuple &tuple = MediaTuple{DEFAULT_VHOST, kRtpAppName, "", ""}, TcpMode tcp_mode = PASSIVE, + bool re_use_port = true, uint32_t ssrc = 0, int only_track = 0, bool multiplex = false); /** * 连接到tcp服务(tcp主动模式)