From patchwork Fri Jul 1 08:45:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 16503 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 BECF8BD808 for ; Fri, 1 Jul 2022 08:45:55 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 86C7365650; Fri, 1 Jul 2022 10:45:55 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1656665155; bh=v0U9bNrGi3HWYfGUSCLofRQkPLlgOPEtaHgIn/WwQb8=; 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=id/mCrqJ1NxImEI39n68OSAjMiV35D4jSExQ7v25ZdTVUwAt2MbfrNvt+pWwNw0l6 3S2z2Rj5XFpC2OrGDIWHmjPuovO+uq8KgXKoZllQVCH5ql+Iil5w4tiTVia5rlfIbU scXkRz5xjZKmoPdJEbkIimgpT2hN0BU5aMRWA0V4g/yMVl4c/+9HPOrN2vbRZPdP3Z MK1pLHFHlLqUB+VFqxzAnchadr8nQib4f0YZuVw8tkGkPjVP5qgF6o6ck2iqlQ7Apd a/ItgIvkAEt/AXWgrQXu/Wfz7QqdqOb3qU1vv4sM53JnHOs6uDrrBn1mZy4ztSn1LO G38Kh3OwFG/EA== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6DBA96565C for ; Fri, 1 Jul 2022 10:45:46 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="hvgjkFTx"; 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 E6A6F6BB; Fri, 1 Jul 2022 10:45:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1656665146; bh=v0U9bNrGi3HWYfGUSCLofRQkPLlgOPEtaHgIn/WwQb8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hvgjkFTxoY7D7NaVhUScMX03FM143nxoeWjfTHPPSoQtJkbMMGnA6jjs9Pr7bTXbK xp96p6z3r1mnFTrvgG+pmCkUYOegPr71+C7+GAhGhw3k+WMxdjyL7ZzWfvyqDd5pIG TiFfoYzj+qSw0GRBz+IYi0rRaWKLbL1Ct1zv/JOc= To: libcamera-devel@lists.libcamera.org, David Plowman , Kieran Bingham , Laurent Pinchart , Jacopo Mondi Date: Fri, 1 Jul 2022 11:45:16 +0300 Message-Id: <20220701084521.31831-13-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220701084521.31831-1-tomi.valkeinen@ideasonboard.com> References: <20220701084521.31831-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 12/17] 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: Jacopo Mondi --- 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 6b6b678b..f19bad57 100755 --- a/src/py/cam/cam.py +++ b/src/py/cam/cam.py @@ -230,11 +230,20 @@ 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) + evs = self.cm.get_events() + for ev in evs: + type = ev.type + + if type == libcam.Event.Type.Undefined: + raise RuntimeError("Bad event type") + elif type == libcam.Event.Type.CameraAdded: + print('Camera added:', ev.camera) + elif type == libcam.Event.Type.CameraRemoved: + print('Camera added:', ev.camera) + elif type == libcam.Event.Type.Disconnect: + self.__disconnect_handler(ev.camera) + elif type == libcam.Event.Type.RequestCompleted: + self.__request_handler(ev.camera, ev.request) running = any(ctx.reqs_completed < ctx.opt_capture for ctx in self.contexts) return running @@ -242,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)) @@ -297,6 +308,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() @@ -447,6 +461,11 @@ def main(): state.do_cmd_capture() + # This is not strictly needed, but it helps to do a proper cleanup as we + # drop any unhandled events, and so makes it easier to use memory leak + # detectors. + cm.get_events() + return 0