161 lines
4.8 KiB
JavaScript
161 lines
4.8 KiB
JavaScript
var max_print_r_depth = 2
|
|
function print_r(obj, t, d) {
|
|
|
|
// define tab spacing
|
|
var tab = t || '';
|
|
|
|
// define depth
|
|
var depth = d || 1;
|
|
|
|
// check if it's array
|
|
var isArr = Object.prototype.toString.call(obj) === '[object Array]';
|
|
|
|
// use {} for object, [] for array
|
|
var str = isArr ? ('Array\n' + tab + '[ \n') : ('Object\n' + tab + '{ \n');
|
|
|
|
// walk through it's properties
|
|
for (var prop in obj) {
|
|
if (obj.hasOwnProperty(prop)) {
|
|
var val1 = obj[prop];
|
|
var val2 = '';
|
|
var type = Object.prototype.toString.call(val1);
|
|
switch (type) {
|
|
|
|
// recursive if object/array
|
|
case '[object Array]':
|
|
case '[object Object]':
|
|
if(depth <= max_print_r_depth)
|
|
val2 = print_r(val1, (tab + ' '), depth+1);
|
|
else
|
|
val2 = type;
|
|
break;
|
|
|
|
case '[object String]':
|
|
if(val1.length > 40)
|
|
val2 = '\'' + val1.substring(0, 40) + '\'';
|
|
else
|
|
val2 = '\'' + val1 + '\'';
|
|
break;
|
|
|
|
case '[object Function]':
|
|
case '[object Uint8Array]':
|
|
val2 = type;
|
|
break;
|
|
|
|
default:
|
|
val2 = val1;
|
|
}
|
|
str += tab + ' ' + prop + ' => ' + val2 + ',\n';
|
|
}
|
|
}
|
|
|
|
// remove extra comma for last property
|
|
str = str.substring(0, str.length - 2) + '\n' + tab;
|
|
|
|
return isArr ? (str + ']') : (str + '}');
|
|
}
|
|
|
|
|
|
|
|
var localVideo;
|
|
var remoteVideo;
|
|
var peerConnection;
|
|
var peerConnectionConfig = {'iceServers': [{'url': 'stun:stun.services.mozilla.com'}, {'url': 'stun:stun.l.google.com:19302'}]};
|
|
var channel;
|
|
|
|
navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia || navigator.webkitGetUserMedia;
|
|
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
|
|
window.RTCIceCandidate = window.RTCIceCandidate || window.mozRTCIceCandidate || window.webkitRTCIceCandidate;
|
|
window.RTCSessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription || window.webkitRTCSessionDescription;
|
|
|
|
function pageReady(ch) {
|
|
localVideo = document.getElementById('localVideo');
|
|
remoteVideo = document.getElementById('remoteVideo');
|
|
channel = ch;
|
|
|
|
serverConnection = new WebSocket('ws://127.0.0.1:3434');
|
|
serverConnection.onmessage = gotMessageFromServer;
|
|
serverConnection.onopen = onConnection;
|
|
|
|
var constraints = {
|
|
video: true,
|
|
audio: false,
|
|
};
|
|
|
|
if(navigator.getUserMedia) {
|
|
navigator.getUserMedia(constraints, getUserMediaSuccess, errorHandler);
|
|
} else {
|
|
alert('Your browser does not support getUserMedia API');
|
|
}
|
|
}
|
|
|
|
function onConnection(data) {
|
|
console.log('['+(new Date().toLocaleString())+'] ' + '[onConnect]' + data.toString());
|
|
serverConnection.send( JSON.stringify({ channel: channel }) );
|
|
}
|
|
|
|
function getUserMediaSuccess(stream) {
|
|
console.log('['+(new Date().toLocaleString())+'] ' + '[getUserMediaSuccess] ' + stream);
|
|
|
|
localStream = stream;
|
|
localVideo.src = window.URL.createObjectURL(stream);
|
|
}
|
|
|
|
function start(isCaller) {
|
|
console.log('['+(new Date().toLocaleString())+'] ' + "[start] caller? " + isCaller);
|
|
peerConnection = new RTCPeerConnection(peerConnectionConfig);
|
|
peerConnection.onicecandidate = gotIceCandidate;
|
|
peerConnection.onaddstream = gotRemoteStream;
|
|
if(typeof localStream !== 'undefined')
|
|
peerConnection.addStream(localStream);
|
|
|
|
if(isCaller)
|
|
peerConnection.createOffer(gotDescription, errorHandler);
|
|
else
|
|
serverConnection.send( JSON.stringify({ channel: channel }) );
|
|
}
|
|
|
|
function gotMessageFromServer(message) {
|
|
if(!peerConnection)
|
|
start(false);
|
|
|
|
// console.log('message : ' + message.data);
|
|
|
|
var signal = JSON.parse(message.data);
|
|
if(signal.sdp) {
|
|
peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp), function() {
|
|
peerConnection.createAnswer(gotDescription, errorHandler);
|
|
}, errorHandler);
|
|
} else if(signal.ice) {
|
|
console.log(signal.ice.candidate)
|
|
var iceCandidate = new RTCIceCandidate(signal.ice);
|
|
console.log('test2');
|
|
peerConnection.addIceCandidate(iceCandidate);
|
|
console.log('test3')
|
|
}
|
|
}
|
|
|
|
function gotIceCandidate(event) {
|
|
console.log('['+(new Date().toLocaleString())+'] ' + '[gotIceCandidate] ' + event);
|
|
if(event.candidate != null) {
|
|
console.log('['+(new Date().toLocaleString())+'] ' + '[gotIceCandidate] ' + event.candidate);
|
|
serverConnection.send(JSON.stringify({'ice': event.candidate}));
|
|
}
|
|
}
|
|
|
|
function gotDescription(description) {
|
|
console.log('['+(new Date().toLocaleString())+'] ' + '[gotDescription] got description');
|
|
peerConnection.setLocalDescription(description, function () {
|
|
serverConnection.send(JSON.stringify({'sdp': description}));
|
|
}, function() {console.log('['+(new Date().toLocaleString())+'] ' + '[gotDescription] set description error')});
|
|
}
|
|
|
|
function gotRemoteStream(event) {
|
|
console.log('['+(new Date().toLocaleString())+'] ' + '[gotRemoteStream] got remote stream');
|
|
remoteVideo.src = window.URL.createObjectURL(event.stream);
|
|
}
|
|
|
|
function errorHandler(error) {
|
|
console.log('['+(new Date().toLocaleString())+'] ' + "[errorHandler] " + error);
|
|
}
|