mpvfs

9P file server for controlling mpv playback

git clone https://git.8pit.net/mpvfs.git

 1package main
 2
 3import (
 4	"github.com/nmeum/mpvfs/mpv"
 5	"github.com/nmeum/mpvfs/playlistfs"
 6	"go.rbn.im/neinp"
 7
 8	"flag"
 9	"fmt"
10	"io"
11	"log"
12	"net"
13	"os"
14)
15
16var (
17	state     *playerState
18	mpvClient *mpv.Client
19)
20
21var (
22	verbose = flag.Bool("v", false, "verbose output for debugging")
23	addr    = flag.String("a", "localhost:9999", "address to listen on")
24)
25
26func usage() {
27	fmt.Fprintf(flag.CommandLine.Output(),
28		"USAGE: %s [FLAGS] MPV_SOCKET\n\n"+
29			"The following flags are supported:\n\n", os.Args[0])
30
31	flag.PrintDefaults()
32	os.Exit(2)
33}
34
35func handleError(pc <-chan error, sc <-chan error) {
36	for {
37		select {
38		case perr := <-pc:
39			log.Println("[player error]", perr)
40		case serr := <-sc:
41			log.Println("[state error]", serr)
42		}
43	}
44}
45
46func startServer(mpvClient *mpv.Client, state *playerState) {
47	listener, err := net.Listen("tcp", *addr)
48	if err != nil {
49		log.Fatal(err)
50	}
51
52	config := playlistfs.Config{
53		Playctl:  newCtl,
54		Playlist: newPlaylist,
55		Playvol:  newVol,
56	}
57
58	fs := playlistfs.NewPlaylistFS(config)
59	for {
60		conn, err := listener.Accept()
61		if err != nil {
62			log.Println(err)
63			continue
64		}
65
66		server := neinp.NewServer(fs)
67
68		err = server.Serve(conn)
69		if err != nil && err != io.EOF {
70			log.Println(err)
71			continue
72		}
73	}
74}
75
76func main() {
77	flag.Parse()
78	flag.Usage = usage
79
80	if flag.NArg() < 1 {
81		usage()
82	}
83	socketFp := flag.Arg(0)
84
85	var err error
86	mpvClient, err = mpv.NewClient(socketFp, *verbose)
87	if err != nil {
88		log.Fatal(err)
89	}
90	state, err = newPlayerState(mpvClient)
91	if err != nil {
92		log.Fatal(err)
93	}
94
95	go handleError(mpvClient.ErrChan, state.ErrChan())
96	startServer(mpvClient, state)
97}