Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
vpDirectShowGrabberImpl.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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 *****************************************************************************/
35 
36 #ifndef vpDirectShowGrabberImpl_hh
37 #define vpDirectShowGrabberImpl_hh
38 
39 #ifndef DOXYGEN_SHOULD_SKIP_THIS
40 
41 #include <visp3/core/vpConfig.h>
42 #if (defined(VISP_HAVE_DIRECTSHOW))
43 
44 #include <atlbase.h>
45 #include <dshow.h>
46 #include <qedit.h>
47 
48 #include <visp3/core/vpFrameGrabber.h>
49 #include <visp3/core/vpFrameGrabberException.h>
50 
51 #include <visp3/core/vpDebug.h>
52 #include <visp3/sensor/vpDirectShowDevice.h>
53 #include <visp3/sensor/vpDirectShowSampleGrabberI.h>
54 
55 BEGIN_VISP_NAMESPACE
64 class VISP_EXPORT vpDirectShowGrabberImpl : public vpFrameGrabber
65 {
66  static const int MAX_DELAY = 10000;
67  static const int MAX_DEVICES = 10;
68 
69 public:
73  /*
74  typedef enum {
75  //Known RGB formats
76  vpMEDIASUBTYPE_ARGB32 = MEDIASUBTYPE_ARGB32,
77  vpMEDIASUBTYPE_RGB32 = MEDIASUBTYPE_RGB32,
78  vpMEDIASUBTYPE_RGB24 = MEDIASUBTYPE_RGB24,
79  vpMEDIASUBTYPE_RGB555 = MEDIASUBTYPE_RGB555,
80  vpMEDIASUBTYPE_RGB565 = MEDIASUBTYPE_RGB565,
81  vpMEDIASUBTYPE_RGB8 = MEDIASUBTYPE_RGB8,
82  vpMEDIASUBTYPE_RGB4 = MEDIASUBTYPE_RGB4,
83  vpMEDIASUBTYPE_RGB1 = MEDIASUBTYPE_RGB1,
84  //Known YUV formats
85  vpMEDIASUBTYPE_AYUV = MEDIASUBTYPE_AYUV,
86  vpMEDIASUBTYPE_UYVY = MEDIASUBTYPE_UYVY,
87  vpMEDIASUBTYPE_Y411 = MEDIASUBTYPE_Y411,
88  vpMEDIASUBTYPE_Y41P = MEDIASUBTYPE_Y41P,
89  vpMEDIASUBTYPE_Y211 = MEDIASUBTYPE_Y211,
90  vpMEDIASUBTYPE_YUY2 = MEDIASUBTYPE_YUY2,
91  vpMEDIASUBTYPE_YVYU = MEDIASUBTYPE_YVYU,
92  vpMEDIASUBTYPE_YUYV = MEDIASUBTYPE_YUYV,
93  vpMEDIASUBTYPE_IF09 = MEDIASUBTYPE_IF09,
94  vpMEDIASUBTYPE_IYUV = MEDIASUBTYPE_IYUV,
95  vpMEDIASUBTYPE_YV12 = MEDIASUBTYPE_YV12,
96  vpMEDIASUBTYPE_YVU9 = MEDIASUBTYPE_YVU9
97  } vpDirectShowMediaSubtype;
98  */
99 
100  vpDirectShowGrabberImpl();
101  virtual ~vpDirectShowGrabberImpl();
102 
103  void open();
104  void open(vpImage<unsigned char> &I);
105  void open(vpImage<vpRGBa> &I);
106 
108  void acquire(vpImage<vpRGBa> &I);
109 
110  void close();
111 
115  unsigned int getDeviceNumber() { return nbDevices; }
116 
117  // change the capture device
118  bool setDevice(unsigned int id);
119 
120  // displays a list of available devices
121  void displayDevices();
122 
123  // set image size
124  bool setImageSize(unsigned int width, unsigned int height);
125 
126  // set capture framerate
127  bool setFramerate(double framerate);
128 
129  // set capture format
130  bool setFormat(unsigned int width, unsigned int height, double framerate);
131 
132  // get capture format
133  void getFormat(unsigned int &width, unsigned int &height, double &framerate);
134 
135  // set capture MediaType
136  bool setMediaType(int mediaTypeID);
137 
138  // get current capture MediaType
139  int getMediaType();
140 
141  // Get the available capture formats
142  bool getStreamCapabilities();
143 
144 private:
145  CComPtr<IGraphBuilder> pGraph; // our DS filter graph
146 
147  CComPtr<ICaptureGraphBuilder2> pBuild; // the interface to the capture graph builder
148  // used to build the filter graph
149 
150  CComPtr<IBaseFilter> pCapSource; // the capture source filter
151 
152  CComPtr<ISampleGrabber> pGrabberI; // the sample grabber's interface and filter
153  CComPtr<IBaseFilter> pGrabberFilter;
154 
155  CComPtr<IMediaControl> pControl; // The DS filter graph control interface
156  CComPtr<IMediaEvent> pEvent; // The DS filter graph event interface
157 
158  vpDirectShowSampleGrabberI sgCB; // Interface used to implement the frame grabber callback
159 
160  HRESULT hr; // contains the result of the last operation
161 
162  static vpDirectShowDevice *deviceList; // This contains the list of the available capture devices
163  // it is shared by all the DirectShow Grabbers
164 
165  static unsigned int nbDevices; // the number of available devices
166  int currentDevice; // the number of the current device
167 
168  // flag to manage CoInitialize() and CoUnInitialze()
169  bool initCo;
170  // setup the directshow filtergraph with the first available device
171  bool initDirectShow();
172 
173  // enumerates the different video inputs
174  bool enumerate(CComPtr<IEnumMoniker> &ppVideoInputEnum);
175 
176  // selects a random video input from the enumeration and returns the
177  // associated filter
178  bool selectRandomSource(CComPtr<IEnumMoniker> &ppVideoInputEnum, CComPtr<IBaseFilter> &pCapSource);
179 
180  // creates the filter graph
181  bool createGraph();
182 
183  // creates the sample grabber
184  bool createSampleGrabber(CComPtr<IBaseFilter> &ppGrabberFilter);
185 
186  // checks the capture filter's media type and sets flags as needed
187  bool checkSourceType(CComPtr<IPin> &pCapSourcePin);
188 
189  // connects the filters as needed
190  bool connectSourceToGrabber(CComPtr<IBaseFilter> &pCapSource, CComPtr<IBaseFilter> &pGrabberFilter);
191 
192  // used to convert HRESULT-associated error message to a string
193  void HRtoStr(std::string &str);
194 
195  // create the list of the available devices
196  bool createDeviceList(CComPtr<IEnumMoniker> &ppVideoInputEnum);
197 
198  // get the n-th device if it is available
199  bool getDevice(unsigned int n, CComPtr<IBaseFilter> &ppCapSource);
200 
201  // get the first available device if any
202  unsigned int getFirstUnusedDevice(CComPtr<IBaseFilter> &ppDevice);
203 
204  // removes all the filters in the graph
205  bool removeAll();
206 
207  // Deletes an allocated AM_MEDIA_TYPE structure, including the format block
208  void MyDeleteMediaType(AM_MEDIA_TYPE *pmt);
209 
210  // Frees the format block in an AM_MEDIA_TYPE structure
211  void MyFreeMediaType(AM_MEDIA_TYPE &mt);
212 };
213 END_VISP_NAMESPACE
214 #endif
215 #endif
216 #endif
Base class for all video devices. It is designed to provide a front end to video sources.
virtual void open(vpImage< unsigned char > &I)=0
virtual void acquire(vpImage< unsigned char > &I)=0
virtual void close()=0