1 /*
2 * Copyright (C) 2011 Texas Instruments
3 * Author: Rob Clark <rob.clark@linaro.org>
4 * Adopted for testing VIP capture by: Nikhil Devshatwar <nikhil.nd@ti.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
17 */
20 #include <pthread.h>
21 #include "util.h"
23 #define NBUF 6
24 #define CNT 500000
25 #define MAX_CAP 6
27 struct thr_data {
28 struct display *disp;
29 struct v4l2 *v4l2;
30 uint32_t fourcc, width, height;
31 };
33 static void usage(char *name)
34 {
35 MSG("Usage: %s [OPTION]...", name);
36 MSG("Test of buffer passing between v4l2 camera and display.");
37 MSG("");
38 MSG("%s options:", name);
39 MSG("\t-h, --help: Print this help and exit.");
40 MSG("\t--multi <num>: Capture from <num> different devices.");
41 MSG("\t\t\tEach device name and format would be parsed in the cmdline");
42 MSG("");
43 disp_usage();
44 v4l2_usage();
45 }
47 void *capture_loop(void *arg)
48 {
49 struct thr_data *data = (struct thr_data *)arg;
50 struct display *disp = data->disp;
51 struct v4l2 *v4l2 = data->v4l2;
52 uint32_t fourcc = data->fourcc;
53 uint32_t width = data->width, height = data->height;
55 struct buffer **buffers, *capt;
56 int ret, i;
58 buffers = disp_get_vid_buffers(disp, NBUF, fourcc, width, height);
59 if (!buffers)
60 return NULL;
62 ret = v4l2_reqbufs(v4l2, buffers, NBUF);
63 if (ret)
64 return NULL;
66 for (i = 0; i < NBUF; i++)
67 v4l2_qbuf(v4l2, buffers[i]);
69 ret = v4l2_streamon(v4l2);
70 if (ret)
71 return NULL;
73 for (i = 1; i < CNT; i++) {
75 capt = v4l2_dqbuf(v4l2);
76 ret = disp_post_vid_buffer(disp, capt,
77 0, 0, width, height);
78 if (ret) {
79 ERROR("Post buffer failed");
80 return NULL;
81 }
82 v4l2_qbuf(v4l2, capt);
83 }
84 v4l2_streamoff(v4l2);
86 MSG("Ok!");
87 return disp;
88 }
90 int main(int argc, char **argv)
91 {
92 struct display *disp;
93 struct v4l2 *v4l2;
94 pthread_t threads[MAX_CAP];
95 struct thr_data tdata[MAX_CAP];
96 void *result = NULL;
97 int ret = 0, i, multi = 1;
99 MSG("Opening Display..");
100 disp = disp_open(argc, argv);
101 if (!disp) {
102 usage(argv[0]);
103 return 1;
104 }
106 disp->multiplanar = false;
108 for (i = 1; i < argc; i++) {
109 if (!argv[i])
110 continue;
112 if (!strcmp("--multi", argv[i])) {
113 argv[i++] = NULL;
114 sscanf(argv[i], "%d", &multi);
115 argv[i] = NULL;
116 if(multi < 1 || multi > MAX_CAP) {
117 usage(argv[i]);
118 return 1;
119 }
120 }
121 }
123 for (i = 0; i < multi; i++) {
124 MSG("Opening V4L2..");
125 v4l2 = v4l2_open(argc, argv, &tdata[i].fourcc,
126 &tdata[i].width, &tdata[i].height);
127 if (!v4l2) {
128 usage(argv[0]);
129 return 1;
130 }
131 tdata[i].disp = disp;
132 tdata[i].v4l2 = v4l2;
133 }
135 if (check_args(argc, argv)) {
136 /* remaining args.. print usage msg */
137 usage(argv[0]);
138 return 0;
139 }
141 for (i = 0; i < multi; i++) {
142 ret = pthread_create(&threads[i], NULL,
143 capture_loop, &tdata[i]);
144 if (ret)
145 MSG("Failed creating thread");
146 }
148 for (i = 0; i < multi; i++)
149 pthread_join(threads[i], &result);
151 disp_close(disp);
153 return ret;
154 }