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

* 支持多个rtc候选地址

* fixed missing extern_ips check
This commit is contained in:
mtdxc 2022-05-12 15:21:09 +08:00 committed by GitHub
parent 943deab664
commit 301cbf0a83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 18 deletions

View File

@ -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数据
#该端口是多线程的,同时支持客户端网络切换导致的连接迁移

View File

@ -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<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) {
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<SdpAttrCandidate>();
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<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> {

View File

@ -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);