-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathindex.tsx
More file actions
131 lines (114 loc) · 3.62 KB
/
Copy pathindex.tsx
File metadata and controls
131 lines (114 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { useEffect, useRef, useState } from 'react';
import { Video } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import { MenuTooltip } from '../menu-tooltip';
import { camera } from '@/libs/media/camera';
export const Recorder = () => {
const { t } = useTranslation();
const [isRecording, setIsRecording] = useState(false);
const [elapsedMs, setElapsedMs] = useState(0);
const mediaRecorderRef = useRef<MediaRecorder>();
const fileWritableRef = useRef<FileSystemWritableFileStream | null>(null);
const timerRef = useRef<number | null>(null);
const startTimeRef = useRef<number>(0);
const stopTimer = () => {
if (timerRef.current !== null) {
window.clearInterval(timerRef.current);
timerRef.current = null;
}
};
const startTimer = () => {
stopTimer();
startTimeRef.current = Date.now();
setElapsedMs(0);
timerRef.current = window.setInterval(() => {
setElapsedMs(Date.now() - startTimeRef.current);
}, 1000);
};
useEffect(() => {
return () => {
stopTimer();
};
}, []);
const formatElapsed = (ms: number) => {
const totalSeconds = Math.floor(ms / 1000);
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
return `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
};
const handleStartRecording = async () => {
const stream = camera.getStream();
if (!stream) {
return;
}
try {
const handle = await (window as any).showSaveFilePicker({
suggestedName: `recording-${new Date().toISOString().slice(0, 19).replace(/:/g, '-')}.webm`,
types: [
{
description: 'Sipeed NanoKVM-USB Recorder',
accept: { 'video/webm': ['.webm'] }
}
]
});
fileWritableRef.current = await handle.createWritable();
const recorder = new MediaRecorder(stream, {
mimeType: 'video/webm'
});
recorder.ondataavailable = async (event) => {
if (event.data && event.data.size > 0) {
if (fileWritableRef.current) {
await fileWritableRef.current.write(event.data);
} else {
recorder.stop();
}
}
};
recorder.onstop = async () => {
if (fileWritableRef.current) {
await fileWritableRef.current.close();
fileWritableRef.current = null;
}
stopTimer();
setElapsedMs(0);
setIsRecording(false);
};
recorder.start(1000);
mediaRecorderRef.current = recorder;
setIsRecording(true);
startTimer();
} catch (err) {
console.error(err);
}
};
const handleStopRecording = () => {
const recorder = mediaRecorderRef.current;
if (recorder && recorder.state !== 'inactive') {
recorder.stop();
stopTimer();
setElapsedMs(0);
setIsRecording(false);
}
};
if (isRecording) {
return (
<div
className="flex h-[28px] min-w-[28px] cursor-pointer items-center justify-center space-x-1 rounded px-1 text-white hover:bg-neutral-700/70"
onClick={handleStopRecording}
>
<Video className="animate-pulse text-red-400" size={18} />
<span className="text-xs text-red-300">{formatElapsed(elapsedMs)}</span>
</div>
);
}
return (
<MenuTooltip title={t('menu.recorder', 'Record')}>
<div
className="flex h-[28px] w-[28px] cursor-pointer items-center justify-center rounded text-neutral-300 hover:bg-neutral-700/70 hover:text-white"
onClick={handleStartRecording}
>
<Video size={18} />
</div>
</MenuTooltip>
);
};