stream-api/frontend/script.js
2021-05-21 14:19:12 +02:00

88 lines
2.9 KiB
JavaScript

(() => {
const findUpstream = () => {
const search = new URLSearchParams(location.search);
search.set("render", "false");
const url = new URL(location.origin);
url.pathname = location.pathname;
url.search = search.toString();
return url.href;
}
const upstream = findUpstream();
const xhr = new XMLHttpRequest();
xhr.open("HEAD", upstream, true);
xhr.send();
let count = 2;
const handleCount = () => {
if(--count === 0) {
handle();
}
}
const handle = () => {
const [body] = document.getElementsByTagName("body");
const video = document.createElement("video");
video.className = "video-js vjs-big-play-centered";
body.appendChild(video);
const ctype = xhr.getResponseHeader("Content-Type");
console.log(ctype);
const options = {};
options.controls = true;
options.liveui = true;
options.responsive = true;
options.fill = true;
options.techOrder = ["chromecast", "html5"];
options.plugins = {};
options.plugins.chromecast = {};
options.plugins.chromecast.addButtonToControlBar = false;
const player = videojs(video, options);
const source = {};
source.type = ctype;
source.src = upstream;
player.src(source);
const Button = videojs.getComponent("Button");
const cbutton = {};
cbutton.clickHandler = (ev) => {
player.trigger("chromecastRequested");
}
cbutton.text = "Chromecast";
const bigCastButton = new Button(player, cbutton);
bigCastButton.addClass("fab");
bigCastButton.addClass("fa-chromecast");
bigCastButton.addClass("vjs-control");
bigCastButton.addClass("vjs-button");
bigCastButton.addClass("vjs-big-chromecast");
player.addChild(bigCastButton);
player.on("play", () => {
bigCastButton.hide();
});
player.on("pause", () => {
bigCastButton.show();
});
const smallCastButton = new Button(player, cbutton);
smallCastButton.addClass("fab");
smallCastButton.addClass("fa-chromecast");
smallCastButton.addClass("vjs-control");
smallCastButton.addClass("vjs-button");
player.on("chromecastConnected", () => {
console.log(player.chromecastSessionManager.remotePlayer.volumeLevel);
if(player.volume() > 0.3) {
player.volume(0.2);
}
});
player.on("chromecastDevicesAvailable", () => {
player.controlBar.addChild(smallCastButton);
});
player.on("chromecastDevicesUnvailable", () => {
player.controlBar.removeChild(smallCastButton);
});
}
document.addEventListener("DOMContentLoaded", handleCount);
xhr.addEventListener("load", handleCount);
})();