forked from peterhagelund/go-v4l2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv4l2_test.go
211 lines (201 loc) · 5.18 KB
/
v4l2_test.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
package v4l2
import (
"bytes"
"image"
"testing"
"unsafe"
_ "image/jpeg"
"golang.org/x/sys/unix"
)
func TestQueryCapabilities(t *testing.T) {
fd, err := unix.Open("/dev/video0", unix.O_RDWR, 0)
if err != nil {
t.Fatal("unable to open device")
}
defer unix.Close(fd)
capability, err := QueryCapabilities(fd)
if err != nil {
t.Fatal("unable to query capabilities")
}
if capability == nil {
t.Fatal("nil capability returned")
}
driver := BytesToString(capability.Driver[:])
if len(driver) == 0 {
t.Fatal("no driver")
}
card := BytesToString(capability.Card[:])
if len(card) == 0 {
t.Fatal("no card")
}
busInfo := BytesToString(capability.BusInfo[:])
if len(busInfo) == 0 {
t.Fatal("no bus info")
}
if capability.Capabilities&CapVideoCapture == 0x00000000 {
t.Fatal("video capture not a capability")
}
}
func TestEnumFormats(t *testing.T) {
fd, err := unix.Open("/dev/video0", unix.O_RDWR, 0)
if err != nil {
t.Fatal("unable to open device")
}
defer unix.Close(fd)
fmtDescs, err := EnumFormats(fd, BufTypeVideoCapture)
if err != nil {
t.Fatal("uanble to enumerate formats")
}
if fmtDescs == nil {
t.Fatal("nil format descriptors returned")
}
if len(fmtDescs) == 0 {
t.Fatal("no format descriptors returned")
}
found := false
for _, fmtDesc := range fmtDescs {
description := BytesToString(fmtDesc.Description[:])
if description == "JFIF JPEG" {
found = true
break
}
}
if !found {
t.Fatal("well-known format not present")
}
}
func TestEnumFrameSizes(t *testing.T) {
fd, err := unix.Open("/dev/video0", unix.O_RDWR, 0)
if err != nil {
t.Fatal("unable to open device")
}
defer unix.Close(fd)
frameSizeEnums, err := EnumFrameSizes(fd, PixFmtJPEG)
if err != nil {
t.Fatal("unable to enumerate frame sizes")
}
if frameSizeEnums == nil {
t.Fatal("nil frame size enums returned")
}
if len(frameSizeEnums) == 0 {
t.Fatal("no frame size enums returned")
}
for _, frameSizeEnum := range frameSizeEnums {
if frameSizeEnum.Type == FrmSizeTypeDiscrete {
frameSizeDiscrete := (*FrameSizeDiscrete)(unsafe.Pointer(&frameSizeEnum.M))
if frameSizeDiscrete.Width == 0 {
t.Fatal("zero width frame size")
}
if frameSizeDiscrete.Height == 0 {
t.Fatal("zero height frame size")
}
} else {
frameSizeStepwise := (*FrameSizeStepwise)(unsafe.Pointer(&frameSizeEnum.M))
if frameSizeStepwise.MinHeight == 0 {
t.Fatal("zero min height")
}
if frameSizeStepwise.MaxHeight == 0 {
t.Fatal("zero max height")
}
if frameSizeStepwise.MinWidth == 0 {
t.Fatal("zero min width")
}
if frameSizeStepwise.MaxWidth == 0 {
t.Fatal("zero max width")
}
}
}
}
func TestSetFormat(t *testing.T) {
fd, err := unix.Open("/dev/video0", unix.O_RDWR, 0)
if err != nil {
t.Fatal("unable to open device")
}
defer unix.Close(fd)
width, height, err := SetFormat(fd, BufTypeVideoCapture, PixFmtJPEG, 1024, 768)
if err != nil {
t.Fatal("unable to set format")
}
if width == 0 {
t.Fatal("zero width returned")
}
if height == 0 {
t.Fatal("zero height returned")
}
}
func TestRequestDriverBuffers(t *testing.T) {
fd, err := unix.Open("/dev/video0", unix.O_RDWR, 0)
if err != nil {
t.Fatal("unable to open device")
}
defer unix.Close(fd)
count, err := RequestDriverBuffers(fd, 4, BufTypeVideoCapture, MemoryMmap)
if err != nil {
t.Fatal("unable to request driver buffers")
}
if count == 0 {
t.Fatal("no driver buffers available")
}
_, err = RequestDriverBuffers(fd, 0, BufTypeVideoCapture, MemoryMmap)
if err != nil {
t.Fatal("unable to adjust requested driver buffers down to zero")
}
}
func TestGrabFrame(t *testing.T) {
fd, err := unix.Open("/dev/video0", unix.O_RDWR, 0)
if err != nil {
t.Fatal("unable to open device")
}
defer unix.Close(fd)
if _, _, err := SetFormat(fd, BufTypeVideoCapture, PixFmtJPEG, 1024, 768); err != nil {
t.Fatal("unable to set format")
}
count, err := RequestDriverBuffers(fd, 4, BufTypeVideoCapture, MemoryMmap)
if err != nil {
t.Fatal("unable to request driver buffers")
}
defer RequestDriverBuffers(fd, 0, BufTypeVideoCapture, MemoryMmap)
buffers, err := MmapBuffers(fd, count, BufTypeVideoCapture)
if err != nil {
t.Fatal("unable to mmap buffers")
}
defer MunmapBuffers(buffers)
if err := StreamOn(fd, BufTypeVideoCapture); err != nil {
t.Fatal("unable to turn on streaming")
}
defer StreamOff(fd, BufTypeVideoCapture)
frame, err := GrabFrame(fd, BufTypeVideoCapture, MemoryMmap, buffers)
if err != nil {
t.Fatal("unable to grab frame")
}
if frame == nil {
t.Fatal("nil frame returned")
}
if len(frame) == 0 {
t.Fatal("empty frame returned")
}
buffer := bytes.NewBuffer(frame)
image, name, err := image.Decode(buffer)
if err != nil {
t.Fatal("unable to decode frame")
}
if name != "jpeg" {
t.Fatal("returned frame is not a JPEG image")
}
bounds := image.Bounds()
if bounds.Dx() != 1024 || bounds.Dy() != 768 {
t.Fatal("image has incorrect size")
}
}
func TestBytesToString(t *testing.T) {
b1 := [...]byte{}
s1 := BytesToString(b1[:])
if len(s1) != 0 {
t.Fatal("empty slice did not yield zero length string")
}
b2 := [...]byte{0x41, 0x42, 0x43, 0x31, 0x32, 0x33}
s2 := BytesToString(b2[:])
if s2 != "ABC123" {
t.Fatal("incorrect string returned")
}
}