2020-04-24 19:38:21 +02:00
|
|
|
// Copyright (c) 2020 by Juliusz Chroboczek.
|
|
|
|
|
2020-12-19 17:26:16 +01:00
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
2020-04-24 19:38:21 +02:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2020-08-24 22:31:22 +02:00
|
|
|
/** @type {string} */
|
2020-04-24 19:38:21 +02:00
|
|
|
let group;
|
2020-08-24 22:31:22 +02:00
|
|
|
|
|
|
|
/** @type {ServerConnection} */
|
2020-08-11 17:09:31 +02:00
|
|
|
let serverConnection;
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2021-10-26 22:22:48 +02:00
|
|
|
/** @type {Object} */
|
|
|
|
let groupStatus = {};
|
|
|
|
|
2024-04-11 00:40:49 +02:00
|
|
|
/** @type {boolean} */
|
|
|
|
let pwAuth = false;
|
|
|
|
|
2022-02-18 20:27:53 +01:00
|
|
|
/** @type {string} */
|
|
|
|
let token = null;
|
|
|
|
|
2024-06-11 11:57:15 +02:00
|
|
|
/** @type {string} */
|
|
|
|
let probingState = null;
|
2023-04-01 13:28:24 +02:00
|
|
|
|
2020-09-18 17:57:37 +02:00
|
|
|
/**
|
|
|
|
* @typedef {Object} settings
|
|
|
|
* @property {boolean} [localMute]
|
|
|
|
* @property {string} [video]
|
|
|
|
* @property {string} [audio]
|
2021-05-08 21:25:22 +02:00
|
|
|
* @property {string} [simulcast]
|
2020-09-18 17:57:37 +02:00
|
|
|
* @property {string} [send]
|
|
|
|
* @property {string} [request]
|
|
|
|
* @property {boolean} [activityDetection]
|
2022-09-16 19:10:01 +02:00
|
|
|
* @property {boolean} [displayAll]
|
2020-12-14 19:31:42 +01:00
|
|
|
* @property {Array.<number>} [resolution]
|
2021-01-23 01:15:53 +01:00
|
|
|
* @property {boolean} [mirrorView]
|
2020-09-18 19:14:10 +02:00
|
|
|
* @property {boolean} [blackboardMode]
|
2021-01-12 01:25:35 +01:00
|
|
|
* @property {string} [filter]
|
2022-02-21 18:07:49 +01:00
|
|
|
* @property {boolean} [preprocessing]
|
|
|
|
* @property {boolean} [hqaudio]
|
2022-03-22 18:22:18 +01:00
|
|
|
* @property {boolean} [forceRelay]
|
2020-09-18 17:57:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/** @type{settings} */
|
|
|
|
let fallbackSettings = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {settings} settings
|
|
|
|
*/
|
|
|
|
function storeSettings(settings) {
|
|
|
|
try {
|
|
|
|
window.sessionStorage.setItem('settings', JSON.stringify(settings));
|
|
|
|
fallbackSettings = null;
|
|
|
|
} catch(e) {
|
2021-03-30 07:28:20 +02:00
|
|
|
console.warn("Couldn't store settings:", e);
|
2020-09-18 17:57:37 +02:00
|
|
|
fallbackSettings = settings;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This always returns a dictionary.
|
|
|
|
*
|
|
|
|
* @returns {settings}
|
|
|
|
*/
|
|
|
|
function getSettings() {
|
2020-09-20 14:33:13 +02:00
|
|
|
/** @type {settings} */
|
2020-09-18 17:57:37 +02:00
|
|
|
let settings;
|
|
|
|
try {
|
|
|
|
let json = window.sessionStorage.getItem('settings');
|
|
|
|
settings = JSON.parse(json);
|
|
|
|
} catch(e) {
|
2021-03-30 07:28:20 +02:00
|
|
|
console.warn("Couldn't retrieve settings:", e);
|
2020-09-18 17:57:37 +02:00
|
|
|
settings = fallbackSettings;
|
|
|
|
}
|
|
|
|
return settings || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {settings} settings
|
|
|
|
*/
|
|
|
|
function updateSettings(settings) {
|
|
|
|
let s = getSettings();
|
|
|
|
for(let key in settings)
|
|
|
|
s[key] = settings[key];
|
|
|
|
storeSettings(s);
|
|
|
|
}
|
|
|
|
|
2020-09-23 21:01:29 +02:00
|
|
|
/**
|
|
|
|
* @param {string} key
|
|
|
|
* @param {any} value
|
|
|
|
*/
|
|
|
|
function updateSetting(key, value) {
|
|
|
|
let s = {};
|
|
|
|
s[key] = value;
|
|
|
|
updateSettings(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} key
|
|
|
|
*/
|
|
|
|
function delSetting(key) {
|
|
|
|
let s = getSettings();
|
|
|
|
if(!(key in s))
|
|
|
|
return;
|
|
|
|
delete(s[key]);
|
2020-12-26 17:28:44 +01:00
|
|
|
storeSettings(s);
|
2020-09-23 21:01:29 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
*/
|
|
|
|
function getSelectElement(id) {
|
|
|
|
let elt = document.getElementById(id);
|
|
|
|
if(!elt || !(elt instanceof HTMLSelectElement))
|
|
|
|
throw new Error(`Couldn't find ${id}`);
|
|
|
|
return elt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
*/
|
|
|
|
function getInputElement(id) {
|
|
|
|
let elt = document.getElementById(id);
|
|
|
|
if(!elt || !(elt instanceof HTMLInputElement))
|
|
|
|
throw new Error(`Couldn't find ${id}`);
|
|
|
|
return elt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
*/
|
|
|
|
function getButtonElement(id) {
|
|
|
|
let elt = document.getElementById(id);
|
|
|
|
if(!elt || !(elt instanceof HTMLButtonElement))
|
|
|
|
throw new Error(`Couldn't find ${id}`);
|
|
|
|
return elt;
|
|
|
|
}
|
|
|
|
|
2020-09-18 17:57:37 +02:00
|
|
|
function reflectSettings() {
|
|
|
|
let settings = getSettings();
|
|
|
|
let store = false;
|
|
|
|
|
|
|
|
setLocalMute(settings.localMute);
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
let videoselect = getSelectElement('videoselect');
|
2020-11-30 21:53:25 +01:00
|
|
|
if(!settings.hasOwnProperty('video') ||
|
|
|
|
!selectOptionAvailable(videoselect, settings.video)) {
|
2020-09-18 17:57:37 +02:00
|
|
|
settings.video = selectOptionDefault(videoselect);
|
|
|
|
store = true;
|
|
|
|
}
|
|
|
|
videoselect.value = settings.video;
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
let audioselect = getSelectElement('audioselect');
|
2020-11-30 21:53:25 +01:00
|
|
|
if(!settings.hasOwnProperty('audio') ||
|
|
|
|
!selectOptionAvailable(audioselect, settings.audio)) {
|
2020-09-18 17:57:37 +02:00
|
|
|
settings.audio = selectOptionDefault(audioselect);
|
|
|
|
store = true;
|
|
|
|
}
|
|
|
|
audioselect.value = settings.audio;
|
|
|
|
|
2021-01-13 13:57:05 +01:00
|
|
|
if(settings.hasOwnProperty('filter')) {
|
|
|
|
getSelectElement('filterselect').value = settings.filter;
|
|
|
|
} else {
|
|
|
|
let s = getSelectElement('filterselect').value;
|
|
|
|
if(s) {
|
|
|
|
settings.filter = s;
|
|
|
|
store = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 21:53:25 +01:00
|
|
|
if(settings.hasOwnProperty('request')) {
|
2020-09-20 14:33:13 +02:00
|
|
|
getSelectElement('requestselect').value = settings.request;
|
2020-11-30 21:53:25 +01:00
|
|
|
} else {
|
2020-09-20 14:33:13 +02:00
|
|
|
settings.request = getSelectElement('requestselect').value;
|
2020-09-18 17:57:37 +02:00
|
|
|
store = true;
|
|
|
|
}
|
|
|
|
|
2020-11-30 21:53:25 +01:00
|
|
|
if(settings.hasOwnProperty('send')) {
|
2020-09-20 14:33:13 +02:00
|
|
|
getSelectElement('sendselect').value = settings.send;
|
2020-11-30 21:53:25 +01:00
|
|
|
} else {
|
2020-09-20 14:33:13 +02:00
|
|
|
settings.send = getSelectElement('sendselect').value;
|
2020-09-18 17:57:37 +02:00
|
|
|
store = true;
|
|
|
|
}
|
|
|
|
|
2021-05-08 21:25:22 +02:00
|
|
|
if(settings.hasOwnProperty('simulcast')) {
|
|
|
|
getSelectElement('simulcastselect').value = settings.simulcast
|
|
|
|
} else {
|
|
|
|
settings.simulcast = getSelectElement('simulcastselect').value;
|
|
|
|
store = true;
|
|
|
|
}
|
|
|
|
|
2021-01-23 01:01:53 +01:00
|
|
|
if(settings.hasOwnProperty('blackboardMode')) {
|
|
|
|
getInputElement('blackboardbox').checked = settings.blackboardMode;
|
|
|
|
} else {
|
|
|
|
settings.blackboardMode = getInputElement('blackboardbox').checked;
|
|
|
|
store = true;
|
|
|
|
}
|
2020-09-18 17:57:37 +02:00
|
|
|
|
2021-01-23 01:15:53 +01:00
|
|
|
if(settings.hasOwnProperty('mirrorView')) {
|
|
|
|
getInputElement('mirrorbox').checked = settings.mirrorView;
|
|
|
|
} else {
|
|
|
|
settings.mirrorView = getInputElement('mirrorbox').checked;
|
|
|
|
store = true;
|
|
|
|
}
|
|
|
|
|
2021-01-23 01:01:53 +01:00
|
|
|
if(settings.hasOwnProperty('activityDetection')) {
|
|
|
|
getInputElement('activitybox').checked = settings.activityDetection;
|
|
|
|
} else {
|
|
|
|
settings.activityDetection = getInputElement('activitybox').checked;
|
|
|
|
store = true;
|
|
|
|
}
|
2020-09-18 19:14:10 +02:00
|
|
|
|
2022-09-16 19:10:01 +02:00
|
|
|
if(settings.hasOwnProperty('displayAll')) {
|
|
|
|
getInputElement('displayallbox').checked = settings.displayAll;
|
|
|
|
} else {
|
|
|
|
settings.displayAll = getInputElement('displayallbox').checked;
|
|
|
|
store = true;
|
|
|
|
}
|
|
|
|
|
2022-02-21 18:07:49 +01:00
|
|
|
if(settings.hasOwnProperty('preprocessing')) {
|
|
|
|
getInputElement('preprocessingbox').checked = settings.preprocessing;
|
|
|
|
} else {
|
|
|
|
settings.preprocessing = getInputElement('preprocessingbox').checked;
|
|
|
|
store = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(settings.hasOwnProperty('hqaudio')) {
|
|
|
|
getInputElement('hqaudiobox').checked = settings.hqaudio;
|
|
|
|
} else {
|
|
|
|
settings.hqaudio = getInputElement('hqaudiobox').checked;
|
|
|
|
store = true;
|
|
|
|
}
|
|
|
|
|
2020-09-18 17:57:37 +02:00
|
|
|
if(store)
|
|
|
|
storeSettings(settings);
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:18:20 +01:00
|
|
|
function isMobileLayout() {
|
2021-03-28 22:29:04 +02:00
|
|
|
if (window.matchMedia('only screen and (max-width: 1024px)').matches)
|
|
|
|
return true;
|
|
|
|
return false;
|
2020-09-01 10:23:35 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 16:34:52 +02:00
|
|
|
/**
|
|
|
|
* @param {boolean} [force]
|
|
|
|
*/
|
2020-09-01 10:23:35 +02:00
|
|
|
function hideVideo(force) {
|
|
|
|
let mediadiv = document.getElementById('peers');
|
2020-09-12 16:34:52 +02:00
|
|
|
if(mediadiv.childElementCount > 0 && !force)
|
|
|
|
return;
|
2021-03-26 20:18:20 +01:00
|
|
|
setVisibility('video-container', false);
|
2021-05-14 18:44:49 +02:00
|
|
|
scheduleReconsiderDownRate();
|
2020-09-09 20:26:19 +02:00
|
|
|
}
|
|
|
|
|
2021-03-26 20:18:20 +01:00
|
|
|
function showVideo() {
|
|
|
|
let hasmedia = document.getElementById('peers').childElementCount > 0;
|
|
|
|
if(isMobileLayout()) {
|
|
|
|
setVisibility('show-video', false);
|
|
|
|
setVisibility('collapse-video', hasmedia);
|
|
|
|
}
|
|
|
|
setVisibility('video-container', hasmedia);
|
2021-05-14 18:44:49 +02:00
|
|
|
scheduleReconsiderDownRate();
|
2020-09-01 10:23:35 +02:00
|
|
|
}
|
|
|
|
|
2023-07-05 23:41:59 +02:00
|
|
|
function isSafari() {
|
|
|
|
let ua = navigator.userAgent.toLowerCase();
|
|
|
|
return ua.indexOf('safari') >= 0 && ua.indexOf('chrome') < 0;
|
|
|
|
}
|
|
|
|
|
2023-07-09 17:33:53 +02:00
|
|
|
function isFirefox() {
|
|
|
|
let ua = navigator.userAgent.toLowerCase();
|
|
|
|
return ua.indexOf('firefox') >= 0;
|
|
|
|
}
|
|
|
|
|
2023-07-05 23:41:59 +02:00
|
|
|
/** @type {MediaStream} */
|
|
|
|
let safariStream = null;
|
|
|
|
|
2020-09-05 12:59:28 +02:00
|
|
|
/**
|
|
|
|
* @param{boolean} connected
|
|
|
|
*/
|
2020-04-24 19:38:21 +02:00
|
|
|
function setConnected(connected) {
|
2020-11-24 17:36:52 +01:00
|
|
|
let userbox = document.getElementById('profile');
|
2020-09-02 15:35:55 +02:00
|
|
|
let connectionbox = document.getElementById('login-container');
|
2020-04-24 19:38:21 +02:00
|
|
|
if(connected) {
|
2020-08-11 17:09:31 +02:00
|
|
|
clearChat();
|
2020-09-02 15:35:55 +02:00
|
|
|
userbox.classList.remove('invisible');
|
|
|
|
connectionbox.classList.add('invisible');
|
2020-04-25 18:09:31 +02:00
|
|
|
displayUsername();
|
2021-05-14 18:44:49 +02:00
|
|
|
window.onresize = function(e) {
|
|
|
|
scheduleReconsiderDownRate();
|
|
|
|
}
|
2023-07-05 23:41:59 +02:00
|
|
|
if(isSafari()) {
|
|
|
|
/* Safari doesn't allow autoplay and omits host candidates
|
2023-12-08 18:39:08 +01:00
|
|
|
* unless there is an open device. */
|
2023-07-05 23:41:59 +02:00
|
|
|
if(!safariStream) {
|
|
|
|
navigator.mediaDevices.getUserMedia({audio: true}).then(s => {
|
|
|
|
safariStream = s;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
} else {
|
2020-09-02 15:35:55 +02:00
|
|
|
userbox.classList.add('invisible');
|
|
|
|
connectionbox.classList.remove('invisible');
|
2020-09-09 20:26:19 +02:00
|
|
|
hideVideo();
|
2021-05-14 18:44:49 +02:00
|
|
|
window.onresize = null;
|
2020-08-11 17:09:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-20 15:32:18 +01:00
|
|
|
/**
|
2023-04-01 13:28:24 +02:00
|
|
|
* @this {ServerConnection}
|
|
|
|
*/
|
2023-03-22 18:18:43 +01:00
|
|
|
async function gotConnected() {
|
2023-04-01 13:28:24 +02:00
|
|
|
setConnected(true);
|
2024-06-11 11:57:15 +02:00
|
|
|
await join();
|
2023-04-01 13:28:24 +02:00
|
|
|
}
|
|
|
|
|
2024-04-11 00:40:49 +02:00
|
|
|
/**
|
|
|
|
* @param {string} username
|
|
|
|
*/
|
|
|
|
function setChangePassword(username) {
|
|
|
|
let s = document.getElementById('chpwspan');
|
|
|
|
let a = s.children[0];
|
|
|
|
if(!(a instanceof HTMLAnchorElement))
|
|
|
|
throw new Error('Bad type for chpwspan');
|
|
|
|
if(username) {
|
|
|
|
a.href = `/change-password.html?group=${encodeURI(group)}&username=${encodeURI(username)}`;
|
|
|
|
a.target = '_blank';
|
|
|
|
s.classList.remove('invisible');
|
|
|
|
} else {
|
|
|
|
a.href = null;
|
|
|
|
s.classList.add('invisible');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-11 11:57:15 +02:00
|
|
|
async function join() {
|
2023-04-01 13:28:24 +02:00
|
|
|
let username = getInputElement('username').value.trim();
|
|
|
|
let credentials;
|
2022-02-18 20:27:53 +01:00
|
|
|
if(token) {
|
2024-04-11 00:40:49 +02:00
|
|
|
pwAuth = false;
|
2021-10-29 23:37:05 +02:00
|
|
|
credentials = {
|
2022-02-18 20:27:53 +01:00
|
|
|
type: 'token',
|
|
|
|
token: token,
|
2021-10-29 23:37:05 +02:00
|
|
|
};
|
2024-06-11 11:57:15 +02:00
|
|
|
switch(probingState) {
|
|
|
|
case null:
|
|
|
|
// when logging in with a token, we need to give the user
|
|
|
|
// a chance to interact with the page in order to enable
|
|
|
|
// autoplay. Probe the group first in order to determine if
|
|
|
|
// we need a username. We should really extend the protocol
|
|
|
|
// to have a simpler protocol for probing.
|
|
|
|
probingState = 'probing';
|
2023-04-01 13:28:24 +02:00
|
|
|
username = null;
|
2024-06-11 11:57:15 +02:00
|
|
|
break;
|
|
|
|
case 'need-username':
|
|
|
|
case 'success':
|
|
|
|
probingState = null;
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
console.warn(`Unexpected probing state ${probingState}`);
|
|
|
|
probingState = null;
|
|
|
|
break;
|
|
|
|
}
|
2022-02-18 20:27:53 +01:00
|
|
|
} else {
|
2024-06-11 11:57:15 +02:00
|
|
|
if(probingState !== null) {
|
|
|
|
console.warn(`Unexpected probing state ${probingState}`);
|
|
|
|
probingState = null;
|
|
|
|
}
|
2022-02-18 20:27:53 +01:00
|
|
|
let pw = getInputElement('password').value;
|
|
|
|
getInputElement('password').value = '';
|
2024-04-11 00:40:49 +02:00
|
|
|
if(!groupStatus.authServer) {
|
|
|
|
pwAuth = true;
|
2022-02-18 20:27:53 +01:00
|
|
|
credentials = pw;
|
2024-04-11 00:40:49 +02:00
|
|
|
} else {
|
|
|
|
pwAuth = false;
|
2022-02-18 20:27:53 +01:00
|
|
|
credentials = {
|
|
|
|
type: 'authServer',
|
|
|
|
authServer: groupStatus.authServer,
|
|
|
|
location: location.href,
|
|
|
|
password: pw,
|
|
|
|
};
|
2024-04-11 00:40:49 +02:00
|
|
|
}
|
2022-02-18 20:27:53 +01:00
|
|
|
}
|
2021-10-29 23:37:05 +02:00
|
|
|
|
2021-10-29 23:37:59 +02:00
|
|
|
try {
|
2023-04-01 13:28:24 +02:00
|
|
|
await serverConnection.join(group, username, credentials);
|
2021-10-29 23:37:59 +02:00
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
|
|
|
displayError(e);
|
|
|
|
serverConnection.close();
|
|
|
|
}
|
2020-08-11 17:09:31 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 18:22:18 +01:00
|
|
|
/**
|
|
|
|
* @this {ServerConnection}
|
|
|
|
*/
|
2022-03-23 00:11:12 +01:00
|
|
|
function onPeerConnection() {
|
2022-03-22 18:22:18 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-12 16:34:52 +02:00
|
|
|
/**
|
2020-09-20 14:33:13 +02:00
|
|
|
* @this {ServerConnection}
|
2020-09-12 16:34:52 +02:00
|
|
|
* @param {number} code
|
|
|
|
* @param {string} reason
|
|
|
|
*/
|
2020-08-11 17:09:31 +02:00
|
|
|
function gotClose(code, reason) {
|
2021-07-16 00:11:38 +02:00
|
|
|
closeUpMedia();
|
2020-08-11 17:09:31 +02:00
|
|
|
setConnected(false);
|
2020-09-01 10:23:35 +02:00
|
|
|
if(code != 1000) {
|
2020-08-11 17:09:31 +02:00
|
|
|
console.warn('Socket close', code, reason);
|
2020-09-01 10:23:35 +02:00
|
|
|
}
|
2024-06-11 11:57:15 +02:00
|
|
|
let form = document.getElementById('loginform');
|
2024-06-10 19:41:23 +02:00
|
|
|
if(!(form instanceof HTMLFormElement))
|
2024-06-11 11:57:15 +02:00
|
|
|
throw new Error('Bad type for loginform');
|
2024-06-10 19:41:23 +02:00
|
|
|
form.active = true;
|
2020-08-11 17:09:31 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 20:55:13 +02:00
|
|
|
/**
|
2020-09-20 14:33:13 +02:00
|
|
|
* @this {ServerConnection}
|
2020-08-13 20:55:13 +02:00
|
|
|
* @param {Stream} c
|
|
|
|
*/
|
2020-08-11 17:09:31 +02:00
|
|
|
function gotDownStream(c) {
|
2021-01-17 20:20:35 +01:00
|
|
|
c.onclose = function(replace) {
|
|
|
|
if(!replace)
|
2021-01-31 19:00:09 +01:00
|
|
|
delMedia(c.localId);
|
2020-08-11 17:09:31 +02:00
|
|
|
};
|
|
|
|
c.onerror = function(e) {
|
|
|
|
console.error(e);
|
2024-06-10 19:41:23 +02:00
|
|
|
displayError(e.toString());
|
2020-12-26 17:28:44 +01:00
|
|
|
};
|
2024-06-10 15:38:44 +02:00
|
|
|
c.ondowntrack = function(track, transceiver, stream) {
|
2022-09-16 19:13:55 +02:00
|
|
|
setMedia(c);
|
2020-12-26 17:28:44 +01:00
|
|
|
};
|
2021-02-03 19:46:44 +01:00
|
|
|
c.onnegotiationcompleted = function() {
|
2021-04-28 17:00:50 +02:00
|
|
|
resetMedia(c);
|
2021-02-03 19:46:44 +01:00
|
|
|
}
|
2020-08-11 17:09:31 +02:00
|
|
|
c.onstatus = function(status) {
|
|
|
|
setMediaStatus(c);
|
2020-12-26 17:28:44 +01:00
|
|
|
};
|
2020-09-11 23:20:46 +02:00
|
|
|
c.onstats = gotDownStats;
|
2020-09-18 17:57:37 +02:00
|
|
|
if(getSettings().activityDetection)
|
2020-09-11 23:20:46 +02:00
|
|
|
c.setStatsInterval(activityDetectionInterval);
|
2020-12-25 20:29:46 +01:00
|
|
|
|
2022-09-16 19:13:55 +02:00
|
|
|
setMedia(c);
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 20:26:19 +02:00
|
|
|
// Store current browser viewport height in css variable
|
|
|
|
function setViewportHeight() {
|
2020-09-12 16:34:52 +02:00
|
|
|
document.documentElement.style.setProperty(
|
|
|
|
'--vh', `${window.innerHeight/100}px`,
|
|
|
|
);
|
2021-03-26 20:18:20 +01:00
|
|
|
showVideo();
|
2020-10-07 09:33:38 +02:00
|
|
|
// Ajust video component size
|
|
|
|
resizePeers();
|
|
|
|
}
|
2020-09-09 20:26:19 +02:00
|
|
|
|
|
|
|
// On resize and orientation change, we update viewport height
|
|
|
|
addEventListener('resize', setViewportHeight);
|
|
|
|
addEventListener('orientationchange', setViewportHeight);
|
|
|
|
|
2020-11-09 01:11:13 +01:00
|
|
|
getButtonElement('presentbutton').onclick = async function(e) {
|
2020-04-24 19:38:21 +02:00
|
|
|
e.preventDefault();
|
2020-11-09 01:11:13 +01:00
|
|
|
let button = this;
|
|
|
|
if(!(button instanceof HTMLButtonElement))
|
|
|
|
throw new Error('Unexpected type for this.');
|
|
|
|
// there's a potential race condition here: the user might click the
|
|
|
|
// button a second time before the stream is set up and the button hidden.
|
|
|
|
button.disabled = true;
|
|
|
|
try {
|
2021-04-28 17:00:50 +02:00
|
|
|
let id = findUpMedia('camera');
|
2020-11-09 01:11:13 +01:00
|
|
|
if(!id)
|
|
|
|
await addLocalMedia();
|
|
|
|
} finally {
|
|
|
|
button.disabled = false;
|
|
|
|
}
|
2020-05-10 21:18:07 +02:00
|
|
|
};
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
getButtonElement('unpresentbutton').onclick = function(e) {
|
2020-05-21 18:22:30 +02:00
|
|
|
e.preventDefault();
|
2021-04-28 17:00:50 +02:00
|
|
|
closeUpMedia('camera');
|
2020-09-03 20:54:27 +02:00
|
|
|
resizePeers();
|
2020-05-21 18:22:30 +02:00
|
|
|
};
|
|
|
|
|
2020-08-13 20:55:13 +02:00
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
* @param {boolean} visible
|
|
|
|
*/
|
2020-05-21 18:22:30 +02:00
|
|
|
function setVisibility(id, visible) {
|
|
|
|
let elt = document.getElementById(id);
|
|
|
|
if(visible)
|
|
|
|
elt.classList.remove('invisible');
|
|
|
|
else
|
|
|
|
elt.classList.add('invisible');
|
|
|
|
}
|
|
|
|
|
|
|
|
function setButtonsVisibility() {
|
2021-02-04 23:11:40 +01:00
|
|
|
let connected = serverConnection && serverConnection.socket;
|
2020-08-11 17:09:31 +02:00
|
|
|
let permissions = serverConnection.permissions;
|
2021-02-14 18:06:50 +01:00
|
|
|
let canWebrtc = !(typeof RTCPeerConnection === 'undefined');
|
2023-12-08 18:46:23 +01:00
|
|
|
let canPresent = canWebrtc &&
|
|
|
|
('mediaDevices' in navigator) &&
|
|
|
|
('getUserMedia' in navigator.mediaDevices) &&
|
|
|
|
permissions.indexOf('present') >= 0;
|
|
|
|
let canShare = canWebrtc &&
|
|
|
|
('mediaDevices' in navigator) &&
|
|
|
|
('getDisplayMedia' in navigator.mediaDevices) &&
|
|
|
|
permissions.indexOf('present') >= 0;
|
|
|
|
let local = !!findUpMedia('camera');
|
2021-03-26 20:18:20 +01:00
|
|
|
let mediacount = document.getElementById('peers').childElementCount;
|
|
|
|
let mobilelayout = isMobileLayout();
|
2020-09-23 21:46:30 +02:00
|
|
|
|
2020-05-21 18:22:30 +02:00
|
|
|
// don't allow multiple presentations
|
2023-12-08 18:46:23 +01:00
|
|
|
setVisibility('presentbutton', canPresent && !local);
|
2020-05-21 18:22:30 +02:00
|
|
|
setVisibility('unpresentbutton', local);
|
2020-09-23 21:46:30 +02:00
|
|
|
|
2023-12-08 18:46:23 +01:00
|
|
|
setVisibility('mutebutton', !connected || canPresent);
|
2020-11-30 17:39:23 +01:00
|
|
|
|
2020-05-21 18:22:30 +02:00
|
|
|
// allow multiple shared documents
|
2023-12-08 18:46:23 +01:00
|
|
|
setVisibility('sharebutton', canShare);
|
2020-05-21 18:22:30 +02:00
|
|
|
|
2023-12-08 18:46:23 +01:00
|
|
|
setVisibility('mediaoptions', canPresent);
|
|
|
|
setVisibility('sendform', canPresent);
|
|
|
|
setVisibility('simulcastform', canPresent);
|
2021-03-26 20:18:20 +01:00
|
|
|
|
|
|
|
setVisibility('collapse-video', mediacount && mobilelayout);
|
2020-05-21 18:22:30 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 16:34:52 +02:00
|
|
|
/**
|
|
|
|
* @param {boolean} mute
|
2020-11-30 14:22:36 +01:00
|
|
|
* @param {boolean} [reflect]
|
2020-09-12 16:34:52 +02:00
|
|
|
*/
|
2020-11-30 14:22:36 +01:00
|
|
|
function setLocalMute(mute, reflect) {
|
2020-09-18 17:57:37 +02:00
|
|
|
muteLocalTracks(mute);
|
2020-06-09 18:05:16 +02:00
|
|
|
let button = document.getElementById('mutebutton');
|
2020-11-09 15:57:30 +01:00
|
|
|
let icon = button.querySelector("span .fas");
|
2020-09-18 17:57:37 +02:00
|
|
|
if(mute){
|
2020-09-11 10:39:32 +02:00
|
|
|
icon.classList.add('fa-microphone-slash');
|
|
|
|
icon.classList.remove('fa-microphone');
|
2020-06-09 18:05:16 +02:00
|
|
|
button.classList.add('muted');
|
2020-09-11 10:39:32 +02:00
|
|
|
} else {
|
|
|
|
icon.classList.remove('fa-microphone-slash');
|
|
|
|
icon.classList.add('fa-microphone');
|
2020-06-09 18:05:16 +02:00
|
|
|
button.classList.remove('muted');
|
2020-09-11 10:39:32 +02:00
|
|
|
}
|
2020-11-30 14:22:36 +01:00
|
|
|
if(reflect)
|
|
|
|
updateSettings({localMute: mute});
|
2020-06-09 18:05:16 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
getSelectElement('videoselect').onchange = function(e) {
|
2020-05-05 17:50:16 +02:00
|
|
|
e.preventDefault();
|
2020-09-20 14:33:13 +02:00
|
|
|
if(!(this instanceof HTMLSelectElement))
|
|
|
|
throw new Error('Unexpected type for this');
|
2020-09-18 17:57:37 +02:00
|
|
|
updateSettings({video: this.value});
|
2021-05-06 23:39:27 +02:00
|
|
|
replaceCameraStream();
|
2020-05-10 21:18:07 +02:00
|
|
|
};
|
2020-05-05 17:50:16 +02:00
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
getSelectElement('audioselect').onchange = function(e) {
|
2020-05-05 17:50:16 +02:00
|
|
|
e.preventDefault();
|
2020-09-20 14:33:13 +02:00
|
|
|
if(!(this instanceof HTMLSelectElement))
|
|
|
|
throw new Error('Unexpected type for this');
|
2020-09-18 17:57:37 +02:00
|
|
|
updateSettings({audio: this.value});
|
2021-05-06 23:39:27 +02:00
|
|
|
replaceCameraStream();
|
2020-05-10 21:18:07 +02:00
|
|
|
};
|
2020-05-05 17:50:16 +02:00
|
|
|
|
2021-01-23 01:15:53 +01:00
|
|
|
getInputElement('mirrorbox').onchange = function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
if(!(this instanceof HTMLInputElement))
|
|
|
|
throw new Error('Unexpected type for this');
|
|
|
|
updateSettings({mirrorView: this.checked});
|
2021-05-06 23:39:27 +02:00
|
|
|
// no need to reopen the camera
|
|
|
|
replaceUpStreams('camera');
|
2021-01-23 01:15:53 +01:00
|
|
|
};
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
getInputElement('blackboardbox').onchange = function(e) {
|
2020-09-18 19:14:10 +02:00
|
|
|
e.preventDefault();
|
2020-09-20 14:33:13 +02:00
|
|
|
if(!(this instanceof HTMLInputElement))
|
|
|
|
throw new Error('Unexpected type for this');
|
2020-09-18 19:14:10 +02:00
|
|
|
updateSettings({blackboardMode: this.checked});
|
2021-05-06 23:39:27 +02:00
|
|
|
replaceCameraStream();
|
2020-12-26 17:28:44 +01:00
|
|
|
};
|
2020-09-18 19:14:10 +02:00
|
|
|
|
2022-02-21 18:07:49 +01:00
|
|
|
getInputElement('preprocessingbox').onchange = function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
if(!(this instanceof HTMLInputElement))
|
|
|
|
throw new Error('Unexpected type for this');
|
|
|
|
updateSettings({preprocessing: this.checked});
|
|
|
|
replaceCameraStream();
|
|
|
|
};
|
|
|
|
|
|
|
|
getInputElement('hqaudiobox').onchange = function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
if(!(this instanceof HTMLInputElement))
|
|
|
|
throw new Error('Unexpected type for this');
|
|
|
|
updateSettings({hqaudio: this.checked});
|
|
|
|
replaceCameraStream();
|
|
|
|
};
|
|
|
|
|
2020-06-09 18:05:16 +02:00
|
|
|
document.getElementById('mutebutton').onclick = function(e) {
|
|
|
|
e.preventDefault();
|
2020-09-18 17:57:37 +02:00
|
|
|
let localMute = getSettings().localMute;
|
|
|
|
localMute = !localMute;
|
2020-11-30 14:22:36 +01:00
|
|
|
setLocalMute(localMute, true);
|
2020-12-26 17:28:44 +01:00
|
|
|
};
|
2020-06-09 18:05:16 +02:00
|
|
|
|
2020-05-21 18:22:30 +02:00
|
|
|
document.getElementById('sharebutton').onclick = function(e) {
|
2020-04-24 19:38:21 +02:00
|
|
|
e.preventDefault();
|
2020-05-21 18:22:30 +02:00
|
|
|
addShareMedia();
|
2020-05-10 21:18:07 +02:00
|
|
|
};
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2021-01-13 13:57:05 +01:00
|
|
|
getSelectElement('filterselect').onchange = async function(e) {
|
|
|
|
if(!(this instanceof HTMLSelectElement))
|
|
|
|
throw new Error('Unexpected type for this');
|
|
|
|
updateSettings({filter: this.value});
|
2021-05-09 18:59:46 +02:00
|
|
|
let c = findUpMedia('camera');
|
|
|
|
if(c) {
|
|
|
|
let filter = (this.value && filters[this.value]) || null;
|
|
|
|
if(filter)
|
|
|
|
c.userdata.filterDefinition = filter;
|
|
|
|
else
|
|
|
|
delete c.userdata.filterDefinition;
|
|
|
|
replaceUpStream(c);
|
|
|
|
}
|
2021-01-13 13:57:05 +01:00
|
|
|
};
|
|
|
|
|
2020-08-26 18:32:01 +02:00
|
|
|
/** @returns {number} */
|
|
|
|
function getMaxVideoThroughput() {
|
2020-09-18 17:57:37 +02:00
|
|
|
let v = getSettings().send;
|
2020-08-26 18:32:01 +02:00
|
|
|
switch(v) {
|
|
|
|
case 'lowest':
|
|
|
|
return 150000;
|
|
|
|
case 'low':
|
|
|
|
return 300000;
|
|
|
|
case 'normal':
|
|
|
|
return 700000;
|
|
|
|
case 'unlimited':
|
|
|
|
return null;
|
|
|
|
default:
|
|
|
|
console.error('Unknown video quality', v);
|
|
|
|
return 700000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
getSelectElement('sendselect').onchange = async function(e) {
|
|
|
|
if(!(this instanceof HTMLSelectElement))
|
|
|
|
throw new Error('Unexpected type for this');
|
2020-09-18 17:57:37 +02:00
|
|
|
updateSettings({send: this.value});
|
2021-05-09 15:06:10 +02:00
|
|
|
await reconsiderSendParameters();
|
2020-12-26 17:28:44 +01:00
|
|
|
};
|
2020-08-26 18:32:01 +02:00
|
|
|
|
2021-05-08 21:25:22 +02:00
|
|
|
getSelectElement('simulcastselect').onchange = async function(e) {
|
|
|
|
if(!(this instanceof HTMLSelectElement))
|
|
|
|
throw new Error('Unexpected type for this');
|
|
|
|
updateSettings({simulcast: this.value});
|
2021-05-09 15:06:10 +02:00
|
|
|
await reconsiderSendParameters();
|
2021-05-08 21:25:22 +02:00
|
|
|
};
|
|
|
|
|
2021-04-28 17:00:50 +02:00
|
|
|
/**
|
|
|
|
* @param {string} what
|
|
|
|
* @returns {Object<string,Array<string>>}
|
|
|
|
*/
|
|
|
|
|
|
|
|
function mapRequest(what) {
|
|
|
|
switch(what) {
|
|
|
|
case '':
|
|
|
|
return {};
|
|
|
|
break;
|
|
|
|
case 'audio':
|
|
|
|
return {'': ['audio']};
|
|
|
|
break;
|
|
|
|
case 'screenshare':
|
|
|
|
return {screenshare: ['audio','video'], '': ['audio']};
|
|
|
|
break;
|
2021-04-29 17:03:25 +02:00
|
|
|
case 'everything-low':
|
|
|
|
return {'': ['audio','video-low']};
|
|
|
|
break;
|
2021-04-28 17:00:50 +02:00
|
|
|
case 'everything':
|
|
|
|
return {'': ['audio','video']}
|
|
|
|
break;
|
|
|
|
default:
|
2021-05-14 18:44:49 +02:00
|
|
|
throw new Error(`Unknown value ${what} in request`);
|
2021-04-28 17:00:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:44:49 +02:00
|
|
|
/**
|
|
|
|
* @param {string} what
|
|
|
|
* @param {string} label
|
|
|
|
* @returns {Array<string>}
|
|
|
|
*/
|
|
|
|
|
|
|
|
function mapRequestLabel(what, label) {
|
|
|
|
let r = mapRequest(what);
|
|
|
|
if(label in r)
|
|
|
|
return r[label];
|
|
|
|
else
|
|
|
|
return r[''];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
getSelectElement('requestselect').onchange = function(e) {
|
2020-05-09 19:39:34 +02:00
|
|
|
e.preventDefault();
|
2020-09-20 14:33:13 +02:00
|
|
|
if(!(this instanceof HTMLSelectElement))
|
|
|
|
throw new Error('Unexpected type for this');
|
2020-09-18 17:57:37 +02:00
|
|
|
updateSettings({request: this.value});
|
2021-04-28 17:00:50 +02:00
|
|
|
serverConnection.request(mapRequest(this.value));
|
2021-05-14 18:44:49 +02:00
|
|
|
reconsiderDownRate();
|
2020-05-10 21:18:07 +02:00
|
|
|
};
|
2020-05-09 19:39:34 +02:00
|
|
|
|
2020-09-14 17:15:21 +02:00
|
|
|
const activityDetectionInterval = 200;
|
2020-09-11 23:20:46 +02:00
|
|
|
const activityDetectionPeriod = 700;
|
|
|
|
const activityDetectionThreshold = 0.2;
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
getInputElement('activitybox').onchange = function(e) {
|
|
|
|
if(!(this instanceof HTMLInputElement))
|
|
|
|
throw new Error('Unexpected type for this');
|
2020-09-18 17:57:37 +02:00
|
|
|
updateSettings({activityDetection: this.checked});
|
2020-09-11 23:20:46 +02:00
|
|
|
for(let id in serverConnection.down) {
|
|
|
|
let c = serverConnection.down[id];
|
2020-09-14 17:15:21 +02:00
|
|
|
if(this.checked)
|
2020-09-11 23:20:46 +02:00
|
|
|
c.setStatsInterval(activityDetectionInterval);
|
|
|
|
else {
|
|
|
|
c.setStatsInterval(0);
|
2020-09-18 17:57:37 +02:00
|
|
|
setActive(c, false);
|
2020-09-11 23:20:46 +02:00
|
|
|
}
|
|
|
|
}
|
2020-12-26 17:28:44 +01:00
|
|
|
};
|
2020-09-11 23:20:46 +02:00
|
|
|
|
2022-09-16 19:10:01 +02:00
|
|
|
getInputElement('displayallbox').onchange = function(e) {
|
|
|
|
if(!(this instanceof HTMLInputElement))
|
|
|
|
throw new Error('Unexpected type for this');
|
|
|
|
updateSettings({displayAll: this.checked});
|
|
|
|
for(let id in serverConnection.down) {
|
|
|
|
let c = serverConnection.down[id];
|
|
|
|
let elt = document.getElementById('peer-' + c.localId);
|
|
|
|
showHideMedia(c, elt);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
/**
|
|
|
|
* @this {Stream}
|
|
|
|
* @param {Object<string,any>} stats
|
|
|
|
*/
|
2020-09-11 23:20:46 +02:00
|
|
|
function gotUpStats(stats) {
|
2020-08-11 17:09:31 +02:00
|
|
|
let c = this;
|
2020-04-26 19:21:01 +02:00
|
|
|
|
2021-04-29 17:03:25 +02:00
|
|
|
let values = [];
|
2020-06-12 15:42:44 +02:00
|
|
|
|
2021-04-29 17:03:25 +02:00
|
|
|
for(let id in stats) {
|
|
|
|
if(stats[id] && stats[id]['outbound-rtp']) {
|
|
|
|
let rate = stats[id]['outbound-rtp'].rate;
|
|
|
|
if(typeof rate === 'number') {
|
|
|
|
values.push(rate);
|
|
|
|
}
|
2020-06-12 15:42:44 +02:00
|
|
|
}
|
2021-04-29 17:03:25 +02:00
|
|
|
}
|
2020-06-12 15:42:44 +02:00
|
|
|
|
2021-04-29 17:03:25 +02:00
|
|
|
if(values.length === 0) {
|
|
|
|
setLabel(c, '');
|
|
|
|
} else {
|
|
|
|
values.sort((x,y) => x - y);
|
|
|
|
setLabel(c, values
|
|
|
|
.map(x => Math.round(x / 1000).toString())
|
|
|
|
.reduce((x, y) => x + '+' + y));
|
|
|
|
}
|
2020-04-26 19:21:01 +02:00
|
|
|
}
|
|
|
|
|
2020-09-11 23:20:46 +02:00
|
|
|
/**
|
|
|
|
* @param {Stream} c
|
|
|
|
* @param {boolean} value
|
|
|
|
*/
|
|
|
|
function setActive(c, value) {
|
2021-01-31 19:00:09 +01:00
|
|
|
let peer = document.getElementById('peer-' + c.localId);
|
2020-09-11 23:20:46 +02:00
|
|
|
if(value)
|
|
|
|
peer.classList.add('peer-active');
|
|
|
|
else
|
|
|
|
peer.classList.remove('peer-active');
|
|
|
|
}
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
/**
|
|
|
|
* @this {Stream}
|
|
|
|
* @param {Object<string,any>} stats
|
|
|
|
*/
|
2020-09-11 23:20:46 +02:00
|
|
|
function gotDownStats(stats) {
|
2020-09-20 14:33:13 +02:00
|
|
|
if(!getInputElement('activitybox').checked)
|
2020-09-11 23:20:46 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
let c = this;
|
|
|
|
|
|
|
|
let maxEnergy = 0;
|
|
|
|
|
|
|
|
c.pc.getReceivers().forEach(r => {
|
|
|
|
let tid = r.track && r.track.id;
|
|
|
|
let s = tid && stats[tid];
|
2023-08-26 07:29:15 +02:00
|
|
|
let energy = s && s['inbound-rtp'] && s['inbound-rtp'].audioEnergy;
|
2020-09-11 23:20:46 +02:00
|
|
|
if(typeof energy === 'number')
|
|
|
|
maxEnergy = Math.max(maxEnergy, energy);
|
|
|
|
});
|
|
|
|
|
|
|
|
// totalAudioEnergy is defined as the integral of the square of the
|
|
|
|
// volume, so square the threshold.
|
|
|
|
if(maxEnergy > activityDetectionThreshold * activityDetectionThreshold) {
|
|
|
|
c.userdata.lastVoiceActivity = Date.now();
|
|
|
|
setActive(c, true);
|
|
|
|
} else {
|
|
|
|
let last = c.userdata.lastVoiceActivity;
|
|
|
|
if(!last || Date.now() - last > activityDetectionPeriod)
|
|
|
|
setActive(c, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-12 16:34:52 +02:00
|
|
|
/**
|
2020-09-18 17:57:37 +02:00
|
|
|
* @param {HTMLSelectElement} select
|
|
|
|
* @param {string} label
|
|
|
|
* @param {string} [value]
|
2020-09-12 16:34:52 +02:00
|
|
|
*/
|
2020-05-05 17:50:16 +02:00
|
|
|
function addSelectOption(select, label, value) {
|
|
|
|
if(!value)
|
|
|
|
value = label;
|
|
|
|
for(let i = 0; i < select.children.length; i++) {
|
2020-09-20 14:33:13 +02:00
|
|
|
let child = select.children[i];
|
|
|
|
if(!(child instanceof HTMLOptionElement)) {
|
|
|
|
console.warn('Unexpected select child');
|
|
|
|
continue;
|
|
|
|
}
|
2020-09-18 17:57:37 +02:00
|
|
|
if(child.value === value) {
|
|
|
|
if(child.label !== label) {
|
|
|
|
child.label = label;
|
|
|
|
}
|
2020-05-05 17:50:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let option = document.createElement('option');
|
|
|
|
option.value = value;
|
|
|
|
option.textContent = label;
|
|
|
|
select.appendChild(option);
|
|
|
|
}
|
|
|
|
|
2020-09-18 17:57:37 +02:00
|
|
|
/**
|
|
|
|
* @param {HTMLSelectElement} select
|
|
|
|
* @param {string} value
|
|
|
|
*/
|
|
|
|
function selectOptionAvailable(select, value) {
|
|
|
|
let children = select.children;
|
|
|
|
for(let i = 0; i < children.length; i++) {
|
2020-09-20 14:33:13 +02:00
|
|
|
let child = select.children[i];
|
|
|
|
if(!(child instanceof HTMLOptionElement)) {
|
|
|
|
console.warn('Unexpected select child');
|
|
|
|
continue;
|
2020-09-18 17:57:37 +02:00
|
|
|
}
|
2020-09-20 14:33:13 +02:00
|
|
|
if(child.value === value)
|
|
|
|
return true;
|
2020-09-18 17:57:37 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {HTMLSelectElement} select
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function selectOptionDefault(select) {
|
|
|
|
/* First non-empty option. */
|
|
|
|
for(let i = 0; i < select.children.length; i++) {
|
2020-09-20 14:33:13 +02:00
|
|
|
let child = select.children[i];
|
|
|
|
if(!(child instanceof HTMLOptionElement)) {
|
|
|
|
console.warn('Unexpected select child');
|
|
|
|
continue;
|
|
|
|
}
|
2020-09-18 17:57:37 +02:00
|
|
|
if(child.value)
|
|
|
|
return child.value;
|
|
|
|
}
|
|
|
|
/* The empty option is always available. */
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* media names might not be available before we call getDisplayMedia. So
|
|
|
|
we call this twice, the second time to update the menu with user-readable
|
|
|
|
labels. */
|
2020-08-24 22:37:48 +02:00
|
|
|
/** @type {boolean} */
|
2020-05-05 17:50:16 +02:00
|
|
|
let mediaChoicesDone = false;
|
|
|
|
|
2020-09-18 17:57:37 +02:00
|
|
|
/**
|
|
|
|
* @param{boolean} done
|
|
|
|
*/
|
|
|
|
async function setMediaChoices(done) {
|
2020-05-05 17:50:16 +02:00
|
|
|
if(mediaChoicesDone)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let devices = [];
|
|
|
|
try {
|
2023-12-08 18:46:23 +01:00
|
|
|
if('mediaDevices' in navigator)
|
|
|
|
devices = await navigator.mediaDevices.enumerateDevices();
|
2020-05-05 17:50:16 +02:00
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let cn = 1, mn = 1;
|
|
|
|
|
|
|
|
devices.forEach(d => {
|
|
|
|
let label = d.label;
|
|
|
|
if(d.kind === 'videoinput') {
|
|
|
|
if(!label)
|
|
|
|
label = `Camera ${cn}`;
|
2020-09-20 14:33:13 +02:00
|
|
|
addSelectOption(getSelectElement('videoselect'),
|
2020-05-05 17:50:16 +02:00
|
|
|
label, d.deviceId);
|
|
|
|
cn++;
|
|
|
|
} else if(d.kind === 'audioinput') {
|
|
|
|
if(!label)
|
|
|
|
label = `Microphone ${mn}`;
|
2020-09-20 14:33:13 +02:00
|
|
|
addSelectOption(getSelectElement('audioselect'),
|
2020-05-05 17:50:16 +02:00
|
|
|
label, d.deviceId);
|
|
|
|
mn++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-09-18 17:57:37 +02:00
|
|
|
mediaChoicesDone = done;
|
2020-05-05 17:50:16 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 17:57:37 +02:00
|
|
|
|
2020-08-13 20:55:13 +02:00
|
|
|
/**
|
2021-01-31 19:00:09 +01:00
|
|
|
* @param {string} [localId]
|
2020-08-13 20:55:13 +02:00
|
|
|
*/
|
2021-01-31 19:00:09 +01:00
|
|
|
function newUpStream(localId) {
|
|
|
|
let c = serverConnection.newUpStream(localId);
|
2020-08-11 17:09:31 +02:00
|
|
|
c.onstatus = function(status) {
|
|
|
|
setMediaStatus(c);
|
2020-12-26 17:28:44 +01:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
c.onerror = function(e) {
|
|
|
|
console.error(e);
|
|
|
|
displayError(e);
|
2020-12-26 17:28:44 +01:00
|
|
|
};
|
2020-08-11 17:09:31 +02:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2020-08-26 18:32:01 +02:00
|
|
|
/**
|
2021-05-09 15:06:10 +02:00
|
|
|
* Sets an up stream's video throughput and simulcast parameters.
|
|
|
|
*
|
2020-08-26 18:32:01 +02:00
|
|
|
* @param {Stream} c
|
2021-05-09 15:06:10 +02:00
|
|
|
* @param {number} bps
|
|
|
|
* @param {boolean} simulcast
|
2020-08-26 18:32:01 +02:00
|
|
|
*/
|
2021-05-09 15:06:10 +02:00
|
|
|
async function setSendParameters(c, bps, simulcast) {
|
|
|
|
if(!c.up)
|
|
|
|
throw new Error('Setting throughput of down stream');
|
2023-07-14 23:59:06 +02:00
|
|
|
if(c.label === 'screenshare')
|
|
|
|
simulcast = false;
|
2020-08-26 18:32:01 +02:00
|
|
|
let senders = c.pc.getSenders();
|
|
|
|
for(let i = 0; i < senders.length; i++) {
|
|
|
|
let s = senders[i];
|
|
|
|
if(!s.track || s.track.kind !== 'video')
|
|
|
|
continue;
|
|
|
|
let p = s.getParameters();
|
2022-03-20 21:52:24 +01:00
|
|
|
if((!p.encodings ||
|
|
|
|
!simulcast && p.encodings.length != 1) ||
|
2021-04-29 17:03:25 +02:00
|
|
|
(simulcast && p.encodings.length != 2)) {
|
|
|
|
await replaceUpStream(c);
|
|
|
|
return;
|
|
|
|
}
|
2020-08-26 18:32:01 +02:00
|
|
|
p.encodings.forEach(e => {
|
2021-04-29 17:03:25 +02:00
|
|
|
if(!e.rid || e.rid === 'h')
|
|
|
|
e.maxBitrate = bps || unlimitedRate;
|
2020-08-26 18:32:01 +02:00
|
|
|
});
|
2021-04-29 17:03:25 +02:00
|
|
|
await s.setParameters(p);
|
2020-08-26 18:32:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-09 15:06:10 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 13:29:45 +01:00
|
|
|
/**
|
|
|
|
* @typedef {Object} filterDefinition
|
|
|
|
* @property {string} [description]
|
2024-12-12 14:18:01 +01:00
|
|
|
* @property {(this: filterDefinition) => Promise<boolean>} [predicate]
|
2024-11-16 23:59:14 +01:00
|
|
|
* @property {(this: Filter) => Promise<void>} [init]
|
|
|
|
* @property {(this: Filter) => Promise<void>} [cleanup]
|
2024-11-17 15:28:42 +01:00
|
|
|
* @property {(this: Filter, src: HTMLVideoElement, ctx: CanvasRenderingContext2D) => Promise<boolean>} draw
|
2021-01-13 13:29:45 +01:00
|
|
|
*/
|
|
|
|
|
2021-01-12 01:25:35 +01:00
|
|
|
/**
|
2021-01-12 03:47:25 +01:00
|
|
|
* @param {MediaStream} stream
|
2021-01-13 13:29:45 +01:00
|
|
|
* @param {filterDefinition} definition
|
2021-01-12 01:25:35 +01:00
|
|
|
* @constructor
|
|
|
|
*/
|
2021-01-13 13:29:45 +01:00
|
|
|
function Filter(stream, definition) {
|
|
|
|
/** @ts-ignore */
|
|
|
|
if(!HTMLCanvasElement.prototype.captureStream) {
|
|
|
|
throw new Error('Filters are not supported on this platform');
|
|
|
|
}
|
|
|
|
|
2021-01-12 01:25:35 +01:00
|
|
|
/** @type {MediaStream} */
|
2021-01-12 03:36:51 +01:00
|
|
|
this.inputStream = stream;
|
2021-01-13 13:29:45 +01:00
|
|
|
/** @type {filterDefinition} */
|
|
|
|
this.definition = definition;
|
2021-01-12 03:36:51 +01:00
|
|
|
/** @type {number} */
|
|
|
|
this.frameRate = 30;
|
2021-01-12 01:25:35 +01:00
|
|
|
/** @type {HTMLVideoElement} */
|
|
|
|
this.video = document.createElement('video');
|
|
|
|
/** @type {HTMLCanvasElement} */
|
|
|
|
this.canvas = document.createElement('canvas');
|
2021-01-13 13:29:45 +01:00
|
|
|
/** @type {any} */
|
2024-11-17 15:28:42 +01:00
|
|
|
this.context = this.canvas.getContext('2d');
|
2021-01-12 01:25:35 +01:00
|
|
|
/** @type {MediaStream} */
|
|
|
|
this.captureStream = null;
|
|
|
|
/** @type {MediaStream} */
|
|
|
|
this.outputStream = null;
|
|
|
|
/** @type {number} */
|
|
|
|
this.timer = null;
|
2021-01-12 03:36:51 +01:00
|
|
|
/** @type {number} */
|
|
|
|
this.count = 0;
|
2021-01-13 13:29:45 +01:00
|
|
|
/** @type {boolean} */
|
|
|
|
this.fixedFramerate = false;
|
2021-01-13 18:10:16 +01:00
|
|
|
/** @type {Object} */
|
|
|
|
this.userdata = {}
|
2022-01-25 01:22:59 +01:00
|
|
|
/** @type {MediaStream} */
|
2021-01-12 01:25:35 +01:00
|
|
|
this.captureStream = this.canvas.captureStream(0);
|
2024-11-16 23:59:14 +01:00
|
|
|
/** @type {boolean} */
|
|
|
|
this.busy = false;
|
2024-11-16 23:49:49 +01:00
|
|
|
}
|
2022-01-25 01:22:59 +01:00
|
|
|
|
2024-11-16 23:59:14 +01:00
|
|
|
Filter.prototype.start = async function() {
|
2021-01-13 13:29:45 +01:00
|
|
|
/** @ts-ignore */
|
|
|
|
if(!this.captureStream.getTracks()[0].requestFrame) {
|
|
|
|
console.warn('captureFrame not supported, using fixed framerate');
|
|
|
|
/** @ts-ignore */
|
|
|
|
this.captureStream = this.canvas.captureStream(this.frameRate);
|
|
|
|
this.fixedFramerate = true;
|
|
|
|
}
|
2021-01-12 01:25:35 +01:00
|
|
|
|
|
|
|
this.outputStream = new MediaStream();
|
|
|
|
this.outputStream.addTrack(this.captureStream.getTracks()[0]);
|
2021-01-12 03:36:51 +01:00
|
|
|
this.inputStream.getTracks().forEach(t => {
|
2021-01-12 01:25:35 +01:00
|
|
|
t.onended = e => this.stop();
|
|
|
|
if(t.kind != 'video')
|
|
|
|
this.outputStream.addTrack(t);
|
|
|
|
});
|
2024-11-16 23:49:49 +01:00
|
|
|
this.video.srcObject = this.inputStream;
|
2021-01-12 01:25:35 +01:00
|
|
|
this.video.muted = true;
|
|
|
|
this.video.play();
|
2021-01-13 18:10:16 +01:00
|
|
|
if(this.definition.init)
|
2024-11-16 23:59:14 +01:00
|
|
|
await this.definition.init.call(this);
|
2021-01-12 03:36:51 +01:00
|
|
|
this.timer = setInterval(() => this.draw(), 1000 / this.frameRate);
|
2021-01-12 01:25:35 +01:00
|
|
|
}
|
|
|
|
|
2024-11-16 23:59:14 +01:00
|
|
|
Filter.prototype.draw = async function() {
|
2024-11-17 15:48:12 +01:00
|
|
|
if(this.video.videoWidth === 0 && this.video.videoHeight === 0) {
|
|
|
|
// video not started yet
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-30 07:28:20 +02:00
|
|
|
// check framerate every 30 frames
|
2021-01-12 03:36:51 +01:00
|
|
|
if((this.count % 30) === 0) {
|
|
|
|
let frameRate = 0;
|
|
|
|
this.inputStream.getTracks().forEach(t => {
|
|
|
|
if(t.kind === 'video') {
|
|
|
|
let r = t.getSettings().frameRate;
|
|
|
|
if(r)
|
|
|
|
frameRate = r;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if(frameRate && frameRate != this.frameRate) {
|
2021-01-13 20:15:39 +01:00
|
|
|
clearInterval(this.timer);
|
2021-01-12 03:36:51 +01:00
|
|
|
this.timer = setInterval(() => this.draw(), 1000 / this.frameRate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-16 23:59:14 +01:00
|
|
|
if(this.busy) {
|
|
|
|
// drop frame
|
|
|
|
return;
|
2021-01-12 03:47:25 +01:00
|
|
|
}
|
2021-01-13 17:07:40 +01:00
|
|
|
|
2024-11-16 23:59:14 +01:00
|
|
|
try {
|
|
|
|
this.busy = true;
|
|
|
|
let ok = false;
|
|
|
|
try {
|
|
|
|
ok = await this.definition.draw.call(
|
|
|
|
this, this.video, this.context,
|
|
|
|
);
|
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
if(ok && !this.fixedFramerate) {
|
|
|
|
/** @ts-ignore */
|
|
|
|
this.captureStream.getTracks()[0].requestFrame();
|
|
|
|
}
|
|
|
|
this.count++;
|
|
|
|
} finally {
|
|
|
|
this.busy = false;
|
|
|
|
}
|
2021-01-12 01:25:35 +01:00
|
|
|
};
|
|
|
|
|
2024-11-16 23:59:14 +01:00
|
|
|
Filter.prototype.stop = async function() {
|
2021-01-12 01:25:35 +01:00
|
|
|
if(!this.timer)
|
|
|
|
return;
|
|
|
|
this.captureStream.getTracks()[0].stop();
|
2021-01-13 20:15:39 +01:00
|
|
|
clearInterval(this.timer);
|
2021-01-12 01:25:35 +01:00
|
|
|
this.timer = null;
|
2021-01-13 18:10:16 +01:00
|
|
|
if(this.definition.cleanup)
|
2024-11-16 23:59:14 +01:00
|
|
|
await this.definition.cleanup.call(this);
|
2021-01-12 01:25:35 +01:00
|
|
|
};
|
|
|
|
|
2021-01-12 03:47:25 +01:00
|
|
|
/**
|
2021-05-06 23:39:27 +02:00
|
|
|
* Removes any filter set on c.
|
|
|
|
*
|
2021-01-13 13:29:45 +01:00
|
|
|
* @param {Stream} c
|
2021-05-06 23:39:27 +02:00
|
|
|
*/
|
2024-11-16 23:59:14 +01:00
|
|
|
async function removeFilter(c) {
|
2021-05-06 23:39:27 +02:00
|
|
|
let old = c.userdata.filter;
|
|
|
|
if(!old)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!(old instanceof Filter))
|
|
|
|
throw new Error('userdata.filter is not a filter');
|
|
|
|
|
2022-03-25 17:06:13 +01:00
|
|
|
c.setStream(old.inputStream);
|
2024-11-16 23:59:14 +01:00
|
|
|
await old.stop();
|
2021-05-06 23:39:27 +02:00
|
|
|
c.userdata.filter = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the filter described by c.userdata.filterDefinition on c.
|
|
|
|
*
|
|
|
|
* @param {Stream} c
|
|
|
|
*/
|
2024-11-16 23:59:14 +01:00
|
|
|
async function setFilter(c) {
|
|
|
|
await removeFilter(c);
|
2021-01-13 13:29:45 +01:00
|
|
|
|
2021-05-06 23:39:27 +02:00
|
|
|
if(!c.userdata.filterDefinition)
|
|
|
|
return;
|
2021-01-13 13:29:45 +01:00
|
|
|
|
2021-05-06 23:39:27 +02:00
|
|
|
let filter = new Filter(c.stream, c.userdata.filterDefinition);
|
2024-11-16 23:59:14 +01:00
|
|
|
await filter.start();
|
2022-03-25 17:06:13 +01:00
|
|
|
c.setStream(filter.outputStream);
|
2021-05-06 23:39:27 +02:00
|
|
|
c.userdata.filter = filter;
|
2021-01-13 13:29:45 +01:00
|
|
|
}
|
|
|
|
|
2024-12-14 14:48:50 +01:00
|
|
|
/**
|
|
|
|
* Sends a message to a worker, then waits for a reply.
|
|
|
|
*
|
|
|
|
* @param {Worker} worker
|
|
|
|
* @param {any} message
|
|
|
|
* @param {any[]} [transfer]
|
|
|
|
*/
|
|
|
|
async function workerSendReceive(worker, message, transfer) {
|
|
|
|
let p = new Promise((resolve, reject) => {
|
|
|
|
worker.onmessage = e => {
|
|
|
|
if(e && e.data) {
|
|
|
|
if(e.data instanceof Error)
|
|
|
|
reject(e.data);
|
|
|
|
else
|
|
|
|
resolve(e.data);
|
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
worker.postMessage(message, transfer);
|
|
|
|
return await p
|
|
|
|
}
|
|
|
|
|
2021-01-13 13:29:45 +01:00
|
|
|
/**
|
|
|
|
* @type {Object.<string,filterDefinition>}
|
2021-01-12 03:47:25 +01:00
|
|
|
*/
|
2021-01-12 01:25:35 +01:00
|
|
|
let filters = {
|
2021-01-13 13:29:45 +01:00
|
|
|
'mirror-h': {
|
|
|
|
description: "Horizontal mirror",
|
2024-11-16 23:59:14 +01:00
|
|
|
draw: async function(src, ctx) {
|
2021-01-13 13:29:45 +01:00
|
|
|
if(!(ctx instanceof CanvasRenderingContext2D))
|
|
|
|
throw new Error('bad context type');
|
2024-11-17 15:48:12 +01:00
|
|
|
if(ctx.canvas.width !== src.videoWidth ||
|
|
|
|
ctx.canvas.height !== src.videoHeight) {
|
|
|
|
ctx.canvas.width = src.videoWidth;
|
|
|
|
ctx.canvas.height = src.videoHeight;
|
2021-01-13 17:07:40 +01:00
|
|
|
}
|
2021-01-13 13:29:45 +01:00
|
|
|
ctx.scale(-1, 1);
|
2024-11-17 15:48:12 +01:00
|
|
|
ctx.drawImage(src, -src.videoWidth, 0);
|
2021-01-13 14:36:29 +01:00
|
|
|
ctx.resetTransform();
|
2021-01-13 13:29:45 +01:00
|
|
|
return true;
|
|
|
|
},
|
2021-01-12 01:25:35 +01:00
|
|
|
},
|
2021-01-13 13:29:45 +01:00
|
|
|
'mirror-v': {
|
|
|
|
description: "Vertical mirror",
|
2024-11-16 23:59:14 +01:00
|
|
|
draw: async function(src, ctx) {
|
2021-01-13 13:29:45 +01:00
|
|
|
if(!(ctx instanceof CanvasRenderingContext2D))
|
|
|
|
throw new Error('bad context type');
|
2024-11-17 15:48:12 +01:00
|
|
|
if(ctx.canvas.width !== src.videoWidth ||
|
|
|
|
ctx.canvas.height !== src.videoHeight) {
|
|
|
|
ctx.canvas.width = src.videoWidth;
|
|
|
|
ctx.canvas.height = src.videoHeight;
|
2021-01-13 17:07:40 +01:00
|
|
|
}
|
2021-01-13 13:29:45 +01:00
|
|
|
ctx.scale(1, -1);
|
2024-11-17 15:48:12 +01:00
|
|
|
ctx.drawImage(src, 0, -src.videoHeight);
|
2021-01-13 14:36:29 +01:00
|
|
|
ctx.resetTransform();
|
2021-01-13 13:29:45 +01:00
|
|
|
return true;
|
|
|
|
},
|
2021-01-12 01:25:35 +01:00
|
|
|
},
|
2024-11-17 15:28:42 +01:00
|
|
|
'background-blur': {
|
|
|
|
description: 'Background blur',
|
2024-12-12 14:42:17 +01:00
|
|
|
predicate: async function() {
|
|
|
|
let r = await fetch('/third-party/tasks-vision/vision_bundle.mjs', {
|
|
|
|
method: 'HEAD',
|
|
|
|
});
|
|
|
|
if(!r.ok && r.status !== 404)
|
|
|
|
console.warn(
|
|
|
|
`Fetch vision_bundle.mjs: ${r.status} ${r.statusText}`,
|
|
|
|
);
|
|
|
|
return r.ok;
|
|
|
|
},
|
2024-11-17 15:28:42 +01:00
|
|
|
init: async function(ctx) {
|
|
|
|
if(!(this instanceof Filter))
|
|
|
|
throw new Error('Bad type for this');
|
|
|
|
if(this.userdata.worker)
|
|
|
|
throw new Error("Worker already running (this shouldn't happen)")
|
|
|
|
this.userdata.worker = new Worker('/background-blur-worker.js');
|
2024-12-14 14:48:50 +01:00
|
|
|
await workerSendReceive(this.userdata.worker, {
|
|
|
|
model: '/third-party/tasks-vision/models/selfie_segmenter.tflite',
|
|
|
|
});
|
2024-11-17 15:28:42 +01:00
|
|
|
},
|
|
|
|
cleanup: async function() {
|
|
|
|
if(this.userdata.worker.onmessage) {
|
|
|
|
this.userdata.worker.onmessage(null);
|
|
|
|
}
|
|
|
|
this.userdata.worker.terminate();
|
|
|
|
this.userdata.worker = null;
|
|
|
|
},
|
|
|
|
draw: async function(src, ctx) {
|
|
|
|
let bitmap = await createImageBitmap(src);
|
2024-12-13 15:54:52 +01:00
|
|
|
try {
|
2024-12-14 14:48:50 +01:00
|
|
|
let result = await workerSendReceive(this.userdata.worker, {
|
2024-12-13 15:54:52 +01:00
|
|
|
bitmap: bitmap,
|
|
|
|
timestamp: performance.now(),
|
|
|
|
}, [bitmap]);
|
|
|
|
|
|
|
|
if(!result)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
let mask = result.mask;
|
|
|
|
bitmap = result.bitmap;
|
|
|
|
|
|
|
|
if(ctx.canvas.width !== src.videoWidth ||
|
|
|
|
ctx.canvas.height !== src.videoHeight) {
|
|
|
|
ctx.canvas.width = src.videoWidth;
|
|
|
|
ctx.canvas.height = src.videoHeight;
|
|
|
|
}
|
|
|
|
|
2024-12-14 02:13:59 +01:00
|
|
|
// set the alpha mask, background is opaque
|
2024-12-13 15:54:52 +01:00
|
|
|
ctx.globalCompositeOperation = 'copy';
|
2024-12-14 02:13:59 +01:00
|
|
|
ctx.filter = 'none';
|
2024-12-13 15:54:52 +01:00
|
|
|
ctx.drawImage(mask, 0, 0);
|
2024-12-14 02:13:59 +01:00
|
|
|
|
|
|
|
// rather than blurring the original image, we first mask
|
|
|
|
// the background then blur, this avoids a halo effect
|
2024-12-13 15:54:52 +01:00
|
|
|
ctx.globalCompositeOperation = 'source-in';
|
2024-12-14 02:13:59 +01:00
|
|
|
ctx.filter = 'none';
|
2024-12-13 15:54:52 +01:00
|
|
|
ctx.drawImage(result.bitmap, 0, 0);
|
2024-12-14 02:13:59 +01:00
|
|
|
ctx.globalCompositeOperation = 'copy';
|
|
|
|
ctx.filter = `blur(${src.videoWidth / 48}px)`;
|
|
|
|
ctx.drawImage(ctx.canvas, 0, 0);
|
|
|
|
|
|
|
|
// now draw the foreground
|
2024-12-13 15:54:52 +01:00
|
|
|
ctx.globalCompositeOperation = 'destination-atop';
|
|
|
|
ctx.filter = 'none';
|
|
|
|
ctx.drawImage(result.bitmap, 0, 0);
|
|
|
|
ctx.globalCompositeOperation = 'source-over';
|
|
|
|
|
|
|
|
mask.close();
|
|
|
|
} finally {
|
|
|
|
bitmap.close();
|
2024-11-17 15:28:42 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
2021-01-12 01:25:35 +01:00
|
|
|
};
|
|
|
|
|
2024-12-12 14:18:01 +01:00
|
|
|
async function addFilters() {
|
2021-01-13 13:57:05 +01:00
|
|
|
for(let name in filters) {
|
|
|
|
let f = filters[name];
|
2024-12-12 14:18:01 +01:00
|
|
|
if(f.predicate) {
|
|
|
|
if(!(await f.predicate.call(f)))
|
|
|
|
continue;
|
|
|
|
}
|
2021-01-13 13:57:05 +01:00
|
|
|
let d = f.description || name;
|
|
|
|
addSelectOption(getSelectElement('filterselect'), d, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 17:03:25 +02:00
|
|
|
const unlimitedRate = 1000000000;
|
|
|
|
const simulcastRate = 100000;
|
2022-02-21 18:07:49 +01:00
|
|
|
const hqAudioRate = 128000;
|
2021-04-29 17:03:25 +02:00
|
|
|
|
|
|
|
/**
|
2021-05-09 15:06:10 +02:00
|
|
|
* Decide whether we want to send simulcast.
|
|
|
|
*
|
2021-04-29 17:03:25 +02:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function doSimulcast() {
|
2021-05-08 21:25:22 +02:00
|
|
|
switch(getSettings().simulcast) {
|
|
|
|
case 'on':
|
|
|
|
return true;
|
|
|
|
case 'off':
|
2021-04-29 17:03:25 +02:00
|
|
|
return false;
|
2021-05-08 21:25:22 +02:00
|
|
|
default:
|
2021-12-02 00:08:48 +01:00
|
|
|
let count = 0;
|
|
|
|
for(let n in serverConnection.users) {
|
|
|
|
if(!serverConnection.users[n].permissions["system"]) {
|
|
|
|
count++;
|
|
|
|
if(count > 2)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(count <= 2)
|
2021-05-09 15:06:10 +02:00
|
|
|
return false;
|
2021-05-08 21:25:22 +02:00
|
|
|
let bps = getMaxVideoThroughput();
|
|
|
|
return bps <= 0 || bps >= 2 * simulcastRate;
|
|
|
|
}
|
2021-04-29 17:03:25 +02:00
|
|
|
}
|
|
|
|
|
2021-04-29 17:03:25 +02:00
|
|
|
/**
|
2021-05-06 23:39:27 +02:00
|
|
|
* Sets up c to send the given stream. Some extra parameters are stored
|
|
|
|
* in c.userdata.
|
|
|
|
*
|
2021-04-29 17:03:25 +02:00
|
|
|
* @param {Stream} c
|
|
|
|
* @param {MediaStream} stream
|
|
|
|
*/
|
2021-04-29 17:03:25 +02:00
|
|
|
|
2024-11-16 23:59:14 +01:00
|
|
|
async function setUpStream(c, stream) {
|
2021-05-06 23:39:27 +02:00
|
|
|
if(c.stream != null)
|
|
|
|
throw new Error("Setting nonempty stream");
|
|
|
|
|
2022-03-25 17:06:13 +01:00
|
|
|
c.setStream(stream);
|
2021-05-06 23:39:27 +02:00
|
|
|
|
2024-12-14 12:20:41 +01:00
|
|
|
// set up the handler early, in case setFilter fails.
|
2024-11-16 23:59:14 +01:00
|
|
|
c.onclose = async replace => {
|
|
|
|
await removeFilter(c);
|
2021-05-06 23:39:27 +02:00
|
|
|
if(!replace) {
|
|
|
|
stopStream(c.stream);
|
|
|
|
if(c.userdata.onclose)
|
|
|
|
c.userdata.onclose.call(c);
|
|
|
|
delMedia(c.localId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-14 12:20:41 +01:00
|
|
|
await setFilter(c);
|
|
|
|
|
2021-05-06 23:39:27 +02:00
|
|
|
/**
|
|
|
|
* @param {MediaStreamTrack} t
|
|
|
|
*/
|
|
|
|
function addUpTrack(t) {
|
2022-02-21 18:07:49 +01:00
|
|
|
let settings = getSettings();
|
2021-05-06 23:39:27 +02:00
|
|
|
if(c.label === 'camera') {
|
|
|
|
if(t.kind == 'audio') {
|
|
|
|
if(settings.localMute)
|
|
|
|
t.enabled = false;
|
|
|
|
} else if(t.kind == 'video') {
|
|
|
|
if(settings.blackboardMode) {
|
|
|
|
t.contentHint = 'detail';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.onended = e => {
|
|
|
|
stream.onaddtrack = null;
|
|
|
|
stream.onremovetrack = null;
|
|
|
|
c.close();
|
|
|
|
};
|
|
|
|
|
2021-04-29 17:03:25 +02:00
|
|
|
let encodings = [];
|
2023-07-14 23:59:06 +02:00
|
|
|
let simulcast = c.label !== 'screenshare' && doSimulcast();
|
2021-05-06 23:39:27 +02:00
|
|
|
if(t.kind === 'video') {
|
|
|
|
let bps = getMaxVideoThroughput();
|
2022-03-21 19:19:19 +01:00
|
|
|
// Firefox doesn't like us setting the RID if we're not
|
|
|
|
// simulcasting.
|
2023-07-14 23:59:06 +02:00
|
|
|
if(simulcast) {
|
2022-03-21 19:19:19 +01:00
|
|
|
encodings.push({
|
|
|
|
rid: 'h',
|
|
|
|
maxBitrate: bps || unlimitedRate,
|
|
|
|
});
|
2021-04-29 17:03:25 +02:00
|
|
|
encodings.push({
|
|
|
|
rid: 'l',
|
|
|
|
scaleResolutionDownBy: 2,
|
|
|
|
maxBitrate: simulcastRate,
|
|
|
|
});
|
2022-03-21 19:19:19 +01:00
|
|
|
} else {
|
|
|
|
encodings.push({
|
|
|
|
maxBitrate: bps || unlimitedRate,
|
|
|
|
});
|
|
|
|
}
|
2022-02-21 17:41:49 +01:00
|
|
|
} else {
|
2022-04-15 00:36:47 +02:00
|
|
|
if(settings.hqaudio) {
|
2022-02-21 17:41:49 +01:00
|
|
|
encodings.push({
|
2022-02-21 18:07:49 +01:00
|
|
|
maxBitrate: hqAudioRate,
|
2022-02-21 17:41:49 +01:00
|
|
|
});
|
|
|
|
}
|
2021-05-06 23:39:27 +02:00
|
|
|
}
|
2021-05-10 02:53:22 +02:00
|
|
|
let tr = c.pc.addTransceiver(t, {
|
2021-05-06 23:39:27 +02:00
|
|
|
direction: 'sendonly',
|
|
|
|
streams: [stream],
|
|
|
|
sendEncodings: encodings,
|
|
|
|
});
|
2023-04-13 22:26:41 +02:00
|
|
|
|
|
|
|
// Firefox before 110 does not implement sendEncodings, and
|
|
|
|
// requires this hack, which throws an exception on Chromium.
|
|
|
|
try {
|
|
|
|
let p = tr.sender.getParameters();
|
|
|
|
if(!p.encodings) {
|
|
|
|
p.encodings = encodings;
|
|
|
|
tr.sender.setParameters(p);
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
}
|
2021-05-06 23:39:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// c.stream might be different from stream if there's a filter
|
|
|
|
c.stream.getTracks().forEach(addUpTrack);
|
|
|
|
|
|
|
|
stream.onaddtrack = function(e) {
|
|
|
|
addUpTrack(e.track);
|
|
|
|
};
|
|
|
|
|
|
|
|
stream.onremovetrack = function(e) {
|
|
|
|
let t = e.track;
|
|
|
|
|
|
|
|
/** @type {RTCRtpSender} */
|
|
|
|
let sender;
|
|
|
|
c.pc.getSenders().forEach(s => {
|
|
|
|
if(s.track === t)
|
|
|
|
sender = s;
|
|
|
|
});
|
|
|
|
if(sender) {
|
|
|
|
c.pc.removeTrack(sender);
|
|
|
|
} else {
|
|
|
|
console.warn('Removing unknown track');
|
|
|
|
}
|
|
|
|
|
|
|
|
let found = false;
|
|
|
|
c.pc.getSenders().forEach(s => {
|
|
|
|
if(s.track)
|
|
|
|
found = true;
|
|
|
|
});
|
|
|
|
if(!found) {
|
|
|
|
stream.onaddtrack = null;
|
|
|
|
stream.onremovetrack = null;
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
c.onstats = gotUpStats;
|
|
|
|
c.setStatsInterval(2000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces c with a freshly created stream, duplicating any relevant
|
|
|
|
* parameters in c.userdata.
|
|
|
|
*
|
|
|
|
* @param {Stream} c
|
|
|
|
* @returns {Promise<Stream>}
|
|
|
|
*/
|
|
|
|
async function replaceUpStream(c) {
|
2024-11-16 23:59:14 +01:00
|
|
|
await removeFilter(c);
|
2021-05-06 23:39:27 +02:00
|
|
|
let cn = newUpStream(c.localId);
|
|
|
|
cn.label = c.label;
|
|
|
|
if(c.userdata.filterDefinition)
|
|
|
|
cn.userdata.filterDefinition = c.userdata.filterDefinition;
|
|
|
|
if(c.userdata.onclose)
|
|
|
|
cn.userdata.onclose = c.userdata.onclose;
|
|
|
|
let media = /** @type{HTMLVideoElement} */
|
|
|
|
(document.getElementById('media-' + c.localId));
|
2024-12-14 12:20:41 +01:00
|
|
|
try {
|
|
|
|
await setUpStream(cn, c.stream);
|
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
|
|
|
displayError(e);
|
|
|
|
cn.close();
|
|
|
|
c.close();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-09-16 19:13:55 +02:00
|
|
|
await setMedia(cn,
|
2021-05-09 18:59:46 +02:00
|
|
|
cn.label == 'camera' && getSettings().mirrorView,
|
|
|
|
cn.label == 'video' && media);
|
2024-12-14 12:20:41 +01:00
|
|
|
|
2021-05-06 23:39:27 +02:00
|
|
|
return cn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces all up streams with the given label. If label is null,
|
|
|
|
* replaces all up stream.
|
|
|
|
*
|
|
|
|
* @param {string} label
|
|
|
|
*/
|
|
|
|
async function replaceUpStreams(label) {
|
|
|
|
let promises = [];
|
|
|
|
for(let id in serverConnection.up) {
|
|
|
|
let c = serverConnection.up[id];
|
|
|
|
if(label && c.label !== label)
|
|
|
|
continue
|
|
|
|
promises.push(replaceUpStream(c));
|
|
|
|
}
|
|
|
|
await Promise.all(promises);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes and reopens the camera then replaces the camera stream.
|
|
|
|
*/
|
|
|
|
function replaceCameraStream() {
|
|
|
|
let c = findUpMedia('camera');
|
|
|
|
if(c)
|
|
|
|
addLocalMedia(c.localId);
|
2021-04-29 17:03:25 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 20:55:13 +02:00
|
|
|
/**
|
2021-01-31 19:00:09 +01:00
|
|
|
* @param {string} [localId]
|
2020-08-13 20:55:13 +02:00
|
|
|
*/
|
2021-01-31 19:00:09 +01:00
|
|
|
async function addLocalMedia(localId) {
|
2020-09-18 17:57:37 +02:00
|
|
|
let settings = getSettings();
|
|
|
|
|
2023-11-04 23:56:47 +01:00
|
|
|
/** @type{boolean|MediaTrackConstraints} */
|
2020-09-18 17:57:37 +02:00
|
|
|
let audio = settings.audio ? {deviceId: settings.audio} : false;
|
2023-11-04 23:56:47 +01:00
|
|
|
/** @type{boolean|MediaTrackConstraints} */
|
2020-11-27 11:13:48 +01:00
|
|
|
let video = settings.video ? {deviceId: settings.video} : false;
|
2020-05-05 17:50:16 +02:00
|
|
|
|
2020-09-18 19:14:10 +02:00
|
|
|
if(video) {
|
2020-12-14 19:31:42 +01:00
|
|
|
let resolution = settings.resolution;
|
|
|
|
if(resolution) {
|
|
|
|
video.width = { ideal: resolution[0] };
|
|
|
|
video.height = { ideal: resolution[1] };
|
|
|
|
} else if(settings.blackboardMode) {
|
2020-09-18 19:14:10 +02:00
|
|
|
video.width = { min: 640, ideal: 1920 };
|
|
|
|
video.height = { min: 400, ideal: 1080 };
|
2023-11-04 22:58:15 +01:00
|
|
|
} else {
|
2023-11-04 23:56:47 +01:00
|
|
|
video.aspectRatio = { ideal: 4/3 };
|
2020-09-18 19:14:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 18:07:49 +01:00
|
|
|
if(audio) {
|
|
|
|
if(!settings.preprocessing) {
|
|
|
|
audio.echoCancellation = false;
|
|
|
|
audio.noiseSuppression = false;
|
|
|
|
audio.autoGainControl = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 19:00:09 +01:00
|
|
|
let old = serverConnection.findByLocalId(localId);
|
2021-07-17 00:38:26 +02:00
|
|
|
if(old) {
|
2021-01-17 20:20:35 +01:00
|
|
|
// make sure that the camera is released before we try to reopen it
|
2024-11-16 23:59:14 +01:00
|
|
|
await removeFilter(old);
|
2021-07-17 00:38:26 +02:00
|
|
|
stopStream(old.stream);
|
2021-01-17 20:20:35 +01:00
|
|
|
}
|
2020-06-10 20:25:25 +02:00
|
|
|
|
2020-05-05 17:50:16 +02:00
|
|
|
let constraints = {audio: audio, video: video};
|
2020-09-20 14:33:13 +02:00
|
|
|
/** @type {MediaStream} */
|
2020-05-05 17:50:16 +02:00
|
|
|
let stream = null;
|
|
|
|
try {
|
|
|
|
stream = await navigator.mediaDevices.getUserMedia(constraints);
|
|
|
|
} catch(e) {
|
2020-09-04 01:17:06 +02:00
|
|
|
displayError(e);
|
2020-05-05 17:50:16 +02:00
|
|
|
return;
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-05-05 17:50:16 +02:00
|
|
|
|
2020-09-18 17:57:37 +02:00
|
|
|
setMediaChoices(true);
|
2020-05-05 17:50:16 +02:00
|
|
|
|
2021-02-14 18:06:50 +01:00
|
|
|
let c;
|
|
|
|
|
|
|
|
try {
|
|
|
|
c = newUpStream(localId);
|
|
|
|
} catch(e) {
|
|
|
|
console.log(e);
|
|
|
|
displayError(e);
|
|
|
|
return;
|
|
|
|
}
|
2020-06-10 20:25:25 +02:00
|
|
|
|
2021-04-28 17:00:50 +02:00
|
|
|
c.label = 'camera';
|
2021-01-13 13:29:45 +01:00
|
|
|
|
2021-05-06 23:39:27 +02:00
|
|
|
if(settings.filter) {
|
|
|
|
let filter = filters[settings.filter];
|
|
|
|
if(filter)
|
|
|
|
c.userdata.filterDefinition = filter;
|
|
|
|
else
|
|
|
|
displayWarning(`Unknown filter ${settings.filter}`);
|
2021-01-12 01:25:35 +01:00
|
|
|
}
|
|
|
|
|
2024-12-14 12:20:41 +01:00
|
|
|
try {
|
|
|
|
await setUpStream(c, stream);
|
|
|
|
await setMedia(c, settings.mirrorView);
|
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
|
|
|
displayError(e);
|
|
|
|
c.close();
|
|
|
|
}
|
2020-09-01 10:23:35 +02:00
|
|
|
setButtonsVisibility();
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-12-05 01:46:45 +01:00
|
|
|
let safariScreenshareDone = false;
|
2020-10-31 22:31:14 +01:00
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
async function addShareMedia() {
|
2022-05-19 13:24:44 +02:00
|
|
|
if(!safariScreenshareDone) {
|
|
|
|
if(isSafari()) {
|
|
|
|
let ok = confirm(
|
2023-07-05 23:41:59 +02:00
|
|
|
'Screen sharing in Safari is broken. ' +
|
2022-05-19 13:24:44 +02:00
|
|
|
'It will work at first, ' +
|
|
|
|
'but then your video will randomly freeze. ' +
|
|
|
|
'Are you sure that you wish to enable screensharing?'
|
|
|
|
);
|
|
|
|
if(!ok)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
safariScreenshareDone = true;
|
|
|
|
}
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
/** @type {MediaStream} */
|
2020-05-21 18:22:30 +02:00
|
|
|
let stream = null;
|
|
|
|
try {
|
2020-09-20 14:33:13 +02:00
|
|
|
if(!('getDisplayMedia' in navigator.mediaDevices))
|
|
|
|
throw new Error('Your browser does not support screen sharing');
|
2022-02-21 18:19:25 +01:00
|
|
|
stream = await navigator.mediaDevices.getDisplayMedia({
|
|
|
|
video: true,
|
|
|
|
audio: true,
|
|
|
|
});
|
2020-05-21 18:22:30 +02:00
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
2020-09-04 01:17:06 +02:00
|
|
|
displayError(e);
|
2020-04-24 19:38:21 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-05-21 18:22:30 +02:00
|
|
|
|
2020-09-08 03:05:25 +02:00
|
|
|
let c = newUpStream();
|
2021-04-28 17:00:50 +02:00
|
|
|
c.label = 'screenshare';
|
2024-11-16 23:59:14 +01:00
|
|
|
await setUpStream(c, stream);
|
2022-09-16 19:13:55 +02:00
|
|
|
await setMedia(c);
|
2020-12-26 17:28:44 +01:00
|
|
|
setButtonsVisibility();
|
2020-05-21 18:22:30 +02:00
|
|
|
}
|
|
|
|
|
2020-11-24 19:22:38 +01:00
|
|
|
/**
|
|
|
|
* @param {File} file
|
|
|
|
*/
|
|
|
|
async function addFileMedia(file) {
|
|
|
|
let url = URL.createObjectURL(file);
|
|
|
|
let video = document.createElement('video');
|
|
|
|
video.src = url;
|
2020-11-24 23:05:43 +01:00
|
|
|
video.controls = true;
|
2021-02-01 01:08:39 +01:00
|
|
|
let stream;
|
|
|
|
/** @ts-ignore */
|
|
|
|
if(video.captureStream)
|
|
|
|
/** @ts-ignore */
|
|
|
|
stream = video.captureStream();
|
2020-11-24 19:22:38 +01:00
|
|
|
/** @ts-ignore */
|
2021-02-01 01:08:39 +01:00
|
|
|
else if(video.mozCaptureStream)
|
|
|
|
/** @ts-ignore */
|
|
|
|
stream = video.mozCaptureStream();
|
|
|
|
else {
|
|
|
|
displayError("This browser doesn't support file playback");
|
|
|
|
return;
|
|
|
|
}
|
2020-11-24 19:22:38 +01:00
|
|
|
|
|
|
|
let c = newUpStream();
|
2021-04-28 17:00:50 +02:00
|
|
|
c.label = 'video';
|
2021-05-06 23:39:27 +02:00
|
|
|
c.userdata.onclose = function() {
|
2021-01-12 02:23:52 +01:00
|
|
|
let media = /** @type{HTMLVideoElement} */
|
2021-01-31 19:00:09 +01:00
|
|
|
(document.getElementById('media-' + this.localId));
|
2021-01-12 02:23:52 +01:00
|
|
|
if(media && media.src) {
|
|
|
|
URL.revokeObjectURL(media.src);
|
|
|
|
media.src = null;
|
|
|
|
}
|
2020-11-24 19:22:38 +01:00
|
|
|
};
|
2021-05-06 23:39:27 +02:00
|
|
|
await setUpStream(c, stream);
|
2020-11-24 23:22:24 +01:00
|
|
|
|
2021-05-06 23:39:27 +02:00
|
|
|
let presenting = !!findUpMedia('camera');
|
|
|
|
let muted = getSettings().localMute;
|
|
|
|
if(presenting && !muted) {
|
|
|
|
setLocalMute(true, true);
|
|
|
|
displayWarning('You have been muted');
|
|
|
|
}
|
2020-11-29 22:16:28 +01:00
|
|
|
|
2022-09-16 19:13:55 +02:00
|
|
|
await setMedia(c, false, video);
|
2020-12-03 01:07:48 +01:00
|
|
|
c.userdata.play = true;
|
2020-12-26 17:28:44 +01:00
|
|
|
setButtonsVisibility();
|
2020-11-24 19:22:38 +01:00
|
|
|
}
|
|
|
|
|
2020-08-13 20:55:13 +02:00
|
|
|
/**
|
2021-01-14 14:56:15 +01:00
|
|
|
* @param {MediaStream} s
|
2020-08-13 20:55:13 +02:00
|
|
|
*/
|
2021-01-14 14:56:15 +01:00
|
|
|
function stopStream(s) {
|
|
|
|
s.getTracks().forEach(t => {
|
2020-06-10 20:25:25 +02:00
|
|
|
try {
|
|
|
|
t.stop();
|
|
|
|
} catch(e) {
|
2021-01-12 01:25:35 +01:00
|
|
|
console.warn(e);
|
2020-06-10 20:25:25 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-13 20:55:13 +02:00
|
|
|
/**
|
2021-04-28 17:00:50 +02:00
|
|
|
* closeUpMedia closes all up connections with the given label. If label
|
|
|
|
* is null, it closes all up connections.
|
2021-01-14 14:56:15 +01:00
|
|
|
*
|
2021-07-16 00:11:38 +02:00
|
|
|
* @param {string} [label]
|
2020-08-23 19:07:52 +02:00
|
|
|
*/
|
2021-04-28 17:00:50 +02:00
|
|
|
function closeUpMedia(label) {
|
2020-08-11 17:09:31 +02:00
|
|
|
for(let id in serverConnection.up) {
|
|
|
|
let c = serverConnection.up[id];
|
2021-04-28 17:00:50 +02:00
|
|
|
if(label && c.label !== label)
|
2020-05-21 18:22:30 +02:00
|
|
|
continue
|
2021-01-14 14:56:15 +01:00
|
|
|
c.close();
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-05-21 18:22:30 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
/**
|
2021-04-28 17:00:50 +02:00
|
|
|
* @param {string} label
|
2021-01-31 19:00:09 +01:00
|
|
|
* @returns {Stream}
|
2020-09-20 14:33:13 +02:00
|
|
|
*/
|
2021-04-28 17:00:50 +02:00
|
|
|
function findUpMedia(label) {
|
2024-12-15 14:14:10 +01:00
|
|
|
if(!serverConnection)
|
|
|
|
return null;
|
2020-08-11 17:09:31 +02:00
|
|
|
for(let id in serverConnection.up) {
|
2024-05-08 15:14:45 +02:00
|
|
|
let c = serverConnection.up[id];
|
2021-04-28 17:00:50 +02:00
|
|
|
if(c.label === label)
|
2021-01-31 19:00:09 +01:00
|
|
|
return c;
|
2020-05-21 18:22:30 +02:00
|
|
|
}
|
2020-06-10 20:25:25 +02:00
|
|
|
return null;
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
/**
|
|
|
|
* @param {boolean} mute
|
|
|
|
*/
|
2020-06-09 18:05:16 +02:00
|
|
|
function muteLocalTracks(mute) {
|
2020-08-11 17:09:31 +02:00
|
|
|
if(!serverConnection)
|
|
|
|
return;
|
|
|
|
for(let id in serverConnection.up) {
|
|
|
|
let c = serverConnection.up[id];
|
2021-04-28 17:00:50 +02:00
|
|
|
if(c.label === 'camera') {
|
2020-06-09 18:05:16 +02:00
|
|
|
let stream = c.stream;
|
|
|
|
stream.getTracks().forEach(t => {
|
|
|
|
if(t.kind === 'audio') {
|
|
|
|
t.enabled = !mute;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:44:49 +02:00
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
* @param {boolean} force
|
|
|
|
* @param {boolean} [value]
|
|
|
|
*/
|
|
|
|
function forceDownRate(id, force, value) {
|
|
|
|
let c = serverConnection.down[id];
|
|
|
|
if(!c)
|
|
|
|
throw new Error("Unknown down stream");
|
|
|
|
if('requested' in c.userdata) {
|
|
|
|
if(force)
|
|
|
|
c.userdata.requested.force = !!value;
|
|
|
|
else
|
|
|
|
delete(c.userdata.requested.force);
|
|
|
|
} else {
|
|
|
|
if(force)
|
|
|
|
c.userdata.requested = {force: value};
|
|
|
|
}
|
|
|
|
reconsiderDownRate(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps 'video' to 'video-low'. Returns null if nothing changed.
|
|
|
|
*
|
|
|
|
* @param {string[]} requested
|
|
|
|
* @returns {string[]}
|
|
|
|
*/
|
|
|
|
function mapVideoToLow(requested) {
|
|
|
|
let result = [];
|
|
|
|
let found = false;
|
|
|
|
for(let i = 0; i < requested.length; i++) {
|
|
|
|
let r = requested[i];
|
|
|
|
if(r === 'video') {
|
|
|
|
r = 'video-low';
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
result.push(r);
|
|
|
|
}
|
|
|
|
if(!found)
|
|
|
|
return null;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reconsider the video track requested for a given down stream.
|
|
|
|
*
|
|
|
|
* @param {string} [id] - the id of the track to reconsider, all if null.
|
|
|
|
*/
|
|
|
|
function reconsiderDownRate(id) {
|
|
|
|
if(!serverConnection)
|
|
|
|
return;
|
|
|
|
if(!id) {
|
|
|
|
for(let id in serverConnection.down) {
|
|
|
|
reconsiderDownRate(id);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let c = serverConnection.down[id];
|
|
|
|
if(!c)
|
|
|
|
throw new Error("Unknown down stream");
|
|
|
|
let normalrequest = mapRequestLabel(getSettings().request, c.label);
|
|
|
|
|
|
|
|
let requestlow = mapVideoToLow(normalrequest);
|
|
|
|
if(requestlow === null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let old = c.userdata.requested;
|
|
|
|
let low = false;
|
|
|
|
if(old && ('force' in old)) {
|
|
|
|
low = old.force;
|
|
|
|
} else {
|
|
|
|
let media = /** @type {HTMLVideoElement} */
|
|
|
|
(document.getElementById('media-' + c.localId));
|
|
|
|
if(!media)
|
|
|
|
throw new Error("No media for stream");
|
|
|
|
let w = media.scrollWidth;
|
|
|
|
let h = media.scrollHeight;
|
|
|
|
if(w && h && w * h <= 320 * 240) {
|
|
|
|
low = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(low !== !!(old && old.low)) {
|
|
|
|
if('requested' in c.userdata)
|
|
|
|
c.userdata.requested.low = low;
|
|
|
|
else
|
|
|
|
c.userdata.requested = {low: low};
|
|
|
|
c.request(low ? requestlow : null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let reconsiderDownRateTimer = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schedules reconsiderDownRate() to be run later. The delay avoids too
|
|
|
|
* much recomputations when resizing the window.
|
|
|
|
*/
|
|
|
|
function scheduleReconsiderDownRate() {
|
|
|
|
if(reconsiderDownRateTimer)
|
|
|
|
return;
|
|
|
|
reconsiderDownRateTimer =
|
|
|
|
setTimeout(() => {
|
|
|
|
reconsiderDownRateTimer = null;
|
|
|
|
reconsiderDownRate();
|
|
|
|
}, 200);
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
2020-11-28 01:53:29 +01:00
|
|
|
* setMedia adds a new media element corresponding to stream c.
|
|
|
|
*
|
2020-08-11 17:09:31 +02:00
|
|
|
* @param {Stream} c
|
2020-12-03 18:38:36 +01:00
|
|
|
* @param {boolean} [mirror]
|
|
|
|
* - whether to mirror the video
|
2020-11-24 19:22:38 +01:00
|
|
|
* @param {HTMLVideoElement} [video]
|
2020-11-28 01:53:29 +01:00
|
|
|
* - the video element to add. If null, a new element with custom
|
|
|
|
* controls will be created.
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2022-09-16 19:13:55 +02:00
|
|
|
async function setMedia(c, mirror, video) {
|
2021-01-31 19:00:09 +01:00
|
|
|
let div = document.getElementById('peer-' + c.localId);
|
2020-04-24 19:38:21 +02:00
|
|
|
if(!div) {
|
|
|
|
div = document.createElement('div');
|
2021-01-31 19:00:09 +01:00
|
|
|
div.id = 'peer-' + c.localId;
|
2020-04-24 19:38:21 +02:00
|
|
|
div.classList.add('peer');
|
2022-09-16 19:10:01 +02:00
|
|
|
let peersdiv = document.getElementById('peers');
|
2020-04-24 19:38:21 +02:00
|
|
|
peersdiv.appendChild(div);
|
|
|
|
}
|
|
|
|
|
2022-09-16 19:10:01 +02:00
|
|
|
showHideMedia(c, div)
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
let media = /** @type {HTMLVideoElement} */
|
2021-01-31 19:00:09 +01:00
|
|
|
(document.getElementById('media-' + c.localId));
|
2021-05-06 23:39:27 +02:00
|
|
|
if(!media) {
|
2020-11-24 19:22:38 +01:00
|
|
|
if(video) {
|
|
|
|
media = video;
|
|
|
|
} else {
|
|
|
|
media = document.createElement('video');
|
2022-09-16 19:13:55 +02:00
|
|
|
if(c.up)
|
2020-11-24 19:22:38 +01:00
|
|
|
media.muted = true;
|
|
|
|
}
|
2020-11-28 01:53:29 +01:00
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
media.classList.add('media');
|
|
|
|
media.autoplay = true;
|
2022-01-25 01:28:18 +01:00
|
|
|
media.playsInline = true;
|
2021-01-31 19:00:09 +01:00
|
|
|
media.id = 'media-' + c.localId;
|
2020-04-24 19:38:21 +02:00
|
|
|
div.appendChild(media);
|
2021-05-08 02:28:04 +02:00
|
|
|
addCustomControls(media, div, c, !!video);
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2021-01-31 19:00:09 +01:00
|
|
|
if(mirror)
|
|
|
|
media.classList.add('mirror');
|
|
|
|
else
|
|
|
|
media.classList.remove('mirror');
|
|
|
|
|
2021-01-13 14:59:43 +01:00
|
|
|
if(!video && media.srcObject !== c.stream)
|
2020-12-01 16:20:25 +01:00
|
|
|
media.srcObject = c.stream;
|
|
|
|
|
2022-09-16 19:13:55 +02:00
|
|
|
if(!c.up) {
|
2021-05-14 18:44:49 +02:00
|
|
|
media.onfullscreenchange = function(e) {
|
|
|
|
forceDownRate(c.id, document.fullscreenElement === media, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 19:00:09 +01:00
|
|
|
let label = document.getElementById('label-' + c.localId);
|
2020-04-24 19:38:21 +02:00
|
|
|
if(!label) {
|
|
|
|
label = document.createElement('div');
|
2021-01-31 19:00:09 +01:00
|
|
|
label.id = 'label-' + c.localId;
|
2020-04-24 19:38:21 +02:00
|
|
|
label.classList.add('label');
|
2020-05-10 21:18:07 +02:00
|
|
|
div.appendChild(label);
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
setLabel(c);
|
|
|
|
setMediaStatus(c);
|
2020-05-01 01:22:17 +02:00
|
|
|
|
2020-09-01 10:23:35 +02:00
|
|
|
showVideo();
|
2020-05-01 01:22:17 +02:00
|
|
|
resizePeers();
|
2020-09-28 17:00:25 +02:00
|
|
|
}
|
|
|
|
|
2022-09-16 19:10:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Stream} c
|
|
|
|
* @param {HTMLElement} elt
|
|
|
|
*/
|
|
|
|
function showHideMedia(c, elt) {
|
|
|
|
let display = c.up || getSettings().displayAll;
|
|
|
|
if(!display && c.stream) {
|
|
|
|
let tracks = c.stream.getTracks();
|
|
|
|
for(let i = 0; i < tracks.length; i++) {
|
|
|
|
let t = tracks[i];
|
|
|
|
if(t.kind === 'video') {
|
|
|
|
display = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(display)
|
|
|
|
elt.classList.remove('peer-hidden');
|
|
|
|
else
|
|
|
|
elt.classList.add('peer-hidden');
|
|
|
|
}
|
|
|
|
|
2021-02-03 19:46:44 +01:00
|
|
|
/**
|
|
|
|
* resetMedia resets the source stream of the media element associated
|
|
|
|
* with c. This has the side-effect of resetting any frozen frames.
|
|
|
|
*
|
|
|
|
* @param {Stream} c
|
|
|
|
*/
|
|
|
|
function resetMedia(c) {
|
|
|
|
let media = /** @type {HTMLVideoElement} */
|
|
|
|
(document.getElementById('media-' + c.localId));
|
|
|
|
if(!media) {
|
|
|
|
console.error("Resetting unknown media element")
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
media.srcObject = media.srcObject;
|
|
|
|
}
|
|
|
|
|
2020-09-28 17:00:25 +02:00
|
|
|
/**
|
2020-11-28 01:53:29 +01:00
|
|
|
* @param {Element} elt
|
2020-09-28 17:00:25 +02:00
|
|
|
*/
|
2020-11-28 01:53:29 +01:00
|
|
|
function cloneHTMLElement(elt) {
|
|
|
|
if(!(elt instanceof HTMLElement))
|
|
|
|
throw new Error('Unexpected element type');
|
|
|
|
return /** @type{HTMLElement} */(elt.cloneNode(true));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {HTMLVideoElement} media
|
|
|
|
* @param {HTMLElement} container
|
|
|
|
* @param {Stream} c
|
|
|
|
*/
|
2021-05-08 02:28:04 +02:00
|
|
|
function addCustomControls(media, container, c, toponly) {
|
|
|
|
if(!toponly && !document.getElementById('controls-' + c.localId)) {
|
|
|
|
media.controls = false;
|
2020-11-28 01:53:29 +01:00
|
|
|
|
2021-05-08 02:28:04 +02:00
|
|
|
let template =
|
|
|
|
document.getElementById('videocontrols-template').firstElementChild;
|
|
|
|
let controls = cloneHTMLElement(template);
|
|
|
|
controls.id = 'controls-' + c.localId;
|
|
|
|
|
|
|
|
let volume = getVideoButton(controls, 'volume');
|
|
|
|
|
2021-05-08 14:19:46 +02:00
|
|
|
if(c.up && c.label === 'camera') {
|
2021-05-08 02:28:04 +02:00
|
|
|
volume.remove();
|
|
|
|
} else {
|
|
|
|
setVolumeButton(media.muted,
|
|
|
|
getVideoButton(controls, "volume-mute"),
|
|
|
|
getVideoButton(controls, "volume-slider"));
|
|
|
|
}
|
|
|
|
container.appendChild(controls);
|
2020-09-28 17:00:25 +02:00
|
|
|
}
|
2020-11-28 01:53:29 +01:00
|
|
|
|
2021-05-08 14:19:46 +02:00
|
|
|
if(c.up && !document.getElementById('topcontrols-' + c.localId)) {
|
2021-05-08 02:28:04 +02:00
|
|
|
let toptemplate =
|
|
|
|
document.getElementById('topvideocontrols-template').firstElementChild;
|
|
|
|
let topcontrols = cloneHTMLElement(toptemplate);
|
|
|
|
topcontrols.id = 'topcontrols-' + c.localId;
|
|
|
|
container.appendChild(topcontrols);
|
|
|
|
}
|
2021-05-06 23:39:27 +02:00
|
|
|
registerControlHandlers(c.localId, media, container);
|
2020-09-28 17:00:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-28 01:53:29 +01:00
|
|
|
* @param {HTMLElement} container
|
|
|
|
* @param {string} name
|
2020-09-28 17:00:25 +02:00
|
|
|
*/
|
2020-11-28 01:53:29 +01:00
|
|
|
function getVideoButton(container, name) {
|
|
|
|
return /** @type {HTMLElement} */(container.getElementsByClassName(name)[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {boolean} muted
|
2020-12-11 19:37:09 +01:00
|
|
|
* @param {HTMLElement} button
|
|
|
|
* @param {HTMLElement} slider
|
2020-11-28 01:53:29 +01:00
|
|
|
*/
|
2020-12-11 19:37:09 +01:00
|
|
|
function setVolumeButton(muted, button, slider) {
|
2020-11-28 01:53:29 +01:00
|
|
|
if(!muted) {
|
2020-12-08 13:27:56 +01:00
|
|
|
button.classList.remove("fa-volume-mute");
|
2020-11-28 01:53:29 +01:00
|
|
|
button.classList.add("fa-volume-up");
|
|
|
|
} else {
|
|
|
|
button.classList.remove("fa-volume-up");
|
2020-12-08 13:27:56 +01:00
|
|
|
button.classList.add("fa-volume-mute");
|
2020-09-28 17:00:25 +02:00
|
|
|
}
|
2020-12-11 19:37:09 +01:00
|
|
|
|
|
|
|
if(!(slider instanceof HTMLInputElement))
|
|
|
|
throw new Error("Couldn't find volume slider");
|
|
|
|
slider.disabled = muted;
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 17:00:25 +02:00
|
|
|
/**
|
2021-05-06 23:39:27 +02:00
|
|
|
* @param {string} localId
|
2020-11-28 01:53:29 +01:00
|
|
|
* @param {HTMLVideoElement} media
|
|
|
|
* @param {HTMLElement} container
|
2020-09-28 17:00:25 +02:00
|
|
|
*/
|
2021-05-06 23:39:27 +02:00
|
|
|
function registerControlHandlers(localId, media, container) {
|
2020-12-15 15:20:53 +01:00
|
|
|
let play = getVideoButton(container, 'video-play');
|
|
|
|
if(play) {
|
|
|
|
play.onclick = function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
media.play();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-29 20:44:24 +02:00
|
|
|
let stop = getVideoButton(container, 'video-stop');
|
|
|
|
if(stop) {
|
|
|
|
stop.onclick = function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
try {
|
2021-05-06 23:39:27 +02:00
|
|
|
let c = serverConnection.findByLocalId(localId);
|
|
|
|
if(!c)
|
|
|
|
throw new Error('Closing unknown stream');
|
2021-05-07 02:44:04 +02:00
|
|
|
c.close();
|
2021-03-29 20:44:24 +02:00
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
|
|
|
displayError(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-28 01:53:29 +01:00
|
|
|
let volume = getVideoButton(container, 'volume');
|
2020-11-09 12:08:04 +01:00
|
|
|
if (volume) {
|
|
|
|
volume.onclick = function(event) {
|
2020-12-08 13:27:56 +01:00
|
|
|
let target = /** @type{HTMLElement} */(event.target);
|
|
|
|
if(!target.classList.contains('volume-mute'))
|
|
|
|
// if click on volume slider, do nothing
|
|
|
|
return;
|
2020-11-09 12:08:04 +01:00
|
|
|
event.preventDefault();
|
2020-11-28 01:53:29 +01:00
|
|
|
media.muted = !media.muted;
|
2020-12-11 19:37:09 +01:00
|
|
|
setVolumeButton(media.muted, target,
|
|
|
|
getVideoButton(volume, "volume-slider"));
|
2020-12-08 13:27:56 +01:00
|
|
|
};
|
|
|
|
volume.oninput = function() {
|
|
|
|
let slider = /** @type{HTMLInputElement} */
|
|
|
|
(getVideoButton(volume, "volume-slider"));
|
|
|
|
media.volume = parseInt(slider.value, 10)/100;
|
2020-11-09 12:08:04 +01:00
|
|
|
};
|
|
|
|
}
|
2020-11-08 17:07:17 +01:00
|
|
|
|
2020-11-28 01:53:29 +01:00
|
|
|
let pip = getVideoButton(container, 'pip');
|
2020-11-24 23:05:43 +01:00
|
|
|
if(pip) {
|
|
|
|
if(HTMLVideoElement.prototype.requestPictureInPicture) {
|
2020-11-28 01:53:29 +01:00
|
|
|
pip.onclick = function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
if(media.requestPictureInPicture) {
|
|
|
|
media.requestPictureInPicture();
|
|
|
|
} else {
|
|
|
|
displayWarning('Picture in Picture not supported.');
|
|
|
|
}
|
2020-11-24 23:05:43 +01:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
pip.style.display = 'none';
|
|
|
|
}
|
2020-11-09 00:35:52 +01:00
|
|
|
}
|
2020-11-08 17:07:17 +01:00
|
|
|
|
2020-11-28 01:53:29 +01:00
|
|
|
let fs = getVideoButton(container, 'fullscreen');
|
2020-11-24 23:05:43 +01:00
|
|
|
if(fs) {
|
2020-12-05 14:13:30 +01:00
|
|
|
if(HTMLVideoElement.prototype.requestFullscreen ||
|
|
|
|
/** @ts-ignore */
|
|
|
|
HTMLVideoElement.prototype.webkitRequestFullscreen) {
|
2020-11-28 01:53:29 +01:00
|
|
|
fs.onclick = function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
if(media.requestFullscreen) {
|
|
|
|
media.requestFullscreen();
|
2020-12-05 14:13:30 +01:00
|
|
|
/** @ts-ignore */
|
|
|
|
} else if(media.webkitRequestFullscreen) {
|
|
|
|
/** @ts-ignore */
|
|
|
|
media.webkitRequestFullscreen();
|
2020-11-28 01:53:29 +01:00
|
|
|
} else {
|
|
|
|
displayWarning('Full screen not supported!');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
fs.style.display = 'none';
|
|
|
|
}
|
2020-11-24 23:05:43 +01:00
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 16:34:52 +02:00
|
|
|
/**
|
2021-01-31 19:00:09 +01:00
|
|
|
* @param {string} localId
|
2020-09-12 16:34:52 +02:00
|
|
|
*/
|
2021-01-31 19:00:09 +01:00
|
|
|
function delMedia(localId) {
|
2020-04-24 19:38:21 +02:00
|
|
|
let mediadiv = document.getElementById('peers');
|
2021-01-31 19:00:09 +01:00
|
|
|
let peer = document.getElementById('peer-' + localId);
|
2020-08-19 14:39:40 +02:00
|
|
|
if(!peer)
|
|
|
|
throw new Error('Removing unknown media');
|
2020-09-20 14:33:13 +02:00
|
|
|
|
|
|
|
let media = /** @type{HTMLVideoElement} */
|
2021-01-31 19:00:09 +01:00
|
|
|
(document.getElementById('media-' + localId));
|
2020-04-24 19:38:21 +02:00
|
|
|
|
|
|
|
media.srcObject = null;
|
|
|
|
mediadiv.removeChild(peer);
|
2020-05-01 01:22:17 +02:00
|
|
|
|
2021-01-14 14:56:15 +01:00
|
|
|
setButtonsVisibility();
|
2020-05-01 01:22:17 +02:00
|
|
|
resizePeers();
|
2020-09-01 10:23:35 +02:00
|
|
|
hideVideo();
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
|
|
|
* @param {Stream} c
|
|
|
|
*/
|
|
|
|
function setMediaStatus(c) {
|
2020-06-12 21:28:35 +02:00
|
|
|
let state = c && c.pc && c.pc.iceConnectionState;
|
|
|
|
let good = state === 'connected' || state === 'completed';
|
|
|
|
|
2021-01-31 19:00:09 +01:00
|
|
|
let media = document.getElementById('media-' + c.localId);
|
2020-06-11 19:33:39 +02:00
|
|
|
if(!media) {
|
|
|
|
console.warn('Setting status of unknown media.');
|
|
|
|
return;
|
|
|
|
}
|
2020-12-03 01:07:48 +01:00
|
|
|
if(good) {
|
2020-06-11 19:33:39 +02:00
|
|
|
media.classList.remove('media-failed');
|
2020-12-03 01:07:48 +01:00
|
|
|
if(c.userdata.play) {
|
|
|
|
if(media instanceof HTMLMediaElement)
|
|
|
|
media.play().catch(e => {
|
|
|
|
console.error(e);
|
|
|
|
displayError(e);
|
|
|
|
});
|
|
|
|
delete(c.userdata.play);
|
|
|
|
}
|
|
|
|
} else {
|
2020-06-11 19:33:39 +02:00
|
|
|
media.classList.add('media-failed');
|
2020-12-03 01:07:48 +01:00
|
|
|
}
|
2020-06-11 19:33:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
|
|
|
* @param {Stream} c
|
|
|
|
* @param {string} [fallback]
|
|
|
|
*/
|
|
|
|
function setLabel(c, fallback) {
|
2021-01-31 19:00:09 +01:00
|
|
|
let label = document.getElementById('label-' + c.localId);
|
2020-04-24 19:38:21 +02:00
|
|
|
if(!label)
|
|
|
|
return;
|
2021-01-03 12:04:39 +01:00
|
|
|
let l = c.username;
|
2020-04-25 14:45:48 +02:00
|
|
|
if(l) {
|
|
|
|
label.textContent = l;
|
|
|
|
label.classList.remove('label-fallback');
|
|
|
|
} else if(fallback) {
|
|
|
|
label.textContent = fallback;
|
|
|
|
label.classList.add('label-fallback');
|
|
|
|
} else {
|
|
|
|
label.textContent = '';
|
|
|
|
label.classList.remove('label-fallback');
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-01 01:22:17 +02:00
|
|
|
function resizePeers() {
|
2020-10-07 09:33:38 +02:00
|
|
|
// Window resize can call this method too early
|
|
|
|
if (!serverConnection)
|
|
|
|
return;
|
2020-08-11 17:09:31 +02:00
|
|
|
let count =
|
|
|
|
Object.keys(serverConnection.up).length +
|
|
|
|
Object.keys(serverConnection.down).length;
|
2020-09-01 10:23:35 +02:00
|
|
|
let peers = document.getElementById('peers');
|
2020-05-01 01:22:17 +02:00
|
|
|
let columns = Math.ceil(Math.sqrt(count));
|
2020-09-03 20:54:27 +02:00
|
|
|
if (!count)
|
|
|
|
// No video, nothing to resize.
|
|
|
|
return;
|
2020-10-07 09:33:38 +02:00
|
|
|
let container = document.getElementById("video-container");
|
|
|
|
// Peers div has total padding of 40px, we remove 40 on offsetHeight
|
|
|
|
// Grid has row-gap of 5px
|
|
|
|
let rows = Math.ceil(count / columns);
|
2020-10-07 09:46:00 +02:00
|
|
|
let margins = (rows - 1) * 5 + 40;
|
2020-09-03 17:01:21 +02:00
|
|
|
|
2020-10-07 09:33:38 +02:00
|
|
|
if (count <= 2 && container.offsetHeight > container.offsetWidth) {
|
|
|
|
peers.style['grid-template-columns'] = "repeat(1, 1fr)";
|
|
|
|
rows = count;
|
|
|
|
} else {
|
|
|
|
peers.style['grid-template-columns'] = `repeat(${columns}, 1fr)`;
|
|
|
|
}
|
2020-10-07 09:46:00 +02:00
|
|
|
if (count === 1)
|
2020-10-07 09:33:38 +02:00
|
|
|
return;
|
|
|
|
let max_video_height = (peers.offsetHeight - margins) / rows;
|
|
|
|
let media_list = peers.querySelectorAll(".media");
|
2020-09-20 14:33:13 +02:00
|
|
|
for(let i = 0; i < media_list.length; i++) {
|
|
|
|
let media = media_list[i];
|
|
|
|
if(!(media instanceof HTMLMediaElement)) {
|
|
|
|
console.warn('Unexpected media');
|
|
|
|
continue;
|
|
|
|
}
|
2020-10-07 09:33:38 +02:00
|
|
|
media.style['max-height'] = max_video_height + "px";
|
2020-09-09 20:26:19 +02:00
|
|
|
}
|
2020-05-01 01:22:17 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 15:11:52 +02:00
|
|
|
/**
|
|
|
|
* Lexicographic order, with case differences secondary.
|
|
|
|
* @param{string} a
|
|
|
|
* @param{string} b
|
|
|
|
*/
|
|
|
|
function stringCompare(a, b) {
|
2020-12-26 17:28:44 +01:00
|
|
|
let la = a.toLowerCase();
|
|
|
|
let lb = b.toLowerCase();
|
2020-10-08 15:11:52 +02:00
|
|
|
if(la < lb)
|
|
|
|
return -1;
|
|
|
|
else if(la > lb)
|
|
|
|
return +1;
|
|
|
|
else if(a < b)
|
|
|
|
return -1;
|
|
|
|
else if(a > b)
|
|
|
|
return +1;
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-05-03 18:37:52 +02:00
|
|
|
/**
|
|
|
|
* @param {string} v
|
|
|
|
*/
|
|
|
|
function dateFromInput(v) {
|
|
|
|
let d = new Date(v);
|
|
|
|
if(d.toString() === 'Invalid Date')
|
|
|
|
throw new Error('Invalid date');
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Date} d
|
|
|
|
*/
|
|
|
|
function dateToInput(d) {
|
|
|
|
let dd = new Date(d);
|
|
|
|
dd.setMinutes(dd.getMinutes() - dd.getTimezoneOffset());
|
|
|
|
return dd.toISOString().slice(0, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function inviteMenu() {
|
|
|
|
let d = /** @type {HTMLDialogElement} */
|
|
|
|
(document.getElementById('invite-dialog'));
|
|
|
|
if(!('HTMLDialogElement' in window) || !d.showModal) {
|
2024-05-08 17:19:41 +02:00
|
|
|
displayError("This browser doesn't support modal dialogs");
|
2023-05-03 18:37:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
d.returnValue = '';
|
|
|
|
let c = getButtonElement('invite-cancel');
|
|
|
|
c.onclick = function(e) { d.close('cancel'); };
|
|
|
|
let u = getInputElement('invite-username');
|
|
|
|
u.value = '';
|
|
|
|
let now = new Date();
|
|
|
|
now.setMilliseconds(0);
|
|
|
|
now.setSeconds(0);
|
|
|
|
let nb = getInputElement('invite-not-before');
|
|
|
|
nb.min = dateToInput(now);
|
|
|
|
let ex = getInputElement('invite-expires');
|
|
|
|
let expires = new Date(now);
|
|
|
|
expires.setDate(expires.getDate() + 2);
|
2024-03-03 13:29:44 +01:00
|
|
|
ex.min = dateToInput(now);
|
2023-05-03 18:37:52 +02:00
|
|
|
ex.value = dateToInput(expires);
|
|
|
|
d.showModal();
|
|
|
|
}
|
|
|
|
|
|
|
|
document.getElementById('invite-dialog').onclose = function(e) {
|
|
|
|
if(!(this instanceof HTMLDialogElement))
|
|
|
|
throw new Error('Unexpected type for this');
|
|
|
|
let dialog = /** @type {HTMLDialogElement} */(this);
|
|
|
|
if(dialog.returnValue !== 'invite')
|
|
|
|
return;
|
|
|
|
let u = getInputElement('invite-username');
|
|
|
|
let username = u.value.trim() || null;
|
|
|
|
let nb = getInputElement('invite-not-before');
|
|
|
|
let notBefore = null;
|
|
|
|
if(nb.value) {
|
|
|
|
try {
|
|
|
|
notBefore = dateFromInput(nb.value);
|
|
|
|
} catch(e) {
|
|
|
|
displayError(`Couldn't parse ${nb.value}: ${e}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let ex = getInputElement('invite-expires');
|
|
|
|
let expires = null;
|
|
|
|
if(ex.value) {
|
|
|
|
try {
|
|
|
|
expires = dateFromInput(ex.value);
|
|
|
|
} catch(e) {
|
|
|
|
displayError(`Couldn't parse ${nb.value}: ${e}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let template = {}
|
|
|
|
if(username)
|
|
|
|
template.username = username;
|
|
|
|
if(notBefore)
|
|
|
|
template['not-before'] = notBefore;
|
|
|
|
if(expires)
|
|
|
|
template.expires = expires;
|
|
|
|
makeToken(template);
|
|
|
|
};
|
|
|
|
|
2022-02-16 20:41:58 +01:00
|
|
|
/**
|
|
|
|
* @param {HTMLElement} elt
|
|
|
|
*/
|
|
|
|
function userMenu(elt) {
|
|
|
|
if(!elt.id.startsWith('user-'))
|
|
|
|
throw new Error('Unexpected id for user menu');
|
|
|
|
let id = elt.id.slice('user-'.length);
|
|
|
|
let user = serverConnection.users[id];
|
|
|
|
if(!user)
|
|
|
|
throw new Error("Couldn't find user")
|
|
|
|
let items = [];
|
|
|
|
if(id === serverConnection.id) {
|
|
|
|
let mydata = serverConnection.users[serverConnection.id].data;
|
|
|
|
if(mydata['raisehand'])
|
2022-03-26 09:13:49 +01:00
|
|
|
items.push({label: 'Unraise hand', onClick: () => {
|
2022-02-16 20:41:58 +01:00
|
|
|
serverConnection.userAction(
|
|
|
|
'setdata', serverConnection.id, {'raisehand': null},
|
|
|
|
);
|
|
|
|
}});
|
|
|
|
else
|
|
|
|
items.push({label: 'Raise hand', onClick: () => {
|
|
|
|
serverConnection.userAction(
|
|
|
|
'setdata', serverConnection.id, {'raisehand': true},
|
|
|
|
);
|
|
|
|
}});
|
2023-04-04 00:30:20 +02:00
|
|
|
if(serverConnection.version !== "1" &&
|
|
|
|
serverConnection.permissions.indexOf('token') >= 0) {
|
|
|
|
items.push({label: 'Invite user', onClick: () => {
|
2023-05-03 18:37:52 +02:00
|
|
|
inviteMenu();
|
2023-04-04 00:30:20 +02:00
|
|
|
}});
|
|
|
|
}
|
|
|
|
if(serverConnection.permissions.indexOf('present') >= 0 && canFile())
|
2022-02-21 18:36:36 +01:00
|
|
|
items.push({label: 'Broadcast file', onClick: presentFile});
|
2022-02-16 20:41:58 +01:00
|
|
|
items.push({label: 'Restart media', onClick: renegotiateStreams});
|
|
|
|
} else {
|
|
|
|
items.push({label: 'Send file', onClick: () => {
|
|
|
|
sendFile(id);
|
|
|
|
}});
|
2022-02-19 23:43:44 +01:00
|
|
|
if(serverConnection.permissions.indexOf('op') >= 0) {
|
2022-02-16 20:41:58 +01:00
|
|
|
items.push({type: 'seperator'}); // sic
|
2022-02-19 23:43:44 +01:00
|
|
|
if(user.permissions.indexOf('present') >= 0)
|
2022-02-16 20:41:58 +01:00
|
|
|
items.push({label: 'Forbid presenting', onClick: () => {
|
|
|
|
serverConnection.userAction('unpresent', id);
|
|
|
|
}});
|
|
|
|
else
|
|
|
|
items.push({label: 'Allow presenting', onClick: () => {
|
|
|
|
serverConnection.userAction('present', id);
|
|
|
|
}});
|
|
|
|
items.push({label: 'Mute', onClick: () => {
|
2022-02-16 23:10:43 +01:00
|
|
|
serverConnection.userMessage('mute', id);
|
2022-02-16 20:41:58 +01:00
|
|
|
}});
|
|
|
|
items.push({label: 'Kick out', onClick: () => {
|
|
|
|
serverConnection.userAction('kick', id);
|
|
|
|
}});
|
2024-04-30 18:18:32 +02:00
|
|
|
items.push({label: 'Identify', onClick: () => {
|
|
|
|
serverConnection.userAction('identify', id);
|
|
|
|
}});
|
2022-02-16 20:41:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/** @ts-ignore */
|
|
|
|
new Contextual({
|
|
|
|
items: items,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-13 20:55:13 +02:00
|
|
|
/**
|
|
|
|
* @param {string} id
|
2022-01-28 22:13:14 +01:00
|
|
|
* @param {user} userinfo
|
2020-08-13 20:55:13 +02:00
|
|
|
*/
|
2022-01-28 22:13:14 +01:00
|
|
|
function addUser(id, userinfo) {
|
2020-04-24 19:38:21 +02:00
|
|
|
let div = document.getElementById('users');
|
|
|
|
let user = document.createElement('div');
|
|
|
|
user.id = 'user-' + id;
|
2020-09-11 10:39:32 +02:00
|
|
|
user.classList.add("user-p");
|
2022-03-25 16:44:37 +01:00
|
|
|
setUserStatus(id, user, userinfo);
|
2022-02-16 20:41:58 +01:00
|
|
|
user.addEventListener('click', function(e) {
|
|
|
|
let elt = e.target;
|
|
|
|
if(!elt || !(elt instanceof HTMLElement))
|
|
|
|
throw new Error("Couldn't find user div");
|
|
|
|
userMenu(elt);
|
|
|
|
});
|
|
|
|
|
2022-02-16 20:33:59 +01:00
|
|
|
let us = div.children;
|
|
|
|
|
|
|
|
if(id === serverConnection.id) {
|
|
|
|
if(us.length === 0)
|
|
|
|
div.appendChild(user);
|
|
|
|
else
|
|
|
|
div.insertBefore(user, us[0]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-28 22:13:14 +01:00
|
|
|
if(userinfo.username) {
|
2020-09-12 15:23:38 +02:00
|
|
|
for(let i = 0; i < us.length; i++) {
|
|
|
|
let child = us[i];
|
2022-02-16 20:33:59 +01:00
|
|
|
let childid = child.id.slice('user-'.length);
|
|
|
|
if(childid === serverConnection.id)
|
|
|
|
continue;
|
|
|
|
let childuser = serverConnection.users[childid] || null;
|
2021-04-27 18:58:21 +02:00
|
|
|
let childname = (childuser && childuser.username) || null;
|
2022-01-28 22:13:14 +01:00
|
|
|
if(!childname || stringCompare(childname, userinfo.username) > 0) {
|
2020-09-12 15:23:38 +02:00
|
|
|
div.insertBefore(user, child);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-16 20:33:59 +01:00
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
div.appendChild(user);
|
|
|
|
}
|
|
|
|
|
2022-03-25 16:44:37 +01:00
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
* @param {user} userinfo
|
|
|
|
*/
|
2021-12-27 21:58:15 +01:00
|
|
|
function changeUser(id, userinfo) {
|
2022-03-25 16:44:37 +01:00
|
|
|
let elt = document.getElementById('user-' + id);
|
|
|
|
if(!elt) {
|
2021-04-27 18:58:21 +02:00
|
|
|
console.warn('Unknown user ' + id);
|
|
|
|
return;
|
|
|
|
}
|
2022-03-25 16:44:37 +01:00
|
|
|
setUserStatus(id, elt, userinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
* @param {HTMLElement} elt
|
|
|
|
* @param {user} userinfo
|
|
|
|
*/
|
|
|
|
function setUserStatus(id, elt, userinfo) {
|
|
|
|
elt.textContent = userinfo.username ? userinfo.username : '(anon)';
|
|
|
|
if(userinfo.data.raisehand)
|
|
|
|
elt.classList.add('user-status-raisehand');
|
2021-12-27 21:58:15 +01:00
|
|
|
else
|
2022-03-25 16:44:37 +01:00
|
|
|
elt.classList.remove('user-status-raisehand');
|
2022-09-16 18:53:02 +02:00
|
|
|
|
|
|
|
let microphone=false, camera = false;
|
|
|
|
for(let label in userinfo.streams) {
|
|
|
|
for(let kind in userinfo.streams[label]) {
|
|
|
|
if(kind == 'audio')
|
|
|
|
microphone = true;
|
|
|
|
else
|
|
|
|
camera = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(camera) {
|
|
|
|
elt.classList.remove('user-status-microphone');
|
|
|
|
elt.classList.add('user-status-camera');
|
|
|
|
} else if(microphone) {
|
|
|
|
elt.classList.add('user-status-microphone');
|
|
|
|
elt.classList.remove('user-status-camera');
|
|
|
|
} else {
|
|
|
|
elt.classList.remove('user-status-microphone');
|
|
|
|
elt.classList.remove('user-status-camera');
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2021-04-27 18:58:21 +02:00
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
*/
|
|
|
|
function delUser(id) {
|
|
|
|
let div = document.getElementById('users');
|
|
|
|
let user = document.getElementById('user-' + id);
|
|
|
|
div.removeChild(user);
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 20:55:13 +02:00
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
* @param {string} kind
|
|
|
|
*/
|
2021-04-27 18:58:21 +02:00
|
|
|
function gotUser(id, kind) {
|
2020-08-12 13:51:31 +02:00
|
|
|
switch(kind) {
|
|
|
|
case 'add':
|
2022-01-28 22:13:14 +01:00
|
|
|
addUser(id, serverConnection.users[id]);
|
2021-05-09 15:06:10 +02:00
|
|
|
if(Object.keys(serverConnection.users).length == 3)
|
|
|
|
reconsiderSendParameters();
|
2020-08-12 13:51:31 +02:00
|
|
|
break;
|
|
|
|
case 'delete':
|
2021-04-27 18:58:21 +02:00
|
|
|
delUser(id);
|
2021-05-09 15:06:10 +02:00
|
|
|
if(Object.keys(serverConnection.users).length < 3)
|
|
|
|
scheduleReconsiderParameters();
|
2021-04-27 18:58:21 +02:00
|
|
|
break;
|
|
|
|
case 'change':
|
2021-12-27 21:58:15 +01:00
|
|
|
changeUser(id, serverConnection.users[id]);
|
2020-08-12 13:51:31 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.warn('Unknown user kind', kind);
|
|
|
|
break;
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-04-25 18:09:31 +02:00
|
|
|
function displayUsername() {
|
2022-02-20 15:32:18 +01:00
|
|
|
document.getElementById('userspan').textContent = serverConnection.username;
|
2022-02-19 23:43:44 +01:00
|
|
|
let op = serverConnection.permissions.indexOf('op') >= 0;
|
|
|
|
let present = serverConnection.permissions.indexOf('present') >= 0;
|
2020-04-25 18:09:31 +02:00
|
|
|
let text = '';
|
2022-02-19 23:43:44 +01:00
|
|
|
if(op && present)
|
2020-11-24 17:36:52 +01:00
|
|
|
text = '(op, presenter)';
|
2022-02-19 23:43:44 +01:00
|
|
|
else if(op)
|
2020-11-24 17:36:52 +01:00
|
|
|
text = 'operator';
|
2022-02-19 23:43:44 +01:00
|
|
|
else if(present)
|
2020-11-24 17:36:52 +01:00
|
|
|
text = 'presenter';
|
|
|
|
document.getElementById('permspan').textContent = text;
|
2020-04-25 18:09:31 +02:00
|
|
|
}
|
|
|
|
|
2020-12-01 00:26:14 +01:00
|
|
|
let presentRequested = null;
|
|
|
|
|
2021-07-16 19:41:00 +02:00
|
|
|
/**
|
2021-07-16 23:03:36 +02:00
|
|
|
* @param {string} s
|
|
|
|
*/
|
|
|
|
function capitalise(s) {
|
|
|
|
if(s.length <= 0)
|
|
|
|
return s;
|
|
|
|
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} title
|
2021-07-16 19:41:00 +02:00
|
|
|
*/
|
|
|
|
function setTitle(title) {
|
|
|
|
function set(title) {
|
|
|
|
document.title = title;
|
|
|
|
document.getElementById('title').textContent = title;
|
|
|
|
}
|
2021-07-16 23:03:36 +02:00
|
|
|
if(title)
|
2021-07-16 19:41:00 +02:00
|
|
|
set(title);
|
2021-10-26 22:38:05 +02:00
|
|
|
else
|
|
|
|
set('Galène');
|
2021-07-16 19:41:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
2020-12-01 22:42:06 +01:00
|
|
|
* @this {ServerConnection}
|
|
|
|
* @param {string} group
|
2022-02-19 23:43:44 +01:00
|
|
|
* @param {Array<string>} perms
|
2021-07-16 19:41:00 +02:00
|
|
|
* @param {Object<string,any>} status
|
2022-01-29 22:54:44 +01:00
|
|
|
* @param {Object<string,any>} data
|
2023-04-01 13:28:24 +02:00
|
|
|
* @param {string} error
|
2021-07-16 19:41:00 +02:00
|
|
|
* @param {string} message
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
2023-04-01 13:28:24 +02:00
|
|
|
async function gotJoined(kind, group, perms, status, data, error, message) {
|
2020-12-04 22:42:20 +01:00
|
|
|
let present = presentRequested;
|
|
|
|
presentRequested = null;
|
|
|
|
|
|
|
|
switch(kind) {
|
|
|
|
case 'fail':
|
2024-06-11 11:57:15 +02:00
|
|
|
if(probingState === 'probing' && error === 'need-username') {
|
|
|
|
probingState = 'need-username';
|
2023-04-01 13:28:24 +02:00
|
|
|
setVisibility('passwordform', false);
|
|
|
|
} else {
|
|
|
|
token = null;
|
|
|
|
displayError('The server said: ' + message);
|
2024-06-11 11:57:15 +02:00
|
|
|
}
|
2020-12-01 22:42:06 +01:00
|
|
|
this.close();
|
2021-02-04 23:11:40 +01:00
|
|
|
setButtonsVisibility();
|
2020-12-01 22:42:06 +01:00
|
|
|
return;
|
2020-12-04 22:42:20 +01:00
|
|
|
case 'redirect':
|
|
|
|
this.close();
|
2023-04-01 13:28:24 +02:00
|
|
|
token = null;
|
2021-07-16 19:41:00 +02:00
|
|
|
document.location.href = message;
|
2020-12-04 22:42:20 +01:00
|
|
|
return;
|
|
|
|
case 'leave':
|
|
|
|
this.close();
|
2021-02-04 23:11:40 +01:00
|
|
|
setButtonsVisibility();
|
2024-04-11 00:40:49 +02:00
|
|
|
setChangePassword(null);
|
2020-12-04 22:42:20 +01:00
|
|
|
return;
|
|
|
|
case 'join':
|
2020-12-12 13:45:49 +01:00
|
|
|
case 'change':
|
2024-06-11 11:57:15 +02:00
|
|
|
if(probingState === 'probing') {
|
|
|
|
probingState = 'success';
|
|
|
|
setVisibility('userform', false);
|
|
|
|
setVisibility('passwordform', false);
|
|
|
|
this.close();
|
|
|
|
setButtonsVisibility();
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
token = null;
|
|
|
|
}
|
2023-04-01 13:28:24 +02:00
|
|
|
// don't discard endPoint and friends
|
|
|
|
for(let key in status)
|
|
|
|
groupStatus[key] = status[key];
|
2021-07-16 23:03:36 +02:00
|
|
|
setTitle((status && status.displayName) || capitalise(group));
|
2020-12-12 13:45:49 +01:00
|
|
|
displayUsername();
|
|
|
|
setButtonsVisibility();
|
2024-04-11 00:40:49 +02:00
|
|
|
setChangePassword(pwAuth && !!groupStatus.canChangePassword &&
|
|
|
|
serverConnection.username
|
|
|
|
);
|
2020-12-12 13:45:49 +01:00
|
|
|
if(kind === 'change')
|
|
|
|
return;
|
2020-12-04 22:42:20 +01:00
|
|
|
break;
|
|
|
|
default:
|
2023-04-01 13:28:24 +02:00
|
|
|
token = null;
|
2020-12-04 22:42:20 +01:00
|
|
|
displayError('Unknown join message');
|
|
|
|
this.close();
|
|
|
|
return;
|
2020-12-01 22:42:06 +01:00
|
|
|
}
|
|
|
|
|
2020-12-11 18:28:17 +01:00
|
|
|
let input = /** @type{HTMLTextAreaElement} */
|
|
|
|
(document.getElementById('input'));
|
|
|
|
input.placeholder = 'Type /help for help';
|
|
|
|
setTimeout(() => {input.placeholder = '';}, 8000);
|
|
|
|
|
2022-03-22 18:18:04 +01:00
|
|
|
if(status.locked)
|
|
|
|
displayWarning('This group is locked');
|
|
|
|
|
2021-02-14 18:06:50 +01:00
|
|
|
if(typeof RTCPeerConnection === 'undefined')
|
|
|
|
displayWarning("This browser doesn't support WebRTC");
|
|
|
|
else
|
2021-04-28 17:00:50 +02:00
|
|
|
this.request(mapRequest(getSettings().request));
|
2020-12-04 22:42:20 +01:00
|
|
|
|
2023-12-08 18:46:23 +01:00
|
|
|
if(('mediaDevices' in navigator) &&
|
|
|
|
('getUserMedia' in navigator.mediaDevices) &&
|
|
|
|
serverConnection.permissions.indexOf('present') >= 0 &&
|
2022-02-19 23:43:44 +01:00
|
|
|
!findUpMedia('camera')) {
|
2020-12-04 22:42:20 +01:00
|
|
|
if(present) {
|
|
|
|
if(present === 'mike')
|
|
|
|
updateSettings({video: ''});
|
|
|
|
else if(present === 'both')
|
|
|
|
delSetting('video');
|
|
|
|
reflectSettings();
|
|
|
|
|
|
|
|
let button = getButtonElement('presentbutton');
|
|
|
|
button.disabled = true;
|
|
|
|
try {
|
|
|
|
await addLocalMedia();
|
|
|
|
} finally {
|
|
|
|
button.disabled = false;
|
2020-12-01 00:26:14 +01:00
|
|
|
}
|
2020-12-04 22:42:20 +01:00
|
|
|
} else {
|
|
|
|
displayMessage(
|
2021-07-29 14:46:02 +02:00
|
|
|
"Press Enable to enable your camera or microphone"
|
2020-12-04 22:42:20 +01:00
|
|
|
);
|
2020-12-01 00:26:14 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-25 02:25:51 +02:00
|
|
|
}
|
|
|
|
|
2022-01-30 06:32:55 +01:00
|
|
|
/**
|
2022-02-01 18:46:51 +01:00
|
|
|
* @param {TransferredFile} f
|
2022-01-30 06:32:55 +01:00
|
|
|
*/
|
2022-07-23 20:52:04 +02:00
|
|
|
function gotFileTransfer(f) {
|
|
|
|
f.onevent = gotFileTransferEvent;
|
2022-01-30 06:32:55 +01:00
|
|
|
let p = document.createElement('p');
|
2022-02-01 18:46:51 +01:00
|
|
|
if(f.up)
|
2022-01-30 06:32:55 +01:00
|
|
|
p.textContent =
|
|
|
|
`We have offered to send a file called "${f.name}" ` +
|
|
|
|
`to user ${f.username}.`;
|
|
|
|
else
|
|
|
|
p.textContent =
|
|
|
|
`User ${f.username} offered to send us a file ` +
|
|
|
|
`called "${f.name}" of size ${f.size}.`
|
|
|
|
let bno = null, byes = null;
|
2022-07-23 20:52:04 +02:00
|
|
|
if(!f.up) {
|
2022-01-30 06:32:55 +01:00
|
|
|
byes = document.createElement('button');
|
|
|
|
byes.textContent = 'Accept';
|
|
|
|
byes.onclick = function(e) {
|
2022-07-23 20:52:04 +02:00
|
|
|
f.receive();
|
2022-01-30 06:32:55 +01:00
|
|
|
};
|
2022-02-01 18:46:51 +01:00
|
|
|
byes.id = "byes-" + f.fullid();
|
2022-01-30 06:32:55 +01:00
|
|
|
}
|
2022-07-23 20:52:04 +02:00
|
|
|
bno = document.createElement('button');
|
|
|
|
bno.textContent = f.up ? 'Cancel' : 'Reject';
|
|
|
|
bno.onclick = function(e) {
|
|
|
|
f.cancel();
|
|
|
|
};
|
|
|
|
bno.id = "bno-" + f.fullid();
|
2023-01-16 15:51:40 +01:00
|
|
|
let status = document.createElement('span');
|
2022-02-01 18:46:51 +01:00
|
|
|
status.id = 'status-' + f.fullid();
|
|
|
|
if(!f.up) {
|
2022-01-30 19:22:43 +01:00
|
|
|
status.textContent =
|
|
|
|
'(Choosing "Accept" will disclose your IP address.)';
|
|
|
|
}
|
2023-01-16 15:51:40 +01:00
|
|
|
let statusp = document.createElement('p');
|
|
|
|
statusp.id = 'statusp-' + f.fullid();
|
|
|
|
statusp.appendChild(status);
|
2022-01-30 06:32:55 +01:00
|
|
|
let div = document.createElement('div');
|
2022-02-01 18:46:51 +01:00
|
|
|
div.id = 'file-' + f.fullid();
|
2022-01-30 06:32:55 +01:00
|
|
|
div.appendChild(p);
|
|
|
|
if(byes)
|
|
|
|
div.appendChild(byes);
|
|
|
|
if(bno)
|
|
|
|
div.appendChild(bno);
|
2023-01-16 15:51:40 +01:00
|
|
|
div.appendChild(statusp);
|
2022-01-30 06:32:55 +01:00
|
|
|
div.classList.add('message');
|
|
|
|
div.classList.add('message-private');
|
2022-02-01 18:46:51 +01:00
|
|
|
div.classList.add('message-row');
|
2022-01-30 06:32:55 +01:00
|
|
|
let box = document.getElementById('box');
|
|
|
|
box.appendChild(div);
|
|
|
|
return div;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-02-01 18:46:51 +01:00
|
|
|
* @param {TransferredFile} f
|
2022-01-30 06:32:55 +01:00
|
|
|
* @param {string} status
|
2023-01-16 15:51:40 +01:00
|
|
|
* @param {number} [value]
|
2022-01-30 06:32:55 +01:00
|
|
|
*/
|
2023-01-16 15:51:40 +01:00
|
|
|
function setFileStatus(f, status, value) {
|
|
|
|
let statuselt = document.getElementById('status-' + f.fullid());
|
|
|
|
if(!statuselt)
|
|
|
|
throw new Error("Couldn't find statusp");
|
|
|
|
statuselt.textContent = status;
|
|
|
|
if(value) {
|
|
|
|
let progress = document.getElementById('progress-' + f.fullid());
|
|
|
|
if(!progress || !(progress instanceof HTMLProgressElement))
|
|
|
|
throw new Error("Couldn't find progress element");
|
|
|
|
progress.value = value;
|
|
|
|
let label = document.getElementById('progresstext-' + f.fullid());
|
|
|
|
let percent = Math.round(100 * value / progress.max);
|
|
|
|
label.textContent = `${percent}%`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {TransferredFile} f
|
|
|
|
* @param {number} [max]
|
|
|
|
*/
|
|
|
|
function createFileProgress(f, max) {
|
|
|
|
let statusp = document.getElementById('statusp-' + f.fullid());
|
|
|
|
if(!statusp)
|
|
|
|
throw new Error("Couldn't find status div");
|
|
|
|
/** @type HTMLProgressElement */
|
|
|
|
let progress = document.createElement('progress');
|
|
|
|
progress.id = 'progress-' + f.fullid();
|
|
|
|
progress.classList.add('file-progress');
|
|
|
|
progress.max = max;
|
|
|
|
progress.value = 0;
|
|
|
|
statusp.appendChild(progress);
|
|
|
|
let progresstext = document.createElement('span');
|
|
|
|
progresstext.id = 'progresstext-' + f.fullid();
|
|
|
|
progresstext.textContent = '0%';
|
|
|
|
statusp.appendChild(progresstext);
|
2023-01-16 14:39:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {TransferredFile} f
|
|
|
|
* @param {boolean} delyes
|
|
|
|
* @param {boolean} delno
|
2023-01-16 15:51:40 +01:00
|
|
|
* @param {boolean} [delprogress]
|
2023-01-16 14:39:20 +01:00
|
|
|
*/
|
2023-01-16 15:51:40 +01:00
|
|
|
function delFileStatusButtons(f, delyes, delno, delprogress) {
|
2023-01-16 14:39:20 +01:00
|
|
|
let div = document.getElementById('file-' + f.fullid());
|
|
|
|
if(!div)
|
|
|
|
throw new Error("Couldn't find file div");
|
|
|
|
if(delyes) {
|
|
|
|
let byes = document.getElementById('byes-' + f.fullid())
|
|
|
|
if(byes)
|
|
|
|
div.removeChild(byes);
|
|
|
|
}
|
|
|
|
if(delno) {
|
|
|
|
let bno = document.getElementById('bno-' + f.fullid())
|
|
|
|
if(bno)
|
|
|
|
div.removeChild(bno);
|
2022-01-30 06:32:55 +01:00
|
|
|
}
|
2023-01-16 15:51:40 +01:00
|
|
|
if(delprogress) {
|
|
|
|
let statusp = document.getElementById('statusp-' + f.fullid());
|
|
|
|
let progress = document.getElementById('progress-' + f.fullid());
|
|
|
|
let progresstext =
|
|
|
|
document.getElementById('progresstext-' + f.fullid());
|
|
|
|
if(progress)
|
|
|
|
statusp.removeChild(progress);
|
|
|
|
if(progresstext)
|
|
|
|
statusp.removeChild(progresstext);
|
|
|
|
}
|
2022-01-30 06:32:55 +01:00
|
|
|
}
|
|
|
|
|
2022-02-01 13:33:02 +01:00
|
|
|
/**
|
2022-07-23 20:52:04 +02:00
|
|
|
* @this {TransferredFile}
|
|
|
|
* @param {string} state
|
|
|
|
* @param {any} [data]
|
2022-01-30 06:32:55 +01:00
|
|
|
*/
|
2022-07-23 20:52:04 +02:00
|
|
|
function gotFileTransferEvent(state, data) {
|
|
|
|
let f = this;
|
|
|
|
switch(state) {
|
|
|
|
case 'inviting':
|
|
|
|
break;
|
|
|
|
case 'connecting':
|
2023-01-16 15:51:40 +01:00
|
|
|
delFileStatusButtons(f, true, false);
|
2023-01-16 14:39:20 +01:00
|
|
|
setFileStatus(f, 'Connecting...');
|
2023-01-16 15:51:40 +01:00
|
|
|
createFileProgress(f, f.size);
|
2022-07-23 20:52:04 +02:00
|
|
|
break;
|
|
|
|
case 'connected':
|
2023-01-16 15:51:40 +01:00
|
|
|
setFileStatus(f, f.up ? 'Sending...' : 'Receiving...', f.datalen);
|
2022-07-23 20:52:04 +02:00
|
|
|
break;
|
|
|
|
case 'done':
|
2023-01-16 15:51:40 +01:00
|
|
|
delFileStatusButtons(f, true, true, true);
|
2023-01-16 14:39:20 +01:00
|
|
|
setFileStatus(f, 'Done.');
|
2022-07-23 20:52:04 +02:00
|
|
|
if(!f.up) {
|
|
|
|
let url = URL.createObjectURL(data);
|
|
|
|
let a = document.createElement('a');
|
|
|
|
a.href = url;
|
|
|
|
a.textContent = f.name;
|
|
|
|
a.download = f.name;
|
|
|
|
a.type = f.mimetype;
|
|
|
|
a.click();
|
|
|
|
URL.revokeObjectURL(url);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'cancelled':
|
2023-01-16 15:51:40 +01:00
|
|
|
delFileStatusButtons(f, true, true, true);
|
2022-07-23 20:52:04 +02:00
|
|
|
if(data)
|
2023-01-16 14:39:20 +01:00
|
|
|
setFileStatus(f, `Cancelled: ${data.toString()}.`);
|
2022-07-23 20:52:04 +02:00
|
|
|
else
|
2023-01-16 14:39:20 +01:00
|
|
|
setFileStatus(f, 'Cancelled.');
|
2022-07-23 20:52:04 +02:00
|
|
|
break;
|
|
|
|
case 'closed':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.error(`Unexpected state "${state}"`);
|
|
|
|
f.cancel(`unexpected state "${state}" (this shouldn't happen)`);
|
|
|
|
break;
|
2022-01-30 06:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 01:42:26 +01:00
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
* @param {string} dest
|
|
|
|
* @param {string} username
|
2022-12-16 17:54:46 +01:00
|
|
|
* @param {Date} time
|
2020-12-28 01:42:26 +01:00
|
|
|
* @param {boolean} privileged
|
|
|
|
* @param {string} kind
|
2023-03-31 21:04:19 +02:00
|
|
|
* @param {string} error
|
2022-01-30 06:32:55 +01:00
|
|
|
* @param {any} message
|
2020-12-28 01:42:26 +01:00
|
|
|
*/
|
2023-03-31 21:04:19 +02:00
|
|
|
function gotUserMessage(id, dest, username, time, privileged, kind, error, message) {
|
2020-12-28 01:42:26 +01:00
|
|
|
switch(kind) {
|
2021-07-30 19:38:02 +02:00
|
|
|
case 'kicked':
|
2020-12-28 01:42:26 +01:00
|
|
|
case 'error':
|
|
|
|
case 'warning':
|
|
|
|
case 'info':
|
2022-12-17 00:15:07 +01:00
|
|
|
if(!privileged) {
|
2020-12-28 01:42:26 +01:00
|
|
|
console.error(`Got unprivileged message of kind ${kind}`);
|
2022-12-17 00:15:07 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let from = id ? (username || 'Anonymous') : 'The Server';
|
|
|
|
displayError(`${from} said: ${message}`, kind);
|
2020-12-28 01:42:26 +01:00
|
|
|
break;
|
|
|
|
case 'mute':
|
2022-12-17 00:15:07 +01:00
|
|
|
if(!privileged) {
|
2020-12-28 01:42:26 +01:00
|
|
|
console.error(`Got unprivileged message of kind ${kind}`);
|
2022-12-17 00:15:07 +01:00
|
|
|
return;
|
2020-12-28 01:42:26 +01:00
|
|
|
}
|
2022-12-17 00:15:07 +01:00
|
|
|
setLocalMute(true, true);
|
|
|
|
let by = username ? ' by ' + username : '';
|
|
|
|
displayWarning(`You have been muted${by}`);
|
2020-12-28 01:42:26 +01:00
|
|
|
break;
|
2024-08-11 23:57:11 +02:00
|
|
|
case 'clearchat': {
|
2022-12-17 00:15:07 +01:00
|
|
|
if(!privileged) {
|
2021-01-03 17:47:56 +01:00
|
|
|
console.error(`Got unprivileged message of kind ${kind}`);
|
2022-12-17 00:15:07 +01:00
|
|
|
return;
|
2021-01-03 17:47:56 +01:00
|
|
|
}
|
2024-08-11 23:57:11 +02:00
|
|
|
let id = message && message.id;
|
|
|
|
let userId = message && message.userId;
|
|
|
|
clearChat(id, userId);
|
2021-01-04 13:15:50 +01:00
|
|
|
break;
|
2024-08-11 23:57:11 +02:00
|
|
|
}
|
2023-03-31 21:04:19 +02:00
|
|
|
case 'token':
|
|
|
|
if(!privileged) {
|
|
|
|
console.error(`Got unprivileged message of kind ${kind}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(error) {
|
|
|
|
displayError(`Token operation failed: ${message}`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if(typeof message != 'object') {
|
|
|
|
displayError('Unexpected type for token');
|
|
|
|
return;
|
|
|
|
}
|
2023-04-08 21:13:35 +02:00
|
|
|
let f = formatToken(message, false);
|
2023-03-31 21:04:19 +02:00
|
|
|
localMessage(f[0] + ': ' + f[1]);
|
2023-04-04 00:30:20 +02:00
|
|
|
if('share' in navigator) {
|
|
|
|
try {
|
|
|
|
navigator.share({
|
|
|
|
title: `Invitation to Galene group ${message.group}`,
|
|
|
|
text: f[0],
|
|
|
|
url: f[1],
|
|
|
|
});
|
|
|
|
} catch(e) {
|
|
|
|
console.warn("Share failed", e);
|
|
|
|
}
|
|
|
|
}
|
2023-03-31 21:04:19 +02:00
|
|
|
break;
|
|
|
|
case 'tokenlist':
|
|
|
|
if(!privileged) {
|
|
|
|
console.error(`Got unprivileged message of kind ${kind}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(error) {
|
|
|
|
displayError(`Token operation failed: ${message}`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let s = '';
|
|
|
|
for(let i = 0; i < message.length; i++) {
|
2023-04-08 21:13:35 +02:00
|
|
|
let f = formatToken(message[i], true);
|
2023-03-31 21:04:19 +02:00
|
|
|
s = s + f[0] + ': ' + f[1] + "\n";
|
|
|
|
}
|
|
|
|
localMessage(s);
|
|
|
|
break;
|
2024-04-30 18:18:32 +02:00
|
|
|
case 'userinfo':
|
|
|
|
if(!privileged) {
|
|
|
|
console.error(`Got unprivileged message of kind ${kind}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let u = message.username ?
|
|
|
|
'username ' + message.username :
|
|
|
|
'unknown username';
|
|
|
|
let a = message.address ?
|
|
|
|
'address ' + message.address :
|
|
|
|
'unknown address';
|
|
|
|
localMessage(`User ${message.id} has ${u} and ${a}.`);
|
|
|
|
break;
|
2020-12-28 01:42:26 +01:00
|
|
|
default:
|
|
|
|
console.warn(`Got unknown user message ${kind}`);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-31 21:04:19 +02:00
|
|
|
/**
|
|
|
|
* @param {Object} token
|
2023-04-08 21:13:35 +02:00
|
|
|
* @param {boolean} [details]
|
2023-03-31 21:04:19 +02:00
|
|
|
*/
|
2023-04-08 21:13:35 +02:00
|
|
|
function formatToken(token, details) {
|
2023-03-31 21:04:19 +02:00
|
|
|
let url = new URL(window.location.href);
|
|
|
|
let params = new URLSearchParams();
|
|
|
|
params.append('token', token.token);
|
|
|
|
url.search = params.toString();
|
2023-04-08 21:13:35 +02:00
|
|
|
let foruser = '', by = '', togroup = '';
|
2023-03-31 21:04:19 +02:00
|
|
|
if(token.username)
|
|
|
|
foruser = ` for user ${token.username}`;
|
2023-04-08 21:13:35 +02:00
|
|
|
if(details) {
|
|
|
|
if(token.issuedBy)
|
|
|
|
by = ' issued by ' + token.issuedBy;
|
|
|
|
if(token.issuedAt) {
|
|
|
|
if(by === '')
|
|
|
|
by = ' issued at ' + token.issuedAt;
|
|
|
|
else
|
|
|
|
by = by + ' at ' + (new Date(token.issuedAt)).toLocaleString();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(token.group)
|
|
|
|
togroup = ' to group ' + token.group;
|
|
|
|
}
|
2023-05-03 20:04:27 +02:00
|
|
|
let since = '';
|
|
|
|
if(token["not-before"])
|
|
|
|
since = ` since ${(new Date(token['not-before'])).toLocaleString()}`
|
2023-03-31 21:04:19 +02:00
|
|
|
/** @type{Date} */
|
|
|
|
let expires = null;
|
2023-05-03 20:04:27 +02:00
|
|
|
let until = '';
|
|
|
|
if(token.expires) {
|
|
|
|
expires = new Date(token.expires)
|
|
|
|
until = ` until ${expires.toLocaleString()}`;
|
|
|
|
}
|
2023-03-31 21:04:19 +02:00
|
|
|
return [
|
|
|
|
(expires && (expires >= new Date())) ?
|
2023-05-03 20:04:27 +02:00
|
|
|
`Invitation${foruser}${togroup}${by} valid${since}${until}` :
|
2023-04-08 21:13:35 +02:00
|
|
|
`Expired invitation${foruser}${togroup}${by}`,
|
2023-03-31 21:04:19 +02:00
|
|
|
url.toString(),
|
|
|
|
];
|
|
|
|
}
|
2020-12-28 01:42:26 +01:00
|
|
|
|
2020-11-09 00:11:55 +01:00
|
|
|
const urlRegexp = /https?:\/\/[-a-zA-Z0-9@:%/._\\+~#&()=?]+[-a-zA-Z0-9@:%/_\\+~#&()=]/g;
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2020-09-12 16:34:52 +02:00
|
|
|
/**
|
2023-12-08 22:43:18 +01:00
|
|
|
* @param {string} text
|
|
|
|
* @returns {HTMLDivElement}
|
2020-09-12 16:34:52 +02:00
|
|
|
*/
|
2023-12-08 22:43:18 +01:00
|
|
|
function formatText(text) {
|
2020-04-24 19:38:21 +02:00
|
|
|
let r = new RegExp(urlRegexp);
|
|
|
|
let result = [];
|
|
|
|
let pos = 0;
|
|
|
|
while(true) {
|
2023-12-08 22:43:18 +01:00
|
|
|
let m = r.exec(text);
|
2020-04-24 19:38:21 +02:00
|
|
|
if(!m)
|
|
|
|
break;
|
2023-12-08 22:43:18 +01:00
|
|
|
result.push(document.createTextNode(text.slice(pos, m.index)));
|
2020-04-24 19:38:21 +02:00
|
|
|
let a = document.createElement('a');
|
|
|
|
a.href = m[0];
|
|
|
|
a.textContent = m[0];
|
|
|
|
a.target = '_blank';
|
|
|
|
a.rel = 'noreferrer noopener';
|
|
|
|
result.push(a);
|
|
|
|
pos = m.index + m[0].length;
|
|
|
|
}
|
2023-12-08 22:43:18 +01:00
|
|
|
result.push(document.createTextNode(text.slice(pos)));
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2023-12-08 22:43:18 +01:00
|
|
|
let div = document.createElement('div');
|
|
|
|
result.forEach(e => {
|
|
|
|
div.appendChild(e);
|
|
|
|
});
|
|
|
|
return div;
|
2022-04-20 17:38:55 +02:00
|
|
|
}
|
|
|
|
|
2020-09-30 00:33:23 +02:00
|
|
|
/**
|
2022-12-16 17:54:46 +01:00
|
|
|
* @param {Date} time
|
2020-09-30 00:33:23 +02:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function formatTime(time) {
|
2022-12-16 17:54:46 +01:00
|
|
|
let delta = Date.now() - time.getTime();
|
|
|
|
let m = time.getMinutes();
|
2020-11-05 21:09:16 +01:00
|
|
|
if(delta > -30000)
|
2022-12-16 17:54:46 +01:00
|
|
|
return time.getHours() + ':' + ((m < 10) ? '0' : '') + m;
|
|
|
|
return time.toLocaleString();
|
2020-09-30 00:33:23 +02:00
|
|
|
}
|
|
|
|
|
2020-08-24 22:37:48 +02:00
|
|
|
/**
|
|
|
|
* @typedef {Object} lastMessage
|
|
|
|
* @property {string} [nick]
|
|
|
|
* @property {string} [peerId]
|
2020-10-01 16:52:01 +02:00
|
|
|
* @property {string} [dest]
|
2022-12-16 17:54:46 +01:00
|
|
|
* @property {Date} [time]
|
2020-08-24 22:37:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/** @type {lastMessage} */
|
2020-04-24 19:38:21 +02:00
|
|
|
let lastMessage = {};
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
/**
|
|
|
|
* @param {string} peerId
|
2021-07-31 14:42:26 +02:00
|
|
|
* @param {string} dest
|
2020-09-20 14:33:13 +02:00
|
|
|
* @param {string} nick
|
2022-12-16 17:54:46 +01:00
|
|
|
* @param {Date} time
|
2021-07-31 14:42:26 +02:00
|
|
|
* @param {boolean} privileged
|
|
|
|
* @param {boolean} history
|
2020-09-20 14:33:13 +02:00
|
|
|
* @param {string} kind
|
2023-12-08 22:43:18 +01:00
|
|
|
* @param {string|HTMLElement} message
|
2020-09-20 14:33:13 +02:00
|
|
|
*/
|
2024-08-11 19:00:17 +02:00
|
|
|
function addToChatbox(id, peerId, dest, nick, time, privileged, history, kind, message) {
|
2024-07-31 20:22:57 +02:00
|
|
|
if(kind === 'caption') {
|
|
|
|
displayCaption(message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-27 21:17:46 +02:00
|
|
|
let row = document.createElement('div');
|
|
|
|
row.classList.add('message-row');
|
2020-04-24 19:38:21 +02:00
|
|
|
let container = document.createElement('div');
|
|
|
|
container.classList.add('message');
|
2020-08-27 21:17:46 +02:00
|
|
|
row.appendChild(container);
|
2020-10-12 12:48:12 +02:00
|
|
|
let footer = document.createElement('p');
|
|
|
|
footer.classList.add('message-footer');
|
2020-09-23 21:01:29 +02:00
|
|
|
if(!peerId)
|
|
|
|
container.classList.add('message-system');
|
2022-03-25 16:30:31 +01:00
|
|
|
if(serverConnection && peerId === serverConnection.id)
|
2020-09-23 21:01:29 +02:00
|
|
|
container.classList.add('message-sender');
|
2020-10-01 16:52:01 +02:00
|
|
|
if(dest)
|
|
|
|
container.classList.add('message-private');
|
|
|
|
|
2024-08-11 19:00:17 +02:00
|
|
|
if(id)
|
|
|
|
container.dataset.id = id;
|
2024-05-02 00:39:44 +02:00
|
|
|
if(peerId) {
|
|
|
|
container.dataset.peerId = peerId;
|
|
|
|
container.dataset.username = nick;
|
|
|
|
container.addEventListener('click', function(e) {
|
|
|
|
if(e.detail !== 2)
|
|
|
|
return;
|
|
|
|
let elt = e.currentTarget;
|
|
|
|
if(!elt || !(elt instanceof HTMLElement))
|
|
|
|
throw new Error("Couldn't find chat message div");
|
|
|
|
chatMessageMenu(elt);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-08 22:43:18 +01:00
|
|
|
/** @type{HTMLElement} */
|
|
|
|
let body;
|
|
|
|
if(message instanceof HTMLElement) {
|
|
|
|
body = message;
|
|
|
|
} else if(typeof message === 'string') {
|
|
|
|
body = formatText(message);
|
|
|
|
} else {
|
|
|
|
throw new Error('Cannot add element to chatbox');
|
|
|
|
}
|
|
|
|
|
2020-08-12 12:17:56 +02:00
|
|
|
if(kind !== 'me') {
|
2020-12-01 02:51:15 +01:00
|
|
|
let doHeader = true;
|
2022-09-06 19:03:50 +02:00
|
|
|
if(lastMessage.nick !== (nick || null) ||
|
|
|
|
lastMessage.peerId !== (peerId || null) ||
|
|
|
|
lastMessage.dest !== (dest || null) ||
|
|
|
|
!time || !lastMessage.time) {
|
2020-12-01 02:51:15 +01:00
|
|
|
doHeader = true;
|
|
|
|
} else {
|
2022-12-16 17:54:46 +01:00
|
|
|
let delta = time.getTime() - lastMessage.time.getTime();
|
2020-12-01 02:51:15 +01:00
|
|
|
doHeader = delta < 0 || delta > 60000;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(doHeader) {
|
2020-09-30 00:33:23 +02:00
|
|
|
let header = document.createElement('p');
|
2022-09-06 19:03:50 +02:00
|
|
|
let user = document.createElement('span');
|
|
|
|
let u = dest && serverConnection.users[dest];
|
|
|
|
let name = (u && u.username);
|
|
|
|
user.textContent = dest ?
|
|
|
|
`${nick || '(anon)'} \u2192 ${name || '(anon)'}` :
|
|
|
|
(nick || '(anon)');
|
|
|
|
user.classList.add('message-user');
|
|
|
|
header.appendChild(user);
|
2020-09-30 00:33:23 +02:00
|
|
|
header.classList.add('message-header');
|
|
|
|
container.appendChild(header);
|
|
|
|
if(time) {
|
|
|
|
let tm = document.createElement('span');
|
|
|
|
tm.textContent = formatTime(time);
|
|
|
|
tm.classList.add('message-time');
|
|
|
|
header.appendChild(tm);
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-12-01 02:51:15 +01:00
|
|
|
|
2023-12-08 22:43:18 +01:00
|
|
|
let p = document.createElement('p');
|
|
|
|
p.appendChild(body);
|
2020-04-24 19:38:21 +02:00
|
|
|
p.classList.add('message-content');
|
|
|
|
container.appendChild(p);
|
2020-10-08 15:18:22 +02:00
|
|
|
lastMessage.nick = (nick || null);
|
2020-04-24 19:38:21 +02:00
|
|
|
lastMessage.peerId = peerId;
|
2020-10-01 16:52:01 +02:00
|
|
|
lastMessage.dest = (dest || null);
|
2020-12-01 02:51:15 +01:00
|
|
|
lastMessage.time = (time || null);
|
2020-04-24 19:38:21 +02:00
|
|
|
} else {
|
|
|
|
let asterisk = document.createElement('span');
|
|
|
|
asterisk.textContent = '*';
|
|
|
|
asterisk.classList.add('message-me-asterisk');
|
|
|
|
let user = document.createElement('span');
|
2020-10-08 15:18:22 +02:00
|
|
|
user.textContent = nick || '(anon)';
|
2020-04-24 19:38:21 +02:00
|
|
|
user.classList.add('message-me-user');
|
2023-12-08 22:43:18 +01:00
|
|
|
body.classList.add('message-me-content');
|
2020-04-24 19:38:21 +02:00
|
|
|
container.appendChild(asterisk);
|
|
|
|
container.appendChild(user);
|
2023-12-08 22:43:18 +01:00
|
|
|
container.appendChild(body);
|
2020-04-24 19:38:21 +02:00
|
|
|
container.classList.add('message-me');
|
2020-10-01 16:52:01 +02:00
|
|
|
lastMessage = {};
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-12-28 01:47:23 +01:00
|
|
|
container.appendChild(footer);
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2020-05-11 02:58:48 +02:00
|
|
|
let box = document.getElementById('box');
|
2020-08-27 21:17:46 +02:00
|
|
|
box.appendChild(row);
|
2020-04-24 19:38:21 +02:00
|
|
|
if(box.scrollHeight > box.clientHeight) {
|
|
|
|
box.scrollTop = box.scrollHeight - box.clientHeight;
|
|
|
|
}
|
|
|
|
|
2024-07-31 20:22:57 +02:00
|
|
|
return;
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2024-05-02 00:39:44 +02:00
|
|
|
/**
|
|
|
|
* @param {HTMLElement} elt
|
|
|
|
*/
|
|
|
|
function chatMessageMenu(elt) {
|
|
|
|
if(!(serverConnection && serverConnection.permissions &&
|
|
|
|
serverConnection.permissions.indexOf('op') >= 0))
|
|
|
|
return;
|
|
|
|
|
2024-08-11 23:57:11 +02:00
|
|
|
let messageId = elt.dataset.id;
|
2024-05-02 00:39:44 +02:00
|
|
|
let peerId = elt.dataset.peerId;
|
|
|
|
if(!peerId)
|
|
|
|
return;
|
|
|
|
let username = elt.dataset.username;
|
2024-08-11 23:57:11 +02:00
|
|
|
let u = username || 'user';
|
2024-05-02 00:39:44 +02:00
|
|
|
|
|
|
|
let items = [];
|
2024-08-11 23:57:11 +02:00
|
|
|
if(messageId)
|
|
|
|
items.push({label: 'Delete message', onClick: () => {
|
|
|
|
serverConnection.groupAction('clearchat', {
|
|
|
|
id: messageId,
|
|
|
|
userId: peerId,
|
|
|
|
});
|
|
|
|
}});
|
|
|
|
items.push({label: `Delete all from ${u}`,
|
|
|
|
onClick: () => {
|
|
|
|
serverConnection.groupAction('clearchat', {
|
|
|
|
userId: peerId,
|
|
|
|
});
|
|
|
|
}});
|
|
|
|
items.push({label: `Identify ${u}`, onClick: () => {
|
2024-05-02 00:39:44 +02:00
|
|
|
serverConnection.userAction('identify', peerId);
|
|
|
|
}});
|
2024-08-11 23:57:11 +02:00
|
|
|
items.push({label: `Kick out ${u}`, onClick: () => {
|
2024-05-02 00:39:44 +02:00
|
|
|
serverConnection.userAction('kick', peerId);
|
|
|
|
}});
|
|
|
|
|
|
|
|
/** @ts-ignore */
|
|
|
|
new Contextual({
|
|
|
|
items: items,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-07-31 20:22:57 +02:00
|
|
|
/**
|
|
|
|
* @param {string|HTMLElement} message
|
|
|
|
*/
|
|
|
|
function setCaption(message) {
|
|
|
|
let container = document.getElementById('captions-container');
|
|
|
|
let captions = document.getElementById('captions');
|
|
|
|
if(!message) {
|
|
|
|
captions.replaceChildren();
|
|
|
|
container.classList.add('invisible');
|
|
|
|
} else {
|
|
|
|
if(message instanceof HTMLElement)
|
|
|
|
captions.replaceChildren(message);
|
|
|
|
else
|
|
|
|
captions.textContent = message;
|
|
|
|
container.classList.remove('invisible');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let captionsTimer = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string|HTMLElement} message
|
|
|
|
*/
|
|
|
|
function displayCaption(message) {
|
|
|
|
if(captionsTimer != null) {
|
|
|
|
clearTimeout(captionsTimer);
|
|
|
|
captionsTimer = null;
|
|
|
|
}
|
|
|
|
setCaption(message);
|
|
|
|
captionsTimer = setTimeout(() => setCaption(null), 3000);
|
|
|
|
}
|
|
|
|
|
2021-01-11 19:28:57 +01:00
|
|
|
/**
|
2023-12-08 22:43:18 +01:00
|
|
|
* @param {string|HTMLElement} message
|
2021-01-11 19:28:57 +01:00
|
|
|
*/
|
|
|
|
function localMessage(message) {
|
2024-08-11 19:00:17 +02:00
|
|
|
return addToChatbox(null, null, null, null, new Date(), false, false, '', message);
|
2021-01-11 19:28:57 +01:00
|
|
|
}
|
|
|
|
|
2024-08-11 23:57:11 +02:00
|
|
|
/**
|
|
|
|
* @param {string} [id]
|
|
|
|
* @param {string} [userId]
|
|
|
|
*/
|
|
|
|
function clearChat(id, userId) {
|
2020-04-25 21:16:49 +02:00
|
|
|
lastMessage = {};
|
2024-08-11 23:57:11 +02:00
|
|
|
|
|
|
|
let box = document.getElementById('box');
|
|
|
|
if(!id && !userId) {
|
|
|
|
box.textContent = '';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let elts = box.children;
|
|
|
|
for(let i = 0; i < elts.length; i++) {
|
|
|
|
let row = elts.item(i);
|
|
|
|
if(!(row instanceof HTMLDivElement))
|
|
|
|
continue;
|
|
|
|
let div = row.firstChild;
|
|
|
|
console.log(div);
|
|
|
|
if(!(div instanceof HTMLDivElement))
|
|
|
|
continue;
|
|
|
|
if((!id || div.dataset.id === id) && div.dataset.peerId === userId)
|
|
|
|
box.removeChild(row);
|
|
|
|
}
|
2020-04-25 21:16:49 +02:00
|
|
|
}
|
|
|
|
|
2020-11-24 14:55:52 +01:00
|
|
|
/**
|
|
|
|
* A command known to the command-line parser.
|
|
|
|
*
|
|
|
|
* @typedef {Object} command
|
|
|
|
* @property {string} [parameters]
|
|
|
|
* - A user-readable list of parameters.
|
|
|
|
* @property {string} [description]
|
|
|
|
* - A user-readable description, null if undocumented.
|
|
|
|
* @property {() => string} [predicate]
|
|
|
|
* - Returns null if the command is available.
|
|
|
|
* @property {(c: string, r: string) => void} f
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The set of commands known to the command-line parser.
|
|
|
|
*
|
|
|
|
* @type {Object.<string,command>}
|
|
|
|
*/
|
2020-12-26 17:28:44 +01:00
|
|
|
let commands = {};
|
2020-11-24 14:55:52 +01:00
|
|
|
|
|
|
|
function operatorPredicate() {
|
2020-12-01 03:01:19 +01:00
|
|
|
if(serverConnection && serverConnection.permissions &&
|
2022-02-19 23:43:44 +01:00
|
|
|
serverConnection.permissions.indexOf('op') >= 0)
|
2020-11-24 14:55:52 +01:00
|
|
|
return null;
|
|
|
|
return 'You are not an operator';
|
|
|
|
}
|
|
|
|
|
|
|
|
function recordingPredicate() {
|
2020-12-01 03:01:19 +01:00
|
|
|
if(serverConnection && serverConnection.permissions &&
|
2022-02-19 23:43:44 +01:00
|
|
|
serverConnection.permissions.indexOf('record') >= 0)
|
2020-11-24 14:55:52 +01:00
|
|
|
return null;
|
|
|
|
return 'You are not allowed to record';
|
|
|
|
}
|
|
|
|
|
|
|
|
commands.help = {
|
|
|
|
description: 'display this help',
|
|
|
|
f: (c, r) => {
|
|
|
|
/** @type {string[]} */
|
|
|
|
let cs = [];
|
|
|
|
for(let cmd in commands) {
|
|
|
|
let c = commands[cmd];
|
|
|
|
if(!c.description)
|
|
|
|
continue;
|
|
|
|
if(c.predicate && c.predicate())
|
|
|
|
continue;
|
|
|
|
cs.push(`/${cmd}${c.parameters?' ' + c.parameters:''}: ${c.description}`);
|
|
|
|
}
|
2021-08-07 15:42:16 +02:00
|
|
|
localMessage(cs.sort().join('\n'));
|
2020-11-24 14:55:52 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
commands.me = {
|
|
|
|
f: (c, r) => {
|
|
|
|
// handled as a special case
|
|
|
|
throw new Error("this shouldn't happen");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
commands.set = {
|
|
|
|
f: (c, r) => {
|
|
|
|
if(!r) {
|
|
|
|
let settings = getSettings();
|
|
|
|
let s = "";
|
|
|
|
for(let key in settings)
|
|
|
|
s = s + `${key}: ${JSON.stringify(settings[key])}\n`;
|
2021-01-11 19:28:57 +01:00
|
|
|
localMessage(s);
|
2020-11-24 14:55:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let p = parseCommand(r);
|
|
|
|
let value;
|
|
|
|
if(p[1]) {
|
2020-12-26 17:28:44 +01:00
|
|
|
value = JSON.parse(p[1]);
|
2020-11-24 14:55:52 +01:00
|
|
|
} else {
|
|
|
|
value = true;
|
|
|
|
}
|
|
|
|
updateSetting(p[0], value);
|
|
|
|
reflectSettings();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
commands.unset = {
|
|
|
|
f: (c, r) => {
|
|
|
|
delSetting(r.trim());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
commands.leave = {
|
|
|
|
description: "leave group",
|
|
|
|
f: (c, r) => {
|
2020-12-01 03:01:19 +01:00
|
|
|
if(!serverConnection)
|
|
|
|
throw new Error('Not connected');
|
2020-11-24 14:55:52 +01:00
|
|
|
serverConnection.close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
commands.clear = {
|
|
|
|
predicate: operatorPredicate,
|
|
|
|
description: 'clear the chat history',
|
|
|
|
f: (c, r) => {
|
2021-01-03 12:04:39 +01:00
|
|
|
serverConnection.groupAction('clearchat');
|
2020-11-24 14:55:52 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
commands.lock = {
|
|
|
|
predicate: operatorPredicate,
|
|
|
|
description: 'lock this group',
|
|
|
|
parameters: '[message]',
|
|
|
|
f: (c, r) => {
|
2021-01-03 12:04:39 +01:00
|
|
|
serverConnection.groupAction('lock', r);
|
2020-11-24 14:55:52 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
commands.unlock = {
|
|
|
|
predicate: operatorPredicate,
|
|
|
|
description: 'unlock this group, revert the effect of /lock',
|
|
|
|
f: (c, r) => {
|
2021-01-03 12:04:39 +01:00
|
|
|
serverConnection.groupAction('unlock');
|
2020-11-24 14:55:52 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
commands.record = {
|
|
|
|
predicate: recordingPredicate,
|
|
|
|
description: 'start recording',
|
|
|
|
f: (c, r) => {
|
2021-01-03 12:04:39 +01:00
|
|
|
serverConnection.groupAction('record');
|
2020-11-24 14:55:52 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
commands.unrecord = {
|
|
|
|
predicate: recordingPredicate,
|
|
|
|
description: 'stop recording',
|
|
|
|
f: (c, r) => {
|
2021-01-03 12:04:39 +01:00
|
|
|
serverConnection.groupAction('unrecord');
|
2020-11-24 14:55:52 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-02 19:47:32 +01:00
|
|
|
commands.subgroups = {
|
|
|
|
predicate: operatorPredicate,
|
|
|
|
description: 'list subgroups',
|
|
|
|
f: (c, r) => {
|
2021-01-03 12:04:39 +01:00
|
|
|
serverConnection.groupAction('subgroups');
|
2020-12-02 19:47:32 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-31 21:04:19 +02:00
|
|
|
/**
|
|
|
|
* @type {Object<string,number>}
|
|
|
|
*/
|
|
|
|
const units = {
|
|
|
|
s: 1000,
|
|
|
|
min: 60 * 1000,
|
|
|
|
h: 60 * 60 * 1000,
|
|
|
|
d: 24 * 60 * 60 * 1000,
|
|
|
|
mon: 31 * 24 * 60 * 60 * 1000,
|
|
|
|
yr: 365 * 24 * 60 * 60 * 1000,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} s
|
2023-05-03 18:48:35 +02:00
|
|
|
* @returns {Date|number}
|
2023-03-31 21:04:19 +02:00
|
|
|
*/
|
|
|
|
function parseExpiration(s) {
|
|
|
|
if(!s)
|
2023-05-03 18:48:35 +02:00
|
|
|
return null;
|
2023-03-31 21:04:19 +02:00
|
|
|
let re = /^([0-9]+)(s|min|h|d|yr)$/
|
|
|
|
let e = re.exec(s)
|
|
|
|
if(e) {
|
|
|
|
let unit = units[e[2]];
|
|
|
|
if(!unit)
|
|
|
|
throw new Error(`Couldn't find unit ${e[2]}`);
|
|
|
|
return parseInt(e[1]) * unit;
|
|
|
|
}
|
|
|
|
let d = new Date(s);
|
|
|
|
if(d.toString() === 'Invalid Date')
|
|
|
|
throw new Error("Couldn't parse expiration date");
|
2023-05-03 18:48:35 +02:00
|
|
|
return d;
|
2023-03-31 21:04:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeTokenPredicate() {
|
2023-07-16 15:54:58 +02:00
|
|
|
return (serverConnection.permissions.indexOf('token') < 0 ?
|
|
|
|
"You don't have permission to create tokens" : null);
|
2023-03-31 21:04:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function editTokenPredicate() {
|
2023-07-16 15:54:58 +02:00
|
|
|
return (serverConnection.permissions.indexOf('token') < 0 ||
|
|
|
|
serverConnection.permissions.indexOf('op') < 0 ?
|
|
|
|
"You don't have permission to edit or list tokens" : null);
|
2023-03-31 21:04:19 +02:00
|
|
|
}
|
|
|
|
|
2023-04-04 01:03:02 +02:00
|
|
|
/**
|
2023-05-03 18:48:35 +02:00
|
|
|
* @param {Object} [template]
|
2023-04-04 01:03:02 +02:00
|
|
|
*/
|
2023-05-03 18:48:35 +02:00
|
|
|
function makeToken(template) {
|
|
|
|
if(!template)
|
|
|
|
template = {};
|
2023-04-04 01:03:02 +02:00
|
|
|
let v = {
|
|
|
|
group: group,
|
2023-05-03 18:48:35 +02:00
|
|
|
}
|
|
|
|
if('username' in template)
|
|
|
|
v.username = template.username;
|
|
|
|
if('expires' in template)
|
|
|
|
v.expires = template.expires;
|
2023-04-04 01:03:02 +02:00
|
|
|
else
|
|
|
|
v.expires = units.d;
|
2023-05-03 18:48:35 +02:00
|
|
|
if('not-before' in template)
|
|
|
|
v["not-before"] = template["not-before"];
|
|
|
|
if('permissions' in template)
|
|
|
|
v.permissions = template.permissions;
|
2024-05-08 16:00:18 +02:00
|
|
|
else {
|
2023-04-04 01:03:02 +02:00
|
|
|
v.permissions = [];
|
2024-05-08 16:00:18 +02:00
|
|
|
if(serverConnection.permissions.indexOf('present') >= 0)
|
|
|
|
v.permissions.push('present');
|
|
|
|
if(serverConnection.permissions.indexOf('message') >= 0)
|
|
|
|
v.permissions.push('message');
|
|
|
|
}
|
2023-04-04 01:03:02 +02:00
|
|
|
serverConnection.groupAction('maketoken', v);
|
|
|
|
}
|
|
|
|
|
2023-03-31 21:04:19 +02:00
|
|
|
commands.invite = {
|
|
|
|
predicate: makeTokenPredicate,
|
|
|
|
description: "create an invitation link",
|
|
|
|
parameters: "[username] [expiration]",
|
|
|
|
f: (c, r) => {
|
|
|
|
let p = parseCommand(r);
|
2023-05-03 18:48:35 +02:00
|
|
|
let template = {};
|
|
|
|
if(p[0])
|
|
|
|
template.username = p[0];
|
|
|
|
let expires = parseExpiration(p[1]);
|
|
|
|
if(expires)
|
|
|
|
template.expires = expires;
|
|
|
|
makeToken(template);
|
2023-03-31 21:04:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} t
|
|
|
|
*/
|
|
|
|
function parseToken(t) {
|
|
|
|
let m = /^https?:\/\/.*?token=([^?]+)/.exec(t);
|
|
|
|
if(m) {
|
|
|
|
return m[1];
|
|
|
|
} else if(!/^https?:\/\//.exec(t)) {
|
|
|
|
return t
|
|
|
|
} else {
|
|
|
|
throw new Error("Couldn't parse link");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
commands.reinvite = {
|
|
|
|
predicate: editTokenPredicate,
|
|
|
|
description: "extend an invitation link",
|
|
|
|
parameters: "link [expiration]",
|
|
|
|
f: (c, r) => {
|
|
|
|
let p = parseCommand(r);
|
|
|
|
let v = {}
|
|
|
|
v.token = parseToken(p[0]);
|
|
|
|
if(p[1])
|
|
|
|
v.expires = parseExpiration(p[1]);
|
|
|
|
else
|
|
|
|
v.expires = units.d;
|
|
|
|
serverConnection.groupAction('edittoken', v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
commands.revoke = {
|
|
|
|
predicate: editTokenPredicate,
|
|
|
|
description: "revoke an invitation link",
|
|
|
|
parameters: "link",
|
|
|
|
f: (c, r) => {
|
|
|
|
let token = parseToken(r);
|
|
|
|
serverConnection.groupAction('edittoken', {
|
|
|
|
token: token,
|
|
|
|
expires: -units.s,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
commands.listtokens = {
|
|
|
|
predicate: editTokenPredicate,
|
|
|
|
description: "list invitation links",
|
|
|
|
f: (c, r) => {
|
|
|
|
serverConnection.groupAction('listtokens');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 20:41:58 +01:00
|
|
|
function renegotiateStreams() {
|
|
|
|
for(let id in serverConnection.up)
|
|
|
|
serverConnection.up[id].restartIce();
|
|
|
|
for(let id in serverConnection.down)
|
|
|
|
serverConnection.down[id].restartIce();
|
|
|
|
}
|
|
|
|
|
2020-12-05 21:02:28 +01:00
|
|
|
commands.renegotiate = {
|
|
|
|
description: 'renegotiate media streams',
|
|
|
|
f: (c, r) => {
|
2022-02-16 20:41:58 +01:00
|
|
|
renegotiateStreams();
|
2020-12-05 21:02:28 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-06 23:39:27 +02:00
|
|
|
commands.replace = {
|
|
|
|
f: (c, r) => {
|
|
|
|
replaceUpStreams(null);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-08 15:14:45 +02:00
|
|
|
commands.stopshare = {
|
|
|
|
description: 'stop screen share',
|
|
|
|
f: (c, r) => {
|
|
|
|
closeUpMedia('screenshare');
|
|
|
|
}
|
|
|
|
}
|
2021-05-06 23:39:27 +02:00
|
|
|
|
2020-09-16 01:42:39 +02:00
|
|
|
/**
|
|
|
|
* parseCommand splits a string into two space-separated parts. The first
|
|
|
|
* part may be quoted and may include backslash escapes.
|
|
|
|
*
|
|
|
|
* @param {string} line
|
2020-09-20 14:33:13 +02:00
|
|
|
* @returns {string[]}
|
2020-09-16 01:42:39 +02:00
|
|
|
*/
|
|
|
|
function parseCommand(line) {
|
|
|
|
let i = 0;
|
|
|
|
while(i < line.length && line[i] === ' ')
|
|
|
|
i++;
|
|
|
|
let start = ' ';
|
|
|
|
if(i < line.length && line[i] === '"' || line[i] === "'") {
|
|
|
|
start = line[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
let first = "";
|
|
|
|
while(i < line.length) {
|
|
|
|
if(line[i] === start) {
|
|
|
|
if(start !== ' ')
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(line[i] === '\\' && i < line.length - 1)
|
|
|
|
i++;
|
|
|
|
first = first + line[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(i < line.length && line[i] === ' ')
|
|
|
|
i++;
|
|
|
|
return [first, line.slice(i)];
|
|
|
|
}
|
|
|
|
|
2020-11-24 14:55:52 +01:00
|
|
|
/**
|
|
|
|
* @param {string} user
|
|
|
|
*/
|
|
|
|
function findUserId(user) {
|
2021-04-27 18:58:21 +02:00
|
|
|
if(user in serverConnection.users)
|
2020-11-24 14:55:52 +01:00
|
|
|
return user;
|
|
|
|
|
2021-04-27 18:58:21 +02:00
|
|
|
for(let id in serverConnection.users) {
|
|
|
|
let u = serverConnection.users[id];
|
|
|
|
if(u && u.username === user)
|
2020-11-24 14:55:52 +01:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
commands.msg = {
|
|
|
|
parameters: 'user message',
|
|
|
|
description: 'send a private message',
|
|
|
|
f: (c, r) => {
|
|
|
|
let p = parseCommand(r);
|
|
|
|
if(!p[0])
|
|
|
|
throw new Error('/msg requires parameters');
|
|
|
|
let id = findUserId(p[0]);
|
|
|
|
if(!id)
|
|
|
|
throw new Error(`Unknown user ${p[0]}`);
|
2021-01-03 12:04:39 +01:00
|
|
|
serverConnection.chat('', id, p[1]);
|
2024-08-11 19:00:17 +02:00
|
|
|
addToChatbox(serverConnection.id, null, id, serverConnection.username,
|
2022-12-16 17:54:46 +01:00
|
|
|
new Date(), false, false, '', p[1]);
|
2020-11-24 14:55:52 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
@param {string} c
|
|
|
|
@param {string} r
|
|
|
|
*/
|
|
|
|
function userCommand(c, r) {
|
|
|
|
let p = parseCommand(r);
|
|
|
|
if(!p[0])
|
|
|
|
throw new Error(`/${c} requires parameters`);
|
|
|
|
let id = findUserId(p[0]);
|
|
|
|
if(!id)
|
|
|
|
throw new Error(`Unknown user ${p[0]}`);
|
2021-01-03 12:04:39 +01:00
|
|
|
serverConnection.userAction(c, id, p[1]);
|
2020-11-24 14:55:52 +01:00
|
|
|
}
|
|
|
|
|
2020-11-30 14:22:36 +01:00
|
|
|
function userMessage(c, r) {
|
|
|
|
let p = parseCommand(r);
|
|
|
|
if(!p[0])
|
|
|
|
throw new Error(`/${c} requires parameters`);
|
|
|
|
let id = findUserId(p[0]);
|
|
|
|
if(!id)
|
|
|
|
throw new Error(`Unknown user ${p[0]}`);
|
2021-01-03 12:04:39 +01:00
|
|
|
serverConnection.userMessage(c, id, p[1]);
|
2020-11-30 14:22:36 +01:00
|
|
|
}
|
2020-11-24 14:55:52 +01:00
|
|
|
|
|
|
|
commands.kick = {
|
|
|
|
parameters: 'user [message]',
|
|
|
|
description: 'kick out a user',
|
|
|
|
predicate: operatorPredicate,
|
|
|
|
f: userCommand,
|
|
|
|
};
|
|
|
|
|
2024-04-30 18:18:32 +02:00
|
|
|
commands.identify = {
|
|
|
|
parameters: 'user [message]',
|
|
|
|
description: 'identify a user',
|
|
|
|
predicate: operatorPredicate,
|
|
|
|
f: userCommand,
|
|
|
|
};
|
|
|
|
|
2020-11-24 14:55:52 +01:00
|
|
|
commands.op = {
|
|
|
|
parameters: 'user',
|
|
|
|
description: 'give operator status',
|
|
|
|
predicate: operatorPredicate,
|
|
|
|
f: userCommand,
|
|
|
|
};
|
|
|
|
|
|
|
|
commands.unop = {
|
|
|
|
parameters: 'user',
|
|
|
|
description: 'revoke operator status',
|
|
|
|
predicate: operatorPredicate,
|
|
|
|
f: userCommand,
|
|
|
|
};
|
|
|
|
|
|
|
|
commands.present = {
|
|
|
|
parameters: 'user',
|
|
|
|
description: 'give user the right to present',
|
|
|
|
predicate: operatorPredicate,
|
|
|
|
f: userCommand,
|
|
|
|
};
|
|
|
|
|
|
|
|
commands.unpresent = {
|
|
|
|
parameters: 'user',
|
|
|
|
description: 'revoke the right to present',
|
|
|
|
predicate: operatorPredicate,
|
|
|
|
f: userCommand,
|
|
|
|
};
|
|
|
|
|
2024-05-08 16:00:18 +02:00
|
|
|
commands.shutup = {
|
|
|
|
parameters: 'user',
|
|
|
|
description: 'revoke the right to send chat messages',
|
|
|
|
predicate: operatorPredicate,
|
|
|
|
f: userCommand,
|
|
|
|
};
|
|
|
|
|
|
|
|
commands.unshutup = {
|
|
|
|
parameters: 'user',
|
|
|
|
description: 'give the right to send chat messages',
|
|
|
|
predicate: operatorPredicate,
|
|
|
|
f: userCommand,
|
|
|
|
};
|
|
|
|
|
2020-11-30 14:22:36 +01:00
|
|
|
commands.mute = {
|
|
|
|
parameters: 'user',
|
|
|
|
description: 'mute a remote user',
|
|
|
|
predicate: operatorPredicate,
|
|
|
|
f: userMessage,
|
|
|
|
};
|
|
|
|
|
2021-01-11 16:30:19 +01:00
|
|
|
commands.muteall = {
|
|
|
|
description: 'mute all remote users',
|
|
|
|
predicate: operatorPredicate,
|
|
|
|
f: (c, r) => {
|
|
|
|
serverConnection.userMessage('mute', null, null, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 15:48:23 +01:00
|
|
|
commands.warn = {
|
|
|
|
parameters: 'user message',
|
|
|
|
description: 'send a warning to a user',
|
|
|
|
predicate: operatorPredicate,
|
|
|
|
f: (c, r) => {
|
|
|
|
userMessage('warning', r);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
commands.wall = {
|
|
|
|
parameters: 'message',
|
|
|
|
description: 'send a warning to all users',
|
|
|
|
predicate: operatorPredicate,
|
|
|
|
f: (c, r) => {
|
|
|
|
if(!r)
|
|
|
|
throw new Error('empty message');
|
2021-01-03 12:04:39 +01:00
|
|
|
serverConnection.userMessage('warning', '', r);
|
2020-11-30 15:48:23 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-28 14:45:45 +02:00
|
|
|
commands.raise = {
|
|
|
|
description: 'raise hand',
|
|
|
|
f: (c, r) => {
|
|
|
|
serverConnection.userAction(
|
2022-01-29 22:54:44 +01:00
|
|
|
"setdata", serverConnection.id, {"raisehand": true},
|
2021-04-28 14:45:45 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
commands.unraise = {
|
|
|
|
description: 'unraise hand',
|
|
|
|
f: (c, r) => {
|
|
|
|
serverConnection.userAction(
|
2022-01-29 22:54:44 +01:00
|
|
|
"setdata", serverConnection.id, {"raisehand": null},
|
2021-04-28 14:45:45 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 18:36:36 +01:00
|
|
|
/** @returns {boolean} */
|
|
|
|
function canFile() {
|
|
|
|
let v =
|
|
|
|
/** @ts-ignore */
|
|
|
|
!!HTMLVideoElement.prototype.captureStream ||
|
|
|
|
/** @ts-ignore */
|
|
|
|
!!HTMLVideoElement.prototype.mozCaptureStream;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:32:32 +01:00
|
|
|
function presentFile() {
|
|
|
|
let input = document.createElement('input');
|
|
|
|
input.type = 'file';
|
|
|
|
input.accept="audio/*,video/*";
|
|
|
|
input.onchange = function(e) {
|
|
|
|
if(!(this instanceof HTMLInputElement))
|
|
|
|
throw new Error('Unexpected type for this');
|
|
|
|
let files = this.files;
|
|
|
|
for(let i = 0; i < files.length; i++) {
|
|
|
|
addFileMedia(files[i]).catch(e => {
|
|
|
|
console.error(e);
|
|
|
|
displayError(e);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
input.click();
|
|
|
|
}
|
|
|
|
|
|
|
|
commands.presentfile = {
|
|
|
|
description: 'broadcast a video or audio file',
|
|
|
|
f: (c, r) => {
|
|
|
|
presentFile();
|
|
|
|
},
|
|
|
|
predicate: () => {
|
2022-02-21 18:36:36 +01:00
|
|
|
if(!canFile())
|
|
|
|
return 'Your browser does not support presenting arbitrary files';
|
2022-02-21 17:32:32 +01:00
|
|
|
if(!serverConnection || !serverConnection.permissions ||
|
|
|
|
serverConnection.permissions.indexOf('present') < 0)
|
2022-02-21 18:36:36 +01:00
|
|
|
return 'You are not authorised to present.';
|
2022-02-21 17:32:32 +01:00
|
|
|
return null;
|
2022-02-21 18:36:36 +01:00
|
|
|
}
|
2022-02-21 17:32:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-02-16 20:41:58 +01:00
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
*/
|
|
|
|
function sendFile(id) {
|
|
|
|
let input = document.createElement('input');
|
|
|
|
input.type = 'file';
|
|
|
|
input.onchange = function(e) {
|
|
|
|
if(!(this instanceof HTMLInputElement))
|
|
|
|
throw new Error('Unexpected type for this');
|
2022-02-21 17:32:32 +01:00
|
|
|
let files = this.files;
|
2022-02-16 20:41:58 +01:00
|
|
|
for(let i = 0; i < files.length; i++) {
|
|
|
|
try {
|
2022-07-23 20:52:04 +02:00
|
|
|
serverConnection.sendFile(id, files[i]);
|
2022-02-16 20:41:58 +01:00
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
|
|
|
displayError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
input.click();
|
|
|
|
}
|
|
|
|
|
2022-01-30 06:32:55 +01:00
|
|
|
commands.sendfile = {
|
|
|
|
parameters: 'user',
|
2022-01-30 19:22:43 +01:00
|
|
|
description: 'send a file (this will disclose your IP address)',
|
2022-01-30 06:32:55 +01:00
|
|
|
f: (c, r) => {
|
|
|
|
let p = parseCommand(r);
|
|
|
|
if(!p[0])
|
|
|
|
throw new Error(`/${c} requires parameters`);
|
|
|
|
let id = findUserId(p[0]);
|
|
|
|
if(!id)
|
|
|
|
throw new Error(`Unknown user ${p[0]}`);
|
2022-02-16 20:41:58 +01:00
|
|
|
sendFile(id);
|
|
|
|
},
|
2022-01-30 06:32:55 +01:00
|
|
|
};
|
|
|
|
|
2021-01-11 18:45:20 +01:00
|
|
|
/**
|
|
|
|
* Test loopback through a TURN relay.
|
|
|
|
*
|
|
|
|
* @returns {Promise<number>}
|
|
|
|
*/
|
2021-01-11 18:18:55 +01:00
|
|
|
async function relayTest() {
|
2021-01-11 18:45:20 +01:00
|
|
|
if(!serverConnection)
|
|
|
|
throw new Error('not connected');
|
2022-03-23 00:12:51 +01:00
|
|
|
let conf = Object.assign({}, serverConnection.getRTCConfiguration());
|
2021-01-11 18:18:55 +01:00
|
|
|
conf.iceTransportPolicy = 'relay';
|
|
|
|
let pc1 = new RTCPeerConnection(conf);
|
|
|
|
let pc2 = new RTCPeerConnection(conf);
|
|
|
|
pc1.onicecandidate = e => {e.candidate && pc2.addIceCandidate(e.candidate);};
|
|
|
|
pc2.onicecandidate = e => {e.candidate && pc1.addIceCandidate(e.candidate);};
|
|
|
|
try {
|
2021-01-11 18:45:20 +01:00
|
|
|
return await new Promise(async (resolve, reject) => {
|
2021-01-11 18:18:55 +01:00
|
|
|
let d1 = pc1.createDataChannel('loopbackTest');
|
|
|
|
d1.onopen = e => {
|
2021-01-11 18:45:20 +01:00
|
|
|
d1.send(Date.now().toString());
|
2021-01-11 18:18:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let offer = await pc1.createOffer();
|
|
|
|
await pc1.setLocalDescription(offer);
|
2021-01-11 20:27:39 +01:00
|
|
|
await pc2.setRemoteDescription(pc1.localDescription);
|
2021-01-11 18:18:55 +01:00
|
|
|
let answer = await pc2.createAnswer();
|
2021-01-11 20:27:39 +01:00
|
|
|
await pc2.setLocalDescription(answer);
|
|
|
|
await pc1.setRemoteDescription(pc2.localDescription);
|
2021-01-11 18:18:55 +01:00
|
|
|
|
|
|
|
pc2.ondatachannel = e => {
|
|
|
|
let d2 = e.channel;
|
|
|
|
d2.onmessage = e => {
|
2021-01-11 18:45:20 +01:00
|
|
|
let t = parseInt(e.data);
|
|
|
|
if(isNaN(t))
|
|
|
|
reject(new Error('corrupt data'));
|
2021-01-11 18:18:55 +01:00
|
|
|
else
|
2021-01-11 18:45:20 +01:00
|
|
|
resolve(Date.now() - t);
|
2021-01-11 18:18:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 18:45:20 +01:00
|
|
|
setTimeout(() => reject(new Error('timeout')), 5000);
|
2021-01-11 18:18:55 +01:00
|
|
|
})
|
|
|
|
} finally {
|
|
|
|
pc1.close();
|
|
|
|
pc2.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
commands['relay-test'] = {
|
|
|
|
f: async (c, r) => {
|
2021-01-11 19:28:57 +01:00
|
|
|
localMessage('Relay test in progress...');
|
2021-01-11 18:18:55 +01:00
|
|
|
try {
|
2021-01-11 18:45:20 +01:00
|
|
|
let s = Date.now();
|
|
|
|
let rtt = await relayTest();
|
|
|
|
let e = Date.now();
|
2021-01-11 19:28:57 +01:00
|
|
|
localMessage(`Relay test successful in ${e-s}ms, RTT ${rtt}ms`);
|
2021-01-11 18:18:55 +01:00
|
|
|
} catch(e) {
|
2021-01-11 19:28:57 +01:00
|
|
|
localMessage(`Relay test failed: ${e}`);
|
2021-01-11 18:18:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
function handleInput() {
|
2020-09-20 14:33:13 +02:00
|
|
|
let input = /** @type {HTMLTextAreaElement} */
|
|
|
|
(document.getElementById('input'));
|
2020-04-24 19:38:21 +02:00
|
|
|
let data = input.value;
|
|
|
|
input.value = '';
|
|
|
|
|
|
|
|
let message, me;
|
|
|
|
|
|
|
|
if(data === '')
|
|
|
|
return;
|
|
|
|
|
2020-09-16 01:42:39 +02:00
|
|
|
if(data[0] === '/') {
|
|
|
|
if(data.length > 1 && data[1] === '/') {
|
2020-11-24 14:55:52 +01:00
|
|
|
message = data.slice(1);
|
2020-04-24 19:38:21 +02:00
|
|
|
me = false;
|
|
|
|
} else {
|
2020-09-12 12:26:07 +02:00
|
|
|
let cmd, rest;
|
|
|
|
let space = data.indexOf(' ');
|
2020-04-24 19:38:21 +02:00
|
|
|
if(space < 0) {
|
2020-11-24 14:55:52 +01:00
|
|
|
cmd = data.slice(1);
|
2020-04-24 19:38:21 +02:00
|
|
|
rest = '';
|
|
|
|
} else {
|
2020-11-24 14:55:52 +01:00
|
|
|
cmd = data.slice(1, space);
|
2020-09-12 12:26:07 +02:00
|
|
|
rest = data.slice(space + 1);
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-11-24 14:55:52 +01:00
|
|
|
if(cmd === 'me') {
|
2020-04-24 19:38:21 +02:00
|
|
|
message = rest;
|
|
|
|
me = true;
|
2020-11-24 14:55:52 +01:00
|
|
|
} else {
|
|
|
|
let c = commands[cmd];
|
|
|
|
if(!c) {
|
|
|
|
displayError(`Uknown command /${cmd}, type /help for help`);
|
2020-09-23 21:01:29 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-11-24 14:55:52 +01:00
|
|
|
if(c.predicate) {
|
|
|
|
let s = c.predicate();
|
|
|
|
if(s) {
|
|
|
|
displayError(s);
|
2020-09-23 21:01:29 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-04-25 17:36:35 +02:00
|
|
|
}
|
2020-11-24 14:55:52 +01:00
|
|
|
try {
|
|
|
|
c.f(cmd, rest);
|
|
|
|
} catch(e) {
|
2021-08-07 15:39:00 +02:00
|
|
|
console.error(e);
|
2020-11-24 14:55:52 +01:00
|
|
|
displayError(e);
|
2020-10-01 16:52:01 +02:00
|
|
|
}
|
2020-04-25 17:36:35 +02:00
|
|
|
return;
|
2020-05-10 21:18:12 +02:00
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
message = data;
|
|
|
|
me = false;
|
|
|
|
}
|
|
|
|
|
2020-09-23 21:22:55 +02:00
|
|
|
if(!serverConnection || !serverConnection.socket) {
|
|
|
|
displayError("Not connected.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-09 01:46:17 +02:00
|
|
|
try {
|
2021-01-03 12:04:39 +01:00
|
|
|
serverConnection.chat(me ? 'me' : '', '', message);
|
2020-06-09 01:46:17 +02:00
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
|
|
|
displayError(e);
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
document.getElementById('inputform').onsubmit = function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
handleInput();
|
|
|
|
};
|
|
|
|
|
|
|
|
document.getElementById('input').onkeypress = function(e) {
|
|
|
|
if(e.key === 'Enter' && !e.ctrlKey && !e.shiftKey && !e.metaKey) {
|
|
|
|
e.preventDefault();
|
|
|
|
handleInput();
|
|
|
|
}
|
2020-05-10 21:18:07 +02:00
|
|
|
};
|
2020-04-24 19:38:21 +02:00
|
|
|
|
|
|
|
function chatResizer(e) {
|
|
|
|
e.preventDefault();
|
2020-09-03 17:01:21 +02:00
|
|
|
let full_width = document.getElementById("mainrow").offsetWidth;
|
|
|
|
let left = document.getElementById("left");
|
|
|
|
let right = document.getElementById("right");
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
let start_x = e.clientX;
|
2020-09-11 22:46:23 +02:00
|
|
|
let start_width = left.offsetWidth;
|
2020-09-03 17:01:21 +02:00
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
function start_drag(e) {
|
2020-09-03 17:01:21 +02:00
|
|
|
let left_width = (start_width + e.clientX - start_x) * 100 / full_width;
|
2020-10-07 09:33:38 +02:00
|
|
|
// set min chat width to 300px
|
|
|
|
let min_left_width = 300 * 100 / full_width;
|
2020-10-05 11:15:31 +02:00
|
|
|
if (left_width < min_left_width) {
|
|
|
|
return;
|
|
|
|
}
|
2020-09-12 16:34:52 +02:00
|
|
|
left.style.flex = left_width.toString();
|
|
|
|
right.style.flex = (100 - left_width).toString();
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
function stop_drag(e) {
|
|
|
|
document.documentElement.removeEventListener(
|
|
|
|
'mousemove', start_drag, false,
|
|
|
|
);
|
|
|
|
document.documentElement.removeEventListener(
|
|
|
|
'mouseup', stop_drag, false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
document.documentElement.addEventListener(
|
|
|
|
'mousemove', start_drag, false,
|
|
|
|
);
|
|
|
|
document.documentElement.addEventListener(
|
|
|
|
'mouseup', stop_drag, false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
document.getElementById('resizer').addEventListener('mousedown', chatResizer, false);
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
/**
|
2020-09-28 20:58:07 +02:00
|
|
|
* @param {unknown} message
|
|
|
|
* @param {string} [level]
|
2020-09-20 14:33:13 +02:00
|
|
|
*/
|
|
|
|
function displayError(message, level) {
|
|
|
|
if(!level)
|
2020-09-28 20:58:07 +02:00
|
|
|
level = "error";
|
2024-12-13 18:08:56 +01:00
|
|
|
let position = 'center';
|
|
|
|
let gravity = 'top';
|
2020-09-20 14:33:13 +02:00
|
|
|
|
|
|
|
switch(level) {
|
2020-09-28 20:58:07 +02:00
|
|
|
case "info":
|
2020-09-20 14:33:13 +02:00
|
|
|
position = 'right';
|
|
|
|
gravity = 'bottom';
|
|
|
|
break;
|
2020-09-28 20:58:07 +02:00
|
|
|
case "warning":
|
2020-09-20 14:33:13 +02:00
|
|
|
break;
|
2021-07-30 19:38:02 +02:00
|
|
|
case "kicked":
|
|
|
|
level = "error";
|
|
|
|
break;
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-09-20 14:33:13 +02:00
|
|
|
|
|
|
|
/** @ts-ignore */
|
2020-09-01 10:23:35 +02:00
|
|
|
Toastify({
|
2020-09-20 14:33:13 +02:00
|
|
|
text: message,
|
|
|
|
duration: 4000,
|
|
|
|
close: true,
|
|
|
|
position: position,
|
|
|
|
gravity: gravity,
|
|
|
|
className: level,
|
2020-09-01 10:23:35 +02:00
|
|
|
}).showToast();
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
/**
|
2020-09-28 20:58:07 +02:00
|
|
|
* @param {unknown} message
|
2020-09-20 14:33:13 +02:00
|
|
|
*/
|
2020-04-24 19:38:21 +02:00
|
|
|
function displayWarning(message) {
|
2020-09-28 20:58:07 +02:00
|
|
|
return displayError(message, "warning");
|
2020-09-03 17:01:21 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
/**
|
2020-09-28 20:58:07 +02:00
|
|
|
* @param {unknown} message
|
2020-09-20 14:33:13 +02:00
|
|
|
*/
|
2020-09-03 17:01:21 +02:00
|
|
|
function displayMessage(message) {
|
2020-09-28 20:58:07 +02:00
|
|
|
return displayError(message, "info");
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2024-06-11 11:57:15 +02:00
|
|
|
document.getElementById('loginform').onsubmit = async function(e) {
|
2020-04-24 19:38:21 +02:00
|
|
|
e.preventDefault();
|
2024-06-10 19:41:23 +02:00
|
|
|
|
|
|
|
let form = this;
|
|
|
|
if(!(form instanceof HTMLFormElement))
|
2024-06-11 11:57:15 +02:00
|
|
|
throw new Error('Bad type for loginform');
|
2023-04-01 13:28:24 +02:00
|
|
|
|
|
|
|
setVisibility('passwordform', true);
|
2020-11-24 17:08:33 +01:00
|
|
|
|
2020-12-01 00:26:14 +01:00
|
|
|
if(getInputElement('presentboth').checked)
|
|
|
|
presentRequested = 'both';
|
|
|
|
else if(getInputElement('presentmike').checked)
|
|
|
|
presentRequested = 'mike';
|
|
|
|
else
|
|
|
|
presentRequested = null;
|
2020-11-30 21:28:45 +01:00
|
|
|
getInputElement('presentoff').checked = true;
|
2023-04-01 13:28:24 +02:00
|
|
|
|
|
|
|
// Connect to the server, gotConnected will join.
|
2024-06-10 19:41:23 +02:00
|
|
|
form.active = false;
|
|
|
|
serverConnect();
|
2020-05-10 21:18:07 +02:00
|
|
|
};
|
2020-04-24 19:38:21 +02:00
|
|
|
|
|
|
|
document.getElementById('disconnectbutton').onclick = function(e) {
|
2020-08-11 17:09:31 +02:00
|
|
|
serverConnection.close();
|
2020-11-24 17:36:52 +01:00
|
|
|
closeNav();
|
2020-05-10 21:18:07 +02:00
|
|
|
};
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2020-08-27 21:17:46 +02:00
|
|
|
function openNav() {
|
|
|
|
document.getElementById("sidebarnav").style.width = "250px";
|
|
|
|
}
|
|
|
|
|
|
|
|
function closeNav() {
|
|
|
|
document.getElementById("sidebarnav").style.width = "0";
|
|
|
|
}
|
|
|
|
|
|
|
|
document.getElementById('sidebarCollapse').onclick = function(e) {
|
|
|
|
document.getElementById("left-sidebar").classList.toggle("active");
|
|
|
|
document.getElementById("mainrow").classList.toggle("full-width-active");
|
|
|
|
};
|
|
|
|
|
|
|
|
document.getElementById('openside').onclick = function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
let sidewidth = document.getElementById("sidebarnav").style.width;
|
|
|
|
if (sidewidth !== "0px" && sidewidth !== "") {
|
|
|
|
closeNav();
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
openNav();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
document.getElementById('clodeside').onclick = function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
closeNav();
|
|
|
|
};
|
|
|
|
|
2020-09-09 20:26:19 +02:00
|
|
|
document.getElementById('collapse-video').onclick = function(e) {
|
2020-09-01 10:23:35 +02:00
|
|
|
e.preventDefault();
|
2021-03-26 20:18:20 +01:00
|
|
|
setVisibility('collapse-video', false);
|
|
|
|
setVisibility('show-video', true);
|
|
|
|
hideVideo(true);
|
2020-09-01 10:23:35 +02:00
|
|
|
};
|
|
|
|
|
2021-03-26 20:18:20 +01:00
|
|
|
document.getElementById('show-video').onclick = function(e) {
|
2020-09-01 10:23:35 +02:00
|
|
|
e.preventDefault();
|
2021-03-26 20:18:20 +01:00
|
|
|
setVisibility('video-container', true);
|
|
|
|
setVisibility('collapse-video', true);
|
|
|
|
setVisibility('show-video', false);
|
2020-09-09 20:26:19 +02:00
|
|
|
};
|
|
|
|
|
2020-10-07 09:33:38 +02:00
|
|
|
document.getElementById('close-chat').onclick = function(e) {
|
2021-03-26 20:18:20 +01:00
|
|
|
e.preventDefault();
|
|
|
|
setVisibility('left', false);
|
|
|
|
setVisibility('show-chat', true);
|
|
|
|
resizePeers();
|
|
|
|
};
|
|
|
|
|
|
|
|
document.getElementById('show-chat').onclick = function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
setVisibility('left', true);
|
|
|
|
setVisibility('show-chat', false);
|
|
|
|
resizePeers();
|
2020-10-07 09:33:38 +02:00
|
|
|
};
|
|
|
|
|
2020-09-18 11:28:13 +02:00
|
|
|
async function serverConnect() {
|
2020-11-24 16:29:19 +01:00
|
|
|
if(serverConnection && serverConnection.socket)
|
|
|
|
serverConnection.close();
|
2020-08-11 17:09:31 +02:00
|
|
|
serverConnection = new ServerConnection();
|
|
|
|
serverConnection.onconnected = gotConnected;
|
2024-06-10 19:41:23 +02:00
|
|
|
serverConnection.onerror = function(e) {
|
|
|
|
console.error(e);
|
|
|
|
displayError(e.toString());
|
|
|
|
};
|
2022-03-22 18:22:18 +01:00
|
|
|
serverConnection.onpeerconnection = onPeerConnection;
|
2020-08-11 17:09:31 +02:00
|
|
|
serverConnection.onclose = gotClose;
|
|
|
|
serverConnection.ondownstream = gotDownStream;
|
|
|
|
serverConnection.onuser = gotUser;
|
2020-12-01 22:42:06 +01:00
|
|
|
serverConnection.onjoined = gotJoined;
|
2020-08-11 17:09:31 +02:00
|
|
|
serverConnection.onchat = addToChatbox;
|
2020-12-28 01:42:26 +01:00
|
|
|
serverConnection.onusermessage = gotUserMessage;
|
2022-07-23 20:52:04 +02:00
|
|
|
serverConnection.onfiletransfer = gotFileTransfer;
|
2020-12-28 01:42:26 +01:00
|
|
|
|
2022-09-01 14:55:52 +02:00
|
|
|
let url = groupStatus.endpoint;
|
|
|
|
if(!url) {
|
|
|
|
console.warn("no endpoint in status");
|
|
|
|
url = `ws${location.protocol === 'https:' ? 's' : ''}://${location.host}/ws`;
|
|
|
|
}
|
|
|
|
|
2020-09-18 11:28:13 +02:00
|
|
|
try {
|
|
|
|
await serverConnection.connect(url);
|
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
|
|
|
displayError(e.message ? e.message : "Couldn't connect to " + url);
|
|
|
|
}
|
2020-08-11 17:09:31 +02:00
|
|
|
}
|
|
|
|
|
2021-10-26 22:22:48 +02:00
|
|
|
async function start() {
|
|
|
|
try {
|
2024-01-17 22:17:54 +01:00
|
|
|
let r = await fetch(".status")
|
2021-10-26 22:22:48 +02:00
|
|
|
if(!r.ok)
|
|
|
|
throw new Error(`${r.status} ${r.statusText}`);
|
|
|
|
groupStatus = await r.json()
|
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
2022-09-01 14:37:42 +02:00
|
|
|
displayWarning("Couldn't fetch status: " + e);
|
|
|
|
groupStatus = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if(groupStatus.name) {
|
|
|
|
group = groupStatus.name;
|
|
|
|
} else {
|
|
|
|
console.warn("no group name in status");
|
|
|
|
group = decodeURIComponent(
|
|
|
|
location.pathname.replace(/^\/[a-z]*\//, '').replace(/\/$/, ''),
|
|
|
|
);
|
2021-10-26 22:22:48 +02:00
|
|
|
}
|
|
|
|
|
2023-07-09 17:33:53 +02:00
|
|
|
// Disable simulcast on Firefox by default, it's buggy.
|
|
|
|
if(isFirefox())
|
|
|
|
getSelectElement('simulcastselect').value = 'off';
|
|
|
|
|
2022-02-18 20:27:53 +01:00
|
|
|
let parms = new URLSearchParams(window.location.search);
|
|
|
|
if(window.location.search)
|
|
|
|
window.history.replaceState(null, '', window.location.pathname);
|
2021-10-26 22:22:48 +02:00
|
|
|
setTitle(groupStatus.displayName || capitalise(group));
|
2022-02-18 20:27:53 +01:00
|
|
|
|
2024-12-15 15:29:00 +01:00
|
|
|
addFilters();
|
2024-06-10 16:08:36 +02:00
|
|
|
await setMediaChoices(false);
|
|
|
|
reflectSettings();
|
2020-06-09 18:05:16 +02:00
|
|
|
|
2023-04-01 13:28:24 +02:00
|
|
|
if(parms.has('token'))
|
2022-02-18 20:27:53 +01:00
|
|
|
token = parms.get('token');
|
2023-04-01 13:28:24 +02:00
|
|
|
|
|
|
|
if(token) {
|
2022-02-18 20:27:53 +01:00
|
|
|
await serverConnect();
|
2022-02-19 23:58:31 +01:00
|
|
|
} else if(groupStatus.authPortal) {
|
|
|
|
window.location.href = groupStatus.authPortal;
|
2022-02-18 20:27:53 +01:00
|
|
|
} else {
|
2023-04-01 13:28:24 +02:00
|
|
|
setVisibility('login-container', true);
|
2022-12-30 10:20:41 +01:00
|
|
|
document.getElementById('username').focus()
|
2022-02-18 20:27:53 +01:00
|
|
|
}
|
2021-05-14 18:04:18 +02:00
|
|
|
setViewportHeight();
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
start();
|