Show a patch.

GET /api/patches/10449/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 10449,
    "url": "https://patchwork.libcamera.org/api/patches/10449/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/10449/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20201119123427.53864-4-email@uajain.com>",
    "date": "2020-11-19T12:34:26",
    "name": "[libcamera-devel,3/3] simple-cam: Provide event-loop backed by libevent",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "081ded049cd5946193c12bcabdb95edbb49fdd9b",
    "submitter": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/people/1/?format=api",
        "name": "Umang Jain",
        "email": "email@uajain.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/10449/mbox/",
    "series": [
        {
            "id": 1469,
            "url": "https://patchwork.libcamera.org/api/series/1469/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=1469",
            "date": "2020-11-19T12:34:23",
            "name": "simple-cam: Provide event-loop backed by libevent",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/1469/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/10449/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/10449/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 1931CBE08A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 19 Nov 2020 12:34:43 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id DAC1D615AD;\n\tThu, 19 Nov 2020 13:34:42 +0100 (CET)",
            "from mail.uajain.com (static.126.159.217.95.clients.your-server.de\n\t[95.217.159.126])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 72E1B615AD\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 19 Nov 2020 13:34:41 +0100 (CET)"
        ],
        "Authentication-Results": "lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=uajain.com header.i=@uajain.com\n\theader.b=\"JFRQiHOi\"; dkim-atps=neutral",
        "From": "Umang Jain <email@uajain.com>",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=uajain.com; s=mail;\n\tt=1605789280; bh=v29nXesu/z4NphJ0BC/9ln8uSCd+sbqWGuMlvhMLPME=;\n\th=From:To:Cc:Subject:In-Reply-To:References;\n\tb=JFRQiHOi837J9HgZarzsivw49qBiH24vUVWz2IE2V7rMZnIudvxEEFgCA3tyVLrHD\n\tbYm+SLak+VmdLCo3srDgm50M75qg792pgltI/h6vgVw5E2j0+4ZPcPtleK/K64vhpH\n\tVYrrAhwVRtc3dExcwGktKCFh1c+HtPXO4GAoU6RM4n9eQWqdACV91NnBITUjztPFzt\n\tatUCpON1jfu47dNfGiAtqhrkAArcoa5NWwAlGAlvCJyA11z9WdQEoLkM/1ovzv4AfG\n\tL+MIbapro/6KKNclrYXsJv7x8MYqbiKdpoRJgNsp5fQDnBtYGOK7aoDd0KeOf/aXF2\n\t/zNK5Uvt0n0Hw==",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Thu, 19 Nov 2020 18:04:26 +0530",
        "Message-Id": "<20201119123427.53864-4-email@uajain.com>",
        "In-Reply-To": "<20201119123427.53864-1-email@uajain.com>",
        "References": "<20201119123427.53864-1-email@uajain.com>",
        "Mime-Version": "1.0",
        "Subject": "[libcamera-devel] [PATCH 3/3] simple-cam: Provide event-loop backed\n\tby libevent",
        "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=\"us-ascii\"",
        "Content-Transfer-Encoding": "7bit",
        "Errors-To": "libcamera-devel-bounces@lists.libcamera.org",
        "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"
    },
    "content": "libcamera moved its EventDispatcher and Timer API to its internal API,\nsince providing an event loop to applications should not be the job of\nlibcamera. Application utility like cam, were ported to use libevent,\nhence inspired from that, un-break simple-cam by using the similar\nimplementation to replace the EventDispatcher and Timer functionality\nby libevent.\n\nSigned-off-by: Umang Jain <email@uajain.com>\n---\n meson.build    |   1 +\n simple-cam.cpp | 100 +++++++++++++++++++++++++++++++++++++++++++------\n 2 files changed, 90 insertions(+), 11 deletions(-)",
    "diff": "diff --git a/meson.build b/meson.build\nindex c312f2c..0eca0a1 100644\n--- a/meson.build\n+++ b/meson.build\n@@ -14,6 +14,7 @@ src_files = files([\n # libcamera install path camera.pc file ($prefix/lib/pkgconfig/camera.pc)\n libcamera_deps = [\n       dependency('camera', required : true),\n+      dependency('libevent_pthreads', required : true),\n ]\n \n cpp_arguments = [ '-Wno-unused-parameter', ]\ndiff --git a/simple-cam.cpp b/simple-cam.cpp\nindex 65e0f14..7fbb890 100644\n--- a/simple-cam.cpp\n+++ b/simple-cam.cpp\n@@ -8,12 +8,57 @@\n #include <iomanip>\n #include <iostream>\n #include <memory>\n+#include <mutex>\n+\n+#include <event2/event.h>\n+#include <event2/thread.h>\n \n #include <libcamera/libcamera.h>\n \n+#define TIMEOUT_SEC 3\n+\n using namespace libcamera;\n std::shared_ptr<Camera> camera;\n \n+/*\n+ * --------------------------------------------------------------------\n+ * Helper functions to interact and control the event loop.\n+ */\n+bool capture = true;\n+std::mutex lock;\n+std::list<std::function<void()>> calls_;\n+struct event_base *event;\n+\n+void interrupt()\n+{\n+\tevent_base_loopbreak(event);\n+}\n+\n+void callLater(const std::function<void()> &func)\n+{\n+\t{\n+\t\tstd::unique_lock<std::mutex> locker(lock);\n+\t\tcalls_.push_back(func);\n+\t}\n+\n+\tinterrupt();\n+}\n+\n+void dispatchCalls()\n+{\n+\tstd::unique_lock<std::mutex> locker(lock);\n+\n+\tfor (auto iter = calls_.begin(); iter != calls_.end(); ) {\n+\t\tstd::function<void()> call = std::move(*iter);\n+\n+\t\titer = calls_.erase(iter);\n+\n+\t\tlocker.unlock();\n+\t\tcall();\n+\t\tlocker.lock();\n+\t}\n+}\n+\n /*\n  * --------------------------------------------------------------------\n  * Handle RequestComplete\n@@ -21,13 +66,26 @@ std::shared_ptr<Camera> camera;\n  * For each Camera::requestCompleted Signal emitted from the Camera the\n  * connected Slot is invoked.\n  *\n+ * The Slot is invoked in the CameraManager's thread, hence one should avoid\n+ * any heavy processing here. The processing of the request shall be re-directed\n+ * to the application's thread instead, so as not to block the CameraManager's\n+ * thread for large amount of time.\n+ *\n  * The Slot receives the Request as a parameter.\n  */\n+\n+static void processRequest(Request *request);\n+\n static void requestComplete(Request *request)\n {\n \tif (request->status() == Request::RequestCancelled)\n \t\treturn;\n \n+\tcallLater(std::bind(&processRequest, request));\n+}\n+\n+static void processRequest(Request *request)\n+{\n \tconst Request::BufferMap &buffers = request->buffers();\n \n \tfor (auto bufferPair : buffers) {\n@@ -60,6 +118,15 @@ static void requestComplete(Request *request)\n \tcamera->queueRequest(request);\n }\n \n+void timeoutTriggered(int fd, short event, void *arg)\n+{\n+\tcapture = false;\n+\tinterrupt();\n+\n+\tstd::cout << \"Capture ran for \" << TIMEOUT_SEC << \" seconds, Exiting.\"\n+\t\t  << std::endl;\n+}\n+\n int main()\n {\n \t/*\n@@ -321,18 +388,25 @@ int main()\n \t * In order to dispatch events received from the video devices, such\n \t * as buffer completions, an event loop has to be run.\n \t *\n-\t * Libcamera provides its own default event dispatcher realized by\n-\t * polling a set of file descriptors, but applications can integrate\n-\t * their own even loop with the Libcamera EventDispatcher.\n-\t *\n-\t * Here, as an example, run the poll-based EventDispatcher for 3\n-\t * seconds.\n+\t * simple-cam uses a base event loop provided by libevent, which is run\n+\t * for TIMEOUT_SEC seconds after which, the event loop is interrupted\n+\t * via interrupt() and begins the cleanup. The interval for which the\n+\t * event loop is being run, completed requests are processed via\n+\t * requestComplete() handler.\n \t */\n-\tEventDispatcher *dispatcher = cm->eventDispatcher();\n-\tTimer timer;\n-\ttimer.start(3000);\n-\twhile (timer.isRunning())\n-\t\tdispatcher->processEvents();\n+\tstruct event *ev;\n+\tstruct timeval tv;\n+\n+\ttv.tv_sec = TIMEOUT_SEC;\n+\ttv.tv_usec = 0;\n+\tevthread_use_pthreads();\n+\tevent = event_base_new();\n+\tev = event_new(event, -1, EV_TIMEOUT, &timeoutTriggered, NULL);\n+\tevent_add(ev, &tv);\n+\twhile (capture) {\n+\t\tdispatchCalls();\n+\t\tevent_base_loop(event, EVLOOP_NO_EXIT_ON_EMPTY);\n+\t}\n \n \t/*\n \t * --------------------------------------------------------------------\n@@ -341,6 +415,10 @@ int main()\n \t * Stop the Camera, release resources and stop the CameraManager.\n \t * Libcamera has now released all resources it owned.\n \t */\n+\tevent_free(ev);\n+\tevent_base_free(event);\n+\tlibevent_global_shutdown();\n+\n \tcamera->stop();\n \tallocator->free(stream);\n \tdelete allocator;\n",
    "prefixes": [
        "libcamera-devel",
        "3/3"
    ]
}