more shit

This commit is contained in:
eyjhb 2025-02-04 20:20:55 +01:00
parent 6ae627522c
commit dae22a9716
Signed by: eyjhb
GPG key ID: 609F508E3239F920
3 changed files with 120 additions and 124 deletions

View file

@ -12,13 +12,15 @@ import (
)
type MPVPLayer struct {
entries []*feedEntry
entries []*feedEntry
finishedEntries []*feedEntry
c *mpvipc.Connection
f *os.File
VideoPosition time.Duration
VideoDuration time.Duration
videoPosition time.Duration
videoDuration time.Duration
isPlaying bool
sync.RWMutex
}
@ -32,52 +34,103 @@ func (mpv *MPVPLayer) Exit() error {
}
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 mpv.play(e.Link)
}
return nil
}
func (mpv *MPVPLayer) Play(url string) error {
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 {
if err := mpv.ensurePlayer(); err != nil {
return err
}
t, err := mpv.c.Call("loadfile", url)
fmt.Println(t)
_, err := mpv.c.Call("loadfile", url)
if err != nil {
return err
}
mpv.isPlaying = true
return err
}
func (mpv *MPVPLayer) Stop() error {
mpv.Lock()
defer mpv.Unlock()
if mpv.c == nil {
return nil
}
err := mpv.c.Set("pause", true)
return err
}
func (mpv *MPVPLayer) IsPlaying() (bool, error) {
if mpv.c == nil || mpv.c.IsClosed() {
return false, nil
}
func (mpv *MPVPLayer) VideoDuration() time.Duration {
mpv.RLock()
defer mpv.RUnlock()
path, err := mpv.c.Get("path")
if err != nil {
return false, err
}
return path != "", nil
return mpv.videoDuration
}
func (mpv *MPVPLayer) finishedVideo() (bool, error) {
func (mpv *MPVPLayer) VideoPosition() time.Duration {
mpv.RLock()
defer mpv.RUnlock()
return mpv.videoPosition
}
func (mpv *MPVPLayer) IsPlaying() bool {
mpv.Lock()
defer mpv.Unlock()
mpv.entries = mpv.entries[1:]
if mpv.c == nil || mpv.c.IsClosed() {
if mpv.isPlaying == true {
mpv.finishedVideo()
}
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 err := mpv.Play(mpv.entries[0].Link); err != nil {
if err := mpv.play(mpv.entries[0].Link); err != nil {
return false, err
}
}
@ -114,7 +167,6 @@ func (mpv *MPVPLayer) ensurePlayer() error {
"mpv",
"--keep-open=yes",
"--idle",
// fmt.Sprintf("--input-ipc-server=%d", mpv.f.Fd()),
fmt.Sprintf("--input-ipc-server=%s", mpv.f.Name()),
)
@ -144,22 +196,27 @@ func (mpv *MPVPLayer) ensurePlayer() error {
go func() {
for event := range events {
fmt.Println("event", event)
mpv.Lock()
if event.ID == 1 {
if event.Data != nil {
mpv.VideoDuration = time.Duration(event.Data.(float64)) * time.Second
mpv.Lock()
mpv.videoDuration = time.Duration(event.Data.(float64)) * time.Second
mpv.Unlock()
}
} else if event.ID == 2 {
if event.Data != nil {
mpv.VideoPosition = time.Duration(event.Data.(float64)) * time.Second
mpv.Lock()
mpv.videoPosition = time.Duration(event.Data.(float64)) * time.Second
mpv.Unlock()
}
} else if event.ID == 3 {
if event.Data != nil {
mpv.finishedVideo()
if event.Data.(bool) == true {
mpv.Lock()
mpv.finishedVideo()
mpv.Unlock()
}
}
}
mpv.Unlock()
}
}()