question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Failed to execute 'setRemoteDescription' on 'RTCPeerConnection': Failed to set remote answer sdp: Called in wrong state: kStable

See original GitHub issue

I’m trying to figure out what’s wrong with what I did, but for some reason, completely random, some users won’t establish a connection. I use Xirsys to get dynamic ICE Servers on the server side. This happens in the socket connected method, and it’s async so before i emit the video_users event, i await on receiving the dynamic ICE Servers. But those are not the problem.

    socket.on("connect", () => {
      this.setState({ id: socket.id });
    });

    socket.open();

This function generates the peer:

  generatePeer(id: string, initiator: boolean = false, iceServers: any[] = null) {
    if (!iceServers) return null;
    let peer = new Peer({
      initiator: initiator,
      trickle: false,
      config: {
        iceServers: iceServers
      },
      stream: this.state.stream
    });

    peer.on("signal", (data: any) => {
      if (this.state.id !== id) {
        socket.emit("video_signal", {
          signal: data,
          to: id,
          name: this.state.name,
          host: this.state.host,
          iceServers: iceServers
        });
      }
    });

    peer.on("connect", () => {
      console.log("CONNECTED PEER " + id);
    });

    peer.on("stream", (stream: any) => {
      let partners = this.state.partners;
      if (partners[id] && partners[id].video) {
        if ("srcObject" in partners[id].video) {
          partners[id].video.srcObject = stream;
        } else {
          partners[id].video.src = window.URL.createObjectURL(stream);
        }
        this.setState({ partners });
      }
    });

    peer.on("error", (err: any) => {
      console.error(err);
    });

    peer.on("close", () => {
      console.log("CLOSED PEER " + id);
      delete this.peers[id];
      delete partners[id];
      this.setState({ partners });
    });

    return peer;
  }

This event calls it (video_users is an event called every time someone enters the room)

    this.io.on("video_users", (peers: any) => {
      peers.map((peer: any) => {
        if (!this.peers[peer.id] && peer.id !== this.state.id) {
            this.peers[peer.id] = this.generatePeer(peer.id, true, data.iceServers);
          }
      });
    });

And this event is called when a signal has been sent:

    this.io.on("video_new_signal", async (data: any) => {
      let id = data.from;
      if (id != this.state.id) {
        if (!this.peers[id])
          this.peers[id] = this.generatePeer(id, false, data.iceServers);

        this.peers[id].signal(data.signal);
        let partners = this.state.partners;
        if (!partners[id]) {
          partners[id] = { name: data.name, full: false, host: data.host };
          this.setState({ partners });
        }
      }
    });

This is a multi-video room chat, so full mesh. But the problem happens also when it’s one-on-one video chat. What am I doing wrong? Is there a way to simulate a problem? Because right now it’s completely random and very hard to debug.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:17 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
t-mullencommented, Aug 7, 2020

Ah, that is the “negotiate” signal that’s being incorrectly sent. See: https://github.com/feross/simple-peer/issues/670

We’re working on a fix. In the meantime, you can disable renegotiation by dropping the signal.

peer.on('signal', (data) => {
  if (data. renegotiate || data.transceiverRequest) return
})

This may or may not solve your issues.

2reactions
t-mullencommented, Aug 7, 2020

You’re trying to process an SDP answer when you haven’t generated an offer or have already processed an answer. Basically, your signalling messages are going to the wrong peers or are arriving in the wrong order.

I recommend logging the signalling messages that each peer is receiving and checking the “type” field is “offer” or “answer” as you would expect.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Failed to set remote answer sdp: Called in wrong state: stable
Since one RTCPeerConnection can only establish one peer-to-peer connection, it will complain when it tries to setRemoteDescription on the ...
Read more >
RTCPeerConnection setting offer sdp does not match the ...
I have received answer sdp by negotiating offer sdp from peer in different process, ... Failed to set remote answer sdp: Called in...
Read more >
setRemoteDescription: Failed to set remote answer sdp ...
[Solved]-"Unable to RTCPeerConnection::setRemoteDescription: Failed to set remote answer sdp: Called in wrong state: kStable"-Flutter ... Worked on the first try.
Read more >
3410 - Failed to set remote offer sdp: Called in wrong state
Failed to create session description: CreateAnswer can't be called before SetRemoteDescription. Can any one help us, when we try three users WEBRTC I...
Read more >
failed to set remote answer sdp: called in wrong state: kstable
OSCHINA.NET 是目前领先的中文开源技术社区。我们传播开源的理念,推广开源项目,为IT 开发者提供了一个发现、使用、并交流开源技术的平台.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found