summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2021-09-27 00:05:54 +0300
committerKieran Bingham <kieran.bingham@ideasonboard.com>2021-09-28 11:53:00 +0100
commit6cb63c923c49a8763ec99eb7a9285ef79558b8f2 (patch)
treed4467fc8805b3d3f66cd1342bd4dc56e08906b36
parent1f795410c0d99aaa15c8155d7d16ae6357487a96 (diff)
simple-cam: Move camera detection check to where it belongs
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@ideasonboard.com> Reviewed-by: Paul Elder <paul.elder@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
-rw-r--r--simple-cam.cpp17
1 files changed, 9 insertions, 8 deletions
diff --git a/simple-cam.cpp b/simple-cam.cpp
index 95e472b..1a60602 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