Skip to content

Commit ca00c70

Browse files
vanceingallsclaude
andcommitted
fix(studio,player,core): eliminate double audio and manifest polling loop
Three bugs that compound in Studio preview: 1. **Double audio on pause/resume**: syncRuntimeMedia played audio through the HTML <audio> element while WebAudioTransport simultaneously played the same source through AudioBufferSourceNode. Fixed by passing webAudio.isActive() as outputMuted so HTML elements stay muted when Web Audio owns playback. Also removed the priorMuted restore in stopAll() which raced with the next play cycle. 2. **Manifest polling loop**: applyStudioManualEditsToPreview and applyStudioMotionToPreview unconditionally fetched from disk on every call, even without forceFromDisk. The runtime posts state messages every frame via postMessage, triggering React re-renders that re-invoked these functions ~60x/second. Fixed by returning early when no disk read is requested, and using refs instead of callbacks in useEffect deps. 3. **Parent proxy double-play**: the player web component created parent-frame audio proxies even when the runtime bridge was available, causing two audio sources on autoplay-blocked promotion. Fixed by skipping proxy creation when _hasRuntimeBridge returns true, and synchronously muting iframe media on promotion to close the async race window. Also fixes pre-existing ResolutionPreset type missing square variants. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent fd5fdfd commit ca00c70

4 files changed

Lines changed: 39 additions & 38 deletions

File tree

‎packages/core/src/runtime/init.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@ export function initSandboxRuntimeModular(): void {
13041304
timeSeconds: state.currentTime,
13051305
playing: state.isPlaying,
13061306
playbackRate: state.playbackRate,
1307-
outputMuted: state.mediaOutputMuted,
1307+
outputMuted: state.mediaOutputMuted || webAudio.isActive(),
13081308
userMuted: state.bridgeMuted,
13091309
userVolume: state.bridgeVolume,
13101310
forceSync,

‎packages/core/src/runtime/webAudioTransport.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ export class WebAudioTransport {
132132
} catch {
133133
// already stopped
134134
}
135-
source.el.muted = source.priorMuted;
135+
// Keep the element muted — syncRuntimeMedia will unmute on the next
136+
// tick if appropriate. Restoring priorMuted here races with el.play()
137+
// in the media sync loop, briefly producing audio from both the HTML
138+
// element and the Web Audio buffer on pause/resume transitions.
136139
}
137140
this._activeSources = [];
138141
this._paused = true;

‎packages/player/src/hyperframes-player.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,19 @@ class HyperframesPlayer extends HTMLElement {
14011401
private _promoteToParentProxy() {
14021402
if (this._audioOwner === "parent") return;
14031403
this._audioOwner = "parent";
1404+
// Synchronously mute iframe media to close the race window where a
1405+
// user gesture could let the runtime's el.play() succeed before the
1406+
// async bridge mute lands.
1407+
try {
1408+
const doc = this.iframe.contentDocument;
1409+
if (doc) {
1410+
for (const el of doc.querySelectorAll<HTMLMediaElement>("video, audio")) {
1411+
el.muted = true;
1412+
}
1413+
}
1414+
} catch {
1415+
/* cross-origin */
1416+
}
14041417
// `_sendControl` is async — the iframe won't see the mute for ~one
14051418
// message-loop tick. In that narrow window the runtime's next
14061419
// `syncRuntimeMedia` pass may still try `el.play()` on the iframe
@@ -1476,6 +1489,15 @@ class HyperframesPlayer extends HTMLElement {
14761489
* to play — a silent hole in the audio track.
14771490
*/
14781491
private _setupParentMedia() {
1492+
// Skip parent proxy creation when the runtime bridge is available —
1493+
// the runtime manages media directly and parent proxies cause double
1494+
// audio when Studio's useTimelinePlayer also controls the runtime.
1495+
try {
1496+
const win = this.iframe.contentWindow;
1497+
if (win && this._hasRuntimeBridge(win)) return;
1498+
} catch {
1499+
/* cross-origin */
1500+
}
14791501
try {
14801502
const doc = this.iframe.contentDocument;
14811503
if (!doc) return;

‎packages/studio/src/App.tsx‎

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,43 +2207,32 @@ export function StudioApp() {
22072207
iframe: HTMLIFrameElement | null = previewIframeRef.current,
22082208
options?: { forceFromDisk?: boolean; readFromDiskFirst?: boolean },
22092209
) => {
2210-
const readRevision = studioManualEditRevisionRef.current;
22112210
const readFromDiskFirst = Boolean(options?.forceFromDisk || options?.readFromDiskFirst);
22122211
if (!readFromDiskFirst) {
22132212
applyCurrentStudioManualEditsToPreview(iframe);
2213+
return;
22142214
}
2215+
const readRevision = studioManualEditRevisionRef.current;
22152216
let content: string;
22162217
try {
22172218
content = await readOptionalProjectFile(STUDIO_MANUAL_EDITS_PATH);
22182219
} catch (error) {
22192220
const message =
22202221
error instanceof Error ? error.message : "Failed to read manual edit manifest";
22212222
showToast(message);
2222-
if (readFromDiskFirst) {
2223-
applyCurrentStudioManualEditsToPreview(iframe);
2224-
}
2223+
applyCurrentStudioManualEditsToPreview(iframe);
22252224
return;
22262225
}
22272226
if (options?.forceFromDisk || readRevision === studioManualEditRevisionRef.current) {
22282227
studioManualEditManifestRef.current = parseStudioManualEditManifest(content);
22292228
if (options?.forceFromDisk) studioManualEditRevisionRef.current += 1;
2230-
applyCurrentStudioManualEditsToPreview(iframe);
2231-
return;
2232-
}
2233-
if (readFromDiskFirst) {
2234-
applyCurrentStudioManualEditsToPreview(iframe);
22352229
}
2230+
applyCurrentStudioManualEditsToPreview(iframe);
22362231
},
22372232
[applyCurrentStudioManualEditsToPreview, readOptionalProjectFile, showToast],
22382233
);
22392234
applyStudioManualEditsToPreviewRef.current = applyStudioManualEditsToPreview;
22402235

2241-
const applyStudioManualEditsToPreviewAfterRefresh = useCallback(
2242-
(iframe: HTMLIFrameElement | null = previewIframeRef.current) =>
2243-
applyStudioManualEditsToPreview(iframe, { readFromDiskFirst: true }),
2244-
[applyStudioManualEditsToPreview],
2245-
);
2246-
22472236
const applyCurrentStudioMotionToPreview = useCallback(
22482237
(iframe: HTMLIFrameElement | null = previewIframeRef.current) => {
22492238
if (!iframe) return;
@@ -2282,43 +2271,32 @@ export function StudioApp() {
22822271
iframe: HTMLIFrameElement | null = previewIframeRef.current,
22832272
options?: { forceFromDisk?: boolean; readFromDiskFirst?: boolean },
22842273
) => {
2285-
const readRevision = studioMotionRevisionRef.current;
22862274
const readFromDiskFirst = Boolean(options?.forceFromDisk || options?.readFromDiskFirst);
22872275
if (!readFromDiskFirst) {
22882276
applyCurrentStudioMotionToPreview(iframe);
2277+
return;
22892278
}
2279+
const readRevision = studioMotionRevisionRef.current;
22902280
let content: string;
22912281
try {
22922282
content = await readOptionalProjectFile(STUDIO_MOTION_PATH);
22932283
} catch (error) {
22942284
const message = error instanceof Error ? error.message : "Failed to read motion manifest";
22952285
showToast(message);
2296-
if (readFromDiskFirst) {
2297-
applyCurrentStudioMotionToPreview(iframe);
2298-
}
2286+
applyCurrentStudioMotionToPreview(iframe);
22992287
return;
23002288
}
23012289
if (options?.forceFromDisk || readRevision === studioMotionRevisionRef.current) {
23022290
studioMotionManifestRef.current = parseStudioMotionManifest(content);
23032291
if (options?.forceFromDisk) studioMotionRevisionRef.current += 1;
23042292
setStudioMotionRevision((revision) => revision + 1);
2305-
applyCurrentStudioMotionToPreview(iframe);
2306-
return;
2307-
}
2308-
if (readFromDiskFirst) {
2309-
applyCurrentStudioMotionToPreview(iframe);
23102293
}
2294+
applyCurrentStudioMotionToPreview(iframe);
23112295
},
23122296
[applyCurrentStudioMotionToPreview, readOptionalProjectFile, showToast],
23132297
);
23142298
applyStudioMotionToPreviewRef.current = applyStudioMotionToPreview;
23152299

2316-
const applyStudioMotionToPreviewAfterRefresh = useCallback(
2317-
(iframe: HTMLIFrameElement | null = previewIframeRef.current) =>
2318-
applyStudioMotionToPreview(iframe, { readFromDiskFirst: true }),
2319-
[applyStudioMotionToPreview],
2320-
);
2321-
23222300
const commitStudioManualEditManifestOptimistically = useCallback(
23232301
(
23242302
updateManifest: (manifest: StudioManualEditManifest) => StudioManualEditManifest,
@@ -3558,8 +3536,8 @@ export function StudioApp() {
35583536
attachErrorCapture();
35593537
syncPreviewHistoryHotkey(previewIframe);
35603538
void (async () => {
3561-
await applyStudioManualEditsToPreviewAfterRefresh(previewIframe);
3562-
await applyStudioMotionToPreviewAfterRefresh(previewIframe);
3539+
await applyStudioManualEditsToPreviewRef.current(previewIframe);
3540+
await applyStudioMotionToPreviewRef.current(previewIframe);
35633541
})();
35643542
syncSelectionFromDocument();
35653543
refreshPreviewDocumentVersion();
@@ -3570,8 +3548,8 @@ export function StudioApp() {
35703548
attachErrorCapture();
35713549
syncPreviewHistoryHotkey(previewIframe);
35723550
void (async () => {
3573-
await applyStudioManualEditsToPreviewAfterRefresh(previewIframe);
3574-
await applyStudioMotionToPreviewAfterRefresh(previewIframe);
3551+
await applyStudioManualEditsToPreviewRef.current(previewIframe);
3552+
await applyStudioMotionToPreviewRef.current(previewIframe);
35753553
})();
35763554
syncSelectionFromDocument();
35773555
refreshPreviewDocumentVersion();
@@ -3584,8 +3562,6 @@ export function StudioApp() {
35843562
}, [
35853563
activeCompPath,
35863564
applyDomSelection,
3587-
applyStudioManualEditsToPreviewAfterRefresh,
3588-
applyStudioMotionToPreviewAfterRefresh,
35893565
buildDomSelectionFromTarget,
35903566
captionEditMode,
35913567
previewIframe,

0 commit comments

Comments
 (0)