From 301cbf0a8347dd1cce6332dbb5496b4493ee833a Mon Sep 17 00:00:00 2001 From: mtdxc Date: Thu, 12 May 2022 15:21:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A4=9A=E4=B8=AArtc?= =?UTF-8?q?=E5=80=99=E9=80=89=E5=9C=B0=E5=9D=80=20(#1622)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 支持多个rtc候选地址 * fixed missing extern_ips check --- conf/config.ini | 2 +- webrtc/WebRtcTransport.cpp | 57 +++++++++++++++++++++++++++----------- webrtc/WebRtcTransport.h | 1 - 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/conf/config.ini b/conf/config.ini index 7094600b..c2d4613f 100644 --- a/conf/config.ini +++ b/conf/config.ini @@ -266,7 +266,7 @@ port_range=30000-35000 [rtc] #rtc播放推流、播放超时时间 timeoutSec=15 -#本机对rtc客户端的可见ip,作为服务器时一般为公网ip,置空时,会自动获取网卡ip +#本机对rtc客户端的可见ip,作为服务器时一般为公网ip,可有多个,用','分开,当置空时,会自动获取网卡ip externIP= #rtc udp服务器监听端口号,所有rtc客户端将通过该端口传输stun/dtls/srtp/srtcp数据, #该端口是多线程的,同时支持客户端网络切换导致的连接迁移 diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index b6649597..b7deceb3 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -471,10 +471,15 @@ void WebRtcTransportImp::onStartWebRTC() { void WebRtcTransportImp::onCheckAnswer(RtcSession &sdp) { //修改answer sdp的ip、端口信息 - GET_CONFIG(string, extern_ip, RTC::kExternIP); + GET_CONFIG_FUNC(std::vector, extern_ips, RTC::kExternIP, [](string str){ + std::vector ret; + if (str.length()) + ret = split(str, ","); + return ret; + }); for (auto &m : sdp.media) { m.addr.reset(); - m.addr.address = extern_ip.empty() ? SockUtil::get_local_ip() : extern_ip; + m.addr.address = extern_ips.empty() ? SockUtil::get_local_ip() : extern_ips[0]; m.rtcp_addr.reset(); m.rtcp_addr.address = m.addr.address; @@ -522,28 +527,48 @@ void WebRtcTransportImp::onCheckSdp(SdpType type, RtcSession &sdp) { } } -void WebRtcTransportImp::onRtcConfigure(RtcConfigure &configure) const { - WebRtcTransport::onRtcConfigure(configure); - //添加接收端口candidate信息 - configure.addCandidate(*getIceCandidate()); -} - -SdpAttrCandidate::Ptr WebRtcTransportImp::getIceCandidate() const{ +SdpAttrCandidate::Ptr makeIceCandidate(std::string ip, uint16_t port, + uint32_t priority = 100, std::string proto = "udp") { auto candidate = std::make_shared(); - candidate->foundation = "udpcandidate"; //rtp端口 candidate->component = 1; - candidate->transport = "udp"; + candidate->transport = proto; + candidate->foundation = proto + "candidate"; //优先级,单candidate时随便 - candidate->priority = 100; - GET_CONFIG(string, extern_ip, RTC::kExternIP); - candidate->address = extern_ip.empty() ? SockUtil::get_local_ip() : extern_ip; - GET_CONFIG(uint16_t, local_port, RTC::kPort); - candidate->port = local_port; + candidate->priority = priority; + candidate->address = ip; + candidate->port = port; candidate->type = "host"; return candidate; } +void WebRtcTransportImp::onRtcConfigure(RtcConfigure &configure) const { + WebRtcTransport::onRtcConfigure(configure); + + GET_CONFIG(uint16_t, local_port, RTC::kPort); + //添加接收端口candidate信息 + GET_CONFIG_FUNC(std::vector, extern_ips, RTC::kExternIP, [](string str){ + std::vector ret; + if (str.length()) + ret = split(str, ","); + return ret; + }); + if (extern_ips.empty()) { + std::string localIp = SockUtil::get_local_ip(); + configure.addCandidate(*makeIceCandidate(localIp, local_port, 120, "udp")); + } + else { + const uint32_t delta = 10; + uint32_t priority = 100 + delta * extern_ips.size(); + for (auto ip : extern_ips) { + configure.addCandidate(*makeIceCandidate(ip, local_port, priority, "udp")); + priority -= delta; + } + } +} + + + /////////////////////////////////////////////////////////////////// class RtpChannel : public RtpTrackImp, public std::enable_shared_from_this { diff --git a/webrtc/WebRtcTransport.h b/webrtc/WebRtcTransport.h index f880c9d9..a5bd4147 100644 --- a/webrtc/WebRtcTransport.h +++ b/webrtc/WebRtcTransport.h @@ -264,7 +264,6 @@ protected: void updateTicker(); private: - SdpAttrCandidate::Ptr getIceCandidate() const; void onSortedRtp(MediaTrack &track, const std::string &rid, mediakit::RtpPacket::Ptr rtp); void onSendNack(MediaTrack &track, const mediakit::FCI_NACK &nack, uint32_t ssrc); void onSendTwcc(uint32_t ssrc, const std::string &twcc_fci);