Skip to content

Commit

Permalink
Save (and update) a backup of the video recording everytime new video…
Browse files Browse the repository at this point in the history
… data is available

With this, there are no more chances of the user losing a video recording because of power loss or cockpit termination.
  • Loading branch information
rafaellehmkuhl committed Nov 24, 2023
1 parent 4038e8d commit 56d1072
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/components/mini-widgets/MiniVideoRecorder.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { useMouseInElement, useTimestamp } from '@vueuse/core'
import { format, intervalToDuration } from 'date-fns'
import { saveAs } from 'file-saver'
import fixWebmDuration from 'fix-webm-duration'
import localforage from 'localforage'
import { storeToRefs } from 'pinia'
import Swal, { type SweetAlertResult } from 'sweetalert2'
import { computed, onBeforeMount, onBeforeUnmount, ref, toRefs, watch } from 'vue'
Expand Down Expand Up @@ -101,6 +102,14 @@ const isRecording = computed(() => {
return mediaRecorder.value !== undefined && mediaRecorder.value.state === 'recording'
})
const cockpitVideoDB = localforage.createInstance({
driver: localforage.INDEXEDDB,
name: 'CockpitVideoDB',
storeName: 'cockpit-video-db',
version: 1.0,
description: 'Local backups of Cockpit video recordings to be retrieved in case of failure.',
})
onBeforeMount(async () => {
// Set initial widget options if they don't exist
if (Object.keys(miniWidget.value.options).length === 0) {
Expand Down Expand Up @@ -187,12 +196,16 @@ const startRecording = async (): Promise<SweetAlertResult | void> => {
mediaRecorder.value = new MediaRecorder(mediaStream.value)
mediaRecorder.value.start(1000)
let chunks: Blob[] = []
mediaRecorder.value.ondataavailable = (e) => chunks.push(e.data)
mediaRecorder.value.ondataavailable = async (e) => {
chunks.push(e.data)
await cockpitVideoDB.setItem(fileName, chunks)
}
mediaRecorder.value.onstop = () => {
const blob = new Blob(chunks, { type: 'video/webm' })
fixWebmDuration(blob, Date.now() - timeRecordingStart.value.getTime()).then((fixedBlob) => {
saveAs(fixedBlob, fileName)
cockpitVideoDB.removeItem(fileName)
})
chunks = []
mediaRecorder.value = undefined
Expand Down

0 comments on commit 56d1072

Please sign in to comment.