处理使用PeerConnection时处理ICE候选者的过程?

Dec*_*phy 7 javascript firefox p2p google-chrome webrtc

我已经用尽所有可能来获得稳定的WebRTC实现工作,并希望得到一些建议.

已经考虑了处理跨浏览器工作连接的所有可能解决方案,例如:

  • 在Chrome浏览器上发送SDP之前,等待所有候选人收集
  • 一旦收集了候选者,并在设置了本地sdp后将其添加到远程连接中
  • 一旦设置了本地和远程描述(远程和本地),添加候选者
  • 在收到候选人时添加候选人,并在回答时发送候选人
  • 在冰故障时重置对等连接
  • 其他人(匆忙)

基本上我要求有人帮助解决可能的图表或逐步处理ice应该处理的过程,以便为chrome和firefox跨浏览器提供有效的解决方案(两者都是当前发布的最新版本)时间).

在这一点上,我已经因为没有考虑任何其他可能性而烦恼,任何帮助都会受到高度赞赏.

谢谢,12月:)

小智 7

我同情你的挫败感.

对于RTCPeerConnection,在调用createOffer()然后调用setLocalDescription()之后,ICE代理将开始收集ICE候选.此时,您可以决定是否使用Trickle ICE,候选人一旦可用就被发送到远程对等体,或者您可以等待所有候选人被收集(大部分教程我都来了)跨越似乎假设涓涓细流的方法,但错过了一些正确处理这个问题的细节).

涓涓细流方法:

触发RTCPeerConnectionIceEvent时可以使用新的候选项:

peerConnection.onicecandidate = function(newRTCPeerConnectionIceEvent) {

    var newCandidate = newRTCPeerConnectionIceEvent.candidate;
    // send candidate to remote via signalling channel
}
Run Code Online (Sandbox Code Playgroud)

在远程端,候选人可以添加到他们的对等连接:

peerConnection.addIceCandidate(RTCIceCandidate);
Run Code Online (Sandbox Code Playgroud)

如果您尚未在远程对等连接上调用setRemoteDescription,我认为过早添加候选项的尝试将生成错误,因为这将尝试在未设置时将其添加到remoteDescription.在收到回复之前收到ICE候选人时,请参阅此错误.

非涓流方法:

您可以等待所有候选人收集如下:

peerConnection.onicecandidate = function(newRTCPeerConnectionIceEvent) {

    if (newRTCPeerConnectionIceEvent.candidate === null) {

       // send the offer (generated previously) to the remote peer
       // the offer sdp should contain all the gathered candidates
    }
}
Run Code Online (Sandbox Code Playgroud)

有关此技术的更多讨论,请参阅此链接:http://muaz-khan.blogspot.co.uk/2015/01/disable-ice-trickling.html(请参阅页面底部有关生成答案sdp的评论当报价已包含所有候选人时).

请注意,信令机制可能会影响您的方法,即您的信令中是否存在任何明显的延迟.我认为涓流方法假设您正在使用低延迟信令,因为它旨在减少呼叫建立时间.


N. *_*cA. 5

我发现这个功能在你的情况下很有用:)

function waitForAllICE(pc) {
  return new Promise((fufill, reject) => {
    pc.onicecandidate = (iceEvent) => {
      if (iceEvent.candidate === null) fufill()
    }
      setTimeout(() => reject("Waited a long time for ice candidates..."), 10000)
  }) 
} 
Run Code Online (Sandbox Code Playgroud)

然后你可以做一些类似的事情

pc.createOffer()
  .then(offer => pc.setLocalDescription(offer))
  .then(  ()  => waitForAllICE(pc))
  .then(  ()  => signallingWire.send(pc.localDescription))
  .catch(  e  => smartErrorHandling(e))
Run Code Online (Sandbox Code Playgroud)

  • 我相信“空冰候选表明所有已被发现”行为在规范中。我不会那样写(明确的比隐含的好)但是你就这样吧。 (2认同)