aboutsummaryrefslogtreecommitdiffstats
path: root/utils/v4l2-compliance/v4l2-compliance.cpp
blob: a4f8775ea6b8114f432be10311ff6bbe6d7968ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
/*
    V4L2 API compliance test tool.

    Copyright (C) 2008, 2010  Hans Verkuil <hverkuil@xs4all.nl>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335  USA
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/sysmacros.h>
#include <dirent.h>
#include <math.h>
#include <sys/utsname.h>
#include <signal.h>
#include <vector>

#include "v4l2-compliance.h"
#include <media-info.h>
#ifndef ANDROID
#include "version.h"
#endif

/* Short option list

   Please keep in alphabetical order.
   That makes it easier to see which short options are still free.

   In general the lower case is used to set something and the upper
   case is used to retrieve a setting. */
enum Option {
	OptStreamAllIO = 'a',
	OptStreamAllColorTest = 'c',
	OptSetDevice = 'd',
	OptSetExpBufDevice = 'e',
	OptExitOnFail = 'E',
	OptStreamAllFormats = 'f',
	OptHelp = 'h',
	OptSetMediaDevice = 'm',
	OptSetMediaDeviceOnly = 'M',
	OptNoWarnings = 'n',
	OptSetRadioDevice = 'r',
	OptStreaming = 's',
	OptSetSWRadioDevice = 'S',
	OptSetTouchDevice = 't',
	OptTrace = 'T',
	OptSetSubDevDevice = 'u',
	OptVerbose = 'v',
	OptSetVbiDevice = 'V',
	OptUseWrapper = 'w',
	OptExitOnWarn = 'W',
	OptMediaBusInfo = 'z',
	OptStreamFrom = 128,
	OptStreamFromHdr,
	OptLast = 256
};

static char options[OptLast];

static int app_result;
static int tests_total, tests_ok;
static int grand_total, grand_ok, grand_warnings;

// Globals
bool show_info;
bool show_warnings = true;
bool exit_on_fail;
bool exit_on_warn;
bool is_vivid;
int kernel_version;
int media_fd = -1;
unsigned warnings;

static unsigned color_component;
static unsigned color_skip;
static unsigned color_perc = 90;

struct dev_state {
	struct node *node;
	std::vector<v4l2_ext_control> control_vec;
	v4l2_ext_controls controls;
	v4l2_rect crop;
	v4l2_rect compose;
	v4l2_rect native_size;
	v4l2_format fmt;
	v4l2_input input;
	v4l2_output output;
	v4l2_audio ainput;
	v4l2_audioout aoutput;
	v4l2_frequency freq;
	v4l2_tuner tuner;
	v4l2_modulator modulator;
	v4l2_std_id std;
	v4l2_dv_timings timings;
	v4l2_fract interval;
};

static struct dev_state state;

static struct option long_options[] = {
	{"device", required_argument, 0, OptSetDevice},
	{"radio-device", required_argument, 0, OptSetRadioDevice},
	{"vbi-device", required_argument, 0, OptSetVbiDevice},
	{"sdr-device", required_argument, 0, OptSetSWRadioDevice},
	{"subdev-device", required_argument, 0, OptSetSubDevDevice},
	{"expbuf-device", required_argument, 0, OptSetExpBufDevice},
	{"touch-device", required_argument, 0, OptSetTouchDevice},
	{"media-device", required_argument, 0, OptSetMediaDevice},
	{"media-device-only", required_argument, 0, OptSetMediaDeviceOnly},
	{"media-bus-info", required_argument, 0, OptMediaBusInfo},
	{"help", no_argument, 0, OptHelp},
	{"verbose", no_argument, 0, OptVerbose},
	{"no-warnings", no_argument, 0, OptNoWarnings},
	{"exit-on-fail", no_argument, 0, OptExitOnFail},
	{"exit-on-warn", no_argument, 0, OptExitOnWarn},
	{"trace", no_argument, 0, OptTrace},
#ifndef NO_LIBV4L2
	{"wrapper", no_argument, 0, OptUseWrapper},
#endif
	{"streaming", optional_argument, 0, OptStreaming},
	{"stream-from", required_argument, 0, OptStreamFrom},
	{"stream-from-hdr", required_argument, 0, OptStreamFromHdr},
	{"stream-all-formats", no_argument, 0, OptStreamAllFormats},
	{"stream-all-io", no_argument, 0, OptStreamAllIO},
	{"stream-all-color", required_argument, 0, OptStreamAllColorTest},
	{0, 0, 0, 0}
};

static void usage(void)
{
	printf("Usage:\n");
	printf("Common options:\n");
	printf("  -d, --device <dev> Use device <dev> as the video device.\n");
	printf("                     If <dev> starts with a digit, then /dev/video<dev> is used.\n");
	printf("                     Otherwise if -z was specified earlier, then <dev> is the entity name\n");
	printf("                     or interface ID (if prefixed with 0x) as found in the topology of the\n");
	printf("                     media device with the bus info string as specified by the -z option.\n");
	printf("  -V, --vbi-device <dev>\n");
	printf("                     Use device <dev> as the vbi device.\n");
	printf("                     If <dev> starts with a digit, then /dev/vbi<dev> is used.\n");
	printf("                     See the -d description of how <dev> is used in combination with -z.\n");
	printf("  -r, --radio-device <dev>\n");
	printf("                     Use device <dev> as the radio device.\n");
	printf("                     If <dev> starts with a digit, then /dev/radio<dev> is used.\n");
	printf("                     See the -d description of how <dev> is used in combination with -z.\n");
	printf("  -S, --sdr-device <dev>\n");
	printf("                     Use device <dev> as the SDR device.\n");
	printf("                     If <dev> starts with a digit, then /dev/swradio<dev> is used.\n");
	printf("                     See the -d description of how <dev> is used in combination with -z.\n");
	printf("  -t, --touch-device <dev>\n");
	printf("                     Use device <dev> as the touch device.\n");
	printf("                     If <dev> starts with a digit, then /dev/v4l-touch<dev> is used.\n");
	printf("                     See the -d description of how <dev> is used in combination with -z.\n");
	printf("  -u, --subdev-device <dev>\n");
	printf("                     Use device <dev> as the v4l-subdev device.\n");
	printf("                     If <dev> starts with a digit, then /dev/v4l-subdev<dev> is used.\n");
	printf("                     See the -d description of how <dev> is used in combination with -z.\n");
	printf("  -e, --expbuf-device <dev>\n");
	printf("                     Use video device <dev> to obtain DMABUF handles.\n");
	printf("                     If <dev> starts with a digit, then /dev/video<dev> is used.\n");
	printf("                     See the -d description of how <dev> is used in combination with -z.\n");
	printf("  -z, --media-bus-info <bus-info>\n");
	printf("                     Find the media device with the given bus info string. If set, then\n");
	printf("                     the options above can use the entity name or interface ID to refer\n");
	printf("                     to the device nodes.\n");
	printf("  -m, --media-device <dev>\n");
	printf("                     Use device <dev> as the media controller device. Besides this\n");
	printf("                     device it also tests all interfaces it finds.\n");
	printf("                     If <dev> starts with a digit, then /dev/media<dev> is used.\n");
	printf("  -M, --media-device-only <dev>\n");
	printf("                     Use device <dev> as the media controller device. Only test this\n");
	printf("                     device, don't walk over all the interfaces.\n");
	printf("                     If <dev> starts with a digit, then /dev/media<dev> is used.\n");
	printf("  -s, --streaming <count>\n");
	printf("                     Enable the streaming tests. Set <count> to the number of\n");
	printf("                     frames to stream (default 60). Requires a valid input/output\n");
	printf("                     and frequency (when dealing with a tuner). For DMABUF testing\n");
	printf("                     --expbuf-device needs to be set as well.\n");
	printf("  --stream-from [<pixelformat>=]<file>\n");
	printf("  --stream-from-hdr [<pixelformat>=]<file>\n");
	printf("                     Use the contents of the file to fill in output buffers.\n");
	printf("                     If the fourcc of the pixelformat is given, then use the file\n");
	printf("                     for output buffers using that pixelformat only.\n");
	printf("                     The --stream-from-hdr variant uses the format written by\n");
	printf("                     v4l2-ctl --stream-to-hdr where the payload sizes for each\n");
	printf("                     buffer are stored in a header. Useful for compressed formats.\n");
	printf("  -f, --stream-all-formats\n");
	printf("                     Test streaming all available formats.\n");
	printf("                     This attempts to stream using MMAP mode or read/write\n");
	printf("                     for one second for all formats, at all sizes, at all intervals\n");
	printf("                     and with all field values.\n");
	printf("  -a, --stream-all-io\n");
	printf("                     Do streaming tests for all inputs or outputs instead of just\n");
	printf("                     the current input or output. This requires that a valid video\n");
	printf("                     signal is present on all inputs and all outputs are hooked up.\n");
	printf("  -c, --stream-all-color color=red|green|blue,skip=<skip>,perc=<percentage>\n");
	printf("                     For all formats stream <skip + 1> frames and check if\n");
	printf("                     the last frame has at least <perc> percent of the pixels with\n");
	printf("                     a <color> component that is higher than the other two color\n");
	printf("                     components. This requires that a valid red, green or blue video\n");
	printf("                     signal is present on the input(s). If <skip> is not specified,\n");
	printf("                     then just capture the first frame. If <perc> is not specified,\n");
	printf("                     then this defaults to 90%%.\n");
	printf("  -E, --exit-on-fail Exit on the first fail.\n");
	printf("  -h, --help         Display this help message.\n");
	printf("  -n, --no-warnings  Turn off warning messages.\n");
	printf("  -T, --trace        Trace all called ioctls.\n");
	printf("  -v, --verbose      Turn on verbose reporting.\n");
#ifndef NO_LIBV4L2
	printf("  -w, --wrapper      Use the libv4l2 wrapper library.\n");
#endif
	printf("  -W, --exit-on-warn Exit on the first warning.\n");
	exit(0);
}

const char *ok(int res)
{
	static char buf[100];

	if (res == ENOTTY) {
		strcpy(buf, "OK (Not Supported)");
		res = 0;
	} else {
		strcpy(buf, "OK");
	}
	tests_total++;
	if (res) {
		app_result = res;
		sprintf(buf, "FAIL");
	} else {
		tests_ok++;
	}
	return buf;
}

int check_string(const char *s, size_t len)
{
	size_t sz = strnlen(s, len);

	if (sz == 0)
		return fail("string empty\n");
	if (sz == len)
		return fail("string not 0-terminated\n");
	return 0;
}

int check_ustring(const __u8 *s, int len)
{
	return check_string((const char *)s, len);
}

int check_0(const void *p, int len)
{
	const __u8 *q = (const __u8 *)p;

	while (len--)
		if (*q++)
			return 1;
	return 0;
}

static std::map<std::string, std::string> stream_from_map;
static std::map<std::string, bool> stream_hdr_map;

std::string stream_from(std::string pixelformat, bool &use_hdr)
{
	if (stream_from_map.find(pixelformat) == stream_from_map.end()) {
		if (pixelformat.empty())
			return "";
		return stream_from("", use_hdr);
	}
	use_hdr = stream_hdr_map[pixelformat];
	return stream_from_map[pixelformat];
}

static void storeStateTimings(struct node *node, __u32 caps)
{
	if (caps & V4L2_IN_CAP_STD)
		node->g_std(state.std);
	if (caps & V4L2_IN_CAP_DV_TIMINGS)
		node->g_dv_timings(state.timings);
	if (caps & V4L2_IN_CAP_NATIVE_SIZE) {
		v4l2_selection sel = {
			node->g_selection_type(),
			V4L2_SEL_TGT_NATIVE_SIZE
		};

		node->g_selection(sel);
		state.native_size = sel.r;
	}
}

static void storeState(struct node *node)
{
	state.node = node;
	if (node->has_inputs) {
		__u32 input;

		node->g_input(input);
		node->enum_input(state.input, true, input);
		if (state.input.audioset)
			node->g_audio(state.ainput);
		if (node->g_caps() & V4L2_CAP_TUNER) {
			node->g_tuner(state.tuner, state.input.tuner);
			node->g_frequency(state.freq, state.input.tuner);
		}
		storeStateTimings(node, state.input.capabilities);
	}
	if (node->has_outputs) {
		__u32 output;

		node->g_output(output);
		node->enum_output(state.output, true, output);
		if (state.output.audioset)
			node->g_audout(state.aoutput);
		if (node->g_caps() & V4L2_CAP_MODULATOR) {
			node->g_modulator(state.modulator, state.output.modulator);
			node->g_frequency(state.freq, state.output.modulator);
		}
		storeStateTimings(node, state.output.capabilities);
	}
	node->g_fmt(state.fmt);

	v4l2_selection sel = {
		node->g_selection_type(),
		V4L2_SEL_TGT_CROP
	};
	if (!node->g_selection(sel))
		state.crop = sel.r;
	sel.target = V4L2_SEL_TGT_COMPOSE;
	if (!node->g_selection(sel))
		state.compose = sel.r;
	node->get_interval(state.interval);

	v4l2_query_ext_ctrl qec = { 0 };

	while (!node->query_ext_ctrl(qec, true, true)) {
		if (qec.flags & (V4L2_CTRL_FLAG_DISABLED |
				 V4L2_CTRL_FLAG_READ_ONLY |
				 V4L2_CTRL_FLAG_WRITE_ONLY |
				 V4L2_CTRL_FLAG_VOLATILE))
			continue;
		v4l2_ext_control ctrl = { qec.id };
		if (qec.flags & V4L2_CTRL_FLAG_HAS_PAYLOAD) {
			ctrl.size = qec.elems * qec.elem_size;
			ctrl.ptr = malloc(ctrl.size);
		}
		state.control_vec.push_back(ctrl);
	}
	if (state.control_vec.empty())
		return;
	state.controls.count = state.control_vec.size();
	state.controls.controls = &state.control_vec[0];
	node->g_ext_ctrls(state.controls);
}

static void restoreStateTimings(struct node *node, __u32 caps)
{
	if (caps & V4L2_IN_CAP_STD)
		node->s_std(state.std);
	if (caps & V4L2_IN_CAP_DV_TIMINGS)
		node->s_dv_timings(state.timings);
	if (caps & V4L2_IN_CAP_NATIVE_SIZE) {
		v4l2_selection sel = {
			node->g_selection_type(),
			V4L2_SEL_TGT_NATIVE_SIZE,
			0, state.native_size
		};

		node->s_selection(sel);
	}
}

static void restoreState()
{
	struct node *node = state.node;

	node->reopen();
	if (node->has_inputs) {
		node->s_input(state.input.index);
		if (state.input.audioset)
			node->s_audio(state.ainput.index);
		if (node->g_caps() & V4L2_CAP_TUNER) {
			node->s_tuner(state.tuner);
			node->s_frequency(state.freq);
		}
		restoreStateTimings(node, state.input.capabilities);
	}
	if (node->has_outputs) {
		node->s_output(state.output.index);
		if (state.output.audioset)
		node->s_audout(state.aoutput.index);
		if (node->g_caps() & V4L2_CAP_MODULATOR) {
			node->s_modulator(state.modulator);
			node->s_frequency(state.freq);
		}
		restoreStateTimings(node, state.output.capabilities);
	}

	/* First restore the format */
	node->s_fmt(state.fmt);

	v4l2_selection sel_compose = {
		node->g_selection_type(),
		V4L2_SEL_TGT_COMPOSE,
		0, state.compose
	};
	v4l2_selection sel_crop = {
		node->g_selection_type(),
		V4L2_SEL_TGT_CROP,
		0, state.crop
	};
	if (node->has_inputs) {
		/*
		 * For capture restore the compose rectangle
		 * before the crop rectangle.
		 */
		if (sel_compose.r.width && sel_compose.r.height)
			node->s_selection(sel_compose);
		if (sel_crop.r.width && sel_crop.r.height)
			node->s_selection(sel_crop);
	}
	if (node->has_outputs) {
		/*
		 * For output the crop rectangle should be
		 * restored before the compose rectangle.
		 */
		if (sel_crop.r.width && sel_crop.r.height)
			node->s_selection(sel_crop);
		if (sel_compose.r.width && sel_compose.r.height)
			node->s_selection(sel_compose);
	}
	if (state.interval.denominator)
		node->set_interval(state.interval);

	node->s_ext_ctrls(state.controls);
	// We need to reopen again so QUERYCAP is called again.
	// The vivid driver has controls (RDS related) that change
	// the capabilities, and if we don't do this, then the
	// internal caps stored in v4l_fd is out-of-sync with the
	// actual caps.
	node->reopen();
}

static void signal_handler_interrupt(int signum)
{
	restoreState();
	printf("\n");
	exit(-1);
}

static void determine_codec_mask(struct node &node)
{
	struct v4l2_fmtdesc fmt_desc;
	int num_cap_fmts = 0;
	int num_compressed_cap_fmts = 0;
	int num_out_fmts = 0;
	int num_compressed_out_fmts = 0;
	unsigned mask = 0;

	node.codec_mask = 0;
	if (!node.has_vid_m2m())
		return;

	if (node.enum_fmt(fmt_desc, true, 0, node.g_type()))
		return;

	do {
		if (fmt_desc.flags & V4L2_FMT_FLAG_COMPRESSED) {
			num_compressed_cap_fmts++;
			switch (fmt_desc.pixelformat) {
			case V4L2_PIX_FMT_JPEG:
			case V4L2_PIX_FMT_MJPEG:
				mask |= JPEG_ENCODER;
				break;
			case V4L2_PIX_FMT_H263:
			case V4L2_PIX_FMT_H264:
			case V4L2_PIX_FMT_H264_NO_SC:
			case V4L2_PIX_FMT_H264_MVC:
			case V4L2_PIX_FMT_MPEG1:
			case V4L2_PIX_FMT_MPEG2:
			case V4L2_PIX_FMT_MPEG4:
			case V4L2_PIX_FMT_XVID:
			case V4L2_PIX_FMT_VC1_ANNEX_G:
			case V4L2_PIX_FMT_VC1_ANNEX_L:
			case V4L2_PIX_FMT_VP8:
			case V4L2_PIX_FMT_VP9:
			case V4L2_PIX_FMT_HEVC:
			case V4L2_PIX_FMT_FWHT:
				mask |= STATEFUL_ENCODER;
				break;
			case V4L2_PIX_FMT_MPEG2_SLICE:
				mask |= STATELESS_ENCODER;
				break;
			default:
				return;
			}
		}
		num_cap_fmts++;
	} while (!node.enum_fmt(fmt_desc));


	if (node.enum_fmt(fmt_desc, true, 0, v4l_type_invert(node.g_type())))
		return;

	do {
		if (fmt_desc.flags & V4L2_FMT_FLAG_COMPRESSED) {
			num_compressed_out_fmts++;
			switch (fmt_desc.pixelformat) {
			case V4L2_PIX_FMT_JPEG:
			case V4L2_PIX_FMT_MJPEG:
				mask |= JPEG_DECODER;
				break;
			case V4L2_PIX_FMT_H263:
			case V4L2_PIX_FMT_H264:
			case V4L2_PIX_FMT_H264_NO_SC:
			case V4L2_PIX_FMT_H264_MVC:
			case V4L2_PIX_FMT_MPEG1:
			case V4L2_PIX_FMT_MPEG2:
			case V4L2_PIX_FMT_MPEG4:
			case V4L2_PIX_FMT_XVID:
			case V4L2_PIX_FMT_VC1_ANNEX_G:
			case V4L2_PIX_FMT_VC1_ANNEX_L:
			case V4L2_PIX_FMT_VP8:
			case V4L2_PIX_FMT_VP9:
			case V4L2_PIX_FMT_HEVC:
			case V4L2_PIX_FMT_FWHT:
				mask |= STATEFUL_DECODER;
				break;
			case V4L2_PIX_FMT_MPEG2_SLICE:
				mask |= STATELESS_DECODER;
				break;
			default:
				return;
			}
		}
		num_out_fmts++;
	} while (!node.enum_fmt(fmt_desc));

	if (num_compressed_out_fmts == 0 && num_compressed_cap_fmts == num_cap_fmts)
		node.codec_mask = mask;
	else if (num_compressed_cap_fmts == 0 && num_compressed_out_fmts == num_out_fmts)
		node.codec_mask = mask;
}

static int testCap(struct node *node)
{
	struct v4l2_capability vcap;
	__u32 caps, dcaps;
	const __u32 video_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
			V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE |
			V4L2_CAP_VIDEO_M2M | V4L2_CAP_VIDEO_M2M_MPLANE |
			V4L2_CAP_VIDEO_OVERLAY | V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
	const __u32 vbi_caps = V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE |
			V4L2_CAP_VBI_OUTPUT | V4L2_CAP_SLICED_VBI_OUTPUT;
	const __u32 sdr_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_SDR_OUTPUT;
	const __u32 radio_caps = V4L2_CAP_RADIO | V4L2_CAP_MODULATOR;
	const __u32 input_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
			V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE |
			V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_HW_FREQ_SEEK |
			V4L2_CAP_TUNER | V4L2_CAP_SDR_CAPTURE | V4L2_CAP_META_CAPTURE;
	const __u32 output_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE |
			V4L2_CAP_VIDEO_OUTPUT_OVERLAY | V4L2_CAP_VBI_OUTPUT |
			V4L2_CAP_SDR_OUTPUT | V4L2_CAP_SLICED_VBI_OUTPUT |
			V4L2_CAP_MODULATOR | V4L2_CAP_META_OUTPUT;
	const __u32 overlay_caps = V4L2_CAP_VIDEO_OVERLAY | V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
	const __u32 m2m_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_VIDEO_M2M_MPLANE;
	const __u32 io_caps = V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
	const __u32 mplane_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE |
		V4L2_CAP_VIDEO_M2M_MPLANE;
	const __u32 splane_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
		V4L2_CAP_VIDEO_M2M;

	memset(&vcap, 0xff, sizeof(vcap));
	// Must always be there
	fail_on_test(doioctl(node, VIDIOC_QUERYCAP, &vcap));
	fail_on_test(check_ustring(vcap.driver, sizeof(vcap.driver)));
	fail_on_test(check_ustring(vcap.card, sizeof(vcap.card)));
	fail_on_test(check_ustring(vcap.bus_info, sizeof(vcap.bus_info)));
	// Check for valid prefixes
	if (memcmp(vcap.bus_info, "usb-", 4) &&
	    memcmp(vcap.bus_info, "PCI:", 4) &&
	    memcmp(vcap.bus_info, "PCIe:", 5) &&
	    memcmp(vcap.bus_info, "ISA:", 4) &&
	    memcmp(vcap.bus_info, "I2C:", 4) &&
	    memcmp(vcap.bus_info, "parport", 7) &&
	    memcmp(vcap.bus_info, "platform:", 9) &&
	    memcmp(vcap.bus_info, "rmi4:", 5))
		return fail("missing bus_info prefix ('%s')\n", vcap.bus_info);
	if (!node->media_bus_info.empty() &&
	    node->media_bus_info != std::string((const char *)vcap.bus_info))
		warn("media bus_info '%s' differs from V4L2 bus_info '%s'\n",
		     node->media_bus_info.c_str(), vcap.bus_info);
	fail_on_test((vcap.version >> 16) < 3);
	fail_on_test(check_0(vcap.reserved, sizeof(vcap.reserved)));
	caps = vcap.capabilities;
	dcaps = vcap.device_caps;
	node->is_m2m = dcaps & m2m_caps;
	fail_on_test(caps == 0);
	fail_on_test(caps & V4L2_CAP_ASYNCIO);
	fail_on_test(!(caps & V4L2_CAP_DEVICE_CAPS));
	fail_on_test(dcaps & V4L2_CAP_DEVICE_CAPS);
	fail_on_test(dcaps & ~caps);
	fail_on_test(!(dcaps & caps));
	// set by the core, so this really should always be there
	// for a modern driver for both caps and dcaps
	fail_on_test(!(caps & V4L2_CAP_EXT_PIX_FORMAT));
	//fail_on_test(!(dcaps & V4L2_CAP_EXT_PIX_FORMAT));
	fail_on_test(node->is_video && !(dcaps & video_caps));
	fail_on_test(node->is_radio && !(dcaps & radio_caps));
	// V4L2_CAP_AUDIO is invalid for radio and sdr
	fail_on_test(node->is_radio && (dcaps & V4L2_CAP_AUDIO));
	fail_on_test(node->is_sdr && (dcaps & V4L2_CAP_AUDIO));
	fail_on_test(node->is_vbi && !(dcaps & vbi_caps));
	fail_on_test(node->is_sdr && !(dcaps & sdr_caps));
	// You can't have both set due to missing buffer type in VIDIOC_G/S_FBUF
	fail_on_test((dcaps & overlay_caps) == overlay_caps);
	// Overlay support makes no sense for m2m devices
	fail_on_test((dcaps & m2m_caps) && (dcaps & overlay_caps));
	fail_on_test(node->is_video && (dcaps & (vbi_caps | radio_caps | sdr_caps)));
	fail_on_test(node->is_radio && (dcaps & (vbi_caps | video_caps | sdr_caps)));
	fail_on_test(node->is_vbi && (dcaps & (video_caps | radio_caps | sdr_caps)));
	fail_on_test(node->is_sdr && (dcaps & (video_caps | V4L2_CAP_RADIO | vbi_caps)));
	if (node->is_m2m) {
		fail_on_test((dcaps & input_caps) && (dcaps & output_caps));
	} else {
		if (dcaps & input_caps)
			fail_on_test(dcaps & output_caps);
		if (dcaps & output_caps)
			fail_on_test(dcaps & input_caps);
	}
	if (node->can_capture || node->can_output) {
		// whether io_caps need to be set for RDS capture/output is
		// checked elsewhere as that depends on the tuner/modulator
		// capabilities.
		if (!(dcaps & (V4L2_CAP_RDS_CAPTURE | V4L2_CAP_RDS_OUTPUT)))
			fail_on_test(!(dcaps & io_caps));
	} else {
		fail_on_test(dcaps & io_caps);
	}
	// having both mplane and splane caps is not allowed (at least for now)
	fail_on_test((dcaps & mplane_caps) && (dcaps & splane_caps));

	if (node->codec_mask) {
		bool found_bit = false;

		// Only one bit may be set in the mask
		for (unsigned i = 0; i < 32; i++)
			if (node->codec_mask & (1U << i)) {
				fail_on_test(found_bit);
				found_bit = true;
			}
	}

	return 0;
}

#define NR_OPENS 100
static int testUnlimitedOpens(struct node *node)
{
	int fds[NR_OPENS];
	unsigned i;
	bool ok;

	/*
	 * There should *not* be an artificial limit to the number
	 * of open()s you can do on a V4L2 device node. So test whether
	 * you can open a device node at least 100 times.
	 *
	 * And please don't start rejecting opens in your driver at 101!
	 * There really shouldn't be a limit in the driver.
	 *
	 * If there are resource limits, then check against those limits
	 * where they are actually needed.
	 */
	for (i = 0; i < NR_OPENS; i++) {
		fds[i] = open(node->device, O_RDWR);
		if (fds[i] < 0)
			break;
	}
	ok = i == NR_OPENS;
	while (i--)
		close(fds[i]);
	fail_on_test(!ok);
	return 0;
}

static int check_prio(struct node *node, struct node *node2, enum v4l2_priority match)
{
	enum v4l2_priority prio;

	// Must be able to get priority
	fail_on_test(doioctl(node, VIDIOC_G_PRIORITY, &prio));
	// Must match the expected prio
	fail_on_test(prio != match);
	fail_on_test(doioctl(node2, VIDIOC_G_PRIORITY, &prio));
	fail_on_test(prio != match);
	return 0;
}

static int testPrio(struct node *node, struct node *node2)
{
	enum v4l2_priority prio;
	int err;

	err = check_prio(node, node2, V4L2_PRIORITY_DEFAULT);
	if (err)
		return err;

	prio = V4L2_PRIORITY_RECORD;
	// Must be able to change priority
	fail_on_test(doioctl(node, VIDIOC_S_PRIORITY, &prio));
	// Must match the new prio
	fail_on_test(check_prio(node, node2, V4L2_PRIORITY_RECORD));

	prio = V4L2_PRIORITY_INTERACTIVE;
	// Going back to interactive on the other node must fail
	fail_on_test(!doioctl(node2, VIDIOC_S_PRIORITY, &prio));
	prio = V4L2_PRIORITY_INTERACTIVE;
	// Changing it on the first node must work.
	fail_on_test(doioctl(node, VIDIOC_S_PRIORITY, &prio));
	fail_on_test(check_prio(node, node2, V4L2_PRIORITY_INTERACTIVE));
	return 0;
}

static void streamingSetup(struct node *node)
{
	if (node->can_capture) {
		v4l2_fract min_period = { 1, 1000 };
		struct v4l2_input input;

		memset(&input, 0, sizeof(input));
		doioctl(node, VIDIOC_G_INPUT, &input.index);
		doioctl(node, VIDIOC_ENUMINPUT, &input);
		node->cur_io_caps = input.capabilities;
		node->set_interval(min_period);
	} else if (node->can_output) {
		struct v4l2_output output;

		memset(&output, 0, sizeof(output));
		doioctl(node, VIDIOC_G_OUTPUT, &output.index);
		doioctl(node, VIDIOC_ENUMOUTPUT, &output);
		node->cur_io_caps = output.capabilities;
	}
}

static int parse_subopt(char **subs, const char * const *subopts, char **value)
{
	int opt = getsubopt(subs, (char * const *)subopts, value);

	if (opt == -1) {
		fprintf(stderr, "Invalid suboptions specified\n");
		return -1;
	}
	if (*value == NULL) {
		fprintf(stderr, "No value given to suboption <%s>\n",
				subopts[opt]);
		return -1;
	}
	return opt;
}

static int open_media_bus_info(const std::string &bus_info)
{
	DIR *dp;
	struct dirent *ep;

	dp = opendir("/dev");
	if (dp == NULL)
		return -1;

	while ((ep = readdir(dp))) {
		const char *name = ep->d_name;

		if (!memcmp(name, "media", 5) && isdigit(name[5])) {
			struct media_device_info mdi;
			std::string devname = std::string("/dev/") + name;

			int fd = open(devname.c_str(), O_RDWR);
			if (fd < 0)
				continue;
			if (!ioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi) &&
			    bus_info == mdi.bus_info) {
				closedir(dp);
				return fd;
			}
			close(fd);
		}
	}
	closedir(dp);
	return -1;
}

static const char *make_devname(const char *device, const char *devname,
				const std::string &media_bus_info)
{
	if (device[0] >= '0' && device[0] <= '9' && strlen(device) <= 3) {
		static char newdev[32];

		sprintf(newdev, "/dev/%s%s", devname, device);
		return newdev;
	}
	if (media_bus_info.empty())
		return device;
	int media_fd = open_media_bus_info(media_bus_info);
	if (media_fd < 0)
		return device;

	media_v2_topology topology;
	memset(&topology, 0, sizeof(topology));
	if (ioctl(media_fd, MEDIA_IOC_G_TOPOLOGY, &topology)) {
		close(media_fd);
		return device;
	}

	media_v2_entity *ents = new media_v2_entity[topology.num_entities];
	topology.ptr_entities = (__u64)ents;
	media_v2_link *links = new media_v2_link[topology.num_links];
	topology.ptr_links = (__u64)links;
	media_v2_interface *ifaces = new media_v2_interface[topology.num_interfaces];
	topology.ptr_interfaces = (__u64)ifaces;

	unsigned i, ent_id, iface_id = 0;

	if (ioctl(media_fd, MEDIA_IOC_G_TOPOLOGY, &topology))
		goto err;

	if (device[0] == '0' && device[1] == 'x')
		iface_id = strtoul(device, NULL, 16);

	if (!iface_id) {
		for (i = 0; i < topology.num_entities; i++)
			if (!strcmp(ents[i].name, device))
				break;
		if (i >= topology.num_entities)
			goto err;
		ent_id = ents[i].id;
		for (i = 0; i < topology.num_links; i++)
			if (links[i].sink_id == ent_id &&
			    (links[i].flags & MEDIA_LNK_FL_LINK_TYPE) ==
			    MEDIA_LNK_FL_INTERFACE_LINK)
				break;
		if (i >= topology.num_links)
			goto err;
		iface_id = links[i].source_id;
	}
	for (i = 0; i < topology.num_interfaces; i++)
		if (ifaces[i].id == iface_id)
			break;
	if (i >= topology.num_interfaces)
		goto err;

	static char newdev[32];
	sprintf(newdev, "/dev/char/%d:%d",
		ifaces[i].devnode.major, ifaces[i].devnode.minor);
	device = newdev;
	
err:
	delete [] ents;
	delete [] links;
	delete [] ifaces;
	close(media_fd);
	return device;
}

void testNode(struct node &node, struct node &expbuf_node, media_type type,
	      unsigned frame_count)
{
	struct node node2;
	struct v4l2_capability vcap;		/* list_cap */
	std::string driver;

	tests_total = tests_ok = warnings = 0;

	node.is_video = type == MEDIA_TYPE_VIDEO;
	node.is_vbi = type == MEDIA_TYPE_VBI;
	node.is_radio = type == MEDIA_TYPE_RADIO;
	node.is_sdr = type == MEDIA_TYPE_SDR;
	node.is_touch = type == MEDIA_TYPE_TOUCH;

	if (node.is_v4l2()) {
		doioctl(&node, VIDIOC_QUERYCAP, &vcap);
		driver = (const char *)vcap.driver;
		is_vivid = driver == "vivid";
		if (is_vivid)
			node.bus_info = (const char *)vcap.bus_info;
		determine_codec_mask(node);
	} else {
		memset(&vcap, 0, sizeof(vcap));
	}

	if (!node.is_media())
		media_fd = mi_get_media_fd(node.g_fd(), node.bus_info);

	int fd = node.is_media() ? node.g_fd() : media_fd;
	if (fd >= 0) {
		struct media_device_info mdinfo;

		if (!ioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdinfo)) {
			if (!node.is_v4l2())
				driver = mdinfo.driver;
			node.media_bus_info = mdinfo.bus_info;
		}
	}

	if (driver.empty())
		printf("Compliance test for device %s%s:\n\n",
		       node.device, node.g_direct() ? "" : " (using libv4l2)");
	else
		printf("Compliance test for %s device %s%s:\n\n",
		       driver.c_str(), node.device, node.g_direct() ? "" : " (using libv4l2)");

	if (node.g_caps() & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
			 V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_SLICED_VBI_CAPTURE))
		node.has_inputs = true;
	if (node.g_caps() & (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VBI_OUTPUT |
			 V4L2_CAP_VIDEO_OUTPUT_MPLANE | V4L2_CAP_SLICED_VBI_OUTPUT))
		node.has_outputs = true;
	if (node.g_caps() & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
			 V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_M2M_MPLANE |
			 V4L2_CAP_VIDEO_M2M | V4L2_CAP_SLICED_VBI_CAPTURE |
			 V4L2_CAP_RDS_CAPTURE | V4L2_CAP_SDR_CAPTURE |
			 V4L2_CAP_META_CAPTURE))
		node.can_capture = true;
	if (node.g_caps() & (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VBI_OUTPUT |
			 V4L2_CAP_VIDEO_OUTPUT_MPLANE | V4L2_CAP_VIDEO_M2M_MPLANE |
			 V4L2_CAP_VIDEO_M2M | V4L2_CAP_SLICED_VBI_OUTPUT |
			 V4L2_CAP_RDS_OUTPUT | V4L2_CAP_SDR_OUTPUT |
			 V4L2_CAP_META_OUTPUT))
		node.can_output = true;
	if (node.g_caps() & (V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE |
			 V4L2_CAP_VIDEO_M2M_MPLANE))
		node.is_planar = true;
	if (node.g_caps() & (V4L2_CAP_META_CAPTURE | V4L2_CAP_META_OUTPUT)) {
		node.is_video = false;
		node.is_meta = true;
	}

	/* Information Opts */

	if (node.is_v4l2()) {
		printf("Driver Info:\n");
		v4l2_info_capability(vcap);
		switch (node.codec_mask) {
		case JPEG_ENCODER:
			printf("\tDetected JPEG Encoder\n");
			break;
		case JPEG_DECODER:
			printf("\tDetected JPEG Decoder\n");
			break;
		case STATEFUL_ENCODER:
			printf("\tDetected Stateful Encoder\n");
			break;
		case STATEFUL_DECODER:
			printf("\tDetected Stateful Decoder\n");
			break;
		case STATELESS_ENCODER:
			printf("\tDetected Stateless Encoder\n");
			break;
		case STATELESS_DECODER:
			printf("\tDetected Stateless Decoder\n");
			break;
		}
	}

	__u32 ent_id = 0;
	bool is_invalid = false;

	if (node.is_media())
		mi_media_info_for_fd(node.g_fd(), -1, &is_invalid);
	else if (media_fd >= 0)
		ent_id = mi_media_info_for_fd(media_fd, node.g_fd(), &is_invalid);

	if (ent_id && ent_id != MEDIA_ENT_F_UNKNOWN) {
		memset(&node.entity, 0, sizeof(node.entity));
		node.entity.id = ent_id;
		if (!ioctl(media_fd, MEDIA_IOC_ENUM_ENTITIES, &node.entity)) {
			struct media_links_enum links_enum;

			node.pads = new media_pad_desc[node.entity.pads];
			node.links = new media_link_desc[node.entity.links];	
			memset(&links_enum, 0, sizeof(links_enum));
			links_enum.entity = ent_id;
			links_enum.pads = node.pads;
			links_enum.links = node.links;
			if (ioctl(media_fd, MEDIA_IOC_ENUM_LINKS, &links_enum))
				node.entity.id = 0;
		} else {
			node.entity.id = 0;
		}
	}

	/* Required ioctls */

	printf("\nRequired ioctls:\n");

	if (ent_id)
		printf("\ttest MC information (see 'Media Driver Info' above): %s\n", ok(is_invalid ? -1 : 0));

	if (node.is_v4l2())
		printf("\ttest VIDIOC_QUERYCAP: %s\n", ok(testCap(&node)));

	if (node.is_media()) {
		printf("\ttest MEDIA_IOC_DEVICE_INFO: %s\n",
		       ok(testMediaDeviceInfo(&node)));
	}
	printf("\n");

	/* Multiple opens */

	printf("Allow for multiple opens:\n");
	node2 = node;
	switch (type) {
	case MEDIA_TYPE_SUBDEV:
		printf("\ttest second %s open: %s\n", node.device,
		       ok(node2.subdev_open(node.device, false) >= 0 ? 0 : errno));
		break;
	case MEDIA_TYPE_MEDIA:
		printf("\ttest second %s open: %s\n", node.device,
		       ok(node2.media_open(node.device, false) >= 0 ? 0 : errno));
		if (node2.g_fd() >= 0)
			printf("\ttest MEDIA_IOC_DEVICE_INFO: %s\n", ok(testMediaDeviceInfo(&node2)));
		break;
	default:
		printf("\ttest second %s open: %s\n", node.device,
		       ok(node2.open(node.device, false) >= 0 ? 0 : errno));
		if (node2.g_fd() >= 0) {
			printf("\ttest VIDIOC_QUERYCAP: %s\n", ok(testCap(&node2)));
			printf("\ttest VIDIOC_G/S_PRIORITY: %s\n",
			       ok(testPrio(&node, &node2)));
		}
		break;
	}
	if (node2.g_fd() >= 0)
		node.node2 = &node2;

	printf("\ttest for unlimited opens: %s\n",
		ok(testUnlimitedOpens(&node)));
	printf("\n");

	storeState(&node);

	/* register signal handler for interrupt signal, to exit gracefully */
	signal(SIGINT, signal_handler_interrupt);

	unsigned cur_io = 0;
	unsigned min_io = 0;
	unsigned max_io = 0;

	/* Media ioctls */

	if (node.is_media()) {
		printf("Media Controller ioctls:\n");
		printf("\ttest MEDIA_IOC_G_TOPOLOGY: %s\n", ok(testMediaTopology(&node)));
		if (node.topology)
			printf("\tEntities: %u Interfaces: %u Pads: %u Links: %u\n",
			       node.topology->num_entities,
			       node.topology->num_interfaces,
			       node.topology->num_pads,
			       node.topology->num_links);
		printf("\ttest MEDIA_IOC_ENUM_ENTITIES/LINKS: %s\n", ok(testMediaEnum(&node)));
		printf("\ttest MEDIA_IOC_SETUP_LINK: %s\n", ok(testMediaSetupLink(&node)));
		printf("\n");
		goto show_total;
	}

	/* Debug ioctls */

	printf("Debug ioctls:\n");
	if (node.is_v4l2())
		printf("\ttest VIDIOC_DBG_G/S_REGISTER: %s\n", ok(testRegister(&node)));
	printf("\ttest VIDIOC_LOG_STATUS: %s\n", ok(testLogStatus(&node)));
	printf("\n");

	/* Input ioctls */

	printf("Input ioctls:\n");
	printf("\ttest VIDIOC_G/S_TUNER/ENUM_FREQ_BANDS: %s\n", ok(testTuner(&node)));
	printf("\ttest VIDIOC_G/S_FREQUENCY: %s\n", ok(testTunerFreq(&node)));
	printf("\ttest VIDIOC_S_HW_FREQ_SEEK: %s\n", ok(testTunerHwSeek(&node)));
	printf("\ttest VIDIOC_ENUMAUDIO: %s\n", ok(testEnumInputAudio(&node)));
	printf("\ttest VIDIOC_G/S/ENUMINPUT: %s\n", ok(testInput(&node)));
	printf("\ttest VIDIOC_G/S_AUDIO: %s\n", ok(testInputAudio(&node)));
	printf("\tInputs: %d Audio Inputs: %d Tuners: %d\n",
			node.inputs, node.audio_inputs, node.tuners);
	printf("\n");

	/* Output ioctls */

	printf("Output ioctls:\n");
	printf("\ttest VIDIOC_G/S_MODULATOR: %s\n", ok(testModulator(&node)));
	printf("\ttest VIDIOC_G/S_FREQUENCY: %s\n", ok(testModulatorFreq(&node)));
	printf("\ttest VIDIOC_ENUMAUDOUT: %s\n", ok(testEnumOutputAudio(&node)));
	printf("\ttest VIDIOC_G/S/ENUMOUTPUT: %s\n", ok(testOutput(&node)));
	printf("\ttest VIDIOC_G/S_AUDOUT: %s\n", ok(testOutputAudio(&node)));
	printf("\tOutputs: %d Audio Outputs: %d Modulators: %d\n",
			node.outputs, node.audio_outputs, node.modulators);
	printf("\n");

	/* I/O configuration ioctls */

	printf("Input/Output configuration ioctls:\n");
	printf("\ttest VIDIOC_ENUM/G/S/QUERY_STD: %s\n", ok(testStd(&node)));
	printf("\ttest VIDIOC_ENUM/G/S/QUERY_DV_TIMINGS: %s\n", ok(testTimings(&node)));
	printf("\ttest VIDIOC_DV_TIMINGS_CAP: %s\n", ok(testTimingsCap(&node)));
	printf("\ttest VIDIOC_G/S_EDID: %s\n", ok(testEdid(&node)));
	printf("\n");

	/* Sub-device ioctls */

	if (node.is_subdev()) {
		bool has_source = false;
		bool has_sink = false;

		node.frame_interval_pad = -1;
		node.enum_frame_interval_pad = -1;
		for (unsigned pad = 0; pad < node.entity.pads; pad++) {
			if (node.pads[pad].flags & MEDIA_PAD_FL_SINK)
				has_sink = true;
			if (node.pads[pad].flags & MEDIA_PAD_FL_SOURCE)
				has_source = true;
		}
		node.is_passthrough_subdev = has_source && has_sink;

		for (unsigned pad = 0; pad < node.entity.pads; pad++) {
			printf("Sub-Device ioctls (%s Pad %u):\n",
			       (node.pads[pad].flags & MEDIA_PAD_FL_SINK) ?
			       "Sink" : "Source", pad);
			node.has_subdev_enum_code = 0;
			node.has_subdev_enum_fsize = 0;
			node.has_subdev_enum_fival = 0;
			for (unsigned which = V4L2_SUBDEV_FORMAT_TRY;
			     which <= V4L2_SUBDEV_FORMAT_ACTIVE; which++) {
				printf("\ttest %s VIDIOC_SUBDEV_ENUM_MBUS_CODE/FRAME_SIZE/FRAME_INTERVAL: %s\n",
				       which ? "Active" : "Try",
				       ok(testSubDevEnum(&node, which, pad)));
				printf("\ttest %s VIDIOC_SUBDEV_G/S_FMT: %s\n",
				       which ? "Active" : "Try",
				       ok(testSubDevFormat(&node, which, pad)));
				printf("\ttest %s VIDIOC_SUBDEV_G/S_SELECTION/CROP: %s\n",
				       which ? "Active" : "Try",
				       ok(testSubDevSelection(&node, which, pad)));
				if (which)
					printf("\ttest VIDIOC_SUBDEV_G/S_FRAME_INTERVAL: %s\n",
					       ok(testSubDevFrameInterval(&node, pad)));
			}
			if (node.has_subdev_enum_code && node.has_subdev_enum_code < 3)
				fail("VIDIOC_SUBDEV_ENUM_MBUS_CODE: try/active mismatch\n");
			if (node.has_subdev_enum_fsize && node.has_subdev_enum_fsize < 3)
				fail("VIDIOC_SUBDEV_ENUM_FRAME_SIZE: try/active mismatch\n");
			if (node.has_subdev_enum_fival && node.has_subdev_enum_fival < 3)
				fail("VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: try/active mismatch\n");
			if (node.has_subdev_fmt && node.has_subdev_fmt < 3)
				fail("VIDIOC_SUBDEV_G/S_FMT: try/active mismatch\n");
			if (node.has_subdev_selection && node.has_subdev_selection < 3)
				fail("VIDIOC_SUBDEV_G/S_SELECTION: try/active mismatch\n");
			if (node.has_subdev_selection &&
			    node.has_subdev_selection != node.has_subdev_fmt)
				fail("VIDIOC_SUBDEV_G/S_SELECTION: fmt/selection mismatch\n");
			printf("\n");
		}
	}

	max_io = node.inputs > node.outputs ? node.inputs : node.outputs;

	for (unsigned io = 0; io < (max_io ? max_io : 1); io++) {
		v4l2_fract min_period = { 1, 1000 };
		char suffix[100] = "";

		node.std_controls = node.priv_controls = 0;
		node.std_compound_controls = node.priv_compound_controls = 0;
		node.controls.clear();
		node.frmsizes.clear();
		node.frmsizes_count.clear();
		node.has_frmintervals = false;
		node.valid_buftypes = 0;
		node.valid_memorytype = 0;
		node.buf_caps = 0;
		for (unsigned idx = 0; idx < V4L2_BUF_TYPE_LAST + 1; idx++)
			node.buftype_pixfmts[idx].clear();

		if (max_io) {
			sprintf(suffix, " (%s %u)",
				node.can_capture ? "Input" : "Output", io);
			if (node.can_capture) {
				struct v4l2_input descr;

				doioctl(&node, VIDIOC_S_INPUT, &io);
				descr.index = io;
				doioctl(&node, VIDIOC_ENUMINPUT, &descr);
				node.cur_io_caps = descr.capabilities;
			} else {
				struct v4l2_output descr;

				doioctl(&node, VIDIOC_S_OUTPUT, &io);
				descr.index = io;
				doioctl(&node, VIDIOC_ENUMOUTPUT, &descr);
				node.cur_io_caps = descr.capabilities;
			}
		}

		/* Control ioctls */

		printf("Control ioctls%s:\n", suffix);
		printf("\ttest VIDIOC_QUERY_EXT_CTRL/QUERYMENU: %s\n", ok(testQueryExtControls(&node)));
		printf("\ttest VIDIOC_QUERYCTRL: %s\n", ok(testQueryControls(&node)));
		printf("\ttest VIDIOC_G/S_CTRL: %s\n", ok(testSimpleControls(&node)));
		printf("\ttest VIDIOC_G/S/TRY_EXT_CTRLS: %s\n", ok(testExtendedControls(&node)));
		printf("\ttest VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: %s\n", ok(testEvents(&node)));
		printf("\ttest VIDIOC_G/S_JPEGCOMP: %s\n", ok(testJpegComp(&node)));
		printf("\tStandard Controls: %d Private Controls: %d\n",
				node.std_controls, node.priv_controls);
		printf("\n");

		/* Format ioctls */

		printf("Format ioctls%s:\n", suffix);
		printf("\ttest VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: %s\n", ok(testEnumFormats(&node)));
		printf("\ttest VIDIOC_G/S_PARM: %s\n", ok(testParm(&node)));
		printf("\ttest VIDIOC_G_FBUF: %s\n", ok(testFBuf(&node)));
		printf("\ttest VIDIOC_G_FMT: %s\n", ok(testGetFormats(&node)));
		printf("\ttest VIDIOC_TRY_FMT: %s\n", ok(testTryFormats(&node)));
		printf("\ttest VIDIOC_S_FMT: %s\n", ok(testSetFormats(&node)));
		printf("\ttest VIDIOC_G_SLICED_VBI_CAP: %s\n", ok(testSlicedVBICap(&node)));
		printf("\ttest Cropping: %s\n", ok(testCropping(&node)));
		printf("\ttest Composing: %s\n", ok(testComposing(&node)));
		printf("\ttest Scaling: %s\n", ok(testScaling(&node)));
		printf("\n");

		/* Codec ioctls */

		printf("Codec ioctls%s:\n", suffix);
		printf("\ttest VIDIOC_(TRY_)ENCODER_CMD: %s\n", ok(testEncoder(&node)));
		printf("\ttest VIDIOC_G_ENC_INDEX: %s\n", ok(testEncIndex(&node)));
		printf("\ttest VIDIOC_(TRY_)DECODER_CMD: %s\n", ok(testDecoder(&node)));
		printf("\n");

		/* Buffer ioctls */

		printf("Buffer ioctls%s:\n", suffix);
		printf("\ttest VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: %s\n", ok(testReqBufs(&node)));
		// Reopen after each streaming test to reset the streaming state
		// in case of any errors in the preceeding test.
		node.reopen();
		printf("\ttest VIDIOC_EXPBUF: %s\n", ok(testExpBuf(&node)));
		if (node.can_capture)
			node.set_interval(min_period);
		printf("\ttest Requests: %s\n", ok(testRequests(&node, options[OptStreaming])));
		// Reopen after each streaming test to reset the streaming state
		// in case of any errors in the preceeding test.
		node.reopen();
		printf("\n");
	}

	cur_io = node.has_inputs ? state.input.index : state.output.index;
	min_io = 0;

	if (!options[OptStreamAllIO]) {
		min_io = cur_io;
		max_io = min_io + 1;
	}

	for (unsigned io = min_io; io < (max_io ? max_io : 1); io++) {
		restoreState();

		if (!node.is_v4l2())
			break;

		if (options[OptStreaming] || (node.is_video && options[OptStreamAllFormats]) ||
		    (node.is_video && node.can_capture && options[OptStreamAllColorTest]))
			printf("Test %s %d:\n\n",
				node.can_capture ? "input" : "output", io);

		if (node.can_capture)
			doioctl(&node, VIDIOC_S_INPUT, &io);
		else
			doioctl(&node, VIDIOC_S_OUTPUT, &io);

		if (options[OptStreaming]) {
			printf("Streaming ioctls:\n");
			streamingSetup(&node);

			printf("\ttest read/write: %s\n", ok(testReadWrite(&node)));
			// Reopen after each streaming test to reset the streaming state
			// in case of any errors in the preceeding test.
			node.reopen();
			printf("\ttest blocking wait: %s\n", ok(testBlockingWait(&node)));
			node.reopen();
			printf("\ttest MMAP (no poll): %s\n",
			       ok(testMmap(&node, frame_count, POLL_MODE_NONE)));
			node.reopen();
			printf("\ttest MMAP (select): %s\n",
			       ok(testMmap(&node, frame_count, POLL_MODE_SELECT)));
			node.reopen();
// vb2 epoll support is broken, so disable this test for now
//			printf("\ttest MMAP (epoll): %s\n",
//			       ok(testMmap(&node, frame_count, POLL_MODE_EPOLL)));
//			node.reopen();
			printf("\ttest USERPTR (no poll): %s\n",
			       ok(testUserPtr(&node, frame_count, POLL_MODE_NONE)));
			node.reopen();
			printf("\ttest USERPTR (select): %s\n",
			       ok(testUserPtr(&node, frame_count, POLL_MODE_SELECT)));
			node.reopen();
			if (options[OptSetExpBufDevice] ||
			    !(node.valid_memorytype & (1 << V4L2_MEMORY_DMABUF))) {
				printf("\ttest DMABUF (no poll): %s\n",
				       ok(testDmaBuf(&expbuf_node, &node, frame_count, POLL_MODE_NONE)));
				node.reopen();
				printf("\ttest DMABUF (select): %s\n",
				       ok(testDmaBuf(&expbuf_node, &node, frame_count, POLL_MODE_SELECT)));
				node.reopen();
			} else if (!options[OptSetExpBufDevice]) {
				printf("\ttest DMABUF: Cannot test, specify --expbuf-device\n");
			}

			printf("\n");
		}

		if (node.is_video && options[OptStreamAllFormats]) {
			printf("Stream using all formats:\n");

			if (node.is_m2m) {
				printf("\tNot supported for M2M devices\n");
			} else {
				streamingSetup(&node);
				streamAllFormats(&node);
			}
		}

		if (node.is_video && node.can_capture && options[OptStreamAllColorTest]) {
			printf("Stream using all formats and do a color check:\n");

			if (node.is_m2m) {
				printf("\tNot supported for M2M devices\n");
			} else {
				streamingSetup(&node);
				testColorsAllFormats(&node, color_component,
						     color_skip, color_perc);
			}
		}
	}

	/*
	 * TODO: VIDIOC_S_FBUF/OVERLAY
	 * 	 S_SELECTION flags tests
	 */

	restoreState();

show_total:
	/* Final test report */
	if (driver.empty())
		printf("Total for device %s%s: %d, Succeeded: %d, Failed: %d, Warnings: %d\n",
		       node.device, node.g_direct() ? "" : " (using libv4l2)",
		       tests_total, tests_ok, tests_total - tests_ok, warnings);
	else
		printf("Total for %s device %s%s: %d, Succeeded: %d, Failed: %d, Warnings: %d\n",
		       driver.c_str(), node.device, node.g_direct() ? "" : " (using libv4l2)",
		       tests_total, tests_ok, tests_total - tests_ok, warnings);
	grand_total += tests_total;
	grand_ok += tests_ok;
	grand_warnings += warnings;

	if (node.is_media() && options[OptSetMediaDevice]) {
		walkTopology(node, expbuf_node, frame_count);
		/* Final test report */
		printf("\nGrand Total for %s device %s: %d, Succeeded: %d, Failed: %d, Warnings: %d\n",
		       driver.c_str(), node.device,
		       grand_total, grand_ok, grand_total - grand_ok, grand_warnings);
	}

	node.close();
	if (node.node2)
		node.node2->close();
}

int main(int argc, char **argv)
{
	int i;
	struct node node;
	media_type type = MEDIA_TYPE_UNKNOWN;
	struct node expbuf_node;
	std::string media_bus_info;

	/* command args */
	int ch;
	const char *device = "/dev/video0";
	const char *expbuf_device = NULL;	/* --expbuf-device device */
	struct utsname uts;
	int v1, v2, v3;
	unsigned frame_count = 60;
	char short_options[26 * 2 * 3 + 1];
	char *value, *subs;
	int idx = 0;

#ifdef SHA
#define STR(x) #x
#define STRING(x) STR(x)
	printf("v4l2-compliance SHA: %s, %zd bits\n", STRING(SHA), sizeof(void *) * 8);
#else
	printf("v4l2-compliance SHA: not available, %zd bits\n", sizeof(void *) * 8);
#endif

	uname(&uts);
	sscanf(uts.release, "%d.%d.%d", &v1, &v2, &v3);
	if (v1 == 2 && v2 == 6)
		kernel_version = v3;
	if (kernel_version)
		printf("Running on 2.6.%d\n", kernel_version);
	printf("\n");

	for (i = 0; long_options[i].name; i++) {
		if (!isalpha(long_options[i].val))
			continue;
		short_options[idx++] = long_options[i].val;
		if (long_options[i].has_arg == required_argument) {
			short_options[idx++] = ':';
		} else if (long_options[i].has_arg == optional_argument) {
			short_options[idx++] = ':';
			short_options[idx++] = ':';
		}
	}
	while (1) {
		int option_index = 0;

		short_options[idx] = 0;
		ch = getopt_long(argc, argv, short_options,
				 long_options, &option_index);
		if (ch == -1)
			break;

		options[(int)ch] = 1;
		if (!option_index) {
			for (i = 0; long_options[i].val; i++) {
				if (long_options[i].val == ch) {
					option_index = i;
					break;
				}
			}
		}
		if (long_options[option_index].has_arg == optional_argument &&
		    !optarg && argv[optind] && argv[optind][0] != '-')
			optarg = argv[optind++];

		switch (ch) {
		case OptHelp:
			usage();
			return 0;
		case OptSetDevice:
			device = make_devname(optarg, "video", media_bus_info);
			break;
		case OptSetVbiDevice:
			device = make_devname(optarg, "vbi", media_bus_info);
			break;
		case OptSetRadioDevice:
			device = make_devname(optarg, "radio", media_bus_info);
			break;
		case OptSetSWRadioDevice:
			device = make_devname(optarg, "swradio", media_bus_info);
			break;
		case OptSetTouchDevice:
			device = make_devname(optarg, "v4l-touch", media_bus_info);
			break;
		case OptSetSubDevDevice:
			device = make_devname(optarg, "v4l-subdev", media_bus_info);
			break;
		case OptSetMediaDevice:
		case OptSetMediaDeviceOnly:
			device = make_devname(optarg, "media", media_bus_info);
			type = MEDIA_TYPE_MEDIA;
			break;
		case OptMediaBusInfo:
			media_bus_info = optarg;
			break;
		case OptSetExpBufDevice:
			expbuf_device = optarg;
			if (expbuf_device[0] >= '0' && expbuf_device[0] <= '9' && strlen(expbuf_device) <= 3) {
				static char newdev[20];

				sprintf(newdev, "/dev/video%s", expbuf_device);
				expbuf_device = newdev;
			}
			break;
		case OptStreaming:
			if (optarg)
				frame_count = strtoul(optarg, NULL, 0);
			break;
		case OptStreamFrom:
		case OptStreamFromHdr: {
			char *equal = strchr(optarg, '=');
			bool has_hdr = ch == OptStreamFromHdr;

			if (equal == optarg)
				equal = NULL;
			if (equal) {
				*equal = '\0';
				stream_from_map[optarg] = equal + 1;
				stream_hdr_map[optarg] = has_hdr;
			} else {
				stream_from_map[""] = optarg;
				stream_hdr_map[""] = has_hdr;
			}
			break;
		}
		case OptStreamAllColorTest:
			subs = optarg;
			while (*subs != '\0') {
				static const char *const subopts[] = {
					"color",
					"skip",
					"perc",
					NULL
				};

				switch (parse_subopt(&subs, subopts, &value)) {
				case 0:
					if (!strcmp(value, "red"))
						color_component = 0;
					else if (!strcmp(value, "green"))
						color_component = 1;
					else if (!strcmp(value, "blue"))
						color_component = 2;
					else {
						usage();
						exit(1);
					}
					break;
				case 1:
					color_skip = strtoul(value, 0L, 0);
					break;
				case 2:
					color_perc = strtoul(value, 0L, 0);
					if (color_perc == 0)
						color_perc = 90;
					if (color_perc > 100)
						color_perc = 100;
					break;
				default:
					usage();
					exit(1);
				}
			}
			break;
		case OptNoWarnings:
			show_warnings = false;
			break;
		case OptExitOnWarn:
			exit_on_warn = true;
			break;
		case OptExitOnFail:
			exit_on_fail = true;
			break;
		case OptVerbose:
			show_info = true;
			break;
		case ':':
			fprintf(stderr, "Option `%s' requires a value\n",
				argv[optind]);
			usage();
			return 1;
		case '?':
			if (argv[optind])
				fprintf(stderr, "Unknown argument `%s'\n",
					argv[optind]);
			usage();
			return 1;
		}
	}
	if (optind < argc) {
		fprintf(stderr, "unknown arguments: ");
		while (optind < argc)
			fprintf(stderr, "%s ", argv[optind++]);
		fprintf(stderr, "\n");
		usage();
		return 1;
	}
	bool direct = !options[OptUseWrapper];
	int fd;

	if (type == MEDIA_TYPE_UNKNOWN)
		type = mi_media_detect_type(device);
	if (type == MEDIA_TYPE_CANT_STAT) {
		fprintf(stderr, "Cannot open device %s, exiting.\n",
			device);
		exit(1);
	}
	if (type == MEDIA_TYPE_UNKNOWN) {
		fprintf(stderr, "Unable to detect what device %s is, exiting.\n",
			device);
		exit(1);
	}

	node.device = device;
	node.s_trace(options[OptTrace]);
	switch (type) {
	case MEDIA_TYPE_MEDIA:
		node.s_direct(true);
		fd = node.media_open(device, false);
		break;
	case MEDIA_TYPE_SUBDEV:
		node.s_direct(true);
		fd = node.subdev_open(device, false);
		break;
	default:
		node.s_direct(direct);
		fd = node.open(device, false);
		break;
	}
	if (fd < 0) {
		fprintf(stderr, "Failed to open %s: %s\n", device,
			strerror(errno));
		exit(1);
	}

	if (expbuf_device) {
		expbuf_node.s_trace(options[OptTrace]);
		expbuf_node.s_direct(true);
		fd = expbuf_node.open(expbuf_device, false);
		if (fd < 0) {
			fprintf(stderr, "Failed to open %s: %s\n", expbuf_device,
				strerror(errno));
			exit(1);
		}
	}

	testNode(node, expbuf_node, type, frame_count);

	if (expbuf_device)
		expbuf_node.close();
	if (media_fd >= 0)
		close(media_fd);

	exit(app_result);
}

Privacy Policy