1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-18 05:42:00 +03:00

Use TURN servers when placing/receiving calls.

This commit is contained in:
Kegan Dougal
2015-07-17 13:36:58 +01:00
parent 1232f6ce3c
commit 8727dd1e34
2 changed files with 28 additions and 9 deletions

View File

@@ -1096,6 +1096,14 @@ MatrixClient.prototype.turnServer = function(callback) {
return this._http.authedRequest(callback, "GET", "/voip/turnServer"); return this._http.authedRequest(callback, "GET", "/voip/turnServer");
}; };
/**
* Get the TURN servers for this home server.
* @return {Array<Object>} The servers or an empty list.
*/
MatrixClient.prototype.getTurnServers = function() {
return this._turnServers || [];
};
/** /**
* @return {boolean} true if there is a valid access_token for this client. * @return {boolean} true if there is a valid access_token for this client.
*/ */
@@ -1570,13 +1578,21 @@ function setupCallEventHandler(client) {
} }
function checkTurnServers(client) { function checkTurnServers(client) {
if (!client._supportsVoip || !client.running) { if (!client._supportsVoip) {
return; return;
} }
client.turnServers().done(function(res) { client.turnServer().done(function(res) {
if (res.uris) { if (res.uris) {
console.log("Got TURN URIs: " + res.uris); console.log("Got TURN URIs: " + res.uris + " refresh in " +
client._turnServers = res; res.ttl + " secs");
// map the response to a format that can be fed to
// RTCPeerConnection
var servers = {
urls: res.uris,
username: res.username,
credential: res.password
};
client._turnServers = [servers];
// re-fetch when we're about to reach the TTL // re-fetch when we're about to reach the TTL
setTimeout(function() { checkTurnServers(client); }, setTimeout(function() { checkTurnServers(client); },
(res.ttl || (60 * 60)) * 1000 * 0.9 (res.ttl || (60 * 60)) * 1000 * 0.9
@@ -1584,7 +1600,6 @@ function checkTurnServers(client) {
} }
}, function(err) { }, function(err) {
console.error("Failed to get TURN URIs"); console.error("Failed to get TURN URIs");
client._turnServers = {};
setTimeout(function() { checkTurnServers(client); }, 60000); setTimeout(function() { checkTurnServers(client); }, 60000);
}); });
} }

View File

@@ -25,9 +25,12 @@ function MatrixCall(opts) {
this.webRtc = opts.webRtc; this.webRtc = opts.webRtc;
this.URL = opts.URL; this.URL = opts.URL;
// Array of Objects with urls, username, credential keys // Array of Objects with urls, username, credential keys
this.turnServers = opts.turnServers || [{ this.turnServers = opts.turnServers || [];
if (this.turnServers.length === 0) {
this.turnServers.push({
urls: [MatrixCall.FALLBACK_STUN_SERVER] urls: [MatrixCall.FALLBACK_STUN_SERVER]
}]; });
};
utils.forEach(this.turnServers, function(server) { utils.forEach(this.turnServers, function(server) {
utils.checkObjectHasKeys(server, ["urls"]); utils.checkObjectHasKeys(server, ["urls"]);
}); });
@@ -899,7 +902,8 @@ module.exports.createNewMatrixCall = function(client, roomId) {
webRtc: webRtc, webRtc: webRtc,
client: client, client: client,
URL: w.URL, URL: w.URL,
roomId: roomId roomId: roomId,
turnServers: client.getTurnServers()
}; };
return new MatrixCall(opts); return new MatrixCall(opts);
}; };