Skip to content

Commit 16c6ac8

Browse files
committed
fix(player): each take plays its own audio when the browser blocks autoplay
1 parent 5b520a7 commit 16c6ac8

3 files changed

Lines changed: 372 additions & 288 deletions

File tree

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

Lines changed: 6 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ describe("HyperframesPlayer parent-frame media", () => {
147147
paused: boolean;
148148
play: ReturnType<typeof vi.fn>;
149149
pause: ReturnType<typeof vi.fn>;
150+
addEventListener: ReturnType<typeof vi.fn>;
150151
load: ReturnType<typeof vi.fn>;
151152
};
152153

@@ -162,6 +163,7 @@ describe("HyperframesPlayer parent-frame media", () => {
162163
paused: true,
163164
play: vi.fn().mockResolvedValue(undefined),
164165
pause: vi.fn(),
166+
addEventListener: vi.fn(),
165167
load: vi.fn(),
166168
};
167169

@@ -779,206 +781,6 @@ describe("HyperframesPlayer media MutationObserver scoping", () => {
779781
});
780782
});
781783

782-
// ── Parent-proxy time-mirror coalescing ──
783-
//
784-
// `_mirrorParentMediaTime` is the steady-state correction loop that nudges
785-
// every parent-frame audio/video proxy back onto the iframe's timeline. The
786-
// post-`P1-4` contract: a single over-threshold sample (one slow bridge tick,
787-
// one tab-throttled rAF, one GC pause) is absorbed by a per-proxy counter and
788-
// does NOT cost a `currentTime` write. Only a *trending* drift — two
789-
// consecutive samples above the 50 ms threshold — triggers a seek. Forced
790-
// callers (audio-ownership promotion, brand-new proxy initialization) bypass
791-
// the gate so the listener never hears a misaligned sample on cut-over.
792-
793-
describe("HyperframesPlayer parent-proxy time-mirror coalescing", () => {
794-
type DriftEntry = {
795-
el: { currentTime: number; src: string; pause: () => void };
796-
start: number;
797-
duration: number;
798-
driftSamples: number;
799-
};
800-
type PlayerInternal = HTMLElement & {
801-
_parentMedia: DriftEntry[];
802-
_mirrorParentMediaTime: (timelineSeconds: number, options?: { force?: boolean }) => void;
803-
_promoteToParentProxy?: () => void;
804-
};
805-
806-
let player: PlayerInternal;
807-
808-
beforeEach(async () => {
809-
await import("./hyperframes-player.js");
810-
player = document.createElement("hyperframes-player") as PlayerInternal;
811-
document.body.appendChild(player);
812-
// No audio-src was set, so `_parentMedia` is empty. Tests push synthetic
813-
// POJO entries — `_mirrorParentMediaTime` only reads/writes
814-
// `el.currentTime`, so a plain object stands in fine for HTMLMediaElement.
815-
});
816-
817-
afterEach(() => {
818-
player.remove();
819-
vi.restoreAllMocks();
820-
});
821-
822-
function makeEntry(
823-
opts: {
824-
currentTime?: number;
825-
start?: number;
826-
duration?: number;
827-
driftSamples?: number;
828-
} = {},
829-
): DriftEntry {
830-
// Include `pause`/`src` so `disconnectedCallback`'s teardown loop
831-
// (`m.el.pause(); m.el.src = ""`) doesn't blow up when the player is
832-
// removed at the end of the test — `_mirrorParentMediaTime` itself only
833-
// touches `currentTime`.
834-
const entry: DriftEntry = {
835-
el: {
836-
currentTime: opts.currentTime ?? 0,
837-
src: "",
838-
pause: vi.fn(),
839-
},
840-
start: opts.start ?? 0,
841-
duration: opts.duration ?? 100,
842-
driftSamples: opts.driftSamples ?? 0,
843-
};
844-
player._parentMedia.push(entry);
845-
return entry;
846-
}
847-
848-
it("initializes new parent-media entries with driftSamples=0", () => {
849-
// Mock Audio just for this test so the audio-src bootstrap path produces
850-
// a real entry rather than throwing on construction.
851-
const mockAudio = {
852-
src: "",
853-
preload: "",
854-
muted: false,
855-
playbackRate: 1,
856-
currentTime: 0,
857-
paused: true,
858-
play: vi.fn().mockResolvedValue(undefined),
859-
pause: vi.fn(),
860-
load: vi.fn(),
861-
};
862-
vi.spyOn(globalThis, "Audio").mockImplementation(function () {
863-
return mockAudio as unknown as HTMLAudioElement;
864-
});
865-
866-
const fresh = document.createElement("hyperframes-player") as PlayerInternal;
867-
fresh.setAttribute("audio-src", "https://cdn.example.com/narration.mp3");
868-
document.body.appendChild(fresh);
869-
870-
expect(fresh._parentMedia).toHaveLength(1);
871-
expect(fresh._parentMedia[0]?.driftSamples).toBe(0);
872-
fresh.remove();
873-
});
874-
875-
it("does nothing when drift is within the 50 ms threshold", () => {
876-
const m = makeEntry({ currentTime: 5 });
877-
player._mirrorParentMediaTime(5.04);
878-
expect(m.el.currentTime).toBe(5);
879-
expect(m.driftSamples).toBe(0);
880-
});
881-
882-
it("absorbs a single over-threshold spike without writing currentTime", () => {
883-
const m = makeEntry({ currentTime: 5 });
884-
player._mirrorParentMediaTime(5.5);
885-
expect(m.el.currentTime).toBe(5);
886-
expect(m.driftSamples).toBe(1);
887-
});
888-
889-
it("issues a seek on the second consecutive over-threshold sample", () => {
890-
const m = makeEntry({ currentTime: 5 });
891-
player._mirrorParentMediaTime(5.5);
892-
expect(m.el.currentTime).toBe(5);
893-
expect(m.driftSamples).toBe(1);
894-
// Second sample with the same drift: the gate trips, the write fires,
895-
// and the counter resets so the proxy doesn't re-seek every later tick.
896-
player._mirrorParentMediaTime(5.5);
897-
expect(m.el.currentTime).toBe(mediaElementTime(5.5));
898-
expect(m.driftSamples).toBe(0);
899-
});
900-
901-
it("resets the counter when a sample comes back within threshold", () => {
902-
const m = makeEntry({ currentTime: 5 });
903-
player._mirrorParentMediaTime(5.5);
904-
expect(m.driftSamples).toBe(1);
905-
// Recovery — counter must clear so a later isolated spike doesn't
906-
// accidentally satisfy the 2-sample gate by piggy-backing on stale state.
907-
player._mirrorParentMediaTime(5.02);
908-
expect(m.driftSamples).toBe(0);
909-
expect(m.el.currentTime).toBe(5);
910-
player._mirrorParentMediaTime(5.5);
911-
expect(m.driftSamples).toBe(1);
912-
expect(m.el.currentTime).toBe(5);
913-
});
914-
915-
it("force: true writes immediately on the first over-threshold sample", () => {
916-
const m = makeEntry({ currentTime: 5 });
917-
player._mirrorParentMediaTime(5.5, { force: true });
918-
expect(m.el.currentTime).toBe(mediaElementTime(5.5));
919-
expect(m.driftSamples).toBe(0);
920-
});
921-
922-
it("force: true clears any pre-existing drift counter", () => {
923-
const m = makeEntry({ currentTime: 5, driftSamples: 1 });
924-
player._mirrorParentMediaTime(5.5, { force: true });
925-
expect(m.el.currentTime).toBe(mediaElementTime(5.5));
926-
expect(m.driftSamples).toBe(0);
927-
});
928-
929-
it("does not seek out-of-range entries and resets their counters", () => {
930-
// Active window [10, 15). currentTime=99 is a sentinel — if the function
931-
// ever writes inside an out-of-range branch the test catches it because
932-
// relTime would be 5 (or 15), not 99.
933-
const m = makeEntry({
934-
currentTime: 99,
935-
start: 10,
936-
duration: 5,
937-
driftSamples: 5,
938-
});
939-
player._mirrorParentMediaTime(5);
940-
expect(m.el.currentTime).toBe(99);
941-
expect(m.driftSamples).toBe(0);
942-
// Boundary: relTime === duration → still out of range (the loop uses `>=`).
943-
m.driftSamples = 7;
944-
player._mirrorParentMediaTime(15);
945-
expect(m.el.currentTime).toBe(99);
946-
expect(m.driftSamples).toBe(0);
947-
});
948-
949-
it("tracks drift independently across multiple proxies", () => {
950-
// a is drifted; b is aligned. A single tick must increment a's counter
951-
// and reset b's — proving the per-entry state is genuinely per-entry.
952-
const a = makeEntry({ currentTime: 5 });
953-
const b = makeEntry({ currentTime: 7.01, driftSamples: 1 });
954-
player._mirrorParentMediaTime(7);
955-
expect(a.el.currentTime).toBe(5);
956-
expect(a.driftSamples).toBe(1);
957-
expect(b.el.currentTime).toBe(7.01);
958-
expect(b.driftSamples).toBe(0);
959-
});
960-
961-
it("force: true bypasses the gate for every proxy in a single sweep", () => {
962-
const a = makeEntry({ currentTime: 5 });
963-
const b = makeEntry({ currentTime: 8 });
964-
player._mirrorParentMediaTime(7, { force: true });
965-
expect(a.el.currentTime).toBe(mediaElementTime(7));
966-
expect(b.el.currentTime).toBe(mediaElementTime(7));
967-
expect(a.driftSamples).toBe(0);
968-
expect(b.driftSamples).toBe(0);
969-
});
970-
971-
it("_promoteToParentProxy invokes _mirrorParentMediaTime with force: true", () => {
972-
// Integration check of the promotion call site — we cannot tolerate even
973-
// ~80 ms of audible drift across an ownership flip, so the call site
974-
// must opt out of the jitter gate.
975-
const spy = vi.spyOn(player, "_mirrorParentMediaTime");
976-
player._promoteToParentProxy?.();
977-
const forcedCall = spy.mock.calls.find(([, opts]) => opts?.force === true);
978-
expect(forcedCall).toBeDefined();
979-
});
980-
});
981-
982784
// ── Synchronous seek() with same-origin detection ──
983785
//
984786
// Studio has long reached past the postMessage bridge and called the runtime's
@@ -1021,7 +823,6 @@ describe("HyperframesPlayer seek() sync path", () => {
1021823
el: { pause: ReturnType<typeof vi.fn>; src: string };
1022824
start: number;
1023825
duration: number;
1024-
driftSamples: number;
1025826
source?: HTMLMediaElement | null;
1026827
}>;
1027828
};
@@ -1251,14 +1052,12 @@ describe("HyperframesPlayer seek() sync path", () => {
12511052
el: { pause: slideProxyPause, src: "https://cdn.example.com/slide.mp4" },
12521053
start: 0,
12531054
duration: 5,
1254-
driftSamples: 0,
12551055
source: iframeVideo,
12561056
},
12571057
{
12581058
el: { pause: globalProxyPause, src: "https://cdn.example.com/background.mp3" },
12591059
start: 0,
12601060
duration: Infinity,
1261-
driftSamples: 0,
12621061
source: null,
12631062
},
12641063
);
@@ -1900,6 +1699,7 @@ describe("HyperframesPlayer volume and mute", () => {
19001699
load: ReturnType<typeof vi.fn>;
19011700
play: ReturnType<typeof vi.fn>;
19021701
pause: ReturnType<typeof vi.fn>;
1702+
addEventListener: ReturnType<typeof vi.fn>;
19031703
};
19041704

19051705
beforeEach(async () => {
@@ -1915,6 +1715,7 @@ describe("HyperframesPlayer volume and mute", () => {
19151715
load: vi.fn(),
19161716
play: vi.fn().mockResolvedValue(undefined),
19171717
pause: vi.fn(),
1718+
addEventListener: vi.fn(),
19181719
};
19191720
vi.spyOn(globalThis, "Audio").mockImplementation(function () {
19201721
return mockAudio as unknown as HTMLAudioElement;
@@ -2850,6 +2651,7 @@ describe("HyperframesPlayer playback rate", () => {
28502651
load: ReturnType<typeof vi.fn>;
28512652
play: ReturnType<typeof vi.fn>;
28522653
pause: ReturnType<typeof vi.fn>;
2654+
addEventListener: ReturnType<typeof vi.fn>;
28532655
};
28542656

28552657
beforeEach(async () => {
@@ -2865,6 +2667,7 @@ describe("HyperframesPlayer playback rate", () => {
28652667
load: vi.fn(),
28662668
play: vi.fn().mockResolvedValue(undefined),
28672669
pause: vi.fn(),
2670+
addEventListener: vi.fn(),
28682671
};
28692672
vi.spyOn(globalThis, "Audio").mockImplementation(function () {
28702673
return mockAudio as unknown as HTMLAudioElement;

0 commit comments

Comments
 (0)