Skip to content
Snippets Groups Projects
Commit 8f15e7ad authored by am0ebe's avatar am0ebe
Browse files

speedup: use threadpool and sublass frameprocessor from qrunnable -> reuse...

speedup: 	use threadpool and sublass frameprocessor from qrunnable -> reuse thread and frameprocessor objects for recording
parent 41a019dc
Branches
No related tags found
No related merge requests found
#include "frameobserver.h"
#include "cam.h"
#include "frameprocessor.h"
#include "qnamespace.h"
#include "cam.h"
#include <VmbCPP/SharedPointerDefines.h>
#include <VmbCPP/Camera.h>
// #include "qnamespace.h"
#include <QThreadPool>
#include <QDebug>
#include <QThread>
#include <QObject>
using namespace VmbCPP;
FrameObserver::FrameObserver( CamPtr cam ) : IFrameObserver( cam->getCameraPtr() ),
_cam(cam)
_cam(cam),
_processor(new FrameProcessor(cam->outDir())),
_threadpool(QThreadPool::globalInstance())
{
// _threadpool->setMaxThreadCount(QThread::idealThreadCount()); //dflt
connect(_processor, &FrameProcessor::frameProcessed, this, &FrameObserver::queueFrame, Qt::QueuedConnection);
}
FrameObserver::~FrameObserver()
{
qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << "";
delete _processor;
}
/* Frame callback notifies about incoming frames
process data afap
some functions are prohibited within this callback!
......@@ -60,15 +64,17 @@ void FrameObserver::FrameReceived ( const FramePtr pframe )
break;
}
FrameProcessor* processor = new FrameProcessor(pframe,_cam->outDir());
QThread *thread = new QThread;
processor->moveToThread(thread);
_processor->setFrame(pframe);
_threadpool->start(_processor); //start processing in a separate thread
}
void FrameObserver::queueFrame(FramePtr pframe)
{
qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << "";
_cam->getCameraPtr()->QueueFrame(pframe); // Queue the frame back to the camera
}
connect(thread, &QThread::started, processor, &FrameProcessor::processFrame);
connect(processor, &FrameProcessor::frameProcessed, this, &FrameObserver::queueFrame); //call **before** thread is deleted!
connect(processor, &FrameProcessor::frameProcessed, processor, &FrameProcessor::deleteLater);
connect(processor, &FrameProcessor::frameProcessed, thread, &QThread::quit, Qt::DirectConnection); // direct!
connect(thread, &QThread::finished, thread, &QThread::deleteLater);
// Optional: Connect to custom debug slots to monitor creation/deletion
// static int count=0;
......@@ -77,11 +83,3 @@ void FrameObserver::FrameReceived ( const FramePtr pframe )
// connect(thread, &QObject::destroyed, this, []() { qDebug() << "Thread destroyed: #" << count; });
// connect(thread, &QThread::started, this, []() { qDebug() << "Thread started: #" << count; });
// connect(thread, &QThread::finished, this, []() { qDebug() << "Thread finished: #" << count; });
thread->start();
}
void FrameObserver::queueFrame(FramePtr pframe)
{
_cam->getCameraPtr()->QueueFrame(pframe); // Queue the frame back to the camera
}
......@@ -4,6 +4,9 @@
#include <QObject>
#include <VmbCPP/IFrameObserver.h>
class FrameProcessor;
class QThreadPool;
using VmbCPP::FramePtr;
using VmbCPP::IFrameObserver;
......@@ -22,5 +25,7 @@ class FrameObserver : public QObject, public IFrameObserver
private:
CamPtr _cam;
FrameProcessor* _processor;
QThreadPool* _threadpool;
};
......@@ -13,27 +13,33 @@
#include <QImage>
#include <QDir>
FrameProcessor::FrameProcessor(const FramePtr pframe, const QString& outDir) : QObject(),
_pframe(pframe),
_outDir( outDir ),
FrameProcessor::FrameProcessor(const QString& outDir) : QRunnable(),
// _pframe(pframe),
_outDir( outDir ), //xx make class static
_pixelFormatStr("RGB8"),
_params({cv::IMWRITE_JPEG_QUALITY, 99, cv::IMWRITE_JPEG_OPTIMIZE, 1, cv::IMWRITE_JPEG_RST_INTERVAL,4}),
_width(0),
_height(0),
_timestamp(0)
{
setAutoDelete(false);
}
FrameProcessor::~FrameProcessor()
{
// static VmbUint64_t count = 0;
// qDebug() << __PRETTY_FUNCTION__ << count++;
static VmbUint64_t count = 0;
qDebug() << __PRETTY_FUNCTION__ << count++;
}
void FrameProcessor::setFrame(const FramePtr pframe)
{
_pframe = pframe;
}
// Process the received frame asynchronously
// pre: Frame needs to be valid!
// post: frame needs to be requeued
void FrameProcessor::processFrame()
void FrameProcessor::run()
{
static VmbUint64_t _count = 0; //all cams, all processors, totalframes
++_count;
......@@ -71,6 +77,9 @@ void FrameProcessor::processFrame()
_timestamp = 0;
_prev_timestamp = _timestamp;
//no need to reset pframe?
emit frameProcessed(_pframe);
//need to reset pframe?
// _pframe = FramePtr(nullptr);
}
......@@ -3,21 +3,21 @@
#include "iprinter.h"
#include <QObject>
#include <QRunnable>
#include <VmbCPP/SharedPointerDefines.h>
using VmbCPP::FramePtr;
class FrameProcessor : public QObject
class FrameProcessor : public QObject, public QRunnable
{
Q_OBJECT
public:
explicit FrameProcessor(const FramePtr, const QString&);
explicit FrameProcessor(const QString&);
~FrameProcessor();
public slots:
void processFrame();
void run() override;
void setFrame(const FramePtr);
signals:
void frameProcessed(FramePtr);
......@@ -34,6 +34,5 @@ private:
VmbUint64_t _timestamp;
VmbUint64_t _prev_timestamp;
VmbPixelFormatType _pixelFormat;
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment