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) {
|
2020-10-22 16:21:42 +00:00
|
|
|
// Viewer element
|
|
|
|
const viewer = document.getElementById("viewer");
|
|
|
|
|
2020-10-20 19:59:07 +00:00
|
|
|
// Default quality
|
|
|
|
let quality = "source";
|
2020-10-20 19:45:26 +00:00
|
|
|
|
2020-10-22 16:21:42 +00:00
|
|
|
// Create WebSocket and WebRTC
|
|
|
|
const websocket = new GsWebSocket();
|
|
|
|
const webrtc = new GsWebRTC(
|
2020-10-21 20:10:39 +00:00
|
|
|
stunServers,
|
2020-10-22 16:21:42 +00:00
|
|
|
viewer,
|
2020-10-21 20:10:39 +00:00
|
|
|
document.getElementById("connectionIndicator"),
|
|
|
|
);
|
2020-10-22 16:21:42 +00:00
|
|
|
webrtc.createOffer();
|
|
|
|
webrtc.onICECandidate(localDescription => {
|
|
|
|
websocket.sendLocalDescription(localDescription, stream, quality);
|
2020-10-21 20:10:39 +00:00
|
|
|
});
|
2020-10-22 16:21:42 +00:00
|
|
|
websocket.onRemoteDescription(sdp => {
|
|
|
|
webrtc.setRemoteDescription(sdp);
|
2020-10-21 20:10:39 +00:00
|
|
|
});
|
2020-10-20 19:59:07 +00:00
|
|
|
|
|
|
|
// Register keyboard events
|
|
|
|
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-21 20:38:36 +00:00
|
|
|
viewerCounter.refreshViewersCounter();
|
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-22 16:41:14 +00:00
|
|
|
// Restart WebRTC negociation
|
|
|
|
webrtc.createOffer();
|
2020-10-20 19:59:07 +00:00
|
|
|
});
|
|
|
|
}
|