yarek
yarek

Reputation: 12044

Webrtc : how to add streams to a connected RTCPeerConnection?

I am using pure HTTP polling for establish connections.

PC1 : opens cam, create and send his offer.

PC2 : accepts offer, create answer, send answer.

PC1 and PC2 are connected and PC2 can watch PC1 cam.

Now PC2 opens his cam :I display the local webcam and add video and audio to the connected pc (RTCPeerConnection)

localStream = await navigator.mediaDevices.getUserMedia({video: true, audio: true});
myVideo.srcObject = localStream; // display local webcam
localStream.getTracks().forEach((track) => pc.addTrack(track, localStream)); // add audio and video to the connected pc

However, PC1 cannot watch PC2 webcam. What did I miss : How to add streams to already connected RTCPeerConnection ?

Upvotes: 2

Views: 612

Answers (1)

Philipp Hancke
Philipp Hancke

Reputation: 17295

Since adding the streams changes the SDP you need to renegotiate, i.e. call createOffer, setLocalDescription and (after getting an answer from the remote end) setRemoteDescription. This is called renegotiation (and you will notice the negotiationneeded event firing).

This sample shows upgrading an audio-only call to an audio-video one but the same principle applies.

Note that in your case you are changing who sends the offer so you might run into a problem called 'glare' which is explained nicely in this Mozilla blog post.

Upvotes: 3

Related Questions