5:de01e29e06b5
Anton Shestakov <engored@ya.ru>, Thu, 14 Mar 2013 23:36:24 +0900
Highlight error messages. Usually they are fatal and thus the last thing appearing on the status bar, so no removeClass (or other methods) needed.

previous change 4:61a86e766285

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();
});
};