-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.go
291 lines (277 loc) · 6.83 KB
/
player.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
package main
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/faiface/beep"
"github.com/faiface/beep/speaker"
"github.com/zergon321/reisen"
"image"
"os"
"time"
)
type Player struct {
ticker <- chan time.Time
perSecond <- chan time.Time
errs chan error
frameBuffer chan *image.RGBA
fpscnt int
videoTotalFramesPlayed int
videoPlaybackFPS int
last time.Time
deltaTime float64
sampleBuffer chan [2]float64
sampleRate int
width int
height int
pause bool
snf int // optionally only show every nth frame
fps int // manually override frame rate
}
func NewPlayer(width, height, sampleRate, snf int, fps int) *Player {
player := new(Player)
player.width = width
player.height = height
player.sampleRate = sampleRate
player.frameBuffer = make(chan *image.RGBA, frameBufferSize)
player.sampleBuffer = make(chan [2]float64, sampleBufferSize)
player.errs = make(chan error)
player.snf = snf
player.fps = fps
return player
}
func (player *Player) GetFrameBufferDepth() int {
return len(player.frameBuffer)
}
func (player *Player) GetSampleBufferDepth() int {
return len(player.sampleBuffer)
}
func (player *Player) GetFrameIdx() int {
return player.videoTotalFramesPlayed
}
func (player *Player) GetFPS() int {
return player.videoPlaybackFPS
}
func (player *Player) Render() error {
player.deltaTime = time.Since(player.last).Seconds()
player.last = time.Now()
select {
case err, ok := <-player.errs:
if ok {
return err
}
default:
}
select {
case <-player.ticker:
frame, ok := <-player.frameBuffer
if ok {
// asciify image
if player.GetFrameIdx() % player.snf == 0 {
ascii.analyzeImage(frame)
ascii.print(os.Stdout)
}
player.videoTotalFramesPlayed++
player.fpscnt++
}
default:
}
select {
case <-player.perSecond:
// metrics
player.videoPlaybackFPS = player.fpscnt
player.fpscnt = 0
default:
}
return nil
}
func (player *Player) Layout(a, b int) (int, int) {
return player.width, player.height
}
func (player *Player) Start(fname string) error {
// find frame rate
media, err := reisen.NewMedia(fname)
if err != nil {
return err
}
spf := float64(1.0)
var frameRateNum, frameRateDen int
if player.fps > 0 {
frameRateNum = player.fps
frameRateDen = 1
} else {
for _, stream := range media.Streams() {
if stream.Type() == reisen.StreamVideo {
frameRateNum, frameRateDen = stream.FrameRate()
}
}
}
spf = 1.0 / float64(frameRateNum/frameRateDen) // "seconds per frame"
frameDuration, err := time.ParseDuration(fmt.Sprintf("%fs", spf))
if err != nil {
return err
}
// open streams
err = media.OpenDecode()
if err != nil {
return err
}
videoStreams := media.VideoStreams()
videoStream := videoStreams[0]
err = videoStream.Open()
if err != nil {
return err
}
player.width = videoStream.Width()
player.height = videoStream.Height()
var audioStream *reisen.AudioStream
if *playAudio {
audioStream = media.AudioStreams()[0]
// init speaker
err = audioStream.Open()
if err != nil {
return err
}
player.sampleRate = audioStream.SampleRate()
err := speaker.Init(beep.SampleRate(player.sampleRate), SpeakerSampleRate.N(time.Second/10))
if err != nil {
return err
}
}
// start decoding streams
player.readVideoAndAudio(media, videoStream, audioStream)
if err != nil {
return err
}
// start playing audio samples
if *playAudio {
speaker.Play(player.streamSamples(player.sampleBuffer))
}
// setup metrics
player.last = time.Now()
player.fpscnt = 0
player.videoTotalFramesPlayed = 0
player.videoPlaybackFPS = 0
player.ticker = time.Tick(frameDuration)
player.perSecond = time.Tick(time.Second)
return nil
}
// readVideoAndAudio reads video and audio frames
// from the opened media and sends the decoded
// data to che channels to be played.
func (player *Player) readVideoAndAudio(media *reisen.Media, videoStream *reisen.VideoStream, audioStream *reisen.AudioStream) {
go func() {
for {
if player.pause {
time.Sleep(100*time.Millisecond)
continue
}
packet, gotPacket, err := media.ReadPacket()
if err != nil {
go func(err error) {
player.errs <- err
}(err)
}
if !gotPacket {
break
}
//TODO: make sure audio and video stays in sync
switch packet.Type() {
case reisen.StreamVideo:
s := media.Streams()[packet.StreamIndex()].(*reisen.VideoStream)
videoFrame, gotFrame, err := s.ReadVideoFrame()
if err != nil {
continue
/*go func(err error) {
errs <- err
}(err)*/
}
if !gotFrame {
continue
//break
}
if videoFrame == nil {
continue
}
// flip image if needed
// flip image if needed
//flippedImage := imaging.FlipV(videoFrame.Image())
//bounds := flippedImage.Bounds()
//flippedImageRGBA := image.NewRGBA(image.Rect(0, 0, bounds.Dx(), bounds.Dy()))
//draw.Draw(flippedImageRGBA, flippedImageRGBA.Bounds(), flippedImage, bounds.Min, draw.Src)
//frameBuffer <- flippedImageRGBA
player.frameBuffer <- videoFrame.Image()
case reisen.StreamAudio:
if !*playAudio {
continue
}
s := media.Streams()[packet.StreamIndex()].(*reisen.AudioStream)
audioFrame, gotFrame, err := s.ReadAudioFrame()
if err != nil {
continue
/*go func(err error) {
errs <- err
}(err)*/
}
if !gotFrame {
continue
//break
}
if audioFrame == nil {
continue
}
// turn the raw byte data into audio samples of type [2]float64.
reader := bytes.NewReader(audioFrame.Data())
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) {
player.errs <- err
}(err)
}
sample[0] = result
err = binary.Read(reader, binary.LittleEndian, &result)
if err != nil {
go func(err error) {
player.errs <- err
}(err)
}
sample[1] = result
player.sampleBuffer <- sample
}
}
}
videoStream.Close()
if audioStream != nil {
audioStream.Close()
}
media.CloseDecode()
close(player.frameBuffer)
close(player.sampleBuffer)
close(player.errs)
}()
}
// 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 (player *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++ {
sample, ok := <-sampleSource
if !ok {
numRead = i + 1
break
}
samples[i] = sample
numRead++
}
if numRead < len(samples) {
return numRead, false
}
return numRead, true
})
}