mirror of
https://github.com/vector-im/element-call.git
synced 2026-10-10 23:25:52 +00:00
Merge pull request #4308 from element-hq/toger5/fix-component-sounds
[ElementCall Component] Decode inlined sounds directly instead of fetching them
This commit is contained in:
@@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2026 Element Creations Ltd.
|
||||||
|
|
||||||
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||||
|
Please see LICENSE in the repository root for full details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
import { prefetchSounds } from "./soundUtils";
|
||||||
|
|
||||||
|
const bytesOf = (buffer: ArrayBuffer): number[] => [...new Uint8Array(buffer)];
|
||||||
|
|
||||||
|
describe("prefetchSounds", () => {
|
||||||
|
const fetchSpy = vi.fn<typeof fetch>();
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.stubGlobal("fetch", fetchSpy);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
vi.unstubAllGlobals();
|
||||||
|
fetchSpy.mockReset();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("fetches sounds given as ordinary URLs", async () => {
|
||||||
|
fetchSpy.mockResolvedValue(
|
||||||
|
new Response(new Uint8Array([1, 2, 3]), { status: 200 }),
|
||||||
|
);
|
||||||
|
const sounds = await prefetchSounds({
|
||||||
|
beep: { ogg: "https://example.org/beep.ogg" },
|
||||||
|
});
|
||||||
|
expect(fetchSpy).toHaveBeenCalledWith("https://example.org/beep.ogg");
|
||||||
|
expect(bytesOf(sounds.beep)).toEqual([1, 2, 3]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("decodes base64 data: URLs without fetching them", async () => {
|
||||||
|
// "OggS" — the bytes 0x4f 0x67 0x67 0x53
|
||||||
|
const sounds = await prefetchSounds({
|
||||||
|
beep: { ogg: "data:audio/ogg;base64,T2dnUw==" },
|
||||||
|
});
|
||||||
|
expect(fetchSpy).not.toHaveBeenCalled();
|
||||||
|
expect(bytesOf(sounds.beep)).toEqual([0x4f, 0x67, 0x67, 0x53]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("leaves out a sound with a percent-encoded data: URL", async () => {
|
||||||
|
const sounds = await prefetchSounds({
|
||||||
|
beep: { ogg: "data:audio/ogg,Ogg%53" },
|
||||||
|
});
|
||||||
|
expect(fetchSpy).not.toHaveBeenCalled();
|
||||||
|
expect(sounds).toEqual({});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("leaves out a sound whose response is not okay", async () => {
|
||||||
|
fetchSpy.mockResolvedValue(new Response(null, { status: 404 }));
|
||||||
|
const sounds = await prefetchSounds({
|
||||||
|
beep: { ogg: "https://example.org/missing.ogg" },
|
||||||
|
});
|
||||||
|
expect(sounds).toEqual({});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("leaves out a sound that fails to load, keeping the others", async () => {
|
||||||
|
fetchSpy.mockRejectedValue(new TypeError("Failed to fetch"));
|
||||||
|
const sounds = await prefetchSounds({
|
||||||
|
beep: { ogg: "https://example.org/beep.ogg" },
|
||||||
|
boop: { ogg: "data:audio/ogg;base64,T2dnUw==" },
|
||||||
|
});
|
||||||
|
expect(Object.keys(sounds)).toEqual(["boop"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("leaves out a sound with a malformed data: URL", async () => {
|
||||||
|
const sounds = await prefetchSounds({
|
||||||
|
beep: { ogg: "data:audio/ogg;base64" },
|
||||||
|
});
|
||||||
|
expect(sounds).toEqual({});
|
||||||
|
});
|
||||||
|
});
|
||||||
+46
-7
@@ -32,6 +32,43 @@ function getPreferredAudioFormat(): "ogg" | "mp3" {
|
|||||||
|
|
||||||
const preferredFormat = getPreferredAudioFormat();
|
const preferredFormat = getPreferredAudioFormat();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode a `data:` URL into the bytes it carries.
|
||||||
|
*
|
||||||
|
* The component build inlines the sound files as `data:` URLs. Those could be
|
||||||
|
* `fetch`ed like any other URL, but that fetch is subject to the host page's
|
||||||
|
* Content Security Policy, and `connect-src` lists rarely allow `data:` (a `*`
|
||||||
|
* source does not cover it). Reading the bytes out of the URL directly needs no
|
||||||
|
* network access, so no policy can stop it.
|
||||||
|
* @param url The `data:` URL to decode.
|
||||||
|
* @returns The decoded bytes.
|
||||||
|
*/
|
||||||
|
function decodeDataUrl(url: string): ArrayBuffer {
|
||||||
|
const separator = url.indexOf(",");
|
||||||
|
if (!url.startsWith("data:") || separator === -1)
|
||||||
|
throw new Error("Not a valid data: URL");
|
||||||
|
const header = url.slice("data:".length, separator);
|
||||||
|
// Vite only percent-encodes SVGs; every other inlined asset is base64.
|
||||||
|
if (!header.split(";").includes("base64"))
|
||||||
|
throw new Error("Only base64 data: URLs are supported");
|
||||||
|
// `decoded` is a binary string: one char per byte
|
||||||
|
const decoded = atob(url.slice(separator + 1));
|
||||||
|
const bytes = Uint8Array.from(decoded, (c) => c.charCodeAt(0));
|
||||||
|
return bytes.buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load the bytes of a single sound file.
|
||||||
|
* @param url Where the sound is: an ordinary URL to fetch, or a `data:` URL.
|
||||||
|
* @returns The bytes, or null if the sound could not be loaded.
|
||||||
|
*/
|
||||||
|
async function loadSound(url: string): Promise<ArrayBuffer | null> {
|
||||||
|
if (url.startsWith("data:")) return decodeDataUrl(url);
|
||||||
|
const response = await fetch(url);
|
||||||
|
if (isFailure(response)) return null;
|
||||||
|
return await response.arrayBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prefetch sounds to be used by the AudioContext. This can
|
* Prefetch sounds to be used by the AudioContext. This can
|
||||||
* be called outside the scope of a component to ensure the
|
* be called outside the scope of a component to ensure the
|
||||||
@@ -47,18 +84,20 @@ export async function prefetchSounds<S extends string>(
|
|||||||
Object.entries(sounds).map(async ([name, file]) => {
|
Object.entries(sounds).map(async ([name, file]) => {
|
||||||
const { mp3, ogg } = file as SoundDefinition;
|
const { mp3, ogg } = file as SoundDefinition;
|
||||||
// Use preferred format, fallback to ogg if no mp3 is provided.
|
// Use preferred format, fallback to ogg if no mp3 is provided.
|
||||||
// Load an audio file
|
const url = preferredFormat === "ogg" ? ogg : (mp3 ?? ogg);
|
||||||
const response = await fetch(
|
let buffer: ArrayBuffer | null = null;
|
||||||
preferredFormat === "ogg" ? ogg : (mp3 ?? ogg),
|
try {
|
||||||
);
|
buffer = await loadSound(url);
|
||||||
if (isFailure(response)) {
|
} catch (e) {
|
||||||
|
logger.warn(`Could not load sound ${name}`, e);
|
||||||
|
}
|
||||||
|
if (buffer === null) {
|
||||||
// If the sound doesn't load, it's not the end of the world. We won't play
|
// If the sound doesn't load, it's not the end of the world. We won't play
|
||||||
// the sound when requested, but it's better than failing the whole application.
|
// the sound when requested, but it's better than failing the whole application.
|
||||||
logger.warn(`Could not load sound ${name}, response was not okay`);
|
logger.warn(`Could not load sound ${name}, response was not okay`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Decode it
|
buffers[name] = buffer;
|
||||||
buffers[name] = await response.arrayBuffer();
|
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
return buffers as Record<S, ArrayBuffer>;
|
return buffers as Record<S, ArrayBuffer>;
|
||||||
|
|||||||
@@ -156,7 +156,7 @@ export function useAudioContext<S extends string>(
|
|||||||
}
|
}
|
||||||
setAudioBuffers(buffers as Record<S, AudioBuffer>);
|
setAudioBuffers(buffers as Record<S, AudioBuffer>);
|
||||||
})().catch((ex) => {
|
})().catch((ex) => {
|
||||||
logger.debug("Failed to setup audio context", ex);
|
logger.warn("Failed to setup audio context", ex);
|
||||||
});
|
});
|
||||||
|
|
||||||
setAudioContext(ctx);
|
setAudioContext(ctx);
|
||||||
@@ -209,7 +209,9 @@ export function useAudioContext<S extends string>(
|
|||||||
},
|
},
|
||||||
playSoundLooping: (name, delayS: number): (() => Promise<void>) => {
|
playSoundLooping: (name, delayS: number): (() => Promise<void>) => {
|
||||||
if (!audioBuffers[name]) {
|
if (!audioBuffers[name]) {
|
||||||
throw Error(`Tried to play a sound that wasn't buffered (${name})`);
|
// We do not throw here. Instead we log and let the component life. Its not a fatal error if sounds are missing.
|
||||||
|
logger.debug(`Tried to play a sound that wasn't buffered (${name})`);
|
||||||
|
return async () => {};
|
||||||
}
|
}
|
||||||
return playSoundLooping(
|
return playSoundLooping(
|
||||||
audioContext,
|
audioContext,
|
||||||
|
|||||||
Reference in New Issue
Block a user