WebRTC:确定所选的 ICE 候选者 [英] WebRTC: Determine the chosen ICE candidate

查看:85
本文介绍了WebRTC:确定所选的 ICE 候选者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 webrtc 应用程序,假设有两个客户端(client1client2),有没有办法找出 client1 给出的 ICE 候选对象client2 使用,反之亦然?因为,每次发现这一点时,我都必须在两个客户端上使用 wireshark,我认为阅读 sdp 可能会有所帮助,但我错了,因为它提供了所有可能的候选人...

I have a webrtc app, and let's say two clients( client1 and client2), is there any way to find out what ICE candidate given by client1 is used by client2 and vice versa? because, every time to find this out, I have to use wireshark on both the clients, I thought reading the sdp might help, but I was wrong, as it gives all possible candidates...

场景:client1 的所有 UDP 端口都被封锁(为了测试目的封锁了我的我).
Client1 的 SDP:

Scenario: all UDP ports of client1 are blocked( blocked my me for testing purpose).
Client1's 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
...

推荐答案

好吧,摘自我的 回答另一个问题

Well, taken from my answer to another question

我编写并测试了以下代码段,适用于最新版本的 Firefox 和 chrome,getConnectionDetails 返回一个解析为连接详细信息的承诺:

I wrote and tested the below piece of code, works in latest versions of both firefox and chrome, getConnectionDetails returns a promise which resolves to connection details:

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

这篇关于WebRTC:确定所选的 ICE 候选者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆