From patchwork Thu Jun 23 14:47:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 16345 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 B8126BD808 for ; Thu, 23 Jun 2022 14:48:08 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 71F3665637; Thu, 23 Jun 2022 16:48:08 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1655995688; bh=kIO3WgRrLWjCEOq4w3LCDR5Kbdy/9I5vKkQ+ah1/vwA=; 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=iikUDItzd5xTmucTFvJYdpqnppmBvwFEUReHMReEB/msjS8n0qWpMuM1MAf4d99Bb cqYeSMEnBASQ0jU3J/lxfsqG9LtIVbnbOwJc0zvhPNPEJNlyuRxsj2xvrvnFq+C0r+ EpCRU6T7ubRPjT+q+WMuLbSDslnt3UQoaD+wLvmKrfYb95wvaytKwKgxJ09B97dWTi c3+ry6BSRmgJGdM6Bpl3T+HfAqNNRgdwd5mUx5eDwtMFJczsr6NmG2b0eHf4unbTmG GLQeVPm9O0SoRgNhQoGLNq+yuC4hXVgZKbwqHsCamerc5V5QDgh+qnIu39tjYL4JlY uw23j2swCYnFA== 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 21E886563D for ; Thu, 23 Jun 2022 16:48:01 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="h9Ms2c4+"; 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 8AC2DDD; Thu, 23 Jun 2022 16:48:00 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1655995680; bh=kIO3WgRrLWjCEOq4w3LCDR5Kbdy/9I5vKkQ+ah1/vwA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=h9Ms2c4+E88kZ+1Dy1VIsq14Q+GUhfhg5+959GFcWxa3Wtu49nDlMDB8lgne0utUY EGfDqysGq2tAWleR5U2qNxOq78uSFdZ8nFtiAGbDrnusKH5W58ru8xGFwva28ris1d cm4qhWXw3nAaAg7xyrCk5MI0ynwU/UaeseL/siRU= To: libcamera-devel@lists.libcamera.org, David Plowman , Kieran Bingham , Laurent Pinchart , Jacopo Mondi Date: Thu, 23 Jun 2022 17:47:36 +0300 Message-Id: <20220623144736.78537-8-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220623144736.78537-1-tomi.valkeinen@ideasonboard.com> References: <20220623144736.78537-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC v1 7/7] 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" Signed-off-by: Tomi Valkeinen Reviewed-by: Laurent Pinchart --- src/py/cam/cam.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/py/cam/cam.py b/src/py/cam/cam.py index 733e9ae5..538f0c1d 100755 --- a/src/py/cam/cam.py +++ b/src/py/cam/cam.py @@ -243,11 +243,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 @@ -255,7 +251,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)) @@ -310,6 +308,9 @@ class CaptureState: ctx.camera.queue_request(req) ctx.reqs_queued += 1 + def __disconnect_handler(self, cam): + print('Camera', cam, 'disconnected') + def __capture_init(self): for ctx in self.contexts: ctx.acquire() @@ -323,6 +324,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() @@ -334,6 +344,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() @@ -402,6 +416,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) @@ -459,6 +476,8 @@ def main(): state.do_cmd_capture() + cm.discard_events() + return 0