-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.go
More file actions
83 lines (70 loc) · 1.52 KB
/
Copy pathtimer.go
File metadata and controls
83 lines (70 loc) · 1.52 KB
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
package main
import (
"time"
"github.com/asticode/go-astiav"
)
type Timer struct {
input chan *Image
output chan *Image
waitTime time.Duration
endTime time.Time
isPlaying bool
startTime time.Time
pctx *PlayerContext
}
// Reset sets up the input and output channels using parameters.
func (t *Timer) Reset(input chan *Image, output chan *Image) {
t.input = input
t.output = output
t.isPlaying = false
}
func NewTimer(pctx *PlayerContext) *Timer {
return &Timer{
endTime: time.Now(),
pctx: pctx,
}
// Output and input channels set in Reset
}
func (t *Timer) wait() {
if !t.isPlaying {
t.endTime = time.Now()
t.startTime = t.endTime
t.isPlaying = true
}
t.endTime = t.endTime.Add(t.waitTime)
timeLeft := time.Until(t.endTime)
if timeLeft > 0 {
time.Sleep(timeLeft)
} else {
logger.Info("timer", "Frame took too long to render")
}
}
func (t *Timer) Start(fps astiav.Rational) error {
num := float64(fps.Num())
den := float64(fps.Den())
t.waitTime = time.Duration((den * 1e9 / num))
for {
// Wait for timing
t.wait()
// Receive from input with context checking
select {
case <-t.pctx.ctx.Done():
logger.Info("timer", "Stopped")
return nil
case data, ok := <-t.input:
if !ok {
close(t.output)
logger.Info("timer", "No more frames to render")
return nil
}
// Send to output with context checking
select {
case <-t.pctx.ctx.Done():
logger.Info("timer", "Stopped")
return nil
case t.output <- data:
// Successfully sent data
}
}
}
}