8000 fix(preview): jump-vs-natural seek policy + shared sync helper by johannboehme · Pull Request #43 · johannboehme/tk-1 · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions frontend/src/components/arrange/CamPreviewArrange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
* aluminium-bezel viewfinder shell that fits the Arrange "playback
* cockpit" look — sub-300px wide on desktop, ~80px square on mobile.
*/
import { useEffect, useState } from "react";
import { useEffect, useRef, useState } from "react";
import { resolveCamAssetUrl } from "../../local/jobs";
import { useArrangeStore } from "../../local/arrange/arrange-store";
import { decideCamPreviewAction } from "../../local/timing/cam-preview-sync";
import { CamPickerDropdown } from "../CamPickerDropdown";

/** Cam preview viewfinder. Sizes itself: 84×84 below sm, 240×135 above. */
Expand All @@ -28,6 +29,16 @@ export function CamPreviewArrange() {
const syncOffsetMs = cam
? (cam.sync?.offsetMs ?? 0) + (cam.syncOverrideMs ?? 0 8000 )
: 0;
const driftRatio = cam?.sync?.driftRatio ?? 1;
const sourceDurationS = cam?.durationS ?? null;

// Tracks the previous master tick so the decider can tell natural
// RAF advance apart from a meaningful jump (chunk click / scrub).
// Reset on cam change so the next tick re-snaps.
const prevMasterTRef = useRef<number | null>(null);
useEffect(() => {
prevMasterTRef.current = null;
}, [camId]);

useEffect(() => {
if (!jobId || !camId) {
Expand All @@ -52,19 +63,44 @@ export function CamPreviewArrange() {

useEffect(() => {
if (!videoEl) return;
const sourceT = currentTime - syncOffsetMs / 1000;
if (sourceT < 0) {
// Skip while the element is recovering from a previous seek; stacking
// another `currentTime` write tears the decoder open mid-flight on
// huge phone recordings — same anti-stutter rule the Triage preview
// uses. `prevMasterT` stays unchanged so the next non-seeking tick
// sees the full delta and classifies the situation correctly.
if (videoEl.seeking) return;

const action = decideCamPreviewAction({
masterT: currentTime,
syncOffsetMs,
sourceDurationS,
driftRatio,
videoCurrentTimeS: Number.isFinite(videoEl.currentTime)
? videoEl.currentTime
: null,
prevMasterT: prevMasterTRef.current,
});
prevMasterTRef.current = currentTime;

if (action.kind === "pause-before-start") {
if (!videoEl.paused) videoEl.pause();
return;
}
if (Math.abs(videoEl.currentTime - sourceT) > 0.05) {
if (action.kind === "seek") {
try {
videoEl.currentTime = sourceT;
videoEl.currentTime = action.sourceT;
} catch {
/* ignore */
/* element not ready yet — next tick */
}
}
}, [currentTime, syncOffsetMs, videoEl]);
if (action.kind === "pause-after-end") {
if (!videoEl.paused) videoEl.pause();
return;
}
if (isPlaying && videoEl.paused) {
void videoEl.play().catch(() => undefined);
}
}, [currentTime, syncOffsetMs, driftRatio, sourceDurationS, isPlaying, videoEl]);

useEffect(() => {
if (!videoEl) return;
Expand Down
37 changes: 31 additions & 6 deletions frontend/src/components/triage/CamPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
* Triage v1: no preload pool, no gapless cam-switch — accept a brief
* black flash on cam-switch since this is a curation phase.
*/
import { useEffect, useState } from "react";
import { useEffect, useRef, useState } from "react";
import { resolveCamAssetUrl } from "../../local/jobs";
import { useTriageStore } from "../../local/triage/triage-store";
import { CamPickerDropdown } from "../CamPickerDropdown";
import { decideCamPreviewAction } from "./cam-preview-sync";
import { decideCamPreviewAction } from "../../local/timing/cam-preview-sync";

export function CamPreview() {
const jobId = useTriageStore((s) => s.jobId);
Expand All @@ -35,6 +35,15 @@ export function CamPreview() {
const driftRatio = cam?.sync?.driftRatio ?? 1;
const sourceDurationS = cam?.durationS ?? null;

// Tracks the previous `masterT` so the per-tick decider can tell
// natural ~16 ms RAF advance apart from a meaningful jump (chunk
// click / scrub / loop wrap). Reset whenever the active cam changes
// so the next tick re-snaps to the new element.
const prevMasterTRef = useRef<number | null>(null);
useEffect(() => {
prevMasterTRef.current = null;
}, [camId]);

useEffect(() => {
if (!jobId || !camId) {
setVideoUrl(null);
Expand All @@ -58,27 +67,43 @@ export function CamPreview() {

useEffect(() => {
if (!videoEl) return;
// Skip while the element is still recovering from a previous seek.
// Stacking another `currentTime = X` on top of an in-flight seek
// tears the decoder open mid-flight on huge phone recordings —
// exactly the post-seek stutter loop the user reports.
if (videoEl.seeking) {
// Don't update prevMasterT — we want the NEXT non-seeking tick to
// see the full delta from before the seek so it can classify it
// as a jump.
return;
}
const action = decideCamPreviewAction({
masterT: currentTime,
syncOffsetMs,
sourceDurationS,
driftRatio,
videoCurrentTimeS: Number.isFinite(videoEl.currentTime)
? videoEl.currentTime
: null,
prevMasterT: prevMasterTRef.current,
});
prevMasterTRef.current = currentTime;

if (action.kind === "pause-before-start") {
if (!videoEl.paused) videoEl.pause();
return;
}
if (Math.abs(videoEl.currentTime - action.sourceT) > 0.05) {
if (action.kind === "seek") {
videoEl.currentTime = action.sourceT;
}
if (action.kind === "pause-after-end") {
if (!videoEl.paused) videoEl.pause();
return;
}
// Cam IS visible. If the master is playing but our element is paused
// (e.g. we just crossed sourceT=0 after a `pause-before-start` window),
// resume — without this, the preview stays frozen on the first
// post-anchor frame even though the master plays on.
// (e.g. we just crossed sourceT=0 after a `pause-before-start`
// window), resume — without this, the preview stays frozen on the
// first post-anchor frame even though the master plays on.
if (isPlaying && videoEl.paused) {
void videoEl.play().catch(() => undefined);
}
Expand Down
115 changes: 0 additions & 115 deletions frontend/src/components/triage/cam-preview-sync.test.ts

This file was deleted.

64 changes: 0 additions & 64 deletions frontend/src/components/triage/cam-preview-sync.ts

This file was deleted.

Loading
Loading
0