-
Notifications
You must be signed in to change notification settings - Fork 119
/
player.go
67 lines (60 loc) · 1.73 KB
/
player.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"log"
"os/exec"
"runtime"
"strconv"
"strings"
)
// GenericPlayer represents most players. The stream URL will be appended to the arguments.
type GenericPlayer struct {
Name string
Args []string
}
// Player opens a stream URL in a video player.
type Player interface {
Open(url string) error
}
var genericPlayers = []GenericPlayer{
{Name: "VLC", Args: []string{"vlc"}},
{Name: "MPV", Args: []string{"mpv"}},
{Name: "MPlayer", Args: []string{"mplayer"}},
}
// Open the given stream in a GenericPlayer.
func (p GenericPlayer) Open(url string) error {
command := []string{}
if runtime.GOOS == "darwin" {
command = []string{"open", "-a"}
}
command = append(command, p.Args...)
command = append(command, url)
// #nosec
// It is the user's responsibility to pass the correct arguments to open the url.
return exec.Command(command[0], command[1:]...).Start()
}
// openPlayer opens a stream using the specified player and port.
func openPlayer(playerName string, port int) {
var player Player
for _, genericPlayer := range genericPlayers {
if strings.EqualFold(genericPlayer.Name, playerName) {
player = genericPlayer
break
}
}
if player == nil {
log.Printf("Player '%s' is not supported. Currently supported players are: %s", playerName, joinPlayerNames())
return
}
log.Printf("Playing in %s", playerName)
if err := player.Open("http://localhost:" + strconv.Itoa(port)); err != nil {
log.Printf("Error opening %s: %s\n", playerName, err)
}
}
// joinPlayerNames returns a list of supported video players ready for display.
func joinPlayerNames() string {
names := make([]string, len(genericPlayers))
for i := range genericPlayers {
names[i] = genericPlayers[i].Name
}
return strings.Join(names, ", ")
}