SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SENSRecorderDataHandler.h
Go to the documentation of this file.
1 #ifndef SENS_RECORDER_DATAHANDLER_H
2 #define SENS_RECORDER_DATAHANDLER_H
3 
4 #include <string>
5 #include <thread>
6 #include <atomic>
7 #include <deque>
8 #include <utility>
9 #include <mutex>
10 #include <fstream>
11 //for video writer on android
12 #include <stdio.h>
13 #include <condition_variable>
14 
15 #include <opencv2/opencv.hpp>
16 
17 #include <Utils.h>
18 #include <SENSGps.h>
19 #include <SENSOrientation.h>
20 #include <SENSCamera.h>
21 
22 using GpsInfo = std::pair<SENSGps::Location, SENSTimePt>;
23 using OrientationInfo = std::pair<SENSOrientation::Quat, SENSTimePt>;
24 using FrameInfo = std::pair<cv::Mat, SENSTimePt>;
25 
26 //-----------------------------------------------------------------------------
27 /*! SENSRecorderDataHandler
28  This class is meant to be used exclusively by the SENSRecorder class. The SENSRecorder listens to sensors
29  and informs SENSRecorderDataHandler backends about new data. The SENSRecorderDataHandler stores values to file.
30  */
31 template<typename T>
33 {
34 public:
35  SENSRecorderDataHandler(const std::string& name);
36  virtual ~SENSRecorderDataHandler();
37 
38  //!start the store thread
39  void start(const std::string& outputDir);
40  //!stop the store thread and clear values in queue
41  void stop();
42  //!add new value to store queue
43  void add(T&& item);
44  //!get error msg (valid if function returns true)
45  bool getErrorMsg(std::string& msg);
46 
47 protected:
48  //!called in thread store routine when thread starts
49  virtual void writeOnThreadStart() {}
50  //!called in thread store routine when file could be opened
51  virtual void writeHeaderToFile(ofstream& file) {}
52  //!called in thread store routine for every new line
53  virtual void writeLineToFile(ofstream& file, const T& data) = 0;
54  //!called in thread store routine when thread finished
55  virtual void writeOnThreadFinish() {}
56 
57  //!output directory
58  std::string _outputDir;
59 
60 private:
61  void store();
62 
63  //new data queue: values are added by SENSRecorder and retrieved and
64  //written by store thread
65  std::deque<T> _queue;
66  //condition variable and mutex for store thread
67  std::mutex _mutex;
68  std::condition_variable _condVar;
69  //store thread
70  std::thread _thread;
71  //stop store thread
72  bool _stop = false;
73  //name of this handler (e.g. gps)
74  std::string _name;
75 
76  std::mutex _msgMutex;
77  std::string _errorMsg;
78 };
79 
80 //-----------------------------------------------------------------------------
81 /*! SENSGpsRecorderDataHandler
82  */
84 {
85 public:
87  void writeLineToFile(ofstream& file, const GpsInfo& data) override;
88 };
89 
90 //-----------------------------------------------------------------------------
91 /*! SENSOrientationRecorderDataHandler
92  */
94 {
95 public:
97  void writeLineToFile(ofstream& file, const OrientationInfo& data) override;
98 };
99 
100 //-----------------------------------------------------------------------------
101 /*! SENSCameraRecorderDataHandler
102  */
104 {
105 public:
107 
108  void writeOnThreadStart() override;
109  void writeLineToFile(ofstream& file, const FrameInfo& data) override;
110  void writeOnThreadFinish() override;
111 
112  void updateConfig(const SENSCameraConfig& config);
113 
114 private:
115  cv::VideoWriter _videoWriter;
116  int _frameIndex = 0;
117 };
118 
119 #endif
std::pair< SENSOrientation::Quat, SENSTimePt > OrientationInfo
std::pair< SENSGps::Location, SENSTimePt > GpsInfo
std::pair< cv::Mat, SENSTimePt > FrameInfo
void updateConfig(const SENSCameraConfig &config)
void writeLineToFile(ofstream &file, const FrameInfo &data) override
called in thread store routine for every new line
void writeOnThreadFinish() override
called in thread store routine when thread finished
void writeOnThreadStart() override
called in thread store routine when thread starts
void writeLineToFile(ofstream &file, const GpsInfo &data) override
called in thread store routine for every new line
void writeLineToFile(ofstream &file, const OrientationInfo &data) override
called in thread store routine for every new line
virtual void writeOnThreadFinish()
called in thread store routine when thread finished
virtual void writeOnThreadStart()
called in thread store routine when thread starts
virtual void writeLineToFile(ofstream &file, const T &data)=0
called in thread store routine for every new line
void add(T &&item)
add new value to store queue
SENSRecorderDataHandler(const std::string &name)
virtual void writeHeaderToFile(ofstream &file)
called in thread store routine when file could be opened
bool getErrorMsg(std::string &msg)
get error msg (valid if function returns true)
void stop()
stop the store thread and clear values in queue
std::string _outputDir
output directory
std::condition_variable _condVar
void start(const std::string &outputDir)
start the store thread
Config config
The configuration set in App::run.
Definition: AppAndroid.cpp:34