Fix keyframes store/load from db

This commit is contained in:
Zoe Roux 2024-08-07 23:58:00 +02:00
parent 286f9f7ea8
commit c3ef7bc0e1

View File

@ -2,14 +2,14 @@ package src
import ( import (
"bufio" "bufio"
"database/sql/driver"
"errors"
"fmt" "fmt"
"log" "log"
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"github.com/lib/pq"
) )
const KeyframeVersion = 1 const KeyframeVersion = 1
@ -64,19 +64,15 @@ func (kf *Keyframe) AddListener(callback func(keyframes []float64)) {
kf.info.listeners = append(kf.info.listeners, callback) kf.info.listeners = append(kf.info.listeners, callback)
} }
func (kf *Keyframe) Value() (driver.Value, error) {
return driver.Value(kf.Keyframes), nil
}
func (kf *Keyframe) Scan(src interface{}) error { func (kf *Keyframe) Scan(src interface{}) error {
switch src.(type) { var arr pq.Float64Array
case []float64: err := arr.Scan(src)
kf.Keyframes = src.([]float64) if err != nil {
kf.IsDone = true return err
kf.info = &KeyframeInfo{}
default:
return errors.New("incompatible type for keyframe in database")
} }
kf.Keyframes = arr
kf.IsDone = true
kf.info = &KeyframeInfo{}
return nil return nil
} }
@ -102,9 +98,9 @@ func (s *MetadataService) GetKeyframes(info *MediaInfo, isVideo bool, idx uint32
} }
kf.info.ready.Add(1) kf.info.ready.Add(1)
var err error
go func() { go func() {
var table string var table string
var err error
if isVideo { if isVideo {
table = "videos" table = "videos"
err = getVideoKeyframes(info.Path, idx, kf) err = getVideoKeyframes(info.Path, idx, kf)
@ -118,21 +114,20 @@ func (s *MetadataService) GetKeyframes(info *MediaInfo, isVideo bool, idx uint32
return return
} }
_, err = s.database.Exec( tx, _ := s.database.Begin()
fmt.Sprint( tx.Exec(
`update %s set keyframes = $3, ver_keyframes = $4 where sha = $1 and idx = $2`, fmt.Sprintf(`update %s set keyframes = $3 where sha = $1 and idx = $2`, table),
table,
),
info.Sha, info.Sha,
idx, idx,
kf.Keyframes, pq.Array(kf.Keyframes),
KeyframeVersion,
) )
tx.Exec(`update info set ver_keyframes = $2 where sha = $1`, info.Sha, KeyframeVersion)
err = tx.Commit()
if err != nil { if err != nil {
log.Printf("Couldn't store keyframes on database: %v", err) log.Printf("Couldn't store keyframes on database: %v", err)
} }
}() }()
return set(kf, err) return set(kf, nil)
} }
// Retrive video's keyframes and store them inside the kf var. // Retrive video's keyframes and store them inside the kf var.
@ -216,10 +211,10 @@ func getVideoKeyframes(path string, video_idx uint32, kf *Keyframe) error {
} }
} }
kf.add(ret) kf.add(ret)
kf.IsDone = true
if done == 0 { if done == 0 {
kf.info.ready.Done() kf.info.ready.Done()
} }
kf.IsDone = true
return nil return nil
} }
@ -232,7 +227,7 @@ func getAudioKeyframes(info *MediaInfo, audio_idx uint32, kf *Keyframe) error {
kf.Keyframes[segmentIndex] = float64(segmentIndex) * dummyKeyframeDuration kf.Keyframes[segmentIndex] = float64(segmentIndex) * dummyKeyframeDuration
} }
kf.info.ready.Done()
kf.IsDone = true kf.IsDone = true
kf.info.ready.Done()
return nil return nil
} }