From ffdc13bfb9f25dff1277bdd70a20f0b4aee3b309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E4=BC=A0=E5=B3=B0?= <59160162+zhang-chuanfeng@users.noreply.github.com> Date: Tue, 5 Mar 2024 10:42:22 +0800 Subject: [PATCH] =?UTF-8?q?RTP=20proxy=E9=80=9A=E8=BF=87UDP=E6=94=B6?= =?UTF-8?q?=E6=B5=81=EF=BC=8C=E8=B0=83=E6=95=B4udp=20recv=20socket=20buffe?= =?UTF-8?q?r=20size=E6=88=90=E9=85=8D=E7=BD=AE=20(#3336)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 国标推流有些情况需要UDP方式接收流,端口复用同一个UDP端口可能需要根据服务器性能 --- conf/config.ini | 3 +++ src/Common/config.cpp | 2 ++ src/Common/config.h | 2 ++ src/Rtp/RtpServer.cpp | 4 +++- src/Rtp/RtpSession.cpp | 11 +++++++---- src/Rtp/RtpSession.h | 1 + 6 files changed, 18 insertions(+), 5 deletions(-) diff --git a/conf/config.ini b/conf/config.ini index a66de19f..21e268fd 100644 --- a/conf/config.ini +++ b/conf/config.ini @@ -332,6 +332,9 @@ gop_cache=1 #国标发送g711 rtp 打包时,每个包的语音时长是多少,默认是100 ms,范围为20~180ms (gb28181-2016,c.2.4规定), #最好为20 的倍数,程序自动向20的倍数取整 rtp_g711_dur_ms = 100 +#udp接收数据socket buffer大小配置 +#4*1024*1024=4196304 +udp_recv_socket_buffer=4194304 [rtc] #rtc播放推流、播放超时时间 diff --git a/src/Common/config.cpp b/src/Common/config.cpp index f31546fe..a72ea8da 100644 --- a/src/Common/config.cpp +++ b/src/Common/config.cpp @@ -345,6 +345,7 @@ const string kPSPT = RTP_PROXY_FIELD "ps_pt"; const string kOpusPT = RTP_PROXY_FIELD "opus_pt"; const string kGopCache = RTP_PROXY_FIELD "gop_cache"; const string kRtpG711DurMs = RTP_PROXY_FIELD "rtp_g711_dur_ms"; +const string kUdpRecvSocketBuffer = RTP_PROXY_FIELD "udp_recv_socket_buffer"; static onceToken token([]() { mINI::Instance()[kDumpDir] = ""; @@ -356,6 +357,7 @@ static onceToken token([]() { mINI::Instance()[kOpusPT] = 100; mINI::Instance()[kGopCache] = 1; mINI::Instance()[kRtpG711DurMs] = 100; + mINI::Instance()[kUdpRecvSocketBuffer] = 4 * 1024 * 1024; }); } // namespace RtpProxy diff --git a/src/Common/config.h b/src/Common/config.h index 3196654b..16ef6303 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -400,6 +400,8 @@ extern const std::string kGopCache; //国标发送g711 rtp 打包时,每个包的语音时长是多少,默认是100 ms,范围为20~180ms (gb28181-2016,c.2.4规定), //最好为20 的倍数,程序自动向20的倍数取整 extern const std::string kRtpG711DurMs; +// udp recv socket buffer size +extern const std::string kUdpRecvSocketBuffer; } // namespace RtpProxy /** diff --git a/src/Rtp/RtpServer.cpp b/src/Rtp/RtpServer.cpp index 51bb5555..560792b3 100644 --- a/src/Rtp/RtpServer.cpp +++ b/src/Rtp/RtpServer.cpp @@ -174,7 +174,8 @@ void RtpServer::start(uint16_t local_port, const string &stream_id, TcpMode tcp_ } //设置udp socket读缓存 - SockUtil::setRecvBuf(rtp_socket->rawFD(), 4 * 1024 * 1024); + GET_CONFIG(int, udpRecvSocketBuffer, RtpProxy::kUdpRecvSocketBuffer); + SockUtil::setRecvBuf(rtp_socket->rawFD(), udpRecvSocketBuffer); TcpServer::Ptr tcp_server; _tcp_mode = tcp_mode; @@ -223,6 +224,7 @@ void RtpServer::start(uint16_t local_port, const string &stream_id, TcpMode tcp_ //单端口多线程接收多个流,根据ssrc区分流 udp_server = std::make_shared(rtp_socket->getPoller()); (*udp_server)[RtpSession::kOnlyAudio] = only_audio; + (*udp_server)[RtpSession::kUdpRecvBuffer] = udpRecvSocketBuffer; udp_server->start(local_port, local_ip); rtp_socket = nullptr; } diff --git a/src/Rtp/RtpSession.cpp b/src/Rtp/RtpSession.cpp index 006b1c82..41c76287 100644 --- a/src/Rtp/RtpSession.cpp +++ b/src/Rtp/RtpSession.cpp @@ -24,6 +24,7 @@ namespace mediakit{ const string RtpSession::kStreamID = "stream_id"; const string RtpSession::kSSRC = "ssrc"; const string RtpSession::kOnlyAudio = "only_audio"; +const string RtpSession::kUdpRecvBuffer = "udp_recv_socket_buffer"; void RtpSession::attachServer(const Server &server) { setParams(const_cast(server)); @@ -33,6 +34,12 @@ void RtpSession::setParams(mINI &ini) { _stream_id = ini[kStreamID]; _ssrc = ini[kSSRC]; _only_audio = ini[kOnlyAudio]; + int udp_socket_buffer = ini[kUdpRecvBuffer]; + if (_is_udp) { + // 设置udp socket读缓存 + SockUtil::setRecvBuf(getSock()->rawFD(), + (udp_socket_buffer > 0) ? udp_socket_buffer : (4 * 1024 * 1024)); + } } RtpSession::RtpSession(const Socket::Ptr &sock) @@ -40,10 +47,6 @@ RtpSession::RtpSession(const Socket::Ptr &sock) socklen_t addr_len = sizeof(_addr); getpeername(sock->rawFD(), (struct sockaddr *)&_addr, &addr_len); _is_udp = sock->sockType() == SockNum::Sock_UDP; - if (_is_udp) { - // 设置udp socket读缓存 - SockUtil::setRecvBuf(getSock()->rawFD(), 4 * 1024 * 1024); - } } RtpSession::~RtpSession() = default; diff --git a/src/Rtp/RtpSession.h b/src/Rtp/RtpSession.h index 5b8d7e1f..de2fa75f 100644 --- a/src/Rtp/RtpSession.h +++ b/src/Rtp/RtpSession.h @@ -25,6 +25,7 @@ public: static const std::string kStreamID; static const std::string kSSRC; static const std::string kOnlyAudio; + static const std::string kUdpRecvBuffer; RtpSession(const toolkit::Socket::Ptr &sock); ~RtpSession() override;