Don't show live view when window is not visible

This commit is contained in:
Nicolas Mowen 2024-02-12 11:54:15 -07:00
parent 8f863e1442
commit 2be7576421
2 changed files with 19 additions and 3 deletions

View File

@ -26,6 +26,7 @@ type LivePlayerProps = {
cameraConfig: CameraConfig;
preferredLiveMode?: LivePlayerMode;
showStillWithoutActivity?: boolean;
windowVisible?: boolean;
};
type Options = { [key: string]: boolean };
@ -35,14 +36,15 @@ export default function LivePlayer({
cameraConfig,
preferredLiveMode,
showStillWithoutActivity = true,
windowVisible = true,
}: LivePlayerProps) {
// camera activity
const { activeMotion, activeAudio, activeTracking } =
useCameraActivity(cameraConfig);
const cameraActive = useMemo(
() => activeMotion || activeTracking,
[activeMotion, activeTracking]
() => windowVisible && (activeMotion || activeTracking),
[activeMotion, activeTracking, windowVisible]
);
const liveMode = useCameraLiveMode(cameraConfig, preferredLiveMode);

View File

@ -5,7 +5,7 @@ import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area";
import { TooltipProvider } from "@/components/ui/tooltip";
import { Event as FrigateEvent } from "@/types/event";
import { FrigateConfig } from "@/types/frigateConfig";
import { useEffect, useMemo } from "react";
import { useCallback, useEffect, useMemo, useState } from "react";
import useSWR from "swr";
function Live() {
@ -64,6 +64,19 @@ function Live() {
.sort((aConf, bConf) => aConf.ui.order - bConf.ui.order);
}, [config]);
const [windowVisible, setWindowVisible] = useState(false);
const visibilityListener = useCallback(() => {
setWindowVisible(document.visibilityState == "visible");
}, []);
useEffect(() => {
addEventListener("visibilitychange", visibilityListener);
return () => {
removeEventListener("visibilitychange", visibilityListener);
};
}, []);
return (
<>
{events && events.length > 0 && (
@ -94,6 +107,7 @@ function Live() {
<LivePlayer
key={camera.name}
className={`mb-2 md:mb-0 rounded-2xl bg-black ${grow}`}
windowVisible={windowVisible}
cameraConfig={camera}
preferredLiveMode="mse"
/>