From d04d8fe4682b3672b13083da3c82e5f6966e9b78 Mon Sep 17 00:00:00 2001 From: James Russo Date: Tue, 24 Nov 2026 03:33:02 +0000 Subject: [PATCH] fix(studio): a media reader reads only the file it was opened on (#8630) * fix(studio): a media reader reads only the file it was opened on Studio's probe, thumbnail and paused-frame readers accepted every input format the media library ships, including playlists, which name further addresses to read. They now accept only formats that hold their media in the file itself, and one reader's fetch reads one address. Co-Authored-By: Claude Fable 5.2 * chore(studio): trim comments in the media fetch options Co-Authored-By: Claude Fable 5.2 --------- Co-authored-by: Claude Fable 5.2 --- .../studio/src/player/lib/mediaFetch.test.ts | 69 ++++++++++++++++++- packages/studio/src/player/lib/mediaFetch.ts | 33 +++++++-- .../studio/src/player/lib/mediaProbe.test.ts | 3 +- packages/studio/src/player/lib/mediaProbe.ts | 4 +- .../src/player/lib/pausedVideoDecoder.test.ts | 6 +- .../src/player/lib/pausedVideoDecoder.ts | 8 ++- .../src/player/lib/thumbnailSourcePool.ts | 4 +- .../player/lib/thumbnailVideoDecoder.test.ts | 3 +- 8 files changed, 116 insertions(+), 14 deletions(-) diff --git a/packages/studio/src/player/lib/mediaFetch.test.ts b/packages/studio/src/player/lib/mediaFetch.test.ts index bf94916572..0ac854802a 100644 --- a/packages/studio/src/player/lib/mediaFetch.test.ts +++ b/packages/studio/src/player/lib/mediaFetch.test.ts @@ -1,5 +1,10 @@ import { afterEach, describe, expect, it, vi } from "vitest"; -import { MediaFetchError, mediaUrlSourceOptions, reportMediaFetchFailure } from "./mediaFetch"; +import { + MediaFetchError, + mediaUrlSourceOptions, + reportMediaFetchFailure, + selfContainedFormats, +} from "./mediaFetch"; const URL_WITH_PATH = "http://127.0.0.1:5190/api/projects/demo/preview/clips/take-1.mp4"; @@ -738,3 +743,65 @@ describe("reportMediaFetchFailure", () => { ]); }); }); + +describe("one source reads one address", () => { + const OTHER = "http://127.0.0.1:5190/api/projects/other/preview/clips/take-2.mp4"; + const PLAYLIST = `#EXTM3U\n#EXT-X-VERSION:3\n#EXT-X-TARGETDURATION:4\n#EXTINF:4,\n${OTHER}\n#EXT-X-ENDLIST\n`; + + function playlistServer() { + const asked: string[] = []; + vi.stubGlobal( + "fetch", + vi.fn(async (input: RequestInfo | URL) => { + asked.push(String(input)); + return new Response(PLAYLIST, { status: 200 }); + }), + ); + return asked; + } + + it("refuses a read of a second address without sending it, and does not retry it", async () => { + const sent = vi.fn(async () => new Response("", { status: 206 })); + vi.stubGlobal("fetch", sent); + const { fetchFn, getRetryDelay } = mediaUrlSourceOptions("thumbnail"); + await fetchFn(URL_WITH_PATH); + + const error = await fetchFn(OTHER).catch((e: unknown) => e); + + expect(error).toMatchObject({ name: "MediaFetchError", status: null, code: "OtherAddress" }); + expect(String((error as Error).message)).not.toContain("take-2"); + expect(getRetryDelay(1, error)).toBeNull(); + expect(sent).toHaveBeenCalledTimes(1); + await expect(fetchFn(new URL(URL_WITH_PATH))).resolves.toBeInstanceOf(Response); + }); + + it("fails a file whose content is a playlist, having read only the address it was given", async () => { + const asked = playlistServer(); + const mb = await import("mediabunny"); + const input = new mb.Input({ + source: new mb.UrlSource(URL_WITH_PATH), // the library's own fetch: nothing but the formats stops a second read + formats: selfContainedFormats(mb), + }); + + const track = await input.getPrimaryVideoTrack().catch((e: unknown) => e); + input.dispose(); + + expect(track).toBeInstanceOf(Error); + expect(new Set(asked)).toEqual(new Set([URL_WITH_PATH])); + }); + + it("sends nothing to the address a playlist names, even when playlists are read", async () => { + const asked = playlistServer(); + const mb = await import("mediabunny"); + const input = new mb.Input({ + source: new mb.UrlSource(URL_WITH_PATH, mediaUrlSourceOptions("thumbnail")), + formats: mb.ALL_FORMATS, + }); + + const track = await input.getPrimaryVideoTrack().catch((e: unknown) => e); + input.dispose(); + + expect(track).toBeInstanceOf(Error); + expect(new Set(asked)).toEqual(new Set([URL_WITH_PATH])); + }); +}); diff --git a/packages/studio/src/player/lib/mediaFetch.ts b/packages/studio/src/player/lib/mediaFetch.ts index c8629709a7..1f900f67b4 100644 --- a/packages/studio/src/player/lib/mediaFetch.ts +++ b/packages/studio/src/player/lib/mediaFetch.ts @@ -168,11 +168,37 @@ function typedResponse(response: Response, asked: number, request: WatchedReques return new Response(body, { status, statusText, headers }); } +const OTHER_ADDRESS = "OtherAddress"; + +const addressOf = (input: RequestInfo | URL): string => + typeof input === "string" ? input : input instanceof URL ? input.href : input.url; + +function oneAddress(reader: MediaReader) { + let address: string | undefined; + return (input: RequestInfo | URL, asked: number): void => { + address ??= addressOf(input); + if (addressOf(input) !== address) throw new MediaFetchError(reader, null, OTHER_ADDRESS, asked); + }; +} + +const neverRetried = (error: unknown): boolean => + error instanceof MediaFetchError && + (error.code === OTHER_ADDRESS || (error.status !== null && error.status < 500)); + +/** A playlist names other addresses to read, so it is left out of the formats Studio reads. */ +export function selfContainedFormats(mediabunny: { + ALL_FORMATS: Format[]; + HLS: Format; +}): Format[] { + return mediabunny.ALL_FORMATS.filter((format) => format !== mediabunny.HLS); +} + /** - * Options for a mediabunny `UrlSource`. Its default retries a same-origin network failure + * Options for one mediabunny `UrlSource`. Its default retries a same-origin network failure * forever and logs a bare `TypeError` each time; these retry twice and fail with a MediaFetchError. */ export function mediaUrlSourceOptions(reader: MediaReader) { + const only = oneAddress(reader); // Per start byte: a resume asks for a new start, while asking for one again uses its budget up. const failuresByStart = new Map(); const inFlightReads = new AbortController(); @@ -183,6 +209,7 @@ export function mediaUrlSourceOptions(reader: MediaReader) { const fetchFn = async (input: RequestInfo | URL, init?: RequestInit): Promise => { const sent = ranges === "readable" ? boundedRange(init) : init; const asked = Number(rangeStart(sent, true) ?? 0); + only(input, asked); const request = watchedRequest( reader, init?.signal ? [inFlightReads.signal, init.signal] : [inFlightReads.signal], @@ -197,10 +224,8 @@ export function mediaUrlSourceOptions(reader: MediaReader) { if (isAbort(error)) return null; const start = error instanceof MediaFetchError ? error.start : null; const failed = failuresByStart.get(start) ?? 0; - const clientError = - error instanceof MediaFetchError && error.status !== null && error.status < 500; if (error instanceof MediaFetchError) error.setRetries(failed); - if (clientError || failed >= MAX_RETRIES) return null; + if (neverRetried(error) || failed >= MAX_RETRIES) return null; failuresByStart.set(start, failed + 1); return 0.5 * (failed + 1); }; diff --git a/packages/studio/src/player/lib/mediaProbe.test.ts b/packages/studio/src/player/lib/mediaProbe.test.ts index de25ffa168..1b9387620f 100644 --- a/packages/studio/src/player/lib/mediaProbe.test.ts +++ b/packages/studio/src/player/lib/mediaProbe.test.ts @@ -16,7 +16,8 @@ const requestedSources: string[] = []; const sourceRetryRules: string[] = []; vi.mock("mediabunny", () => ({ - ALL_FORMATS: {}, + ALL_FORMATS: [], + HLS: {}, UrlSource: class { constructor( readonly url: string, diff --git a/packages/studio/src/player/lib/mediaProbe.ts b/packages/studio/src/player/lib/mediaProbe.ts index bf9ff3ac68..eb68c4f910 100644 --- a/packages/studio/src/player/lib/mediaProbe.ts +++ b/packages/studio/src/player/lib/mediaProbe.ts @@ -1,6 +1,6 @@ import { authoredSrcPath, resolveMediaPreviewUrl } from "../components/thumbnailUtils"; import { TIMELINE_VIEWPORT_BUDGETS } from "./timelineViewportBudgets"; -import { mediaUrlSourceOptions, reportMediaFetchFailure } from "./mediaFetch"; +import { mediaUrlSourceOptions, reportMediaFetchFailure, selfContainedFormats } from "./mediaFetch"; export interface MediaProbeResult { duration: number; @@ -57,7 +57,7 @@ async function probeOne(url: string): Promise { const input = new mb.Input({ source: new mb.UrlSource(url, mediaUrlSourceOptions("probe")), - formats: mb.ALL_FORMATS, + formats: selfContainedFormats(mb), }); try { const duration = await input.getDurationFromMetadata(); diff --git a/packages/studio/src/player/lib/pausedVideoDecoder.test.ts b/packages/studio/src/player/lib/pausedVideoDecoder.test.ts index a3c7e0dd0f..1140678afa 100644 --- a/packages/studio/src/player/lib/pausedVideoDecoder.test.ts +++ b/packages/studio/src/player/lib/pausedVideoDecoder.test.ts @@ -20,6 +20,7 @@ let displayScale = 1; vi.mock("mediabunny", () => ({ ALL_FORMATS: [], + HLS: {}, UrlSource: class { constructor(readonly url: string) {} }, @@ -55,7 +56,10 @@ vi.mock("mediabunny", () => ({ } }, })); -vi.mock("./mediaFetch", () => ({ mediaUrlSourceOptions: () => ({ cancelReads }) })); +vi.mock("./mediaFetch", () => ({ + mediaUrlSourceOptions: () => ({ cancelReads }), + selfContainedFormats: () => [], +})); vi.mock("../store/playerStore", () => ({ usePlayerStore: { getState: () => ({ previewQuality, previewQualityChosenBy: qualityChosenBy }), diff --git a/packages/studio/src/player/lib/pausedVideoDecoder.ts b/packages/studio/src/player/lib/pausedVideoDecoder.ts index fff60e5653..191d2e0df5 100644 --- a/packages/studio/src/player/lib/pausedVideoDecoder.ts +++ b/packages/studio/src/player/lib/pausedVideoDecoder.ts @@ -1,5 +1,6 @@ import { ALL_FORMATS, + HLS, Input, UrlSource, VideoSample, @@ -9,7 +10,7 @@ import { import type { PausedVideoFrame } from "@hyperframes/core"; import { PAUSED_FRAME_SOURCES } from "@hyperframes/core/runtime/editing-port"; import { untaggedVideoMatrix } from "@hyperframes/parsers/media-streams"; -import { mediaUrlSourceOptions } from "./mediaFetch"; +import { mediaUrlSourceOptions, selfContainedFormats } from "./mediaFetch"; import { previewDecodeTime, previewPresentationTime, @@ -162,7 +163,10 @@ export function createPausedVideoDecoder(): PausedVideoDecoder { }; const open = async (source: string): Promise => { const options = mediaUrlSourceOptions("thumbnail"); - const input = new Input({ source: new UrlSource(source, options), formats: ALL_FORMATS }); + const input = new Input({ + source: new UrlSource(source, options), + formats: selfContainedFormats({ ALL_FORMATS, HLS }), + }); opening.set(input, options.cancelReads); try { const track = await input.getPrimaryVideoTrack(); diff --git a/packages/studio/src/player/lib/thumbnailSourcePool.ts b/packages/studio/src/player/lib/thumbnailSourcePool.ts index 87a02488f8..30e41d4bec 100644 --- a/packages/studio/src/player/lib/thumbnailSourcePool.ts +++ b/packages/studio/src/player/lib/thumbnailSourcePool.ts @@ -1,5 +1,5 @@ import type { Input, InputVideoTrack } from "mediabunny"; -import { mediaUrlSourceOptions } from "./mediaFetch"; +import { mediaUrlSourceOptions, selfContainedFormats } from "./mediaFetch"; export interface SourceInfo { aspect: number; @@ -72,7 +72,7 @@ async function open(source: string, pooled: () => PooledSource): Promise ({ - ALL_FORMATS: {}, + ALL_FORMATS: [], + HLS: {}, UrlSource: class { constructor( readonly url: string,