9 Commits

Author SHA1 Message Date
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
930caa62c2 implemented Quit function 2024-01-10 22:25:48 +01:00
8 changed files with 89 additions and 36 deletions

View File

@ -1,5 +1,10 @@
package mpvipc
import (
"context"
"time"
)
type LoadFileFlag string
const (
@ -17,3 +22,45 @@ func LoadFile(socket string, file string, flags LoadFileFlag) error {
_, 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:
if Ping(socket) {
out <- true
return
}
case <-ctx.Done():
out <- false
return
}
}
}()
return out
}

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=

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

@ -17,6 +17,10 @@ func IsPaused(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 SetPlaylistPosition(socket string, index int) error {
return SetProperty(socket, "playlist-pos", index)
}
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")
}

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

@ -17,3 +17,7 @@ func SetTimePos(socket string, timePos time.Duration) error {
func SetPercentPos(socket string, percentPos float64) error {
return SetProperty[float64](socket, "percent-pos", percentPos)
}
func SetVolume(socket string, volume float64) error {
return SetProperty[float64](socket, "volume", volume)
}

View File

@ -5,9 +5,8 @@ import (
"encoding/json"
"errors"
"net"
"time"
"git.milar.in/milarin/channel"
"git.tordarus.net/tordarus/channel"
)
type Command struct {
@ -28,34 +27,6 @@ type Event[T any] struct {
Reason string `json:"reason"`
}
func IsReady(ctx context.Context, socket string) <-chan bool {
out := make(chan bool, 1)
go func() {
defer close(out)
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()
out <- true
return
}
case <-ctx.Done():
out <- false
return
}
}
}()
return out
}
func SendCommand[T any](socket string, cmd *Command) (*Response[T], error) {
conn, err := net.Dial("unix", socket)
if err != nil {