From patchwork Wed Jun 29 07:04:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 16418 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 8182EBE173 for ; Wed, 29 Jun 2022 07:04:59 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 26A4765656; Wed, 29 Jun 2022 09:04:59 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1656486299; bh=cDIG+D1ixMFp/dsesXD5Bu5CWYwPNvTWd8LiZXLOx7A=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=pV0RG5FKGmfzqdodBM/SmZWxRsm6xdq1XD5wpAmtXm5ncr3S7I04mJkD6HjWFa41v m1hUE0UrSIj8iDhrLIgCJyzzxKLxANrof9IKY6ZYRssMovifGGkwS3Mpj4j7sRO01k X15QE6Bkzx4JTX1nn978kpcB0zN+cxZuIb2CEKe8CPlnuKqg4GAmunax7AmgwimkyK RL9CQzm+GcI6Wrj41hFOYc/GlKAkBCDx5Fuasbc1OHpjjA4/LqdNUWPbF2BSTQkfug 09WDVTM2juUeqQFWU8fSc3ne8su2zJYhgvCAKYDGQEj4+jYH1Ht5uXBlK6jkGtT9Lz JF+h/yGRApyyA== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 45FAA65649 for ; Wed, 29 Jun 2022 09:04:47 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="uNLlaRcf"; dkim-atps=neutral Received: from deskari.lan (91-158-154-79.elisa-laajakaista.fi [91.158.154.79]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id ADBEE4A8; Wed, 29 Jun 2022 09:04:46 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1656486287; bh=cDIG+D1ixMFp/dsesXD5Bu5CWYwPNvTWd8LiZXLOx7A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uNLlaRcfuDqbWZj3mTokkSMMDXY5fgS1GTpy5Qa1S5HOezGsNfLtnqeOHSqpeWUbg /6mlK7WCD19ct8HdeqbmBGwSsEaQ2S4Y9qXBvvwmWcm1dEdwhomzOzfIhZcJNpwIOi UEda54zrbyvuoulrJ+onsLGGYAt6YsOQtWFA+CSU= To: libcamera-devel@lists.libcamera.org, David Plowman , Kieran Bingham , Laurent Pinchart , Jacopo Mondi 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 Subject: [libcamera-devel] [PATCH v2 12/14] py: cam.py: Use new events support X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tomi Valkeinen via libcamera-devel From: Tomi Valkeinen Reply-To: Tomi Valkeinen Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Convert cam.py to use the new event dispatching. In addition to handling the request-completed event, handle also disconnect, camera-added and camera-removed events (which only do a simple print). Signed-off-by: Tomi Valkeinen Reviewed-by: Laurent Pinchart --- src/py/cam/cam.py | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/py/cam/cam.py b/src/py/cam/cam.py index 6b6b678b..17a4268b 100755 --- a/src/py/cam/cam.py +++ b/src/py/cam/cam.py @@ -230,11 +230,7 @@ class CaptureState: # Called from renderer when there is a libcamera event def event_handler(self): try: - reqs = self.cm.get_ready_requests() - - for req in reqs: - ctx = next(ctx for ctx in self.contexts if ctx.idx == req.cookie) - self.__request_handler(ctx, req) + self.cm.dispatch_events() running = any(ctx.reqs_completed < ctx.opt_capture for ctx in self.contexts) return running @@ -242,7 +238,9 @@ class CaptureState: traceback.print_exc() return False - def __request_handler(self, ctx, req): + def __request_handler(self, cam, req): + ctx = next(ctx for ctx in self.contexts if ctx.camera == cam) + if req.status != libcam.Request.Status.Complete: raise Exception('{}: Request failed: {}'.format(ctx.id, req.status)) @@ -297,6 +295,9 @@ class CaptureState: ctx.camera.queue_request(req) ctx.reqs_queued += 1 + def __disconnect_handler(self, cam): + print(f'Camera {cam} disconnected') + def __capture_init(self): for ctx in self.contexts: ctx.acquire() @@ -310,6 +311,15 @@ class CaptureState: for ctx in self.contexts: ctx.create_requests() + for ctx in self.contexts: + # These cause circular dependencies: + # The callback in the camera points to CaptureState. + # The CaptureState points to the camera. + # This can be solved by setting the callbacks to None after stopping + # or using lambdas here and weakref.WeakMethod. + ctx.camera.request_completed = self.__request_handler + ctx.camera.disconnected = self.__disconnect_handler + def __capture_start(self): for ctx in self.contexts: ctx.start() @@ -321,6 +331,10 @@ class CaptureState: for ctx in self.contexts: ctx.stop() + for ctx in self.contexts: + ctx.camera.request_completed = None + ctx.camera.disconnected = None + for ctx in self.contexts: ctx.release() @@ -389,6 +403,9 @@ def main(): cm = libcam.CameraManager.singleton() + cm.camera_added = lambda c: print('Camera added:', c) + cm.camera_removed = lambda c: print('Camera removed:', c) + if args.list: do_cmd_list(cm) @@ -447,6 +464,10 @@ def main(): state.do_cmd_capture() + # This is not strictly needed, but it helps to do a proper cleanup and + # so makes it easier to use memory leak detectors. + cm.discard_events() + return 0