perf(core): open the preview's audio context when a clip with sound plays (#7658)

This commit is contained in:
Miguel Ángel
2026-11-13 18:56:47 +00:00
committed by GitHub
parent 0c2384db88
commit ccdcf6710c
4 changed files with 103 additions and 9 deletions
@@ -2165,7 +2165,7 @@ describe("the preview's scene window", () => {
const audio = (src: string) => `<audio src="${src}" data-start="0.5" data-duration="1"></audio>`;
it("lets a scene mounting ahead of a playing film join its mix, leaving what already plays alone", async () => {
vi.spyOn(WebAudioTransport.prototype, "init").mockResolvedValue(true);
vi.spyOn(WebAudioTransport.prototype, "open").mockReturnValue(true);
const schedule = vi
.spyOn(WebAudioTransport.prototype, "scheduleMediaElementPlayback")
.mockResolvedValue(null);
+13 -8
View File
@@ -666,10 +666,8 @@ export function initSandboxRuntimeModular(): void {
const clock = new TransportClock();
state.transportClock = clock;
const webAudio = new WebAudioTransport();
let webAudioReady = false;
void webAudio.init().then((ok) => {
webAudioReady = ok;
});
let webAudioOpened: boolean | null = null;
const openWebAudio = () => (webAudioOpened ??= webAudio.open());
window.__hf = window.__hf || {};
/** Hidden by an ancestor, or by the BUS this clip belongs to. The bus is
* never an ancestor — membership is on the member's `data-audio-group` — so
@@ -4513,8 +4511,10 @@ export function initSandboxRuntimeModular(): void {
const pageAnimations = seekTimelineAndAdapters(state.currentTime);
runAdapters("pause", 0, pageAnimations);
runAdapters("play");
if (webAudioReady && !state.nativeMediaSyncDisabled && !state.webAudioMediaDisabled)
scheduleWebAudioFor(hosts.flatMap(webAudioMediaIn), webAudio.currentGeneration());
const clips = hosts.flatMap(webAudioMediaIn);
const mayRoute =
clips.length > 0 && !state.nativeMediaSyncDisabled && !state.webAudioMediaDisabled;
if (mayRoute && openWebAudio()) scheduleWebAudioFor(clips, webAudio.currentGeneration());
};
// What a load does after scene markup changed: bindings, media, captions, the root timeline, then a redraw.
const settleChangedScenes = (
@@ -5241,7 +5241,12 @@ export function initSandboxRuntimeModular(): void {
// Schedule audio through WebAudio for sample-accurate timing.
// Falls back to HTMLMediaElement playback if WebAudio isn't ready
// or decoding fails (the syncRuntimeMedia path handles that).
if (webAudioReady && !state.nativeMediaSyncDisabled && !state.webAudioMediaDisabled) {
if (
!state.nativeMediaSyncDisabled &&
!state.webAudioMediaDisabled &&
webAudioMediaIn(document).length > 0 &&
openWebAudio()
) {
scheduleWebAudioForActiveClips();
}
runAdapters("play");
@@ -6554,7 +6559,7 @@ export function initSandboxRuntimeModular(): void {
changed &&
!state.nativeMediaSyncDisabled &&
!state.webAudioMediaDisabled &&
webAudioReady &&
webAudioOpened === true &&
clock.isPlaying() &&
(webAudio.hasBoundedActiveSources() || webAudio.hasHandedOverSources())
) {
@@ -0,0 +1,83 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { initSandboxRuntimeModular } from "./init";
import { createMockTimeline, resetRuntimeFixtureDom } from "./runtimeSeekFixture.test-helpers";
// Opening an AudioContext opens the audio device on the page's thread; on Windows that took up to 7 s.
describe("the audio context opens at the first clip that needs it", () => {
let built = 0;
const original = (globalThis as Record<string, unknown>).AudioContext;
beforeEach(() => {
resetRuntimeFixtureDom();
built = 0;
(globalThis as Record<string, unknown>).AudioContext = class {
state = "running";
destination = {};
currentTime = 0;
constructor() {
built += 1;
}
resume = () => Promise.resolve();
suspend = () => Promise.resolve();
close = () => Promise.resolve();
createGain = () => ({ gain: { value: 1 }, connect() {}, disconnect() {} });
createMediaElementSource = () => ({ connect() {}, disconnect() {} });
};
});
afterEach(() => {
(globalThis as Record<string, unknown>).AudioContext = original;
vi.restoreAllMocks();
});
const mount = (withClip: boolean) => {
const root = document.createElement("div");
root.setAttribute("data-composition-id", "main");
root.setAttribute("data-root", "true");
root.setAttribute("data-start", "0");
root.setAttribute("data-duration", "10");
root.setAttribute("data-width", "1920");
root.setAttribute("data-height", "1080");
document.body.appendChild(root);
if (withClip) {
const audio = document.createElement("audio");
audio.setAttribute("data-start", "0");
audio.setAttribute("data-duration", "10");
audio.setAttribute("src", "/assets/track.mp3");
audio.load = () => {};
audio.play = vi.fn(() => Promise.resolve());
root.appendChild(audio);
}
window.__timelines = { main: createMockTimeline(10) };
};
const flush = async () => {
for (let turn = 0; turn < 4; turn++) await Promise.resolve();
};
it("builds none while a page with sound only loads", async () => {
mount(true);
initSandboxRuntimeModular();
await flush();
expect(built, "a paused page built an AudioContext").toBe(0);
});
it("builds none when a page with no sound plays", async () => {
mount(false);
initSandboxRuntimeModular();
window.__player?.play();
await flush();
expect(built, "a page with no clips built an AudioContext").toBe(0);
});
it("builds one when a page with sound plays, however often it plays", async () => {
mount(true);
initSandboxRuntimeModular();
window.__player?.play();
await flush();
window.__player?.pause();
window.__player?.play();
await flush();
expect(built).toBe(1);
});
});
@@ -297,6 +297,12 @@ export class WebAudioTransport {
private _restTimer: ReturnType<typeof setTimeout> | null = null;
async init(): Promise<boolean> {
return this.open();
}
/** Builds the context on the calling thread: on Windows that opens the audio device and can block seconds. */
open(): boolean {
if (this._ctx) return true;
try {
this._ctx = new AudioContext();
this._ctx.onstatechange = () => this.rest();