mirror of
https://git.libcamera.org/libcamera/libcamera.git
synced 2025-07-19 18:35:07 +03:00
cam: Move CameraConfiguration creation to CameraSession class
Creating a configuration for a camera is an operation that logically belongs to the CameraSession class. Move it there. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
This commit is contained in:
parent
34d986d1ec
commit
8e716be529
3 changed files with 56 additions and 61 deletions
|
@ -15,14 +15,53 @@
|
||||||
#include "camera_session.h"
|
#include "camera_session.h"
|
||||||
#include "event_loop.h"
|
#include "event_loop.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
#include "stream_options.h"
|
||||||
|
|
||||||
using namespace libcamera;
|
using namespace libcamera;
|
||||||
|
|
||||||
CameraSession::CameraSession(std::shared_ptr<Camera> camera,
|
CameraSession::CameraSession(std::shared_ptr<Camera> camera,
|
||||||
CameraConfiguration *config)
|
const OptionsParser::Options &options)
|
||||||
: camera_(camera), config_(config), last_(0), queueCount_(0),
|
: camera_(camera), last_(0), queueCount_(0), captureCount_(0),
|
||||||
captureCount_(0), captureLimit_(0), printMetadata_(false)
|
captureLimit_(0), printMetadata_(false)
|
||||||
{
|
{
|
||||||
|
StreamRoles roles = StreamKeyValueParser::roles(options[OptStream]);
|
||||||
|
|
||||||
|
std::unique_ptr<CameraConfiguration> config =
|
||||||
|
camera_->generateConfiguration(roles);
|
||||||
|
if (!config || config->size() != roles.size()) {
|
||||||
|
std::cerr << "Failed to get default stream configuration"
|
||||||
|
<< std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Apply configuration if explicitly requested. */
|
||||||
|
if (StreamKeyValueParser::updateConfiguration(config.get(),
|
||||||
|
options[OptStream])) {
|
||||||
|
std::cerr << "Failed to update configuration" << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool strictFormats = options.isSet(OptStrictFormats);
|
||||||
|
|
||||||
|
switch (config->validate()) {
|
||||||
|
case CameraConfiguration::Valid:
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CameraConfiguration::Adjusted:
|
||||||
|
if (strictFormats) {
|
||||||
|
std::cout << "Adjusting camera configuration disallowed by --strict-formats argument"
|
||||||
|
<< std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
std::cout << "Camera configuration adjusted" << std::endl;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CameraConfiguration::Invalid:
|
||||||
|
std::cout << "Camera configuration invalid" << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
config_ = std::move(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
int CameraSession::start(const OptionsParser::Options &options)
|
int CameraSession::start(const OptionsParser::Options &options)
|
||||||
|
@ -34,7 +73,7 @@ int CameraSession::start(const OptionsParser::Options &options)
|
||||||
captureLimit_ = options[OptCapture].toInteger();
|
captureLimit_ = options[OptCapture].toInteger();
|
||||||
printMetadata_ = options.isSet(OptMetadata);
|
printMetadata_ = options.isSet(OptMetadata);
|
||||||
|
|
||||||
ret = camera_->configure(config_);
|
ret = camera_->configure(config_.get());
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
std::cout << "Failed to configure camera" << std::endl;
|
std::cout << "Failed to configure camera" << std::endl;
|
||||||
return ret;
|
return ret;
|
||||||
|
|
|
@ -26,7 +26,10 @@ class CameraSession
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CameraSession(std::shared_ptr<libcamera::Camera> camera,
|
CameraSession(std::shared_ptr<libcamera::Camera> camera,
|
||||||
libcamera::CameraConfiguration *config);
|
const OptionsParser::Options &options);
|
||||||
|
|
||||||
|
bool isValid() const { return config_ != nullptr; }
|
||||||
|
libcamera::CameraConfiguration *config() { return config_.get(); }
|
||||||
|
|
||||||
int start(const OptionsParser::Options &options);
|
int start(const OptionsParser::Options &options);
|
||||||
void stop();
|
void stop();
|
||||||
|
@ -41,7 +44,7 @@ private:
|
||||||
void processRequest(libcamera::Request *request);
|
void processRequest(libcamera::Request *request);
|
||||||
|
|
||||||
std::shared_ptr<libcamera::Camera> camera_;
|
std::shared_ptr<libcamera::Camera> camera_;
|
||||||
libcamera::CameraConfiguration *config_;
|
std::unique_ptr<libcamera::CameraConfiguration> config_;
|
||||||
|
|
||||||
std::map<const libcamera::Stream *, std::string> streamName_;
|
std::map<const libcamera::Stream *, std::string> streamName_;
|
||||||
std::unique_ptr<BufferWriter> writer_;
|
std::unique_ptr<BufferWriter> writer_;
|
||||||
|
|
|
@ -40,7 +40,6 @@ private:
|
||||||
void cameraRemoved(std::shared_ptr<Camera> cam);
|
void cameraRemoved(std::shared_ptr<Camera> cam);
|
||||||
void captureDone();
|
void captureDone();
|
||||||
int parseOptions(int argc, char *argv[]);
|
int parseOptions(int argc, char *argv[]);
|
||||||
int prepareConfig();
|
|
||||||
int listControls();
|
int listControls();
|
||||||
int listProperties();
|
int listProperties();
|
||||||
int infoConfiguration();
|
int infoConfiguration();
|
||||||
|
@ -53,19 +52,15 @@ private:
|
||||||
CameraManager *cm_;
|
CameraManager *cm_;
|
||||||
|
|
||||||
std::shared_ptr<Camera> camera_;
|
std::shared_ptr<Camera> camera_;
|
||||||
std::unique_ptr<libcamera::CameraConfiguration> config_;
|
|
||||||
std::unique_ptr<CameraSession> session_;
|
std::unique_ptr<CameraSession> session_;
|
||||||
|
|
||||||
EventLoop loop_;
|
EventLoop loop_;
|
||||||
|
|
||||||
bool strictFormats_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CamApp *CamApp::app_ = nullptr;
|
CamApp *CamApp::app_ = nullptr;
|
||||||
|
|
||||||
CamApp::CamApp()
|
CamApp::CamApp()
|
||||||
: cm_(nullptr), camera_(nullptr), config_(nullptr),
|
: cm_(nullptr), camera_(nullptr)
|
||||||
strictFormats_(false)
|
|
||||||
{
|
{
|
||||||
CamApp::app_ = this;
|
CamApp::app_ = this;
|
||||||
}
|
}
|
||||||
|
@ -88,9 +83,6 @@ int CamApp::init(int argc, char **argv)
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
if (options_.isSet(OptStrictFormats))
|
|
||||||
strictFormats_ = true;
|
|
||||||
|
|
||||||
cm_ = new CameraManager();
|
cm_ = new CameraManager();
|
||||||
|
|
||||||
ret = cm_->start();
|
ret = cm_->start();
|
||||||
|
@ -125,13 +117,13 @@ int CamApp::init(int argc, char **argv)
|
||||||
|
|
||||||
std::cout << "Using camera " << camera_->id() << std::endl;
|
std::cout << "Using camera " << camera_->id() << std::endl;
|
||||||
|
|
||||||
ret = prepareConfig();
|
session_ = std::make_unique<CameraSession>(camera_, options_);
|
||||||
if (ret) {
|
if (!session_->isValid()) {
|
||||||
|
std::cout << "Failed to create camera session" << std::endl;
|
||||||
cleanup();
|
cleanup();
|
||||||
return ret;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
session_ = std::make_unique<CameraSession>(camera_, config_.get());
|
|
||||||
session_->captureDone.connect(this, &CamApp::captureDone);
|
session_->captureDone.connect(this, &CamApp::captureDone);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,7 +143,7 @@ void CamApp::cleanup()
|
||||||
camera_.reset();
|
camera_.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
config_.reset();
|
session_.reset();
|
||||||
|
|
||||||
cm_->stop();
|
cm_->stop();
|
||||||
}
|
}
|
||||||
|
@ -220,45 +212,6 @@ int CamApp::parseOptions(int argc, char *argv[])
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CamApp::prepareConfig()
|
|
||||||
{
|
|
||||||
StreamRoles roles = StreamKeyValueParser::roles(options_[OptStream]);
|
|
||||||
|
|
||||||
config_ = camera_->generateConfiguration(roles);
|
|
||||||
if (!config_ || config_->size() != roles.size()) {
|
|
||||||
std::cerr << "Failed to get default stream configuration"
|
|
||||||
<< std::endl;
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Apply configuration if explicitly requested. */
|
|
||||||
if (StreamKeyValueParser::updateConfiguration(config_.get(),
|
|
||||||
options_[OptStream])) {
|
|
||||||
std::cerr << "Failed to update configuration" << std::endl;
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (config_->validate()) {
|
|
||||||
case CameraConfiguration::Valid:
|
|
||||||
break;
|
|
||||||
case CameraConfiguration::Adjusted:
|
|
||||||
if (strictFormats_) {
|
|
||||||
std::cout << "Adjusting camera configuration disallowed by --strict-formats argument"
|
|
||||||
<< std::endl;
|
|
||||||
config_.reset();
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
std::cout << "Camera configuration adjusted" << std::endl;
|
|
||||||
break;
|
|
||||||
case CameraConfiguration::Invalid:
|
|
||||||
std::cout << "Camera configuration invalid" << std::endl;
|
|
||||||
config_.reset();
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int CamApp::listControls()
|
int CamApp::listControls()
|
||||||
{
|
{
|
||||||
if (!camera_) {
|
if (!camera_) {
|
||||||
|
@ -299,14 +252,14 @@ int CamApp::listProperties()
|
||||||
|
|
||||||
int CamApp::infoConfiguration()
|
int CamApp::infoConfiguration()
|
||||||
{
|
{
|
||||||
if (!config_) {
|
if (!camera_) {
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int index = 0;
|
unsigned int index = 0;
|
||||||
for (const StreamConfiguration &cfg : *config_) {
|
for (const StreamConfiguration &cfg : *session_->config()) {
|
||||||
std::cout << index << ": " << cfg.toString() << std::endl;
|
std::cout << index << ": " << cfg.toString() << std::endl;
|
||||||
|
|
||||||
const StreamFormats &formats = cfg.formats();
|
const StreamFormats &formats = cfg.formats();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue