javascriptwebrtc

How to generate an ICE candidate?


I'm developing a video conference with WebRTC in a local network, so I use only one signaling server to exchage SDP data. As I understand, I also need to exchange ICE candidates, but I don't know how to generate them. Thanks.


Solution

  • You can get the generated iceCandidate by setting the peerConnection.onicecandidate event.

    (async () => {
      const pc = new RTCPeerConnection();
      pc.onicecandidate = evt => {
        console.log(evt.candidate?.candidate);
      };
      const stream = await navigator.mediaDevices.getUserMedia({video:true});
      stream.getTracks().forEach(track => pc.addTrack(track, stream));
      const offer = await pc.createOffer();
      await pc.setLocalDescription(offer);
    })();