7:ecb710cf56fb default tip
Anton Shestakov <engored@ya.ru>, Wed, 20 Nov 2013 19:41:52 +0900
Cosmetic changes in the template.

previous change 5:de01e29e06b5

static/js/main.js

Permissions: -rw-r--r--

Other formats: Feeds:
var ws = new WebSocket('ws://' + location.host + '/ws');
var initiator;
var pc;
function call() {
$('#btn-call').addClass('btn-active');
initiator = true;
init();
}
function receive() {
$('#btn-receive').addClass('btn-active');
initiator = false;
init();
}
function init() {
var constraints = {
audio: $('#audio').prop('checked'),
video: $('#video').prop('checked')
};
if (constraints.audio || constraints.video) {
getUserMedia(constraints, connect, fail);
} else {
connect();
}
}
function connect(stream) {
pc = new RTCPeerConnection(null);
if (stream) {
pc.addStream(stream);
$('#local').attachStream(stream);
}
pc.onaddstream = function(event) {
$('#remote').attachStream(event.stream);
logStreaming(true);
};
pc.onicecandidate = function(event) {
if (event.candidate) {
ws.send(JSON.stringify(event.candidate));
}
};
ws.onmessage = function (event) {
var signal = JSON.parse(event.data);
if (signal.sdp) {
if (initiator) {
receiveAnswer(signal);
} else {
receiveOffer(signal);
}
} else if (signal.candidate) {
pc.addIceCandidate(new RTCIceCandidate(signal));
}
};
if (initiator) {
createOffer();
} else {
log('waiting for offer...');
}
logStreaming(false);
}
function createOffer() {
log('creating offer...');
pc.createOffer(function(offer) {
log('created offer...');
pc.setLocalDescription(offer, function() {
log('sending to remote...');
ws.send(JSON.stringify(offer));
}, fail);
}, fail);
}
function receiveOffer(offer) {
log('received offer...');
pc.setRemoteDescription(new RTCSessionDescription(offer), function() {
log('creating answer...');
pc.createAnswer(function(answer) {
log('created answer...');
pc.setLocalDescription(answer, function() {
log('sent answer');
ws.send(JSON.stringify(answer));
}, fail);
}, fail);
}, fail);
}
function receiveAnswer(answer) {
log('received answer');
pc.setRemoteDescription(new RTCSessionDescription(answer));
}
function log() {
$('#status').text(Array.prototype.join.call(arguments, ' '));
console.log.apply(console, arguments);
}
function logStreaming(streaming) {
$('#streaming').text(streaming ? '[streaming]' : '[..]');
}
function fail() {
$('#status').text(Array.prototype.join.call(arguments, ' '));
$('#status').addClass('error');
console.error.apply(console, arguments);
}
jQuery.fn.attachStream = function(stream) {
this.each(function() {
this.src = URL.createObjectURL(stream);
this.play();
});
};