Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(recorder): integrate recording spool and recovered recordings into useWebRecorder

Richie McIlroy8314a984cb75
diff --git a/apps/web/app/(org)/dashboard/caps/components/web-recorder-dialog/useWebRecorder.ts b/apps/web/app/(org)/dashboard/caps/components/web-recorder-dialog/useWebRecorder.ts
index e00638ed9..aa1e0cc35 100644
--- a/apps/web/app/(org)/dashboard/caps/components/web-recorder-dialog/useWebRecorder.ts
+++ b/apps/web/app/(org)/dashboard/caps/components/web-recorder-dialog/useWebRecorder.ts
@@ -6,21 +6,30 @@ import { Cause, Exit, Option } from "effect";
import { useRouter } from "next/navigation";
import { useCallback, useEffect, useRef, useState } from "react";
import { toast } from "sonner";
-import {
- createVideoAndGetUploadUrl,
- deleteVideoResultFile,
-} from "@/actions/video/upload";
+import { createVideoAndGetUploadUrl } from "@/actions/video/upload";
import { useEffectMutation, useRpcClient } from "@/lib/EffectRuntime";
import { ThumbnailRequest } from "@/lib/Requests/ThumbnailRequest";
import { useUploadingContext } from "../../UploadingContext";
import { sendProgressUpdate } from "../sendProgressUpdate";
import {
- InstantMp4Uploader,
+ InstantRecordingUploader,
initiateMultipartUpload,
+ MultipartCompletionUncertainError,
+ ProcessingStartError,
} from "./instant-mp4-uploader";
import type { RecordingMode } from "./RecordingModeSelector";
import { captureThumbnail, convertToMp4 } from "./recording-conversion";
+import {
+ canUseRecordingSpool,
+ deleteRecoveredRecordingSpool,
+ RecordingSpool,
+} from "./recording-spool";
+import { moveRecordingSpoolToInMemoryBackup } from "./recording-spool-fallback";
import { uploadRecording } from "./recording-upload";
+import {
+ loadRecoveredRecordingSpools,
+ removeRecoveredRecordingSpoolFromCache,
+} from "./recovered-recording-cache";
import { useMediaRecorderSetup } from "./useMediaRecorderSetup";
import { useRecordingTimer } from "./useRecordingTimer";
import { useStreamManagement } from "./useStreamManagement";
@@ -32,22 +41,22 @@ import {
type DisplaySurfacePreference,
type ExtendedDisplayMediaStreamOptions,
FREE_PLAN_MAX_RECORDING_MS,
- MP4_MIME_TYPES,
RECORDING_MODE_TO_DISPLAY_SURFACE,
- WEBM_MIME_TYPES,
} from "./web-recorder-constants";
import type {
ChunkUploadState,
PresignedPost,
RecorderPhase,
RecordingFailureDownload,
+ RecoveredRecordingDownload,
VideoId,
} from "./web-recorder-types";
import {
detectCapabilities,
isUserCancellationError,
- pickSupportedMimeType,
type RecorderCapabilities,
+ type RecordingPipeline,
+ selectRecordingPipeline,
shouldRetryDisplayMediaWithoutPreferences,
} from "./web-recorder-utils";
@@ -69,6 +78,11 @@ const INSTANT_UPLOAD_REQUEST_INTERVAL_MS = 1000;
const INSTANT_CHUNK_GUARD_DELAY_MS = INSTANT_UPLOAD_REQUEST_INTERVAL_MS * 3;
type InstantChunkingMode = "manual" | "timeslice";
+type InstantVideoCreation = {
+ id: VideoId;
+ shareUrl: string;
+ upload: PresignedPost;
+};
const unwrapExitOrThrow = <T, E>(exit: Exit.Exit<T, E>) => {
if (Exit.isFailure(exit)) {
@@ -89,6 +103,15 @@ const getFileExtensionFromMime = (mime?: string | null) => {
return normalized || "mp4";
};
+const createRecordingDownloadName = (
+ createdAt: number,
+ mime?: string | null,
+) => {
+ const timestamp = new Date(createdAt).toISOString().replace(/[:.]/g, "-");
+ const extension = getFileExtensionFromMime(mime);
+ return `cap-recording-${timestamp}.${extension}`;
+};
+
export const useWebRecorder = ({
organisationId,
selectedMicId,
@@ -110,6 +133,9 @@ export const useWebRecorder = ({
const [chunkUploads, setChunkUploads] = useState<ChunkUploadState[]>([]);
const [errorDownload, setErrorDownload] =
useState<RecordingFailureDownload | null>(null);
+ const [recoveredDownloads, setRecoveredDownloads] = useState<
+ RecoveredRecordingDownload[]
+ >([]);
const [capabilities, setCapabilities] = useState<RecorderCapabilities>(() =>
detectCapabilities(),
);
@@ -140,6 +166,8 @@ export const useWebRecorder = ({
mediaRecorderRef,
recordedChunksRef,
totalRecordedBytesRef,
+ setLocalRecordingStrategy,
+ replaceLocalRecording,
onRecorderDataAvailable,
onRecorderStop,
onRecorderError,
@@ -182,18 +210,20 @@ export const useWebRecorder = ({
: null
: null;
- const dimensionsRef = useRef<{ width?: number; height?: number }>({});
+ const dimensionsRef = useRef<{
+ width?: number;
+ height?: number;
+ fps?: number;
+ }>({});
const stopRecordingRef = useRef<(() => Promise<void>) | null>(null);
- const startRecordingRef = useRef<
- ((options?: { reuseInstantVideo?: boolean }) => Promise<void>) | null
- >(null);
- const instantUploaderRef = useRef<InstantMp4Uploader | null>(null);
+ const startRecordingRef = useRef<(() => Promise<void>) | null>(null);
+ const instantUploaderRef = useRef<InstantRecordingUploader | null>(null);
+ const recordingPipelineRef = useRef<RecordingPipeline | null>(null);
const videoCreationRef = useRef<{
id: VideoId;
- upload: PresignedPost;
shareUrl: string;
+ upload: PresignedPost;
} | null>(null);
- const instantMp4ActiveRef = useRef(false);
const pendingInstantVideoIdRef = useRef<VideoId | null>(null);
const dataRequestIntervalRef = useRef<number | null>(null);
const instantChunkModeRef = useRef<InstantChunkingMode | null>(null);
@@ -202,6 +232,17 @@ export const useWebRecorder = ({
const freePlanAutoStopTriggeredRef = useRef(false);
const shareUrlOpenedRef = useRef(false);
const errorDownloadUrlRef = useRef<string | null>(null);
+ const stopInFlightRef = useRef(false);
+ const recordingSpoolRef = useRef<RecordingSpool | null>(null);
+ const recordingSpoolDegradingRef = useRef(false);
+ const recordingSpoolWarningShownRef = useRef(false);
+ const recoveredDownloadUrlsRef = useRef(new Map<string, string>());
+
+ const isStreamingPipelineActive = useCallback(
+ () => recordingPipelineRef.current?.mode === "streaming-webm",
+ [],
+ );
+
const requestInstantRecorderData = useCallback(() => {
if (instantChunkModeRef.current !== "manual") return;
const recorder = mediaRecorderRef.current;
@@ -232,14 +273,27 @@ export const useWebRecorder = ({
return;
}
- const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
- const extension = getFileExtensionFromMime(blob.type);
const url = URL.createObjectURL(blob);
errorDownloadUrlRef.current = url;
setErrorDownload({
url,
- fileName: `cap-recording-${timestamp}.${extension}`,
+ fileName: createRecordingDownloadName(Date.now(), blob.type),
+ });
+ }, []);
+
+ const dismissRecoveredDownload = useCallback((id: string) => {
+ const url = recoveredDownloadUrlsRef.current.get(id);
+ if (url) {
+ URL.revokeObjectURL(url);
+ recoveredDownloadUrlsRef.current.delete(id);
+ }
+ removeRecoveredRecordingSpoolFromCache(id);
+ void deleteRecoveredRecordingSpool(id).catch((error) => {
+ console.error("Failed to delete recovered recording spool", error);
});
+ setRecoveredDownloads((current) =>
+ current.filter((download) => download.id !== id),
+ );
}, []);
useEffect(() => {
@@ -248,9 +302,154 @@ export const useWebRecorder = ({
URL.revokeObjectURL(errorDownloadUrlRef.current);
errorDownloadUrlRef.current = null;
}
+ recoveredDownloadUrlsRef.current.forEach((url) => {
+ URL.revokeObjectURL(url);
+ });
+ recoveredDownloadUrlsRef.current.clear();
+ };
+ }, []);
+
+ useEffect(() => {
+ if (!canUseRecordingSpool()) {
+ return;
+ }
+
+ let cancelled = false;
+
+ void loadRecoveredRecordingSpools()
+ .then((recovered) => {
+ if (
+ cancelled ||
+ recovered.length === 0 ||
+ typeof window === "undefined"
+ ) {
+ return;
+ }
+
+ const nextDownloads = recovered.map((item) => {
+ const url = URL.createObjectURL(item.blob);
+ recoveredDownloadUrlsRef.current.set(item.sessionId, url);
+ return {
+ id: item.sessionId,
+ url,
+ fileName: createRecordingDownloadName(
+ item.createdAt,
+ item.blob.type || item.mimeType,
+ ),
+ createdAt: item.createdAt,
+ } satisfies RecoveredRecordingDownload;
+ });
+
+ setRecoveredDownloads(nextDownloads);
+ toast.info(
+ nextDownloads.length === 1
+ ? "Recovered an unfinished local recording."
+ : `Recovered ${nextDownloads.length} unfinished local recordings.`,
+ );
+ })
+ .catch((error) => {
+ console.error("Failed to recover orphaned recording spools", error);
+ });
+
+ return () => {
+ cancelled = true;
};
}, []);
+ const disposeRecordingSpool = useCallback(async () => {
+ const spool = recordingSpoolRef.current;
+ recordingSpoolRef.current = null;
+ recordingSpoolDegradingRef.current = false;
+ recordingSpoolWarningShownRef.current = false;
+ if (!spool) return;
+
+ try {
+ await spool.dispose();
+ } catch (error) {
+ console.error("Failed to dispose recording spool", error);
+ }
+ }, []);
+
+ const createRecordingSpool = useCallback(async (mimeType: string) => {
+ if (!canUseRecordingSpool()) {
+ return null;
+ }
+
+ try {
+ const spool = await RecordingSpool.create({ mimeType });
+ recordingSpoolDegradingRef.current = false;
+ recordingSpoolWarningShownRef.current = false;
+ recordingSpoolRef.current = spool;
+ return spool;
+ } catch (error) {
+ console.error("Failed to initialize recording spool", error);
+ return null;
+ }
+ }, []);
+
+ const persistChunkToRecordingSpool = useCallback(
+ (chunk: Blob) => {
+ if (recordingSpoolDegradingRef.current) return;
+
+ const spool = recordingSpoolRef.current;
+ if (!spool) return;
+
+ void spool.appendChunk(chunk).catch(async (error) => {
+ console.error("Failed to persist recording chunk locally", error);
+ if (recordingSpoolRef.current !== spool) {
+ return;
+ }
+
+ recordingSpoolDegradingRef.current = true;
+ recordingSpoolRef.current = null;
+ await moveRecordingSpoolToInMemoryBackup({
+ spool,
+ setLocalRecordingStrategy,
+ getRetainedChunks: () => [...recordedChunksRef.current],
+ replaceLocalRecording,
+ });
+ recordingSpoolDegradingRef.current = false;
+
+ try {
+ await spool.dispose();
+ } catch (disposeError) {
+ console.error(
+ "Failed to dispose degraded recording spool",
+ disposeError,
+ );
+ }
+
+ if (recordingSpoolWarningShownRef.current) {
+ return;
+ }
+
+ recordingSpoolWarningShownRef.current = true;
+ toast.warning(
+ "Local recovery switched to in-memory backup. Upload will continue, but large recordings may use more memory.",
+ );
+ });
+ },
+ [recordedChunksRef, replaceLocalRecording, setLocalRecordingStrategy],
+ );
+
+ const resolveFailureBlob = useCallback(async (blob: Blob | null) => {
+ if (blob) {
+ return blob;
+ }
+
+ const spool = recordingSpoolRef.current;
+ if (!spool) {
+ return null;
+ }
+
+ try {
+ return await spool.recoverBlob();
+ } catch (error) {
+ console.error("Failed to reconstruct recording from local spool", error);
+ return null;
+ }
+ }, []);
+
const openShareUrl = useCallback((shareUrl?: string | null) => {
if (!shareUrl || shareUrlOpenedRef.current) return;
if (typeof window === "undefined") return;
@@ -265,6 +464,18 @@ export const useWebRecorder = ({
mutationFn: (variables: VideoInstantCreateVariables) =>
rpc.VideoInstantCreate(variables),
});
+ const deletePendingVideoSafely = useCallback(
+ async (id: VideoId) => {
+ try {
+ await deleteVideo.mutateAsync(id);
+ return true;
+ } catch (error) {
+ console.error("Failed to delete pending instant video", error);
+ return false;
+ }
+ },
+ [deleteVideo],
+ );
const isFreePlan = !isProUser;
@@ -323,52 +534,57 @@ export const useWebRecorder = ({
[onPhaseChange],
);
- const cleanupRecordingState = useCallback(
- (options?: { preserveInstantVideo?: boolean }) => {
- cleanupStreams();
- clearTimer();
- resetRecorder();
- resetTimer();
- stopInstantChunkInterval();
- clearInstantChunkGuard();
- instantChunkModeRef.current = null;
- lastInstantChunkAtRef.current = null;
- instantMp4ActiveRef.current = false;
- if (instantUploaderRef.current) {
- void instantUploaderRef.current.cancel();
+ const cleanupRecordingState = useCallback(async () => {
+ cleanupStreams();
+ clearTimer();
+ resetRecorder();
+ resetTimer();
+ stopInstantChunkInterval();
+ clearInstantChunkGuard();
+ instantChunkModeRef.current = null;
+ lastInstantChunkAtRef.current = null;
+ recordingPipelineRef.current = null;
+ await disposeRecordingSpool();
+ const instantUploader = instantUploaderRef.current;
+ instantUploaderRef.current = null;
+ if (instantUploader) {
+ try {
+ await instantUploader.cancel();
+ } catch (error) {
+ console.error(
+ "Failed to cancel multipart upload during cleanup",
+ error,
+ );
}
- instantUploaderRef.current = null;
- setUploadStatus(undefined);
- setChunkUploads([]);
- setHasAudioTrack(false);
- replaceErrorDownload(null);
- shareUrlOpenedRef.current = false;
+ }
+ setUploadStatus(undefined);
+ setChunkUploads([]);
+ setHasAudioTrack(false);
+ replaceErrorDownload(null);
+ shareUrlOpenedRef.current = false;
- if (!options?.preserveInstantVideo) {
- const pendingInstantVideoId = pendingInstantVideoIdRef.current;
- pendingInstantVideoIdRef.current = null;
- videoCreationRef.current = null;
- setVideoId(null);
- if (pendingInstantVideoId) {
- void deleteVideo.mutateAsync(pendingInstantVideoId);
- }
- }
- },
- [
- cleanupStreams,
- clearTimer,
- resetRecorder,
- resetTimer,
- stopInstantChunkInterval,
- clearInstantChunkGuard,
- deleteVideo,
- setUploadStatus,
- replaceErrorDownload,
- ],
- );
+ const pendingInstantVideoId = pendingInstantVideoIdRef.current;
+ pendingInstantVideoIdRef.current = null;
+ videoCreationRef.current = null;
+ setVideoId(null);
+ if (pendingInstantVideoId) {
+ await deletePendingVideoSafely(pendingInstantVideoId);
+ }
+ }, [
+ cleanupStreams,
+ clearTimer,
+ resetRecorder,
+ resetTimer,
+ stopInstantChunkInterval,
+ clearInstantChunkGuard,
+ disposeRecordingSpool,
+ deletePendingVideoSafely,
+ setUploadStatus,
+ replaceErrorDownload,
+ ]);
- const resetState = useCallback(() => {
- cleanupRecordingState();
+ const resetState = useCallback(async () => {
+ await cleanupRecordingState();
updatePhase("idle");
}, [cleanupRecordingState, updatePhase]);
@@ -384,31 +600,45 @@ export const useWebRecorder = ({
useEffect(() => {
return () => {
- resetStateRef.current();
+ void resetStateRef.current();
};
}, []);
const handleRecorderDataAvailable = useCallback(
(event: BlobEvent) => {
onRecorderDataAvailable(event, (chunk: Blob, totalBytes: number) => {
- if (instantMp4ActiveRef.current && chunk.size > 0) {
+ if (isStreamingPipelineActive() && chunk.size > 0) {
lastInstantChunkAtRef.current =
typeof performance !== "undefined" ? performance.now() : Date.now();
if (instantChunkModeRef.current === "timeslice") {
clearInstantChunkGuard();
}
}
- instantUploaderRef.current?.handleChunk(chunk, totalBytes);
+ persistChunkToRecordingSpool(chunk);
+ try {
+ instantUploaderRef.current?.handleChunk(chunk, totalBytes);
+ } catch (error) {
+ console.error("Failed to upload recording chunk", error);
+ toast.error(
+ "Upload could not keep up with recording. Stopping to protect the recording.",
+ );
+ void stopRecordingRef.current?.();
+ }
});
},
- [onRecorderDataAvailable, clearInstantChunkGuard],
+ [
+ onRecorderDataAvailable,
+ clearInstantChunkGuard,
+ isStreamingPipelineActive,
+ persistChunkToRecordingSpool,
+ ],
);
const stopRecordingInternalWrapper = useCallback(async () => {
return stopRecordingInternal(cleanupStreams, clearTimer);
}, [stopRecordingInternal, cleanupStreams, clearTimer]);
- const startRecording = async (options?: { reuseInstantVideo?: boolean }) => {
+ const startRecording = async () => {
if (!organisationId) {
toast.error("Select an organization before recording.");
return;
@@ -597,6 +827,10 @@ export const useWebRecorder = ({
dimensionsRef.current = {
width: settings?.width || undefined,
height: settings?.height || undefined,
+ fps:
+ typeof settings?.frameRate === "number"
+ ? Math.round(settings.frameRate)
+ : undefined,
};
const systemAudioTracks =
@@ -685,94 +919,79 @@ export const useWebRecorder = ({
const hasAudio = mixedStream.getAudioTracks().length > 0;
setHasAudioTrack(hasAudio);
+ const pipeline = selectRecordingPipeline(hasAudio);
+ if (!pipeline) {
+ throw new Error("No supported recording pipeline available");
+ }
+
recordedChunksRef.current = [];
totalRecordedBytesRef.current = 0;
+ await disposeRecordingSpool();
+ if (pipeline.mode === "streaming-webm") {
+ const spool = await createRecordingSpool(pipeline.mimeType);
+ if (spool) {
+ setLocalRecordingStrategy({ mode: "off" });
+ } else {
+ setLocalRecordingStrategy({ mode: "full" });
+ toast.warning(
+ "Durable local backup is unavailable. This recording will use in-memory recovery.",
+ );
+ }
+ } else {
+ setLocalRecordingStrategy({ mode: "full" });
+ }
instantUploaderRef.current = null;
- instantMp4ActiveRef.current = false;
-
- const mp4Candidates = hasAudio
- ? [...MP4_MIME_TYPES.withAudio, ...MP4_MIME_TYPES.videoOnly]
- : [...MP4_MIME_TYPES.videoOnly, ...MP4_MIME_TYPES.withAudio];
- const supportedMp4MimeType = pickSupportedMimeType(mp4Candidates);
- const webmCandidates = hasAudio
- ? [...WEBM_MIME_TYPES.withAudio, ...WEBM_MIME_TYPES.videoOnly]
- : [...WEBM_MIME_TYPES.videoOnly, ...WEBM_MIME_TYPES.withAudio];
- const fallbackMimeType = pickSupportedMimeType(webmCandidates);
- const mimeType = supportedMp4MimeType ?? fallbackMimeType;
- const useInstantMp4 = Boolean(supportedMp4MimeType);
- instantMp4ActiveRef.current = useInstantMp4;
- const shouldReuseInstantVideo = Boolean(
- options?.reuseInstantVideo && videoCreationRef.current,
- );
+ recordingPipelineRef.current = pipeline;
- if (useInstantMp4) {
- let creationResult = videoCreationRef.current;
+ if (pipeline.mode === "streaming-webm") {
const width = dimensionsRef.current.width;
const height = dimensionsRef.current.height;
const resolution = width && height ? `${width}x${height}` : undefined;
- if (!shouldReuseInstantVideo || !creationResult) {
- const creation = unwrapExitOrThrow(
- await videoInstantCreate.mutateAsync({
- orgId: Organisation.OrganisationId.make(organisationId),
- folderId: Option.none(),
- resolution,
- width,
- height,
- videoCodec: "h264",
- audioCodec: hasAudio ? "aac" : undefined,
- supportsUploadProgress: true,
- }),
- );
- creationResult = {
- id: creation.id,
- upload: creation.upload,
- shareUrl: creation.shareUrl,
- };
- videoCreationRef.current = creationResult;
- }
- if (creationResult) {
- setVideoId(creationResult.id);
- pendingInstantVideoIdRef.current = creationResult.id;
- }
-
- let uploadId: string | null = null;
- try {
- if (!creationResult)
- throw new Error("Missing instant recording context");
- uploadId = await initiateMultipartUpload(creationResult.id);
- } catch (initError) {
- const orphanId = creationResult?.id;
- if (orphanId) {
- await deleteVideo.mutateAsync(orphanId);
- }
- pendingInstantVideoIdRef.current = null;
- videoCreationRef.current = null;
- throw initError;
- }
+ const creation = unwrapExitOrThrow(
+ await videoInstantCreate.mutateAsync({
+ orgId: Organisation.OrganisationId.make(organisationId),
+ folderId: Option.none(),
+ resolution,
+ width,
+ height,
+ videoCodec: "h264",
+ audioCodec: hasAudio ? "aac" : undefined,
+ supportsUploadProgress: true,
+ }),
+ ) as InstantVideoCreation;
+ const creationResult = {
+ id: creation.id,
+ shareUrl: creation.shareUrl,
+ upload: creation.upload,
+ };
+ videoCreationRef.current = creationResult;
+ setVideoId(creation.id);
+ pendingInstantVideoIdRef.current = creation.id;
- if (!creationResult) {
- throw new Error("Instant recording metadata missing");
- }
- instantUploaderRef.current = new InstantMp4Uploader({
+ const rawSubpath = `raw-upload.${pipeline.fileExtension}`;
+ const uploadId = await initiateMultipartUpload({
+ videoId: creationResult.id,
+ contentType: pipeline.mimeType,
+ subpath: rawSubpath,
+ });
+ instantUploaderRef.current = new InstantRecordingUploader({
videoId: creationResult.id,
uploadId,
- mimeType: supportedMp4MimeType ?? "",
+ mimeType: pipeline.mimeType,
+ subpath: rawSubpath,
setUploadStatus,
sendProgressUpdate: (uploaded, total) =>
sendProgressUpdate(creationResult.id, uploaded, total),
onChunkStateChange: setChunkUploads,
+ onFatalError: () => {
+ void stopRecordingRef.current?.();
+ },
});
- } else {
- if (!shouldReuseInstantVideo) {
- videoCreationRef.current = null;
- pendingInstantVideoIdRef.current = null;
- }
}
- const recorder = new MediaRecorder(
- mixedStream,
- mimeType ? { mimeType } : undefined,
- );
+ const recorder = new MediaRecorder(mixedStream, {
+ mimeType: pipeline.mimeType,
+ });
recorder.ondataavailable = handleRecorderDataAvailable;
recorder.onstop = onRecorderStop;
recorder.onerror = onRecorderError;
@@ -790,7 +1009,7 @@ export const useWebRecorder = ({
lastInstantChunkAtRef.current = null;
clearInstantChunkGuard();
stopInstantChunkInterval();
- if (useInstantMp4) {
+ if (pipeline.mode === "streaming-webm") {
let startedWithTimeslice = false;
try {
recorder.start(INSTANT_UPLOAD_REQUEST_INTERVAL_MS);
@@ -817,21 +1036,22 @@ export const useWebRecorder = ({
startTimer();
updatePhase("recording");
} catch (err) {
- const orphanVideoId =
- instantMp4ActiveRef.current && videoCreationRef.current?.id
- ? videoCreationRef.current.id
- : null;
+ const orphanVideoId = videoCreationRef.current?.id ?? null;
+ if (instantUploaderRef.current) {
+ await instantUploaderRef.current.cancel();
+ }
+ await disposeRecordingSpool();
if (orphanVideoId) {
instantUploaderRef.current = null;
- instantMp4ActiveRef.current = false;
+ recordingPipelineRef.current = null;
videoCreationRef.current = null;
pendingInstantVideoIdRef.current = null;
- await deleteVideo.mutateAsync(orphanVideoId);
+ await deletePendingVideoSafely(orphanVideoId);
}
console.error("Failed to start recording", err);
toast.error("Could not start recording.");
- resetState();
+ await resetState();
} finally {
setIsSettingUp(false);
}
@@ -864,7 +1084,7 @@ export const useWebRecorder = ({
const timestamp = performance.now();
resumeTimer(timestamp);
recorder.resume();
- if (instantMp4ActiveRef.current) {
+ if (isStreamingPipelineActive()) {
startInstantChunkInterval();
}
updatePhase("recording");
@@ -877,6 +1097,7 @@ export const useWebRecorder = ({
resumeTimer,
updatePhase,
mediaRecorderRef,
+ isStreamingPipelineActive,
startInstantChunkInterval,
]);
@@ -887,32 +1108,37 @@ export const useWebRecorder = ({
lastInstantChunkAtRef.current = null;
replaceErrorDownload(null);
if (phase !== "recording" && phase !== "paused") return;
-
- const orgId = organisationId;
- if (!orgId) {
- updatePhase("error");
- return;
- }
-
- const timestamp = performance.now();
- commitPausedDuration(timestamp);
- const recordedDurationMs = syncDurationFromClock(timestamp);
-
- const brandedOrgId = Organisation.OrganisationId.make(orgId);
- let thumbnailBlob: Blob | null = null;
- let thumbnailPreviewUrl: string | undefined;
+ if (stopInFlightRef.current) return;
+ stopInFlightRef.current = true;
let createdVideoId: VideoId | null = videoCreationRef.current?.id ?? null;
let rawRecordingBlob: Blob | null = null;
- let processedRecordingBlob: Blob | null = null;
- const instantUploader = instantUploaderRef.current;
- const useInstantMp4 = Boolean(instantUploader);
try {
+ const orgId = organisationId;
+ if (!orgId) {
+ updatePhase("error");
+ return;
+ }
+
+ const timestamp = performance.now();
+ commitPausedDuration(timestamp);
+ const recordedDurationMs = syncDurationFromClock(timestamp);
+
+ const pipeline = recordingPipelineRef.current;
+ if (!pipeline) {
+ updatePhase("error");
+ return;
+ }
+
+ const instantUploader = instantUploaderRef.current;
+
onRecordingStop?.();
updatePhase("creating");
rawRecordingBlob = await stopRecordingInternalWrapper();
- if (!rawRecordingBlob) throw new Error("No recording available");
+ if (pipeline.mode === "buffered-raw" && !rawRecordingBlob) {
+ throw new Error("No recording available");
+ }
const durationSeconds = Math.max(
1,
@@ -920,6 +1146,7 @@ export const useWebRecorder = ({
);
const width = dimensionsRef.current.width;
const height = dimensionsRef.current.height;
+ const fps = dimensionsRef.current.fps;
const resolution = width && height ? `${width}x${height}` : undefined;
setUploadStatus({ status: "creating" });
@@ -928,7 +1155,7 @@ export const useWebRecorder = ({
if (!creationResult) {
const result = unwrapExitOrThrow(
await videoInstantCreate.mutateAsync({
- orgId: brandedOrgId,
+ orgId: Organisation.OrganisationId.make(orgId),
folderId: Option.none(),
resolution,
durationSeconds,
@@ -938,173 +1165,245 @@ export const useWebRecorder = ({
audioCodec: hasAudioTrack ? "aac" : undefined,
supportsUploadProgress: true,
}),
- );
+ ) as InstantVideoCreation;
creationResult = {
id: result.id,
- upload: result.upload,
shareUrl: result.shareUrl,
+ upload: result.upload,
};
videoCreationRef.current = creationResult;
setVideoId(result.id);
+ pendingInstantVideoIdRef.current = result.id;
}
createdVideoId = creationResult.id;
- if (creationResult.shareUrl) {
+ if (pipeline.mode === "streaming-webm" && creationResult.shareUrl) {
openShareUrl(creationResult.shareUrl);
}
- if (useInstantMp4) {
- processedRecordingBlob =
- rawRecordingBlob.type === "video/mp4"
- ? rawRecordingBlob
- : new File([rawRecordingBlob], "result.mp4", {
- type: "video/mp4",
- });
- } else {
- processedRecordingBlob = await convertToMp4(
- rawRecordingBlob,
- hasAudioTrack,
- creationResult.id,
- setUploadStatus,
- updatePhase,
- );
- }
-
- if (!processedRecordingBlob) {
- throw new Error("Failed to prepare recording for upload");
- }
-
- thumbnailBlob = await captureThumbnail(
- processedRecordingBlob,
- dimensionsRef.current,
- );
- thumbnailPreviewUrl = thumbnailBlob
- ? URL.createObjectURL(thumbnailBlob)
- : undefined;
-
updatePhase("uploading");
setUploadStatus({
status: "uploadingVideo",
capId: creationResult.id,
progress: 0,
- thumbnailUrl: thumbnailPreviewUrl,
+ thumbnailUrl: undefined,
});
- if (useInstantMp4 && instantUploader) {
- instantUploader.setThumbnailUrl(thumbnailPreviewUrl);
- await instantUploader.finalize({
- finalBlob: processedRecordingBlob,
+ if (pipeline.mode === "streaming-webm") {
+ let uploader = instantUploader;
+ const rawSubpath = `raw-upload.${pipeline.fileExtension}`;
+
+ if (!uploader) {
+ const uploadId = await initiateMultipartUpload({
+ videoId: creationResult.id,
+ contentType: pipeline.mimeType,
+ subpath: rawSubpath,
+ });
+ uploader = new InstantRecordingUploader({
+ videoId: creationResult.id,
+ uploadId,
+ mimeType: pipeline.mimeType,
+ subpath: rawSubpath,
+ setUploadStatus,
+ sendProgressUpdate: (uploaded, total) =>
+ sendProgressUpdate(creationResult.id, uploaded, total),
+ onChunkStateChange: setChunkUploads,
+ onFatalError: () => {
+ void stopRecordingRef.current?.();
+ },
+ });
+ instantUploaderRef.current = uploader;
+ }
+
+ await uploader.finalize({
+ finalBlob: rawRecordingBlob,
durationSeconds,
width,
height,
- thumbnailUrl: thumbnailPreviewUrl,
+ fps,
+ subpath: rawSubpath,
});
- instantUploaderRef.current = null;
- instantMp4ActiveRef.current = false;
} else {
- await uploadRecording(
- processedRecordingBlob,
- creationResult.upload,
- creationResult.id,
- thumbnailPreviewUrl,
- setUploadStatus,
- );
- }
+ const processedRecordingBlob =
+ pipeline.fileExtension === "mp4"
+ ? rawRecordingBlob
+ : await convertToMp4(
+ rawRecordingBlob as Blob,
+ hasAudioTrack,
+ creationResult.id,
+ setUploadStatus,
+ () => updatePhase("converting"),
+ );
- pendingInstantVideoIdRef.current = null;
+ if (!processedRecordingBlob) {
+ throw new Error("Failed to prepare recording for upload");
+ }
+
+ const thumbnailBlob = await captureThumbnail(processedRecordingBlob, {
+ width,
+ height,
+ });
+ const thumbnailPreviewUrl = thumbnailBlob
+ ? URL.createObjectURL(thumbnailBlob)
+ : undefined;
- if (thumbnailBlob) {
try {
- const screenshotData = await createVideoAndGetUploadUrl({
- videoId: creationResult.id,
- isScreenshot: true,
- orgId: brandedOrgId,
+ setUploadStatus({
+ status: "uploadingVideo",
+ capId: creationResult.id,
+ progress: 0,
+ thumbnailUrl: thumbnailPreviewUrl,
});
- const screenshotFormData = new FormData();
- Object.entries(screenshotData.presignedPostData.fields).forEach(
- ([key, value]) => {
- screenshotFormData.append(key, value as string);
- },
- );
- screenshotFormData.append(
- "file",
- thumbnailBlob,
- "screen-capture.jpg",
+ await uploadRecording(
+ processedRecordingBlob,
+ creationResult.upload,
+ creationResult.id,
+ thumbnailPreviewUrl,
+ setUploadStatus,
);
- setUploadStatus({
- status: "uploadingThumbnail",
- capId: creationResult.id,
- progress: 90,
- });
+ if (thumbnailBlob) {
+ try {
+ const screenshotData = await createVideoAndGetUploadUrl({
+ videoId: creationResult.id,
+ isScreenshot: true,
+ orgId: Organisation.OrganisationId.make(orgId),
+ });
- await new Promise<void>((resolve, reject) => {
- const xhr = new XMLHttpRequest();
- xhr.open("POST", screenshotData.presignedPostData.url);
-
- xhr.upload.onprogress = (event) => {
- if (event.lengthComputable) {
- const percent = 90 + (event.loaded / event.total) * 10;
- setUploadStatus({
- status: "uploadingThumbnail",
- capId: creationResult.id,
- progress: percent,
- });
- }
- };
-
- xhr.onload = () => {
- if (xhr.status >= 200 && xhr.status < 300) {
- resolve();
- } else {
- reject(
- new Error(
- `Screenshot upload failed with status ${xhr.status}`,
- ),
- );
- }
- };
+ const screenshotFormData = new FormData();
+ Object.entries(screenshotData.presignedPostData.fields).forEach(
+ ([key, value]) => {
+ screenshotFormData.append(key, value as string);
+ },
+ );
+ screenshotFormData.append(
+ "file",
+ thumbnailBlob,
+ "screen-capture.jpg",
+ );
- xhr.onerror = () => {
- reject(new Error("Screenshot upload failed"));
- };
+ setUploadStatus({
+ status: "uploadingThumbnail",
+ capId: creationResult.id,
+ progress: 90,
+ });
- xhr.send(screenshotFormData);
- });
+ await new Promise<void>((resolve, reject) => {
+ const xhr = new XMLHttpRequest();
+ xhr.open("POST", screenshotData.presignedPostData.url);
+
+ xhr.upload.onprogress = (event) => {
+ if (event.lengthComputable) {
+ const percent = 90 + (event.loaded / event.total) * 10;
+ setUploadStatus({
+ status: "uploadingThumbnail",
+ capId: creationResult.id,
+ progress: percent,
+ });
+ }
+ };
+
+ xhr.onload = () => {
+ if (xhr.status >= 200 && xhr.status < 300) {
+ resolve();
+ } else {
+ reject(
+ new Error(
+ `Screenshot upload failed with status ${xhr.status}`,
+ ),
+ );
+ }
+ };
+
+ xhr.onerror = () => {
+ reject(new Error("Screenshot upload failed"));
+ };
+
+ xhr.send(screenshotFormData);
+ });
- queryClient.refetchQueries({
- queryKey: ThumbnailRequest.queryKey(creationResult.id),
- });
- } catch (thumbnailError) {
- console.error("Failed to upload thumbnail", thumbnailError);
- toast.warning("Recording uploaded, but thumbnail failed to upload.");
+ queryClient.refetchQueries({
+ queryKey: ThumbnailRequest.queryKey(creationResult.id),
+ });
+ } catch (thumbnailError) {
+ console.error("Failed to upload thumbnail", thumbnailError);
+ toast.warning(
+ "Recording uploaded, but thumbnail failed to upload.",
+ );
+ }
+ }
+ } finally {
+ if (thumbnailPreviewUrl) {
+ URL.revokeObjectURL(thumbnailPreviewUrl);
+ }
}
}
+ instantUploaderRef.current = null;
+ recordingPipelineRef.current = null;
+ pendingInstantVideoIdRef.current = null;
+ await disposeRecordingSpool();
+
setUploadStatus(undefined);
updatePhase("completed");
- toast.success("Recording uploaded");
+ toast.success(
+ pipeline.mode === "streaming-webm"
+ ? "Recording uploaded. Processing will continue shortly."
+ : "Recording uploaded.",
+ );
openShareUrl(creationResult.shareUrl);
router.refresh();
} catch (err) {
console.error("Failed to process recording", err);
setUploadStatus(undefined);
+ const failureBlob = await resolveFailureBlob(rawRecordingBlob);
+ if (err instanceof ProcessingStartError) {
+ instantUploaderRef.current = null;
+ recordingPipelineRef.current = null;
+ pendingInstantVideoIdRef.current = null;
+ videoCreationRef.current = null;
+ replaceErrorDownload(failureBlob);
+ await disposeRecordingSpool();
+ updatePhase("error");
+ toast.error(
+ "Recording uploaded, but processing could not start. Open the video to retry processing.",
+ );
+ router.refresh();
+ return;
+ }
+ if (err instanceof MultipartCompletionUncertainError) {
+ instantUploaderRef.current = null;
+ recordingPipelineRef.current = null;
+ pendingInstantVideoIdRef.current = null;
+ replaceErrorDownload(failureBlob);
+ await disposeRecordingSpool();
+ updatePhase("error");
+ toast.error(
+ "Upload confirmation was interrupted. Open the video to verify processing before retrying.",
+ );
+ openShareUrl(videoCreationRef.current?.shareUrl ?? null);
+ router.refresh();
+ return;
+ }
updatePhase("error");
- replaceErrorDownload(processedRecordingBlob ?? rawRecordingBlob);
+ replaceErrorDownload(failureBlob);
+ if (instantUploaderRef.current) {
+ await instantUploaderRef.current.cancel();
+ instantUploaderRef.current = null;
+ }
+ await disposeRecordingSpool();
const idToDelete = createdVideoId ?? videoId;
if (idToDelete) {
- await deleteVideo.mutateAsync(idToDelete);
+ await deletePendingVideoSafely(idToDelete);
if (pendingInstantVideoIdRef.current === idToDelete) {
pendingInstantVideoIdRef.current = null;
}
}
} finally {
- if (thumbnailPreviewUrl) {
- URL.revokeObjectURL(thumbnailPreviewUrl);
- }
+ stopInFlightRef.current = false;
}
}, [
stopInstantChunkInterval,
@@ -1114,16 +1413,18 @@ export const useWebRecorder = ({
videoId,
updatePhase,
setUploadStatus,
- deleteVideo,
+ deletePendingVideoSafely,
videoInstantCreate,
+ queryClient,
router,
stopRecordingInternalWrapper,
- queryClient,
onRecordingStop,
commitPausedDuration,
syncDurationFromClock,
openShareUrl,
replaceErrorDownload,
+ resolveFailureBlob,
+ disposeRecordingSpool,
clearInstantChunkGuard,
]);
@@ -1161,8 +1462,6 @@ export const useWebRecorder = ({
if (isRestarting) return;
if (phase !== "recording" && phase !== "paused") return;
- const creationToReuse = videoCreationRef.current;
- const shouldReuseInstantVideo = Boolean(creationToReuse);
setIsRestarting(true);
try {
@@ -1172,24 +1471,18 @@ export const useWebRecorder = ({
console.warn("Failed to stop recorder before restart", error);
}
- cleanupRecordingState({ preserveInstantVideo: shouldReuseInstantVideo });
+ await cleanupRecordingState();
updatePhase("idle");
- if (shouldReuseInstantVideo && creationToReuse) {
- await deleteVideoResultFile({ videoId: creationToReuse.id });
- }
-
const latestStartRecording = startRecordingRef.current;
if (!latestStartRecording) {
throw new Error("Recorder not ready to start");
}
- await latestStartRecording({
- reuseInstantVideo: shouldReuseInstantVideo,
- });
+ await latestStartRecording();
} catch (error) {
console.error("Failed to restart recording", error);
toast.error("Could not restart recording. Please try again.");
- cleanupRecordingState();
+ await cleanupRecordingState();
updatePhase("idle");
} finally {
setIsRestarting(false);
@@ -1224,6 +1517,7 @@ export const useWebRecorder = ({
hasAudioTrack,
chunkUploads,
errorDownload,
+ recoveredDownloads,
isSettingUp,
isRecording: isRecordingActive,
isPaused,
@@ -1235,6 +1529,7 @@ export const useWebRecorder = ({
stopRecording,
restartRecording,
resetState,
+ dismissRecoveredDownload,
isRestarting,
isBrowserSupported,
unsupportedReason,
diff --git a/apps/web/app/(org)/dashboard/caps/components/web-recorder-dialog/web-recorder-types.ts b/apps/web/app/(org)/dashboard/caps/components/web-recorder-dialog/web-recorder-types.ts
index dd84b3e37..078bc5a23 100644
--- a/apps/web/app/(org)/dashboard/caps/components/web-recorder-dialog/web-recorder-types.ts
+++ b/apps/web/app/(org)/dashboard/caps/components/web-recorder-dialog/web-recorder-types.ts
@@ -26,3 +26,8 @@ export type RecordingFailureDownload = {
url: string;
fileName: string;
};
+
+export type RecoveredRecordingDownload = RecordingFailureDownload & {
+ id: string;
+ createdAt: number;
+};