This repository has been archived by the owner on Nov 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmpv.go
345 lines (283 loc) · 9.48 KB
/
mpv.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package mp
// #include <mpv/client.h>
// #include <stdlib.h>
// #cgo LDFLAGS: -lmpv
//
// /* some helper functions for string arrays */
// char** makeCharArray(int size) {
// return calloc(sizeof(char*), size);
// }
// void setArrayString(char** a, int i, char* s) {
// a[i] = s;
// }
import "C"
import "unsafe"
import (
"errors"
"flag"
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/aykevl/plaincast/config"
"github.com/aykevl/plaincast/log"
)
var MPV_PROPERTY_UNAVAILABLE = errors.New("mpv: property unavailable")
// MPV is an implementation of Backend, using libmpv.
type MPV struct {
handle *C.mpv_handle
running bool
runningMutex sync.Mutex
mainloopExit chan struct{}
}
var mpvLogger = log.New("mpv", "log MPV wrapper output")
var logLibMPV = flag.Bool("log-libmpv", false, "log output of libmpv")
// New creates a new MPV instance and initializes the libmpv player
func (mpv *MPV) initialize() (chan State, int) {
if mpv.handle != nil || mpv.running {
panic("already initialized")
}
mpv.mainloopExit = make(chan struct{})
mpv.running = true
mpv.handle = C.mpv_create()
conf := config.Get()
initialVolume, err := conf.GetInt("player.mpv.volume", func() (int, error) {
return INITIAL_VOLUME, nil
})
if err != nil {
// should not happen
panic(err)
}
mpv.setOptionFlag("resume-playback", false)
//mpv.setOptionString("softvol", "yes")
//mpv.setOptionString("ao", "pulse")
mpv.setOptionInt("volume", initialVolume)
// Disable video in three ways.
mpv.setOptionFlag("video", false)
mpv.setOptionString("vo", "null")
mpv.setOptionString("vid", "no")
// Cache settings assume 128kbps audio stream (16kByte/s).
// The default is a cache size of 25MB, these are somewhat more sensible
// cache sizes IMO.
mpv.setOptionInt("cache-default", 160) // 10 seconds
mpv.setOptionInt("cache-seek-min", 16) // 1 second
// Some extra debugging information, but don't read from stdin.
// libmpv has a problem with signal handling, though: when `terminal` is
// true, Ctrl+C doesn't work correctly anymore and program output is
// disabled.
mpv.setOptionFlag("terminal", *logLibMPV)
mpv.setOptionFlag("input-terminal", false)
mpv.setOptionFlag("quiet", true)
mpv.checkError(C.mpv_initialize(mpv.handle))
eventChan := make(chan State)
go mpv.eventHandler(eventChan)
return eventChan, initialVolume
}
// Function quit quits the player.
// WARNING: This MUST be the last call on this media player.
func (mpv *MPV) quit() {
mpv.runningMutex.Lock()
if !mpv.running {
panic("quit called twice")
}
mpv.running = false
mpv.runningMutex.Unlock()
// Wake up the event handler mainloop, probably sending the MPV_EVENT_NONE
// signal.
// See mpv_wait_event below: this doesn't work yet (it uses a workaround
// now).
//C.mpv_wakeup(handle)
// Wait until the mainloop has exited.
<-mpv.mainloopExit
// Actually destroy the MPV player. This blocks until the player has been
// fully brought down.
handle := mpv.handle
mpv.handle = nil // make it easier to catch race conditions
C.mpv_terminate_destroy(handle)
}
// setOptionFlag passes a boolean flag to mpv
func (mpv *MPV) setOptionFlag(key string, value bool) {
cValue := C.int(0)
if value {
cValue = 1
}
mpv.setOption(key, C.MPV_FORMAT_FLAG, unsafe.Pointer(&cValue))
}
// setOptionInt passes an integer option to mpv
func (mpv *MPV) setOptionInt(key string, value int) {
cValue := C.int64_t(value)
mpv.setOption(key, C.MPV_FORMAT_INT64, unsafe.Pointer(&cValue))
}
// setOptionString passes a string option to mpv
func (mpv *MPV) setOptionString(key, value string) {
cValue := C.CString(value)
defer C.free(unsafe.Pointer(cValue))
mpv.setOption(key, C.MPV_FORMAT_STRING, unsafe.Pointer(&cValue))
}
// setOption is a generic function to pass options to mpv
func (mpv *MPV) setOption(key string, format C.mpv_format, value unsafe.Pointer) {
cKey := C.CString(key)
defer C.free(unsafe.Pointer(cKey))
mpv.checkError(C.mpv_set_option(mpv.handle, cKey, format, value))
}
// sendCommand sends a command to the libmpv player
func (mpv *MPV) sendCommand(command []string) {
// Print command, but without the stream
cmd := make([]string, len(command))
copy(cmd, command)
if command[0] == "loadfile" {
cmd[1] = "<stream>"
}
logger.Println("MPV command:", cmd)
cArray := C.makeCharArray(C.int(len(command) + 1))
if cArray == nil {
panic("got NULL from calloc")
}
defer C.free(unsafe.Pointer(cArray))
for i, s := range command {
cStr := C.CString(s)
C.setArrayString(cArray, C.int(i), cStr)
defer C.free(unsafe.Pointer(cStr))
}
mpv.checkError(C.mpv_command_async(mpv.handle, 0, cArray))
}
// getProperty returns the MPV player property as a string
// Warning: this function can take an unbounded time. Call inside a new
// goroutine to prevent blocking / deadlocks.
func (mpv *MPV) getProperty(name string) (float64, error) {
logger.Printf("MPV get property: %s\n", name)
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
var cValue C.double
status := C.mpv_get_property(mpv.handle, cName, C.MPV_FORMAT_DOUBLE, unsafe.Pointer(&cValue))
if status == C.MPV_ERROR_PROPERTY_UNAVAILABLE {
return 0, MPV_PROPERTY_UNAVAILABLE
} else if status != 0 {
return 0, errors.New("mpv: " + C.GoString(C.mpv_error_string(status)))
}
return float64(cValue), nil
}
// setProperty sets the MPV player property
func (mpv *MPV) setProperty(name, value string) {
logger.Printf("MPV set property: %s=%s\n", name, value)
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
cValue := C.CString(value)
defer C.free(unsafe.Pointer(cValue))
// setProperty can take an unbounded time, don't block here using _async
// TODO: use some form of error handling. Sometimes, it is impossible to
// know beforehand whether setting a property will cause an error.
// Importantly, catch the 'property unavailable' error.
mpv.checkError(C.mpv_set_property_async(mpv.handle, 1, cName, C.MPV_FORMAT_STRING, unsafe.Pointer(&cValue)))
}
func (mpv *MPV) play(stream string, position time.Duration, volume int) {
options := "pause=no"
if position != 0 {
options += fmt.Sprintf(",start=%.3f", position.Seconds())
}
if volume >= 0 {
options += fmt.Sprintf(",volume=%d", volume)
}
// The proxy is a workaround for misbehaving libav/libnettle that appear to
// try to read the whole HTTP response before closing the connection. Go has
// a better HTTPS implementation, which is used here as a workaround.
// This libav/libnettle combination is in use on Debian jessie. FFmpeg
// doesn't have a problem with it.
if !strings.HasPrefix(stream, "https://") {
logger.Panic("Stream does not start with https://...")
}
mpv.sendCommand([]string{"loadfile", "http://localhost:8008/proxy/" + stream[len("https://"):], "replace", options})
}
func (mpv *MPV) pause() {
mpv.setProperty("pause", "yes")
}
func (mpv *MPV) resume() {
mpv.setProperty("pause", "no")
}
func (mpv *MPV) getDuration() (time.Duration, error) {
duration, err := mpv.getProperty("duration")
if err == MPV_PROPERTY_UNAVAILABLE {
return 0, PROPERTY_UNAVAILABLE
} else if err != nil {
// should not happen
panic(err)
}
return time.Duration(duration * float64(time.Second)), nil
}
func (mpv *MPV) getPosition() (time.Duration, error) {
position, err := mpv.getProperty("time-pos")
if err == MPV_PROPERTY_UNAVAILABLE {
return 0, PROPERTY_UNAVAILABLE
} else if err != nil {
// should not happen
panic(err)
}
if position < 0 {
// Sometimes, the position appears to be slightly off.
position = 0
}
return time.Duration(position * float64(time.Second)), nil
}
func (mpv *MPV) setPosition(position time.Duration) {
mpv.sendCommand([]string{"seek", fmt.Sprintf("%.3f", position.Seconds()), "absolute"})
}
func (mpv *MPV) getVolume() int {
volume, err := mpv.getProperty("volume")
if err != nil {
// should not happen
panic(err)
}
return int(volume + 0.5)
}
func (mpv *MPV) setVolume(volume int) {
mpv.setProperty("volume", strconv.Itoa(volume))
config.Get().SetInt("player.mpv.volume", volume)
}
func (mpv *MPV) stop() {
mpv.sendCommand([]string{"stop"})
}
// playerEventHandler waits for libmpv player events and sends them on a channel
func (mpv *MPV) eventHandler(eventChan chan State) {
for {
// wait until there is an event (negative timeout means infinite timeout)
// The timeout is 1 second to work around libmpv bug #1372 (mpv_wakeup
// does not actually wake up mpv_wait_event). It keeps checking every
// second whether MPV has exited.
// TODO revert this as soon as the fix for that bug lands in a stable
// release. Check for the problematic versions and keep the old behavior
// for older MPV versions.
event := C.mpv_wait_event(mpv.handle, 1)
if event.event_id != C.MPV_EVENT_NONE {
logger.Printf("MPV event: %s (%d)\n", C.GoString(C.mpv_event_name(event.event_id)), int(event.event_id))
}
if event.error != 0 {
panic("MPV API error")
}
mpv.runningMutex.Lock()
running := mpv.running
mpv.runningMutex.Unlock()
if !running {
close(eventChan)
mpv.mainloopExit <- struct{}{}
return
}
switch event.event_id {
case C.MPV_EVENT_PLAYBACK_RESTART:
eventChan <- STATE_PLAYING
case C.MPV_EVENT_END_FILE:
eventChan <- STATE_STOPPED
case C.MPV_EVENT_PAUSE:
eventChan <- STATE_PAUSED
case C.MPV_EVENT_UNPAUSE:
eventChan <- STATE_PLAYING
}
}
}
// checkError checks for libmpv errors and panics if it finds one
func (mpv *MPV) checkError(status C.int) {
if status < 0 {
// this C string should not be freed (it is static)
panic(fmt.Sprintf("mpv API error: %s (%d)", C.GoString(C.mpv_error_string(status)), int(status)))
}
}