-
Notifications
You must be signed in to change notification settings - Fork 0
/
libmpv.go
230 lines (187 loc) · 3.78 KB
/
libmpv.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
// +build !nolibmpv
package player
import (
"time"
"github.com/YouROK/go-mpv/mpv"
)
type LibMPV struct {
cmdPause map[bool]string
volume float64
volumeChan chan<- int
seekChan chan<- *Pos
pos pos
}
type pos struct {
timePos float64
timeEnd float64
}
func (p pos) Pos() *Pos {
return &Pos{
time.Duration(p.timePos * float64(time.Second)),
time.Duration(p.timeEnd * float64(time.Second)),
}
}
func NewLibMPV(volume chan<- int, seek chan<- *Pos) Player {
return &LibMPV{
map[bool]string{
true: "yes",
false: "no",
},
100,
volume,
seek,
pos{0, 0},
}
}
func (m *LibMPV) Name() string {
return "libmpv"
}
func (m *LibMPV) Spawn(file string, params []Param) (chan Command, func(), error) {
commands := make(chan Command)
done := make(chan struct{})
p := mpv.Create()
m.adjustVolume(p, 0)
c := make(chan *mpv.Event)
go func() {
for {
e := p.WaitEvent(.05)
if e.Event_Id == mpv.EVENT_END_FILE ||
e.Event_Id == mpv.EVENT_SHUTDOWN {
done <- struct{}{}
p.TerminateDestroy()
close(c)
return
}
c <- e
}
}()
for _, par := range params {
switch par {
case ParamNoVideo:
p.SetOption("vid", mpv.FORMAT_FLAG, false)
case ParamSilent:
p.SetOption("really-quiet", mpv.FORMAT_FLAG, true)
}
}
if err := p.Initialize(); err != nil {
return nil, nil, err
}
go func() {
paused := false
closed := false
quickTO := time.Millisecond * 200
slowTO := time.Second * 2
quick := time.After(slowTO)
slow := time.After(quickTO)
for {
select {
case <-done:
closed = true
case command, ok := <-commands:
if !ok && closed {
return
}
if closed {
continue
}
switch command {
case CmdPause:
paused = !paused
p.SetPropertyString("pause", m.cmdPause[paused])
case CmdStop:
p.Command([]string{"quit"})
case CmdVolDown:
m.adjustVolume(p, -5)
case CmdVolUp:
m.adjustVolume(p, 5)
case CmdSeekBackward:
m.seek(p, -time.Second*10)
case CmdSeekForward:
m.seek(p, time.Second*10)
}
case <-quick:
quick = time.After(quickTO)
if closed {
continue
}
m.positionQuick(p)
case <-slow:
slow = time.After(slowTO)
if closed {
continue
}
m.position(p)
}
}
}()
wait := func() {
for range c {
}
}
p.Command([]string{"loadfile", file})
return commands, wait, nil
}
func (m *LibMPV) Supported() bool {
return true
}
func (m *LibMPV) seek(p *mpv.Mpv, adjustment time.Duration) error {
_cur, err := p.GetProperty("time-pos", mpv.FORMAT_INT64)
if err != nil {
return err
}
if adjustment != 0 {
cur := _cur.(int64) + int64(adjustment.Seconds())
if cur < 0 {
cur = 0
}
p.SetProperty("time-pos", mpv.FORMAT_INT64, cur)
}
m.positionQuick(p)
return nil
}
func (m *LibMPV) positionQuick(p *mpv.Mpv) error {
_cur, err := p.GetProperty("time-pos", mpv.FORMAT_DOUBLE)
if err != nil {
return err
}
m.pos.timePos = _cur.(float64)
select {
case m.seekChan <- m.pos.Pos():
default:
}
return nil
}
func (m *LibMPV) position(p *mpv.Mpv) error {
_byteCur, err := p.GetProperty("stream-pos", mpv.FORMAT_DOUBLE)
if err != nil {
return err
}
_byteTotal, err := p.GetProperty("stream-end", mpv.FORMAT_DOUBLE)
if err != nil {
return err
}
byteTotal := _byteTotal.(float64)
if byteTotal < 1 {
byteTotal = 1
}
bytePos := _byteCur.(float64) / byteTotal
if bytePos > 1.0 {
bytePos = 1.0
}
m.pos.timeEnd = float64(m.pos.timePos) / bytePos
return m.positionQuick(p)
}
func (m *LibMPV) adjustVolume(p *mpv.Mpv, adjustment float64) error {
m.volume += adjustment
if m.volume < 0 {
m.volume = 0
} else if m.volume > 100 {
m.volume = 100
}
p.SetProperty("volume", mpv.FORMAT_DOUBLE, m.volume)
select {
case m.volumeChan <- int(m.volume):
default:
}
return nil
}