Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): keep early instant playback to browsers with MediaSource

Richie McIlroy81dc0663319e
diff --git a/apps/web/__tests__/unit/hls-recording-readiness.test.ts b/apps/web/__tests__/unit/hls-recording-readiness.test.ts
index a85e9241c..45091da89 100644
--- a/apps/web/__tests__/unit/hls-recording-readiness.test.ts
+++ b/apps/web/__tests__/unit/hls-recording-readiness.test.ts
@@ -206,6 +206,37 @@ describe("Instant player readiness and failure UX", () => {
expect(mocks.pause).toHaveBeenCalled();
});
+ it.each([
+ [false, "requireComplete=1"],
+ [true, "earlyPlayback=1"],
+ ])(
+ "plays early only on MediaSource (available: %s)",
+ async (available, expected) => {
+ if (available) vi.stubGlobal("MediaSource", class {});
+ await act(async () => {
+ root.render(
+ createElement(HLSVideoPlayer, {
+ videoId: "recording" as Parameters<
+ typeof HLSVideoPlayer
+ >[0]["videoId"],
+ videoSrc:
+ "/api/playlist?videoId=recording&videoType=segments-master&earlyPlayback=1",
+ videoRef,
+ chaptersSrc: "",
+ captionsSrc: "",
+ isLiveSegments: true,
+ hasActiveUpload: true,
+ allowSegmentProbeDuringUpload: true,
+ earlyPlayback: true,
+ }),
+ );
+ });
+ const probe = String(vi.mocked(fetch).mock.calls[0]?.[0]);
+ expect(probe).toContain("videoType=segments-status");
+ expect(probe).toContain(expected);
+ },
+ );
+
it("keeps a playable source available when only final processing fails", async () => {
await render();
await decodeFrame();
diff --git a/apps/web/app/s/[videoId]/_components/HLSVideoPlayer.tsx b/apps/web/app/s/[videoId]/_components/HLSVideoPlayer.tsx
index 0993959fd..816f466c2 100644
--- a/apps/web/app/s/[videoId]/_components/HLSVideoPlayer.tsx
+++ b/apps/web/app/s/[videoId]/_components/HLSVideoPlayer.tsx
@@ -190,10 +190,14 @@ export function HLSVideoPlayer({
const hasTriedRouterRefreshRef = useRef(false);
const isBackgroundPreview = previewMode === "background";
// Native HLS players end a growing playlist when playback catches up with
- // its last segment, so early playback is limited to hls.js and everything
- // else keeps waiting for the complete recording.
+ // its last segment, and on ManagedMediaSource alone (iPhone) the final
+ // segment was seen to go missing when it ended, so early playback needs
+ // hls.js on MediaSource. Everything else waits for the complete recording.
const isEarlyPlayback =
- earlyPlayback && typeof window !== "undefined" && Hls.isSupported();
+ earlyPlayback &&
+ typeof window !== "undefined" &&
+ typeof window.MediaSource !== "undefined" &&
+ Hls.isSupported();
const sourceSrc =
earlyPlayback && !isEarlyPlayback
? videoSrc.replace("earlyPlayback=1", "requireComplete=1")