ghostream/web/static/js/ovenplayer.js

107 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-11-09 15:15:23 +00:00
import { ViewerCounter } from "./modules/viewerCounter.js";
/**
* Initialize viewer page
*
* @param {String} stream
2020-11-09 16:18:37 +00:00
* @param {String} omeApp
2020-11-09 15:15:23 +00:00
* @param {Number} viewersCounterRefreshPeriod
* @param {String} posterUrl
*/
2020-11-09 16:18:37 +00:00
export function initViewerPage(stream, omeApp, viewersCounterRefreshPeriod, posterUrl) {
2020-11-09 15:15:23 +00:00
// Create viewer counter
const viewerCounter = new ViewerCounter(
document.getElementById("connected-people"),
stream,
);
viewerCounter.regularUpdate(viewersCounterRefreshPeriod);
viewerCounter.refreshViewersCounter();
// 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 player
let player = OvenPlayer.create("viewer", {
title: stream,
image: posterUrl,
autoStart: true,
mute: true,
expandFullScreenUI: true,
sources: [
{
2020-11-09 16:18:37 +00:00
"file": "wss://" + window.location.host + "/" + omeApp + "/" + stream,
2020-11-09 15:15:23 +00:00
"type": "webrtc",
"label": " WebRTC - Source"
},
{
"type": "hls",
2020-11-09 16:18:37 +00:00
"file": "https://" + window.location.host + "/" + omeApp + "/" + stream + "_bypass/playlist.m3u8",
2020-11-09 15:15:23 +00:00
"label": " HLS"
}
]
});
2020-11-09 15:48:05 +00:00
player.on("stateChanged", function (data) {
if (data.newstate === "loading") {
2020-11-09 15:23:13 +00:00
document.getElementById("connectionIndicator").style.fill = '#ffc107'
}
2020-11-09 15:48:05 +00:00
if (data.newstate === "playing") {
2020-11-09 15:23:13 +00:00
document.getElementById("connectionIndicator").style.fill = '#28a745'
}
2020-11-09 15:48:05 +00:00
if (data.newstate === "idle") {
document.getElementById("connectionIndicator").style.fill = '#dc3545'
}
2020-11-09 15:23:13 +00:00
})
2020-11-09 15:15:23 +00:00
player.on("error", function (error) {
2020-11-09 15:23:13 +00:00
document.getElementById("connectionIndicator").style.fill = '#dc3545'
2020-11-09 15:15:23 +00:00
if (error.code === 501 || error.code === 406) {
// Clear messages
const errorMsg = document.getElementsByClassName("op-message-text")[0]
2020-11-09 15:48:05 +00:00
errorMsg.textContent = ""
2020-11-09 15:15:23 +00:00
const warningIcon = document.getElementsByClassName("op-message-icon")[0]
2020-11-09 15:48:05 +00:00
warningIcon.textContent = ""
2020-11-09 15:15:23 +00:00
// Reload in 30s
setTimeout(function () {
player.load()
}, 30000)
} else {
console.log(error);
}
});
2020-11-09 15:24:39 +00:00
// Register keyboard events
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "f":
// F key put player in fullscreen
if (document.fullscreenElement !== null) {
2020-11-09 15:53:35 +00:00
document.exitFullscreen()
2020-11-09 15:24:39 +00:00
} else {
2020-11-09 15:53:35 +00:00
document.getElementsByTagName("video")[0].requestFullscreen()
2020-11-09 15:24:39 +00:00
}
break;
case "m":
case " ":
// M and space key mute player
2020-11-09 15:53:35 +00:00
player.setMute(!player.getMute())
event.preventDefault()
player.play()
2020-11-09 15:24:39 +00:00
break;
}
});
2020-11-09 15:15:23 +00:00
}