Show a patch.

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

{
    "id": 16182,
    "url": "https://patchwork.libcamera.org/api/patches/16182/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/16182/",
    "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": "<20220608072418.13154-4-tomi.valkeinen@ideasonboard.com>",
    "date": "2022-06-08T07:24:17",
    "name": "[libcamera-devel,v5,3/4] py: examples: Add simple-continuous-capture.py",
    "commit_ref": null,
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "f4b35605bf44e2afc8770936b6e45451924f4b5c",
    "submitter": {
        "id": 109,
        "url": "https://patchwork.libcamera.org/api/people/109/?format=api",
        "name": "Tomi Valkeinen",
        "email": "tomi.valkeinen@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/16182/mbox/",
    "series": [
        {
            "id": 3165,
            "url": "https://patchwork.libcamera.org/api/series/3165/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=3165",
            "date": "2022-06-08T07:24:14",
            "name": "More misc Python patches",
            "version": 5,
            "mbox": "https://patchwork.libcamera.org/series/3165/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/16182/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/16182/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 A3A5CC3274\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  8 Jun 2022 07:24:46 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id D5A3665636;\n\tWed,  8 Jun 2022 09:24:45 +0200 (CEST)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 82CE565631\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  8 Jun 2022 09:24:41 +0200 (CEST)",
            "from deskari.lan (91-156-85-209.elisa-laajakaista.fi\n\t[91.156.85.209])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id EB923D1C;\n\tWed,  8 Jun 2022 09:24:40 +0200 (CEST)"
        ],
        "DKIM-Signature": [
            "v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1654673085;\n\tbh=y4VOcrGGpifJBOVzcwjaTzFy0r0DJ1RO+SPe5KtVKCg=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=e8EijUSHklM4DUUKMOzJcI6dG+1FUTmQWp4WRlL/K0iJhCN0yX3wYEyPLEBPeSQ5g\n\tEesCUIXMonfw6Qk/jWTeegfYFnK2QnX964a8/EilzXPvv1SR9WqHMHhXsufB7sm5jJ\n\tNbW5GujnC4mTNhrMxe4jooslK/Vry7ZctXWEe2RdekBNx24hbUWvNeamLzSiLo7Iu/\n\tv938OJWq3YUYoFVQEYDHWYQFdumRHORPgGLqPAxDhGBUA0LOZXisvTMxJGgJMied/e\n\tPMbpWfgdcM21ArLSeaUZf/5lpFMBo5oNkfb3npZaZ7z5+2G/yWWYPRgYPh6ijT4Cwr\n\t++DAO3bSo0HFw==",
            "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1654673081;\n\tbh=y4VOcrGGpifJBOVzcwjaTzFy0r0DJ1RO+SPe5KtVKCg=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=qbe/PrpMm0DFLfJcIB6bJYU7rGCLK6BGGH3nQzK7Oh6FpB9TTkwFqHMAyN7hFXPhc\n\tOl/HQFbrL2Lgl2+1BSccBcgOx6sZn04FxJW9TXD176o8zsayzb7Br1gE0LnRUGHehN\n\tz9SiHydjtYOjPXoHQs9Ik/Xe7eV8L/dOrEE+rr/Y="
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"qbe/PrpM\"; dkim-atps=neutral",
        "To": "libcamera-devel@lists.libcamera.org,\n\tDavid Plowman <david.plowman@raspberrypi.com>,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>,\n\tLaurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tJacopo Mondi <jacopo@jmondi.org>",
        "Date": "Wed,  8 Jun 2022 10:24:17 +0300",
        "Message-Id": "<20220608072418.13154-4-tomi.valkeinen@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.34.1",
        "In-Reply-To": "<20220608072418.13154-1-tomi.valkeinen@ideasonboard.com>",
        "References": "<20220608072418.13154-1-tomi.valkeinen@ideasonboard.com>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v5 3/4] py: examples: Add\n\tsimple-continuous-capture.py",
        "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>",
        "From": "Tomi Valkeinen via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>",
        "Reply-To": "Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>",
        "Errors-To": "libcamera-devel-bounces@lists.libcamera.org",
        "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"
    },
    "content": "Add a slightly more complex, and I think a more realistic, example,\nwhere the script reacts to events and re-queues the buffers.\n\nSigned-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\nReviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n---\n src/py/examples/simple-continuous-capture.py | 191 +++++++++++++++++++\n 1 file changed, 191 insertions(+)\n create mode 100755 src/py/examples/simple-continuous-capture.py",
    "diff": "diff --git a/src/py/examples/simple-continuous-capture.py b/src/py/examples/simple-continuous-capture.py\nnew file mode 100755\nindex 00000000..fe78a2dd\n--- /dev/null\n+++ b/src/py/examples/simple-continuous-capture.py\n@@ -0,0 +1,191 @@\n+#!/usr/bin/env python3\n+\n+# SPDX-License-Identifier: BSD-3-Clause\n+# Copyright (C) 2022, Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\n+\n+# A simple capture example extending the simple-capture.py example:\n+# - Capture frames using events from multiple cameras\n+# - Listening events from stdin to exit the application\n+# - Memory mapping the frames and calculating CRC\n+\n+import binascii\n+import libcamera as libcam\n+import libcamera.utils\n+import selectors\n+import sys\n+\n+\n+# A container class for our state per camera\n+class CameraCaptureContext:\n+    idx: int\n+    cam: libcam.Camera\n+    reqs: list[libcam.Request]\n+    mfbs: dict[libcam.FrameBuffer, libcamera.utils.MappedFrameBuffer]\n+\n+    def __init__(self, cam, idx):\n+        self.idx = idx\n+        self.cam = cam\n+\n+        # Acquire the camera for our use\n+\n+        ret = cam.acquire()\n+        assert ret == 0\n+\n+        # Configure the camera\n+\n+        cam_config = cam.generate_configuration([libcam.StreamRole.Viewfinder])\n+\n+        stream_config = cam_config.at(0)\n+\n+        ret = cam.configure(cam_config)\n+        assert ret == 0\n+\n+        stream = stream_config.stream\n+\n+        # Allocate the buffers for capture\n+\n+        allocator = libcam.FrameBufferAllocator(cam)\n+        ret = allocator.allocate(stream)\n+        assert ret > 0\n+\n+        num_bufs = len(allocator.buffers(stream))\n+\n+        print(f'cam{idx} ({cam.id}): capturing {num_bufs} buffers with {stream_config}')\n+\n+        # Create the requests and assign a buffer for each request\n+\n+        self.reqs = []\n+        self.mfbs = {}\n+\n+        for i in range(num_bufs):\n+            # Use the buffer index as the \"cookie\"\n+            req = cam.create_request(idx)\n+\n+            buffer = allocator.buffers(stream)[i]\n+            ret = req.add_buffer(stream, buffer)\n+            assert ret == 0\n+\n+            self.reqs.append(req)\n+\n+            # Save a mmapped buffer so we can calculate the CRC later\n+            self.mfbs[buffer] = libcamera.utils.MappedFrameBuffer(buffer).mmap()\n+\n+    def uninit_camera(self):\n+        # Stop the camera\n+\n+        ret = self.cam.stop()\n+        assert ret == 0\n+\n+        # Release the camera\n+\n+        ret = self.cam.release()\n+        assert ret == 0\n+\n+\n+# A container class for our state\n+class CaptureContext:\n+    cm: libcam.CameraManager\n+    camera_contexts: list[CameraCaptureContext] = []\n+\n+    def handle_camera_event(self):\n+        # cm.get_ready_requests() will not block here, as we know there is an event\n+        # to read.\n+\n+        reqs = self.cm.get_ready_requests()\n+\n+        # Process the captured frames\n+\n+        for req in reqs:\n+            self.handle_request(req)\n+\n+        return True\n+\n+    def handle_request(self, req: libcam.Request):\n+        cam_ctx = self.camera_contexts[req.cookie]\n+\n+        buffers = req.buffers\n+\n+        assert len(buffers) == 1\n+\n+        # A ready Request could contain multiple buffers if multiple streams\n+        # were being used. Here we know we only have a single stream,\n+        # and we use next(iter()) to get the first and only buffer.\n+\n+        stream, fb = next(iter(buffers.items()))\n+\n+        # Use the MappedFrameBuffer to access the pixel data with CPU. We calculate\n+        # the crc for each plane.\n+\n+        mfb = cam_ctx.mfbs[fb]\n+        crcs = [binascii.crc32(p) for p in mfb.planes]\n+\n+        meta = fb.metadata\n+\n+        print('cam{:<6} seq {:<6} bytes {:10} CRCs {}'\n+              .format(cam_ctx.idx,\n+                      meta.sequence,\n+                      '/'.join([str(p.bytes_used) for p in meta.planes]),\n+                      crcs))\n+\n+        # We want to re-queue the buffer we just handled. Instead of creating\n+        # a new Request, we re-use the old one. We need to call req.reuse()\n+        # to re-initialize the Request before queuing.\n+\n+        req.reuse()\n+        cam_ctx.cam.queue_request(req)\n+\n+    def handle_key_event(self):\n+        sys.stdin.readline()\n+        print('Exiting...')\n+        return False\n+\n+    def capture(self):\n+        # Queue the requests to the camera\n+\n+        for cam_ctx in self.camera_contexts:\n+            for req in cam_ctx.reqs:\n+                ret = cam_ctx.cam.queue_request(req)\n+                assert ret == 0\n+\n+        # Use Selector to wait for events from the camera and from the keyboard\n+\n+        sel = selectors.DefaultSelector()\n+        sel.register(sys.stdin, selectors.EVENT_READ, self.handle_key_event)\n+        sel.register(self.cm.event_fd, selectors.EVENT_READ, lambda: self.handle_camera_event())\n+\n+        running = True\n+\n+        while running:\n+            events = sel.select()\n+            for key, mask in events:\n+                # If the handler return False, we should exit\n+                if not key.data():\n+                    running = False\n+\n+\n+def main():\n+    cm = libcam.CameraManager.singleton()\n+\n+    ctx = CaptureContext()\n+    ctx.cm = cm\n+\n+    for idx, cam in enumerate(cm.cameras):\n+        cam_ctx = CameraCaptureContext(cam, idx)\n+        ctx.camera_contexts.append(cam_ctx)\n+\n+    # Start the cameras\n+\n+    for cam_ctx in ctx.camera_contexts:\n+        ret = cam_ctx.cam.start()\n+        assert ret == 0\n+\n+    ctx.capture()\n+\n+    for cam_ctx in ctx.camera_contexts:\n+        cam_ctx.uninit_camera()\n+\n+    return 0\n+\n+\n+if __name__ == '__main__':\n+    sys.exit(main())\n",
    "prefixes": [
        "libcamera-devel",
        "v5",
        "3/4"
    ]
}