WebRTC:确定所选的ICE候选者

mid*_*ido 6 javascript webrtc

我有一个webrtc应用程序,让我们说两个客户端(client1client2),有没有办法找出ICE候选人给出的client1是什么client2,反之亦然?因为,每次发现这个,我必须wireshark在两个客户端使用,我认为阅读sdp可能有帮助,但我错了,因为它给所有可能的候选人...

场景:UDP client1的所有端口都被阻止(为了测试目的阻止了我).
Client1的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
...
Run Code Online (Sandbox Code Playgroud)

mid*_*ido 6

好吧,从我对另一个问题的回答中提取

我编写并测试了下面的代码,在firefox和chrome的最新版本中工作,getConnectionDetails返回一个解析为连接细节的promise:

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));
Run Code Online (Sandbox Code Playgroud)


Pri*_*ame 5

这个问题很老了,但在 2021 年,这是可靠的!- 至少对于我来说。

您可以使用RTCIceTransport.getSelectedCandidatePair() 方法,该方法返回RTCIceCandidatePair对象(本地和远程冰候选对)。

示例:获取用于从对等连接发送媒体的选定候选对。

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
    }
});
Run Code Online (Sandbox Code Playgroud)

对于在对等连接对象上接收到的媒体,上述操作类似。RTCPeerConnection.getReceivers()在这种情况下请改用