Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(web): recover failed voice-note playback
Akshar Patel0c303c37e650
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 926bdcd44..b7f47e360 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -132,6 +132,7 @@ jobs:__tests__/unit/homepage-demo-accessibility.test.ts__tests__/unit/linkified-text.test.ts__tests__/unit/loom-import-ui.test.ts+ __tests__/unit/media-comment-playback-cleanup.test.ts__tests__/unit/media-format-converter-selection.test.ts__tests__/unit/media-format-converter.test.ts__tests__/unit/media-player-store-refs.test.ts@@ -146,6 +147,7 @@ jobs:__tests__/unit/sso-login-pages.test.ts__tests__/unit/transcript-numbers.test.ts__tests__/unit/video-analytics-access.test.ts+ __tests__/unit/voice-note-playback.test.ts- name: Typecheckrun: bun run typecheckdiff --git a/apps/web/__tests__/unit/voice-note-playback.test.ts b/apps/web/__tests__/unit/voice-note-playback.test.tsnew file mode 100644index 000000000..bb42d6342--- /dev/null+++ b/apps/web/__tests__/unit/voice-note-playback.test.ts@@ -0,0 +1,249 @@+// @vitest-environment jsdom++import {+ act,+ type ComponentProps,+ createElement,+ Fragment,+ type PropsWithChildren,+} from "react";+import { createRoot, type Root } from "react-dom/client";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import type { MediaComment } from "@/app/s/[videoId]/_components/media-comment/media-comment-types";+import {+ stopAll,+ useMainVideoInterlock,+} from "@/app/s/[videoId]/_components/media-comment/media-playback-registry";+import { VoiceNotePill } from "@/app/s/[videoId]/_components/media-comment/VoiceNotePill";+import {+ createPlaybackStore,+ type PlaybackStore,+} from "@/app/s/[videoId]/_components/playback/playback-store";++const mocks = vi.hoisted(() => ({ store: null as PlaybackStore | null }));+vi.mock("@/app/s/[videoId]/_components/playback/PlaybackContext", () => ({+ useOptionalPlayback: () => mocks.store,+}));++vi.mock("motion/react", () => ({+ AnimatePresence: ({ children }: PropsWithChildren) => children,+ motion: {+ span: ({+ initial: _initial,+ animate: _animate,+ exit: _exit,+ transition: _transition,+ ...props+ }: ComponentProps<"span"> & {+ initial?: unknown;+ animate?: unknown;+ exit?: unknown;+ transition?: unknown;+ }) => createElement("span", props),+ },+}));++const comment: MediaComment = {+ id: "fixture-comment" as MediaComment["id"],+ videoId: "fixture-video" as MediaComment["videoId"],+ type: "audio",+ content: "",+ mediaKey: "fixture.webm",+ mediaDuration: 10,+ mediaMeta: null,+};++function Interlock() {+ useMainVideoInterlock();+ return null;+}++const deferred = () => {+ let resolve!: () => void;+ let reject!: (cause: unknown) => void;+ const promise = new Promise<void>((yes, no) => {+ resolve = yes;+ reject = no;+ });+ return { promise, resolve, reject };+};++let root: Root;+let container: HTMLDivElement;+let main: HTMLVideoElement;+let mainPlaying: boolean;+let pending: ReturnType<typeof deferred>;+let note: HTMLAudioElement;+let frames: Set<number>;+let nextFrame: number;++beforeEach(async () => {+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);+ pending = deferred();+ pending.promise.catch(() => {});+ frames = new Set();+ nextFrame = 0;+ vi.spyOn(window, "requestAnimationFrame").mockImplementation(() => {+ frames.add(++nextFrame);+ return nextFrame;+ });+ vi.spyOn(window, "cancelAnimationFrame").mockImplementation((id) => {+ frames.delete(id);+ });+ vi.spyOn(HTMLMediaElement.prototype, "pause").mockImplementation(() => {});+ vi.spyOn(HTMLMediaElement.prototype, "play").mockImplementation(function (+ this: HTMLMediaElement,+ ) {+ note = this as HTMLAudioElement;+ return pending.promise;+ });+ container = document.createElement("div");+ main = document.createElement("video");+ mainPlaying = true;+ Object.defineProperty(main, "paused", { get: () => !mainPlaying });+ vi.spyOn(main, "pause").mockImplementation(() => {+ mainPlaying = false;+ main.dispatchEvent(new Event("pause"));+ });+ vi.spyOn(main, "play").mockImplementation(async () => {+ mainPlaying = true;+ main.dispatchEvent(new Event("play"));+ });+ mocks.store = createPlaybackStore();+ mocks.store.attach(main);+ document.body.append(container, main);+ root = createRoot(container);+ await act(async () =>+ root.render(+ createElement(+ Fragment,+ null,+ createElement(Interlock),+ createElement(VoiceNotePill, { comment }),+ ),+ ),+ );+});++afterEach(async () => {+ await act(async () => root.unmount());+ stopAll();+ mocks.store?.detach();+ mocks.store = null;+ container.remove();+ main.remove();+ vi.unstubAllGlobals();+});++const click = () =>+ act(async () => {+ container.querySelector<HTMLButtonElement>("button")?.click();+ });+const label = () =>+ container.querySelector("button")?.getAttribute("aria-label");++describe("voice-note playback promises", () => {+ it.each([true, false])(+ "recovers from unavailable media when main was playing=%s",+ async (playing) => {+ mainPlaying = playing;+ main.dispatchEvent(new Event(playing ? "play" : "pause"));+ await click();+ expect(mainPlaying).toBe(false);+ await act(async () => {+ pending.reject(+ new DOMException("Failed to load media", "NotSupportedError"),+ );+ });+ expect.soft(label()).toBe("Play voice note");+ expect.soft(mainPlaying).toBe(playing);+ expect.soft(frames.size).toBe(0);+ },+ );++ it("resumes after rejection before queued main-video media events", async () => {+ const events: Array<() => void> = [];+ vi.mocked(main.pause).mockImplementation(() => {+ mainPlaying = false;+ events.push(() => main.dispatchEvent(new Event("pause")));+ });+ vi.mocked(main.play).mockImplementation(async () => {+ mainPlaying = true;+ events.push(() => main.dispatchEvent(new Event("play")));+ });+ pending.reject(+ new DOMException("Already failed source", "NotSupportedError"),+ );+ await click();+ await act(async () => {+ for (const emit of events) emit();+ });+ expect(label()).toBe("Play voice note");+ expect(mainPlaying).toBe(true);+ expect(mocks.store?.getPlaying()).toBe(true);+ expect(frames.size).toBe(0);+ });++ it("keeps a successfully started note active and returns playback when it ends", async () => {+ await click();+ await act(async () => pending.resolve());+ expect(label()).toBe("Pause voice note");+ expect(mainPlaying).toBe(false);+ await act(async () => note.dispatchEvent(new Event("ended")));+ expect(label()).toBe("Play voice note");+ expect(mainPlaying).toBe(true);+ expect(frames.size).toBe(0);+ });++ it("keeps the main video paused if the viewer pauses while play is pending", async () => {+ await click();+ await click();+ await act(async () => {+ pending.reject(+ new DOMException(+ "The play request was interrupted by pause",+ "AbortError",+ ),+ );+ });+ expect(label()).toBe("Play voice note");+ expect(mainPlaying).toBe(false);+ expect(frames.size).toBe(0);+ });++ it("does not resume the main video after a different note takes over", async () => {+ await click();+ await act(async () => {+ root.render(+ createElement(+ Fragment,+ null,+ createElement(Interlock),+ createElement(VoiceNotePill, { comment }),+ createElement(VoiceNotePill, {+ comment: { ...comment, id: "other" as MediaComment["id"] },+ }),+ ),+ );+ });+ const first = pending;+ pending = deferred();+ await act(async () => {+ container+ .querySelectorAll<HTMLButtonElement>(+ '[aria-label="Play voice note"]',+ )[0]+ ?.click();+ pending.resolve();+ });+ await act(async () => {+ first.reject(+ new DOMException("Failed to load media", "NotSupportedError"),+ );+ });+ expect(mainPlaying).toBe(false);+ expect(+ container.querySelector('[aria-label="Pause voice note"]'),+ ).not.toBeNull();+ });+});diff --git a/apps/web/app/s/[videoId]/_components/media-comment/VoiceNotePill.tsx b/apps/web/app/s/[videoId]/_components/media-comment/VoiceNotePill.tsxindex 7562b4a50..ecdc13480 100644--- a/apps/web/app/s/[videoId]/_components/media-comment/VoiceNotePill.tsx+++ b/apps/web/app/s/[videoId]/_components/media-comment/VoiceNotePill.tsx@@ -150,7 +150,12 @@ export const VoiceNotePill = ({}claimPlayback(el, handleRegistryStop);suspendMainVideo();- void el.play();+ void el.play().catch((cause: unknown) => {+ if (cause instanceof DOMException && cause.name === "AbortError") return;+ console.error("Failed to play voice note", cause);+ handleRegistryStop();+ releasePlayback(el, { resumeMain: true });+ });setPlaying(true);paintLoop();}, [ensureAudio, handleRegistryStop, paintLoop, playing, stopPainting]);diff --git a/apps/web/app/s/[videoId]/_components/media-comment/media-playback-registry.ts b/apps/web/app/s/[videoId]/_components/media-comment/media-playback-registry.tsindex 597575826..ff26f427d 100644--- a/apps/web/app/s/[videoId]/_components/media-comment/media-playback-registry.ts+++ b/apps/web/app/s/[videoId]/_components/media-comment/media-playback-registry.ts@@ -60,9 +60,8 @@ export function releasePlayback(if (!resumeMain) return;resumeMain = false;if (!options?.resumeMain) return;- // Guard against the store already running: `play()` on a playing element is- // a no-op, but skipping it keeps us out of its subscriber notifications.- if (mainStore && !mainStore.getPlaying()) mainStore.play();+ // A rejected play() can settle before the main video's queued pause event.+ if (mainStore?.getElement()?.paused) mainStore.play();}/**