Debian Codegraph @

utils/v4l2-compliance/v4l2-test-buffers.cpp

raw
1 /*
2 V4L2 API compliance buffer ioctl tests.
3
4 Copyright (C) 2012 Hans Verkuil <[email protected]>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
19 */
20
21 #include <algorithm>
22 #include <atomic>
23 #include <csignal>
24 #include <map>
25 #include <set>
26 #include <vector>
27
28 #include <arpa/inet.h>
29 #include <poll.h>
30 #include <pthread.h>
31 #include <sys/epoll.h>
32
33 #include "v4l2-compliance.h"
34
35 static cv4l_fmt cur_fmt;
36 static cv4l_fmt cur_m2m_fmt;
37 static int stream_from_fd = -1;
38 static bool stream_use_hdr;
39 static unsigned max_bytesused[VIDEO_MAX_PLANES];
40 static unsigned min_data_offset[VIDEO_MAX_PLANES];
41
42 static bool operator<(struct timeval const& n1, struct timeval const& n2)
43 {
44 return n1.tv_sec < n2.tv_sec ||
45 (n1.tv_sec == n2.tv_sec && n1.tv_usec < n2.tv_usec);
46 }
47
48 struct buf_seq {
49 buf_seq() { init(); }
50 void init()
51 {
52 last_seq = -1;
53 field_nr = 1;
54 }
55
56 int last_seq;
57 unsigned last_field;
58 unsigned field_nr;
59 };
60
61 static struct buf_seq last_seq, last_m2m_seq;
62
63 static int buf_req_fds[VIDEO_MAX_FRAME * 2];
64
65 static inline int named_ioctl_fd(int fd, bool trace, const char *cmd_name, unsigned long cmd, void *arg)
66 {
67 int retval;
68 int e;
69
70 retval = ioctl(fd, cmd, arg);
71 e = retval == 0 ? 0 : errno;
72 if (trace)
73 fprintf(stderr, "\t\t%s returned %d (%s)\n",
74 cmd_name, retval, strerror(e));
75 return retval == -1 ? e : (retval ? -1 : 0);
76 }
77 #define doioctl_fd(fd, r, p) named_ioctl_fd((fd), node->g_trace(), #r, r, p)
78
79 enum QueryBufMode {
80 Unqueued,
81 Prepared,
82 Queued,
83 Dequeued
84 };
85
86 using buf_info_map = std::map<struct timeval, struct v4l2_buffer>;
87 static buf_info_map buffer_info;
88
89 #define FILE_HDR_ID v4l2_fourcc('V', 'h', 'd', 'r')
90
91 static void stream_close()
92 {
93 if (stream_from_fd >= 0) {
94 close(stream_from_fd);
95 stream_from_fd = -1;
96 }
97 }
98
99 static void stream_for_fmt(__u32 pixelformat)
100 {
101 stream_close();
102
103 std::string file = stream_from(fcc2s(pixelformat), stream_use_hdr);
104 if (file.empty())
105 return;
106
107 stream_from_fd = open(file.c_str(), O_RDONLY);
108
109 if (stream_from_fd < 0)
110 fprintf(stderr, "cannot open file %s\n", file.c_str());
111 }
112
113 static void stream_reset()
114 {
115 if (stream_from_fd >= 0)
116 lseek(stream_from_fd, 0, SEEK_SET);
117 }
118
119 static bool fill_output_buffer(const cv4l_queue &q, cv4l_buffer &buf, bool first_run = true)
120 {
121 bool seek = false;
122
123 if (stream_from_fd < 0)
124 return true;
125
126 if (stream_use_hdr) {
127 __u32 v;
128
129 if (read(stream_from_fd, &v, sizeof(v)) != sizeof(v))
130 seek = true;
131 else if (ntohl(v) != FILE_HDR_ID) {
132 fprintf(stderr, "Unknown header ID\n");
133 return false;
134 }
135 }
136
137 for (unsigned p = 0; !seek && p < buf.g_num_planes(); p++) {
138 __u32 len = buf.g_length(p);
139
140 buf.s_bytesused(len, p);
141 buf.s_data_offset(0, p);
142 if (stream_from_fd < 0)
143 continue;
144
145 if (!stream_use_hdr) {
146 ssize_t sz = read(stream_from_fd, q.g_dataptr(buf.g_index(), p), len);
147
148 if (sz < static_cast<ssize_t>(len)) {
149 seek = true;
150 break;
151 }
152 continue;
153 }
154 __u32 bytesused;
155
156 if (read(stream_from_fd, &bytesused, sizeof(bytesused)) != sizeof(bytesused)) {
157 seek = true;
158 break;
159 }
160 bytesused = ntohl(bytesused);
161 if (bytesused > len) {
162 fprintf(stderr, "plane size is too large (%u > %u)\n",
163 bytesused, len);
164 return false;
165 }
166 buf.s_bytesused(bytesused, p);
167
168 ssize_t sz = read(stream_from_fd, q.g_dataptr(buf.g_index(), p), bytesused);
169
170 if (sz < static_cast<ssize_t>(bytesused)) {
171 seek = true;
172 break;
173 }
174 }
175 if (!seek)
176 return true;
177 if (!first_run)
178 return false;
179
180 stream_reset();
181 if (fill_output_buffer(q, buf, false))
182 return true;
183
184 close(stream_from_fd);
185 stream_from_fd = -1;
186
187 unsigned size = 0;
188
189 for (unsigned p = 0; p < buf.g_num_planes(); p++)
190 size += buf.g_length(p);
191
192 if (stream_use_hdr)
193 fprintf(stderr, "the input file is too small\n");
194 else
195 fprintf(stderr, "the input file is smaller than %d bytes\n", size);
196 return false;
197 }
198
199 class buffer : public cv4l_buffer {
200 public:
201 explicit buffer(unsigned type = 0, unsigned memory = 0, unsigned index = 0) :
202 cv4l_buffer(type, memory, index) {}
203 explicit buffer(const cv4l_queue &q, unsigned index = 0) :
204 cv4l_buffer(q, index) {}
205 explicit buffer(const cv4l_buffer &b) : cv4l_buffer(b) {}
206
207 int querybuf(node *node, unsigned index)
208 {
209 return node->querybuf(*this, index);
210 }
211 int prepare_buf(node *node, bool fill_bytesused = true)
212 {
213 if (v4l_type_is_output(g_type()))
214 fill_output_buf(fill_bytesused);
215 int err = node->prepare_buf(*this);
216 if (err == 0 &&
217 v4l_type_is_video(g_type()) && v4l_type_is_output(g_type()))
218 fail_on_test(g_field() == V4L2_FIELD_ANY);
219 return err;
220 }
221 int prepare_buf(node *node, const cv4l_queue &q)
222 {
223 if (v4l_type_is_output(g_type()))
224 fill_output_buffer(q, *this);
225 return prepare_buf(node, false);
226 }
227 int dqbuf(node *node)
228 {
229 return node->dqbuf(*this);
230 }
231 int qbuf(node *node, bool fill_bytesused = true)
232 {
233 int err;
234
235 if (v4l_type_is_output(g_type()))
236 fill_output_buf(fill_bytesused);
237 err = node->qbuf(*this);
238 if (err == 0 &&
239 v4l_type_is_video(g_type()) && v4l_type_is_output(g_type())) {
240 fail_on_test(g_field() == V4L2_FIELD_ANY);
241 buffer_info[g_timestamp()] = buf;
242 }
243 return err;
244 }
245 int qbuf(node *node, const cv4l_queue &q)
246 {
247 if (v4l_type_is_output(g_type()))
248 fill_output_buffer(q, *this);
249 return qbuf(node, false);
250 }
251 int check(const cv4l_queue &q, enum QueryBufMode mode, bool is_m2m = false)
252 {
253 int ret = check(q.g_type(), q.g_memory(), g_index(), mode, last_seq, is_m2m);
254
255 if (!ret)
256 ret = check_planes(q, mode);
257 return ret;
258 }
259 int check(const cv4l_queue &q, enum QueryBufMode mode, __u32 index, bool is_m2m = false)
260 {
261 int ret = check(q.g_type(), q.g_memory(), index, mode, last_seq, is_m2m);
262
263 if (!ret)
264 ret = check_planes(q, mode);
265 return ret;
266 }
267 int check(const cv4l_queue &q, buf_seq &seq, bool is_m2m = false)
268 {
269 int ret = check(q.g_type(), q.g_memory(), g_index(), Dequeued, seq, is_m2m);
270
271 if (!ret)
272 ret = check_planes(q, Dequeued);
273 return ret;
274 }
275 int check(enum QueryBufMode mode, __u32 index, bool is_m2m = false)
276 {
277 return check(g_type(), g_memory(), index, mode, last_seq, is_m2m);
278 }
279 int check(buf_seq &seq, bool is_m2m = false)
280 {
281 return check(g_type(), g_memory(), g_index(), Dequeued, seq, is_m2m);
282 }
283
284 private:
285 int check(unsigned type, unsigned memory, unsigned index,
286 enum QueryBufMode mode, struct buf_seq &seq, bool is_m2m);
287 int check_planes(const cv4l_queue &q, enum QueryBufMode mode);
288 void fill_output_buf(bool fill_bytesused);
289 };
290
291 void buffer::fill_output_buf(bool fill_bytesused = true)
292 {
293 timespec ts;
294 v4l2_timecode tc;
295
296 clock_gettime(CLOCK_MONOTONIC, &ts);
297 if (ts_is_copy()) {
298 s_timestamp_ts(ts);
299 s_timestamp_src(V4L2_BUF_FLAG_TSTAMP_SRC_SOE);
300 }
301 s_flags(g_flags() | V4L2_BUF_FLAG_TIMECODE);
302 tc.type = V4L2_TC_TYPE_30FPS;
303 tc.flags = V4L2_TC_USERBITS_8BITCHARS;
304 tc.frames = ts.tv_nsec * 30 / 1000000000;
305 tc.seconds = ts.tv_sec % 60;
306 tc.minutes = (ts.tv_sec / 60) % 60;
307 tc.hours = (ts.tv_sec / 3600) % 30;
308 tc.userbits[0] = 't';
309 tc.userbits[1] = 'e';
310 tc.userbits[2] = 's';
311 tc.userbits[3] = 't';
312 s_timecode(tc);
313 s_field(V4L2_FIELD_ANY);
314 if (!fill_bytesused)
315 return;
316 for (unsigned p = 0; p < g_num_planes(); p++) {
317 s_bytesused(g_length(p), p);
318 s_data_offset(0, p);
319 }
320 }
321
322 int buffer::check_planes(const cv4l_queue &q, enum QueryBufMode mode)
323 {
324 if (mode == Dequeued || mode == Prepared) {
325 for (unsigned p = 0; p < g_num_planes(); p++) {
326 if (g_memory() == V4L2_MEMORY_USERPTR)
327 fail_on_test(g_userptr(p) != q.g_userptr(g_index(), p));
328 else if (g_memory() == V4L2_MEMORY_DMABUF)
329 fail_on_test(g_fd(p) != q.g_fd(g_index(), p));
330 }
331 }
332 return 0;
333 }
334
335 int buffer::check(unsigned type, unsigned memory, unsigned index,
336 enum QueryBufMode mode, struct buf_seq &seq, bool is_m2m)
337 {
338 unsigned timestamp = g_timestamp_type();
339 bool ts_copy = ts_is_copy();
340 unsigned timestamp_src = g_timestamp_src();
341 unsigned frame_types = 0;
342 unsigned buf_states = 0;
343 const cv4l_fmt &fmt = is_m2m ? cur_m2m_fmt : cur_fmt;
344
345 fail_on_test(g_type() != type);
346 fail_on_test(g_memory() == 0);
347 fail_on_test(g_memory() != memory);
348 fail_on_test(g_index() >= VIDEO_MAX_FRAME);
349 fail_on_test(g_index() != index);
350 fail_on_test(buf.reserved2);
351 if (g_flags() & V4L2_BUF_FLAG_REQUEST_FD)
352 fail_on_test(g_request_fd() < 0);
353 else
354 fail_on_test(g_request_fd());
355 fail_on_test(timestamp != V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC &&
356 timestamp != V4L2_BUF_FLAG_TIMESTAMP_COPY);
357 fail_on_test(timestamp_src != V4L2_BUF_FLAG_TSTAMP_SRC_SOE &&
358 timestamp_src != V4L2_BUF_FLAG_TSTAMP_SRC_EOF);
359 fail_on_test(!ts_copy && v4l_type_is_output(g_type()) &&
360 timestamp_src == V4L2_BUF_FLAG_TSTAMP_SRC_SOE);
361 if (g_flags() & V4L2_BUF_FLAG_KEYFRAME)
362 frame_types++;
363 if (g_flags() & V4L2_BUF_FLAG_PFRAME)
364 frame_types++;
365 if (g_flags() & V4L2_BUF_FLAG_BFRAME)
366 frame_types++;
367 fail_on_test(frame_types > 1);
368 if (g_flags() & V4L2_BUF_FLAG_QUEUED)
369 buf_states++;
370 if (g_flags() & V4L2_BUF_FLAG_DONE)
371 buf_states++;
372 if (g_flags() & V4L2_BUF_FLAG_PREPARED)
373 buf_states++;
374 if (g_flags() & V4L2_BUF_FLAG_IN_REQUEST) {
375 fail_on_test(!(g_flags() & V4L2_BUF_FLAG_REQUEST_FD));
376 if (!(g_flags() & V4L2_BUF_FLAG_PREPARED))
377 buf_states++;
378 }
379 fail_on_test(buf_states > 1);
380 fail_on_test(buf.length == 0);
381 if (v4l_type_is_planar(g_type())) {
382 fail_on_test(buf.length > VIDEO_MAX_PLANES);
383 for (unsigned p = 0; p < buf.length; p++) {
384 struct v4l2_plane *vp = buf.m.planes + p;
385
386 fail_on_test(check_0(vp->reserved, sizeof(vp->reserved)));
387 fail_on_test(vp->length == 0);
388 }
389 }
390
391 if (v4l_type_is_capture(g_type()) && !ts_copy && !is_vivid &&
392 (g_flags() & V4L2_BUF_FLAG_TIMECODE))
393 warn_once("V4L2_BUF_FLAG_TIMECODE was used!\n");
394
395 if (mode == Dequeued) {
396 for (unsigned p = 0; p < g_num_planes(); p++) {
397 if (!(g_flags() & V4L2_BUF_FLAG_LAST))
398 fail_on_test(!g_bytesused(p));
399 if (!g_bytesused(p))
400 fail_on_test(g_data_offset(p));
401 else
402 fail_on_test(g_data_offset(p) >= g_bytesused(p));
403 fail_on_test(g_bytesused(p) > g_length(p));
404 }
405 fail_on_test(!g_timestamp().tv_sec && !g_timestamp().tv_usec);
406 fail_on_test(g_flags() & V4L2_BUF_FLAG_DONE);
407
408 // The vivid driver has unreliable timings causing wrong
409 // sequence numbers on occasion. Skip this test until this
410 // bug is solved.
411 if (!is_vivid)
412 fail_on_test((int)g_sequence() < seq.last_seq + 1);
413 else if ((int)g_sequence() < seq.last_seq + 1)
414 info("(int)g_sequence() < seq.last_seq + 1): %d < %d\n",
415 (int)g_sequence(), seq.last_seq + 1);
416
417 if (v4l_type_is_video(g_type())) {
418 fail_on_test(g_field() == V4L2_FIELD_ALTERNATE);
419 fail_on_test(g_field() == V4L2_FIELD_ANY);
420 if (fmt.g_field() == V4L2_FIELD_ALTERNATE) {
421 fail_on_test(g_field() != V4L2_FIELD_BOTTOM &&
422 g_field() != V4L2_FIELD_TOP);
423 fail_on_test(g_field() == seq.last_field);
424 seq.field_nr ^= 1;
425 if (seq.field_nr) {
426 if (static_cast<int>(g_sequence()) != seq.last_seq)
427 warn("got sequence number %u, expected %u\n",
428 g_sequence(), seq.last_seq);
429 } else {
430 fail_on_test((int)g_sequence() == seq.last_seq + 1);
431 if (static_cast<int>(g_sequence()) != seq.last_seq + 1)
432 warn("got sequence number %u, expected %u\n",
433 g_sequence(), seq.last_seq + 1);
434 }
435 } else {
436 fail_on_test(g_field() != fmt.g_field());
437 if (static_cast<int>(g_sequence()) != seq.last_seq + 1)
438 warn_or_info(is_vivid,
439 "got sequence number %u, expected %u\n",
440 g_sequence(), seq.last_seq + 1);
441 }
442 } else if (!v4l_type_is_meta(g_type()) && static_cast<int>(g_sequence()) != seq.last_seq + 1) {
443 // Don't do this for meta streams: the sequence counter is typically
444 // linked to the video capture to sync the metadata with the video
445 // data. So the sequence counter would start at a non-zero value.
446 warn_or_info(is_vivid, "got sequence number %u, expected %u\n",
447 g_sequence(), seq.last_seq + 1);
448 }
449 seq.last_seq = static_cast<int>(g_sequence());
450 seq.last_field = g_field();
451 } else {
452 fail_on_test(g_sequence());
453 if (mode == Queued && ts_copy && v4l_type_is_output(g_type())) {
454 fail_on_test(!g_timestamp().tv_sec && !g_timestamp().tv_usec);
455 } else {
456 fail_on_test(g_timestamp().tv_sec || g_timestamp().tv_usec);
457 }
458 if (!v4l_type_is_output(g_type()) || mode == Unqueued)
459 fail_on_test(frame_types);
460 if (mode == Unqueued)
461 fail_on_test(g_flags() & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_PREPARED |
462 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR));
463 else if (mode == Prepared)
464 fail_on_test((g_flags() & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_PREPARED |
465 V4L2_BUF_FLAG_DONE | V4L2_BUF_FLAG_ERROR)) !=
466 V4L2_BUF_FLAG_PREPARED);
467 else
468 fail_on_test(!(g_flags() & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_PREPARED)));
469 }
470 return 0;
471 }
472
473 static int testQueryBuf(struct node *node, unsigned type, unsigned count)
474 {
475 buffer buf(type);
476 unsigned i;
477 int ret;
478
479 for (i = 0; i < count; i++) {
480 fail_on_test(buf.querybuf(node, i));
481 if (v4l_type_is_planar(buf.g_type()))
482 fail_on_test(buf.buf.m.planes != buf.planes);
483 fail_on_test(buf.check(Unqueued, i));
484 }
485 ret = buf.querybuf(node, count);
486 fail_on_test_val(ret != EINVAL, ret);
487 return 0;
488 }
489
490 static int testSetupVbi(struct node *node, int type)
491 {
492 if (!v4l_type_is_vbi(type))
493 return 0;
494
495 if (!(node->cur_io_caps & V4L2_IN_CAP_STD))
496 return -1;
497
498 node->s_type(type);
499 cv4l_fmt vbi_fmt;
500
501 if (!node->g_fmt(vbi_fmt))
502 node->s_fmt(vbi_fmt);
503 return 0;
504 }
505
506 static int testCanSetSameTimings(struct node *node)
507 {
508 if (node->cur_io_caps & V4L2_IN_CAP_STD) {
509 v4l2_std_id std;
510
511 fail_on_test(node->g_std(std));
512 fail_on_test(node->s_std(std));
513 }
514 if (node->cur_io_caps & V4L2_IN_CAP_DV_TIMINGS) {
515 v4l2_dv_timings timings;
516
517 fail_on_test(node->g_dv_timings(timings));
518 fail_on_test(node->s_dv_timings(timings));
519 }
520 if (node->cur_io_caps & V4L2_IN_CAP_NATIVE_SIZE) {
521 v4l2_selection sel = {
522 node->g_selection_type(),
523 V4L2_SEL_TGT_NATIVE_SIZE,
524 };
525
526 fail_on_test(node->g_selection(sel));
527 fail_on_test(node->s_selection(sel));
528 }
529 return 0;
530 }
531
532 int testRemoveBufs(struct node *node)
533 {
534 int ret;
535 unsigned i;
536
537 node->reopen();
538
539 for (i = 1; i <= V4L2_BUF_TYPE_LAST; i++) {
540 struct v4l2_remove_buffers removebufs = { };
541 unsigned buffers;
542
543 if (!(node->valid_buftypes & (1 << i)))
544 continue;
545
546 cv4l_queue q(i, V4L2_MEMORY_MMAP);
547
548 if (testSetupVbi(node, i))
549 continue;
550 ret = q.remove_bufs(node, 0, 0);
551 if (ret == ENOTTY)
552 continue;
553
554 q.init(i, V4L2_MEMORY_MMAP);
555 ret = q.create_bufs(node, 0);
556
557 memset(&removebufs, 0xff, sizeof(removebufs));
558 removebufs.index = 0;
559 removebufs.count = 0;
560 removebufs.type = q.g_type();
561 fail_on_test(doioctl(node, VIDIOC_REMOVE_BUFS, &removebufs));
562 fail_on_test(check_0(removebufs.reserved, sizeof(removebufs.reserved)));
563
564 buffer buf(i);
565
566 /* Create only 1 buffer */
567 fail_on_test(q.create_bufs(node, 1));
568 buffers = q.g_buffers();
569 fail_on_test(buffers != 1);
570 /* Removing buffer index 1 must fail */
571 fail_on_test(q.remove_bufs(node, 1, buffers) != EINVAL);
572 /* Removing buffer index 0 is valid */
573 fail_on_test(q.remove_bufs(node, 0, buffers));
574 /* Removing buffer index 0 again must fail */
575 fail_on_test(q.remove_bufs(node, 0, 1) != EINVAL);
576 /* Create 3 buffers indexes 0 to 2 */
577 fail_on_test(q.create_bufs(node, 3));
578 /* Remove them one by one */
579 fail_on_test(q.remove_bufs(node, 2, 1));
580 fail_on_test(q.remove_bufs(node, 0, 1));
581 fail_on_test(q.remove_bufs(node, 1, 1));
582 /* Removing buffer index 0 again must fail */
583 fail_on_test(q.remove_bufs(node, 0, 1) != EINVAL);
584
585 /* Create 4 buffers indexes 0 to 3 */
586 fail_on_test(q.create_bufs(node, 4));
587 /* Remove buffers index 1 and 2 */
588 fail_on_test(q.remove_bufs(node, 1, 2));
589 /* Add 3 more buffers should be indexes 4 to 6 */
590 fail_on_test(q.create_bufs(node, 3));
591 /* Query buffers:
592 * 1 and 2 have been removed they must fail
593 * 0 and 3 to 6 must exist*/
594 fail_on_test(buf.querybuf(node, 0));
595 fail_on_test(buf.querybuf(node, 1) != EINVAL);
596 fail_on_test(buf.querybuf(node, 2) != EINVAL);
597 fail_on_test(buf.querybuf(node, 3));
598 fail_on_test(buf.querybuf(node, 4));
599 fail_on_test(buf.querybuf(node, 5));
600 fail_on_test(buf.querybuf(node, 6));
601
602 /* Remove existing buffer index 6 with bad type must fail */
603 memset(&removebufs, 0xff, sizeof(removebufs));
604 removebufs.index = 6;
605 removebufs.count = 1;
606 removebufs.type = 0;
607 fail_on_test(doioctl(node, VIDIOC_REMOVE_BUFS, &removebufs) != EINVAL);
608
609 /* Remove existing buffer index 6 with bad type and count == 0
610 * must fail */
611 memset(&removebufs, 0xff, sizeof(removebufs));
612 removebufs.index = 6;
613 removebufs.count = 0;
614 removebufs.type = 0;
615 fail_on_test(doioctl(node, VIDIOC_REMOVE_BUFS, &removebufs) != EINVAL);
616
617 /* Remove with count == 0 must always return 0 */
618 fail_on_test(q.remove_bufs(node, 0, 0));
619 fail_on_test(q.remove_bufs(node, 1, 0));
620 fail_on_test(q.remove_bufs(node, 6, 0));
621 fail_on_test(q.remove_bufs(node, 7, 0));
622 fail_on_test(q.remove_bufs(node, 0xffffffff, 0));
623
624 /* Remove crossing max allowed buffers boundary must fail */
625 fail_on_test(q.remove_bufs(node, q.g_max_num_buffers() - 2, 7) != EINVAL);
626
627 /* Remove overflow must fail */
628 fail_on_test(q.remove_bufs(node, 3, 0xfffffff) != EINVAL);
629
630 /* Remove 2 buffers on index 2 when index 2 is free must fail */
631 fail_on_test(q.remove_bufs(node, 2, 2) != EINVAL);
632
633 /* Remove 2 buffers on index 0 when index 1 is free must fail */
634 fail_on_test(q.remove_bufs(node, 0, 2) != EINVAL);
635
636 /* Remove 2 buffers on index 1 when index 1 and 2 are free must fail */
637 fail_on_test(q.remove_bufs(node, 1, 2) != EINVAL);
638
639 /* Create 2 buffers, that must fill the hole */
640 fail_on_test(q.create_bufs(node, 2));
641 /* Remove all buffers */
642 fail_on_test(q.remove_bufs(node, 0, 7));
643
644 fail_on_test(q.reqbufs(node, 0));
645 }
646
647 return 0;
648 }
649
650 int testReqBufs(struct node *node)
651 {
652 struct v4l2_create_buffers crbufs = { };
653 struct v4l2_requestbuffers reqbufs = { };
654 bool can_stream = node->g_caps() & V4L2_CAP_STREAMING;
655 bool can_rw = node->g_caps() & V4L2_CAP_READWRITE;
656 bool mmap_valid;
657 bool userptr_valid;
658 bool dmabuf_valid;
659 int ret;
660 unsigned i, m;
661
662 node->reopen();
663
664 cv4l_queue q(0, 0);
665
666 ret = q.reqbufs(node, 0);
667 if (ret == ENOTTY) {
668 fail_on_test(can_stream);
669 return ret;
670 }
671 fail_on_test_val(ret != EINVAL, ret);
672 fail_on_test(node->node2 == nullptr);
673 for (i = 1; i <= V4L2_BUF_TYPE_LAST; i++) {
674 bool is_vbi_raw = (i == V4L2_BUF_TYPE_VBI_CAPTURE ||
675 i == V4L2_BUF_TYPE_VBI_OUTPUT);
676 bool is_overlay = v4l_type_is_overlay(i);
677 __u32 caps = 0;
678
679 if (!(node->valid_buftypes & (1 << i)))
680 continue;
681
682 if (testSetupVbi(node, i))
683 continue;
684
685 info("test buftype %s\n", buftype2s(i).c_str());
686 if (node->valid_buftype == 0)
687 node->valid_buftype = i;
688
689 q.init(0, 0);
690 fail_on_test(q.reqbufs(node, i) != EINVAL);
691 q.init(i, V4L2_MEMORY_MMAP);
692 ret = q.reqbufs(node, 0);
693 fail_on_test_val(ret && ret != EINVAL, ret);
694 mmap_valid = !ret;
695 if (mmap_valid)
696 node->buf_caps = caps = q.g_capabilities();
697 if (caps) {
698 fail_on_test(mmap_valid ^ !!(caps & V4L2_BUF_CAP_SUPPORTS_MMAP));
699 if (caps & V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS)
700 node->supports_orphaned_bufs = true;
701 }
702
703 q.init(i, V4L2_MEMORY_USERPTR);
704 ret = q.reqbufs(node, 0);
705 fail_on_test_val(ret && ret != EINVAL, ret);
706 userptr_valid = !ret;
707 fail_on_test(!mmap_valid && userptr_valid);
708 if (caps)
709 fail_on_test(userptr_valid ^ !!(caps & V4L2_BUF_CAP_SUPPORTS_USERPTR));
710
711 q.init(i, V4L2_MEMORY_DMABUF);
712 ret = q.reqbufs(node, 0);
713 fail_on_test_val(ret && ret != EINVAL, ret);
714 dmabuf_valid = !ret;
715 fail_on_test(!mmap_valid && dmabuf_valid);
716 fail_on_test(dmabuf_valid && (caps != q.g_capabilities()));
717 if (caps)
718 fail_on_test(dmabuf_valid ^ !!(caps & V4L2_BUF_CAP_SUPPORTS_DMABUF));
719
720 fail_on_test((can_stream && !is_overlay) && !mmap_valid && !userptr_valid && !dmabuf_valid);
721 fail_on_test((!can_stream || is_overlay) && (mmap_valid || userptr_valid || dmabuf_valid));
722 if (!can_stream || is_overlay)
723 continue;
724
725 if (mmap_valid) {
726 q.init(i, V4L2_MEMORY_MMAP);
727 fail_on_test(q.reqbufs(node, 1));
728 fail_on_test(q.g_buffers() == 0);
729 fail_on_test(q.g_memory() != V4L2_MEMORY_MMAP);
730 fail_on_test(q.g_type() != i);
731 fail_on_test(q.reqbufs(node, 1));
732 fail_on_test(testQueryBuf(node, i, q.g_buffers()));
733 node->valid_memorytype |= 1 << V4L2_MEMORY_MMAP;
734 }
735
736 if (userptr_valid) {
737 q.init(i, V4L2_MEMORY_USERPTR);
738 fail_on_test(q.reqbufs(node, 1));
739 fail_on_test(q.g_buffers() == 0);
740 fail_on_test(q.g_memory() != V4L2_MEMORY_USERPTR);
741 fail_on_test(q.g_type() != i);
742 fail_on_test(q.reqbufs(node, 1));
743 fail_on_test(testQueryBuf(node, i, q.g_buffers()));
744 node->valid_memorytype |= 1 << V4L2_MEMORY_USERPTR;
745 }
746
747 if (dmabuf_valid) {
748 q.init(i, V4L2_MEMORY_DMABUF);
749 fail_on_test(q.reqbufs(node, 1));
750 fail_on_test(q.g_buffers() == 0);
751 fail_on_test(q.g_memory() != V4L2_MEMORY_DMABUF);
752 fail_on_test(q.g_type() != i);
753 fail_on_test(q.reqbufs(node, 1));
754 fail_on_test(testQueryBuf(node, i, q.g_buffers()));
755 node->valid_memorytype |= 1 << V4L2_MEMORY_DMABUF;
756 }
757
758 /*
759 * It should be possible to set the same std, timings or
760 * native size even after reqbufs was called.
761 */
762 fail_on_test(testCanSetSameTimings(node));
763
764 if (can_rw) {
765 char buf = 0;
766
767 if (node->can_capture)
768 ret = node->read(&buf, 1);
769 else
770 ret = node->write(&buf, 1);
771 if (ret != -1)
772 return fail("Expected -1, got %d\n", ret);
773 if (errno != EBUSY)
774 return fail("Expected EBUSY, got %d\n", errno);
775 }
776 fail_on_test(q.reqbufs(node, 0));
777
778 for (m = V4L2_MEMORY_MMAP; m <= V4L2_MEMORY_DMABUF; m++) {
779 bool cache_hints_cap = false;
780 bool coherent;
781
782 cache_hints_cap = q.g_capabilities() & V4L2_BUF_CAP_SUPPORTS_MMAP_CACHE_HINTS;
783 if (!(node->valid_memorytype & (1 << m)))
784 continue;
785 cv4l_queue q2(i, m);
786 fail_on_test(q.reqbufs(node, 1));
787 if (!node->is_m2m) {
788 fail_on_test(q2.reqbufs(node->node2, 1) != EBUSY);
789 fail_on_test(q2.reqbufs(node->node2) != EBUSY);
790 fail_on_test(q.reqbufs(node));
791 fail_on_test(q2.reqbufs(node->node2, 1));
792 fail_on_test(q2.reqbufs(node->node2));
793 }
794 memset(&reqbufs, 0xff, sizeof(reqbufs));
795 reqbufs.count = 1;
796 reqbufs.type = i;
797 reqbufs.memory = m;
798 reqbufs.flags = V4L2_MEMORY_FLAG_NON_COHERENT;
799 fail_on_test(doioctl(node, VIDIOC_REQBUFS, &reqbufs));
800 coherent = reqbufs.flags & V4L2_MEMORY_FLAG_NON_COHERENT;
801 if (!cache_hints_cap) {
802 fail_on_test(coherent);
803 } else {
804 if (m == V4L2_MEMORY_MMAP)
805 fail_on_test(!coherent);
806 else
807 fail_on_test(coherent);
808 }
809 q.reqbufs(node);
810
811 ret = q.create_bufs(node, 0);
812 if (ret == ENOTTY) {
813 warn("VIDIOC_CREATE_BUFS not supported\n");
814 break;
815 }
816
817 memset(&crbufs, 0xff, sizeof(crbufs));
818 node->g_fmt(crbufs.format, i);
819 crbufs.count = 1;
820 crbufs.memory = m;
821 crbufs.flags = V4L2_MEMORY_FLAG_NON_COHERENT;
822 fail_on_test(doioctl(node, VIDIOC_CREATE_BUFS, &crbufs));
823 fail_on_test(check_0(crbufs.reserved, sizeof(crbufs.reserved)));
824 fail_on_test(crbufs.index != q.g_buffers());
825
826 coherent = crbufs.flags & V4L2_MEMORY_FLAG_NON_COHERENT;
827 if (!cache_hints_cap) {
828 fail_on_test(coherent);
829 } else {
830 if (m == V4L2_MEMORY_MMAP)
831 fail_on_test(!coherent);
832 else
833 fail_on_test(coherent);
834 }
835
836 if (cache_hints_cap) {
837 /*
838 * Different memory consistency model. Should fail for MMAP
839 * queues which support cache hints.
840 */
841 crbufs.flags = 0;
842 if (m == V4L2_MEMORY_MMAP)
843 fail_on_test(doioctl(node, VIDIOC_CREATE_BUFS, &crbufs) != EINVAL);
844 else
845 fail_on_test(doioctl(node, VIDIOC_CREATE_BUFS, &crbufs));
846 }
847 q.reqbufs(node);
848
849 fail_on_test(q.create_bufs(node, 1));
850 fail_on_test(q.g_buffers() == 0);
851 fail_on_test(q.g_type() != i);
852 fail_on_test(testQueryBuf(node, i, q.g_buffers()));
853 fail_on_test(q.create_bufs(node, 1));
854 fail_on_test(testQueryBuf(node, i, q.g_buffers()));
855 if (!node->is_m2m)
856 fail_on_test(q2.create_bufs(node->node2, 1) != EBUSY);
857 q.reqbufs(node);
858
859 cv4l_fmt fmt;
860
861 node->g_fmt(fmt, q.g_type());
862 if (V4L2_TYPE_IS_MULTIPLANAR(q.g_type())) {
863 // num_planes == 0 is not allowed
864 fmt.s_num_planes(0);
865 fail_on_test(q.create_bufs(node, 1, &fmt) != EINVAL);
866 node->g_fmt(fmt, q.g_type());
867
868 if (fmt.g_num_planes() > 1) {
869 // fewer planes than required by the format
870 // is not allowed
871 fmt.s_num_planes(fmt.g_num_planes() - 1);
872 fail_on_test(q.create_bufs(node, 1, &fmt) != EINVAL);
873 node->g_fmt(fmt, q.g_type());
874
875 // A last plane with a 0 sizeimage is not allowed
876 fmt.s_sizeimage(0, fmt.g_num_planes() - 1);
877 fail_on_test(q.create_bufs(node, 1, &fmt) != EINVAL);
878 node->g_fmt(fmt, q.g_type());
879 }
880
881 if (fmt.g_num_planes() < VIDEO_MAX_PLANES) {
882 // Add an extra plane, but with size 0. The vb2
883 // framework should test for this.
884 fmt.s_num_planes(fmt.g_num_planes() + 1);
885 fmt.s_sizeimage(0, fmt.g_num_planes() - 1);
886 fail_on_test(q.create_bufs(node, 1, &fmt) != EINVAL);
887 node->g_fmt(fmt, q.g_type());
888
889 // This test is debatable: should we allow CREATE_BUFS
890 // to create buffers with more planes than required
891 // by the format?
892 //
893 // For now disallow this. If there is a really good
894 // reason for allowing this, then that should be
895 // documented and carefully tested.
896 //
897 // It is the driver in queue_setup that has to check
898 // this.
899 fmt.s_num_planes(fmt.g_num_planes() + 1);
900 fmt.s_sizeimage(65536, fmt.g_num_planes() - 1);
901 fail_on_test(q.create_bufs(node, 1, &fmt) != EINVAL);
902 node->g_fmt(fmt, q.g_type());
903 }
904 }
905 if (is_vbi_raw) {
906 fmt.fmt.vbi.count[0] = 0;
907 fmt.fmt.vbi.count[1] = 0;
908 } else {
909 fmt.s_sizeimage(0, 0);
910 }
911 // zero size for the first plane is not allowed
912 fail_on_test(q.create_bufs(node, 1, &fmt) != EINVAL);
913 node->g_fmt(fmt, q.g_type());
914
915 // plane sizes that are too small are not allowed
916 for (unsigned p = 0; p < fmt.g_num_planes(); p++) {
917 if (is_vbi_raw) {
918 fmt.fmt.vbi.count[0] /= 2;
919 fmt.fmt.vbi.count[1] /= 2;
920 } else {
921 fmt.s_sizeimage(fmt.g_sizeimage(p) / 2, p);
922 }
923 }
924 fail_on_test(q.create_bufs(node, 1, &fmt) != EINVAL);
925 fail_on_test(testQueryBuf(node, fmt.type, q.g_buffers()));
926 node->g_fmt(fmt, q.g_type());
927
928 // Add 1 MB to each plane or double the vbi counts.
929 // This is allowed.
930 for (unsigned p = 0; p < fmt.g_num_planes(); p++) {
931 if (is_vbi_raw) {
932 fmt.fmt.vbi.count[0] *= 2;
933 fmt.fmt.vbi.count[1] *= 2;
934 } else {
935 fmt.s_sizeimage(fmt.g_sizeimage(p) + (1 << 20), p);
936 }
937 }
938 fail_on_test(q.create_bufs(node, 1, &fmt));
939 buffer buf(q);
940
941 // Check that the new buffer lengths are at least those of
942 // the large sizes as specified by CREATE_BUFS
943 fail_on_test(buf.querybuf(node, 0));
944 fail_on_test(buf.g_num_planes() != fmt.g_num_planes());
945 // Verify that the new buffers actually have the requested
946 // buffer size
947 for (unsigned p = 0; p < buf.g_num_planes(); p++)
948 fail_on_test(buf.g_length(p) < fmt.g_sizeimage(p));
949 node->g_fmt(fmt, q.g_type());
950 }
951 fail_on_test(q.reqbufs(node));
952 }
953 return 0;
954 }
955
956 int testCreateBufsMax(struct node *node)
957 {
958 unsigned int i;
959 int ret;
960
961 node->reopen();
962
963 cv4l_queue q(0, 0);
964
965 for (i = 1; i <= V4L2_BUF_TYPE_LAST; i++) {
966 if (!(node->valid_buftypes & (1 << i)))
967 continue;
968
969 q.init(i, V4L2_MEMORY_MMAP);
970 ret = q.create_bufs(node, 0);
971 if (!ret && (q.g_capabilities() & V4L2_BUF_CAP_SUPPORTS_MAX_NUM_BUFFERS)) {
972 fail_on_test(q.create_bufs(node, q.g_max_num_buffers()));
973 /* Some drivers may not have allocated all the requested buffers
974 * because of memory limitation, that is OK but make the next test
975 * failed so skip it
976 */
977 if (q.g_max_num_buffers() != q.g_buffers())
978 continue;
979 ret = q.create_bufs(node, 1);
980 fail_on_test(ret != ENOBUFS);
981 }
982 }
983
984 return 0;
985 }
986
987 int testExpBuf(struct node *node)
988 {
989 bool have_expbuf = false;
990 int type;
991
992 for (type = 0; type <= V4L2_BUF_TYPE_LAST; type++) {
993 if (!(node->valid_buftypes & (1 << type)))
994 continue;
995 if (v4l_type_is_overlay(type))
996 continue;
997
998 if (testSetupVbi(node, type))
999 continue;
1000
1001 cv4l_queue q(type, V4L2_MEMORY_MMAP);
1002
1003 if (!(node->valid_memorytype & (1 << V4L2_MEMORY_MMAP))) {
1004 if (q.has_expbuf(node)) {
1005 if (node->valid_buftypes)
1006 fail("VIDIOC_EXPBUF is supported, but the V4L2_MEMORY_MMAP support is missing or malfunctioning.\n");
1007 fail("VIDIOC_EXPBUF is supported, but the V4L2_MEMORY_MMAP support is missing, probably due to earlier failing format tests.\n");
1008 }
1009 return ENOTTY;
1010 }
1011
1012 fail_on_test(q.reqbufs(node, 2));
1013 if (q.has_expbuf(node)) {
1014 fail_on_test(q.export_bufs(node, q.g_type()));
1015 have_expbuf = true;
1016 } else {
1017 fail_on_test(!q.export_bufs(node, q.g_type()));
1018 }
1019 q.close_exported_fds();
1020 fail_on_test(q.reqbufs(node));
1021 }
1022 return have_expbuf ? 0 : ENOTTY;
1023 }
1024
1025 int testReadWrite(struct node *node)
1026 {
1027 bool can_rw = node->g_caps() & V4L2_CAP_READWRITE;
1028 int fd_flags = fcntl(node->g_fd(), F_GETFL);
1029 char buf = 0;
1030 int ret, ret2;
1031 int err, err2;
1032
1033 fcntl(node->g_fd(), F_SETFL, fd_flags | O_NONBLOCK);
1034 errno = 0;
1035 if (node->can_capture)
1036 ret = node->read(&buf, 1);
1037 else
1038 ret = node->write(&buf, 1);
1039 err = errno;
1040 fail_on_test(v4l_has_vbi(node->g_v4l_fd()) &&
1041 !(node->cur_io_caps & V4L2_IN_CAP_STD) && ret >= 0);
1042
1043 // Note: RDS can only return multiples of 3, so we accept
1044 // both 0 and 1 as return code.
1045 // EBUSY can be returned when attempting to read/write to a
1046 // multiplanar format.
1047 // EINVAL can be returned if read()/write() is not supported
1048 // for the current input/output.
1049 if (can_rw)
1050 fail_on_test((ret < 0 && err != EAGAIN && err != EBUSY && err != EINVAL) || ret > 1);
1051 else
1052 fail_on_test(ret >= 0 || err != EINVAL);
1053 if (!can_rw)
1054 return ENOTTY;
1055
1056 node->reopen();
1057 fcntl(node->g_fd(), F_SETFL, fd_flags | O_NONBLOCK);
1058
1059 /* check that the close cleared the busy flag */
1060 errno = 0;
1061 if (node->can_capture)
1062 ret2 = node->read(&buf, 1);
1063 else
1064 ret2 = node->write(&buf, 1);
1065 err2 = errno;
1066 fail_on_test(ret2 != ret || err2 != err);
1067 return 0;
1068 }
1069
1070 static int setupM2M(struct node *node, cv4l_queue &q, bool init = true)
1071 {
1072 unsigned buffers = 2;
1073 __u32 caps;
1074
1075 last_m2m_seq.init();
1076
1077 if (node->codec_mask & STATEFUL_DECODER) {
1078 struct v4l2_control ctrl = {
1079 .id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
1080 };
1081 fail_on_test(node->g_ctrl(ctrl));
1082 fail_on_test(ctrl.value <= 0);
1083 buffers = ctrl.value;
1084 fail_on_test(q.reqbufs(node, 1));
1085 fail_on_test((unsigned)ctrl.value < q.g_buffers());
1086 }
1087 fail_on_test(q.reqbufs(node, buffers));
1088 fail_on_test(q.mmap_bufs(node));
1089 caps = q.g_capabilities();
1090 fail_on_test(node->supports_orphaned_bufs ^ !!(caps & V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS));
1091 if (v4l_type_is_video(q.g_type())) {
1092 cv4l_fmt fmt(q.g_type());
1093
1094 node->g_fmt(fmt);
1095 cur_m2m_fmt = fmt;
1096 if (init) {
1097 last_m2m_seq.last_field = fmt.g_field();
1098 if (v4l_type_is_output(q.g_type()))
1099 stream_for_fmt(fmt.g_pixelformat());
1100 }
1101 }
1102 for (unsigned i = 0; i < q.g_buffers(); i++) {
1103 buffer buf(q);
1104
1105 fail_on_test(buf.querybuf(node, i));
1106 buf.s_flags(buf.g_flags() & ~V4L2_BUF_FLAG_REQUEST_FD);
1107 fail_on_test(buf.qbuf(node, q));
1108 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_DONE);
1109 }
1110 fail_on_test(node->streamon(q.g_type()));
1111 return 0;
1112 }
1113
1114 static int captureBufs(struct node *node, struct node *node_m2m_cap, const cv4l_queue &q,
1115 cv4l_queue &m2m_q, unsigned frame_count, int pollmode,
1116 unsigned &capture_count)
1117 {
1118 static constexpr const char *pollmode_str[] = {
1119 "",
1120 " (select)",
1121 " (epoll)",
1122 };
1123 unsigned valid_output_flags =
1124 V4L2_BUF_FLAG_TIMECODE | V4L2_BUF_FLAG_TSTAMP_SRC_MASK |
1125 V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_PFRAME | V4L2_BUF_FLAG_BFRAME;
1126 int fd_flags = fcntl(node->g_fd(), F_GETFL);
1127 cv4l_fmt fmt_q;
1128 buffer buf(q);
1129 unsigned count = frame_count;
1130 unsigned req_idx = q.g_buffers();
1131 bool stopped = false;
1132 bool got_eos = false;
1133 bool got_source_change = false;
1134 struct epoll_event ev;
1135 int epollfd = -1;
1136 int ret;
1137
1138 if (node->is_m2m) {
1139 if (count <= q.g_buffers())
1140 count = 1;
1141 else
1142 count -= q.g_buffers();
1143 }
1144
1145 capture_count = 0;
1146
1147 if (show_info) {
1148 printf("\t %s%s:\n",
1149 buftype2s(q.g_type()).c_str(), pollmode_str[pollmode]);
1150 }
1151
1152 /*
1153 * It should be possible to set the same std, timings or
1154 * native size even while streaming.
1155 */
1156 fail_on_test(testCanSetSameTimings(node));
1157
1158 node->g_fmt(fmt_q, q.g_type());
1159 if (node->buftype_pixfmts[q.g_type()][fmt_q.g_pixelformat()] &
1160 V4L2_FMT_FLAG_COMPRESSED)
1161 valid_output_flags = V4L2_BUF_FLAG_TIMECODE | V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1162 if (node->is_m2m) {
1163 node_m2m_cap->g_fmt(fmt_q, m2m_q.g_type());
1164 if (node_m2m_cap->buftype_pixfmts[m2m_q.g_type()][fmt_q.g_pixelformat()] &
1165 V4L2_FMT_FLAG_COMPRESSED)
1166 valid_output_flags = V4L2_BUF_FLAG_TIMECODE | V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
1167
1168 struct v4l2_event_subscription sub = { 0 };
1169
1170 sub.type = V4L2_EVENT_EOS;
1171 if (node->codec_mask & (STATEFUL_ENCODER | STATEFUL_DECODER))
1172 doioctl(node, VIDIOC_SUBSCRIBE_EVENT, &sub);
1173 }
1174
1175 if (pollmode == POLL_MODE_EPOLL) {
1176 epollfd = epoll_create1(0);
1177
1178 fail_on_test(epollfd < 0);
1179
1180 /*
1181 * Many older versions of the vb2 and m2m have a bug where
1182 * EPOLLIN and EPOLLOUT events are never signaled unless they
1183 * are part of the initial EPOLL_CTL_ADD. We set an initial
1184 * empty set of events, which we then modify with EPOLL_CTL_MOD,
1185 * in order to detect that condition.
1186 */
1187 ev.events = 0;
1188 fail_on_test(epoll_ctl(epollfd, EPOLL_CTL_ADD, node->g_fd(), &ev));
1189
1190 if (node->is_m2m)
1191 ev.events = EPOLLIN | EPOLLOUT | EPOLLPRI;
1192 else if (v4l_type_is_output(q.g_type()))
1193 ev.events = EPOLLOUT;
1194 else
1195 ev.events = EPOLLIN;
1196 ev.data.fd = node->g_fd();
1197 fail_on_test(epoll_ctl(epollfd, EPOLL_CTL_MOD, node->g_fd(), &ev));
1198 }
1199
1200 if (pollmode)
1201 fcntl(node->g_fd(), F_SETFL, fd_flags | O_NONBLOCK);
1202 for (;;) {
1203 buf.init(q);
1204
1205 bool can_read = true;
1206 bool have_event = false;
1207
1208 if (pollmode == POLL_MODE_SELECT) {
1209 struct timeval tv = { 2, 0 };
1210 fd_set rfds, wfds, efds;
1211
1212 FD_ZERO(&rfds);
1213 FD_SET(node->g_fd(), &rfds);
1214 FD_ZERO(&wfds);
1215 FD_SET(node->g_fd(), &wfds);
1216 FD_ZERO(&efds);
1217 FD_SET(node->g_fd(), &efds);
1218 if (node->is_m2m)
1219 ret = select(node->g_fd() + 1, &rfds, &wfds, &efds, &tv);
1220 else if (v4l_type_is_output(q.g_type()))
1221 ret = select(node->g_fd() + 1, nullptr, &wfds, nullptr, &tv);
1222 else
1223 ret = select(node->g_fd() + 1, &rfds, nullptr, nullptr, &tv);
1224 fail_on_test(ret == 0);
1225 fail_on_test(ret < 0);
1226 fail_on_test(!FD_ISSET(node->g_fd(), &rfds) &&
1227 !FD_ISSET(node->g_fd(), &wfds) &&
1228 !FD_ISSET(node->g_fd(), &efds));
1229 can_read = FD_ISSET(node->g_fd(), &rfds);
1230 have_event = FD_ISSET(node->g_fd(), &efds);
1231 } else if (pollmode == POLL_MODE_EPOLL) {
1232 /*
1233 * This can fail with a timeout on older kernels for
1234 * drivers using vb2_core_poll() v4l2_m2m_poll().
1235 */
1236 ret = epoll_wait(epollfd, &ev, 1, 2000);
1237 fail_on_test(ret == 0);
1238 fail_on_test_val(ret < 0, ret);
1239 can_read = ev.events & EPOLLIN;
1240 have_event = ev.events & EPOLLPRI;
1241 }
1242
1243 if (have_event) {
1244 struct v4l2_event ev;
1245
1246 while (!doioctl(node, VIDIOC_DQEVENT, &ev)) {
1247 if (ev.type == V4L2_EVENT_EOS) {
1248 fail_on_test(got_eos);
1249 got_eos = true;
1250 fail_on_test(!stopped);
1251 }
1252 if (ev.type == V4L2_EVENT_SOURCE_CHANGE) {
1253 fail_on_test(got_source_change);
1254 got_source_change = true;
1255 //fail_on_test(stopped);
1256 stopped = true;
1257 }
1258 }
1259 }
1260
1261 ret = EAGAIN;
1262 if (!node->is_m2m || !stopped)
1263 ret = buf.dqbuf(node);
1264 if (ret != EAGAIN) {
1265 fail_on_test_val(ret, ret);
1266 if (show_info)
1267 printf("\t\t%s Buffer: %d Sequence: %d Field: %s Size: %d Flags: %s Timestamp: %lld.%06llds\n",
1268 v4l_type_is_output(buf.g_type()) ? "Out" : "Cap",
1269 buf.g_index(), buf.g_sequence(),
1270 field2s(buf.g_field()).c_str(), buf.g_bytesused(),
1271 bufferflags2s(buf.g_flags()).c_str(),
1272 static_cast<__u64>(buf.g_timestamp().tv_sec), static_cast<__u64>(buf.g_timestamp().tv_usec));
1273 for (unsigned p = 0; p < buf.g_num_planes(); p++) {
1274 if (max_bytesused[p] < buf.g_bytesused(p))
1275 max_bytesused[p] = buf.g_bytesused(p);
1276 if (min_data_offset[p] > buf.g_data_offset(p))
1277 min_data_offset[p] = buf.g_data_offset(p);
1278 }
1279
1280 fail_on_test(buf.check(q, last_seq));
1281 if (!show_info && !no_progress) {
1282 printf("\r\t%s: Frame #%03d%s",
1283 buftype2s(q.g_type()).c_str(),
1284 frame_count - count,
1285 pollmode_str[pollmode]);
1286 if (node->g_trace())
1287 printf("\n");
1288 fflush(stdout);
1289 }
1290 if (v4l_type_is_capture(buf.g_type()) && node->is_m2m && buf.ts_is_copy()) {
1291 fail_on_test(buffer_info.find(buf.g_timestamp()) == buffer_info.end());
1292 struct v4l2_buffer &orig_buf = buffer_info[buf.g_timestamp()];
1293 fail_on_test(buf.g_field() != orig_buf.field);
1294 fail_on_test((buf.g_flags() & valid_output_flags) !=
1295 (orig_buf.flags & valid_output_flags));
1296 if (buf.g_flags() & V4L2_BUF_FLAG_TIMECODE)
1297 fail_on_test(memcmp(&buf.g_timecode(), &orig_buf.timecode,
1298 sizeof(orig_buf.timecode)));
1299 }
1300 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_DONE);
1301
1302 if (buf.g_flags() & V4L2_BUF_FLAG_REQUEST_FD) {
1303 buf.querybuf(node, buf.g_index());
1304 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_REQUEST_FD);
1305 fail_on_test(buf.g_request_fd());
1306 fail_on_test(!buf.qbuf(node));
1307 buf.s_flags(V4L2_BUF_FLAG_REQUEST_FD);
1308 buf.s_request_fd(buf_req_fds[req_idx]);
1309 }
1310 fail_on_test(buf.qbuf(node, q));
1311 // This is not necessarily wrong (v4l-touch drivers can do this),
1312 // but it is certainly unusual enough to warn about this.
1313 if (buf.g_flags() & V4L2_BUF_FLAG_DONE)
1314 warn_once("QBUF returned the buffer as DONE.\n");
1315 if (buf.g_flags() & V4L2_BUF_FLAG_REQUEST_FD) {
1316 fail_on_test(doioctl_fd(buf_req_fds[req_idx],
1317 MEDIA_REQUEST_IOC_QUEUE, nullptr));
1318 // testRequests will close some of these request fds,
1319 // so we need to find the next valid fds.
1320 do {
1321 req_idx = (req_idx + 1) % (2 * q.g_buffers());
1322 } while (buf_req_fds[req_idx] < 0);
1323 }
1324 count--;
1325 if (!node->is_m2m && !count)
1326 break;
1327 if (!count && (node->codec_mask & STATEFUL_ENCODER)) {
1328 struct v4l2_encoder_cmd cmd;
1329
1330 memset(&cmd, 0, sizeof(cmd));
1331 cmd.cmd = V4L2_ENC_CMD_STOP;
1332 fail_on_test(doioctl(node, VIDIOC_ENCODER_CMD, &cmd));
1333 stopped = true;
1334 }
1335 if (!count && (node->codec_mask & STATEFUL_DECODER)) {
1336 struct v4l2_decoder_cmd cmd;
1337
1338 memset(&cmd, 0, sizeof(cmd));
1339 cmd.cmd = V4L2_DEC_CMD_STOP;
1340 fail_on_test(doioctl(node, VIDIOC_DECODER_CMD, &cmd));
1341 stopped = true;
1342 }
1343 }
1344 if (!node->is_m2m || !can_read)
1345 continue;
1346
1347 buf.init(m2m_q);
1348 do {
1349 ret = buf.dqbuf(node_m2m_cap);
1350 } while (ret == EAGAIN);
1351 capture_count++;
1352
1353 if (show_info)
1354 printf("\t\t%s Buffer: %d Sequence: %d Field: %s Size: %d Flags: %s Timestamp: %lld.%06llds\n",
1355 v4l_type_is_output(buf.g_type()) ? "Out" : "Cap",
1356 buf.g_index(), buf.g_sequence(),
1357 field2s(buf.g_field()).c_str(), buf.g_bytesused(),
1358 bufferflags2s(buf.g_flags()).c_str(),
1359 static_cast<__u64>(buf.g_timestamp().tv_sec), static_cast<__u64>(buf.g_timestamp().tv_usec));
1360 fail_on_test_val(ret, ret);
1361 if (v4l_type_is_capture(buf.g_type()) && buf.g_bytesused())
1362 fail_on_test(buf.check(m2m_q, last_m2m_seq, true));
1363 if (v4l_type_is_capture(buf.g_type()) && buf.ts_is_copy() && buf.g_bytesused()) {
1364 fail_on_test(buffer_info.find(buf.g_timestamp()) == buffer_info.end());
1365 struct v4l2_buffer &orig_buf = buffer_info[buf.g_timestamp()];
1366 if (cur_fmt.g_field() == cur_m2m_fmt.g_field())
1367 fail_on_test(buf.g_field() != orig_buf.field);
1368 fail_on_test((buf.g_flags() & valid_output_flags) !=
1369 (orig_buf.flags & valid_output_flags));
1370 if (buf.g_flags() & V4L2_BUF_FLAG_TIMECODE)
1371 fail_on_test(memcmp(&buf.g_timecode(), &orig_buf.timecode,
1372 sizeof(orig_buf.timecode)));
1373 }
1374 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_DONE);
1375 if (!count || stopped) {
1376 if (!(node_m2m_cap->codec_mask & (STATEFUL_ENCODER | STATEFUL_DECODER)))
1377 break;
1378 if (buf.g_flags() & V4L2_BUF_FLAG_LAST) {
1379 fail_on_test(buf.dqbuf(node_m2m_cap) != EPIPE);
1380 fail_on_test(!got_eos && !got_source_change);
1381 if (!count)
1382 break;
1383 fail_on_test(node_m2m_cap->streamoff(m2m_q.g_type()));
1384 m2m_q.munmap_bufs(node_m2m_cap);
1385 fail_on_test(setupM2M(node_m2m_cap, m2m_q, false));
1386 stopped = false;
1387 got_source_change = false;
1388
1389 struct v4l2_decoder_cmd cmd;
1390
1391 memset(&cmd, 0, sizeof(cmd));
1392 cmd.cmd = V4L2_DEC_CMD_START;
1393 fail_on_test(doioctl(node_m2m_cap, VIDIOC_DECODER_CMD, &cmd));
1394 continue;
1395 }
1396 }
1397 buf.s_flags(buf.g_flags() & ~V4L2_BUF_FLAG_REQUEST_FD);
1398 fail_on_test(buf.qbuf(node_m2m_cap, m2m_q));
1399 // If the queued buffer is immediately returned as a last
1400 // empty buffer, then FLAG_DONE is set here.
1401 // Need to look at this more closely.
1402 //fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_DONE);
1403 }
1404 if (pollmode)
1405 fcntl(node->g_fd(), F_SETFL, fd_flags);
1406 if (epollfd >= 0)
1407 close(epollfd);
1408 if (!show_info && !no_progress) {
1409 printf("\r\t \r");
1410 fflush(stdout);
1411 }
1412 if (node->is_m2m)
1413 printf("\t%s: Captured %d buffers\n", buftype2s(m2m_q.g_type()).c_str(), capture_count);
1414
1415 return 0;
1416 }
1417
1418 static int bufferOutputErrorTest(struct node *node, const buffer &orig_buf)
1419 {
1420 buffer buf(orig_buf);
1421 bool have_prepare = false;
1422 int ret;
1423
1424 for (unsigned p = 0; p < buf.g_num_planes(); p++) {
1425 buf.s_bytesused(buf.g_length(p) + 1, p);
1426 buf.s_data_offset(0, p);
1427 }
1428 ret = buf.prepare_buf(node, false);
1429 fail_on_test_val(ret != EINVAL && ret != ENOTTY, ret);
1430 have_prepare = ret != ENOTTY;
1431 fail_on_test(buf.qbuf(node, false) != EINVAL);
1432
1433 if (v4l_type_is_planar(buf.g_type())) {
1434 for (unsigned p = 0; p < buf.g_num_planes(); p++) {
1435 buf.s_bytesused(buf.g_length(p) / 2, p);
1436 buf.s_data_offset(buf.g_bytesused(p), p);
1437 }
1438 if (have_prepare)
1439 fail_on_test(buf.prepare_buf(node, false) != EINVAL);
1440 fail_on_test(buf.qbuf(node, false) != EINVAL);
1441 }
1442 buf.init(orig_buf);
1443 if (have_prepare) {
1444 fail_on_test(buf.prepare_buf(node, false));
1445 fail_on_test(buf.check(Prepared, 0));
1446 buf.init(orig_buf);
1447 for (unsigned p = 0; p < buf.g_num_planes(); p++) {
1448 buf.s_bytesused(0xdeadbeef, p);
1449 buf.s_data_offset(0xdeadbeef, p);
1450 }
1451 }
1452 fail_on_test(buf.qbuf(node, false));
1453 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_DONE);
1454 for (unsigned p = 0; p < buf.g_num_planes(); p++) {
1455 fail_on_test(buf.g_bytesused(p) != orig_buf.g_bytesused(p));
1456 fail_on_test(buf.g_data_offset(p));
1457 }
1458 return 0;
1459 }
1460
1461 static int setupMmap(struct node *node, cv4l_queue &q)
1462 {
1463 bool cache_hints = q.g_capabilities() & V4L2_BUF_CAP_SUPPORTS_MMAP_CACHE_HINTS;
1464
1465 fail_on_test(q.mmap_bufs(node));
1466 for (unsigned i = 0; i < q.g_buffers(); i++) {
1467 buffer buf(q);
1468 unsigned int flags;
1469 int ret;
1470
1471 fail_on_test(buf.querybuf(node, i));
1472 fail_on_test(buf.check(q, Unqueued, i));
1473
1474 /*
1475 * Do not set cache hints for all the buffers, but only on
1476 * some of them, so that we can test more cases.
1477 */
1478 if (i == 0) {
1479 flags = buf.g_flags();
1480 flags |= V4L2_BUF_FLAG_NO_CACHE_INVALIDATE;
1481 flags |= V4L2_BUF_FLAG_NO_CACHE_CLEAN;
1482 buf.s_flags(flags);
1483 }
1484
1485 for (unsigned p = 0; p < buf.g_num_planes(); p++) {
1486 // Try a random offset
1487 fail_on_test(node->mmap(buf.g_length(p),
1488 buf.g_mem_offset(p) + 0xdeadbeef) != MAP_FAILED);
1489 }
1490 fail_on_test(!buf.dqbuf(node));
1491 if (v4l_type_is_output(buf.g_type()) && i == 0) {
1492 for (unsigned p = 0; p < buf.g_num_planes(); p++) {
1493 buf.s_bytesused(buf.g_length(p), p);
1494 buf.s_data_offset(0, p);
1495 }
1496 fill_output_buffer(q, buf);
1497 fail_on_test(bufferOutputErrorTest(node, buf));
1498 fail_on_test(buf.querybuf(node, i));
1499 fail_on_test(buf.check(q, Queued, i));
1500 } else {
1501 ret = buf.prepare_buf(node, q);
1502 fail_on_test_val(ret && ret != ENOTTY, ret);
1503 if (ret == 0) {
1504 fail_on_test(buf.querybuf(node, i));
1505 fail_on_test(buf.check(q, Prepared, i));
1506 fail_on_test(!buf.prepare_buf(node));
1507 }
1508
1509 fail_on_test(buf.qbuf(node));
1510 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_DONE);
1511 fail_on_test(!buf.qbuf(node));
1512 fail_on_test(!buf.prepare_buf(node));
1513 // Test with invalid buffer index
1514 buf.s_index(buf.g_index() + VIDEO_MAX_FRAME);
1515 fail_on_test(!buf.prepare_buf(node));
1516 fail_on_test(!buf.qbuf(node));
1517 fail_on_test(!buf.querybuf(node, buf.g_index()));
1518 buf.s_index(buf.g_index() - VIDEO_MAX_FRAME);
1519 fail_on_test(buf.g_index() != i);
1520 }
1521 flags = buf.g_flags();
1522 if (cache_hints) {
1523 if (i == 0) {
1524 /* We do expect cache hints on this buffer */
1525 fail_on_test(!(flags & V4L2_BUF_FLAG_NO_CACHE_INVALIDATE));
1526 fail_on_test(!(flags & V4L2_BUF_FLAG_NO_CACHE_CLEAN));
1527 } else {
1528 /* We expect no cache hints on this buffer */
1529 fail_on_test(flags & V4L2_BUF_FLAG_NO_CACHE_INVALIDATE);
1530 fail_on_test(flags & V4L2_BUF_FLAG_NO_CACHE_CLEAN);
1531 }
1532 } else if (node->might_support_cache_hints) {
1533 fail_on_test(flags & V4L2_BUF_FLAG_NO_CACHE_INVALIDATE);
1534 fail_on_test(flags & V4L2_BUF_FLAG_NO_CACHE_CLEAN);
1535 }
1536 fail_on_test(buf.querybuf(node, i));
1537 fail_on_test(buf.check(q, Queued, i));
1538 fail_on_test(!buf.dqbuf(node));
1539 }
1540 return 0;
1541 }
1542
1543 int testMmap(struct node *node, struct node *node_m2m_cap, unsigned frame_count,
1544 enum poll_mode pollmode, bool use_create_bufs)
1545 {
1546 bool can_stream = node->g_caps() & V4L2_CAP_STREAMING;
1547 bool have_createbufs = true;
1548 int type;
1549 int ret;
1550
1551 if (!node->valid_buftypes)
1552 return ENOTTY;
1553
1554 buffer_info.clear();
1555 for (type = 0; type <= V4L2_BUF_TYPE_LAST; type++) {
1556 unsigned reqbufs_buf_count;
1557 unsigned buffers = 2;
1558 cv4l_fmt fmt;
1559
1560 if (!(node->valid_buftypes & (1 << type)))
1561 continue;
1562 if (v4l_type_is_overlay(type))
1563 continue;
1564 if (node->is_m2m && !v4l_type_is_output(type))
1565 continue;
1566
1567 cv4l_queue q(type, V4L2_MEMORY_MMAP);
1568 cv4l_queue m2m_q(v4l_type_invert(type));
1569
1570 if (testSetupVbi(node, type))
1571 continue;
1572
1573 stream_close();
1574
1575 ret = q.reqbufs(node, 0);
1576 if (ret) {
1577 fail_on_test(can_stream);
1578 return ret;
1579 }
1580 fail_on_test(!can_stream);
1581
1582 fail_on_test(node->streamon(q.g_type()) != EINVAL);
1583 fail_on_test(node->streamoff(q.g_type()));
1584
1585 q.init(type, V4L2_MEMORY_MMAP);
1586
1587 if (node->is_m2m && (node->codec_mask & STATEFUL_ENCODER)) {
1588 struct v4l2_control ctrl = {
1589 .id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
1590 };
1591
1592 fail_on_test(node->g_ctrl(ctrl));
1593 fail_on_test(ctrl.value <= 0);
1594 buffers = ctrl.value;
1595 fail_on_test(q.reqbufs(node, 1));
1596 fail_on_test((unsigned)ctrl.value < q.g_buffers());
1597 }
1598 fail_on_test(q.reqbufs(node, buffers));
1599 reqbufs_buf_count = q.g_buffers();
1600 fail_on_test(node->streamoff(q.g_type()));
1601 last_seq.init();
1602
1603 // Test queuing buffers...
1604 for (unsigned i = 0; i < q.g_buffers(); i++) {
1605 buffer buf(q);
1606
1607 fail_on_test(buf.querybuf(node, i));
1608 fail_on_test(buf.qbuf(node));
1609 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_DONE);
1610 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_REQUEST_FD);
1611 fail_on_test(buf.g_request_fd());
1612 }
1613 // calling STREAMOFF...
1614 fail_on_test(node->streamoff(q.g_type()));
1615 // and now we should be able to queue those buffers again since
1616 // STREAMOFF should return them back to the dequeued state.
1617 for (unsigned i = 0; i < q.g_buffers(); i++) {
1618 buffer buf(q);
1619
1620 fail_on_test(buf.querybuf(node, i));
1621 fail_on_test(buf.qbuf(node));
1622 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_DONE);
1623 }
1624 // Now request buffers again, freeing the old buffers.
1625 // Good check for whether all the internal vb2 calls are in
1626 // balance.
1627 fail_on_test(q.reqbufs(node, q.g_buffers()));
1628 fail_on_test(node->g_fmt(cur_fmt, q.g_type()));
1629
1630 ret = q.create_bufs(node, 0);
1631 fail_on_test_val(ret != ENOTTY && ret != 0, ret);
1632 if (ret == ENOTTY) {
1633 have_createbufs = false;
1634 if (use_create_bufs)
1635 return ENOTTY;
1636 }
1637 if (have_createbufs) {
1638 q.reqbufs(node);
1639 q.create_bufs(node, 2, &cur_fmt, V4L2_MEMORY_FLAG_NON_COHERENT);
1640 fail_on_test(setupMmap(node, q));
1641 q.munmap_bufs(node);
1642 q.reqbufs(node, buffers);
1643
1644 cv4l_fmt fmt(cur_fmt);
1645
1646 if (node->is_video) {
1647 last_seq.last_field = cur_fmt.g_field();
1648 fmt.s_height(fmt.g_height() / 2);
1649 for (unsigned p = 0; p < fmt.g_num_planes(); p++)
1650 fmt.s_sizeimage(fmt.g_sizeimage(p) / 2, p);
1651 fail_on_test(q.create_bufs(node, 1, &fmt) != EINVAL);
1652 fail_on_test(testQueryBuf(node, cur_fmt.type, q.g_buffers()));
1653 q.reqbufs(node);
1654 fail_on_test(q.create_bufs(node, 1, &fmt) != EINVAL);
1655 fail_on_test(testQueryBuf(node, cur_fmt.type, q.g_buffers()));
1656 fmt = cur_fmt;
1657 for (unsigned p = 0; p < fmt.g_num_planes(); p++)
1658 fmt.s_sizeimage(fmt.g_sizeimage(p) * 2, p);
1659 q.reqbufs(node, buffers);
1660 }
1661 fail_on_test(q.create_bufs(node, 1, &fmt));
1662 if (node->is_video) {
1663 buffer buf(q);
1664
1665 fail_on_test(buf.querybuf(node, q.g_buffers() - 1));
1666 for (unsigned p = 0; p < fmt.g_num_planes(); p++)
1667 fail_on_test(buf.g_length(p) < fmt.g_sizeimage(p));
1668 }
1669 fail_on_test(q.reqbufs(node));
1670 if (use_create_bufs) {
1671 fmt = cur_fmt;
1672 if (node->is_video)
1673 for (unsigned p = 0; p < fmt.g_num_planes(); p++)
1674 fmt.s_sizeimage(fmt.g_sizeimage(p) + 10240, p);
1675 q.create_bufs(node, reqbufs_buf_count - 1, &fmt);
1676 q.create_bufs(node, 1, &fmt);
1677 fail_on_test(q.g_buffers() != reqbufs_buf_count);
1678 if (node->is_video) {
1679 for (unsigned b = 0; b < q.g_buffers(); b++) {
1680 buffer buf(q);
1681
1682 fail_on_test(buf.querybuf(node, b));
1683 for (unsigned p = 0; p < fmt.g_num_planes(); p++)
1684 fail_on_test(buf.g_length(p) != fmt.g_sizeimage(p));
1685 }
1686 }
1687 fail_on_test(q.reqbufs(node));
1688 q.create_bufs(node, reqbufs_buf_count, &cur_fmt);
1689 fail_on_test(q.g_buffers() != reqbufs_buf_count);
1690 } else {
1691 fail_on_test(q.reqbufs(node, reqbufs_buf_count));
1692 }
1693 }
1694 if (v4l_type_is_output(type))
1695 stream_for_fmt(cur_fmt.g_pixelformat());
1696
1697 fail_on_test(setupMmap(node, q));
1698
1699 if (node->inject_error(VIVID_CID_START_STR_ERROR))
1700 fail_on_test(!node->streamon(q.g_type()));
1701
1702 if (node->codec_mask & STATEFUL_DECODER) {
1703 struct v4l2_event_subscription sub = { 0 };
1704
1705 sub.type = V4L2_EVENT_SOURCE_CHANGE;
1706 fail_on_test(doioctl(node, VIDIOC_SUBSCRIBE_EVENT, &sub));
1707 }
1708
1709 fail_on_test(node->streamon(q.g_type()));
1710 fail_on_test(node->streamon(q.g_type()));
1711
1712 unsigned capture_count;
1713
1714 if (node->is_m2m) {
1715 if (node->codec_mask & STATEFUL_DECODER) {
1716 int fd_flags = fcntl(node->g_fd(), F_GETFL);
1717 struct timeval tv = { 1, 0 };
1718 fd_set efds;
1719 v4l2_event ev;
1720
1721 fcntl(node->g_fd(), F_SETFL, fd_flags | O_NONBLOCK);
1722 FD_ZERO(&efds);
1723 FD_SET(node->g_fd(), &efds);
1724 ret = select(node->g_fd() + 1, nullptr, nullptr, &efds, &tv);
1725 fail_on_test_val(ret < 0, ret);
1726 fail_on_test(ret == 0);
1727 fail_on_test(node->dqevent(ev));
1728 fcntl(node->g_fd(), F_SETFL, fd_flags);
1729 fail_on_test(ev.type != V4L2_EVENT_SOURCE_CHANGE);
1730 fail_on_test(!(ev.u.src_change.changes & V4L2_EVENT_SRC_CH_RESOLUTION));
1731 }
1732 fail_on_test(setupM2M(node_m2m_cap, m2m_q));
1733 }
1734
1735 fail_on_test(captureBufs(node, node_m2m_cap, q, m2m_q, frame_count,
1736 pollmode, capture_count));
1737 fail_on_test(node->streamoff(q.g_type()));
1738 fail_on_test(node->streamoff(q.g_type()));
1739 if (node->is_m2m)
1740 fail_on_test(node_m2m_cap->streamoff(m2m_q.g_type()));
1741
1742 if (node->codec_mask & STATEFUL_ENCODER) {
1743 struct v4l2_encoder_cmd cmd;
1744 buffer buf_cap(m2m_q);
1745
1746 memset(&cmd, 0, sizeof(cmd));
1747 cmd.cmd = V4L2_ENC_CMD_STOP;
1748
1749 /* No buffers are queued, call STREAMON, then STOP */
1750 fail_on_test(node->streamon(q.g_type()));
1751 fail_on_test(node_m2m_cap->streamon(m2m_q.g_type()));
1752 fail_on_test(doioctl(node, VIDIOC_ENCODER_CMD, &cmd));
1753
1754 fail_on_test(buf_cap.querybuf(node, 0));
1755 fail_on_test(buf_cap.qbuf(node));
1756 fail_on_test(buf_cap.dqbuf(node));
1757 fail_on_test(!(buf_cap.g_flags() & V4L2_BUF_FLAG_LAST));
1758 for (unsigned p = 0; p < buf_cap.g_num_planes(); p++)
1759 fail_on_test(buf_cap.g_bytesused(p));
1760 fail_on_test(node->streamoff(q.g_type()));
1761 fail_on_test(node_m2m_cap->streamoff(m2m_q.g_type()));
1762
1763 /* Call STREAMON, queue one CAPTURE buffer, then STOP */
1764 fail_on_test(node->streamon(q.g_type()));
1765 fail_on_test(node_m2m_cap->streamon(m2m_q.g_type()));
1766 fail_on_test(buf_cap.querybuf(node, 0));
1767 fail_on_test(buf_cap.qbuf(node));
1768 fail_on_test(doioctl(node, VIDIOC_ENCODER_CMD, &cmd));
1769
1770 fail_on_test(buf_cap.dqbuf(node));
1771 fail_on_test(!(buf_cap.g_flags() & V4L2_BUF_FLAG_LAST));
1772 for (unsigned p = 0; p < buf_cap.g_num_planes(); p++)
1773 fail_on_test(buf_cap.g_bytesused(p));
1774 fail_on_test(node->streamoff(q.g_type()));
1775 fail_on_test(node_m2m_cap->streamoff(m2m_q.g_type()));
1776 }
1777
1778 if (node->codec_mask & STATEFUL_DECODER) {
1779 struct v4l2_decoder_cmd cmd;
1780 buffer buf_cap(m2m_q);
1781
1782 memset(&cmd, 0, sizeof(cmd));
1783 cmd.cmd = V4L2_DEC_CMD_STOP;
1784
1785 /* No buffers are queued, call STREAMON, then STOP */
1786 fail_on_test(node->streamon(q.g_type()));
1787 fail_on_test(node_m2m_cap->streamon(m2m_q.g_type()));
1788 fail_on_test(doioctl(node, VIDIOC_DECODER_CMD, &cmd));
1789
1790 fail_on_test(buf_cap.querybuf(node, 0));
1791 fail_on_test(buf_cap.qbuf(node));
1792 fail_on_test(buf_cap.dqbuf(node));
1793 fail_on_test(!(buf_cap.g_flags() & V4L2_BUF_FLAG_LAST));
1794 for (unsigned p = 0; p < buf_cap.g_num_planes(); p++)
1795 fail_on_test(buf_cap.g_bytesused(p));
1796 fail_on_test(node->streamoff(q.g_type()));
1797 fail_on_test(node_m2m_cap->streamoff(m2m_q.g_type()));
1798
1799 /* Call STREAMON, queue one CAPTURE buffer, then STOP */
1800 fail_on_test(node->streamon(q.g_type()));
1801 fail_on_test(node_m2m_cap->streamon(m2m_q.g_type()));
1802 fail_on_test(buf_cap.querybuf(node, 0));
1803 fail_on_test(buf_cap.qbuf(node));
1804 fail_on_test(doioctl(node, VIDIOC_DECODER_CMD, &cmd));
1805
1806 fail_on_test(buf_cap.dqbuf(node));
1807 fail_on_test(!(buf_cap.g_flags() & V4L2_BUF_FLAG_LAST));
1808 for (unsigned p = 0; p < buf_cap.g_num_planes(); p++)
1809 fail_on_test(buf_cap.g_bytesused(p));
1810 fail_on_test(node->streamoff(q.g_type()));
1811 fail_on_test(node_m2m_cap->streamoff(m2m_q.g_type()));
1812 }
1813
1814 if (node->supports_orphaned_bufs) {
1815 fail_on_test(q.reqbufs(node, 0));
1816 q.munmap_bufs(node);
1817 } else if (q.reqbufs(node, 0) != EBUSY) {
1818 // It's either a bug or this driver should set
1819 // V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS
1820 warn("Can free buffers even if still mmap()ed\n");
1821 q.munmap_bufs(node);
1822 } else {
1823 q.munmap_bufs(node);
1824 fail_on_test(q.reqbufs(node, 0));
1825 }
1826
1827 if (node->is_m2m) {
1828 if (node->supports_orphaned_bufs) {
1829 fail_on_test(m2m_q.reqbufs(node, 0));
1830 m2m_q.munmap_bufs(node_m2m_cap);
1831 } else if (m2m_q.reqbufs(node, 0) != EBUSY) {
1832 // It's either a bug or this driver should set
1833 // V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS
1834 warn("Can free buffers even if still mmap()ed\n");
1835 q.munmap_bufs(node);
1836 } else {
1837 m2m_q.munmap_bufs(node_m2m_cap);
1838 fail_on_test(m2m_q.reqbufs(node_m2m_cap, 0));
1839 }
1840 fail_on_test(!capture_count);
1841 }
1842 stream_close();
1843 }
1844 return 0;
1845 }
1846
1847 static int setupUserPtr(struct node *node, cv4l_queue &q)
1848 {
1849 for (unsigned i = 0; i < q.g_buffers(); i++) {
1850 buffer buf(q);
1851 unsigned int flags;
1852 int ret;
1853
1854 fail_on_test(buf.querybuf(node, i));
1855 fail_on_test(buf.check(q, Unqueued, i));
1856
1857 flags = buf.g_flags();
1858 flags |= V4L2_BUF_FLAG_NO_CACHE_INVALIDATE;
1859 flags |= V4L2_BUF_FLAG_NO_CACHE_CLEAN;
1860 buf.s_flags(flags);
1861
1862 for (unsigned p = 0; p < buf.g_num_planes(); p++) {
1863 // This should not work!
1864 fail_on_test(node->mmap(buf.g_length(p), 0) != MAP_FAILED);
1865 }
1866
1867 ret = ENOTTY;
1868 // Try to use VIDIOC_PREPARE_BUF for every other buffer
1869 if ((i & 1) == 0) {
1870 for (unsigned p = 0; p < buf.g_num_planes(); p++)
1871 buf.s_userptr(nullptr, p);
1872 ret = buf.prepare_buf(node);
1873 fail_on_test(!ret);
1874 for (unsigned p = 0; p < buf.g_num_planes(); p++)
1875 buf.s_userptr((char *)0x0eadb000 + buf.g_length(p), p);
1876 ret = buf.prepare_buf(node);
1877 fail_on_test(!ret);
1878 for (unsigned p = 0; p < buf.g_num_planes(); p++)
1879 buf.s_userptr(q.g_userptr(i, p), p);
1880 ret = buf.prepare_buf(node, q);
1881 fail_on_test_val(ret && ret != ENOTTY, ret);
1882
1883 if (ret == 0) {
1884 fail_on_test(buf.querybuf(node, i));
1885 fail_on_test(buf.check(q, Prepared, i));
1886 for (unsigned p = 0; p < buf.g_num_planes(); p++) {
1887 buf.s_userptr(nullptr, p);
1888 buf.s_bytesused(0, p);
1889 buf.s_length(0, p);
1890 }
1891 }
1892 }
1893 if (ret == ENOTTY) {
1894 for (unsigned p = 0; p < buf.g_num_planes(); p++)
1895 buf.s_userptr(nullptr, p);
1896 ret = buf.qbuf(node);
1897 fail_on_test(!ret);
1898
1899 for (unsigned p = 0; p < buf.g_num_planes(); p++)
1900 buf.s_userptr((char *)0x0eadb000 + buf.g_length(p), p);
1901 ret = buf.qbuf(node);
1902 fail_on_test(!ret);
1903
1904 for (unsigned p = 0; p < buf.g_num_planes(); p++)
1905 buf.s_userptr(q.g_userptr(i, p), p);
1906 }
1907
1908 if ((i & 1) == 0)
1909 fail_on_test(buf.qbuf(node));
1910 else
1911 fail_on_test(buf.qbuf(node, q));
1912
1913 for (unsigned p = 0; p < buf.g_num_planes(); p++) {
1914 fail_on_test(buf.g_userptr(p) != q.g_userptr(i, p));
1915 fail_on_test(buf.g_length(p) != q.g_length(p));
1916 if (v4l_type_is_output(q.g_type()))
1917 fail_on_test(!buf.g_bytesused(p));
1918 }
1919 flags = buf.g_flags();
1920 fail_on_test(flags & V4L2_BUF_FLAG_NO_CACHE_INVALIDATE);
1921 fail_on_test(flags & V4L2_BUF_FLAG_NO_CACHE_CLEAN);
1922 fail_on_test(flags & V4L2_BUF_FLAG_DONE);
1923 fail_on_test(buf.querybuf(node, i));
1924 fail_on_test(buf.check(q, Queued, i));
1925 }
1926 return 0;
1927 }
1928
1929 int testUserPtr(struct node *node, struct node *node_m2m_cap, unsigned frame_count,
1930 enum poll_mode pollmode)
1931 {
1932 const __u32 filler = 0xdeadbeef;
1933 bool can_stream = node->g_caps() & V4L2_CAP_STREAMING;
1934 int type;
1935 int ret;
1936
1937 if (!node->valid_buftypes)
1938 return ENOTTY;
1939
1940 buffer_info.clear();
1941 for (type = 0; type <= V4L2_BUF_TYPE_LAST; type++) {
1942 if (!(node->valid_buftypes & (1 << type)))
1943 continue;
1944 if (v4l_type_is_overlay(type))
1945 continue;
1946 if (node->is_m2m && !v4l_type_is_output(type))
1947 continue;
1948
1949 cv4l_queue q(type, V4L2_MEMORY_USERPTR);
1950 cv4l_queue m2m_q(v4l_type_invert(type));
1951
1952 if (testSetupVbi(node, type))
1953 continue;
1954
1955 stream_close();
1956 ret = q.reqbufs(node, 0);
1957 if (ret) {
1958 fail_on_test_val(!can_stream && ret != ENOTTY, ret);
1959 fail_on_test_val(can_stream && ret != EINVAL, ret);
1960 return ENOTTY;
1961 }
1962 fail_on_test(!can_stream);
1963
1964 q.init(type, V4L2_MEMORY_USERPTR);
1965 fail_on_test(q.reqbufs(node, 2));
1966 fail_on_test(node->streamoff(q.g_type()));
1967 last_seq.init();
1968 if (node->is_video)
1969 last_seq.last_field = cur_fmt.g_field();
1970 if (v4l_type_is_output(type))
1971 stream_for_fmt(cur_fmt.g_pixelformat());
1972
1973 __u32 *buffers[q.g_buffers()][q.g_num_planes()];
1974
1975 /*
1976 * The alignment malloc uses depends on the gcc version and
1977 * architecture. Applications compiled for 64-bit all use a
1978 * 16 byte alignment. Applications compiled for 32-bit will
1979 * use an 8 byte alignment if glibc was compiled with gcc
1980 * version 6 or older, and 16 bytes when compiled with a newer
1981 * gcc. This is due to the support for _float128 that gcc
1982 * added in version 7 and that required this alignment change.
1983 *
1984 * Bottom line, in order to test user pointers the assumption
1985 * has to be that the DMA can handle writing just 8 bytes to a
1986 * page, since that's the worst case scenario.
1987 */
1988 for (unsigned i = 0; i < q.g_buffers(); i++) {
1989 for (unsigned p = 0; p < q.g_num_planes(); p++) {
1990 /* ensure that len is a multiple of 4 */
1991 __u32 len = ((q.g_length(p) + 3) & ~0x3) + 4 * 4096;
1992 auto m = static_cast<__u32 *>(malloc(len));
1993
1994 fail_on_test(!m);
1995 fail_on_test((uintptr_t)m & 0x7);
1996 for (__u32 *x = m; x < m + len / 4; x++)
1997 *x = filler;
1998 buffers[i][p] = m;
1999 m = m + 2 * 4096 / 4;
2000 /*
2001 * Put the start of the buffer at the last 8 bytes
2002 * of a page.
2003 */
2004 m = (__u32 *)(((uintptr_t)m & ~0xfff) | 0xff8);
2005 q.s_userptr(i, p, m);
2006 }
2007 }
2008 // captureBufs() will update these values
2009 memset(max_bytesused, 0, sizeof(max_bytesused));
2010 memset(min_data_offset, 0xff, sizeof(min_data_offset));
2011
2012 fail_on_test(setupUserPtr(node, q));
2013
2014 if (node->codec_mask & STATEFUL_DECODER) {
2015 struct v4l2_event_subscription sub = { 0 };
2016
2017 sub.type = V4L2_EVENT_SOURCE_CHANGE;
2018 fail_on_test(doioctl(node, VIDIOC_SUBSCRIBE_EVENT, &sub));
2019 }
2020
2021 fail_on_test(node->streamon(q.g_type()));
2022 fail_on_test(node->streamon(q.g_type()));
2023
2024 unsigned capture_count;
2025
2026 if (node->is_m2m) {
2027 if (node->codec_mask & STATEFUL_DECODER) {
2028 int fd_flags = fcntl(node->g_fd(), F_GETFL);
2029 struct timeval tv = { 1, 0 };
2030 fd_set efds;
2031 v4l2_event ev;
2032
2033 fcntl(node->g_fd(), F_SETFL, fd_flags | O_NONBLOCK);
2034 FD_ZERO(&efds);
2035 FD_SET(node->g_fd(), &efds);
2036 ret = select(node->g_fd() + 1, nullptr, nullptr, &efds, &tv);
2037 fail_on_test_val(ret < 0, ret);
2038 fail_on_test(ret == 0);
2039 fail_on_test(node->dqevent(ev));
2040 fcntl(node->g_fd(), F_SETFL, fd_flags);
2041 fail_on_test(ev.type != V4L2_EVENT_SOURCE_CHANGE);
2042 fail_on_test(!(ev.u.src_change.changes & V4L2_EVENT_SRC_CH_RESOLUTION));
2043 }
2044 fail_on_test(setupM2M(node_m2m_cap, m2m_q));
2045 }
2046 fail_on_test(captureBufs(node, node_m2m_cap, q, m2m_q, frame_count,
2047 pollmode, capture_count));
2048 fail_on_test(node->streamoff(q.g_type()));
2049 fail_on_test(node->streamoff(q.g_type()));
2050 if (node->is_m2m) {
2051 fail_on_test(node_m2m_cap->streamoff(m2m_q.g_type()));
2052 m2m_q.munmap_bufs(node_m2m_cap);
2053 fail_on_test(m2m_q.reqbufs(node_m2m_cap, 0));
2054 fail_on_test(!capture_count);
2055 }
2056 for (unsigned i = 0; i < q.g_buffers(); i++) {
2057 for (unsigned p = 0; p < q.g_num_planes(); p++) {
2058 __u32 buflen = (q.g_length(p) + 3U) & ~3U;
2059 __u32 memlen = buflen + 4 * 4096;
2060 __u32 *m = buffers[i][p];
2061 auto u = static_cast<__u32 *>(q.g_userptr(i, p));
2062
2063 for (__u32 *x = m; x < u; x++)
2064 if (*x != filler)
2065 fail("data at %zd bytes before start of the buffer was touched\n",
2066 (u - x) * 4);
2067
2068 unsigned data_offset = min_data_offset[p];
2069 data_offset = (data_offset + 3U) & ~3U;
2070 if (!v4l_type_is_output(type) && u[data_offset / 4] == filler)
2071 fail("data at data_offset %u was untouched\n", data_offset);
2072
2073 unsigned used = max_bytesused[p];
2074 // Should never happen
2075 fail_on_test(!used);
2076 used = (used + 3U) & ~3U;
2077
2078 for (__u32 *x = u + used / 4; x < u + buflen / 4; x++) {
2079 if (*x == filler)
2080 continue;
2081 warn_once("data from max bytesused %u+%zd to length %u was touched in plane %u\n",
2082 used, (x - u) * 4 - used, buflen, p);
2083 break;
2084 }
2085 for (__u32 *x = u + buflen / 4; x < m + memlen / 4; x++)
2086 if (*x != filler)
2087 fail("data at %zd bytes after the end of the buffer was touched\n",
2088 (x - (u + buflen / 4)) * 4);
2089 free(m);
2090 q.s_userptr(i, p, nullptr);
2091 }
2092 }
2093 stream_close();
2094 }
2095 return 0;
2096 }
2097
2098 static int setupDmaBuf(struct node *expbuf_node, struct node *node,
2099 cv4l_queue &q, cv4l_queue &exp_q)
2100 {
2101 fail_on_test(exp_q.reqbufs(expbuf_node, q.g_buffers()));
2102 fail_on_test(exp_q.g_buffers() < q.g_buffers());
2103 fail_on_test(exp_q.export_bufs(expbuf_node, exp_q.g_type()));
2104
2105 for (unsigned i = 0; i < q.g_buffers(); i++) {
2106 buffer buf(q);
2107 int ret;
2108
2109 fail_on_test(buf.querybuf(node, i));
2110 fail_on_test(buf.check(q, Unqueued, i));
2111 fail_on_test(exp_q.g_num_planes() < buf.g_num_planes());
2112 for (unsigned p = 0; p < buf.g_num_planes(); p++) {
2113 if (exp_q.g_length(p) < buf.g_length(p))
2114 return fail("exp_q.g_length(%u) < buf.g_length(%u): %u < %u\n",
2115 p, p, exp_q.g_length(p), buf.g_length(p));
2116 // This should not work!
2117 fail_on_test(node->mmap(buf.g_length(p), 0) != MAP_FAILED);
2118 q.s_fd(i, p, exp_q.g_fd(i, p));
2119 }
2120
2121 for (unsigned p = 0; p < buf.g_num_planes(); p++)
2122 buf.s_fd(0xdeadbeef + q.g_fd(i, p), p);
2123 ret = buf.prepare_buf(node);
2124 fail_on_test(!ret);
2125 }
2126 fail_on_test(q.mmap_bufs(node));
2127 for (unsigned i = 0; i < q.g_buffers(); i++) {
2128 buffer buf(q);
2129 unsigned int flags;
2130 int ret;
2131
2132 buf.init(q, i);
2133 fail_on_test(buf.querybuf(node, i));
2134 for (unsigned p = 0; p < buf.g_num_planes(); p++)
2135 buf.s_fd(q.g_fd(i, p), p);
2136 flags = buf.g_flags();
2137 flags |= V4L2_BUF_FLAG_NO_CACHE_INVALIDATE;
2138 flags |= V4L2_BUF_FLAG_NO_CACHE_CLEAN;
2139 buf.s_flags(flags);
2140 ret = buf.prepare_buf(node, q);
2141 if (ret != ENOTTY) {
2142 fail_on_test_val(ret, ret);
2143 fail_on_test(buf.querybuf(node, i));
2144 fail_on_test(buf.check(q, Prepared, i));
2145 for (unsigned p = 0; p < buf.g_num_planes(); p++) {
2146 buf.s_fd(-1, p);
2147 buf.s_bytesused(0, p);
2148 buf.s_length(0, p);
2149 }
2150 }
2151
2152 fail_on_test(buf.qbuf(node, false));
2153 for (unsigned p = 0; p < buf.g_num_planes(); p++) {
2154 fail_on_test(buf.g_fd(p) != q.g_fd(i, p));
2155 fail_on_test(buf.g_length(p) != q.g_length(p));
2156 if (v4l_type_is_output(q.g_type()))
2157 fail_on_test(!buf.g_bytesused(p));
2158 }
2159 flags = buf.g_flags();
2160
2161 /* Make sure that flags are cleared */
2162 fail_on_test(flags & V4L2_BUF_FLAG_NO_CACHE_INVALIDATE);
2163 fail_on_test(flags & V4L2_BUF_FLAG_NO_CACHE_CLEAN);
2164 fail_on_test(flags & V4L2_BUF_FLAG_DONE);
2165 fail_on_test(buf.querybuf(node, i));
2166 fail_on_test(buf.check(q, Queued, i));
2167 }
2168 return 0;
2169 }
2170
2171 int testDmaBuf(struct node *expbuf_node, struct node *node, struct node *node_m2m_cap,
2172 unsigned frame_count, enum poll_mode pollmode)
2173 {
2174 bool can_stream = node->g_caps() & V4L2_CAP_STREAMING;
2175 int expbuf_type, type;
2176 int ret;
2177
2178 if (!node->valid_buftypes)
2179 return ENOTTY;
2180
2181 buffer_info.clear();
2182 for (type = 0; type <= V4L2_BUF_TYPE_LAST; type++) {
2183 if (!(node->valid_buftypes & (1 << type)))
2184 continue;
2185 if (v4l_type_is_sdr(type))
2186 continue;
2187 if (v4l_type_is_overlay(type))
2188 continue;
2189 if (node->is_m2m && !v4l_type_is_output(type))
2190 continue;
2191
2192 if (expbuf_node->g_caps() & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
2193 expbuf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
2194 else if (expbuf_node->g_caps() & V4L2_CAP_VIDEO_CAPTURE)
2195 expbuf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2196 else if (expbuf_node->g_caps() & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
2197 expbuf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
2198 else
2199 expbuf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2200
2201 cv4l_queue q(type, V4L2_MEMORY_DMABUF);
2202 cv4l_queue m2m_q(v4l_type_invert(type));
2203 cv4l_queue exp_q(expbuf_type, V4L2_MEMORY_MMAP);
2204
2205 if (testSetupVbi(node, type))
2206 continue;
2207
2208 stream_close();
2209 ret = q.reqbufs(node, 0);
2210 if (ret) {
2211 fail_on_test_val(!can_stream && ret != ENOTTY, ret);
2212 fail_on_test_val(can_stream && ret != EINVAL, ret);
2213 return ENOTTY;
2214 }
2215 fail_on_test(!can_stream);
2216
2217 fail_on_test(q.reqbufs(node, 2));
2218 fail_on_test(node->streamoff(q.g_type()));
2219 last_seq.init();
2220 if (node->is_video)
2221 last_seq.last_field = cur_fmt.g_field();
2222 if (v4l_type_is_output(type))
2223 stream_for_fmt(cur_fmt.g_pixelformat());
2224
2225 fail_on_test(setupDmaBuf(expbuf_node, node, q, exp_q));
2226
2227 if (node->codec_mask & STATEFUL_DECODER) {
2228 struct v4l2_event_subscription sub = { 0 };
2229
2230 sub.type = V4L2_EVENT_SOURCE_CHANGE;
2231 fail_on_test(doioctl(node, VIDIOC_SUBSCRIBE_EVENT, &sub));
2232 }
2233
2234 fail_on_test(node->streamon(q.g_type()));
2235 fail_on_test(node->streamon(q.g_type()));
2236
2237 unsigned capture_count;
2238
2239 if (node->is_m2m) {
2240 if (node->codec_mask & STATEFUL_DECODER) {
2241 int fd_flags = fcntl(node->g_fd(), F_GETFL);
2242 struct timeval tv = { 1, 0 };
2243 fd_set efds;
2244 v4l2_event ev;
2245
2246 fcntl(node->g_fd(), F_SETFL, fd_flags | O_NONBLOCK);
2247 FD_ZERO(&efds);
2248 FD_SET(node->g_fd(), &efds);
2249 ret = select(node->g_fd() + 1, nullptr, nullptr, &efds, &tv);
2250 fail_on_test_val(ret < 0, ret);
2251 fail_on_test(ret == 0);
2252 fail_on_test(node->dqevent(ev));
2253 fcntl(node->g_fd(), F_SETFL, fd_flags);
2254 fail_on_test(ev.type != V4L2_EVENT_SOURCE_CHANGE);
2255 fail_on_test(!(ev.u.src_change.changes & V4L2_EVENT_SRC_CH_RESOLUTION));
2256 }
2257 fail_on_test(setupM2M(node_m2m_cap, m2m_q));
2258 }
2259 fail_on_test(captureBufs(node, node_m2m_cap, q, m2m_q, frame_count,
2260 pollmode, capture_count));
2261 fail_on_test(node->streamoff(q.g_type()));
2262 fail_on_test(node->streamoff(q.g_type()));
2263 if (node->supports_orphaned_bufs) {
2264 fail_on_test(q.reqbufs(node, 0));
2265 exp_q.close_exported_fds();
2266 } else if (q.reqbufs(node, 0) != EBUSY) {
2267 // It's either a bug or this driver should set
2268 // V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS
2269 warn("Can free buffers even if exported DMABUF fds still open\n");
2270 q.munmap_bufs(node);
2271 } else {
2272 exp_q.close_exported_fds();
2273 fail_on_test(q.reqbufs(node, 0));
2274 }
2275 if (node->is_m2m) {
2276 fail_on_test(node_m2m_cap->streamoff(m2m_q.g_type()));
2277 if (node_m2m_cap->supports_orphaned_bufs) {
2278 fail_on_test(m2m_q.reqbufs(node, 0));
2279 m2m_q.munmap_bufs(node_m2m_cap);
2280 } else if (m2m_q.reqbufs(node_m2m_cap, 0) != EBUSY) {
2281 // It's either a bug or this driver should set
2282 // V4L2_BUF_CAP_SUPPORTS_ORPHANED_BUFS
2283 warn("Can free buffers even if still mmap()ed\n");
2284 q.munmap_bufs(node);
2285 } else {
2286 m2m_q.munmap_bufs(node_m2m_cap);
2287 fail_on_test(m2m_q.reqbufs(node_m2m_cap, 0));
2288 }
2289 fail_on_test(!capture_count);
2290 }
2291 stream_close();
2292 }
2293 return 0;
2294 }
2295
2296 int testRequests(struct node *node, bool test_streaming)
2297 {
2298 filehandles fhs;
2299 int media_fd = fhs.add(mi_get_media_fd(node->g_fd(), node->bus_info));
2300 int req_fd;
2301 struct test_query_ext_ctrl valid_qctrl;
2302 v4l2_ext_controls ctrls;
2303 v4l2_ext_control ctrl;
2304 v4l2_ext_control vivid_ro_ctrl = {
2305 .id = VIVID_CID_RO_INTEGER,
2306 };
2307 v4l2_ext_controls vivid_ro_ctrls = {};
2308 // Note: the vivid dynamic array has range 10-90
2309 // and the maximum number of elements is 100.
2310 __u32 vivid_dyn_array[101] = {};
2311 // Initialize with these values
2312 static const __u32 vivid_dyn_array_init[16] = {
2313 6, 12, 18, 24, 30, 36, 42, 48,
2314 54, 60, 66, 72, 78, 84, 90, 96
2315 };
2316 // This is the clamped version to compare against
2317 static const __u32 vivid_dyn_array_clamped[16] = {
2318 10, 12, 18, 24, 30, 36, 42, 48,
2319 54, 60, 66, 72, 78, 84, 90, 90
2320 };
2321 const unsigned elem_size = sizeof(vivid_dyn_array[0]);
2322 v4l2_ext_control vivid_dyn_array_ctrl = {
2323 .id = VIVID_CID_U32_DYN_ARRAY,
2324 };
2325 v4l2_ext_controls vivid_dyn_array_ctrls = {};
2326 unsigned vivid_pixel_array_size = 0;
2327 v4l2_ext_control vivid_pixel_array_ctrl = {
2328 .id = VIVID_CID_U8_PIXEL_ARRAY,
2329 };
2330 v4l2_ext_controls vivid_pixel_array_ctrls = {};
2331 bool have_controls;
2332 int ret;
2333
2334 // Note: trying to initialize vivid_ro_ctrls as was done for
2335 // vivid_ro_ctrl fails with gcc 7 with this error:
2336 // sorry, unimplemented: non-trivial designated initializers not supported
2337 // So just set this struct the old-fashioned way.
2338 vivid_ro_ctrls.which = V4L2_CTRL_WHICH_REQUEST_VAL;
2339 vivid_ro_ctrls.count = 1;
2340 vivid_ro_ctrls.controls = &vivid_ro_ctrl;
2341
2342 if (is_vivid) {
2343 v4l2_query_ext_ctrl qec = { .id = VIVID_CID_U8_PIXEL_ARRAY };
2344 node->query_ext_ctrl(qec);
2345 vivid_pixel_array_size = qec.elems;
2346 }
2347 __u8 vivid_pixel_array[vivid_pixel_array_size + 1];
2348 vivid_pixel_array[vivid_pixel_array_size] = 0xff;
2349 vivid_pixel_array_ctrl.size = vivid_pixel_array_size;
2350 vivid_pixel_array_ctrl.p_u8 = vivid_pixel_array;
2351
2352 // If requests are supported, then there must be a media device
2353 if (node->buf_caps & V4L2_BUF_CAP_SUPPORTS_REQUESTS)
2354 fail_on_test(media_fd < 0);
2355
2356 // Check if the driver has controls that can be used to test requests
2357 memset(&valid_qctrl, 0, sizeof(valid_qctrl));
2358 memset(&ctrls, 0, sizeof(ctrls));
2359 memset(&ctrl, 0, sizeof(ctrl));
2360 for (auto &control : node->controls) {
2361 test_query_ext_ctrl &qctrl = control.second;
2362
2363 if (qctrl.type != V4L2_CTRL_TYPE_INTEGER &&
2364 qctrl.type != V4L2_CTRL_TYPE_BOOLEAN)
2365 continue;
2366 if (qctrl.flags & V4L2_CTRL_FLAG_WRITE_ONLY ||
2367 qctrl.flags & V4L2_CTRL_FLAG_READ_ONLY)
2368 continue;
2369 if (is_vivid && V4L2_CTRL_ID2WHICH(qctrl.id) == V4L2_CTRL_CLASS_VIVID)
2370 continue;
2371 if (qctrl.minimum != qctrl.maximum) {
2372 valid_qctrl = qctrl;
2373 ctrl.id = qctrl.id;
2374 break;
2375 }
2376 }
2377
2378 if (ctrl.id == 0) {
2379 info("could not test the Request API, no suitable control found\n");
2380 return (node->buf_caps & V4L2_BUF_CAP_SUPPORTS_REQUESTS) ?
2381 0 : ENOTTY;
2382 }
2383
2384 // Test if V4L2_CTRL_WHICH_REQUEST_VAL is supported
2385 ctrls.which = V4L2_CTRL_WHICH_REQUEST_VAL;
2386 ret = doioctl(node, VIDIOC_G_EXT_CTRLS, &ctrls);
2387 fail_on_test_val(ret != EINVAL && ret != EBADR && ret != ENOTTY, ret);
2388 have_controls = ret != ENOTTY;
2389
2390 if (media_fd < 0 || ret == EBADR) {
2391 // Should not happen if requests are supported
2392 fail_on_test(node->buf_caps & V4L2_BUF_CAP_SUPPORTS_REQUESTS);
2393 return ENOTTY;
2394 }
2395 if (have_controls) {
2396 ctrls.request_fd = 10;
2397 // Test that querying controls with an invalid request_fd
2398 // returns EINVAL
2399 fail_on_test(doioctl(node, VIDIOC_G_EXT_CTRLS, &ctrls) != EINVAL);
2400 }
2401 ret = doioctl_fd(media_fd, MEDIA_IOC_REQUEST_ALLOC, &req_fd);
2402 if (ret == ENOTTY) {
2403 // Should not happen if requests are supported
2404 fail_on_test(node->buf_caps & V4L2_BUF_CAP_SUPPORTS_REQUESTS);
2405 return ENOTTY;
2406 }
2407 // Check that a request was allocated with a valid fd
2408 fail_on_test_val(ret, ret);
2409 fail_on_test(req_fd < 0);
2410 fhs.add(req_fd);
2411 if (have_controls) {
2412 ctrls.request_fd = req_fd;
2413 // The request is in unqueued state, so this should return EACCES
2414 fail_on_test(doioctl(node, VIDIOC_G_EXT_CTRLS, &ctrls) != EACCES);
2415 }
2416 // You cannot queue a request that has no buffer
2417 fail_on_test(doioctl_fd(req_fd, MEDIA_REQUEST_IOC_QUEUE, nullptr) != ENOENT);
2418 // REINIT must work for an unqueued request
2419 fail_on_test(doioctl_fd(req_fd, MEDIA_REQUEST_IOC_REINIT, nullptr));
2420 // Close media_fd
2421 fhs.del(media_fd);
2422 // This should have no effect on req_fd
2423 fail_on_test(doioctl_fd(req_fd, MEDIA_REQUEST_IOC_QUEUE, nullptr) != ENOENT);
2424 fail_on_test(doioctl_fd(req_fd, MEDIA_REQUEST_IOC_REINIT, nullptr));
2425 // Close req_fd
2426 fhs.del(req_fd);
2427 // G_EXT_CTRLS must now return EINVAL for req_fd since it no longer exists
2428 if (have_controls)
2429 fail_on_test(doioctl(node, VIDIOC_G_EXT_CTRLS, &ctrls) != EINVAL);
2430 // And the media request ioctls now must return EBADF
2431 fail_on_test(doioctl_fd(req_fd, MEDIA_REQUEST_IOC_QUEUE, nullptr) != EBADF);
2432 fail_on_test(doioctl_fd(req_fd, MEDIA_REQUEST_IOC_REINIT, nullptr) != EBADF);
2433
2434 // Open media_fd and alloc a request again
2435 media_fd = fhs.add(mi_get_media_fd(node->g_fd(), node->bus_info));
2436 fail_on_test(doioctl_fd(media_fd, MEDIA_IOC_REQUEST_ALLOC, &req_fd));
2437 fhs.add(req_fd);
2438 ctrls.count = 1;
2439 ctrls.controls = &ctrl;
2440 if (have_controls) {
2441 ctrl.value = valid_qctrl.minimum;
2442 ctrls.which = 0;
2443 // Set control without requests
2444 fail_on_test(doioctl(node, VIDIOC_S_EXT_CTRLS, &ctrls));
2445 ctrl.value = valid_qctrl.maximum;
2446 ctrls.which = V4L2_CTRL_WHICH_REQUEST_VAL;
2447 ctrls.request_fd = req_fd;
2448 // Set control for a request
2449 fail_on_test(doioctl(node, VIDIOC_S_EXT_CTRLS, &ctrls));
2450 ctrl.value = valid_qctrl.minimum;
2451 ctrls.request_fd = req_fd;
2452 // But you cannot get the value of an unqueued request
2453 fail_on_test(doioctl(node, VIDIOC_G_EXT_CTRLS, &ctrls) != EACCES);
2454 ctrls.which = 0;
2455 // But you can without a request
2456 fail_on_test(doioctl(node, VIDIOC_G_EXT_CTRLS, &ctrls));
2457 fail_on_test(ctrl.value != valid_qctrl.minimum);
2458 ctrls.request_fd = req_fd;
2459 ctrls.which = V4L2_CTRL_WHICH_REQUEST_VAL;
2460 ctrl.id = 1;
2461 // Setting an invalid control in a request must fail
2462 fail_on_test(!doioctl(node, VIDIOC_S_EXT_CTRLS, &ctrls));
2463 // And also when trying to read an invalid control of a request
2464 fail_on_test(doioctl(node, VIDIOC_G_EXT_CTRLS, &ctrls) != EACCES);
2465 }
2466 ctrl.id = valid_qctrl.id;
2467 // Close req_fd and media_fd and reopen device node
2468 fhs.del(req_fd);
2469 fhs.del(media_fd);
2470 node->reopen();
2471
2472 int type = node->g_type();
2473 // For m2m devices g_type() will return the capture type, so
2474 // we need to invert it to get the output type.
2475 // At the moment only the output type of an m2m device can use
2476 // requests.
2477 if (node->is_m2m)
2478 type = v4l_type_invert(type);
2479 if (v4l_type_is_vbi(type)) {
2480 cv4l_fmt fmt;
2481
2482 node->g_fmt(fmt, type);
2483 node->s_fmt(fmt, type);
2484 }
2485
2486 if (!(node->valid_buftypes & (1 << type))) {
2487 // If the type is not supported, then check that requests
2488 // are also not supported.
2489 fail_on_test(node->buf_caps & V4L2_BUF_CAP_SUPPORTS_REQUESTS);
2490 return ENOTTY;
2491 }
2492 bool supports_requests = node->buf_caps & V4L2_BUF_CAP_SUPPORTS_REQUESTS;
2493
2494 buffer_info.clear();
2495
2496 cv4l_queue q(type, V4L2_MEMORY_MMAP);
2497 // For m2m devices q is the output queue and m2m_q is the capture queue
2498 cv4l_queue m2m_q(v4l_type_invert(type));
2499
2500 q.init(type, V4L2_MEMORY_MMAP);
2501 fail_on_test(q.reqbufs(node, 15));
2502
2503 unsigned min_bufs = q.g_buffers();
2504 fail_on_test(q.reqbufs(node, min_bufs + 6));
2505 unsigned num_bufs = q.g_buffers();
2506 // Create twice as many requests as there are buffers
2507 unsigned num_requests = 2 * num_bufs;
2508 last_seq.init();
2509
2510 media_fd = fhs.add(mi_get_media_fd(node->g_fd(), node->bus_info));
2511
2512 // Allocate the requests
2513 for (unsigned i = 0; i < num_requests; i++) {
2514 fail_on_test(doioctl_fd(media_fd, MEDIA_IOC_REQUEST_ALLOC, &buf_req_fds[i]));
2515 fhs.add(buf_req_fds[i]);
2516 fail_on_test(buf_req_fds[i] < 0);
2517 // Check that empty requests can't be queued
2518 fail_on_test(!doioctl_fd(buf_req_fds[i], MEDIA_REQUEST_IOC_QUEUE, nullptr));
2519 }
2520 // close the media fd, should not be needed anymore
2521 fhs.del(media_fd);
2522
2523 buffer buf(q);
2524
2525 fail_on_test(buf.querybuf(node, 0));
2526 // Queue a buffer without using requests
2527 ret = buf.qbuf(node);
2528 // If this fails, then that can only happen if the queue
2529 // requires requests. In that case EBADR is returned.
2530 fail_on_test_val(ret && ret != EBADR, ret);
2531 fail_on_test(buf.querybuf(node, 1));
2532 // Now try to queue the buffer to the request
2533 buf.s_flags(V4L2_BUF_FLAG_REQUEST_FD);
2534 buf.s_request_fd(buf_req_fds[1]);
2535 // If requests are required, then this must now work
2536 // If requests are optional, then this must now fail since the
2537 // queue in is non-request mode.
2538 if (ret == EBADR)
2539 fail_on_test(buf.qbuf(node));
2540 else
2541 fail_on_test(!buf.qbuf(node));
2542
2543 // Reopen device node, clearing any pending requests
2544 node->reopen();
2545
2546 q.init(type, V4L2_MEMORY_MMAP);
2547 fail_on_test(q.reqbufs(node, num_bufs));
2548
2549 if (node->is_m2m) {
2550 // Setup the capture queue
2551 fail_on_test(m2m_q.reqbufs(node, 2));
2552 fail_on_test(node->streamon(m2m_q.g_type()));
2553
2554 buffer buf(m2m_q);
2555
2556 fail_on_test(buf.querybuf(node, 0));
2557 buf.s_flags(V4L2_BUF_FLAG_REQUEST_FD);
2558 buf.s_request_fd(buf_req_fds[0]);
2559 // Only the output queue can support requests,
2560 // so if the capture queue also supports requests,
2561 // then something is wrong.
2562 fail_on_test(!buf.qbuf(node));
2563 fail_on_test(node->streamoff(m2m_q.g_type()));
2564 fail_on_test(m2m_q.reqbufs(node, 0));
2565
2566 fail_on_test(setupM2M(node, m2m_q));
2567 }
2568
2569 ctrls.which = V4L2_CTRL_WHICH_REQUEST_VAL;
2570 // Test queuing buffers...
2571 for (unsigned i = 0; i < num_bufs; i++) {
2572 buffer buf(q);
2573
2574 fail_on_test(buf.querybuf(node, i));
2575 // No request was set, so this should return 0
2576 fail_on_test(buf.g_request_fd());
2577 buf.s_flags(V4L2_BUF_FLAG_REQUEST_FD);
2578 if (i == 0) {
2579 buf.s_request_fd(-1);
2580 // Can't queue to an invalid request fd
2581 fail_on_test(!buf.qbuf(node));
2582 buf.s_request_fd(0xdead);
2583 fail_on_test(!buf.qbuf(node));
2584 }
2585 buf.s_request_fd(buf_req_fds[i]);
2586 if (v4l_type_is_video(buf.g_type()))
2587 buf.s_field(V4L2_FIELD_ANY);
2588 if (!(i & 1)) {
2589 // VIDIOC_PREPARE_BUF is incompatible with requests
2590 fail_on_test(buf.prepare_buf(node) != EINVAL);
2591 buf.s_flags(0);
2592 // Test vivid error injection
2593 if (node->inject_error(VIVID_CID_BUF_PREPARE_ERROR))
2594 fail_on_test(buf.prepare_buf(node) != EINVAL);
2595 fail_on_test(buf.prepare_buf(node));
2596 fail_on_test(buf.querybuf(node, i));
2597 // Check that the buffer was prepared
2598 fail_on_test(!(buf.g_flags() & V4L2_BUF_FLAG_PREPARED));
2599 buf.s_flags(buf.g_flags() | V4L2_BUF_FLAG_REQUEST_FD);
2600 buf.s_request_fd(buf_req_fds[i]);
2601 }
2602 // Queue the buffer to the request
2603 int err = buf.qbuf(node);
2604 if (!err) {
2605 // If requests are not supported, this should fail
2606 fail_on_test(!supports_requests);
2607 // You can't queue the same buffer again
2608 fail_on_test(!buf.qbuf(node));
2609 } else {
2610 // Can only fail if requests are not supported
2611 fail_on_test(supports_requests);
2612 // and should fail with EBADR in that case
2613 fail_on_test(err != EBADR);
2614 }
2615 if (err) {
2616 // Requests are not supported, so clean up and return
2617 fail_on_test(node->streamoff(q.g_type()));
2618 fail_on_test(q.reqbufs(node, 0));
2619 if (node->is_m2m) {
2620 fail_on_test(node->streamoff(m2m_q.g_type()));
2621 m2m_q.munmap_bufs(node);
2622 fail_on_test(m2m_q.reqbufs(node, 0));
2623 }
2624 node->reopen();
2625 return ENOTTY;
2626 }
2627 // Check flags and request fd
2628 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_DONE);
2629 fail_on_test(!(buf.g_flags() & V4L2_BUF_FLAG_IN_REQUEST));
2630 fail_on_test(!(buf.g_flags() & V4L2_BUF_FLAG_REQUEST_FD));
2631 fail_on_test(buf.g_request_fd() < 0);
2632 // Query the buffer again
2633 fail_on_test(buf.querybuf(node, i));
2634 // Check returned flags and request fd
2635 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_DONE);
2636 fail_on_test(!(buf.g_flags() & V4L2_BUF_FLAG_IN_REQUEST));
2637 fail_on_test(!(buf.g_flags() & V4L2_BUF_FLAG_REQUEST_FD));
2638 fail_on_test(buf.g_request_fd() < 0);
2639 if (i & 1)
2640 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_PREPARED);
2641 else
2642 fail_on_test(!(buf.g_flags() & V4L2_BUF_FLAG_PREPARED));
2643 // Check that you can't queue it again
2644 buf.s_request_fd(buf_req_fds[i]);
2645 fail_on_test(!buf.qbuf(node));
2646
2647 // Set a control in the request, except for every third request.
2648 ctrl.value = (i & 1) ? valid_qctrl.maximum : valid_qctrl.minimum;
2649 ctrls.request_fd = buf_req_fds[i];
2650 if (i % 3 < 2)
2651 fail_on_test(doioctl(node, VIDIOC_S_EXT_CTRLS, &ctrls));
2652 if (is_vivid) {
2653 // For vivid, check modifiable array support
2654 memset(vivid_pixel_array, i, vivid_pixel_array_size);
2655 vivid_pixel_array_ctrls.which = V4L2_CTRL_WHICH_REQUEST_VAL;
2656 vivid_pixel_array_ctrls.count = 1;
2657 vivid_pixel_array_ctrls.controls = &vivid_pixel_array_ctrl;
2658 vivid_pixel_array_ctrls.request_fd = buf_req_fds[i];
2659 fail_on_test(doioctl(node, VIDIOC_S_EXT_CTRLS,
2660 &vivid_pixel_array_ctrls));
2661 fail_on_test(vivid_pixel_array[vivid_pixel_array_size] != 0xff);
2662
2663 // For vivid, check dynamic array support:
2664 vivid_dyn_array_ctrl.size = sizeof(vivid_dyn_array);
2665 vivid_dyn_array_ctrl.p_u32 = vivid_dyn_array;
2666 memset(vivid_dyn_array, 0xff, sizeof(vivid_dyn_array));
2667 vivid_dyn_array_ctrls.which = V4L2_CTRL_WHICH_REQUEST_VAL;
2668 vivid_dyn_array_ctrls.count = 1;
2669 vivid_dyn_array_ctrls.controls = &vivid_dyn_array_ctrl;
2670 vivid_dyn_array_ctrls.request_fd = buf_req_fds[i];
2671 // vivid_dyn_array_ctrl.size is too large, must return ENOSPC
2672 fail_on_test(doioctl(node, VIDIOC_S_EXT_CTRLS,
2673 &vivid_dyn_array_ctrls) != ENOSPC);
2674 // and size is set at 100 elems
2675 fail_on_test(vivid_dyn_array_ctrl.size != 100 * elem_size);
2676 // Check that the array is not overwritten
2677 fail_on_test(vivid_dyn_array[0] != 0xffffffff);
2678 if (i % 3 < 2) {
2679 unsigned size = (2 + 2 * (i % 8)) * elem_size;
2680
2681 // Set proper size, varies per request
2682 vivid_dyn_array_ctrl.size = size;
2683 memcpy(vivid_dyn_array, vivid_dyn_array_init, size);
2684 fail_on_test(doioctl(node, VIDIOC_S_EXT_CTRLS,
2685 &vivid_dyn_array_ctrls));
2686 // check that the size is as expected
2687 fail_on_test(vivid_dyn_array_ctrl.size != size);
2688 // and the array values are correctly clamped
2689 fail_on_test(memcmp(vivid_dyn_array, vivid_dyn_array_clamped, size));
2690 // and the end of the array is not overwritten
2691 fail_on_test(vivid_dyn_array[size / elem_size] != 0xffffffff);
2692 }
2693 }
2694 // Re-init the unqueued request
2695 fail_on_test(doioctl_fd(buf_req_fds[i], MEDIA_REQUEST_IOC_REINIT, nullptr));
2696
2697 // Make sure that the buffer is no longer in a request
2698 fail_on_test(buf.querybuf(node, i));
2699 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_IN_REQUEST);
2700 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_REQUEST_FD);
2701
2702 // Set the control again
2703 ctrls.request_fd = buf_req_fds[i];
2704 if (i % 3 < 2)
2705 fail_on_test(doioctl(node, VIDIOC_S_EXT_CTRLS, &ctrls));
2706 if (is_vivid && i % 3 < 2) {
2707 // Set the pixel array control again
2708 memset(vivid_pixel_array, i, vivid_pixel_array_size);
2709 vivid_pixel_array_ctrls.request_fd = buf_req_fds[i];
2710 fail_on_test(doioctl(node, VIDIOC_S_EXT_CTRLS,
2711 &vivid_pixel_array_ctrls));
2712 // Set the dynamic array control again
2713 vivid_dyn_array_ctrls.request_fd = buf_req_fds[i];
2714 vivid_dyn_array_ctrl.size = (2 + 2 * (i % 8)) * elem_size;
2715 memcpy(vivid_dyn_array, vivid_dyn_array_init,
2716 sizeof(vivid_dyn_array_init));
2717 fail_on_test(doioctl(node, VIDIOC_S_EXT_CTRLS,
2718 &vivid_dyn_array_ctrls));
2719 }
2720
2721 // After the re-init the buffer is no longer marked for
2722 // a request. If a request has been queued before (hence
2723 // the 'if (i)' check), then queuing the buffer without
2724 // a request must fail since you can't mix the two streamining
2725 // models.
2726 if (i)
2727 fail_on_test(!buf.qbuf(node));
2728 buf.s_flags(buf.g_flags() | V4L2_BUF_FLAG_REQUEST_FD);
2729 buf.s_request_fd(buf_req_fds[i]);
2730 buf.s_field(V4L2_FIELD_ANY);
2731 // Queue the buffer for the request
2732 fail_on_test(buf.qbuf(node));
2733 // Verify that drivers will replace FIELD_ANY for video output queues
2734 if (v4l_type_is_video(buf.g_type()) && v4l_type_is_output(buf.g_type()))
2735 fail_on_test(buf.g_field() == V4L2_FIELD_ANY);
2736 // Query buffer and check that it is marked as being part of a request
2737 fail_on_test(buf.querybuf(node, i));
2738 fail_on_test(!(buf.g_flags() & V4L2_BUF_FLAG_IN_REQUEST));
2739 fail_on_test(!(buf.g_flags() & V4L2_BUF_FLAG_REQUEST_FD));
2740 // Use vivid to check buffer prepare or request validation error injection
2741 if ((i & 1) && node->inject_error(i > num_bufs / 2 ?
2742 VIVID_CID_BUF_PREPARE_ERROR :
2743 VIVID_CID_REQ_VALIDATE_ERROR))
2744 fail_on_test(doioctl_fd(buf_req_fds[i],
2745 MEDIA_REQUEST_IOC_QUEUE, nullptr) != EINVAL);
2746 // Queue the request
2747 ret = doioctl_fd(buf_req_fds[i], MEDIA_REQUEST_IOC_QUEUE, nullptr);
2748 if (node->codec_mask & STATELESS_DECODER) {
2749 // Stateless decoders might require that certain
2750 // controls are present in the request. In that
2751 // case they return ENOENT and we just stop testing
2752 // since we don't know what those controls are.
2753 fail_on_test_val(ret != ENOENT, ret);
2754 test_streaming = false;
2755 break;
2756 }
2757 fail_on_test_val(ret, ret);
2758 if (i < min_bufs) {
2759 fail_on_test(buf.querybuf(node, i));
2760 // Check that the buffer is now queued up (i.e. no longer 'IN_REQUEST')
2761 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_IN_REQUEST);
2762 fail_on_test(!(buf.g_flags() & V4L2_BUF_FLAG_REQUEST_FD));
2763 fail_on_test(!(buf.g_flags() & V4L2_BUF_FLAG_QUEUED));
2764 // Re-initing or requeuing the request is no longer possible
2765 fail_on_test(doioctl_fd(buf_req_fds[i], MEDIA_REQUEST_IOC_REINIT, nullptr) != EBUSY);
2766 fail_on_test(doioctl_fd(buf_req_fds[i], MEDIA_REQUEST_IOC_QUEUE, nullptr) != EBUSY);
2767 }
2768 if (i >= min_bufs) {
2769 // Close some of the request fds to check that this
2770 // is safe to do
2771 close(buf_req_fds[i]);
2772 buf_req_fds[i] = -1;
2773 }
2774 if (i == min_bufs - 1) {
2775 // Check vivid STREAMON error injection
2776 if (node->inject_error(VIVID_CID_START_STR_ERROR))
2777 fail_on_test(!node->streamon(q.g_type()));
2778 fail_on_test(node->streamon(q.g_type()));
2779 }
2780 }
2781
2782 fail_on_test(node->g_fmt(cur_fmt, q.g_type()));
2783
2784 if (test_streaming) {
2785 unsigned capture_count;
2786
2787 // Continue streaming
2788 // For m2m devices captureBufs() behaves a bit odd: you pass
2789 // in the total number of output buffers that you want to
2790 // stream, but since there are already q.g_buffers() output
2791 // buffers queued up (see previous loop), the captureBufs()
2792 // function will subtract that from frame_count, so it will
2793 // only queue frame_count - q.g_buffers() output buffers.
2794 // In order to ensure we captured at least
2795 // min_bufs buffers we need to add min_bufs to the frame_count.
2796 fail_on_test(captureBufs(node, node, q, m2m_q,
2797 num_bufs + (node->is_m2m ? min_bufs : 0),
2798 POLL_MODE_SELECT, capture_count));
2799 }
2800 fail_on_test(node->streamoff(q.g_type()));
2801
2802 // Note that requests min_bufs...2*min_bufs-1 close their filehandles,
2803 // so here we just go through the first half of the requests.
2804 for (unsigned i = 0; test_streaming && i < min_bufs; i++) {
2805 buffer buf(q);
2806
2807 // Get the control
2808 ctrls.request_fd = buf_req_fds[i];
2809 fail_on_test(doioctl(node, VIDIOC_G_EXT_CTRLS, &ctrls));
2810 bool is_max = i & 1;
2811 // Since the control was not set for every third request,
2812 // the value will actually be that of the previous request.
2813 if (i % 3 == 2)
2814 is_max = !is_max;
2815 // Check that the value is as expected
2816 fail_on_test(ctrl.value != (is_max ? valid_qctrl.maximum :
2817 valid_qctrl.minimum));
2818 if (is_vivid) {
2819 // vivid specific: check that the read-only control
2820 // of the completed request has the expected value
2821 // (sequence number & 0xff).
2822 vivid_ro_ctrls.request_fd = buf_req_fds[i];
2823 fail_on_test(doioctl(node, VIDIOC_G_EXT_CTRLS, &vivid_ro_ctrls));
2824 // FIXME: due to unreliable sequence counters from vivid this
2825 // test fails regularly. For now replace the 'warn_once' by
2826 // 'info_once' until vivid is fixed.
2827 if (node->is_video && !node->can_output &&
2828 vivid_ro_ctrl.value != (int)i)
2829 info_once("vivid_ro_ctrl.value (%d) != i (%u)\n",
2830 vivid_ro_ctrl.value, i);
2831
2832 // Check that the dynamic control array is set as
2833 // expected and with the correct values.
2834 vivid_dyn_array_ctrls.request_fd = buf_req_fds[i];
2835 memset(vivid_dyn_array, 0xff, sizeof(vivid_dyn_array));
2836 vivid_dyn_array_ctrl.size = sizeof(vivid_dyn_array);
2837 fail_on_test(doioctl(node, VIDIOC_G_EXT_CTRLS, &vivid_dyn_array_ctrls));
2838 unsigned size = (2 + 2 * (i % 8)) * elem_size;
2839 if (i % 3 == 2)
2840 size = (2 + 2 * ((i - 1) % 8)) * elem_size;
2841 fail_on_test(vivid_dyn_array_ctrl.size != size);
2842 fail_on_test(memcmp(vivid_dyn_array, vivid_dyn_array_clamped,
2843 vivid_dyn_array_ctrl.size));
2844 fail_on_test(vivid_dyn_array[size / elem_size] != 0xffffffff);
2845 // Check that the pixel array control is set as
2846 // expected and with the correct values.
2847 vivid_pixel_array_ctrls.request_fd = buf_req_fds[i];
2848 memset(vivid_pixel_array, 0xfe, vivid_pixel_array_size);
2849 fail_on_test(doioctl(node, VIDIOC_G_EXT_CTRLS, &vivid_pixel_array_ctrls));
2850 bool ok = true;
2851 __u8 expected = (i % 3 == 2) ? i - 1 : i;
2852 for (unsigned j = 0; j < vivid_pixel_array_size; j++)
2853 if (vivid_pixel_array[j] != expected) {
2854 ok = false;
2855 break;
2856 }
2857 fail_on_test(!ok);
2858 fail_on_test(vivid_pixel_array[vivid_pixel_array_size] != 0xff);
2859 }
2860 fail_on_test(buf.querybuf(node, i));
2861 // Check that all the buffers of the stopped stream are
2862 // no longer marked as belonging to a request.
2863 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_REQUEST_FD);
2864 fail_on_test(buf.g_request_fd());
2865 struct pollfd pfd = {
2866 buf_req_fds[i],
2867 POLLPRI, 0
2868 };
2869 // Check that polling the request fd will immediately return,
2870 // indicating that the request has completed.
2871 fail_on_test(poll(&pfd, 1, 100) != 1);
2872 // Requeuing the request must fail
2873 fail_on_test(doioctl_fd(buf_req_fds[i], MEDIA_REQUEST_IOC_QUEUE, nullptr) != EBUSY);
2874 // But reinit must succeed.
2875 fail_on_test(doioctl_fd(buf_req_fds[i], MEDIA_REQUEST_IOC_REINIT, nullptr));
2876 fail_on_test(buf.querybuf(node, i));
2877 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_REQUEST_FD);
2878 fail_on_test(buf.g_request_fd());
2879 ctrls.request_fd = buf_req_fds[i];
2880 // Check that getting controls from a reinited request fails
2881 fail_on_test(!doioctl(node, VIDIOC_G_EXT_CTRLS, &ctrls));
2882 // Close the request fd
2883 fhs.del(buf_req_fds[i]);
2884 buf_req_fds[i] = -1;
2885 }
2886 // Close any remaining open request fds
2887 for (unsigned i = 0; i < num_requests; i++)
2888 if (buf_req_fds[i] >= 0)
2889 fhs.del(buf_req_fds[i]);
2890
2891 // Getting the current control value must work
2892 ctrls.which = 0;
2893 fail_on_test(doioctl(node, VIDIOC_G_EXT_CTRLS, &ctrls));
2894 // Check the final control value
2895 if (test_streaming) {
2896 bool is_max = (num_bufs - 1) & 1;
2897 if ((num_bufs - 1) % 3 == 2)
2898 is_max = !is_max;
2899 fail_on_test(ctrl.value != (is_max ? valid_qctrl.maximum :
2900 valid_qctrl.minimum));
2901 if (is_vivid) {
2902 // For vivid check the final read-only value,
2903 vivid_ro_ctrls.which = 0;
2904 fail_on_test(doioctl(node, VIDIOC_G_EXT_CTRLS, &vivid_ro_ctrls));
2905 // FIXME: due to unreliable sequence counters from vivid this
2906 // test fails regularly. For now replace the 'warn' by 'info'
2907 // until vivid is fixed.
2908 if (node->is_video && !node->can_output &&
2909 vivid_ro_ctrl.value != (int)(num_bufs - 1))
2910 info("vivid_ro_ctrl.value (%d) != num_bufs - 1 (%d)\n",
2911 vivid_ro_ctrl.value, num_bufs - 1);
2912
2913 // the final dynamic array value,
2914 v4l2_query_ext_ctrl q_dyn_array = {
2915 .id = VIVID_CID_U32_DYN_ARRAY,
2916 };
2917 fail_on_test(doioctl(node, VIDIOC_QUERY_EXT_CTRL, &q_dyn_array));
2918 unsigned elems = 2 + 2 * ((num_bufs - 1) % 8);
2919 if ((num_bufs - 1) % 3 == 2)
2920 elems = 2 + 2 * ((num_bufs - 2) % 8);
2921 fail_on_test(q_dyn_array.elems != elems);
2922 vivid_dyn_array_ctrls.which = 0;
2923 fail_on_test(doioctl(node, VIDIOC_G_EXT_CTRLS, &vivid_dyn_array_ctrls));
2924 fail_on_test(vivid_dyn_array_ctrl.size != elems * elem_size);
2925 fail_on_test(memcmp(vivid_dyn_array, vivid_dyn_array_clamped,
2926 vivid_dyn_array_ctrl.size));
2927
2928 // and the final pixel array value.
2929 vivid_pixel_array_ctrls.which = 0;
2930 fail_on_test(doioctl(node, VIDIOC_G_EXT_CTRLS, &vivid_pixel_array_ctrls));
2931 bool ok = true;
2932 __u8 expected = (num_bufs - 1) % 3 == 2 ? num_bufs - 2 : num_bufs - 1;
2933 for (unsigned j = 0; j < vivid_pixel_array_size; j++)
2934 if (vivid_pixel_array[j] != expected) {
2935 ok = false;
2936 break;
2937 }
2938 fail_on_test(!ok);
2939 }
2940 }
2941
2942 // Cleanup
2943 fail_on_test(q.reqbufs(node, 0));
2944 if (node->is_m2m) {
2945 fail_on_test(node->streamoff(m2m_q.g_type()));
2946 m2m_q.munmap_bufs(node);
2947 fail_on_test(m2m_q.reqbufs(node, 0));
2948 }
2949
2950 return 0;
2951 }
2952
2953 /* Android does not have support for pthread_cancel */
2954 #ifndef ANDROID
2955
2956 /*
2957 * This class wraps a pthread in such a way that it simplifies passing
2958 * parameters, checking completion, gracious halting, and aggressive
2959 * termination (an empty signal handler, as installed by testBlockingDQBuf,
2960 * is necessary). This alleviates the need for spaghetti error paths when
2961 * multiple potentially blocking threads are involved.
2962 */
2963 class BlockingThread
2964 {
2965 public:
2966 virtual ~BlockingThread()
2967 {
2968 stop();
2969 }
2970
2971 int start()
2972 {
2973 int ret = pthread_create(&thread, nullptr, startRoutine, this);
2974 if (ret < 0)
2975 return ret;
2976
2977 running = true;
2978 return 0;
2979 }
2980
2981 void stop()
2982 {
2983 if (!running)
2984 return;
2985
2986 /*
2987 * If the thread is blocked on an ioctl, try to wake it up with
2988 * a signal.
2989 */
2990 if (!done) {
2991 pthread_kill(thread, SIGUSR1);
2992 sleep(1);
2993 }
2994
2995 /*
2996 * If the signal failed to interrupt the ioctl, use the heavy
2997 * artillery and cancel the thread.
2998 */
2999 if (!done) {
3000 pthread_cancel(thread);
3001 sleep(1);
3002 }
3003
3004 pthread_join(thread, nullptr);
3005 running = false;
3006 }
3007
3008 std::atomic<bool> done{};
3009
3010 private:
3011 static void *startRoutine(void *arg)
3012 {
3013 auto self = static_cast<BlockingThread *>(arg);
3014
3015 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, nullptr);
3016 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, nullptr);
3017
3018 self->run();
3019
3020 self->done = true;
3021 return nullptr;
3022 }
3023
3024 virtual void run() = 0;
3025
3026 pthread_t thread;
3027 std::atomic<bool> running{};
3028 };
3029
3030 class DqbufThread : public BlockingThread
3031 {
3032 public:
3033 DqbufThread(cv4l_queue *q, struct node *n) : queue(q), node(n) {}
3034
3035 private:
3036 void run() override
3037 {
3038 /*
3039 * In this thread we call VIDIOC_DQBUF and wait indefinitely
3040 * since no buffers are queued.
3041 */
3042 cv4l_buffer buf(queue->g_type(), V4L2_MEMORY_MMAP);
3043 node->dqbuf(buf);
3044 }
3045
3046 cv4l_queue *queue;
3047 struct node *node;
3048 };
3049
3050 class StreamoffThread : public BlockingThread
3051 {
3052 public:
3053 StreamoffThread(cv4l_queue *q, struct node *n) : queue(q), node(n) {}
3054
3055 private:
3056 void run() override
3057 {
3058 /*
3059 * In this thread call STREAMOFF; this shouldn't
3060 * be blocked by the DQBUF!
3061 */
3062 node->streamoff(queue->g_type());
3063 }
3064
3065 cv4l_queue *queue;
3066 struct node *node;
3067 };
3068
3069 static void pthread_sighandler(int sig)
3070 {
3071 }
3072
3073 static int testBlockingDQBuf(struct node *node, cv4l_queue &q)
3074 {
3075 DqbufThread thread_dqbuf(&q, node);
3076 StreamoffThread thread_streamoff(&q, node);
3077
3078 /*
3079 * SIGUSR1 is ignored by default, so install an empty signal handler
3080 * so that we can use SIGUSR1 to wake up threads potentially blocked
3081 * on ioctls.
3082 */
3083 signal(SIGUSR1, pthread_sighandler);
3084
3085 fail_on_test(q.reqbufs(node, 2));
3086 fail_on_test(node->streamon(q.g_type()));
3087
3088 /*
3089 * This test checks if a blocking wait in VIDIOC_DQBUF doesn't block
3090 * other ioctls.
3091 *
3092 * If this fails, check that the vb2_ops wait_prepare/finish callbacks
3093 * are set.
3094 */
3095 fflush(stdout);
3096 thread_dqbuf.start();
3097
3098 /* Wait for the child thread to start and block */
3099 sleep(1);
3100 /* Check that it is really blocking */
3101 fail_on_test(thread_dqbuf.done);
3102
3103 fflush(stdout);
3104 thread_streamoff.start();
3105
3106 /* Wait for the second child to start and exit */
3107 sleep(3);
3108 fail_on_test(!thread_streamoff.done);
3109
3110 fail_on_test(node->streamoff(q.g_type()));
3111 fail_on_test(q.reqbufs(node, 0));
3112 return 0;
3113 }
3114
3115 #endif //ANDROID
3116
3117 int testBlockingWait(struct node *node)
3118 {
3119 bool can_stream = node->g_caps() & V4L2_CAP_STREAMING;
3120 int type;
3121
3122 if (!can_stream || !node->valid_buftypes)
3123 return ENOTTY;
3124
3125 buffer_info.clear();
3126 for (type = 0; type <= V4L2_BUF_TYPE_LAST; type++) {
3127 if (!(node->valid_buftypes & (1 << type)))
3128 continue;
3129 if (v4l_type_is_overlay(type))
3130 continue;
3131
3132 cv4l_queue q(type, V4L2_MEMORY_MMAP);
3133 cv4l_queue m2m_q(v4l_type_invert(type), V4L2_MEMORY_MMAP);
3134
3135 if (testSetupVbi(node, type))
3136 continue;
3137
3138 #ifndef ANDROID
3139 fail_on_test(testBlockingDQBuf(node, q));
3140 if (node->is_m2m)
3141 fail_on_test(testBlockingDQBuf(node, m2m_q));
3142 #endif
3143 }
3144 return 0;
3145 }
3146
3147 static void restoreCropCompose(struct node *node, __u32 field,
3148 v4l2_selection &crop, v4l2_selection &compose)
3149 {
3150 if (node->has_inputs) {
3151 /*
3152 * For capture restore the compose rectangle
3153 * before the crop rectangle.
3154 */
3155 if (compose.r.width)
3156 node->s_frame_selection(compose, field);
3157 if (crop.r.width)
3158 node->s_frame_selection(crop, field);
3159 } else {
3160 /*
3161 * For output the crop rectangle should be
3162 * restored before the compose rectangle.
3163 */
3164 if (crop.r.width)
3165 node->s_frame_selection(crop, field);
3166 if (compose.r.width)
3167 node->s_frame_selection(compose, field);
3168 }
3169 }
3170
3171 int restoreFormat(struct node *node)
3172 {
3173 cv4l_fmt fmt;
3174 unsigned h;
3175
3176 node->g_fmt(fmt);
3177
3178 h = fmt.g_frame_height();
3179 if (node->cur_io_caps & V4L2_IN_CAP_STD) {
3180 v4l2_std_id std;
3181
3182 fail_on_test(node->g_std(std));
3183 fmt.s_width(720);
3184 h = (std & V4L2_STD_525_60) ? 480 : 576;
3185 }
3186 if (node->cur_io_caps & V4L2_IN_CAP_DV_TIMINGS) {
3187 v4l2_dv_timings timings;
3188
3189 fail_on_test(node->g_dv_timings(timings));
3190 fmt.s_width(timings.bt.width);
3191 h = timings.bt.height;
3192 }
3193 if (node->cur_io_caps & V4L2_IN_CAP_NATIVE_SIZE) {
3194 v4l2_selection sel = {
3195 node->g_selection_type(),
3196 V4L2_SEL_TGT_NATIVE_SIZE,
3197 };
3198
3199 fail_on_test(node->g_selection(sel));
3200 fmt.s_width(sel.r.width);
3201 h = sel.r.height;
3202 }
3203 fmt.s_frame_height(h);
3204 /* First restore the format */
3205 node->s_fmt(fmt);
3206
3207 v4l2_selection sel_compose = {
3208 node->g_selection_type(),
3209 V4L2_SEL_TGT_COMPOSE,
3210 };
3211 v4l2_selection sel_crop = {
3212 node->g_selection_type(),
3213 V4L2_SEL_TGT_CROP,
3214 };
3215 sel_compose.r.width = fmt.g_width();
3216 sel_compose.r.height = fmt.g_frame_height();
3217 sel_crop.r.width = fmt.g_width();
3218 sel_crop.r.height = fmt.g_frame_height();
3219 restoreCropCompose(node, fmt.g_field(), sel_crop, sel_compose);
3220 return 0;
3221 }
3222
3223 static int testStreaming(struct node *node, unsigned frame_count)
3224 {
3225 int type = node->g_type();
3226
3227 if (!(node->valid_buftypes & (1 << type)))
3228 return ENOTTY;
3229
3230 buffer_info.clear();
3231
3232 cur_fmt.s_type(type);
3233 node->g_fmt(cur_fmt);
3234
3235 bool is_output = v4l_type_is_output(type);
3236
3237 if (node->g_caps() & V4L2_CAP_STREAMING) {
3238 cv4l_queue q(type, V4L2_MEMORY_MMAP);
3239 bool alternate = cur_fmt.g_field() == V4L2_FIELD_ALTERNATE;
3240 v4l2_std_id std = 0;
3241
3242 node->g_std(std);
3243
3244 unsigned field = cur_fmt.g_first_field(std);
3245 cv4l_buffer buf(q);
3246
3247 if (is_output)
3248 stream_for_fmt(cur_fmt.g_pixelformat());
3249
3250 fail_on_test(q.reqbufs(node, 3));
3251 fail_on_test(q.obtain_bufs(node));
3252 for (unsigned i = 0; i < q.g_buffers(); i++) {
3253 buf.init(q, i);
3254 buf.s_field(field);
3255 if (alternate)
3256 field ^= 1;
3257 if (is_output &&
3258 !fill_output_buffer(q, buf))
3259 return 0;
3260 fail_on_test(node->qbuf(buf));
3261 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_DONE);
3262 }
3263 fail_on_test(node->streamon());
3264
3265 while (node->dqbuf(buf) == 0) {
3266 if (!no_progress)
3267 printf("\r\t\t%s: Frame #%03d Field %s ",
3268 buftype2s(q.g_type()).c_str(),
3269 buf.g_sequence(), field2s(buf.g_field()).c_str());
3270 fflush(stdout);
3271 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_DONE);
3272 buf.s_field(field);
3273 if (alternate)
3274 field ^= 1;
3275 if (is_output &&
3276 !fill_output_buffer(q, buf))
3277 return 0;
3278 fail_on_test(node->qbuf(buf));
3279 fail_on_test(buf.g_flags() & V4L2_BUF_FLAG_DONE);
3280 if (--frame_count == 0)
3281 break;
3282 }
3283 q.free(node);
3284 if (is_output)
3285 stream_close();
3286 if (!no_progress)
3287 printf("\r\t\t ");
3288 return 0;
3289 }
3290 fail_on_test(!(node->g_caps() & V4L2_CAP_READWRITE));
3291
3292 int size = cur_fmt.g_sizeimage();
3293 void *tmp = malloc(size);
3294
3295 for (unsigned i = 0; i < frame_count; i++) {
3296 int ret;
3297
3298 if (node->can_capture)
3299 ret = node->read(tmp, size);
3300 else
3301 ret = node->write(tmp, size);
3302 fail_on_test(ret != size);
3303 if (!no_progress)
3304 printf("\r\t\t%s: Frame #%03d", buftype2s(type).c_str(), i);
3305 fflush(stdout);
3306 }
3307 if (!no_progress)
3308 printf("\r\t\t ");
3309 return 0;
3310 }
3311
3312 /*
3313 * Remember which combination of fmt, crop and compose rectangles have been
3314 * used to test streaming.
3315 * This helps prevent duplicate streaming tests.
3316 */
3317 struct selTest {
3318 unsigned fmt_w, fmt_h, fmt_field;
3319 unsigned crop_w, crop_h;
3320 unsigned compose_w, compose_h;
3321 };
3322
3323 static std::vector<selTest> selTests;
3324
3325 static selTest createSelTest(const cv4l_fmt &fmt,
3326 const v4l2_selection &crop, const v4l2_selection &compose)
3327 {
3328 selTest st = {
3329 fmt.g_width(), fmt.g_height(), fmt.g_field(),
3330 crop.r.width, crop.r.height,
3331 compose.r.width, compose.r.height
3332 };
3333
3334 return st;
3335 }
3336
3337 static selTest createSelTest(struct node *node)
3338 {
3339 v4l2_selection crop = {
3340 node->g_selection_type(),
3341 V4L2_SEL_TGT_CROP
3342 };
3343 v4l2_selection compose = {
3344 node->g_selection_type(),
3345 V4L2_SEL_TGT_COMPOSE
3346 };
3347 cv4l_fmt fmt;
3348
3349 node->g_fmt(fmt);
3350 node->g_selection(crop);
3351 node->g_selection(compose);
3352 return createSelTest(fmt, crop, compose);
3353 }
3354
3355 static bool haveSelTest(const selTest &test)
3356 {
3357 return std::any_of(selTests.begin(), selTests.end(), [&](const selTest &selfTest)
3358 { return &selfTest != &test; });
3359 }
3360
3361 static void streamFmtRun(struct node *node, cv4l_fmt &fmt, unsigned frame_count,
3362 bool testSelection = false)
3363 {
3364 v4l2_selection crop = {
3365 node->g_selection_type(),
3366 V4L2_SEL_TGT_CROP
3367 };
3368 v4l2_selection compose = {
3369 node->g_selection_type(),
3370 V4L2_SEL_TGT_COMPOSE
3371 };
3372 bool has_compose = node->cur_io_has_compose();
3373 bool has_crop = node->cur_io_has_crop();
3374 char s_crop[32] = "";
3375 char s_compose[32] = "";
3376
3377 if (has_crop) {
3378 node->g_frame_selection(crop, fmt.g_field());
3379 sprintf(s_crop, "Crop (%d,%d)/%ux%u, ",
3380 crop.r.left, crop.r.top,
3381 crop.r.width, crop.r.height);
3382 }
3383 if (has_compose) {
3384 node->g_frame_selection(compose, fmt.g_field());
3385 sprintf(s_compose, "Compose (%d,%d)/%ux%u, ",
3386 compose.r.left, compose.r.top,
3387 compose.r.width, compose.r.height);
3388 }
3389 printf("\r\t\t%s%sStride %u, Field %s%s: %s \n",
3390 s_crop, s_compose,
3391 fmt.g_bytesperline(),
3392 field2s(fmt.g_field()).c_str(),
3393 testSelection ? ", SelTest" : "",
3394 ok(testStreaming(node, frame_count)));
3395 node->reopen();
3396 }
3397
3398 static void streamFmt(struct node *node, __u32 pixelformat, __u32 w, __u32 h,
3399 v4l2_fract *f, unsigned frame_count)
3400 {
3401 const char *op = (node->g_caps() & V4L2_CAP_STREAMING) ? "MMAP" :
3402 (node->can_capture ? "read()" : "write()");
3403 bool has_compose = node->cur_io_has_compose();
3404 bool has_crop = node->cur_io_has_crop();
3405 __u32 default_field;
3406 v4l2_selection crop = {
3407 node->g_selection_type(),
3408 V4L2_SEL_TGT_CROP
3409 };
3410 v4l2_selection min_crop, max_crop;
3411 v4l2_selection compose = {
3412 node->g_selection_type(),
3413 V4L2_SEL_TGT_COMPOSE
3414 };
3415 v4l2_selection min_compose, max_compose;
3416 cv4l_fmt fmt;
3417 char hz[32] = "";
3418
3419 if (!frame_count)
3420 frame_count = f ? static_cast<unsigned>(1.0 / fract2f(f)) : 10;
3421 node->g_fmt(fmt);
3422 fmt.s_pixelformat(pixelformat);
3423 fmt.s_width(w);
3424 fmt.s_field(V4L2_FIELD_ANY);
3425 fmt.s_height(h);
3426 node->try_fmt(fmt);
3427 default_field = fmt.g_field();
3428 fmt.s_frame_height(h);
3429 node->s_fmt(fmt);
3430 if (f) {
3431 node->set_interval(*f);
3432 sprintf(hz, "@%.2f Hz", 1.0 / fract2f(f));
3433 }
3434
3435 printf("\ttest %s for Format %s, Frame Size %ux%u%s:\n", op,
3436 fcc2s(pixelformat).c_str(),
3437 fmt.g_width(), fmt.g_frame_height(), hz);
3438
3439 if (has_crop)
3440 node->g_frame_selection(crop, fmt.g_field());
3441 if (has_compose)
3442 node->g_frame_selection(compose, fmt.g_field());
3443
3444 for (unsigned field = V4L2_FIELD_NONE;
3445 field <= V4L2_FIELD_INTERLACED_BT; field++) {
3446 node->g_fmt(fmt);
3447 fmt.s_field(field);
3448 fmt.s_width(w);
3449 fmt.s_frame_height(h);
3450 node->s_fmt(fmt);
3451 if (fmt.g_field() != field)
3452 continue;
3453
3454 restoreCropCompose(node, fmt.g_field(), crop, compose);
3455 streamFmtRun(node, fmt, frame_count);
3456
3457 // Test if the driver allows for user-specified 'bytesperline' values
3458 node->g_fmt(fmt);
3459 unsigned bpl = fmt.g_bytesperline();
3460 unsigned size = fmt.g_sizeimage();
3461 fmt.s_bytesperline(bpl + 64);
3462 node->s_fmt(fmt, false);
3463 if (fmt.g_bytesperline() == bpl)
3464 continue;
3465 if (fmt.g_sizeimage() <= size)
3466 fail("fmt.g_sizeimage() <= size\n");
3467 streamFmtRun(node, fmt, frame_count);
3468 }
3469
3470 fmt.s_field(default_field);
3471 fmt.s_frame_height(h);
3472 node->s_fmt(fmt);
3473 restoreCropCompose(node, fmt.g_field(), crop, compose);
3474
3475 if (has_crop) {
3476 min_crop = crop;
3477 min_crop.r.width = 0;
3478 min_crop.r.height = 0;
3479 node->s_frame_selection(min_crop, fmt.g_field());
3480 node->s_fmt(fmt);
3481 node->g_frame_selection(min_crop, fmt.g_field());
3482 max_crop = crop;
3483 max_crop.r.width = ~0;
3484 max_crop.r.height = ~0;
3485 node->s_frame_selection(max_crop, fmt.g_field());
3486 node->s_fmt(fmt);
3487 node->g_frame_selection(max_crop, fmt.g_field());
3488 restoreCropCompose(node, fmt.g_field(), crop, compose);
3489 }
3490
3491 if (has_compose) {
3492 min_compose = compose;
3493 min_compose.r.width = 0;
3494 min_compose.r.height = 0;
3495 node->s_frame_selection(min_compose, fmt.g_field());
3496 node->s_fmt(fmt);
3497 node->g_frame_selection(min_compose, fmt.g_field());
3498 max_compose = compose;
3499 max_compose.r.width = ~0;
3500 max_compose.r.height = ~0;
3501 node->s_frame_selection(max_compose, fmt.g_field());
3502 node->s_fmt(fmt);
3503 node->g_frame_selection(max_compose, fmt.g_field());
3504 restoreCropCompose(node, fmt.g_field(), crop, compose);
3505 }
3506
3507 if (min_crop.r.width == max_crop.r.width &&
3508 min_crop.r.height == max_crop.r.height)
3509 has_crop = false;
3510 if (min_compose.r.width == max_compose.r.width &&
3511 min_compose.r.height == max_compose.r.height)
3512 has_compose = false;
3513
3514 if (!has_crop && !has_compose)
3515 return;
3516
3517 if (!has_compose) {
3518 cv4l_fmt tmp;
3519
3520 node->s_frame_selection(min_crop, fmt.g_field());
3521 node->g_fmt(tmp);
3522 if (tmp.g_width() != fmt.g_width() ||
3523 tmp.g_height() != fmt.g_height())
3524 fail("Format resolution changed after changing to min crop\n");
3525 selTest test = createSelTest(node);
3526 if (!haveSelTest(test)) {
3527 selTests.push_back(test);
3528 streamFmtRun(node, fmt, frame_count, true);
3529 }
3530 node->s_frame_selection(max_crop, fmt.g_field());
3531 node->g_fmt(tmp);
3532 if (tmp.g_width() != fmt.g_width() ||
3533 tmp.g_height() != fmt.g_height())
3534 fail("Format resolution changed after changing to max crop\n");
3535 test = createSelTest(node);
3536 if (!haveSelTest(test)) {
3537 selTests.push_back(test);
3538 streamFmtRun(node, fmt, frame_count, true);
3539 }
3540 restoreCropCompose(node, fmt.g_field(), crop, compose);
3541 return;
3542 }
3543 if (!has_crop) {
3544 cv4l_fmt tmp;
3545 node->s_frame_selection(min_compose, fmt.g_field());
3546 node->g_fmt(tmp);
3547 if (tmp.g_width() != fmt.g_width() ||
3548 tmp.g_height() != fmt.g_height())
3549 fail("Format resolution changed after changing to min compose\n");
3550 selTest test = createSelTest(node);
3551 if (!haveSelTest(test)) {
3552 selTests.push_back(test);
3553 streamFmtRun(node, fmt, frame_count, true);
3554 }
3555 node->s_frame_selection(max_compose, fmt.g_field());
3556 node->g_fmt(tmp);
3557 if (tmp.g_width() != fmt.g_width() ||
3558 tmp.g_height() != fmt.g_height())
3559 fail("Format resolution changed after changing to max compose\n");
3560 test = createSelTest(node);
3561 if (!haveSelTest(test)) {
3562 selTests.push_back(test);
3563 streamFmtRun(node, fmt, frame_count, true);
3564 }
3565 restoreCropCompose(node, fmt.g_field(), crop, compose);
3566 return;
3567 }
3568
3569 v4l2_selection *selections[2][4] = {
3570 { &min_crop, &max_crop, &min_compose, &max_compose },
3571 { &min_compose, &max_compose, &min_crop, &max_crop }
3572 };
3573
3574 selTest test = createSelTest(node);
3575 if (!haveSelTest(test))
3576 selTests.push_back(test);
3577
3578 for (unsigned i = 0; i < 8; i++) {
3579 v4l2_selection *sel1 = selections[node->can_output][i & 1];
3580 v4l2_selection *sel2 = selections[node->can_output][2 + ((i & 2) >> 1)];
3581 v4l2_selection *sel3 = selections[node->can_output][(i & 4) >> 2];
3582 cv4l_fmt tmp;
3583
3584 restoreCropCompose(node, fmt.g_field(), crop, compose);
3585 node->s_frame_selection(*sel1, fmt.g_field());
3586 node->g_fmt(tmp);
3587 if (tmp.g_width() != fmt.g_width() ||
3588 tmp.g_height() != fmt.g_height())
3589 fail("Format resolution changed after changing first selection\n");
3590 selTest test = createSelTest(node);
3591 if (!haveSelTest(test)) {
3592 selTests.push_back(test);
3593 streamFmtRun(node, fmt, frame_count, true);
3594 }
3595
3596 node->s_frame_selection(*sel2, fmt.g_field());
3597 node->g_fmt(tmp);
3598 if (tmp.g_width() != fmt.g_width() ||
3599 tmp.g_height() != fmt.g_height())
3600 fail("Format resolution changed after changing second selection\n");
3601 test = createSelTest(node);
3602 if (!haveSelTest(test)) {
3603 selTests.push_back(test);
3604 streamFmtRun(node, fmt, frame_count, true);
3605 }
3606
3607 node->s_frame_selection(*sel3, fmt.g_field());
3608 node->g_fmt(tmp);
3609 if (tmp.g_width() != fmt.g_width() ||
3610 tmp.g_height() != fmt.g_height())
3611 fail("Format resolution changed after changing third selection\n");
3612 test = createSelTest(node);
3613 if (!haveSelTest(test)) {
3614 selTests.push_back(test);
3615 streamFmtRun(node, fmt, frame_count, true);
3616 }
3617 }
3618 restoreCropCompose(node, fmt.g_field(), crop, compose);
3619 }
3620
3621 static void streamIntervals(struct node *node, __u32 pixelformat, __u32 w, __u32 h,
3622 unsigned frame_count)
3623 {
3624 v4l2_frmivalenum frmival = { 0 };
3625
3626 if (node->enum_frameintervals(frmival, pixelformat, w, h)) {
3627 streamFmt(node, pixelformat, w, h, nullptr, frame_count);
3628 return;
3629 }
3630
3631 if (frmival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
3632 do {
3633 streamFmt(node, pixelformat, w, h, &frmival.discrete,
3634 frame_count);
3635 } while (!node->enum_frameintervals(frmival));
3636 return;
3637 }
3638 streamFmt(node, pixelformat, w, h, &frmival.stepwise.min, frame_count);
3639 streamFmt(node, pixelformat, w, h, &frmival.stepwise.max, frame_count);
3640 }
3641
3642 void streamAllFormats(struct node *node, unsigned frame_count)
3643 {
3644 v4l2_fmtdesc fmtdesc;
3645
3646 if (node->enum_fmt(fmtdesc, true))
3647 return;
3648 selTests.clear();
3649 do {
3650 v4l2_frmsizeenum frmsize;
3651 cv4l_fmt fmt;
3652
3653 if (node->enum_framesizes(frmsize, fmtdesc.pixelformat)) {
3654 cv4l_fmt min, max;
3655
3656 restoreFormat(node);
3657 node->g_fmt(fmt);
3658 min = fmt;
3659 min.s_width(0);
3660 min.s_height(0);
3661 node->try_fmt(min);
3662 max = fmt;
3663 max.s_width(~0);
3664 max.s_height(~0);
3665 node->try_fmt(max);
3666 if (min.g_width() != fmt.g_width() ||
3667 min.g_height() != fmt.g_height()) {
3668 streamIntervals(node, fmtdesc.pixelformat,
3669 min.g_width(), min.g_frame_height(),
3670 frame_count);
3671 restoreFormat(node);
3672 }
3673 if (max.g_width() != fmt.g_width() ||
3674 max.g_height() != fmt.g_height()) {
3675 streamIntervals(node, fmtdesc.pixelformat,
3676 max.g_width(), max.g_frame_height(),
3677 frame_count);
3678 restoreFormat(node);
3679 }
3680 streamIntervals(node, fmtdesc.pixelformat,
3681 fmt.g_width(), fmt.g_frame_height(),
3682 frame_count);
3683 continue;
3684 }
3685
3686 v4l2_frmsize_stepwise &ss = frmsize.stepwise;
3687
3688 switch (frmsize.type) {
3689 case V4L2_FRMSIZE_TYPE_DISCRETE:
3690 do {
3691 streamIntervals(node, fmtdesc.pixelformat,
3692 frmsize.discrete.width,
3693 frmsize.discrete.height,
3694 frame_count);
3695 } while (!node->enum_framesizes(frmsize));
3696 break;
3697 default:
3698 restoreFormat(node);
3699 streamIntervals(node, fmtdesc.pixelformat,
3700 ss.min_width, ss.min_height,
3701 frame_count);
3702 restoreFormat(node);
3703 if (ss.max_width != ss.min_width ||
3704 ss.max_height != ss.min_height) {
3705 streamIntervals(node, fmtdesc.pixelformat,
3706 ss.max_width, ss.max_height,
3707 frame_count);
3708 restoreFormat(node);
3709 }
3710 node->g_fmt(fmt);
3711 if (fmt.g_width() != ss.min_width ||
3712 fmt.g_frame_height() != ss.min_height) {
3713 streamIntervals(node, fmtdesc.pixelformat,
3714 fmt.g_width(), fmt.g_frame_height(),
3715 frame_count);
3716 restoreFormat(node);
3717 }
3718 break;
3719 }
3720 } while (!node->enum_fmt(fmtdesc));
3721 }
3722
3723 static void streamM2MRun(struct node *node, unsigned frame_count)
3724 {
3725 cv4l_fmt cap_fmt, out_fmt;
3726 unsigned out_type = v4l_type_invert(node->g_type());
3727
3728 node->g_fmt(cap_fmt);
3729 node->g_fmt(out_fmt, out_type);
3730 if (!no_progress)
3731 printf("\r");
3732 printf("\t%s (%s) %ux%u -> %s (%s) %ux%u: %s\n",
3733 fcc2s(out_fmt.g_pixelformat()).c_str(),
3734 pixfmt2s(out_fmt.g_pixelformat()).c_str(),
3735 out_fmt.g_width(), out_fmt.g_height(),
3736 fcc2s(cap_fmt.g_pixelformat()).c_str(),
3737 pixfmt2s(cap_fmt.g_pixelformat()).c_str(),
3738 cap_fmt.g_width(), cap_fmt.g_height(),
3739 ok(testMmap(node, node, frame_count, POLL_MODE_SELECT, false)));
3740 }
3741
3742 static int streamM2MOutFormat(struct node *node, __u32 pixelformat, __u32 w, __u32 h,
3743 unsigned frame_count)
3744 {
3745 unsigned cap_type = node->g_type();
3746 v4l2_fmtdesc fmtdesc;
3747 cv4l_fmt out_fmt;
3748
3749 node->g_fmt(out_fmt, v4l_type_invert(cap_type));
3750 out_fmt.s_pixelformat(pixelformat);
3751 out_fmt.s_width(w);
3752 out_fmt.s_height(h);
3753 fail_on_test(node->s_fmt(out_fmt));
3754
3755 if (node->enum_fmt(fmtdesc, true, 0))
3756 return 0;
3757 do {
3758 cv4l_fmt fmt;
3759
3760 fail_on_test(node->g_fmt(fmt));
3761 fmt.s_pixelformat(fmtdesc.pixelformat);
3762 fmt.s_width(w);
3763 fmt.s_height(h);
3764 fail_on_test(node->s_fmt(fmt));
3765 streamM2MRun(node, frame_count);
3766 } while (!node->enum_fmt(fmtdesc));
3767 return 0;
3768 }
3769
3770 void streamM2MAllFormats(struct node *node, unsigned frame_count)
3771 {
3772 v4l2_fmtdesc fmtdesc;
3773 unsigned out_type = v4l_type_invert(node->g_type());
3774
3775 if (!frame_count)
3776 frame_count = 10;
3777
3778 if (node->enum_fmt(fmtdesc, true, 0, out_type))
3779 return;
3780 selTests.clear();
3781 do {
3782 v4l2_frmsizeenum frmsize;
3783 cv4l_fmt fmt;
3784
3785 if (node->enum_framesizes(frmsize, fmtdesc.pixelformat)) {
3786 cv4l_fmt min, max;
3787
3788 restoreFormat(node);
3789 node->g_fmt(fmt, out_type);
3790 min = fmt;
3791 min.s_width(0);
3792 min.s_height(0);
3793 node->try_fmt(min);
3794 max = fmt;
3795 max.s_width(~0);
3796 max.s_height(~0);
3797 node->try_fmt(max);
3798 if (min.g_width() != fmt.g_width() ||
3799 min.g_height() != fmt.g_height()) {
3800 streamM2MOutFormat(node, fmtdesc.pixelformat,
3801 min.g_width(), min.g_frame_height(),
3802 frame_count);
3803 restoreFormat(node);
3804 }
3805 if (max.g_width() != fmt.g_width() ||
3806 max.g_height() != fmt.g_height()) {
3807 streamM2MOutFormat(node, fmtdesc.pixelformat,
3808 max.g_width(), max.g_frame_height(),
3809 frame_count);
3810 restoreFormat(node);
3811 }
3812 streamM2MOutFormat(node, fmtdesc.pixelformat,
3813 fmt.g_width(), fmt.g_frame_height(),
3814 frame_count);
3815 continue;
3816 }
3817
3818 v4l2_frmsize_stepwise &ss = frmsize.stepwise;
3819
3820 switch (frmsize.type) {
3821 case V4L2_FRMSIZE_TYPE_DISCRETE:
3822 do {
3823 streamM2MOutFormat(node, fmtdesc.pixelformat,
3824 frmsize.discrete.width,
3825 frmsize.discrete.height,
3826 frame_count);
3827 } while (!node->enum_framesizes(frmsize));
3828 break;
3829 default:
3830 node->g_fmt(fmt, out_type);
3831
3832 if (fmt.g_width() != ss.min_width ||
3833 fmt.g_frame_height() != ss.min_height) {
3834 streamM2MOutFormat(node, fmtdesc.pixelformat,
3835 ss.min_width, ss.min_height,
3836 frame_count);
3837 restoreFormat(node);
3838 }
3839 if (fmt.g_width() != ss.max_width ||
3840 fmt.g_frame_height() != ss.max_height) {
3841 streamM2MOutFormat(node, fmtdesc.pixelformat,
3842 ss.max_width, ss.max_height,
3843 frame_count);
3844 restoreFormat(node);
3845 }
3846 streamM2MOutFormat(node, fmtdesc.pixelformat,
3847 fmt.g_width(), fmt.g_frame_height(),
3848 frame_count);
3849 restoreFormat(node);
3850 break;
3851 }
3852 } while (!node->enum_fmt(fmtdesc));
3853 }