Commit 301cbf0a by mtdxc Committed by GitHub

支持多个rtc候选地址 (#1622)

* 支持多个rtc候选地址

* fixed missing extern_ips check
parent 943deab6
...@@ -266,7 +266,7 @@ port_range=30000-35000 ...@@ -266,7 +266,7 @@ port_range=30000-35000
[rtc] [rtc]
#rtc播放推流、播放超时时间 #rtc播放推流、播放超时时间
timeoutSec=15 timeoutSec=15
#本机对rtc客户端的可见ip,作为服务器时一般为公网ip,置空时,会自动获取网卡ip #本机对rtc客户端的可见ip,作为服务器时一般为公网ip,可有多个,用','分开,当置空时,会自动获取网卡ip
externIP= externIP=
#rtc udp服务器监听端口号,所有rtc客户端将通过该端口传输stun/dtls/srtp/srtcp数据, #rtc udp服务器监听端口号,所有rtc客户端将通过该端口传输stun/dtls/srtp/srtcp数据,
#该端口是多线程的,同时支持客户端网络切换导致的连接迁移 #该端口是多线程的,同时支持客户端网络切换导致的连接迁移
......
...@@ -471,10 +471,15 @@ void WebRtcTransportImp::onStartWebRTC() { ...@@ -471,10 +471,15 @@ void WebRtcTransportImp::onStartWebRTC() {
void WebRtcTransportImp::onCheckAnswer(RtcSession &sdp) { void WebRtcTransportImp::onCheckAnswer(RtcSession &sdp) {
//修改answer sdp的ip、端口信息 //修改answer sdp的ip、端口信息
GET_CONFIG(string, extern_ip, RTC::kExternIP); GET_CONFIG_FUNC(std::vector<std::string>, extern_ips, RTC::kExternIP, [](string str){
std::vector<std::string> ret;
if (str.length())
ret = split(str, ",");
return ret;
});
for (auto &m : sdp.media) { for (auto &m : sdp.media) {
m.addr.reset(); 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.reset();
m.rtcp_addr.address = m.addr.address; m.rtcp_addr.address = m.addr.address;
...@@ -522,28 +527,48 @@ void WebRtcTransportImp::onCheckSdp(SdpType type, RtcSession &sdp) { ...@@ -522,28 +527,48 @@ void WebRtcTransportImp::onCheckSdp(SdpType type, RtcSession &sdp) {
} }
} }
void WebRtcTransportImp::onRtcConfigure(RtcConfigure &configure) const { SdpAttrCandidate::Ptr makeIceCandidate(std::string ip, uint16_t port,
WebRtcTransport::onRtcConfigure(configure); uint32_t priority = 100, std::string proto = "udp") {
//添加接收端口candidate信息
configure.addCandidate(*getIceCandidate());
}
SdpAttrCandidate::Ptr WebRtcTransportImp::getIceCandidate() const{
auto candidate = std::make_shared<SdpAttrCandidate>(); auto candidate = std::make_shared<SdpAttrCandidate>();
candidate->foundation = "udpcandidate";
//rtp端口 //rtp端口
candidate->component = 1; candidate->component = 1;
candidate->transport = "udp"; candidate->transport = proto;
candidate->foundation = proto + "candidate";
//优先级,单candidate时随便 //优先级,单candidate时随便
candidate->priority = 100; candidate->priority = priority;
GET_CONFIG(string, extern_ip, RTC::kExternIP); candidate->address = ip;
candidate->address = extern_ip.empty() ? SockUtil::get_local_ip() : extern_ip; candidate->port = port;
GET_CONFIG(uint16_t, local_port, RTC::kPort);
candidate->port = local_port;
candidate->type = "host"; candidate->type = "host";
return candidate; 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<std::string>, extern_ips, RTC::kExternIP, [](string str){
std::vector<std::string> 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<RtpChannel> { class RtpChannel : public RtpTrackImp, public std::enable_shared_from_this<RtpChannel> {
......
...@@ -264,7 +264,6 @@ protected: ...@@ -264,7 +264,6 @@ protected:
void updateTicker(); void updateTicker();
private: private:
SdpAttrCandidate::Ptr getIceCandidate() const;
void onSortedRtp(MediaTrack &track, const std::string &rid, mediakit::RtpPacket::Ptr rtp); 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 onSendNack(MediaTrack &track, const mediakit::FCI_NACK &nack, uint32_t ssrc);
void onSendTwcc(uint32_t ssrc, const std::string &twcc_fci); void onSendTwcc(uint32_t ssrc, const std::string &twcc_fci);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论