From d33600055390f5b8e40214af9df6fb51d199681b Mon Sep 17 00:00:00 2001 From: Jan Grewe Date: Fri, 13 Mar 2020 17:39:58 +0100 Subject: [PATCH] [writer] thread --- writer.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ writer.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 writer.cpp create mode 100644 writer.h diff --git a/writer.cpp b/writer.cpp new file mode 100644 index 0000000..93918e0 --- /dev/null +++ b/writer.cpp @@ -0,0 +1,43 @@ +#include "writer.h" + +void Writer::setVideoSpecs(VideoSpecs specs) { + videoSpecs = specs; + valid = true; +} + +void Writer::run() { + if (valid) { + stop_request = false; + Pylon::CVideoWriter videoWriter; + videoWriter.SetParameter(videoSpecs.width, videoSpecs.height, videoSpecs.pixelType, + videoSpecs.fps, videoSpecs.quality); + videoWriter.Open(videoSpecs.filename.c_str()); + MyImage img; + int64_t count = 0; + while (!stop_request || buffer->bufferLoad() > 0) { + std::cerr << "running!\n"; + if (buffer->bufferLoad() > 0 ) { + bool valid = buffer->pop(img); + if (valid) { + count ++; + Pylon::CPylonImage pyImage; + try { + pyImage.AttachUserBuffer(img.data(), videoSpecs.width * videoSpecs.height, videoSpecs.pixelType, videoSpecs.width, videoSpecs.height, 0, videoSpecs.orientation); + videoWriter.Add( pyImage ); + } catch (const Pylon::GenericException &e) { + std::cerr << "An exception occurred." << std::endl << e.GetDescription() << std::endl; + } + } + } else { + while (buffer->bufferLoad() < 5 && !stop_request) { + msleep(10); + std::cerr << "waiting.." << buffer->bufferLoad() << std::endl; + } + } + } + videoWriter.Close(); + std::cerr << "writer terminated: " << count << std::endl; + } else { + std::cerr << "Got no video specifications, not writing!" << std::endl; + } +} diff --git a/writer.h b/writer.h new file mode 100644 index 0000000..a5e69dd --- /dev/null +++ b/writer.h @@ -0,0 +1,44 @@ +#ifndef WRITER_H +#define WRITER_H + +#include +#include +#include +#include "pylonwrapper.h" +#include "imagebuffer.h" + +struct VideoSpecs { + std::string filename; + uint32_t width, height, quality = 95; + int fps; + Pylon::EPixelType pixelType; + Pylon::EImageOrientation orientation; +}; + +class Writer : public QThread +{ + Q_OBJECT +public: + explicit Writer(ImageBuffer*buffer, QObject *parent = nullptr) : + QThread(parent), buffer(buffer) {} + + void setVideoSpecs(VideoSpecs specs); + void run() override; + void stop(); + +private: + ImageBuffer *buffer; + VideoSpecs videoSpecs; + bool stop_request = false; + bool valid = false; + +public slots: + void requestStop() { + stop_request=true; + } + +signals: + void terminated(); +}; + +#endif // WRITER_H