getframe.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include <libmegapixels.h>
  2. #include <stdio.h>
  3. #include <limits.h>
  4. #include <string.h>
  5. #include <linux/videodev2.h>
  6. #include <sys/ioctl.h>
  7. #include <stdlib.h>
  8. #include <sys/types.h>
  9. #include <errno.h>
  10. #include <sys/mman.h>
  11. #include <getopt.h>
  12. #include <ctype.h>
  13. struct buffer {
  14. void *start;
  15. size_t length;
  16. };
  17. struct buffer *buffers;
  18. int
  19. xioctl(int fd, int request, void *arg)
  20. {
  21. int r;
  22. do {
  23. r = ioctl(fd, request, arg);
  24. } while (r == -1 && errno == EINTR);
  25. return r;
  26. }
  27. int
  28. main(int argc, char *argv[])
  29. {
  30. int c;
  31. int camera_id = 0;
  32. long res;
  33. char *end;
  34. char *outfile = NULL;
  35. while ((c = getopt(argc, argv, "c:o:")) != -1) {
  36. switch (c) {
  37. case 'c':
  38. res = strtol(optarg, &end, 10);
  39. if (end == optarg || end == NULL || *end != '\0') {
  40. fprintf(stderr, "Invalid number for -c\n");
  41. return 1;
  42. }
  43. camera_id = (int) res;
  44. break;
  45. case 'o':
  46. outfile = optarg;
  47. break;
  48. case '?':
  49. if (optopt == 'd' || optopt == 'l') {
  50. fprintf(stderr, "Option -%c requires an argument.\n", optopt);
  51. } else if (isprint(optopt)) {
  52. fprintf(stderr, "Unknown option '-%c'\n", optopt);
  53. } else {
  54. fprintf(stderr, "Unknown option character x%x\n", optopt);
  55. }
  56. return 1;
  57. default:
  58. return 1;
  59. }
  60. }
  61. char configpath[PATH_MAX];
  62. int ret = libmegapixels_find_config(configpath);
  63. libmegapixels_devconfig *config = {0};
  64. if (ret) {
  65. printf("Using config: %s\n", configpath);
  66. libmegapixels_load_file(&config, configpath);
  67. } else {
  68. fprintf(stderr, "No config found for this device\n");
  69. return 1;
  70. }
  71. if (config->count == 0) {
  72. fprintf(stderr, "No valid camera configuration\n");
  73. return 1;
  74. }
  75. if (camera_id > config->count) {
  76. fprintf(stderr, "Camera id %d does not exist\n", camera_id);
  77. }
  78. libmegapixels_camera *camera = config->cameras[camera_id];
  79. if (libmegapixels_open(camera) != 0) {
  80. fprintf(stderr, "Could not open default camera\n");
  81. return 1;
  82. }
  83. libmegapixels_mode *mode = camera->modes[0];
  84. unsigned int frame_size = libmegapixels_select_mode(camera, mode);
  85. if (frame_size == 0) {
  86. fprintf(stderr, "Could not select mode\n");
  87. return 1;
  88. }
  89. // Do the reqular V4L2 stuff to get a frame
  90. struct v4l2_capability cap;
  91. if (ioctl(camera->video_fd, VIDIOC_QUERYCAP, &cap) != 0) {
  92. fprintf(stderr, "VIDIOC_QUERYCAP failed\n");
  93. return 1;
  94. }
  95. if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
  96. fprintf(stderr, "Device does not support streaming i/o\n");
  97. return 1;
  98. }
  99. struct v4l2_requestbuffers req = {0};
  100. req.count = 4;
  101. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  102. req.memory = V4L2_MEMORY_MMAP;
  103. if (xioctl(camera->video_fd, VIDIOC_REQBUFS, &req) == -1) {
  104. fprintf(stderr, "VIDIOC_REQBUFS failed\n");
  105. return 1;
  106. }
  107. buffers = calloc(req.count, sizeof(*buffers));
  108. for (int i = 0; i < req.count; i++) {
  109. struct v4l2_buffer buf = {0};
  110. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  111. buf.memory = V4L2_MEMORY_MMAP;
  112. buf.index = i;
  113. if (xioctl(camera->video_fd, VIDIOC_QUERYBUF, &buf) == -1) {
  114. fprintf(stderr, "VIDIOC_QUERYBUF failed\n");
  115. return 1;
  116. }
  117. buffers[i].length = buf.length;
  118. buffers[i].start = mmap(NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, camera->video_fd, buf.m.offset);
  119. if (buffers[i].start == MAP_FAILED) {
  120. fprintf(stderr, "mmap() failed\n");
  121. return 1;
  122. }
  123. }
  124. for (int i = 0; i < req.count; i++) {
  125. struct v4l2_buffer qbuf = {0};
  126. qbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  127. qbuf.memory = V4L2_MEMORY_MMAP;
  128. qbuf.index = i;
  129. if (xioctl(camera->video_fd, VIDIOC_QBUF, &qbuf) == -1) {
  130. fprintf(stderr, "VIDIOC_QBUF failed\n");
  131. return 1;
  132. }
  133. }
  134. enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  135. if (xioctl(camera->video_fd, VIDIOC_STREAMON, &type) == -1) {
  136. fprintf(stderr, "VIDIOC_STREAMON failed\n");
  137. return 1;
  138. }
  139. int count = 5;
  140. while (count-- > 0) {
  141. while (1) {
  142. fd_set(fds);
  143. FD_ZERO(&fds);
  144. FD_SET(camera->video_fd, &fds);
  145. int sr = select(FD_SETSIZE, &fds, NULL, NULL, NULL);
  146. if (sr == -1) {
  147. if (errno == EINTR) {
  148. count++;
  149. continue;
  150. }
  151. fprintf(stderr, "select() failed: %s\n", strerror(errno));
  152. return 1;
  153. }
  154. struct v4l2_buffer buf = {0};
  155. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  156. buf.memory = V4L2_MEMORY_MMAP;
  157. if (xioctl(camera->video_fd, VIDIOC_DQBUF, &buf) == -1) {
  158. fprintf(stderr, "VIDIOC_DQBUF failed\n");
  159. return 1;
  160. }
  161. fprintf(stderr, "received frame\n");
  162. if (count == 1 && outfile != NULL) {
  163. FILE *fp = fopen(outfile, "w");
  164. fwrite(buffers[buf.index].start, buf.bytesused, 1, fp);
  165. fclose(fp);
  166. printf("Stored frame to: %s\n", outfile);
  167. printf("Format: %dx%x\n", mode->width, mode->height);
  168. char fourcc[5] = {0};
  169. fourcc[0] = (char) (mode->v4l_pixfmt & 0xff);
  170. fourcc[1] = (char) ((mode->v4l_pixfmt >> 8) & 0xff);
  171. fourcc[2] = (char) ((mode->v4l_pixfmt >> 16) & 0xff);
  172. fourcc[3] = (char) ((mode->v4l_pixfmt >> 24) & 0xff);
  173. printf("Pixfmt: %s\n", fourcc);
  174. }
  175. if (xioctl(camera->video_fd, VIDIOC_QBUF, &buf) == -1) {
  176. fprintf(stderr, "VIDIOC_DQBUF failed\n");
  177. return 1;
  178. }
  179. break;
  180. }
  181. }
  182. return 0;
  183. }