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