🐛 Prevent browser seek quantization from displaying the previous frame

This commit is contained in:
eust-w 2026-09-15 04:53:34 +08:00
parent b6396197e2
commit 6eb21ada38
4 changed files with 34 additions and 7 deletions

View File

@ -124,10 +124,12 @@ type Props = {
import { annotationTimelineColors as colors } from "../lib/annotation-colors.js";
const formatTime = (seconds: number) => {
const value = Math.max(0, seconds);
const minutes = Math.floor(value / 60);
const wholeSeconds = Math.floor(value % 60);
const milliseconds = Math.floor((value - Math.floor(value)) * 1000);
// Convert once: subtracting integer seconds can turn 4.1 into a displayed
// 04.099 through floating-point cancellation. Preserve millisecond truncation.
const totalMilliseconds = Math.floor(Math.max(0, seconds) * 1000 + 1e-7);
const minutes = Math.floor(totalMilliseconds / 60000);
const wholeSeconds = Math.floor(totalMilliseconds / 1000) % 60;
const milliseconds = totalMilliseconds % 1000;
return `${String(minutes).padStart(2, "0")}:${String(wholeSeconds).padStart(2, "0")}.${String(milliseconds).padStart(3, "0")}`;
};

View File

@ -1,6 +1,6 @@
import { useEffect, useRef, useState } from "react";
import type { RefObject } from "react";
import { createMediaSeekQueue } from "../lib/media-seek-queue.js";
import { browserMediaSeekTime, createMediaSeekQueue } from "../lib/media-seek-queue.js";
type Camera = "left" | "right";
type Options = {
@ -62,7 +62,7 @@ export function useCaptureVideoTransport(options: Options) {
}
}
function physicalTarget(video: HTMLVideoElement, time: number) {
return Math.max(0, Number.isFinite(video.duration) ? Math.min(video.duration, time) : time);
return browserMediaSeekTime(time, video.duration);
}
function seekTrack(target: (typeof targets.current)[number]) {
const { video, time } = target;

View File

@ -1,3 +1,12 @@
// Chromium can truncate a boundary such as 4.1 to 4.099999, displaying the
// previous frame. Seek just inside the requested frame (at most 2 microseconds)
// without rounding or modifying the logical annotation boundary itself.
export function browserMediaSeekTime(time, duration = Infinity) {
const nonnegative = Math.max(0, time);
const insideFrame = nonnegative === 0 ? 0 : (Math.ceil(nonnegative * 1e6) + 1) / 1e6;
return Number.isFinite(duration) ? Math.min(duration, insideFrame) : insideFrame;
}
// Pointer previews are latest-only and never interrupt an in-flight preview.
// Explicit commits (release, click, frame step) bypass the preview throttle.
export function createMediaSeekQueue({

View File

@ -1,6 +1,22 @@
import assert from "node:assert/strict";
import test from "node:test";
import { createMediaSeekQueue } from "../../src/lib/media-seek-queue.js";
import { browserMediaSeekTime, createMediaSeekQueue } from "../../src/lib/media-seek-queue.js";
test("physical seek survives microsecond truncation without changing the requested boundary", () => {
for (const time of [4.1, 4 + 2 / 30, 11 / 30, 1 / 30, 10.7, 899.966666666667]) {
const target = browserMediaSeekTime(time, 900);
assert(target > time && target - time <= 0.00000201);
assert(Math.floor(target * 1e6) >= Math.ceil(time * 1e6));
}
});
test("physical seek stays inside media duration and keeps zero unchanged", () => {
assert.equal(browserMediaSeekTime(0, 900), 0);
assert.equal(browserMediaSeekTime(-1, 900), 0);
assert.equal(browserMediaSeekTime(900, 900), 900);
assert.equal(browserMediaSeekTime(910, 900), 900);
assert(browserMediaSeekTime(4.1, NaN) > 4.1);
});
function fixture() {
let clock = 0;