H3D API  2.4.1
DirectShowDecoder.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 //
27 //
29 
30 #ifndef __DIRECTSHOWDECODER_H__
31 #define __DIRECTSHOWDECODER_H__
32 
33 #include <H3D/FieldTemplates.h>
34 #include <H3D/SFTime.h>
35 
36 #ifdef HAVE_DSHOW
37 
38 // Include from DirectShow BaseClasses
39 // Remove the DEBUG defines when including DShow classes in order
40 // to always use the release version. The debug version caused us
41 // problems.
42 #ifdef _DEBUG
43 #define RESTORE__DEBUG
44 #undef _DEBUG
45 #endif
46 
47 #ifdef DEBUG
48 #define RESTORE_DEBUG
49 #undef DEBUG
50 #endif
51 #include <streams.h>
52 #include <atlcomcli.h>
53 #include <strsafe.h>
54 
55 #ifdef RESTORE__DEBUG
56 #define _DEBUG
57 #undef RESTORE__DEBUG
58 #endif
59 
60 #ifdef RESTORE_DEBUG
61 #define DEBUG
62 #undef RESTORE_DEBUG
63 #endif
64 
66 
67 namespace H3D {
68 
69 struct __declspec(uuid("{71771540-2017-11cf-ae26-0020afd79767}")) CLSID_TextureRenderer;
70 
76  class H3DAPI_API DirectShowDecoder : public H3DVideoClipDecoderNode {
77 
79  class DShowEventHandler: public AutoUpdate< SFTime > {
80  virtual void update();
81  };
82 
85  class CFrameGrabber: public CBaseVideoRenderer {
86  public:
88  CFrameGrabber(LPUNKNOWN pUnk, HRESULT *phr, DirectShowDecoder *d );
89 
90  // CBaseVideoRenderer functions
91 
93  HRESULT CheckMediaType(const CMediaType *pmt );
94 
96  HRESULT SetMediaType(const CMediaType *pmt );
97 
99  HRESULT DoRenderSample(IMediaSample *pMediaSample);
100 
104  void OnReceiveFirstSample(IMediaSample *pSample) {
105  DoRenderSample( pSample );
106  }
107 
108  DirectShowDecoder *decoder;
109  };
110 
111  friend class CFrameGrabber;
112 
113  public:
115  DirectShowDecoder();
116 
118  ~DirectShowDecoder();
119 
121  virtual bool loadClip( const string &url );
122 
124  virtual void startPlaying();
125 
128  virtual void stopPlaying();
129 
131  virtual void pausePlaying();
132 
135  virtual void setLooping( bool v ) {
136  looping = v;
137  }
138 
140  virtual H3DTime getPosition();
141 
143  virtual void setPosition( H3DTime pos );
144 
149  virtual bool setRate( double r ) {
150  rate = r;
151  if( g_pMS ) {
152  HRESULT hr;
153  hr = g_pMS->SetRate( r );
154  return hr == S_OK;
155  }
156  return false;
157  }
158 
161  virtual H3DTime getDuration() {
162  return duration;
163  }
164 
166  virtual bool haveNewFrame() { return have_new_frame; }
167 
169  virtual void getNewFrame( unsigned char *buffer );
170 
172  virtual unsigned int getFrameWidth() { return frame_width; }
173 
175  virtual unsigned int getFrameHeight() { return frame_height; }
176 
178  virtual unsigned int getFrameBitsPerPixel() { return 24; }
179 
181  virtual unsigned int getFrameSize() {
182  return frame_size;
183  }
184 
186  virtual unsigned int getFrameByteAlignment() {
187  return 4;
188  }
189 
191  virtual Image::PixelType getFramePixelType() { return Image::BGR; }
192 
194  virtual Image::PixelComponentType getFramePixelComponentType() {
195  return Image::UNSIGNED;
196  }
197 
199  static H3DNodeDatabase database;
200 
202  static DecoderRegistration reader_registration;
203 
206  static bool supportsFileType( const string &url );
207  protected:
208  // DirectShow pointers
209  CComPtr<IGraphBuilder> g_pGB; // GraphBuilder
210  CComPtr<IMediaControl> g_pMC; // Media Control
211  CComPtr<IMediaSeeking> g_pMS; // Media Seeking
212  CComPtr<IMediaEvent> g_pME; // Media Event
213  CComPtr<IBaseFilter> g_pRenderer; // our custom renderer
214 
216  HRESULT initDShowTextureRenderer( const string &url );
217 
220  void cleanupDShow( void );
221 
223  unsigned int frame_width;
224 
226  unsigned int frame_height;
227 
229  unsigned int frame_size;
230 
232  unsigned int data_size;
233 
235  unsigned char *data;
236 
239  bool have_new_frame;
240 
242  double rate;
243 
245  CFrameGrabber *frame_grabber;
246 
248  bool looping;
249 
251  H3DTime duration;
252 
254  H3DUniquePtr< DShowEventHandler > event_handler;
255  };
256 
257 }
258 #endif
259 #endif
Contains different templates to modify field behaviour.
Header file for H3DVideoClipDecoderNode, the abstract base class for all nodes the decodes video clip...
Contains the SFTime field class.
H3D API namespace.
Definition: Anchor.h:38