From e0f8d7885abcc57222db6ca717e7061a22d5750f Mon Sep 17 00:00:00 2001 From: rdb Date: Sun, 11 Mar 2018 16:33:26 +0100 Subject: [PATCH] video4linux: don't block on reading camera frames Add v4l-blocking variable to enable the old behaviour. --- panda/src/vision/config_vision.cxx | 4 ++++ panda/src/vision/config_vision.h | 3 +++ panda/src/vision/webcamVideoCursorV4L.cxx | 12 +++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/panda/src/vision/config_vision.cxx b/panda/src/vision/config_vision.cxx index 3b53c0ed02..50d4b8aeb3 100644 --- a/panda/src/vision/config_vision.cxx +++ b/panda/src/vision/config_vision.cxx @@ -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(); } diff --git a/panda/src/vision/config_vision.h b/panda/src/vision/config_vision.h index 5eec1a95b2..1ad2aaa59d 100644 --- a/panda/src/vision/config_vision.h +++ b/panda/src/vision/config_vision.h @@ -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 diff --git a/panda/src/vision/webcamVideoCursorV4L.cxx b/panda/src/vision/webcamVideoCursorV4L.cxx index 497115c0dd..a78c55135a 100644 --- a/panda/src/vision/webcamVideoCursorV4L.cxx +++ b/panda/src/vision/webcamVideoCursorV4L.cxx @@ -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; }