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

chore: optimize framing algorithm #110

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 42 additions & 51 deletions vhs.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"context"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -126,6 +126,8 @@ const cleanupWaitTime = 100 * time.Millisecond
//
// It also begins the rendering process of the frames into videos.
func (vhs *VHS) Cleanup() {
vhs.PauseRecording()

// Give some time for any commands executed (such as `rm`) to finish.
//
// If a user runs a long running command, they must sleep for the required time
Expand Down Expand Up @@ -158,64 +160,53 @@ func (vhs *VHS) Cleanup() {
}
}

const quality = 0.92

// Record begins the goroutine which captures images from the xterm.js canvases.
func (vhs *VHS) Record(ctx context.Context) <-chan error {
ch := make(chan error)
func (vhs *VHS) Record() {
interval := time.Second / time.Duration(vhs.Options.Video.Framerate)
time.Sleep(interval)
save := vhs.frameSaver()
current := time.Now()

go func() {
counter := 0
for {
select {
case <-ctx.Done():
close(ch)
return

default:
if !vhs.recording {
time.Sleep(interval + interval)
continue
}

if vhs.Page != nil {
counter++
start := time.Now()
cursor, cursorErr := vhs.CursorCanvas.CanvasToImage("image/png", quality)
text, textErr := vhs.TextCanvas.CanvasToImage("image/png", quality)
if textErr == nil && cursorErr == nil {
if err := os.WriteFile(
filepath.Join(vhs.Options.Video.Input, fmt.Sprintf(cursorFrameFormat, counter)),
cursor,
os.ModePerm,
); err != nil {
ch <- fmt.Errorf("error writing cursor frame: %w", err)
}
if err := os.WriteFile(
filepath.Join(vhs.Options.Video.Input, fmt.Sprintf(textFrameFormat, counter)),
text,
os.ModePerm,
); err != nil {
ch <- fmt.Errorf("error writing text frame: %w", err)
}
} else {
ch <- fmt.Errorf("error: %v, %v", textErr, cursorErr)
}

elapsed := time.Since(start)
if elapsed >= interval {
continue
} else {
time.Sleep(interval - elapsed)
}
}
for vhs.recording {
if time.Since(current) >= interval {
current = time.Now()
go save()
}
time.Sleep(time.Millisecond)
}
}()
}

return ch
func (vhs *VHS) frameSaver() func() {
const quality = 0.92
counter := 0
lock := &sync.Mutex{}

return func() {
lock.Lock()
defer lock.Unlock()

cursor, cursorErr := vhs.CursorCanvas.CanvasToImage("image/png", quality)
text, textErr := vhs.TextCanvas.CanvasToImage("image/png", quality)
if textErr == nil && cursorErr == nil {
if err := os.WriteFile(
filepath.Join(vhs.Options.Video.Input, fmt.Sprintf(cursorFrameFormat, counter)),
cursor,
os.ModePerm,
); err != nil {
log.Printf("error writing cursor frame: %v", err)
}
if err := os.WriteFile(
filepath.Join(vhs.Options.Video.Input, fmt.Sprintf(textFrameFormat, counter)),
text,
os.ModePerm,
); err != nil {
log.Printf("error writing text frame: %v", err)
}
} else {
log.Printf("error: %v, %v", textErr, cursorErr)
}
}
}

// ResumeRecording indicates to VHS that the recording should be resumed.
Expand Down