fixed resizing not working

This commit is contained in:
eyjhb 2025-02-08 21:13:09 +01:00
parent 2e3e74e128
commit cb1839a5bc
Signed by: eyjhb
GPG key ID: 609F508E3239F920
4 changed files with 107 additions and 102 deletions

View file

@ -1,7 +1,6 @@
package cmd
import (
"context"
"fmt"
"os"
"os/exec"
@ -11,16 +10,17 @@ import (
"github.com/dexterlb/mpvipc"
)
type MPVPLayer struct {
entries []*feedEntry
finishedEntries []*feedEntry
type VideoID string
type MPVPLayer struct {
c *mpvipc.Connection
f *os.File
videoID VideoID
videoPosition time.Duration
videoDuration time.Duration
isPlaying bool
finishedVideo string
sync.RWMutex
}
@ -33,54 +33,27 @@ func (mpv *MPVPLayer) Exit() error {
return nil
}
func (mpv *MPVPLayer) Queue(e feedEntry) error {
mpv.Lock()
defer mpv.Unlock()
// TODO(eyJhb): implement checking
mpv.entries = append(mpv.entries, &e)
if len(mpv.entries) == 1 {
return mpv.play(e.Link)
}
return nil
}
func (mpv *MPVPLayer) FinishedPlaying() []*feedEntry {
mpv.Lock()
defer mpv.Unlock()
f := mpv.finishedEntries
mpv.finishedEntries = nil
return f
}
func (mpv *MPVPLayer) CurrentlyPlaying() *feedEntry {
mpv.Lock()
defer mpv.Unlock()
if !mpv.isPlaying {
return nil
}
return mpv.entries[0]
}
func (mpv *MPVPLayer) play(url string) error {
func (mpv *MPVPLayer) Play(id VideoID, url string) error {
if err := mpv.ensurePlayer(); err != nil {
return err
}
mpv.Lock()
defer mpv.Unlock()
_, err := mpv.c.Call("loadfile", url)
if err != nil {
return err
}
mpv.videoID = id
mpv.isPlaying = true
return err
if err := mpv.c.Set("pause", false); err != nil {
return err
}
return nil
}
func (mpv *MPVPLayer) Stop() error {
@ -112,61 +85,43 @@ func (mpv *MPVPLayer) IsPlaying() bool {
mpv.Lock()
defer mpv.Unlock()
if mpv.c == nil || mpv.c.IsClosed() {
if mpv.isPlaying == true {
mpv.finishedVideo()
}
if mpv.c != nil && mpv.c.IsClosed() {
return false
}
return mpv.isPlaying
}
func (mpv *MPVPLayer) finishedVideo() (bool, error) {
if len(mpv.entries) > 0 {
mpv.finishedEntries = append(mpv.finishedEntries, mpv.entries[0])
mpv.entries = mpv.entries[1:]
}
mpv.isPlaying = false
if len(mpv.entries) == 1 {
if videoPercentageWatched(mpv.VideoPosition(), mpv.VideoDuration()) > 90 {
if err := mpv.play(mpv.entries[0].Link); err != nil {
return false, err
}
}
}
return false, nil
}
// TODO: this fucking sucks, a lot, what the fuck
func (mpv *MPVPLayer) ensurePlayer() error {
mpv.Lock()
defer mpv.Unlock()
if mpv.c != nil && !mpv.c.IsClosed() {
return nil
}
// TODO: cleanup this file
var err error
mpv.f, err = os.CreateTemp("", "example")
mpv.f, err = os.CreateTemp("", "mpvminiflux")
if err != nil {
return err
}
// close and delete file
defer func() {
if mpv.f == nil {
return
}
// defer func() {
// if mpv.f == nil {
// return
// }
filename := mpv.f.Name()
mpv.f.Close()
os.Remove(filename)
}()
// filename := mpv.f.Name()
// mpv.f.Close()
// os.Remove(filename)
// }()
// defer mpv.finishedVideo()
ctx := context.TODO()
cmd := exec.CommandContext(ctx,
// ctx := context.TODO()
cmd := exec.Command(
"mpv",
"--keep-open=yes",
"--idle",
@ -197,33 +152,46 @@ func (mpv *MPVPLayer) ensurePlayer() error {
return err
}
// TODO: this will never close any old ones, so it will keep spawning new ones
go func() {
for event := range events {
if event.ID == 1 {
if event.Data != nil {
mpv.Lock()
mpv.videoDuration = time.Duration(event.Data.(float64)) * time.Second
mpv.Unlock()
}
} else if event.ID == 2 {
if event.Data != nil {
mpv.Lock()
mpv.videoPosition = time.Duration(event.Data.(float64)) * time.Second
mpv.Unlock()
}
} else if event.ID == 3 {
// TODO: this might not fire once the media ends
if event.Data != nil {
if event.Data.(bool) == true {
data := event.Data.(float64)
if data != 0 {
mpv.Lock()
mpv.finishedVideo()
mpv.videoDuration = time.Duration(data) * time.Second
mpv.Unlock()
}
}
} else if event.ID == 2 {
if event.Data != nil {
data := event.Data.(float64)
if data != 0 {
mpv.Lock()
mpv.videoPosition = time.Duration(data) * time.Second
mpv.Unlock()
}
}
} else if event.ID == 3 {
if event.Data != nil && event.Data.(bool) == true {
mpv.Lock()
mpv.isPlaying = false
mpv.Unlock()
}
// TODO: this might not fire once the media ends
// if event.Data != nil {
// if event.Data.(bool) == true {
// mpv.Lock()
// mpv.finishedVideo()
// mpv.Unlock()
// }
// }
}
}
}()
return nil
// return cmd.Wait()
}