summaryrefslogtreecommitdiff
path: root/app/client/live/player.js
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2018-05-21 21:18:35 +0200
committerJules Laplace <julescarbon@gmail.com>2018-05-21 21:18:35 +0200
commit4711e2988c91bdf0a70525ffe18040d1b8158b9a (patch)
treeb91f62bb63c9c04db1d1f32173539c6ce3bd9c4a /app/client/live/player.js
parentaf3bed7b5b1b01531e3bb34c56612c37717e06b7 (diff)
add saving to disk
Diffstat (limited to 'app/client/live/player.js')
-rw-r--r--app/client/live/player.js70
1 files changed, 70 insertions, 0 deletions
diff --git a/app/client/live/player.js b/app/client/live/player.js
new file mode 100644
index 0000000..b7088f5
--- /dev/null
+++ b/app/client/live/player.js
@@ -0,0 +1,70 @@
+import { store } from '../store'
+import Whammy from './whammy'
+
+let fps = 0, last_frame
+let recording = false, saving = false
+let videoWriter
+
+export function startRecording(){
+ videoWriter = new Whammy.Video(10)
+ recording = true
+ store.dispatch({
+ type: 'START_RECORDING',
+ })
+}
+
+export function stopRecording(){
+ recording = false
+ videoWriter.compile(false, function(blob){
+ console.log(blob)
+ store.dispatch({
+ type: 'SAVE_VIDEO',
+ blob: blob,
+ })
+ })
+}
+
+export function saveFrame(){
+ saving = true
+}
+
+export function onFrame (data) {
+ const blob = new Blob([data.frame], { type: 'image/jpg' })
+ const url = URL.createObjectURL(blob)
+ const img = new Image ()
+ img.onload = function() {
+ last_frame = data.meta
+ URL.revokeObjectURL(url)
+ const canvas = document.querySelector('.player canvas')
+ const ctx = canvas.getContext('2d')
+ ctx.drawImage(img, 0, 0, canvas.width, canvas.height)
+ if (recording) {
+ console.log('record frame')
+ videoWriter.add(canvas)
+ }
+ if (saving) {
+ saving = false
+ canvas.toBlob(function(blob) {
+ store.dispatch({
+ type: 'SAVE_FRAME',
+ blob: blob,
+ })
+ })
+ }
+ fps += 1
+ }
+ img.src = url
+}
+
+setInterval(() => {
+ store.dispatch({
+ type: 'SET_FPS',
+ fps: fps,
+ })
+ store.dispatch({
+ type: 'CURRENT_FRAME',
+ meta: last_frame,
+ })
+ fps = 0
+}, 1000)
+