MIRA
TapePlayer.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_TAPEPLAYER_H_
48 #define _MIRA_TAPEPLAYER_H_
49 
51 #include <fw/Framework.h>
52 #include <fw/TapeVisitor.h>
53 
54 namespace mira {
55 
57 
63 {
64 public:
65 
66  TapePlayer();
67 
68  virtual ~TapePlayer()
69  {
70  stop();
71  mAuthority.checkout();
72  }
73 
74  template <typename Reflector>
75  void reflect(Reflector& r)
76  {}
77 
81  void pause()
82  {
83  mStepTo.reset();
84  mStepToChannel.reset();
85  mJumpTo.reset();
86  mPlayImmediately = false;
87  mPause = true;
88  }
89 
94  bool isPaused() const
95  {
96  return mPause;
97  }
98 
102  bool inStepTo() const
103  {
104  return mStepTo.is_initialized() || mStepToChannel.is_initialized();
105  }
106 
111  void step()
112  {
113  stepTo(Duration::microseconds(-1));
114  }
115 
122  void stepTo(Duration time);
123 
133  bool stepTo(const std::string& channelID);
134 
145  bool stepToAnyOf(const std::vector<std::string>& channelIDs);
146 
150  void stop()
151  {
152  mThread.interrupt();
153  mThread.join();
154  mIsPlaying = false;
155  mErrorMessage.reset();
156  }
157 
164  void setLoop(bool loop, int32 count = -1)
165  {
166  mLoopCount = count;
167  mLoop = loop;
168  }
169 
177  void setTimeScaler(float scaler)
178  {
179  assert(scaler >= 0.0f);
180  mTimeScaler = 1.0f/scaler;
181  }
182 
187  float getTimeScaler() const
188  {
189  return 1.0f/mTimeScaler;
190  }
191 
198  void load(TapeVisitor* iVisitor, Time startTime=Time::now(), const std::string& prefix = "");
199 
203  void play();
204 
209  bool isPlaying() const
210  {
211  return mIsPlaying;
212  }
213 
220  bool hasError() const
221  {
222  return mErrorMessage.is_initialized();
223  }
224 
228  std::string getErrorMessage() const
229  {
230  if (mErrorMessage)
231  return *mErrorMessage;
232  return "";
233  }
234 
242  {
243  if (mVisitor == NULL)
244  return Duration::nanoseconds(0);
245  if (mMessage == mVisitor->end())
246  return mVisitor->getLastMessageTimeOffset();
247  return mMessage.getTimeOffset();
248  }
249 
257  {
258  if (mVisitor == NULL)
259  return Duration::nanoseconds(0);
260  if (mMessage == mVisitor->end())
261  return mVisitor->getLastMessageTZOffset();
262  return mMessage.getTZOffset();
263  }
264 
270  {
271  return mStartTime;
272  }
273 
280  void jumpTo(Duration time);
281 
286  {
287  return &mAuthority;
288  }
289 
290 protected:
291 
292  void stepTo(const TapeVisitor::iterator& i);
293  void doPlay();
294 
295 protected:
296 
298  boost::thread mThread;
299  int32 mLoopCount;
300  bool mLoop;
301  bool mPause;
304  float mTimeScaler;
306  boost::optional<Duration> mJumpTo;
307  boost::optional<Duration> mStepTo;
308  boost::optional<TapeVisitor::iterator> mStepToChannel;
309  boost::optional<std::string> mErrorMessage;
312  boost::mutex mConditionMutex;
313  boost::condition_variable mCondition;
314  std::string mNamespacePrefix;
315 };
316 
318 
319 }
320 
321 #endif
float getTimeScaler() const
Get the time scaling factor.
Definition: TapePlayer.h:187
void setTimeScaler(float scaler)
Set the time scaling factor for play back.
Definition: TapePlayer.h:177
bool mPlayImmediately
Definition: TapePlayer.h:302
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Authority * getAuthority()
Return the authority of the player.
Definition: TapePlayer.h:285
boost::optional< Duration > mJumpTo
Definition: TapePlayer.h:306
boost::thread mThread
Definition: TapePlayer.h:298
bool isPaused() const
Is the playback paused.
Definition: TapePlayer.h:94
bool mLoop
Definition: TapePlayer.h:300
TapeVisitor * mVisitor
Definition: TapePlayer.h:310
Time getRelativeStartTime() const
Returns the relative time the player plays back messages to.
Definition: TapePlayer.h:269
Provides MIRA_CLASS_SERIALIZATION macro and includes the major headers of the serialization framework...
void setLoop(bool loop, int32 count=-1)
Turn the loop mode on/off.
Definition: TapePlayer.h:164
Iterator to iterate over all messages in all tapes visited by the visitor.
Definition: TapeVisitor.h:133
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:416
float mTimeScaler
Definition: TapePlayer.h:304
boost::optional< Duration > mStepTo
Definition: TapePlayer.h:307
Authority mAuthority
Definition: TapePlayer.h:297
boost::mutex mConditionMutex
Definition: TapePlayer.h:312
#define MIRA_FRAMEWORK_EXPORT
Definition: FrameworkExports.h:61
int32 mLoopCount
Definition: TapePlayer.h:299
Duration getCurrentMessageTZOffset() const
Get the recorded time zone offset of the tape containing the currently played or to be played message...
Definition: TapePlayer.h:256
Use this class to represent time durations.
Definition: Time.h:104
Authorities act as a facade to the framework.
Definition: Authority.h:94
boost::optional< TapeVisitor::iterator > mStepToChannel
Definition: TapePlayer.h:308
virtual ~TapePlayer()
Definition: TapePlayer.h:68
tick_type microseconds() const
Returns normalized number of microseconds (0..999)
Definition: Time.h:288
boost::condition_variable mCondition
Definition: TapePlayer.h:313
Visitor class to inspect tapes by channels and/or time interval.
Definition: TapeVisitor.h:66
Time mStartTime
Definition: TapePlayer.h:305
Class for playing back tape files.
Definition: TapePlayer.h:62
void reflect(Reflector &r)
Definition: TapePlayer.h:75
static Time now() static Time eternity()
Returns the current utc based time.
Definition: Time.h:479
void step()
Do a single step.
Definition: TapePlayer.h:111
bool mIsPlaying
Definition: TapePlayer.h:303
std::string getErrorMessage() const
Returns the error message of an error that occurred during play back.
Definition: TapePlayer.h:228
bool hasError() const
When an error occurs during play back this method will return true.
Definition: TapePlayer.h:220
tick_type nanoseconds() const
Returns normalized number of nanoseconds (0..999)
Definition: Time.h:293
void pause()
Pauses the play back.
Definition: TapePlayer.h:81
Class implementing a visitor concept for tapes.
boost::optional< std::string > mErrorMessage
Definition: TapePlayer.h:309
bool inStepTo() const
return if we are currently in stepTo mode
Definition: TapePlayer.h:102
The framework that holds all manager classes and provides startup and shutdown of all framework relat...
void stop()
Stops play back.
Definition: TapePlayer.h:150
bool mPause
Definition: TapePlayer.h:301
std::string mNamespacePrefix
Definition: TapePlayer.h:314
TapeVisitor::iterator mMessage
Definition: TapePlayer.h:311
Duration getCurrentMessageTimeOffset() const
Get the recorded time offset of the currently played or to be played message (if currently paused the...
Definition: TapePlayer.h:241
bool isPlaying() const
Are we currently in playback mode (i.e.
Definition: TapePlayer.h:209