mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-26 00:57:05 +00:00
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 <noreply@anthropic.com> * chore(studio): trim comments in the media fetch options Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
57ffe0b07d
commit
d04d8fe468
@@ -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]));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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<Format>(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<number | null, number>();
|
||||
const inFlightReads = new AbortController();
|
||||
@@ -183,6 +209,7 @@ export function mediaUrlSourceOptions(reader: MediaReader) {
|
||||
const fetchFn = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
|
||||
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);
|
||||
};
|
||||
|
||||
@@ -16,7 +16,8 @@ const requestedSources: string[] = [];
|
||||
const sourceRetryRules: string[] = [];
|
||||
|
||||
vi.mock("mediabunny", () => ({
|
||||
ALL_FORMATS: {},
|
||||
ALL_FORMATS: [],
|
||||
HLS: {},
|
||||
UrlSource: class {
|
||||
constructor(
|
||||
readonly url: string,
|
||||
|
||||
@@ -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<MediaProbeResult | null> {
|
||||
|
||||
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();
|
||||
|
||||
@@ -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 }),
|
||||
|
||||
@@ -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<Reader> => {
|
||||
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();
|
||||
|
||||
@@ -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<OpenedS
|
||||
const options = mediaUrlSourceOptions("thumbnail");
|
||||
const input = new mediabunny.Input({
|
||||
source: new mediabunny.UrlSource(source, options),
|
||||
formats: mediabunny.ALL_FORMATS,
|
||||
formats: selfContainedFormats(mediabunny),
|
||||
});
|
||||
entry.input = input;
|
||||
entry.cancelReads = options.cancelReads;
|
||||
|
||||
@@ -29,7 +29,8 @@ const input = {
|
||||
let servedEtag: string | undefined;
|
||||
|
||||
vi.mock("mediabunny", () => ({
|
||||
ALL_FORMATS: {},
|
||||
ALL_FORMATS: [],
|
||||
HLS: {},
|
||||
UrlSource: class {
|
||||
constructor(
|
||||
readonly url: string,
|
||||
|
||||
Reference in New Issue
Block a user