stream-api/frontend/script.js

68 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-05-14 10:54:53 +00:00
(() => {
2021-05-20 16:33:51 +00:00
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;
2021-05-20 15:02:45 +00:00
}
2021-05-20 16:33:51 +00:00
const upstream = findUpstream();
2021-05-20 15:02:45 +00:00
const xhr = new XMLHttpRequest();
2021-05-14 10:54:53 +00:00
xhr.open("HEAD", upstream, true);
xhr.send();
let count = 2;
2021-05-20 15:02:45 +00:00
const handleCount = () => {
2021-05-14 10:54:53 +00:00
if(--count === 0) {
handle();
}
}
2021-05-20 15:02:45 +00:00
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 = {};
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");
}
const button = new Button(player, cbutton);
button.addClass("fab");
button.addClass("fa-chromecast");
button.addClass("vjs-control");
button.addClass("vjs-button");
button.addClass("vjs-big-chromecast");
player.addChild(button);
player.one("play", () => {
player.removeChild(button);
});
2021-05-20 16:33:51 +00:00
player.on("chromecastConnected", () => {
2021-05-20 16:42:50 +00:00
if(player.volume() > 0.3) {
2021-05-20 16:49:19 +00:00
player.volume(0.2);
2021-05-20 16:42:50 +00:00
}
2021-05-20 16:33:51 +00:00
});
2021-05-12 07:50:51 +00:00
}
2021-05-20 16:33:51 +00:00
document.addEventListener("DOMContentLoaded", handleCount);
2021-05-14 10:54:53 +00:00
xhr.addEventListener("load", handleCount);
})();