-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamerChrome.go
252 lines (221 loc) · 5.67 KB
/
streamerChrome.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package piplayer
import (
"errors"
"os"
"os/exec"
"strconv"
)
// Chrome represents Google Chrome as the video stream playback software.
// It implements the streamer interface.
type Chrome struct {
Supports []string
status int
// closing chan error
cmd *exec.Cmd
ConnViewer ConnectionWS
ConnControl ConnectionWS
}
const (
// defaultProgram = "chromium-browser"
defaultProgram = "chromium"
viewerPage = "http://localhost:8080/viewer"
)
var defaultFlags = []string{
"--window-size=1920,1080",
"--window-position=0,0",
"--kiosk",
"--incognito",
"--disable-infobars",
"--noerrdialogs",
"--no-first-run",
"--enable-experimental-web-platform-features",
"--javascript-harmony",
"--autoplay-policy=no-user-gesture-required",
"--remote-debugging-port=9222",
// Experimental gpu enabling flags for potentially higher video playback performance.
// Use with caution, these are not stable.
/*
"--ignore-gpu-blacklist",
"--enable-gpu-rasterization",
"--enable-native-gpu-memory-buffers",
"--enable-checker-imaging",
"--disable-quic",
"--enable-tcp-fast-openhardware-media-key-handling
"--disable-gpu-composithardware-media-key-handling
"--enable-fast-unload",hardware-media-key-handling
"--enable-experimental-canvas-features",
"--enable-scroll-prediction",
"--enable-simple-cache-backend",
"--answers-in-suggest",
"--ppapi-flash-path=/usr/lib/chromium-browser/libpepflashplayer.so",
"--ppapi-flash-args=enable_stagevideo_auto=0",
"--ppapi-flash-version=",
"--max-tiles-for-interest-area=512",
"--num-raster-threads=4",
"--default-tile-height=512",
*/
// End of experimental flags
"http://localhost:8080/viewer",
}
// Open starts Chrome in the relevant page with the relevant flags.
func (c *Chrome) Open(file string, status chan string, test string, debug bool) error {
// If Chrome is already running ignore and return error
if c.status == statusStopped {
return errors.New("cannot start Chrome, it's already running")
}
flags := defaultFlags
program := defaultProgram
if test != "" {
flags = []string{
"--incognito",
"--remote-debugging-port=9222",
}
}
switch test {
case "linux":
program = "google-chrome"
case "mac":
program = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
default:
}
flags = append(flags, viewerPage)
c.cmd = exec.Command(program, flags...)
c.cmd.Stdin = os.Stdin
c.cmd.Stderr = os.Stderr
if debug {
c.cmd.Stdout = os.Stdout
}
if err := c.cmd.Start(); err != nil {
return err
}
c.status = statusStarting
// TODO: handle graceful shutdown properly with context
// ctxt, cancel := context.WithCancel(context.Background())
return nil
}
// HandleRes checks the WebSocket response for errors.
func handleRes(res wsMessage) error {
if !res.Success {
resMsg := res.Message.(string)
return errors.New(resMsg)
}
return nil
}
// Close closes Google Chrome.
func (c *Chrome) Close() error {
// TODO: everything
return nil
}
// Play sends a play command.
func (c *Chrome) Play() error {
msg := wsMessage{
Component: "player",
Method: "play",
}
// TODO: create convenience method that sends and waits?
send := c.ConnViewer.getChanSend()
send <- msg
// Wait for response
res := <-c.ConnViewer.getChanReceive()
return handleRes(res)
}
// Pause sends a pause command.
func (c *Chrome) Pause() error {
msg := wsMessage{
Component: "player",
Method: "pause",
}
send := c.ConnViewer.getChanSend()
send <- msg
res := <-c.ConnViewer.getChanReceive()
return handleRes(res)
}
// PlaybackRate sets the video playback rate.
func (c *Chrome) PlaybackRate(rate int) error {
msg := wsMessage{
Component: "player",
Method: "playbackRate",
Arguments: map[string]string{
"rate": strconv.Itoa(rate),
},
}
send := c.ConnViewer.getChanSend()
send <- msg
res := <-c.ConnViewer.getChanReceive()
return handleRes(res)
}
// Seek seeks to a specific time in a video.
func (c *Chrome) Seek(seconds int) error {
msg := wsMessage{
Component: "player",
Method: "seek",
Arguments: map[string]string{
"seconds": strconv.Itoa(seconds),
},
}
send := c.ConnViewer.getChanSend()
send <- msg
res := <-c.ConnViewer.getChanReceive()
return handleRes(res)
}
// Chapter seeks to a specific chapter in the video.
func (c *Chrome) Chapter(chp int) error {
msg := wsMessage{
Component: "player",
Method: "chapter",
Arguments: map[string]string{
"chapter": strconv.Itoa(chp),
},
}
send := c.ConnViewer.getChanSend()
send <- msg
res := <-c.ConnViewer.getChanReceive()
return handleRes(res)
}
// Volume sets the video volume.
func (c *Chrome) Volume(v int) error {
msg := wsMessage{
Component: "player",
Method: "volume",
Arguments: map[string]string{
"rate": strconv.Itoa(v),
},
}
send := c.ConnViewer.getChanSend()
send <- msg
res := <-c.ConnViewer.getChanReceive()
return handleRes(res)
}
// AudioStream sets the video audio stream.
func (c *Chrome) AudioStream(a int) error {
msg := wsMessage{
Component: "player",
Method: "audioStream",
Arguments: map[string]string{
"rate": strconv.Itoa(a),
},
}
send := c.ConnViewer.getChanSend()
send <- msg
res := <-c.ConnViewer.getChanReceive()
return handleRes(res)
}
// SubtitleStream sets the video subtitle stream.
func (c *Chrome) SubtitleStream(s int) error {
msg := wsMessage{
Component: "player",
Method: "subtitleStream",
Arguments: map[string]string{
"rate": strconv.Itoa(s),
},
}
send := c.ConnViewer.getChanSend()
send <- msg
res := <-c.ConnViewer.getChanReceive()
return handleRes(res)
}
// Listen does nothing for now.
func (c *Chrome) Listen(s chan string) error {
// TODO: This whole implementation. I'm not sure if the Chrome streamer needs this?
return nil
}