PeerConnection无法创建答案 [英] PeerConnection cannot create an answer
问题描述
当我执行 myPeerConnection.createAnswer()
PeerConnection无法在
have-remote-offer或have-local-pranswer以外的状态下创建答案。
PeerConnection cannot create an answer in a state other than have-remote-offer or have-local-pranswer.
我使用 socket.io 作为信令服务器。我正在关注 MDN 的教程
I am using socket.io as the signalling server. I am following the tutorial from MDN
这是我的代码:
myPeerConnection.setRemoteDescription(desc).then(() => {
return navigator.mediaDevices.getUserMedia(mediaConstraints);
}).then((stream) => {
localStream = stream;
document.getElementById("localVideo").srcObject = localStream;
return myPeerConnection.addStream(localStream);
}).then(() => {
return myPeerConnection.createAnswer(); //No error when removed this then chain
}).then((answer) => {
return myPeerConnection.setLocalDescription(answer); // No error when removed this then chain
}).then(() => {
socket.emit('video-answer', {
sdp: myPeerConnection.localDescription
});
}).catch(handleGetUserMediaError);
这里的答案对我没有帮助。
我已经在Github上传了整个项目。您可以查看脚本文件此处。
I have uploaded the whole project on Github. You can look at the script file here.
感谢任何帮助。
推荐答案
这是一个长期存在的 Chrome中的错误我提交了一份一年半以前。
This is long-standing a bug in Chrome I filed a year and a half ago.
您正在 onclick
处理程序和<$ c中创建对等连接$ c> handleVideoOfferMsg ,包含 onnegotiationneeded
处理程序,该处理程序调用 createOffer
。可以直接使用规范示例。
You're creating a peer connection in both the onclick
handler and handleVideoOfferMsg
, complete with an onnegotiationneeded
handler that calls createOffer
. That's OK and straight out of the spec example.
在 handleVideoOfferMsg
中,您继续调用 setRemoteDescription( desc)
,将该对等连接带到 has-remote-offer
状态,然后为其添加曲目以供回答。
In handleVideoOfferMsg
you go on to call setRemoteDescription(desc)
, bringing that peer connection to have-remote-offer
state, and then you add tracks to it for your answer.
Chrome中的错误是当 negotiationneeded 事件.github.io / webrtc-pc / #dfn-update-the-negotiation-needed-flagrel =nofollow noreferrer> spec
表示只在<$ c $中设置negotiationneeded标志c>稳定州。
The bug in Chrome is that adding those tracks fire the negotiationneeded
event, when the spec
says to only set the negotiationneeded flag in "stable"
state.
在Firefox中试用。它应该可以在那里工作。
Try it in Firefox. It should work there.
你可以在Chrome中解决这个问题,比如:
You can work around this in Chrome somewhat, like this:
pc.onnegotiationneeded = e => {
if (pc.signalingState != "stable") return;
..
}
这篇关于PeerConnection无法创建答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!