-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathv4l.go
164 lines (144 loc) · 4.07 KB
/
v4l.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
package main
// Using the Video4Linux docs and examples at http://linuxtv.org/downloads/v4l-dvb-apis/.
import (
"log"
"syscall"
"unsafe"
"launchpad.net/gommap"
)
// #include <stdlib.h>
// #include <errno.h>
// #include <string.h>
// #include <sys/select.h>
// #include <linux/videodev2.h>
//
// int selectfd(int fd)
// {
// struct timeval tout = { 2, 0 };
// fd_set fds;
//
// FD_ZERO(&fds);
// FD_SET(fd, &fds);
//
// return select(fd + 1, &fds, NULL, NULL, &tout);
// }
import "C"
func ioctl(fd int, request, argp uintptr) error {
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), request, argp)
if errno != 0 {
return errno
}
return nil
}
// vidCapture captures frames using the Video4Linux API.
// We use select()+mmap() because many drivers can't do read().
func vidCapture() {
fd, err := syscall.Open(*videoSource, syscall.O_RDWR|syscall.O_NONBLOCK, 0)
if err != nil {
log.Println("disabling video: can't open video device file:", err)
return
}
defer syscall.Close(fd)
// Check camera capabilities.
var cap C.struct_v4l2_capability
err = ioctl(fd, C.VIDIOC_QUERYCAP, uintptr(unsafe.Pointer(&cap)))
if err != nil {
log.Fatalf("%s is not a V4L2 device: %v", *videoSource, err)
}
if (cap.capabilities & C.V4L2_CAP_VIDEO_CAPTURE) == 0 {
log.Fatalf("%s does not support video capture", *videoSource)
}
if (cap.capabilities & C.V4L2_CAP_STREAMING) == 0 {
log.Fatalf("%s does not support streaming io", *videoSource)
}
// Ask for MJPEG format.
// TODO encode JPEGs ourselves on cameras which don't do MJPEG
fmt := C.struct_v4l2_format{
_type: C.V4L2_BUF_TYPE_VIDEO_CAPTURE,
}
pix := (*C.struct_v4l2_pix_format)(unsafe.Pointer(&fmt.fmt))
pix.width = 640
pix.height = 480
pix.pixelformat = C.V4L2_PIX_FMT_MJPEG
pix.field = C.V4L2_FIELD_INTERLACED
err = ioctl(fd, C.VIDIOC_S_FMT, uintptr(unsafe.Pointer(&fmt)))
if err != nil {
log.Fatalf("couldn't set capture format: %v", err)
}
// Sort out buffers.
req := C.struct_v4l2_requestbuffers{
count: 4,
_type: C.V4L2_BUF_TYPE_VIDEO_CAPTURE,
memory: C.V4L2_MEMORY_MMAP,
}
err = ioctl(fd, C.VIDIOC_REQBUFS, uintptr(unsafe.Pointer(&req)))
if err != nil {
log.Fatalf("couldn't get capture buffers: %v", err)
}
if req.count < 2 {
log.Fatalf("not enough buffers: %v", err)
}
buffers := make([]gommap.MMap, req.count)
for i := 0; i < len(buffers); i++ {
buf := C.struct_v4l2_buffer{
_type: C.V4L2_BUF_TYPE_VIDEO_CAPTURE,
memory: C.V4L2_MEMORY_MMAP,
index: C.__u32(i),
}
offset := (*C.__u32)(unsafe.Pointer(&buf.m))
err = ioctl(fd, C.VIDIOC_QUERYBUF, uintptr(unsafe.Pointer(&buf)))
if err != nil {
log.Fatalf("couldn't query buffer %d: %v", i, err)
}
buffers[i], err = gommap.MapRegion(uintptr(fd),
int64(*offset), int64(buf.length),
gommap.PROT_READ|gommap.PROT_WRITE,
gommap.MAP_SHARED)
if err != nil {
log.Fatalf("failed to map buffer %d: %v", i, err)
}
}
// Start capture
for i := 0; i < len(buffers); i++ {
buf := C.struct_v4l2_buffer{
_type: C.V4L2_BUF_TYPE_VIDEO_CAPTURE,
memory: C.V4L2_MEMORY_MMAP,
index: C.__u32(i),
}
err = ioctl(fd, C.VIDIOC_QBUF, uintptr(unsafe.Pointer(&buf)))
if err != nil {
log.Fatalf("couldn't queue buffer %d: %v", i, err)
}
}
typ := C.V4L2_BUF_TYPE_VIDEO_CAPTURE
err = ioctl(fd, C.VIDIOC_STREAMON, uintptr(unsafe.Pointer(&typ)))
if err != nil {
log.Fatalf("couldn't start stream: %v", err)
}
// Loop over frames!
for {
errno := C.selectfd(C.int(fd))
if errno == C.EINTR || errno == C.EAGAIN {
continue
}
if errno < 0 {
log.Fatalf("select err: %v", C.GoString(C.strerror(errno)))
}
buf := C.struct_v4l2_buffer{
_type: C.V4L2_BUF_TYPE_VIDEO_CAPTURE,
memory: C.V4L2_MEMORY_MMAP,
}
err = ioctl(fd, C.VIDIOC_DQBUF, uintptr(unsafe.Pointer(&buf)))
if err == syscall.EAGAIN {
continue
}
if err != nil {
log.Fatalf("couldn't dequeue buffer: %v", err)
}
stream.UpdateJPEG(buffers[buf.index][:buf.bytesused])
err = ioctl(fd, C.VIDIOC_QBUF, uintptr(unsafe.Pointer(&buf)))
if err != nil {
log.Fatalf("couldn't queue buffer %d: %v", buf.index, err)
}
}
}