From patchwork Thu Apr 25 16:03:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 19954 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 7338CC3220 for ; Thu, 25 Apr 2024 16:03:27 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 905B461B43; Thu, 25 Apr 2024 18:03:26 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=protonmail.com header.i=@protonmail.com header.b="lc9xcQ1d"; dkim-atps=neutral Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 653A461A9B for ; Thu, 25 Apr 2024 18:03:24 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1714061003; x=1714320203; bh=cBci5P9lqCSMjhbRtuANGmsj65JaAPeVZqEZrY9vfUw=; h=Date:To:From:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=lc9xcQ1dKvvCIZK3g+Mv+yK8o/KEAAZwpYgrfxH2GL2/5XdgXgUMWMYAjdFKthutX fohKnTNQR0kTxZZ6sU/pXCr7l7S6JTpjnqIjXJPRcZEvMwfLK+mMrMEn42IsbLfjCZ e8d5ku24CzPwavMeVe08juaCm2A62rOo/ikK4aQ3i8ABC0Lvjh4wC7BPUSSTBdqzSD C6R7UVP8leWhL6Vfi7pzN84P+9zzUDUPbkbzcSYzEuns59h2OZmE8f0GAuepxWgP1b MH8JtIOPYZ8wTI/AIxFoMta51CWvsdnATunHTwZ8pM1m8KXNcxLcaP5jR1CX4Z9ZzB WjSycEE30edGA== Date: Thu, 25 Apr 2024 16:03:19 +0000 To: libcamera-devel@lists.libcamera.org From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Subject: [PATCH v2] treewide: Query list of cameras just once Message-ID: <20240425160317.326458-1-pobrn@protonmail.com> Feedback-ID: 20568564:user:proton X-Pm-Message-ID: b3e1c77aaad357cfc2b27f2b2cea1cfd45751893 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. --- Documentation/guides/application-developer.rst | 11 ++++++----- src/apps/cam/camera_session.cpp | 6 ++++-- src/gstreamer/gstlibcamerasrc.cpp | 5 +++-- 3 files changed, 13 insertions(+), 9 deletions(-) base-commit: fb74bb7df66b96dbe28702155cddfc96a1b30f78 diff --git a/Documentation/guides/application-developer.rst b/Documentation/guides/application-developer.rst index 9a9905b1..e09ddfb1 100644 --- a/Documentation/guides/application-developer.rst +++ b/Documentation/guides/application-developer.rst @@ -116,20 +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(); /* - * Note that is equivalent to: - * camera = cm->cameras()[0]; + * Note that `camera` may be nullptr as the camera + * might have disappeared in the meantime. */ + auto camera = cm->get(cameraId); Once a camera has been selected an application needs to acquire an exclusive lock to it so no other application can use it. diff --git a/src/apps/cam/camera_session.cpp b/src/apps/cam/camera_session.cpp index 8447f932..5afd087f 100644 --- a/src/apps/cam/camera_session.cpp +++ b/src/apps/cam/camera_session.cpp @@ -39,8 +39,10 @@ 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]; + auto cameras = cm->cameras(); + + if (*endptr == '\0' && index > 0 && index <= cameras.size()) + camera_ = std::move(cameras[index - 1]); else camera_ = cm->get(cameraId); diff --git a/src/gstreamer/gstlibcamerasrc.cpp b/src/gstreamer/gstlibcamerasrc.cpp index f015c6d2..8613d66d 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 = std::move(cameras[0]); } GST_INFO_OBJECT(self, "Using camera '%s'", cam->id().c_str());