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 @@ -726,6 +726,98 @@ describe("MediaElementPlaybackSource native playback", () => {
}
);

test("recovery reload canceling a pending load does not report AbortError", async () => {
jest.useFakeTimers();
const browser = installBrowser();
const onError = mock(
(_error: Error, _recoveryPending?: boolean) => undefined
);
const onReady = mock(() => undefined);

try {
const source = new MediaElementPlaybackSource(null, "native", {
onError,
onReady,
});
const audio = browser.audio();
const url = "https://radio.example/icecast-live.mp3";
const loading = source.load({
format: "progressive",
live: true,
src: url,
});
const loadResult = loading.catch((error: unknown) => error);
const playing = source.play();
const playResult = playing.catch((error: unknown) => error);
await flushMicrotasks();

audio.emit("waiting");
jest.advanceTimersByTime(6000);
jest.advanceTimersByTime(0);
await flushMicrotasks();

expect(audio.loadSources).toEqual([url, url]);
// Recovery cancelLoad()-aborts the pending waitUntilReady. That must not
// surface as an unexpected PLAY_ERROR / onError AbortError.
expect(onError).not.toHaveBeenCalled();

audio.emit("canplay");
await flushMicrotasks();
expect(await loadResult).toBeUndefined();
expect(await playResult).toBeUndefined();
expect(onError).not.toHaveBeenCalled();
expect(onReady).toHaveBeenCalled();
expect(source.status).not.toBe("error");
source.cleanup();
} finally {
browser.restore();
}
});

test("a real media error during a pending load still reports onError", async () => {
const browser = installBrowser();
const onError = mock(
(_error: Error, _recoveryPending?: boolean) => undefined
);

try {
const source = new MediaElementPlaybackSource(null, "native", {
onError,
});
const audio = browser.audio();
const loading = source.load({
format: "progressive",
live: true,
src: "https://radio.example/icecast-live.mp3",
});
const loadResult = loading.catch((error: unknown) => error);
const playing = source.play();
const playResult = playing.catch((error: unknown) => error);
await flushMicrotasks();

audio.error = { code: MediaError.MEDIA_ERR_NETWORK } as MediaError;
audio.emit("error");

expect(await loadResult).toHaveProperty(
"message",
"Audio stream failed to load"
);
expect(await playResult).toHaveProperty(
"message",
"Audio stream failed to load"
);
expect(onError).toHaveBeenCalledTimes(1);
expect(onError.mock.calls[0]?.[0]).toHaveProperty(
"message",
"Audio stream failed to load"
);
expect(source.status).toBe("error");
source.cleanup();
} finally {
browser.restore();
}
});

test("reloads after connectivity returns without media progress", async () => {
jest.useFakeTimers();
const browser = installBrowser();
Expand Down
40 changes: 35 additions & 5 deletions apps/radio/src/lib/audio/playback/media-element-playback-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import type {
} from "./playback-source.js";
import type { StreamStatus } from "./types.js";

function isAttachmentAbortError(error: unknown): boolean {
return error instanceof DOMException && error.name === "AbortError";
}

export class MediaElementPlaybackSource implements PlaybackSource {
private readonly callbacks: PlaybackSourceCallbacks;
private readonly sourceId: string;
Expand All @@ -22,6 +26,7 @@ export class MediaElementPlaybackSource implements PlaybackSource {
private _status: StreamStatus = "idle";
private _isBuffering = false;
private generation = 0;
private attachmentEpoch = 0;
private playbackIntent = 0;
private playbackRate = 1;
private shouldResumeAfterLoad: boolean;
Expand Down Expand Up @@ -219,6 +224,12 @@ export class MediaElementPlaybackSource implements PlaybackSource {
throw error;
}

// Recovery may cancel the initial attachment to start a same-generation
// reload. Leave resume to that reload instead of racing play() here.
if (this.recovery.isReloading) {
return;
}

const playResult = await playResultPromise;
if (!playResult.ok) {
const canRecoverPlayError = this.canRecoverPendingPlayError(
Expand Down Expand Up @@ -389,14 +400,33 @@ export class MediaElementPlaybackSource implements PlaybackSource {
input: PlaybackInput,
generation: number
): Promise<void> {
await this.attachment.load(
input,
() => generation === this.generation,
(event) => this.handleFatalHlsEvent(event)
);
this.attachmentEpoch += 1;
const epoch = this.attachmentEpoch;
try {
await this.attachment.load(
input,
() => generation === this.generation && epoch === this.attachmentEpoch,
(event) => this.handleFatalHlsEvent(event)
);
} catch (error) {
// Recovery reload calls loadAttachedSource again without bumping
// generation, which cancelLoad()-aborts this waiter. That AbortError is
// expected; the newer epoch owns readiness and error reporting.
if (
isAttachmentAbortError(error) &&
generation === this.generation &&
epoch !== this.attachmentEpoch
) {
return;
}
throw error;
}
if (generation !== this.generation) {
throw new DOMException("Load aborted", "AbortError");
}
if (epoch !== this.attachmentEpoch) {
return;
}
this._status = this.audio.paused ? "buffering" : "streaming";
this.setBuffering(false);
this.callbacks.onReady?.();
Expand Down
Loading