Show a patch.

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

{
    "id": 10534,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/10534/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/10534/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/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": "<20201201174314.12774-5-email@uajain.com>",
    "date": "2020-12-01T17:43:14",
    "name": "[libcamera-devel,v2,4/4] simple-cam: Provide event-loop backed by libevent",
    "commit_ref": null,
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "e83e5147163ababed6a13c1258d8aa7d4f009f56",
    "submitter": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/people/1/?format=api",
        "name": "Umang Jain",
        "email": "email@uajain.com"
    },
    "delegate": {
        "id": 11,
        "url": "https://patchwork.libcamera.org/api/1.1/users/11/?format=api",
        "username": "kbingham",
        "first_name": "Kieran",
        "last_name": "Bingham",
        "email": "kieran.bingham@ideasonboard.com"
    },
    "mbox": "https://patchwork.libcamera.org/patch/10534/mbox/",
    "series": [
        {
            "id": 1498,
            "url": "https://patchwork.libcamera.org/api/1.1/series/1498/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=1498",
            "date": "2020-12-01T17:43:10",
            "name": "simple-cam: Provide event-loop backed by libevent",
            "version": 2,
            "mbox": "https://patchwork.libcamera.org/series/1498/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/10534/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/10534/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 E0AEEBE177\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  1 Dec 2020 17:43:33 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id AB16063501;\n\tTue,  1 Dec 2020 18:43:33 +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 D227163460\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  1 Dec 2020 18:43:31 +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=\"jlpPDv5f\"; 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=1606844611; bh=pNoQ0U/VmFqQ14vuFDziSpJb3Gj2Au/s+/uuIPwLXPo=;\n\th=From:To:Cc:Subject:In-Reply-To:References;\n\tb=jlpPDv5fN34w3KaHw5m3vlU+x9LVKBa/1vdn5soFln5VXmPLPL7RkYMTrIeKrXyUi\n\tmfSE8G6jzfg9czvxBHgM8TeDi556ca/OZ+6I1WboVPpwJfbkhjDFdyldHtGO3k8Pku\n\t8ZvFca+HHRjZiyAZWC+ZriMqK9tOJnvJtUjlLqH2OttRXAPv5TsgaHuvHbSzsW4nwA\n\tti2kARJVeNoLi/K6Ylgv3O6uif/7gVS9f7eNrjZiA5WzaTrbCjYINyJ/kONUr2wCKf\n\t6osqhtlUsNfzhEtyE8VsqW+6rRFbajOe6bOvQbpN/ntpZQVpMmA1bWn94BFbFCgIae\n\t9vSR2ilpbljBg==",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Tue,  1 Dec 2020 23:13:14 +0530",
        "Message-Id": "<20201201174314.12774-5-email@uajain.com>",
        "In-Reply-To": "<20201201174314.12774-1-email@uajain.com>",
        "References": "<20201201174314.12774-1-email@uajain.com>",
        "Mime-Version": "1.0",
        "Subject": "[libcamera-devel] [PATCH v2 4/4] simple-cam: Provide event-loop\n\tbacked by 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 | 101 +++++++++++++++++++++++++++++++++++++++++++------\n 2 files changed, 91 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 0e26895..dd785fc 100644\n--- a/simple-cam.cpp\n+++ b/simple-cam.cpp\n@@ -8,12 +8,58 @@\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+ * \\todo Split event loop handling to a separate class and file.\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 +67,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 +119,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 +389,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 +416,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",
        "v2",
        "4/4"
    ]
}