mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-02-07 11:45:24 +03:00
Fix jsmpeg
This commit is contained in:
parent
aabfe5f615
commit
9e00f4a7f5
@ -1,5 +1,5 @@
|
|||||||
import WebRtcPlayer from "./WebRTCPlayer";
|
import WebRtcPlayer from "./WebRTCPlayer";
|
||||||
import { BirdseyeConfig, CameraConfig } from "@/types/frigateConfig";
|
import { BirdseyeConfig } from "@/types/frigateConfig";
|
||||||
import ActivityIndicator from "../ui/activity-indicator";
|
import ActivityIndicator from "../ui/activity-indicator";
|
||||||
import JSMpegPlayer from "./JSMpegPlayer";
|
import JSMpegPlayer from "./JSMpegPlayer";
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
import { baseUrl } from "@/api/baseUrl";
|
import { baseUrl } from "@/api/baseUrl";
|
||||||
|
import { useResizeObserver } from "@/hooks/resize-observer";
|
||||||
|
// @ts-ignore we know this doesn't have types
|
||||||
import JSMpeg from "@cycjimmy/jsmpeg-player";
|
import JSMpeg from "@cycjimmy/jsmpeg-player";
|
||||||
import { useEffect, useRef } from "react";
|
import { useEffect, useMemo, useRef } from "react";
|
||||||
|
|
||||||
type JSMpegPlayerProps = {
|
type JSMpegPlayerProps = {
|
||||||
camera: string;
|
camera: string;
|
||||||
@ -13,10 +15,43 @@ export default function JSMpegPlayer({
|
|||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
}: JSMpegPlayerProps) {
|
}: JSMpegPlayerProps) {
|
||||||
const playerRef = useRef<HTMLDivElement | null>(null);
|
|
||||||
const url = `${baseUrl.replace(/^http/, "ws")}live/jsmpeg/${camera}`;
|
const url = `${baseUrl.replace(/^http/, "ws")}live/jsmpeg/${camera}`;
|
||||||
|
const playerRef = useRef<HTMLDivElement | null>(null);
|
||||||
|
const containerRef = useRef<HTMLDivElement | null>(null);
|
||||||
|
const [{ width: containerWidth }] = useResizeObserver(containerRef);
|
||||||
|
|
||||||
|
// Add scrollbar width (when visible) to the available observer width to eliminate screen juddering.
|
||||||
|
// https://github.com/blakeblackshear/frigate/issues/1657
|
||||||
|
let scrollBarWidth = 0;
|
||||||
|
if (window.innerWidth && document.body.offsetWidth) {
|
||||||
|
scrollBarWidth = window.innerWidth - document.body.offsetWidth;
|
||||||
|
}
|
||||||
|
const availableWidth = scrollBarWidth
|
||||||
|
? containerWidth + scrollBarWidth
|
||||||
|
: containerWidth;
|
||||||
|
const aspectRatio = width / height;
|
||||||
|
|
||||||
|
const scaledHeight = useMemo(() => {
|
||||||
|
const scaledHeight = Math.floor(availableWidth / aspectRatio);
|
||||||
|
const finalHeight = Math.min(scaledHeight, height);
|
||||||
|
|
||||||
|
if (finalHeight > 0) {
|
||||||
|
return finalHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 100;
|
||||||
|
}, [availableWidth, aspectRatio, height]);
|
||||||
|
const scaledWidth = useMemo(
|
||||||
|
() => Math.ceil(scaledHeight * aspectRatio - scrollBarWidth),
|
||||||
|
[scaledHeight, aspectRatio, scrollBarWidth]
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (!playerRef.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("player ref exists and creating video");
|
||||||
const video = new JSMpeg.VideoElement(
|
const video = new JSMpeg.VideoElement(
|
||||||
playerRef.current,
|
playerRef.current,
|
||||||
url,
|
url,
|
||||||
@ -35,18 +70,25 @@ export default function JSMpegPlayer({
|
|||||||
video.els.canvas.addEventListener("click", fullscreen);
|
video.els.canvas.addEventListener("click", fullscreen);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
video.destroy();
|
if (playerRef.current) {
|
||||||
|
try {
|
||||||
|
video.destroy();
|
||||||
|
} catch (e) {}
|
||||||
|
playerRef.current = null;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}, [url]);
|
}, [url]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div ref={containerRef}>
|
||||||
ref={playerRef}
|
<div
|
||||||
className="jsmpeg"
|
ref={playerRef}
|
||||||
style={{
|
className={`jsmpeg`}
|
||||||
maxHeight: `${height}px`,
|
style={{
|
||||||
maxWidth: `${width} px`,
|
height: `${scaledHeight}px`,
|
||||||
}}
|
width: `${scaledWidth} px`,
|
||||||
/>
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user