2023-10-01 17:34:37 +02:00
|
|
|
import fscreen from "fscreen";
|
|
|
|
|
2023-09-30 20:57:00 +02:00
|
|
|
import {
|
|
|
|
DisplayInterface,
|
|
|
|
DisplayInterfaceEvents,
|
|
|
|
} from "@/components/player/display/displayInterface";
|
|
|
|
import { Source } from "@/components/player/hooks/usePlayer";
|
2023-10-01 21:08:26 +02:00
|
|
|
import { handleBuffered } from "@/components/player/utils/handleBuffered";
|
2023-10-01 17:34:37 +02:00
|
|
|
import {
|
2023-10-01 21:08:26 +02:00
|
|
|
canChangeVolume,
|
2023-10-01 17:34:37 +02:00
|
|
|
canFullscreen,
|
|
|
|
canFullscreenAnyElement,
|
|
|
|
canWebkitFullscreen,
|
|
|
|
} from "@/utils/detectFeatures";
|
2023-09-30 20:57:00 +02:00
|
|
|
import { makeEmitter } from "@/utils/events";
|
|
|
|
|
|
|
|
export function makeVideoElementDisplayInterface(): DisplayInterface {
|
|
|
|
const { emit, on, off } = makeEmitter<DisplayInterfaceEvents>();
|
|
|
|
let source: Source | null = null;
|
|
|
|
let videoElement: HTMLVideoElement | null = null;
|
2023-10-01 17:34:37 +02:00
|
|
|
let containerElement: HTMLElement | null = null;
|
|
|
|
let isFullscreen = false;
|
2023-10-01 21:08:26 +02:00
|
|
|
let isPausedBeforeSeeking = false;
|
2023-10-02 21:04:40 +02:00
|
|
|
let isSeeking = false;
|
2023-09-30 20:57:00 +02:00
|
|
|
|
|
|
|
function setSource() {
|
|
|
|
if (!videoElement || !source) return;
|
|
|
|
videoElement.src = source.url;
|
|
|
|
videoElement.addEventListener("play", () => emit("play", undefined));
|
|
|
|
videoElement.addEventListener("pause", () => emit("pause", undefined));
|
2023-10-01 21:08:26 +02:00
|
|
|
videoElement.addEventListener("volumechange", () =>
|
|
|
|
emit("volumechange", videoElement?.volume ?? 0)
|
|
|
|
);
|
|
|
|
videoElement.addEventListener("timeupdate", () =>
|
|
|
|
emit("time", videoElement?.currentTime ?? 0)
|
|
|
|
);
|
|
|
|
videoElement.addEventListener("loadedmetadata", () => {
|
|
|
|
emit("duration", videoElement?.duration ?? 0);
|
|
|
|
});
|
|
|
|
videoElement.addEventListener("progress", () => {
|
|
|
|
if (videoElement)
|
|
|
|
emit(
|
|
|
|
"buffered",
|
|
|
|
handleBuffered(videoElement.currentTime, videoElement.buffered)
|
|
|
|
);
|
|
|
|
});
|
2023-09-30 20:57:00 +02:00
|
|
|
}
|
|
|
|
|
2023-10-01 17:34:37 +02:00
|
|
|
function fullscreenChange() {
|
|
|
|
isFullscreen =
|
|
|
|
!!document.fullscreenElement || // other browsers
|
|
|
|
!!(document as any).webkitFullscreenElement; // safari
|
|
|
|
}
|
|
|
|
fscreen.addEventListener("fullscreenchange", fullscreenChange);
|
|
|
|
|
2023-09-30 20:57:00 +02:00
|
|
|
return {
|
|
|
|
on,
|
|
|
|
off,
|
2023-10-01 17:34:37 +02:00
|
|
|
destroy: () => {
|
|
|
|
fscreen.removeEventListener("fullscreenchange", fullscreenChange);
|
|
|
|
},
|
2023-09-30 20:57:00 +02:00
|
|
|
load(newSource) {
|
|
|
|
source = newSource;
|
|
|
|
setSource();
|
|
|
|
},
|
|
|
|
|
|
|
|
processVideoElement(video) {
|
|
|
|
videoElement = video;
|
|
|
|
setSource();
|
|
|
|
},
|
2023-10-01 17:34:37 +02:00
|
|
|
processContainerElement(container) {
|
|
|
|
containerElement = container;
|
|
|
|
},
|
2023-09-30 20:57:00 +02:00
|
|
|
|
|
|
|
pause() {
|
|
|
|
videoElement?.pause();
|
|
|
|
},
|
|
|
|
play() {
|
|
|
|
videoElement?.play();
|
|
|
|
},
|
2023-10-01 21:08:26 +02:00
|
|
|
setSeeking(active) {
|
2023-10-02 21:04:40 +02:00
|
|
|
if (active === isSeeking) return;
|
|
|
|
isSeeking = active;
|
|
|
|
|
2023-10-01 21:08:26 +02:00
|
|
|
// if it was playing when starting to seek, play again
|
|
|
|
if (!active) {
|
|
|
|
if (!isPausedBeforeSeeking) this.play();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isPausedBeforeSeeking = videoElement?.paused ?? true;
|
|
|
|
this.pause();
|
|
|
|
},
|
|
|
|
setTime(t) {
|
|
|
|
if (!videoElement) return;
|
|
|
|
// clamp time between 0 and max duration
|
|
|
|
let time = Math.min(t, videoElement.duration);
|
|
|
|
time = Math.max(0, time);
|
|
|
|
|
|
|
|
if (Number.isNaN(time)) return;
|
|
|
|
emit("time", time);
|
|
|
|
videoElement.currentTime = time;
|
|
|
|
},
|
|
|
|
async setVolume(v) {
|
|
|
|
if (!videoElement) return;
|
|
|
|
|
|
|
|
// clamp time between 0 and 1
|
|
|
|
let volume = Math.min(v, 1);
|
|
|
|
volume = Math.max(0, volume);
|
|
|
|
|
|
|
|
// update state
|
|
|
|
if (await canChangeVolume()) videoElement.volume = volume;
|
|
|
|
},
|
2023-10-01 17:34:37 +02:00
|
|
|
toggleFullscreen() {
|
|
|
|
if (isFullscreen) {
|
|
|
|
isFullscreen = false;
|
|
|
|
emit("fullscreen", isFullscreen);
|
|
|
|
if (!fscreen.fullscreenElement) return;
|
|
|
|
fscreen.exitFullscreen();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// enter fullscreen
|
|
|
|
isFullscreen = true;
|
|
|
|
emit("fullscreen", isFullscreen);
|
|
|
|
if (!canFullscreen() || fscreen.fullscreenElement) return;
|
|
|
|
if (canFullscreenAnyElement()) {
|
|
|
|
if (containerElement) fscreen.requestFullscreen(containerElement);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (canWebkitFullscreen()) {
|
|
|
|
if (videoElement) (videoElement as any).webkitEnterFullscreen();
|
|
|
|
}
|
|
|
|
},
|
2023-09-30 20:57:00 +02:00
|
|
|
};
|
|
|
|
}
|