diff --git a/apps/radio/src/lib/audio/manager/audio-manager.test.ts b/apps/radio/src/lib/audio/manager/audio-manager.test.ts index 1efa4f996..cc94bf808 100644 --- a/apps/radio/src/lib/audio/manager/audio-manager.test.ts +++ b/apps/radio/src/lib/audio/manager/audio-manager.test.ts @@ -139,6 +139,34 @@ afterEach(() => { }); describe("AudioManager", () => { + // RADIO-22: single-mode playSound must go through toPlaybackInput so a + // persisted RadioJar mount (including query variants) never reaches media. + test("single-mode playSound remounts a persisted Radio Alhara RadioJar URL", async () => { + const harness = createMediaPlaybackHarness(); + try { + const { manager, browser, effects } = harness; + effects.connectGraph = mock(async () => true); + const soundId = manager.createSound( + { + name: "Radio Alhara", + streamUrl: "https://n03.radiojar.com/78cxy6wkxtzuv?source=radio", + }, + "single:alhara", + "native", + "station" + ); + const starting = manager.playSound(soundId); + await flushMicrotasks(); + expect(browser.audio().loadSources).toEqual([ + "https://stream.radioalhara.net/ra", + ]); + browser.audio().emit("canplay"); + await starting; + } finally { + harness.restore(); + } + }); + test.each([ [Number.POSITIVE_INFINITY, 1], [120, 1], diff --git a/apps/radio/src/lib/audio/playback/media-element-playback-source.test.ts b/apps/radio/src/lib/audio/playback/media-element-playback-source.test.ts index 2c20a4642..a47bb3ade 100644 --- a/apps/radio/src/lib/audio/playback/media-element-playback-source.test.ts +++ b/apps/radio/src/lib/audio/playback/media-element-playback-source.test.ts @@ -342,6 +342,60 @@ describe("MediaElementPlaybackSource native playback", () => { } ); + // RADIO-22: Chrome maps the retired RadioJar 404 to MEDIA_ERR_SRC_NOT_SUPPORTED. + // Single-mode loads through toPlaybackInput; the override must keep that mount + // off the media element so readiness never rejects with PLAY_ERROR. + test.each([ + "https://n03.radiojar.com/78cxy6wkxtzuv", + "https://n03.radiojar.com/78cxy6wkxtzuv?source=radio", + ])( + "single-mode Radio Alhara loads never attach the retired RadioJar mount (%s)", + async (retiredMount) => { + const browser = installBrowser(); + const onError = mock(() => undefined); + const source = new MediaElementPlaybackSource(null, "native", { + onError, + }); + const currentMount = "https://stream.radioalhara.net/ra"; + try { + const audio = browser.audio(); + audio.load = () => { + if (audio.src.includes("n03.radiojar.com/78cxy6wkxtzuv")) { + queueMicrotask(() => { + audio.error = { + code: MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED, + } as MediaError; + audio.emit("error"); + }); + return; + } + if (audio.src !== "") { + queueMicrotask(() => { + audio.error = null; + audio.emit("canplay"); + }); + } + }; + const input = toPlaybackInput({ + name: "Radio Alhara", + streamUrl: retiredMount, + }); + await source.load(input); + expect(input).toMatchObject({ + format: "progressive", + src: currentMount, + }); + expect(audio.loadSources).toEqual([currentMount]); + expect(audio.src).toBe(currentMount); + expect(onError).not.toHaveBeenCalled(); + expect(source.status).not.toBe("error"); + } finally { + source.cleanup(); + browser.restore(); + } + } + ); + test("a failed start stays stopped when late media events arrive", async () => { const browser = installBrowser(); const onPlaying = mock(() => undefined); diff --git a/apps/radio/src/lib/audio/playback/playback-input.test.ts b/apps/radio/src/lib/audio/playback/playback-input.test.ts index 79774620b..cb9712fad 100644 --- a/apps/radio/src/lib/audio/playback/playback-input.test.ts +++ b/apps/radio/src/lib/audio/playback/playback-input.test.ts @@ -50,22 +50,58 @@ describe("toPlaybackInput", () => { }); test("upgrades persisted Radio Alhara URLs to its first-party stream", () => { + const currentMount = "https://stream.radioalhara.net/ra"; + for (const streamUrl of [ + "https://n03.radiojar.com/78cxy6wkxtzuv", + "https://n03.radiojar.com/78cxy6wkxtzuv?source=radio", + "https://N03.RadioJar.com/78cxy6wkxtzuv#legacy", + "https://n03.radiojar.com:443/78cxy6wkxtzuv/", + ]) { + expect( + toPlaybackInput({ + name: "Radio Alhara", + streamUrl, + }) + ).toMatchObject({ + format: "progressive", + live: true, + src: currentMount, + }); + } + expect( + toPlaybackInput({ + name: "Lookalike", + streamUrl: "https://example.com/78cxy6wkxtzuv", + }) + ).toMatchObject({ src: "https://example.com/78cxy6wkxtzuv" }); expect( toPlaybackInput({ - name: "Radio Alhara", - streamUrl: "https://n03.radiojar.com/78cxy6wkxtzuv", + name: "Embedded path", + streamUrl: + "https://evil.example/https://n03.radiojar.com/78cxy6wkxtzuv", }) ).toMatchObject({ - format: "progressive", - live: true, - src: "https://stream.radioalhara.net/ra", + src: "https://evil.example/https://n03.radiojar.com/78cxy6wkxtzuv", }); expect( toPlaybackInput({ - name: "Lookalike", - streamUrl: "https://example.com/78cxy6wkxtzuv", + name: "Path sibling", + streamUrl: "https://n03.radiojar.com/78cxy6wkxtzuv/extra", }) - ).toMatchObject({ src: "https://example.com/78cxy6wkxtzuv" }); + ).toMatchObject({ + src: "https://n03.radiojar.com/78cxy6wkxtzuv/extra", + }); + }); + + test.each([ + "https://n03.radiojar.com:8443/78cxy6wkxtzuv", + "http://n03.radiojar.com:8080/78cxy6wkxtzuv?source=radio", + "https://radio.syg.ma:8443/audio.ogg", + "https://stream-relay-geo.ntslive.net:8443/stream", + ])("preserves a distinct stream origin (%s)", (streamUrl) => { + expect(toPlaybackInput({ name: "Other origin", streamUrl })).toMatchObject({ + src: streamUrl, + }); }); test("upgrades persisted NTS relay URLs to Web Audio compatible sources", () => { diff --git a/apps/radio/src/lib/audio/playback/playback-input.ts b/apps/radio/src/lib/audio/playback/playback-input.ts index 8c0f48b3a..ef8620484 100644 --- a/apps/radio/src/lib/audio/playback/playback-input.ts +++ b/apps/radio/src/lib/audio/playback/playback-input.ts @@ -25,13 +25,44 @@ const STREAM_URL_OVERRIDES: Readonly> = { "https://streams.radiomast.io/nts2", }; +/** Preserve the origin; query/hash and trailing slash must not bypass. */ +function streamOverrideIdentity(url: string): string | null { + try { + const parsed = new URL(url); + if (parsed.protocol !== "http:" && parsed.protocol !== "https:") { + return null; + } + const path = + parsed.pathname.length > 1 && parsed.pathname.endsWith("/") + ? parsed.pathname.slice(0, -1) + : parsed.pathname; + return `${parsed.origin}${path}`; + } catch { + return null; + } +} + +const STREAM_URL_OVERRIDE_BY_IDENTITY = new Map( + Object.entries(STREAM_URL_OVERRIDES).flatMap(([from, to]) => { + const identity = streamOverrideIdentity(from); + return identity ? [[identity, to] as const] : []; + }) +); + +function resolveStreamUrlOverride(streamUrl: string): string | undefined { + return ( + STREAM_URL_OVERRIDES[streamUrl] ?? + STREAM_URL_OVERRIDE_BY_IDENTITY.get(streamOverrideIdentity(streamUrl) ?? "") + ); +} + export function toPlaybackInput( radio: Radio, sourceKind?: RadioSourceKind ): PlaybackInput { const src = isRadioBlackoutStreamUrl(radio.streamUrl) ? RADIO_BLACKOUT_STREAM_URL - : (STREAM_URL_OVERRIDES[radio.streamUrl] ?? radio.streamUrl); + : (resolveStreamUrlOverride(radio.streamUrl) ?? radio.streamUrl); const format = radio.streamFormat ?? (radio.platformMetadata?.platform === "radio-browser" && diff --git a/apps/radio/src/lib/node-playback.test.ts b/apps/radio/src/lib/node-playback.test.ts index 8179d11af..6912e0171 100644 --- a/apps/radio/src/lib/node-playback.test.ts +++ b/apps/radio/src/lib/node-playback.test.ts @@ -1562,7 +1562,7 @@ describe("Node Playback starts", () => { expect(getPlaybackChannelRuntime(channelOf("bad")).error).toMatchObject({ code: "PLAY_ERROR", message: - "The stream could not be reached. Check the station URL and try again.", + "Playback could not start. Check the station stream and try again.", }); expect(harness.context.reportError).toHaveBeenCalledTimes(1); expect(harness.context.reportError).toHaveBeenCalledWith( diff --git a/apps/radio/src/lib/playback-action-errors.test.ts b/apps/radio/src/lib/playback-action-errors.test.ts index a28c42763..c3ae25ce7 100644 --- a/apps/radio/src/lib/playback-action-errors.test.ts +++ b/apps/radio/src/lib/playback-action-errors.test.ts @@ -21,6 +21,9 @@ test("support limits preserve guidance; ordinary media failures still reach Sent "The operation is not supported.", "NotSupportedError" ); + // RADIO-22 symptom: MEDIA_ERR_SRC_NOT_SUPPORTED mapped by mediaElementError. + // Keep reporting unexpected stream failures; do not silence the diagnostic. + const unsupportedStream = new Error("Audio stream format is not supported"); try { expect(toRuntimeAudioError(support).message).toBe(support.safeMessage); capturePlaybackActionError( @@ -35,6 +38,23 @@ test("support limits preserve guidance; ordinary media failures still reach Sent ); expect(capture).toHaveBeenCalledTimes(1); expect(capture.mock.calls[0]?.[0]).toBe(failure); + const unsupportedAction = createPlaybackActionError({ + cause: unsupportedStream, + mode: "single", + }); + expect(unsupportedAction.userMessage).toBe( + "This audio format is not supported. Check the station stream or try another source." + ); + const unreachableAction = createPlaybackActionError({ + cause: new Error("Audio stream failed to load"), + mode: "single", + }); + expect(unreachableAction.userMessage).toBe( + "The stream could not be reached. Check the station URL and try again." + ); + capturePlaybackActionError(unsupportedAction); + expect(capture).toHaveBeenCalledTimes(2); + expect(capture.mock.calls[1]?.[0]).toBe(unsupportedStream); } finally { capture.mockRestore(); enabled.mockRestore(); diff --git a/apps/radio/src/lib/playback-action-errors.ts b/apps/radio/src/lib/playback-action-errors.ts index f68a69a16..5f64073dc 100644 --- a/apps/radio/src/lib/playback-action-errors.ts +++ b/apps/radio/src/lib/playback-action-errors.ts @@ -94,7 +94,22 @@ function getFriendlyPlaybackErrorMessage( ) { return "Playback source is no longer available. Reload the station and try again."; } - if (messageIncludesAny(rawMessage, ["network", "fetch", "stream"])) { + // mediaElementError phrases: format/decode before reachability. Do not match + // bare "stream" — that mislabels format failures as URL problems. + if (messageIncludesAny(rawMessage, ["format is not supported"])) { + return "This audio format is not supported. Check the station stream or try another source."; + } + if (messageIncludesAny(rawMessage, ["could not be decoded"])) { + return "The stream could not be decoded. Try another station or format."; + } + if ( + messageIncludesAny(rawMessage, [ + "network", + "fetch", + "failed to load", + "timed out", + ]) + ) { return "The stream could not be reached. Check the station URL and try again."; }