{"id":16418,"url":"https://patchwork.libcamera.org/api/1.1/patches/16418/?format=json","web_url":"https://patchwork.libcamera.org/patch/16418/","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":"<20220629070416.17550-13-tomi.valkeinen@ideasonboard.com>","date":"2022-06-29T07:04:14","name":"[libcamera-devel,v2,12/14] py: cam.py: Use new events support","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"f0c56aae5a5ffee7ed7723f97e61fa40e317919d","submitter":{"id":109,"url":"https://patchwork.libcamera.org/api/1.1/people/109/?format=json","name":"Tomi Valkeinen","email":"tomi.valkeinen@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/16418/mbox/","series":[{"id":3230,"url":"https://patchwork.libcamera.org/api/1.1/series/3230/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3230","date":"2022-06-29T07:04:02","name":"Python bindings event handling","version":2,"mbox":"https://patchwork.libcamera.org/series/3230/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/16418/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/16418/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 8182EBE173\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 29 Jun 2022 07:04:59 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 26A4765656;\n\tWed, 29 Jun 2022 09:04:59 +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 45FAA65649\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 29 Jun 2022 09:04:47 +0200 (CEST)","from deskari.lan (91-158-154-79.elisa-laajakaista.fi\n\t[91.158.154.79])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id ADBEE4A8;\n\tWed, 29 Jun 2022 09:04:46 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1656486299;\n\tbh=cDIG+D1ixMFp/dsesXD5Bu5CWYwPNvTWd8LiZXLOx7A=;\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=pV0RG5FKGmfzqdodBM/SmZWxRsm6xdq1XD5wpAmtXm5ncr3S7I04mJkD6HjWFa41v\n\tm1hUE0UrSIj8iDhrLIgCJyzzxKLxANrof9IKY6ZYRssMovifGGkwS3Mpj4j7sRO01k\n\tX15QE6Bkzx4JTX1nn978kpcB0zN+cxZuIb2CEKe8CPlnuKqg4GAmunax7AmgwimkyK\n\tRL9CQzm+GcI6Wrj41hFOYc/GlKAkBCDx5Fuasbc1OHpjjA4/LqdNUWPbF2BSTQkfug\n\t09WDVTM2juUeqQFWU8fSc3ne8su2zJYhgvCAKYDGQEj4+jYH1Ht5uXBlK6jkGtT9Lz\n\tJF+h/yGRApyyA==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1656486287;\n\tbh=cDIG+D1ixMFp/dsesXD5Bu5CWYwPNvTWd8LiZXLOx7A=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=uNLlaRcfuDqbWZj3mTokkSMMDXY5fgS1GTpy5Qa1S5HOezGsNfLtnqeOHSqpeWUbg\n\t/6mlK7WCD19ct8HdeqbmBGwSsEaQ2S4Y9qXBvvwmWcm1dEdwhomzOzfIhZcJNpwIOi\n\tUEda54zrbyvuoulrJ+onsLGGYAt6YsOQtWFA+CSU="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"uNLlaRcf\"; 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, 29 Jun 2022 10:04:14 +0300","Message-Id":"<20220629070416.17550-13-tomi.valkeinen@ideasonboard.com>","X-Mailer":"git-send-email 2.34.1","In-Reply-To":"<20220629070416.17550-1-tomi.valkeinen@ideasonboard.com>","References":"<20220629070416.17550-1-tomi.valkeinen@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 12/14] py: cam.py: Use new events\n\tsupport","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":"Convert cam.py to use the new event dispatching. In addition to handling\nthe request-completed event, handle also disconnect, camera-added and\ncamera-removed events (which only do a simple print).\n\nSigned-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/py/cam/cam.py | 33 +++++++++++++++++++++++++++------\n 1 file changed, 27 insertions(+), 6 deletions(-)","diff":"diff --git a/src/py/cam/cam.py b/src/py/cam/cam.py\nindex 6b6b678b..17a4268b 100755\n--- a/src/py/cam/cam.py\n+++ b/src/py/cam/cam.py\n@@ -230,11 +230,7 @@ class CaptureState:\n     # Called from renderer when there is a libcamera event\n     def event_handler(self):\n         try:\n-            reqs = self.cm.get_ready_requests()\n-\n-            for req in reqs:\n-                ctx = next(ctx for ctx in self.contexts if ctx.idx == req.cookie)\n-                self.__request_handler(ctx, req)\n+            self.cm.dispatch_events()\n \n             running = any(ctx.reqs_completed < ctx.opt_capture for ctx in self.contexts)\n             return running\n@@ -242,7 +238,9 @@ class CaptureState:\n             traceback.print_exc()\n             return False\n \n-    def __request_handler(self, ctx, req):\n+    def __request_handler(self, cam, req):\n+        ctx = next(ctx for ctx in self.contexts if ctx.camera == cam)\n+\n         if req.status != libcam.Request.Status.Complete:\n             raise Exception('{}: Request failed: {}'.format(ctx.id, req.status))\n \n@@ -297,6 +295,9 @@ class CaptureState:\n             ctx.camera.queue_request(req)\n             ctx.reqs_queued += 1\n \n+    def __disconnect_handler(self, cam):\n+        print(f'Camera {cam} disconnected')\n+\n     def __capture_init(self):\n         for ctx in self.contexts:\n             ctx.acquire()\n@@ -310,6 +311,15 @@ class CaptureState:\n         for ctx in self.contexts:\n             ctx.create_requests()\n \n+        for ctx in self.contexts:\n+            # These cause circular dependencies:\n+            # The callback in the camera points to CaptureState.\n+            # The CaptureState points to the camera.\n+            # This can be solved by setting the callbacks to None after stopping\n+            # or using lambdas here and weakref.WeakMethod.\n+            ctx.camera.request_completed = self.__request_handler\n+            ctx.camera.disconnected = self.__disconnect_handler\n+\n     def __capture_start(self):\n         for ctx in self.contexts:\n             ctx.start()\n@@ -321,6 +331,10 @@ class CaptureState:\n         for ctx in self.contexts:\n             ctx.stop()\n \n+        for ctx in self.contexts:\n+            ctx.camera.request_completed = None\n+            ctx.camera.disconnected = None\n+\n         for ctx in self.contexts:\n             ctx.release()\n \n@@ -389,6 +403,9 @@ def main():\n \n     cm = libcam.CameraManager.singleton()\n \n+    cm.camera_added = lambda c: print('Camera added:', c)\n+    cm.camera_removed = lambda c: print('Camera removed:', c)\n+\n     if args.list:\n         do_cmd_list(cm)\n \n@@ -447,6 +464,10 @@ def main():\n \n         state.do_cmd_capture()\n \n+        # This is not strictly needed, but it helps to do a proper cleanup and\n+        # so makes it easier to use memory leak detectors.\n+        cm.discard_events()\n+\n     return 0\n \n \n","prefixes":["libcamera-devel","v2","12/14"]}