Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backup video while recording and offer download on next Cockpit boot #573

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"gsap": "^3.11.3",
"haversine-distance": "^1.2.1",
"leaflet": "1.9.3",
"localforage": "^1.10.0",
"pinia": "^2.0.13",
"roboto-fontface": "*",
"sweetalert2": "^11.7.1",
Expand Down
19 changes: 16 additions & 3 deletions 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 @@ -183,16 +192,20 @@ const startRecording = async (): Promise<SweetAlertResult | void> => {
}

timeRecordingStart.value = new Date()
const fileName = `${missionName || 'Cockpit'} (${format(timeRecordingStart.value, 'LLL dd, yyyy - HH꞉mm꞉ss O')})`
mediaRecorder.value = new MediaRecorder(mediaStream.value)
mediaRecorder.value.start()
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) => {
const fileName = `${missionName || 'Cockpit'} (${format(timeRecordingStart.value, 'LLL dd, yyyy - HH꞉mm꞉ss O')})`
saveAs(fixedBlob, fileName)
cockpitVideoDB.removeItem(fileName)
})
chunks = []
mediaRecorder.value = undefined
Expand Down
42 changes: 42 additions & 0 deletions src/stores/video.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,52 @@
import { useStorage } from '@vueuse/core'
import { saveAs } from 'file-saver'
import localforage from 'localforage'
import { defineStore } from 'pinia'
import Swal from 'sweetalert2'
import { reactive } from 'vue'

export const useVideoStore = defineStore('video', () => {
const availableIceIps = reactive<string[]>([])
const allowedIceIps = useStorage<string[]>('cockpit-allowed-stream-ips', [])

// Offer download of backuped videos
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.',
})

cockpitVideoDB.length().then((len) => {
if (len === 0) return

Swal.fire({
title: 'Video recording recovery',
text: `Cockpit has pending backups for videos that you started recording but did not download.
Click 'Discard' to remove the backuped files.
Click 'Dismiss' to postpone this decision for the next boot.
Click 'Download' to download the files. If you decide to download them, they will be removed afterwards.
`,
icon: 'warning',
showDenyButton: true,
showCancelButton: true,
confirmButtonText: 'Download',
denyButtonText: 'Discard',
cancelButtonText: 'Dismiss',
}).then((decision) => {
if (decision.isDismissed) return
if (decision.isDenied) {
cockpitVideoDB.iterate((_, videoName) => cockpitVideoDB.removeItem(videoName))
} else if (decision.isConfirmed) {
cockpitVideoDB.iterate((videoFile, videoName) => {
const blob = (videoFile as Blob[]).reduce((a, b) => new Blob([a, b], { type: 'video/webm' }))
saveAs(blob, videoName)
})
cockpitVideoDB.iterate((_, videoName) => cockpitVideoDB.removeItem(videoName))
}
})
})

return { availableIceIps, allowedIceIps }
})