wip video device
This commit is contained in:
+207
@@ -0,0 +1,207 @@
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/videodev2.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "logs.h"
|
||||
#include "types.h"
|
||||
#include "video.h"
|
||||
|
||||
static VideoDevice open_device(char *name) {
|
||||
VideoDevice device;
|
||||
|
||||
device.name = name;
|
||||
device.error = false;
|
||||
device.fd = -1;
|
||||
|
||||
device.fd = open(name, O_RDWR);
|
||||
if (device.fd == -1) {
|
||||
log_warn("(%s) Cannot open device", name);
|
||||
device.error = true;
|
||||
}
|
||||
|
||||
return device;
|
||||
}
|
||||
|
||||
static bool check_device_caps(VideoDevice *device) {
|
||||
struct v4l2_capability cap;
|
||||
|
||||
memset(&cap, 0, sizeof(cap));
|
||||
|
||||
if (ioctl(device->fd, VIDIOC_QUERYCAP, &cap) == -1) {
|
||||
if (EINVAL == errno) {
|
||||
log_warn("(%s) Not a V4L2 device", device->name);
|
||||
} else {
|
||||
log_error("(%s) VIDIOC_QUERYCAP unkown error", device->name);
|
||||
}
|
||||
device->error = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
|
||||
log_warn("(%s) Not a video capture device", device->name);
|
||||
device->error = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
|
||||
log_warn("(%s) No streaming i/o support", device->name);
|
||||
device->error = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool set_device_format(VideoDevice *device, unsigned int preferred_width,
|
||||
unsigned int preferred_height) {
|
||||
struct v4l2_format fmt;
|
||||
|
||||
memset(&fmt, 0, sizeof(fmt));
|
||||
|
||||
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||
fmt.fmt.pix.width = preferred_width;
|
||||
fmt.fmt.pix.height = preferred_height;
|
||||
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGRX32;
|
||||
fmt.fmt.pix.field = V4L2_FIELD_NONE;
|
||||
|
||||
if (ioctl(device->fd, VIDIOC_S_FMT, &fmt) == -1) {
|
||||
log_warn("(%s) Format set failed", device->name);
|
||||
device->error = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
device->width = fmt.fmt.pix.width;
|
||||
device->height = fmt.fmt.pix.height;
|
||||
device->pixelformat = fmt.fmt.pix.pixelformat;
|
||||
device->bytesperline = fmt.fmt.pix.bytesperline;
|
||||
|
||||
log_info("(%s) Format fourcc: %c%c%c%c", device->name,
|
||||
fmt.fmt.pix.pixelformat, fmt.fmt.pix.pixelformat >> 8,
|
||||
fmt.fmt.pix.pixelformat >> 16, fmt.fmt.pix.pixelformat >> 24);
|
||||
log_info("(%s) Resolution: %dx%d", device->name, fmt.fmt.pix.width,
|
||||
fmt.fmt.pix.height);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool request_buffers(VideoDevice *device) {
|
||||
struct v4l2_requestbuffers reqbuf;
|
||||
|
||||
memset(&reqbuf, 0, sizeof(reqbuf));
|
||||
|
||||
reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||
reqbuf.memory = V4L2_MEMORY_MMAP;
|
||||
reqbuf.count = 1;
|
||||
|
||||
if (ioctl(device->fd, VIDIOC_REQBUFS, &reqbuf) == -1) {
|
||||
if (errno == EINVAL) {
|
||||
log_info("(%s) Video capturing or DMABUF streaming is not supported",
|
||||
device->name);
|
||||
} else {
|
||||
log_error("(%s) VIDIOC_REQBUFS unkown error", device->name);
|
||||
}
|
||||
device->error = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
log_info("(%s) V4L2 Buffer Count: %d", device->name, reqbuf.count);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool export_buffer(VideoDevice *device) {
|
||||
struct v4l2_exportbuffer expbuf;
|
||||
|
||||
device->exp_fd = -1;
|
||||
|
||||
memset(&expbuf, 0, sizeof(expbuf));
|
||||
|
||||
expbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||
expbuf.index = 0; /// '0' for one buffer
|
||||
expbuf.flags = O_RDONLY;
|
||||
|
||||
if (ioctl(device->fd, VIDIOC_EXPBUF, &expbuf) == -1) {
|
||||
log_error("(%s) VIDIOC_EXPBUF error", device->name);
|
||||
device->error = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
device->exp_fd = expbuf.fd;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static const enum v4l2_buf_type buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||
|
||||
static bool open_stream(VideoDevice *device) {
|
||||
if (ioctl(device->fd, VIDIOC_STREAMON, &buf_type) == -1) {
|
||||
log_error("(%s) VIDIOC_STREAMON error", device->name);
|
||||
device->error = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void create_image_buffer(VideoDevice *device) {
|
||||
memset(&device->buf, 0, sizeof(device->buf));
|
||||
|
||||
device->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||
device->buf.memory = V4L2_MEMORY_MMAP;
|
||||
device->buf.index = 0;
|
||||
|
||||
ioctl(device->fd, VIDIOC_QBUF, &device->buf);
|
||||
}
|
||||
|
||||
static void close_stream(VideoDevice device) {
|
||||
ioctl(device.fd, VIDIOC_STREAMOFF, &buf_type);
|
||||
}
|
||||
|
||||
VideoDevice video_init(char *name, unsigned int preferred_width,
|
||||
unsigned int preferred_height) {
|
||||
VideoDevice device;
|
||||
|
||||
device = open_device(name);
|
||||
|
||||
if (device.error) {
|
||||
return device;
|
||||
}
|
||||
|
||||
if (!check_device_caps(&device)) {
|
||||
return device;
|
||||
}
|
||||
|
||||
if (!set_device_format(&device, preferred_width, preferred_height)) {
|
||||
return device;
|
||||
}
|
||||
|
||||
if (!request_buffers(&device)) {
|
||||
return device;
|
||||
}
|
||||
|
||||
if (!export_buffer(&device)) {
|
||||
return device;
|
||||
}
|
||||
|
||||
if (!open_stream(&device)) {
|
||||
return device;
|
||||
}
|
||||
|
||||
create_image_buffer(&device);
|
||||
|
||||
return device;
|
||||
}
|
||||
|
||||
void video_read(VideoDevice device) {
|
||||
ioctl(device.fd, VIDIOC_DQBUF, &device.buf);
|
||||
ioctl(device.fd, VIDIOC_QBUF, &device.buf);
|
||||
// TODO check for error and close
|
||||
}
|
||||
|
||||
void video_free(VideoDevice device) {
|
||||
close_stream(device);
|
||||
close(device.exp_fd);
|
||||
close(device.fd);
|
||||
}
|
||||
Reference in New Issue
Block a user