Skip to content

Commit

Permalink
widgets[video-recorder]: Add screen recording
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaellehmkuhl authored and patrickelectric committed Mar 23, 2023
1 parent d8c1e81 commit 41b3cbb
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/components/widgets/VideoRecorder.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
hide-details
return-object
color="white"
@update:model-value="updateCurrentStream"
/>
</div>
</template>
Expand Down Expand Up @@ -57,15 +58,30 @@ const isRecording = computed(() => {
return mediaRecorder.value !== undefined && mediaRecorder.value.state === 'recording'
})
onBeforeMount(() => {
onBeforeMount(async () => {
// Set initial widget options if they don't exist
if (Object.keys(widget.value.options).length === 0) {
widget.value.options = {
streamName: undefined as string | undefined,
}
}
addScreenStream()
})
const addScreenStream = (): void => {
const screenStream = {
id: 'screenStream',
name: 'Entire screen',
encode: null,
height: null,
width: null,
interval: null,
source: null,
created: null,
}
availableStreams.value.push(screenStream)
}
onBeforeUnmount(() => {
webRTCManager.close('WebRTC manager removed')
})
Expand All @@ -88,10 +104,27 @@ const stopRecording = (): void => {
if (mediaRecorder.value === undefined || !isRecording.value) return
mediaRecorder.value.stop()
mediaRecorder.value = undefined
if (mediaStream.value !== undefined) {
mediaStream.value.getTracks().forEach((track: MediaStreamTrack) => track.stop())
}
}
const updateCurrentStream = async (): Promise<void> => {
if (selectedStream.value?.id === 'screenStream') {
try {
// @ts-ignore: preferCurrentTab option is currently available in most browsers, including chromium-based ones
mediaStream.value = await navigator.mediaDevices.getDisplayMedia({ preferCurrentTab: true })
} catch (err) {
console.error(err)
}
}
}
watch(availableStreams, () => {
const savedStreamName: string | undefined = widget.value.options.streamName
if (!availableStreams.value.find((stream) => stream.id === 'screenStream')) {
addScreenStream()
}
if (availableStreams.value.isEmpty()) {
return
}
Expand Down

0 comments on commit 41b3cbb

Please sign in to comment.