Patch Detail
Show a patch.
GET /api/patches/19974/?format=api
{ "id": 19974, "url": "https://patchwork.libcamera.org/api/patches/19974/?format=api", "web_url": "https://patchwork.libcamera.org/patch/19974/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20240430192543.779374-1-pobrn@protonmail.com>", "date": "2024-04-30T19:25:44", "name": "[v3] treewide: Query list of cameras just once", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "0c56d8cd2686d0ab36a347cf1ee78861286a125b", "submitter": { "id": 133, "url": "https://patchwork.libcamera.org/api/people/133/?format=api", "name": "Pőcze Barnabás", "email": "pobrn@protonmail.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/19974/mbox/", "series": [ { "id": 4281, "url": "https://patchwork.libcamera.org/api/series/4281/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4281", "date": "2024-04-30T19:25:44", "name": "[v3] treewide: Query list of cameras just once", "version": 3, "mbox": "https://patchwork.libcamera.org/series/4281/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/19974/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/19974/checks/", "tags": {}, "headers": { "Return-Path": "<libcamera-devel-bounces@lists.libcamera.org>", "X-Original-To": "parsemail@patchwork.libcamera.org", "Delivered-To": "parsemail@patchwork.libcamera.org", "Received": [ "from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id BE050BE08B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 30 Apr 2024 19:25:55 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 83D6D63418;\n\tTue, 30 Apr 2024 21:25:54 +0200 (CEST)", "from mail-4322.protonmail.ch (mail-4322.protonmail.ch\n\t[185.70.43.22])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E766C633F3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 30 Apr 2024 21:25:51 +0200 (CEST)" ], "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (2048-bit key;\n\tunprotected) header.d=protonmail.com header.i=@protonmail.com\n\theader.b=\"nYi2VCKQ\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com;\n\ts=protonmail3; t=1714505151; x=1714764351;\n\tbh=9h0QJ7cHA/nEDNA8hhvlK76ajxV+OWSVbXwajNzrgWM=;\n\th=Date:To:From:Subject:Message-ID:Feedback-ID:From:To:Cc:Date:\n\tSubject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector;\n\tb=nYi2VCKQAG/FsdWiO0XfJ12frr1+rZizA6Dn30QtLILCnDvu4SHrfbKt93WTnXixd\n\tSwSYB5s7B429ry1R9djYbJ9rphxXuJ5WkKvV/4kiB85qtjtCK5hg3uMP2MjRaBxLiL\n\tNuO0tiGSLeROQBdcJ3hnrl45pSnCZbOehoTBvUSfChakyp4x9RiZOCoajtIzQrcNh1\n\tsGHvcySmvdu7yAstLoR+Igfa7h0lnrLB8wckfjbJ7nEGdpyaUIPDI/Rv2sfnR34yUq\n\tqRXU3AMAUsRH/Tf67WRFITpKp+gfJEB3V1l8zRakcQbJ+yiLMRcOES2yUW1eqito6i\n\tyrGLMAhCpE+Fw==", "Date": "Tue, 30 Apr 2024 19:25:44 +0000", "To": "libcamera-devel@lists.libcamera.org", "From": "=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>", "Subject": "[PATCH v3] treewide: Query list of cameras just once", "Message-ID": "<20240430192543.779374-1-pobrn@protonmail.com>", "Feedback-ID": "20568564:user:proton", "X-Pm-Message-ID": "765263e160121b5552228141aac16c16cb2b4942", "MIME-Version": "1.0", "Content-Type": "text/plain; charset=utf-8", "Content-Transfer-Encoding": "quoted-printable", "X-BeenThere": "libcamera-devel@lists.libcamera.org", "X-Mailman-Version": "2.1.29", "Precedence": "list", "List-Id": "<libcamera-devel.lists.libcamera.org>", "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>", "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>", "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>", "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>", "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>", "Errors-To": "libcamera-devel-bounces@lists.libcamera.org", "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>" }, "content": "This is more efficient since only a single vector will be constructed,\nand furthermore, it prevents the TOCTOU issue that might arise when\nthe list of cameras changes between the two queries.\n\nSigned-off-by: Barnabás Pőcze <pobrn@protonmail.com>\n---\n\nchanges in v3\n * drop std::move() from this change\n * extend note in documentation\n * limit scope of `cameras` in `CameraSession::CameraSession()`\n\nchanges in v2\n * fix code block in documentation\n * add comment noting that the camera may disappear\n\n---\n Documentation/guides/application-developer.rst | 12 +++++++-----\n src/apps/cam/camera_session.cpp | 11 ++++++++---\n src/gstreamer/gstlibcamerasrc.cpp | 5 +++--\n 3 files changed, 18 insertions(+), 10 deletions(-)\n\n\nbase-commit: fb74bb7df66b96dbe28702155cddfc96a1b30f78\n--\n2.44.0", "diff": "diff --git a/Documentation/guides/application-developer.rst b/Documentation/guides/application-developer.rst\nindex 9a9905b1..92e2a373 100644\n--- a/Documentation/guides/application-developer.rst\n+++ b/Documentation/guides/application-developer.rst\n@@ -116,19 +116,21 @@ available.\n\n .. code:: cpp\n\n- if (cm->cameras().empty()) {\n+ auto cameras = cm->cameras();\n+ if (cameras.empty()) {\n std::cout << \"No cameras were identified on the system.\"\n << std::endl;\n cm->stop();\n return EXIT_FAILURE;\n }\n\n- std::string cameraId = cm->cameras()[0]->id();\n- camera = cm->get(cameraId);\n+ std::string cameraId = cameras[0]->id();\n\n+ auto camera = cm->get(cameraId);\n /*\n- * Note that is equivalent to:\n- * camera = cm->cameras()[0];\n+ * Note that `camera` may not compare equal to `cameras[0]`.\n+ * In fact, it might simply be a `nullptr`, as the particular\n+ * device might have disappeared (and reappeared) in the meantime.\n */\n\n Once a camera has been selected an application needs to acquire an exclusive\ndiff --git a/src/apps/cam/camera_session.cpp b/src/apps/cam/camera_session.cpp\nindex 8447f932..334d2ed8 100644\n--- a/src/apps/cam/camera_session.cpp\n+++ b/src/apps/cam/camera_session.cpp\n@@ -39,9 +39,14 @@ CameraSession::CameraSession(CameraManager *cm,\n {\n \tchar *endptr;\n \tunsigned long index = strtoul(cameraId.c_str(), &endptr, 10);\n-\tif (*endptr == '\\0' && index > 0 && index <= cm->cameras().size())\n-\t\tcamera_ = cm->cameras()[index - 1];\n-\telse\n+\n+\tif (*endptr == '\\0' && index > 0) {\n+\t\tauto cameras = cm->cameras();\n+\t\tif (index <= cameras.size())\n+\t\t\tcamera_ = cameras[index - 1];\n+\t}\n+\n+\tif (!camera_)\n \t\tcamera_ = cm->get(cameraId);\n\n \tif (!camera_) {\ndiff --git a/src/gstreamer/gstlibcamerasrc.cpp b/src/gstreamer/gstlibcamerasrc.cpp\nindex f015c6d2..4eddfa3e 100644\n--- a/src/gstreamer/gstlibcamerasrc.cpp\n+++ b/src/gstreamer/gstlibcamerasrc.cpp\n@@ -385,13 +385,14 @@ gst_libcamera_src_open(GstLibcameraSrc *self)\n \t\t\treturn false;\n \t\t}\n \t} else {\n-\t\tif (cm->cameras().empty()) {\n+\t\tauto cameras = cm->cameras();\n+\t\tif (cameras.empty()) {\n \t\t\tGST_ELEMENT_ERROR(self, RESOURCE, NOT_FOUND,\n \t\t\t\t\t (\"Could not find any supported camera on this system.\"),\n \t\t\t\t\t (\"libcamera::CameraMananger::cameras() is empty\"));\n \t\t\treturn false;\n \t\t}\n-\t\tcam = cm->cameras()[0];\n+\t\tcam = cameras[0];\n \t}\n\n \tGST_INFO_OBJECT(self, \"Using camera '%s'\", cam->id().c_str());\n", "prefixes": [ "v3" ] }