首页 > 解决方案 > WebRTC,如何在没有冰涓涓细的情况下收集全套候选人?

问题描述

我正在尝试在两台电脑之间进行视频通话,我不想使用冰滴,有时我可以进行视频通话,其他时候(我不知道为什么)我无法收集所有的冰候选人,iceGatheringState 停留在聚集状态,永远不会完成。

我已经尝试使用事件 onicecandidate 并等待空候选人。现在我正在使用 onIceGatheringStateChange。

pc=new RTCPeerConnection(iceServers);
pc.onicegatheringstatechange=function(){
  if(pc.iceGatheringState=='complete'){
    send_to_target(pc.localDescription);
  }
}   
localStream.getTracks().forEach(track=>pc.addTrack(track,localStream));
pc.createOffer().then(function(sessionDescription){
  pc.setLocalDescription(sessionDescription);
})

我正在两台带有 chrome 的笔记本电脑上对此进行测试,我希望 iceGatheringState 进入完整状态或知道另一种方式/条件来收集候选冰,以便在不使用冰滴的情况下将 sessionDescription 发送到目标。

谢谢。

标签: javascriptwebrtc

解决方案


据我所知,有两种方法可以确定“ICE 候选人聚集”/“ICE Trickle”何时完成。

  1. 您可以通过观察 icegatheringstatechange 事件并检查 iceGatheringState 的值是否等于“完成”来确定 ICE 候选收集已完成。

来自 MDN 的代码示例

let pc = new RTCPeerConnection();
pc.onicegatheringstatechange = ev => {
  let connection = ev.target;

  switch(connection.iceGatheringState) {
    case "gathering":
      /* collection of candidates has begun */
      break;
    case "complete":
      /* collection of candidates is finished */
      break;
  }
}
  1. 您还可以让 icecandidate 事件的处理程序查看其候选属性是否等于“null”。

来自 MDN 的代码示例

let pc = new RTCPeerConnection();
pc.onicecandidate = function(event) {
  if (event.candidate) {
    /* Code for each candidate */
    /* Send the candidate to the remote peer */
  } else {
    /* All ICE candidates have been sent */
  }
}

WebRTC 的有用链接:


推荐阅读