Skip to content

The open-source full-stack studio for Opus 5.5 motion video. Reverse engineering for any video. Code video with HTML: recreate, edit and render. BYOA + BYOK · Motion.md · Images, Video, Music & Voice.

Fix live track mute synchronization

qindong3cbcd25f926c
diff --git a/studio/src/hooks/timelineElementsAttribute.test.ts b/studio/src/hooks/timelineElementsAttribute.test.ts
index 06df9ec0..53b770a6 100644
--- a/studio/src/hooks/timelineElementsAttribute.test.ts
+++ b/studio/src/hooks/timelineElementsAttribute.test.ts
@@ -1,10 +1,12 @@
// @vitest-environment happy-dom
import { act, createElement } from "react";
import { createRoot } from "react-dom/client";
-import { describe, expect, it, vi } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", { configurable: true, value: true });
import type { TimelineElement } from "../player";
+import { usePlayerStore } from "../player/store/playerStore";
+import { saveProjectFilesWithHistory } from "../utils/studioFileHistory";
const saved = vi.hoisted(() => ({ inputs: [] as Array<Record<string, unknown>> }));
vi.mock("../utils/studioFileHistory", () => ({
@@ -26,8 +28,90 @@ const clip = (id: string, sourceFile?: string): TimelineElement => ({
sourceFile,
});
+afterEach(() => {
+ document.body.innerHTML = "";
+ usePlayerStore.getState().reset();
+ vi.restoreAllMocks();
+});
+
describe("useSetElementsAttribute", () => {
+ it.each([false, true])("syncs saved track mute and unmute with monitor mute=%s", async (audioMuted) => {
+ const iframe = document.createElement("iframe");
+ document.body.append(iframe);
+ const doc = iframe.contentDocument!;
+ doc.body.innerHTML = '<audio id="a" data-volume="0.5"></audio><video id="b"></video>';
+ const audio = doc.getElementById("a")!;
+ const video = doc.getElementById("b")!;
+ const elements = [clip("a"), { ...clip("b"), tag: "video" }];
+ usePlayerStore.setState({ audioMuted, elements });
+ const pushedAttributes: boolean[][] = [];
+ const postMessage = vi.spyOn(iframe.contentWindow!, "postMessage").mockImplementation(() => {
+ pushedAttributes.push([audio.hasAttribute("muted"), video.hasAttribute("muted")]);
+ });
+ let write: ReturnType<typeof useSetElementsAttribute> | null = null;
+ function Probe() {
+ write = useSetElementsAttribute({
+ projectIdRef: { current: "p1" },
+ activeCompPath: "index.html",
+ showToast: vi.fn(),
+ writeProjectFile: vi.fn(async () => {}),
+ recordEdit: vi.fn(async () => {}),
+ previewIframeRef: { current: iframe },
+ pendingTimelineEditPathRef: { current: new Set<string>() },
+ });
+ return null;
+ }
+ const root = createRoot(document.createElement("div"));
+ act(() => root.render(createElement(Probe)));
+ await act(async () => {
+ await write?.(elements.map(element => ({ element, value: "true" })), "muted", "Mute audio");
+ });
+ expect(usePlayerStore.getState().elements.every(element => element.muted)).toBe(true);
+ await act(async () => {
+ await write?.(elements.map(element => ({ element, value: null })), "muted", "Unmute audio");
+ });
+ expect(usePlayerStore.getState().elements.every(element => !element.muted)).toBe(true);
+ expect(pushedAttributes).toEqual([[true, true], [false, false]]);
+ expect(postMessage).toHaveBeenCalledTimes(2);
+ for (const [message] of postMessage.mock.calls) {
+ expect(message).toMatchObject({ action: "set-muted", muted: audioMuted });
+ }
+ expect(audio.getAttribute("data-volume")).toBe("0.5");
+ expect(usePlayerStore.getState().audioMuted).toBe(audioMuted);
+ act(() => root.unmount());
+ });
+
+ it("leaves live media unchanged when the mute save fails", async () => {
+ vi.mocked(saveProjectFilesWithHistory).mockRejectedValueOnce(new Error("Save failed"));
+ const iframe = document.createElement("iframe");
+ document.body.append(iframe);
+ iframe.contentDocument!.body.innerHTML = '<audio id="a"></audio>';
+ const postMessage = vi.spyOn(iframe.contentWindow!, "postMessage");
+ const showToast = vi.fn();
+ let write: ReturnType<typeof useSetElementsAttribute> | null = null;
+ function Probe() {
+ write = useSetElementsAttribute({
+ projectIdRef: { current: "p1" },
+ activeCompPath: "index.html",
+ showToast,
+ writeProjectFile: vi.fn(async () => {}),
+ recordEdit: vi.fn(async () => {}),
+ previewIframeRef: { current: iframe },
+ pendingTimelineEditPathRef: { current: new Set<string>() },
+ });
+ return null;
+ }
+ const root = createRoot(document.createElement("div"));
+ act(() => root.render(createElement(Probe)));
+ await write?.([{ element: clip("a"), value: "true" }], "muted", "Mute audio");
+ expect(iframe.contentDocument!.getElementById("a")!.hasAttribute("muted")).toBe(false);
+ expect(postMessage).not.toHaveBeenCalled();
+ expect(showToast).toHaveBeenCalledWith("Save failed", "error");
+ act(() => root.unmount());
+ });
+
it("writes every clip's attribute in one history entry, one builder per file", async () => {
+ saved.inputs.length = 0;
const showToast = vi.fn();
let write: ReturnType<typeof useSetElementsAttribute> | null = null;
function Probe() {
diff --git a/studio/src/hooks/timelineElementsAttribute.ts b/studio/src/hooks/timelineElementsAttribute.ts
index e39bd7fe..bf1e957e 100644
--- a/studio/src/hooks/timelineElementsAttribute.ts
+++ b/studio/src/hooks/timelineElementsAttribute.ts
@@ -1,6 +1,8 @@
import { useCallback } from "react";
import type { TimelineEditCallbacks } from "../player/components/timelineCallbacks";
import { syncStoredElementAttribute } from "../player/lib/automationStoreSync";
+import { setPreviewMediaMuted } from "../player/lib/timelineIframeHelpers";
+import { usePlayerStore } from "../player/store/playerStore";
import { saveProjectFilesWithHistory } from "../utils/studioFileHistory";
import { applyPatchByTarget } from "../utils/sourcePatcher";
import {
@@ -43,6 +45,11 @@ function patchLive(
else node?.setAttribute(attr, value);
syncStoredElementAttribute(element, attr, value);
}
+ if (attr === "muted") {
+ // The HTML attribute changes defaultMuted, not the currently playing media.
+ // Reapply the monitor state so the runtime also reads each clip's saved mute.
+ setPreviewMediaMuted(iframe, usePlayerStore.getState().audioMuted);
+ }
}
/** One attribute written on several clips as a single save and a single undo step. */
diff --git a/studio/src/player/lib/timelineIframeHelpers.test.ts b/studio/src/player/lib/timelineIframeHelpers.test.ts
index b49a6e95..03d9903c 100644
--- a/studio/src/player/lib/timelineIframeHelpers.test.ts
+++ b/studio/src/player/lib/timelineIframeHelpers.test.ts
@@ -3,6 +3,7 @@
import { describe, expect, it, vi } from "vitest";
import {
applyPreviewAudioFlags,
+ setPreviewMediaMuted,
buildMissingCompositionElements,
scrubPreviewAudio,
setPreviewMediaVolume,
@@ -195,6 +196,37 @@ describe("buildMissingCompositionElements — nested master time", () => {
});
});
+describe("setPreviewMediaMuted", () => {
+ it.each([false, true])("reapplies mute when the shadow host already has muted=%s", (muted) => {
+ const host = Object.assign(document.createElement("div"), { muted });
+ const iframe = document.createElement("iframe");
+ host.attachShadow({ mode: "open" }).append(iframe);
+ document.body.append(host);
+ const postMessage = vi.spyOn(iframe.contentWindow!, "postMessage");
+
+ setPreviewMediaMuted(iframe, muted);
+
+ expect(host.muted).toBe(muted);
+ expect(postMessage).toHaveBeenCalledExactlyOnceWith(
+ expect.objectContaining({ action: "set-muted", muted }),
+ "*",
+ );
+ });
+
+ it("updates the shadow host when its mute preference changes", () => {
+ const host = Object.assign(document.createElement("div"), { muted: false });
+ const iframe = document.createElement("iframe");
+ host.attachShadow({ mode: "open" }).append(iframe);
+ document.body.append(host);
+ const postMessage = vi.spyOn(iframe.contentWindow!, "postMessage");
+
+ setPreviewMediaMuted(iframe, true);
+
+ expect(host.muted).toBe(true);
+ expect(postMessage).not.toHaveBeenCalled();
+ });
+});
+
describe("setPreviewMediaVolume", () => {
it("sends a clamped runtime volume to a direct preview iframe", () => {
const iframe = document.createElement("iframe");
diff --git a/studio/src/player/lib/timelineIframeHelpers.ts b/studio/src/player/lib/timelineIframeHelpers.ts
index 0ee8ce1d..4fe816d9 100644
--- a/studio/src/player/lib/timelineIframeHelpers.ts
+++ b/studio/src/player/lib/timelineIframeHelpers.ts
@@ -125,10 +125,11 @@ export function setPreviewMediaMuted(iframe: HTMLIFrameElement | null, muted: bo
if (!iframe) return;
try {
const host = resolvePreviewPlayerHost(iframe);
- if (host && typeof host.muted === "boolean") {
+ if (host && typeof host.muted === "boolean" && host.muted !== muted) {
host.muted = muted;
return;
}
+ // An unchanged host preference may still need to apply newly edited clip mutes.
postPreviewControl(iframe, "set-muted", { muted });
} catch {}
}