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
|
/* -*- c-file-style: "linux" -*- */
/*
* v4l2loopback-ctl -- An application to control v4l2loopback devices driver
*
* Copyright (C) 2020-2023 IOhannes m zmoelnig (zmoelnig@iem.at)
*
* 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.
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <ctype.h>
#include <getopt.h>
#include <glob.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
#include <errno.h>
#include "v4l2loopback.h"
#ifndef GLOB_ONLYDIR
/* Fix for musl libc and other libcs missing GLOB_ONLYDIR at glob.h */
/* (GLOB_ONLYDIR is not required by POSIX) */
#define GLOB_ONLYDIR 0
#endif
#define CONTROLDEVICE "/dev/v4l2loopback"
#if 0
#define MARK() dprintf(2, "%s:%d @ %s\n", __FILE__, __LINE__, __func__)
#else
#define MARK()
#endif
struct v4l2l_format {
char *name;
int fourcc; /* video4linux 2 */
int depth; /* bit/pixel */
int flags;
};
#define FORMAT_FLAGS_PLANAR 0x01
#define FORMAT_FLAGS_COMPRESSED 0x02
#include "../v4l2loopback_formats.h"
/********************/
/* helper functions */
/* running externals programs */
static char *which(char *outbuf, size_t bufsize, const char *filename)
{
struct stat statbuf;
char *paths, *saveptr = NULL;
if (filename && '/' == *filename) {
/* an absolute filename */
int err = stat(filename, &statbuf);
if (!err) {
snprintf(outbuf, bufsize, "%s", filename);
return outbuf;
}
return NULL;
}
for (paths = getenv("PATH");; paths = NULL) {
char *path = strtok_r(paths, ":", &saveptr);
int err;
if (path == NULL)
return NULL;
snprintf(outbuf, bufsize, "%s/%s", path, filename);
err = stat(outbuf, &statbuf);
if (!err)
return outbuf;
}
return NULL;
}
static pid_t pid;
void exec_cleanup(int signal)
{
if (pid) {
switch (signal) {
default:
break;
case SIGINT:
kill(pid, SIGTERM);
break;
}
}
while (waitpid((pid_t)(-1), 0, WNOHANG) > 0) {
}
}
static int my_execv(char *const *cmdline)
{
char exe[1024];
int res = 0;
if (!which(exe, 1024, cmdline[0])) {
dprintf(2, "cannot find %s - is it installed???\n", cmdline[0]);
return 1;
}
#if 0
do {
char *const *argp = cmdline;
dprintf(2, "%s:", exe);
while (*argp) {
dprintf(2, " %s", *argp++);
};
dprintf(2, "\n");
} while(0);
#endif
pid = fork();
if (pid == 0) { /* this is the child-process */
res = execv(exe, cmdline);
if (res < 0) {
dprintf(2, "ERROR running helper program (%d, %d)", res,
errno);
dprintf(2, "failed program was:\n\t");
while (*cmdline)
dprintf(2, " %s", *cmdline++);
dprintf(2, "\n");
exit(0);
}
exit(0);
} else if (pid > 0) { /* we are parent: wait for child */
int status = 0;
int waitoptions = 0;
signal(SIGCHLD, exec_cleanup);
signal(SIGINT, exec_cleanup);
waitpid(pid, &status, waitoptions);
pid = 0;
if (WIFEXITED(status))
return WEXITSTATUS(status);
return 0;
} else { /* pid < 0, error */
dprintf(2, "ERROR: child fork failed\n");
exit(1);
}
return 0;
}
/* misc */
static int my_atoi(const char *name, const char *s)
{
char *endptr = 0;
int n = strtol(s, &endptr, 10);
if (*endptr) {
dprintf(2, "%s must be a number (got: '%s')\n", name, s);
exit(1);
}
return n;
}
static void printf_raw(const char *str, int escape_level)
{
const char *backslash = (escape_level > 1) ? "\\\\" : "\\";
if (escape_level > 0)
while (*str) {
char c = *str++;
switch (c) {
case '\"':
printf("%s\"", backslash);
break;
case '\'':
printf("%s\'", backslash);
break;
case '\\':
printf("%s\\", backslash);
break;
case '\a':
printf("%sa", backslash);
break;
case '\b':
printf("%sb", backslash);
break;
case '\n':
printf("%sn", backslash);
break;
case '\t':
printf("%st", backslash);
break;
// and so on
default:
if (iscntrl(c))
printf("%s%03o", backslash, c);
else
printf("%c", c);
}
}
else
printf("%s", str);
}
static char *fourcc2str(unsigned int fourcc, char buf[4])
{
buf[0] = (fourcc >> 0) & 0xFF;
buf[1] = (fourcc >> 8) & 0xFF;
buf[2] = (fourcc >> 16) & 0xFF;
buf[3] = (fourcc >> 24) & 0xFF;
return buf;
}
unsigned int str2fourcc(char buf[4])
{
return (buf[0]) + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24);
}
/* helper functions */
/********************/
static unsigned int _get_control_id(int fd, const char *control)
{
const size_t length = strnlen(control, 1024);
const unsigned next = V4L2_CTRL_FLAG_NEXT_CTRL;
struct v4l2_queryctrl qctrl;
int id;
memset(&qctrl, 0, sizeof(qctrl));
while (ioctl(fd, VIDIOC_QUERYCTRL, &qctrl) == 0) {
if (!strncmp(qctrl.name, control, length))
return qctrl.id;
qctrl.id |= next;
}
for (id = V4L2_CID_USER_BASE; id < V4L2_CID_LASTP1; id++) {
qctrl.id = id;
if (ioctl(fd, VIDIOC_QUERYCTRL, &qctrl) == 0) {
if (!strncmp(qctrl.name, control, length))
return qctrl.id;
}
}
for (qctrl.id = V4L2_CID_PRIVATE_BASE;
ioctl(fd, VIDIOC_QUERYCTRL, &qctrl) == 0; qctrl.id++) {
if (!strncmp(qctrl.name, control, length)) {
unsigned int id = qctrl.id;
return id;
}
}
return 0;
}
static int set_control_i(int fd, const char *control, int value)
{
struct v4l2_control ctrl;
memset(&ctrl, 0, sizeof(ctrl));
ctrl.id = _get_control_id(fd, control);
ctrl.value = value;
if (ctrl.id && ioctl(fd, VIDIOC_S_CTRL, &ctrl) == 0) {
int value = ctrl.value;
return value;
}
return 0;
}
static int get_control_i(int fd, const char *control)
{
struct v4l2_control ctrl;
memset(&ctrl, 0, sizeof(ctrl));
ctrl.id = _get_control_id(fd, control);
if (ctrl.id && ioctl(fd, VIDIOC_G_CTRL, &ctrl) == 0) {
int value = ctrl.value;
return value;
}
return 0;
}
/********************/
/* main logic */
typedef enum {
VERSION,
HELP,
ADD,
DELETE,
LIST,
QUERY,
SET_FPS,
GET_FPS,
SET_CAPS,
GET_CAPS,
SET_TIMEOUTIMAGE,
MOO,
_UNKNOWN
} t_command;
static void _help(int detail, const char *section, const char *program,
const char *verb, const char *argstring,
const char *description, const char *options)
{
(void)section;
if (!detail) {
dprintf(2, "%s%s%s %s\n", program ? program : "",
program ? " " : "", verb, argstring);
return;
}
dprintf(2, "\n%s%s%s %s:\n", program ? program : "", program ? " " : "",
verb, argstring);
dprintf(2, " %s\n ", description);
if (options) {
dprintf(2, "%s\n", options);
//dprintf(2, "\n\n'%s' OPTIONS:%s\n", verb, options);
}
}
static void help_list(const char *program, int detail)
{
_help(detail, "Listing Devices", program, "list", "[OPTIONS]",
"list all available loopback-devices",
"\n\t-e, --escape escape control-characters in (device) names"
"\n\t-h, --help print this help and exit"
"");
}
static void help_add(const char *program, int detail)
{
_help(detail, "Adding Devices", program, "add",
"[OPTIONS] [<outputdevice> [<capturedevice>]]",
"create/add a new loopback-device",
"\n\t-b <num>, --buffers <num> buffers to queue"
"\n\t-h <h>, --max-height <h> maximum allowed frame height"
"\n\t-n <name>, --name <name> pretty name for the device"
"\n\t-o <num>, --max-openers <num> maximum allowed concurrent openers"
"\n\t-v, --verbose verbose mode (print properties of device after successfully creating it)"
"\n\t-w <w>, --max-width <w> maximum allowed frame width"
"\n\t-x <x>, --exclusive-caps <x> whether to announce OUTPUT/CAPTURE capabilities exclusively"
"\n\t--min-width <w> minimum allowed frame width"
"\n\t--min-height <w> minimum allowed frame height"
"\n\t-?, --help print this help and exit"
"\n"
"\n <outputdevice>\tif given, create a specific device (otherwise just create a free one)."
"\n \teither specify a device name (e.g. '/dev/video1') or a device number ('1')."
"\n <capturedevice>\tif given, use separate output & capture devices (otherwise they are the same).");
}
static void help_delete(const char *program, int detail)
{
_help(detail, "Deleting Devices", program, "delete", "<device>",
"delete/remove an unused loopback device",
"\n <device>\tcan be given one more more times (to delete multiple devices at once)."
"\n \teither specify a device name (e.g. '/dev/video1') or a device number ('1').");
}
static void help_query(const char *program, int detail)
{
_help(detail, "Querying Devices", program, "query",
"[OPTIONS] <device>", "query information about a loopback device",
"\n\t-e, --escape escape control-characters in (device) names"
"\n\t-h, --help print this help and exit"
"\n"
"\n <device>\tcan be given one more more times (to query multiple devices at once)."
"\n \teither specify a device name (e.g. '/dev/video1') or a device number ('1').");
}
static void help_setfps(const char *program, int detail)
{
_help(detail, "Setting Framerate", program, "set-fps", "<device> <fps>",
"set the default framerate for a loopback device",
"\n <device>\teither specify a device name (e.g. '/dev/video1') or a device number ('1')."
"\n <fps>\tframes per second, either as integer ('30') or fraction ('50/2').");
}
static void help_getfps(const char *program, int detail)
{
_help(detail, "Getting Framerate", program, "get-fps", "<device>",
"query the framerate of a loopback device", 0);
}
static void help_setcaps(const char *program, int detail)
{
_help(detail, "Setting Capabilities", program, "set-caps",
"<device> <caps>",
"set format/dimension/framerate of a loopback device",
"\n <device>\teither specify a device name (e.g. '/dev/video1') or a device number ('1')."
"\n <caps>\tformat specification as '<fourcc>:<width>x<height>@<fps>' (e.g. 'UYVY:1024x768@60/1')"
"\n \tunset the current caps with the special value 'any'");
if (detail > 1) {
dprintf(2, "\nknown fourcc-codes"
"\n=================="
"\nFOURCC\thex \tdec \tdescription"
"\n------\t----------\t------------\t-----------"
"");
char fourcc[5];
const size_t num_formats = sizeof(formats) / sizeof(*formats);
size_t i = 0;
for (i = 0; i < num_formats; i++) {
const struct v4l2l_format *fmt = formats + i;
memset(fourcc, 0, 5);
dprintf(2, "'%4s'\t0x%08X\t%12d\t%s\n",
fourcc2str(fmt->fourcc, fourcc), fmt->fourcc,
fmt->fourcc, fmt->name);
}
}
}
static void help_getcaps(const char *program, int detail)
{
_help(detail, "Getting Capabilities", program, "get-caps", "<device>",
"get current format/dimension/framerate of a loopback device", 0);
}
static void help_settimeoutimage(const char *program, int detail)
{
_help(detail, "Setting Timeout Image", program, "set-timeout-image",
"[OPTIONS] <device> <image>",
"set a fallback image to be used if a video producer does not send new frames in time.",
"\n <flags>\tany of the following flags may be present"
"\n\t-h, --help print this help and exit"
"\n\t-t <timeout>, --timeout <timeout> timeout (in ms)"
"\n\t-v, --verbose raise verbosity (print what is being done)"
"\n"
"\n <device>\teither specify a device name (e.g. '/dev/video1') or a device number ('1')."
"\n <image>\timage file");
}
static void help_none(const char *program, int detail)
{
}
typedef void (*t_help)(const char *, int);
static t_help get_help(t_command cmd)
{
switch (cmd) {
default:
break;
case ADD:
return help_add;
case DELETE:
return help_delete;
case LIST:
return help_list;
case QUERY:
return help_query;
case SET_FPS:
return help_setfps;
case GET_FPS:
return help_getfps;
case SET_CAPS:
return help_setcaps;
case GET_CAPS:
return help_getcaps;
case SET_TIMEOUTIMAGE:
return help_settimeoutimage;
}
return help_none;
}
static void help(const char *name, int status)
{
t_command cmd;
dprintf(2, "Usage: %s [OPTIONS]\n", name);
for (cmd = ADD; cmd < _UNKNOWN; cmd++) {
t_help hlp = get_help(cmd);
if (help_none == hlp)
continue;
dprintf(2, " or: %s ", name);
hlp(0, 0);
}
dprintf(2, "\nManage v4l2 loopback devices.");
dprintf(2,
"\n"
"\nThe general invocation uses a verb (like 'add' or 'delete') that defines"
"\nan action to be executed. Each verb has their own options and arguments."
"\n"
"\nOptions:"
"\n\t-h, -?, --help: print this help and exit"
"\n\t-v, --version: print version and exit"
"\n\n");
/* long helps */
dprintf(2, "*Verbs and their arguments*\n");
for (cmd = ADD; cmd < _UNKNOWN; cmd++) {
t_help hlp = get_help(cmd);
if (help_none == hlp)
continue;
hlp("v4l2loopback-ctl", 1);
dprintf(2, "\n\n");
}
dprintf(2,
"*Reporting Bugs*\n"
"\nIssue tracker: https://github.com/umlaeute/v4l2loopback/issues"
"\nSecurity Issue tracker: https://git.iem.at/zmoelnig/v4l2loopback/-/issues"
"\n\n");
exit(status);
}
static void usage(const char *name)
{
help(name, 1);
}
static void usage_topic(const char *name, t_command cmd, int argc, char **argv)
{
t_help hlp = get_help(cmd);
if (help_none == hlp)
usage(name);
else
hlp(name, 2);
dprintf(2, "\n");
exit(1);
}
static const char *my_realpath(const char *path, char *resolved_path)
{
char *str = realpath(path, resolved_path);
return str ? str : path;
}
static int parse_device(const char *devicename_)
{
char devicenamebuf[4096];
const char *devicename = my_realpath(devicename_, devicenamebuf);
int ret = strncmp(devicename, "/dev/video", 10);
const char *device = (ret) ? devicename : (devicename + 10);
char *endptr = 0;
int dev = strtol(device, &endptr, 10);
if (!*endptr)
return dev;
return -1;
}
static void print_conf(struct v4l2_loopback_config *cfg, int escape_level)
{
int output_nr, capture_nr;
MARK();
if (!cfg) {
printf("configuration: %p\n", cfg);
return;
}
output_nr = capture_nr = cfg->output_nr;
#ifdef SPLIT_DEVICES
capture_nr = cfg->capture_nr;
#endif
MARK();
printf("\tcapture_device# : %d"
"\n\toutput_device# : %d"
"\n\tcard_label : ",
capture_nr, output_nr);
printf_raw(cfg->card_label, escape_level);
printf("\n\tmin_width : %d"
"\n\tmax_width : %d"
"\n\tmin_height : %d"
"\n\tmax_height : %d"
"\n\tannounce_all_caps: %d"
"\n\tmax_buffers : %d"
"\n\tmax_openers : %d"
"\n\tdebug : %d"
"\n",
cfg->min_width, cfg->max_width, cfg->min_height, cfg->max_height,
cfg->announce_all_caps, cfg->max_buffers, cfg->max_openers,
cfg->debug);
MARK();
}
static struct v4l2_loopback_config *
make_conf(struct v4l2_loopback_config *cfg, const char *label, int min_width,
int max_width, int min_height, int max_height, int exclusive_caps,
int buffers, int openers, int capture_device, int output_device)
{
if (!cfg)
return 0;
/* check if at least one of the args are non-default */
if (!label && min_width <= 0 && max_width <= 0 && min_height <= 0 &&
max_height <= 0 && exclusive_caps < 0 && buffers <= 0 &&
openers <= 0 && capture_device < 0 && output_device < 0)
return 0;
#ifdef SPLIT_DEVICES
cfg->capture_nr = capture_device;
#endif
cfg->output_nr = output_device;
cfg->card_label[0] = 0;
if (label)
snprintf(cfg->card_label, 32, "%s", label);
cfg->min_width = (min_width < 0) ? 0 : min_width;
cfg->max_width = (max_width < 0) ? 0 : max_width;
cfg->min_height = (min_height < 0) ? 0 : min_height;
cfg->max_height = (max_height < 0) ? 0 : max_height;
cfg->announce_all_caps = (exclusive_caps < 0) ? -1 : !exclusive_caps;
cfg->max_buffers = buffers;
cfg->max_openers = openers;
cfg->debug = 0;
return cfg;
}
static int add_device(int fd, struct v4l2_loopback_config *cfg, int verbose)
{
int err = 0;
MARK();
int ret = ioctl(fd, V4L2LOOPBACK_CTL_ADD, cfg);
MARK();
if (ret < 0) {
err = errno;
perror("failed to create device");
return err;
}
MARK();
printf("/dev/video%d\n", ret);
if (verbose > 0) {
MARK();
struct v4l2_loopback_config config;
memset(&config, 0, sizeof(config));
config.output_nr = ret;
#ifdef SPLIT_DEVICES
config.capture_nr = ret;
#endif
ret = ioctl(fd, V4L2LOOPBACK_CTL_QUERY, &config);
if (ret < 0) {
err = errno;
perror("failed querying newly added device");
}
MARK();
print_conf(&config, 0);
MARK();
}
return err;
}
static int delete_device(int fd, const char *devicename)
{
int err = 0;
int dev = parse_device(devicename);
if (dev < 0) {
dprintf(2, "ignoring illegal devicename '%s'\n", devicename);
return 1;
}
if (ioctl(fd, V4L2LOOPBACK_CTL_REMOVE, dev) < 0) {
err = errno;
perror(devicename);
}
return err;
}
static int query_device(int fd, const char *devicename, int escape)
{
int err;
struct v4l2_loopback_config config;
int dev = parse_device(devicename);
if (dev < 0) {
dprintf(2, "ignoring illegal devicename '%s'\n", devicename);
return 1;
}
memset(&config, 0, sizeof(config));
config.output_nr = dev;
#ifdef SPLIT_DEVICES
config.capture_nr = dev;
#endif
err = ioctl(fd, V4L2LOOPBACK_CTL_QUERY, &config);
if (err)
perror("query failed");
else {
printf("%s\n", devicename);
print_conf(&config, escape);
return 0;
}
return err;
}
static int list_devices(int fd, int escape)
{
struct devnode_ {
int output, capture;
char name[32];
} *devices = 0;
size_t numdevices = 0, i;
glob_t globbuf = { 0 };
int output_nr, capture_nr;
glob("/sys/devices/virtual/video4linux/video*", GLOB_ONLYDIR, 0,
&globbuf);
if (globbuf.gl_pathc) {
devices = malloc(globbuf.gl_pathc * sizeof(*devices));
}
for (i = 0; i < globbuf.gl_pathc; i++) {
size_t j;
struct v4l2_loopback_config config = { 0 };
int dev = -1;
char *endptr;
struct stat sb;
const char *path = globbuf.gl_pathv[i];
if (lstat(path, &sb)) {
//perror("stat");
continue;
}
if (!S_ISDIR(sb.st_mode)) {
//dprintf(2, "not a directory\n");
continue;
}
dev = strtol(path + 38, &endptr, 10);
if (*endptr) {
//dprintf(2, "unable to parse device-name\n");
continue;
}
/* check if this is a loopback device */
config.output_nr = dev;
#ifdef SPLIT_DEVICES
config.capture_nr = -1;
if (ioctl(fd, V4L2LOOPBACK_CTL_QUERY, &config)) {
memset(&config, 0, sizeof(config));
config.output_nr = -1;
config.capture_nr = dev;
if (ioctl(fd, V4L2LOOPBACK_CTL_QUERY, &config)) {
//dprintf(2, "not a loopback device\n");
continue;
}
}
capture_nr = config.capture_nr;
#else
if (ioctl(fd, V4L2LOOPBACK_CTL_QUERY, &config)) {
//dprintf(2, "not a loopback device\n");
continue;
}
capture_nr = config.output_nr;
#endif
output_nr = config.output_nr;
/* check if we already have this device */
for (j = 0; j < numdevices; j++) {
if ((devices[j].output == output_nr) &&
(devices[j].capture == capture_nr)) {
//dprintf(2, "duplicate device\n");
output_nr = capture_nr = -1;
break;
}
}
if ((output_nr < 0) || (capture_nr < 0))
continue;
devices[numdevices].output = output_nr;
devices[numdevices].capture = capture_nr;
snprintf(devices[numdevices].name,
sizeof(devices[numdevices].name), "%s",
config.card_label);
numdevices++;
}
if (numdevices) {
dprintf(2, "OUTPUT \tCAPTURE \tNAME\n");
} else {
dprintf(2, "no loopback devices found\n");
}
for (i = 0; i < numdevices; i++) {
const char *str = devices[i].name;
printf("/dev/video%-3d\t/dev/video%-3d\t", devices[i].output,
devices[i].capture);
printf_raw(str, escape);
printf("\n");
}
globfree(&globbuf);
free(devices);
return 0;
}
static int open_videodevice(const char *devicename, int mode)
{
int fd = open(devicename, mode);
if (fd < 0) {
int devnr = parse_device(devicename);
if (devnr >= 0) {
char devname[100];
snprintf(devname, 99, "/dev/video%d", devnr);
devname[99] = 0;
fd = open(devname, mode);
}
}
return fd;
}
static int open_controldevice()
{
int fd = open(CONTROLDEVICE, 0);
if (fd < 0) {
perror("unable to open control device '" CONTROLDEVICE "'");
exit(1);
}
return fd;
}
static int open_sysfs_file(const char *devicename, const char *filename,
int flags)
{
int fd = -1;
char sysdev[100];
int dev = parse_device(devicename);
if (dev < 0) {
dprintf(2, "ignoring illegal devicename '%s'\n", devicename);
return -1;
}
snprintf(sysdev, sizeof(sysdev) - 1,
"/sys/devices/virtual/video4linux/video%d/%s", dev, filename);
sysdev[sizeof(sysdev) - 1] = 0;
fd = open(sysdev, flags);
if (fd < 0) {
perror(sysdev);
return -1;
}
//dprintf(2, "%s\n", sysdev);
return fd;
}
static int parse_fps(const char *fps, int *numerator, int *denominator)
{
int num = 0;
int denom = 1;
if (sscanf(fps, "%d/%d", &num, &denom) <= 0) {
return 1;
}
if (numerator)
*numerator = num;
if (denominator)
*denominator = denom;
return 0;
}
static int is_fps(const char *fps)
{
return parse_fps(fps, 0, 0);
}
static int set_fps(const char *devicename, const char *fps)
{
int result = 1;
char _fps[100];
int fd = open_sysfs_file(devicename, "format", O_WRONLY);
if (fd < 0)
return 1;
snprintf(_fps, sizeof(_fps) - 1, "@%s", fps);
_fps[sizeof(_fps) - 1] = 0;
if (write(fd, _fps, strnlen(_fps, sizeof(_fps))) < 0) {
perror("failed to set fps");
goto done;
}
result = 0;
done:
close(fd);
return result;
}
typedef struct _caps {
unsigned int fourcc;
int width, height;
int fps_num, fps_denom;
} t_caps;
static void print_caps(t_caps *caps)
{
char fourcc[4];
if (!caps) {
dprintf(2, "no caps\n");
return;
}
dprintf(2, "FOURCC : %.4s\n", fourcc2str(caps->fourcc, fourcc));
dprintf(2, "dimen : %dx%d\n", caps->width, caps->height);
dprintf(2, "fps : %d/%d\n", caps->fps_num, caps->fps_denom);
}
static int parse_caps(const char *buffer, t_caps *caps)
{
char fourcc[5];
memset(caps, 0, sizeof(*caps));
memset(fourcc, 0, sizeof(*fourcc));
caps->fps_denom = 1;
if (!(buffer && *buffer))
return 1;
if (sscanf(buffer, "%4c:%dx%d@%d/%d", fourcc, &caps->width,
&caps->height, &caps->fps_num, &caps->fps_denom) <= 0) {
}
caps->fourcc = str2fourcc(fourcc);
return (0 == caps->fourcc);
}
static int read_caps(const char *devicename, t_caps *caps)
{
int result = 1;
char _caps[100];
int len;
int fd = open_sysfs_file(devicename, "format", O_RDONLY);
if (fd < 0)
return 1;
len = read(fd, _caps, 100);
if (len <= 0) {
if (len)
perror("failed to read fps");
goto done;
}
_caps[100 - 1] = 0;
if (caps) {
if (parse_caps(_caps, caps)) {
dprintf(2, "unable to parse format '%s'\n", _caps);
goto done;
}
}
result = 0;
done:
close(fd);
return result;
}
static int get_fps(const char *devicename)
{
t_caps caps;
struct v4l2_streamparm param;
int fd = -1;
int num = -1, denom = -1;
int ret = 0;
if (!read_caps(devicename, &caps)) {
num = caps.fps_num;
denom = caps.fps_denom;
goto done;
}
/* get the framerate via ctls */
fd = open_videodevice(devicename, O_RDWR);
if (fd < 0) {
ret = 1;
goto done;
}
memset(¶m, 0, sizeof(param));
param.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
if (ioctl(fd, VIDIOC_G_PARM, ¶m) == 0) {
const struct v4l2_fract *tf = ¶m.parm.output.timeperframe;
num = tf->numerator;
denom = tf->denominator;
goto done;
}
memset(¶m, 0, sizeof(param));
param.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (ioctl(fd, VIDIOC_G_PARM, ¶m) == 0) {
const struct v4l2_fract *tf = ¶m.parm.output.timeperframe;
num = tf->numerator;
denom = tf->denominator;
goto done;
}
ret = 1;
done:
if (fd >= 0)
close(fd);
printf("%d/%d\n", num, denom);
return ret;
}
static int set_caps(const char *devicename, const char *capsstring)
{
int result = 1;
int fd = open_videodevice(devicename, O_RDWR);
struct v4l2_format vid_format;
struct v4l2_capability vid_caps;
t_caps caps;
/* now open up the device */
if (fd < 0)
goto done;
if (!strncmp("any", capsstring, 4)) {
/* skip caps-parsing */
} else if (!strncmp("video/", capsstring, 6)) {
dprintf(2,
"ERROR: GStreamer-style caps are no longer supported!\n");
dprintf(2,
"ERROR: use '<FOURCC>:<width>x<height>[@<fps>] instead\n");
dprintf(2,
" e.g. 'UYVY:640x480@30/1' or 'RGBA:1024x768'\n");
goto done;
} else if (parse_caps(capsstring, &caps)) {
dprintf(2, "unable to parse format '%s'\n", capsstring);
goto done;
}
//print_caps(&caps);
/* check whether this is actually a video-device */
if (ioctl(fd, VIDIOC_QUERYCAP, &vid_caps) == -1) {
perror("VIDIOC_QUERYCAP");
goto done;
}
if (!strncmp("any", capsstring, 4)) {
set_control_i(fd, "keep_format", 0);
//set_control_i(fd, "sustain_framerate", 0);
result = 0;
goto done;
}
/* try to get the default values for the format first */
memset(&vid_format, 0, sizeof(vid_format));
vid_format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
if (ioctl(fd, VIDIOC_G_FMT, &vid_format) == -1) {
perror("VIDIOC_G_FMT");
}
/* and set those caps that we have */
if (caps.width)
vid_format.fmt.pix.width = caps.width;
if (caps.height)
vid_format.fmt.pix.height = caps.height;
if (caps.fourcc)
vid_format.fmt.pix.pixelformat = caps.fourcc;
if (ioctl(fd, VIDIOC_S_FMT, &vid_format) == -1) {
perror("unable to set requested format");
goto done;
}
set_control_i(fd, "keep_format", 1);
/* finally, try setting the fps */
if (caps.fps_num && caps.fps_denom) {
char fps[100];
int didit;
snprintf(fps, 100, "%d/%d", caps.fps_num, caps.fps_denom);
didit = set_fps(devicename, fps);
if (!didit) {
set_control_i(fd, "sustain_framerate", 1);
}
close(fd);
fd = -1;
return didit;
}
result = 0;
done:
if (fd >= 0)
close(fd);
return result;
}
static int get_caps(const char *devicename)
{
int format = 0;
t_caps caps;
char fourcc[4];
if (read_caps(devicename, &caps))
return 1;
switch (format) {
default:
printf("%.4s:%dx%d@%d/%d\n", fourcc2str(caps.fourcc, fourcc),
caps.width, caps.height, caps.fps_num, caps.fps_denom);
break;
case 1: /* GStreamer-1.0 */
/* FOURCC is different everywhere... */
switch (caps.fourcc) {
default:
break;
case 0x56595559: /* YUYV */
caps.fourcc = str2fourcc("YUY2");
break;
}
printf("video/x-raw,format=%.4s,width=%d,height=%d,framerate=%d/%d\n",
fourcc2str(caps.fourcc, fourcc), caps.width, caps.height,
caps.fps_num, caps.fps_denom);
break;
}
return 0;
}
static int set_timeoutimage(const char *devicename, const char *imagefile,
int timeout, int verbose)
{
int err = 0;
int fd = -1;
char imagearg[4096], imagefile2[4096], devicearg[4096];
char *args[] = { "gst-launch-1.0",
"uridecodebin",
0,
"!",
"videoconvert",
"!",
"videoscale",
"!",
"imagefreeze",
"!",
"identity",
"eos-after=2",
"drop-allocation=1",
"!",
"v4l2sink",
"show-preroll-frame=false",
0,
0 };
if (verbose)
printf("set-timeout-image '%s' for '%s' with %dms timeout\n",
imagefile, devicename, timeout);
snprintf(imagearg, 4096, "uri=file://%s",
realpath(imagefile, imagefile2));
snprintf(devicearg, 4096, "device=%s", devicename);
imagearg[4095] = devicearg[4095] = 0;
args[2] = imagearg;
args[16] = devicearg;
fd = open_videodevice(devicename, O_RDWR);
if (fd >= 0) {
dprintf(2, "v4l2-ctl -d %s -c timeout_image_io=1\n",
devicename);
set_control_i(fd, "timeout_image_io", 1);
close(fd);
} else {
err = errno;
}
if (verbose > 1) {
char **ap = args;
while (*ap) {
dprintf(2, "%s", *ap);
if (*ap++)
dprintf(2, " ");
else
dprintf(2, "\n");
}
}
dprintf(2,
"v======================================================================v\n");
if (my_execv(args)) {
dprintf(2, "ERROR: setting time-out image failed\n");
err = 1;
}
dprintf(2,
"^======================================================================^\n");
fd = open_videodevice(devicename, O_RDWR);
if (fd >= 0) {
/* finally check the timeout */
if (timeout < 0) {
timeout = get_control_i(fd, "timeout");
} else {
dprintf(2, "v4l2-ctl -d %s -c timeout=%d\n", devicename,
timeout);
timeout = set_control_i(fd, "timeout", timeout);
}
if (timeout <= 0) {
dprintf(2,
"Timeout is currently disabled; you can set it to some positive value, e.g.:\n");
dprintf(2, " $ v4l2-ctl -d %s -c timeout=3000\n",
devicename);
}
close(fd);
} else {
err = errno;
}
return err;
}
static t_command get_command(const char *command)
{
if (!strncmp(command, "-h", 3))
return HELP;
if (!strncmp(command, "-?", 3))
return HELP;
if (!strncmp(command, "--help", 7))
return HELP;
if (!strncmp(command, "-v", 3))
return VERSION;
if (!strncmp(command, "--version", 10))
return VERSION;
if (!strncmp(command, "list", 5))
return LIST;
if (!strncmp(command, "add", 4))
return ADD;
if (!strncmp(command, "del", 3)) /* also allow delete */
return DELETE;
if (!strncmp(command, "query", 6))
return QUERY;
if (!strncmp(command, "set-fps", 8))
return SET_FPS;
if (!strncmp(command, "get-fps", 8))
return GET_FPS;
if (!strncmp(command, "set-caps", 9))
return SET_CAPS;
if (!strncmp(command, "get-caps", 9))
return GET_CAPS;
if (!strncmp(command, "set-timeout-image", 18))
return SET_TIMEOUTIMAGE;
if (!strncmp(command, "moo", 10))
return MOO;
return _UNKNOWN;
}
typedef int (*t_argcheck)(const char *);
static int called_deprecated(const char *device, const char *argument,
const char *programname, const char *cmdname,
const char *argname, t_argcheck argcheck)
{
/* check if <device> does not look like a device, but <argument> does
* if so, assume that the user swapped the two */
/* if the <device> looks about right, optionally do some extra
* <argument>-check, to see if it can be used
*/
int deviceswapped = 0;
int argswapped = 0;
if (argcheck)
argswapped =
((argcheck(argument) != 0) && (argcheck(device) == 0));
if (!argswapped)
deviceswapped = (parse_device(device) < 0 &&
parse_device(argument) >= 0);
if (argswapped || deviceswapped) {
dprintf(2, "WARNING: '%s %s <%s> <image>' is deprecated!\n",
programname, cmdname, argname);
dprintf(2, "WARNING: use '%s %s <device> <%s>' instead.\n",
programname, cmdname, argname);
return 1;
}
return 0;
}
static int do_defaultargs(const char *progname, t_command cmd, int argc,
char **argv)
{
static const char options_short[] = "?h";
static const struct option options_long[] = {
{ "help", no_argument, NULL, 'h' }, { 0, 0, 0, 0 }
};
for (;;) {
int c;
int idx;
c = getopt_long(argc - 1, argv + 1, options_short, options_long,
&idx);
if (-1 == c)
break;
switch (c) {
case 'h':
usage_topic(argv[0], cmd, argc - 1, argv + 1);
exit(0);
default:
usage_topic(argv[0], cmd, argc - 1, argv + 1);
exit(1);
}
}
return optind;
}
int main(int argc, char **argv)
{
const char *progname = argv[0];
int i;
int fd = -1;
int verbose = 0;
t_command cmd;
char *label = 0;
int min_width = -1;
int max_width = -1;
int min_height = -1;
int max_height = -1;
int exclusive_caps = -1;
int buffers = -1;
int openers = -1;
int escape_strings = 0;
int ret = 0;
static const char add_options_short[] = "?vn:w:h:x:b:o:";
static const struct option add_options_long[] = {
{ "help", no_argument, NULL, '?' },
{ "verbose", no_argument, NULL, 'v' },
{ "name", required_argument, NULL, 'n' },
{ "min-width", required_argument, NULL, 'w' + 0xFFFF },
{ "max-width", required_argument, NULL, 'w' },
{ "min-height", required_argument, NULL, 'h' + 0xFFFF },
{ "max-height", required_argument, NULL, 'h' },
{ "exclusive-caps", required_argument, NULL, 'x' },
{ "buffers", required_argument, NULL, 'b' },
{ "max-openers", required_argument, NULL, 'o' },
{ 0, 0, 0, 0 }
};
static const char list_options_short[] = "?he";
static const struct option list_options_long[] = {
{ "help", no_argument, NULL, 'h' },
{ "escape", no_argument, NULL, 'e' },
{ 0, 0, 0, 0 }
};
static const char timeoutimg_options_short[] = "?ht:v";
static const struct option timeoutimg_options_long[] = {
{ "help", no_argument, NULL, 'h' },
{ "timeout", required_argument, NULL, 't' },
{ "verbose", no_argument, NULL, 'v' },
{ 0, 0, 0, 0 }
};
if (argc < 2)
usage(progname);
cmd = get_command(argv[1]);
if (_UNKNOWN == cmd) {
dprintf(2, "unknown command '%s'\n\n", argv[1]);
usage(progname);
return 1;
}
argc--;
argv++;
switch (cmd) {
case HELP:
help(progname, 0);
break;
case LIST:
for (;;) {
int c;
int idx;
c = getopt_long(argc, argv, list_options_short,
list_options_long, &idx);
if (-1 == c)
break;
switch (c) {
case 'e':
escape_strings++;
break;
default:
usage_topic(progname, cmd, argc - 1, argv + 1);
return 1;
}
}
argc -= optind;
argv += optind;
if (argc) {
dprintf(2, "'list' does not take any arguments\n");
return 1;
}
fd = open_controldevice();
if (fd >= 0)
list_devices(fd, escape_strings);
else
return 1;
break;
case ADD:
for (;;) {
int c;
int idx;
c = getopt_long(argc, argv, add_options_short,
add_options_long, &idx);
if (-1 == c)
break;
switch (c) {
case 'v':
verbose++;
break;
case 'n':
label = optarg;
break;
case 'w' + 0xFFFF:
min_width = my_atoi("min_width", optarg);
break;
case 'h' + 0xFFFF:
min_height = my_atoi("min_height", optarg);
break;
case 'w':
max_width = my_atoi("max_width", optarg);
break;
case 'h':
max_height = my_atoi("max_height", optarg);
break;
case 'x':
exclusive_caps =
my_atoi("exclusive_caps", optarg);
break;
case 'b':
buffers = my_atoi("buffers", optarg);
break;
case 'o':
openers = my_atoi("openers", optarg);
break;
default:
usage_topic(progname, cmd, argc - 1, argv + 1);
return 1;
}
}
argc -= optind;
argv += optind;
fd = open_controldevice();
if (min_width > max_width && max_width > 0) {
dprintf(2,
"min_width (%d) must not be greater than max_width (%d)\n",
min_width, max_width);
return 1;
}
if (min_height > max_height && max_height > 0) {
dprintf(2,
"min_height (%d) must not be greater than max_height (%d)\n",
min_height, max_height);
return 1;
}
do {
struct v4l2_loopback_config cfg;
int capture_nr = -1, output_nr = -1;
switch (argc) {
case 0:
/* no device given: pick some */
break;
case 2:
/* two devices given: capture_device and output_device */
output_nr = parse_device(argv[0]);
capture_nr = parse_device(argv[1]);
#ifndef SPLIT_DEVICES
if (capture_nr != output_nr)
dprintf(2,
"split output/capture devices currently not supported...ignoring capture device\n");
capture_nr = output_nr;
#endif
break;
case 1:
/* single device given: use it for both input and output */
capture_nr = output_nr = parse_device(argv[0]);
break;
default:
usage_topic(progname, cmd, argc, argv);
return 1;
}
ret = add_device(fd,
make_conf(&cfg, label, min_width,
max_width, min_height,
max_height, exclusive_caps,
buffers, openers, capture_nr,
output_nr),
verbose);
} while (0);
break;
case DELETE:
optind = do_defaultargs(progname, cmd, argc, argv);
argc -= optind;
argv += optind;
if (!argc)
usage_topic(progname, cmd, argc, argv);
fd = open_controldevice();
for (i = 0; i < argc; i++) {
int err = delete_device(fd, argv[i]);
if (err)
ret = err;
}
break;
case QUERY:
for (;;) {
int c;
int idx;
c = getopt_long(argc, argv, list_options_short,
list_options_long, &idx);
if (-1 == c)
break;
switch (c) {
case 'e':
escape_strings++;
break;
default:
usage_topic(progname, cmd, argc - 1, argv + 1);
return 1;
}
}
argc -= optind;
argv += optind;
if (!argc)
usage_topic(progname, cmd, argc, argv);
fd = open_controldevice();
for (i = 0; i < argc; i++) {
ret += query_device(fd, argv[i], escape_strings);
}
ret = (ret > 0);
break;
case SET_FPS:
optind = do_defaultargs(progname, cmd, argc, argv);
argc -= optind;
argv += optind;
if (argc != 2)
usage_topic(progname, cmd, argc, argv);
if (called_deprecated(argv[0], argv[1], progname, "set-fps",
"fps", is_fps)) {
ret = set_fps(argv[1], argv[0]);
} else
ret = set_fps(argv[0], argv[1]);
break;
case GET_FPS:
optind = do_defaultargs(progname, cmd, argc, argv);
argc -= optind;
argv += optind;
if (argc != 1)
usage_topic(progname, cmd, argc, argv);
ret = get_fps(argv[0]);
break;
case SET_CAPS:
optind = do_defaultargs(progname, cmd, argc, argv);
argc -= optind;
argv += optind;
if (argc != 2)
usage_topic(progname, cmd, argc, argv);
if (called_deprecated(argv[0], argv[1], progname, "set-caps",
"caps", 0)) {
ret = set_caps(argv[1], argv[0]);
} else {
ret = set_caps(argv[0], argv[1]);
}
break;
case GET_CAPS:
optind = do_defaultargs(progname, cmd, argc, argv);
argc -= optind;
argv += optind;
if (argc != 1)
usage_topic(progname, cmd, argc, argv);
ret = get_caps(argv[0]);
break;
case SET_TIMEOUTIMAGE:
if ((3 == argc) && (strncmp("-t", argv[1], 3)) &&
(strncmp("--timeout", argv[1], 10)) &&
(called_deprecated(argv[1], argv[2], progname,
"set-timeout-image", "image", 0))) {
ret = set_timeoutimage(argv[2], argv[1], -1, verbose);
} else {
int timeout = -1;
for (;;) {
int c, idx;
c = getopt_long(argc, argv,
timeoutimg_options_short,
timeoutimg_options_long, &idx);
if (-1 == c)
break;
switch (c) {
case 't':
timeout = my_atoi("timeout", optarg);
break;
case 'v':
verbose++;
break;
default:
usage_topic(progname, cmd, argc, argv);
}
}
argc -= optind;
argv += optind;
if (argc != 2)
usage_topic(progname, cmd, argc, argv);
ret = set_timeoutimage(argv[0], argv[1], timeout,
verbose);
}
break;
case VERSION:
#ifdef SNAPSHOT_VERSION
printf("%s v%s\n", progname, SNAPSHOT_VERSION);
#else
printf("%s v%d.%d.%d\n", progname, V4L2LOOPBACK_VERSION_MAJOR,
V4L2LOOPBACK_VERSION_MINOR, V4L2LOOPBACK_VERSION_BUGFIX);
#endif
fd = open("/sys/module/v4l2loopback/version", O_RDONLY);
if (fd >= 0) {
char buf[1024];
int len = read(fd, buf, sizeof(buf));
if (len > 0) {
if (len < sizeof(buf))
buf[len] = 0;
printf("v4l2loopback sysfs v%s", buf);
}
close(fd);
}
fd = open_controldevice();
if (fd >= 0) {
__u32 version = 0;
if (ioctl(fd, V4L2LOOPBACK_CTL_VERSION, &version) ==
0) {
printf("v4l2loopback module v%d.%d.%d\n",
(version >> 16) & 0xff,
(version >> 8) & 0xff,
(version >> 0) & 0xff);
}
}
break;
default:
dprintf(2, "not implemented: '%s'\n", argv[0]);
break;
}
if (fd >= 0)
close(fd);
return ret;
}
|