Skip to content

Commit 3a28ef4

Browse files
perf(studio): a long film's media durations write the store once per file, not once per clip (#6745)
* fix(studio): a film that cuts one file into many clips plays smoothly while their durations load * test(studio): the metadata test counts one store write per file
1 parent a5a6121 commit 3a28ef4

4 files changed

Lines changed: 51 additions & 26 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ it("publishes resolved metadata only for clips that still need it", async () =>
134134
expect(getMediaProbeDiagnostics()).toEqual({ cached: 30, failed: 0, inflight: 0 });
135135
expect(elements.slice(0, 20).every((element) => element.sourceDuration === 99)).toBe(true);
136136
expect(elements.slice(20).every((element) => element.sourceDuration === 12)).toBe(true);
137-
expect(changed).toBe(560);
137+
expect(changed).toBe(30); // one write per file
138138
expect(unchanged).toBe(0);
139139
expect(other).toBe(0);
140140
} finally {

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,18 @@ export function useTimelinePlayer({
127127

128128
// Asynchronously enrich media elements still missing sourceDuration
129129
// (header-only probe, cheap), applying each resolved value to the store.
130-
void probeMissingSourceDurations(
131-
mergedElements,
132-
state.timelineProjectId,
133-
(key, durationSeconds) => {
134-
usePlayerStore.setState((state) => {
135-
const idx = state.elements.findIndex((e) => (e.key ?? e.id) === key);
136-
if (idx === -1 || state.elements[idx].sourceDuration != null) return state;
137-
const patched = state.elements.slice();
138-
patched[idx] = { ...state.elements[idx], sourceDuration: durationSeconds };
139-
return { elements: patched };
130+
void probeMissingSourceDurations(mergedElements, state.timelineProjectId, (durations) => {
131+
usePlayerStore.setState((state) => {
132+
let patched: TimelineElement[] | null = null;
133+
state.elements.forEach((element, idx) => {
134+
const durationSeconds = durations.get(element.key ?? element.id);
135+
if (durationSeconds === undefined || element.sourceDuration != null) return;
136+
patched ??= state.elements.slice();
137+
patched[idx] = { ...element, sourceDuration: durationSeconds };
140138
});
141-
},
142-
);
139+
return patched ? { elements: patched } : state;
140+
});
141+
});
143142
},
144143
[setElements, requestTimelineReady, setDuration],
145144
);

‎packages/studio/src/player/lib/mediaProbe.test.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,31 @@ describe("media probe registry", () => {
109109
expect(getDurationFromMetadata).toHaveBeenCalledTimes(2);
110110
});
111111

112+
it("hands back every clip of one file in a single apply, so the store is written once per file", async () => {
113+
const apply = vi.fn();
114+
await probeMissingSourceDurations(
115+
[
116+
{ id: "a1", tag: "audio", src: "assets/voice.wav" },
117+
{ id: "a2", tag: "audio", src: "assets/voice.wav" },
118+
{ id: "a3", tag: "audio", src: "assets/voice.wav" },
119+
{ id: "v1", tag: "video", src: "assets/clip.mp4" },
120+
],
121+
"project-a",
122+
apply,
123+
);
124+
125+
expect(requestedSources).toHaveLength(2);
126+
expect(apply).toHaveBeenCalledTimes(2);
127+
expect(apply).toHaveBeenCalledWith(
128+
new Map([
129+
["a1", 5],
130+
["a2", 5],
131+
["a3", 5],
132+
]),
133+
);
134+
expect(apply).toHaveBeenCalledWith(new Map([["v1", 5]]));
135+
});
136+
112137
it("probes a dropped clip's URL-spelled src at its own file", async () => {
113138
await probeMissingSourceDurations(
114139
[{ id: "clip", tag: "video", src: "assets/My%20clip%20%231.mp4" }],
@@ -138,7 +163,7 @@ describe("media probe registry", () => {
138163
expect(requestedSources).toEqual([
139164
`${window.location.origin}/api/projects/project-a/preview/assets/clip.mp4`,
140165
]);
141-
expect(apply).toHaveBeenCalledWith("clip", 5);
166+
expect(apply).toHaveBeenCalledWith(new Map([["clip", 5]]));
142167
expect(
143168
applyCachedSourceDurations(
144169
[

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,34 +128,35 @@ export function applyCachedSourceDurations<
128128

129129
/**
130130
* Probe (header-only, cheap) any media elements still missing sourceDuration
131-
* after the cache pass, applying each resolved duration via `apply(key, secs)`.
132-
* Skips already-cached srcs.
131+
* after the cache pass. Each file is probed once and all its clips' durations go to one
132+
* `apply(durations)`, so a file cut into many clips writes the store once. Skips already-cached srcs.
133133
*/
134134
export async function probeMissingSourceDurations<
135135
T extends { src?: string; tag: string; sourceDuration?: number; key?: string; id: string },
136136
>(
137137
elements: T[],
138138
projectId: string | null,
139-
apply: (key: string, durationSeconds: number) => void,
139+
apply: (durations: ReadonlyMap<string, number>) => void,
140140
): Promise<void> {
141-
const needs = elements.flatMap((el) => {
141+
const keysBySource = new Map<string, string[]>();
142+
for (const el of elements) {
142143
if (
143144
!el.src ||
144145
el.sourceDuration != null ||
145146
!["video", "audio"].includes(el.tag.toLowerCase())
146147
) {
147-
return [];
148+
continue;
148149
}
149150
const source = resolveProbeSource(el.src, projectId);
150-
return !getCachedProbe(source) && !hasFreshFailure(normalizeUrl(source))
151-
? [{ el, source }]
152-
: [];
153-
});
154-
if (needs.length === 0) return;
151+
if (getCachedProbe(source) || hasFreshFailure(normalizeUrl(source))) continue;
152+
const keys = keysBySource.get(source);
153+
if (keys) keys.push(el.key ?? el.id);
154+
else keysBySource.set(source, [el.key ?? el.id]);
155+
}
155156
await Promise.allSettled(
156-
needs.map(async ({ el, source }) => {
157+
[...keysBySource].map(async ([source, keys]) => {
157158
const result = await probeMediaUrl(source);
158-
if (result) apply(el.key ?? el.id, result.duration);
159+
if (result) apply(new Map(keys.map((key) => [key, result.duration])));
159160
}),
160161
);
161162
}

0 commit comments

Comments
 (0)