{"id":10090,"url":"https://patchwork.libcamera.org/api/1.1/patches/10090/?format=json","web_url":"https://patchwork.libcamera.org/patch/10090/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20201018015754.3784352-1-niklas.soderlund@ragnatech.se>","date":"2020-10-18T01:57:54","name":"[libcamera-devel] libcamera: v4l2_device: Move start of frame detection to V4L2Device","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"fb96750fe45f5560f4391a17f6e431e79920aae5","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/1.1/people/5/?format=json","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/10090/mbox/","series":[{"id":1381,"url":"https://patchwork.libcamera.org/api/1.1/series/1381/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1381","date":"2020-10-18T01:57:54","name":"[libcamera-devel] libcamera: v4l2_device: Move start of frame detection to V4L2Device","version":1,"mbox":"https://patchwork.libcamera.org/series/1381/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/10090/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/10090/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 27D97BDB1F\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 18 Oct 2020 01:58:14 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 768E2610D5;\n\tSun, 18 Oct 2020 03:58:13 +0200 (CEST)","from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net\n\t[195.74.38.228])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id F1BBE60532\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 18 Oct 2020 03:58:11 +0200 (CEST)","from bismarck.berto.se (p4fca2fef.dip0.t-ipconnect.de\n\t[79.202.47.239]) by bin-vsp-out-01.atm.binero.net (Halon) with ESMTPA\n\tid 53eec93a-10e5-11eb-9f83-005056917a89;\n\tSun, 18 Oct 2020 03:57:55 +0200 (CEST)"],"X-Halon-ID":"53eec93a-10e5-11eb-9f83-005056917a89","Authorized-sender":"niklas.soderlund@fsdn.se","From":"=?utf-8?q?Niklas_S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","To":"libcamera-devel@lists.libcamera.org","Date":"Sun, 18 Oct 2020 03:57:54 +0200","Message-Id":"<20201018015754.3784352-1-niklas.soderlund@ragnatech.se>","X-Mailer":"git-send-email 2.28.0","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH] libcamera: v4l2_device: Move start of\n\tframe detection to V4L2Device","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Content-Type":"text/plain; charset=\"utf-8\"","Content-Transfer-Encoding":"base64","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"The V4L2_EVENT_FRAME_SYNC event may occur on both V4L2 video-devices\n(V4L2VideoDevice) and sub-devices (V4L2Subdevice). Move the start of\nframe detection to the common base class of the two, V4L2Device.\n\nThere is no functional change.\n\nSigned-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n---\n include/libcamera/internal/v4l2_device.h      | 13 ++++\n include/libcamera/internal/v4l2_videodevice.h |  8 --\n src/libcamera/v4l2_device.cpp                 | 78 ++++++++++++++++++-\n src/libcamera/v4l2_videodevice.cpp            | 75 +-----------------\n 4 files changed, 91 insertions(+), 83 deletions(-)","diff":"diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h\nindex 722fb72207d74111..b0a9ed0a1d3557e6 100644\n--- a/include/libcamera/internal/v4l2_device.h\n+++ b/include/libcamera/internal/v4l2_device.h\n@@ -13,11 +13,15 @@\n \n #include <linux/videodev2.h>\n \n+#include <libcamera/signal.h>\n+\n #include \"libcamera/internal/log.h\"\n #include \"libcamera/internal/v4l2_controls.h\"\n \n namespace libcamera {\n \n+class EventNotifier;\n+\n class V4L2Device : protected Loggable\n {\n public:\n@@ -34,6 +38,9 @@ public:\n \tconst std::string &deviceNode() const { return deviceNode_; }\n \tstd::string devicePath() const;\n \n+\tint setFrameStartEnabled(bool enable);\n+\tSignal<uint32_t> frameStart;\n+\n protected:\n \tV4L2Device(const std::string &deviceNode);\n \t~V4L2Device();\n@@ -45,6 +52,8 @@ protected:\n \n \tint fd() { return fd_; }\n \n+\tvoid eventAvailable(EventNotifier *notifier);\n+\n private:\n \tvoid listControls();\n \tvoid updateControls(ControlList *ctrls,\n@@ -56,6 +65,10 @@ private:\n \tControlInfoMap controls_;\n \tstd::string deviceNode_;\n \tint fd_;\n+\n+\tEventNotifier *fdEventNotifier_;\n+\n+\tbool frameStartEnabled_;\n };\n \n } /* namespace libcamera */\ndiff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h\nindex 40ed87e17cfa6d3c..2fd09a1343566efa 100644\n--- a/include/libcamera/internal/v4l2_videodevice.h\n+++ b/include/libcamera/internal/v4l2_videodevice.h\n@@ -204,9 +204,6 @@ public:\n \tint queueBuffer(FrameBuffer *buffer);\n \tSignal<FrameBuffer *> bufferReady;\n \n-\tint setFrameStartEnabled(bool enable);\n-\tSignal<uint32_t> frameStart;\n-\n \tint streamOn();\n \tint streamOff();\n \n@@ -240,8 +237,6 @@ private:\n \tvoid bufferAvailable(EventNotifier *notifier);\n \tFrameBuffer *dequeueBuffer();\n \n-\tvoid eventAvailable(EventNotifier *notifier);\n-\n \tV4L2Capability caps_;\n \n \tenum v4l2_buf_type bufferType_;\n@@ -251,9 +246,6 @@ private:\n \tstd::map<unsigned int, FrameBuffer *> queuedBuffers_;\n \n \tEventNotifier *fdBufferNotifier_;\n-\tEventNotifier *fdEventNotifier_;\n-\n-\tbool frameStartEnabled_;\n };\n \n class V4L2M2MDevice\ndiff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\nindex 31d4dad0ee47b734..36a2c21e6544af22 100644\n--- a/src/libcamera/v4l2_device.cpp\n+++ b/src/libcamera/v4l2_device.cpp\n@@ -16,6 +16,8 @@\n #include <sys/syscall.h>\n #include <unistd.h>\n \n+#include <libcamera/event_notifier.h>\n+\n #include \"libcamera/internal/log.h\"\n #include \"libcamera/internal/sysfs.h\"\n #include \"libcamera/internal/utils.h\"\n@@ -52,7 +54,8 @@ LOG_DEFINE_CATEGORY(V4L2)\n  * at open() time, and the \\a logTag to prefix log messages with.\n  */\n V4L2Device::V4L2Device(const std::string &deviceNode)\n-\t: deviceNode_(deviceNode), fd_(-1)\n+\t: deviceNode_(deviceNode), fd_(-1), fdEventNotifier_(nullptr),\n+\t  frameStartEnabled_(false)\n {\n }\n \n@@ -91,6 +94,10 @@ int V4L2Device::open(unsigned int flags)\n \n \tlistControls();\n \n+\tfdEventNotifier_ = new EventNotifier(fd_, EventNotifier::Exception);\n+\tfdEventNotifier_->activated.connect(this, &V4L2Device::eventAvailable);\n+\tfdEventNotifier_->setEnabled(false);\n+\n \treturn 0;\n }\n \n@@ -117,6 +124,10 @@ int V4L2Device::setFd(int fd)\n \n \tfd_ = fd;\n \n+\tfdEventNotifier_ = new EventNotifier(fd_, EventNotifier::Exception);\n+\tfdEventNotifier_->activated.connect(this, &V4L2Device::eventAvailable);\n+\tfdEventNotifier_->setEnabled(false);\n+\n \treturn 0;\n }\n \n@@ -130,6 +141,8 @@ void V4L2Device::close()\n \tif (!isOpen())\n \t\treturn;\n \n+\tdelete fdEventNotifier_;\n+\n \tif (::close(fd_) < 0)\n \t\tLOG(V4L2, Error) << \"Failed to close V4L2 device: \"\n \t\t\t\t << strerror(errno);\n@@ -396,6 +409,40 @@ std::string V4L2Device::devicePath() const\n \treturn path;\n }\n \n+/**\n+ * \\brief Enable or disable frame start event notification\n+ * \\param[in] enable True to enable frame start events, false to disable them\n+ *\n+ * This function enables or disables generation of frame start events. Once\n+ * enabled, the events are signalled through the frameStart signal.\n+ *\n+ * \\return 0 on success, a negative error code otherwise\n+ */\n+int V4L2Device::setFrameStartEnabled(bool enable)\n+{\n+\tif (frameStartEnabled_ == enable)\n+\t\treturn 0;\n+\n+\tstruct v4l2_event_subscription event{};\n+\tevent.type = V4L2_EVENT_FRAME_SYNC;\n+\n+\tunsigned long request = enable ? VIDIOC_SUBSCRIBE_EVENT\n+\t\t\t      : VIDIOC_UNSUBSCRIBE_EVENT;\n+\tint ret = ioctl(request, &event);\n+\tif (enable && ret)\n+\t\treturn ret;\n+\n+\tfdEventNotifier_->setEnabled(enable);\n+\tframeStartEnabled_ = enable;\n+\n+\treturn ret;\n+}\n+\n+/**\n+ * \\var V4L2Device::frameStart\n+ * \\brief A Signal emitted when capture of a frame has started\n+ */\n+\n /**\n  * \\brief Perform an IOCTL system call on the device node\n  * \\param[in] request The IOCTL request code\n@@ -414,6 +461,35 @@ int V4L2Device::ioctl(unsigned long request, void *argp)\n \treturn 0;\n }\n \n+/**\n+ * \\brief Slot to handle V4L2 events from the V4L2 device\n+ * \\param[in] notifier The event notifier\n+ *\n+ * When this slot is called, a V4L2 event is available to be dequeued from the\n+ * device.\n+ */\n+void V4L2Device::eventAvailable([[maybe_unused]] EventNotifier *notifier)\n+{\n+\tstruct v4l2_event event{};\n+\tint ret = ioctl(VIDIOC_DQEVENT, &event);\n+\tif (ret < 0) {\n+\t\tLOG(V4L2, Error)\n+\t\t\t<< \"Failed to dequeue event, disabling event notifier\";\n+\t\tfdEventNotifier_->setEnabled(false);\n+\t\treturn;\n+\t}\n+\n+\tif (event.type != V4L2_EVENT_FRAME_SYNC) {\n+\t\tLOG(V4L2, Error)\n+\t\t\t<< \"Spurious event (\" << event.type\n+\t\t\t<< \"), disabling event notifier\";\n+\t\tfdEventNotifier_->setEnabled(false);\n+\t\treturn;\n+\t}\n+\n+\tframeStart.emit(event.u.frame_sync.frame_sequence);\n+}\n+\n /**\n  * \\fn V4L2Device::deviceNode()\n  * \\brief Retrieve the device node path\ndiff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\nindex 508522ef42bb52cb..6e86849b0013c373 100644\n--- a/src/libcamera/v4l2_videodevice.cpp\n+++ b/src/libcamera/v4l2_videodevice.cpp\n@@ -471,8 +471,7 @@ const std::string V4L2DeviceFormat::toString() const\n  * \\param[in] deviceNode The file-system path to the video device node\n  */\n V4L2VideoDevice::V4L2VideoDevice(const std::string &deviceNode)\n-\t: V4L2Device(deviceNode), cache_(nullptr), fdBufferNotifier_(nullptr),\n-\t  fdEventNotifier_(nullptr), frameStartEnabled_(false)\n+\t: V4L2Device(deviceNode), cache_(nullptr), fdBufferNotifier_(nullptr)\n {\n \t/*\n \t * We default to an MMAP based CAPTURE video device, however this will\n@@ -565,10 +564,6 @@ int V4L2VideoDevice::open()\n \tfdBufferNotifier_->activated.connect(this, &V4L2VideoDevice::bufferAvailable);\n \tfdBufferNotifier_->setEnabled(false);\n \n-\tfdEventNotifier_ = new EventNotifier(fd(), EventNotifier::Exception);\n-\tfdEventNotifier_->activated.connect(this, &V4L2VideoDevice::eventAvailable);\n-\tfdEventNotifier_->setEnabled(false);\n-\n \tLOG(V4L2, Debug)\n \t\t<< \"Opened device \" << caps_.bus_info() << \": \"\n \t\t<< caps_.driver() << \": \" << caps_.card();\n@@ -658,10 +653,6 @@ int V4L2VideoDevice::open(int handle, enum v4l2_buf_type type)\n \tfdBufferNotifier_->activated.connect(this, &V4L2VideoDevice::bufferAvailable);\n \tfdBufferNotifier_->setEnabled(false);\n \n-\tfdEventNotifier_ = new EventNotifier(fd(), EventNotifier::Exception);\n-\tfdEventNotifier_->activated.connect(this, &V4L2VideoDevice::eventAvailable);\n-\tfdEventNotifier_->setEnabled(false);\n-\n \tLOG(V4L2, Debug)\n \t\t<< \"Opened device \" << caps_.bus_info() << \": \"\n \t\t<< caps_.driver() << \": \" << caps_.card();\n@@ -679,7 +670,6 @@ void V4L2VideoDevice::close()\n \n \treleaseBuffers();\n \tdelete fdBufferNotifier_;\n-\tdelete fdEventNotifier_;\n \n \tV4L2Device::close();\n }\n@@ -1532,74 +1522,11 @@ FrameBuffer *V4L2VideoDevice::dequeueBuffer()\n \treturn buffer;\n }\n \n-/**\n- * \\brief Slot to handle V4L2 events from the V4L2 video device\n- * \\param[in] notifier The event notifier\n- *\n- * When this slot is called, a V4L2 event is available to be dequeued from the\n- * device.\n- */\n-void V4L2VideoDevice::eventAvailable([[maybe_unused]] EventNotifier *notifier)\n-{\n-\tstruct v4l2_event event{};\n-\tint ret = ioctl(VIDIOC_DQEVENT, &event);\n-\tif (ret < 0) {\n-\t\tLOG(V4L2, Error)\n-\t\t\t<< \"Failed to dequeue event, disabling event notifier\";\n-\t\tfdEventNotifier_->setEnabled(false);\n-\t\treturn;\n-\t}\n-\n-\tif (event.type != V4L2_EVENT_FRAME_SYNC) {\n-\t\tLOG(V4L2, Error)\n-\t\t\t<< \"Spurious event (\" << event.type\n-\t\t\t<< \"), disabling event notifier\";\n-\t\tfdEventNotifier_->setEnabled(false);\n-\t\treturn;\n-\t}\n-\n-\tframeStart.emit(event.u.frame_sync.frame_sequence);\n-}\n-\n /**\n  * \\var V4L2VideoDevice::bufferReady\n  * \\brief A Signal emitted when a framebuffer completes\n  */\n \n-/**\n- * \\brief Enable or disable frame start event notification\n- * \\param[in] enable True to enable frame start events, false to disable them\n- *\n- * This function enables or disables generation of frame start events. Once\n- * enabled, the events are signalled through the frameStart signal.\n- *\n- * \\return 0 on success, a negative error code otherwise\n- */\n-int V4L2VideoDevice::setFrameStartEnabled(bool enable)\n-{\n-\tif (frameStartEnabled_ == enable)\n-\t\treturn 0;\n-\n-\tstruct v4l2_event_subscription event{};\n-\tevent.type = V4L2_EVENT_FRAME_SYNC;\n-\n-\tunsigned long request = enable ? VIDIOC_SUBSCRIBE_EVENT\n-\t\t\t      : VIDIOC_UNSUBSCRIBE_EVENT;\n-\tint ret = ioctl(request, &event);\n-\tif (enable && ret)\n-\t\treturn ret;\n-\n-\tfdEventNotifier_->setEnabled(enable);\n-\tframeStartEnabled_ = enable;\n-\n-\treturn ret;\n-}\n-\n-/**\n- * \\var V4L2VideoDevice::frameStart\n- * \\brief A Signal emitted when capture of a frame has started\n- */\n-\n /**\n  * \\brief Start the video stream\n  * \\return 0 on success or a negative error code otherwise\n","prefixes":["libcamera-devel"]}