From patchwork Sun Sep 26 21:05:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 13939 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 92B8CBF01C for ; Sun, 26 Sep 2021 21:06:05 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id ABD4A6918E; Sun, 26 Sep 2021 23:06:04 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="wpmBE/7Z"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id CFB366012D for ; Sun, 26 Sep 2021 23:06:02 +0200 (CEST) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 58707EF for ; Sun, 26 Sep 2021 23:06:02 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1632690362; bh=aKeMvpk3MlFdgNyFyuivJuHzdbbZ5ipLmzU+SCxyAHc=; h=From:To:Subject:Date:From; b=wpmBE/7ZBb3H8jiFzYbB4k6fNj7s6WhZA0xcgah75M10SZAcyolGlfit7rDeOYHuh AKRNAxqpKtJrbeYUXyP7aPkIHbBylwO3oH7/1e9G2Szxanfjxlt3WgHj4qZjepQmtK 7vT4hMP5jBTKZ8AhFcWrqrtVYyeXA6sWqCMf1790= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Mon, 27 Sep 2021 00:05:54 +0300 Message-Id: <20210926210554.20901-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH] simple-cam: Move camera detection check to where it belongs 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: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Commit 14529b6d1c4a ("simple-cam: Early return if no cameras are found on the system") added a check to ensure that at least one camera is present in the system to avoid a crash otherwise. The check was however placed towards the end of the main() function, way after the camera is accessed, possibly due to a rebase conflict. Move it before accessing the first camera. While at it, replace usage of std::vector::size() with std::vector::empty() which expresses the intent better. Fixes: 14529b6d1c4a ("simple-cam: Early return if no cameras are found on the system") Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder Reviewed-by: Kieran Bingham --- simple-cam.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) base-commit: 1f795410c0d99aaa15c8155d7d16ae6357487a96 prerequisite-patch-id: a9984c79d9b952f4004a670f8828600af70d732e prerequisite-patch-id: 8dcf991243c61f7931da9f18ed5acd4920ea32c5 diff --git a/simple-cam.cpp b/simple-cam.cpp index e374e45849dc..71a715a0c27d 100644 --- a/simple-cam.cpp +++ b/simple-cam.cpp @@ -163,13 +163,21 @@ int main() * Application lock usage of Camera by 'acquiring' them. * Once done with it, application shall similarly 'release' the Camera. * - * As an example, use the first available camera in the system. + * As an example, use the first available camera in the system after + * making sure that at least one camera is available. * * Cameras can be obtained by their ID or their index, to demonstrate * this, the following code gets the ID of the first camera; then gets * the camera associated with that ID (which is of course the same as * cm->cameras()[0]). */ + if (cm->cameras().empty()) { + std::cout << "No cameras were identified on the system." + << std::endl; + cm->stop(); + return EXIT_FAILURE; + } + std::string cameraId = cm->cameras()[0]->id(); camera = cm->get(cameraId); camera->acquire(); @@ -386,13 +394,6 @@ int main() for (std::unique_ptr &request : requests) camera->queueRequest(request.get()); - if (!cm->cameras().size()) { - std::cout << "No cameras were identified on the system." - << std::endl; - cm->stop(); - return EXIT_FAILURE; - } - /* * -------------------------------------------------------------------- * Run an EventLoop