WebRTC: Called in wrong state: STATE_SENTOFFER

折月煮酒 提交于 2019-12-23 17:58:59

问题


I'm following this tutorial, to make a simple example of WebRTC. But the remote video does not appear in any browser and Chrome does not show the error:

Uncaught (in promise) DOMException: Error processing ICE candidate

I made a log not setRemoteDescription method:

peerConn.setRemoteDescription(new RTCSessionDescription(signal.sdp), function(){
       alert('success')
    }, function(e){ console.log(e); alert(e)});

And then I get the following error:

OperationError: Failed to set remote offer sdp: Called in wrong state: STATE_SENTOFFER

In the tutorial in question, the author claims that he is able to do everything right and that the error should be on my side. Has anyone ever experienced this?

(sorry about my English)


EDIT: (Include code)

I am still a layman on the subject, the tutorial link quoted at the beginning was the cleanest I found to start having fun. I'll put the source I think it's important:

Back-end - server.js

/** successful connection */
wss.on('connection', function (client) {
  console.log("A new WebSocket client was connected.");
  /** incomming message */
  client.on('message', function (message) {
    /** broadcast message to all clients */
    wss.broadcast(message, client);
  });
});
// broadcasting the message to all WebSocket clients.
wss.broadcast = function (data, exclude) {
  var i = 0, n = this.clients ? this.clients.length : 0, client = null;
  if (n < 1) return;
  console.log("Broadcasting message to all " + n + " WebSocket clients.");
  for (; i < n; i++) {
    client = this.clients[i];
    // don't send the message to the sender...
    if (client === exclude) continue;
    if (client.readyState === client.OPEN) client.send(data);
    else console.error('Error: the client state is ' + client.readyState);
  }
};

Front-end - webrtc.js

wsc.onmessage = function (evt) {
  var signal = null;
  if (!peerConn) answerCall();
  signal = JSON.parse(evt.data);
  if (signal.sdp) {
    console.log("Received SDP from remote peer.");
    peerConn.setRemoteDescription(new RTCSessionDescription(signal.sdp), 
      function(){}, 
      function(e){ console.log(e); 
    });
  }
  else if (signal.candidate) {
    console.log("Received ICECandidate from remote peer.");
    peerConn.addIceCandidate(new RTCIceCandidate(signal.candidate));
  } else if ( signal.closeConnection){
    console.log("Received 'close call' signal from remote peer.");
    endCall();
  }
};

All font: Code taken from this github repository.


回答1:


Hard to answer without seeing code, but you have at least two problems, judging by the two errors:

Uncaught (in promise) DOMException: Error processing ICE candidate

This is from peerConn.addIceCandidate(candidate) and there's something wrong with the candidate input, suggesting it's incorrect or mangled somehow. You're supposed to signal it over your signaling channel from the other side's peerConn.onicecandidate. Show code please if more help is needed.

It's "uncaught" because it returns a promise, and you're missing a .catch:

peerConn.addIceCandidate(candidate).catch(e => console.log(e));

OperationError: Failed to set remote offer sdp: Called in wrong state: STATE_SENTOFFER

This suggests both peers tried to send an offer at the same time, which is symmetric and wrong.

The offer/answer exchange is inherently asymmetric. One side must start with an offer, the other side receives it, does SetRemote, createAnswer, and sends answer back to the first peer, which does setRemote. This dance is a state-machine. Any misstep and you get an error like this.



来源:https://stackoverflow.com/questions/41571718/webrtc-called-in-wrong-state-state-sentoffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!