MIRA
TapeRecorder.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_TAPERECORDER_H_
48 #define _MIRA_TAPERECORDER_H_
49 
51 #include <fw/Framework.h>
52 #include <fw/Tape.h>
54 
55 namespace mira {
56 
58 
63 {
64 public:
65 
68  interval(Duration::microseconds(0)),
69  avoidDataLoss(false) {}
70 
71  template <typename Reflector>
72  void reflect(Reflector& r)
73  {
75  r.member("Codecs", codecs, "The codecs used for this channel",
76  std::list<BinarySerializerCodecPtr>());
77  if (v < 2) {
78  bool compress;
79  r.member("Compress", compress, "Should the data be stored compressed", false);
80  compressionLevel = (compress ? -1 : 0);
81  } else {
82  r.member("CompressionLevel", compressionLevel,
83  "Compression level (0=none, 1..9=best speed to best compression, -1=default)", 0);
84  }
85  r.member("Interval", interval, "Stores only data in that interval (if 0 stores all data)",
87  r.member("AvoidDataLoss", avoidDataLoss,
88  "Avoid data loss while recording this channel by reading all data from a channel since last recording",
89  false);
90  }
91 
92  std::list<BinarySerializerCodecPtr> codecs;
96 };
97 
99 typedef std::map<std::string, RecordedChannelInfo> RecordedChannelInfoMap;
100 
106 {
107 public:
108 
109  template <typename Reflector>
110  void reflect(Reflector& r)
111  {
112  r.member("Channels", channels, "The channels to be recorded");
113  r.member("RecordTransformations", recordTransforms,
114  "Record all transformation channels?", false);
115  r.member("MaxMessageBlockSize", maxMessageBlockSize,
116  "The maximum size of a message block in bytes",
117  64*1024*1024); // default size is 64MB
118  r.member("Filename", filename, "The filename for the output tape");
119  r.member("TimestampFilename", timestampFilename,
120  "Automatically extend filename by timestamp?", false);
121  }
122 
124  bool recordTransforms = false;
125  uint32 maxMessageBlockSize = 64*1024*1024;
127  bool timestampFilename = false;
128 };
134 {
135 public:
136 
139  {
140  public:
142  seqID(0), recordCounter(0) {}
143  uint32 seqID;
149  };
150 
152  typedef std::map<std::string, ChannelInfo> ChannelMap;
153 
154 public:
165  TapeRecorder(const Duration& initialWaitForDataDuration = Duration::seconds(0));
166 
167  virtual ~TapeRecorder();
168 
169  template <typename Reflector>
170  void reflect(Reflector& r)
171  {
172  r.property("MaxMessageBlockSize", mMaxMessageBlockSize,
173  "The maximum size of a message block in bytes");
174  r.property("WarnOfDataLoss", mWarnDataLoss,
175  "Warn when missed update is detected for a channel with AvoidDataLoss", false);
176  }
177 
189  MIRA_DEPRECATED("Please use int compressionLevel(=0/-1) instead of bool compress(=false/true)",
190  void addChannel(const std::string& channelID,
191  const std::list<BinarySerializerCodecPtr>& codecs,
192  bool compress, Duration interval = Duration::microseconds(0), bool avoidDataLoss = false))
193  {
194  addChannel(channelID, codecs, (compress ? -1 : 0), interval, avoidDataLoss);
195  }
196 
197 
210  void addChannel(const std::string& channelID,
211  const std::list<BinarySerializerCodecPtr>& codecs = std::list<BinarySerializerCodecPtr>(),
212  int compressionLevel = 0,
213  Duration interval = Duration::microseconds(0), bool avoidDataLoss = false);
214 
221  void addChannel(const std::string& channelID,
222  const RecordedChannelInfo& info);
223 
228  void record(const Path& file);
229 
235  void record(const TapeRecorderConfig& config);
236 
244  void stop(bool finishQueue = true);
245 
250  size_t storageQueueSize();
251 
261  void processStorageQueue(int numEntries = 1);
262 
267  void setMaxMessageBlockSize(uint32 maxMessageBlockSize)
268  {
269  mMaxMessageBlockSize = maxMessageBlockSize;
270  }
271 
278  void setWarnOfDataLoss(bool enable)
279  {
280  mWarnDataLoss = enable;
281  }
282 
285  {
286  return mWarnDataLoss;
287  }
288 
292  bool isRecording() const
293  {
294  return mIsRecording;
295  }
296 
297  const PerformanceStatistics& getStats(const std::string& name);
298 
299 protected:
300 
301  // normal callback for data changes (writes data to tape)
302  bool valueChanged(ChannelRead<void> value, bool forceStorage = false);
303  void intervalValueChanged(ChannelReadInterval<void> values, bool forceStorage = false);
304 
305  void addAllTransformationChannels();
306  Path timestampFilename(const Path& filenameBase);
307 
308 private:
309 
310  struct MessageToStore
311  {
312  StampedHeader header;
313  std::string channelID;
314  std::string typeName;
315  Time enqueueTime;
316  Buffer<uint8> buf;
317  ChannelInfo* info;
318  };
319 
320  void storageThread();
321  void storeMessage(MessageToStore& msg);
322 
323 protected:
324 
333 
334  boost::thread mStorageThread;
335  boost::condition_variable mStorageThreadCondition;
336  boost::mutex mStorageMutex;
338 
339  std::deque<MessageToStore> mStorageQueue;
341 };
342 
344 
345 }
346 
347 #endif
A tape is a binary file that contains recorded/serialized data of one or multiple channels...
Definition: Tape.h:142
RecordedChannelInfo()
Definition: TapeRecorder.h:66
Database that stores all meta type information and provides additional functions for accessing the da...
Definition: MetaSerializer.h:509
An object that allows read access to a whole interval of channel data.
Definition: ChannelReadInterval.h:72
Duration interval
Definition: TapeRecorder.h:94
A config that can be passed to the TapeRecorder which contains all settings that should be used for r...
Definition: TapeRecorder.h:105
Class for recording channel data to tape.
Definition: TapeRecorder.h:133
Authority mAuthority
Definition: TapeRecorder.h:325
boost::mutex mStorageMutex
Definition: TapeRecorder.h:336
PropertyHint file(const std::string &filters=std::string(), bool save=false)
Tells the property editor that the path is for a file, and that it should show a "File Open"/"File Sa...
Definition: Path.h:247
Path filename
Definition: TapeRecorder.h:126
A Tape is a representation of recorded channel data in a binary file.
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
boost::filesystem::path Path
Typedef of a Path (shorter version for boost::filesystem::path)
Definition: Path.h:69
MetaTypeDatabase metaDB
Definition: TapeRecorder.h:147
std::map< std::string, ChannelInfo > ChannelMap
Typedef for ChannelInfo pointers.
Definition: TapeRecorder.h:152
std::deque< MessageToStore > mStorageQueue
Definition: TapeRecorder.h:339
std::list< BinarySerializerCodecPtr > codecs
Definition: TapeRecorder.h:92
TypeMetaPtr meta
Definition: TapeRecorder.h:146
ChannelMap mChannels
Definition: TapeRecorder.h:331
ChannelInfo()
Definition: TapeRecorder.h:141
void setMaxMessageBlockSize(uint32 maxMessageBlockSize)
Set the maximum size of the chunks.
Definition: TapeRecorder.h:267
int compressionLevel
Definition: TapeRecorder.h:93
Provides MIRA_CLASS_SERIALIZATION macro and includes the major headers of the serialization framework...
An object that allows read access to data of a channel.
Definition: ChannelReadWrite.h:440
boost::condition_variable mStorageThreadCondition
Definition: TapeRecorder.h:335
uint8 VersionType
Definition: ReflectorInterface.h:72
bool mFirstCallbackMessage
Definition: TapeRecorder.h:330
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:416
uint32 maxMessageBlockSize
Definition: TapeRecorder.h:125
uint64 recordCounter
Definition: TapeRecorder.h:145
#define MIRA_FRAMEWORK_EXPORT
Definition: FrameworkExports.h:61
void setWarnOfDataLoss(bool enable)
Set whether detected data loss should show a warning.
Definition: TapeRecorder.h:278
sec_type seconds() const
Returns normalized number of seconds (0..59)
Definition: Time.h:278
Use this class to represent time durations.
Definition: Time.h:104
The common header for all stamped data.
Definition: Stamped.h:62
Informations about a recorded channel.
Definition: TapeRecorder.h:138
boost::thread mStorageThread
Definition: TapeRecorder.h:334
RecordedChannelInfoMap channels
Definition: TapeRecorder.h:123
void reflect(Reflector &r)
Definition: TapeRecorder.h:170
Authorities act as a facade to the framework.
Definition: Authority.h:94
bool mStorageThreadStarted
Definition: TapeRecorder.h:337
Definition: PerformanceStatistics.h:57
uint32 mMaxMessageBlockSize
Definition: TapeRecorder.h:326
Gather statistics of the flow rate per second.
bool mWarnDataLoss
Definition: TapeRecorder.h:327
boost::shared_ptr< TypeMeta > TypeMetaPtr
Definition: MetaSerializer.h:309
volatile bool mStorageThreadTerminationRequest
Definition: TapeRecorder.h:340
tick_type microseconds() const
Returns normalized number of microseconds (0..999)
Definition: Time.h:288
Tape mTape
Definition: TapeRecorder.h:328
bool timestampFilename
Definition: TapeRecorder.h:127
std::map< std::string, RecordedChannelInfo > RecordedChannelInfoMap
A map of recorded channel settings.
Definition: TapeRecorder.h:99
Contains all settings for a recorded channel like compression, codecs,...
Definition: TapeRecorder.h:62
bool isRecording() const
Returns whether TapeRecorder is currently gathering data after record was called. ...
Definition: TapeRecorder.h:292
uint32 seqID
Definition: TapeRecorder.h:143
Time expected
Definition: TapeRecorder.h:144
MIRA_DEPRECATED("Please use int compressionLevel(=0/-1) instead of bool compress(=false/true)", void addChannel(const std::string &channelID, const std::list< BinarySerializerCodecPtr > &codecs, bool compress, Duration interval=Duration::microseconds(0), bool avoidDataLoss=false))
Add a channel to the list of recorded channels.
Definition: TapeRecorder.h:189
Duration mInitialWaitForDataDuration
Definition: TapeRecorder.h:332
bool mIsRecording
Definition: TapeRecorder.h:329
#define MIRA_REFLECT_VERSION(reflector, versionNumber)
Macro that should be used to specify the (reflected) version of the class.
Definition: ReflectorInterface.h:874
void reflect(Reflector &r)
Definition: TapeRecorder.h:110
PerformanceStatistics stats
Definition: TapeRecorder.h:148
bool recordTransforms
Definition: TapeRecorder.h:124
The framework that holds all manager classes and provides startup and shutdown of all framework relat...
bool getWarnOfDataLoss()
Query if data loss warning is enabled.
Definition: TapeRecorder.h:284
Typename typeName(bool cvqualify=true)
Returns a compiler and platform independent typename of T.
Definition: Typename.h:103
bool avoidDataLoss
Definition: TapeRecorder.h:95
void reflect(Reflector &r)
Definition: TapeRecorder.h:72