Skip to content

Commit 5600230

Browse files
committed
add signal to indicate capture completion and improve thread handling in LibCameraWorker
1 parent 86fa203 commit 5600230

2 files changed

Lines changed: 19 additions & 17 deletions

File tree

src/libcameracamera.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "libcameracamera.h"
22
#include <QDebug>
33
#include <QThread>
4+
#include <QTimer>
45
#include <cerrno>
56
#include <chrono>
67
#include <cstring>
@@ -86,6 +87,12 @@ void LibcameraDevice::onErrorOccurred(const QString &error) {
8687
}
8788

8889
LibCameraWorker::LibCameraWorker(QObject *parent) : QObject(parent) {
90+
// Connect captureCompleted to resumeViewfinder with QueuedConnection
91+
// This ensures resumeViewfinder runs on the Qt event loop, not libcamera's thread
92+
connect(this, &LibCameraWorker::captureCompleted,
93+
this, &LibCameraWorker::resumeViewfinder,
94+
Qt::QueuedConnection);
95+
8996
initCameraManager();
9097
}
9198

@@ -322,27 +329,21 @@ void LibCameraWorker::captureImage() {
322329
void LibCameraWorker::processCaptureComplete(Request *request) {
323330
qDebug() << "[DEBUG] processCaptureComplete called, status:" << request->status();
324331

325-
if (request->status() == Request::RequestCancelled) {
326-
// Defer resumeViewfinder to run outside this callback
327-
QMetaObject::invokeMethod(this, "resumeViewfinder", Qt::QueuedConnection);
328-
return;
329-
}
330-
331-
// Convert buffer to image
332-
if (!request->buffers().empty()) {
333-
QImage img = convertBufferToImage(request->buffers());
334-
if (!img.isNull()) {
335-
Q_EMIT imageCaptured(img);
332+
if (request->status() != Request::RequestCancelled) {
333+
if (!request->buffers().empty()) {
334+
QImage img = convertBufferToImage(request->buffers());
335+
if (!img.isNull()) {
336+
Q_EMIT imageCaptured(img);
337+
} else {
338+
Q_EMIT errorOccurred("libcamera: failed to convert capture buffer");
339+
}
336340
} else {
337-
Q_EMIT errorOccurred("libcamera: failed to convert capture buffer");
341+
Q_EMIT errorOccurred("libcamera: capture request has no buffers");
338342
}
339-
} else {
340-
Q_EMIT errorOccurred("libcamera: capture request has no buffers");
341343
}
342344

343-
// Defer resumeViewfinder to run outside this callback
344-
// This is critical - we cannot call mCamera->stop() from within the requestCompleted callback
345-
QMetaObject::invokeMethod(this, "resumeViewfinder", Qt::QueuedConnection);
345+
// Emit signal to trigger resume on Qt thread
346+
Q_EMIT captureCompleted();
346347
}
347348

348349
void LibCameraWorker::resumeViewfinder() {

src/libcameracamera.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public slots:
4040
void frameReady(const QImage &image);
4141
void imageCaptured(const QImage &image);
4242
void errorOccurred(const QString &error);
43+
void captureCompleted(); // Add this
4344

4445
private:
4546
void initCameraManager();

0 commit comments

Comments
 (0)