15 #include "webcamVideoV4L.h"
17 #ifdef HAVE_VIDEO4LINUX
19 #include "webcamVideoCursorV4L.h"
23 #include <sys/ioctl.h>
24 #include <linux/videodev2.h>
34 add_options_for_size(
int fd,
const string &dev,
const char *name,
unsigned width,
unsigned height,
unsigned pixelformat) {
35 struct v4l2_frmivalenum frmivalenum;
36 for (
int k = 0;; k++) {
37 memset(&frmivalenum, 0,
sizeof frmivalenum);
38 frmivalenum.index = k;
39 frmivalenum.pixel_format = pixelformat;
40 frmivalenum.width = width;
41 frmivalenum.height = height;
42 if (ioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmivalenum) == -1) {
46 switch (frmivalenum.type) {
47 case V4L2_FRMIVAL_TYPE_DISCRETE:
48 fps = ((double) frmivalenum.discrete.denominator) / ((double) frmivalenum.discrete.numerator);
51 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
52 case V4L2_FRMIVAL_TYPE_STEPWISE:
55 double max_fps = ((double) frmivalenum.stepwise.max.denominator) / ((double) frmivalenum.stepwise.max.numerator);
65 PT(WebcamVideoV4L) wc = new WebcamVideoV4L;
71 wc->_pformat = pixelformat;
72 wc->_pixel_format =
string((
char*) &pixelformat, 4);
74 WebcamVideoV4L::_all_webcams.push_back(DCAST(
WebcamVideo, wc));
84 void find_all_webcams_v4l() {
85 struct v4l2_capability cap2;
89 pattern.match_files(devs);
90 for (vector_string::iterator it = devs.begin(); it != devs.end(); ++it) {
91 int fd = open(it->c_str(), O_RDWR);
94 if (ioctl(fd, VIDIOC_QUERYCAP, &cap2) != -1) {
95 if ((cap2.capabilities & V4L2_CAP_VIDEO_CAPTURE) &&
96 (cap2.capabilities & V4L2_CAP_STREAMING)) {
97 struct v4l2_fmtdesc fmt;
98 for (
int i = 0;; i++) {
99 memset(&fmt, 0,
sizeof fmt);
101 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
102 if (ioctl(fd, VIDIOC_ENUM_FMT, &fmt) == -1) {
107 switch (fmt.pixelformat) {
109 case V4L2_PIX_FMT_MJPEG:
111 case V4L2_PIX_FMT_YUYV:
112 case V4L2_PIX_FMT_BGR24:
113 case V4L2_PIX_FMT_BGR32:
114 case V4L2_PIX_FMT_RGB24:
115 case V4L2_PIX_FMT_RGB32:
122 struct v4l2_frmsizeenum frmsizeenum;
123 for (
int j = 0;; j++) {
124 memset(&frmsizeenum, 0,
sizeof frmsizeenum);
125 frmsizeenum.index = j;
126 frmsizeenum.pixel_format = fmt.pixelformat;
127 if (ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frmsizeenum) == -1) {
131 switch (frmsizeenum.type) {
132 case V4L2_FRMSIZE_TYPE_DISCRETE:
135 add_options_for_size(fd, *it, (
const char *)cap2.card,
136 frmsizeenum.discrete.width,
137 frmsizeenum.discrete.height,
141 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
147 for (; width <= frmsizeenum.stepwise.max_width; width *= 2) {
149 for (; height <= frmsizeenum.stepwise.max_height; height *= 2) {
151 add_options_for_size(fd, *it, (
const char *)cap2.card, width, height, fmt.pixelformat);
157 case V4L2_FRMSIZE_TYPE_STEPWISE:
160 for (; width <= frmsizeenum.stepwise.max_width; width *= 2) {
162 for (; height <= frmsizeenum.stepwise.max_height; height *= 2) {
163 if ((width - frmsizeenum.stepwise.min_width) % frmsizeenum.stepwise.step_width == 0 &&
164 (height - frmsizeenum.stepwise.min_height) % frmsizeenum.stepwise.step_height == 0) {
166 add_options_for_size(fd, *it, (
const char *)cap2.card, width, height, fmt.pixelformat);
190 return new WebcamVideoCursorV4L(
this);
Allows you to open a webcam or other video capture device as a video stream.
A MovieVideo is actually any source that provides a sequence of video frames.
static int up_to_power_2(int value)
Returns the smallest power of 2 greater than or equal to value.
TypeHandle is the identifier used to differentiate C++ class types.
This class can be used to test for string matches against standard Unix-shell filename globbing conve...