我有两个想要通过 WebRTC 相互连接的对等方。通常,第一个对等点会创建一个报价并通过信令 channel /服务器将其发送给第二个对等点,第二个对等点会回应一个答案。这个场景工作正常。
但是,是否有可能支持这样一种情况,即两个对等点碰巧尝试同时相互连接,并且都通过信令服务器同时向对方发送 SDP 提议。
// Both peers do this simultaneously:
const conn = new RTCPeerConnection(null);
const sdpOffer = await conn.createOffer();
await conn.setLocalDescription(sdpOffer);
signalingService.send(peerId, sdpOffer);
// At some point in the future both peers also receive an SDP offer
// (rather than answer) from the other peer whom they sent an offer to
// via the signaling service. If this was an answer we'd call
// RTCPeerConnection.setRemoteDescription, however this doesn't work for an
// offer:
conn.setRemoteDescription(peerSDPOffer);
// In Chrome results in "DOMException: Failed to execute 'setRemoteDescription' on 'RTCPeerConnection': Failed to set remote offer sdp: Called in wrong state: kHaveLocalOffer"
我什至尝试通过将 SDP 类型从 offer
重写为 answer
和 setup:actpass
来“转换”接收到的对等方提供的答案到 setup:active
但这似乎不起作用,相反我只是得到一个新的异常。
所以问题是,这种同时连接/提供用例是否以某种方式得到支持——或者我应该关闭一侧/对等 RTCPeerConnection 并使用 RTCPeerConnection.createAnswer
实例化一个新连接?
最佳答案
这种情况被称为“信号眩光”。 WebRTC API 并没有真正定义如何处理这种情况(除了称为“回滚”的东西,但它还没有在任何浏览器中实现,到目前为止没有人错过它)所以你必须自己避免这种情况。
简单地替换 a=setup 是行不通的,因为底层 DTLS 机制仍然需要客户端和服务器的概念。
关于javascript - 是否可以将 WebRTC SDP 报价转换为答案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53990959/