5

I have a problem, sometime I need to reset the WebRTC state (for example I sometimes receive this error:

Failed to set remote offer sdp: Called in wrong state: kHaveLocalOffer

But is it possible to do so without dropping and creating a new RTCPeerConnection object? I do not want to stop the current local video capture...

jib
  • 40,579
  • 17
  • 100
  • 158
zeus
  • 12,173
  • 9
  • 63
  • 184

2 Answers2

7

But is it possible to do so without dropping and creating a new RTCPeerConnection object?

Yes, it's called "rollback":

(async () => {
  try {
    const pc1 = new RTCPeerConnection(), pc2 = new RTCPeerConnection();
    pc1.createDataChannel("dummy");
    const offer1 = await pc1.createOffer();

    // Say a remote offer comes in we're not ready for (most observable difference)
    const offer2 = await pc2.createOffer({offerToReceiveAudio: true,
                                          offerToReceiveVideo: true});
    await pc1.setRemoteDescription(offer2);
    console.log(pc1.getTransceivers().length); // 2

    await pc1.setRemoteDescription({type: "rollback"}); // <---

    await pc1.setLocalDescription(offer1);
    console.log(pc1.getTransceivers().length); // 0
  } catch(e) {
    console.log(e);
  }
})();

Unfortunately, Chrome does not implement "rollback" yet, but it works in Firefox. Chrome says:

TypeError: Failed to execute 'setRemoteDescription' on 'RTCPeerConnection': The
provided value 'rollback' is not a valid enum value of type RTCSdpType.

Please ★ this bug to urge Chrome to fix it.

jib
  • 40,579
  • 17
  • 100
  • 158
0

check your offer object's type:"offer"