Rafal
Rafal

Reputation: 85

(webrtc) setRemoteDescription not working for the initiator

I have a very simple HTML page with a simple webrtc implementation, and a socket.io server. I have two video elements, one local and one remote. A user ID is generated randomly on page load. When a page loads, it joins a specific socket.io room with its random ID. I also have a call button that will call the other user.

When I click call, I create an offer, I set the local stream, I set local description, and send that SDP to the server. The second user picks it up, sets its remote description to the received description. Then it creates an answer, sets its local description to the new description, and sets its local stream. It then sends the answer sdp to the server. The initiator picks up the sdp and sets its remote description.

Is the above process more or less how this is supposed to work? I'm ignoring stun and turn for simplicity.

The problem is, I have the initiator's local stream loading, and both streams for the guest (both remote and local!) but the remote video isn't loading on the initiator's page. So steps 1, 2 and 3 work, but 4 doesn't.

I feel like I'm missing something extremely obvious here... Since the guest is able to see the local and the remote video, I don't think it's my stun/turn setup, but maybe?

Any tips? Anything immediately wrong here?

Upvotes: 1

Views: 418

Answers (1)

Rafal
Rafal

Reputation: 85

Problem solved. I was missing one part.

When the guest created the answer, it sent it correctly to the original caller. However, the guest did not add its local track to the peerconnection instance before generating the answer!

Instead of this:

                localVideo.srcObject = await navigator.mediaDevices.getUserMedia({ video: true, audio: false });
                let sdp = await pc.createAnswer();
                await pc.setLocalDescription(sdp);

It should have been this

                localVideo.srcObject = await navigator.mediaDevices.getUserMedia({ video: true, audio: false });

                // this the missing piece
                for (let track of localVideo.srcObject.getTracks()) {
                    pc.addTrack(track, localVideo.srcObject);
                }

                let sdp = await pc.createAnswer();
                await pc.setLocalDescription(sdp);

Upvotes: 0

Related Questions