1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-10 02:35:58 +01:00

Don't enable simulcast when there are just two users.

If simulcast is set to auto, disable simulcast when there are just
two users in the group.
This commit is contained in:
Juliusz Chroboczek 2021-05-09 15:06:10 +02:00
parent 2f44961589
commit 32663dbf3f

View file

@ -538,22 +538,14 @@ getSelectElement('sendselect').onchange = async function(e) {
if(!(this instanceof HTMLSelectElement)) if(!(this instanceof HTMLSelectElement))
throw new Error('Unexpected type for this'); throw new Error('Unexpected type for this');
updateSettings({send: this.value}); updateSettings({send: this.value});
let t = getMaxVideoThroughput(); await reconsiderSendParameters();
for(let id in serverConnection.up) {
let c = serverConnection.up[id];
await setMaxVideoThroughput(c, t);
}
}; };
getSelectElement('simulcastselect').onchange = async function(e) { getSelectElement('simulcastselect').onchange = async function(e) {
if(!(this instanceof HTMLSelectElement)) if(!(this instanceof HTMLSelectElement))
throw new Error('Unexpected type for this'); throw new Error('Unexpected type for this');
updateSettings({simulcast: this.value}); updateSettings({simulcast: this.value});
let t = getMaxVideoThroughput(); await reconsiderSendParameters();
for(let id in serverConnection.up) {
let c = serverConnection.up[id];
await setMaxVideoThroughput(c, t);
}
}; };
/** /**
@ -826,11 +818,15 @@ function newUpStream(localId) {
} }
/** /**
* Sets an up stream's video throughput and simulcast parameters.
*
* @param {Stream} c * @param {Stream} c
* @param {number} [bps] * @param {number} bps
* @param {boolean} simulcast
*/ */
async function setMaxVideoThroughput(c, bps) { async function setSendParameters(c, bps, simulcast) {
let simulcast = doSimulcast(); if(!c.up)
throw new Error('Setting throughput of down stream');
let senders = c.pc.getSenders(); let senders = c.pc.getSenders();
for(let i = 0; i < senders.length; i++) { for(let i = 0; i < senders.length; i++) {
let s = senders[i]; let s = senders[i];
@ -853,6 +849,40 @@ async function setMaxVideoThroughput(c, bps) {
} }
} }
let reconsiderParametersTimer = null;
/**
* Sets the send parameters for all up streams.
*/
async function reconsiderSendParameters() {
cancelReconsiderParameters();
let t = getMaxVideoThroughput();
let s = doSimulcast();
let promises = [];
for(let id in serverConnection.up) {
let c = serverConnection.up[id];
promises.push(setSendParameters(c, t, s));
}
await Promise.all(promises);
}
/**
* Schedules a call to reconsiderSendParameters after a delay.
* The delay avoids excessive flapping.
*/
function scheduleReconsiderParameters() {
cancelReconsiderParameters();
reconsiderParametersTimer =
setTimeout(reconsiderSendParameters, 10000 + Math.random() * 10000);
}
function cancelReconsiderParameters() {
if(reconsiderParametersTimer) {
clearTimeout(reconsiderParametersTimer);
reconsiderParametersTimer = null;
}
}
/** /**
* @typedef {Object} filterDefinition * @typedef {Object} filterDefinition
* @property {string} [description] * @property {string} [description]
@ -1057,6 +1087,8 @@ const unlimitedRate = 1000000000;
const simulcastRate = 100000; const simulcastRate = 100000;
/** /**
* Decide whether we want to send simulcast.
*
* @returns {boolean} * @returns {boolean}
*/ */
function doSimulcast() { function doSimulcast() {
@ -1066,6 +1098,8 @@ function doSimulcast() {
case 'off': case 'off':
return false; return false;
default: default:
if(Object.keys(serverConnection.users).length <= 2)
return false;
let bps = getMaxVideoThroughput(); let bps = getMaxVideoThroughput();
return bps <= 0 || bps >= 2 * simulcastRate; return bps <= 0 || bps >= 2 * simulcastRate;
} }
@ -1872,9 +1906,13 @@ function gotUser(id, kind) {
switch(kind) { switch(kind) {
case 'add': case 'add':
addUser(id, serverConnection.users[id].username); addUser(id, serverConnection.users[id].username);
if(Object.keys(serverConnection.users).length == 3)
reconsiderSendParameters();
break; break;
case 'delete': case 'delete':
delUser(id); delUser(id);
if(Object.keys(serverConnection.users).length < 3)
scheduleReconsiderParameters();
break; break;
case 'change': case 'change':
changeUser(id, serverConnection.users[id].username); changeUser(id, serverConnection.users[id].username);