16 Commits

Author SHA1 Message Date
6fc6cec502 added LoopState datatype for more precise control 2025-08-30 15:23:33 +02:00
95d2a1e1a4 fixed SetLoopFile 2025-08-30 14:42:14 +02:00
0a02d8ffc5 SetLoopSong and SetLoopPlaylist added 2025-08-30 14:37:15 +02:00
e5f6becf97 removed GetPause double 2025-08-30 14:18:39 +02:00
86c9dc0cc5 added GetLoopFile and GetLoopPlaylist 2025-08-30 14:16:13 +02:00
e425b08ef8 added ShufflePlaylist 2025-06-28 15:33:45 +02:00
f413f97824 added GetPlaylistPosition 2025-06-28 14:18:56 +02:00
5eb15eb7a5 GetPause implemented 2025-06-28 14:14:55 +02:00
3e68bbda29 added SetPlaylistPosition 2025-06-28 13:47:16 +02:00
bca0e0bf3a added GetPlaylist() 2025-06-28 13:36:09 +02:00
4fef37ceca introduced Ping() function 2025-06-28 13:17:11 +02:00
0e44626c13 check instantly if mpv is ready 2025-06-28 13:10:43 +02:00
195b00abf2 fixed dependencies 2025-06-25 23:23:31 +02:00
cb478e8b4d Update go.mod 2024-12-16 19:47:28 +01:00
0e93c0cafb added GetDemuxerCacheTime 2024-02-22 19:48:21 +01:00
ddb1e3af17 added volume getter and setter 2024-01-13 17:13:14 +01:00
9 changed files with 167 additions and 23 deletions

View File

@ -2,7 +2,6 @@ package mpvipc
import (
"context"
"net"
"time"
)
@ -24,27 +23,41 @@ func LoadFile(socket string, file string, flags LoadFileFlag) error {
return err
}
func ShufflePlaylist(socket string) error {
cmd := &Command{Command: []interface{}{"playlist-shuffle"}}
_, err := SendCommand[any](socket, cmd)
return err
}
func Quit(socket string) error {
cmd := &Command{Command: []interface{}{"quit"}}
_, err := SendCommand[any](socket, cmd)
return err
}
func Ping(socket string) bool {
_, err := GetPID(socket)
return err == nil
}
func IsReady(ctx context.Context, socket string) <-chan bool {
out := make(chan bool, 1)
go func() {
defer close(out)
if Ping(socket) {
out <- true
return
}
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
conn, err := net.Dial("unix", socket)
if err == nil {
defer conn.Close()
if Ping(socket) {
out <- true
return
}

8
go.mod
View File

@ -1,5 +1,7 @@
module git.milar.in/milarin/mpvipc
module git.tordarus.net/tordarus/mpvipc
go 1.21.5
go 1.23
require git.milar.in/milarin/channel v0.1.1
toolchain go1.24.4
require git.tordarus.net/tordarus/channel v0.1.19

4
go.sum
View File

@ -1,2 +1,2 @@
git.milar.in/milarin/channel v0.1.1 h1:s8+BdiOMmuRUDmChQ2i4G5GWsDCK9tKNHt1knLJx9zM=
git.milar.in/milarin/channel v0.1.1/go.mod h1:We83LTI8S7u7II3pD+A2ChCDWJfCkcBUCUqii9HjTtM=
git.tordarus.net/tordarus/channel v0.1.19 h1:d9xnSwFyvBh4B1/82mt0A7Gpm2nIZJTc+9ceJMIOu5Q=
git.tordarus.net/tordarus/channel v0.1.19/go.mod h1:8/dWFTdGO7g4AeSZ7cF6GerkGbe9c4dBVMVDBxOd9m4=

66
loopstate.go Normal file
View File

@ -0,0 +1,66 @@
package mpvipc
import (
"fmt"
"reflect"
"strconv"
"strings"
)
type LoopState int
const (
LoopInfinite LoopState = -2
LoopForced LoopState = -1
LoopInvalid LoopState = 0
LoopNo LoopState = 1
)
func Loop(amount int) LoopState {
if amount < int(LoopInfinite) {
amount = int(LoopInfinite)
} else if amount == 0 {
amount = int(LoopNo)
}
return LoopState(amount)
}
func ParseLoopState(value any) (LoopState, error) {
switch v := value.(type) {
case string:
switch strings.ToLower(v) {
case "no":
return LoopNo, nil
case "inf":
return LoopInfinite, nil
case "force":
return LoopForced, nil
}
case int:
return Loop(v), nil
case float64:
return Loop(int(v)), nil
case bool:
if v {
return LoopInfinite, nil
} else {
return LoopNo, nil
}
}
return LoopInvalid, fmt.Errorf("could not parse value as valid loop state: %#v (type: %#v)", value, reflect.TypeOf(value).Name())
}
func (s LoopState) String() string {
switch s {
case LoopInfinite:
return "inf"
case LoopNo:
return "no"
case LoopForced:
return "force"
default:
return strconv.Itoa(int(s))
}
}

8
playlist_entry.go Normal file
View File

@ -0,0 +1,8 @@
package mpvipc
type PlaylistEntry struct {
Filename string `json:"filename"`
Current bool `json:"current"`
Playing bool `json:"playing"`
ID int `json:"id"`
}

View File

@ -9,14 +9,18 @@ func GetDisplayNames(socket string) ([]string, error) {
return GetProperty[[]string](socket, "display-names")
}
func IsFullscreen(socket string) (bool, error) {
func GetFullscreen(socket string) (bool, error) {
return GetProperty[bool](socket, "fullscreen")
}
func IsPaused(socket string) (bool, error) {
func GetPaused(socket string) (bool, error) {
return GetProperty[bool](socket, "pause")
}
func GetVolume(socket string) (float64, error) {
return GetProperty[float64](socket, "volume")
}
func GetFilename(socket string) (string, error) {
return GetProperty[string](socket, "filename")
}
@ -45,6 +49,14 @@ func GetFileFormat(socket string) (string, error) {
return GetProperty[string](socket, "file-format")
}
func GetPlaylist(socket string) ([]PlaylistEntry, error) {
return GetProperty[[]PlaylistEntry](socket, "playlist")
}
func GetPlaylistPosition(socket string) (int, error) {
return GetProperty[int](socket, "playlist-pos")
}
func GetDuration(socket string) (time.Duration, error) {
durationInSeconds, err := GetProperty[float64](socket, "duration")
if err != nil {
@ -54,6 +66,15 @@ func GetDuration(socket string) (time.Duration, error) {
return time.Duration(durationInSeconds * float64(time.Second)), nil
}
func GetDemuxerCacheTime(socket string) (time.Duration, error) {
cacheInSeconds, err := GetProperty[float64](socket, "demuxer-cache-time")
if err != nil {
return 0, err
}
return time.Duration(cacheInSeconds * float64(time.Second)), nil
}
func GetPercentPos(socket string) (float64, error) {
return GetProperty[float64](socket, "percent-pos")
}
@ -76,7 +97,7 @@ func GetTimeRemaining(socket string) (time.Duration, error) {
return time.Duration(durationInSeconds * float64(time.Second)), nil
}
func IsSeeking(socket string) (bool, error) {
func GetSeeking(socket string) (bool, error) {
return GetProperty[bool](socket, "seeking")
}
@ -120,18 +141,36 @@ func GetChapterList(socket string) ([]Chapter, error) {
return GetProperty[[]Chapter](socket, "chapter-list")
}
func IsSeekable(socket string) (bool, error) {
func GetSeekable(socket string) (bool, error) {
return GetProperty[bool](socket, "seekable")
}
func IsPartiallySeekable(socket string) (bool, error) {
func GetPartiallySeekable(socket string) (bool, error) {
return GetProperty[bool](socket, "partially-seekable")
}
func IsPlaybackAborted(socket string) (bool, error) {
func GetPlaybackAborted(socket string) (bool, error) {
return GetProperty[bool](socket, "playback-abort")
}
func GetPropertyList(socket string) ([]string, error) {
return GetProperty[[]string](socket, "property-list")
}
func GetLoopFile(socket string) (LoopState, error) {
value, err := GetProperty[any](socket, "loop-file")
if err != nil {
return LoopInvalid, err
}
return ParseLoopState(value)
}
func GetLoopPlaylist(socket string) (LoopState, error) {
value, err := GetProperty[any](socket, "loop-playlist")
if err != nil {
return LoopInvalid, err
}
return ParseLoopState(value)
}

View File

@ -4,7 +4,7 @@ import (
"context"
"time"
"git.milar.in/milarin/channel"
"git.tordarus.net/tordarus/channel"
)
func ObserveEndOfFile(ctx context.Context, socket string) (<-chan string, error) {

View File

@ -3,17 +3,33 @@ package mpvipc
import "time"
func SetFullscreen(socket string, fullscreen bool) error {
return SetProperty[bool](socket, "fullscreen", fullscreen)
return SetProperty(socket, "fullscreen", fullscreen)
}
func SetPause(socket string, pause bool) error {
return SetProperty[bool](socket, "pause", pause)
return SetProperty(socket, "pause", pause)
}
func SetTimePos(socket string, timePos time.Duration) error {
return SetProperty[float64](socket, "time-pos", float64(timePos)/float64(time.Second))
return SetProperty(socket, "time-pos", float64(timePos)/float64(time.Second))
}
func SetPercentPos(socket string, percentPos float64) error {
return SetProperty[float64](socket, "percent-pos", percentPos)
return SetProperty(socket, "percent-pos", percentPos)
}
func SetVolume(socket string, volume float64) error {
return SetProperty(socket, "volume", volume)
}
func SetPlaylistPosition(socket string, index int) error {
return SetProperty(socket, "playlist-pos", index)
}
func SetLoopFile(socket string, loopFile LoopState) error {
return SetProperty(socket, "loop-file", loopFile.String())
}
func SetLoopPlaylist(socket string, loopPlaylist LoopState) error {
return SetProperty(socket, "loop-playlist", loopPlaylist.String())
}

View File

@ -6,7 +6,7 @@ import (
"errors"
"net"
"git.milar.in/milarin/channel"
"git.tordarus.net/tordarus/channel"
)
type Command struct {
@ -47,7 +47,7 @@ func SendCommand[T any](socket string, cmd *Command) (*Response[T], error) {
}
func GetProperty[T any](socket string, propertyName string) (T, error) {
cmd := &Command{[]interface{}{"get_property", propertyName}}
cmd := &Command{[]any{"get_property", propertyName}}
resp, err := SendCommand[T](socket, cmd)
if err != nil {
return *new(T), err
@ -61,7 +61,7 @@ func GetProperty[T any](socket string, propertyName string) (T, error) {
}
func SetProperty[T any](socket string, propertyName string, propertyValue T) error {
cmd := &Command{[]interface{}{"set_property", propertyName, propertyValue}}
cmd := &Command{[]any{"set_property", propertyName, propertyValue}}
resp, err := SendCommand[T](socket, cmd)
if err != nil {
return err