Open History to see the last commit
1import type { DesktopPreviewRecordingInput } from "@t3tools/contracts";
2
3import { readPreviewAnnotationTheme } from "./annotationTheme";
4
5interface RecordingDecorationOptions {
6 readonly showKeyPresses: boolean;
7 readonly showMousePresses: boolean;
8 readonly frameRate: number;
9}
10
11export async function createRecordingCompositor(
12 source: MediaStream,
13 options: RecordingDecorationOptions,
14 subscribe: (listener: (input: DesktopPreviewRecordingInput) => void) => () => void,
15) {
16 if (!options.showKeyPresses && !options.showMousePresses) return null;
17 const canvas = document.createElement("canvas");
18 const context = canvas.getContext("2d", { alpha: false });
19 if (!context) throw new Error("Recording canvas is unavailable.");
20 const video = document.createElement("video");
21 video.muted = true;
22 video.playsInline = true;
23 video.srcObject = source;
24 const settings = source.getVideoTracks()[0]?.getSettings();
25 canvas.width = settings?.width ?? 1920;
26 canvas.height = settings?.height ?? 1080;
27 const decorations = new RecordingDecorations(options, readPreviewAnnotationTheme().primary);
28 let disposed = false;
29 let frameId: number | undefined;
30 let timer: number | undefined;
31 const draw = () => {
32 if (disposed || video.readyState < 2) return;
33 const width = video.videoWidth || canvas.width;
34 const height = video.videoHeight || canvas.height;
35 if (canvas.width !== width) canvas.width = width;
36 if (canvas.height !== height) canvas.height = height;
37 context.drawImage(video, 0, 0, width, height);
38 const now = performance.now();
39 decorations.draw(context, width, height, now);
40 window.clearTimeout(timer);
41 const next = decorations.nextRedraw(now);
42 if (next !== null) timer = window.setTimeout(draw, next);
43 };
44 const frame = () => {
45 if (disposed) return;
46 draw();
47 frameId = video.requestVideoFrameCallback(frame);
48 };
49 const output = canvas.captureStream(options.frameRate);
50 let unsubscribe: (() => void) | undefined;
51 const dispose = () => {
52 if (disposed) return;
53 disposed = true;
54 unsubscribe?.();
55 window.clearTimeout(timer);
56 if (frameId !== undefined) video.cancelVideoFrameCallback(frameId);
57 video.pause();
58 video.srcObject = null;
59 for (const track of output.getTracks()) track.stop();
60 };
61 try {
62 unsubscribe = subscribe((input) => {
63 decorations.apply(input, performance.now());
64 draw();
65 });
66 frameId = video.requestVideoFrameCallback(frame);
67 await video.play();
68 draw();
69 return { stream: output, dispose };
70 } catch (error) {
71 dispose();
72 throw error;
73 }
74}
75
76export class RecordingDecorations {
77 private ring: {
78 x: number;
79 y: number;
80 width: number;
81 height: number;
82 held: boolean;
83 releasedAt: number | null;
84 } | null = null;
85 private key: { label: string; width: number; expiresAt: number | null } | null = null;
86
87 constructor(
88 private readonly options: RecordingDecorationOptions,
89 private readonly primaryColor: string,
90 ) {}
91
92 apply(input: DesktopPreviewRecordingInput, now: number) {
93 if (input.type === "clear") {
94 this.ring = null;
95 this.key = null;
96 } else if (input.type === "key" && this.options.showKeyPresses) {
97 this.key = input.label
98 ? { label: input.label, width: input.width, expiresAt: input.held ? null : now + 900 }
99 : null;
100 } else if (input.type === "pointer" && this.options.showMousePresses) {
101 if (input.phase === "down" || input.phase === "click") {
102 this.ring = {
103 x: input.x,
104 y: input.y,
105 width: input.width,
106 height: input.height,
107 held: input.phase === "down",
108 releasedAt: input.phase === "click" ? now : null,
109 };
110 } else if (this.ring?.held) {
111 this.ring = {
112 ...this.ring,
113 x: input.x,
114 y: input.y,
115 width: input.width,
116 height: input.height,
117 held: input.phase !== "up",
118 releasedAt: input.phase === "up" ? now : null,
119 };
120 }
121 }
122 }
123
124 nextRedraw(now: number): number | null {
125 if (
126 this.ring?.releasedAt !== null &&
127 this.ring?.releasedAt !== undefined &&
128 now < this.ring.releasedAt + 600
129 ) {
130 return 1000 / this.options.frameRate;
131 }
132 return this.key?.expiresAt !== null &&
133 this.key?.expiresAt !== undefined &&
134 now < this.key.expiresAt
135 ? this.key.expiresAt - now
136 : null;
137 }
138
139 draw(context: CanvasRenderingContext2D, width: number, height: number, now: number) {
140 // Guest coordinates are CSS pixels; native frames include zoom and display scale.
141 const scale = width / (this.key?.width ?? this.ring?.width ?? 1280);
142 const ring = this.ring;
143 if (ring && (ring.held || (ring.releasedAt !== null && now < ring.releasedAt + 600))) {
144 const progress = ring.releasedAt === null ? 0 : Math.min(1, (now - ring.releasedAt) / 600);
145 context.save();
146 const opacity = 0.9 * (1 - progress);
147 context.strokeStyle = this.primaryColor;
148 context.fillStyle = this.primaryColor;
149 context.lineWidth = 2 * scale;
150 context.beginPath();
151 context.ellipse(
152 (ring.x * width) / ring.width,
153 (ring.y * height) / ring.height,
154 ((20 * width) / ring.width) * (1 + progress * 0.5),
155 ((20 * height) / ring.height) * (1 + progress * 0.5),
156 0,
157 0,
158 Math.PI * 2,
159 );
160 context.globalAlpha = opacity * 0.15;
161 context.fill();
162 context.globalAlpha = opacity;
163 context.stroke();
164 context.restore();
165 }
166 const key = this.key;
167 if (key && (key.expiresAt === null || now < key.expiresAt)) {
168 context.save();
169 context.font = `500 ${26 * scale}px system-ui, sans-serif`;
170 const badgeWidth = Math.min(
171 width - 32 * scale,
172 context.measureText(key.label).width + 36 * scale,
173 );
174 const badgeHeight = 54 * scale;
175 const left = (width - badgeWidth) / 2;
176 const top = height - 24 * scale - badgeHeight;
177 context.fillStyle = "rgba(32,32,34,.86)";
178 context.beginPath();
179 context.roundRect(left, top, badgeWidth, badgeHeight, 14 * scale);
180 context.fill();
181 context.fillStyle = "white";
182 context.textAlign = "center";
183 context.textBaseline = "middle";
184 context.fillText(key.label, width / 2, top + badgeHeight / 2, badgeWidth - 24 * scale);
185 context.restore();
186 }
187 }
188}