qcam: Use pointer when choosing camera

In order to remove redundant camera ID lookups and comparisons switch
to pointer-based checks when choosing and switching cameras.

Co-developed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Stanislaw Gruszka <stanislaw.gruszka@linux.intel.com>
Tested-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
This commit is contained in:
Stanislaw Gruszka 2024-10-31 12:02:56 +01:00 committed by Kieran Bingham
parent 1a2be886c0
commit a43ea7ff70
2 changed files with 19 additions and 26 deletions

View file

@ -251,16 +251,14 @@ void MainWindow::updateTitle()
void MainWindow::switchCamera() void MainWindow::switchCamera()
{ {
/* Get and acquire the new camera. */ /* Get and acquire the new camera. */
std::string newCameraId = chooseCamera(); std::shared_ptr<Camera> cam = chooseCamera();
if (newCameraId.empty()) if (!cam)
return; return;
if (camera_ && newCameraId == camera_->id()) if (camera_ && cam == camera_)
return; return;
const std::shared_ptr<Camera> &cam = cm_->get(newCameraId);
if (cam->acquire()) { if (cam->acquire()) {
qInfo() << "Failed to acquire camera" << cam->id().c_str(); qInfo() << "Failed to acquire camera" << cam->id().c_str();
return; return;
@ -282,46 +280,41 @@ void MainWindow::switchCamera()
startStopAction_->setChecked(true); startStopAction_->setChecked(true);
/* Display the current cameraId in the toolbar .*/ /* Display the current cameraId in the toolbar .*/
cameraSelectButton_->setText(QString::fromStdString(newCameraId)); cameraSelectButton_->setText(QString::fromStdString(cam->id()));
} }
std::string MainWindow::chooseCamera() std::shared_ptr<Camera> MainWindow::chooseCamera()
{ {
if (cameraSelectorDialog_->exec() != QDialog::Accepted) if (cameraSelectorDialog_->exec() != QDialog::Accepted)
return std::string(); return {};
return cameraSelectorDialog_->getCameraId(); std::string id = cameraSelectorDialog_->getCameraId();
return cm_->get(id);
} }
int MainWindow::openCamera() int MainWindow::openCamera()
{ {
std::string cameraName;
/* /*
* If a camera is specified on the command line, get it. Otherwise, if * If a camera is specified on the command line, get it. Otherwise, if
* only one camera is available, pick it automatically, else, display * only one camera is available, pick it automatically, else, display
* the selector dialog box. * the selector dialog box.
*/ */
if (options_.isSet(OptCamera)) { if (options_.isSet(OptCamera)) {
cameraName = static_cast<std::string>(options_[OptCamera]); std::string cameraName = static_cast<std::string>(options_[OptCamera]);
camera_ = cm_->get(cameraName);
if (!camera_)
qInfo() << "Camera" << cameraName.c_str() << "not found";
} else { } else {
std::vector<std::shared_ptr<Camera>> cameras = cm_->cameras(); std::vector<std::shared_ptr<Camera>> cameras = cm_->cameras();
if (cameras.size() == 1) camera_ = (cameras.size() == 1) ? cameras[0] : chooseCamera();
cameraName = cameras[0]->id(); if (!camera_)
else qInfo() << "No camera detected";
cameraName = chooseCamera();
} }
if (cameraName == "") if (!camera_)
return -EINVAL;
/* Get and acquire the camera. */
camera_ = cm_->get(cameraName);
if (!camera_) {
qInfo() << "Camera" << cameraName.c_str() << "not found";
return -ENODEV; return -ENODEV;
}
/* Acquire the camera. */
if (camera_->acquire()) { if (camera_->acquire()) {
qInfo() << "Failed to acquire camera"; qInfo() << "Failed to acquire camera";
camera_.reset(); camera_.reset();
@ -329,7 +322,7 @@ int MainWindow::openCamera()
} }
/* Set the camera switch button with the currently selected Camera id. */ /* Set the camera switch button with the currently selected Camera id. */
cameraSelectButton_->setText(QString::fromStdString(cameraName)); cameraSelectButton_->setText(QString::fromStdString(camera_->id()));
return 0; return 0;
} }

View file

@ -73,7 +73,7 @@ private Q_SLOTS:
private: private:
int createToolbars(); int createToolbars();
std::string chooseCamera(); std::shared_ptr<libcamera::Camera> chooseCamera();
int openCamera(); int openCamera();
int startCapture(); int startCapture();