3

I am trying to run a sample webRTC program in my PC with Mozilla Firefox browser . I have a HTML file with JavaScript . I am trying to show client and peer video in a page . This is my HTML file :

<body>
<div id="container">
    <video id="client"  autoplay></video>
    <video id="peer" autoplay/></video>
</div>
    <script src="script.js"></script>
</body>

and the java script file :

function hasUserMedia() {
    navigator.getUserMedia = navigator.getUserMedia ||
                         navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
    return !!navigator.getUserMedia;
};

function hasRTCPeerConnection(){
    window.RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
    window.RTCSessionDescription = window.RTCSessionDescription || window.webkitRTCSessionDescription || window.mozRTCSessionDescription;
    window.RTCIceCandidate = window.RTCIceCandidate || window.webkitRTCIceCandidate || window.mozRTCIceCandidate;

    return !!window.RTCPeerConnection;
};


function startPeerConnection(stream) {
    var configuration = {
        //"iceServers": [{ "url": "stun:stun.1.google.com:19302" }]
        iceServers: [{ url: 'stun:stun.1.google.com:19302' }]
    };
    clientConnection = new RTCPeerConnection(configuration);
    peeerConnection = new RTCPeerConnection(configuration);

    // Setup stream listening
    clientConnection.addStream(stream);
    peeerConnection.onaddstream = function (e) {
        peervideo.src = window.URL.createObjectURL(e.stream);
    };

    // Setup ice handling
    clientConnection.onicecandidate = function (event) {
        if (event.candidate){
            peeerConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
        }
    };

    peeerConnection.onicecandidate = function (event) {
        if (event.candidate) {
            clientConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
        }
    };

    // Begin the offer
    clientConnection.createOffer(function (offer) {
        clientConnection.setLocalDescription(offer);
        peeerConnection.setRemoteDescription(offer);

        peeerConnection.createAnswer(function (offer) {
            peeerConnection.setLocalDescription(offer);
            clientConnection.setRemoteDescription(offer);
        });
    });
}


var clientvideo = document.querySelector("#client"),
    peervideo = document.querySelector("#peer"),
    clientConnection, peeerConnection,stream;

if (hasUserMedia()) {
    navigator.getUserMedia({ video: true, audio: false }, function (mystream) {
            stream = mystream ;
            clientvideo.src = window.URL.createObjectURL(stream);
            if (hasRTCPeerConnection()) {
                startPeerConnection(stream);
            } else {
                alert("Sorry, your browser does not support WebRTC.");
            }
        }, function (error) {
            console.log(error);
        }
    );
} else {
    alert("Sorry, your browser does not support WebRTC.");
} 

I don't know what i am missing . But client video working fine that is getUsermedia() . But i can't see any remote video . What i am missing here ? any suggestion ?

user2986042
  • 1,098
  • 2
  • 16
  • 37
  • Possible duplicate of [WebRTC remoteVideo stream not working](http://stackoverflow.com/questions/38279635/webrtc-remotevideo-stream-not-working) – jib Apr 26 '17 at 14:09
  • Are you also following Dan Ristic's book by any chance? – jib Apr 26 '17 at 14:16

0 Answers0