PylonRecorder/writer.cpp

82 lines
3.0 KiB
C++

#include "writer.h"
#include <chrono>
#include <fstream>
#include <pylon/VideoWriter.h>
#include <chrono>
using namespace std::chrono;
typedef high_resolution_clock Time;
typedef milliseconds ms;
typedef duration<float> fsec;
void Writer::setVideoSpecs(VideoSpecs specs) {
videoSpecs = specs;
specs_valid = true;
}
void Writer::setProjectMetadata(ProjectMetadata mdata) {
qDebug() << "received metadata";
metadata = mdata;
metadata_valid = true;
}
void Writer::writeMetadata(nix::Section &s){
s.createProperty("project", nix::Value(this->metadata.project().toStdString()));
s.createProperty("experimenter", nix::Value(this->metadata.experimenter().toStdString()));
s.createProperty("experiment", nix::Value(this->metadata.experiment().toStdString()));
s.createProperty("condition", nix::Value(this->metadata.condition().toStdString()));
s.createProperty("comment", nix::Value(this->metadata.comment().toStdString()));
s.createProperty("subject", nix::Value(this->metadata.subject().toStdString()));
s.createProperty("setup", nix::Value(this->metadata.setup().toStdString()));
}
void Writer::run() {
qDebug() << "writer running!";
size_t count = 0;
size_t chunksize = 256;
// size_t framecount = 0;
std::ofstream myFile;
if (videoSpecs.format == VideoFormat::mp4 && !Pylon::CVideoWriter::IsSupported()) {
qWarning() << "VideoWriter is not supported at the moment. Please install the pylon Supplementary Package for MPEG-4 which is available on the Basler website.";
// Releases all pylon resources.
// PylonTerminate();
// Return with error code 1.
emit writingDone();
return;
}
qDebug() << "checks done!";
Pylon::CVideoWriter videoWriter;
if (specs_valid && videoSpecs.format != VideoFormat::raw) {
stop_request = false;
stopNow = false;
videoWriter.SetParameter((uint32_t)videoSpecs.width, (uint32_t)videoSpecs.height,
videoSpecs.pixelType, (double)videoSpecs.fps, videoSpecs.quality);
videoWriter.Open(videoSpecs.filename.c_str());
qDebug() << "preparations done, starting loop!";
while ((!stop_request || buffer->bufferLoad() > 0) && !stopNow) {
if (buffer->bufferLoad() > 0 ) {
size_t framecount = 0;
MyImage *img = buffer->read(framecount);
if (img != nullptr) {
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 << "Writer::run: An exception occurred." << std::endl << e.GetDescription() << std::endl;
}
}
} else {
while (buffer->bufferLoad() < 1 && !stop_request) {
msleep(2);
}
}
}
videoWriter.Close();
} else {
qDebug() << "Got no video specifications, not writing!";
}
emit writingDone();
}