Visual Servoing Platform  version 3.6.1 under development (2024-03-29)
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 
126 class VISP_EXPORT vpV4l2Grabber : public vpFrameGrabber
127 {
128 public:
129  static const unsigned int DEFAULT_INPUT;
130  static const unsigned int DEFAULT_SCALE;
131  static const __u32 MAX_INPUTS;
132  static const __u32 MAX_NORM;
133  static const __u32 MAX_FORMAT;
134  static const unsigned int MAX_CTRL;
135  static const unsigned int MAX_BUFFERS;
136  static const unsigned int FRAME_SIZE;
137 
141  typedef enum
142  {
144  framerate_25fps
145  } vpV4l2FramerateType;
146 
150  typedef enum
151  {
153  V4L2_IMAGE_FORMAT
154  } vpV4l2FrameFormatType;
155 
159  typedef enum
160  {
166  V4L2_MAX_FORMAT
167  } vpV4l2PixelFormatType;
168 
169 #ifndef DOXYGEN_SHOULD_SKIP_THIS
170  struct ng_video_fmt
171  {
172  unsigned int pixelformat; /* VIDEO_* */
173  unsigned int width;
174  unsigned int height;
175  unsigned int bytesperline; /* zero for compressed formats */
176  };
177 
178  struct ng_video_buf
179  {
180  struct ng_video_fmt fmt;
181  size_t size;
182  unsigned char *data;
183  int refcount;
184  };
185 #endif
186 
187  // private:
188  //#ifndef DOXYGEN_SHOULD_SKIP_THIS
189  // vpV4l2Grabber(const vpV4l2Grabber &)
190  // : fd(-1), device(), cap(), streamparm(), inp(nullptr), std(nullptr),
191  // fmt(nullptr), ctl(nullptr),
192  // fmt_v4l2(), fmt_me(), reqbufs(), buf_v4l2(nullptr), buf_me(nullptr),
193  // queue(0), waiton_cpt(0), index_buffer(0), m_verbose(false),
194  // m_nbuffers(3), field(0), streaming(false),
195  // m_input(vpV4l2Grabber::DEFAULT_INPUT),
196  // m_framerate(vpV4l2Grabber::framerate_25fps),
197  // m_frameformat(vpV4l2Grabber::V4L2_FRAME_FORMAT),
198  // m_pixelformat(vpV4l2Grabber::V4L2_YUYV_FORMAT)
199  // {
200  // throw vpException(vpException::functionNotImplementedError,"Not
201  // implemented!");
202  // }
203  // vpV4l2Grabber &operator=(const vpV4l2Grabber &){
204  // throw vpException(vpException::functionNotImplementedError,"Not
205  // implemented!"); return *this;
206  // }
207  //#endif
208 
209 public:
210  vpV4l2Grabber();
211  explicit vpV4l2Grabber(bool verbose);
212  vpV4l2Grabber(unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
213  vpV4l2Grabber(vpImage<unsigned char> &I, unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
214  vpV4l2Grabber(vpImage<vpRGBa> &I, unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
215  virtual ~vpV4l2Grabber();
216 
217 public:
218  void open(vpImage<unsigned char> &I);
219  void open(vpImage<vpRGBa> &I);
220 
222  void acquire(vpImage<unsigned char> &I, const vpRect &roi);
223  void acquire(vpImage<unsigned char> &I, struct timeval &timestamp, const vpRect &roi = vpRect());
224  void acquire(vpImage<vpRGBa> &I);
225  void acquire(vpImage<vpRGBa> &I, const vpRect &roi);
226  void acquire(vpImage<vpRGBa> &I, struct timeval &timestamp, const vpRect &roi = vpRect());
227  bool getField();
228  vpV4l2FramerateType getFramerate();
236  inline vpV4l2PixelFormatType getPixelFormat() { return (this->m_pixelformat); }
237 
238  vpV4l2Grabber &operator>>(vpImage<unsigned char> &I);
239  vpV4l2Grabber &operator>>(vpImage<vpRGBa> &I);
240 
245  void setVerboseMode(bool verbose) { this->m_verbose = verbose; };
246  void setFramerate(vpV4l2FramerateType framerate);
247 
248  void setInput(unsigned input = vpV4l2Grabber::DEFAULT_INPUT);
249 
254  inline void setWidth(unsigned w) { this->width = w; }
259  inline void setHeight(unsigned h) { this->height = h; }
260 
261  void setScale(unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
262 
274  inline void setNBuffers(unsigned nbuffers) { this->m_nbuffers = nbuffers; }
275 
282  inline void setDevice(const std::string &devname) { device = devname; }
291  inline void setPixelFormat(vpV4l2PixelFormatType pixelformat)
292  {
293  this->m_pixelformat = pixelformat;
294  if (this->m_pixelformat >= V4L2_MAX_FORMAT)
295  this->m_pixelformat = V4L2_RGB24_FORMAT;
296  }
297 
298  void close();
299 
300 private:
301  void setFormat();
309  inline void setFrameFormat(vpV4l2FrameFormatType frameformat) { this->m_frameformat = frameformat; }
310  void open();
311  void getCapabilities();
312  void startStreaming();
313  void stopStreaming();
314  unsigned char *waiton(__u32 &index, struct timeval &timestamp);
315  int queueBuffer();
316  void queueAll();
317  void printBufInfo(struct v4l2_buffer buf);
318 
319  int fd;
320  std::string device;
321  /* device descriptions */
322  struct v4l2_capability cap;
323  struct v4l2_streamparm streamparm;
324  struct v4l2_input *inp; //[vpV4l2Grabber::MAX_INPUTS];
325  struct v4l2_standard *std; //[vpV4l2Grabber::MAX_NORM];
326  struct v4l2_fmtdesc *fmt; //[vpV4l2Grabber::MAX_FORMAT];
327  struct v4l2_queryctrl *ctl; //[vpV4l2Grabber::MAX_CTRL*2];
328 
329  /* capture */
330  struct v4l2_format fmt_v4l2;
331  struct ng_video_fmt fmt_me;
332  struct v4l2_requestbuffers reqbufs;
333  struct v4l2_buffer *buf_v4l2; //[vpV4l2Grabber::MAX_BUFFERS];
334  struct ng_video_buf *buf_me; //[vpV4l2Grabber::MAX_BUFFERS];
335  unsigned int queue;
336  unsigned int waiton_cpt;
337  __u32 index_buffer;
338 
339  bool m_verbose;
340  unsigned m_nbuffers;
341  unsigned int field;
342  bool streaming;
343 
344  unsigned m_input;
345  vpV4l2FramerateType m_framerate;
346  vpV4l2FrameFormatType m_frameformat;
347  vpV4l2PixelFormatType m_pixelformat;
348 };
349 
350 #endif
351 #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)