2022-02-28 00:08:20 +01:00
|
|
|
import { IconPatch } from "components/buttons/IconPatch";
|
|
|
|
import { Icons } from "components/Icon";
|
|
|
|
import { Loading } from "components/layout/Loading";
|
2022-03-13 17:46:56 +01:00
|
|
|
import { MWMediaCaption, MWMediaStream } from "providers";
|
2022-03-06 14:41:51 +01:00
|
|
|
import { ReactElement, useEffect, useRef, useState } from "react";
|
2022-02-20 16:45:46 +01:00
|
|
|
|
|
|
|
export interface VideoPlayerProps {
|
|
|
|
source: MWMediaStream;
|
2022-03-13 17:46:56 +01:00
|
|
|
captions: MWMediaCaption[];
|
2022-02-28 00:08:20 +01:00
|
|
|
startAt?: number;
|
2022-02-20 16:45:46 +01:00
|
|
|
onProgress?: (event: ProgressEvent) => void;
|
|
|
|
}
|
|
|
|
|
2022-02-28 00:08:20 +01:00
|
|
|
export function SkeletonVideoPlayer(props: { error?: boolean }) {
|
|
|
|
return (
|
|
|
|
<div className="bg-denim-200 flex aspect-video w-full items-center justify-center rounded-xl">
|
|
|
|
{props.error ? (
|
|
|
|
<div className="flex flex-col items-center">
|
|
|
|
<IconPatch icon={Icons.WARNING} className="text-red-400" />
|
2022-03-06 14:41:51 +01:00
|
|
|
<p className="mt-5 text-white">Couldn't get your stream</p>
|
2022-02-28 00:08:20 +01:00
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className="flex flex-col items-center">
|
|
|
|
<Loading />
|
|
|
|
<p className="mt-3 text-white">Getting your stream...</p>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-20 16:45:46 +01:00
|
|
|
export function VideoPlayer(props: VideoPlayerProps) {
|
|
|
|
const videoRef = useRef<HTMLVideoElement | null>(null);
|
2022-02-28 22:00:32 +01:00
|
|
|
const [hasErrored, setErrored] = useState(false);
|
|
|
|
const [isLoading, setLoading] = useState(true);
|
|
|
|
const showVideo = !isLoading && !hasErrored;
|
2022-02-20 16:45:46 +01:00
|
|
|
const mustUseHls = props.source.type === "m3u8";
|
|
|
|
|
2022-02-28 22:00:32 +01:00
|
|
|
// reset if stream url changes
|
|
|
|
useEffect(() => {
|
|
|
|
setLoading(true);
|
|
|
|
setErrored(false);
|
|
|
|
}, [props.source.url]);
|
|
|
|
|
2022-03-06 14:41:51 +01:00
|
|
|
let skeletonUi: null | ReactElement = null;
|
|
|
|
if (hasErrored) {
|
|
|
|
skeletonUi = <SkeletonVideoPlayer error />;
|
|
|
|
} else if (isLoading) {
|
|
|
|
skeletonUi = <SkeletonVideoPlayer />;
|
|
|
|
}
|
|
|
|
|
2022-02-20 16:45:46 +01:00
|
|
|
return (
|
2022-02-28 22:00:32 +01:00
|
|
|
<>
|
2022-03-06 14:41:51 +01:00
|
|
|
{skeletonUi}
|
2022-02-28 22:00:32 +01:00
|
|
|
<video
|
|
|
|
className={`bg-denim-500 w-full rounded-xl ${
|
|
|
|
!showVideo ? "hidden" : ""
|
|
|
|
}`}
|
|
|
|
ref={videoRef}
|
|
|
|
onProgress={(e) =>
|
|
|
|
props.onProgress && props.onProgress(e.nativeEvent as ProgressEvent)
|
|
|
|
}
|
|
|
|
onLoadedData={(e) => {
|
|
|
|
setLoading(false);
|
|
|
|
if (props.startAt)
|
|
|
|
(e.target as HTMLVideoElement).currentTime = props.startAt;
|
|
|
|
}}
|
|
|
|
onError={(e) => {
|
|
|
|
console.error("failed to playback stream", e);
|
|
|
|
setErrored(true);
|
|
|
|
}}
|
|
|
|
controls
|
|
|
|
autoPlay
|
|
|
|
>
|
|
|
|
{!mustUseHls ? (
|
|
|
|
<source src={props.source.url} type="video/mp4" />
|
|
|
|
) : null}
|
2022-03-13 17:46:56 +01:00
|
|
|
{props.captions.map((v) => (
|
|
|
|
<track key={v.id} kind="captions" label={v.label} src={v.url} />
|
|
|
|
))}
|
2022-02-28 22:00:32 +01:00
|
|
|
</video>
|
|
|
|
</>
|
2022-02-20 16:45:46 +01:00
|
|
|
);
|
|
|
|
}
|