2023-12-20 17:34:27 +03:00
|
|
|
import { baseUrl } from "@/api/baseUrl";
|
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-03-13 17:00:37 +03:00
|
|
|
import { useEffect, 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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default function JSMpegPlayer({
|
|
|
|
|
camera,
|
|
|
|
|
width,
|
|
|
|
|
height,
|
2024-02-10 15:30:53 +03:00
|
|
|
className,
|
2023-12-20 17:34:27 +03:00
|
|
|
}: JSMpegPlayerProps) {
|
|
|
|
|
const url = `${baseUrl.replace(/^http/, "ws")}live/jsmpeg/${camera}`;
|
|
|
|
|
const playerRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
|
const containerRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!playerRef.current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const video = new JSMpeg.VideoElement(
|
|
|
|
|
playerRef.current,
|
|
|
|
|
url,
|
|
|
|
|
{},
|
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-02-10 15:30:53 +03:00
|
|
|
<div className={className} ref={containerRef}>
|
2023-12-20 17:34:27 +03:00
|
|
|
<div
|
|
|
|
|
ref={playerRef}
|
2024-03-13 17:00:37 +03:00
|
|
|
className="jsmpeg h-full"
|
|
|
|
|
style={{ aspectRatio: width / height }}
|
2023-12-20 17:34:27 +03:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|