ViSP  2.8.0
vpDirectShowGrabberImpl.h
1 /****************************************************************************
2  *
3  * $Id: vpDirectShowGrabberImpl.h 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * DirectShow framegrabber Implementation.
36  *
37  * Authors:
38  * Bruno Renier
39  * Anthony Saunier
40  *
41  *****************************************************************************/
42 
43 #ifndef vpDirectShowGrabberImpl_hh
44 #define vpDirectShowGrabberImpl_hh
45 
46 #ifndef DOXYGEN_SHOULD_SKIP_THIS
47 
48 #include <visp/vpConfig.h>
49 #if ( defined(VISP_HAVE_DIRECTSHOW) )
50 
51 #include <atlbase.h>
52 #include <qedit.h>
53 #include <dshow.h>
54 
55 #include <visp/vpFrameGrabber.h>
56 #include <visp/vpFrameGrabberException.h>
57 
58 #include <visp/vpDirectShowSampleGrabberI.h>
59 #include <visp/vpDirectShowDevice.h>
60 #include <visp/vpDebug.h>
71 class VISP_EXPORT vpDirectShowGrabberImpl : public vpFrameGrabber
72 {
73  static const int MAX_DELAY = 10000;
74  static const int MAX_DEVICES = 10;
75 
76 
77  public:
78 
82 /*
83  typedef enum {
84  //Known RGB formats
85  vpMEDIASUBTYPE_ARGB32 = MEDIASUBTYPE_ARGB32,
86  vpMEDIASUBTYPE_RGB32 = MEDIASUBTYPE_RGB32,
87  vpMEDIASUBTYPE_RGB24 = MEDIASUBTYPE_RGB24,
88  vpMEDIASUBTYPE_RGB555 = MEDIASUBTYPE_RGB555,
89  vpMEDIASUBTYPE_RGB565 = MEDIASUBTYPE_RGB565,
90  vpMEDIASUBTYPE_RGB8 = MEDIASUBTYPE_RGB8,
91  vpMEDIASUBTYPE_RGB4 = MEDIASUBTYPE_RGB4,
92  vpMEDIASUBTYPE_RGB1 = MEDIASUBTYPE_RGB1,
93  //Known YUV formats
94  vpMEDIASUBTYPE_AYUV = MEDIASUBTYPE_AYUV,
95  vpMEDIASUBTYPE_UYVY = MEDIASUBTYPE_UYVY,
96  vpMEDIASUBTYPE_Y411 = MEDIASUBTYPE_Y411,
97  vpMEDIASUBTYPE_Y41P = MEDIASUBTYPE_Y41P,
98  vpMEDIASUBTYPE_Y211 = MEDIASUBTYPE_Y211,
99  vpMEDIASUBTYPE_YUY2 = MEDIASUBTYPE_YUY2,
100  vpMEDIASUBTYPE_YVYU = MEDIASUBTYPE_YVYU,
101  vpMEDIASUBTYPE_YUYV = MEDIASUBTYPE_YUYV,
102  vpMEDIASUBTYPE_IF09 = MEDIASUBTYPE_IF09,
103  vpMEDIASUBTYPE_IYUV = MEDIASUBTYPE_IYUV,
104  vpMEDIASUBTYPE_YV12 = MEDIASUBTYPE_YV12,
105  vpMEDIASUBTYPE_YVU9 = MEDIASUBTYPE_YVU9
106  } vpDirectShowMediaSubtype;
107 */
108 
109  vpDirectShowGrabberImpl();
110  virtual ~vpDirectShowGrabberImpl();
111 
112  void open();
113  void open(vpImage<unsigned char> &I);
114  void open(vpImage<vpRGBa> &I);
115 
117  void acquire(vpImage<vpRGBa> &I);
118 
119  void close();
120 
124  unsigned int getDeviceNumber() {return nbDevices;}
125 
126  //change the capture device
127  bool setDevice(unsigned int id);
128 
129  //displays a list of available devices
130  void displayDevices();
131 
132  //set image size
133  bool setImageSize(unsigned int width,unsigned int height);
134 
135  //set capture framerate
136  bool setFramerate(double framerate);
137 
138  //set capture format
139  bool setFormat(unsigned int width,unsigned int height, double framerate);
140 
141  //get capture format
142  void getFormat(unsigned int &width,unsigned int &height, double &framerate);
143 
144  //set capture MediaType
145  bool setMediaType(int mediaTypeID);
146 
147  //get current capture MediaType
148  int getMediaType();
149 
150  //Get the available capture formats
151  bool getStreamCapabilities();
152 
153 
154  private:
155 
156  CComPtr<IGraphBuilder> pGraph; //our DS filter graph
157 
158  CComPtr<ICaptureGraphBuilder2> pBuild; //the interface to the capture graph builder
159  //used to build the filter graph
160 
161  CComPtr<IBaseFilter> pCapSource; //the capture source filter
162 
163  CComPtr<ISampleGrabber> pGrabberI; //the sample grabber's interface and filter
164  CComPtr<IBaseFilter> pGrabberFilter;
165 
166  CComPtr<IMediaControl> pControl; //The DS filter graph control interface
167  CComPtr<IMediaEvent> pEvent; //The DS filter graph event interface
168 
169  vpDirectShowSampleGrabberI sgCB; //Interface used to implement the frame grabber callback
170 
171  HRESULT hr; //contains the result of the last operation
172 
173  static vpDirectShowDevice * deviceList; //This contains the list of the available capture devices
174  //it is shared by all the DirectShow Grabbers
175 
176  static unsigned int nbDevices; //the number of available devices
177  int currentDevice; //the number of the current device
178 
179  // flag to manage CoInitialize() and CoUnInitialze()
180  bool initCo ;
181  //setup the directshow filtergraph with the first available device
182  bool initDirectShow();
183 
184  //enumerates the different video inputs
185  bool enumerate(CComPtr<IEnumMoniker>& ppVideoInputEnum);
186 
187  //selects a random video input from the enumeration and returns the associated filter
188  bool selectRandomSource(CComPtr<IEnumMoniker>& ppVideoInputEnum, CComPtr<IBaseFilter>& pCapSource);
189 
190  //creates the filter graph
191  bool createGraph();
192 
193  //creates the sample grabber
194  bool createSampleGrabber(CComPtr<IBaseFilter>& ppGrabberFilter);
195 
196  //checks the capture filter's media type and sets flags as needed
197  bool checkSourceType(CComPtr<IPin>& pCapSourcePin);
198 
199  //connects the filters as needed
200  bool connectSourceToGrabber(CComPtr<IBaseFilter>& pCapSource, CComPtr<IBaseFilter>& pGrabberFilter);
201 
202  //used to convert HRESULT-associated error message to a string
203  void HRtoStr(std::string str);
204 
205  //create the list of the available devices
206  bool createDeviceList(CComPtr<IEnumMoniker>& ppVideoInputEnum);
207 
208  //get the n-th device if it is available
209  bool getDevice(unsigned int n, CComPtr<IBaseFilter>& ppCapSource);
210 
211  //get the first available device if any
212  unsigned int getFirstUnusedDevice(CComPtr<IBaseFilter>& ppDevice);
213 
214  //removes all the filters in the graph
215  bool removeAll();
216 
217  //Deletes an allocated AM_MEDIA_TYPE structure, including the format block
218  void MyDeleteMediaType(AM_MEDIA_TYPE *pmt);
219 
220  //Frees the format block in an AM_MEDIA_TYPE structure
221  void MyFreeMediaType(AM_MEDIA_TYPE& mt);
222 
223 };
224 
225 #endif
226 #endif
227 #endif
virtual void open(vpImage< unsigned char > &I)=0
Base class for all video devices. It is designed to provide a front end to video sources.
virtual void close()=0
virtual void acquire(vpImage< unsigned char > &I)=0