Visual Servoing Platform  version 3.6.0 under development (2023-09-25)
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 
42 #ifndef vpV4l2Grabber_hh
43 #define vpV4l2Grabber_hh
44 
45 #include <visp3/core/vpConfig.h>
46 
47 #ifdef VISP_HAVE_V4L2
48 
49 #include <libv4l2.h> // Video For Linux Two interface
50 #include <linux/videodev2.h> // Video For Linux Two interface
51 #include <string>
52 
53 #include <visp3/core/vpFrameGrabber.h>
54 #include <visp3/core/vpImage.h>
55 #include <visp3/core/vpRGBa.h>
56 #include <visp3/core/vpRect.h>
57 
132 class VISP_EXPORT vpV4l2Grabber : public vpFrameGrabber
133 {
134 public:
135  static const unsigned int DEFAULT_INPUT;
136  static const unsigned int DEFAULT_SCALE;
137  static const __u32 MAX_INPUTS;
138  static const __u32 MAX_NORM;
139  static const __u32 MAX_FORMAT;
140  static const unsigned int MAX_CTRL;
141  static const unsigned int MAX_BUFFERS;
142  static const unsigned int FRAME_SIZE;
143 
147  typedef enum {
149  framerate_25fps
150  } vpV4l2FramerateType;
151 
155  typedef enum {
157  V4L2_IMAGE_FORMAT
158  } vpV4l2FrameFormatType;
159 
163  typedef enum {
169  V4L2_MAX_FORMAT
170  } vpV4l2PixelFormatType;
171 
172 #ifndef DOXYGEN_SHOULD_SKIP_THIS
173  struct ng_video_fmt {
174  unsigned int pixelformat; /* VIDEO_* */
175  unsigned int width;
176  unsigned int height;
177  unsigned int bytesperline; /* zero for compressed formats */
178  };
179 
180  struct ng_video_buf {
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(NULL), std(NULL),
192  // fmt(NULL), ctl(NULL),
193  // fmt_v4l2(), fmt_me(), reqbufs(), buf_v4l2(NULL), buf_me(NULL),
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 };
350 
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:76
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
void setVerboseMode(bool verbose)
vpV4l2PixelFormatType getPixelFormat()
static const unsigned int FRAME_SIZE
static const unsigned int DEFAULT_SCALE
static const unsigned int DEFAULT_INPUT
static const __u32 MAX_FORMAT
void setHeight(unsigned h)
void setPixelFormat(vpV4l2PixelFormatType pixelformat)
void setNBuffers(unsigned nbuffers)
void setDevice(const std::string &devname)