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