mirror of
https://git.libcamera.org/libcamera/libcamera.git
synced 2025-07-19 18:35:07 +03:00
cam: Move camera acquire to the CameraSession class
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 <laurent.pinchart@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
This commit is contained in:
parent
8e716be529
commit
8519df23a9
3 changed files with 43 additions and 41 deletions
|
@ -19,11 +19,30 @@
|
||||||
|
|
||||||
using namespace libcamera;
|
using namespace libcamera;
|
||||||
|
|
||||||
CameraSession::CameraSession(std::shared_ptr<Camera> camera,
|
CameraSession::CameraSession(CameraManager *cm,
|
||||||
const OptionsParser::Options &options)
|
const OptionsParser::Options &options)
|
||||||
: camera_(camera), last_(0), queueCount_(0), captureCount_(0),
|
: last_(0), queueCount_(0), captureCount_(0),
|
||||||
captureLimit_(0), printMetadata_(false)
|
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]);
|
StreamRoles roles = StreamKeyValueParser::roles(options[OptStream]);
|
||||||
|
|
||||||
std::unique_ptr<CameraConfiguration> config =
|
std::unique_ptr<CameraConfiguration> config =
|
||||||
|
@ -64,6 +83,12 @@ CameraSession::CameraSession(std::shared_ptr<Camera> camera,
|
||||||
config_ = std::move(config);
|
config_ = std::move(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CameraSession::~CameraSession()
|
||||||
|
{
|
||||||
|
if (camera_)
|
||||||
|
camera_->release();
|
||||||
|
}
|
||||||
|
|
||||||
int CameraSession::start(const OptionsParser::Options &options)
|
int CameraSession::start(const OptionsParser::Options &options)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
#include <libcamera/base/signal.h>
|
#include <libcamera/base/signal.h>
|
||||||
|
|
||||||
#include <libcamera/camera.h>
|
#include <libcamera/camera.h>
|
||||||
|
#include <libcamera/camera_manager.h>
|
||||||
#include <libcamera/framebuffer.h>
|
#include <libcamera/framebuffer.h>
|
||||||
#include <libcamera/framebuffer_allocator.h>
|
#include <libcamera/framebuffer_allocator.h>
|
||||||
#include <libcamera/request.h>
|
#include <libcamera/request.h>
|
||||||
|
@ -25,10 +26,13 @@
|
||||||
class CameraSession
|
class CameraSession
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CameraSession(std::shared_ptr<libcamera::Camera> camera,
|
CameraSession(libcamera::CameraManager *cm,
|
||||||
const OptionsParser::Options &options);
|
const OptionsParser::Options &options);
|
||||||
|
~CameraSession();
|
||||||
|
|
||||||
bool isValid() const { return config_ != nullptr; }
|
bool isValid() const { return config_ != nullptr; }
|
||||||
|
|
||||||
|
libcamera::Camera *camera() { return camera_.get(); }
|
||||||
libcamera::CameraConfiguration *config() { return config_.get(); }
|
libcamera::CameraConfiguration *config() { return config_.get(); }
|
||||||
|
|
||||||
int start(const OptionsParser::Options &options);
|
int start(const OptionsParser::Options &options);
|
||||||
|
|
|
@ -51,7 +51,6 @@ private:
|
||||||
OptionsParser::Options options_;
|
OptionsParser::Options options_;
|
||||||
CameraManager *cm_;
|
CameraManager *cm_;
|
||||||
|
|
||||||
std::shared_ptr<Camera> camera_;
|
|
||||||
std::unique_ptr<CameraSession> session_;
|
std::unique_ptr<CameraSession> session_;
|
||||||
|
|
||||||
EventLoop loop_;
|
EventLoop loop_;
|
||||||
|
@ -60,7 +59,7 @@ private:
|
||||||
CamApp *CamApp::app_ = nullptr;
|
CamApp *CamApp::app_ = nullptr;
|
||||||
|
|
||||||
CamApp::CamApp()
|
CamApp::CamApp()
|
||||||
: cm_(nullptr), camera_(nullptr)
|
: cm_(nullptr)
|
||||||
{
|
{
|
||||||
CamApp::app_ = this;
|
CamApp::app_ = this;
|
||||||
}
|
}
|
||||||
|
@ -93,37 +92,16 @@ int CamApp::init(int argc, char **argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options_.isSet(OptCamera)) {
|
if (options_.isSet(OptCamera)) {
|
||||||
const std::string &cameraId = options_[OptCamera];
|
session_ = std::make_unique<CameraSession>(cm_, options_);
|
||||||
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<CameraSession>(camera_, options_);
|
|
||||||
if (!session_->isValid()) {
|
if (!session_->isValid()) {
|
||||||
std::cout << "Failed to create camera session" << std::endl;
|
std::cout << "Failed to create camera session" << std::endl;
|
||||||
cleanup();
|
cleanup();
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::cout << "Using camera " << session_->camera()->id()
|
||||||
|
<< std::endl;
|
||||||
|
|
||||||
session_->captureDone.connect(this, &CamApp::captureDone);
|
session_->captureDone.connect(this, &CamApp::captureDone);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,11 +116,6 @@ int CamApp::init(int argc, char **argv)
|
||||||
|
|
||||||
void CamApp::cleanup()
|
void CamApp::cleanup()
|
||||||
{
|
{
|
||||||
if (camera_) {
|
|
||||||
camera_->release();
|
|
||||||
camera_.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
session_.reset();
|
session_.reset();
|
||||||
|
|
||||||
cm_->stop();
|
cm_->stop();
|
||||||
|
@ -214,13 +187,13 @@ int CamApp::parseOptions(int argc, char *argv[])
|
||||||
|
|
||||||
int CamApp::listControls()
|
int CamApp::listControls()
|
||||||
{
|
{
|
||||||
if (!camera_) {
|
if (!session_) {
|
||||||
std::cout << "Cannot list controls without a camera"
|
std::cout << "Cannot list controls without a camera"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto &ctrl : camera_->controls()) {
|
for (const auto &ctrl : session_->camera()->controls()) {
|
||||||
const ControlId *id = ctrl.first;
|
const ControlId *id = ctrl.first;
|
||||||
const ControlInfo &info = ctrl.second;
|
const ControlInfo &info = ctrl.second;
|
||||||
|
|
||||||
|
@ -233,13 +206,13 @@ int CamApp::listControls()
|
||||||
|
|
||||||
int CamApp::listProperties()
|
int CamApp::listProperties()
|
||||||
{
|
{
|
||||||
if (!camera_) {
|
if (!session_) {
|
||||||
std::cout << "Cannot list properties without a camera"
|
std::cout << "Cannot list properties without a camera"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto &prop : camera_->properties()) {
|
for (const auto &prop : session_->camera()->properties()) {
|
||||||
const ControlId *id = properties::properties.at(prop.first);
|
const ControlId *id = properties::properties.at(prop.first);
|
||||||
const ControlValue &value = prop.second;
|
const ControlValue &value = prop.second;
|
||||||
|
|
||||||
|
@ -252,7 +225,7 @@ int CamApp::listProperties()
|
||||||
|
|
||||||
int CamApp::infoConfiguration()
|
int CamApp::infoConfiguration()
|
||||||
{
|
{
|
||||||
if (!camera_) {
|
if (!session_) {
|
||||||
std::cout << "Cannot print stream information without a camera"
|
std::cout << "Cannot print stream information without a camera"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
@ -328,7 +301,7 @@ int CamApp::run()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options_.isSet(OptCapture)) {
|
if (options_.isSet(OptCapture)) {
|
||||||
if (!camera_) {
|
if (!session_) {
|
||||||
std::cout << "Can't capture without a camera" << std::endl;
|
std::cout << "Can't capture without a camera" << std::endl;
|
||||||
return -ENODEV;
|
return -ENODEV;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue