From patchwork Mon Apr 29 14:24:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 19964 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 B6AADBE08B for ; Mon, 29 Apr 2024 14:24:18 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B6BD763415; Mon, 29 Apr 2024 16:24:17 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=protonmail.com header.i=@protonmail.com header.b="N2oZMVzd"; dkim-atps=neutral Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 96529633F2 for ; Mon, 29 Apr 2024 16:24:15 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1714400654; x=1714659854; bh=9h0QJ7cHA/nEDNA8hhvlK76ajxV+OWSVbXwajNzrgWM=; h=Date:To:From:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=N2oZMVzdsrZ7fyGgQwVjEXxUW9LEN6r2/DygyfFyOSezribH6N3AMeipRPn6+e2oo 585JyTsl6duGZj3MihyzjzXjM1TR9DSc75NILZCHYM17fRtzorYDlSmZFwsUSWb8j2 AlMZfCidS9d6pcrxwq5bv+4ksFdLFZIcNnuxOIh4RhPHcW/qRQ2f6Sn2U6RusWvbB2 SJL5+/X/ihzqYG4DI9538YCc+SEGgeo2Xbce3WFzx/95dwtAX+aKrqbuu08B0V/WkT Z/gyWVH3FR9U1wh5EUxhFp5/1OQfO+Vzmot5Rdfbe52xlBF8ETIMYavlQaAEipBjqO tN1DU3lSqmqzg== Date: Mon, 29 Apr 2024 14:24:09 +0000 To: libcamera-devel@lists.libcamera.org From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Subject: [PATCH v3] treewide: Query list of cameras just once Message-ID: <20240429142406.59765-1-pobrn@protonmail.com> Feedback-ID: 20568564:user:proton X-Pm-Message-ID: 900f6db4446b275d500bc26e2477b3811ffe5640 MIME-Version: 1.0 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" This is more efficient since only a single vector will be constructed, and furthermore, it prevents the TOCTOU issue that might arise when the list of cameras changes between the two queries. Signed-off-by: Barnabás Pőcze Reviewed-by: Jacopo Mondi Reviewed-by: Kieran Bingham --- changes in v3 * drop std::move() from this change * extend note in documentation * limit scope of `cameras` in `CameraSession::CameraSession()` changes in v2 * fix code block in documentation * add comment noting that the camera may disappear --- Documentation/guides/application-developer.rst | 12 +++++++----- src/apps/cam/camera_session.cpp | 11 ++++++++--- src/gstreamer/gstlibcamerasrc.cpp | 5 +++-- 3 files changed, 18 insertions(+), 10 deletions(-) base-commit: fb74bb7df66b96dbe28702155cddfc96a1b30f78 -- 2.44.0 diff --git a/Documentation/guides/application-developer.rst b/Documentation/guides/application-developer.rst index 9a9905b1..92e2a373 100644 --- a/Documentation/guides/application-developer.rst +++ b/Documentation/guides/application-developer.rst @@ -116,19 +116,21 @@ available. .. code:: cpp - if (cm->cameras().empty()) { + auto cameras = cm->cameras(); + if (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); + std::string cameraId = cameras[0]->id(); + auto camera = cm->get(cameraId); /* - * Note that is equivalent to: - * camera = cm->cameras()[0]; + * Note that `camera` may not compare equal to `cameras[0]`. + * In fact, it might simply be a `nullptr`, as the particular + * device might have disappeared (and reappeared) in the meantime. */ Once a camera has been selected an application needs to acquire an exclusive diff --git a/src/apps/cam/camera_session.cpp b/src/apps/cam/camera_session.cpp index 8447f932..334d2ed8 100644 --- a/src/apps/cam/camera_session.cpp +++ b/src/apps/cam/camera_session.cpp @@ -39,9 +39,14 @@ CameraSession::CameraSession(CameraManager *cm, { char *endptr; unsigned long index = strtoul(cameraId.c_str(), &endptr, 10); - if (*endptr == '\0' && index > 0 && index <= cm->cameras().size()) - camera_ = cm->cameras()[index - 1]; - else + + if (*endptr == '\0' && index > 0) { + auto cameras = cm->cameras(); + if (index <= cameras.size()) + camera_ = cameras[index - 1]; + } + + if (!camera_) camera_ = cm->get(cameraId); if (!camera_) { diff --git a/src/gstreamer/gstlibcamerasrc.cpp b/src/gstreamer/gstlibcamerasrc.cpp index f015c6d2..4eddfa3e 100644 --- a/src/gstreamer/gstlibcamerasrc.cpp +++ b/src/gstreamer/gstlibcamerasrc.cpp @@ -385,13 +385,14 @@ gst_libcamera_src_open(GstLibcameraSrc *self) return false; } } else { - if (cm->cameras().empty()) { + auto cameras = cm->cameras(); + if (cameras.empty()) { GST_ELEMENT_ERROR(self, RESOURCE, NOT_FOUND, ("Could not find any supported camera on this system."), ("libcamera::CameraMananger::cameras() is empty")); return false; } - cam = cm->cameras()[0]; + cam = cameras[0]; } GST_INFO_OBJECT(self, "Using camera '%s'", cam->id().c_str());