Added main loop for loading new frames
This commit is contained in:
parent
6190add3bc
commit
38ec7621fc
47
main.c
47
main.c
|
@ -19,6 +19,7 @@ struct buffer {
|
|||
};
|
||||
|
||||
struct buffer *buffers;
|
||||
static int *outbuffer;
|
||||
static unsigned int n_buffers;
|
||||
static char *rear_dev_name;
|
||||
static char *front_dev_name;
|
||||
|
@ -81,7 +82,7 @@ start_capturing(int fd)
|
|||
.memory = V4L2_MEMORY_USERPTR,
|
||||
.index = i,
|
||||
};
|
||||
buf.m.userptr = (unsigned long)buffers[i].start;
|
||||
buf.m.userptr = (unsigned long) buffers[i].start;
|
||||
buf.length = buffers[i].length;
|
||||
|
||||
if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) {
|
||||
|
@ -100,7 +101,7 @@ start_capturing(int fd)
|
|||
static void
|
||||
init_mmap(int fd)
|
||||
{
|
||||
struct v4l2_requestbuffers req = { 0 };
|
||||
struct v4l2_requestbuffers req = {0};
|
||||
req.count = 4;
|
||||
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||
req.memory = V4L2_MEMORY_MMAP;
|
||||
|
@ -195,7 +196,7 @@ init_device(int fd)
|
|||
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
|
||||
};
|
||||
|
||||
struct v4l2_crop crop = { 0 };
|
||||
struct v4l2_crop crop = {0};
|
||||
if (xioctl(fd, VIDIOC_CROPCAP, &cropcap) == 0) {
|
||||
crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||
crop.c = cropcap.defrect; /* reset to default */
|
||||
|
@ -245,8 +246,10 @@ init_device(int fd)
|
|||
g_printerr("Driver returned %dx%d fmt %d\n",
|
||||
fmt.fmt.pix.width, fmt.fmt.pix.height,
|
||||
fmt.fmt.pix.pixelformat);
|
||||
|
||||
preview_width = fmt.fmt.pix.width;
|
||||
preview_height = fmt.fmt.pix.height;
|
||||
}
|
||||
preview_fmt = fmt.fmt.pix.pixelformat;
|
||||
|
||||
/* Buggy driver paranoia. */
|
||||
unsigned int min = fmt.fmt.pix.width * 2;
|
||||
|
@ -274,17 +277,19 @@ init_device(int fd)
|
|||
}
|
||||
|
||||
static void
|
||||
process_image(const void *p, int size)
|
||||
process_image(const int *p, int size)
|
||||
{
|
||||
GdkPixbuf *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);
|
||||
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 = { 0 };
|
||||
struct v4l2_buffer buf = {0};
|
||||
|
||||
switch (io) {
|
||||
case IO_METHOD_READ:
|
||||
|
@ -343,7 +348,7 @@ read_frame(int fd)
|
|||
}
|
||||
unsigned int i;
|
||||
for (i = 0; i < n_buffers; ++i) {
|
||||
if (buf.m.userptr == (unsigned long)buffers[i].start
|
||||
if (buf.m.userptr == (unsigned long) buffers[i].start
|
||||
&& buf.length == buffers[i].length) {
|
||||
break;
|
||||
}
|
||||
|
@ -351,7 +356,7 @@ read_frame(int fd)
|
|||
|
||||
//assert(i < n_buffers);
|
||||
|
||||
process_image((void *)buf.m.userptr, buf.bytesused);
|
||||
process_image((void *) buf.m.userptr, buf.bytesused);
|
||||
if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) {
|
||||
errno_exit("VIDIOC_QBUF");
|
||||
}
|
||||
|
@ -361,7 +366,7 @@ read_frame(int fd)
|
|||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
static gboolean
|
||||
get_frame(int fd)
|
||||
{
|
||||
while (1) {
|
||||
|
@ -393,21 +398,25 @@ get_frame(int fd)
|
|||
}
|
||||
/* EAGAIN - continue select loop. */
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static int
|
||||
config_ini_handler(void *user, const char *section, const char *name,
|
||||
const char *value) {
|
||||
const char *value)
|
||||
{
|
||||
if (strcmp(section, "preview") == 0) {
|
||||
if (strcmp(name, "width") == 0) {
|
||||
preview_width = strtol(value, NULL, 10);
|
||||
} else if (strcmp(name, "height") == 0) {
|
||||
preview_height = strtol(value, NULL, 10);
|
||||
} else if (strcmp(name, "fmt") == 0) {
|
||||
if (strcmp(value, "RGB") == 0){
|
||||
if (strcmp(value, "RGB") == 0) {
|
||||
preview_fmt = V4L2_PIX_FMT_RGB24;
|
||||
} else if (strcmp(value, "UYVY8") == 0) {
|
||||
} else if (strcmp(value, "UYVY") == 0) {
|
||||
preview_fmt = V4L2_PIX_FMT_UYVY;
|
||||
} else if (strcmp(value, "YUYV") == 0) {
|
||||
preview_fmt = V4L2_PIX_FMT_YUYV;
|
||||
} else if (strcmp(value, "JPEG") == 0) {
|
||||
preview_fmt = V4L2_PIX_FMT_JPEG;
|
||||
} else if (strcmp(value, "NV12") == 0) {
|
||||
|
@ -454,8 +463,8 @@ main(int argc, char *argv[])
|
|||
GError *error = NULL;
|
||||
gtk_init(&argc, &argv);
|
||||
GtkBuilder *builder = gtk_builder_new();
|
||||
char* glade_file = "/usr/share/camera/ui/camera.glade";
|
||||
if (access( "camera.glade", F_OK) != -1) {
|
||||
char *glade_file = "/usr/share/camera/ui/camera.glade";
|
||||
if (access("camera.glade", F_OK) != -1) {
|
||||
glade_file = "camera.glade";
|
||||
}
|
||||
if (gtk_builder_add_from_file(builder, glade_file, &error) == 0) {
|
||||
|
@ -470,7 +479,7 @@ main(int argc, char *argv[])
|
|||
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
|
||||
|
||||
GtkCssProvider *provider = gtk_css_provider_new();
|
||||
if (access( "camera.css", F_OK) != -1) {
|
||||
if (access("camera.css", F_OK) != -1) {
|
||||
gtk_css_provider_load_from_path(provider, "camera.css", NULL);
|
||||
} else {
|
||||
gtk_css_provider_load_from_path(provider, "/usr/share/camera/ui/camera.css", NULL);
|
||||
|
@ -502,7 +511,9 @@ main(int argc, char *argv[])
|
|||
|
||||
init_device(fd);
|
||||
start_capturing(fd);
|
||||
get_frame(fd);
|
||||
|
||||
// Get a new frame every 34ms ~30fps
|
||||
g_timeout_add(34, get_frame, fd);
|
||||
|
||||
gtk_widget_show(window);
|
||||
gtk_main();
|
||||
|
|
Loading…
Reference in New Issue