2023-12-20 17:34:27 +03:00
|
|
|
import { baseUrl } from "@/api/baseUrl";
|
2024-05-28 01:18:04 +03:00
|
|
|
import { useResizeObserver } from "@/hooks/resize-observer";
|
2024-02-29 01:23:56 +03:00
|
|
|
// @ts-expect-error we know this doesn't have types
|
2023-12-20 17:34:27 +03:00
|
|
|
import JSMpeg from "@cycjimmy/jsmpeg-player";
|
2024-06-03 14:39:19 +03:00
|
|
|
import React, { useEffect, useMemo, useRef, useId } from "react";
|
2023-12-20 17:34:27 +03:00
|
|
|
|
|
|
|
|
type JSMpegPlayerProps = {
|
2024-02-10 15:30:53 +03:00
|
|
|
className?: string;
|
2023-12-20 17:34:27 +03:00
|
|
|
camera: string;
|
|
|
|
|
width: number;
|
|
|
|
|
height: number;
|
2024-05-28 01:18:04 +03:00
|
|
|
containerRef?: React.MutableRefObject<HTMLDivElement | null>;
|
2024-06-11 02:24:25 +03:00
|
|
|
onPlaying?: () => void;
|
2023-12-20 17:34:27 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default function JSMpegPlayer({
|
|
|
|
|
camera,
|
|
|
|
|
width,
|
|
|
|
|
height,
|
2024-02-10 15:30:53 +03:00
|
|
|
className,
|
2024-05-28 01:18:04 +03:00
|
|
|
containerRef,
|
2024-06-11 02:24:25 +03:00
|
|
|
onPlaying,
|
2023-12-20 17:34:27 +03:00
|
|
|
}: JSMpegPlayerProps) {
|
|
|
|
|
const url = `${baseUrl.replace(/^http/, "ws")}live/jsmpeg/${camera}`;
|
|
|
|
|
const playerRef = useRef<HTMLDivElement | null>(null);
|
2024-05-28 01:18:04 +03:00
|
|
|
const internalContainerRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
|
|
2024-05-28 21:41:51 +03:00
|
|
|
const selectedContainerRef = useMemo(
|
|
|
|
|
() => containerRef ?? internalContainerRef,
|
|
|
|
|
[containerRef, internalContainerRef],
|
|
|
|
|
);
|
|
|
|
|
|
2024-05-28 01:18:04 +03:00
|
|
|
const [{ width: containerWidth, height: containerHeight }] =
|
2024-05-28 21:41:51 +03:00
|
|
|
useResizeObserver(selectedContainerRef);
|
2024-05-28 01:18:04 +03:00
|
|
|
|
|
|
|
|
const stretch = true;
|
|
|
|
|
const aspectRatio = width / height;
|
|
|
|
|
|
|
|
|
|
const fitAspect = useMemo(
|
|
|
|
|
() => containerWidth / containerHeight,
|
|
|
|
|
[containerWidth, containerHeight],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const scaledHeight = useMemo(() => {
|
2024-05-28 21:41:51 +03:00
|
|
|
if (selectedContainerRef?.current && width && height) {
|
2024-05-28 01:18:04 +03:00
|
|
|
const scaledHeight =
|
|
|
|
|
aspectRatio < (fitAspect ?? 0)
|
|
|
|
|
? Math.floor(
|
2024-05-28 21:41:51 +03:00
|
|
|
Math.min(
|
|
|
|
|
containerHeight,
|
|
|
|
|
selectedContainerRef.current?.clientHeight,
|
|
|
|
|
),
|
2024-05-28 01:18:04 +03:00
|
|
|
)
|
2024-05-29 22:18:51 +03:00
|
|
|
: aspectRatio >= fitAspect
|
2024-05-28 01:18:04 +03:00
|
|
|
? Math.floor(containerWidth / aspectRatio)
|
|
|
|
|
: Math.floor(containerWidth / aspectRatio) / 1.5;
|
|
|
|
|
const finalHeight = stretch
|
|
|
|
|
? scaledHeight
|
|
|
|
|
: Math.min(scaledHeight, height);
|
|
|
|
|
|
|
|
|
|
if (finalHeight > 0) {
|
|
|
|
|
return finalHeight;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, [
|
|
|
|
|
aspectRatio,
|
|
|
|
|
containerWidth,
|
|
|
|
|
containerHeight,
|
|
|
|
|
fitAspect,
|
|
|
|
|
height,
|
|
|
|
|
width,
|
|
|
|
|
stretch,
|
2024-05-28 21:41:51 +03:00
|
|
|
selectedContainerRef,
|
2024-05-28 01:18:04 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const scaledWidth = useMemo(() => {
|
|
|
|
|
if (aspectRatio && scaledHeight) {
|
|
|
|
|
return Math.ceil(scaledHeight * aspectRatio);
|
|
|
|
|
}
|
|
|
|
|
}, [scaledHeight, aspectRatio]);
|
2023-12-20 17:34:27 +03:00
|
|
|
|
2024-06-03 14:39:19 +03:00
|
|
|
const uniqueId = useId();
|
|
|
|
|
|
2023-12-20 17:34:27 +03:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!playerRef.current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const video = new JSMpeg.VideoElement(
|
|
|
|
|
playerRef.current,
|
|
|
|
|
url,
|
2024-06-03 14:39:19 +03:00
|
|
|
{ canvas: `#${CSS.escape(uniqueId)}` },
|
2024-06-11 02:24:25 +03:00
|
|
|
{
|
|
|
|
|
protocols: [],
|
|
|
|
|
audio: false,
|
|
|
|
|
videoBufferSize: 1024 * 1024 * 4,
|
|
|
|
|
onPlay: () => {
|
|
|
|
|
onPlaying?.();
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-12-20 17:34:27 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
if (playerRef.current) {
|
|
|
|
|
try {
|
|
|
|
|
video.destroy();
|
2024-02-29 01:23:56 +03:00
|
|
|
// eslint-disable-next-line no-empty
|
2023-12-20 17:34:27 +03:00
|
|
|
} catch (e) {}
|
|
|
|
|
playerRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-06-11 02:24:25 +03:00
|
|
|
}, [url, uniqueId, onPlaying]);
|
2023-12-20 17:34:27 +03:00
|
|
|
|
|
|
|
|
return (
|
2024-05-28 21:41:51 +03:00
|
|
|
<div className={className}>
|
|
|
|
|
<div className="size-full" ref={internalContainerRef}>
|
|
|
|
|
<div ref={playerRef} className="jsmpeg">
|
|
|
|
|
<canvas
|
2024-06-03 14:39:19 +03:00
|
|
|
id={uniqueId}
|
2024-05-28 21:41:51 +03:00
|
|
|
style={{
|
|
|
|
|
width: scaledWidth ?? width,
|
|
|
|
|
height: scaledHeight ?? height,
|
|
|
|
|
}}
|
|
|
|
|
></canvas>
|
|
|
|
|
</div>
|
2024-05-28 01:18:04 +03:00
|
|
|
</div>
|
2023-12-20 17:34:27 +03:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|