node.js - WebRTC: One to one Audio call not working in different machine -
i trying implement 1 one audio call using webrtc (signalling using websockets) . works when try in 1 system using multiple tabs of chrome (localhost). when try hit server machine initial handshakes , call doesn't happen.
but when try change tag , changed constraints video constraints . works if try access other machine (i.e video call works ).
i thought because if firewall when video call worked puzzled .
here code:
// constraints audio stream $scope.constraints = { audio: { mandatory: { googechocancellation: true }, optional: [] }, video:false }; navigator.getusermedia = navigator.getusermedia || navigator.webkitgetusermedia || navigator.mozgetusermedia; // success callback of getusermedia(), stream variable audio stream. $scope.successcallback = function (stream) { if (window.url) { myvideo.src = window.url.createobjecturl(stream); // converting media stream blob url. } else { myvideo.src = stream; } //attachmediastream(audiotag, stream); localstream = stream; if (initiator) maybestart(); else doanswer(); }; // failure callback of getusermedia() $scope.failurecallback = function (error) { console.log('navigator.getusermedia failed: ', error); }; var initiator, started = false; $("#call").click(function () { socket.emit("message", undefined); initiator = true; navigator.getusermedia($scope.constraints, $scope.successcallback, $scope.failurecallback); }); var channelready = false; socket.on('message', function (data) { channelready = true; if (data) { if (data.type === 'offer') { if (!initiator) { $("#acceptcall").show(); $("#acceptcall").click(function(){ if (!initiator && !started) { var pc_config = { iceservers: [ { url: "stun:stun.l.google.com:19302" }, { url: "turn:numb.viagenie.ca", credential: "drfunk", username: "toadums@hotmail.com"} ] }; pc = new webkitrtcpeerconnection(pc_config); pc.onicecandidate = onicecandidate; pc.onaddstream = onremotestreamadded; } pc.setremotedescription(new rtcsessiondescription(data)); $scope.acceptcall(); }); } } else if (data.type === 'answer' && started) { pc.onaddstream = onremotestreamadded; pc.setremotedescription(new rtcsessiondescription(data)); } else if (data.type === 'candidate' && started) { var candidate = new rtcicecandidate({ sdpmlineindex: data.label, candidate: data.candidate }); pc.addicecandidate(candidate); } else if (data.type === 'bye' && started) { console.log("bye"); } } }); function onremotestreamadded(event) { othersvideo.src = url.createobjecturl(event.stream); }; var sdpconstraints = { 'mandatory': { 'offertoreceiveaudio': true, 'offertoreceivevideo': false } }; function doanswer() { pc.addstream(localstream); pc.createanswer(gotdescription,null,sdpconstraints); } function gotdescription(desc) { pc.setlocaldescription(desc); socket.send(desc); } function maybestart() { if (!started && localstream && channelready) createpeerconnection(); pc.addstream(localstream); started = true; if (initiator) docall(); } $scope.acceptcall = function () { navigator.getusermedia($scope.constraints, $scope.successcallback, $scope.failurecallback); } function createpeerconnection() { var pc_config = { iceservers: [ { url: "stun:stun.l.google.com:19302" }, { url: "turn:numb.viagenie.ca", credential: "drfunk", username: "toadums@hotmail.com"} ] }; pc = new webkitrtcpeerconnection(pc_config); pc.onicecandidate = onicecandidate; console.log("created rtcpeerconnnection config:\n" + " \"" + json.stringify(pc_config) + "\"."); }; function docall() { $scope.caller = true; pc.createoffer(setlocalandsendmessage,null,sdpconstraints); }; function setlocalandsendmessage(sessiondescription) { pc.setlocaldescription(sessiondescription); socket.send(sessiondescription); } function onicecandidate(event) { if (event.candidate) { socket.emit('message', { type: 'candidate', label: event.candidate.sdpmlineindex, id: event.candidate.sdpmid, candidate: event.candidate.candidate }); } else { console.log("end of candidates."); } }
Comments
Post a Comment