2020-10-20 19:59:07 +00:00
|
|
|
import { GsWebSocket } from "./modules/websocket.js";
|
2020-10-20 19:29:41 +00:00
|
|
|
import { ViewerCounter } from "./modules/viewerCounter.js";
|
2020-10-21 20:10:39 +00:00
|
|
|
import { GsWebRTC } from "./modules/webrtc.js";
|
2020-10-20 19:29:41 +00:00
|
|
|
|
2020-10-20 19:59:07 +00:00
|
|
|
/**
|
|
|
|
* Initialize viewer page
|
|
|
|
*
|
|
|
|
* @param {String} stream
|
|
|
|
* @param {List} stunServers
|
|
|
|
* @param {Number} viewersCounterRefreshPeriod
|
|
|
|
*/
|
|
|
|
export function initViewerPage(stream, stunServers, viewersCounterRefreshPeriod) {
|
|
|
|
// Default quality
|
|
|
|
let quality = "source";
|
2020-10-20 19:45:26 +00:00
|
|
|
|
2020-10-20 19:59:07 +00:00
|
|
|
// Create WebSocket
|
|
|
|
const s = new GsWebSocket();
|
|
|
|
s.open();
|
2020-10-20 19:45:26 +00:00
|
|
|
|
2020-10-20 19:59:07 +00:00
|
|
|
// Create WebRTC
|
2020-10-21 20:10:39 +00:00
|
|
|
const c = new GsWebRTC(
|
|
|
|
stunServers,
|
|
|
|
document.getElementById("connectionIndicator"),
|
|
|
|
);
|
|
|
|
c.createOffer();
|
|
|
|
c.onICECandidate(localDescription => {
|
|
|
|
s.sendDescription(localDescription, stream, quality);
|
|
|
|
});
|
|
|
|
s.onDescription(data => {
|
|
|
|
c.setRemoteDescription(data);
|
|
|
|
});
|
2020-10-20 19:59:07 +00:00
|
|
|
|
|
|
|
// Register keyboard events
|
|
|
|
const viewer = document.getElementById("viewer");
|
|
|
|
window.addEventListener("keydown", (event) => {
|
|
|
|
switch (event.key) {
|
|
|
|
case "f":
|
|
|
|
// F key put player in fullscreen
|
|
|
|
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;
|
2020-10-20 19:45:26 +00:00
|
|
|
}
|
2020-10-20 19:59:07 +00:00
|
|
|
});
|
2020-10-20 19:29:41 +00:00
|
|
|
|
2020-10-20 19:59:07 +00:00
|
|
|
// Create viewer counter
|
|
|
|
const viewerCounter = new ViewerCounter(
|
|
|
|
document.getElementById("connected-people"),
|
|
|
|
stream,
|
|
|
|
);
|
|
|
|
viewerCounter.regularUpdate(viewersCounterRefreshPeriod);
|
2020-10-20 19:45:26 +00:00
|
|
|
|
2020-10-20 19:59:07 +00:00
|
|
|
// 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 = "«";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-10-20 19:45:26 +00:00
|
|
|
|
2020-10-20 19:59:07 +00:00
|
|
|
// Video quality toggler
|
|
|
|
document.getElementById("quality").addEventListener("change", (event) => {
|
|
|
|
quality = event.target.value;
|
|
|
|
console.log(`Stream quality changed to ${quality}`);
|
2020-10-20 19:45:26 +00:00
|
|
|
|
2020-10-20 19:59:07 +00:00
|
|
|
// Restart the connection with a new quality
|
|
|
|
// FIXME
|
|
|
|
});
|
|
|
|
}
|