Commits

iorodeo committed 3ae4d27

Added fmf video logger.

Comments (0)

Files changed (8)

src/facade/basic_types.hpp

         // Video Writer Errors
         ERROR_VIDEO_WRITER_ADD_FRAME,
         ERROR_VIDEO_WRITER_INITIALIZE,
+        ERROR_VIDEO_WRITER_FINISH,
 
         // Image Logger Errors
         ERROR_IMAGE_LOGGER_MAX_QUEUE_SIZE,

src/gui/camera_window.cpp

             else 
             {
                 stopImageCapture();
-                threadPoolPtr_ -> waitForDone();
             }
         }
 
         double rem = timeStamp; 
 
         hrs = std::floor(timeStamp/3600.0);
-        rem -= hrs;
+        rem -= 3600*hrs;
         min = std::floor(rem/60.0);
-        rem -= min;
+        rem -= 60*min;
         sec = std::floor(rem);
 
         QString timeString;

src/gui/image_logger.cpp

             logQueueSize =  logImageQueuePtr_ -> size();
             logImageQueuePtr_ -> releaseLock();
 
-            //std::cout << "queue size: " << logQueueSize << std::endl;
+            std::cout << "queue size: " << logQueueSize << std::endl;
 
             if (logQueueSize > MAX_LOG_QUEUE_SIZE)
             {
             acquireLock();
             done = stopped_;
             releaseLock();
+
+        } // while (!done)
+
+        try
+        {
+            videoWriterPtr_ -> finish();
         }
+        catch (RuntimeError &runtimeError)
+        {
+            unsigned int errorId = runtimeError.id();;
+            QString errorMsg = QString::fromStdString(runtimeError.what());
+            emit imageLoggingError(errorId, errorMsg);
+        }
+    
+    }  // void ImageLogger::run()
 
-    } // while (!done)
+
 
 } // namespace bias
 

src/gui/video_writer.cpp

 #include "video_writer.hpp"
 #include "Stamped_image.hpp"
 #include <iostream>
+#include <QFileInfo>
+#include <QDir>
 
 namespace bias
 {
         return frameSkip_;
     }
 
+    void VideoWriter::finish() {};
+
+    QString VideoWriter::getUniqueFileName()
+    {
+        QFileInfo fileInfo(fileName_);
+        QString incrFileName = fileName_;
+
+        if (fileInfo.exists())
+        {
+            QDir filePath = QDir(fileInfo.absolutePath());
+            QString baseName = fileInfo.baseName();
+            QString ext = fileInfo.suffix();
+
+            unsigned int cnt = 2;
+            while(fileInfo.exists())
+            {
+                QString ver = "_v" + QString::number(cnt);
+                fileInfo = QFileInfo(filePath, baseName + ver + "." + ext);
+                incrFileName = fileInfo.absoluteFilePath();
+                cnt++;
+            }
+        }
+
+        return incrFileName;
+    }
+
 
 } // namespace bias

src/gui/video_writer.hpp

             virtual QString getFileName();
             virtual cv::Size getSize();
             virtual unsigned int getFrameSkip();
+            virtual void finish();
 
         protected:
 
             QString fileName_;
             unsigned long frameCount_;
             unsigned int frameSkip_;
+            QString getUniqueFileName();
     };
 
 } // namespace bias

src/gui/video_writer_avi.cpp

 
     void VideoWriter_avi::initialize(StampedImage stampedImg)
     {
-        QFileInfo fileInfo(fileName_);
-        QString incrFileName = fileName_;
-
-        if (fileInfo.exists())
-        {
-            QDir filePath = QDir(fileInfo.absolutePath());
-            QString baseName = fileInfo.baseName();
-            QString ext = fileInfo.suffix();
-
-            unsigned int cnt = 2;
-            while(fileInfo.exists())
-            {
-                QString ver = "_v" + QString::number(cnt);
-                fileInfo = QFileInfo(filePath, baseName + ver + "." + ext);
-                incrFileName = fileInfo.absoluteFilePath();
-                cnt++;
-            }
-        }
+        QString incrFileName = getUniqueFileName();
 
         try
         {

src/gui/video_writer_fmf.cpp

 #include "video_writer_fmf.hpp"
+#include "basic_types.hpp"
+#include "exception.hpp"
 #include <iostream>
 #include <stdint.h>
+#include <stdexcept>
 
 namespace bias
 {
-    const unsigned int VideoWriter_fmf::DEFAULT_FRAME_SKIP = 5;
+    const unsigned int VideoWriter_fmf::DEFAULT_FRAME_SKIP = 2;
+    const unsigned int VideoWriter_fmf::FMF_VERSION = 1;
 
     VideoWriter_fmf::VideoWriter_fmf() : VideoWriter() 
     {
+        numWritten_ = 0;
         isFirst_ = true;
         setFrameSkip(DEFAULT_FRAME_SKIP);
     }
 
     VideoWriter_fmf::VideoWriter_fmf(QString fileName) : VideoWriter(fileName)
     {
+        numWritten_ = 0;
         isFirst_ = true;
         setFrameSkip(DEFAULT_FRAME_SKIP);
     }
 
-
     VideoWriter_fmf::~VideoWriter_fmf()
     {
-        std::cout << __PRETTY_FUNCTION__ << std::endl;
+        file_.close();
+    }
+
+    void VideoWriter_fmf::finish()
+    {
+        try
+        {
+            file_.seekp(20);
+            file_.write((char*) &numWritten_, sizeof(uint64_t));
+        }
+        catch (std::ifstream::failure &exc)
+        {
+            unsigned int errorId = ERROR_VIDEO_WRITER_FINISH;
+            std::string errorMsg("video writer finish - failed");
+            errorMsg +=  "to write number frames:\n\n"; 
+            errorMsg += exc.what();
+            throw RuntimeError(errorId, errorMsg); 
+        }
     }
 
     void VideoWriter_fmf::addFrame(StampedImage stampedImg)
     {
-        std::cout << __PRETTY_FUNCTION__ << std::endl;
         if (isFirst_)
         {
             initialize(stampedImg);
         }
         if (frameCount_%frameSkip_==0)
         {
-            // Add frame to file
+            //std::cout << "write" << std::endl;
+            try
+            {
+                file_.write((char*) &stampedImg.timeStamp, sizeof(double));
+                file_.write((char*) stampedImg.image.data, size_.width*size_.height*sizeof(char)); 
+            }
+            catch (std::ifstream::failure &exc)
+            {
+                unsigned int errorId = ERROR_VIDEO_WRITER_ADD_FRAME;
+                std::string errorMsg("video writer add frame failed:\n\n"); 
+                errorMsg += exc.what();
+                throw RuntimeError(errorId, errorMsg); 
+            }
+            numWritten_++;
+        }
+        else 
+        {
+            //std::cout << "skip" << std::endl;
         }
         frameCount_++;
     }
 
     void VideoWriter_fmf::initialize(StampedImage stampedImg)
     {
-        std::cout << __PRETTY_FUNCTION__ << std::endl;
+        file_.clear();
+        file_.exceptions(std::ifstream::failbit | std::ifstream::badbit);
+       
+        // Get unique name for file and open for reading
+        QString incrFileName = getUniqueFileName();
+
+        try
+        {
+            file_.open(incrFileName.toStdString(), std::ios::binary | std::ios::out);
+        }
+        catch (std::ifstream::failure &exc)
+        {
+            unsigned int errorId = ERROR_VIDEO_WRITER_INITIALIZE;
+            std::string errorMsg("video writer unable to open file:\n\n"); 
+            errorMsg += exc.what();
+            throw RuntimeError(errorId, errorMsg); 
+        }
+
+        if (!file_.is_open())
+        {
+            unsigned int errorId = ERROR_VIDEO_WRITER_INITIALIZE;
+            std::string errorMsg("video writer unable to open file:\n\n"); 
+            errorMsg += "no exception thrown";
+            throw RuntimeError(errorId, errorMsg); 
+        }
+
+        setSize(stampedImg.image.size());
+
+        // Cast values to integers with specific widths
+        uint32_t fmfVersion = uint32_t(FMF_VERSION);
+        uint32_t width = uint32_t(size_.width);
+        uint32_t height = uint32_t(size_.height);
+        uint64_t bytesPerChunk = uint64_t(width)*uint64_t(height) + uint64_t(8);
+
+        // Add fmf header to file
+        try 
+        {
+            file_.write((char*) &fmfVersion, sizeof(uint32_t));
+            file_.write((char*) &height, sizeof(uint32_t));
+            file_.write((char*) &width, sizeof(uint32_t));
+            file_.write((char*) &bytesPerChunk, sizeof(uint64_t));
+            file_.write((char*) &numWritten_, sizeof(uint64_t));
+        }
+        catch (std::ifstream::failure &exc)
+        {
+            unsigned int errorId = ERROR_VIDEO_WRITER_INITIALIZE;
+            std::string errorMsg("video writer unable to write fmf header:\n\n"); 
+            errorMsg += exc.what();
+            throw RuntimeError(errorId, errorMsg); 
+        }
+
     }
 
 

src/gui/video_writer_fmf.hpp

 #define BIAS_VIDEO_WRITER_FMF_HPP
 
 #include "video_writer.hpp"
+#include <fstream>
 
 namespace bias 
 {
             VideoWriter_fmf();
             VideoWriter_fmf(QString fileName);
             ~VideoWriter_fmf();
+            virtual void finish();
             virtual void addFrame(StampedImage stampedImg);
 
             static const unsigned int DEFAULT_FRAME_SKIP;
+            static const unsigned int FMF_VERSION;
 
         private:
             bool isFirst_;
+            std::fstream file_;
+            uint64_t numWritten_;
             void initialize(StampedImage stampImg);
     };