int readframe(void) { struct v4l2_buffer buf; unsigned int i; 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: default: return errnoexit ("VIDIOC_DQBUF"); } } assert (buf.index < n_buffers); processimage (buffers[buf.index].start); if (-1 == xioctl (fd, VIDIOC_QBUF, &buf)) return errnoexit ("VIDIOC_QBUF"); return 1; }
int read_frame(int fd, buffer* frame_buffers, int width, int height, int* rgb_buffer, int* y_buffer) { struct v4l2_buffer buf; 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: default: return errnoexit("VIDIOC_DQBUF"); } } assert(buf.index < BUFFER_COUNT); yuyv422_to_argb(frame_buffers[buf.index].start, width, height, rgb_buffer, y_buffer); if(-1 == xioctl(fd, VIDIOC_QBUF, &buf)) { return errnoexit("VIDIOC_QBUF"); } return 1; }
int startcapturing(void) { unsigned int i; enum v4l2_buf_type type; 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)) return errnoexit ("VIDIOC_QBUF"); } type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (-1 == xioctl (fd, VIDIOC_STREAMON, &type)) return errnoexit ("VIDIOC_STREAMON"); return SUCCESS_LOCAL; }
int readframe(int i) { struct v4l2_buffer buf; CLEAR (buf); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; if (-1 == xioctl (fd[i], VIDIOC_DQBUF, &buf)) { switch (errno) { case EAGAIN: return 0; case EIO: default: return errnoexit ("VIDIOC_DQBUF"); } } assert (buf.index < n_buffers[i]); processimage (i, buffers[i][buf.index].start, buffers[i][buf.index].length); if (-1 == xioctl (fd[i], VIDIOC_QBUF, &buf)){ LOGE("in readframe"); return errnoexit ("VIDIOC_QBUF"); } return 1; }
int initmmap(void) { 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) { LOGE("%s does not support memory mapping", dev_name); return ERROR_LOCAL; } else { return errnoexit ("VIDIOC_REQBUFS"); } } if (req.count < 2) { LOGE("Insufficient buffer memory on %s", dev_name); return ERROR_LOCAL; } buffers = calloc (req.count, sizeof (*buffers)); if (!buffers) { LOGE("Out of memory"); return ERROR_LOCAL; } 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)) return errnoexit ("VIDIOC_QUERYBUF"); buffers[n_buffers].length = buf.length; buffers[n_buffers].start = mmap (NULL , buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf.m.offset); if (MAP_FAILED == buffers[n_buffers].start) return errnoexit ("mmap"); } return SUCCESS_LOCAL; }
int readframeonce(void) { for (;;) { fd_set fds; struct timeval tv; int r; FD_ZERO(&fds); FD_SET(fd, &fds); tv.tv_sec = 2; tv.tv_usec = 0; r = select(fd + 1, &fds, NULL, NULL, &tv); if (-1 == r) { if (EINTR == errno) continue; return errnoexit("select"); } if (0 == r) { LOGE("select timeout"); return ERROR_LOCAL; } if (readframe() == 1) break; } return SUCCESS_LOCAL; }
int say(int socket, char *s) { int result = send(socket, s, strlen(s), 0); if (result == -1) errnoexit("Error talking to the client"); return result; }
void process_camera(int fd, buffer* frame_buffers, int width, int height, int* rgb_buffer, int* ybuf) { if(fd == -1) { return; } for(;;) { fd_set fds; FD_ZERO(&fds); FD_SET(fd, &fds); struct timeval tv; tv.tv_sec = 2; tv.tv_usec = 0; int result = select(fd + 1, &fds, NULL, NULL, &tv); if(-1 == result) { if(EINTR == errno) { continue; } errnoexit("select"); } else if(0 == result) { LOGE("select timeout"); } if(read_frame(fd, frame_buffers, width, height, rgb_buffer, ybuf) == 1) { break; } } }
int stop_capturing(int fd) { enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if(-1 != fd && -1 == xioctl(fd, VIDIOC_STREAMOFF, &type)) { return errnoexit("VIDIOC_STREAMOFF"); } return SUCCESS_LOCAL; }
int closedevice(void) { if (-1 == close(fd)) { fd = -1; return errnoexit("close"); } fd = -1; return SUCCESS_LOCAL; }
int closedevice(int i) { if (-1 == close (fd[i])){ fd[i] = -1; return errnoexit ("close"); } fd[i] = -1; return SUCCESS_LOCAL; }
int uninitdevice(void) { unsigned int i; for (i = 0; i < n_buffers; ++i) if (-1 == munmap(buffers[i].start, buffers[i].length)) return errnoexit("munmap"); free(buffers); return SUCCESS_LOCAL; }
int stopcapturing(void) { enum v4l2_buf_type type; type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (-1 == xioctl(fd, VIDIOC_STREAMOFF, &type)) return errnoexit("VIDIOC_STREAMOFF"); return SUCCESS_LOCAL; }
int initdevice(void) { 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) { LOGE("%s is no V4L2 device", dev_name); return ERROR_LOCAL; } else { return errnoexit ("VIDIOC_QUERYCAP"); } } if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { LOGE("%s is no video capture device", dev_name); return ERROR_LOCAL; } if (!(cap.capabilities & V4L2_CAP_STREAMING)) { LOGE("%s does not support streaming i/o", dev_name); return ERROR_LOCAL; } 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; if (-1 == xioctl (fd, VIDIOC_S_CROP, &crop)) { switch (errno) { case EINVAL: break; default: break; } } } else { } CLEAR (fmt); fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; fmt.fmt.pix.width = IMG_WIDTH; fmt.fmt.pix.height = IMG_HEIGHT; fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; fmt.fmt.pix.field = V4L2_FIELD_INTERLACED; if (-1 == xioctl (fd, VIDIOC_S_FMT, &fmt)) return errnoexit ("VIDIOC_S_FMT"); 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; return initmmap (); }
void error(char *msg) { errnoexit(msg); exit(1); }