-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathoptions.go
209 lines (174 loc) · 4.48 KB
/
options.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package open_in_mpv
import (
"encoding/json"
"fmt"
"net/url"
"strings"
)
// The Options struct defines a model for the data contained in the mpv://
// URL and acts as a command generator (both CLI and IPC) to spawn and
// communicate with an mpv player window.
type Options struct {
Enqueue bool
Flags string
Fullscreen bool
NeedsIpc bool
NewWindow bool
Pip bool
Player string
Url *url.URL
}
// Utility object to marshal an mpv-compatible JSON command. As defined in the
// documentation, a valid IPC command is a JSON array of strings
type enqueueCmd struct {
Command []string `json:"command,omitempty"`
}
// Default constructor for an Option object
func NewOptions() Options {
return Options{
Enqueue: false,
Flags: "",
Fullscreen: false,
NeedsIpc: false,
NewWindow: false,
Pip: false,
Player: "mpv",
Url: nil,
}
}
// Parse a mpv:// URL and populate the current Options
func (o *Options) Parse(uri string) error {
u, err := url.Parse(uri)
if err != nil {
return err
}
if u.Scheme != "mpv" {
return fmt.Errorf("Unsupported protocol: %s", u.Scheme)
}
if u.Path != "/open" {
return fmt.Errorf("Unsupported method: %s", u.Path)
}
if len(u.RawQuery) < 2 {
return fmt.Errorf("Empty or malformed query: %s", u.RawQuery)
}
playerConfig := GetPlayerConfig(o.Player)
if playerConfig == nil {
return fmt.Errorf("Unsupported player: %s", o.Player)
}
// Extract player command line flags
o.Flags, err = url.QueryUnescape(u.Query().Get("flags"))
if err != nil {
return err
}
// Extract video file URL
rawUrl, err := url.QueryUnescape(u.Query().Get("url"))
if err != nil {
return err
}
// Parse the unprocessed URL
o.Url, err = url.Parse(rawUrl)
if err != nil {
return err
}
// Validate the raw URL scheme against the configured ones
if !stringSliceContains(o.Url.Scheme, playerConfig.SupportedSchemes) {
return fmt.Errorf(
"Unsupported schema for player '%s': %s. Did you forget to add it in the configuration?",
playerConfig.Name,
o.Url.Scheme,
)
}
if p, ok := u.Query()["player"]; ok {
o.Player = p[0]
}
o.Enqueue = u.Query().Get("enqueue") == "1"
o.Fullscreen = u.Query().Get("full_screen") == "1"
o.NewWindow = u.Query().Get("new_window") == "1"
o.Pip = u.Query().Get("pip") == "1"
o.NeedsIpc = playerConfig.NeedsIpc
return nil
}
// Parses flag overrides and returns the final flags
func (o Options) overrideFlags() string {
var (
ret []string
star bool
)
playerConfig := GetPlayerConfig(o.Player)
if playerConfig == nil {
return ""
}
// Premature look for star override in configuration
_, star = playerConfig.FlagOverrides["*"]
for _, flag := range strings.Split(o.Flags, " ") {
if star {
// Unconditionally replace all flags with the star template
stripped := strings.TrimLeft(flag, "-")
replaced := strings.ReplaceAll(
playerConfig.FlagOverrides["*"],
`%s`,
stripped,
)
ret = append(ret, replaced)
continue
}
// Otherwise, iterate over all templates for the current flag and
// do the necessary string replacements
if override, ok := playerConfig.FlagOverrides[flag]; ok {
stripped := strings.TrimLeft(flag, "-")
ret = append(ret, strings.ReplaceAll(
override,
`%s`,
stripped,
))
}
}
return strings.Join(ret, " ")
}
// Builds a CLI command used to invoke the player with the appropriate
// arguments
func (o Options) GenerateCommand() (string, []string) {
var ret []string
playerConfig := GetPlayerConfig(o.Player)
if o.Fullscreen {
ret = append(ret, playerConfig.Fullscreen)
}
if o.Pip {
ret = append(ret, strings.Split(playerConfig.Pip, " ")...)
}
if o.Flags != "" {
if len(playerConfig.FlagOverrides) == 0 {
ret = append(ret, o.Flags)
} else {
ret = append(ret, o.overrideFlags())
}
}
ret = append(ret, o.Url.String())
return playerConfig.Executable, ret
}
// Builds the IPC command needed to enqueue videos if the player requires it
func (o Options) GenerateIPC() ([]byte, error) {
if !o.NeedsIpc {
return []byte{}, fmt.Errorf("This player does not need IPC")
}
cmd := enqueueCmd{
[]string{"loadfile", o.Url.String(), "append-play"},
}
ret, err := json.Marshal(cmd)
if err != nil {
return []byte{}, err
}
if ret[len(ret)-1] != '\n' {
ret = append(ret, '\n')
}
return ret, nil
}
// Simple linear search for value in slice of strings
func stringSliceContains(value string, v []string) bool {
for _, elem := range v {
if elem == value {
return true
}
}
return false
}