Javascript WebRTC:确定所选的ICE候选者
我有一个webrtc应用程序,比如说两个客户端(Javascript WebRTC:确定所选的ICE候选者,javascript,webrtc,Javascript,Webrtc,我有一个webrtc应用程序,比如说两个客户端(client1和client2),有没有办法找出client1给出的ICE候选对象被client2使用,反之亦然?因为,每次我都要在两个客户端上使用wireshark,我认为阅读sdp可能会有所帮助,但我错了,因为它提供了所有可能的候选者 场景:客户端1的所有UDP端口都被阻止(出于测试目的而阻止了我的me)。 客户1的SDP: ... a=rtcp:49407 IN IP4 <client1's IP> a=candidate:386
client1
和client2
),有没有办法找出client1
给出的ICE候选对象被client2
使用,反之亦然?因为,每次我都要在两个客户端上使用wireshark
,我认为阅读sdp
可能会有所帮助,但我错了,因为它提供了所有可能的候选者
场景:客户端1的所有UDP
端口都被阻止(出于测试目的而阻止了我的me)。客户1的SDP:
...
a=rtcp:49407 IN IP4 <client1's IP>
a=candidate:3864409487 1 udp 2122194687 <client1's IP> 49407 typ host generation 0 // this would never work, since the udp ports are blocked...
a=candidate:3864409487 2 udp 2122194687 <client1's IP> 49407 typ host generation 0
a=candidate:2832583039 1 tcp 1518214911 <client1's IP> 0 typ host tcptype active generation 0
a=candidate:2832583039 2 tcp 1518214911 <client1's IP> 0 typ host tcptype active generation 0
a=candidate:973648460 1 udp 25042687 <TURN server IP> 64790 typ relay raddr <Proxy IP> rport 39963 generation 0
a=ice-ufrag:YSvrOiav8TglpCWD
...
。。。
a=rtcp:49407在IP4 49407典型主机生成0//中,这永远不会工作,因为udp端口被阻止。。。
a=候选:3864409487 2 udp 2122194687 0典型主机tcptype活动生成0
a=候选人:2832583039 2 tcp 1518214911好吧,从我的问题转到另一个问题
我编写并测试了以下代码,适用于firefox和chrome的最新版本,getConnectionDetails
返回一个承诺,该承诺将解析连接细节:
function getConnectionDetails(peerConnection){
var connectionDetails = {}; // the final result object.
if(window.chrome){ // checking if chrome
var reqFields = [ 'googLocalAddress',
'googLocalCandidateType',
'googRemoteAddress',
'googRemoteCandidateType'
];
return new Promise(function(resolve, reject){
peerConnection.getStats(function(stats){
var filtered = stats.result().filter(function(e){return e.id.indexOf('Conn-audio')==0 && e.stat('googActiveConnection')=='true'})[0];
if(!filtered) return reject('Something is wrong...');
reqFields.forEach(function(e){connectionDetails[e.replace('goog', '')] = filtered.stat(e)});
resolve(connectionDetails);
});
});
}else{ // assuming it is firefox
return peerConnection.getStats(null).then(function(stats){
var selectedCandidatePair = stats[Object.keys(stats).filter(function(key){return stats[key].selected})[0]]
, localICE = stats[selectedCandidatePair.localCandidateId]
, remoteICE = stats[selectedCandidatePair.remoteCandidateId];
connectionDetails.LocalAddress = [localICE.ipAddress, localICE.portNumber].join(':');
connectionDetails.RemoteAddress = [remoteICE.ipAddress, remoteICE.portNumber].join(':');
connectionDetails.LocalCandidateType = localICE.candidateType;
connectionDetails.RemoteCandidateType = remoteICE.candidateType;
return connectionDetails;
});
}
}
//usage example:
getConnectionDetails(pc).then(console.log.bind(console));
在chrome中,我使用了以下内容:
let pc1 = new RTCPeerConnection(cfg);
pc1.addEventListener('connectionstatechange', async (e) => {
if (pc1.connectionState === 'connected') {
// Peers connected!
let stats = await pc1.getStats();
for (const value of stats.values()) {
if(value.type=="local-candidate" || value.type=="remote-candidate")
console.log(value);
}
}
})
这个问题由来已久,但以下是在2021年可靠运行的方法至少对我来说
可以使用返回对象(本地和远程ice候选对)的
例如:
从对等连接获取用于发送媒体的选定候选对
peerConnection.getSenders().map(sender => {
const kindOfTrack = sender.track?.kind;
if (sender.transport) {
const iceTransport = sender.transport.iceTransport;
const logSelectedCandidate = (e) => {
const selectedCandidatePair = iceTransport.getSelectedCandidatePair();
console.log(`SELECTED ${kindOfTrack || 'unknown'} SENDER CANDIDATE PAIR`, selectedCandidatePair);
};
iceTransport.onselectedcandidatepairchange = logSelectedCandidate;
logSelectedCandidate();
} else {
// retry at some time later
}
});
上述方法同样适用于对等连接对象上的接收媒体。
使用rtpeerconnection.getReceivers()
代替,在这种情况下检查此线程:我为googRemoteCandidateType
获取了prflx
,为GoogGlocalCandidateType
获取了relay
。你知道prflxprflx
是什么意思吗?@ana必须检查一下,但我认为是晕眩ice候选者prflx
意味着同伴自反候选者。看到这篇文章也有一些好的信息