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-05-28 01:18:04 +03:00
|
|
|
import React, { useEffect, useMemo, useRef } 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>;
|
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,
|
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);
|
|
|
|
|
|
|
|
|
|
const [{ width: containerWidth, height: containerHeight }] =
|
|
|
|
|
useResizeObserver(containerRef ?? internalContainerRef);
|
|
|
|
|
|
|
|
|
|
const stretch = true;
|
|
|
|
|
const aspectRatio = width / height;
|
|
|
|
|
|
|
|
|
|
const fitAspect = useMemo(
|
|
|
|
|
() => containerWidth / containerHeight,
|
|
|
|
|
[containerWidth, containerHeight],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const scaledHeight = useMemo(() => {
|
|
|
|
|
if (containerRef?.current && width && height) {
|
|
|
|
|
const scaledHeight =
|
|
|
|
|
aspectRatio < (fitAspect ?? 0)
|
|
|
|
|
? Math.floor(
|
|
|
|
|
Math.min(containerHeight, containerRef.current?.clientHeight),
|
|
|
|
|
)
|
|
|
|
|
: aspectRatio > fitAspect
|
|
|
|
|
? 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,
|
|
|
|
|
containerRef,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const scaledWidth = useMemo(() => {
|
|
|
|
|
if (aspectRatio && scaledHeight) {
|
|
|
|
|
return Math.ceil(scaledHeight * aspectRatio);
|
|
|
|
|
}
|
|
|
|
|
}, [scaledHeight, aspectRatio]);
|
2023-12-20 17:34:27 +03:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!playerRef.current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const video = new JSMpeg.VideoElement(
|
|
|
|
|
playerRef.current,
|
|
|
|
|
url,
|
2024-05-28 01:18:04 +03:00
|
|
|
{ canvas: "#video-canvas" },
|
2024-02-29 01:23:56 +03:00
|
|
|
{ protocols: [], audio: false, videoBufferSize: 1024 * 1024 * 4 },
|
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;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}, [url]);
|
|
|
|
|
|
|
|
|
|
return (
|
2024-05-28 01:18:04 +03:00
|
|
|
<div className={className} ref={internalContainerRef}>
|
|
|
|
|
<div ref={playerRef} className="jsmpeg">
|
|
|
|
|
<canvas
|
|
|
|
|
id="video-canvas"
|
|
|
|
|
style={{
|
|
|
|
|
width: scaledWidth ?? width,
|
|
|
|
|
height: scaledHeight ?? height,
|
|
|
|
|
}}
|
|
|
|
|
></canvas>
|
|
|
|
|
</div>
|
2023-12-20 17:34:27 +03:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|