2024-03-21 14:43:37 +01:00
|
|
|
import FilterCheckBox from "@/components/filter/FilterCheckBox";
|
|
|
|
import { CalendarFilterButton } from "@/components/filter/ReviewFilterGroup";
|
2024-03-12 00:31:05 +01:00
|
|
|
import PreviewPlayer, {
|
|
|
|
PreviewController,
|
|
|
|
} from "@/components/player/PreviewPlayer";
|
2024-03-13 21:24:24 +01:00
|
|
|
import { DynamicVideoController } from "@/components/player/dynamic/DynamicVideoController";
|
|
|
|
import DynamicVideoPlayer from "@/components/player/dynamic/DynamicVideoPlayer";
|
2024-03-05 20:55:44 +01:00
|
|
|
import MotionReviewTimeline from "@/components/timeline/MotionReviewTimeline";
|
2024-02-23 01:03:34 +01:00
|
|
|
import { Button } from "@/components/ui/button";
|
2024-03-21 14:43:37 +01:00
|
|
|
import { Drawer, DrawerContent, DrawerTrigger } from "@/components/ui/drawer";
|
2024-03-12 00:31:05 +01:00
|
|
|
import { FrigateConfig } from "@/types/frigateConfig";
|
2024-02-28 23:23:56 +01:00
|
|
|
import { Preview } from "@/types/preview";
|
2024-03-21 14:43:37 +01:00
|
|
|
import {
|
|
|
|
MotionData,
|
|
|
|
ReviewFilter,
|
|
|
|
ReviewSegment,
|
|
|
|
ReviewSummary,
|
|
|
|
} from "@/types/review";
|
|
|
|
import { getEndOfDayTimestamp } from "@/utils/dateUtil";
|
2024-03-04 05:21:30 +01:00
|
|
|
import { getChunkedTimeDay } from "@/utils/timelineUtil";
|
2024-03-05 13:03:10 +01:00
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
2024-03-17 13:30:19 +01:00
|
|
|
import { isDesktop, isMobile } from "react-device-detect";
|
2024-03-21 14:43:37 +01:00
|
|
|
import { FaCircle, FaVideo } from "react-icons/fa";
|
2024-02-23 01:03:34 +01:00
|
|
|
import { IoMdArrowRoundBack } from "react-icons/io";
|
|
|
|
import { useNavigate } from "react-router-dom";
|
2024-03-05 20:55:44 +01:00
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
const SEGMENT_DURATION = 30;
|
2024-02-23 01:03:34 +01:00
|
|
|
|
2024-03-17 13:30:19 +01:00
|
|
|
type RecordingViewProps = {
|
2024-03-05 13:03:10 +01:00
|
|
|
startCamera: string;
|
|
|
|
startTime: number;
|
2024-03-21 14:43:37 +01:00
|
|
|
reviewItems?: ReviewSegment[];
|
|
|
|
reviewSummary?: ReviewSummary;
|
2024-03-05 13:03:10 +01:00
|
|
|
allCameras: string[];
|
|
|
|
allPreviews?: Preview[];
|
2024-03-21 14:43:37 +01:00
|
|
|
filter?: ReviewFilter;
|
|
|
|
updateFilter: (newFilter: ReviewFilter) => void;
|
2024-03-05 13:03:10 +01:00
|
|
|
};
|
2024-03-17 13:30:19 +01:00
|
|
|
export function RecordingView({
|
2024-03-05 13:03:10 +01:00
|
|
|
startCamera,
|
|
|
|
startTime,
|
|
|
|
reviewItems,
|
2024-03-21 14:43:37 +01:00
|
|
|
reviewSummary,
|
2024-03-05 13:03:10 +01:00
|
|
|
allCameras,
|
|
|
|
allPreviews,
|
2024-03-21 14:43:37 +01:00
|
|
|
filter,
|
|
|
|
updateFilter,
|
2024-03-17 13:30:19 +01:00
|
|
|
}: RecordingViewProps) {
|
2024-03-12 00:31:05 +01:00
|
|
|
const { data: config } = useSWR<FrigateConfig>("config");
|
2024-03-05 13:03:10 +01:00
|
|
|
const navigate = useNavigate();
|
|
|
|
const contentRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
|
|
|
|
// controller state
|
|
|
|
|
|
|
|
const [mainCamera, setMainCamera] = useState(startCamera);
|
2024-03-12 00:31:05 +01:00
|
|
|
const mainControllerRef = useRef<DynamicVideoController | null>(null);
|
|
|
|
const previewRefs = useRef<{ [camera: string]: PreviewController }>({});
|
2024-03-05 13:03:10 +01:00
|
|
|
|
2024-03-10 14:17:09 +01:00
|
|
|
const [playbackStart, setPlaybackStart] = useState(startTime);
|
|
|
|
|
2024-03-12 18:08:31 +01:00
|
|
|
const mainCameraReviewItems = useMemo(
|
2024-03-21 14:43:37 +01:00
|
|
|
() => reviewItems?.filter((cam) => cam.camera == mainCamera) ?? [],
|
2024-03-12 18:08:31 +01:00
|
|
|
[reviewItems, mainCamera],
|
|
|
|
);
|
|
|
|
|
2024-03-05 13:03:10 +01:00
|
|
|
// timeline time
|
|
|
|
|
|
|
|
const timeRange = useMemo(() => getChunkedTimeDay(startTime), [startTime]);
|
|
|
|
const [selectedRangeIdx, setSelectedRangeIdx] = useState(
|
|
|
|
timeRange.ranges.findIndex((chunk) => {
|
|
|
|
return chunk.start <= startTime && chunk.end >= startTime;
|
|
|
|
}),
|
|
|
|
);
|
2024-03-08 01:31:43 +01:00
|
|
|
const currentTimeRange = useMemo(
|
|
|
|
() => timeRange.ranges[selectedRangeIdx],
|
|
|
|
[selectedRangeIdx, timeRange],
|
|
|
|
);
|
2024-03-05 13:03:10 +01:00
|
|
|
|
|
|
|
// move to next clip
|
2024-03-13 21:24:24 +01:00
|
|
|
|
|
|
|
const onClipEnded = useCallback(() => {
|
2024-03-12 00:31:05 +01:00
|
|
|
if (!mainControllerRef.current) {
|
2024-03-05 13:03:10 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-13 21:24:24 +01:00
|
|
|
if (selectedRangeIdx < timeRange.ranges.length - 1) {
|
|
|
|
setSelectedRangeIdx(selectedRangeIdx + 1);
|
|
|
|
}
|
|
|
|
}, [selectedRangeIdx, timeRange]);
|
2024-03-05 13:03:10 +01:00
|
|
|
|
|
|
|
// scrubbing and timeline state
|
|
|
|
|
|
|
|
const [scrubbing, setScrubbing] = useState(false);
|
|
|
|
const [currentTime, setCurrentTime] = useState<number>(startTime);
|
2024-03-13 15:05:01 +01:00
|
|
|
const [playerTime, setPlayerTime] = useState(startTime);
|
|
|
|
|
|
|
|
const updateSelectedSegment = useCallback(
|
2024-03-14 15:28:06 +01:00
|
|
|
(currentTime: number, updateStartTime: boolean) => {
|
2024-03-13 15:05:01 +01:00
|
|
|
const index = timeRange.ranges.findIndex(
|
|
|
|
(seg) => seg.start <= currentTime && seg.end >= currentTime,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (index != -1) {
|
2024-03-14 15:28:06 +01:00
|
|
|
if (updateStartTime) {
|
|
|
|
setPlaybackStart(currentTime);
|
|
|
|
}
|
|
|
|
|
2024-03-13 21:24:24 +01:00
|
|
|
setSelectedRangeIdx(index);
|
2024-03-13 15:05:01 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
[timeRange],
|
|
|
|
);
|
2024-03-05 13:03:10 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (scrubbing) {
|
2024-03-08 01:31:43 +01:00
|
|
|
if (
|
|
|
|
currentTime > currentTimeRange.end + 60 ||
|
|
|
|
currentTime < currentTimeRange.start - 60
|
|
|
|
) {
|
2024-03-14 15:28:06 +01:00
|
|
|
updateSelectedSegment(currentTime, false);
|
2024-03-08 01:31:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-12 00:31:05 +01:00
|
|
|
mainControllerRef.current?.scrubToTimestamp(currentTime);
|
|
|
|
|
|
|
|
Object.values(previewRefs.current).forEach((controller) => {
|
2024-03-05 13:03:10 +01:00
|
|
|
controller.scrubToTimestamp(currentTime);
|
|
|
|
});
|
|
|
|
}
|
2024-03-13 15:05:01 +01:00
|
|
|
}, [
|
|
|
|
currentTime,
|
|
|
|
scrubbing,
|
|
|
|
timeRange,
|
|
|
|
currentTimeRange,
|
|
|
|
updateSelectedSegment,
|
|
|
|
]);
|
2024-03-05 13:03:10 +01:00
|
|
|
|
2024-03-13 15:05:01 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (!scrubbing) {
|
|
|
|
if (Math.abs(currentTime - playerTime) > 10) {
|
|
|
|
if (
|
|
|
|
currentTimeRange.start <= currentTime &&
|
|
|
|
currentTimeRange.end >= currentTime
|
|
|
|
) {
|
|
|
|
mainControllerRef.current?.seekToTimestamp(currentTime, true);
|
|
|
|
} else {
|
2024-03-14 15:28:06 +01:00
|
|
|
updateSelectedSegment(currentTime, true);
|
2024-03-13 15:05:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// we only want to seek when current time doesn't match the player update time
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2024-03-14 15:28:06 +01:00
|
|
|
}, [currentTime, scrubbing]);
|
2024-03-13 15:05:01 +01:00
|
|
|
|
2024-03-05 13:03:10 +01:00
|
|
|
const onSelectCamera = useCallback(
|
|
|
|
(newCam: string) => {
|
|
|
|
setMainCamera(newCam);
|
2024-03-12 00:31:05 +01:00
|
|
|
setPlaybackStart(currentTime);
|
2024-03-05 13:03:10 +01:00
|
|
|
},
|
2024-03-12 00:31:05 +01:00
|
|
|
[currentTime],
|
2024-03-05 13:03:10 +01:00
|
|
|
);
|
|
|
|
|
2024-03-05 20:55:44 +01:00
|
|
|
// motion timeline data
|
|
|
|
|
2024-03-21 14:43:37 +01:00
|
|
|
const { data: motionData } = useSWR<MotionData[]>([
|
|
|
|
"review/activity/motion",
|
|
|
|
{
|
|
|
|
before: timeRange.end,
|
|
|
|
after: timeRange.start,
|
|
|
|
scale: SEGMENT_DURATION / 2,
|
|
|
|
cameras: mainCamera,
|
|
|
|
},
|
|
|
|
]);
|
2024-03-05 20:55:44 +01:00
|
|
|
|
2024-03-16 00:28:57 +01:00
|
|
|
const mainCameraAspect = useMemo(() => {
|
2024-03-12 00:31:05 +01:00
|
|
|
if (!config) {
|
2024-03-16 00:28:57 +01:00
|
|
|
return "normal";
|
2024-03-12 00:31:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const aspectRatio =
|
|
|
|
config.cameras[mainCamera].detect.width /
|
|
|
|
config.cameras[mainCamera].detect.height;
|
2024-03-16 00:28:57 +01:00
|
|
|
|
2024-03-12 00:31:05 +01:00
|
|
|
if (aspectRatio > 2) {
|
2024-03-16 00:28:57 +01:00
|
|
|
return "wide";
|
|
|
|
} else if (aspectRatio < 16 / 9) {
|
|
|
|
return "tall";
|
2024-03-12 00:31:05 +01:00
|
|
|
} else {
|
2024-03-16 00:28:57 +01:00
|
|
|
return "normal";
|
2024-03-12 00:31:05 +01:00
|
|
|
}
|
|
|
|
}, [config, mainCamera]);
|
|
|
|
|
2024-03-16 00:28:57 +01:00
|
|
|
const grow = useMemo(() => {
|
|
|
|
if (mainCameraAspect == "wide") {
|
|
|
|
return "w-full aspect-wide";
|
2024-03-18 01:53:47 +01:00
|
|
|
} else if (isDesktop && mainCameraAspect == "tall") {
|
2024-03-16 00:28:57 +01:00
|
|
|
return "h-full aspect-tall";
|
|
|
|
} else {
|
|
|
|
return "w-full aspect-video";
|
|
|
|
}
|
|
|
|
}, [mainCameraAspect]);
|
|
|
|
|
2024-03-05 13:03:10 +01:00
|
|
|
return (
|
|
|
|
<div ref={contentRef} className="relative size-full">
|
2024-03-21 14:43:37 +01:00
|
|
|
<div
|
|
|
|
className={`absolute left-0 top-0 mr-2 flex items-center justify-between ${isMobile ? "right-0" : "right-24"}`}
|
2024-03-05 13:03:10 +01:00
|
|
|
>
|
2024-03-21 14:43:37 +01:00
|
|
|
<Button className="rounded-lg" onClick={() => navigate(-1)}>
|
|
|
|
<IoMdArrowRoundBack className="size-5 mr-[10px]" />
|
|
|
|
Back
|
|
|
|
</Button>
|
|
|
|
<div className="flex items-center justify-end">
|
|
|
|
<CalendarFilterButton
|
|
|
|
day={
|
|
|
|
filter?.after == undefined
|
|
|
|
? undefined
|
|
|
|
: new Date(filter.after * 1000)
|
|
|
|
}
|
|
|
|
reviewSummary={reviewSummary}
|
|
|
|
updateSelectedDay={(day) => {
|
|
|
|
updateFilter({
|
|
|
|
...filter,
|
|
|
|
after: day == undefined ? undefined : day.getTime() / 1000,
|
|
|
|
before:
|
|
|
|
day == undefined ? undefined : getEndOfDayTimestamp(day),
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{isMobile && (
|
|
|
|
<Drawer>
|
|
|
|
<DrawerTrigger asChild>
|
|
|
|
<Button
|
|
|
|
className="rounded-lg capitalize flex items-center gap-2"
|
|
|
|
size="sm"
|
|
|
|
variant="secondary"
|
2024-03-17 13:30:19 +01:00
|
|
|
>
|
2024-03-21 14:43:37 +01:00
|
|
|
<FaVideo className="text-muted-foreground" />
|
|
|
|
{mainCamera.replaceAll("_", " ")}
|
|
|
|
</Button>
|
|
|
|
</DrawerTrigger>
|
|
|
|
<DrawerContent className="max-h-[75dvh] overflow-hidden">
|
|
|
|
{allCameras.map((cam) => (
|
|
|
|
<FilterCheckBox
|
|
|
|
key={cam}
|
|
|
|
CheckIcon={FaCircle}
|
|
|
|
iconClassName="size-2"
|
|
|
|
label={cam.replaceAll("_", " ")}
|
|
|
|
isChecked={cam == mainCamera}
|
|
|
|
onCheckedChange={() => {
|
|
|
|
setPlaybackStart(currentTime);
|
|
|
|
setMainCamera(cam);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</DrawerContent>
|
|
|
|
</Drawer>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-03-05 13:03:10 +01:00
|
|
|
|
2024-03-17 13:30:19 +01:00
|
|
|
<div
|
|
|
|
className={`flex h-full justify-center overflow-hidden ${isDesktop ? "" : "flex-col pt-12"}`}
|
|
|
|
>
|
2024-03-11 23:07:56 +01:00
|
|
|
<div className="flex flex-1 flex-wrap">
|
2024-03-16 00:28:57 +01:00
|
|
|
<div
|
|
|
|
className={`size-full flex px-2 items-center ${mainCameraAspect == "tall" ? "flex-row justify-evenly" : "flex-col justify-center"}`}
|
|
|
|
>
|
2024-03-12 00:31:05 +01:00
|
|
|
<div
|
|
|
|
key={mainCamera}
|
2024-03-17 13:30:19 +01:00
|
|
|
className={
|
|
|
|
isDesktop
|
|
|
|
? `flex justify-center items mb-5 ${mainCameraAspect == "tall" ? "h-[96%]" : "w-[82%]"}`
|
|
|
|
: `w-full ${mainCameraAspect == "wide" ? "" : "aspect-video"}`
|
|
|
|
}
|
2024-03-12 00:31:05 +01:00
|
|
|
>
|
2024-03-05 13:03:10 +01:00
|
|
|
<DynamicVideoPlayer
|
2024-03-16 00:28:57 +01:00
|
|
|
className={grow}
|
2024-03-11 23:07:56 +01:00
|
|
|
camera={mainCamera}
|
2024-03-08 01:31:43 +01:00
|
|
|
timeRange={currentTimeRange}
|
2024-03-05 13:03:10 +01:00
|
|
|
cameraPreviews={allPreviews ?? []}
|
2024-03-13 21:24:24 +01:00
|
|
|
startTimestamp={playbackStart}
|
|
|
|
onTimestampUpdate={(timestamp) => {
|
|
|
|
setPlayerTime(timestamp);
|
|
|
|
setCurrentTime(timestamp);
|
|
|
|
Object.values(previewRefs.current ?? {}).forEach((prev) =>
|
|
|
|
prev.scrubToTimestamp(Math.floor(timestamp)),
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
onClipEnded={onClipEnded}
|
2024-03-05 13:03:10 +01:00
|
|
|
onControllerReady={(controller) => {
|
2024-03-12 00:31:05 +01:00
|
|
|
mainControllerRef.current = controller;
|
2024-03-05 13:03:10 +01:00
|
|
|
}}
|
2024-03-14 15:28:06 +01:00
|
|
|
isScrubbing={scrubbing}
|
2024-03-05 13:03:10 +01:00
|
|
|
/>
|
|
|
|
</div>
|
2024-03-17 13:30:19 +01:00
|
|
|
{isDesktop && (
|
|
|
|
<div
|
|
|
|
className={`flex justify-center gap-2 ${mainCameraAspect == "tall" ? "h-full flex-col overflow-y-auto items-center" : "w-full overflow-x-auto"}`}
|
|
|
|
>
|
|
|
|
{allCameras.map((cam) => {
|
|
|
|
if (cam !== mainCamera) {
|
|
|
|
return (
|
|
|
|
<div key={cam}>
|
|
|
|
<PreviewPlayer
|
|
|
|
className={
|
|
|
|
mainCameraAspect == "tall" ? "" : "size-full"
|
|
|
|
}
|
|
|
|
camera={cam}
|
|
|
|
timeRange={currentTimeRange}
|
|
|
|
cameraPreviews={allPreviews ?? []}
|
|
|
|
startTime={startTime}
|
2024-03-18 01:53:47 +01:00
|
|
|
isScrubbing={scrubbing}
|
2024-03-17 13:30:19 +01:00
|
|
|
onControllerReady={(controller) => {
|
|
|
|
previewRefs.current[cam] = controller;
|
|
|
|
controller.scrubToTimestamp(startTime);
|
|
|
|
}}
|
|
|
|
onClick={() => onSelectCamera(cam)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
)}
|
2024-03-11 23:07:56 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
2024-03-05 13:03:10 +01:00
|
|
|
|
2024-03-17 13:30:19 +01:00
|
|
|
<div
|
|
|
|
className={
|
|
|
|
isDesktop
|
|
|
|
? "w-[100px] mt-2 overflow-y-auto no-scrollbar"
|
|
|
|
: "flex-grow overflow-hidden"
|
|
|
|
}
|
|
|
|
>
|
2024-03-21 14:43:37 +01:00
|
|
|
<MotionReviewTimeline
|
|
|
|
segmentDuration={30}
|
|
|
|
timestampSpread={15}
|
|
|
|
timelineStart={timeRange.end}
|
|
|
|
timelineEnd={timeRange.start}
|
|
|
|
showHandlebar
|
|
|
|
handlebarTime={currentTime}
|
|
|
|
setHandlebarTime={setCurrentTime}
|
|
|
|
events={mainCameraReviewItems}
|
|
|
|
motion_events={motionData ?? []}
|
|
|
|
severityType="significant_motion"
|
|
|
|
contentRef={contentRef}
|
|
|
|
onHandlebarDraggingChange={(scrubbing) => setScrubbing(scrubbing)}
|
|
|
|
/>
|
2024-03-11 23:07:56 +01:00
|
|
|
</div>
|
2024-03-05 13:03:10 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|