Skip to content

Commit

Permalink
Change ICE server fallback to opt-in
Browse files Browse the repository at this point in the history
This changes ICE server fallback to be disabled by default. The SDK consumer
will receive a new event in case the homeserver has no ICE servers of its own,
and can prompt the user to agree to the fallback if desired.

Part of element-hq/element-web#10173
  • Loading branch information
jryans committed Aug 13, 2019
1 parent 3d8dd29 commit a02814b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
33 changes: 29 additions & 4 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ function MatrixClient(opts) {
this._verificationMethods = opts.verificationMethods;

this._forceTURN = opts.forceTURN || false;
this._fallbackICEServerAllowed = false;

// List of which rooms have encryption enabled: separate from crypto because
// we still want to know which rooms are encrypted even if crypto is disabled:
Expand Down Expand Up @@ -3883,6 +3884,28 @@ MatrixClient.prototype.getTurnServers = function() {
return this._turnServers || [];
};

/**
* Set whether to allow a fallback ICE server should be used for negotiating a
* WebRTC connection if the homeserver doesn't provide any servers. Defaults to
* false.
*
* @param {boolean} allow
*/
MatrixClient.prototype.setFallbackICEServerAllowed = function(allow) {
this._fallbackICEServerAllowed = allow;
};

/**
* Get whether to allow a fallback ICE server should be used for negotiating a
* WebRTC connection if the homeserver doesn't provide any servers. Defaults to
* false.
*
* @returns {boolean}
*/
MatrixClient.prototype.isFallbackICEServerAllowed = function() {
return this._fallbackICEServerAllowed;
};

// Higher level APIs
// =================

Expand Down Expand Up @@ -4330,13 +4353,15 @@ function checkTurnServers(client) {
client._checkTurnServersTimeoutID = setTimeout(() => {
checkTurnServers(client);
}, (res.ttl || (60 * 60)) * 1000 * 0.9);
} else {
logger.warn("No TURN URIs from homeserver");
client.emit("Call.noTURNServers");
}
}, function(err) {
logger.error("Failed to get TURN URIs");
client._checkTurnServersTimeoutID =
setTimeout(function() {
checkTurnServers(client);
}, 60000);
client._checkTurnServersTimeoutID = setTimeout(function() {
checkTurnServers(client);
}, 60000);
});
}

Expand Down
12 changes: 7 additions & 5 deletions src/webrtc/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ function MatrixCall(opts) {
this.URL = opts.URL;
// Array of Objects with urls, username, credential keys
this.turnServers = opts.turnServers || [];
if (this.turnServers.length === 0) {
if (this.turnServers.length === 0 && this.client.isFallbackICEServerAllowed()) {
this.turnServers.push({
urls: [MatrixCall.FALLBACK_STUN_SERVER],
urls: [MatrixCall.FALLBACK_ICE_SERVER],
});
}
utils.forEach(this.turnServers, function(server) {
Expand Down Expand Up @@ -92,8 +92,8 @@ function MatrixCall(opts) {
}
/** The length of time a call can be ringing for. */
MatrixCall.CALL_TIMEOUT_MS = 60000;
/** The fallback server to use for STUN. */
MatrixCall.FALLBACK_STUN_SERVER = 'stun:turn.matrix.org';
/** The fallback ICE server to use for STUN or TURN protocols. */
MatrixCall.FALLBACK_ICE_SERVER = 'stun:turn.matrix.org';
/** An error code when the local client failed to create an offer. */
MatrixCall.ERR_LOCAL_OFFER_FAILED = "local_offer_failed";
/**
Expand Down Expand Up @@ -1337,7 +1337,9 @@ module.exports.setVideoInput = function(deviceId) { videoInput = deviceId; };
* @param {MatrixClient} client The client instance to use.
* @param {string} roomId The room the call is in.
* @param {Object?} options DEPRECATED optional options map.
* @param {boolean} options.forceTURN DEPRECATED whether relay through TURN should be forced. This option is deprecated - use opts.forceTURN when creating the matrix client since it's only possible to set this option on outbound calls.
* @param {boolean} options.forceTURN DEPRECATED whether relay through TURN should be
* forced. This option is deprecated - use opts.forceTURN when creating the matrix client
* since it's only possible to set this option on outbound calls.
* @return {MatrixCall} the call or null if the browser doesn't support calling.
*/
module.exports.createNewMatrixCall = function(client, roomId, options) {
Expand Down

0 comments on commit a02814b

Please sign in to comment.