OpenWalnut  1.5.0dev
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 #include <boost/signals2.hpp>
31 #include <boost/function.hpp>
32 
33 #include <osg/Camera>
34 #include <osg/Image>
35 #include <osg/RenderInfo>
36 
37 #include "../common/WSharedObject.h"
38 #include "../common/WCondition.h"
39 
40 #include "WGECamera.h"
41 
42 #include "animation/WGEAnimationFrameTimer.h"
43 
44 
45 
46 /**
47  * 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
48  * reasons. You should not distribute the instance among multiple threads. It can be applied to <b>ONE</b> camera only by setting it as
49  * finalDrawCallback (WGECamera::setFinalDrawCallback). Each camera can only use ONE final draw callback.
50  *
51  * This class is abstract. Derive your own class and handle image writing.
52  *
53  * \note This class does NOT write the images to disk. Set a callback for this.
54  *
55  * \ingroup GE
56  */
57 class WGEScreenCapture: public WGECamera::DrawCallback
58 {
59 public:
60  /**
61  * Keeps track of several frame-counts.
62  */
63  typedef struct
64  {
65  size_t m_frames; //!< current number of frames.
66  size_t m_framesLeft; //!< the frames to take until stop.
67  }
69 
70  /**
71  * The shared access type to the FrameCounting struct.
72  */
74 
75  /**
76  * Convenience typedef
77  */
78  typedef osg::ref_ptr< WGEScreenCapture > RefPtr;
79 
80  /**
81  * Convenience typedef
82  */
83  typedef osg::ref_ptr< const WGEScreenCapture > ConstRefPtr;
84 
85  /**
86  * This callback signature is needed to subscribe to the handleImage signal.
87  */
88  typedef boost::function< void( size_t, size_t, osg::ref_ptr< osg::Image > ) > HandleImageCallbackType;
89 
90  /**
91  * Creates a screen capture callback.
92  */
94 
95  /**
96  * Destructor. Cleans up.
97  */
98  virtual ~WGEScreenCapture();
99 
100  /**
101  * Starts recording. If it already is running, nothing happens.
102  */
103  void recordStart();
104 
105  /**
106  * Stops recording. If not recording, nothing happens.
107  */
108  void recordStop();
109 
110  /**
111  * Checks if there are frames left for recording.
112  *
113  * \return true if yes.
114  */
115  bool isRecording();
116 
117  /**
118  * Makes a screenshot with the <b>next</b> frame. This is a shortcut for record( 1 ).
119  */
120  void screenshot();
121 
122  /**
123  * Resets the frame-counter to 0.
124  */
125  void resetFrameCounter();
126 
127  /**
128  * The draw callback operator. Gets called by OSG in draw traversal.
129  *
130  * \param renderInfo the OSG renderinfo
131  */
132  virtual void operator()( osg::RenderInfo& renderInfo ) const; // NOLINT - osg wants this to be a non-const reference
133 
134  /**
135  * The condition returned here is actually the change condition of the frame counter. This can be used to update GUI or something as it
136  * contains frame-counts, recording information and so on (updated per frame).
137  *
138  * \return the condition
139  */
141 
142  /**
143  * Returns the current recording information. Release the lock after you grabbed the info you need.
144  *
145  * \return the info struct - read ticket
146  */
148 
149  /**
150  * Returns a timer getting ticked on each recorded frame. This can then be used for animations for example.
151  *
152  * \return the timer.
153  */
155 
156  /**
157  * Subscribes a specified function to the new-image-signal. This gets emitted every time a new image was grabbed.
158  *
159  * \param callback the callback
160  *
161  * \return the connection.
162  */
163  boost::signals2::connection subscribeSignal( HandleImageCallbackType callback );
164 
165 protected:
166  /**
167  * The function handles new images. Implement it.
168  *
169  * \param framesLeft how much frames to come
170  * \param totalFrames the total number of frames until now
171  * \param image the image
172  */
173  virtual void handleImage( size_t framesLeft, size_t totalFrames, osg::ref_ptr< osg::Image > image ) const;
174 
175  /**
176  * Starts recording. If already recording, it continues recording.
177  *
178  * \param frames the number of frames to record. 0 means stop, 1 is a single screenshot.
179  */
180  void record( size_t frames = std::numeric_limits< size_t >::max() );
181 
182 private:
183  /**
184  * Counts the frames to take.
185  */
187 
188  /**
189  * The frame timer. Ticket on each recorded frame.
190  */
192 
193  /**
194  * The type of the signal for handleSignal.
195  */
196  typedef boost::signals2::signal< void( size_t, size_t, osg::ref_ptr< osg::Image > ) > HandleImageSignalType;
197 
198  /**
199  * The signal emitted on every new grabbed image.
200  */
202 };
203 
204 #endif // WGESCREENCAPTURE_H
std::shared_ptr< const WCondition > ConstSPtr
Const shared pointer type for WCondition.
Definition: WCondition.h:53
std::shared_ptr< WGEAnimationFrameTimer > SPtr
Convenience typedef for a shared_ptr.
std::shared_ptr< const WGEAnimationFrameTimer > ConstSPtr
Convenience typedef for a const shared_ptr.
This class is a screen recorder.
void recordStop()
Stops recording.
void record(size_t frames=std::numeric_limits< size_t >::max())
Starts recording.
virtual ~WGEScreenCapture()
Destructor.
SharedRecordingInformation m_recordingInformation
Counts the frames to take.
boost::function< void(size_t, size_t, osg::ref_ptr< osg::Image >) > HandleImageCallbackType
This callback signature is needed to subscribe to the handleImage signal.
osg::ref_ptr< const WGEScreenCapture > ConstRefPtr
Convenience typedef.
WGEAnimationFrameTimer::ConstSPtr getFrameTimer() const
Returns a timer getting ticked on each recorded frame.
WCondition::ConstSPtr getRecordCondition() const
The condition returned here is actually the change condition of the frame counter.
void recordStart()
Starts recording.
osg::ref_ptr< WGEScreenCapture > RefPtr
Convenience typedef.
SharedRecordingInformation::ReadTicket getRecordingInformation() const
Returns the current recording information.
virtual void operator()(osg::RenderInfo &renderInfo) const
The draw callback operator.
WGEAnimationFrameTimer::SPtr m_timer
The frame timer.
void screenshot()
Makes a screenshot with the next frame.
boost::signals2::signal< void(size_t, size_t, osg::ref_ptr< osg::Image >) > HandleImageSignalType
The type of the signal for handleSignal.
WGEScreenCapture()
Creates a screen capture callback.
HandleImageSignalType m_signalHandleImage
The signal emitted on every new grabbed image.
virtual void handleImage(size_t framesLeft, size_t totalFrames, osg::ref_ptr< osg::Image > image) const
The function handles new images.
WSharedObject< RecordingInformation > SharedRecordingInformation
The shared access type to the FrameCounting struct.
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.
std::shared_ptr< WSharedObjectTicketRead< RecordingInformation > > ReadTicket
Type for read tickets.
Definition: WSharedObject.h:65
Keeps track of several frame-counts.
size_t m_framesLeft
the frames to take until stop.
size_t m_frames
current number of frames.