SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SENSRecorder.cpp
Go to the documentation of this file.
1 #include "SENSRecorder.h"
2 #include <Utils.h>
3 
4 SENSRecorder::SENSRecorder(const std::string& outputDir)
5  : _outputDir(outputDir)
6 {
8  {
12  }
13  else
14  Utils::log("SENS", "SENSRecorder: Directory does not exist: %s", outputDir.c_str());
15 }
16 
18 {
19  stop();
20 
21  if (_gpsDataHandler)
22  delete _gpsDataHandler;
26  delete _cameraDataHandler;
27 }
28 
29 //try to activate sensors before starting them, otherwise listener registration may become a threading problem
31 {
32  if (!_running && sensor)
33  {
34  deactivateGps();
35  _gps = sensor;
36  return true;
37  }
38  else
39  return false;
40 }
42 {
43  if (!_running && _gps)
44  {
45  _gps = nullptr;
46  return true;
47  }
48  else
49  return false;
50 }
51 //try to activate sensors before starting them, otherwise listener registration may become a threading problem
53 {
54  if (!_running && sensor)
55  {
57  _orientation = sensor;
58  return true;
59  }
60  else
61  return false;
62 }
63 
65 {
66  if (!_running && _orientation)
67  {
68  _orientation = nullptr;
69  return true;
70  }
71  else
72  return true;
73 }
74 
75 //try to activate sensors before starting them, otherwise listener registration may become a threading problem
77 {
78  if (!_running && sensor)
79  {
81  _camera = sensor;
82  return true;
83  }
84  else
85  return false;
86 }
87 
89 {
90  if (!_running && _camera)
91  {
92  _camera = nullptr;
93  return true;
94  }
95  else
96  return true;
97 }
98 
100 {
101  if (_running)
102  return false;
103 
104  std::string recordDir = Utils::unifySlashes(_outputDir) + Utils::getDateTime2String() + "_SENSRecorder/";
105  if (!Utils::makeDir(recordDir))
106  {
107  Utils::log("SENS", "SENSRecorder start: could not create record directory: %s", recordDir.c_str());
108  return false;
109  }
110 
111  if (_gps && _gpsDataHandler)
112  {
113  _gpsDataHandler->start(recordDir);
114  _gps->registerListener(this);
115  _running = true;
116  }
117 
119  {
120  _orientationDataHandler->start(recordDir);
122  _running = true;
123  }
124 
126  {
127  _cameraDataHandler->start(recordDir);
128  _camera->registerListener(this);
129  _running = true;
130  }
131 
132  return _running;
133 }
134 
136 {
137  if (_gps)
138  {
139  _gps->unregisterListener(this);
140  if (_gpsDataHandler)
142  }
143 
144  if (_orientation)
145  {
149  }
150 
151  if (_camera)
152  {
154  if (_cameraDataHandler)
156  }
157 
158  _running = false;
159 }
160 
162 {
164  return true;
165  else
166  return false;
167 }
168 
170 {
172  return true;
173  else
174  return false;
175 }
176 
178 {
180  return true;
181  else
182  return false;
183 }
184 
185 void SENSRecorder::onGps(const SENSTimePt& timePt, const SENSGps::Location& loc)
186 {
187  auto newData = std::make_pair(loc, timePt);
188  if (_gpsDataHandler)
189  _gpsDataHandler->add(std::move(newData));
190 }
191 
193 {
194  auto newData = std::make_pair(ori, timePt);
196  _orientationDataHandler->add(std::move(newData));
197 }
198 
199 void SENSRecorder::onFrame(const SENSTimePt& timePt, cv::Mat frame)
200 {
201  auto newData = std::make_pair(frame, timePt);
202  if (_cameraDataHandler)
203  _cameraDataHandler->add(std::move(newData));
204 }
205 
207 {
208  if (_cameraDataHandler)
210 }
std::chrono::high_resolution_clock::time_point SENSTimePt
Definition: SENS.h:57
Pure abstract camera class.
Definition: SENSCamera.h:160
virtual void registerListener(SENSCameraListener *listener)=0
virtual void unregisterListener(SENSCameraListener *listener)=0
void updateConfig(const SENSCameraConfig &config)
void registerListener(SENSGpsListener *listener)
Definition: SENSGps.cpp:29
void unregisterListener(SENSGpsListener *listener)
Definition: SENSGps.cpp:36
void registerListener(SENSOrientationListener *listener)
void unregisterListener(SENSOrientationListener *listener)
void add(T &&item)
add new value to store queue
bool getErrorMsg(std::string &msg)
get error msg (valid if function returns true)
void stop()
stop the store thread and clear values in queue
void start(const std::string &outputDir)
start the store thread
void onGps(const SENSTimePt &timePt, const SENSGps::Location &loc) override
gps listener callback
std::atomic_bool _running
Definition: SENSRecorder.h:65
bool activateGps(SENSGps *sensor)
bool getGpsHandlerError(std::string &errorMsg)
bool getOrientationHandlerError(std::string &errorMsg)
SENSOrientationRecorderDataHandler * _orientationDataHandler
Definition: SENSRecorder.h:58
bool activateCamera(SENSCamera *sensor)
void onCameraConfigChanged(const SENSCameraConfig &config) override
void onFrame(const SENSTimePt &timePt, cv::Mat frame) override
camera listener callback
bool activateOrientation(SENSOrientation *sensor)
SENSGps * _gps
Definition: SENSRecorder.h:61
const std::string & outputDir() const
Definition: SENSRecorder.h:44
void onOrientation(const SENSTimePt &timePt, const SENSOrientation::Quat &ori) override
orientation listener callback
SENSRecorder(const std::string &outputDir)
Definition: SENSRecorder.cpp:4
std::string _outputDir
Definition: SENSRecorder.h:55
SENSCameraRecorderDataHandler * _cameraDataHandler
Definition: SENSRecorder.h:59
bool getCameraHandlerError(std::string &errorMsg)
bool deactivateOrientation()
bool deactivateCamera()
SENSCamera * _camera
Definition: SENSRecorder.h:63
SENSGpsRecorderDataHandler * _gpsDataHandler
Definition: SENSRecorder.h:57
void stop()
Stops a running recording session.
bool deactivateGps()
SENSOrientation * _orientation
Definition: SENSRecorder.h:62
Config config
The configuration set in App::run.
Definition: AppAndroid.cpp:34
string getDateTime2String()
Returns local time as string like "20190213-154611".
Definition: Utils.cpp:289
string unifySlashes(const string &inputDir, bool withTrailingSlash)
Returns the inputDir string with unified forward slashes, e.g.: "dirA/dirB/".
Definition: Utils.cpp:367
bool dirExists(const string &path)
Returns true if a directory exists.
Definition: Utils.cpp:789
bool makeDir(const string &path)
Creates a directory with given path.
Definition: Utils.cpp:809
void errorMsg(const char *tag, const char *msg, const int line, const char *file)
Platform independent error message output.
Definition: Utils.cpp:1165
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
Definition: Utils.cpp:1100