[libcamera-devel,23/30] cam: Move camera session creation and monitoring setup to run()
diff mbox series

Message ID 20210707021941.20804-24-laurent.pinchart@ideasonboard.com
State Superseded
Headers show
Series
  • Multi-camera support in the cam application
Related show

Commit Message

Laurent Pinchart July 7, 2021, 2:19 a.m. UTC
Move creationg of the camera session and setup of the hotplug monitoring
from the init() function to the run() function, to group all the code
that performs operations based on command line options in a single
place. The cleanup() call on session creation failure isn't required
anymore, as the cleanup() function is called unconditionally open return
from run().

This change allows merging two different code blocks related to hotplug
monitoring.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/cam/main.cpp | 38 ++++++++++++++++++--------------------
 1 file changed, 18 insertions(+), 20 deletions(-)

Comments

Kieran Bingham July 12, 2021, 3:53 p.m. UTC | #1
On 07/07/2021 03:19, Laurent Pinchart wrote:
> Move creationg of the camera session and setup of the hotplug monitoring

s/creationg/creation/

> from the init() function to the run() function, to group all the code
> that performs operations based on command line options in a single
> place. The cleanup() call on session creation failure isn't required
> anymore, as the cleanup() function is called unconditionally open return

s/open/upon/

> from run().
> 
> This change allows merging two different code blocks related to hotplug
> monitoring.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> ---
>  src/cam/main.cpp | 38 ++++++++++++++++++--------------------
>  1 file changed, 18 insertions(+), 20 deletions(-)
> 
> diff --git a/src/cam/main.cpp b/src/cam/main.cpp
> index 03e62f305f24..cba0793ac39b 100644
> --- a/src/cam/main.cpp
> +++ b/src/cam/main.cpp
> @@ -84,26 +84,6 @@ int CamApp::init(int argc, char **argv)
>  		return ret;
>  	}
>  
> -	if (options_.isSet(OptCamera)) {
> -		session_ = std::make_unique<CameraSession>(cm_.get(), options_);
> -		if (!session_->isValid()) {
> -			std::cout << "Failed to create camera session" << std::endl;
> -			cleanup();
> -			return -EINVAL;
> -		}
> -
> -		std::cout << "Using camera " << session_->camera()->id()
> -			  << std::endl;
> -
> -		session_->captureDone.connect(this, &CamApp::captureDone);
> -	}
> -
> -	if (options_.isSet(OptMonitor)) {
> -		cm_->cameraAdded.connect(this, &CamApp::cameraAdded);
> -		cm_->cameraRemoved.connect(this, &CamApp::cameraRemoved);
> -		std::cout << "Monitoring new hotplug and unplug events" << std::endl;
> -	}
> -
>  	return 0;
>  }
>  
> @@ -275,6 +255,19 @@ int CamApp::run()
>  		}
>  	}
>  
> +	if (options_.isSet(OptCamera)) {
> +		session_ = std::make_unique<CameraSession>(cm_.get(), options_);
> +		if (!session_->isValid()) {
> +			std::cout << "Failed to create camera session" << std::endl;
> +			return -EINVAL;
> +		}
> +
> +		std::cout << "Using camera " << session_->camera()->id()
> +			  << std::endl;
> +
> +		session_->captureDone.connect(this, &CamApp::captureDone);
> +	}
> +
>  	if (options_.isSet(OptListControls)) {
>  		ret = listControls();
>  		if (ret)
> @@ -312,7 +305,12 @@ int CamApp::run()
>  	}
>  
>  	if (options_.isSet(OptMonitor)) {
> +		std::cout << "Monitoring new hotplug and unplug events" << std::endl;
>  		std::cout << "Press Ctrl-C to interrupt" << std::endl;
> +
> +		cm_->cameraAdded.connect(this, &CamApp::cameraAdded);
> +		cm_->cameraRemoved.connect(this, &CamApp::cameraRemoved);
> +
>  		loop_.exec();
>  	}
>  
>

Patch
diff mbox series

diff --git a/src/cam/main.cpp b/src/cam/main.cpp
index 03e62f305f24..cba0793ac39b 100644
--- a/src/cam/main.cpp
+++ b/src/cam/main.cpp
@@ -84,26 +84,6 @@  int CamApp::init(int argc, char **argv)
 		return ret;
 	}
 
-	if (options_.isSet(OptCamera)) {
-		session_ = std::make_unique<CameraSession>(cm_.get(), options_);
-		if (!session_->isValid()) {
-			std::cout << "Failed to create camera session" << std::endl;
-			cleanup();
-			return -EINVAL;
-		}
-
-		std::cout << "Using camera " << session_->camera()->id()
-			  << std::endl;
-
-		session_->captureDone.connect(this, &CamApp::captureDone);
-	}
-
-	if (options_.isSet(OptMonitor)) {
-		cm_->cameraAdded.connect(this, &CamApp::cameraAdded);
-		cm_->cameraRemoved.connect(this, &CamApp::cameraRemoved);
-		std::cout << "Monitoring new hotplug and unplug events" << std::endl;
-	}
-
 	return 0;
 }
 
@@ -275,6 +255,19 @@  int CamApp::run()
 		}
 	}
 
+	if (options_.isSet(OptCamera)) {
+		session_ = std::make_unique<CameraSession>(cm_.get(), options_);
+		if (!session_->isValid()) {
+			std::cout << "Failed to create camera session" << std::endl;
+			return -EINVAL;
+		}
+
+		std::cout << "Using camera " << session_->camera()->id()
+			  << std::endl;
+
+		session_->captureDone.connect(this, &CamApp::captureDone);
+	}
+
 	if (options_.isSet(OptListControls)) {
 		ret = listControls();
 		if (ret)
@@ -312,7 +305,12 @@  int CamApp::run()
 	}
 
 	if (options_.isSet(OptMonitor)) {
+		std::cout << "Monitoring new hotplug and unplug events" << std::endl;
 		std::cout << "Press Ctrl-C to interrupt" << std::endl;
+
+		cm_->cameraAdded.connect(this, &CamApp::cameraAdded);
+		cm_->cameraRemoved.connect(this, &CamApp::cameraRemoved);
+
 		loop_.exec();
 	}