javascriptwebrtcvideo-streamingsimplewebrtc

WebRTC failed to play in chrome & edge but playing in Firefiox


I have a very simple code for video calling using WebRTC. The entire system working different for different browsers.

Capture Browser Player Browser Working
Chrome Firefox
Chrome Chrome X
Firefox Chrome X
Firefox Firefox

The capture code is JS:

(function () {
    var localVideo, localConnection;
    const signaling = new WebSocket('wss://crs4kx11s1/websockets');
    signaling.onmessage = function (message) {
        var data = JSON.parse(message.data);
        if (data.sdp) {
            var answerSDP = data.sdp;
            if (answerSDP.type == "answer") {
                localConnection.setRemoteDescription(answerSDP);
            }
        }
        if (data.candidate && data.candidateType == "answerClient") {
            localConnection.addIceCandidate(data.candidate);
        }
    }
    localConnection = new RTCPeerConnection({
        iceServers: [{
            urls: 'turn:127.0.0.1:8043?transport=tcp',
            credential: 'jupiter',
            username: 'simpleshare'
        }]
    });
    document.addEventListener("DOMContentLoaded", function (event) {
        $("#share").click(function (event) {
            navigator.mediaDevices.getUserMedia({ video: true })
                .then(function (stream) {
                    stream.getTracks().forEach(
                        function (track) {
                            localConnection.addTrack(
                                track,
                                stream
                            );
                        }
                    );
                    localVideo = document.getElementById('local');
                    localVideo.srcObject = stream;
                    localConnection.onnegotiationneeded = function () {
                        localConnection.createOffer()
                            .then(offer => {
                                localConnection.setLocalDescription(offer)
                                    .then(() => {
                                        signaling.send(JSON.stringify({ sdp: offer }));
                                    })
                            });
                    }
                    localConnection.onicecandidate = function (e) {
                        if (e.candidate) {
                            signaling.send(JSON.stringify({
                                candidateType: 'offerClient',
                                candidate: e.candidate.toJSON()
                            }));
                        }
                        console.log('offerClient is on icecandidate');
                    };
                });
        });
    }); 
})();

HTML

<div>
    <button id="share">Share</button>
    <video id="local" autoplay></video>
 </div>

Now the player code

JS

(function () {
    var localVideo, localConnection;
    const signaling = new WebSocket('wss://crs4kx11s1/websockets');
    signaling.onmessage = function (message) {
        const data = JSON.parse(message.data);
        // const content = data.content;
        try {
            if (data.sdp) {
                let offerSDP = data.sdp;
                if (offerSDP.type == "offer") {
                    console.log("Accepting the offer.")
                    localConnection.setRemoteDescription(offerSDP);
                    localConnection.createAnswer().then(function (answer) {
                        console.log("Answer created!")
                        localConnection.setLocalDescription(answer);
                        signaling.send(JSON.stringify({ sdp: answer }));
                    });
                    
                }
            }
            if (data.candidate && data.candidateType == "offerClient") {
                console.log("ICE candidate added!");
                localConnection.addIceCandidate(data.candidate);
            }

        } catch (err) {
            console.error(err);
        }
    };
    document.addEventListener("DOMContentLoaded", function (event) {

            startConnection();
            localVideo = document.getElementById('self-view');

    });

    function startConnection() {
        console.info("Starting connection");
        localConnection = new RTCPeerConnection({iceServers: [{
                urls: 'turn:127.0.0.1:8043?transport=tcp',
                credential: 'jupiter',
                username: 'simpleshare'
            }]
        });
        //startCapture();
        localConnection.onicecandidate = function (e) {
            console.info("onicecandidate", e);
            if (e.candidate) {
                    signaling.send(JSON.stringify({
                        candidateType: 'answerClient',
                        candidate: e.candidate.toJSON()
                    }));
                }
            console.log('answerClient is on icecandidate');
        };
        

        localConnection.onconnectionstatechange = function (e) {
            console.log("Current state", localConnection.connectionState);
        }
        localConnection.ontrack = function (e) {
            localVideo.srcObject = e.streams[0];
        }

    }

})();

HTML

<div id="chat-room">
   <div id="videos">
      <video id="self-view" autoplay></video>
    </div>
</div>

Apart from these there is a WebSocket server which relays the SDP offers and candidates. Please note that I have used our own TURN server for.


Solution

  • Got it worked. It was because of new autoplay policy in chrome. Just added localVideo.play(); and it worked.