Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change ICE fallback server and make fallback opt-in #1015

Merged
merged 6 commits into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ function keyFromRecoverySession(session, decryptionKey) {
* that the application can handle. Each element should be an item from {@link
* module:crypto~verificationMethods verificationMethods}, or a class that
* implements the {$link module:crypto/verification/Base verifier interface}.
*
* @param {boolean} [opts.forceTURN]
* Optional. Whether relaying calls through a TURN server should be forced.
*
* @param {boolean} [opts.fallbackICEServerAllowed]
* Optional. 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.
*/
function MatrixClient(opts) {
opts.baseUrl = utils.ensureNoTrailingSlash(opts.baseUrl);
Expand Down Expand Up @@ -221,6 +228,7 @@ function MatrixClient(opts) {
this._verificationMethods = opts.verificationMethods;

this._forceTURN = opts.forceTURN || false;
this._fallbackICEServerAllowed = opts.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 +3891,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 @@ -4333,10 +4363,9 @@ function checkTurnServers(client) {
}
}, 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
33 changes: 10 additions & 23 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:stun.l.google.com:19302';
/** 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 @@ -665,7 +665,7 @@ MatrixCall.prototype._maybeGotUserMediaForAnswer = function(stream) {
},
};
self.peerConn.createAnswer(function(description) {
debuglog("Created answer: " + description);
debuglog("Created answer: ", description);
self.peerConn.setLocalDescription(description, function() {
self._answerContent = {
version: 0,
Expand Down Expand Up @@ -754,7 +754,7 @@ MatrixCall.prototype._receivedAnswer = function(msg) {
*/
MatrixCall.prototype._gotLocalOffer = function(description) {
const self = this;
debuglog("Created offer: " + description);
debuglog("Created offer: ", description);

if (self.state == 'ended') {
debuglog("Ignoring newly created offer on call ID " + self.callId +
Expand Down Expand Up @@ -1217,24 +1217,9 @@ const _placeCallWithConstraints = function(self, constraints) {
};

const _createPeerConnection = function(self) {
let servers = self.turnServers;
if (self.webRtc.vendor === "mozilla") {
// modify turnServers struct to match what mozilla expects.
servers = [];
for (let i = 0; i < self.turnServers.length; i++) {
for (let j = 0; j < self.turnServers[i].urls.length; j++) {
servers.push({
url: self.turnServers[i].urls[j],
username: self.turnServers[i].username,
credential: self.turnServers[i].credential,
});
}
}
}

const pc = new self.webRtc.RtcPeerConnection({
iceTransportPolicy: self.forceTURN ? 'relay' : undefined,
iceServers: servers,
iceServers: self.turnServers,
});
pc.oniceconnectionstatechange = hookCallback(self, self._onIceConnectionStateChanged);
pc.onsignalingstatechange = hookCallback(self, self._onSignallingStateChanged);
Expand Down Expand Up @@ -1352,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