forked from foxis/EasyRobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fps.go
93 lines (79 loc) · 1.97 KB
/
fps.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
package fps
import (
"time"
. "github.com/itohio/EasyRobot/pkg/core/logger"
"github.com/itohio/EasyRobot/pkg/core/options"
"github.com/itohio/EasyRobot/pkg/core/pipeline"
"github.com/itohio/EasyRobot/pkg/core/pipeline/steps"
"github.com/itohio/EasyRobot/pkg/core/plugin"
"github.com/itohio/EasyRobot/pkg/core/store"
)
func init() {
pipeline.Register(NAME, New)
}
func New(opts ...options.Option) (pipeline.Step, error) {
o := Options{
numFrames: 30,
}
options.ApplyOptions(&o, opts...)
newOpts := append([]options.Option{plugin.WithName(NAME)}, opts...)
newOpts = append(newOpts, WithFPSCounter(o.numFrames, o.suffix))
return steps.NewProcessor(newOpts...)
}
func WithFPSCounter(numFrames int, suffix store.FQDNType) options.Option {
fps := store.FPS
drop := store.DROPPED_FRAMES
if suffix != store.NONE {
fps += suffix
drop += suffix
}
return steps.WithProcessor(&fpsCounter{
nameFPS: fps,
nameDropped: drop,
numFrames: numFrames,
})
}
type fpsCounter struct {
numFrames int
nameFPS store.FQDNType
nameDropped store.FQDNType
lastTimestamp int64
lastIndex int64
dropped int64
fps float32
}
func (s *fpsCounter) Process(src, dst store.Store) error {
index, err := src.Index()
if err != nil {
Log.Error().Err(err).Msg(store.INDEX.String())
return nil
}
timestamp, err := src.Timestamp()
if err != nil {
Log.Error().Err(err).Msg(store.TIMESTAMP.String())
return nil
}
if index%int64(s.numFrames) == 0 {
duration := time.Duration(timestamp - s.lastTimestamp)
s.lastTimestamp = timestamp
s.lastIndex = index
if s.lastIndex >= 0 {
s.fps = float32(s.numFrames) / float32(duration.Seconds())
}
}
dst.SetFPS(float32(s.fps))
dst.SetDropCount(int64(s.dropped))
// dst.SetName(src.Name())
return nil
}
func (s *fpsCounter) Init() error {
s.lastTimestamp = time.Now().UnixNano()
s.lastIndex = -1
return nil
}
func (s *fpsCounter) Reset() {
s.dropped = 0
}
func (s *fpsCounter) Close() {
s.dropped = 0
}