Automatically skip to next preview / clip when watching full recordings (#10055)

* Fix useEffect and try to load next clip for preview

* Get scrubbing to next preview working

* Handle skipping to next preview
This commit is contained in:
Nicolas Mowen 2024-02-26 09:04:56 -07:00 committed by GitHub
parent 41194966c7
commit 4061be602d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 57 additions and 24 deletions

View File

@ -199,8 +199,6 @@ export default function DynamicVideoPlayer({
return <ActivityIndicator />; return <ActivityIndicator />;
} }
//console.log(`${config.detect.width / config.detect.height < 1.7 ? "16:9" : undefined}`)
return ( return (
<div className={className}> <div className={className}>
<div <div
@ -228,7 +226,7 @@ export default function DynamicVideoPlayer({
player.on("timeupdate", () => { player.on("timeupdate", () => {
controller.updateProgress(player.currentTime() || 0); controller.updateProgress(player.currentTime() || 0);
}); });
player.on("ended", () => controller.fireClipEndEvent()); player.on("ended", () => controller.fireClipChangeEvent("forward"));
if (onControllerReady) { if (onControllerReady) {
onControllerReady(controller); onControllerReady(controller);
@ -264,6 +262,7 @@ export default function DynamicVideoPlayer({
previewRef.current = player; previewRef.current = player;
player.pause(); player.pause();
player.on("seeked", () => controller.finishedSeeking()); player.on("seeked", () => controller.finishedSeeking());
player.on("loadeddata", () => controller.previewReady());
}} }}
onDispose={() => { onDispose={() => {
previewRef.current = undefined; previewRef.current = undefined;
@ -285,7 +284,8 @@ export class DynamicVideoController {
// playback // playback
private recordings: Recording[] = []; private recordings: Recording[] = [];
private onPlaybackTimestamp: ((time: number) => void) | undefined = undefined; private onPlaybackTimestamp: ((time: number) => void) | undefined = undefined;
private onClipEnded: (() => void) | undefined = undefined; private onClipChange: ((dir: "forward" | "backward") => void) | undefined =
undefined;
private annotationOffset: number; private annotationOffset: number;
private timeToStart: number | undefined = undefined; private timeToStart: number | undefined = undefined;
@ -293,6 +293,7 @@ export class DynamicVideoController {
private preview: Preview | undefined = undefined; private preview: Preview | undefined = undefined;
private timeToSeek: number | undefined = undefined; private timeToSeek: number | undefined = undefined;
private seeking = false; private seeking = false;
private readyToScrub = true;
constructor( constructor(
playerRef: MutableRefObject<Player | undefined>, playerRef: MutableRefObject<Player | undefined>,
@ -395,32 +396,52 @@ export class DynamicVideoController {
this.onPlaybackTimestamp = listener; this.onPlaybackTimestamp = listener;
} }
onClipEndedEvent(listener: () => void) { onClipChangedEvent(listener: (dir: "forward" | "backward") => void) {
this.onClipEnded = listener; this.onClipChange = listener;
} }
fireClipEndEvent() { fireClipChangeEvent(dir: "forward" | "backward") {
if (this.onClipEnded) { if (this.onClipChange) {
this.onClipEnded(); this.onClipChange(dir);
} }
} }
scrubToTimestamp(time: number) { scrubToTimestamp(time: number) {
if (!this.preview) {
return;
}
if (!this.readyToScrub) {
return;
}
if (time > this.preview.end) {
if (this.playerMode == "scrubbing") {
this.playerMode = "playback";
this.setScrubbing(false);
this.timeToSeek = undefined;
this.seeking = false;
this.readyToScrub = false;
this.fireClipChangeEvent("forward");
}
return;
}
if (this.playerMode != "scrubbing") { if (this.playerMode != "scrubbing") {
this.playerMode = "scrubbing"; this.playerMode = "scrubbing";
this.playerRef.current?.pause(); this.playerRef.current?.pause();
this.setScrubbing(true); this.setScrubbing(true);
} }
if (this.preview) {
if (this.seeking) { if (this.seeking) {
this.timeToSeek = time; this.timeToSeek = time;
} else { } else {
this.previewRef.current?.currentTime(time - this.preview.start); this.previewRef.current?.currentTime(
Math.max(0, time - this.preview.start)
);
this.seeking = true; this.seeking = true;
} }
} }
}
finishedSeeking() { finishedSeeking() {
if (!this.preview || this.playerMode == "playback") { if (!this.preview || this.playerMode == "playback") {
@ -438,4 +459,9 @@ export class DynamicVideoController {
this.seeking = false; this.seeking = false;
} }
} }
previewReady() {
this.previewRef.current?.pause();
this.readyToScrub = true;
}
} }

View File

@ -20,9 +20,13 @@ export default function DesktopRecordingView({
relevantPreviews, relevantPreviews,
}: DesktopRecordingViewProps) { }: DesktopRecordingViewProps) {
const navigate = useNavigate(); const navigate = useNavigate();
const controllerRef = useRef<DynamicVideoController | undefined>(undefined);
const contentRef = useRef<HTMLDivElement | null>(null); const contentRef = useRef<HTMLDivElement | null>(null);
// controller state
const [playerReady, setPlayerReady] = useState(false);
const controllerRef = useRef<DynamicVideoController | undefined>(undefined);
// timeline time // timeline time
const timeRange = useMemo( const timeRange = useMemo(
@ -44,12 +48,14 @@ export default function DesktopRecordingView({
return; return;
} }
if (selectedRangeIdx < timeRange.ranges.length - 1) { controllerRef.current.onClipChangedEvent((dir) => {
controllerRef.current.onClipEndedEvent(() => { if (dir == "forward" && selectedRangeIdx < timeRange.ranges.length - 1) {
setSelectedRangeIdx(selectedRangeIdx + 1); setSelectedRangeIdx(selectedRangeIdx + 1);
}); } else if (selectedRangeIdx > 0) {
setSelectedRangeIdx(selectedRangeIdx - 1);
} }
}, [controllerRef, selectedRangeIdx]); });
}, [playerReady, selectedRangeIdx]);
// scrubbing and timeline state // scrubbing and timeline state
@ -62,13 +68,13 @@ export default function DesktopRecordingView({
if (scrubbing) { if (scrubbing) {
controllerRef.current?.scrubToTimestamp(currentTime); controllerRef.current?.scrubToTimestamp(currentTime);
} }
}, [controllerRef, currentTime, scrubbing]); }, [currentTime, scrubbing]);
useEffect(() => { useEffect(() => {
if (!scrubbing) { if (!scrubbing) {
controllerRef.current?.seekToTimestamp(currentTime, true); controllerRef.current?.seekToTimestamp(currentTime, true);
} }
}, [controllerRef, scrubbing]); }, [scrubbing]);
return ( return (
<div ref={contentRef} className="relative w-full h-full"> <div ref={contentRef} className="relative w-full h-full">
@ -87,6 +93,7 @@ export default function DesktopRecordingView({
cameraPreviews={relevantPreviews || []} cameraPreviews={relevantPreviews || []}
onControllerReady={(controller) => { onControllerReady={(controller) => {
controllerRef.current = controller; controllerRef.current = controller;
setPlayerReady(true);
controllerRef.current.onPlayerTimeUpdate((timestamp: number) => { controllerRef.current.onPlayerTimeUpdate((timestamp: number) => {
setCurrentTime(timestamp); setCurrentTime(timestamp);
}); });