[libcamera-devel] [PATCH] simple-cam: Move camera detection check to where it belongs

Laurent Pinchart laurent.pinchart at ideasonboard.com
Sun Sep 26 23:05:54 CEST 2021


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 <laurent.pinchart at ideasonboard.com>
---
 simple-cam.cpp | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

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> &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

base-commit: 1f795410c0d99aaa15c8155d7d16ae6357487a96
prerequisite-patch-id: a9984c79d9b952f4004a670f8828600af70d732e
prerequisite-patch-id: 8dcf991243c61f7931da9f18ed5acd4920ea32c5
-- 
Regards,

Laurent Pinchart



More information about the libcamera-devel mailing list