Search code examples
javascriptwebrtcvideo-streamingvideo-conferencing

WebRTC PeerConnection addTrack after connection established


I'm doing a video conference website. The use case is a user are showing camera and everyone already see here camera. It mean the connection is stable. And user want to share screen. After I have screen stream, I add track to peerConnection but remote computer not fire ontrack event.

Here is my code after I got screen stream:

       if (this.screenStream != null && 'getTracks' in this.screenStream) {
          this.screenStream.getTracks().forEach(track => {
            console.log('ADD TRACK FOR CONNECTION');
            // const sender = peerConnection.getSenders();
            peerConnection.addTrack(track, this.screenStream);
          });

        }

Please tell me what I was wrong, or guide me some solutions, thank you so much!


Solution

  • You need to renegotiate after addTrack. You can either do so manually by calling createOffer, setLocalDescription and setRemoteDescription or rely on the onnegotiationneeded callback to happen as described in https://blog.mozilla.org/webrtc/perfect-negotiation-in-webrtc/