2022-02-14 16:34:56 +00:00
|
|
|
const loadVolume = () => {
|
|
|
|
try {
|
|
|
|
const savedVolume = localStorage.getItem("watch-party-volume");
|
|
|
|
if (savedVolume != null && savedVolume != "") {
|
|
|
|
return +savedVolume;
|
|
|
|
}
|
|
|
|
} catch (_err) {
|
|
|
|
// Sometimes localStorage is blocked from use
|
|
|
|
}
|
|
|
|
// default
|
|
|
|
return 0.5;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} volume
|
|
|
|
*/
|
|
|
|
const saveVolume = (volume) => {
|
|
|
|
try {
|
|
|
|
localStorage.setItem("watch-party-volume", volume);
|
|
|
|
} catch (_err) {
|
|
|
|
// see loadVolume
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-14 17:05:20 +00:00
|
|
|
const loadCaptionTrack = () => {
|
|
|
|
try {
|
|
|
|
const savedTrack = localStorage.getItem("watch-party-captions");
|
|
|
|
if (savedTrack != null && savedTrack != "") {
|
|
|
|
return +savedTrack;
|
|
|
|
}
|
|
|
|
} catch (_err) {
|
|
|
|
// Sometimes localStorage is blocked from use
|
|
|
|
}
|
|
|
|
// default
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} track
|
|
|
|
*/
|
|
|
|
const saveCaptionsTrack = (track) => {
|
|
|
|
try {
|
|
|
|
localStorage.setItem("watch-party-captions", track);
|
|
|
|
} catch (_err) {
|
|
|
|
// see loadCaptionsTrack
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-09 13:21:14 +00:00
|
|
|
/**
|
|
|
|
* @param {string} videoUrl
|
|
|
|
* @param {{name: string, url: string}[]} subtitles
|
|
|
|
*/
|
|
|
|
const createVideoElement = (videoUrl, subtitles) => {
|
2022-03-30 13:02:08 +00:00
|
|
|
const oldVideo = document.getElementById("video");
|
|
|
|
if (oldVideo) {
|
|
|
|
oldVideo.remove();
|
|
|
|
}
|
2021-11-09 13:21:14 +00:00
|
|
|
const video = document.createElement("video");
|
2022-03-30 13:02:08 +00:00
|
|
|
video.id = "video";
|
2021-11-09 13:21:14 +00:00
|
|
|
video.controls = true;
|
|
|
|
video.autoplay = false;
|
2022-02-14 16:34:56 +00:00
|
|
|
video.volume = loadVolume();
|
2021-11-09 13:21:14 +00:00
|
|
|
video.crossOrigin = "anonymous";
|
|
|
|
|
2022-02-14 16:34:56 +00:00
|
|
|
video.addEventListener("volumechange", async () => {
|
|
|
|
saveVolume(video.volume);
|
|
|
|
});
|
|
|
|
|
2021-11-09 13:21:14 +00:00
|
|
|
const source = document.createElement("source");
|
|
|
|
source.src = videoUrl;
|
|
|
|
|
|
|
|
video.appendChild(source);
|
|
|
|
|
2022-02-14 17:05:20 +00:00
|
|
|
const storedTrack = loadCaptionTrack();
|
|
|
|
let id = 0;
|
2021-11-09 13:21:14 +00:00
|
|
|
for (const { name, url } of subtitles) {
|
|
|
|
const track = document.createElement("track");
|
|
|
|
track.label = name;
|
|
|
|
track.src = url;
|
|
|
|
track.kind = "captions";
|
|
|
|
|
2022-02-14 17:05:20 +00:00
|
|
|
if (id == storedTrack) {
|
2021-11-09 13:21:14 +00:00
|
|
|
track.default = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
video.appendChild(track);
|
2022-02-14 17:05:20 +00:00
|
|
|
id++;
|
2021-11-09 13:21:14 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 17:05:20 +00:00
|
|
|
video.textTracks.addEventListener("change", async () => {
|
|
|
|
let id = 0;
|
|
|
|
for (const track of video.textTracks) {
|
|
|
|
if (track.mode != "disabled") {
|
|
|
|
saveCaptionsTrack(id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
id++;
|
|
|
|
}
|
|
|
|
saveCaptionsTrack(-1);
|
|
|
|
});
|
|
|
|
|
2021-11-23 01:17:23 +00:00
|
|
|
// watch for attribute changes on the video object to detect hiding/showing of controls
|
|
|
|
// as far as i can tell this is the least hacky solutions to get control visibility change events
|
|
|
|
const observer = new MutationObserver(async (mutations) => {
|
|
|
|
for (const mutation of mutations) {
|
|
|
|
if (mutation.attributeName == "controls") {
|
|
|
|
if (video.controls) {
|
|
|
|
// enable media button support
|
|
|
|
navigator.mediaSession.setActionHandler("play", null);
|
|
|
|
navigator.mediaSession.setActionHandler("pause", null);
|
|
|
|
navigator.mediaSession.setActionHandler("stop", null);
|
|
|
|
navigator.mediaSession.setActionHandler("seekbackward", null);
|
|
|
|
navigator.mediaSession.setActionHandler("seekforward", null);
|
|
|
|
navigator.mediaSession.setActionHandler("seekto", null);
|
|
|
|
navigator.mediaSession.setActionHandler("previoustrack", null);
|
|
|
|
navigator.mediaSession.setActionHandler("nexttrack", null);
|
|
|
|
} else {
|
|
|
|
// disable media button support by ignoring the events
|
2022-02-14 16:34:56 +00:00
|
|
|
navigator.mediaSession.setActionHandler("play", () => {});
|
|
|
|
navigator.mediaSession.setActionHandler("pause", () => {});
|
|
|
|
navigator.mediaSession.setActionHandler("stop", () => {});
|
|
|
|
navigator.mediaSession.setActionHandler("seekbackward", () => {});
|
|
|
|
navigator.mediaSession.setActionHandler("seekforward", () => {});
|
|
|
|
navigator.mediaSession.setActionHandler("seekto", () => {});
|
|
|
|
navigator.mediaSession.setActionHandler("previoustrack", () => {});
|
|
|
|
navigator.mediaSession.setActionHandler("nexttrack", () => {});
|
2021-11-23 01:17:23 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
observer.observe(video, { attributes: true });
|
|
|
|
|
2021-11-09 13:21:14 +00:00
|
|
|
return video;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} videoUrl
|
|
|
|
* @param {{name: string, url: string}[]} subtitles
|
|
|
|
* @param {number} currentTime
|
|
|
|
* @param {boolean} playing
|
|
|
|
*/
|
|
|
|
export const setupVideo = async (videoUrl, subtitles, currentTime, playing) => {
|
|
|
|
document.querySelector("#pre-join-controls").style["display"] = "none";
|
|
|
|
const video = createVideoElement(videoUrl, subtitles);
|
2022-02-14 20:58:59 +00:00
|
|
|
const videoContainer = document.querySelector("#video-container");
|
|
|
|
videoContainer.style.display = "block";
|
|
|
|
videoContainer.appendChild(video);
|
2021-11-09 13:21:14 +00:00
|
|
|
|
|
|
|
video.currentTime = currentTime / 1000.0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (playing) {
|
|
|
|
await video.play();
|
|
|
|
} else {
|
|
|
|
video.pause();
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// Auto-play is probably disabled, we should uhhhhhhh do something about it
|
|
|
|
}
|
|
|
|
|
|
|
|
return video;
|
|
|
|
};
|