Skip to content

Commit 6bb85af

Browse files
feat(studio): a host can hold playback so nothing starts while it is held (#5060)
* feat(studio): a host can hold playback so nothing starts while it is held * feat(studio): a held player seeks without scrub audio * fix(studio): a held player neither loops nor changes speed on J and L * fix(studio): pause clears playing even before the preview has a player * chore(studio): keep the play hold's comment share flat * refactor(studio): keep the play hold out of seek's and the subscription's branches * test(studio): a timeline reset keeps the host's play hold
1 parent 856aad5 commit 6bb85af

6 files changed

Lines changed: 160 additions & 7 deletions

File tree

‎packages/studio/src/player/hooks/usePlaybackKeyboard.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export function usePlaybackKeyboard({
6060

6161
const shuttle = useCallback(
6262
(direction: "forward" | "backward") => {
63+
if (usePlayerStore.getState().playLocked) return;
6364
if (shuttleDirectionRef.current === direction) {
6465
shuttleSpeedIndexRef.current = Math.min(
6566
shuttleSpeedIndexRef.current + 1,
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// @vitest-environment happy-dom
2+
3+
import { act } from "react";
4+
import { afterEach, expect, it, vi } from "vitest";
5+
import {
6+
makeAdapterWindow,
7+
makeFakeIframe,
8+
renderTimelinePlayerHarness,
9+
resetPlayerStore,
10+
} from "./timelinePlayerTestHarness";
11+
import { usePlayerStore } from "../store/playerStore";
12+
import { scrubMusicAtSeek } from "../lib/playbackScrub";
13+
14+
vi.mock("../lib/playbackScrub", async (importOriginal) => ({
15+
...(await importOriginal<typeof import("../lib/playbackScrub")>()),
16+
scrubMusicAtSeek: vi.fn(),
17+
}));
18+
19+
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
20+
21+
afterEach(() => {
22+
document.body.innerHTML = "";
23+
resetPlayerStore();
24+
usePlayerStore.setState({ playLocked: false, loopEnabled: false, playbackRate: 1 });
25+
vi.mocked(scrubMusicAtSeek).mockClear();
26+
});
27+
28+
function readyPlayer() {
29+
const { api, root } = renderTimelinePlayerHarness();
30+
const { adapter, win } = makeAdapterWindow();
31+
act(() => {
32+
api.iframeRef.current = makeFakeIframe(win);
33+
api.onIframeLoad();
34+
usePlayerStore.setState({ timelineReady: true });
35+
});
36+
return { api, root, adapter };
37+
}
38+
39+
const press = (key: string) =>
40+
act(() => {
41+
window.dispatchEvent(new KeyboardEvent("keydown", { key, code: `Key${key.toUpperCase()}` }));
42+
});
43+
44+
it("starts nothing while the host holds playback: Play, a playback request or play backward", () => {
45+
const { api, root, adapter } = readyPlayer();
46+
act(() => usePlayerStore.getState().setPlayLocked(true));
47+
act(() => api.play());
48+
act(() => usePlayerStore.getState().requestPlayback(true));
49+
press("j");
50+
expect(adapter.play).not.toHaveBeenCalled();
51+
expect(usePlayerStore.getState().isPlaying).toBe(false);
52+
act(() => root.unmount());
53+
});
54+
55+
it("J and L keep the chosen speed while the host holds playback", () => {
56+
const { root } = readyPlayer();
57+
act(() => usePlayerStore.getState().setPlaybackRate(0.5));
58+
act(() => usePlayerStore.getState().setPlayLocked(true));
59+
press("l");
60+
press("j");
61+
expect(usePlayerStore.getState().playbackRate).toBe(0.5);
62+
act(() => root.unmount());
63+
});
64+
65+
it("a looping film reaching its end while held stops instead of starting again", () => {
66+
const frames: FrameRequestCallback[] = [];
67+
const raf = vi.spyOn(window, "requestAnimationFrame").mockImplementation((tick) => {
68+
frames.push(tick);
69+
return frames.length;
70+
});
71+
const { api, root, adapter } = readyPlayer();
72+
act(() => usePlayerStore.setState({ loopEnabled: true }));
73+
act(() => api.play());
74+
// Held with the store already reading stopped, so nothing pauses the frame loop first.
75+
act(() => usePlayerStore.setState({ playLocked: true, isPlaying: false }));
76+
adapter.seek(30);
77+
act(() => frames.at(-1)!(0));
78+
expect(adapter.play).toHaveBeenCalledTimes(1);
79+
expect(usePlayerStore.getState().isPlaying).toBe(false);
80+
raf.mockRestore();
81+
act(() => root.unmount());
82+
});
83+
84+
it("turning the hold on before the preview has a player still clears playing", () => {
85+
const { root } = renderTimelinePlayerHarness();
86+
act(() => usePlayerStore.setState({ isPlaying: true }));
87+
act(() => usePlayerStore.getState().setPlayLocked(true));
88+
expect(usePlayerStore.getState().isPlaying).toBe(false);
89+
act(() => root.unmount());
90+
});
91+
92+
it("a seek that would keep playing does not resume while the host holds playback", () => {
93+
const { api, root, adapter } = readyPlayer();
94+
act(() => usePlayerStore.getState().setPlayLocked(true));
95+
// The store still reads playing (a host that set the hold before the player caught up): only the hold stops it.
96+
act(() => usePlayerStore.setState({ isPlaying: true }));
97+
act(() => {
98+
api.seek(5, { keepPlaying: true });
99+
});
100+
expect(adapter.play).not.toHaveBeenCalled();
101+
act(() => root.unmount());
102+
});
103+
104+
it("a seek while the host holds playback moves the picture without scrub audio", () => {
105+
const { api, root, adapter } = readyPlayer();
106+
act(() => {
107+
api.seek(2);
108+
});
109+
expect(scrubMusicAtSeek).toHaveBeenCalledTimes(1);
110+
act(() => usePlayerStore.getState().setPlayLocked(true));
111+
act(() => {
112+
api.seek(4);
113+
});
114+
expect(adapter.getTime()).toBe(4);
115+
expect(scrubMusicAtSeek).toHaveBeenCalledTimes(1);
116+
act(() => root.unmount());
117+
});
118+
119+
it("a timeline reset keeps the hold: the host owns it", () => {
120+
act(() => usePlayerStore.getState().setPlayLocked(true));
121+
act(() => usePlayerStore.getState().reset());
122+
expect(usePlayerStore.getState().playLocked).toBe(true);
123+
});
124+
125+
it("turning the hold on pauses a playing film; turning it off lets Play start again", () => {
126+
const { api, root, adapter } = readyPlayer();
127+
act(() => api.play());
128+
expect(usePlayerStore.getState().isPlaying).toBe(true);
129+
act(() => usePlayerStore.getState().setPlayLocked(true));
130+
expect(adapter.pause).toHaveBeenCalled();
131+
expect(usePlayerStore.getState().isPlaying).toBe(false);
132+
act(() => usePlayerStore.getState().setPlayLocked(false));
133+
act(() => api.play());
134+
expect(adapter.play).toHaveBeenCalledTimes(2);
135+
expect(usePlayerStore.getState().isPlaying).toBe(true);
136+
act(() => root.unmount());
137+
});

‎packages/studio/src/player/hooks/useTimelinePlayer.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ export function useTimelinePlayer({
200200
applyPreviewAudioFlags(iframeRef.current, audioMuted, audioVolume);
201201
}, []);
202202
const play = useCallback(() => {
203-
if (!usePlayerStore.getState().timelineReady) return;
203+
const { timelineReady, playLocked } = usePlayerStore.getState();
204+
if (!timelineReady || playLocked) return;
204205
stopRAFLoop();
205206
stopReverseLoop();
206207
stopScrubPreviewAudio();
@@ -286,9 +287,8 @@ export function useTimelinePlayer({
286287
const pause = useCallback(() => {
287288
stopReverseLoop();
288289
const adapter = getAdapter();
289-
if (!adapter) return;
290-
adapter.pause();
291-
setCurrentTime(adapter.getTime()); // sync store so Split/Delete have accurate time
290+
adapter?.pause();
291+
if (adapter) setCurrentTime(adapter.getTime()); // sync store so Split/Delete have accurate time
292292
setIsPlaying(false);
293293
shuttleDirectionRef.current = null;
294294
shuttleSpeedIndexRef.current = 0;
@@ -306,17 +306,19 @@ export function useTimelinePlayer({
306306
const duration = Math.max(0, adapter.getDuration());
307307
const nextTime = clampToDuration(time, duration);
308308
const keepPlaying = options?.keepPlaying === true;
309+
const { playLocked, isPlaying: storeWasPlaying } = usePlayerStore.getState();
309310
const shouldResumeAfterSeek = shouldResumeForwardPlaybackAfterSeek({
310311
keepPlaying,
311312
wasReverseShuttle,
312-
storeWasPlaying: usePlayerStore.getState().isPlaying,
313+
storeWasPlaying,
313314
duration,
314315
nextTime,
316+
playLocked,
315317
});
316318
adapter.seek(nextTime, options);
317319
publishSeek(nextTime, options); // Direct DOM updates (playhead, timecode, progress) — no re-render
318320
setCurrentTime(nextTime); // sync store so Split/Delete have accurate time
319-
if (!shouldResumeAfterSeek && !keepPlaying) scrubMusicAtSeek(iframeRef.current, nextTime);
321+
if (!keepPlaying && !playLocked) scrubMusicAtSeek(iframeRef.current, nextTime);
320322
if (shouldResumeAfterSeek) {
321323
stopRAFLoop();
322324
applyPlaybackRate(usePlayerStore.getState().playbackRate);
@@ -349,6 +351,13 @@ export function useTimelinePlayer({
349351
],
350352
);
351353

354+
useEffect(
355+
() =>
356+
usePlayerStore.subscribe((state, prev) => {
357+
if (state.playLocked && !prev.playLocked && state.isPlaying) pause();
358+
}),
359+
[pause],
360+
);
352361
useEffect(() => {
353362
return usePlayerStore.subscribe((state, prev) => {
354363
if (state.requestedSeekTime !== null && state.requestedSeekTime !== prev.requestedSeekTime) {

‎packages/studio/src/player/hooks/useTimelinePlayerLoop.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function useTimelinePlayerLoop({
6060
const looping = usePlayerStore.getState().loopEnabled && dur > 0;
6161
setLoopStart(looping ? loopStart : null);
6262
if (time >= loopEnd) {
63-
if (looping) {
63+
if (looping && !usePlayerStore.getState().playLocked) {
6464
// keepPlaying skips the adapter's implicit pause; play() below is then a no-op.
6565
adapter.seek(loopStart, { keepPlaying: true });
6666
liveTime.notify(loopStart);

‎packages/studio/src/player/lib/playbackSeek.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ export function shouldResumeForwardPlaybackAfterSeek(input: {
44
storeWasPlaying: boolean;
55
duration: number;
66
nextTime: number;
7+
playLocked: boolean;
78
}): boolean {
89
return (
10+
!input.playLocked &&
911
input.keepPlaying &&
1012
!input.wasReverseShuttle &&
1113
input.storeWasPlaying &&

‎packages/studio/src/player/store/readinessSlice.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export interface PlaybackReadinessSlice {
1313
previewBooted: boolean;
1414
setTimelineReady: (ready: boolean) => void;
1515
markPreviewBooted: () => void;
16+
playLocked: boolean;
17+
setPlayLocked: (locked: boolean) => void;
1618
/** Sets timelineReady once doc's readiness inputs settle, or immediately
1719
* if doc is null. A wait a later call supersedes never wins the race.
1820
* Waits for doc's load step first: it pauses and rewinds the preview, so a
@@ -47,6 +49,8 @@ export function createPlaybackReadinessSlice(
4749
timelineReady: false,
4850
previewBooted: false,
4951
markPreviewBooted: () => set({ previewBooted: true }),
52+
playLocked: false,
53+
setPlayLocked: (locked) => set({ playLocked: locked }),
5054
setTimelineReady: (ready) => {
5155
timelineReadyGeneration++;
5256
set({ timelineReady: ready });

0 commit comments

Comments
 (0)