-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
recording.ts
218 lines (191 loc) · 5.63 KB
/
recording.ts
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { nextTick, Ref, ref, shallowRef, watch } from 'vue'
import { useEventListener, useDevicesList } from '@vueuse/core'
import { isTruthy } from '@antfu/utils'
import RecorderType from 'recordrtc'
import type { Options as RecorderOptions } from 'recordrtc'
import { currentCamera, currentMic } from '../state'
export const recordingName = ref('')
export const recordCamera = ref(true)
export function getFilename(media?: string) {
const d = new Date()
const pad = (v: number) => `${v}`.padStart(2, '0')
const date = `${pad(d.getMonth() + 1)}${pad(d.getDate())}-${pad(d.getHours())}${pad(d.getMinutes())}`
return `${[media, recordingName.value, date].filter(isTruthy).join('-')}.webm`
}
export const {
devices,
videoInputs: cameras,
audioInputs: microphones,
ensurePermissions: ensureDevicesListPermissions,
} = useDevicesList({
onUpdated() {
if (currentCamera.value !== 'none') {
if (!cameras.value.find(i => i.deviceId === currentCamera.value))
currentCamera.value = cameras.value[0]?.deviceId || 'default'
}
if (currentMic.value !== 'none') {
if (!microphones.value.find(i => i.deviceId === currentMic.value))
currentMic.value = microphones.value[0]?.deviceId || 'default'
}
},
})
export function download(name: string, url: string) {
const a = document.createElement('a')
a.setAttribute('href', url)
a.setAttribute('download', name)
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}
export function useRecording() {
const recording = ref(false)
const showAvatar = ref(false)
const recorderCamera: Ref<RecorderType | undefined> = shallowRef()
const recorderSlides: Ref<RecorderType | undefined> = shallowRef()
const streamCamera: Ref<MediaStream | undefined> = shallowRef()
const streamSlides: Ref<MediaStream | undefined> = shallowRef()
const config: RecorderOptions = {
type: 'video',
bitsPerSecond: 4 * 256 * 8 * 1024,
}
async function toggleAvatar() {
if (currentCamera.value === 'none')
return
if (showAvatar.value) {
showAvatar.value = false
if (!recording.value)
closeStream(streamCamera)
}
else {
await startCameraStream()
if (streamCamera.value)
showAvatar.value = !!streamCamera.value
}
}
async function startCameraStream() {
await ensureDevicesListPermissions()
await nextTick()
if (!streamCamera.value) {
if (currentCamera.value === 'none' && currentMic.value === 'none')
return
streamCamera.value = await navigator.mediaDevices.getUserMedia({
video: currentCamera.value === 'none' || recordCamera.value !== true
? false
: {
deviceId: currentCamera.value,
},
audio: currentMic.value === 'none'
? false
: {
deviceId: currentMic.value,
},
})
}
}
watch(currentCamera, async(v) => {
if (v === 'none') {
closeStream(streamCamera)
}
else {
if (recording.value)
return
// restart camera stream
if (streamCamera.value) {
closeStream(streamCamera)
await startCameraStream()
}
}
})
async function startRecording() {
await ensureDevicesListPermissions()
const { default: Recorder } = await import('recordrtc')
await startCameraStream()
streamSlides.value = await navigator.mediaDevices.getDisplayMedia({
video: {
// aspectRatio: 1.6,
frameRate: 15,
width: 3840,
height: 2160,
// @ts-expect-error
cursor: 'motion',
resizeMode: 'crop-and-scale',
},
})
if (streamCamera.value) {
const audioTrack = streamCamera.value!.getAudioTracks()?.[0]
if (audioTrack)
streamSlides.value!.addTrack(audioTrack)
recorderCamera.value = new Recorder(
streamCamera.value!,
config,
)
recorderCamera.value.startRecording()
}
recorderSlides.value = new Recorder(
streamSlides.value!,
config,
)
recorderSlides.value.startRecording()
recording.value = true
}
async function stopRecording() {
recording.value = false
recorderCamera.value?.stopRecording(() => {
if (recordCamera.value) {
const blob = recorderCamera.value!.getBlob()
const url = URL.createObjectURL(blob)
download(getFilename('camera'), url)
window.URL.revokeObjectURL(url)
}
recorderCamera.value = undefined
if (!showAvatar.value)
closeStream(streamCamera)
})
recorderSlides.value?.stopRecording(() => {
const blob = recorderSlides.value!.getBlob()
const url = URL.createObjectURL(blob)
download(getFilename('screen'), url)
window.URL.revokeObjectURL(url)
closeStream(streamSlides)
recorderSlides.value = undefined
})
}
function closeStream(stream: Ref<MediaStream | undefined>) {
const s = stream.value
if (!s)
return
s.getTracks().forEach((i) => {
i.stop()
s.removeTrack(i)
})
stream.value = undefined
}
function toggleRecording() {
if (recording.value)
stopRecording()
else
startRecording()
}
useEventListener('beforeunload', (event) => {
if (!recording.value)
return
// eslint-disable-next-line no-alert
if (confirm('Recording is not saved yet, do you want to leave?'))
return
event.preventDefault()
event.returnValue = ''
})
return {
recording,
showAvatar,
toggleRecording,
startRecording,
stopRecording,
toggleAvatar,
recorderCamera,
recorderSlides,
streamCamera,
streamSlides,
}
}
export const recorder = useRecording()