在 createOffer 或 createAnswer 之前获取 WebRTC IceCandidates

Getting WebRTC IceCandidates before createOffer or createAnswer

在建立视频通话期间,在调用 createOffer 或 createAnswer 后开始收集候选人。在调用 createOffer 或 createAnswer 之前,我想为本地对等方获取所有本地 ice 候选对象。这样,我将在 sdp 描述中设置所有这些,并且无需将它们单独发送到远程对等点,因为它们将在 sdp 中一起使用。可能吗?

更新: 我只想收集 Ice 候选人并留住他们。在我想创建要约和回答但不等待候选人聚集的情况下就会结束。还有如何手动将 Ice 候选人添加到描述中?是否可以在不重新创建 RTCPeerConnection 的情况下禁用 Ice 候选人收集?

候选人收集仅在您调用 setLocalDescription 时开始。

如果您想要一个包含所有候选者的 SDP,请等待没有候选者的 onicecandidate 事件并检查对等连接的 localDescription.sdp,其中包含目前收集到的所有候选者。

根据https://developer.mozilla.org/zh-CN/docs/Web/API/RTCPeerConnection/onicecandidate

当回调中的onicecandidate事件为null时,发出peer连接已经收集ice candidate完成。所以那时为其他对等连接创建报价,报价的 SDP 信息将包含 'a=candidate' 属性。

参考:https://aggresss.github.io/webrtc-samples/src/content/peerconnection/pc1-mod/

您可以通过设置 icecandidatepoolsize 来减少延迟 - 然后浏览器将尝试在 Offer/Answer 之前预先收集候选人。

见: https://github.com/pipe/two/blob/master/index.html#L181