javascriptwebrtc

webrtc peer connection: can not create an answer


I have a problem when I can't create an answer but the connection state is already have-remote-offer.

Code:

pc.ontrack = function(evt) {

  // signaling state is have-remote-offer
  logEvent('REMOTE', 'signalingstate', pc.signalingState);

  pc.createAnswer() // error here
    .then((answer) => {
       localConnection.setLocalDescription(answer);
       logEvent('REMOTE', 'signalingstate', pc.signalingState);

       socket.emit('session_description', JSON.stringify({ desc: answer.toJSON() }));
  });
};

Error:

Uncaught (in promise) DOMException: Peer Connection cannot create an answer in a state other than have-remote-offer or have-local-pranswer

Any ideas?


Solution

  • calling createAnswer from ontrack is not going to work typically. You'll want to call it from the setRemoteDescription .then()