H3D API  2.4.1
H3DVideoClipDecoderNode.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
28 //
30 #ifndef __H3DVIDEOCLIPDECODERNODE_H__
31 #define __H3DVIDEOCLIPDECODERNODE_H__
32 
33 #include <H3D/Node.h>
34 #include <H3DUtil/Image.h>
35 #include <list>
36 
37 namespace H3D {
38 
43  class H3DAPI_API H3DVideoClipDecoderNode : public Node {
44  public:
45  typedef enum {
46  PLAYING = 0,
47  STOPPED = 1,
48  PAUSED = 2
49  } PlayStatus;
50 
51  typedef H3DVideoClipDecoderNode*( *CreateNodeFunc)();
52 
54  typedef bool ( *SupportsFileFunc)( const string &url );
55 
56  template< class N >
57  static H3DVideoClipDecoderNode *newImageLoaderNode() { return new N; }
58 
60  struct H3DAPI_API DecoderRegistration{
61  public:
63  DecoderRegistration( const string &_name,
64  CreateNodeFunc _create,
65  SupportsFileFunc _supports ):
66  name( _name ),
67  create_func( _create ),
68  supports_func( _supports ) {
69 
70  if( !H3DVideoClipDecoderNode::initialized ) {
71  H3DVideoClipDecoderNode::registered_decoders.reset(
72  new list< DecoderRegistration >);
73  initialized = true;
74  }
76  }
77 
78  string name;
79  CreateNodeFunc create_func;
80  SupportsFileFunc supports_func;
81  };
82 #ifdef __BORLANDC__
83  friend struct DecoderRegistration;
84 #endif
85 
88  status( STOPPED ) {
89  type_name = "H3DVideoClipDecoderNode";
90  }
91 
94  virtual bool loadClip( const string &url ) = 0;
95 
97  virtual bool haveNewFrame() = 0;
98 
100  virtual void getNewFrame( unsigned char *buffer ) = 0;
101 
103  virtual unsigned int getFrameSize() = 0;
104 
106  virtual unsigned int getFrameWidth() = 0;
107 
109  virtual unsigned int getFrameHeight() = 0;
110 
113 
115  virtual unsigned int getFrameByteAlignment() {
116  return 1;
117  }
118 
121 
123  virtual unsigned int getFrameBitsPerPixel() = 0;
124 
126  virtual void startPlaying() = 0;
127 
130  virtual void stopPlaying() = 0;
131 
133  virtual void pausePlaying() = 0;
134 
137  virtual void setLooping( bool on ) = 0;
138 
140  virtual H3DTime getPosition() = 0;
141 
143  virtual void setPosition( H3DTime pos ) = 0;
144 
148  virtual bool setRate( double r ) = 0;
149 
152  virtual H3DTime getDuration() = 0;
153 
155  inline PlayStatus getPlayStatus() { return status; }
156 
160  virtual string defaultXMLContainerField() {
161  return "decoder";
162  }
163 
168  static H3DVideoClipDecoderNode *getSupportedDecoder( const string &url );
169 
176  static void registerDecoder( const string &name,
177  CreateNodeFunc create,
178  SupportsFileFunc supports ) {
179  registerDecoder( DecoderRegistration( name, create, supports ) );
180  }
181 
184  static void registerDecoder( const DecoderRegistration &fr ) {
185  registered_decoders->push_back( fr );
186  }
187 
188 
189  // Creating a new auto_ptr local for this node, because
190  // registered_decoders caused a memory leak and because
191  // of the order of setting the static variables the autp_ptr's
192  // constructor resets the auto_ptr to 0 eventhough the
193  // registered_decoders has been initilazed, and therefore
194  // cause an error making it imposible to use the standard auto_ptr.
195  template<class T>
196  class local_auto_ptr{
197  private:
198  T* ap; // refers to the actual owned object (if any)
199  public:
200  typedef T element_type;
201 
202  // constructor
203  explicit local_auto_ptr (T* ptr = 0) {
204  if(!initialized){
205  ap=ptr;
206  }
207  }
208 
209  // copy constructors (with implicit conversion)
210  // - note: nonconstant parameter
211  local_auto_ptr (local_auto_ptr& rhs) throw() : ap(rhs.release()) { }
212 
213  template<class Y>
214  local_auto_ptr (local_auto_ptr<Y>& rhs) throw() : ap(rhs.release()) { }
215 
216  // assignments (with implicit conversion)
217  // - note: nonconstant parameter
218  local_auto_ptr& operator= (local_auto_ptr& rhs) throw(){
219  if(!initialized){
220  reset(rhs.release());
221  return *this;
222  }
223  }
224  template<class Y>
225  local_auto_ptr& operator= (local_auto_ptr<Y>& rhs) throw(){
226  if(!initialized){
227  reset(rhs.release());
228  return *this;
229  }
230  }
231 
232  // destructor
233  ~local_auto_ptr() throw(){
234  delete ap;
235  }
236 
237  // value access
238  T* get() const throw(){
239  return ap;
240  }
241  T& operator*() const throw(){
242  return *ap;
243  }
244  T* operator->() const throw(){
245  return ap;
246  }
247 
248  // release ownership
249  T* release() throw(){
250  if(!initialized){
251  T* tmp(ap);
252  ap = 0;
253  return tmp;
254  }
255  }
256 
257  // reset value
258  void reset (T* ptr=0) throw(){
259  if(!initialized){
260  if (ap != ptr){
261  delete ap;
262  ap = ptr;
263  }
264  }
265  }
266  };
267 
268  protected:
269  static local_auto_ptr< list< DecoderRegistration > > registered_decoders;
270  //static list< DecoderRegistration > *registered_decoders;
271 
272  static bool initialized;
273  PlayStatus status;
274  };
275 }
276 
277 #endif
H3DUTIL_API void reset()
Node base class.
H3DVideoClipDecoderNode is a virtual base class for classes decoding video clips making new frames av...
Definition: H3DVideoClipDecoderNode.h:43
virtual unsigned int getFrameBitsPerPixel()=0
Get the number of bits per pixel in the current frame.
virtual unsigned int getFrameSize()=0
The size in bytes of the current frame.
virtual bool loadClip(const string &url)=0
Pure virtual function to load an video clip from a url.
virtual bool setRate(double r)=0
Set the playback rate.
static void registerDecoder(const string &name, CreateNodeFunc create, SupportsFileFunc supports)
Register a decoder that can then be returned by getSupportedDecoder().
Definition: H3DVideoClipDecoderNode.h:176
virtual void setLooping(bool on)=0
Set whether the clip should loop and start from the start again when the end has been reached.
static void registerDecoder(const DecoderRegistration &fr)
Register a file reader that can then be returned by getSupportedDecoder().
Definition: H3DVideoClipDecoderNode.h:184
virtual void pausePlaying()=0
Pause the decoding of the video clip.
H3DVideoClipDecoderNode()
Constructor.
Definition: H3DVideoClipDecoderNode.h:87
virtual string defaultXMLContainerField()
Returns the default xml containerField attribute value.
Definition: H3DVideoClipDecoderNode.h:160
virtual void stopPlaying()=0
Stop decoding the video clip and set the position to the start position.
virtual void startPlaying()=0
Start decoding the video clip.
virtual void setPosition(H3DTime pos)=0
Set the current position in the clip(in seconds from start position)
virtual unsigned int getFrameWidth()=0
The width in pixels of the current frame.
virtual bool haveNewFrame()=0
Returns true when a new frame is available.
virtual Image::PixelType getFramePixelType()=0
The pixel type of the current frame.
virtual unsigned int getFrameByteAlignment()
The byte alignment of each row in the buffer data from getNewFrame.
Definition: H3DVideoClipDecoderNode.h:115
virtual Image::PixelComponentType getFramePixelComponentType()=0
The pixel component type of the current frame.
PlayStatus getPlayStatus()
Returns the current play status.
Definition: H3DVideoClipDecoderNode.h:155
virtual void getNewFrame(unsigned char *buffer)=0
Get the new frame. The buffer must be at least getFrameSize() bytes.
virtual H3DTime getDuration()=0
Returns the duration in seconds at normal play rate of the currently loaded video clip.
virtual unsigned int getFrameHeight()=0
The height in pixels of the current frame.
virtual H3DTime getPosition()=0
Get the current position in the clip (in seconds from start position)
Node is the base class for all classes that can be part of the H3D scene-graph.
Definition: Node.h:46
DualQuaternion operator*(const DualQuaternion &q1, const DualQuaternion &q2)
double H3DTime
H3D API namespace.
Definition: Anchor.h:38
Class used to register a class to the registered file readers.
Definition: H3DVideoClipDecoderNode.h:60
DecoderRegistration(const string &_name, CreateNodeFunc _create, SupportsFileFunc _supports)
Constructor.
Definition: H3DVideoClipDecoderNode.h:63