Visual Servoing Platform  version 3.6.1 under development (2024-06-21)
vpV4l2Grabber.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  * Video for linux two framegrabber.
33  *
34 *****************************************************************************/
35 
41 #ifndef vpV4l2Grabber_hh
42 #define vpV4l2Grabber_hh
43 
44 #include <visp3/core/vpConfig.h>
45 
46 #ifdef VISP_HAVE_V4L2
47 
48 #include <libv4l2.h> // Video For Linux Two interface
49 #include <linux/videodev2.h> // Video For Linux Two interface
50 #include <string>
51 
52 #include <visp3/core/vpFrameGrabber.h>
53 #include <visp3/core/vpImage.h>
54 #include <visp3/core/vpRGBa.h>
55 #include <visp3/core/vpRect.h>
56 
127 class VISP_EXPORT vpV4l2Grabber : public vpFrameGrabber
128 {
129 public:
130  static const unsigned int DEFAULT_INPUT;
131  static const unsigned int DEFAULT_SCALE;
132  static const __u32 MAX_INPUTS;
133  static const __u32 MAX_NORM;
134  static const __u32 MAX_FORMAT;
135  static const unsigned int MAX_CTRL;
136  static const unsigned int MAX_BUFFERS;
137  static const unsigned int FRAME_SIZE;
138 
142  typedef enum
143  {
145  framerate_25fps
146  } vpV4l2FramerateType;
147 
151  typedef enum
152  {
154  V4L2_IMAGE_FORMAT
155  } vpV4l2FrameFormatType;
156 
160  typedef enum
161  {
167  V4L2_MAX_FORMAT
168  } vpV4l2PixelFormatType;
169 
170 #ifndef DOXYGEN_SHOULD_SKIP_THIS
171  struct ng_video_fmt
172  {
173  unsigned int pixelformat; /* VIDEO_* */
174  unsigned int width;
175  unsigned int height;
176  unsigned int bytesperline; /* zero for compressed formats */
177  };
178 
179  struct ng_video_buf
180  {
181  struct ng_video_fmt fmt;
182  size_t size;
183  unsigned char *data;
184  int refcount;
185  };
186 #endif
187 
188  // private:
189  //#ifndef DOXYGEN_SHOULD_SKIP_THIS
190  // vpV4l2Grabber(const vpV4l2Grabber &)
191  // : fd(-1), device(), cap(), streamparm(), inp(nullptr), std(nullptr),
192  // fmt(nullptr), ctl(nullptr),
193  // fmt_v4l2(), fmt_me(), reqbufs(), buf_v4l2(nullptr), buf_me(nullptr),
194  // queue(0), waiton_cpt(0), index_buffer(0), m_verbose(false),
195  // m_nbuffers(3), field(0), streaming(false),
196  // m_input(vpV4l2Grabber::DEFAULT_INPUT),
197  // m_framerate(vpV4l2Grabber::framerate_25fps),
198  // m_frameformat(vpV4l2Grabber::V4L2_FRAME_FORMAT),
199  // m_pixelformat(vpV4l2Grabber::V4L2_YUYV_FORMAT)
200  // {
201  // throw vpException(vpException::functionNotImplementedError,"Not
202  // implemented!");
203  // }
204  // vpV4l2Grabber &operator=(const vpV4l2Grabber &){
205  // throw vpException(vpException::functionNotImplementedError,"Not
206  // implemented!"); return *this;
207  // }
208  //#endif
209 
210 public:
211  vpV4l2Grabber();
212  explicit vpV4l2Grabber(bool verbose);
213  vpV4l2Grabber(unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
214  vpV4l2Grabber(vpImage<unsigned char> &I, unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
215  vpV4l2Grabber(vpImage<vpRGBa> &I, unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
216  virtual ~vpV4l2Grabber();
217 
218 public:
219  void open(vpImage<unsigned char> &I);
220  void open(vpImage<vpRGBa> &I);
221 
223  void acquire(vpImage<unsigned char> &I, const vpRect &roi);
224  void acquire(vpImage<unsigned char> &I, struct timeval &timestamp, const vpRect &roi = vpRect());
225  void acquire(vpImage<vpRGBa> &I);
226  void acquire(vpImage<vpRGBa> &I, const vpRect &roi);
227  void acquire(vpImage<vpRGBa> &I, struct timeval &timestamp, const vpRect &roi = vpRect());
228  bool getField();
229  vpV4l2FramerateType getFramerate();
237  inline vpV4l2PixelFormatType getPixelFormat() { return (this->m_pixelformat); }
238 
239  vpV4l2Grabber &operator>>(vpImage<unsigned char> &I);
240  vpV4l2Grabber &operator>>(vpImage<vpRGBa> &I);
241 
246  void setVerboseMode(bool verbose) { this->m_verbose = verbose; };
247  void setFramerate(vpV4l2FramerateType framerate);
248 
249  void setInput(unsigned input = vpV4l2Grabber::DEFAULT_INPUT);
250 
255  inline void setWidth(unsigned w) { this->width = w; }
260  inline void setHeight(unsigned h) { this->height = h; }
261 
262  void setScale(unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
263 
275  inline void setNBuffers(unsigned nbuffers) { this->m_nbuffers = nbuffers; }
276 
283  inline void setDevice(const std::string &devname) { device = devname; }
292  inline void setPixelFormat(vpV4l2PixelFormatType pixelformat)
293  {
294  this->m_pixelformat = pixelformat;
295  if (this->m_pixelformat >= V4L2_MAX_FORMAT)
296  this->m_pixelformat = V4L2_RGB24_FORMAT;
297  }
298 
299  void close();
300 
301 private:
302  void setFormat();
310  inline void setFrameFormat(vpV4l2FrameFormatType frameformat) { this->m_frameformat = frameformat; }
311  void open();
312  void getCapabilities();
313  void startStreaming();
314  void stopStreaming();
315  unsigned char *waiton(__u32 &index, struct timeval &timestamp);
316  int queueBuffer();
317  void queueAll();
318  void printBufInfo(struct v4l2_buffer buf);
319 
320  int fd;
321  std::string device;
322  /* device descriptions */
323  struct v4l2_capability cap;
324  struct v4l2_streamparm streamparm;
325  struct v4l2_input *inp; //[vpV4l2Grabber::MAX_INPUTS];
326  struct v4l2_standard *std; //[vpV4l2Grabber::MAX_NORM];
327  struct v4l2_fmtdesc *fmt; //[vpV4l2Grabber::MAX_FORMAT];
328  struct v4l2_queryctrl *ctl; //[vpV4l2Grabber::MAX_CTRL*2];
329 
330  /* capture */
331  struct v4l2_format fmt_v4l2;
332  struct ng_video_fmt fmt_me;
333  struct v4l2_requestbuffers reqbufs;
334  struct v4l2_buffer *buf_v4l2; //[vpV4l2Grabber::MAX_BUFFERS];
335  struct ng_video_buf *buf_me; //[vpV4l2Grabber::MAX_BUFFERS];
336  unsigned int queue;
337  unsigned int waiton_cpt;
338  __u32 index_buffer;
339 
340  bool m_verbose;
341  unsigned m_nbuffers;
342  unsigned int field;
343  bool streaming;
344 
345  unsigned m_input;
346  vpV4l2FramerateType m_framerate;
347  vpV4l2FrameFormatType m_frameformat;
348  vpV4l2PixelFormatType m_pixelformat;
349 };
351 #endif
352 #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
Defines a rectangle in the plane.
Definition: vpRect.h:79
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
@ framerate_50fps
50 frames per second
void setWidth(unsigned w)
static const __u32 MAX_NORM
static const unsigned int MAX_CTRL
static const unsigned int MAX_BUFFERS
static const __u32 MAX_INPUTS
static const unsigned int DEFAULT_INPUT
void setVerboseMode(bool verbose)
vpV4l2PixelFormatType getPixelFormat()
static const unsigned int FRAME_SIZE
static const unsigned int DEFAULT_SCALE
static const __u32 MAX_FORMAT
void setHeight(unsigned h)
void setPixelFormat(vpV4l2PixelFormatType pixelformat)
void setNBuffers(unsigned nbuffers)
void setDevice(const std::string &devname)