commit f40f17a588d372e1659aeb7aeb4321578c4536d9 Author: Martijn Braam Date: Thu Jan 23 18:15:28 2020 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6577148 --- /dev/null +++ b/.gitignore @@ -0,0 +1,91 @@ +# Created by .ignore support plugin (hsz.mobi) +### C template +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +### C template +# Prerequisites + +# Object files + +# Linker output + +# Precompiled Headers + +# Libraries + +# Shared objects (inc. Windows DLLs) + +# Executables + +# Debug files + +# Kernel Module Compile Results + +### CMake template +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps + +/.idea +/cmake-build-debug +/cmake-build-release +*~ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..a240f05 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.14) +project(GTKCamera C) + +set(CMAKE_C_STANDARD 11) + +# Use the package PkgConfig to detect GTK+ headers/library files +FIND_PACKAGE(PkgConfig REQUIRED) +PKG_CHECK_MODULES(GTK3 REQUIRED gtk+-3.0) + +# Setup CMake to use GTK+, tell the compiler where to look for headers +# and to the linker where to look for libraries +INCLUDE_DIRECTORIES(${GTK3_INCLUDE_DIRS}) +LINK_DIRECTORIES(${GTK3_LIBRARY_DIRS}) + +# Add other flags to the compiler +ADD_DEFINITIONS(${GTK3_CFLAGS_OTHER}) + +add_executable(GTKCamera main.c) +target_link_libraries(GTKCamera ${GTK3_LIBRARIES}) \ No newline at end of file diff --git a/camera.css b/camera.css new file mode 100644 index 0000000..224d68f --- /dev/null +++ b/camera.css @@ -0,0 +1,3 @@ +.black { + background: #000000; +} \ No newline at end of file diff --git a/camera.glade b/camera.glade new file mode 100644 index 0000000..c20f4ba --- /dev/null +++ b/camera.glade @@ -0,0 +1,159 @@ + + + + + + False + Camera + + + + + + True + False + + + True + False + vertical + + + True + False + vertical + + + True + False + gtk-missing-image + + + True + True + 0 + + + + + + True + True + 0 + + + + + True + False + + + True + False + 8 + 8 + 10 + + + True + True + True + + + True + False + gtk-preferences + + + + + False + True + 0 + + + + + + + + + + + True + True + 10 + 0 + + + + + 48 + 48 + True + True + True + True + + + True + False + gtk-cdrom + + + + + + False + True + 2 + + + + + True + False + 8 + 8 + 10 + + + + + + + + + + + + True + True + 10 + end + 1 + + + + + False + True + 10 + 1 + + + + + page0 + page0 + + + + + + diff --git a/main.c b/main.c new file mode 100644 index 0000000..51d3e23 --- /dev/null +++ b/main.c @@ -0,0 +1,435 @@ +#include +#include +#include +#include +#include +#include +#include + +#define CLEAR(x) memset(&(x), 0, sizeof(x)) + +enum io_method { + IO_METHOD_READ, + IO_METHOD_MMAP, + IO_METHOD_USERPTR, +}; + +struct buffer { + void *start; + size_t length; +}; + + +struct buffer *buffers; +static unsigned int n_buffers; +static char *dev_name; +static enum io_method io = IO_METHOD_MMAP; +static int force_format_width = 640; +static int force_format_height = 480; +GObject *preview_image; + + +static int xioctl(int fd, int request, void *arg) { + int r; + do r = ioctl(fd, request, arg); + while (-1 == r && EINTR == errno); + return r; +} + +static void errno_exit(const char *s) { + fprintf(stderr, "%s error %d, %s\\n", s, errno, strerror(errno)); + exit(EXIT_FAILURE); +} + +static void start_capturing(int fd) { + unsigned int i; + enum v4l2_buf_type type; + + switch (io) { + case IO_METHOD_READ: + /* Nothing to do. */ + break; + + case IO_METHOD_MMAP: + for (i = 0; i < n_buffers; ++i) { + struct v4l2_buffer buf; + + CLEAR(buf); + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + buf.memory = V4L2_MEMORY_MMAP; + buf.index = i; + + if (-1 == xioctl(fd, VIDIOC_QBUF, &buf)) + errno_exit("VIDIOC_QBUF"); + } + type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + if (-1 == xioctl(fd, VIDIOC_STREAMON, &type)) + errno_exit("VIDIOC_STREAMON"); + break; + + case IO_METHOD_USERPTR: + for (i = 0; i < n_buffers; ++i) { + struct v4l2_buffer buf; + + CLEAR(buf); + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + buf.memory = V4L2_MEMORY_USERPTR; + buf.index = i; + buf.m.userptr = (unsigned long) buffers[i].start; + buf.length = buffers[i].length; + + if (-1 == xioctl(fd, VIDIOC_QBUF, &buf)) + errno_exit("VIDIOC_QBUF"); + } + type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + if (-1 == xioctl(fd, VIDIOC_STREAMON, &type)) + errno_exit("VIDIOC_STREAMON"); + break; + } +} + +static void init_mmap(int fd) { + struct v4l2_requestbuffers req; + + CLEAR(req); + + req.count = 4; + req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + req.memory = V4L2_MEMORY_MMAP; + + if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) { + if (EINVAL == errno) { + fprintf(stderr, "%s does not support " + "memory mappingn", dev_name); + exit(EXIT_FAILURE); + } else { + errno_exit("VIDIOC_REQBUFS"); + } + } + + if (req.count < 2) { + fprintf(stderr, "Insufficient buffer memory on %s\\n", + dev_name); + exit(EXIT_FAILURE); + } + + buffers = calloc(req.count, sizeof(*buffers)); + + if (!buffers) { + fprintf(stderr, "Out of memory\\n"); + exit(EXIT_FAILURE); + } + + for (n_buffers = 0; n_buffers < req.count; ++n_buffers) { + struct v4l2_buffer buf; + + CLEAR(buf); + + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + buf.memory = V4L2_MEMORY_MMAP; + buf.index = n_buffers; + + if (-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf)) + errno_exit("VIDIOC_QUERYBUF"); + + buffers[n_buffers].length = buf.length; + buffers[n_buffers].start = + mmap(NULL /* start anywhere */, + buf.length, + PROT_READ | PROT_WRITE /* required */, + MAP_SHARED /* recommended */, + fd, buf.m.offset); + + if (MAP_FAILED == buffers[n_buffers].start) + errno_exit("mmap"); + } +} + +static void init_device(int fd) { + struct v4l2_capability cap; + struct v4l2_cropcap cropcap; + struct v4l2_crop crop; + struct v4l2_format fmt; + unsigned int min; + + if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) { + if (EINVAL == errno) { + fprintf(stderr, "%s is no V4L2 device\\n", + dev_name); + exit(EXIT_FAILURE); + } else { + errno_exit("VIDIOC_QUERYCAP"); + } + } + + if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { + fprintf(stderr, "%s is no video capture device\\n", + dev_name); + exit(EXIT_FAILURE); + } + + switch (io) { + case IO_METHOD_READ: + if (!(cap.capabilities & V4L2_CAP_READWRITE)) { + fprintf(stderr, "%s does not support read i/o\\n", + dev_name); + exit(EXIT_FAILURE); + } + break; + + case IO_METHOD_MMAP: + case IO_METHOD_USERPTR: + if (!(cap.capabilities & V4L2_CAP_STREAMING)) { + fprintf(stderr, "%s does not support streaming i/o\\n", + dev_name); + exit(EXIT_FAILURE); + } + break; + } + + + /* Select video input, video standard and tune here. */ + + + CLEAR(cropcap); + + cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + + if (0 == xioctl(fd, VIDIOC_CROPCAP, &cropcap)) { + crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + crop.c = cropcap.defrect; /* reset to default */ + + if (-1 == xioctl(fd, VIDIOC_S_CROP, &crop)) { + switch (errno) { + case EINVAL: + /* Cropping not supported. */ + break; + default: + /* Errors ignored. */ + break; + } + } + } else { + /* Errors ignored. */ + } + + + CLEAR(fmt); + + fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + if (force_format_width > 0) { + fmt.fmt.pix.width = force_format_width; + fmt.fmt.pix.height = force_format_height; + fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24; + fmt.fmt.pix.field = V4L2_FIELD_INTERLACED; + printf("Requesting a cooler format!\n"); + + if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt)) + errno_exit("VIDIOC_S_FMT"); + + /* Note VIDIOC_S_FMT may change width and height. */ + } else { + /* Preserve original settings as set by v4l2-ctl for example */ + if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt)) + errno_exit("VIDIOC_G_FMT"); + } + + /* Buggy driver paranoia. */ + min = fmt.fmt.pix.width * 2; + if (fmt.fmt.pix.bytesperline < min) + fmt.fmt.pix.bytesperline = min; + min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height; + if (fmt.fmt.pix.sizeimage < min) + fmt.fmt.pix.sizeimage = min; + + switch (io) { + case IO_METHOD_READ: + //init_read(fmt.fmt.pix.sizeimage); + break; + + case IO_METHOD_MMAP: + init_mmap(fd); + break; + + case IO_METHOD_USERPTR: + //init_userp(fmt.fmt.pix.sizeimage); + break; + } +} + +static void process_image(const void *p, int size) { + GdkPixbuf *pixbuf; + pixbuf = gdk_pixbuf_new_from_data(p, GDK_COLORSPACE_RGB, FALSE, 8, 640, 480, 2 * 640, NULL, NULL); + gtk_image_set_from_pixbuf(preview_image, pixbuf); +} + +static int read_frame(int fd) { + struct v4l2_buffer buf; + unsigned int i; + + switch (io) { + case IO_METHOD_READ: + if (-1 == read(fd, buffers[0].start, buffers[0].length)) { + switch (errno) { + case EAGAIN: + return 0; + + case EIO: + /* Could ignore EIO, see spec. */ + + /* fall through */ + + default: + errno_exit("read"); + } + } + + process_image(buffers[0].start, buffers[0].length); + break; + + case IO_METHOD_MMAP: + CLEAR(buf); + + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + buf.memory = V4L2_MEMORY_MMAP; + + if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) { + switch (errno) { + case EAGAIN: + return 0; + + case EIO: + /* Could ignore EIO, see spec. */ + + /* fall through */ + + default: + errno_exit("VIDIOC_DQBUF"); + } + } + + //assert(buf.index < n_buffers); + + process_image(buffers[buf.index].start, buf.bytesused); + + if (-1 == xioctl(fd, VIDIOC_QBUF, &buf)) + errno_exit("VIDIOC_QBUF"); + break; + + case IO_METHOD_USERPTR: + CLEAR(buf); + + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + buf.memory = V4L2_MEMORY_USERPTR; + + if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) { + switch (errno) { + case EAGAIN: + return 0; + + case EIO: + /* Could ignore EIO, see spec. */ + + /* fall through */ + + default: + errno_exit("VIDIOC_DQBUF"); + } + } + + for (i = 0; i < n_buffers; ++i) + if (buf.m.userptr == (unsigned long) buffers[i].start + && buf.length == buffers[i].length) + break; + + //assert(i < n_buffers); + + process_image((void *) buf.m.userptr, buf.bytesused); + + if (-1 == xioctl(fd, VIDIOC_QBUF, &buf)) + errno_exit("VIDIOC_QBUF"); + break; + } + + return 1; +} + +static void get_frame(int fd) { + for (;;) { + fd_set fds; + struct timeval tv; + int r; + + FD_ZERO(&fds); + FD_SET(fd, &fds); + + /* Timeout. */ + tv.tv_sec = 2; + tv.tv_usec = 0; + + r = select(fd + 1, &fds, NULL, NULL, &tv); + + if (-1 == r) { + if (EINTR == errno) + continue; + errno_exit("select"); + } + + if (0 == r) { + fprintf(stderr, "select timeout\\n"); + exit(EXIT_FAILURE); + } + + if (read_frame(fd)) + break; + /* EAGAIN - continue select loop. */ + } +} + +int main(int argc, + char *argv[]) { + GtkBuilder *builder; + GObject *window; + GObject *preview_box; + GError *error = NULL; + + gtk_init(&argc, &argv); + + /* Construct a GtkBuilder instance and load our UI description */ + builder = gtk_builder_new(); + if (gtk_builder_add_from_file(builder, "camera.glade", &error) == 0) { + g_printerr("Error loading file: %s\n", error->message); + g_clear_error(&error); + return 1; + } + + /* Connect signal handlers to the constructed widgets. */ + window = gtk_builder_get_object(builder, "window"); + preview_box = gtk_builder_get_object(builder, "preview_box"); + preview_image = gtk_builder_get_object(builder, "preview"); + g_signal_connect (window, "destroy", G_CALLBACK(gtk_main_quit), NULL); + + /* Load the css */ + GtkCssProvider *provider = gtk_css_provider_new(); + gtk_css_provider_load_from_path(provider, "camera.css", NULL); + GtkStyleContext *context; + context = gtk_widget_get_style_context(preview_box); + gtk_style_context_add_provider(context, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_USER); + + /* Grab a frame from the camera */ + int fd; + fd = open("/dev/video0", O_RDWR); + if (fd == -1) { + g_printerr("Error opening video device: /dev/video0\n"); + return 1; + } + init_device(fd); + start_capturing(fd); + get_frame(fd); + + /* Show application */ + gtk_widget_show(window); + gtk_main(); + + return 0; +} \ No newline at end of file