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