Skip to content

Commit fa2e635

Browse files
fix(core): a muted video's picture no longer waits on its own audio track at a cut (#7412)
* fix(core): a muted video's picture no longer waits on its own audio track at a cut A muted video takes its clock from its audio track in Safari; a late start of that track stalls the picture after the cut. The runtime now switches the track off. * test(core): a video muted by script and unmuted later keeps its audio track * fix(core): type the audio track loop so it passes the strict index check
1 parent eb16510 commit fa2e635

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

‎packages/core/src/runtime/media.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,53 @@ describe("syncRuntimeMedia", () => {
16241624
expect(onElementVolume).toHaveBeenLastCalledWith(clip.el, 0.5, 1, 0.8);
16251625
});
16261626

1627+
describe("a muted video's own audio track", () => {
1628+
// A muted video still takes its clock from its audio track; a late track start stalls the picture after a cut.
1629+
const withTracks = (el: HTMLMediaElement, tracks: Array<{ enabled: boolean }>) =>
1630+
Object.defineProperty(el, "audioTracks", { value: tracks, configurable: true });
1631+
const tick = (clip: RuntimeMediaClip) =>
1632+
syncRuntimeMedia({ clips: [clip], timeSeconds: 5, playing: true, playbackRate: 1 });
1633+
1634+
it("is switched off for a video the author muted", () => {
1635+
const clip = createMockClip({ start: 0, end: 10 });
1636+
clip.el.setAttribute("muted", "");
1637+
const tracks = [{ enabled: true }];
1638+
withTracks(clip.el, tracks);
1639+
tick(clip);
1640+
expect(tracks[0].enabled).toBe(false);
1641+
});
1642+
1643+
it("stays on for a video that can sound", () => {
1644+
const clip = createMockClip({ start: 0, end: 10 });
1645+
const tracks = [{ enabled: true }];
1646+
withTracks(clip.el, tracks);
1647+
tick(clip);
1648+
expect(tracks[0].enabled).toBe(true);
1649+
});
1650+
1651+
it("stays on for a video that is muted by script and unmuted later", () => {
1652+
const clip = createMockClip({ start: 0, end: 10 });
1653+
const tracks = [{ enabled: true }];
1654+
withTracks(clip.el, tracks);
1655+
clip.el.muted = true;
1656+
tick(clip);
1657+
clip.el.muted = false;
1658+
tick(clip);
1659+
expect(tracks[0].enabled).toBe(true);
1660+
});
1661+
1662+
it("is switched off when the browser lists it after the first tick", () => {
1663+
const clip = createMockClip({ start: 0, end: 10 });
1664+
clip.el.setAttribute("muted", "");
1665+
const tracks: Array<{ enabled: boolean }> = [];
1666+
withTracks(clip.el, tracks);
1667+
tick(clip);
1668+
tracks.push({ enabled: true });
1669+
tick(clip);
1670+
expect(tracks[0].enabled).toBe(false);
1671+
});
1672+
});
1673+
16271674
describe("per-element mute (Web Audio ownership)", () => {
16281675
it("mutes a clip whose element the transport owns", () => {
16291676
const clip = createMockClip({ start: 0, end: 10 });

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,13 @@ function learnVideoStartLatency(el: HTMLMediaElement, time: number, offset: numb
473473
videoStartLatency = Math.min(0.1, Math.max(0, averaged));
474474
}
475475

476+
/** A muted video takes its clock from its audio track, whose late start stalls the picture after a cut. */
477+
function dropSilentAudioTrack(el: HTMLMediaElement): void {
478+
if (el.tagName !== "VIDEO" || !el.defaultMuted) return;
479+
const tracks = (el as { audioTracks?: ArrayLike<{ enabled: boolean }> }).audioTracks;
480+
for (const track of Array.from(tracks ?? [])) track.enabled = false;
481+
}
482+
476483
// fallow-ignore-next-line complexity
477484
export function syncRuntimeMedia(params: {
478485
clips: RuntimeMediaClip[];
@@ -534,6 +541,7 @@ export function syncRuntimeMedia(params: {
534541
for (const clip of params.clips) {
535542
const { el } = clip;
536543
if (!el.isConnected) continue;
544+
dropSilentAudioTrack(el);
537545
const clipRate = clip.rate ?? clip.playbackRate;
538546
const origin = clip.origin ?? clip.start;
539547
const isNonLoopVideo = el.tagName === "VIDEO" && !clip.loop;

0 commit comments

Comments
 (0)