forked from go-flutter-desktop/plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cv.go
196 lines (163 loc) · 3.84 KB
/
cv.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
package video_player
// This file hides the ffmpeg related computations.
//
// The api exposed by github.com/3d0c/gmf is messy (all go-ffmpeg lib are)
// which explain some of the following mess :).
//
// Based on the examples 'video-to-goImage.go' of 3d0c/gmf.
// TODO: fix the memory leak will occur..
import (
// "errors"
"fmt"
// "io"
// "sync"
"gocv.io/x/gocv"
"runtime"
"sync/atomic"
)
type ffmpegVideo struct {
// img gocv.Mat
// rgba gocv.Mat
source string
duration float64
fps float64
Frames chan *ffmpegFrame
paused chan bool
player *playerStatus
width int
height int
}
type ffmpegFrame struct {
// ffmpeg packet
buffer []byte
// in second
time float64
}
func (f *ffmpegFrame) Time() float64 {
return f.time
}
func (f *ffmpegFrame) Data() []byte {
return f.buffer
}
type playerStatus struct{ flag int32 }
const (
stopped = 3
paused = 2
playing = 1
start = 0
)
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
func PrintMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tHeapAlloc = %v MiB", bToMb(m.HeapAlloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
}
func (f *ffmpegVideo) Init(srcFileName string, bufferSize int) (err error) {
f.player = new(playerStatus)
f.paused = make(chan bool)
f.Frames = make(chan *ffmpegFrame, bufferSize)
f.source = srcFileName
// webcam, err := gocv.OpenVideoCapture(srcFileName)
webcam, err := gocv.OpenVideoCapture(0)
defer webcam.Close()
if err != nil {
fmt.Printf("Error opening video capture device: %v\n", srcFileName)
return
}
img := gocv.NewMat()
defer img.Close()
if ok := webcam.Read(&img); !ok {
fmt.Printf("Cannot read device\n")
return
}
f.width = int(webcam.Get(gocv.VideoCaptureFrameWidth))
f.height = int(webcam.Get(gocv.VideoCaptureFrameHeight))
webcam.Set(gocv.VideoCapturePosAVIRatio, 1)
f.duration = webcam.Get(gocv.VideoCapturePosFrames)
f.fps = webcam.Get(gocv.VideoCaptureFPS)
return nil
}
func (f *ffmpegVideo) Free() {
PrintMemUsage()
}
func (f *ffmpegVideo) Stream(onFirstFrame func()) {
hasConsumer := false
defer func() {
if r := recover(); r != nil {
fmt.Println("go-flutter/plugins/video_player: recover: ", r)
f.Cancel()
}
}()
webcam, err := gocv.OpenVideoCapture(0)
// webcam, err := gocv.OpenVideoCapture(f.source)
defer webcam.Close()
if err != nil {
fmt.Printf("Error opening video capture device: %v\n", f.source)
return
}
img := gocv.NewMat()
rgba := gocv.NewMat()
defer img.Close()
defer rgba.Close()
for webcam.Get(gocv.VideoCapturePosAVIRatio) < 1 && !f.Stopped() {
if ok := webcam.Read(&img); !ok {
break
}
if img.Empty() {
continue
}
time := f.fps * webcam.Get(gocv.VideoCapturePosMsec)
gocv.CvtColor(img, &rgba, gocv.ColorBGRToRGBA)
buffer := rgba.ToBytes()
f.Frames <- &ffmpegFrame{buffer, time}
if !hasConsumer {
f.play()
go func() {
onFirstFrame()
}()
hasConsumer = true
}
}
}
func (f *ffmpegVideo) Bounds() (int, int) {
return f.width, f.height
}
func (f *ffmpegVideo) GetFrameRate() float64 {
return f.fps
}
func (f *ffmpegVideo) Duration() float64 {
return f.duration
}
func (f *ffmpegVideo) play() {
f.Set(playing)
}
func (f *ffmpegVideo) Pause() {
f.Set(paused)
}
func (f *ffmpegVideo) UnPause() {
f.paused <- true
}
func (f *ffmpegVideo) Cancel() {
f.Set(stopped)
}
func (f *ffmpegVideo) Stopped() bool {
return f.Get() == stopped
}
func (f *ffmpegVideo) Get() int32 {
return atomic.LoadInt32(&(f.player.flag))
}
func (f *ffmpegVideo) Set(value int32) {
atomic.StoreInt32(&(f.player.flag), value)
}
func (f *ffmpegVideo) WaitUnPause() {
if f.Get() == paused {
<-f.paused
}
}