Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to make a one way video stream to multiple peers. The caller will be able to send his video and the other peers will be able to receive it and not send any video back.

Only the caller can see himself so he has a localVideo element and startButton and no remoteVideo element and for the others its vice versa.

So far only one peer is able to view the video sent by the caller. What am I doing wrong here?

The lines commented in my code. Is that the way how it should be done to add more than 2 peers? But then in that way I am getting error on adding iceCandidates

What I have tried:

This is my code:

let serverConfig = {
    iceServers: [
      {"urls": "stun:stun.l.google.com:19302"},
      {"urls":  [
        "turn:13.250.13.83:3478?transport=udp"
        ],
      "username": "YzYNCouZM1mhqhmseWk6",
      "credential": "YzYNCouZM1mhqhmseWk6"}
    ]
  };

  let pc1 = new window.RTCPeerConnection(serverConfig);
  let pc2 = new window.RTCPeerConnection(serverConfig);
 // let pc3 = new window.RTCPeerConnection(serverConfig);

  let mediaConstraints = {
    audio: true,
    video: {
      width: { min: 1024, ideal: 1280, max: 1920 },
      height: { min: 576, ideal: 720, max: 1080 },
    }
  };

  const offerOptions = {
    offerToReceiveAudio: 1,
    offerToReceiveVideo: 1
  };

  pc1.onicecandidate = (evt) => {
    if (!evt.candidate) return;
    onIceCandidate('pc1', evt);
  }

  pc2.onicecandidate = (evt) => {
    if (!evt.candidate) return;
    onIceCandidate('pc2', evt);
  }

  // pc3.onicecandidate = (evt) => {
  //   if (!evt.candidate) return;
  //   onIceCandidate('pc3', evt);
  // }


  function onIceCandidate(peer, evt) {
    if (evt.candidate) {
      socket.emit("candidate", JSON.stringify({
        "candidate": evt.candidate,
        "peer": peer
      }));
    }
  }

  pc2.ontrack = function (evt) {
    if (remoteView != null) {
      remoteView.srcObject = evt.streams[0];
    }
  };

  // pc3.ontrack = function (evt) {
  //   if (remoteView != null) {
  //     remoteView.srcObject = evt.streams[0];
  //   }
  // };

if (localView != null) {
      
    if (navigator.mediaDevices.getUserMedia) {
      navigator.mediaDevices.getUserMedia(mediaConstraints).then(function (stream) {
        localView.srcObject = stream;
        pc1.addStream(stream);
      }).catch(function (err) {
        console.log('An error occurred', err);
      }); 
    } else {
      navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
      navigator.getUserMedia(mediaConstraints, function (stream) {
        localView.srcObject = stream;
        pc1.addStream(stream);
      }, function (evt) {
        console.log('An error occurred');
      });
    }
  }

if (startButton) {
    startButton.addEventListener("click", function () {
      pc1.createOffer(offerOptions).then(function (desc) {
        pc1.setLocalDescription(new RTCSessionDescription(desc));
        socket.emit("sdp", JSON.stringify({
          "sdp": desc,
          "peer": 'pc1'
        }));
      });
    });
  }

  socket.on("candidate", function (msg) {

    const candidateMsg = JSON.parse(msg);
    if (candidateMsg.peer === 'pc1') {
      pc2.addIceCandidate(new RTCIceCandidate(candidateMsg.candidate));
      // pc3.addIceCandidate(new RTCIceCandidate(candidateMsg.candidate));
    } else {
      pc1.addIceCandidate(new RTCIceCandidate(candidateMsg.candidate));
    }
    
  });

  socket.on("sdp", function (msg) {
    const sdpMsg = JSON.parse(msg);
    var sessionDesc = new RTCSessionDescription(sdpMsg.sdp);
    if (sdpMsg.peer === 'pc1') {
      pc2.setRemoteDescription(sessionDesc);
      pc2.createAnswer(offerOptions).then(function (sdp) {
        pc2.setLocalDescription(new RTCSessionDescription(sdp));
        socket.emit("answer", JSON.stringify({"sdp": sdp}));
      });

      // pc3.setRemoteDescription(sessionDesc);
      // pc3.createAnswer(offerOptions).then(function (sdp) {
      //   pc3.setLocalDescription(new RTCSessionDescription(sdp));
      //   socket.emit("answer", JSON.stringify({"sdp": sdp}));
      // });
    }   
  });

  socket.on("answer", function (answer) {
    if (localView) {
      pc1.setRemoteDescription(new RTCSessionDescription(JSON.parse(answer).sdp));
    }
  });
Posted
Updated 17-Jul-18 1:57am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900