From patchwork Wed Jul 7 02:19:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 12832 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 18EEBC3228 for ; Wed, 7 Jul 2021 02:20:48 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id AC4556851F; Wed, 7 Jul 2021 04:20:47 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="dSYTUszf"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 41CDA68507 for ; Wed, 7 Jul 2021 04:20:38 +0200 (CEST) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id D0E31466 for ; Wed, 7 Jul 2021 04:20:37 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1625624438; bh=v1pxMR2HDLsv6jJiq+tgRAXxfD6FTYtdBWKEUoTvvcs=; h=From:To:Subject:Date:In-Reply-To:References:From; b=dSYTUszfNISxBhqZ3Lqly1dT6LH/USOtz9US58jgqb5qcT8hPYeFMjFOpgb7PA82h PssvZJMkL54MwnA3S59+cXmbXj0GUiNxpNCUvLmTVASjkf/SN1pJocCLaz4yaDU79r GQqEgSGV/A52n2fwrrzBeZ4HpSX8D+7ax4ssLAeY= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Wed, 7 Jul 2021 05:19:30 +0300 Message-Id: <20210707021941.20804-20-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210707021941.20804-1-laurent.pinchart@ideasonboard.com> References: <20210707021941.20804-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 19/30] cam: Move camera acquire to the CameraSession class X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Continue moving towards making the CameraSession class the central point to handle a camera by moving the camera acquire operation. A new CameraSession::camera() function is needed to allow access to the camera from CamApp. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- src/cam/camera_session.cpp | 29 ++++++++++++++++++++-- src/cam/camera_session.h | 6 ++++- src/cam/main.cpp | 49 +++++++++----------------------------- 3 files changed, 43 insertions(+), 41 deletions(-) diff --git a/src/cam/camera_session.cpp b/src/cam/camera_session.cpp index edc7205d6bda..10e66446ee67 100644 --- a/src/cam/camera_session.cpp +++ b/src/cam/camera_session.cpp @@ -19,11 +19,30 @@ using namespace libcamera; -CameraSession::CameraSession(std::shared_ptr camera, +CameraSession::CameraSession(CameraManager *cm, const OptionsParser::Options &options) - : camera_(camera), last_(0), queueCount_(0), captureCount_(0), + : last_(0), queueCount_(0), captureCount_(0), captureLimit_(0), printMetadata_(false) { + const std::string &cameraId = options[OptCamera]; + char *endptr; + unsigned long index = strtoul(cameraId.c_str(), &endptr, 10); + if (*endptr == '\0' && index > 0 && index <= cm->cameras().size()) + camera_ = cm->cameras()[index - 1]; + else + camera_ = cm->get(cameraId); + + if (!camera_) { + std::cerr << "Camera " << cameraId << " not found" << std::endl; + return; + } + + if (camera_->acquire()) { + std::cerr << "Failed to acquire camera " << cameraId + << std::endl; + return; + } + StreamRoles roles = StreamKeyValueParser::roles(options[OptStream]); std::unique_ptr config = @@ -64,6 +83,12 @@ CameraSession::CameraSession(std::shared_ptr camera, config_ = std::move(config); } +CameraSession::~CameraSession() +{ + if (camera_) + camera_->release(); +} + int CameraSession::start(const OptionsParser::Options &options) { int ret; diff --git a/src/cam/camera_session.h b/src/cam/camera_session.h index 39dbbdf37913..88baf9061629 100644 --- a/src/cam/camera_session.h +++ b/src/cam/camera_session.h @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -25,10 +26,13 @@ class CameraSession { public: - CameraSession(std::shared_ptr camera, + CameraSession(libcamera::CameraManager *cm, const OptionsParser::Options &options); + ~CameraSession(); bool isValid() const { return config_ != nullptr; } + + libcamera::Camera *camera() { return camera_.get(); } libcamera::CameraConfiguration *config() { return config_.get(); } int start(const OptionsParser::Options &options); diff --git a/src/cam/main.cpp b/src/cam/main.cpp index 082a053efeda..8ed5e841a1e9 100644 --- a/src/cam/main.cpp +++ b/src/cam/main.cpp @@ -51,7 +51,6 @@ private: OptionsParser::Options options_; CameraManager *cm_; - std::shared_ptr camera_; std::unique_ptr session_; EventLoop loop_; @@ -60,7 +59,7 @@ private: CamApp *CamApp::app_ = nullptr; CamApp::CamApp() - : cm_(nullptr), camera_(nullptr) + : cm_(nullptr) { CamApp::app_ = this; } @@ -93,37 +92,16 @@ int CamApp::init(int argc, char **argv) } if (options_.isSet(OptCamera)) { - const std::string &cameraId = options_[OptCamera]; - char *endptr; - unsigned long index = strtoul(cameraId.c_str(), &endptr, 10); - if (*endptr == '\0' && index > 0 && index <= cm_->cameras().size()) - camera_ = cm_->cameras()[index - 1]; - else - camera_ = cm_->get(cameraId); - - if (!camera_) { - std::cout << "Camera " - << std::string(options_[OptCamera]) - << " not found" << std::endl; - cleanup(); - return -ENODEV; - } - - if (camera_->acquire()) { - std::cout << "Failed to acquire camera" << std::endl; - cleanup(); - return -EINVAL; - } - - std::cout << "Using camera " << camera_->id() << std::endl; - - session_ = std::make_unique(camera_, options_); + session_ = std::make_unique(cm_, 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); } @@ -138,11 +116,6 @@ int CamApp::init(int argc, char **argv) void CamApp::cleanup() { - if (camera_) { - camera_->release(); - camera_.reset(); - } - session_.reset(); cm_->stop(); @@ -214,13 +187,13 @@ int CamApp::parseOptions(int argc, char *argv[]) int CamApp::listControls() { - if (!camera_) { + if (!session_) { std::cout << "Cannot list controls without a camera" << std::endl; return -EINVAL; } - for (const auto &ctrl : camera_->controls()) { + for (const auto &ctrl : session_->camera()->controls()) { const ControlId *id = ctrl.first; const ControlInfo &info = ctrl.second; @@ -233,13 +206,13 @@ int CamApp::listControls() int CamApp::listProperties() { - if (!camera_) { + if (!session_) { std::cout << "Cannot list properties without a camera" << std::endl; return -EINVAL; } - for (const auto &prop : camera_->properties()) { + for (const auto &prop : session_->camera()->properties()) { const ControlId *id = properties::properties.at(prop.first); const ControlValue &value = prop.second; @@ -252,7 +225,7 @@ int CamApp::listProperties() int CamApp::infoConfiguration() { - if (!camera_) { + if (!session_) { std::cout << "Cannot print stream information without a camera" << std::endl; return -EINVAL; @@ -328,7 +301,7 @@ int CamApp::run() } if (options_.isSet(OptCapture)) { - if (!camera_) { + if (!session_) { std::cout << "Can't capture without a camera" << std::endl; return -ENODEV; }