Cleanup to use new preview video player in dynamic player (#10335)

* Cleanup to use new preview video player

* Make it so motion persists the selected time

* Don't jump when player first starts

* Get autoplay working right
This commit is contained in:
Nicolas Mowen 2024-03-08 16:24:12 -07:00 committed by GitHub
parent 05a66ce90d
commit 086eb37ece
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 172 additions and 245 deletions

View File

@ -1,11 +1,4 @@
import { import { useCallback, useEffect, useMemo, useRef, useState } from "react";
MutableRefObject,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import VideoPlayer from "./VideoPlayer"; import VideoPlayer from "./VideoPlayer";
import Player from "video.js/dist/types/player"; import Player from "video.js/dist/types/player";
import TimelineEventOverlay from "../overlay/TimelineDataOverlay"; import TimelineEventOverlay from "../overlay/TimelineDataOverlay";
@ -16,6 +9,9 @@ import useKeyboardListener from "@/hooks/use-keyboard-listener";
import { Recording } from "@/types/record"; import { Recording } from "@/types/record";
import { Preview } from "@/types/preview"; import { Preview } from "@/types/preview";
import { DynamicPlayback } from "@/types/playback"; import { DynamicPlayback } from "@/types/playback";
import PreviewVideoPlayer, {
PreviewVideoController,
} from "./PreviewVideoPlayer";
type PlayerMode = "playback" | "scrubbing"; type PlayerMode = "playback" | "scrubbing";
@ -28,7 +24,6 @@ type DynamicVideoPlayerProps = {
timeRange: { start: number; end: number }; timeRange: { start: number; end: number };
cameraPreviews: Preview[]; cameraPreviews: Preview[];
previewOnly?: boolean; previewOnly?: boolean;
preloadRecordings: boolean;
onControllerReady: (controller: DynamicVideoController) => void; onControllerReady: (controller: DynamicVideoController) => void;
onClick?: () => void; onClick?: () => void;
}; };
@ -38,7 +33,6 @@ export default function DynamicVideoPlayer({
timeRange, timeRange,
cameraPreviews, cameraPreviews,
previewOnly = false, previewOnly = false,
preloadRecordings = true,
onControllerReady, onControllerReady,
onClick, onClick,
}: DynamicVideoPlayerProps) { }: DynamicVideoPlayerProps) {
@ -51,35 +45,36 @@ export default function DynamicVideoPlayer({
); );
// playback behavior // playback behavior
const tallVideo = useMemo(() => { const wideVideo = useMemo(() => {
if (!config) { if (!config) {
return false; return false;
} }
return ( return (
config.cameras[camera].detect.width / config.cameras[camera].detect.width /
config.cameras[camera].detect.height < config.cameras[camera].detect.height >
1 1.7
); );
}, [camera, config]); }, [camera, config]);
// controlling playback // controlling playback
const [playerRef, setPlayerRef] = useState<Player | undefined>(undefined); const playerRef = useRef<Player | null>(null);
const previewRef = useRef<HTMLVideoElement | null>(null); const [previewController, setPreviewController] =
useState<PreviewVideoController | null>(null);
const [isScrubbing, setIsScrubbing] = useState(previewOnly); const [isScrubbing, setIsScrubbing] = useState(previewOnly);
const [focusedItem, setFocusedItem] = useState<Timeline | undefined>( const [focusedItem, setFocusedItem] = useState<Timeline | undefined>(
undefined, undefined,
); );
const controller = useMemo(() => { const controller = useMemo(() => {
if (!config || !playerRef || !previewRef.current) { if (!config || !playerRef.current || !previewController) {
return undefined; return undefined;
} }
return new DynamicVideoController( return new DynamicVideoController(
camera, camera,
playerRef, playerRef.current,
previewRef, previewController,
(config.cameras[camera]?.detect?.annotation_offset || 0) / 1000, (config.cameras[camera]?.detect?.annotation_offset || 0) / 1000,
previewOnly ? "scrubbing" : "playback", previewOnly ? "scrubbing" : "playback",
setIsScrubbing, setIsScrubbing,
@ -87,7 +82,7 @@ export default function DynamicVideoPlayer({
); );
// we only want to fire once when players are ready // we only want to fire once when players are ready
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [camera, config, playerRef, previewRef]); }, [camera, config, playerRef.current, previewController]);
useEffect(() => { useEffect(() => {
if (!controller) { if (!controller) {
@ -105,7 +100,7 @@ export default function DynamicVideoPlayer({
const [initPreviewOnly, setInitPreviewOnly] = useState(previewOnly); const [initPreviewOnly, setInitPreviewOnly] = useState(previewOnly);
useEffect(() => { useEffect(() => {
if (!controller || !playerRef) { if (!controller || !playerRef.current) {
return; return;
} }
@ -113,10 +108,8 @@ export default function DynamicVideoPlayer({
return; return;
} }
if (previewOnly) { if (!previewOnly) {
playerRef.autoplay(false); controller.seekToTimestamp(playerRef.current.currentTime() || 0, true);
} else {
controller.seekToTimestamp(playerRef.currentTime() || 0, true);
} }
setInitPreviewOnly(previewOnly); setInitPreviewOnly(previewOnly);
@ -124,48 +117,52 @@ export default function DynamicVideoPlayer({
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [controller, previewOnly]); }, [controller, previewOnly]);
const [hasRecordingAtTime, setHasRecordingAtTime] = useState(true);
// keyboard control // keyboard control
const onKeyboardShortcut = useCallback( const onKeyboardShortcut = useCallback(
(key: string, down: boolean, repeat: boolean) => { (key: string, down: boolean, repeat: boolean) => {
if (!playerRef.current || previewOnly) {
return;
}
switch (key) { switch (key) {
case "ArrowLeft": case "ArrowLeft":
if (down) { if (down) {
const currentTime = playerRef?.currentTime(); const currentTime = playerRef.current.currentTime();
if (currentTime) { if (currentTime) {
playerRef?.currentTime(Math.max(0, currentTime - 5)); playerRef.current.currentTime(Math.max(0, currentTime - 5));
} }
} }
break; break;
case "ArrowRight": case "ArrowRight":
if (down) { if (down) {
const currentTime = playerRef?.currentTime(); const currentTime = playerRef.current.currentTime();
if (currentTime) { if (currentTime) {
playerRef?.currentTime(currentTime + 5); playerRef.current.currentTime(currentTime + 5);
} }
} }
break; break;
case "m": case "m":
if (down && !repeat && playerRef) { if (down && !repeat && playerRef) {
playerRef.muted(!playerRef.muted()); playerRef.current.muted(!playerRef.current.muted());
} }
break; break;
case " ": case " ":
if (down && playerRef) { if (down && playerRef) {
if (playerRef.paused()) { if (playerRef.current.paused()) {
playerRef.play(); playerRef.current.play();
} else { } else {
playerRef.pause(); playerRef.current.pause();
} }
} }
break; break;
} }
}, },
[playerRef], // only update when preview only changes
// eslint-disable-next-line react-hooks/exhaustive-deps
[playerRef.current, previewOnly],
); );
useKeyboardListener( useKeyboardListener(
["ArrowLeft", "ArrowRight", "m", " "], ["ArrowLeft", "ArrowRight", "m", " "],
@ -188,35 +185,6 @@ export default function DynamicVideoPlayer({
// we only want to calculate this once // we only want to calculate this once
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, []); }, []);
const initialPreview = useMemo(() => {
return cameraPreviews.find(
(preview) =>
preview.camera == camera &&
Math.round(preview.start) >= timeRange.start &&
Math.floor(preview.end) <= timeRange.end,
);
// we only want to calculate this once
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const [currentPreview, setCurrentPreview] = useState(initialPreview);
const onPreviewSeeked = useCallback(() => {
if (!controller) {
return;
}
controller.finishedSeeking();
if (currentPreview && previewOnly && previewRef.current && onClick) {
setHasRecordingAtTime(
controller.hasRecordingAtTime(
currentPreview.start + previewRef.current.currentTime,
),
);
}
}, [controller, currentPreview, onClick, previewOnly]);
// state of playback player // state of playback player
@ -246,23 +214,9 @@ export default function DynamicVideoPlayer({
",", ",",
)}/master.m3u8`; )}/master.m3u8`;
const preview = cameraPreviews.find(
(preview) =>
preview.camera == camera &&
Math.round(preview.start) >= timeRange.start &&
Math.floor(preview.end) <= timeRange.end,
);
setCurrentPreview(preview);
if (preview && previewRef.current) {
previewRef.current.load();
}
controller.newPlayback({ controller.newPlayback({
recordings: recordings ?? [], recordings: recordings ?? [],
playbackUri, playbackUri,
preview,
timeRange,
}); });
// we only want this to change when recordings update // we only want this to change when recordings update
@ -271,15 +225,12 @@ export default function DynamicVideoPlayer({
return ( return (
<div <div
className={`relative ${className ?? ""} ${onClick ? (hasRecordingAtTime ? "cursor-pointer" : "") : ""}`} className={`relative ${className ?? ""} ${onClick ? "cursor-pointer" : ""}`}
onClick={onClick} onClick={onClick}
> >
{preloadRecordings && (
<div <div
className={`w-full relative ${ className={`w-full relative ${
previewOnly || (currentPreview != undefined && isScrubbing) previewOnly || isScrubbing ? "hidden" : "visible"
? "hidden"
: "visible"
}`} }`}
> >
<VideoPlayer <VideoPlayer
@ -287,7 +238,7 @@ export default function DynamicVideoPlayer({
preload: "auto", preload: "auto",
autoplay: !previewOnly, autoplay: !previewOnly,
sources: [initialPlaybackSource], sources: [initialPlaybackSource],
aspectRatio: tallVideo ? "16:9" : undefined, aspectRatio: wideVideo ? undefined : "16:9",
controlBar: { controlBar: {
remainingTimeDisplay: false, remainingTimeDisplay: false,
progressControl: { progressControl: {
@ -297,10 +248,10 @@ export default function DynamicVideoPlayer({
}} }}
seekOptions={{ forward: 10, backward: 5 }} seekOptions={{ forward: 10, backward: 5 }}
onReady={(player) => { onReady={(player) => {
setPlayerRef(player); playerRef.current = player;
}} }}
onDispose={() => { onDispose={() => {
setPlayerRef(undefined); playerRef.current = null;
}} }}
> >
{config && focusedItem && ( {config && focusedItem && (
@ -311,22 +262,16 @@ export default function DynamicVideoPlayer({
)} )}
</VideoPlayer> </VideoPlayer>
</div> </div>
)} <PreviewVideoPlayer
<video className={`${isScrubbing ? "visible" : "hidden"} ${className ?? ""}`}
ref={previewRef} camera={camera}
className={`size-full rounded-2xl ${currentPreview != undefined && (previewOnly || isScrubbing) ? "visible" : "hidden"} ${tallVideo ? "aspect-tall" : ""} bg-black`} timeRange={timeRange}
preload="auto" cameraPreviews={cameraPreviews}
autoPlay onControllerReady={(previewController) => {
playsInline setPreviewController(previewController);
muted }}
disableRemotePlayback onClick={onClick}
onSeeked={onPreviewSeeked} />
onLoadedData={() => controller?.previewReady()}
>
{currentPreview != undefined && (
<source src={currentPreview.src} type={currentPreview.type} />
)}
</video>
</div> </div>
); );
} }
@ -334,22 +279,17 @@ export default function DynamicVideoPlayer({
export class DynamicVideoController { export class DynamicVideoController {
// main state // main state
public camera = ""; public camera = "";
private playerRef: Player; private playerController: Player;
private previewRef: MutableRefObject<HTMLVideoElement | null>; private previewController: PreviewVideoController;
private setScrubbing: (isScrubbing: boolean) => void; private setScrubbing: (isScrubbing: boolean) => void;
private setFocusedItem: (timeline: Timeline) => void; private setFocusedItem: (timeline: Timeline) => void;
private playerMode: PlayerMode = "playback"; private playerMode: PlayerMode = "playback";
private timeRange: { start: number; end: number } | undefined = undefined;
// playback // playback
private recordings: Recording[] = []; private recordings: Recording[] = [];
private annotationOffset: number; private annotationOffset: number;
private timeToStart: number | undefined = undefined; private timeToStart: number | undefined = undefined;
private canPlay: boolean = false;
// preview
private preview: Preview | undefined = undefined;
private timeToSeek: number | undefined = undefined;
private seeking = false;
// listeners // listeners
private playerProgressListener: (() => void) | null = null; private playerProgressListener: (() => void) | null = null;
@ -358,16 +298,16 @@ export class DynamicVideoController {
constructor( constructor(
camera: string, camera: string,
playerRef: Player, playerController: Player,
previewRef: MutableRefObject<HTMLVideoElement | null>, previewController: PreviewVideoController,
annotationOffset: number, annotationOffset: number,
defaultMode: PlayerMode, defaultMode: PlayerMode,
setScrubbing: (isScrubbing: boolean) => void, setScrubbing: (isScrubbing: boolean) => void,
setFocusedItem: (timeline: Timeline) => void, setFocusedItem: (timeline: Timeline) => void,
) { ) {
this.camera = camera; this.camera = camera;
this.playerRef = playerRef; this.playerController = playerController;
this.previewRef = previewRef; this.previewController = previewController;
this.annotationOffset = annotationOffset; this.annotationOffset = annotationOffset;
this.playerMode = defaultMode; this.playerMode = defaultMode;
this.setScrubbing = setScrubbing; this.setScrubbing = setScrubbing;
@ -376,33 +316,26 @@ export class DynamicVideoController {
newPlayback(newPlayback: DynamicPlayback) { newPlayback(newPlayback: DynamicPlayback) {
this.recordings = newPlayback.recordings; this.recordings = newPlayback.recordings;
this.playerController.src({
this.playerRef.src({
src: newPlayback.playbackUri, src: newPlayback.playbackUri,
type: "application/vnd.apple.mpegurl", type: "application/vnd.apple.mpegurl",
}); });
this.canPlay = false;
if (this.timeToStart) { if (this.timeToStart) {
this.seekToTimestamp(this.timeToStart); this.seekToTimestamp(this.timeToStart);
this.timeToStart = undefined; this.timeToStart = undefined;
} }
this.preview = newPlayback.preview;
if (this.preview) {
this.seeking = false;
this.timeToSeek = undefined;
} }
this.timeRange = newPlayback.timeRange; autoPlay(play: boolean) {
this.playerController.autoplay(play);
} }
seekToTimestamp(time: number, play: boolean = false) { seekToTimestamp(time: number, play: boolean = false) {
if (this.playerMode != "playback") { if (this.playerMode != "playback") {
this.playerMode = "playback"; this.playerMode = "playback";
this.setScrubbing(false); this.setScrubbing(false);
this.timeToSeek = undefined;
this.seeking = false;
} }
if (this.recordings.length == 0) { if (this.recordings.length == 0) {
@ -425,29 +358,17 @@ export class DynamicVideoController {
segment.end_time - segment.start_time - (segment.end_time - time); segment.end_time - segment.start_time - (segment.end_time - time);
return true; return true;
}); });
this.playerRef.currentTime(seekSeconds); this.playerController.currentTime(seekSeconds);
if (play) { if (play) {
this.playerRef.play(); this.playerController.play();
} else { } else {
this.playerRef.pause(); this.playerController.pause();
}
}
onCanPlay(listener: (() => void) | null) {
if (listener) {
this.canPlayListener = listener;
this.playerRef.on("canplay", this.canPlayListener);
} else {
if (this.canPlayListener) {
this.playerRef.off("canplay", this.canPlayListener);
this.canPlayListener = null;
}
} }
} }
seekToTimelineItem(timeline: Timeline) { seekToTimelineItem(timeline: Timeline) {
this.playerRef.pause(); this.playerController.pause();
this.seekToTimestamp(timeline.timestamp + this.annotationOffset); this.seekToTimestamp(timeline.timestamp + this.annotationOffset);
this.setFocusedItem(timeline); this.setFocusedItem(timeline);
} }
@ -470,79 +391,61 @@ export class DynamicVideoController {
return timestamp; return timestamp;
} }
onPlayerTimeUpdate(listener: ((timestamp: number) => void) | undefined) { onCanPlay(listener: (() => void) | null) {
if (this.playerProgressListener) { if (this.canPlayListener) {
this.playerRef.off("timeupdate", this.playerProgressListener); this.playerController.off("canplay", this.canPlayListener);
this.canPlayListener = null;
} }
if (listener) { if (listener) {
this.playerProgressListener = () => this.canPlayListener = () => {
listener(this.getProgress(this.playerRef.currentTime() || 0)); this.canPlay = true;
this.playerRef.on("timeupdate", this.playerProgressListener); listener();
};
this.playerController.on("canplay", this.canPlayListener);
} }
} }
onClipChangedEvent(listener: ((dir: "forward") => void) | undefined) { onPlayerTimeUpdate(listener: ((timestamp: number) => void) | null) {
if (this.playerProgressListener) {
this.playerController.off("timeupdate", this.playerProgressListener);
this.playerProgressListener = null;
}
if (listener) {
this.playerProgressListener = () => {
if (this.canPlay) {
listener(this.getProgress(this.playerController.currentTime() || 0));
}
};
this.playerController.on("timeupdate", this.playerProgressListener);
}
}
onClipChangedEvent(listener: ((dir: "forward") => void) | null) {
if (this.playerEndedListener) { if (this.playerEndedListener) {
this.playerRef.off("ended", this.playerEndedListener); this.playerController.off("ended", this.playerEndedListener);
this.playerEndedListener = null;
} }
if (listener) { if (listener) {
this.playerEndedListener = () => listener("forward"); this.playerEndedListener = () => listener("forward");
this.playerRef.on("ended", this.playerEndedListener); this.playerController.on("ended", this.playerEndedListener);
} }
} }
scrubToTimestamp(time: number) { scrubToTimestamp(time: number, saveIfNotReady: boolean = false) {
if (!this.preview || !this.timeRange) { const scrubResult = this.previewController.scrubToTimestamp(time);
return;
if (!scrubResult && saveIfNotReady) {
this.previewController.setNewPreviewStartTime(time);
} }
if (time < this.preview.start || time > this.preview.end) { if (scrubResult && this.playerMode != "scrubbing") {
return;
}
if (this.playerMode != "scrubbing") {
this.playerMode = "scrubbing"; this.playerMode = "scrubbing";
this.playerRef.pause(); this.playerController.pause();
this.setScrubbing(true); this.setScrubbing(true);
} }
if (this.seeking) {
this.timeToSeek = time;
} else {
if (this.previewRef.current) {
this.previewRef.current.currentTime = Math.max(
0,
time - this.preview.start,
);
this.seeking = true;
}
}
}
finishedSeeking() {
if (
!this.previewRef.current ||
!this.preview ||
this.playerMode == "playback"
) {
return;
}
if (
this.timeToSeek &&
this.timeToSeek != this.previewRef.current?.currentTime
) {
this.previewRef.current.currentTime =
this.timeToSeek - this.preview.start;
} else {
this.seeking = false;
}
}
previewReady() {
this.previewRef.current?.pause();
} }
hasRecordingAtTime(time: number): boolean { hasRecordingAtTime(time: number): boolean {

View File

@ -16,6 +16,7 @@ type PreviewVideoPlayerProps = {
camera: string; camera: string;
timeRange: { start: number; end: number }; timeRange: { start: number; end: number };
cameraPreviews: Preview[]; cameraPreviews: Preview[];
startTime?: number;
onControllerReady: (controller: PreviewVideoController) => void; onControllerReady: (controller: PreviewVideoController) => void;
onClick?: () => void; onClick?: () => void;
}; };
@ -24,6 +25,7 @@ export default function PreviewVideoPlayer({
camera, camera,
timeRange, timeRange,
cameraPreviews, cameraPreviews,
startTime,
onControllerReady, onControllerReady,
onClick, onClick,
}: PreviewVideoPlayerProps) { }: PreviewVideoPlayerProps) {
@ -128,6 +130,10 @@ export default function PreviewVideoPlayer({
} else { } else {
previewRef.current?.pause(); previewRef.current?.pause();
} }
if (previewRef.current && startTime && currentPreview) {
previewRef.current.currentTime = startTime - currentPreview.start;
}
}} }}
> >
{currentPreview != undefined && ( {currentPreview != undefined && (
@ -164,13 +170,13 @@ export class PreviewVideoController {
this.timeRange = newPlayback.timeRange; this.timeRange = newPlayback.timeRange;
} }
scrubToTimestamp(time: number) { scrubToTimestamp(time: number): boolean {
if (!this.preview || !this.timeRange) { if (!this.preview || !this.timeRange) {
return; return false;
} }
if (time < this.preview.start || time > this.preview.end) { if (time < this.preview.start || time > this.preview.end) {
return; return false;
} }
if (this.seeking) { if (this.seeking) {
@ -184,6 +190,8 @@ export class PreviewVideoController {
this.seeking = true; this.seeking = true;
} }
} }
return true;
} }
setNewPreviewStartTime(time: number) { setNewPreviewStartTime(time: number) {

View File

@ -31,6 +31,7 @@ export default function Events() {
"alert", "alert",
); );
const [selectedReviewId, setSelectedReviewId] = useOverlayState("review"); const [selectedReviewId, setSelectedReviewId] = useOverlayState("review");
const [startTime, setStartTime] = useState<number>();
// review filter // review filter
@ -221,11 +222,13 @@ export default function Events() {
if (selectedReviewId.startsWith("motion")) { if (selectedReviewId.startsWith("motion")) {
const motionData = selectedReviewId.split(","); const motionData = selectedReviewId.split(",");
const motionStart = parseFloat(motionData[2]);
setStartTime(motionStart);
// format is motion,camera,start_time // format is motion,camera,start_time
return { return {
camera: motionData[1], camera: motionData[1],
severity: "significant_motion" as ReviewSeverity, severity: "significant_motion" as ReviewSeverity,
start_time: parseFloat(motionData[2]), start_time: motionStart,
allCameras: allCameras, allCameras: allCameras,
cameraSegments: reviews.filter((seg) => cameraSegments: reviews.filter((seg) =>
allCameras.includes(seg.camera), allCameras.includes(seg.camera),
@ -292,6 +295,7 @@ export default function Events() {
timeRange={selectedTimeRange} timeRange={selectedTimeRange}
filter={reviewFilter} filter={reviewFilter}
severity={severity ?? "alert"} severity={severity ?? "alert"}
startTime={startTime}
setSeverity={setSeverity} setSeverity={setSeverity}
markItemAsReviewed={markItemAsReviewed} markItemAsReviewed={markItemAsReviewed}
onOpenReview={setSelectedReviewId} onOpenReview={setSelectedReviewId}

View File

@ -4,8 +4,6 @@ import { Recording } from "./record";
export type DynamicPlayback = { export type DynamicPlayback = {
recordings: Recording[]; recordings: Recording[];
playbackUri: string; playbackUri: string;
preview: Preview | undefined;
timeRange: { end: number; start: number };
}; };
export type PreviewPlayback = { export type PreviewPlayback = {

View File

@ -44,6 +44,7 @@ type EventViewProps = {
timeRange: { before: number; after: number }; timeRange: { before: number; after: number };
filter?: ReviewFilter; filter?: ReviewFilter;
severity: ReviewSeverity; severity: ReviewSeverity;
startTime?: number;
setSeverity: (severity: ReviewSeverity) => void; setSeverity: (severity: ReviewSeverity) => void;
markItemAsReviewed: (review: ReviewSegment) => void; markItemAsReviewed: (review: ReviewSegment) => void;
onOpenReview: (reviewId: string) => void; onOpenReview: (reviewId: string) => void;
@ -57,6 +58,7 @@ export default function EventView({
timeRange, timeRange,
filter, filter,
severity, severity,
startTime,
setSeverity, setSeverity,
markItemAsReviewed, markItemAsReviewed,
onOpenReview, onOpenReview,
@ -262,6 +264,7 @@ export default function EventView({
reviewItems={reviewItems} reviewItems={reviewItems}
relevantPreviews={relevantPreviews} relevantPreviews={relevantPreviews}
timeRange={timeRange} timeRange={timeRange}
startTime={startTime}
filter={filter} filter={filter}
onSelectReview={onSelectReview} onSelectReview={onSelectReview}
/> />
@ -518,6 +521,7 @@ type MotionReviewProps = {
}; };
relevantPreviews?: Preview[]; relevantPreviews?: Preview[];
timeRange: { before: number; after: number }; timeRange: { before: number; after: number };
startTime?: number;
filter?: ReviewFilter; filter?: ReviewFilter;
onSelectReview: (data: string, ctrl: boolean) => void; onSelectReview: (data: string, ctrl: boolean) => void;
}; };
@ -526,6 +530,7 @@ function MotionReview({
reviewItems, reviewItems,
relevantPreviews, relevantPreviews,
timeRange, timeRange,
startTime,
filter, filter,
onSelectReview, onSelectReview,
}: MotionReviewProps) { }: MotionReviewProps) {
@ -579,11 +584,21 @@ function MotionReview({
[lastFullHour, timeRange], [lastFullHour, timeRange],
); );
const [selectedRangeIdx, setSelectedRangeIdx] = useState( const initialIndex = useMemo(() => {
timeRangeSegments.ranges.length - 1, if (!startTime) {
return timeRangeSegments.ranges.length - 1;
}
return timeRangeSegments.ranges.findIndex(
(seg) => seg.start <= startTime && seg.end >= startTime,
); );
// only render once
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const [selectedRangeIdx, setSelectedRangeIdx] = useState(initialIndex);
const [currentTime, setCurrentTime] = useState<number>( const [currentTime, setCurrentTime] = useState<number>(
timeRangeSegments.ranges[selectedRangeIdx].start, startTime ?? timeRangeSegments.ranges[selectedRangeIdx].start,
); );
const currentTimeRange = useMemo( const currentTimeRange = useMemo(
() => timeRangeSegments.ranges[selectedRangeIdx], () => timeRangeSegments.ranges[selectedRangeIdx],
@ -642,6 +657,7 @@ function MotionReview({
className={`${grow}`} className={`${grow}`}
camera={camera.name} camera={camera.name}
timeRange={currentTimeRange} timeRange={currentTimeRange}
startTime={startTime}
cameraPreviews={relevantPreviews || []} cameraPreviews={relevantPreviews || []}
onControllerReady={(controller) => { onControllerReady={(controller) => {
videoPlayersRef.current[camera.name] = controller; videoPlayersRef.current[camera.name] = controller;

View File

@ -35,7 +35,6 @@ export function DesktopRecordingView({
// controller state // controller state
const [playerReady, setPlayerReady] = useState(false);
const [mainCamera, setMainCamera] = useState(startCamera); const [mainCamera, setMainCamera] = useState(startCamera);
const videoPlayersRef = useRef<{ [camera: string]: DynamicVideoController }>( const videoPlayersRef = useRef<{ [camera: string]: DynamicVideoController }>(
{}, {},
@ -74,7 +73,9 @@ export function DesktopRecordingView({
} }
}); });
} }
}, [selectedRangeIdx, timeRange, videoPlayersRef, playerReady, mainCamera]); // we only want to fire once when players are ready
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedRangeIdx, timeRange, videoPlayersRef.current, mainCamera]);
// scrubbing and timeline state // scrubbing and timeline state
@ -116,9 +117,11 @@ export function DesktopRecordingView({
(newCam: string) => { (newCam: string) => {
const lastController = videoPlayersRef.current[mainCamera]; const lastController = videoPlayersRef.current[mainCamera];
const newController = videoPlayersRef.current[newCam]; const newController = videoPlayersRef.current[newCam];
lastController.onPlayerTimeUpdate(undefined); lastController.onPlayerTimeUpdate(null);
lastController.onClipChangedEvent(undefined); lastController.onClipChangedEvent(null);
lastController.autoPlay(false);
lastController.scrubToTimestamp(currentTime); lastController.scrubToTimestamp(currentTime);
newController.autoPlay(true);
newController.onCanPlay(() => { newController.onCanPlay(() => {
newController.seekToTimestamp(currentTime, true); newController.seekToTimestamp(currentTime, true);
newController.onCanPlay(null); newController.onCanPlay(null);
@ -176,10 +179,8 @@ export function DesktopRecordingView({
camera={cam} camera={cam}
timeRange={currentTimeRange} timeRange={currentTimeRange}
cameraPreviews={allPreviews ?? []} cameraPreviews={allPreviews ?? []}
preloadRecordings
onControllerReady={(controller) => { onControllerReady={(controller) => {
videoPlayersRef.current[cam] = controller; videoPlayersRef.current[cam] = controller;
setPlayerReady(true);
controller.onPlayerTimeUpdate((timestamp: number) => { controller.onPlayerTimeUpdate((timestamp: number) => {
setCurrentTime(timestamp); setCurrentTime(timestamp);
@ -210,11 +211,9 @@ export function DesktopRecordingView({
timeRange={currentTimeRange} timeRange={currentTimeRange}
cameraPreviews={allPreviews ?? []} cameraPreviews={allPreviews ?? []}
previewOnly previewOnly
preloadRecordings
onControllerReady={(controller) => { onControllerReady={(controller) => {
videoPlayersRef.current[cam] = controller; videoPlayersRef.current[cam] = controller;
setPlayerReady(true); controller.scrubToTimestamp(startTime, true);
controller.scrubToTimestamp(startTime);
}} }}
onClick={() => onSelectCamera(cam)} onClick={() => onSelectCamera(cam)}
/> />
@ -372,7 +371,6 @@ export function MobileRecordingView({
camera={startCamera} camera={startCamera}
timeRange={currentTimeRange} timeRange={currentTimeRange}
cameraPreviews={relevantPreviews || []} cameraPreviews={relevantPreviews || []}
preloadRecordings
onControllerReady={(controller) => { onControllerReady={(controller) => {
controllerRef.current = controller; controllerRef.current = controller;
setPlayerReady(true); setPlayerReady(true);