8000 fix(triage): flip cam-preview source-time sign so pre-roll maps forward by johannboehme · Pull Request #41 · 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
8000
29 changes: 24 additions & 5 deletions frontend/src/components/triage/CamPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { useEffect, 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";

export function CamPreview() {
const jobId = useTriageStore((s) => s.jobId);
Expand All @@ -31,6 +32,8 @@ export function CamPreview() {
const syncOffsetMs = cam
? (cam.sync?.offsetMs ?? 0) + (cam.syncOverrideMs ?? 0)
: 0;
const driftRatio = cam?.sync?.driftRatio ?? 1;
const sourceDurationS = cam?.durationS ?? null;

useEffect(() => {
if (!jobId || !camId) {
Expand All @@ -55,15 +58,31 @@ export function CamPreview() {

useEffect(() => {
if (!videoEl) return;
const sourceT = currentTime - syncOffsetMs / 1000;
if (sourceT < 0) {
const action = decideCamPreviewAction({
masterT: currentTime,
syncOffsetMs,
sourceDurationS,
driftRatio,
});
if (action.kind === "pause-before-start") {
if (!videoEl.paused) videoEl.pause();
return;
}
if (Math.abs(videoEl.currentTime - action.sourceT) > 0.05) {
videoEl.currentTime = action.sourceT;
}
if (action.kind === "pause-after-end") {
if (!videoEl.paused) videoEl.pause();
return;
}
if (Math.abs(videoEl.currentTime - sourceT) > 0.05) {
videoEl.currentTime = sourceT;
// 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.
if (isPlaying && videoEl.paused) {
void videoEl.play().catch(() => undefined);
}
}, [currentTime, syncOffsetMs, videoEl]);
}, [currentTime, syncOffsetMs, driftRatio, sourceDurationS, isPlaying, videoEl]);

useEffect(() => {
if (!videoEl) return;
Expand Down
115 changes: 115 additions & 0 deletions frontend/src/components/triage/cam-preview-sync.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { describe, it, expect } from "vitest";
import { decideCamPreviewAction } from "./cam-preview-sync";

describe("decideCamPreviewAction", () => {
it("identity when there is no sync offset", () => {
expect(
decideCamPreviewAction({
masterT: 0,
syncOffsetMs: 0,
sourceDurationS: 60,
}),
).toEqual({ kind: "seek", sourceT: 0 });
expect(
decideCamPreviewAction({
masterT: 12.5,
syncOffsetMs: 0,
sourceDurationS: 60,
}),
).toEqual({ kind: "seek", sourceT: 12.5 });
});

it("positive sync offset (cam pre-roll) jumps the source forward", () => {
// Phone recorded 1.5 s of mic-check before the song; master starts at song.
// At master t=0 the cam is already 1.5 s in.
const action = decideCamPreviewAction({
masterT: 0,
syncOffsetMs: 1500,
sourceDurationS: 60,
});
expect(action).toEqual({ kind: "seek", sourceT: 1.5 });
});

it("regression: 'Maria José' — 193 s of preroll, master at t=0", () => {
// Real bug: 938 s video, 740 s audio, ~193 s preroll → matcher returned
// sync.offsetMs ≈ +193000. The old formula (master − offset/1000) gave
// sourceT = −193 and paused the preview for 3:13; the correct formula
// gives sourceT = +193 (cam plays from where the song starts in the
// source).
const action = decideCamPreviewAction({
masterT: 0,
syncOffsetMs: 193_000,
sourceDurationS: 938.1,
});
expect(action).toEqual({ kind: "seek", sourceT: 193 });
});

it("negative sync offset (cam started AFTER master) pauses until the cam appears", () => {
// Master plays 2 s before this cam joins.
expect(
decideCamPreviewAction({
masterT: 0,
syncOffsetMs: -2000,
sourceDurationS: 60,
}),
).toEqual({ kind: "pause-before-start" });
expect(
decideCamPreviewAction({
masterT: 1.5,
syncOffsetMs: -2000,
sourceDurationS: 60,
}),
).toEqual({ kind: "pause-before-start" });
// First frame as the cam comes in.
expect(
decideCamPreviewAction({
masterT: 2,
syncOffsetMs: -2000,
sourceDurationS: 60,
}),
).toEqual({ kind: "seek", sourceT: 0 });
});

it("holds the final frame when the cam runs past its source duration", () => {
// 60 s cam, master t=70 with 1 s pre-roll → would want sourceT = 71 (>60).
const action = decideCamPreviewAction({
masterT: 70,
syncOffsetMs: 1000,
10BC8 sourceDurationS: 60,
});
expect(action.kind).toBe("pause-after-end");
if (action.kind === "pause-after-end") {
expect(action.sourceT).toBeCloseTo(59.95, 5);
}
});

it("treats unknown source duration as 'seek anyway' (metadata still loading)", () => {
expect(
decideCamPreviewAction({
masterT: 5,
syncOffsetMs: 1000,
sourceDurationS: null,
}),
).toEqual({ kind: "seek", sourceT: 6 });
});

it("applies driftRatio so a faster-clock cam catches up to master", () => {
// Cam clock ran 0.1 % faster than master. After 60 s of master the cam
// is 60 ms ahead at the source.
expect(
decideCamPreviewAction({
masterT: 60,
syncOffsetMs: 0,
sourceDurationS: 600,
driftRatio: 1.001,
}),
).toMatchObject({ kind: "seek" });
const a = decideCamPreviewAction({
masterT: 60,
syncOffsetMs: 0,
sourceDurationS: 600,
driftRatio: 1.001,
});
if (a.kind === "seek") expect(a.sourceT).toBeCloseTo(60.06, 5);
});
});
64 changes: 64 additions & 0 deletions frontend/src/components/triage/cam-preview-sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Pure decision function for CamPreview's per-tick video-element sync.
*
* Given a master-timeline second and the cam's sync metadata, returns
* what the preview should do: pause (cam hasn't started yet, or the
* source has run out), or seek to a specific source-time.
*
* Sign convention follows `local/timing/cam-time.ts` and `clipRangeS`
* in `editor/types.ts`: `sync.offsetMs > 0` means the cam started
* recording BEFORE master t=0 (pre-roll), so at master t=0 the cam is
* already at source-time `+offsetMs/1000`. The previous formula
* `sourceT = masterT − offsetMs/1000` had the sign flipped, which
* froze the preview for the entire pre-roll window and then started
* playback from the cam's silence prelude — see the "Maria José" bug
* report (sync.offsetMs ≈ +193 s, preview frozen for 3:13 then jumped).
*/

import { camSourceTimeS, type CamTimeRef } from "../../local/timing/cam-time";

export type CamPreviewAction =
/** Cam isn't on screen yet (master playhead < cam's anchorS). */
| { kind: "pause-before-start" }
/** Cam's source has run out. Hold on its final frame. */
| { kind: "pause-after-end"; sourceT: number }
/** Cam IS on screen — seek the element to this source-time. */
| { kind: "seek"; sourceT: number };

export interface CamPreviewSyncInput {
/** Master-timeline position in seconds. */
masterT: number;
/** Combined `sync.offsetMs + syncOverrideMs` in milliseconds. */
syncOffsetMs: number;
/** Cam's source duration in seconds. Pass `null`/`undefined` when the
* duration isn't known yet (metadata still loading) — the caller
* treats the seek target as authoritative until it learns more. */
sourceDurationS: number | null | undefined;
/** Cam-clock vs master-clock ratio. Default 1. */
driftRatio?: number;
}

/** End-of-source guard padding. Browsers fire `seeked` past `duration`
* inconsistently; staying 50 ms inside is enough to keep the last
* frame stable without losing any usable content. */
const END_GUARD_S = 0.05;

export function decideCamPreviewAction(
input: CamPreviewSyncInput,
): CamPreviewAction {
const ref: CamTimeRef = {
// Positive sync offset → cam started before master t=0 → masterStartS
// is negative (anchor sits to the LEFT of master t=0).
masterStartS: -input.syncOffsetMs / 1000,
driftRatio: input.driftRatio ?? 1,
};
const sourceT = camSourceTimeS(input.masterT, ref);

if (sourceT < 0) return { kind: "pause-before-start" };

const dur = input.sourceDurationS ?? null;
if (dur != null && dur > 0 && sourceT > dur - END_GUARD_S) {
return { kind: "pause-after-end", sourceT: Math.max(0, dur - END_GUARD_S) };
}
return { kind: "seek", sourceT };
}
18 changes: 15 additions & 3 deletions frontend/src/local/arrange/chunk-thumbnails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import type { Chunk, VideoAsset } from "../../storage/jobs-db";
import { loadAssetFile } from "../asset-source";
import { resolveCamAssetUrl } from "../jobs";
import { planTileStrip } from "../render/frames/strategy";
import { camSourceTimeS } from "../timing/cam-time";

/** Target output width for tier-3 (on-demand seek) thumbnails — drawn
* from the full-res video, so we can pick a number that's plenty
Expand Down Expand Up @@ -450,8 +451,15 @@ function findCoveringTileIdx(
syncOffsetMs: number,
plan: StripPlan,
): number | null {
const sourceInS = chunk.startMs / 1000 - syncOffsetMs / 1000;
const sourceOutS = chunk.endMs / 1000 - syncOffsetMs / 1000;
// Sign convention: positive `syncOffsetMs` means the cam started BEFORE
// master t=0 (pre-roll). The cam's source-time-0 sits at master
// `-syncOffsetMs/1000`, so source-time at master T is
// `T − (−syncOffsetMs/1000) = T + syncOffsetMs/1000`. See
// `local/timing/cam-time.ts` and `editor/types.ts:clipRangeS` for the
// canonical definition.
const camRef = { masterStartS: -syncOffsetMs / 1000, driftRatio: 1 };
const sourceInS = camSourceTimeS(chunk.startMs / 1000, camRef);
const sourceOutS = camSourceTimeS(chunk.endMs / 1000, camRef);
if (sourceOutS <= 0) return null;
const midS = (sourceInS + sourceOutS) / 2;
let bestIdx = -1;
Expand Down Expand Up @@ -561,7 +569,11 @@ async function seekAndCapture(
// its end (sourceTimeS > duration) get the boundary frame instead
// of nothing — better a slightly-off thumbnail than an "out of
// range" placeholder that the user has no way to act on.
const rawSource = masterTimeS - probe.syncOffsetMs / 1000;
// See `findCoveringTileIdx` above for the sign convention.
const rawSource = camSourceTimeS(masterTimeS, {
masterStartS: -probe.syncOffsetMs / 1000,
driftRatio: 1,
});
const sourceTimeS = Math.max(0, Math.min(v.duration - 0.05, rawSource));

let seekedFired = false;
Expand Down
Loading
0