video4linux: don't block on reading camera frames

Add v4l-blocking variable to enable the old behaviour.
This commit is contained in:
rdb 2018-03-11 16:33:26 +01:00
parent 2563b65249
commit e0f8d7885a
3 changed files with 18 additions and 1 deletions

View File

@ -26,6 +26,10 @@
Configure(config_vision);
NotifyCategoryDef(vision, "");
ConfigVariableBool v4l_blocking
("v4l-blocking", false,
PRC_DESC("Set this to true if you want to block waiting for webcam frames."));
ConfigureFn(config_vision) {
init_libvision();
}

View File

@ -16,9 +16,12 @@
#include "pandabase.h"
#include "notifyCategoryProxy.h"
#include "configVariableBool.h"
NotifyCategoryDecl(vision, EXPCL_VISION, EXPTP_VISION);
extern ConfigVariableBool v4l_blocking;
extern EXPCL_VISION void init_libvision();
#endif

View File

@ -209,7 +209,13 @@ WebcamVideoCursorV4L(WebcamVideoV4L *src) : MovieVideoCursor(src) {
_buffers = NULL;
_buflens = NULL;
_fd = open(src->_device.c_str(), O_RDWR);
int mode = O_RDWR;
if (!v4l_blocking) {
mode = O_NONBLOCK;
}
_fd = open(src->_device.c_str(), mode);
if (-1 == _fd) {
vision_cat.error() << "Failed to open " << src->_device.c_str() << "\n";
return;
@ -397,6 +403,10 @@ fetch_buffer() {
vbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
vbuf.memory = V4L2_MEMORY_MMAP;
if (-1 == ioctl(_fd, VIDIOC_DQBUF, &vbuf) && errno != EIO) {
if (errno == EAGAIN) {
// Simply nothing is available yet.
return NULL;
}
vision_cat.error() << "Failed to dequeue buffer!\n";
return NULL;
}