OpenWalnut  1.4.0
WGEScreenCapture.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WGESCREENCAPTURE_H
26 #define WGESCREENCAPTURE_H
27 
28 #include <limits>
29 
30 #ifndef Q_MOC_RUN
31 #include <boost/signals2.hpp>
32 #endif
33 #ifndef Q_MOC_RUN
34 #include <boost/function.hpp>
35 #endif
36 
37 #include <osg/Camera>
38 #include <osg/Image>
39 #include <osg/RenderInfo>
40 
41 #include "../common/WSharedObject.h"
42 #include "../common/WCondition.h"
43 
44 #include "WGECamera.h"
45 
46 #include "animation/WGEAnimationFrameTimer.h"
47 
48 
49 
50 /**
51  * This class is a screen recorder. It records the frame buffer to files on a per-frame-basis. This class is NOT thread-safe due to performance
52  * reasons. You should not distribute the instance among multiple threads. It can be applied to <b>ONE</b> camera only by setting it as
53  * finalDrawCallback (WGECamera::setFinalDrawCallback). Each camera can only use ONE final draw callback.
54  *
55  * This class is abstract. Derive your own class and handle image writing.
56  *
57  * \note This class does NOT write the images to disk. Set a callback for this.
58  *
59  * \ingroup GE
60  */
61 class WGEScreenCapture: public WGECamera::DrawCallback
62 {
63 public:
64  /**
65  * Keeps track of several frame-counts.
66  */
67  typedef struct
68  {
69  size_t m_frames; //!< current number of frames.
70  size_t m_framesLeft; //!< the frames to take until stop.
71  }
73 
74  /**
75  * The shared access type to the FrameCounting struct.
76  */
78 
79  /**
80  * Convenience typedef
81  */
82  typedef osg::ref_ptr< WGEScreenCapture > RefPtr;
83 
84  /**
85  * Convenience typedef
86  */
87  typedef osg::ref_ptr< const WGEScreenCapture > ConstRefPtr;
88 
89  /**
90  * This callback signature is needed to subscribe to the handleImage signal.
91  */
92  typedef boost::function< void( size_t, size_t, osg::ref_ptr< osg::Image > ) > HandleImageCallbackType;
93 
94  /**
95  * Creates a screen capture callback.
96  */
98 
99  /**
100  * Destructor. Cleans up.
101  */
102  virtual ~WGEScreenCapture();
103 
104  /**
105  * Starts recording. If it already is running, nothing happens.
106  */
107  void recordStart();
108 
109  /**
110  * Stops recording. If not recording, nothing happens.
111  */
112  void recordStop();
113 
114  /**
115  * Checks if there are frames left for recording.
116  *
117  * \return true if yes.
118  */
119  bool isRecording();
120 
121  /**
122  * Makes a screenshot with the <b>next</b> frame. This is a shortcut for record( 1 ).
123  */
124  void screenshot();
125 
126  /**
127  * Resets the frame-counter to 0.
128  */
129  void resetFrameCounter();
130 
131  /**
132  * The draw callback operator. Gets called by OSG in draw traversal.
133  *
134  * \param renderInfo the OSG renderinfo
135  */
136  virtual void operator()( osg::RenderInfo& renderInfo ) const; // NOLINT - osg wants this to be a non-const reference
137 
138  /**
139  * The condition returned here is actually the change condition of the frame counter. This can be used to update GUI or something as it
140  * contains frame-counts, recording information and so on (updated per frame).
141  *
142  * \return the condition
143  */
145 
146  /**
147  * Returns the current recording information. Release the lock after you grabbed the info you need.
148  *
149  * \return the info struct - read ticket
150  */
152 
153  /**
154  * Returns a timer getting ticked on each recorded frame. This can then be used for animations for example.
155  *
156  * \return the timer.
157  */
159 
160  /**
161  * Subscribes a specified function to the new-image-signal. This gets emitted every time a new image was grabbed.
162  *
163  * \param callback the callback
164  *
165  * \return the connection.
166  */
167  boost::signals2::connection subscribeSignal( HandleImageCallbackType callback );
168 
169 protected:
170  /**
171  * The function handles new images. Implement it.
172  *
173  * \param framesLeft how much frames to come
174  * \param totalFrames the total number of frames until now
175  * \param image the image
176  */
177  virtual void handleImage( size_t framesLeft, size_t totalFrames, osg::ref_ptr< osg::Image > image ) const;
178 
179  /**
180  * Starts recording. If already recording, it continues recording.
181  *
182  * \param frames the number of frames to record. 0 means stop, 1 is a single screenshot.
183  */
184  void record( size_t frames = std::numeric_limits< size_t >::max() );
185 
186 private:
187  /**
188  * Counts the frames to take.
189  */
190  SharedRecordingInformation m_recordingInformation;
191 
192  /**
193  * The frame timer. Ticket on each recorded frame.
194  */
196 
197  /**
198  * The type of the signal for handleSignal.
199  */
200  typedef boost::signals2::signal< void( size_t, size_t, osg::ref_ptr< osg::Image > ) > HandleImageSignalType;
201 
202  /**
203  * The signal emitted on every new grabbed image.
204  */
206 };
207 
208 #endif // WGESCREENCAPTURE_H
SharedRecordingInformation m_recordingInformation
Counts the frames to take.
This class is a screen recorder.
void recordStart()
Starts recording.
Keeps track of several frame-counts.
boost::shared_ptr< WGEAnimationFrameTimer > SPtr
Convenience typedef for a shared_ptr.
void record(size_t frames=std::numeric_limits< size_t >::max())
Starts recording.
boost::shared_ptr< const WCondition > ConstSPtr
Const shared pointer type for WCondition.
Definition: WCondition.h:59
virtual void operator()(osg::RenderInfo &renderInfo) const
The draw callback operator.
boost::shared_ptr< const WGEAnimationFrameTimer > ConstSPtr
Convenience typedef for a const shared_ptr.
virtual void handleImage(size_t framesLeft, size_t totalFrames, osg::ref_ptr< osg::Image > image) const
The function handles new images.
osg::ref_ptr< WGEScreenCapture > RefPtr
Convenience typedef.
WCondition::ConstSPtr getRecordCondition() const
The condition returned here is actually the change condition of the frame counter.
boost::function< void(size_t, size_t, osg::ref_ptr< osg::Image >) > HandleImageCallbackType
This callback signature is needed to subscribe to the handleImage signal.
void recordStop()
Stops recording.
size_t m_framesLeft
the frames to take until stop.
SharedRecordingInformation::ReadTicket getRecordingInformation() const
Returns the current recording information.
virtual ~WGEScreenCapture()
Destructor.
HandleImageSignalType m_signalHandleImage
The signal emitted on every new grabbed image.
void screenshot()
Makes a screenshot with the next frame.
void resetFrameCounter()
Resets the frame-counter to 0.
boost::signals2::connection subscribeSignal(HandleImageCallbackType callback)
Subscribes a specified function to the new-image-signal.
bool isRecording()
Checks if there are frames left for recording.
osg::ref_ptr< const WGEScreenCapture > ConstRefPtr
Convenience typedef.
WGEScreenCapture()
Creates a screen capture callback.
WGEAnimationFrameTimer::SPtr m_timer
The frame timer.
size_t m_frames
current number of frames.
boost::signals2::signal< void(size_t, size_t, osg::ref_ptr< osg::Image >) > HandleImageSignalType
The type of the signal for handleSignal.
WGEAnimationFrameTimer::ConstSPtr getFrameTimer() const
Returns a timer getting ticked on each recorded frame.
boost::shared_ptr< WSharedObjectTicketRead< RecordingInformation > > ReadTicket
Type for read tickets.
Definition: WSharedObject.h:64
WSharedObject< RecordingInformation > SharedRecordingInformation
The shared access type to the FrameCounting struct.