stream-api/frontend/script.js

90 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

2021-05-14 10:54:53 +00:00
(() => {
2021-05-25 07:39:34 +00:00
const findUpstreamVideo = () => {
2021-05-20 16:33:51 +00:00
const search = new URLSearchParams(location.search);
search.set("render", "false");
2021-05-25 07:39:34 +00:00
const url = new URL(location.origin);
url.pathname = location.pathname;
url.search = search.toString();
return url.href;
}
2021-05-25 07:54:54 +00:00
2021-05-25 07:39:34 +00:00
const upstream = findUpstreamVideo();
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");
2021-05-25 08:22:22 +00:00
const image = xhr.getResponseHeader("Custom-Poster");
2021-05-20 15:02:45 +00:00
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 = {};
2021-05-21 08:58:13 +00:00
options.plugins.chromecast.addButtonToControlBar = false;
2021-05-20 15:02:45 +00:00
const player = videojs(video, options);
2021-05-25 08:22:22 +00:00
if((image instanceof String) || ((typeof image) == "string")) {
player.poster(image);
2021-05-25 07:54:54 +00:00
}
2021-05-20 15:02:45 +00:00
const source = {};
source.type = ctype;
source.src = upstream;
player.src(source);
const canPlayTypeRaw = player.canPlayType(ctype);
const canPlayType = (canPlayTypeRaw === "maybe") || (canPlayTypeRaw === "probably");
if(canPlayType) {
const Button = videojs.getComponent("Button");
const cbutton = {};
cbutton.clickHandler = (ev) => {
player.trigger("chromecastRequested");
}
cbutton.text = "Chromecast";
2021-05-23 20:02:33 +00:00
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");
2021-05-23 20:02:33 +00:00
player.addChild(bigCastButton);
player.one("error", () => {
player.removeChild(bigCastButton);
});
2021-05-21 12:19:12 +00:00
player.one("play", () => {
player.removeChild(bigCastButton);
});
2021-05-25 06:23:25 +00:00
const smallCastButton = new Button(player, cbutton);
smallCastButton.addClass("fab");
smallCastButton.addClass("fa-chromecast");
smallCastButton.addClass("vjs-control");
smallCastButton.addClass("vjs-button");
player.controlBar.addChild(smallCastButton);
player.on("chromecastConnected", () => {
if(player.volume() > 0.3) {
player.volume(0.2);
}
});
}
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);
})();