Browse Source

- Add Turn support in WebRTC

pull/16/head
Jaussoin Timothée 12 years ago
parent
commit
2fc65e457f
  1. 1
      app/widgets/Visio/Visio.php
  2. 2
      app/widgets/Visio/adapter.js
  3. 53
      app/widgets/Visio/turn.js
  4. 2
      app/widgets/Visio/visio.js
  5. 28
      app/widgets/Visio/webrtc.js

1
app/widgets/Visio/Visio.php

@ -23,6 +23,7 @@ class Visio extends WidgetBase
$this->addjs('visio.js');
$this->addjs('adapter.js');
$this->addjs('webrtc.js');
$this->addjs('turn.js');
if(isset($_GET['f'])) {
list($jid, $ressource) = explode('/', htmlentities($_GET['f']));

2
app/widgets/Visio/adapter.js

@ -33,6 +33,7 @@ if (navigator.mozGetUserMedia) {
// Get UserMedia (only difference is the prefix).
// Code from Adam Barth.
getUserMedia = navigator.mozGetUserMedia.bind(navigator);
navigator.getUserMedia = getUserMedia;
// Creates iceServer from the url for FF.
createIceServer = function(url, username, password) {
@ -118,6 +119,7 @@ if (navigator.mozGetUserMedia) {
// Get UserMedia (only difference is the prefix).
// Code from Adam Barth.
getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
navigator.getUserMedia = getUserMedia;
// Attach a media stream to an element.
attachMediaStream = function(element, stream) {

53
app/widgets/Visio/turn.js

@ -0,0 +1,53 @@
var turnUrl = 'https://computeengineondemand.appspot.com/turn?username=93773443&key=4080218913';
var turnDone = false;
var caller = false;
function maybeRequestTurn(isCaller) {
caller = isCaller;
if (turnUrl == '') {
turnDone = true;
}
for (var i = 0, len = configuration.iceServers.length; i < len; i++) {
if (configuration.iceServers[i].url.substr(0, 5) === 'turn:') {
turnDone = true;
}
}
var currentDomain = document.domain;
if (currentDomain.search('localhost') === -1 &&
currentDomain.search('apprtc') === -1) {
// Not authorized domain. Try with default STUN instead.
turnDone = true;
}
// No TURN server. Get one from computeengineondemand.appspot.com.
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = onTurnResult;
xmlhttp.open('GET', turnUrl, true);
xmlhttp.send();
}
function onTurnResult() {
if (xmlhttp.status === 200) {
var turnServer = JSON.parse(xmlhttp.responseText);
for (i = 0; i < turnServer.uris.length; i++) {
// Create a turnUri using the polyfill (adapter.js).
var iceServer = createIceServer(turnServer.uris[i],
turnServer.username,
turnServer.password);
if (iceServer !== null) {
configuration.iceServers.push(iceServer);
}
}
} else {
messageError('No TURN server; unlikely that media will traverse networks. '
+ 'If this persists please report it to '
+ 'discuss-webrtc@googlegroups.com.');
}
// If TURN request failed, continue the call with default STUN.
turnDone = true;
init(caller);
}

2
app/widgets/Visio/visio.js

@ -138,7 +138,7 @@ var Visio = {
movim_add_onload(function()
{
document.getElementById("call").onclick = function() { init(true); };
document.getElementById("call").onclick = function() { preInit(true); };
document.getElementById("hang-up").onclick = function() { sendTerminate('success'); terminate(); };
document.getElementById("toggle-screen").onclick = function() { Visio.fullScreen(); };
document.getElementById("toggle-camera").onclick = function() { Visio.toggleVideoMute(); };

28
app/widgets/Visio/webrtc.js

@ -10,6 +10,9 @@ var pc;
var remoteStream;
var localStream;
// The RTCPeerConnection configuration
var configuration = {"iceServers":[{"url": "stun:stun.services.mozilla.com"}]};
// Set up audio and video regardless of what devices are present.
var sdpConstraints = {'mandatory': {
'OfferToReceiveAudio': true,
@ -80,8 +83,11 @@ function onAnswerCreated(offer) {
function onIceCandidate(event) {
Visio.log('onIceCandidate');
console.log('CANDIDATE');
console.log(event);
candidate = {};
/*
if(event.candidate != null) {
candidate.sdp = event.candidate.candidate;
candidate.mid = event.candidate.sdpMid;
@ -92,9 +98,9 @@ function onIceCandidate(event) {
var msgString = JSON.stringify(candidate);
//Visio.call(['VisioExt_ajaxSendCandidate', msgString]);
Visio.call(['VisioExt_ajaxSendCandidate', msgString]);
}
*/
}
function sendTerminate(reason) {
@ -171,7 +177,7 @@ function onOffer(offer) {
Visio.log('OFFER ' + offer);
if(!pc)
init(false);
preInit(false);
if(offer != null) {
@ -214,12 +220,20 @@ function onCandidate(message) {
pc.addIceCandidate(candidate, onRemoteIceCandidateAdded, onRemoteIceCandidateError);
}
function init(isCaller) {
var configuration = {"iceServers":[{"url": "stun:23.21.150.121:3478"}]};
function preInit(isCaller) {
// We try to grab TURN servers, init() is called here
maybeRequestTurn(isCaller);
}
function init(isCaller) {
try {
console.log(configuration);
pc = new RTCPeerConnection(configuration, optional);
console.log('Created RTCPeerConnnection with:\n' +
' config: \'' + JSON.stringify(configuration) + '\';\n' +
' constraints: \'' + JSON.stringify(optional) + '\'.');
pc.onicecandidate = onIceCandidate;
pc.onsignalingstatechange = onSignalingStateChanged;
pc.oniceconnectionstatechange = onIceConnectionStateChanged;
@ -231,6 +245,8 @@ function init(isCaller) {
return;
}
console.log(pc);
if(getUserMedia) {
if (getUserMedia) {
getUserMedia = getUserMedia.bind(navigator);

Loading…
Cancel
Save