2020-04-24 19:38:21 +02:00
|
|
|
// Copyright (c) 2020 by Juliusz Chroboczek.
|
|
|
|
|
|
|
|
// This is not open source software. Copy it, and I'll break into your
|
|
|
|
// house and tell your three year-old that Santa doesn't exist.
|
|
|
|
|
|
|
|
'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
|
|
|
|
2020-09-12 16:34:52 +02:00
|
|
|
/**
|
|
|
|
* @typedef {Object} userpass
|
|
|
|
* @property {string} username
|
|
|
|
* @property {string} password
|
|
|
|
*/
|
|
|
|
|
2020-09-10 15:28:24 +02:00
|
|
|
/* Some browsers disable session storage when cookies are disabled,
|
|
|
|
we fall back to a global variable. */
|
2020-09-12 16:34:52 +02:00
|
|
|
/**
|
|
|
|
* @type {userpass}
|
|
|
|
*/
|
2020-09-10 15:28:24 +02:00
|
|
|
let fallbackUserPass = null;
|
|
|
|
|
2020-09-12 16:34:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} username
|
|
|
|
* @param {string} password
|
|
|
|
*/
|
2020-09-18 17:57:37 +02:00
|
|
|
function storeUserPass(username, password) {
|
2020-09-10 15:28:24 +02:00
|
|
|
let userpass = {username: username, password: password};
|
|
|
|
try {
|
|
|
|
window.sessionStorage.setItem('userpass', JSON.stringify(userpass));
|
|
|
|
fallbackUserPass = null;
|
|
|
|
} catch(e) {
|
|
|
|
console.warn("Couldn't store password:", e);
|
2020-09-12 16:34:52 +02:00
|
|
|
fallbackUserPass = userpass;
|
2020-09-10 15:28:24 +02:00
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 16:34:52 +02:00
|
|
|
/**
|
2020-09-18 17:57:37 +02:00
|
|
|
* Returns null if the user hasn't logged in yet.
|
|
|
|
*
|
2020-09-12 16:34:52 +02:00
|
|
|
* @returns {userpass}
|
|
|
|
*/
|
2020-04-24 19:38:21 +02:00
|
|
|
function getUserPass() {
|
2020-09-18 17:57:37 +02:00
|
|
|
/** @type{userpass} */
|
2020-09-10 15:28:24 +02:00
|
|
|
let userpass;
|
|
|
|
try {
|
|
|
|
let json = window.sessionStorage.getItem('userpass');
|
|
|
|
userpass = JSON.parse(json);
|
|
|
|
} catch(e) {
|
|
|
|
console.warn("Couldn't retrieve password:", e);
|
|
|
|
userpass = fallbackUserPass;
|
|
|
|
}
|
|
|
|
return userpass || null;
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 16:34:52 +02:00
|
|
|
/**
|
2020-09-18 17:57:37 +02:00
|
|
|
* Return null if the user hasn't logged in yet.
|
|
|
|
*
|
2020-09-12 16:34:52 +02:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
2020-04-24 19:38:21 +02:00
|
|
|
function getUsername() {
|
|
|
|
let userpass = getUserPass();
|
|
|
|
if(!userpass)
|
|
|
|
return null;
|
|
|
|
return userpass.username;
|
|
|
|
}
|
|
|
|
|
2020-09-18 17:57:37 +02:00
|
|
|
/**
|
|
|
|
* @typedef {Object} settings
|
|
|
|
* @property {boolean} [localMute]
|
|
|
|
* @property {string} [video]
|
|
|
|
* @property {string} [audio]
|
|
|
|
* @property {string} [send]
|
|
|
|
* @property {string} [request]
|
|
|
|
* @property {boolean} [activityDetection]
|
2020-09-18 19:14:10 +02:00
|
|
|
* @property {boolean} [blackboardMode]
|
|
|
|
* @property {boolean} [studioMode]
|
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) {
|
|
|
|
console.warn("Couldn't store password:", e);
|
|
|
|
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) {
|
|
|
|
console.warn("Couldn't retrieve password:", e);
|
|
|
|
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]);
|
|
|
|
storeSettings(s)
|
|
|
|
}
|
|
|
|
|
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-09-18 17:57:37 +02:00
|
|
|
if(!settings.video || !selectOptionAvailable(videoselect, settings.video)) {
|
|
|
|
settings.video = selectOptionDefault(videoselect);
|
|
|
|
store = true;
|
|
|
|
}
|
|
|
|
videoselect.value = settings.video;
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
let audioselect = getSelectElement('audioselect');
|
2020-09-18 17:57:37 +02:00
|
|
|
if(!settings.audio || !selectOptionAvailable(audioselect, settings.audio)) {
|
|
|
|
settings.audio = selectOptionDefault(audioselect);
|
|
|
|
store = true;
|
|
|
|
}
|
|
|
|
audioselect.value = settings.audio;
|
|
|
|
|
|
|
|
if(settings.request)
|
2020-09-20 14:33:13 +02:00
|
|
|
getSelectElement('requestselect').value = settings.request;
|
2020-09-18 17:57:37 +02: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(settings.send)
|
2020-09-20 14:33:13 +02:00
|
|
|
getSelectElement('sendselect').value = settings.send;
|
2020-09-18 17:57:37 +02: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;
|
|
|
|
}
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
getInputElement('activitybox').checked = settings.activityDetection;
|
2020-09-18 17:57:37 +02:00
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
getInputElement('blackboardbox').checked = settings.blackboardMode;
|
2020-09-18 19:14:10 +02:00
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
getInputElement('studiobox').checked = settings.studioMode;
|
2020-09-18 19:14:10 +02:00
|
|
|
|
2020-09-18 17:57:37 +02:00
|
|
|
if(store)
|
|
|
|
storeSettings(settings);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-09-01 10:23:35 +02:00
|
|
|
function showVideo() {
|
2020-09-09 20:26:19 +02:00
|
|
|
let width = window.innerWidth;
|
2020-09-01 10:23:35 +02:00
|
|
|
let video_container = document.getElementById('video-container');
|
|
|
|
video_container.classList.remove('no-video');
|
2020-09-09 20:26:19 +02:00
|
|
|
if (width <= 768)
|
|
|
|
document.getElementById('collapse-video').style.display = "block";
|
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;
|
2020-09-01 10:23:35 +02:00
|
|
|
let video_container = document.getElementById('video-container');
|
|
|
|
video_container.classList.add('no-video');
|
2020-09-09 20:26:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function closeVideoControls() {
|
|
|
|
// hide all video buttons used to switch video on mobile layout
|
2020-09-04 17:34:38 +02:00
|
|
|
document.getElementById('switch-video').style.display = "";
|
2020-09-09 20:26:19 +02:00
|
|
|
document.getElementById('collapse-video').style.display = "";
|
2020-09-01 10:23:35 +02:00
|
|
|
}
|
|
|
|
|
2020-09-05 12:59:28 +02:00
|
|
|
/**
|
|
|
|
* @param{boolean} connected
|
|
|
|
*/
|
2020-04-24 19:38:21 +02:00
|
|
|
function setConnected(connected) {
|
|
|
|
let statspan = document.getElementById('statspan');
|
2020-09-02 15:35:55 +02:00
|
|
|
let userbox = document.getElementById('user');
|
|
|
|
let connectionbox = document.getElementById('login-container');
|
2020-04-24 19:38:21 +02:00
|
|
|
if(connected) {
|
2020-08-11 17:09:31 +02:00
|
|
|
resetUsers();
|
|
|
|
clearChat();
|
2020-04-24 19:38:21 +02:00
|
|
|
statspan.textContent = 'Connected';
|
|
|
|
statspan.classList.remove('disconnected');
|
|
|
|
statspan.classList.add('connected');
|
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();
|
2020-04-24 19:38:21 +02:00
|
|
|
} else {
|
2020-09-05 12:59:28 +02:00
|
|
|
resetUsers();
|
2020-04-24 19:38:21 +02:00
|
|
|
let userpass = getUserPass();
|
2020-09-20 14:33:13 +02:00
|
|
|
getInputElement('username').value =
|
2020-04-24 19:38:21 +02:00
|
|
|
userpass ? userpass.username : '';
|
2020-09-20 14:33:13 +02:00
|
|
|
getInputElement('password').value =
|
2020-04-24 19:38:21 +02:00
|
|
|
userpass ? userpass.password : '';
|
|
|
|
statspan.textContent = 'Disconnected';
|
|
|
|
statspan.classList.remove('connected');
|
|
|
|
statspan.classList.add('disconnected');
|
2020-09-02 15:35:55 +02:00
|
|
|
userbox.classList.add('invisible');
|
|
|
|
connectionbox.classList.remove('invisible');
|
2020-08-11 17:09:31 +02:00
|
|
|
clearUsername();
|
2020-09-01 10:23:35 +02:00
|
|
|
displayError("Disconnected!", "error");
|
2020-09-09 20:26:19 +02:00
|
|
|
hideVideo();
|
|
|
|
closeVideoControls();
|
2020-08-11 17:09:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
/** @this {ServerConnection} */
|
2020-08-11 17:09:31 +02:00
|
|
|
function gotConnected() {
|
|
|
|
setConnected(true);
|
|
|
|
let up = getUserPass();
|
|
|
|
this.login(up.username, up.password);
|
|
|
|
this.join(group);
|
2020-09-18 17:57:37 +02:00
|
|
|
this.request(getSettings().request);
|
2020-08-11 17:09:31 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-08-23 19:07:52 +02:00
|
|
|
delUpMediaKind(null);
|
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
|
|
|
}
|
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) {
|
|
|
|
c.onclose = function() {
|
|
|
|
delMedia(c.id);
|
|
|
|
};
|
|
|
|
c.onerror = function(e) {
|
|
|
|
console.error(e);
|
|
|
|
displayError(e);
|
|
|
|
}
|
|
|
|
c.ondowntrack = function(track, transceiver, label, stream) {
|
|
|
|
setMedia(c, false);
|
|
|
|
}
|
|
|
|
c.onlabel = function(label) {
|
|
|
|
setLabel(c);
|
|
|
|
}
|
|
|
|
c.onstatus = function(status) {
|
|
|
|
setMediaStatus(c);
|
2020-04-24 19:38:21 +02: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-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`,
|
|
|
|
);
|
2020-09-09 20:26:19 +02:00
|
|
|
};
|
|
|
|
setViewportHeight();
|
|
|
|
|
|
|
|
// On resize and orientation change, we update viewport height
|
|
|
|
addEventListener('resize', setViewportHeight);
|
|
|
|
addEventListener('orientationchange', setViewportHeight);
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
getButtonElement('presentbutton').onclick = function(e) {
|
2020-04-24 19:38:21 +02:00
|
|
|
e.preventDefault();
|
2020-05-21 18:22:30 +02:00
|
|
|
addLocalMedia();
|
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();
|
|
|
|
delUpMediaKind('local');
|
2020-09-03 20:54:27 +02:00
|
|
|
resizePeers();
|
2020-05-21 18:22:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function changePresentation() {
|
2020-06-10 20:25:25 +02:00
|
|
|
let id = findUpMedia('local');
|
|
|
|
if(id) {
|
|
|
|
addLocalMedia(id);
|
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() {
|
2020-08-11 17:09:31 +02:00
|
|
|
let permissions = serverConnection.permissions;
|
2020-06-10 20:25:25 +02:00
|
|
|
let local = !!findUpMedia('local');
|
|
|
|
let share = !!findUpMedia('screenshare')
|
2020-09-23 21:46:30 +02:00
|
|
|
|
2020-05-21 18:22:30 +02:00
|
|
|
// don't allow multiple presentations
|
|
|
|
setVisibility('presentbutton', permissions.present && !local);
|
|
|
|
setVisibility('unpresentbutton', local);
|
2020-09-23 21:46:30 +02:00
|
|
|
|
2020-05-21 18:22:30 +02:00
|
|
|
// allow multiple shared documents
|
2020-09-23 21:46:30 +02:00
|
|
|
setVisibility('sharebutton', permissions.present &&
|
|
|
|
('getDisplayMedia' in navigator.mediaDevices))
|
2020-05-21 18:22:30 +02:00
|
|
|
setVisibility('unsharebutton', share);
|
|
|
|
|
|
|
|
setVisibility('mediaoptions', permissions.present);
|
|
|
|
}
|
|
|
|
|
2020-09-12 16:34:52 +02:00
|
|
|
/**
|
|
|
|
* @param {boolean} mute
|
|
|
|
*/
|
2020-06-09 18:05:16 +02:00
|
|
|
function setLocalMute(mute) {
|
2020-09-18 17:57:37 +02:00
|
|
|
muteLocalTracks(mute);
|
2020-06-09 18:05:16 +02:00
|
|
|
let button = document.getElementById('mutebutton');
|
2020-09-11 10:39:32 +02:00
|
|
|
let icon = button.querySelector("span .fa");
|
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-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});
|
2020-05-21 18:22:30 +02:00
|
|
|
changePresentation();
|
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});
|
2020-05-21 18:22:30 +02:00
|
|
|
changePresentation();
|
2020-05-10 21:18:07 +02:00
|
|
|
};
|
2020-05-05 17:50:16 +02: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});
|
|
|
|
changePresentation();
|
|
|
|
}
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
getInputElement('studiobox').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({studioMode: this.checked});
|
|
|
|
changePresentation();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
updateSettings({localMute: localMute})
|
|
|
|
setLocalMute(localMute);
|
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
|
|
|
|
2020-05-21 18:22:30 +02:00
|
|
|
document.getElementById('unsharebutton').onclick = function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
delUpMediaKind('screenshare');
|
|
|
|
}
|
|
|
|
|
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});
|
2020-08-26 18:32:01 +02:00
|
|
|
let t = getMaxVideoThroughput();
|
|
|
|
for(let id in serverConnection.up) {
|
|
|
|
let c = serverConnection.up[id];
|
|
|
|
if(c.kind === 'local')
|
|
|
|
await setMaxVideoThroughput(c, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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});
|
2020-08-11 17:09:31 +02:00
|
|
|
serverConnection.request(this.value);
|
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-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
|
|
|
|
|
|
|
let text = '';
|
2020-06-12 15:42:44 +02:00
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
c.pc.getSenders().forEach(s => {
|
2020-06-12 15:42:44 +02:00
|
|
|
let tid = s.track && s.track.id;
|
2020-08-11 17:09:31 +02:00
|
|
|
let stats = tid && c.stats[tid];
|
2020-09-11 23:20:46 +02:00
|
|
|
let rate = stats && stats['outbound-rtp'] && stats['outbound-rtp'].rate;
|
|
|
|
if(typeof rate === 'number') {
|
2020-06-12 15:42:44 +02:00
|
|
|
if(text)
|
|
|
|
text = text + ' + ';
|
2020-09-11 23:20:46 +02:00
|
|
|
text = text + Math.round(rate / 1000) + 'kbps';
|
2020-06-12 15:42:44 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
setLabel(c, text);
|
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) {
|
|
|
|
let peer = document.getElementById('peer-' + c.id);
|
|
|
|
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];
|
|
|
|
let energy = s && s['track'] && s['track'].audioEnergy;
|
|
|
|
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 {
|
|
|
|
devices = await navigator.mediaDevices.enumerateDevices();
|
|
|
|
} 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
|
|
|
/**
|
2020-09-08 03:05:25 +02:00
|
|
|
* @param {string} [id]
|
2020-08-13 20:55:13 +02:00
|
|
|
*/
|
2020-08-11 17:09:31 +02:00
|
|
|
function newUpStream(id) {
|
2020-08-13 20:16:52 +02:00
|
|
|
let c = serverConnection.newUpStream(id);
|
2020-08-11 17:09:31 +02:00
|
|
|
c.onstatus = function(status) {
|
|
|
|
setMediaStatus(c);
|
|
|
|
}
|
|
|
|
c.onerror = function(e) {
|
|
|
|
console.error(e);
|
|
|
|
displayError(e);
|
2020-08-13 20:30:55 +02:00
|
|
|
delUpMedia(c);
|
2020-08-11 17:09:31 +02:00
|
|
|
}
|
|
|
|
c.onabort = function() {
|
2020-08-13 20:30:55 +02:00
|
|
|
delUpMedia(c);
|
2020-08-11 17:09:31 +02:00
|
|
|
}
|
2020-08-26 18:32:01 +02:00
|
|
|
c.onnegotiationcompleted = function() {
|
|
|
|
setMaxVideoThroughput(c, getMaxVideoThroughput())
|
|
|
|
}
|
2020-08-11 17:09:31 +02:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2020-08-26 18:32:01 +02:00
|
|
|
/**
|
|
|
|
* @param {Stream} c
|
|
|
|
* @param {number} [bps]
|
|
|
|
*/
|
|
|
|
async function setMaxVideoThroughput(c, bps) {
|
|
|
|
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();
|
|
|
|
if(!p.encodings)
|
2020-09-03 19:56:23 +02:00
|
|
|
p.encodings = [{}];
|
2020-08-26 18:32:01 +02:00
|
|
|
p.encodings.forEach(e => {
|
|
|
|
if(bps > 0)
|
|
|
|
e.maxBitrate = bps;
|
|
|
|
else
|
|
|
|
delete e.maxBitrate;
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
await s.setParameters(p);
|
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 20:55:13 +02:00
|
|
|
/**
|
|
|
|
* @param {string} [id]
|
|
|
|
*/
|
2020-06-10 20:25:25 +02:00
|
|
|
async function addLocalMedia(id) {
|
2020-04-24 19:38:21 +02:00
|
|
|
if(!getUserPass())
|
|
|
|
return;
|
|
|
|
|
2020-09-18 17:57:37 +02:00
|
|
|
let settings = getSettings();
|
|
|
|
|
|
|
|
let audio = settings.audio ? {deviceId: settings.audio} : false;
|
|
|
|
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(audio) {
|
|
|
|
if(settings.studioMode) {
|
|
|
|
audio.echoCancellation = false;
|
|
|
|
audio.noiseSuppression = false;
|
|
|
|
audio.channelCount = 2;
|
|
|
|
audio.latency = 0.01;
|
|
|
|
audio.autoGainControl = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(video) {
|
|
|
|
if(settings.blackboardMode) {
|
|
|
|
video.width = { min: 640, ideal: 1920 };
|
|
|
|
video.height = { min: 400, ideal: 1080 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 20:30:55 +02:00
|
|
|
let old = id && serverConnection.up[id];
|
|
|
|
|
2020-06-10 20:25:25 +02:00
|
|
|
if(!audio && !video) {
|
2020-08-13 20:30:55 +02:00
|
|
|
if(old)
|
|
|
|
delUpMedia(old);
|
2020-05-05 17:50:16 +02:00
|
|
|
return;
|
2020-06-10 20:25:25 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 20:30:55 +02:00
|
|
|
if(old)
|
|
|
|
stopUpMedia(old);
|
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-08-13 20:30:55 +02:00
|
|
|
if(old)
|
|
|
|
delUpMedia(old);
|
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
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
let c = newUpStream(id);
|
2020-06-10 20:25:25 +02:00
|
|
|
|
2020-05-21 18:22:30 +02:00
|
|
|
c.kind = 'local';
|
2020-05-05 17:50:16 +02:00
|
|
|
c.stream = stream;
|
2020-09-18 17:57:37 +02:00
|
|
|
let mute = getSettings().localMute;
|
2020-05-05 17:50:16 +02:00
|
|
|
stream.getTracks().forEach(t => {
|
2020-05-17 22:31:29 +02:00
|
|
|
c.labels[t.id] = t.kind
|
2020-09-18 19:14:10 +02:00
|
|
|
if(t.kind == 'audio') {
|
|
|
|
if(mute)
|
|
|
|
t.enabled = false;
|
|
|
|
} else if(t.kind == 'video') {
|
|
|
|
if(settings.blackboardMode) {
|
2020-09-20 14:33:13 +02:00
|
|
|
/** @ts-ignore */
|
|
|
|
t.contentHint = 'detail';
|
2020-09-18 19:14:10 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-20 14:33:13 +02:00
|
|
|
c.pc.addTrack(t, stream);
|
2020-05-05 17:50:16 +02:00
|
|
|
});
|
2020-08-26 18:32:01 +02:00
|
|
|
|
2020-09-11 23:20:46 +02:00
|
|
|
c.onstats = gotUpStats;
|
2020-08-11 17:09:31 +02:00
|
|
|
c.setStatsInterval(2000);
|
|
|
|
await setMedia(c, true);
|
2020-09-01 10:23:35 +02:00
|
|
|
setButtonsVisibility();
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
async function addShareMedia() {
|
2020-04-24 19:38:21 +02:00
|
|
|
if(!getUserPass())
|
|
|
|
return;
|
|
|
|
|
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');
|
|
|
|
/** @ts-ignore */
|
2020-09-10 10:02:34 +02:00
|
|
|
stream = await navigator.mediaDevices.getDisplayMedia({video: 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();
|
2020-05-21 18:22:30 +02:00
|
|
|
c.kind = 'screenshare';
|
|
|
|
c.stream = stream;
|
|
|
|
stream.getTracks().forEach(t => {
|
2020-09-20 14:33:13 +02:00
|
|
|
c.pc.addTrack(t, stream);
|
2020-05-21 18:22:30 +02:00
|
|
|
t.onended = e => {
|
2020-08-24 22:24:33 +02:00
|
|
|
delUpMedia(c);
|
2020-05-21 18:22:30 +02:00
|
|
|
};
|
|
|
|
c.labels[t.id] = 'screenshare';
|
|
|
|
});
|
2020-09-11 23:20:46 +02:00
|
|
|
c.onstats = gotUpStats;
|
2020-08-11 17:09:31 +02:00
|
|
|
c.setStatsInterval(2000);
|
|
|
|
await setMedia(c, true);
|
2020-05-21 18:22:30 +02:00
|
|
|
setButtonsVisibility()
|
|
|
|
}
|
|
|
|
|
2020-08-13 20:55:13 +02:00
|
|
|
/**
|
|
|
|
* @param {Stream} c
|
|
|
|
*/
|
2020-08-11 17:09:31 +02:00
|
|
|
function stopUpMedia(c) {
|
2020-06-10 20:25:25 +02:00
|
|
|
if(!c.stream)
|
|
|
|
return;
|
|
|
|
c.stream.getTracks().forEach(t => {
|
|
|
|
try {
|
|
|
|
t.stop();
|
|
|
|
} catch(e) {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-13 20:55:13 +02:00
|
|
|
/**
|
|
|
|
* @param {Stream} c
|
|
|
|
*/
|
2020-08-11 17:09:31 +02:00
|
|
|
function delUpMedia(c) {
|
|
|
|
stopUpMedia(c);
|
2020-08-19 14:39:40 +02:00
|
|
|
try {
|
|
|
|
delMedia(c.id);
|
|
|
|
} catch(e) {
|
|
|
|
console.warn(e);
|
|
|
|
}
|
2020-06-10 20:25:25 +02:00
|
|
|
c.close(true);
|
2020-08-11 17:09:31 +02:00
|
|
|
delete(serverConnection.up[c.id]);
|
2020-05-21 18:22:30 +02:00
|
|
|
setButtonsVisibility()
|
|
|
|
}
|
|
|
|
|
2020-08-23 19:07:52 +02:00
|
|
|
/**
|
|
|
|
* delUpMediaKind reoves all up media of the given kind. If kind is
|
|
|
|
* falseish, it removes all up media.
|
|
|
|
* @param {string} kind
|
|
|
|
*/
|
2020-05-21 18:22:30 +02:00
|
|
|
function delUpMediaKind(kind) {
|
2020-08-11 17:09:31 +02:00
|
|
|
for(let id in serverConnection.up) {
|
|
|
|
let c = serverConnection.up[id];
|
2020-08-23 19:07:52 +02:00
|
|
|
if(kind && c.kind != kind)
|
2020-05-21 18:22:30 +02:00
|
|
|
continue
|
|
|
|
c.close(true);
|
|
|
|
delMedia(id);
|
2020-08-11 17:09:31 +02:00
|
|
|
delete(serverConnection.up[id]);
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-05-21 18:22:30 +02:00
|
|
|
|
2020-09-01 10:23:35 +02:00
|
|
|
setButtonsVisibility();
|
|
|
|
hideVideo();
|
2020-05-21 18:22:30 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
/**
|
|
|
|
* @param {string} kind
|
|
|
|
*/
|
2020-05-21 18:22:30 +02:00
|
|
|
function findUpMedia(kind) {
|
2020-08-11 17:09:31 +02:00
|
|
|
for(let id in serverConnection.up) {
|
|
|
|
if(serverConnection.up[id].kind === kind)
|
2020-06-10 20:25:25 +02:00
|
|
|
return id;
|
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];
|
2020-06-09 18:05:16 +02:00
|
|
|
if(c.kind === 'local') {
|
|
|
|
let stream = c.stream;
|
|
|
|
stream.getTracks().forEach(t => {
|
|
|
|
if(t.kind === 'audio') {
|
|
|
|
t.enabled = !mute;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
|
|
|
* @param {Stream} c
|
|
|
|
* @param {boolean} isUp
|
|
|
|
*/
|
|
|
|
function setMedia(c, isUp) {
|
2020-04-24 19:38:21 +02:00
|
|
|
let peersdiv = document.getElementById('peers');
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
let div = document.getElementById('peer-' + c.id);
|
2020-04-24 19:38:21 +02:00
|
|
|
if(!div) {
|
|
|
|
div = document.createElement('div');
|
2020-08-11 17:09:31 +02:00
|
|
|
div.id = 'peer-' + c.id;
|
2020-04-24 19:38:21 +02:00
|
|
|
div.classList.add('peer');
|
|
|
|
peersdiv.appendChild(div);
|
|
|
|
}
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
let media = /** @type {HTMLVideoElement} */
|
|
|
|
(document.getElementById('media-' + c.id));
|
2020-04-24 19:38:21 +02:00
|
|
|
if(!media) {
|
|
|
|
media = document.createElement('video');
|
2020-08-11 17:09:31 +02:00
|
|
|
media.id = 'media-' + c.id;
|
2020-04-24 19:38:21 +02:00
|
|
|
media.classList.add('media');
|
|
|
|
media.autoplay = true;
|
2020-09-20 14:33:13 +02:00
|
|
|
/** @ts-ignore */
|
2020-04-24 19:38:21 +02:00
|
|
|
media.playsinline = true;
|
|
|
|
media.controls = true;
|
2020-08-11 17:09:31 +02:00
|
|
|
if(isUp)
|
2020-04-24 19:38:21 +02:00
|
|
|
media.muted = true;
|
|
|
|
div.appendChild(media);
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
let label = document.getElementById('label-' + c.id);
|
2020-04-24 19:38:21 +02:00
|
|
|
if(!label) {
|
|
|
|
label = document.createElement('div');
|
2020-08-11 17:09:31 +02:00
|
|
|
label.id = 'label-' + c.id;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
media.srcObject = c.stream;
|
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-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 16:34:52 +02:00
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
*/
|
2020-04-24 19:38:21 +02:00
|
|
|
function delMedia(id) {
|
|
|
|
let mediadiv = document.getElementById('peers');
|
|
|
|
let peer = document.getElementById('peer-' + id);
|
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} */
|
|
|
|
(document.getElementById('media-' + id));
|
2020-04-24 19:38:21 +02:00
|
|
|
|
|
|
|
media.srcObject = null;
|
|
|
|
mediadiv.removeChild(peer);
|
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';
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
let media = document.getElementById('media-' + c.id);
|
2020-06-11 19:33:39 +02:00
|
|
|
if(!media) {
|
|
|
|
console.warn('Setting status of unknown media.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(good)
|
|
|
|
media.classList.remove('media-failed');
|
|
|
|
else
|
|
|
|
media.classList.add('media-failed');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
|
|
|
* @param {Stream} c
|
|
|
|
* @param {string} [fallback]
|
|
|
|
*/
|
|
|
|
function setLabel(c, fallback) {
|
|
|
|
let label = document.getElementById('label-' + c.id);
|
2020-04-24 19:38:21 +02:00
|
|
|
if(!label)
|
|
|
|
return;
|
2020-08-11 17:09:31 +02:00
|
|
|
let l = c.label;
|
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-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-09-09 20:26:19 +02:00
|
|
|
let container = document.getElementById("video-container")
|
2020-09-04 17:34:38 +02:00
|
|
|
// Peers div has total padding of 30px, we remove 30 on offsetHeight
|
2020-09-03 17:01:21 +02:00
|
|
|
let max_video_height = Math.trunc((peers.offsetHeight - 30) / columns);
|
|
|
|
|
|
|
|
let media_list = document.getElementsByClassName("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;
|
|
|
|
}
|
|
|
|
media.style['max_height'] = max_video_height + "px";
|
|
|
|
}
|
2020-09-09 20:26:19 +02:00
|
|
|
|
|
|
|
if (count <= 2 && container.offsetHeight > container.offsetWidth) {
|
|
|
|
peers.style['grid-template-columns'] = "repeat(1, 1fr)";
|
|
|
|
} else {
|
|
|
|
peers.style['grid-template-columns'] = `repeat(${columns}, 1fr)`;
|
|
|
|
}
|
2020-05-01 01:22:17 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
/** @type{Object<string,string>} */
|
2020-04-24 19:38:21 +02:00
|
|
|
let users = {};
|
|
|
|
|
2020-08-13 20:55:13 +02:00
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
* @param {string} name
|
|
|
|
*/
|
2020-04-24 19:38:21 +02:00
|
|
|
function addUser(id, name) {
|
|
|
|
if(!name)
|
|
|
|
name = null;
|
|
|
|
if(id in users)
|
|
|
|
throw new Error('Duplicate user id');
|
|
|
|
users[id] = name;
|
|
|
|
|
|
|
|
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");
|
2020-04-24 19:38:21 +02:00
|
|
|
user.textContent = name ? name : '(anon)';
|
2020-09-12 15:23:38 +02:00
|
|
|
|
|
|
|
if(name) {
|
|
|
|
let us = div.children;
|
|
|
|
for(let i = 0; i < us.length; i++) {
|
|
|
|
let child = us[i];
|
|
|
|
let childname = users[child.id.slice('user-'.length)];
|
|
|
|
if(!childname || childname > name) {
|
|
|
|
div.insertBefore(user, child);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
div.appendChild(user);
|
|
|
|
}
|
|
|
|
|
2020-08-13 20:55:13 +02:00
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
* @param {string} name
|
|
|
|
*/
|
2020-04-24 19:38:21 +02:00
|
|
|
function delUser(id, name) {
|
|
|
|
if(!name)
|
|
|
|
name = null;
|
2020-05-10 21:18:10 +02:00
|
|
|
if(!(id in users))
|
2020-04-24 19:38:21 +02:00
|
|
|
throw new Error('Unknown user id');
|
|
|
|
if(users[id] !== name)
|
|
|
|
throw new Error('Inconsistent user name');
|
|
|
|
delete(users[id]);
|
|
|
|
let div = document.getElementById('users');
|
|
|
|
let user = document.getElementById('user-' + id);
|
|
|
|
div.removeChild(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
function resetUsers() {
|
|
|
|
for(let id in users)
|
|
|
|
delUser(id, users[id]);
|
|
|
|
}
|
|
|
|
|
2020-08-13 20:55:13 +02:00
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
* @param {string} kind
|
|
|
|
* @param {string} name
|
|
|
|
*/
|
2020-08-12 13:51:31 +02:00
|
|
|
function gotUser(id, kind, name) {
|
|
|
|
switch(kind) {
|
|
|
|
case 'add':
|
2020-04-24 19:38:21 +02:00
|
|
|
addUser(id, name);
|
2020-08-12 13:51:31 +02:00
|
|
|
break;
|
|
|
|
case 'delete':
|
|
|
|
delUser(id, name);
|
|
|
|
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() {
|
|
|
|
let userpass = getUserPass();
|
|
|
|
let text = '';
|
|
|
|
if(userpass && userpass.username)
|
|
|
|
text = 'as ' + userpass.username;
|
2020-08-11 17:09:31 +02:00
|
|
|
if(serverConnection.permissions.op && serverConnection.permissions.present)
|
2020-04-25 18:09:31 +02:00
|
|
|
text = text + ' (op, presenter)';
|
2020-08-11 17:09:31 +02:00
|
|
|
else if(serverConnection.permissions.op)
|
2020-04-25 18:09:31 +02:00
|
|
|
text = text + ' (op)';
|
2020-08-11 17:09:31 +02:00
|
|
|
else if(serverConnection.permissions.present)
|
2020-04-25 18:09:31 +02:00
|
|
|
text = text + ' (presenter)';
|
|
|
|
document.getElementById('userspan').textContent = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearUsername() {
|
|
|
|
document.getElementById('userspan').textContent = '';
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
/**
|
2020-09-20 14:33:13 +02:00
|
|
|
* @param {Object<string,boolean>} perms
|
2020-08-11 17:09:31 +02:00
|
|
|
*/
|
|
|
|
function gotPermissions(perms) {
|
2020-04-25 18:09:31 +02:00
|
|
|
displayUsername();
|
2020-05-21 18:22:30 +02:00
|
|
|
setButtonsVisibility();
|
2020-09-10 01:22:06 +02:00
|
|
|
if(serverConnection.permissions.present)
|
2020-09-11 22:46:23 +02:00
|
|
|
displayMessage("Press Present to enable your camera or microphone");
|
2020-04-25 02:25:51 +02:00
|
|
|
}
|
|
|
|
|
2020-05-11 00:05:22 +02: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
|
|
|
/**
|
|
|
|
* @param {string} line
|
2020-09-20 14:33:13 +02:00
|
|
|
* @returns {(Text|HTMLElement)[]}
|
2020-09-12 16:34:52 +02:00
|
|
|
*/
|
2020-04-24 19:38:21 +02:00
|
|
|
function formatLine(line) {
|
|
|
|
let r = new RegExp(urlRegexp);
|
|
|
|
let result = [];
|
|
|
|
let pos = 0;
|
|
|
|
while(true) {
|
|
|
|
let m = r.exec(line);
|
|
|
|
if(!m)
|
|
|
|
break;
|
|
|
|
result.push(document.createTextNode(line.slice(pos, m.index)));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
result.push(document.createTextNode(line.slice(pos)));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-09-12 16:34:52 +02:00
|
|
|
/**
|
2020-09-20 14:33:13 +02:00
|
|
|
* @param {string[]} lines
|
2020-09-12 16:34:52 +02:00
|
|
|
* @returns {HTMLElement}
|
|
|
|
*/
|
2020-04-24 19:38:21 +02:00
|
|
|
function formatLines(lines) {
|
|
|
|
let elts = [];
|
|
|
|
if(lines.length > 0)
|
|
|
|
elts = formatLine(lines[0]);
|
|
|
|
for(let i = 1; i < lines.length; i++) {
|
|
|
|
elts.push(document.createElement('br'));
|
|
|
|
elts = elts.concat(formatLine(lines[i]));
|
|
|
|
}
|
|
|
|
let elt = document.createElement('p');
|
|
|
|
elts.forEach(e => elt.appendChild(e));
|
|
|
|
return elt;
|
|
|
|
}
|
|
|
|
|
2020-09-30 00:33:23 +02:00
|
|
|
/**
|
|
|
|
* @param {number} time
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function formatTime(time) {
|
|
|
|
let delta = Date.now() - time;
|
|
|
|
let date = new Date(time);
|
|
|
|
if(delta >= 0)
|
|
|
|
return date.toTimeString().slice(null, 8);
|
|
|
|
return date.toLocaleString();
|
|
|
|
}
|
|
|
|
|
2020-08-24 22:37:48 +02:00
|
|
|
/**
|
|
|
|
* @typedef {Object} lastMessage
|
|
|
|
* @property {string} [nick]
|
|
|
|
* @property {string} [peerId]
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @type {lastMessage} */
|
2020-04-24 19:38:21 +02:00
|
|
|
let lastMessage = {};
|
|
|
|
|
2020-09-20 14:33:13 +02:00
|
|
|
/**
|
|
|
|
* @param {string} peerId
|
|
|
|
* @param {string} nick
|
2020-09-30 00:33:23 +02:00
|
|
|
* @param {number} time
|
2020-09-20 14:33:13 +02:00
|
|
|
* @param {string} kind
|
|
|
|
* @param {string} message
|
|
|
|
*/
|
2020-09-30 00:33:23 +02:00
|
|
|
function addToChatbox(peerId, nick, time, kind, message){
|
2020-09-02 15:35:55 +02:00
|
|
|
let userpass = getUserPass();
|
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-09-23 21:01:29 +02:00
|
|
|
if(!peerId)
|
|
|
|
container.classList.add('message-system');
|
|
|
|
else if(userpass.username === nick) {
|
|
|
|
container.classList.add('message-sender');
|
2020-09-02 15:35:55 +02:00
|
|
|
}
|
2020-08-12 12:17:56 +02:00
|
|
|
if(kind !== 'me') {
|
2020-04-24 19:38:21 +02:00
|
|
|
let p = formatLines(message.split('\n'));
|
|
|
|
if (lastMessage.nick !== nick || lastMessage.peerId !== peerId) {
|
2020-09-30 00:33:23 +02:00
|
|
|
let header = document.createElement('p');
|
|
|
|
let user = document.createElement('span');
|
2020-04-24 19:38:21 +02:00
|
|
|
user.textContent = nick;
|
|
|
|
user.classList.add('message-user');
|
2020-09-30 00:33:23 +02:00
|
|
|
header.appendChild(user);
|
|
|
|
if(time) {
|
|
|
|
let tm = document.createElement('span');
|
|
|
|
tm.textContent = formatTime(time);
|
|
|
|
tm.classList.add('message-time');
|
|
|
|
header.appendChild(tm);
|
|
|
|
}
|
|
|
|
header.classList.add('message-header');
|
|
|
|
container.appendChild(header);
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
p.classList.add('message-content');
|
|
|
|
container.appendChild(p);
|
|
|
|
lastMessage.nick = nick;
|
|
|
|
lastMessage.peerId = peerId;
|
|
|
|
} else {
|
|
|
|
let asterisk = document.createElement('span');
|
|
|
|
asterisk.textContent = '*';
|
|
|
|
asterisk.classList.add('message-me-asterisk');
|
|
|
|
let user = document.createElement('span');
|
|
|
|
user.textContent = nick;
|
|
|
|
user.classList.add('message-me-user');
|
|
|
|
let content = document.createElement('span');
|
|
|
|
formatLine(message).forEach(elt => {
|
|
|
|
content.appendChild(elt);
|
|
|
|
});
|
|
|
|
content.classList.add('message-me-content');
|
|
|
|
container.appendChild(asterisk);
|
|
|
|
container.appendChild(user);
|
|
|
|
container.appendChild(content);
|
|
|
|
container.classList.add('message-me');
|
|
|
|
delete(lastMessage.nick);
|
|
|
|
delete(lastMessage.peerId);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
function clearChat() {
|
2020-04-25 21:16:49 +02:00
|
|
|
lastMessage = {};
|
|
|
|
document.getElementById('box').textContent = '';
|
|
|
|
}
|
|
|
|
|
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-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-04-24 19:38:21 +02:00
|
|
|
message = data.substring(1);
|
|
|
|
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) {
|
|
|
|
cmd = data;
|
|
|
|
rest = '';
|
|
|
|
} else {
|
|
|
|
cmd = data.slice(0, space);
|
2020-09-12 12:26:07 +02:00
|
|
|
rest = data.slice(space + 1);
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch(cmd) {
|
|
|
|
case '/me':
|
|
|
|
message = rest;
|
|
|
|
me = true;
|
|
|
|
break;
|
2020-04-25 18:43:22 +02:00
|
|
|
case '/leave':
|
2020-08-11 17:09:31 +02:00
|
|
|
serverConnection.close();
|
2020-04-25 18:43:22 +02:00
|
|
|
return;
|
2020-04-30 19:13:10 +02:00
|
|
|
case '/clear':
|
2020-08-11 17:09:31 +02:00
|
|
|
if(!serverConnection.permissions.op) {
|
2020-04-30 19:13:10 +02:00
|
|
|
displayError("You're not an operator");
|
|
|
|
return;
|
|
|
|
}
|
2020-08-11 17:09:31 +02:00
|
|
|
serverConnection.groupAction('clearchat');
|
2020-04-30 19:13:10 +02:00
|
|
|
return;
|
2020-09-23 21:01:29 +02:00
|
|
|
case '/set':
|
|
|
|
if(!rest) {
|
|
|
|
let settings = getSettings();
|
|
|
|
let s = "";
|
|
|
|
for(let key in settings)
|
|
|
|
s = s + `${key}: ${JSON.stringify(settings[key])}\n`
|
2020-09-30 00:33:23 +02:00
|
|
|
addToChatbox(null, null, Date.now(), null, s);
|
2020-09-23 21:01:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let parsed = parseCommand(rest);
|
|
|
|
let value;
|
|
|
|
if(parsed[1]) {
|
|
|
|
try {
|
|
|
|
value = JSON.parse(parsed[1])
|
|
|
|
} catch(e) {
|
|
|
|
displayError(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
value = true;
|
|
|
|
}
|
|
|
|
updateSetting(parsed[0], value);
|
|
|
|
reflectSettings();
|
|
|
|
return;
|
|
|
|
case '/unset':
|
|
|
|
delSetting(rest.trim());
|
|
|
|
return;
|
2020-05-18 15:24:04 +02:00
|
|
|
case '/lock':
|
|
|
|
case '/unlock':
|
2020-08-11 17:09:31 +02:00
|
|
|
if(!serverConnection.permissions.op) {
|
2020-05-18 15:24:04 +02:00
|
|
|
displayError("You're not an operator");
|
|
|
|
return;
|
|
|
|
}
|
2020-09-18 11:40:00 +02:00
|
|
|
serverConnection.groupAction(cmd.slice(1), rest);
|
2020-05-30 00:23:54 +02:00
|
|
|
return;
|
|
|
|
case '/record':
|
|
|
|
case '/unrecord':
|
2020-08-11 17:09:31 +02:00
|
|
|
if(!serverConnection.permissions.record) {
|
2020-05-30 00:23:54 +02:00
|
|
|
displayError("You're not allowed to record");
|
|
|
|
return;
|
|
|
|
}
|
2020-08-11 17:09:31 +02:00
|
|
|
serverConnection.groupAction(cmd.slice(1));
|
2020-05-18 15:24:04 +02:00
|
|
|
return;
|
2020-04-25 17:36:35 +02:00
|
|
|
case '/op':
|
|
|
|
case '/unop':
|
|
|
|
case '/kick':
|
|
|
|
case '/present':
|
2020-05-10 21:18:12 +02:00
|
|
|
case '/unpresent': {
|
2020-08-11 17:09:31 +02:00
|
|
|
if(!serverConnection.permissions.op) {
|
2020-04-25 18:09:31 +02:00
|
|
|
displayError("You're not an operator");
|
2020-04-25 17:36:35 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-09-16 01:42:39 +02:00
|
|
|
let parsed = parseCommand(rest);
|
2020-04-25 17:36:35 +02:00
|
|
|
let id;
|
2020-09-16 01:42:39 +02:00
|
|
|
if(parsed[0] in users) {
|
|
|
|
id = parsed[0];
|
2020-04-25 17:36:35 +02:00
|
|
|
} else {
|
|
|
|
for(let i in users) {
|
2020-09-16 01:42:39 +02:00
|
|
|
if(users[i] === parsed[0]) {
|
2020-04-25 17:36:35 +02:00
|
|
|
id = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!id) {
|
2020-09-16 01:42:39 +02:00
|
|
|
displayError('Unknown user ' + parsed[0]);
|
2020-04-25 17:36:35 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-09-16 01:42:39 +02:00
|
|
|
serverConnection.userAction(cmd.slice(1), id, parsed[1]);
|
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
|
|
|
default:
|
|
|
|
displayError('Uknown command ' + cmd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
message = data;
|
|
|
|
me = false;
|
|
|
|
}
|
|
|
|
|
2020-09-23 21:22:55 +02:00
|
|
|
if(!serverConnection || !serverConnection.socket) {
|
|
|
|
displayError("Not connected.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
let username = getUsername();
|
2020-04-25 18:42:06 +02:00
|
|
|
if(!username) {
|
|
|
|
displayError("Sorry, you're anonymous, you cannot chat");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-09 01:46:17 +02:00
|
|
|
try {
|
2020-08-11 17:09:31 +02:00
|
|
|
serverConnection.chat(username, 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-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";
|
2020-09-20 14:33:13 +02:00
|
|
|
|
|
|
|
var background = 'linear-gradient(to right, #e20a0a, #df2d2d)';
|
|
|
|
var position = 'center';
|
|
|
|
var gravity = 'top';
|
|
|
|
|
|
|
|
switch(level) {
|
2020-09-28 20:58:07 +02:00
|
|
|
case "info":
|
2020-09-20 14:33:13 +02:00
|
|
|
background = 'linear-gradient(to right, #529518, #96c93d)';
|
|
|
|
position = 'right';
|
|
|
|
gravity = 'bottom';
|
|
|
|
break;
|
2020-09-28 20:58:07 +02:00
|
|
|
case "warning":
|
2020-09-20 14:33:13 +02:00
|
|
|
background = "linear-gradient(to right, #edd800, #c9c200)";
|
|
|
|
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,
|
|
|
|
backgroundColor: background,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
document.getElementById('userform').onsubmit = function(e) {
|
2020-04-24 19:38:21 +02:00
|
|
|
e.preventDefault();
|
2020-09-20 14:33:13 +02:00
|
|
|
let username = getInputElement('username').value.trim();
|
|
|
|
let password = getInputElement('password').value;
|
2020-09-18 17:57:37 +02:00
|
|
|
storeUserPass(username, password);
|
2020-08-11 17:09:31 +02:00
|
|
|
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-09-02 15:35:55 +02:00
|
|
|
let user_box = document.getElementById('userDropdown');
|
|
|
|
if (user_box.classList.contains("show")) {
|
|
|
|
user_box.classList.toggle("show");
|
|
|
|
}
|
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('user').onclick = function(e) {
|
2020-09-02 17:55:44 +02:00
|
|
|
e.preventDefault();
|
2020-08-27 21:17:46 +02:00
|
|
|
document.getElementById("userDropdown").classList.toggle("show");
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
2020-09-20 14:33:13 +02:00
|
|
|
if(!(this instanceof HTMLElement))
|
|
|
|
throw new Error('Unexpected type for this');
|
2020-09-09 20:26:19 +02:00
|
|
|
let width = window.innerWidth;
|
2020-09-01 10:23:35 +02:00
|
|
|
if (width <= 768) {
|
|
|
|
let user_box = document.getElementById('userDropdown');
|
|
|
|
if (user_box.classList.contains("show")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// fixed div for small screen
|
2020-09-09 20:26:19 +02:00
|
|
|
this.style.display = "";
|
2020-09-01 10:23:35 +02:00
|
|
|
hideVideo(true);
|
|
|
|
document.getElementById('switch-video').style.display = "block";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
document.getElementById('switch-video').onclick = function(e) {
|
|
|
|
e.preventDefault();
|
2020-09-20 14:33:13 +02:00
|
|
|
if(!(this instanceof HTMLElement))
|
|
|
|
throw new Error('Unexpected type for this');
|
2020-09-01 10:23:35 +02:00
|
|
|
showVideo();
|
|
|
|
this.style.display = "";
|
2020-09-09 20:26:19 +02:00
|
|
|
document.getElementById('collapse-video').style.display = "block";
|
|
|
|
};
|
|
|
|
|
|
|
|
window.onclick = function(event) {
|
|
|
|
let user_box = document.getElementById('userDropdown');
|
|
|
|
if (user_box.classList.contains("show") && event.target.id != "user") {
|
|
|
|
let parent = event.target;
|
|
|
|
while (parent.id !== "main" && parent.id !== "userDropdown" &&
|
|
|
|
parent.id !== "user" && parent.tagName !== "body") {
|
|
|
|
parent = parent.parentNode;
|
|
|
|
}
|
|
|
|
if (parent.id !="userDropdown" && parent.id !== "user") {
|
|
|
|
user_box.classList.toggle("show");
|
|
|
|
}
|
|
|
|
}
|
2020-09-01 10:23:35 +02:00
|
|
|
};
|
|
|
|
|
2020-09-18 11:28:13 +02:00
|
|
|
async function serverConnect() {
|
2020-08-11 17:09:31 +02:00
|
|
|
serverConnection = new ServerConnection();
|
|
|
|
serverConnection.onconnected = gotConnected;
|
|
|
|
serverConnection.onclose = gotClose;
|
|
|
|
serverConnection.ondownstream = gotDownStream;
|
|
|
|
serverConnection.onuser = gotUser;
|
|
|
|
serverConnection.onpermissions = gotPermissions;
|
|
|
|
serverConnection.onchat = addToChatbox;
|
|
|
|
serverConnection.onclearchat = clearChat;
|
|
|
|
serverConnection.onusermessage = function(kind, message) {
|
|
|
|
if(kind === 'error')
|
|
|
|
displayError(`The server said: ${message}`);
|
|
|
|
else
|
|
|
|
displayWarning(`The server said: ${message}`);
|
|
|
|
}
|
2020-09-18 11:28:13 +02:00
|
|
|
let url = `ws${location.protocol === 'https:' ? 's' : ''}://${location.host}/ws`;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
function start() {
|
|
|
|
group = decodeURIComponent(location.pathname.replace(/^\/[a-z]*\//, ''));
|
2020-04-25 17:43:56 +02:00
|
|
|
let title = group.charAt(0).toUpperCase() + group.slice(1);
|
|
|
|
if(group !== '') {
|
|
|
|
document.title = title;
|
2020-05-10 21:18:07 +02:00
|
|
|
document.getElementById('title').textContent = title;
|
2020-04-25 17:43:56 +02:00
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2020-09-18 17:57:37 +02:00
|
|
|
setMediaChoices(false).then(e => reflectSettings());
|
2020-06-09 18:05:16 +02:00
|
|
|
|
2020-09-16 01:02:46 +02:00
|
|
|
document.getElementById("user").classList.add('invisible');
|
|
|
|
document.getElementById("login-container").classList.remove('invisible');
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
start();
|