stream-api/frontend/script.js

102 lines
3.7 KiB
JavaScript
Raw Normal View History

2021-05-14 10:54:53 +00:00
(() => {
2021-05-31 10:59:25 +00:00
const info = {% raw info %};
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 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);
2022-02-13 13:15:33 +00:00
if(info.poster) {
2021-05-31 10:59:25 +00:00
player.poster(info.poster);
2021-05-25 07:54:54 +00:00
}
2021-05-20 15:02:45 +00:00
const source = {};
2021-05-31 10:59:25 +00:00
source.type = info.ctype;
source.src = info.upstream;
2021-05-20 15:02:45 +00:00
player.src(source);
2022-02-05 22:39:23 +00:00
2021-05-31 10:59:25 +00:00
const canPlayTypeRaw = player.canPlayType(info.ctype);
const canPlayType = (canPlayTypeRaw === "maybe") || (canPlayTypeRaw === "probably");
if(canPlayType) {
const Button = videojs.getComponent("Button");
2023-07-07 18:54:20 +00:00
class DownloadButton extends videojs.getComponent("Component") {
2022-03-13 11:04:16 +00:00
createEl: () => {
const elem = document.createElement("a");
elem.href = info.download;
elem.classList.add("vjs-control");
elem.classList.add("vjs-button");
elem.classList.add("fas");
elem.classList.add("fa-download");
elem.classList.add("vjs-big-download");
return elem;
}
2023-07-07 18:54:20 +00:00
}
2022-03-13 08:53:25 +00:00
const cbutton = {};
cbutton.clickHandler = (ev) => {
player.trigger("chromecastRequested");
}
2022-02-05 22:39:23 +00:00
2021-06-10 10:23:55 +00:00
const bigCastButtonWrapper = new Button(player, cbutton);
2022-03-13 11:04:16 +00:00
const bigDownloadButtonWrapper = new DownloadButton(player);
2021-06-10 10:23:55 +00:00
const bigCastButton = bigCastButtonWrapper.el();
2022-02-05 22:39:23 +00:00
const bigDownloadButton = bigDownloadButtonWrapper.el();
2021-06-10 10:23:55 +00:00
bigCastButton.classList.add("vjs-control");
bigCastButton.classList.add("vjs-button");
bigCastButton.classList.add("fab");
bigCastButton.classList.add("fa-chromecast");
bigCastButton.classList.add("vjs-big-chromecast");
player.addChild(bigCastButtonWrapper);
2022-03-13 11:04:16 +00:00
2022-02-13 13:15:33 +00:00
if(info.download) {
2022-02-05 22:39:23 +00:00
player.addChild(bigDownloadButtonWrapper);
}
player.one("error", () => {
2021-06-10 10:23:55 +00:00
player.removeChild(bigCastButtonWrapper);
2022-02-13 13:15:33 +00:00
if(info.download) {
2022-02-05 22:39:23 +00:00
player.removeChild(bigDownloadButtonWrapper);
}
});
2021-05-21 12:19:12 +00:00
player.one("play", () => {
2021-06-10 10:23:55 +00:00
player.removeChild(bigCastButtonWrapper);
2022-02-13 14:47:15 +00:00
if(info.download) {
2022-02-05 22:39:23 +00:00
player.removeChild(bigDownloadButtonWrapper);
}
});
2021-05-25 06:23:25 +00:00
2021-06-10 10:23:55 +00:00
const smallCastButtonWrapper = new Button(player, cbutton);
const smallCastButton = smallCastButtonWrapper.el();
smallCastButton.classList.add("vjs-control");
smallCastButton.classList.add("vjs-button");
for(const elem of smallCastButton.children) {
if(elem.classList.contains("vjs-icon-placeholder")) {
elem.classList.add("fab");
elem.classList.add("fa-chromecast");
}
}
player.controlBar.addChild(smallCastButtonWrapper);
player.on("chromecastConnected", () => {
if(player.volume() > 0.3) {
player.volume(0.2);
}
});
}
2021-05-12 07:50:51 +00:00
}
2021-05-31 10:59:25 +00:00
document.addEventListener("DOMContentLoaded", handle);
2021-05-14 10:54:53 +00:00
})();