60 lines
1.0 KiB
Go
60 lines
1.0 KiB
Go
package mpvipc
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
type LoadFileFlag string
|
|
|
|
const (
|
|
LoadFileReplace LoadFileFlag = "replace"
|
|
LoadFileAppend LoadFileFlag = "append"
|
|
LoadFileAppendPlay LoadFileFlag = "append-play"
|
|
)
|
|
|
|
func LoadFile(socket string, file string, flags LoadFileFlag) error {
|
|
if flags == "" {
|
|
flags = LoadFileReplace
|
|
}
|
|
|
|
cmd := &Command{[]interface{}{"loadfile", file, flags}}
|
|
_, 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 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
|
|
}
|