Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

Code Animation: Record (real-time) saves an MP4 to Media History and plays it inline

Claudeed7e6be31754
diff --git a/client/src/components/codeAnimation/CodeAnimationPreview.jsx b/client/src/components/codeAnimation/CodeAnimationPreview.jsx
index e0a9ae2848..1b046782f9 100644
--- a/client/src/components/codeAnimation/CodeAnimationPreview.jsx
+++ b/client/src/components/codeAnimation/CodeAnimationPreview.jsx
@@ -1,8 +1,8 @@
import { useEffect, useMemo, useRef, useState } from 'react';
-import { Circle, Clapperboard, Download, FileCode2, LoaderCircle, Save } from 'lucide-react';
+import { Circle, Clapperboard, Download, FileCode2, LoaderCircle } from 'lucide-react';
import { Link } from 'react-router';
import toast from '../ui/Toast';
-import { cancelCodeAnimationExport, exportCodeAnimation, getCodeAnimationPackage, getCodeAnimationThreeVendor, uploadGalleryVideo } from '../../services/api';
+import { cancelCodeAnimationExport, exportCodeAnimation, getCodeAnimationPackage, getCodeAnimationThreeVendor } from '../../services/api';
import { useSseProgress } from '../../hooks/useSseProgress';
import { downloadBlob } from '../../lib/downloadBlob';
import { importsThree, inlineThreeVendor } from '../../lib/threeVendorImportMap';
@@ -85,8 +85,9 @@ export default function CodeAnimationPreview({ html, audioUrl, messages, audioGl
const [meta, setMeta] = useState(null);
const [recording, setRecording] = useState(false);
const [recordProgress, setRecordProgress] = useState(0);
+ // The recording as saved to Media History: { id, filename, url }.
const [video, setVideo] = useState(null);
- const [saving, setSaving] = useState(false);
+ const [savingRecording, setSavingRecording] = useState(false);
const [packageDownloading, setPackageDownloading] = useState(false);
const recordTimerRef = useRef(null);
const recordingSessionRef = useRef(null);
@@ -159,16 +160,10 @@ export default function CodeAnimationPreview({ html, audioUrl, messages, audioGl
clearTimeout(recordTimerRef.current);
setMeta(null);
setRecording(false);
+ setSavingRecording(false);
setVideo(null);
}, [request]);
- // Keyed on the URL, not the object: marking a video saved replaces the
- // object but keeps its URL alive.
- const videoUrl = video?.url;
- useEffect(() => () => {
- if (videoUrl) URL.revokeObjectURL(videoUrl);
- }, [videoUrl]);
-
useEffect(() => () => clearTimeout(recordTimerRef.current), []);
useEffect(() => {
@@ -191,10 +186,13 @@ export default function CodeAnimationPreview({ html, audioUrl, messages, audioGl
}, [preview.lastEvent, messages]);
useEffect(() => {
- if (!preview.recording) return;
+ const saved = preview.recording;
+ if (!saved) { setSavingRecording(false); return; }
clearTimeout(recordTimerRef.current);
setRecording(false);
- setVideo({ blob: preview.recording.blob, url: preview.recording.url, saved: false });
+ setSavingRecording(saved.status === 'saving');
+ if (saved.status === 'saved') setVideo(saved.video);
+ if (saved.status === 'failed') toast.error(saved.message);
}, [preview.recording]);
const duration = Number(meta?.duration) || frame?.durationSeconds || 0;
@@ -202,11 +200,11 @@ export default function CodeAnimationPreview({ html, audioUrl, messages, audioGl
const fileBase = slug(title);
const handleRecord = () => {
- if (!preview.ready || recording || !messages?.record) return;
+ if (!preview.ready || recording || savingRecording || !messages?.record) return;
recordingSessionRef.current = preview.sessionId;
setRecording(true);
setRecordProgress(0);
- preview.command('record', { message: { type: messages.record } });
+ preview.command('record', { message: { type: messages.record }, title: title || '' });
clearTimeout(recordTimerRef.current);
recordTimerRef.current = setTimeout(() => {
setRecording(false);
@@ -230,20 +228,6 @@ export default function CodeAnimationPreview({ html, audioUrl, messages, audioGl
setExportJob(queued.jobId);
};
- const handleSave = async () => {
- if (!video || video.saved || saving) return;
- setSaving(true);
- const saved = await uploadGalleryVideo(video.blob, `${fileBase}.webm`, { silent: true }).catch((error) => {
- toast.error(error.message || 'Failed to save the video');
- return null;
- });
- setSaving(false);
- if (saved?.id) {
- setVideo((current) => (current?.blob === video.blob ? { ...current, saved: true } : current));
- toast.success('Saved to Media History');
- }
- };
-
const handleDownloadPackage = async () => {
if (!jobId || packageDownloading) return;
setPackageDownloading(true);
@@ -322,11 +306,12 @@ export default function CodeAnimationPreview({ html, audioUrl, messages, audioGl
<button
type="button"
onClick={handleRecord}
- disabled={!preview.ready || recording}
+ disabled={!preview.ready || recording || savingRecording}
className="inline-flex items-center gap-2 rounded-lg bg-port-error/90 px-3 py-1.5 text-sm text-white hover:opacity-90 disabled:cursor-not-allowed disabled:opacity-40"
>
- {recording ? <LoaderCircle className="h-4 w-4 animate-spin" /> : <Circle className="h-4 w-4 fill-current" />}
- {recording ? `Recording ${Math.floor(recordProgress)}s / ${Math.round(duration)}s` : 'Record (real-time)'}
+ {recording || savingRecording ? <LoaderCircle className="h-4 w-4 animate-spin" /> : <Circle className="h-4 w-4 fill-current" />}
+ {recording ? `Recording ${Math.floor(recordProgress)}s / ${Math.round(duration)}s`
+ : savingRecording ? 'Saving MP4…' : 'Record (real-time)'}
</button>
{jobId && (
<button
@@ -392,27 +377,12 @@ export default function CodeAnimationPreview({ html, audioUrl, messages, audioGl
{video && (
<div className="space-y-2 rounded-lg border border-port-border bg-port-card p-3">
- <video src={video.url} controls className="max-h-80 w-full rounded bg-black" aria-label="Recorded animation" />
+ <video src={video.url} controls playsInline className="max-h-80 w-full rounded bg-black" aria-label="Recorded animation" />
<div className="flex flex-wrap items-center gap-2">
- <button
- type="button"
- onClick={() => downloadBlob(video.blob, `${fileBase}.webm`)}
- className={BUTTON_SECONDARY}
- >
- <Download className="h-4 w-4" /> Download WebM
- </button>
- <button
- type="button"
- onClick={handleSave}
- disabled={saving || video.saved}
- className={BUTTON_PRIMARY}
- >
- {saving ? <LoaderCircle className="h-4 w-4 animate-spin" /> : <Save className="h-4 w-4" />}
- {video.saved ? 'Saved' : 'Save to Media History'}
- </button>
- {video.saved && (
- <Link to="/media/history" className="text-xs text-port-accent hover:underline">Open Media History</Link>
- )}
+ <a href={video.url} download={`${fileBase}.${video.filename?.split('.').pop() || 'mp4'}`} className={BUTTON_SECONDARY}>
+ <Download className="h-4 w-4" /> Download {video.filename?.endsWith('.webm') ? 'WebM' : 'MP4'}
+ </a>
+ <Link to="/media/history" className="text-xs text-port-accent hover:underline">Saved to Media History</Link>
</div>
</div>
)}
diff --git a/client/src/components/codeAnimation/CodeAnimationPreview.test.jsx b/client/src/components/codeAnimation/CodeAnimationPreview.test.jsx
index 76edbfaa1d..57cb7f7a19 100644
--- a/client/src/components/codeAnimation/CodeAnimationPreview.test.jsx
+++ b/client/src/components/codeAnimation/CodeAnimationPreview.test.jsx
@@ -36,12 +36,14 @@ const socket = vi.hoisted(() => {
vi.mock('../../services/socket', () => ({ default: socket }));
vi.mock('../../services/api', () => ({ uploadGalleryVideo: vi.fn(), exportCodeAnimation: vi.fn(), cancelCodeAnimationExport: vi.fn(), getCodeAnimationPackage: vi.fn(), getCodeAnimationThreeVendor: vi.fn() }));
vi.mock('../../lib/downloadBlob', () => ({ downloadBlob: vi.fn() }));
+vi.mock('../ui/Toast', () => ({ default: Object.assign(vi.fn(), { error: vi.fn(), success: vi.fn() }) }));
vi.mock('../../hooks/useSseProgress', () => ({ useSseProgress: vi.fn(() => ({ latest: null })) }));
import CodeAnimationPreview, { prepareAnimationHtml } from './CodeAnimationPreview';
import { exportCodeAnimation, getCodeAnimationPackage, getCodeAnimationThreeVendor } from '../../services/api';
import { downloadBlob } from '../../lib/downloadBlob';
import { useSseProgress } from '../../hooks/useSseProgress';
+import toast from '../ui/Toast';
const MESSAGES = { ready: 'ca:ready', record: 'ca:record', recorded: 'ca:recorded', progress: 'ca:progress', error: 'ca:error' };
const HTML = '<!DOCTYPE html><html><head><title>x</title></head><body><canvas></canvas></body></html>';
@@ -139,7 +141,7 @@ describe('CodeAnimationPreview', () => {
expect(socket.emit.mock.calls.filter(([event]) => event === 'generated-preview:command')).toHaveLength(1);
act(() => {
for (const fn of socket.__handlers.get('generated-preview:recording') || []) {
- fn({ requestId: oldOpen.requestId, sessionId: socket.__sessions.get(oldOpen.requestId), index: 0, count: 1, mime: 'video/webm', chunk: btoa('x') });
+ fn({ requestId: oldOpen.requestId, sessionId: socket.__sessions.get(oldOpen.requestId), status: 'saved', video: { id: 'upload-1', filename: 'upload-1.mp4', url: '/data/videos/upload-1.mp4' } });
}
});
expect(screen.queryByLabelText('Recorded animation')).not.toBeInTheDocument();
@@ -222,23 +224,50 @@ describe('CodeAnimationPreview', () => {
await user.click(screen.getByRole('button', { name: /record \(real-time\)/i }));
expect(socket.emit).toHaveBeenCalledWith('generated-preview:command', expect.objectContaining({
- action: 'record', message: { type: MESSAGES.record },
+ action: 'record', message: { type: MESSAGES.record }, title: 'Lantern',
}));
await act(async () => {
for (const fn of socket.__handlers.get('generated-preview:recording') || []) {
- fn({ requestId: 'other', sessionId: 'other', index: 0, count: 1, mime: 'video/webm', chunk: btoa('x') });
+ fn({ requestId: 'other', sessionId: 'other', status: 'saved', video: { id: 'x', filename: 'x.mp4', url: '/data/videos/x.mp4' } });
}
});
expect(screen.queryByLabelText('Recorded animation')).not.toBeInTheDocument();
await act(async () => {
for (const fn of socket.__handlers.get('generated-preview:recording') || []) {
- fn({ requestId: open.requestId, sessionId: socket.__sessions.get(open.requestId), index: 0, count: 1, mime: 'video/webm', chunk: btoa('x') });
+ fn({ requestId: open.requestId, sessionId: socket.__sessions.get(open.requestId), status: 'saving' });
}
});
- expect(screen.getByLabelText('Recorded animation')).toHaveAttribute('src', 'blob:recorded');
- expect(screen.getByRole('button', { name: /save to media history/i })).toBeEnabled();
+ expect(screen.getByRole('button', { name: /saving mp4/i })).toBeDisabled();
+
+ await act(async () => {
+ for (const fn of socket.__handlers.get('generated-preview:recording') || []) {
+ fn({ requestId: open.requestId, sessionId: socket.__sessions.get(open.requestId), status: 'saved', video: { id: 'upload-1', filename: 'upload-1.mp4', url: '/data/videos/upload-1.mp4' } });
+ }
+ });
+ expect(screen.getByLabelText('Recorded animation')).toHaveAttribute('src', '/data/videos/upload-1.mp4');
+ const download = screen.getByRole('link', { name: /download mp4/i });
+ expect(download).toHaveAttribute('href', '/data/videos/upload-1.mp4');
+ expect(download).toHaveAttribute('download', 'lantern.mp4');
+ expect(screen.getByRole('link', { name: /saved to media history/i })).toHaveAttribute('href', '/media/history');
+ expect(screen.getByRole('button', { name: /record \(real-time\)/i })).toBeEnabled();
+ });
+
+ it('reports a recording that could not be saved and lets the user record again', async () => {
+ const user = userEvent.setup();
+ renderPreview();
+ await screen.findByTitle('Code animation preview');
+ const open = previewOpens().at(-1)[1];
+ await user.click(screen.getByRole('button', { name: /record \(real-time\)/i }));
+ await act(async () => {
+ for (const fn of socket.__handlers.get('generated-preview:recording') || []) {
+ fn({ requestId: open.requestId, sessionId: socket.__sessions.get(open.requestId), status: 'failed', message: 'The recording is too large to save.' });
+ }
+ });
+ expect(toast.error).toHaveBeenCalledWith('The recording is too large to save.');
+ expect(screen.queryByLabelText('Recorded animation')).not.toBeInTheDocument();
+ expect(screen.getByRole('button', { name: /record \(real-time\)/i })).toBeEnabled();
});
it('does not read the selected audio until the user allows the isolated preview to use it', async () => {
@@ -336,7 +365,7 @@ describe('CodeAnimationPreview playhead', () => {
await act(async () => {
for (const fn of socket.__handlers.get('generated-preview:recording') || []) {
- fn({ requestId: open.requestId, sessionId, index: 0, count: 1, mime: 'video/webm', chunk: btoa('x') });
+ fn({ requestId: open.requestId, sessionId, status: 'saved', video: { id: 'upload-1', filename: 'upload-1.mp4', url: '/data/videos/upload-1.mp4' } });
}
});
await waitFor(() => expect(socket.emit.mock.calls.slice(recordAt).some(([event, payload]) => event === 'generated-preview:seek' && payload.playing === false)).toBe(true));
diff --git a/client/src/hooks/useGeneratedPreview.js b/client/src/hooks/useGeneratedPreview.js
index 4f4586c504..cbb8eeb9e0 100644
--- a/client/src/hooks/useGeneratedPreview.js
+++ b/client/src/hooks/useGeneratedPreview.js
@@ -50,7 +50,6 @@ export default function useGeneratedPreview({ active, request, controls = null }
const decodedUrl = useRef('');
const frameUrls = useRef(new Map());
const presentation = useRef({ lease: 0, current: null, queue: [], blocked: true, task: 0, raf: 0, deadline: 0 });
- const recordingUrl = useRef('');
const controlsRef = useRef(controls);
controlsRef.current = controls;
const [state, setState] = useState(EMPTY);
@@ -346,7 +345,6 @@ export default function useGeneratedPreview({ active, request, controls = null }
return undefined;
}
let requestId = null;
- const recordingChunks = new Map();
let stoppedStatus = null;
// Sessions belong to one socket connection. Never buffer an open/command
// while offline: the server has already destroyed the previous session.
@@ -374,8 +372,8 @@ export default function useGeneratedPreview({ active, request, controls = null }
const onDisconnect = () => {
cancelPlayback();
pending.current = null;
- recordingChunks.clear();
- if (mounted.current) setState((prev) => ({ ...prev, status: 'disconnected', sessionId: null, ready: false, lastEvent: null }));
+ // A save in flight reports to the lost connection; Media History still gets it.
+ if (mounted.current) setState((prev) => ({ ...prev, status: 'disconnected', sessionId: null, ready: false, lastEvent: null, recording: null }));
};
const onConnect = () => {
if (!stoppedStatus) open();
@@ -494,30 +492,22 @@ export default function useGeneratedPreview({ active, request, controls = null }
if (!mine(payload) || !mounted.current) return;
setState((prev) => ({ ...prev, lastEvent: payload }));
};
+ // The server saves a requested recording to Media History and reports
+ // `saving`, then `saved` with the library video or `failed` with a reason.
const onRecording = (payload) => {
- if (!mine(payload) || typeof payload.chunk !== 'string' || !payload.count) return;
- const gen = payload.generation ?? 0;
- let genChunks = recordingChunks.get(gen);
- if (!genChunks) {
- genChunks = [];
- recordingChunks.set(gen, genChunks);
+ if (!mine(payload) || !mounted.current) return;
+ if (payload.status === 'saving') setState((prev) => ({ ...prev, recording: { status: 'saving' } }));
+ else if (payload.status === 'saved' && typeof payload.video?.url === 'string') {
+ setState((prev) => ({ ...prev, recording: { status: 'saved', video: payload.video } }));
+ } else if (payload.status === 'failed') {
+ setState((prev) => ({ ...prev, recording: { status: 'failed', message: String(payload.message || 'The recording could not be saved.') } }));
}
- genChunks[payload.index] = payload.chunk;
- if (genChunks.filter(Boolean).length !== payload.count) return;
- recordingChunks.delete(gen);
- const blob = new Blob(genChunks.map((chunk) => decode(chunk)), { type: payload.mime || 'video/webm' });
- const url = URL.createObjectURL(blob);
- if (recordingUrl.current) URL.revokeObjectURL(recordingUrl.current);
- recordingUrl.current = url;
- if (!mounted.current) return;
- setState((prev) => ({ ...prev, recording: { blob, url, mime: payload.mime || 'video/webm' } }));
};
const onClosed = (payload) => {
if (!pending.current?.sessionId || payload?.sessionId !== pending.current.sessionId) return;
const failed = pending.current.hasError;
cancelPlayback();
pending.current = null;
- recordingChunks.clear();
stoppedStatus = failed ? 'error' : 'idle';
if (!mounted.current) return;
// Idle/capacity cleanup stays effective until the viewer explicitly
@@ -590,7 +580,6 @@ export default function useGeneratedPreview({ active, request, controls = null }
URL.revokeObjectURL(url);
frameUrls.current.delete(url);
}
- if (recordingUrl.current) URL.revokeObjectURL(recordingUrl.current);
}, []);
return { ...state, seek, command, resume, preparePlayback, startPlayback, syncPlayback, pausePlayback, imageRef, onFrameLoad, captureFrame };
diff --git a/server/services/generatedPreview/recordingSave.js b/server/services/generatedPreview/recordingSave.js
new file mode 100644
index 0000000000..d3dd9a6fd0
--- /dev/null
+++ b/server/services/generatedPreview/recordingSave.js
@@ -0,0 +1,80 @@
+/**
+ * Saves a real-time preview recording into Media History.
+ *
+ * The page's MediaRecorder WebM is read out of the preview browser in slices
+ * into a private temp file, re-encoded to H.264/AAC MP4 (faststart, even
+ * dimensions) so it plays and downloads everywhere, then committed through the
+ * gallery upload path, which writes the history entry and thumbnail. Without
+ * ffmpeg the WebM itself is saved.
+ */
+import { mkdtemp, open, rm } from 'node:fs/promises';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { ServerError } from '../../lib/errorHandler.js';
+import { AAC_ENCODE_ARGS, H264_ENCODE_ARGS, findFfmpeg, runFfmpegProcess } from '../../lib/ffmpeg.js';
+
+// The page already holds the whole blob in memory; this bounds the disk copy.
+const RECORDING_CAP = 1024 * 1024 * 1024;
+const SLICE_BYTES = 4 * 1024 * 1024;
+const MAX_TITLE = 190;
+
+const BLOB_INFO = `(() => {
+ const blob = window.__portosLastBlob;
+ return blob instanceof Blob ? { bytes: blob.size } : null;
+})()`;
+
+// FileReader encodes natively; a JS byte loop over megabytes stalls the page.
+const sliceScript = (start, end) => `new Promise((resolve, reject) => {
+ const blob = window.__portosLastBlob;
+ if (!(blob instanceof Blob)) { resolve(null); return; }
+ const reader = new FileReader();
+ reader.onload = () => resolve(String(reader.result).split(',')[1] || '');
+ reader.onerror = () => reject(reader.error || new Error('read failed'));
+ reader.readAsDataURL(blob.slice(${start}, ${end}));
+})`;
+
+const mp4Args = (input, output) => [
+ '-i', input, '-map', '0:v:0', '-map', '0:a:0?',
+ '-vf', 'scale=trunc(iw/2)*2:trunc(ih/2)*2',
+ ...H264_ENCODE_ARGS, ...AAC_ENCODE_ARGS,
+ '-movflags', '+faststart', '-y', output,
+];
+
+/**
+ * Returns the Media History entry, or null when the page holds no recording.
+ * `deps` replaces ffmpeg discovery, the encoder and the gallery save in tests.
+ */
+export async function saveRecordingToMediaHistory(page, { title = '', deps = {} } = {}) {
+ const info = await page.evaluate(BLOB_INFO);
+ if (!info) return null;
+ if (!info.bytes) throw new ServerError('The recording was empty.', { status: 422, code: 'RECORDING_EMPTY' });
+ if (info.bytes > RECORDING_CAP) throw new ServerError('The recording is too large to save.', { status: 413, code: 'RECORDING_TOO_LARGE' });
+ const {
+ ffmpeg = await findFfmpeg(),
+ run = runFfmpegProcess,
+ save = (await import('../videoUpload.js')).saveUploadedGalleryVideoFile,
+ } = deps;
+ const dir = await mkdtemp(join(tmpdir(), 'portos-recording-'));
+ try {
+ const webm = join(dir, 'recording.webm');
+ const file = await open(webm, 'w');
+ try {
+ for (let start = 0; start < info.bytes; start += SLICE_BYTES) {
+ const data = await page.evaluate(sliceScript(start, Math.min(info.bytes, start + SLICE_BYTES)));
+ if (data == null) throw new Error('the recording was released before it was saved');
+ await file.write(Buffer.from(data, 'base64'));
+ }
+ } finally {
+ await file.close();
+ }
+ await page.evaluate('window.__portosLastBlob = null').catch(() => {});
+ const name = String(title || '').trim().slice(0, MAX_TITLE) || 'Code animation recording';
+ if (!ffmpeg) return await save(webm, `${name}.webm`);
+ const mp4 = join(dir, 'recording.mp4');
+ const encoded = await run({ bin: ffmpeg, args: mp4Args(webm, mp4) });
+ if (!encoded.ok) throw new Error(`MP4 encode failed: ${encoded.reason}`);
+ return await save(mp4, `${name}.mp4`);
+ } finally {
+ await rm(dir, { recursive: true, force: true });
+ }
+}
diff --git a/server/services/generatedPreview/recordingSave.test.js b/server/services/generatedPreview/recordingSave.test.js
new file mode 100644
index 0000000000..5a9c178922
--- /dev/null
+++ b/server/services/generatedPreview/recordingSave.test.js
@@ -0,0 +1,54 @@
+import { describe, expect, it, vi } from 'vitest';
+import { readFile } from 'node:fs/promises';
+import { saveRecordingToMediaHistory } from './recordingSave.js';
+
+// A page whose recording blob is `bytes`, read back through the same slice
+// script the preview browser runs.
+const pageWith = (bytes) => {
+ const window = { __portosLastBlob: new Blob([bytes], { type: 'video/webm' }) };
+ class FileReader {
+ readAsDataURL(blob) {
+ blob.arrayBuffer().then((buffer) => {
+ this.result = `data:video/webm;base64,${Buffer.from(buffer).toString('base64')}`;
+ this.onload();
+ });
+ }
+ }
+ return { window, evaluate: async (source) => new Function('window', 'FileReader', 'Blob', `return ${source}`)(window, FileReader, Blob) };
+};
+
+// Uniquely catches a slice boundary dropping or duplicating bytes, and the
+// saved file not being the H.264 MP4 the viewer downloads.
+describe('saveRecordingToMediaHistory', () => {
+ it('copies every slice of the WebM, encodes an MP4, and saves it under the animation title', async () => {
+ const bytes = Buffer.alloc(4 * 1024 * 1024 + 123);
+ for (let i = 0; i < bytes.length; i += 1) bytes[i] = i % 251;
+ const page = pageWith(bytes);
+ let input = null;
+ const run = vi.fn(async ({ args }) => {
+ input = await readFile(args[args.indexOf('-i') + 1]);
+ return { ok: true };
+ });
+ const save = vi.fn(async (path, name) => ({ id: 'upload-1', filename: 'upload-1.mp4', path, name }));
+ const entry = await saveRecordingToMediaHistory(page, { title: 'Station boot', deps: { ffmpeg: '/bin/ffmpeg', run, save } });
+
+ expect(Buffer.compare(input, bytes)).toBe(0);
+ const { args } = run.mock.calls[0][0];
+ expect(args).toEqual(expect.arrayContaining(['libx264', 'aac', '+faststart']));
+ expect(args.at(-1)).toMatch(/recording\.mp4$/);
+ expect(save).toHaveBeenCalledWith(args.at(-1), 'Station boot.mp4');
+ expect(entry.id).toBe('upload-1');
+ expect(page.window.__portosLastBlob).toBeNull();
+ });
+
+ it('keeps the WebM without ffmpeg, and refuses an empty or missing recording', async () => {
+ const save = vi.fn(async () => ({ id: 'upload-2', filename: 'upload-2.webm' }));
+ await saveRecordingToMediaHistory(pageWith(Buffer.from('webm')), { deps: { ffmpeg: null, save } });
+ expect(save).toHaveBeenCalledWith(expect.stringMatching(/recording\.webm$/), 'Code animation recording.webm');
+
+ await expect(saveRecordingToMediaHistory(pageWith(Buffer.alloc(0)), { deps: { ffmpeg: null, save } }))
+ .rejects.toMatchObject({ code: 'RECORDING_EMPTY' });
+ const none = { evaluate: async () => null };
+ await expect(saveRecordingToMediaHistory(none, { deps: { ffmpeg: null, save } })).resolves.toBeNull();
+ });
+});
diff --git a/server/services/generatedPreview/sessions.js b/server/services/generatedPreview/sessions.js
index 04bd704a12..e672bccb8b 100644
--- a/server/services/generatedPreview/sessions.js
+++ b/server/services/generatedPreview/sessions.js
@@ -2,9 +2,10 @@
* Isolated generated-preview sessions (#11157).
*
* Music-video documents, code-rendered videos, and code animations execute in
- * the OS-jailed composition browser. The user's browser receives JPEG frames
- * and, when a page records, bounded WebM slices. It never receives executable
- * generated HTML or private media bytes.
+ * the OS-jailed composition browser. The user's browser receives JPEG frames;
+ * a recording the user asked for is saved to Media History as an MP4 and the
+ * browser receives its library URL. It never receives executable generated
+ * HTML or private media bytes.
*/
import { randomUUID } from 'node:crypto';
import { rm, writeFile } from 'node:fs/promises';
@@ -14,11 +15,10 @@ import { PATHS } from '../../lib/fileUtils.js';
import { PREVIEW_PAGE_CSP } from '../../lib/generatedPreviewSandbox.js';
import { createCooperativePlayback, playbackSchema } from './cooperativePlayback.js';
import { stageCodeAnimationPage, writeComposition as writeNamedComposition } from './codeAnimationPage.js';
+import { saveRecordingToMediaHistory } from './recordingSave.js';
const MAX_SESSIONS = 3;
const IDLE_MS = 120_000;
-const RECORDING_CAP = 8 * 1024 * 1024;
-const SLICE_BYTES = 256 * 1024;
const AUDIO_CAP = 12 * 1024 * 1024;
const MUSIC_URL = /^\/data\/music\/([^/]+)$/;
@@ -60,6 +60,7 @@ const commandSchema = z.object({
look: z.record(z.string(), z.unknown()).nullable().optional(),
card: z.record(z.string(), z.unknown()).nullable().optional(),
message: z.object({ type: z.string().min(1).max(80) }).strict().optional(),
+ title: z.string().max(200).optional(),
}).strict();
const closeSchema = z.object({
@@ -281,44 +282,35 @@ async function runSeek(session, generation, t, playing = false) {
await session.seekTail;
}
-async function pullRecording(session) {
- if (session.pulling || session.closed) return;
- session.pulling = true;
+// A recording is accepted only while a record command is outstanding, so a
+// page cannot fill the library by posting blobs on its own.
+async function saveRecording(session) {
+ const pending = session.recordPending;
+ if (!pending || session.saving) return;
+ session.saving = true;
+ session.recordPending = null;
+ const { requestId, id: sessionId, socket } = session;
+ socket.emit('generated-preview:recording', { requestId, sessionId, status: 'saving' });
try {
- const info = await session.page.evaluate(`(async () => {
- const blob = window.__portosLastBlob;
- if (!(blob instanceof Blob)) return null;
- if (blob.size > ${RECORDING_CAP}) return { error: 'too-large' };
- window.__portosRecordingBytes = new Uint8Array(await blob.arrayBuffer());
- return { bytes: blob.size, mime: blob.type || 'video/webm' };
- })()`);
- if (!info) return;
- if (info.error) {
- fail(session.socket, { requestId: session.requestId, sessionId: session.id, message: 'The recording is too large to return.' });
- return;
- }
- const count = Math.ceil(info.bytes / SLICE_BYTES) || 1;
- for (let index = 0; index < count; index += 1) {
- const start = index * SLICE_BYTES;
- const end = Math.min(info.bytes, start + SLICE_BYTES);
- const chunk = await session.page.evaluate(`(() => {
- const bytes = window.__portosRecordingBytes;
- let text = '';
- for (let i = ${start}; i < ${end}; i += 1) text += String.fromCharCode(bytes[i]);
- return btoa(text);
- })()`);
- if (session.closed) return;
- session.socket.emit('generated-preview:recording', {
- requestId: session.requestId, sessionId: session.id, generation: session.appliedGeneration,
- index, count, mime: info.mime, chunk,
- });
- }
- await session.page.evaluate('window.__portosRecordingBytes = null; window.__portosLastBlob = null;');
+ const entry = await saveRecordingToMediaHistory(session.page, { title: pending.title });
+ if (!entry) throw new Error('the page posted no recording');
+ socket.emit('generated-preview:recording', {
+ requestId, sessionId, status: 'saved',
+ video: { id: entry.id, filename: entry.filename, url: `/data/videos/${entry.filename}`, durationSec: entry.durationSec ?? null },
+ });
+ console.log(`🎞️ Preview recording saved to Media History: ${entry.filename}`);
} catch (error) {
- console.error(`❌ Generated preview recording failed: ${error.message}`);
- fail(session.socket, { requestId: session.requestId, sessionId: session.id, message: 'The recording could not be returned.' });
+ // Reported on the recording channel, not as a preview error: the preview
+ // itself is still running.
+ const refused = error?.code === 'RECORDING_TOO_LARGE' || error?.code === 'RECORDING_EMPTY';
+ if (refused) console.log(`🚫 Preview recording refused: ${error.message}`);
+ else console.error(`❌ Generated preview recording failed: ${error.message}`);
+ socket.emit('generated-preview:recording', {
+ requestId, sessionId, status: 'failed',
+ message: refused ? error.message : 'The recording could not be saved to Media History.',
+ });
} finally {
- session.pulling = false;
+ session.saving = false;
}
}
@@ -338,7 +330,7 @@ function attachEvents(session) {
session.socket.emit('generated-preview:event', {
requestId: session.requestId, sessionId: session.id, ...params,
});
- if (params.hasBlob) pullRecording(session).catch(() => {});
+ if (params.hasBlob) saveRecording(session).catch(() => {});
return;
}
if (message.method === 'Portos.pageError') {
@@ -620,6 +612,7 @@ async function commandGeneratedPreview(socket, data) {
else if (parsed.data.action === 'record' && parsed.data.message) {
// A real-time recording runs on the page's own clock.
if (session.kind === 'code-animation') await session.page.evaluate(RELEASE_CODE_ANIMATION_CLOCK);
+ session.recordPending = { title: parsed.data.title || '' };
await session.page.evaluate(`window.postMessage({ type: ${JSON.stringify(parsed.data.message.type)} }, '*')`);
}
};
diff --git a/server/services/generatedPreview/sessions.test.js b/server/services/generatedPreview/sessions.test.js
index d8548bc5d4..5a810fbee9 100644
--- a/server/services/generatedPreview/sessions.test.js
+++ b/server/services/generatedPreview/sessions.test.js
@@ -11,7 +11,9 @@ vi.mock('./codeAnimationPage.js', async (importOriginal) => ({
...await importOriginal(),
stageCodeAnimationPage: async () => ({ scratchRoot: null, directory: 'example-animation' }),
}));
-vi.mock('../htmlComposition/browser.js', () => ({ openComposition: async () => fixture.page }));
+vi.mock('../htmlComposition/browser.js', () => ({ openComposition: async (_directory, options) => { fixture.onEvent = options.onEvent; return fixture.page; } }));
+const saveRecording = vi.hoisted(() => vi.fn());
+vi.mock('./recordingSave.js', () => ({ saveRecordingToMediaHistory: saveRecording }));
import { AUTHORED_PREVIEW_PLAYBACK_TARGET } from '../../lib/generatedPreviewPlaybackSchema.js';
import { closeSession, registerGeneratedPreviewHandlers } from './sessions.js';
@@ -631,3 +633,40 @@ describe('code animation playhead', () => {
await vi.waitFor(() => expect(frames).toBeGreaterThan(after + 3));
});
});
+
+// Catches a page filling Media History with blobs nobody asked for, and the
+// saved video failing to reach the viewer that pressed Record.
+describe('code animation real-time recording', () => {
+ it('saves only a requested recording and hands the viewer its library video', async () => {
+ saveRecording.mockResolvedValue({ id: 'upload-1', filename: 'upload-1.mp4', durationSec: 4 });
+ const win = { requestAnimationFrame: (callback) => setTimeout(() => callback(performance.now()), 1), postMessage: vi.fn() };
+ fixture.page = {
+ evaluate: (source) => new Function('window', 'return ' + source)(win),
+ send: async () => ({ data: 'frame' }),
+ close: async () => {},
+ };
+ registerGeneratedPreviewHandlers(socket);
+ const requestId = '00000000-0000-4000-8000-000000000004';
+ socket.handlers.get('generated-preview:open')({ requestId, kind: 'code-animation', html: '<canvas></canvas>' });
+ await vi.waitFor(() => expect(socket.emit).toHaveBeenCalledWith('generated-preview:opened', expect.any(Object)));
+ const sessionId = socket.emit.mock.calls.find(([event]) => event === 'generated-preview:opened')[1].sessionId;
+ const recordings = () => socket.emit.mock.calls.filter(([event]) => event === 'generated-preview:recording').map(([, payload]) => payload);
+
+ // An unrequested blob is refused synchronously; an accepted one reaches the
+ // save before the handler's first await.
+ fixture.onEvent({ method: 'Portos.preview', params: { type: 'example:recorded', hasBlob: true } });
+ expect(saveRecording).not.toHaveBeenCalled();
+
+ await socket.handlers.get('generated-preview:command')({ requestId, sessionId, action: 'record', message: { type: 'example:record' }, title: 'Station boot' });
+ fixture.onEvent({ method: 'Portos.preview', params: { type: 'example:recorded', hasBlob: true } });
+ await vi.waitFor(() => expect(recordings().at(-1)).toMatchObject({ status: 'saved' }));
+ expect(saveRecording).toHaveBeenCalledWith(fixture.page, { title: 'Station boot' });
+ expect(recordings()).toEqual([
+ { requestId, sessionId, status: 'saving' },
+ { requestId, sessionId, status: 'saved', video: { id: 'upload-1', filename: 'upload-1.mp4', url: '/data/videos/upload-1.mp4', durationSec: 4 } },
+ ]);
+
+ fixture.onEvent({ method: 'Portos.preview', params: { type: 'example:recorded', hasBlob: true } });
+ expect(saveRecording).toHaveBeenCalledTimes(1);
+ });
+});