1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2024-12-22 13:52:19 +00:00

Working javascript modules

This commit is contained in:
Alexandre Iooss 2020-10-20 21:59:07 +02:00
parent c88f473ec0
commit 0b3fb87fa2
No known key found for this signature in database
GPG Key ID: 6C79278F3FCDCC02
2 changed files with 73 additions and 64 deletions

View File

@ -1,65 +1,75 @@
import { GSWebSocket } from "./modules/websocket.js"; import { GsWebSocket } from "./modules/websocket.js";
import { ViewerCounter } from "./modules/viewerCounter.js"; import { ViewerCounter } from "./modules/viewerCounter.js";
// Some variables that need to be fixed by web page /**
const ViewersCounterRefreshPeriod = 1000; * Initialize viewer page
const stream = "demo"; *
let quality = "source"; * @param {String} stream
* @param {List} stunServers
* @param {Number} viewersCounterRefreshPeriod
*/
export function initViewerPage(stream, stunServers, viewersCounterRefreshPeriod) {
// Default quality
let quality = "source";
// Create WebSocket // Create WebSocket
const s = new GSWebSocket(); const s = new GsWebSocket();
s.open(); s.open();
// Register keyboard events // Create WebRTC
const viewer = document.getElementById("viewer"); // FIXME startPeerConnection() with stunServers
window.addEventListener("keydown", (event) => {
switch (event.key) { // Register keyboard events
case "f": const viewer = document.getElementById("viewer");
// F key put player in fullscreen window.addEventListener("keydown", (event) => {
if (document.fullscreenElement !== null) { switch (event.key) {
document.exitFullscreen(); case "f":
} else { // F key put player in fullscreen
viewer.requestFullscreen(); if (document.fullscreenElement !== null) {
document.exitFullscreen();
} else {
viewer.requestFullscreen();
}
break;
case "m":
case " ":
// M and space key mute player
viewer.muted = !viewer.muted;
event.preventDefault();
viewer.play();
break;
} }
break; });
case "m":
case " ": // Create viewer counter
// M and space key mute player const viewerCounter = new ViewerCounter(
viewer.muted = !viewer.muted; document.getElementById("connected-people"),
event.preventDefault(); stream,
viewer.play(); );
break; viewerCounter.regularUpdate(viewersCounterRefreshPeriod);
// Side widget toggler
const sideWidgetToggle = document.getElementById("sideWidgetToggle");
const sideWidget = document.getElementById("sideWidget");
if (sideWidgetToggle !== null && sideWidget !== null) {
// On click, toggle side widget visibility
sideWidgetToggle.addEventListener("click", function () {
if (sideWidget.style.display === "none") {
sideWidget.style.display = "block";
sideWidgetToggle.textContent = "»";
} else {
sideWidget.style.display = "none";
sideWidgetToggle.textContent = "«";
}
});
} }
});
// Create viewer counter // Video quality toggler
const viewerCounter = new ViewerCounter( document.getElementById("quality").addEventListener("change", (event) => {
document.getElementById("connected-people"), quality = event.target.value;
stream, console.log(`Stream quality changed to ${quality}`);
);
viewerCounter.regularUpdate(ViewersCounterRefreshPeriod);
// Side widget toggler // Restart the connection with a new quality
const sideWidgetToggle = document.getElementById("sideWidgetToggle"); // FIXME
const sideWidget = document.getElementById("sideWidget"); });
if (sideWidgetToggle !== null && sideWidget !== null) {
// On click, toggle side widget visibility
sideWidgetToggle.addEventListener("click", function () {
if (sideWidget.style.display === "none") {
sideWidget.style.display = "block";
sideWidgetToggle.textContent = "»";
} else {
sideWidget.style.display = "none";
sideWidgetToggle.textContent = "«";
}
});
} }
// Video quality toggler
document.getElementById("quality").addEventListener("change", (event) => {
quality = event.target.value;
console.log(`Stream quality changed to ${quality}`);
// Restart the connection with a new quality
// FIXME
});

View File

@ -34,18 +34,17 @@
{{end}} {{end}}
</div> </div>
<script type="module" src="/static/js/main.js"></script> <script type="module">
<script> import { initViewerPage } from "/static/js/main.js";
// Some variables that need to be fixed by web page
const viewersCounterRefreshPeriod = Number("{{.Cfg.ViewersCounterRefreshPeriod}}");
const stream = "{{.Path}}";
const stunServers = [ const stunServers = [
{{range $id, $value := .Cfg.STUNServers}} {{range $id, $value := .Cfg.STUNServers}}
"{{$value}}", "{{$value}}",
{{end}} {{end}}
] ]
startPeerConnection() initViewerPage(stream, stunServers, viewersCounterRefreshPeriod)
// Wait a bit before pulling viewers counter for the first time
setTimeout(() => {
refreshViewersCounter("{{.Path}}", {{.Cfg.ViewersCounterRefreshPeriod}})
}, 1000)
</script> </script>
{{end}} {{end}}