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
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ describe("NewEditorShell togglePlay cross-asset replay", () => {
it.each([
true,
false,
])("keeps playback=%s when a manual seek crosses into another asset", async (playing) => {
])("lets the preview resolve implicit seek intent when the transport is %s", async (playing) => {
useProjectStore.setState({
document: makeCrossAssetDocument(),
projectId: "p1",
Expand All @@ -240,6 +240,8 @@ describe("NewEditorShell togglePlay cross-asset replay", () => {
fireEvent.keyDown(document.body, { key: "ArrowRight", code: "ArrowRight" });
});
expect(mocks.latestSeekTarget?.timeSec).toBeGreaterThan(5);
expect(mocks.latestSeekTarget?.preservePlayback).toBe(playing);
// A replacement video briefly reports paused before metadata. The shell
// must leave an implicit seek to the preview's current or queued intent.
expect(mocks.latestSeekTarget?.preservePlayback).toBeUndefined();
});
});
6 changes: 3 additions & 3 deletions src/components/ai-edition/NewEditorShell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,9 @@ export function NewEditorShell() {
timeSec,
isSource: false,
requestId: ++seekSeqRef.current,
// Timeline and transcript seeks omit the flag. Carry the transport's
// intent across a source swap, as a seek within the same video does.
preservePlayback: preservePlayback ?? useProjectStore.getState().playing,
// The preview owns queued playback while another source loads. Its new
// video is initially paused, so the store is not the scrub's intent.
preservePlayback,
});
},
[setCurrentTime, canSeek],
Expand Down
99 changes: 99 additions & 0 deletions src/components/ai-edition/VirtualPreview.playback.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,105 @@ describe("VirtualPreview replay", () => {
expect(reportedTimes(onTimeChange).at(-1)).toBe(14);
});

it.each([
{
initiallyPlaying: true,
firstIntent: undefined,
nextIntent: undefined,
shouldPlay: true,
intermediateImplicitSeek: false,
},
{
initiallyPlaying: true,
firstIntent: true,
nextIntent: undefined,
shouldPlay: true,
intermediateImplicitSeek: false,
},
{
initiallyPlaying: true,
firstIntent: true,
nextIntent: false,
shouldPlay: false,
intermediateImplicitSeek: false,
},
{
initiallyPlaying: false,
firstIntent: undefined,
nextIntent: undefined,
shouldPlay: false,
intermediateImplicitSeek: false,
},
{
initiallyPlaying: true,
firstIntent: true,
nextIntent: false,
shouldPlay: false,
intermediateImplicitSeek: true,
},
])("keeps source-switch intent (playing $initiallyPlaying; intermediate $intermediateImplicitSeek; next $nextIntent)", ({
initiallyPlaying,
firstIntent,
nextIntent,
shouldPlay,
intermediateImplicitSeek,
}) => {
const sources: VideoSource[] = [
{ id: "a1", src: "file:///tmp/a1.mp4", label: "a1" },
{ id: "a2", src: "file:///tmp/a2.mp4", label: "a2" },
];
const clips = [clip("clip_1", "a1", 0, 5, 0), clip("clip_2", "a2", 0, 5, 5)];
let seekTarget: {
timeSec: number;
requestId: number;
preservePlayback?: boolean;
} | null = null;
const view = render(
<VirtualPreview videoSources={sources} clips={clips} seekTarget={seekTarget} />,
);
const firstElement = view.container.querySelector("video");
if (!firstElement) throw new Error("no initial video");
const firstVideo = driveVideo(firstElement);
act(() => fireEvent.loadedMetadata(firstElement));
if (initiallyPlaying) firstVideo.play();
firstVideo.seekTo(3);

act(() => {
seekTarget = { timeSec: 6, requestId: 1, preservePlayback: firstIntent };
view.rerender(
<VirtualPreview videoSources={sources} clips={clips} seekTarget={seekTarget} />,
);
});
const nextElement = view.container.querySelector("video");
if (!nextElement || nextElement === firstElement) throw new Error("source did not switch");
const nextVideo = driveVideo(nextElement);
expect(nextElement.paused).toBe(true);
if (intermediateImplicitSeek) {
act(() => {
seekTarget = { timeSec: 6.25, requestId: 2 };
view.rerender(
<VirtualPreview videoSources={sources} clips={clips} seekTarget={seekTarget} />,
);
});
}

// Pointerup or a later drag position arrives while the new decoder is
// still paused before metadata. It must retain the scrub's play intent.
act(() => {
seekTarget = {
timeSec: 6.5,
requestId: intermediateImplicitSeek ? 3 : 2,
preservePlayback: nextIntent,
};
view.rerender(
<VirtualPreview videoSources={sources} clips={clips} seekTarget={seekTarget} />,
);
});
act(() => fireEvent.loadedMetadata(nextElement));
expect(nextVideo.currentTime).toBe(1.5);
expect(nextElement.paused).toBe(!shouldPlay);
});

it("does not let stale loadState in tick closure block clip boundary checks or timeline end", () => {
const sources: VideoSource[] = [
{ id: "a1", src: "file:///tmp/a1.mp4", label: "a1" },
Expand Down
15 changes: 13 additions & 2 deletions src/components/ai-edition/VirtualPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1535,7 +1535,7 @@ export function VirtualPreview({
}, []);

const seekToVirtualTime = useCallback(
(nextVirtualTimeSec: number, preservePlayback = false, forceResume = false) => {
(nextVirtualTimeSec: number, preservePlayback?: boolean, forceResume = false) => {
const position = locateVirtualPosition(clips, nextVirtualTimeSec);
if (!position) {
videoRef.current?.pause();
Expand Down Expand Up @@ -1565,7 +1565,14 @@ export function VirtualPreview({
// race against the rAF tick's own boundary check: the native `ended`
// event fires first, stops playback outright, and the multi-clip
// timeline never advances — the "stops at clip end" bug.
const shouldContinuePlayback = preservePlayback && (forceResume || !videoRef.current?.paused);
// A scrub can publish again before the replacement video's metadata
// arrives. That element starts paused; keep the queued intent instead of
// turning pointerup or the next drag position into a request to stop.
// An explicit false still cancels the queued resume.
const shouldContinuePlayback =
preservePlayback === undefined
? (pendingSeekRef.current?.play ?? !videoRef.current?.paused)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
: preservePlayback && (forceResume || !videoRef.current?.paused);

if (isAssetSwitch) {
if (!shouldContinuePlayback) {
Expand Down Expand Up @@ -1595,6 +1602,10 @@ export function VirtualPreview({
sourceTimeSec: position.sourceTimeSec,
play: shouldContinuePlayback,
};
updateVirtualTime(position.virtualTimeSec);
// Metadata owns the pending seek and resume. Calling play now would
// queue a browser autoplay that a later paused seek cannot cancel.
return;
}
updateVirtualTime(position.virtualTimeSec);
applySourceTime(video, position.sourceTimeSec);
Expand Down
Loading