RTCPeerConnection: canTrickleIceCandidates 属性

Baseline 已广泛支持

此特性已经十分成熟,可在许多设备和浏览器版本上使用。自 2022 年 3 月起,它已在各浏览器中可用。

RTCPeerConnection 接口的只读属性 canTrickleIceCandidates 返回一个布尔值,该值指示远程端点是否能够接收 “涓流式” ICE 候选(trickled ICE candidates)

ICE 涓流(ICE trickling)是指在初始的 offer 或 answer 已发送给对方之后,继续发送 ICE 候选的过程。

此属性仅在调用 RTCPeerConnection.setRemoteDescription() 后设置。理想情况下,您的信令协议会提供一种检测涓流支持的方法,这样您就不需要依赖此属性。WebRTC 浏览器始终支持涓流 ICE。如果不支持涓流,或者您无法得知其支持情况,您可以检查此属性是否为假值,然后在 iceGatheringState 的值变为 "completed" 之前,再创建并发送初始 offer。这样,offer 将包含所有候选。

如果远程端点能够接收涓流式 ICE 候选,则返回 true;如果不能,则返回 false。如果尚未建立远程端点,则此值为 null

注意:此属性的值在本地端点调用 RTCPeerConnection.setRemoteDescription() 后确定;ICE 代理使用提供的描述来确定远程端点是否支持涓流式 ICE 候选。

示例

js
const pc = new RTCPeerConnection();

function waitToCompleteIceGathering(pc) {
  return new Promise((resolve) => {
    pc.addEventListener(
      "icegatheringstatechange",
      (e) =>
        e.target.iceGatheringState === "complete" &&
        resolve(pc.localDescription),
    );
  });
}

// The following code might be used to handle an offer from a peer when
// it isn't known whether it supports trickle ICE.
async function newPeer(remoteOffer) {
  await pc.setRemoteDescription(remoteOffer);
  const offer = await pc.createOffer();
  await pc.setLocalDescription(offer);
  if (pc.canTrickleIceCandidates) return pc.localDescription;
  const answer = await waitToCompleteIceGathering(pc);
  sendAnswerToPeer(answer); // To peer via signaling channel
}
// Handle error with try/catch

pc.addEventListener(
  "icecandidate",
  (e) => pc.canTrickleIceCandidates && sendCandidateToPeer(e.candidate),
);

规范

规范
WebRTC:浏览器中的实时通信
# dom-rtcpeerconnection-cantrickleicecandidates

浏览器兼容性

另见