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
|
////////////////////////////////////////////////////////
//
// GEM - Graphics Environment for Multimedia
//
// zmoelnig@iem.at
//
// Implementation file
//
// Copyright (c) 1997-1998 Mark Danks.
// Copyright (c) Günther Geiger.
// Copyright (c) 2001-2011 IOhannes m zmölnig. forum::für::umläute. IEM. zmoelnig@iem.at
// For information on usage and redistribution, and for a DISCLAIMER OF ALL
// WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution.
//
/////////////////////////////////////////////////////////
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "videoV4L2.h"
#include "plugins/PluginFactory.h"
using namespace gem::plugins;
#include "Gem/RTE.h"
#include "Gem/Files.h"
#ifndef HAVE_LIBV4L2
# define v4l2_open ::open
# define v4l2_close ::close
# define v4l2_dup ::dup
# define v4l2_ioctl ::ioctl
# define v4l2_read ::read
# define v4l2_mmap ::mmap
# define v4l2_munmap ::munmap
#endif /* libv4l-2 */
/* debugging helpers */
#undef debugPost
#undef debugThread
#undef debugIOCTL
#if 0
# define debugPost ::startpost("%s:%s[%d]", __FILE__, __FUNCTION__, __LINE__); ::post
#else
# include "Utils/nop.h"
# define debugPost nop_post
#endif
#if 0
# define debugThread ::startpost("%s:%s[%d]", __FILE__, __FUNCTION__, __LINE__); ::post
#else
# include "Utils/nop.h"
# define debugThread nop_post
#endif
#if 0
# define debugIOCTL ::post
#else
# include "Utils/nop.h"
# define debugIOCTL nop_post
#endif
#define WIDTH_FLAG 1
#define HEIGHT_FLAG 2
/////////////////////////////////////////////////////////
//
// videoV4L2
//
/////////////////////////////////////////////////////////
// Constructor
//
/////////////////////////////////////////////////////////
#ifdef HAVE_VIDEO4LINUX2
#include <sys/stat.h>
#include <string.h>
REGISTER_VIDEOFACTORY("v4l2", videoV4L2);
videoV4L2 :: videoV4L2() : videoBase("v4l2", 0)
, m_gotFormat(0), m_colorConvert(0),
m_tvfd(0),
m_buffers(NULL), m_nbuffers(0),
m_currentBuffer(NULL),
m_frame(0), m_last_frame(0),
m_maxwidth(844), m_minwidth(32),
m_maxheight(650), m_minheight(32),
m_thread_id(0), m_continue_thread(false), m_frame_ready(false),
m_rendering(false),
m_stopTransfer(false),
m_frameSize(0)
{
memset(&m_caps, 0, sizeof(m_caps));
if (!m_width) {
m_width=320;
}
if (!m_height) {
m_height=240;
}
m_capturing=false;
m_devicenum=V4L2_DEVICENO;
provide("analog");
}
////////////////////////////////////////////////////////
// Destructor
//
////////////////////////////////////////////////////////
videoV4L2 :: ~videoV4L2()
{
close();
}
static int xioctl(int fd,
int request,
void * arg)
{
int r;
const unsigned char req=(request&0xFF);
debugIOCTL("V4L2: xioctl %d\n", req);
do {
r = v4l2_ioctl (fd, request, arg);
debugIOCTL("V4L2: xioctl %d->%d\n", r, errno);
} while (-1 == r && EINTR == errno);
debugIOCTL("V4L2: xioctl done %d\n", r);
#if 0
if(r!=0) {
char buf[13];
snprintf(buf, 13, "xioctl[%03d]:", req);
buf[12]=0;
perror(buf);
}
#endif
return r;
}
static int reqbufs(int fd, unsigned int numbufs)
{
struct v4l2_requestbuffers req;
memset (&(req), 0, sizeof (req));
req.count = numbufs;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
if (-1 == xioctl (fd, VIDIOC_REQBUFS, &req)) {
return -1;
}
return req.count;
}
int videoV4L2::init_mmap (void)
{
const char *devname=(m_devicename.empty())?"device":m_devicename.c_str();
int count = reqbufs(m_tvfd, V4L2_NBUF);
if(count<0) {
if (EINVAL == errno) {
error("[GEM:videoV4L2] %s does not support memory mapping", devname);
return 0;
} else {
perror("[GEM:videoV4L2] VIDIOC_REQBUFS");
return 0;
}
}
if (count < V4L2_NBUF) {
//error("[GEM:videoV4L2] Insufficient buffer memory on %s: %d", devname, count);
//return(0);
}
m_buffers = (t_v4l2_buffer*)calloc (count, sizeof (*m_buffers));
if (!m_buffers) {
perror("[GEM:videoV4L2] out of memory");
return(0);
}
for (m_nbuffers = 0; m_nbuffers < count; ++m_nbuffers) {
struct v4l2_buffer buf;
memset (&(buf), 0, sizeof (buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = m_nbuffers;
debugPost("v4l2: buf.index==%d", buf.index);
if (-1 == xioctl (m_tvfd, VIDIOC_QUERYBUF, &buf)) {
perror("[GEM:videoV4L2] VIDIOC_QUERYBUF");
return(0);
}
m_buffers[m_nbuffers].length = buf.length;
m_buffers[m_nbuffers].start =
v4l2_mmap (NULL /* start anywhere */,
buf.length,
PROT_READ | PROT_WRITE /* required */,
MAP_SHARED /* recommended */,
m_tvfd, buf.m.offset);
if (MAP_FAILED == m_buffers[m_nbuffers].start) {
perror("[GEM:videoV4L2] mmap");
return 0;
}
}
return 1;
}
/////////////////////////////////////////////////////////
// this is the work-horse
// a thread that does the capturing
//
/////////////////////////////////////////////////////////
void *videoV4L2::capturing_(void*you)
{
videoV4L2 *me=(videoV4L2 *)you;
return me->capturing();
}
void *videoV4L2 :: capturing(void)
{
int errorcount=0;
t_v4l2_buffer*buffers=m_buffers;
void *currentBuffer=NULL;
const __u32 expectedSize=m_frameSize;
__u32 gotSize=0;
struct v4l2_buffer buf;
int nbuf=m_nbuffers;
fd_set fds;
struct timeval tv;
m_capturing=true;
debugThread("V4L2: memset");
memset(&(buf), 0, sizeof (buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
while(m_continue_thread) {
bool captureerror=false;
FD_ZERO (&fds);
FD_SET (m_tvfd, &fds);
debugThread("V4L2: grab");
m_frame++;
m_frame%=nbuf;
/* Timeout. */
tv.tv_sec = 0;
tv.tv_usec = 100;
int r = select(0,0,0,0,&tv);
debugThread("V4L2: waited...");
if (-1 == r) {
if (EINTR == errno) {
continue;
}
perror("[GEM:videoV4L2] select");//exit
}
memset(&(buf), 0, sizeof (buf));
debugThread("V4L2: memset...");
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
if (-1 == xioctl (m_tvfd, VIDIOC_DQBUF, &buf)) {
switch (errno) {
/* coverity[unterminated_case] */
case EAGAIN:
perror("[GEM:videoV4L2] VIDIOC_DQBUF: stopping capture thread!");
m_stopTransfer=true;
m_continue_thread=false;
/* coverity[unterminated_case] */
case EIO:
/* Could ignore EIO, see spec. */
/* fall through */
default:
captureerror=true;
perror("[GEM:videoV4L2] VIDIOC_DQBUF");
}
}
debugThread("V4L2: grabbed %d", buf.index);
gotSize=buf.bytesused;
currentBuffer=buffers[buf.index].start;
//process_image (m_buffers[buf.index].start);
if (-1 == xioctl (m_tvfd, VIDIOC_QBUF, &buf)) {
perror("[GEM:videoV4L2] VIDIOC_QBUF");
captureerror=true;
}
debugThread("V4L2: dequeueued");
if(expectedSize<=gotSize) {
m_frame_ready = 1;
m_last_frame=m_frame;
m_currentBuffer=currentBuffer;
} else {
fprintf(stderr,
"[GEM:videoV4L2] oops, skipping incomplete capture %d of %d bytes\n",
gotSize, expectedSize);
}
if(captureerror) {
errorcount++;
if(errorcount>1000) {
error("[GEM:videoV4L2] %d capture errors in a row... I think I better stop now...",
errorcount);
m_continue_thread=false;
m_stopTransfer=true;
}
} else {
errorcount=0;
}
}
// stop capturing
m_capturing=false;
debugThread("V4L2: thread finished");
return NULL;
}
//////////////////
// this reads the data that was captured by capturing() and returns it within a pixBlock
pixBlock *videoV4L2 :: getFrame()
{
if(!(m_haveVideo && m_capturing)) {
return NULL;
}
if(m_stopTransfer) {
bool rendering=m_rendering;
stopTransfer();
m_rendering=rendering;
return NULL;
}
//debugPost("v4l2: getting frame %d", m_frame_ready);
m_image.newfilm=0;
if (!m_frame_ready) {
m_image.newimage = 0;
} else {
unsigned char*data=(unsigned char*)m_currentBuffer;
if (m_colorConvert) {
m_image.image.notowned = false;
switch(m_gotFormat) {
#if 1
# define CONVERT(type) m_image.image.from##type (data)
#else
# define CONVERT(type) post("from " #type "!");m_image.image.from##type (data)
#endif
case V4L2_PIX_FMT_RGB24:
CONVERT(RGB );
break;
case V4L2_PIX_FMT_BGR32:
CONVERT(BGRA );
break;
case V4L2_PIX_FMT_RGB32:
CONVERT(ARGB );
break;
case V4L2_PIX_FMT_GREY :
CONVERT(Gray );
break;
case V4L2_PIX_FMT_UYVY :
CONVERT(YUV422);
break;
case V4L2_PIX_FMT_YUYV :
CONVERT(YUY2 );
break;
case V4L2_PIX_FMT_YUV420:
CONVERT(YU12 );
break;
default: // ? what should we do ?
m_image.image.data=data;
m_image.image.notowned = true;
}
} else {
m_image.image.data=data;
m_image.image.notowned = true;
}
m_image.image.upsidedown=true;
m_image.newimage = 1;
m_frame_ready = false;
}
return &m_image;
}
bool videoV4L2 :: openDevice(gem::Properties&props)
{
close();
/* check the device */
// if we don't have a devicename, create one
std::string devname = m_devicename;
if(devname.empty()) {
devname="/dev/video";
if(m_devicenum>=0) {
char buf[256];
snprintf(buf, 255, "%d", m_devicenum);
buf[255]=0;
devname+=buf;
}
}
if (devname.at(0) !=
'/') { // assuming all v4l2 device's paths starts with '/'
std::vector<std::string> alldev = enumerate();
int i;
for(i=0; i<alldev.size(); i++) {
std::string dev=alldev[i];
verbose(1, "[GEM:videoV4L2] found possible device %s", dev.c_str());
int fd=v4l2_open(dev.c_str(), O_RDWR);
verbose(1, "[GEM:videoV4L2] v4l2_open returned %d", fd);
if(fd<0) {
continue;
}
struct v4l2_capability cap;
if (-1 != xioctl (fd, VIDIOC_QUERYCAP, &cap)) {
std::string bus;
bus = (char*) cap.bus_info;
if ( devname == bus ) {
devname = dev;
break;
}
} else {
verbose(1, "[GEM:videoV4L2] %s is no v4l2 device\n", dev.c_str());
}
v4l2_close(fd);
}
if ( i >= alldev.size() ) {
verbose(0, "[GEM:videoV4L2] no v4l2 input device on bus %s\n",
devname.c_str());
devname = "";
}
}
const char*dev_name=devname.c_str();
debugPost("v4l2: device: %s", dev_name);
// try to open the device
m_tvfd = v4l2_open (dev_name, O_RDWR /* required */, 0);
if (-1 == m_tvfd) {
verbose(0, "[GEM:videoV4L2] Cannot open '%s': %d, %s", dev_name, errno,
strerror (errno));
closeDevice();
return false;
}
struct stat st;
if (-1 == fstat (m_tvfd, &st)) {
verbose(0, "[GEM:videoV4L2] Cannot identify '%s': %d, %s", dev_name, errno,
strerror (errno));
closeDevice();
return false;
}
if (!S_ISCHR (st.st_mode)) {
verbose(0, "[GEM:videoV4L2] %s is no device", dev_name);
closeDevice();
return false;
}
// by now, we have an open file-descriptor
// check whether this is really a v4l2-device
struct v4l2_capability cap;
if (-1 == xioctl (m_tvfd, VIDIOC_QUERYCAP, &cap)) {
if (EINVAL == errno) {
verbose(0, "[GEM:videoV4L2] %s is no V4L2 device", dev_name);
closeDevice();
return false;
} else {
perror("[GEM:videoV4L2] VIDIOC_QUERYCAP");//exit
closeDevice();
return false;
}
}
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
verbose(0, "[GEM:videoV4L2] %s is no video capture device", dev_name);
closeDevice();
return false;
}
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
verbose(0, "[GEM:videoV4L2] %s does not support streaming i/o", dev_name);
closeDevice();
return false;
}
verbose(1, "[GEM:videoV4L2] successfully opened %s", dev_name);
setProperties(props);
return true;
}
void videoV4L2 :: closeDevice()
{
verbose(1, "[GEM:videoV4L2] closing device %d", m_tvfd);
if (m_tvfd>=0) {
v4l2_close(m_tvfd);
}
m_tvfd=-1;
}
/////////////////////////////////////////////////////////
// restartTransfer
//
/////////////////////////////////////////////////////////
bool videoV4L2 :: restartTransfer()
{
bool rendering=m_rendering;
debugPost("v4l2: restart transfer");
if(m_capturing) {
stopTransfer();
}
debugPost("v4l2: restart stopped");
if (rendering) {
startTransfer();
}
debugPost("v4l2: restart started");
return true;
}
/////////////////////////////////////////////////////////
// startTransfer
//
/////////////////////////////////////////////////////////
bool videoV4L2 :: startTransfer()
{
if(m_tvfd<0) {
return false;
}
debugPost("v4l2: startTransfer: %d", m_capturing);
if(m_capturing) {
stopTransfer(); // just in case we are already running!
}
debugPost("v4l2: start transfer");
m_stopTransfer=false;
m_rendering=true;
verbose(1, "[GEM:videoV4L2] starting transfer");
int i;
__u32 pixelformat=0;
struct v4l2_format fmt;
enum v4l2_buf_type type;
m_frame = 0;
m_last_frame = 0;
/* Select video format */
memset (&(fmt), 0, sizeof (fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
/* initialize the format struct to what we currently have */
if (-1 == xioctl (m_tvfd, VIDIOC_G_FMT, &fmt)) {
perror("[GEM:videoV4L2] VIDIOC_G_FMT");//exit
}
switch(m_reqFormat) {
case GEM_YUV:
pixelformat = V4L2_PIX_FMT_UYVY;
break;
case GEM_GRAY:
pixelformat = V4L2_PIX_FMT_GREY;
break;
case GEM_RGB:
pixelformat = V4L2_PIX_FMT_RGB24;
break;
default:
pixelformat = V4L2_PIX_FMT_RGB32;
m_reqFormat=GEM_RGBA;
break;
}
if(fmt.fmt.pix.pixelformat != pixelformat) {
fmt.fmt.pix.pixelformat = pixelformat;
verbose(1, "[GEM:videoV4L2] want 0x%X == '%c%c%c%c' ", m_reqFormat,
(char)(fmt.fmt.pix.pixelformat),
(char)(fmt.fmt.pix.pixelformat>>8),
(char)(fmt.fmt.pix.pixelformat>>16),
(char)(fmt.fmt.pix.pixelformat>>24));
if (-1 == xioctl (m_tvfd, VIDIOC_S_FMT, &fmt)) {
perror("[GEM:videoV4L2] VIDIOC_S_FMT(fmt)");//exit
}
// query back what we have set
/* in theory this should not be needed,
* as S_FMT is supposed to return the actual data
* however, some buggy drivers seem to not do that,
* so we have to make sure...
*/
if (-1 == xioctl (m_tvfd, VIDIOC_G_FMT, &fmt)) {
perror("[GEM:videoV4L2] VIDIOC_G_FMT");//exit
}
}
m_gotFormat=fmt.fmt.pix.pixelformat;
switch(m_gotFormat) {
case V4L2_PIX_FMT_RGB32:
debugPost("v4l2: ARGB");
break;
case V4L2_PIX_FMT_RGB24:
debugPost("v4l2: RGB");
break;
case V4L2_PIX_FMT_UYVY:
debugPost("v4l2: YUV ");
break;
case V4L2_PIX_FMT_GREY:
debugPost("v4l2: gray");
break;
case V4L2_PIX_FMT_YUV420:
debugPost("v4l2: YUV 4:2:0");
break;
default:
/* hmm, we don't know how to handle this
* let's try formats that should be always supported by libv4l2
*/
switch(m_reqFormat) {
case GEM_YUV:
case GEM_GRAY:
pixelformat = V4L2_PIX_FMT_YUV420;
break;
default:
pixelformat = V4L2_PIX_FMT_RGB24;
break;
}
fmt.fmt.pix.pixelformat = pixelformat;
if (-1 == xioctl (m_tvfd, VIDIOC_S_FMT, &fmt)) {
perror("[GEM:videoV4L2] VIDIOC_S_FMT(fmt2)");
}
// query back what we have set
if (-1 == xioctl (m_tvfd, VIDIOC_G_FMT, &fmt)) {
perror("[GEM:videoV4L2] VIDIOC_G_FMT(fmt2)");
}
m_gotFormat=fmt.fmt.pix.pixelformat;
}
switch(m_gotFormat) {
case V4L2_PIX_FMT_RGB32:
case V4L2_PIX_FMT_RGB24:
case V4L2_PIX_FMT_UYVY:
case V4L2_PIX_FMT_YUV420:
case V4L2_PIX_FMT_YUYV:
case V4L2_PIX_FMT_GREY:
break;
default:
error("[GEM:videoV4L2] unknown format '%c%c%c%c'",
(char)(m_gotFormat),
(char)(m_gotFormat>>8),
(char)(m_gotFormat>>16),
(char)(m_gotFormat>>24));
/* we should really return here! */
}
verbose(1, "[GEM:videoV4L2] got '%c%c%c%c'",
(char)(m_gotFormat),
(char)(m_gotFormat>>8),
(char)(m_gotFormat>>16),
(char)(m_gotFormat>>24));
if(!init_mmap ()) {
goto closit;
}
for (i = 0; i < m_nbuffers; ++i) {
struct v4l2_buffer buf;
memset (&(buf), 0, sizeof (buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
if (-1 == xioctl (m_tvfd, VIDIOC_QBUF, &buf)) {
perror("[GEM:videoV4L2] VIDIOC_QBUF");//exit
}
}
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl (m_tvfd, VIDIOC_STREAMON, &type)) {
perror("[GEM:videoV4L2] VIDIOC_STREAMON");//exit
}
m_frameSize=fmt.fmt.pix.sizeimage;
/* fill in image specifics for Gem pixel object. Could we have
just used RGB, I wonder? */
m_image.image.xsize = fmt.fmt.pix.width;
m_image.image.ysize = fmt.fmt.pix.height;
m_image.image.setCsizeByFormat(m_reqFormat);
m_image.image.reallocate();
debugPost("v4l2: format: %c%c%c%c -> 0x%X",
(char)(m_gotFormat),
(char)(m_gotFormat>>8),
(char)(m_gotFormat>>16),
(char)(m_gotFormat>>24),
m_reqFormat);
switch(m_gotFormat) {
case V4L2_PIX_FMT_GREY :
m_colorConvert=(m_reqFormat!=GEM_GRAY);
break;
case V4L2_PIX_FMT_RGB24 :
m_colorConvert=true;
break;
case V4L2_PIX_FMT_RGB32 :
m_colorConvert=true;
break;
case V4L2_PIX_FMT_UYVY :
m_colorConvert=(m_reqFormat!=GEM_YUV);
break;
case V4L2_PIX_FMT_YUV420:
m_colorConvert=1;
break;
default:
m_colorConvert=true;
}
debugPost("v4l2: colorconvert=%d", m_colorConvert);
/* create thread */
m_continue_thread = 1;
m_frame_ready = 0;
pthread_create(&m_thread_id, 0, capturing_, this);
while(!m_capturing) {
usleep(10);
debugPost("v4l2: waiting for thread to come up");
}
verbose(1, "[GEM:videoV4L2] Opened video connection 0x%X", m_tvfd);
return(1);
closit:
debugPost("v4l2: closing it!");
stopTransfer();
debugPost("v4l2: closed it");
return(0);
}
/////////////////////////////////////////////////////////
// stopTransfer
//
/////////////////////////////////////////////////////////
bool videoV4L2 :: stopTransfer()
{
debugPost("v4l2: stoptransfer");
if(!m_capturing) {
return false;
}
/* close the v4l2 device and dealloc buffer */
/* terminate thread if there is one */
if(m_continue_thread) {
void *dummy;
m_continue_thread = 0;
pthread_join (m_thread_id, &dummy);
}
while(m_capturing) {
usleep(10);
debugPost("v4l2: waiting for thread to finish");
}
// unmap the mmap
debugPost("v4l2: unmapping %d buffers: %x", m_nbuffers, m_buffers);
if(m_buffers) {
for (int i = 0; i < m_nbuffers; ++i)
if (-1 == v4l2_munmap (m_buffers[i].start, m_buffers[i].length)) {
// oops: couldn't unmap the memory
}
debugPost("v4l2: freeing buffers: %x", m_buffers);
free (m_buffers);
}
m_buffers=NULL;
debugPost("v4l2: freed");
// stop streaming
if(m_tvfd) {
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl (m_tvfd, VIDIOC_STREAMOFF, &type)) {
perror("[GEM:videoV4L2] VIDIOC_STREAMOFF");
}
}
debugPost("v4l2: de-requesting buffers");
reqbufs(m_tvfd, 0);
m_frame_ready = 0;
m_rendering=false;
debugPost("v4l2: stoppedTransfer");
return true;
}
bool videoV4L2 :: setColor(int format)
{
if (format<=0 || format==m_reqFormat) {
return -1;
}
m_reqFormat=format;
restartTransfer();
return true;
}
std::vector<std::string> videoV4L2::enumerate()
{
std::vector<std::string> result;
std::vector<std::string> glob, allglob;
glob=gem::files::getFilenameListing("/dev/video*");
for(int i=0; i<glob.size(); i++) {
allglob.push_back(glob[i]);
}
glob=gem::files::getFilenameListing("/dev/v4l/*");
for(int i=0; i<glob.size(); i++) {
allglob.push_back(glob[i]);
}
for(int i=0; i<allglob.size(); i++) {
std::string dev=allglob[i];
verbose(1, "[GEM:videoV4L2] found possible device %s", dev.c_str());
int fd=v4l2_open(dev.c_str(), O_RDWR);
verbose(1, "[GEM:videoV4L2] v4l2_open returned %d", fd);
if(fd<0) {
continue;
}
struct v4l2_capability cap;
memset (&cap, 0, sizeof (cap));
if (-1 != xioctl (fd, VIDIOC_QUERYCAP, &cap)) {
if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) {
result.push_back(dev);
} else {
verbose(1, "[GEM:videoV4L2] %s is v4l2 but cannot capture", dev.c_str());
}
} else {
verbose(1, "[GEM:videoV4L2] %s is no v4l2 device", dev.c_str());
}
v4l2_close(fd);
}
return result;
}
void videoV4L2::addProperties(struct v4l2_queryctrl queryctrl,
gem::Properties&readable,
gem::Properties&writeable)
{
const char* name=NULL;
gem::any typ;
if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED) {
return;
}
switch(queryctrl.type) {
case V4L2_CTRL_TYPE_BUTTON:
break;
case V4L2_CTRL_TYPE_BOOLEAN:
typ=1;
break;
case V4L2_CTRL_TYPE_MENU:
typ=queryctrl.maximum;
break;
case V4L2_CTRL_TYPE_INTEGER:
typ=queryctrl.maximum;
break;
case V4L2_CTRL_TYPE_INTEGER64:
typ=0;
break;
default:
return;
}
name=(const char*)(queryctrl.name);
m_readprops[name]=queryctrl;
readable.set(name, typ);
if (!(queryctrl.flags & V4L2_CTRL_FLAG_READ_ONLY)) {
m_writeprops[name]=queryctrl;
writeable.set(name, typ);
}
}
bool videoV4L2 :: enumProperties(gem::Properties&readable,
gem::Properties&writeable)
{
struct v4l2_queryctrl queryctrl;
__u32 id=0;
std::string dummy_s;
if(m_tvfd<0) {
return false;
}
readable.clear();
writeable.clear();
m_readprops.clear();
m_writeprops.clear();
memset (&queryctrl, 0, sizeof (queryctrl));
for (id = V4L2_CID_BASE;
id < V4L2_CID_LASTP1;
id++) {
queryctrl.id = id;
if (0 == xioctl (m_tvfd, VIDIOC_QUERYCTRL, &queryctrl)) {
addProperties(queryctrl, readable, writeable);
} else {
if (errno == EINVAL) {
continue;
}
}
}
for (id = V4L2_CID_PRIVATE_BASE;;
id++) {
queryctrl.id = id;
if (0 == xioctl (m_tvfd, VIDIOC_QUERYCTRL, &queryctrl)) {
addProperties(queryctrl, readable, writeable);
} else {
if (errno == EINVAL) {
break;
}
}
}
readable.set("channel",0);
readable.set("frequency",0);
readable.set("norm", dummy_s);
readable.set("width",0);
readable.set("height",0);
writeable.set("channel",0);
writeable.set("frequency",0);
writeable.set("norm", dummy_s);
writeable.set("width",0);
writeable.set("height",0);
if (-1 != xioctl (m_tvfd, VIDIOC_QUERYCAP, &m_caps)) {
readable.set("driver", (char*) m_caps.driver);
readable.set("card", (char*) m_caps.card);
readable.set("bus_info", (char*) m_caps.bus_info);
}
return true;
}
void videoV4L2 :: getProperties(gem::Properties&props)
{
int getformat=0;
if(m_tvfd<0) {
props.clear();
return;
}
std::vector<std::string>keys=props.keys();
for(int i=0; i<keys.size(); i++) {
std::string key=keys[i];
std::map<std::string, struct v4l2_queryctrl>::iterator it =
m_readprops.find(key);
if(it != m_readprops.end()) {
struct v4l2_queryctrl qc=it->second;
struct v4l2_control vc;
memset (&vc, 0, sizeof (vc));
vc.id=qc.id;
vc.value=0;
int err=xioctl(m_tvfd, VIDIOC_G_CTRL, &vc);
if(0==err) {
props.set(key, vc.value);
} else {
props.erase(key);
}
} else {
if("norm" == key) {
v4l2_std_id stdid = 0;
if(0==xioctl(m_tvfd, VIDIOC_G_STD, &stdid)) {
std::string std;
switch(stdid) {
default:
case V4L2_STD_UNKNOWN:
std="UNKNOWN";
break;
case V4L2_STD_ALL:
std="ALL";
break;
case V4L2_STD_ATSC:
std="ATSC";
break;
case V4L2_STD_625_50:
std="625_50";
break;
case V4L2_STD_525_60:
std="525_60";
break;
case V4L2_STD_SECAM:
std="SECAM";
break;
case V4L2_STD_SECAM_DK:
std="SECAM_DK";
break;
case V4L2_STD_NTSC:
std="NTSC";
break;
case V4L2_STD_PAL:
std="PAL";
break;
case V4L2_STD_PAL_DK:
std="PAL_DK";
break;
case V4L2_STD_PAL_BG:
std="PAL_BG";
break;
case V4L2_STD_DK:
std="DK";
break;
case V4L2_STD_GH:
std="GH";
break;
case V4L2_STD_B:
std="B";
break;
case V4L2_STD_MN:
std="MN";
break;
case V4L2_STD_ATSC_16_VSB:
std="ATSC_16_VSB";
break;
case V4L2_STD_ATSC_8_VSB:
std="ATSC_8_VSB";
break;
case V4L2_STD_SECAM_LC:
std="SECAM_LC";
break;
case V4L2_STD_SECAM_L:
std="SECAM_L";
break;
case V4L2_STD_SECAM_K1:
std="SECAM_K1";
break;
case V4L2_STD_SECAM_K:
std="SECAM_K";
break;
case V4L2_STD_SECAM_H:
std="SECAM_H";
break;
case V4L2_STD_SECAM_G:
std="SECAM_G";
break;
case V4L2_STD_SECAM_D:
std="SECAM_D";
break;
case V4L2_STD_SECAM_B:
std="SECAM_B";
break;
case V4L2_STD_NTSC_M_KR:
std="NTSC_M_KR";
break;
case V4L2_STD_NTSC_443:
std="NTSC_443";
break;
case V4L2_STD_NTSC_M_JP:
std="NTSC_M_JP";
break;
case V4L2_STD_NTSC_M:
std="NTSC_M";
break;
case V4L2_STD_PAL_60:
std="PAL_60";
break;
case V4L2_STD_PAL_Nc:
std="PAL_Nc";
break;
case V4L2_STD_PAL_N:
std="PAL_N";
break;
case V4L2_STD_PAL_M:
std="PAL_M";
break;
case V4L2_STD_PAL_K:
std="PAL_K";
break;
case V4L2_STD_PAL_D1:
std="PAL_D1";
break;
case V4L2_STD_PAL_D:
std="PAL_D";
break;
case V4L2_STD_PAL_I:
std="PAL_I";
break;
case V4L2_STD_PAL_H:
std="PAL_H";
break;
case V4L2_STD_PAL_G:
std="PAL_G";
break;
case V4L2_STD_PAL_B1:
std="PAL_B1";
break;
case V4L2_STD_PAL_B:
std="PAL_B";
break;
}
props.set("norm", std);
}
} else if("channel" == key) {
int channel=0;
if(0==xioctl(m_tvfd, VIDIOC_G_INPUT, &channel)) {
props.set("channel", channel);
}
} else if("frequency" == key) {
struct v4l2_frequency freq;
memset (&(freq), 0, sizeof (freq));
freq.tuner=0; /* FIXXME: this should be a bit more intelligent... */
if(0==xioctl(m_tvfd, VIDIOC_G_FREQUENCY, &freq)) {
props.set("frequency", freq.frequency);
}
} else if("width" == key) {
getformat|=WIDTH_FLAG;
} else if("height" == key) {
getformat|=HEIGHT_FLAG;
} else if("driver" == key) {
props.set("driver", (char*) m_caps.driver);
} else if("card" == key) {
props.set("card", (char*) m_caps.card);
} else if("bus_info" == key) {
props.set("bus_info", (char*) m_caps.bus_info);
} else {
}
}
}
if(getformat) {
struct v4l2_format fmt;
memset (&(fmt), 0, sizeof (fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (0 == xioctl (m_tvfd, VIDIOC_G_FMT, &fmt)) {
if(getformat & WIDTH_FLAG) {
props.set("width", fmt.fmt.pix.width);
}
if(getformat & HEIGHT_FLAG) {
props.set("height", fmt.fmt.pix.height);
}
} else {
perror("[GEM:videoV4L2] VIDIOC_G_FMT");
}
}
}
void videoV4L2 :: setProperties(gem::Properties&props)
{
if(m_tvfd<0) {
return;
}
bool restart=false;
bool setformat=false, setcropping=false;
std::vector<std::string>keys=props.keys();
for(int i=0; i<keys.size(); i++) {
std::string key=keys[i];
std::map<std::string, struct v4l2_queryctrl>::iterator it =
m_writeprops.find(key);
if(it != m_writeprops.end()) {
double d=0;
struct v4l2_queryctrl qc=it->second;
struct v4l2_control vc;
memset (&vc, 0, sizeof (vc));
vc.id=qc.id;
if(props.get(key, d)) {
vc.value=d;
}
int err=xioctl(m_tvfd, VIDIOC_S_CTRL, &vc);
if(V4L2_CTRL_TYPE_BUTTON == qc.type) {
props.erase(key);
}
} else {
if("norm" == key) {
v4l2_std_id stdid = V4L2_STD_UNKNOWN;
std::string std;
switch(props.type(key)) {
case gem::Properties::STRING:
if(props.get(key, std)) {
if("ALL" == std) {
stdid=V4L2_STD_ALL;
} else if("PAL_B" == std) {
stdid=V4L2_STD_PAL_B;
} else if("PAL_B1" == std) {
stdid=V4L2_STD_PAL_B1;
} else if("PAL_G" == std) {
stdid=V4L2_STD_PAL_G;
} else if("PAL_H" == std) {
stdid=V4L2_STD_PAL_H;
} else if("PAL_I" == std) {
stdid=V4L2_STD_PAL_I;
} else if("PAL_D" == std) {
stdid=V4L2_STD_PAL_D;
} else if("PAL_D1" == std) {
stdid=V4L2_STD_PAL_D1;
} else if("PAL_K" == std) {
stdid=V4L2_STD_PAL_K;
} else if("PAL_M" == std) {
stdid=V4L2_STD_PAL_M;
} else if("PAL_N" == std) {
stdid=V4L2_STD_PAL_N;
} else if("PAL_Nc" == std) {
stdid=V4L2_STD_PAL_Nc;
} else if("PAL_60" == std) {
stdid=V4L2_STD_PAL_60;
} else if("NTSC_M" == std) {
stdid=V4L2_STD_NTSC_M;
} else if("NTSC_M_JP" == std) {
stdid=V4L2_STD_NTSC_M_JP;
} else if("NTSC_443" == std) {
stdid=V4L2_STD_NTSC_443;
} else if("NTSC_M_KR" == std) {
stdid=V4L2_STD_NTSC_M_KR;
} else if("SECAM_B" == std) {
stdid=V4L2_STD_SECAM_B;
} else if("SECAM_D" == std) {
stdid=V4L2_STD_SECAM_D;
} else if("SECAM_G" == std) {
stdid=V4L2_STD_SECAM_G;
} else if("SECAM_H" == std) {
stdid=V4L2_STD_SECAM_H;
} else if("SECAM_K" == std) {
stdid=V4L2_STD_SECAM_K;
} else if("SECAM_K1" == std) {
stdid=V4L2_STD_SECAM_K1;
} else if("SECAM_L" == std) {
stdid=V4L2_STD_SECAM_L;
} else if("SECAM_LC" == std) {
stdid=V4L2_STD_SECAM_LC;
} else if("ATSC_8_VSB" == std) {
stdid=V4L2_STD_ATSC_8_VSB;
} else if("ATSC_16_VSB" == std) {
stdid=V4L2_STD_ATSC_16_VSB;
} else if("MN" == std) {
stdid=V4L2_STD_MN;
} else if("B" == std) {
stdid=V4L2_STD_B;
} else if("GH" == std) {
stdid=V4L2_STD_GH;
} else if("DK" == std) {
stdid=V4L2_STD_DK;
} else if("PAL_BG" == std) {
stdid=V4L2_STD_PAL_BG;
} else if("PAL_DK" == std) {
stdid=V4L2_STD_PAL_DK;
} else if("PAL" == std) {
stdid=V4L2_STD_PAL;
} else if("NTSC" == std) {
stdid=V4L2_STD_NTSC;
} else if("SECAM_DK" == std) {
stdid=V4L2_STD_SECAM_DK;
} else if("SECAM" == std) {
stdid=V4L2_STD_SECAM;
} else if("525_60" == std) {
stdid=V4L2_STD_525_60;
} else if("625_50" == std) {
stdid=V4L2_STD_625_50;
} else if("ATSC" == std) {
stdid=V4L2_STD_ATSC;
}
}
break;
default:
continue;
}
if(0 != xioctl(m_tvfd, VIDIOC_S_STD, &stdid)) {
perror("[GEM:videoV4L2] unable to set standard");
}
} else if("channel" == key) {
double ch;
if(props.get("channel", ch)) {
int channel=ch;
if(0 != xioctl(m_tvfd, VIDIOC_S_INPUT, &channel)) {
perror("[GEM:videoV4L2] unable to set channel");
}
}
} else if("frequency" == key) {
} else if("width" == key) {
double d=0.;
if(props.get("width", d)) {
int i=d;
if(m_width != i) {
setformat=true;
}
m_width = i;
}
} else if("height" == key) {
double d=0.;
if(props.get("height", d)) {
int i=d;
if(m_height != i) {
setformat=true;
}
m_height = i;
}
} else {
}
}
}
if(setformat || setcropping) {
restart=true;
}
if(restart) {
bool rendering=m_rendering;
if(m_capturing) {
stopTransfer();
}
#if 0
setcropping=true;
if(setcropping) {
struct v4l2_cropcap cropcap;
cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl (m_tvfd, VIDIOC_CROPCAP, &cropcap)) {
/* Errors ignored. */
}
memset(&(cropcap), 0, sizeof (cropcap));
cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (0 == xioctl (m_tvfd, VIDIOC_CROPCAP, &cropcap)) {
struct v4l2_crop crop;
crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
crop.c = cropcap.defrect; /* reset to default */
if (-1 == xioctl (m_tvfd, VIDIOC_S_CROP, &crop)) {
perror("[GEM:videoV4L2] vidioc_s_crop");
switch (errno) {
case EINVAL:
/* Cropping not supported. */
break;
default:
/* Errors ignored. */
break;
}
}
}
} // cropping
#endif
if(setformat) {
struct v4l2_format fmt;
memset (&(fmt), 0, sizeof (fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (0 == xioctl (m_tvfd, VIDIOC_G_FMT, &fmt)) {
double d;
debugPost("current format %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
if(props.get("width", d)) {
fmt.fmt.pix.width=d;
}
if(props.get("height", d)) {
fmt.fmt.pix.height=d;
}
debugPost("want format %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
if(0 != xioctl (m_tvfd, VIDIOC_S_FMT, &fmt)) {
perror("[GEM:videoV4L2] VIDIOC_S_FMT(dim)");
}
debugPost("new format %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
}
} // format
if (rendering) {
startTransfer();
}
}
}
#else
videoV4L2 :: videoV4L2() : videoBase("") {}
videoV4L2 :: ~videoV4L2() {}
#endif /* HAVE_VIDEO4LINUX2 */
|