Line data Source code
1 : import * as THREE from 'three'
2 :
3 : import { downloadBlob } from './downloadBlob'
4 :
5 : import type { EventManagerInterface } from './interfaces'
6 :
7 24 : export class RecordingManager {
8 : private mediaRecorder: MediaRecorder | null = null
9 : private recordedChunks: Blob[] = []
10 : private isRecording: boolean = false
11 : private recordingStream: MediaStream | null = null
12 : private recordingIndicator: THREE.Sprite | null = null
13 : private scene: THREE.Scene
14 : private sourceCanvas: HTMLCanvasElement
15 : private eventManager: EventManagerInterface
16 : private recordingStartTime: number = 0
17 : private recordingDuration: number = 0
18 : private recordingCanvas: HTMLCanvasElement | null = null
19 : private recordingContext: CanvasRenderingContext2D | null = null
20 : private animationFrameId: number | null = null
21 :
22 : constructor(
23 : scene: THREE.Scene,
24 : sourceCanvas: HTMLCanvasElement,
25 : eventManager: EventManagerInterface
26 : ) {
27 27 : this.scene = scene
28 27 : this.sourceCanvas = sourceCanvas
29 27 : this.eventManager = eventManager
30 27 : this.setupRecordingIndicator()
31 : }
32 :
33 : private setupRecordingIndicator(): void {
34 27 : const map = new THREE.TextureLoader().load(
35 : 'data:image/svg+xml;base64,' +
36 : btoa(`<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 64 64">
37 : <circle cx="32" cy="32" r="24" fill="#4CAF50" opacity="0.8" />
38 : <circle cx="32" cy="32" r="16" fill="#2E7D32" opacity="0.8" />
39 : </svg>`)
40 : )
41 27 : const material = new THREE.SpriteMaterial({
42 : map: map,
43 : transparent: true,
44 : depthTest: false,
45 : depthWrite: false
46 : })
47 27 : this.recordingIndicator = new THREE.Sprite(material)
48 27 : this.recordingIndicator.scale.set(0.5, 0.5, 0.5)
49 27 : this.recordingIndicator.position.set(-0.8, 0.8, 0)
50 27 : this.recordingIndicator.visible = false
51 :
52 27 : this.scene.add(this.recordingIndicator)
53 : }
54 :
55 : public async startRecording(
56 : targetWidth?: number,
57 : targetHeight?: number
58 : ): Promise<void> {
59 2 : if (this.isRecording) {
60 0 : return
61 : }
62 :
63 2 : try {
64 2 : const sourceCanvas = this.sourceCanvas
65 2 : const sourceWidth = sourceCanvas.width
66 2 : const sourceHeight = sourceCanvas.height
67 :
68 2 : const recordWidth = targetWidth || sourceWidth
69 2 : const recordHeight = targetHeight || sourceHeight
70 :
71 2 : this.recordingCanvas = document.createElement('canvas')
72 2 : this.recordingCanvas.width = recordWidth
73 2 : this.recordingCanvas.height = recordHeight
74 2 : this.recordingContext = this.recordingCanvas.getContext('2d', {
75 : alpha: false
76 : })
77 :
78 2 : if (!this.recordingContext) {
79 0 : throw new Error('Failed to get 2D context for recording canvas')
80 : }
81 :
82 2 : const sourceAspectRatio = sourceWidth / sourceHeight
83 2 : const targetAspectRatio = recordWidth / recordHeight
84 :
85 2 : let sx = 0,
86 2 : sy = 0,
87 2 : sw = sourceWidth,
88 2 : sh = sourceHeight
89 :
90 2 : if (Math.abs(sourceAspectRatio - targetAspectRatio) > 0.01) {
91 2 : if (sourceAspectRatio > targetAspectRatio) {
92 2 : sw = sourceHeight * targetAspectRatio
93 2 : sx = (sourceWidth - sw) / 2
94 : } else {
95 0 : sh = sourceWidth / targetAspectRatio
96 0 : sy = (sourceHeight - sh) / 2
97 : }
98 : }
99 :
100 2 : const captureFrame = () => {
101 81 : if (!this.isRecording || !this.recordingContext) {
102 0 : return
103 : }
104 :
105 81 : this.recordingContext.drawImage(
106 : sourceCanvas,
107 : sx,
108 : sy,
109 : sw,
110 : sh,
111 : 0,
112 : 0,
113 : recordWidth,
114 : recordHeight
115 : )
116 :
117 81 : this.animationFrameId = requestAnimationFrame(captureFrame)
118 : }
119 :
120 2 : this.recordingStream = this.recordingCanvas.captureStream(30)
121 :
122 2 : this.mediaRecorder = new MediaRecorder(this.recordingStream, {
123 : mimeType: 'video/webm;codecs=vp9',
124 : videoBitsPerSecond: 5000000
125 : })
126 :
127 2 : this.recordedChunks = []
128 :
129 2 : this.mediaRecorder.ondataavailable = (event) => {
130 11 : if (event.data.size > 0) {
131 10 : this.recordedChunks.push(event.data)
132 : }
133 : }
134 :
135 2 : this.mediaRecorder.onstop = () => {
136 2 : this.recordingIndicator!.visible = false
137 2 : this.isRecording = false
138 2 : this.recordingStream = null
139 :
140 2 : if (this.animationFrameId !== null) {
141 0 : cancelAnimationFrame(this.animationFrameId)
142 0 : this.animationFrameId = null
143 : }
144 :
145 2 : this.eventManager.emitEvent('recordingStopped', {
146 : duration: this.recordingDuration,
147 : hasRecording: this.recordedChunks.length > 0
148 : })
149 : }
150 :
151 2 : if (this.recordingIndicator) {
152 2 : this.recordingIndicator.visible = true
153 : }
154 :
155 2 : this.mediaRecorder.start(100)
156 2 : this.isRecording = true
157 2 : this.recordingStartTime = Date.now()
158 :
159 2 : captureFrame()
160 :
161 2 : this.eventManager.emitEvent('recordingStarted', null)
162 : } catch (error) {
163 0 : console.error('Error starting recording:', error)
164 0 : this.eventManager.emitEvent('recordingError', error)
165 : }
166 : }
167 :
168 : public stopRecording(): void {
169 5 : if (!this.isRecording || !this.mediaRecorder) {
170 3 : return
171 : }
172 :
173 2 : this.recordingDuration = (Date.now() - this.recordingStartTime) / 1000
174 :
175 2 : if (this.animationFrameId !== null) {
176 2 : cancelAnimationFrame(this.animationFrameId)
177 2 : this.animationFrameId = null
178 : }
179 :
180 2 : this.mediaRecorder.stop()
181 2 : if (this.recordingStream) {
182 2 : this.recordingStream.getTracks().forEach((track) => track.stop())
183 : }
184 :
185 2 : this.recordingCanvas = null
186 2 : this.recordingContext = null
187 : }
188 :
189 : public getIsRecording(): boolean {
190 456 : return this.isRecording
191 : }
192 :
193 : public hasRecording(): boolean {
194 0 : return this.recordedChunks.length > 0
195 : }
196 :
197 : public getRecordingDuration(): number {
198 5 : return this.recordingDuration
199 : }
200 :
201 : public getRecordingData(): string | null {
202 3 : if (this.recordedChunks.length !== 0) {
203 0 : const blob = new Blob(this.recordedChunks, { type: 'video/webm' })
204 :
205 0 : return URL.createObjectURL(blob)
206 : }
207 :
208 3 : return null
209 : }
210 :
211 : public exportRecording(filename: string = 'scene-recording.mp4'): void {
212 0 : if (this.recordedChunks.length === 0) {
213 0 : this.eventManager.emitEvent(
214 : 'recordingError',
215 : new Error('No recording available to export')
216 : )
217 0 : return
218 : }
219 :
220 0 : this.eventManager.emitEvent('exportingRecording', null)
221 :
222 0 : try {
223 : const blob = new Blob(this.recordedChunks, { type: 'video/webm' })
224 0 : downloadBlob(filename, blob)
225 :
226 0 : this.eventManager.emitEvent('recordingExported', null)
227 : } catch (error) {
228 0 : console.error('Error exporting recording:', error)
229 0 : this.eventManager.emitEvent('recordingError', error)
230 : }
231 : }
232 :
233 : public clearRecording(): void {
234 2 : this.recordedChunks = []
235 2 : this.recordingDuration = 0
236 2 : this.eventManager.emitEvent('recordingCleared', null)
237 : }
238 :
239 : public dispose(): void {
240 1 : if (this.animationFrameId !== null) {
241 0 : cancelAnimationFrame(this.animationFrameId)
242 0 : this.animationFrameId = null
243 : }
244 :
245 1 : this.stopRecording()
246 1 : this.clearRecording()
247 :
248 1 : this.recordingCanvas = null
249 1 : this.recordingContext = null
250 :
251 1 : if (this.recordingIndicator) {
252 1 : this.scene.remove(this.recordingIndicator)
253 1 : this.recordingIndicator.material.map?.dispose()
254 1 : this.recordingIndicator.material.dispose()
255 : }
256 : }
257 : }
|