-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f59f99d
commit 645ccc5
Showing
1 changed file
with
181 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,191 +1,250 @@ | ||
$(document).ready(function(){ | ||
var App = (function(){ | ||
|
||
$('#textModal').modal('show'); | ||
$("#callBtn").on("click",createOffer); | ||
$("#joinBtn").on("click",joinSession); | ||
$("#hangBtn").on("click",closeCall); | ||
|
||
}); | ||
var servers = {"iceServers" :[{ | ||
url: 'stun:stun.l.google.com:19302' | ||
}]}, | ||
|
||
signalData = {"desc":null,"ice":[]}, | ||
|
||
peerConnection, | ||
|
||
var servers = {"iceServers" :[{ | ||
url: 'stun:stun.l.google.com:19302' | ||
}]}; | ||
localVideo = document.getElementById("localVideo"), | ||
|
||
var signalData = { "desc":null,"ice":[]}; | ||
remoteVideo = document.getElementById("remoteVideo"), | ||
|
||
var peerConnection = new webkitRTCPeerConnection(servers); | ||
txtBox = $("#desc"), | ||
|
||
var localVideo = document.getElementById("localVideo"); | ||
var remoteVideo = document.getElementById("remoteVideo"); | ||
getUserMedia = navigator.webkitGetUserMedia.bind(navigator); | ||
|
||
var localStream,remoteStream; | ||
|
||
getUserMedia = navigator.webkitGetUserMedia.bind(navigator); | ||
var | ||
|
||
console.log(peerConnection); | ||
getPeerConnection = function(){ | ||
|
||
peerConnection.onaddstream = gotRemoteStream; | ||
peerConnection.onicecandidate = gotIceCandidate; | ||
peerConnection = new webkitRTCPeerConnection(servers); | ||
}, | ||
|
||
peerConnection.oniceconnectionstatechange = function (event) { | ||
console.log("state changed"); | ||
switch (peerConnection.iceConnectionState) { | ||
case 'checking': | ||
console.log('Connecting to peer...'); | ||
break; | ||
case 'connected': | ||
case 'completed': // on caller side | ||
console.log('Connection established.'); | ||
$('#textModal').modal('hide'); | ||
$("#localVideo").show(); | ||
$("#hangupdiv").show(); | ||
break; | ||
case 'disconnected': | ||
console.log('Disconnected.'); | ||
closeCall(); | ||
break; | ||
case 'failed': | ||
console.log('Failed.'); | ||
break; | ||
case 'closed': | ||
console.log('Connection closed.'); | ||
break; | ||
} | ||
} | ||
getMediaStream = function(callback){ | ||
|
||
getUserMedia( | ||
{ audio:true, | ||
video:true | ||
},function(stream){ | ||
callback(stream); | ||
},function(error){ | ||
|
||
function createOffer(){ | ||
console.log("getUserMedia error: ", error); | ||
|
||
getUserMedia({audio:true, video:true}, function(stream){ | ||
}); | ||
}, | ||
|
||
localVideo.src = URL.createObjectURL(stream); | ||
localStream = stream; | ||
peerConnection.addStream(localStream); | ||
peerConnection.createOffer(onConnection,handleError); | ||
$("#desc").popover('show'); | ||
}, | ||
function(error) { | ||
console.log("getUserMedia error: ", error); | ||
}); | ||
|
||
} | ||
joinSession = function(){ | ||
|
||
function gotRemoteStream(event){ | ||
var sigdata = JSON.parse(txtBox.val().trim()); | ||
|
||
console.log("Received remote stream"); | ||
remoteVideo.src = URL.createObjectURL(event.stream); | ||
if(sigdata["desc"] == ""){ | ||
|
||
} | ||
alert("Please enter the offer"); | ||
return; | ||
} | ||
|
||
function joinSession(){ | ||
getMediaStream(createAnswer); | ||
|
||
console.log("Inside Join Session"); | ||
var sigdata = document.getElementById("desc").value.trim(); | ||
sigdata = JSON.parse(sigdata) | ||
if(sigdata["desc"] == ""){ | ||
|
||
alert("Please enter the offer"); | ||
return; | ||
} | ||
|
||
|
||
}, | ||
|
||
getUserMedia({audio:true, video:true}, function(stream){ | ||
createOffer = function(stream){ | ||
|
||
localVideo.src = URL.createObjectURL(stream); | ||
localStream = stream; | ||
peerConnection.addStream(stream); | ||
peerConnection.createOffer(onConnection,handleError); | ||
txtBox.popover('show'); | ||
|
||
}, | ||
|
||
initiateOffer = function(){ | ||
|
||
getMediaStream(createOffer); | ||
|
||
}, | ||
|
||
onConnection = function(desc){ | ||
|
||
peerConnection.addStream(localStream); | ||
console.log("Description is "+desc.sdp); | ||
peerConnection.setLocalDescription(desc); | ||
signalData["desc"] = desc; | ||
|
||
//Change the event on click of Join Button to Complete Handshake on Initiator Side | ||
$("#joinBtn").off("click").on("click",completeHandshake); | ||
|
||
}, | ||
|
||
createAnswer = function(stream){ | ||
|
||
var sigdata = JSON.parse(txtBox.val().trim()); | ||
|
||
localVideo.src = URL.createObjectURL(stream); | ||
|
||
peerConnection.addStream(stream); | ||
|
||
peerConnection.setRemoteDescription(new RTCSessionDescription(sigdata["desc"]),function(){console.log("Success");},handleError); | ||
|
||
peerConnection.createAnswer(sendReply,handleError); | ||
|
||
addIceCandidates(sigdata["ice"]); | ||
|
||
$("#desc").popover("show"); | ||
txtBox.popover("show"); | ||
|
||
}, | ||
function(error) { | ||
console.log("getUserMedia error: ", error); | ||
}); | ||
|
||
|
||
} | ||
completeHandshake = function(){ | ||
|
||
console.log("Inside complete handshake"); | ||
var sigdata = document.getElementById("desc").value.trim(); | ||
sigdata = JSON.parse(sigdata) | ||
if(sigdata["desc"] == ""){ | ||
|
||
function completeHandshake(){ | ||
alert("Please enter the answer"); | ||
return; | ||
} | ||
|
||
console.log("Inside complete handshake"); | ||
var sigdata = document.getElementById("desc").value.trim(); | ||
sigdata = JSON.parse(sigdata) | ||
if(sigdata["desc"] == ""){ | ||
peerConnection.setRemoteDescription(new RTCSessionDescription(sigdata["desc"]),function(){console.log("Success");},handleError); | ||
addIceCandidates(sigdata["ice"]); | ||
|
||
alert("Please enter the answer"); | ||
return; | ||
} | ||
}, | ||
|
||
peerConnection.setRemoteDescription(new RTCSessionDescription(sigdata["desc"]),function(){console.log("Success");},handleError); | ||
addIceCandidates(sigdata["ice"]); | ||
sendReply = function(desc){ | ||
|
||
} | ||
peerConnection.setLocalDescription(desc); | ||
signalData["desc"] = desc; | ||
console.log(JSON.stringify(desc)); | ||
|
||
function sendReply(desc){ | ||
}, | ||
|
||
peerConnection.setLocalDescription(desc); | ||
signalData["desc"] = desc; | ||
console.log(JSON.stringify(desc)); | ||
|
||
} | ||
gotIceCandidate = function(event){ | ||
if(event.candidate){ | ||
signalData["ice"].push(event.candidate); | ||
document.getElementById("desc").value=JSON.stringify(signalData); | ||
} | ||
|
||
|
||
}, | ||
|
||
function onConnection(desc){ | ||
addIceCandidates = function(canArr){ | ||
|
||
console.log("Description is "+desc.sdp); | ||
peerConnection.setLocalDescription(desc); | ||
signalData["desc"] = desc; | ||
$("#joinBtn").off("click").on("click",completeHandshake); | ||
|
||
} | ||
for(var i in canArr){ | ||
|
||
peerConnection.addIceCandidate(new RTCIceCandidate(canArr[i])); | ||
|
||
function gotIceCandidate(event){ | ||
if(event.candidate){ | ||
signalData["ice"].push(event.candidate); | ||
document.getElementById("desc").value=JSON.stringify(signalData); | ||
} | ||
} | ||
|
||
}, | ||
|
||
} | ||
handleError = function(err){ | ||
|
||
console.log("Error occured "+err); | ||
}, | ||
|
||
|
||
closeCall = function(){ | ||
|
||
peerConnection.close(); | ||
peerConnection=null; | ||
|
||
txtBox.popover('hide'); | ||
txtBox.val(''); | ||
$(localVideo).hide(); | ||
showModal(); | ||
}, | ||
|
||
showModal = function(){ | ||
|
||
$("#textModal").modal('show'); | ||
}, | ||
|
||
hideModal = function(){ | ||
|
||
function addIceCandidates(canArr){ | ||
$("#textModal").modal('hide'); | ||
|
||
}, | ||
|
||
|
||
onConnectionStatusChange = function (event) { | ||
|
||
switch (peerConnection.iceConnectionState) { | ||
case 'checking': | ||
console.log('Connecting to peer...'); | ||
break; | ||
case 'connected': | ||
case 'completed': // on caller side | ||
console.log('Connection established.'); | ||
$('#textModal').modal('hide'); | ||
$("#localVideo").show(); | ||
$("#hangupdiv").show(); | ||
break; | ||
case 'disconnected': | ||
console.log('Disconnected.'); | ||
closeCall(); | ||
break; | ||
case 'failed': | ||
console.log('Failed.'); | ||
break; | ||
case 'closed': | ||
console.log('Connection closed.'); | ||
break; | ||
} | ||
}, | ||
|
||
gotRemoteStream = function(event){ | ||
|
||
console.log("Received remote stream"); | ||
remoteVideo.src = URL.createObjectURL(event.stream); | ||
|
||
}, | ||
|
||
for(var i in canArr){ | ||
attachEvents = function(){ | ||
|
||
peerConnection.addIceCandidate(new RTCIceCandidate(canArr[i])); | ||
|
||
peerConnection.onaddstream = gotRemoteStream; | ||
peerConnection.onicecandidate = gotIceCandidate; | ||
peerConnection.oniceconnectionstatechange = onConnectionStatusChange; | ||
$("#callBtn").on("click",initiateOffer); | ||
$("#joinBtn").on("click",joinSession); | ||
$("#hangBtn").on("click",closeCall); | ||
|
||
|
||
}, | ||
|
||
init = function(){ | ||
|
||
|
||
getPeerConnection(); | ||
|
||
attachEvents(); | ||
|
||
showModal(); | ||
|
||
} | ||
|
||
} | ||
|
||
function handleError(err){ | ||
|
||
console.log("Error occured "+err); | ||
} | ||
return { | ||
init:init | ||
}; | ||
|
||
})(); | ||
|
||
|
||
|
||
$(document).ready(function(){ | ||
|
||
function closeCall(){ | ||
|
||
peerConnection.close(); | ||
peerConnection=null; | ||
App.init(); | ||
|
||
$("#desc").popover('hide'); | ||
$("#desc").val(''); | ||
$("#localVideo").hide(); | ||
$('#textModal').modal('show'); | ||
} | ||
}); | ||
|
||
|