From 34b21a25987656136097262877e2b05595b9ae25 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Tue, 22 Mar 2022 18:22:18 +0100 Subject: [PATCH] Add preference forceRelay. This may be useful to get around network blocks in some cases. --- static/galene.js | 18 ++++++++++++++++++ static/protocol.js | 26 ++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/static/galene.js b/static/galene.js index 560cb74..5e56d8e 100644 --- a/static/galene.js +++ b/static/galene.js @@ -47,6 +47,7 @@ let token = null; * @property {string} [filter] * @property {boolean} [preprocessing] * @property {boolean} [hqaudio] + * @property {boolean} [forceRelay] */ /** @type{settings} */ @@ -324,6 +325,22 @@ async function gotConnected(username) { } } +/** + * @this {ServerConnection} + * @param {boolean} up + */ +function onPeerConnection(up) { + if(!getSettings().forceRelay) + return null; + let old = this.rtcConfiguration; + /** @type {RTCConfiguration} */ + let conf = {}; + for(let key in old) + conf[key] = old[key]; + conf.iceTransportPolicy = 'relay'; + return conf; +} + /** * @this {ServerConnection} * @param {number} code @@ -3827,6 +3844,7 @@ async function serverConnect() { serverConnection.close(); serverConnection = new ServerConnection(); serverConnection.onconnected = gotConnected; + serverConnection.onpeerconnection = onPeerConnection; serverConnection.onclose = gotClose; serverConnection.ondownstream = gotDownStream; serverConnection.onuser = gotUser; diff --git a/static/protocol.js b/static/protocol.js index bd11f8e..ff4f127 100644 --- a/static/protocol.js +++ b/static/protocol.js @@ -151,6 +151,14 @@ function ServerConnection() { * @type{(this: ServerConnection, code: number, reason: string) => void} */ this.onclose = null; + /** + * onpeerconnection is called before we establish a new peer connection. + * It may either return null, or a new RTCConfiguration that overrides + * the value obtained from the server. + * + * @type{(this: ServerConnection, up: boolean) => RTCConfiguration} + */ + this.onpeerconnection = null; /** * onuser is called whenever a user in the group changes. The users * array has already been updated. @@ -512,6 +520,19 @@ ServerConnection.prototype.findByLocalId = function(localId) { return null; } +/** + * @param {boolean} up + * @returns {RTCConfiguration} + */ +ServerConnection.prototype.getRTCConfiguration = function(up) { + if(this.onpeerconnection) { + let conf = this.onpeerconnection.call(this, up); + if(conf !== null) + return conf; + } + return this.rtcConfiguration; +} + /** * newUpStream requests the creation of a new up stream. * @@ -529,7 +550,8 @@ ServerConnection.prototype.newUpStream = function(localId) { if(typeof RTCPeerConnection === 'undefined') throw new Error("This browser doesn't support WebRTC"); - let pc = new RTCPeerConnection(sc.rtcConfiguration); + + let pc = new RTCPeerConnection(sc.getRTCConfiguration(true)); if(!pc) throw new Error("Couldn't create peer connection"); @@ -685,7 +707,7 @@ ServerConnection.prototype.gotOffer = async function(id, label, source, username if(!c) { let pc; try { - pc = new RTCPeerConnection(sc.rtcConfiguration); + pc = new RTCPeerConnection(sc.getRTCConfiguration(false)); } catch(e) { console.error(e); sc.send({