Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
import {
CloudFrontClient,
CreateInvalidationCommand,
} from "@aws-sdk/client-cloudfront";
import { db } from "@cap/database";
import { videos, videoUploads } from "@cap/database/schema";
import { serverEnv } from "@cap/env";
import { AwsCredentials } from "@cap/web-backend/src/Aws";
import { Storage } from "@cap/web-backend/src/Storage/index";
import { Video } from "@cap/web-domain";
import { eq } from "drizzle-orm";
import { Effect } from "effect";
import { FatalError, sleep } from "workflow";
import { retireDesktopRecordingJobForOutputReplacement } from "@/lib/desktop-recording-jobs";
import {
createMediaServerCapacityError,
isMediaServerCapacityError,
} from "@/lib/media-server-backpressure";
import { decodeStorageVideo } from "@/lib/video-storage";
import { runWorkflowPromise } from "@/lib/workflow-runtime";
interface AdminReprocessVideoWorkflowPayload {
videoId: string;
}
interface MediaServerProcessResult {
metadata: {
duration: number;
width: number;
height: number;
fps: number;
};
ownerId: string;
bucketId: string | null;
}
interface MediaServerJobBody {
videoId: string;
userId: string;
videoUrl: string;
outputPresignedUrl: string;
thumbnailPresignedUrl: string;
previewGifPresignedUrl: string;
webhookUrl: string;
webhookSecret?: string;
inputExtension: string;
}
const MEDIA_SERVER_START_MAX_ATTEMPTS = 2;
const MEDIA_SERVER_START_RETRY_BASE_MS = 250;
const MEDIA_SERVER_COMPLETION_MAX_ATTEMPTS = 720;
const MEDIA_SERVER_COMPLETION_POLL_INTERVAL_MS = 5000;
const MEDIA_SERVER_PRESIGNED_GET_EXPIRES_SECONDS = 3 * 60 * 60;
const MEDIA_SERVER_PRESIGNED_PUT_EXPIRES_SECONDS = 3 * 60 * 60;
function getValidDuration(duration: number) {
return Number.isFinite(duration) && duration > 0 ? duration : undefined;
}
function isPositiveNumber(value: number | null): value is number {
return typeof value === "number" && Number.isFinite(value) && value > 0;
}
async function waitForRetry(delayMs: number): Promise<void> {
await new Promise((resolve) => setTimeout(resolve, delayMs));
}
async function startMediaServerProcessJob(
mediaServerUrl: string,
body: MediaServerJobBody,
): Promise<string> {
for (let attempt = 0; attempt < MEDIA_SERVER_START_MAX_ATTEMPTS; attempt++) {
const headers: Record<string, string> = {
"Content-Type": "application/json",
};
if (body.webhookSecret) {
headers["x-media-server-secret"] = body.webhookSecret;
}
const response = await fetch(`${mediaServerUrl}/video/process`, {
method: "POST",
headers,
body: JSON.stringify(body),
});
if (response.ok) {
const { jobId } = (await response.json()) as { jobId: string };
return jobId;
}
const errorData = (await response.json().catch(() => ({}))) as {
error?: string;
code?: string;
details?: string;
instanceId?: string;
pid?: number;
activeVideoProcesses?: number;
maxConcurrentVideoProcesses?: number;
jobCount?: number;
};
const baseErrorMessage =
errorData.error ||
errorData.details ||
"Video reprocessing failed to start";
const busyDiagnostics =
errorData.code === "SERVER_BUSY"
? [
errorData.instanceId ? `instance=${errorData.instanceId}` : null,
typeof errorData.pid === "number" ? `pid=${errorData.pid}` : null,
typeof errorData.activeVideoProcesses === "number" &&
typeof errorData.maxConcurrentVideoProcesses === "number"
? `active=${errorData.activeVideoProcesses}/${errorData.maxConcurrentVideoProcesses}`
: null,
typeof errorData.jobCount === "number"
? `jobCount=${errorData.jobCount}`
: null,
]
.filter(Boolean)
.join(", ")
: "";
const errorMessage = busyDiagnostics
? `${baseErrorMessage} (${busyDiagnostics})`
: baseErrorMessage;
const shouldRetry =
response.status === 503 &&
(errorData.code === "SERVER_BUSY" ||
errorMessage.includes("Server is busy"));
if (shouldRetry && attempt < MEDIA_SERVER_START_MAX_ATTEMPTS - 1) {
await waitForRetry(MEDIA_SERVER_START_RETRY_BASE_MS * 2 ** attempt);
continue;
}
if (shouldRetry) {
throw createMediaServerCapacityError({
response,
message: errorMessage,
videoId: body.videoId,
});
}
throw new Error(errorMessage);
}
throw new Error("Video reprocessing failed to start");
}
function getMetadataFromVideoRow(
video:
| {
duration: number | null;
width: number | null;
height: number | null;
fps: number | null;
}
| undefined,
): MediaServerProcessResult["metadata"] | null {
if (
!video ||
!isPositiveNumber(video.width) ||
!isPositiveNumber(video.height) ||
!isPositiveNumber(video.fps)
) {
return null;
}
return {
duration: isPositiveNumber(video.duration) ? video.duration : 0,
width: video.width,
height: video.height,
fps: video.fps,
};
}
async function getCompletedMetadata(
videoId: string,
): Promise<MediaServerProcessResult["metadata"] | null> {
const [video] = await db()
.select({
duration: videos.duration,
width: videos.width,
height: videos.height,
fps: videos.fps,
})
.from(videos)
.where(eq(videos.id, videoId as Video.VideoId));
return getMetadataFromVideoRow(video);
}
async function waitForProcessingCompletion(
videoId: string,
): Promise<MediaServerProcessResult["metadata"]> {
let lastStatus = "processing";
for (
let attempt = 0;
attempt < MEDIA_SERVER_COMPLETION_MAX_ATTEMPTS;
attempt++
) {
await waitForRetry(MEDIA_SERVER_COMPLETION_POLL_INTERVAL_MS);
const [upload] = await db()
.select({
phase: videoUploads.phase,
processingProgress: videoUploads.processingProgress,
processingMessage: videoUploads.processingMessage,
processingError: videoUploads.processingError,
})
.from(videoUploads)
.where(eq(videoUploads.videoId, videoId as Video.VideoId));
if (!upload || upload.phase === "complete") {
const metadata = await getCompletedMetadata(videoId);
if (!metadata) {
throw new Error("Reprocessing completed but video metadata is missing");
}
return metadata;
}
if (upload.processingError) {
throw new Error(upload.processingError);
}
if (upload.phase === "error") {
throw new Error(upload.processingMessage || "Video reprocessing failed");
}
lastStatus = [
upload.phase,
typeof upload.processingProgress === "number"
? `${upload.processingProgress}%`
: null,
upload.processingMessage,
]
.filter(Boolean)
.join(" ");
}
throw new Error(`Video reprocessing timed out while ${lastStatus}`);
}
async function validateReprocessRequest(videoId: string): Promise<void> {
"use step";
const mediaServerUrl = serverEnv().MEDIA_SERVER_URL;
if (!mediaServerUrl) {
throw new FatalError("MEDIA_SERVER_URL is not configured");
}
const [video] = await db()
.select({ id: videos.id })
.from(videos)
.where(eq(videos.id, Video.VideoId.make(videoId)));
if (!video) {
throw new FatalError("Video does not exist");
}
}
async function processExistingResultOnMediaServer(
videoId: string,
): Promise<MediaServerProcessResult> {
"use step";
const mediaServerUrl = serverEnv().MEDIA_SERVER_URL;
const webhookBaseUrl =
serverEnv().MEDIA_SERVER_WEBHOOK_URL || serverEnv().WEB_URL;
if (!mediaServerUrl) {
throw new FatalError("MEDIA_SERVER_URL is not configured");
}
const [video] = await db()
.select()
.from(videos)
.where(eq(videos.id, Video.VideoId.make(videoId)));
if (!video) {
throw new FatalError("Video does not exist");
}
const videoDomain = decodeStorageVideo(video);
const [bucket] =
await Storage.getAccessForVideo(videoDomain).pipe(runWorkflowPromise);
const resultKey = `${video.ownerId}/${video.id}/result.mp4`;
const thumbnailKey = `${video.ownerId}/${video.id}/screenshot/screen-capture.jpg`;
const previewGifKey = `${video.ownerId}/${video.id}/preview/animated-preview.gif`;
const videoUrl = await bucket
.getInternalSignedObjectUrl(resultKey, {
expiresIn: MEDIA_SERVER_PRESIGNED_GET_EXPIRES_SECONDS,
})
.pipe(runWorkflowPromise);
const outputPresignedUrl = await bucket
.getInternalPresignedPutUrl(
resultKey,
{
ContentType: "video/mp4",
},
{ expiresIn: MEDIA_SERVER_PRESIGNED_PUT_EXPIRES_SECONDS },
)
.pipe(runWorkflowPromise);
const thumbnailPresignedUrl = await bucket
.getInternalPresignedPutUrl(
thumbnailKey,
{
ContentType: "image/jpeg",
},
{ expiresIn: MEDIA_SERVER_PRESIGNED_PUT_EXPIRES_SECONDS },
)
.pipe(runWorkflowPromise);
const previewGifPresignedUrl = await bucket
.getInternalPresignedPutUrl(
previewGifKey,
{
ContentType: "image/gif",
CacheControl: "public, max-age=31536000, immutable",
},
{ expiresIn: MEDIA_SERVER_PRESIGNED_PUT_EXPIRES_SECONDS },
)
.pipe(runWorkflowPromise);
const webhookUrl = `${webhookBaseUrl}/api/webhooks/media-server/progress`;
const webhookSecret = serverEnv().MEDIA_SERVER_WEBHOOK_SECRET;
await db()
.insert(videoUploads)
.values({
videoId: video.id,
uploaded: 0,
total: 0,
mode: "singlepart",
phase: "processing",
processingProgress: 0,
processingMessage: "Starting admin reprocess...",
processingError: null,
rawFileKey: resultKey,
updatedAt: new Date(),
})
.onDuplicateKeyUpdate({
set: {
uploaded: 0,
total: 0,
mode: "singlepart",
phase: "processing",
processingProgress: 0,
processingMessage: "Starting admin reprocess...",
processingError: null,
rawFileKey: resultKey,
updatedAt: new Date(),
},
});
await startMediaServerProcessJob(mediaServerUrl, {
videoId,
userId: video.ownerId,
videoUrl,
outputPresignedUrl,
thumbnailPresignedUrl,
previewGifPresignedUrl,
webhookUrl,
webhookSecret: webhookSecret || undefined,
inputExtension: ".mp4",
});
const metadata = await waitForProcessingCompletion(videoId);
return {
metadata,
ownerId: video.ownerId,
bucketId: video.bucket,
};
}
export async function saveMetadataAndComplete(
videoId: string,
metadata: { duration: number; width: number; height: number; fps: number },
): Promise<void> {
"use step";
const duration = getValidDuration(metadata.duration);
const [video] = await db()
.select()
.from(videos)
.where(eq(videos.id, Video.VideoId.make(videoId)));
if (!video) throw new Error("Video does not exist");
const [bucket] = await Storage.getAccessForVideo(decodeStorageVideo(video), {
resolvePublishedOutput: false,
}).pipe(runWorkflowPromise);
const head = await bucket
.headObject(`${video.ownerId}/${video.id}/result.mp4`)
.pipe(runWorkflowPromise);
if (!head.ETag || !head.ContentLength || head.ContentLength <= 0) {
throw new Error("Reprocessed recording output is missing or empty");
}
await db().transaction(async (tx) => {
await retireDesktopRecordingJobForOutputReplacement(tx, {
videoId: video.id,
userId: video.ownerId,
});
const [lockedVideo] = await tx
.select()
.from(videos)
.where(eq(videos.id, video.id))
.for("update");
if (
!lockedVideo ||
lockedVideo.ownerId !== video.ownerId ||
lockedVideo.bucket !== video.bucket ||
lockedVideo.storageIntegrationId !== video.storageIntegrationId
) {
throw new Error("Recording storage changed during reprocessing");
}
const nextMetadata = { ...(lockedVideo.metadata ?? {}) };
delete nextMetadata.desktopRecordingUpload;
await tx
.update(videos)
.set({
width: metadata.width,
height: metadata.height,
fps: metadata.fps,
metadata: nextMetadata,
...(lockedVideo.source.type === "desktopMP4" ||
lockedVideo.source.type === "webMP4"
? { source: { type: lockedVideo.source.type } }
: {}),
...(duration === undefined ? {} : { duration }),
})
.where(eq(videos.id, video.id));
await tx.delete(videoUploads).where(eq(videoUploads.videoId, video.id));
});
}
async function invalidateResultCache(
videoId: string,
ownerId: string,
bucketId: string | null,
): Promise<void> {
"use step";
if (bucketId) return;
const distributionId = serverEnv().CAP_CLOUDFRONT_DISTRIBUTION_ID;
if (!distributionId) return;
const basePath = `/${ownerId}/${videoId}`;
const paths = [
`${basePath}/result.mp4`,
`${basePath}/screenshot/screen-capture.jpg`,
`${basePath}/preview/animated-preview.gif`,
];
try {
const cloudfront = new CloudFrontClient({
region: serverEnv().CAP_AWS_REGION || "us-east-1",
credentials: await runWorkflowPromise(
Effect.map(AwsCredentials, (c) => c.credentials),
),
});
await cloudfront.send(
new CreateInvalidationCommand({
DistributionId: distributionId,
InvalidationBatch: {
CallerReference: `admin-reprocess-${videoId}-${Date.now()}`,
Paths: {
Quantity: paths.length,
Items: paths,
},
},
}),
);
} catch (error) {
console.warn("[adminReprocessVideoWorkflow] Failed to invalidate cache", {
error,
videoId,
});
}
}
async function setReprocessError(
videoId: string,
errorMessage: string,
): Promise<void> {
"use step";
await db()
.update(videoUploads)
.set({
phase: "error",
processingProgress: 0,
processingMessage: "Admin reprocess failed",
processingError: errorMessage,
updatedAt: new Date(),
})
.where(eq(videoUploads.videoId, videoId as Video.VideoId));
}
export async function adminReprocessVideoWorkflow(
payload: AdminReprocessVideoWorkflowPayload,
): Promise<{ success: boolean; message: string }> {
"use workflow";
const { videoId } = payload;
try {
await validateReprocessRequest(videoId);
let result: MediaServerProcessResult;
let capacityRetryCount = 0;
while (true) {
try {
result = await processExistingResultOnMediaServer(videoId);
break;
} catch (error) {
if (!isMediaServerCapacityError(error)) throw error;
await markReprocessWaitingForCapacity(videoId);
await sleep(`${Math.min(120, 15 + capacityRetryCount * 15)}s`);
capacityRetryCount++;
}
}
await saveMetadataAndComplete(videoId, result.metadata);
await invalidateResultCache(videoId, result.ownerId, result.bucketId);
return {
success: true,
message: "Video reprocessing completed",
};
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
await setReprocessError(videoId, errorMessage);
throw new FatalError(errorMessage);
}
}
async function markReprocessWaitingForCapacity(videoId: string): Promise<void> {
"use step";
await db()
.update(videoUploads)
.set({
processingMessage: "Queued for video reprocessing...",
processingError: null,
updatedAt: new Date(),
})
.where(eq(videoUploads.videoId, videoId as Video.VideoId));
}