#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <asm/types.h>
#include <linux/videodev2.h>
#define CLEAR(x) memset (&(x), 0, sizeof (x))
struct buffer
{
void * start;
size_t length;
};
static char * dev_name= "/dev/video0";
static int fd= -1;
struct buffer * buffers= NULL;
FILE *file_fd;
static unsigned long file_length;
static unsigned char *file_name;
int main (int argc,char ** argv)
{
printf("1\n"); struct v4l2_capability cap;
printf("2\n"); struct v4l2_format fmt;
printf("3\n");
file_fd = fopen("test.jpg", "w");
printf("4\n"); fd = open (dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
printf("fd: %d\n",fd);
ioctl (fd, VIDIOC_QUERYCAP, &cap);
printf("6\n");
CLEAR (fmt);
printf("7\n"); fmt.type= V4L2_BUF_TYPE_VIDEO_CAPTURE;
printf("8\n"); fmt.fmt.pix.width= 640;printf("9\n");
fmt.fmt.pix.height= 480;
printf("10\n"); fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
printf("11\n"); fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
printf("12\n"); ioctl (fd, VIDIOC_S_FMT, &fmt);
printf("13\n"); file_length = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;printf("14\n");
buffers = calloc (1, sizeof (*buffers));
printf("15\n");buffers[0].length = file_length;
printf("16\n"); buffers[0].start = malloc (file_length);
printf("17\n"); for (;;)
{
printf("-1\n"); fd_set fds;
printf("-2\n"); struct timeval tv;
printf("-3\n"); int r;
printf("-4\n"); FD_ZERO (&fds);
printf("-5\n"); FD_SET (fd, &fds);
/* Timeout. */
printf("-6\n"); tv.tv_sec = 3;
printf("-7\n"); tv.tv_usec = 0;
printf("-8\n"); r = select (fd + 1, &fds, NULL, NULL, &tv);
printf("-9\n"); if (-1 == r)
{printf("-10\n");
if (EINTR == errno)
continue;printf("-11\n");
printf ("select");
}
if (0 == r)
{printf("-12\n");
fprintf (stderr, "select timeout\n");
exit (EXIT_FAILURE);
}
printf("asdfasdfsad\n"); if (read (fd, buffers[0].start, buffers[0].length))
break;
printf("123214234123\n"); }
printf("18\n"); fwrite(buffers[0].start, buffers[0].length, 1, file_fd);
printf("19\n");
free (buffers[0].start);
printf("20\n"); close (fd);printf("21\n");
fclose (file_fd);
printf("22\n"); exit (EXIT_SUCCESS);
printf("19\n"); return 0;
}