switch libmpv binding

This commit is contained in:
Drew Weymouth
2023-04-05 17:35:26 -07:00
parent 7164fa15c6
commit 34f65e45ca
4 changed files with 21 additions and 213 deletions
+18 -18
View File
@@ -5,6 +5,8 @@ import (
"errors"
"fmt"
"strconv"
"github.com/dweymouth/go-mpv"
)
// Error returned by many Player functions if called before the player has not been initialized.
@@ -58,7 +60,7 @@ type ReplayGainOptions struct {
// Player encapsulates the mpv instance and provides functions
// to control it and to check its status.
type Player struct {
mpv libmpv
mpv *mpv.Mpv
initialized bool
vol int
replayGainOpts ReplayGainOptions
@@ -99,10 +101,8 @@ func NewWithClientName(c string) *Player {
// Most Player functions will return ErrUnitialized if called before Init.
func (p *Player) Init(maxCacheMB int) error {
if !p.initialized {
m, err := CreateMPV()
if err != nil {
return err
}
m := mpv.Create()
m.SetOptionString("idle", "yes")
m.SetOptionString("video", "no")
m.SetOptionString("audio-display", "no")
@@ -117,7 +117,7 @@ func (p *Player) Init(maxCacheMB int) error {
if p.vol < 0 {
p.vol = 100
}
m.SetOption("volume", MPVFormatInt64, p.vol)
m.SetOption("volume", mpv.FORMAT_INT64, p.vol)
p.SetAudioExclusive(p.audioExclusive)
if p.haveRGainOpts {
@@ -246,7 +246,7 @@ func (p *Player) SetVolume(vol int) error {
vol = 0
}
if p.initialized {
err := p.mpv.SetProperty("volume", MPVFormatInt64, vol)
err := p.mpv.SetProperty("volume", mpv.FORMAT_INT64, vol)
if err == nil {
p.vol = vol
}
@@ -266,7 +266,7 @@ func (p *Player) SetReplayGainOptions(options ReplayGainOptions) error {
if err := p.mpv.SetPropertyString("replaygain", string(options.Mode)); err != nil {
return err
}
if err := p.mpv.SetProperty("replaygain-preamp", MPVFormatDouble, options.PreampGain); err != nil {
if err := p.mpv.SetProperty("replaygain-preamp", mpv.FORMAT_DOUBLE, options.PreampGain); err != nil {
return err
}
clip := "no"
@@ -300,7 +300,7 @@ func (p *Player) GetVolume() int {
}
func (p *Player) setPaused(paused bool) error {
return p.mpv.SetProperty("pause", MPVFormatFlag, paused)
return p.mpv.SetProperty("pause", mpv.FORMAT_FLAG, paused)
}
// Start playback from the first track in the play queue.
@@ -362,8 +362,8 @@ func (p *Player) GetStatus() Status {
return p.status
}
pos, _ := p.mpv.GetProperty("playback-time", MPVFormatDouble)
dur, _ := p.mpv.GetProperty("duration", MPVFormatDouble)
pos, _ := p.mpv.GetProperty("playback-time", mpv.FORMAT_DOUBLE)
dur, _ := p.mpv.GetProperty("duration", mpv.FORMAT_DOUBLE)
if pos != nil {
p.status.TimePos = pos.(float64)
}
@@ -377,7 +377,7 @@ func (p *Player) GetStatus() Status {
}
func (p *Player) getInt64Property(propName string) (int64, error) {
playpos, err := p.mpv.GetProperty(propName, MPVFormatInt64)
playpos, err := p.mpv.GetProperty(propName, mpv.FORMAT_INT64)
if err != nil {
return -1, err
}
@@ -463,19 +463,19 @@ func (p *Player) eventHandler(ctx context.Context) {
return
default:
e := p.mpv.WaitEvent(1 /*timeout seconds*/)
if e.ID != MPVEventNone {
if e.Event_Id != mpv.EVENT_NONE {
//log.Printf("mpv event: %+v\n", e)
}
switch e.ID {
case MPVEventPlaybackRestart:
switch e.Event_Id {
case mpv.EVENT_PLAYBACK_RESTART:
if p.seeking {
p.seeking = false
}
case MPVEventSeek:
case mpv.EVENT_SEEK:
for _, cb := range p.onSeek {
cb()
}
case MPVEventFileLoaded:
case mpv.EVENT_FILE_LOADED:
if p.status.State == Paused {
// seek while paused switches to a new file
// mpv does not fire seek event in this case
@@ -489,7 +489,7 @@ func (p *Player) eventHandler(ctx context.Context) {
cb(pos)
}
}
case MPVEventIdle:
case mpv.EVENT_IDLE:
p.status.Duration = 0
p.status.TimePos = 0
p.setState(Stopped)