kejm
kejm

Reputation: 113

How to make WebRTC video streaming on local network working?

I'm trying to establish peer connection between two clients via WebRTC and then stream the video from camera through the connection. The problem is, there's no video shown on the remote side, although I can clearly see the remotePc.ontrack event was fired. Also no error was thrown. I do NOT want to use the icecandidates mechanism (and it should NOT be needed), because the result application will only be used on a local network (the signaling server will only exchange the SDPs for the clients). Why is my example not working?

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Title</title>
  </head>
  <body>
    <video id="local" autoplay playsinline></video>
    <video id="remote" autoplay playsinline></video>
    <script>
      const localVideoElem = document.getElementById("local");
      const remoteVideoElem = document.getElementById("remote");

      const localPc = new RTCPeerConnection();
      const remotePc = new RTCPeerConnection();
      remotePc.ontrack = (event) => {
        remoteVideoElem.srcObject = event.streams[0];
      };

      navigator.mediaDevices
        .getUserMedia({
          video: true,
          audio: false,
        })
        .then((stream) => {
          localVideoElem.srcObject = stream;
          stream
            .getTracks()
            .forEach((track) => localPc.addTrack(track, stream));
          return localPc.createOffer({
            offerToReceiveVideo: true,
            offerToReceiveAudio: false,
          });
        })
        .then((offer) => {
          localPc.setLocalDescription(offer);
          remotePc
            .setRemoteDescription(offer)
            .then(() => remotePc.createAnswer())
            .then((answer) => {
              remotePc.setLocalDescription(answer).then(() => {
                localPc.setRemoteDescription(answer).then(() => {
                  console.log(localPc);
                  console.log(remotePc);
                });
              });
            });
        });
    </script>
  </body>
</html>

Upvotes: 1

Views: 2381

Answers (1)

Philipp Hancke
Philipp Hancke

Reputation: 17295

ICE candidates are needed, as they tell you the local addresses where the clients will connect to each other.

You won't need STUN servers though.

Upvotes: 2

Related Questions