From patchwork Thu Mar 9 14:25:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 18364 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 18952BDE17 for ; Thu, 9 Mar 2023 14:26:24 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 87F00626E1; Thu, 9 Mar 2023 15:26:19 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1678371979; bh=FUnyVT+SxtKHp4nPrOfxAgg6Mxeo3KgPtH5RKskTD0E=; 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=Pu3C0GFkKSlYi3nQdPZ2I6lQmINzjTwSa3wy3sxZnnlEt4S1TBoLxnqxwyWdi6TBs +emM1ONGv/gZsoFC+rjHCJundpxfegcQF+Kdjo87hUcycE+N45A3fzGuHX5XbavQdv bYahprzivATB1d9f09E5oCJyrZdSmevSBlbzWz8Q3fpnC8ADsef4IXDHTFCwjJTzoi d9iOKta37pvEe72Z0ucGs7SEfc8lnZ7oPB01LCFvXkLssxvaqiP5CK6YTrvTgcYV1L oNFb2J4bTxvnr7aAY+t7yl1ilJnXIaZLdtia8wFKL1l3EXPxu0AcnQs8BilsvQsdYu ZxxacsjDWH0wA== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 756AA626D5 for ; Thu, 9 Mar 2023 15:26:17 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="ih7Me3KS"; dkim-atps=neutral Received: from desky.lan (91-154-32-225.elisa-laajakaista.fi [91.154.32.225]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id F1EBF886; Thu, 9 Mar 2023 15:26:16 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1678371977; bh=FUnyVT+SxtKHp4nPrOfxAgg6Mxeo3KgPtH5RKskTD0E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ih7Me3KSsJQDEWzckjzW04xtAVRIrMpiXNIpZkPyofy819AB1+mmkV+MMTs/3WB/S +c2AZU2U06b/cZ70juAQmNl0ob7wGFAqNaedBp1n8ytR1qAzy0j4d5ek6LIIaRUBWH BwoXRjovp2Sg1tWocbpmotvA+tLh+6ZjF28XyW1I= To: libcamera-devel@lists.libcamera.org Date: Thu, 9 Mar 2023 16:25:49 +0200 Message-Id: <20230309142601.70556-4-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230309142601.70556-1-tomi.valkeinen@ideasonboard.com> References: <20230309142601.70556-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 03/15] 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 Reviewed-by: Laurent Pinchart --- src/py/cam/cam.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/py/cam/cam.py b/src/py/cam/cam.py index a2a115c1..0b464256 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.CameraAdded: + print('Camera added:', ev.camera) + elif type == libcam.Event.Type.CameraRemoved: + print('Camera removed:', ev.camera) + elif type == libcam.Event.Type.Disconnect: + print(f'Camera {ev.camera} disconnected') + elif type == libcam.Event.Type.RequestCompleted: + self.__request_handler(ev.camera, ev.request) + else: + raise RuntimeError("Bad event type") 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)) @@ -447,6 +458,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