2:a26b16f37c1b
Anton Shestakov <engored@ya.ru>, Mon, 11 Mar 2013 23:44:44 +0900
User can choose to use audio/video or both. Also visually mark pressed button.

next change 3:d69239d4ae6d
previous change 0:be29d4a1df6d

static/js/main.js

Permissions: -rw-r--r--

Other formats: Feeds:
var ws = new WebSocket('ws://' + location.host + '/ws');
var pc;
function call() {
document.getElementById('btn-call').className += ' btn-active';
init(true);
}
function receive() {
document.getElementById('btn-receive').className += ' btn-active';
init(false);
}
function init(initiator) {
var constraints = {
audio: document.getElementById('audio').checked,
video: document.getElementById('video').checked
};
getUserMedia(constraints, function(stream) {
pc = new RTCPeerConnection(null);
pc.addStream(stream);
attachMediaStream(document.getElementById('local'), stream);
pc.onaddstream = function(event) {
attachMediaStream(document.getElementById('remote'), event.stream);
};
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();
}
}, fail);
}
function createOffer() {
console.log('creating offer');
pc.createOffer(function(offer) {
console.log('created offer');
pc.setLocalDescription(offer, function() {
console.log('setLocalDescription done, sending to remote');
ws.send(JSON.stringify(offer));
}, fail);
}, fail);
}
function receiveOffer(offer) {
console.log('received offer');
pc.setRemoteDescription(new RTCSessionDescription(offer), function() {
console.log('creating answer');
pc.createAnswer(function(answer) {
console.log('created answer');
pc.setLocalDescription(answer, function() {
console.log('setLocalDescription done, sending to remote');
ws.send(JSON.stringify(answer));
}, fail);
}, fail);
}, fail);
}
function receiveAnswer(answer) {
console.log('received answer');
pc.setRemoteDescription(new RTCSessionDescription(answer));
}
function fail(error) {
console.log(error);
}