1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <getopt.h>
6
7 #include <string>
8
9 #include "media_v4l2_device.h"
10
PrintUsage(int argc,char ** argv)11 static void PrintUsage(int argc, char** argv) {
12 printf("Usage: %s [options]\n\n"
13 "Options:\n"
14 "--device=DEVICE_NAME Video device name [/dev/video]\n"
15 "--help Print usage\n"
16 "--mmap Use memory mapped buffers\n"
17 "--read Use read() calls\n"
18 "--userp Use application allocated buffers\n"
19 "--buffers=[NUM] Minimum buffers required\n"
20 "--frames=[NUM] Maximum frame to capture\n"
21 "--width=[NUM] Picture width to capture\n"
22 "--height=[NUM] Picture height to capture\n"
23 "--pixel-format=[fourcc] Picture format fourcc code\n"
24 "--fps=[NUM] Frame rate for capture\n"
25 "--time=[NUM] Time to capture in seconds\n",
26 argv[0]);
27 }
28
29 static const char short_options[] = "d:?mrun:f:w:h:t:x:z:";
30 static const struct option
31 long_options[] = {
32 { "device", required_argument, NULL, 'd' },
33 { "help", no_argument, NULL, '?' },
34 { "mmap", no_argument, NULL, 'm' },
35 { "read", no_argument, NULL, 'r' },
36 { "userp", no_argument, NULL, 'u' },
37 { "buffers", required_argument, NULL, 'n' },
38 { "frames", required_argument, NULL, 'f' },
39 { "width", required_argument, NULL, 'w' },
40 { "height", required_argument, NULL, 'h' },
41 { "pixel-format", required_argument, NULL, 't' },
42 { "fps", required_argument, NULL, 'x' },
43 { "time", required_argument, NULL, 'z' },
44 { 0, 0, 0, 0 }
45 };
46
main(int argc,char ** argv)47 int main(int argc, char** argv) {
48 std::string dev_name = "/dev/video";
49 V4L2Device::IOMethod io = V4L2Device::IO_METHOD_MMAP;
50 uint32_t buffers = 4;
51 uint32_t frames = 100;
52 uint32_t width = 640;
53 uint32_t height = 480;
54 uint32_t pixfmt = V4L2_PIX_FMT_YUYV;
55 uint32_t fps = 0;
56 uint32_t time_to_capture = 0;
57
58 for (;;) {
59 int32_t index;
60 int32_t c = getopt_long(argc, argv, short_options, long_options, &index);
61 if (-1 == c)
62 break;
63 switch (c) {
64 case 0: // getopt_long() flag.
65 break;
66 case 'd':
67 // Initialize default v4l2 device name.
68 dev_name = strdup(optarg);
69 break;
70 case '?':
71 PrintUsage(argc, argv);
72 exit (EXIT_SUCCESS);
73 case 'm':
74 io = V4L2Device::IO_METHOD_MMAP;
75 break;
76 case 'r':
77 io = V4L2Device::IO_METHOD_READ;
78 break;
79 case 'u':
80 io = V4L2Device::IO_METHOD_USERPTR;
81 break;
82 case 'n':
83 buffers = atoi(optarg);
84 break;
85 case 'f':
86 frames = atoi(optarg);
87 break;
88 case 'w':
89 width = atoi(optarg);
90 break;
91 case 'h':
92 height = atoi(optarg);
93 break;
94 case 't': {
95 std::string fourcc = optarg;
96 if (fourcc.length() != 4) {
97 PrintUsage(argc, argv);
98 exit (EXIT_FAILURE);
99 }
100 pixfmt = V4L2Device::MapFourCC(fourcc.c_str());
101 break;
102 }
103 case 'x':
104 fps = atoi(optarg);
105 break;
106 case 'z':
107 time_to_capture = atoi(optarg);
108 break;
109 default:
110 PrintUsage(argc, argv);
111 exit(EXIT_FAILURE);
112 }
113 }
114
115 if (time_to_capture) {
116 printf("capture %dx%d %c%c%c%c picture for %d seconds at %d fps\n",
117 width, height, (pixfmt >> 0) & 0xff, (pixfmt >> 8) & 0xff,
118 (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff, time_to_capture, fps);
119 } else {
120 printf("capture %dx%d %c%c%c%c picture for %d frames at %d fps\n",
121 width, height, (pixfmt >> 0) & 0xff, (pixfmt >> 8) & 0xff,
122 (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff, frames, fps);
123 }
124
125 V4L2Device* device = new V4L2Device(dev_name.c_str(), io, buffers);
126
127 int32_t retcode = 0;
128
129 if (!device->OpenDevice())
130 retcode = 1;
131
132 if (!retcode && !device->InitDevice(width, height, pixfmt, fps))
133 retcode = 2;
134
135 if (!retcode && !device->StartCapture())
136 retcode = 3;
137
138 if (!retcode && !device->Run(frames, time_to_capture))
139 retcode = 4;
140
141 if (!retcode && !device->StopCapture())
142 retcode = 5;
143
144 if (!retcode && !device->UninitDevice())
145 retcode = 6;
146
147 device->CloseDevice();
148
149 delete device;
150
151 return retcode;
152 }
153
154