You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

657 lines
19 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * V4L2 video capture example
  3. *
  4. * This program can be used and distributed without restrictions.
  5. *
  6. * This program is provided with the V4L2 API
  7. * see http://linuxtv.org/docs.php for more information
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <assert.h>
  13. #include <getopt.h> /* getopt_long() */
  14. #include <fcntl.h> /* low-level i/o */
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include <sys/stat.h>
  18. #include <sys/types.h>
  19. #include <sys/time.h>
  20. #include <sys/mman.h>
  21. #include <sys/ioctl.h>
  22. #include <linux/videodev2.h>
  23. #define CLEAR(x) memset(&(x), 0, sizeof(x))
  24. enum io_method {
  25. IO_METHOD_READ,
  26. IO_METHOD_MMAP,
  27. IO_METHOD_USERPTR,
  28. };
  29. struct buffer {
  30. void *start;
  31. size_t length;
  32. };
  33. static char *dev_name;
  34. static enum io_method io = IO_METHOD_MMAP;
  35. static int fd = -1;
  36. struct buffer *buffers;
  37. static unsigned int n_buffers;
  38. static int out_buf;
  39. static int force_format;
  40. static int frame_count = 70;
  41. static void errno_exit(const char *s)
  42. {
  43. fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno));
  44. exit(EXIT_FAILURE);
  45. }
  46. static int xioctl(int fh, int request, void *arg)
  47. {
  48. int r;
  49. do {
  50. r = ioctl(fh, request, arg);
  51. } while (-1 == r && EINTR == errno);
  52. return r;
  53. }
  54. static void process_image(const void *p, int size)
  55. {
  56. if (out_buf)
  57. fwrite(p, size, 1, stdout);
  58. fflush(stderr);
  59. fprintf(stderr, ".");
  60. fflush(stdout);
  61. }
  62. static int read_frame(void)
  63. {
  64. struct v4l2_buffer buf;
  65. unsigned int i;
  66. switch (io) {
  67. case IO_METHOD_READ:
  68. if (-1 == read(fd, buffers[0].start, buffers[0].length)) {
  69. switch (errno) {
  70. case EAGAIN:
  71. return 0;
  72. case EIO:
  73. /* Could ignore EIO, see spec. */
  74. /* fall through */
  75. default:
  76. errno_exit("read");
  77. }
  78. }
  79. process_image(buffers[0].start, buffers[0].length);
  80. break;
  81. case IO_METHOD_MMAP:
  82. CLEAR(buf);
  83. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  84. buf.memory = V4L2_MEMORY_MMAP;
  85. if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {
  86. switch (errno) {
  87. case EAGAIN:
  88. return 0;
  89. case EIO:
  90. /* Could ignore EIO, see spec. */
  91. /* fall through */
  92. default:
  93. errno_exit("VIDIOC_DQBUF");
  94. }
  95. }
  96. assert(buf.index < n_buffers);
  97. process_image(buffers[buf.index].start, buf.bytesused);
  98. if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
  99. errno_exit("VIDIOC_QBUF");
  100. break;
  101. case IO_METHOD_USERPTR:
  102. CLEAR(buf);
  103. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  104. buf.memory = V4L2_MEMORY_USERPTR;
  105. if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {
  106. switch (errno) {
  107. case EAGAIN:
  108. return 0;
  109. case EIO:
  110. /* Could ignore EIO, see spec. */
  111. /* fall through */
  112. default:
  113. errno_exit("VIDIOC_DQBUF");
  114. }
  115. }
  116. for (i = 0; i < n_buffers; ++i)
  117. if (buf.m.userptr == (unsigned long)buffers[i].start
  118. && buf.length == buffers[i].length)
  119. break;
  120. assert(i < n_buffers);
  121. process_image((void *)buf.m.userptr, buf.bytesused);
  122. if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
  123. errno_exit("VIDIOC_QBUF");
  124. break;
  125. }
  126. return 1;
  127. }
  128. static void mainloop(void)
  129. {
  130. unsigned int count;
  131. count = frame_count;
  132. while (count-- > 0) {
  133. for (;;) {
  134. fd_set fds;
  135. struct timeval tv;
  136. int r;
  137. FD_ZERO(&fds);
  138. FD_SET(fd, &fds);
  139. /* Timeout. */
  140. tv.tv_sec = 2;
  141. tv.tv_usec = 0;
  142. r = select(fd + 1, &fds, NULL, NULL, &tv);
  143. if (-1 == r) {
  144. if (EINTR == errno)
  145. continue;
  146. errno_exit("select");
  147. }
  148. if (0 == r) {
  149. fprintf(stderr, "select timeout\n");
  150. exit(EXIT_FAILURE);
  151. }
  152. if (read_frame())
  153. break;
  154. /* EAGAIN - continue select loop. */
  155. }
  156. }
  157. }
  158. static void stop_capturing(void)
  159. {
  160. enum v4l2_buf_type type;
  161. switch (io) {
  162. case IO_METHOD_READ:
  163. /* Nothing to do. */
  164. break;
  165. case IO_METHOD_MMAP:
  166. case IO_METHOD_USERPTR:
  167. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  168. if (-1 == xioctl(fd, VIDIOC_STREAMOFF, &type))
  169. errno_exit("VIDIOC_STREAMOFF");
  170. break;
  171. }
  172. }
  173. static void start_capturing(void)
  174. {
  175. unsigned int i;
  176. enum v4l2_buf_type type;
  177. switch (io) {
  178. case IO_METHOD_READ:
  179. /* Nothing to do. */
  180. break;
  181. case IO_METHOD_MMAP:
  182. for (i = 0; i < n_buffers; ++i) {
  183. struct v4l2_buffer buf;
  184. CLEAR(buf);
  185. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  186. buf.memory = V4L2_MEMORY_MMAP;
  187. buf.index = i;
  188. if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
  189. errno_exit("VIDIOC_QBUF");
  190. }
  191. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  192. if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
  193. errno_exit("VIDIOC_STREAMON");
  194. break;
  195. case IO_METHOD_USERPTR:
  196. for (i = 0; i < n_buffers; ++i) {
  197. struct v4l2_buffer buf;
  198. CLEAR(buf);
  199. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  200. buf.memory = V4L2_MEMORY_USERPTR;
  201. buf.index = i;
  202. buf.m.userptr = (unsigned long)buffers[i].start;
  203. buf.length = buffers[i].length;
  204. if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
  205. errno_exit("VIDIOC_QBUF");
  206. }
  207. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  208. if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
  209. errno_exit("VIDIOC_STREAMON");
  210. break;
  211. }
  212. }
  213. static void uninit_device(void)
  214. {
  215. unsigned int i;
  216. switch (io) {
  217. case IO_METHOD_READ:
  218. free(buffers[0].start);
  219. break;
  220. case IO_METHOD_MMAP:
  221. for (i = 0; i < n_buffers; ++i)
  222. if (-1 == munmap(buffers[i].start, buffers[i].length))
  223. errno_exit("munmap");
  224. break;
  225. case IO_METHOD_USERPTR:
  226. for (i = 0; i < n_buffers; ++i)
  227. free(buffers[i].start);
  228. break;
  229. }
  230. free(buffers);
  231. }
  232. static void init_read(unsigned int buffer_size)
  233. {
  234. buffers = calloc(1, sizeof(*buffers));
  235. if (!buffers) {
  236. fprintf(stderr, "Out of memory\n");
  237. exit(EXIT_FAILURE);
  238. }
  239. buffers[0].length = buffer_size;
  240. buffers[0].start = malloc(buffer_size);
  241. if (!buffers[0].start) {
  242. fprintf(stderr, "Out of memory\n");
  243. exit(EXIT_FAILURE);
  244. }
  245. }
  246. static void init_mmap(void)
  247. {
  248. struct v4l2_requestbuffers req;
  249. CLEAR(req);
  250. req.count = 4;
  251. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  252. req.memory = V4L2_MEMORY_MMAP;
  253. if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
  254. if (EINVAL == errno) {
  255. fprintf(stderr, "%s does not support "
  256. "memory mapping\n", dev_name);
  257. exit(EXIT_FAILURE);
  258. } else {
  259. errno_exit("VIDIOC_REQBUFS");
  260. }
  261. }
  262. if (req.count < 2) {
  263. fprintf(stderr, "Insufficient buffer memory on %s\n",
  264. dev_name);
  265. exit(EXIT_FAILURE);
  266. }
  267. buffers = calloc(req.count, sizeof(*buffers));
  268. if (!buffers) {
  269. fprintf(stderr, "Out of memory\n");
  270. exit(EXIT_FAILURE);
  271. }
  272. for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
  273. struct v4l2_buffer buf;
  274. CLEAR(buf);
  275. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  276. buf.memory = V4L2_MEMORY_MMAP;
  277. buf.index = n_buffers;
  278. if (-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf))
  279. errno_exit("VIDIOC_QUERYBUF");
  280. buffers[n_buffers].length = buf.length;
  281. buffers[n_buffers].start =
  282. mmap(NULL /* start anywhere */,
  283. buf.length,
  284. PROT_READ | PROT_WRITE /* required */,
  285. MAP_SHARED /* recommended */,
  286. fd, buf.m.offset);
  287. if (MAP_FAILED == buffers[n_buffers].start)
  288. errno_exit("mmap");
  289. }
  290. }
  291. static void init_userp(unsigned int buffer_size)
  292. {
  293. struct v4l2_requestbuffers req;
  294. CLEAR(req);
  295. req.count = 4;
  296. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  297. req.memory = V4L2_MEMORY_USERPTR;
  298. if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
  299. if (EINVAL == errno) {
  300. fprintf(stderr, "%s does not support "
  301. "user pointer i/o\n", dev_name);
  302. exit(EXIT_FAILURE);
  303. } else {
  304. errno_exit("VIDIOC_REQBUFS");
  305. }
  306. }
  307. buffers = calloc(4, sizeof(*buffers));
  308. if (!buffers) {
  309. fprintf(stderr, "Out of memory\n");
  310. exit(EXIT_FAILURE);
  311. }
  312. for (n_buffers = 0; n_buffers < 4; ++n_buffers) {
  313. buffers[n_buffers].length = buffer_size;
  314. buffers[n_buffers].start = malloc(buffer_size);
  315. if (!buffers[n_buffers].start) {
  316. fprintf(stderr, "Out of memory\n");
  317. exit(EXIT_FAILURE);
  318. }
  319. }
  320. }
  321. static void init_device(void)
  322. {
  323. struct v4l2_capability cap;
  324. struct v4l2_cropcap cropcap;
  325. struct v4l2_crop crop;
  326. struct v4l2_format fmt;
  327. unsigned int min;
  328. if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) {
  329. if (EINVAL == errno) {
  330. fprintf(stderr, "%s is no V4L2 device\n",
  331. dev_name);
  332. exit(EXIT_FAILURE);
  333. } else {
  334. errno_exit("VIDIOC_QUERYCAP");
  335. }
  336. }
  337. if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  338. fprintf(stderr, "%s is no video capture device\n",
  339. dev_name);
  340. exit(EXIT_FAILURE);
  341. }
  342. switch (io) {
  343. case IO_METHOD_READ:
  344. if (!(cap.capabilities & V4L2_CAP_READWRITE)) {
  345. fprintf(stderr, "%s does not support read i/o\n",
  346. dev_name);
  347. exit(EXIT_FAILURE);
  348. }
  349. break;
  350. case IO_METHOD_MMAP:
  351. case IO_METHOD_USERPTR:
  352. if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
  353. fprintf(stderr, "%s does not support streaming i/o\n",
  354. dev_name);
  355. exit(EXIT_FAILURE);
  356. }
  357. break;
  358. }
  359. /* Select video input, video standard and tune here. */
  360. CLEAR(cropcap);
  361. cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  362. if (0 == xioctl(fd, VIDIOC_CROPCAP, &cropcap)) {
  363. crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  364. crop.c = cropcap.defrect; /* reset to default */
  365. if (-1 == xioctl(fd, VIDIOC_S_CROP, &crop)) {
  366. switch (errno) {
  367. case EINVAL:
  368. /* Cropping not supported. */
  369. break;
  370. default:
  371. /* Errors ignored. */
  372. break;
  373. }
  374. }
  375. } else {
  376. /* Errors ignored. */
  377. }
  378. CLEAR(fmt);
  379. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  380. if (force_format) {
  381. fmt.fmt.pix.width = 640;
  382. fmt.fmt.pix.height = 480;
  383. fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  384. fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
  385. if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt))
  386. errno_exit("VIDIOC_S_FMT");
  387. /* Note VIDIOC_S_FMT may change width and height. */
  388. } else {
  389. /* Preserve original settings as set by v4l2-ctl for example */
  390. if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt))
  391. errno_exit("VIDIOC_G_FMT");
  392. }
  393. /* Buggy driver paranoia. */
  394. min = fmt.fmt.pix.width * 2;
  395. if (fmt.fmt.pix.bytesperline < min)
  396. fmt.fmt.pix.bytesperline = min;
  397. min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
  398. if (fmt.fmt.pix.sizeimage < min)
  399. fmt.fmt.pix.sizeimage = min;
  400. switch (io) {
  401. case IO_METHOD_READ:
  402. init_read(fmt.fmt.pix.sizeimage);
  403. break;
  404. case IO_METHOD_MMAP:
  405. init_mmap();
  406. break;
  407. case IO_METHOD_USERPTR:
  408. init_userp(fmt.fmt.pix.sizeimage);
  409. break;
  410. }
  411. }
  412. static void close_device(void)
  413. {
  414. if (-1 == close(fd))
  415. errno_exit("close");
  416. fd = -1;
  417. }
  418. static void open_device(void)
  419. {
  420. struct stat st;
  421. if (-1 == stat(dev_name, &st)) {
  422. fprintf(stderr, "Cannot identify '%s': %d, %s\n",
  423. dev_name, errno, strerror(errno));
  424. exit(EXIT_FAILURE);
  425. }
  426. if (!S_ISCHR(st.st_mode)) {
  427. fprintf(stderr, "%s is no device\n", dev_name);
  428. exit(EXIT_FAILURE);
  429. }
  430. fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
  431. if (-1 == fd) {
  432. fprintf(stderr, "Cannot open '%s': %d, %s\n",
  433. dev_name, errno, strerror(errno));
  434. exit(EXIT_FAILURE);
  435. }
  436. }
  437. static void usage(FILE *fp, int argc, char **argv)
  438. {
  439. fprintf(fp,
  440. "Usage: %s [options]\n\n"
  441. "Version 1.3\n"
  442. "Options:\n"
  443. "-d | --device name Video device name [%s]\n"
  444. "-h | --help Print this message\n"
  445. "-m | --mmap Use memory mapped buffers [default]\n"
  446. "-r | --read Use read() calls\n"
  447. "-u | --userp Use application allocated buffers\n"
  448. "-o | --output Outputs stream to stdout\n"
  449. "-f | --format Force format to 640x480 YUYV\n"
  450. "-c | --count Number of frames to grab [%i]\n"
  451. "",
  452. argv[0], dev_name, frame_count);
  453. }
  454. static const char short_options[] = "d:hmruofc:";
  455. static const struct option
  456. long_options[] = {
  457. { "device", required_argument, NULL, 'd' },
  458. { "help", no_argument, NULL, 'h' },
  459. { "mmap", no_argument, NULL, 'm' },
  460. { "read", no_argument, NULL, 'r' },
  461. { "userp", no_argument, NULL, 'u' },
  462. { "output", no_argument, NULL, 'o' },
  463. { "format", no_argument, NULL, 'f' },
  464. { "count", required_argument, NULL, 'c' },
  465. { 0, 0, 0, 0 }
  466. };
  467. int main(int argc, char **argv)
  468. {
  469. dev_name = "/dev/video0";
  470. for (;;) {
  471. int idx;
  472. int c;
  473. c = getopt_long(argc, argv,
  474. short_options, long_options, &idx);
  475. if (-1 == c)
  476. break;
  477. switch (c) {
  478. case 0: /* getopt_long() flag */
  479. break;
  480. case 'd':
  481. dev_name = optarg;
  482. break;
  483. case 'h':
  484. usage(stdout, argc, argv);
  485. exit(EXIT_SUCCESS);
  486. case 'm':
  487. io = IO_METHOD_MMAP;
  488. break;
  489. case 'r':
  490. io = IO_METHOD_READ;
  491. break;
  492. case 'u':
  493. io = IO_METHOD_USERPTR;
  494. break;
  495. case 'o':
  496. out_buf++;
  497. break;
  498. case 'f':
  499. force_format++;
  500. break;
  501. case 'c':
  502. errno = 0;
  503. frame_count = strtol(optarg, NULL, 0);
  504. if (errno)
  505. errno_exit(optarg);
  506. break;
  507. default:
  508. usage(stderr, argc, argv);
  509. exit(EXIT_FAILURE);
  510. }
  511. }
  512. open_device();
  513. init_device();
  514. start_capturing();
  515. mainloop();
  516. stop_capturing();
  517. uninit_device();
  518. close_device();
  519. fprintf(stderr, "\n");
  520. return 0;
  521. }