Mohamad Alasly
Mohamad Alasly

Reputation: 340

webrtc is working on all browsers but not working on firefox

before anything ! i have checked all answers in similar questions and the answers wasn't usefull and old . i have tried to use webrtc on firefox but it did not work ! and no error was givin !

this code work on all browsers but firefox

this is my code

let configuration = {

        "iceServers":[{
            'url':'stun:stun.l.google.com:19302'
        }]

};


io.on("signaling_message",(data)=>{

    //displayMessage(data.message);
    if(!rtcPeerConn){
       startSignaling();
     }

     if(data.type!="user_here"){
         let message  = JSON.parse(data.message);
         if(message.sdp){
             rtcPeerConn.setRemoteDescription(new RTCSessionDescription(message.sdp),()=>{
                 if(rtcPeerConn.remoteDescription.type=="offer"){
                     rtcPeerConn.createAnswer(sendLocalDesc,logerror)
                 }
             })
         }else{

            rtcPeerConn.addIceCandidate(new RTCIceCandidate(message.candidate));
         }
     }

})

function startSignaling(){
    displayMessage("start signaling...");
  rtcPeerConn = new RTCPeerConnection(configuration);
 //send ice candidate to other peer
        rtcPeerConn.onicecandidate  = function(evt){
            if(evt.candidate){
                io.emit("signal",{"type":"ice candidate","message":JSON.stringify({'candidate':evt.candidate}),room:signal_room})
                displayMessage("completed that ice candidate");
            }
        }
rtcPeerConn.onnegotiationneeded = function(){
    displayMessage("on negotiationnneded");
    rtcPeerConn.createOffer(sendLocalDesc,logerror);

}
rtcPeerConn.onaddstream = (evt,err)=>{
    displayMessage("creating  the other stream");
    if(err){
        displayMessage(err)
    }
    success2(evt.stream);
}

navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;

        navigator.mediaDevices.getUserMedia({video:true,audio:true}).then(stream=>{
             success(stream);
             rtcPeerConn.addStream(stream);
        }).catch(err=>{
             logerror(err);
            });

}


function sendLocalDesc(desc){

rtcPeerConn.setLocalDescription(desc,()=>{
displayMessage("set local description");
io.emit("signal",{type:"SDP",message:JSON.stringify({'sdp':rtcPeerConn.localDescription}),room:signal_room})


},logerror);

}


     function success(stream){
        if ("srcObject" in video) {
        video.srcObject = stream;
        } else {
        // Avoid using this in new browsers, as it is going away.
        video.src = window.URL.createObjectURL(stream);
        }

        video.play();
        }




        function success2(stream){
        if ("srcObject" in video2) {
        video2.srcObject = stream;
        } else {
        // Avoid using this in new browsers, as it is going away.
        video2.src = window.URL.createObjectURL(stream);
        }


        video2.play();
        }

is there anything wrong with my code?

another question , can i use google stun iceservers on production ? are they reliable?

EDIT

the firefox stuck at this script

  rtcPeerConn.onicecandidate  = function(evt){
            if(evt.candidate){
                io.emit("signal",{"type":"ice candidate","message":JSON.stringify({'candidate':evt.candidate}),room:signal_room})
                displayMessage("completed that ice candidate");
            }
        }

it keeps displaying this message (completed that ice candidate) like a loop

Upvotes: 3

Views: 2723

Answers (1)

Jinho Jang
Jinho Jang

Reputation: 175

Have you applied webrtc-adapter?

And google stun server is not proper on production.

Because if your clients are located behind NAT or firewall, P2P connection is impossible.

If P2P Connection failed in WebRTC, you have to ready TURN server for relay.

for example, you can use coTURN

Upvotes: 1

Related Questions