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

Message ID 20210926210554.20901-1-laurent.pinchart@ideasonboard.com
State Accepted
Headers show
Series
  • [libcamera-devel] simple-cam: Move camera detection check to where it belongs
Related show

Commit Message

Laurent Pinchart Sept. 26, 2021, 9:05 p.m. UTC
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>
---
 simple-cam.cpp | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)


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

Comments

Paul Elder Sept. 27, 2021, 6:55 a.m. UTC | #1
Hi Laurent,

On Mon, Sep 27, 2021 at 12:05:54AM +0300, Laurent Pinchart wrote:
> 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>

> ---
>  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
>
Kieran Bingham Sept. 28, 2021, 10:35 a.m. UTC | #2
On Mon, Sep 27, 2021 at 03:55:09PM +0900, paul.elder@ideasonboard.com wrote:
> Hi Laurent,
> 
> On Mon, Sep 27, 2021 at 12:05:54AM +0300, Laurent Pinchart wrote:
> > 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>

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

Patch
diff mbox series

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