2024-02-23 01:03:34 +01:00
|
|
|
import DynamicVideoPlayer, {
|
|
|
|
DynamicVideoController,
|
|
|
|
} from "@/components/player/DynamicVideoPlayer";
|
|
|
|
import EventReviewTimeline from "@/components/timeline/EventReviewTimeline";
|
|
|
|
import { Button } from "@/components/ui/button";
|
2024-02-28 23:23:56 +01:00
|
|
|
import { Preview } from "@/types/preview";
|
2024-03-05 13:03:10 +01:00
|
|
|
import { ReviewSegment, ReviewSeverity } from "@/types/review";
|
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-02-23 01:03:34 +01:00
|
|
|
import { IoMdArrowRoundBack } from "react-icons/io";
|
|
|
|
import { useNavigate } from "react-router-dom";
|
|
|
|
|
2024-03-05 13:03:10 +01:00
|
|
|
type DesktopRecordingViewProps = {
|
|
|
|
startCamera: string;
|
|
|
|
startTime: number;
|
|
|
|
severity: ReviewSeverity;
|
|
|
|
reviewItems: ReviewSegment[];
|
|
|
|
allCameras: string[];
|
|
|
|
allPreviews?: Preview[];
|
|
|
|
};
|
|
|
|
export function DesktopRecordingView({
|
|
|
|
startCamera,
|
|
|
|
startTime,
|
|
|
|
severity,
|
|
|
|
reviewItems,
|
|
|
|
allCameras,
|
|
|
|
allPreviews,
|
|
|
|
}: DesktopRecordingViewProps) {
|
|
|
|
const navigate = useNavigate();
|
|
|
|
const contentRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
|
|
|
|
// controller state
|
|
|
|
|
|
|
|
const [playerReady, setPlayerReady] = useState(false);
|
|
|
|
const [mainCamera, setMainCamera] = useState(startCamera);
|
|
|
|
const videoPlayersRef = useRef<{ [camera: string]: DynamicVideoController }>(
|
|
|
|
{},
|
|
|
|
);
|
|
|
|
|
|
|
|
// timeline time
|
|
|
|
|
|
|
|
const timeRange = useMemo(() => getChunkedTimeDay(startTime), [startTime]);
|
|
|
|
const [selectedRangeIdx, setSelectedRangeIdx] = useState(
|
|
|
|
timeRange.ranges.findIndex((chunk) => {
|
|
|
|
return chunk.start <= startTime && chunk.end >= startTime;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
// move to next clip
|
|
|
|
useEffect(() => {
|
|
|
|
if (
|
|
|
|
!videoPlayersRef.current &&
|
|
|
|
Object.values(videoPlayersRef.current).length > 0
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const firstController = Object.values(videoPlayersRef.current)[0];
|
|
|
|
|
|
|
|
if (firstController) {
|
|
|
|
firstController.onClipChangedEvent((dir) => {
|
|
|
|
if (
|
|
|
|
dir == "forward" &&
|
|
|
|
selectedRangeIdx < timeRange.ranges.length - 1
|
|
|
|
) {
|
|
|
|
setSelectedRangeIdx(selectedRangeIdx + 1);
|
|
|
|
} else if (selectedRangeIdx > 0) {
|
|
|
|
setSelectedRangeIdx(selectedRangeIdx - 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [selectedRangeIdx, timeRange, videoPlayersRef, playerReady]);
|
|
|
|
|
|
|
|
// scrubbing and timeline state
|
|
|
|
|
|
|
|
const [scrubbing, setScrubbing] = useState(false);
|
|
|
|
const [currentTime, setCurrentTime] = useState<number>(startTime);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (scrubbing) {
|
|
|
|
Object.values(videoPlayersRef.current).forEach((controller) => {
|
|
|
|
controller.scrubToTimestamp(currentTime);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [currentTime, scrubbing]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!scrubbing) {
|
|
|
|
videoPlayersRef.current[mainCamera]?.seekToTimestamp(currentTime, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// we only want to seek when user stops scrubbing
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [scrubbing]);
|
|
|
|
|
|
|
|
const onSelectCamera = useCallback(
|
|
|
|
(newCam: string) => {
|
|
|
|
videoPlayersRef.current[mainCamera].onPlayerTimeUpdate(undefined);
|
|
|
|
videoPlayersRef.current[mainCamera].scrubToTimestamp(currentTime);
|
|
|
|
videoPlayersRef.current[newCam].seekToTimestamp(currentTime, true);
|
|
|
|
videoPlayersRef.current[newCam].onPlayerTimeUpdate(
|
|
|
|
(timestamp: number) => {
|
|
|
|
setCurrentTime(timestamp);
|
|
|
|
|
|
|
|
allCameras.forEach((cam) => {
|
|
|
|
if (cam != newCam) {
|
|
|
|
videoPlayersRef.current[cam]?.scrubToTimestamp(
|
|
|
|
Math.floor(timestamp),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
setMainCamera(newCam);
|
|
|
|
},
|
|
|
|
[allCameras, currentTime, mainCamera],
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div ref={contentRef} className="relative size-full">
|
|
|
|
<Button
|
|
|
|
className="absolute top-0 left-0 rounded-lg"
|
|
|
|
onClick={() => navigate(-1)}
|
|
|
|
>
|
|
|
|
<IoMdArrowRoundBack className="size-5 mr-[10px]" />
|
|
|
|
Back
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
<div className="absolute h-32 left-2 right-28 bottom-4 flex justify-center gap-1">
|
|
|
|
{allCameras.map((cam) => {
|
|
|
|
if (cam == mainCamera) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
key={cam}
|
|
|
|
className="fixed left-96 right-96 top-[40%] -translate-y-[50%]"
|
|
|
|
>
|
|
|
|
<DynamicVideoPlayer
|
|
|
|
camera={cam}
|
|
|
|
timeRange={timeRange.ranges[selectedRangeIdx]}
|
|
|
|
cameraPreviews={allPreviews ?? []}
|
|
|
|
onControllerReady={(controller) => {
|
|
|
|
videoPlayersRef.current[cam] = controller;
|
|
|
|
setPlayerReady(true);
|
|
|
|
controller.onPlayerTimeUpdate((timestamp: number) => {
|
|
|
|
setCurrentTime(timestamp);
|
|
|
|
|
|
|
|
allCameras.forEach((otherCam) => {
|
|
|
|
if (cam != otherCam) {
|
|
|
|
videoPlayersRef.current[otherCam]?.scrubToTimestamp(
|
|
|
|
Math.floor(timestamp),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
controller.seekToTimestamp(startTime, true);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div key={cam} className="aspect-video flex items-center">
|
|
|
|
<DynamicVideoPlayer
|
|
|
|
className="size-full"
|
|
|
|
camera={cam}
|
|
|
|
timeRange={timeRange.ranges[selectedRangeIdx]}
|
|
|
|
cameraPreviews={allPreviews ?? []}
|
|
|
|
previewOnly
|
|
|
|
onControllerReady={(controller) => {
|
|
|
|
videoPlayersRef.current[cam] = controller;
|
|
|
|
setPlayerReady(true);
|
|
|
|
controller.scrubToTimestamp(startTime);
|
|
|
|
}}
|
|
|
|
onClick={() => onSelectCamera(cam)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="absolute overflow-hidden w-56 inset-y-0 right-0">
|
|
|
|
<EventReviewTimeline
|
|
|
|
segmentDuration={30}
|
|
|
|
timestampSpread={15}
|
|
|
|
timelineStart={timeRange.end}
|
|
|
|
timelineEnd={timeRange.start}
|
|
|
|
showHandlebar
|
|
|
|
handlebarTime={currentTime}
|
|
|
|
setHandlebarTime={setCurrentTime}
|
|
|
|
events={reviewItems}
|
|
|
|
severityType={severity}
|
|
|
|
contentRef={contentRef}
|
|
|
|
onHandlebarDraggingChange={(scrubbing) => setScrubbing(scrubbing)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
type MobileRecordingViewProps = {
|
2024-02-23 01:03:34 +01:00
|
|
|
selectedReview: ReviewSegment;
|
|
|
|
reviewItems: ReviewSegment[];
|
|
|
|
relevantPreviews?: Preview[];
|
|
|
|
};
|
2024-03-05 13:03:10 +01:00
|
|
|
export function MobileRecordingView({
|
2024-02-23 01:03:34 +01:00
|
|
|
selectedReview,
|
|
|
|
reviewItems,
|
|
|
|
relevantPreviews,
|
2024-03-05 13:03:10 +01:00
|
|
|
}: MobileRecordingViewProps) {
|
2024-02-23 01:03:34 +01:00
|
|
|
const navigate = useNavigate();
|
|
|
|
const contentRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
|
2024-02-26 17:04:56 +01:00
|
|
|
// controller state
|
|
|
|
|
|
|
|
const [playerReady, setPlayerReady] = useState(false);
|
|
|
|
const controllerRef = useRef<DynamicVideoController | undefined>(undefined);
|
|
|
|
|
2024-02-23 01:03:34 +01:00
|
|
|
// timeline time
|
|
|
|
|
|
|
|
const timeRange = useMemo(
|
2024-03-04 05:21:30 +01:00
|
|
|
() => getChunkedTimeDay(selectedReview.start_time),
|
2024-02-28 23:23:56 +01:00
|
|
|
[selectedReview],
|
2024-02-23 01:03:34 +01:00
|
|
|
);
|
|
|
|
const [selectedRangeIdx, setSelectedRangeIdx] = useState(
|
|
|
|
timeRange.ranges.findIndex((chunk) => {
|
|
|
|
return (
|
|
|
|
chunk.start <= selectedReview.start_time &&
|
|
|
|
chunk.end >= selectedReview.start_time
|
|
|
|
);
|
2024-02-28 23:23:56 +01:00
|
|
|
}),
|
2024-02-23 01:03:34 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// move to next clip
|
|
|
|
useEffect(() => {
|
|
|
|
if (!controllerRef.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-26 17:04:56 +01:00
|
|
|
controllerRef.current.onClipChangedEvent((dir) => {
|
|
|
|
if (dir == "forward" && selectedRangeIdx < timeRange.ranges.length - 1) {
|
2024-02-23 01:03:34 +01:00
|
|
|
setSelectedRangeIdx(selectedRangeIdx + 1);
|
2024-02-26 17:04:56 +01:00
|
|
|
} else if (selectedRangeIdx > 0) {
|
|
|
|
setSelectedRangeIdx(selectedRangeIdx - 1);
|
|
|
|
}
|
|
|
|
});
|
2024-02-28 23:23:56 +01:00
|
|
|
}, [playerReady, selectedRangeIdx, timeRange]);
|
2024-02-23 01:03:34 +01:00
|
|
|
|
|
|
|
// scrubbing and timeline state
|
|
|
|
|
|
|
|
const [scrubbing, setScrubbing] = useState(false);
|
|
|
|
const [currentTime, setCurrentTime] = useState<number>(
|
2024-02-28 23:23:56 +01:00
|
|
|
selectedReview?.start_time || Date.now() / 1000,
|
2024-02-23 01:03:34 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (scrubbing) {
|
|
|
|
controllerRef.current?.scrubToTimestamp(currentTime);
|
|
|
|
}
|
2024-02-26 17:04:56 +01:00
|
|
|
}, [currentTime, scrubbing]);
|
2024-02-23 01:03:34 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!scrubbing) {
|
|
|
|
controllerRef.current?.seekToTimestamp(currentTime, true);
|
|
|
|
}
|
2024-02-28 23:23:56 +01:00
|
|
|
|
|
|
|
// we only want to seek when user stops scrubbing
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2024-02-26 17:04:56 +01:00
|
|
|
}, [scrubbing]);
|
2024-02-23 01:03:34 +01:00
|
|
|
|
|
|
|
return (
|
2024-03-04 14:35:10 +01:00
|
|
|
<div ref={contentRef} className="flex flex-col relative w-full h-full">
|
2024-03-05 13:03:10 +01:00
|
|
|
<Button className="rounded-lg" onClick={() => navigate(-1)}>
|
2024-02-28 04:40:57 +01:00
|
|
|
<IoMdArrowRoundBack className="size-5 mr-[10px]" />
|
2024-02-23 01:03:34 +01:00
|
|
|
Back
|
|
|
|
</Button>
|
|
|
|
|
2024-03-05 13:03:10 +01:00
|
|
|
<div>
|
2024-02-23 01:03:34 +01:00
|
|
|
<DynamicVideoPlayer
|
|
|
|
camera={selectedReview.camera}
|
|
|
|
timeRange={timeRange.ranges[selectedRangeIdx]}
|
|
|
|
cameraPreviews={relevantPreviews || []}
|
|
|
|
onControllerReady={(controller) => {
|
|
|
|
controllerRef.current = controller;
|
2024-02-26 17:04:56 +01:00
|
|
|
setPlayerReady(true);
|
2024-02-23 01:03:34 +01:00
|
|
|
controllerRef.current.onPlayerTimeUpdate((timestamp: number) => {
|
|
|
|
setCurrentTime(timestamp);
|
|
|
|
});
|
|
|
|
|
|
|
|
controllerRef.current?.seekToTimestamp(
|
|
|
|
selectedReview.start_time,
|
2024-02-28 23:23:56 +01:00
|
|
|
true,
|
2024-02-23 01:03:34 +01:00
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2024-03-05 13:03:10 +01:00
|
|
|
<div className="flex-grow overflow-hidden">
|
2024-02-23 01:03:34 +01:00
|
|
|
<EventReviewTimeline
|
|
|
|
segmentDuration={30}
|
|
|
|
timestampSpread={15}
|
|
|
|
timelineStart={timeRange.end}
|
|
|
|
timelineEnd={timeRange.start}
|
|
|
|
showHandlebar
|
|
|
|
handlebarTime={currentTime}
|
|
|
|
setHandlebarTime={setCurrentTime}
|
|
|
|
events={reviewItems}
|
|
|
|
severityType={selectedReview.severity}
|
|
|
|
contentRef={contentRef}
|
|
|
|
onHandlebarDraggingChange={(scrubbing) => setScrubbing(scrubbing)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|