为啥“onecandidate”不起作用?

Posted

技术标签:

【中文标题】为啥“onecandidate”不起作用?【英文标题】:why doesn't "onicecandidate" work?为什么“onecandidate”不起作用? 【发布时间】:2013-03-07 06:08:46 【问题描述】:

我无法理解 webRTC 的 PeerConnection 和 'oniceccandidate' 事件。

据我了解,您必须使用 STUN(或 TURN)服务器发起对等连接,因为它会将您的 ICE 候选者发送回与另一个对等方通信。

我见过一些示例将 PeerConnection 对象的服务器参数排除在外,我也不太理解,但我们只是说它确实需要服务器参数。

所以,当我写下以下代码时:

    var pc, ice =  "iceServers": [ "url": "stun:stun.l.google.com:19302" ] ;
if(typeof mozRTCPeerConnection === 'function') 

    pc = new mozRTCPeerConnection(ice);

else 
    console.log('google');
    pc = new webkitRTCPeerConnection(ice);



pc.onicecandidate  = function(event)  
    console.log(event);

我希望 'onicecandidate' 事件会触发,但它不起作用。我也尝试了其他公共 STUN 服务器,但没有任何反应。所以我认为我的理解可能有问题:)

【问题讨论】:

我不太确定,为什么我把它写成评论,但我认为你必须在设置事件处理程序以实际创建候选人时运行 pc.createOffer。 谢谢安德烈亚斯,我一定会尝试的。在阅读了 htmlRocks 上的“WebRTC 入门”教程后,我得出结论,收集 ICE 候选人和 SDP 信息是获取远程流的独立且强制性的过程,但您可能就在那儿。 WebRTC never fires onIceCandidate的可能重复 【参考方案1】:

在您调用 setLocalDescription(); 之前,PeerConnection 不会开始收集候选人。提供给 setLocalDescription 的信息告诉 PeerConnection 需要收集多少候选人。 (setLocalDescription 的这种行为在 https://datatracker.ietf.org/doc/html/draft-ietf-rtcweb-jsep-03#section-4.2.4 的定义中指出)

这是在同一浏览器窗口中的两个 PeerConnection 之间建立连接的完整流程(省略添加 MediaStreams 以专注于信令):

var pc1, pc2, offer, answer;

pc1 = new webkitRTCPeerConnection(options);
pc2 = new webkitRTCPeerConnection(options);

pc1.onicecandidate = function(candidate) 
  pc2.addIceCandidate(candidate);
;

pc2.onicecandidate = function(candidate) 
  pc1.addIceCandidate(candidate);
;

pc1.createOffer(onOfferCreated, onError);

function onError(err) 
  window.alert(err.message);


function onOfferCreated(description) 
  offer = description;
  pc1.setLocalDescription(offer, onPc1LocalDescriptionSet, onError);


function onPc1LocalDescriptionSet() 
  // after this function returns, pc1 will start firing icecandidate events
  pc2.setRemoteDescription(offer, onPc2RemoteDescriptionSet, onError);


function onPc2RemoteDescriptionSet() 
  pc2.createAnswer(onAnswerCreated, onError);


function onAnswerCreated(description) 
  answer = description;
  pc2.setLocalDescription(answer, onPc2LocalDescriptionSet, onError);


function onPc2LocalDescriptionSet() 
  // after this function returns, you'll start getting icecandidate events on pc2
  pc1.setRemoteDescription(answer, onPc1RemoteDescriptionSet, onError);


function onPc1RemoteDescriptionSet() 
  window.alert('Yay, we finished signaling offers and answers');

由于您在问题中包含了 mozPeerConnection,我会注意到 Firefox 目前不生成“涓流候选者”。这意味着它将在提议/答案中将其候选地址作为“c”行包含在内,并且永远不会调用 onececandidate 回调。

这种方法的缺点是 Firefox 必须等待所有候选者都被收集到,然后才能创建它的提议/答案(这个过程可能涉及联系 STUN 和 TURN 服务器并等待响应或请求超时) .

【讨论】:

感谢您的解释!现在开始变得更有意义了。 这仍然不会调用onicecandidate。 我也是。也许是因为两个频道在同一边? 请参阅this answer 以让冰候选人工作。此答案中的方法是正确的,但 createOffer() 参数已更改(现在已更改多次)。

以上是关于为啥“onecandidate”不起作用?的主要内容,如果未能解决你的问题,请参考以下文章

Angular 的 $http.post 不起作用,它的 $http... 也不起作用,但 jQuerys ajax 起作用。为啥?

为啥 preventDefault 不起作用?

为啥 innerHtml 不起作用

为啥(ngModel)不起作用?

css 为啥有时MARGIN 不起作用,

为啥我的poi autoSizeColumn()不起作用