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';
|
|
|
|
|
|
|
|
let group;
|
2020-08-11 17:09:31 +02:00
|
|
|
let serverConnection;
|
2020-04-24 19:38:21 +02:00
|
|
|
|
|
|
|
function setUserPass(username, password) {
|
|
|
|
window.sessionStorage.setItem(
|
|
|
|
'userpass',
|
|
|
|
JSON.stringify({username: username, password: password}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getUserPass() {
|
|
|
|
let userpass = window.sessionStorage.getItem('userpass');
|
|
|
|
if(!userpass)
|
|
|
|
return null;
|
|
|
|
return JSON.parse(userpass);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getUsername() {
|
|
|
|
let userpass = getUserPass();
|
|
|
|
if(!userpass)
|
|
|
|
return null;
|
|
|
|
return userpass.username;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setConnected(connected) {
|
|
|
|
let statspan = document.getElementById('statspan');
|
|
|
|
let userform = document.getElementById('userform');
|
2020-05-14 12:24:23 +02:00
|
|
|
let disconnectbutton = document.getElementById('disconnectbutton');
|
2020-04-24 19:38:21 +02:00
|
|
|
if(connected) {
|
2020-04-26 19:36:35 +02:00
|
|
|
clearError();
|
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-05-21 18:22:30 +02:00
|
|
|
userform.classList.add('invisible');
|
2020-04-24 19:38:21 +02:00
|
|
|
userform.classList.remove('userform');
|
2020-05-21 18:22:30 +02:00
|
|
|
disconnectbutton.classList.remove('invisible');
|
2020-04-25 18:09:31 +02:00
|
|
|
displayUsername();
|
2020-04-24 19:38:21 +02:00
|
|
|
} else {
|
|
|
|
let userpass = getUserPass();
|
|
|
|
document.getElementById('username').value =
|
|
|
|
userpass ? userpass.username : '';
|
|
|
|
document.getElementById('password').value =
|
|
|
|
userpass ? userpass.password : '';
|
|
|
|
statspan.textContent = 'Disconnected';
|
|
|
|
statspan.classList.remove('connected');
|
|
|
|
statspan.classList.add('disconnected');
|
|
|
|
userform.classList.add('userform');
|
2020-05-21 18:22:30 +02:00
|
|
|
userform.classList.remove('invisible');
|
|
|
|
disconnectbutton.classList.add('invisible');
|
2020-08-11 17:09:31 +02:00
|
|
|
clearUsername();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function gotConnected() {
|
|
|
|
setConnected(true);
|
|
|
|
let up = getUserPass();
|
|
|
|
this.login(up.username, up.password);
|
|
|
|
this.join(group);
|
|
|
|
this.request(document.getElementById('requestselect').value);
|
|
|
|
}
|
|
|
|
|
|
|
|
function gotClose(code, reason) {
|
|
|
|
setConnected(false);
|
|
|
|
if(code != 1000)
|
|
|
|
console.warn('Socket close', code, reason);
|
|
|
|
}
|
|
|
|
|
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-05-21 18:22:30 +02:00
|
|
|
document.getElementById('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-05-21 18:22:30 +02:00
|
|
|
document.getElementById('unpresentbutton').onclick = function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
delUpMediaKind('local');
|
|
|
|
};
|
|
|
|
|
|
|
|
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-05-21 18:22:30 +02:00
|
|
|
// don't allow multiple presentations
|
|
|
|
setVisibility('presentbutton', permissions.present && !local);
|
|
|
|
setVisibility('unpresentbutton', local);
|
|
|
|
// allow multiple shared documents
|
|
|
|
setVisibility('sharebutton', permissions.present);
|
|
|
|
setVisibility('unsharebutton', share);
|
|
|
|
|
|
|
|
setVisibility('mediaoptions', permissions.present);
|
|
|
|
}
|
|
|
|
|
2020-06-09 18:05:16 +02:00
|
|
|
let localMute = false;
|
|
|
|
|
|
|
|
function toggleLocalMute() {
|
|
|
|
setLocalMute(!localMute);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setLocalMute(mute) {
|
|
|
|
localMute = mute;
|
|
|
|
muteLocalTracks(localMute);
|
|
|
|
let button = document.getElementById('mutebutton');
|
|
|
|
button.textContent = localMute ? 'Unmute' : 'Mute';
|
|
|
|
if(localMute)
|
|
|
|
button.classList.add('muted');
|
|
|
|
else
|
|
|
|
button.classList.remove('muted');
|
|
|
|
}
|
|
|
|
|
|
|
|
document.getElementById('videoselect').onchange = function(e) {
|
2020-05-05 17:50:16 +02:00
|
|
|
e.preventDefault();
|
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-06-09 18:05:16 +02:00
|
|
|
document.getElementById('audioselect').onchange = function(e) {
|
2020-05-05 17:50:16 +02:00
|
|
|
e.preventDefault();
|
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-06-09 18:05:16 +02:00
|
|
|
document.getElementById('mutebutton').onclick = function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
toggleLocalMute();
|
|
|
|
}
|
|
|
|
|
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-05-17 22:31:29 +02:00
|
|
|
document.getElementById('requestselect').onchange = function(e) {
|
2020-05-09 19:39:34 +02:00
|
|
|
e.preventDefault();
|
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-08-11 17:09:31 +02:00
|
|
|
function displayStats(stats) {
|
|
|
|
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-06-12 15:42:44 +02:00
|
|
|
if(stats && stats.rate > 0) {
|
|
|
|
if(text)
|
|
|
|
text = text + ' + ';
|
|
|
|
text = text + Math.round(stats.rate / 1000) + 'kbps';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
setLabel(c, text);
|
2020-04-26 19:21:01 +02:00
|
|
|
}
|
|
|
|
|
2020-05-05 17:50:16 +02:00
|
|
|
function mapMediaOption(value) {
|
|
|
|
console.assert(typeof(value) === 'string');
|
|
|
|
switch(value) {
|
|
|
|
case 'default':
|
|
|
|
return true;
|
|
|
|
case 'off':
|
|
|
|
return false;
|
|
|
|
default:
|
2020-05-29 15:54:39 +02:00
|
|
|
return {deviceId: value};
|
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++) {
|
|
|
|
if(select.children[i].value === value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let option = document.createElement('option');
|
|
|
|
option.value = value;
|
|
|
|
option.textContent = label;
|
|
|
|
select.appendChild(option);
|
|
|
|
}
|
|
|
|
|
|
|
|
// media names might not be available before we call getDisplayMedia. So
|
|
|
|
// we call this lazily.
|
|
|
|
let mediaChoicesDone = false;
|
|
|
|
|
|
|
|
async function setMediaChoices() {
|
|
|
|
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}`;
|
|
|
|
addSelectOption(document.getElementById('videoselect'),
|
|
|
|
label, d.deviceId);
|
|
|
|
cn++;
|
|
|
|
} else if(d.kind === 'audioinput') {
|
|
|
|
if(!label)
|
|
|
|
label = `Microphone ${mn}`;
|
|
|
|
addSelectOption(document.getElementById('audioselect'),
|
|
|
|
label, d.deviceId);
|
|
|
|
mn++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mediaChoicesDone = true;
|
|
|
|
}
|
|
|
|
|
2020-08-13 20:55:13 +02:00
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
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-05-05 17:50:16 +02:00
|
|
|
let audio = mapMediaOption(document.getElementById('audioselect').value);
|
|
|
|
let video = mapMediaOption(document.getElementById('videoselect').value);
|
|
|
|
|
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};
|
|
|
|
let stream = null;
|
|
|
|
try {
|
|
|
|
stream = await navigator.mediaDevices.getUserMedia(constraints);
|
|
|
|
} catch(e) {
|
|
|
|
console.error(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
|
|
|
|
|
|
|
setMediaChoices();
|
|
|
|
|
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;
|
|
|
|
stream.getTracks().forEach(t => {
|
2020-05-17 22:31:29 +02:00
|
|
|
c.labels[t.id] = t.kind
|
2020-06-09 18:05:16 +02:00
|
|
|
if(t.kind == 'audio' && localMute)
|
|
|
|
t.enabled = false;
|
2020-05-05 17:50:16 +02:00
|
|
|
let sender = c.pc.addTrack(t, stream);
|
|
|
|
});
|
2020-08-11 17:09:31 +02:00
|
|
|
c.onstats = displayStats;
|
|
|
|
c.setStatsInterval(2000);
|
|
|
|
await setMedia(c, true);
|
2020-05-21 18:22:30 +02:00
|
|
|
setButtonsVisibility()
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-21 18:22:30 +02:00
|
|
|
async function addShareMedia(setup) {
|
2020-04-24 19:38:21 +02:00
|
|
|
if(!getUserPass())
|
|
|
|
return;
|
|
|
|
|
2020-05-21 18:22:30 +02:00
|
|
|
let stream = null;
|
|
|
|
try {
|
|
|
|
stream = await navigator.mediaDevices.getDisplayMedia({});
|
|
|
|
} catch(e) {
|
|
|
|
console.error(e);
|
2020-04-24 19:38:21 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-05-21 18:22:30 +02:00
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
let c = await serverConnection.newUpStream();
|
2020-05-21 18:22:30 +02:00
|
|
|
c.kind = 'screenshare';
|
|
|
|
c.stream = stream;
|
|
|
|
stream.getTracks().forEach(t => {
|
|
|
|
let sender = c.pc.addTrack(t, stream);
|
|
|
|
t.onended = e => {
|
2020-08-11 17:09:31 +02:00
|
|
|
delUpMedia(c.id);
|
2020-05-21 18:22:30 +02:00
|
|
|
};
|
|
|
|
c.labels[t.id] = 'screenshare';
|
|
|
|
});
|
2020-08-11 17:09:31 +02:00
|
|
|
c.onstats = displayStats;
|
|
|
|
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-13 20:30:55 +02:00
|
|
|
delMedia(c.id);
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
function delUpMediaKind(kind) {
|
2020-08-11 17:09:31 +02:00
|
|
|
for(let id in serverConnection.up) {
|
|
|
|
let c = serverConnection.up[id];
|
2020-05-21 18:22:30 +02:00
|
|
|
if(c.kind != kind)
|
|
|
|
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
|
|
|
|
|
|
|
setButtonsVisibility()
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-08-11 17:09:31 +02:00
|
|
|
let media = 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;
|
|
|
|
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
|
|
|
|
|
|
|
resizePeers();
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function delMedia(id) {
|
|
|
|
let mediadiv = document.getElementById('peers');
|
|
|
|
let peer = document.getElementById('peer-' + id);
|
|
|
|
let media = document.getElementById('media-' + id);
|
|
|
|
|
|
|
|
media.srcObject = null;
|
|
|
|
mediadiv.removeChild(peer);
|
2020-05-01 01:22:17 +02:00
|
|
|
|
|
|
|
resizePeers();
|
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-05-01 01:22:17 +02:00
|
|
|
let columns = Math.ceil(Math.sqrt(count));
|
|
|
|
document.getElementById('peers').style['grid-template-columns'] =
|
|
|
|
`repeat(${columns}, 1fr)`;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
user.textContent = name ? name : '(anon)';
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @param {Object.<string,boolean>} perms
|
|
|
|
*/
|
|
|
|
function gotPermissions(perms) {
|
2020-04-25 18:09:31 +02:00
|
|
|
displayUsername();
|
2020-05-21 18:22:30 +02:00
|
|
|
setButtonsVisibility();
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
let lastMessage = {};
|
|
|
|
|
2020-08-12 12:17:56 +02:00
|
|
|
function addToChatbox(peerId, nick, kind, message){
|
2020-04-24 19:38:21 +02:00
|
|
|
let container = document.createElement('div');
|
|
|
|
container.classList.add('message');
|
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) {
|
|
|
|
let user = document.createElement('p');
|
|
|
|
user.textContent = nick;
|
|
|
|
user.classList.add('message-user');
|
|
|
|
container.appendChild(user);
|
|
|
|
}
|
|
|
|
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');
|
|
|
|
box.appendChild(container);
|
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-04-24 19:38:21 +02:00
|
|
|
function handleInput() {
|
|
|
|
let input = document.getElementById('input');
|
|
|
|
let data = input.value;
|
|
|
|
input.value = '';
|
|
|
|
|
|
|
|
let message, me;
|
|
|
|
|
|
|
|
if(data === '')
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(data.charAt(0) === '/') {
|
|
|
|
if(data.charAt(1) === '/') {
|
|
|
|
message = data.substring(1);
|
|
|
|
me = false;
|
|
|
|
} else {
|
|
|
|
let space, cmd, rest;
|
|
|
|
space = data.indexOf(' ');
|
|
|
|
if(space < 0) {
|
|
|
|
cmd = data;
|
|
|
|
rest = '';
|
|
|
|
} else {
|
|
|
|
cmd = data.slice(0, space);
|
|
|
|
rest = data.slice(space + 1).trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-08-11 17:09:31 +02:00
|
|
|
serverConnection.groupAction(cmd.slice(1));
|
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;
|
|
|
|
}
|
|
|
|
let id;
|
2020-08-11 17:09:31 +02:00
|
|
|
if(rest in users) {
|
2020-04-25 17:36:35 +02:00
|
|
|
id = rest;
|
|
|
|
} else {
|
|
|
|
for(let i in users) {
|
|
|
|
if(users[i] === rest) {
|
|
|
|
id = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!id) {
|
|
|
|
displayError('Unknown user ' + rest);
|
|
|
|
return;
|
|
|
|
}
|
2020-08-11 17:09:31 +02:00
|
|
|
serverConnection.userAction(cmd.slice(1), id)
|
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-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();
|
|
|
|
let chat = document.getElementById('chat');
|
|
|
|
let start_x = e.clientX;
|
|
|
|
let start_width = parseFloat(
|
|
|
|
document.defaultView.getComputedStyle(chat).width.replace('px', ''),
|
|
|
|
);
|
|
|
|
let inputbutton = document.getElementById('inputbutton');
|
|
|
|
function start_drag(e) {
|
|
|
|
let width = start_width + e.clientX - start_x;
|
|
|
|
if(width < 40)
|
|
|
|
inputbutton.style.display = 'none';
|
|
|
|
else
|
|
|
|
inputbutton.style.display = 'inline';
|
|
|
|
chat.style.width = width + 'px';
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
|
|
|
let errorTimeout = null;
|
|
|
|
|
|
|
|
function setErrorTimeout(ms) {
|
|
|
|
if(errorTimeout) {
|
|
|
|
clearTimeout(errorTimeout);
|
|
|
|
errorTimeout = null;
|
|
|
|
}
|
|
|
|
if(ms) {
|
|
|
|
errorTimeout = setTimeout(clearError, ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function displayError(message) {
|
|
|
|
let errspan = document.getElementById('errspan');
|
|
|
|
errspan.textContent = message;
|
|
|
|
errspan.classList.remove('noerror');
|
|
|
|
errspan.classList.add('error');
|
|
|
|
setErrorTimeout(8000);
|
|
|
|
}
|
|
|
|
|
|
|
|
function displayWarning(message) {
|
|
|
|
// don't overwrite real errors
|
|
|
|
if(!errorTimeout)
|
|
|
|
return displayError(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearError() {
|
|
|
|
let errspan = document.getElementById('errspan');
|
|
|
|
errspan.textContent = '';
|
|
|
|
errspan.classList.remove('error');
|
|
|
|
errspan.classList.add('noerror');
|
|
|
|
setErrorTimeout(null);
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
document.getElementById('userform').onsubmit = function(e) {
|
2020-04-24 19:38:21 +02:00
|
|
|
e.preventDefault();
|
|
|
|
let username = document.getElementById('username').value.trim();
|
|
|
|
let password = document.getElementById('password').value;
|
|
|
|
setUserPass(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-05-10 21:18:07 +02:00
|
|
|
};
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
function serverConnect() {
|
|
|
|
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}`);
|
|
|
|
}
|
|
|
|
return serverConnection.connect(`ws${location.protocol === 'https:' ? 's' : ''}://${location.host}/ws`);
|
|
|
|
}
|
|
|
|
|
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-06-09 18:05:16 +02:00
|
|
|
setLocalMute(localMute);
|
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
document.getElementById('connectbutton').disabled = false;
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2020-08-11 17:09:31 +02:00
|
|
|
let userpass = getUserPass();
|
|
|
|
if(userpass)
|
|
|
|
serverConnect();
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
start();
|