0:be29d4a1df6d
Anton Shestakov <engored@ya.ru>, Sat, 09 Mar 2013 13:51:40 +0900
Sort of works. This is as fragile as it sounds actually, because it works only in perfect cases. Ice candidates from Chrome produce errors in Firefox; also Firefox cannot create answer to Chrome's offer; sound is missing in Firefox(?).

next change 2:a26b16f37c1b

static/js/main.js

Permissions: -rw-r--r--

Other formats: Feeds:
var ws = new WebSocket('ws://' + location.host + '/ws');
var pc;
function call() {
init({audio: true, video: true}, true);
}
function receive() {
init({audio: true, video: true}, false);
}
function init(constraints, initiator) {
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);
}