-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
executable file
·365 lines (294 loc) · 7.1 KB
/
main.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Package main creates a simple video player using the reisen library for accessing the data.
// It is derived from the excellent example at https://github.com/zergon321/reisen/blob/master/examples/player/main.go
package main
import (
"bytes"
"encoding/binary"
"fmt"
"image"
"image/color"
"log"
"os"
"path/filepath"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/faiface/beep"
"github.com/faiface/beep/speaker"
"github.com/zergon321/reisen"
)
const (
width = 1280
height = 720
frameBufferSize = 1024
sampleRate = 44100
channelCount = 2
bitDepth = 8
sampleBufferSize = 32 * channelCount * bitDepth * 1024
SpeakerSampleRate beep.SampleRate = 44100
)
// readVideoAndAudio reads video and audio frames
// from the opened media and sends the decoded
// data to che channels to be played.
func (p *player) readVideoAndAudio(media *reisen.Media) (<-chan *image.RGBA, <-chan [2]float64, chan error, error) {
frameBuffer := make(chan *image.RGBA, frameBufferSize)
sampleBuffer := make(chan [2]float64, sampleBufferSize)
errs := make(chan error)
err := media.OpenDecode()
if err != nil {
return nil, nil, nil, err
}
videoStream := media.VideoStreams()[0]
err = videoStream.Open()
if err != nil {
return nil, nil, nil, err
}
audioStream := media.AudioStreams()[0]
err = audioStream.Open()
if err != nil {
return nil, nil, nil, err
}
go func() {
for {
packet, gotPacket, err := media.ReadPacket()
if err != nil {
go func(err error) {
errs <- err
}(err)
}
if !gotPacket {
break
}
switch packet.Type() {
case reisen.StreamVideo:
s := media.Streams()[packet.StreamIndex()].(*reisen.VideoStream)
videoFrame, gotFrame, err := s.ReadVideoFrame()
if err != nil {
go func(err error) {
errs <- err
}(err)
}
if !gotFrame {
break
}
if videoFrame == nil {
continue
}
frameBuffer <- videoFrame.Image()
case reisen.StreamAudio:
s := media.Streams()[packet.StreamIndex()].(*reisen.AudioStream)
audioFrame, gotFrame, err := s.ReadAudioFrame()
if err != nil {
go func(err error) {
errs <- err
}(err)
}
if !gotFrame {
break
}
if audioFrame == nil {
continue
}
// Turn the raw byte data into
// audio samples of type [2]float64.
reader := bytes.NewReader(audioFrame.Data())
// See the README.md file for
// detailed scheme of the sample structure.
for reader.Len() > 0 {
sample := [2]float64{0, 0}
var result float64
err = binary.Read(reader, binary.LittleEndian, &result)
if err != nil {
go func(err error) {
errs <- err
}(err)
}
sample[0] = result
err = binary.Read(reader, binary.LittleEndian, &result)
if err != nil {
go func(err error) {
errs <- err
}(err)
}
sample[1] = result
sampleBuffer <- sample
}
}
}
videoStream.Close()
audioStream.Close()
media.CloseDecode()
close(frameBuffer)
close(sampleBuffer)
close(errs)
}()
return frameBuffer, sampleBuffer, errs, nil
}
// streamSamples creates a new custom streamer for
// playing audio samples provided by the source channel.
//
// See https://github.com/faiface/beep/wiki/Making-own-streamers
// for reference.
func (p *player) streamSamples(sampleSource <-chan [2]float64) beep.Streamer {
return beep.StreamerFunc(func(samples [][2]float64) (n int, ok bool) {
numRead := 0
for i := 0; i < len(samples); i++ {
if p.paused {
time.Sleep(time.Millisecond * 8)
i--
continue
}
sample, ok := <-sampleSource
if !ok {
numRead = i + 1
break
}
samples[i] = sample
numRead++
}
if numRead < len(samples) {
return numRead, false
}
return numRead, true
})
}
// player holds all the data
// necessary for playing video.
type player struct {
pix *image.NRGBA
ticker <-chan time.Time
errs <-chan error
frameBuffer <-chan *image.RGBA
last time.Time
deltaTime float64
paused bool
}
// Starts reading samples and frames
// of the media file.
func (p *player) open(fname string) error {
// Initialize the audio speaker.
err := speaker.Init(sampleRate,
SpeakerSampleRate.N(time.Second/10))
if err != nil {
return err
}
// Sprite for drawing video frames.
p.pix = image.NewNRGBA(image.Rect(0, 0, width, height))
if err != nil {
return err
}
// Open the media file.
media, err := reisen.NewMedia(fname)
if err != nil {
return err
}
// Get the FPS for playing
// video frames.
videoFPS, _ := media.Streams()[0].FrameRate()
if videoFPS == 0 {
videoFPS = 60
}
if err != nil {
return err
}
// SPF for frame ticker.
spf := 1.0 / float64(videoFPS)
frameDuration, err := time.
ParseDuration(fmt.Sprintf("%fs", spf))
if err != nil {
return err
}
// Start decoding streams.
var sampleSource <-chan [2]float64
p.frameBuffer, sampleSource,
p.errs, err = p.readVideoAndAudio(media)
if err != nil {
return err
}
// Start playing audio samples.
speaker.Play(p.streamSamples(sampleSource))
p.ticker = time.Tick(frameDuration)
// Setup metrics.
p.last = time.Now()
return nil
}
func (p *player) update(screen *canvas.Image) error {
// Compute dt.
p.deltaTime = time.Since(p.last).Seconds()
p.last = time.Now()
// Check for incoming errors.
select {
case err, ok := <-p.errs:
if ok {
return err
}
default:
}
// Read video frames and draw them.
select {
case <-p.ticker:
frame, ok := <-p.frameBuffer
if ok {
p.pix.Pix = frame.Pix
screen.Refresh()
}
default:
}
return nil
}
func main() {
if len(os.Args) == 1 {
log.Println("Please specify a video file to play (e.g. myVid.mp4)")
return
}
p := &player{}
path, _ := filepath.Abs(os.Args[1])
name := filepath.Base(path)
err := p.open(path)
a := app.New()
w := a.NewWindow("Video Player")
w.Resize(fyne.NewSize(width/2, height/2))
w.SetPadded(false)
if err != nil {
dialog.ShowError(err, w)
}
var play *widget.Button
play = widget.NewButtonWithIcon("", theme.MediaPauseIcon(), func() {
p.paused = !p.paused
if p.paused {
play.SetIcon(theme.MediaPlayIcon())
} else {
play.SetIcon(theme.MediaPauseIcon())
}
})
bg := canvas.NewRectangle(color.NRGBA{R: 0x42, G: 0x42, B: 0x42, A: 0x66})
buttons := container.NewCenter(container.NewMax(bg,
container.NewHBox(
widget.NewLabel(name),
play,
)))
space := canvas.NewRectangle(color.Transparent)
space.SetMinSize(fyne.NewSize(48, 48))
controls := container.NewBorder(nil, container.NewVBox(buttons, space), nil, nil)
i := canvas.NewImageFromImage(p.pix)
i.ScaleMode = canvas.ImageScaleFastest
w.SetContent(container.NewMax(i, controls))
go func() {
for {
if p.paused {
time.Sleep(time.Millisecond * 8)
continue
}
err = p.update(i)
if err != nil {
log.Println("Error playing:", err)
}
}
}()
w.ShowAndRun()
}