diff --git a/imagebuffer.cpp b/imagebuffer.cpp index eef78d3..ac63f2f 100644 --- a/imagebuffer.cpp +++ b/imagebuffer.cpp @@ -39,7 +39,7 @@ size_t ImageBuffer::bufferLoad() { } void ImageBuffer::resize(size_t new_size) { - std::cerr << "Buffer Resize" << std::endl; + qDebug() << "Buffer Resize"; mutex.lock(); buffer_size = new_size; @@ -54,9 +54,8 @@ void ImageBuffer::resize(size_t new_size) { void ImageBuffer::push(MyImage *img) { mutex.lock(); if (buffer[current_write_index] != nullptr) { - std::cerr << "possible frame drop detected!!" << std::endl; - std::cerr << "buffer.push write: " << current_write_index << " read: " << current_read_index << " load: " << load << std::endl; - + qWarning() << "possible frame drop detected!!"; + qWarning() << "buffer.push write: " << current_write_index << " read: " << current_read_index << " load: " << load; delete(buffer[current_write_index]); } buffer[current_write_index] = img; @@ -93,7 +92,7 @@ MyImage* ImageBuffer::pop() { load -= 1; } mutex.unlock(); - std::cerr << "buffer.pop write: " << current_write_index << " read: " << current_read_index << " load: " << load << std::endl; + // std::cerr << "buffer.pop write: " << current_write_index << " read: " << current_read_index << " load: " << load << std::endl; return img; } diff --git a/pylonrecorder.cpp b/pylonrecorder.cpp index 877bb10..004afec 100644 --- a/pylonrecorder.cpp +++ b/pylonrecorder.cpp @@ -688,7 +688,7 @@ VideoSpecs PylonRecorder::getVideoSpecs(const ImageSettings &settings) { void PylonRecorder::startRecording() { qDebug() << "start recording!"; - std::string filename = createFilename(); + std::string filename = createFilename(".mp4"); fileLabel->setText(QString::fromStdString(filename)); qDebug() << "storing to file " << filename.c_str(); @@ -697,6 +697,7 @@ void PylonRecorder::startRecording() { VideoSpecs specs = getVideoSpecs(settings); specs.filename = filename; + specs.format = VideoFormat::mp4; qDebug() << "got video specifications"; if (buffer != nullptr) { @@ -825,11 +826,10 @@ QColor PylonRecorder::progressColor(int value) { } -std::string PylonRecorder::createFilename() { +std::string PylonRecorder::createFilename(const std::string &extension) { QDateTime dt(QDateTime::currentDateTimeUtc()); QDate date = dt.date(); std::string base = (date.toString("yyyy.MM.dd") + "_").toStdString(); - std::string extension = ".raw"; QString idx = QString::number(movieCount); std::string fname = base + idx.toStdString() + extension; while (QFile::exists(QString::fromStdString(fname))) { diff --git a/pylonrecorder.h b/pylonrecorder.h index 7bd7832..12c0616 100644 --- a/pylonrecorder.h +++ b/pylonrecorder.h @@ -78,7 +78,7 @@ private: void setExperimenterName(); void setSubjectID(); QColor progressColor(int value); - std::string createFilename(); + std::string createFilename(const std::string &extension=".raw"); void cameraConfiguration(); VideoSpecs getVideoSpecs(const ImageSettings &settings); diff --git a/util.h b/util.h index 7c3ba2c..0c5aa57 100644 --- a/util.h +++ b/util.h @@ -25,4 +25,8 @@ struct CameraLayout { CameraMode mode; }; +enum class VideoFormat { + raw, + mp4 +}; #endif /*UTIL_H*/ \ No newline at end of file diff --git a/writer.cpp b/writer.cpp index 2a84929..8f6eab9 100644 --- a/writer.cpp +++ b/writer.cpp @@ -2,6 +2,7 @@ #include #include +#include using namespace std::chrono; void Writer::setVideoSpecs(VideoSpecs specs) { @@ -28,21 +29,28 @@ void Writer::run() { 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; + } + Pylon::CVideoWriter videoWriter; if (specs_valid) { stop_request = false; stopNow = false; + if (videoSpecs.format == VideoFormat::raw) { + myFile.open(videoSpecs.filename, std::ios::out | std::ios::binary); + myFile.write((char*)&videoSpecs.width, 4); + myFile.write((char*)&videoSpecs.height, 4); + } else { + videoWriter.SetParameter(videoSpecs.width, videoSpecs.height, videoSpecs.pixelType, videoSpecs.fps, videoSpecs.quality); + videoWriter.Open(videoSpecs.filename.c_str()); + } - std::ofstream myFile; - myFile.open(videoSpecs.filename, std::ios::out | std::ios::binary); - myFile.write((char*)&videoSpecs.width, 4); - myFile.write((char*)&videoSpecs.height, 4); - // Pylon::CVideoWriter videoWriter; - // videoWriter.SetParameter(videoSpecs.width, videoSpecs.height, videoSpecs.pixelType, - // videoSpecs.fps, videoSpecs.quality); - // std::cout << !Pylon::CVideoWriter::IsSupported() << std::endl; - // videoWriter.SetParameter(); - // videoWriter.Open(videoSpecs.filename.c_str()); - // std::cerr << "Video writer is open!" << videoWriter.IsOpen() << std::endl; nix::File nix_file =nix::File::open(videoSpecs.filename + ".nix", nix::FileMode::Overwrite, "hdf5", nix::Compression::DeflateNormal); nix::Block b = nix_file.createBlock("Recording", "nix.recording"); nix::Section s = nix_file.createSection("Recording", "nix.recording"); @@ -88,17 +96,17 @@ void Writer::run() { if (img != nullptr) { auto start = high_resolution_clock::now(); // framecount += 1; - myFile.write((char*)img->data(), img->size()); - - // Pylon::CPylonImage pyImage; - // try { - // pyImage.AttachUserBuffer(img->data(), videoSpecs.width * videoSpecs.height, videoSpecs.pixelType, videoSpecs.width, videoSpecs.height, 0, videoSpecs.orientation); - // // Test duration of writing... - // videoWriter.Add( pyImage ); - - // } catch (const Pylon::GenericException &e) { - // std::cerr << "Writer::run: An exception occurred." << std::endl << e.GetDescription() << std::endl; - // } + if (videoSpecs.format == VideoFormat::raw) { + myFile.write((char*)img->data(), img->size()); + } else { + 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; + } + } auto stop = high_resolution_clock::now(); auto duration = duration_cast(stop - start); std::cerr << "wrote binary to file " << duration.count() << std::endl; diff --git a/writer.h b/writer.h index c8e2fde..83153ee 100644 --- a/writer.h +++ b/writer.h @@ -10,6 +10,7 @@ #include "imagebuffer.h" #include "projectsettings.h" #include +#include "util.h" struct VideoSpecs { std::string filename; @@ -20,6 +21,7 @@ struct VideoSpecs { double detectorGain; Pylon::EPixelType pixelType; Pylon::EImageOrientation orientation; + VideoFormat format = VideoFormat::raw; }; class Writer : public QThread