Merge branch 'streamline-live' of https://github.com/hawkeye217/frigate into streamline-live

This commit is contained in:
Josh Hawkins 2024-02-07 14:10:46 -06:00
commit cd16e280a0
7 changed files with 139 additions and 59 deletions

View File

@ -4,7 +4,6 @@ import { useState } from "react";
import Wrapper from "@/components/Wrapper"; import Wrapper from "@/components/Wrapper";
import Sidebar from "@/components/Sidebar"; import Sidebar from "@/components/Sidebar";
import Header from "@/components/Header"; import Header from "@/components/Header";
import Dashboard from "@/pages/Dashboard";
import Live from "@/pages/Live"; import Live from "@/pages/Live";
import History from "@/pages/History"; import History from "@/pages/History";
import Export from "@/pages/Export"; import Export from "@/pages/Export";
@ -30,13 +29,9 @@ function App() {
<Header onToggleNavbar={toggleNavbar} /> <Header onToggleNavbar={toggleNavbar} />
<div className="grid grid-cols-[auto,1fr] flex-grow-1 overflow-auto"> <div className="grid grid-cols-[auto,1fr] flex-grow-1 overflow-auto">
<Sidebar sheetOpen={sheetOpen} setSheetOpen={setSheetOpen} /> <Sidebar sheetOpen={sheetOpen} setSheetOpen={setSheetOpen} />
<div <div id="pageRoot" className="overflow-x-hidden px-4 py-2 w-screen">
id="pageRoot"
className="overflow-x-hidden px-4 py-2 w-screen md:w-full"
>
<Routes> <Routes>
<Route path="/" element={<Dashboard />} /> <Route path="/" element={<Live />} />
<Route path="/live" element={<Live />} />
<Route path="/history" element={<History />} /> <Route path="/history" element={<History />} />
<Route path="/export" element={<Export />} /> <Route path="/export" element={<Export />} />
<Route path="/storage" element={<Storage />} /> <Route path="/storage" element={<Storage />} />

View File

@ -228,7 +228,7 @@ export function useMotionActivity(camera: string): { payload: string } {
return { payload }; return { payload };
} }
export function useAudioActivity(camera: string): { payload: string } { export function useAudioActivity(camera: string): { payload: number } {
const { const {
value: { payload }, value: { payload },
} = useWs(`${camera}/audio/rms`, ""); } = useWs(`${camera}/audio/rms`, "");

View File

@ -66,26 +66,21 @@ function HeaderNavigation() {
const navbarLinks = [ const navbarLinks = [
{ {
id: 1, id: 1,
title: "Dashboard", title: "Live",
url: "/", url: "/",
}, },
{ {
id: 2, id: 2,
title: "Live",
url: "/live",
},
{
id: 3,
title: "History", title: "History",
url: "/history", url: "/history",
}, },
{ {
id: 4, id: 3,
title: "Export", title: "Export",
url: "/export", url: "/export",
}, },
{ {
id: 5, id: 4,
title: "UI Playground", title: "UI Playground",
url: "/playground", url: "/playground",
dev: true, dev: true,

View File

@ -108,7 +108,7 @@ export default function DynamicCameraImage({
{camera.audio.enabled_in_config && ( {camera.audio.enabled_in_config && (
<LuEar <LuEar
className={`${ className={`${
parseInt(audioRms) >= camera.audio.min_volume audioRms >= camera.audio.min_volume
? "text-audio" ? "text-audio"
: "text-gray-600" : "text-gray-600"
}`} }`}

View File

@ -11,8 +11,10 @@ import { Label } from "../ui/label";
import { usePersistence } from "@/hooks/use-persistence"; import { usePersistence } from "@/hooks/use-persistence";
import MSEPlayer from "./MsePlayer"; import MSEPlayer from "./MsePlayer";
import JSMpegPlayer from "./JSMpegPlayer"; import JSMpegPlayer from "./JSMpegPlayer";
import { MdCircle } from "react-icons/md"; import { MdCircle, MdLeakAdd, MdSelectAll } from "react-icons/md";
import { BsSoundwave } from "react-icons/bs";
import Chip from "../Chip"; import Chip from "../Chip";
import useCameraActivity from "@/hooks/use-camera-activity";
const emptyObject = Object.freeze({}); const emptyObject = Object.freeze({});
@ -20,6 +22,7 @@ type LivePlayerProps = {
className?: string; className?: string;
cameraConfig: CameraConfig; cameraConfig: CameraConfig;
liveMode?: "webrtc" | "mse" | "jsmpeg" | "debug"; liveMode?: "webrtc" | "mse" | "jsmpeg" | "debug";
liveChips?: boolean;
}; };
type Options = { [key: string]: boolean }; type Options = { [key: string]: boolean };
@ -28,14 +31,19 @@ export default function LivePlayer({
className, className,
cameraConfig, cameraConfig,
liveMode = "mse", liveMode = "mse",
liveChips = false,
}: LivePlayerProps) { }: LivePlayerProps) {
const [showSettings, setShowSettings] = useState(false); // camera activity
const { activeMotion, activeAudio, activeTracking } =
useCameraActivity(cameraConfig);
// debug view settings
const [showSettings, setShowSettings] = useState(false);
const [options, setOptions] = usePersistence( const [options, setOptions] = usePersistence(
`${cameraConfig?.name}-feed`, `${cameraConfig?.name}-feed`,
emptyObject emptyObject
); );
const handleSetOption = useCallback( const handleSetOption = useCallback(
(id: string, value: boolean) => { (id: string, value: boolean) => {
const newOptions = { ...options, [id]: value }; const newOptions = { ...options, [id]: value };
@ -43,7 +51,6 @@ export default function LivePlayer({
}, },
[options, setOptions] [options, setOptions]
); );
const searchParams = useMemo( const searchParams = useMemo(
() => () =>
new URLSearchParams( new URLSearchParams(
@ -55,7 +62,6 @@ export default function LivePlayer({
), ),
[options] [options]
); );
const handleToggleSettings = useCallback(() => { const handleToggleSettings = useCallback(() => {
setShowSettings(!showSettings); setShowSettings(!showSettings);
}, [showSettings, setShowSettings]); }, [showSettings, setShowSettings]);
@ -126,6 +132,30 @@ export default function LivePlayer({
return ( return (
<div className={`relative flex justify-center ${className}`}> <div className={`relative flex justify-center ${className}`}>
{player} {player}
<div className="absolute flex left-2 top-2 gap-2">
<Chip className="bg-gray-500 bg-gradient-to-br">
<MdLeakAdd
className={`w-4 h-4 ${activeMotion ? "text-motion" : "text-white"}`}
/>
<div className="ml-1 capitalize text-white text-xs">Motion</div>
</Chip>
{cameraConfig.audio.enabled_in_config && (
<Chip className="bg-gray-500 bg-gradient-to-br">
<BsSoundwave
className={`w-4 h-4 ${activeAudio ? "text-audio" : "text-white"}`}
/>
<div className="ml-1 capitalize text-white text-xs">Sound</div>
</Chip>
)}
<Chip className="bg-gray-500 bg-gradient-to-br">
<MdSelectAll
className={`w-4 h-4 ${
activeTracking ? "text-object" : "text-white"
}`}
/>
<div className="ml-1 capitalize text-white text-xs">Tracking</div>
</Chip>
</div>
<Chip className="absolute right-2 top-2 bg-gray-500 bg-gradient-to-br"> <Chip className="absolute right-2 top-2 bg-gray-500 bg-gradient-to-br">
<MdCircle className="w-2 h-2 text-danger" /> <MdCircle className="w-2 h-2 text-danger" />
<div className="ml-1 capitalize text-white text-xs"> <div className="ml-1 capitalize text-white text-xs">

View File

@ -0,0 +1,64 @@
import {
useAudioActivity,
useFrigateEvents,
useMotionActivity,
} from "@/api/ws";
import { CameraConfig } from "@/types/frigateConfig";
import { useEffect, useMemo, useState } from "react";
type useCameraActivityReturn = {
activeTracking: boolean;
activeMotion: boolean;
activeAudio: boolean;
};
export default function useCameraActivity(
camera: CameraConfig
): useCameraActivityReturn {
const [activeObjects, setActiveObjects] = useState<string[]>([]);
const hasActiveObjects = useMemo(
() => activeObjects.length > 0,
[activeObjects]
);
const { payload: detectingMotion } = useMotionActivity(camera.name);
const { payload: event } = useFrigateEvents();
const { payload: audioRms } = useAudioActivity(camera.name);
useEffect(() => {
if (!event) {
return;
}
if (event.after.camera != camera.name) {
return;
}
if (event.type == "end") {
const eventIndex = activeObjects.indexOf(event.after.id);
if (eventIndex != -1) {
const newActiveObjects = [...activeObjects];
newActiveObjects.splice(eventIndex, 1);
setActiveObjects(newActiveObjects);
}
} else {
if (!event.after.stationary) {
const eventIndex = activeObjects.indexOf(event.after.id);
if (eventIndex == -1) {
const newActiveObjects = [...activeObjects, event.after.id];
setActiveObjects(newActiveObjects);
}
}
}
}, [event, activeObjects]);
return {
activeTracking: hasActiveObjects,
activeMotion: detectingMotion == "ON",
activeAudio: camera.audio.enabled_in_config
? audioRms >= camera.audio.min_volume
: false,
};
}

View File

@ -4,7 +4,7 @@ import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area";
import { Event as FrigateEvent } from "@/types/event"; import { Event as FrigateEvent } from "@/types/event";
import { FrigateConfig } from "@/types/frigateConfig"; import { FrigateConfig } from "@/types/frigateConfig";
import axios from "axios"; import axios from "axios";
import { useCallback, useEffect, useMemo, useState } from "react"; import { useCallback, useMemo } from "react";
import useSWR from "swr"; import useSWR from "swr";
function Live() { function Live() {
@ -12,26 +12,23 @@ function Live() {
// recent events // recent events
const [recentCutoff, setRecentCutoff] = useState<number>(0); const { data: allEvents, mutate: updateEvents } = useSWR<FrigateEvent[]>(
useEffect(() => { ["events", { limit: 10 }],
const date = new Date(); { revalidateOnFocus: false, refreshInterval: 60000 }
date.setHours(date.getHours() - 4); );
setRecentCutoff(date.getTime() / 1000);
const intervalId: NodeJS.Timeout = setInterval(() => { const events = useMemo(() => {
const date = new Date(); if (!allEvents) {
date.setHours(date.getHours() - 4); return [];
setRecentCutoff(date.getTime() / 1000); }
}, 30000);
return () => clearInterval(intervalId);
}, [30000]);
const { data: events, mutate: updateEvents } = useSWR<FrigateEvent[]>([
"events",
{ limit: 10, after: recentCutoff },
]);
const onFavorite = useCallback( const date = new Date();
async (e: Event, event: FrigateEvent) => { date.setHours(date.getHours() - 1);
const cutoff = date.getTime() / 1000;
return allEvents.filter((event) => event.start_time > cutoff);
}, [allEvents]);
const onFavorite = useCallback(async (e: Event, event: FrigateEvent) => {
e.stopPropagation(); e.stopPropagation();
let response; let response;
if (!event.retain_indefinitely) { if (!event.retain_indefinitely) {
@ -42,9 +39,7 @@ function Live() {
if (response.status === 200) { if (response.status === 200) {
updateEvents(); updateEvents();
} }
}, }, []);
[event]
);
// camera live views // camera live views
@ -77,21 +72,22 @@ function Live() {
</ScrollArea> </ScrollArea>
)} )}
<div className="mt-4 grid grid-cols-3 gap-4"> <div className="mt-4 grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 3xl:grid-cols-4 gap-4">
{cameras.map((camera) => { {cameras.map((camera) => {
let grow; let grow;
if (camera.detect.width / camera.detect.height > 2) { if (camera.detect.width / camera.detect.height > 2) {
grow = "h-[424px] col-span-2"; grow = "md:col-span-2";
} else if (camera.detect.width / camera.detect.height < 1) { } else if (camera.detect.width / camera.detect.height < 1) {
grow = "h-[840px] row-span-2"; grow = "md:row-span-2";
} else { } else {
grow = "h-[425px]"; grow = "aspect-video";
} }
return ( return (
<LivePlayer <LivePlayer
key={camera.name} key={camera.name}
className={`rounded-2xl bg-black ${grow}`} className={`rounded-2xl bg-black ${grow}`}
cameraConfig={camera} cameraConfig={camera}
liveChips
/> />
); );
})} })}