javascript - WebRTC works locally, but not across different IP addresses -
my code on github at: https://github.com/rashadrussell/webrtc_experiment/blob/master/public/script.js
i trying write 1-to-1 video video conferencing script webrtc. , being stored on appfog, cloud hosting website. works on localhost when test 2 different chrome windows on single computer. works on appfog when test on 2 different computers @ home.
the problem occurs when test app friend living @ different house. remote streams not being set. guess there error ip addresses, means wrong setup of ice candidates. pops black box remote stream supposed be.
here of code:
**client-side** var isinitiator = false; socket.on('initiatorfound', function(data) { isinitiator = data.setinitiator; console.log("is initiator? " + isinitiator); }); navigator.getmedia = ( navigator.getusermedia || navigator.webkitgetusermedia || navigator.mozgetusermedia || navigator.msgetusermedia ); navigator.getmedia( {video: true, audio: false}, function(stream) { var video = document.getelementbyid("localview"); video.src = window.url.createobjecturl(stream); console.log("add stream"); sendmessage('streamadd', {streamadded: 'stream-added'}); createpeerconnection(); pc.addstream(stream); if(isinitiator) { callpeer(); } }, function(err) { console.log("the following error occured: "); console.dir(err); } ); function sendmessage(type, message) { console.log("sending message"); socket.emit('message',{ "type": type, "message": message }); } function createpeerconnection() { pc = new rtcpeerconnection(servers, options); console.dir(pc); pc.onicecandidate = function(evt) { if(evt.candidate == null) return; pc.onicecandidate = null; console.log("send ice candidate"); sendmessage("icecandidate", json.stringify(evt.candidate)); }; pc.onaddstream = function(evt) { document.body.append("<video id='remotevideo' autoplay></video>"); var remotevid = document.getelementbyid("remotevideo"); remotevid.src = window.url.createobjecturl(evt.stream); }; } function callpeer() { pc.createoffer(function (offer) { pc.setlocaldescription(offer, function() { sendmessage("offer", json.stringify(offer)); }); console.log("send offer"); }, function(err) { console.log("offer error: " + err) }, videoconstraints ); } function answerpeer() { pc.createanswer(function(answer) { pc.setlocaldescription(answer); sendmessage("answer", json.stringify(answer)) }, function(err) { console.log("sending answer error: " + err) }, videoconstraints ); } socket.on('message', function(message) { console.log("console message:"); console.dir(message); if(message.type == 'streamadd') { console.log('stream added'); createpeerconnection(); if(isinitiator) { callpeer(); } } else if(message.type == 'offer') { pc.setremotedescription( new rtcsessiondescription(json.parse(message.message))); if(!isinitiator) { console.log("sending answer"); answerpeer(); } } else if(message.type == 'answer') { pc.setremotedescription( new rtcsessiondescription(json.parse(message.message))); } else if(message.type == 'icecandidate') { console.log("get ice candidate"); pc.addicecandidate(new rtcicecandidate(json.parse(message.message)) ); } });
server-side
var isinitiator = false; io.sockets.on('connection', function(socket) { if (!isinitiator) { isinitiator = true; socket.emit('initiatorfound', {setinitiator: isinitiator}); } else { socket.emit('initiatorfound', {setinitiator: !isinitiator}); } // signaling channel socket.on('message', function(message) { if (message.type == 'streamadd') { console.log('got message: ' + message); } //socket.emit('message' ,message); // should be: socket.broadcast.emit('message', message); }); });
in line
pc = new rtcpeerconnection(servers, options);
while sending server info through server variable, need mention addr of turn servers along withthe stun server. typically data gets blocked due nat or firewall , turn should in case.
for more server details, can check out https://gist.github.com/yetithefoot/7592580
Comments
Post a Comment