Show a patch.

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

{
    "id": 16121,
    "url": "https://patchwork.libcamera.org/api/patches/16121/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/16121/",
    "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": "<20220530142722.57618-15-tomi.valkeinen@ideasonboard.com>",
    "date": "2022-05-30T14:27:20",
    "name": "[libcamera-devel,v4,14/16] py: examples: Add simple-continuous-capture.py",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "3e429c94bb000098c7da4323f48abe46fd15e2d9",
    "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/16121/mbox/",
    "series": [
        {
            "id": 3149,
            "url": "https://patchwork.libcamera.org/api/series/3149/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=3149",
            "date": "2022-05-30T14:27:06",
            "name": "More misc Python patches",
            "version": 4,
            "mbox": "https://patchwork.libcamera.org/series/3149/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/16121/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/16121/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 A16B4C3278\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 30 May 2022 14:27:55 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 1C5FF65658;\n\tMon, 30 May 2022 16:27:55 +0200 (CEST)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 6A3DD6564E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 30 May 2022 16:27:46 +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 D7EDD6BD;\n\tMon, 30 May 2022 16:27:45 +0200 (CEST)"
        ],
        "DKIM-Signature": [
            "v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1653920875;\n\tbh=aKA4jstH6cyqJmG0C/jiNqrb9//1jSJbxG3xC5syilw=;\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=CJF1SYuh7DtJ1C7ppMd4TSVACJsJBkH1UvKW9wi35HHXCJGKD3vTSk5wbMR0cUAxH\n\tG0da889vCgmIKWRTlFJV5zaFONwBmRrQpf+ppc0C1fMf7Jek6OXwq4wSVEXHQ3gWPE\n\tvPEJbP+2CCUBovZpx1jvARrMWvsJN/LxT4bRFK4VpqGuR7WGbMAnk4HiQcqG9aCdJW\n\tG0YzQhZV6Nnb+r8K4J3hVy9S7FpndFnA95rcJjc2Bpb+xml/qjBg7lN1UBPGWLJ6Xk\n\t/cIJp52Qo9+Ra0HXK3asUTyL89iuqtIzlCwqGdJb34hw/c6byy5zSo4QeburWFu+1F\n\tIyOTkyAqN/+sA==",
            "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1653920866;\n\tbh=aKA4jstH6cyqJmG0C/jiNqrb9//1jSJbxG3xC5syilw=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=jNxXjMiMc0yzKjLOA+SvBspo25a6kX4OvcCAvVqaQA2orJUI+E3oxVrUGi5oftRq7\n\t2AOFf1ZM6vlPtMozP0UdTydnf8lDYnRYZYo1FAH47ICV1Y8SbVSwQ00R/x+kX4k6Ek\n\tk3KkpyXwzw7LPRyGIogk09xHa2mr+K/vN8UBPnes="
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"jNxXjMiM\"; 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": "Mon, 30 May 2022 17:27:20 +0300",
        "Message-Id": "<20220530142722.57618-15-tomi.valkeinen@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.34.1",
        "In-Reply-To": "<20220530142722.57618-1-tomi.valkeinen@ideasonboard.com>",
        "References": "<20220530142722.57618-1-tomi.valkeinen@ideasonboard.com>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v4 14/16] 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>\n---\n src/py/examples/simple-continuous-capture.py | 189 +++++++++++++++++++\n 1 file changed, 189 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..d0f8a7e9\n--- /dev/null\n+++ b/src/py/examples/simple-continuous-capture.py\n@@ -0,0 +1,189 @@\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+        # 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+        reqs = []\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+            reqs.append(req)\n+\n+        self.idx = idx\n+        self.cam = cam\n+        self.reqs = reqs\n+        self.mfbs = dict([(fb, libcamera.utils.MappedFrameBuffer(fb).mmap()) for fb in allocator.buffers(stream)])\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 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, 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 handle_key_event():\n+    sys.stdin.readline()\n+    print('Exiting...')\n+    return 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",
        "v4",
        "14/16"
    ]
}