diff --git a/lib/client.js b/lib/client.js index fe7032e02..43a7ba567 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1096,6 +1096,14 @@ MatrixClient.prototype.turnServer = function(callback) { return this._http.authedRequest(callback, "GET", "/voip/turnServer"); }; +/** + * Get the TURN servers for this home server. + * @return {Array} 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. */ @@ -1570,13 +1578,21 @@ function setupCallEventHandler(client) { } function checkTurnServers(client) { - if (!client._supportsVoip || !client.running) { + if (!client._supportsVoip) { return; } - client.turnServers().done(function(res) { + client.turnServer().done(function(res) { if (res.uris) { - console.log("Got TURN URIs: " + res.uris); - client._turnServers = res; + console.log("Got TURN URIs: " + res.uris + " refresh in " + + 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 setTimeout(function() { checkTurnServers(client); }, (res.ttl || (60 * 60)) * 1000 * 0.9 @@ -1584,7 +1600,6 @@ function checkTurnServers(client) { } }, function(err) { console.error("Failed to get TURN URIs"); - client._turnServers = {}; setTimeout(function() { checkTurnServers(client); }, 60000); }); } diff --git a/lib/webrtc/call.js b/lib/webrtc/call.js index 2e8361db3..b0c688e35 100644 --- a/lib/webrtc/call.js +++ b/lib/webrtc/call.js @@ -25,9 +25,12 @@ function MatrixCall(opts) { this.webRtc = opts.webRtc; this.URL = opts.URL; // Array of Objects with urls, username, credential keys - this.turnServers = opts.turnServers || [{ - urls: [MatrixCall.FALLBACK_STUN_SERVER] - }]; + this.turnServers = opts.turnServers || []; + if (this.turnServers.length === 0) { + this.turnServers.push({ + urls: [MatrixCall.FALLBACK_STUN_SERVER] + }); + }; utils.forEach(this.turnServers, function(server) { utils.checkObjectHasKeys(server, ["urls"]); }); @@ -899,7 +902,8 @@ module.exports.createNewMatrixCall = function(client, roomId) { webRtc: webRtc, client: client, URL: w.URL, - roomId: roomId + roomId: roomId, + turnServers: client.getTurnServers() }; return new MatrixCall(opts); };