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

test("a recovery reload does not reject an in-flight play for the same resume intent", async () => {
jest.useFakeTimers();
const browser = installBrowser();
const source = new MediaElementPlaybackSource(null, "native");
try {
const audio = browser.audio();
await startNativeStream(source, audio, "https://radio.example/live.mp3");
source.pause();
const pending = Promise.withResolvers<void>();
audio.play = () => {
audio.paused = false;
return pending.promise;
};
const result = source.play().catch((error: unknown) => error);
audio.emit("waiting");
jest.advanceTimersByTime(6000);
jest.advanceTimersByTime(0);
await flushMicrotasks();
// Recovery reattaches through loadAttachedSource without going through
// load(), so the browser rejects the earlier play() with this AbortError.
pending.reject(
new DOMException(
"The play() request was interrupted by a new load request.",
"AbortError"
)
);
expect(await result).toBeUndefined();
expect(source.status).not.toBe("error");
} finally {
source.cleanup();
browser.restore();
}
});

test.each(["AbortError", "NotAllowedError", "NotSupportedError"])(
"an active play still reports %s",
async (name) => {
Expand Down
19 changes: 15 additions & 4 deletions apps/radio/src/lib/audio/playback/media-element-playback-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,18 @@ export class MediaElementPlaybackSource implements PlaybackSource {
this.setBuffering(false);
this.callbacks.onError?.(error, recoveryPending);
},
reload: (input, generation) => this.loadAttachedSource(input, generation),
reload: (input, generation) => {
// Recovery reattaches without load(), so it must still retire any
// in-flight play() whose browser promise the reattach will abort.
// Keep shouldResumeAfterLoad so resume can continue afterward.
this.supersedeInFlightPlay();
return this.loadAttachedSource(input, generation);
},
resume: async () => {
this.audio.autoplay = true;
this.shouldResumeAfterLoad = true;
if (this.audio.paused) {
await this.audio.play();
await this.playForIntent(this.playbackIntent);
}
},
setState: (status, isBuffering) => {
Expand Down Expand Up @@ -435,15 +442,19 @@ export class MediaElementPlaybackSource implements PlaybackSource {
private cancelPendingPlaybackIntent(): void {
this.shouldResumeAfterLoad = false;
this.audio.autoplay = false;
this.supersedeInFlightPlay();
}

private supersedeInFlightPlay(): void {
this.playbackIntent += 1;
}

private async playForIntent(playbackIntent: number): Promise<void> {
try {
await this.audio.play();
} catch (error) {
// A later pause, stop, load or play owns the element now. The browser
// can reject the old play promise after that action has completed.
// A later pause, stop, load, play or recovery reload owns the element
// now. The browser can reject the old play promise after that action.
if (!this.isPlaybackIntentCanceled(playbackIntent)) {
throw error;
}
Expand Down
Loading