Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
vpV4l2Grabber.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
45 #ifndef vpV4l2Grabber_hh
46 #define vpV4l2Grabber_hh
47 
48 #include <visp3/core/vpConfig.h>
49 
50 #ifdef VISP_HAVE_V4L2
51 
52 #include <libv4l2.h> // Video For Linux Two interface
53 #include <linux/videodev2.h> // Video For Linux Two interface
54 
55 #include <visp3/core/vpFrameGrabber.h>
56 #include <visp3/core/vpImage.h>
57 #include <visp3/core/vpRGBa.h>
58 #include <visp3/core/vpRect.h>
59 
134 class VISP_EXPORT vpV4l2Grabber : public vpFrameGrabber
135 {
136 public:
137  static const unsigned int DEFAULT_INPUT;
138  static const unsigned int DEFAULT_SCALE;
139  static const __u32 MAX_INPUTS;
140  static const __u32 MAX_NORM;
141  static const __u32 MAX_FORMAT;
142  static const unsigned int MAX_CTRL;
143  static const unsigned int MAX_BUFFERS;
144  static const unsigned int FRAME_SIZE;
145 
149  typedef enum {
151  framerate_25fps
153 
157  typedef enum {
159  V4L2_IMAGE_FORMAT
161 
165  typedef enum {
171  V4L2_MAX_FORMAT
173 
174 #ifndef DOXYGEN_SHOULD_SKIP_THIS
175  struct ng_video_fmt {
176  unsigned int pixelformat; /* VIDEO_* */
177  unsigned int width;
178  unsigned int height;
179  unsigned int bytesperline; /* zero for compressed formats */
180  };
181 
182  struct ng_video_buf {
183  struct ng_video_fmt fmt;
184  size_t size;
185  unsigned char *data;
186  int refcount;
187  };
188 #endif
189 
190  // private:
191  //#ifndef DOXYGEN_SHOULD_SKIP_THIS
192  // vpV4l2Grabber(const vpV4l2Grabber &)
193  // : fd(-1), device(), cap(), streamparm(), inp(NULL), std(NULL),
194  // fmt(NULL), ctl(NULL),
195  // fmt_v4l2(), fmt_me(), reqbufs(), buf_v4l2(NULL), buf_me(NULL),
196  // queue(0), waiton_cpt(0), index_buffer(0), m_verbose(false),
197  // m_nbuffers(3), field(0), streaming(false),
198  // m_input(vpV4l2Grabber::DEFAULT_INPUT),
199  // m_framerate(vpV4l2Grabber::framerate_25fps),
200  // m_frameformat(vpV4l2Grabber::V4L2_FRAME_FORMAT),
201  // m_pixelformat(vpV4l2Grabber::V4L2_YUYV_FORMAT)
202  // {
203  // throw vpException(vpException::functionNotImplementedError,"Not
204  // implemented!");
205  // }
206  // vpV4l2Grabber &operator=(const vpV4l2Grabber &){
207  // throw vpException(vpException::functionNotImplementedError,"Not
208  // implemented!"); return *this;
209  // }
210  //#endif
211 
212 public:
213  vpV4l2Grabber();
214  explicit vpV4l2Grabber(bool verbose);
215  vpV4l2Grabber(unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
216  vpV4l2Grabber(vpImage<unsigned char> &I, unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
217  vpV4l2Grabber(vpImage<vpRGBa> &I, unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
218  virtual ~vpV4l2Grabber();
219 
220 public:
221  void open(vpImage<unsigned char> &I);
222  void open(vpImage<vpRGBa> &I);
223 
224  void acquire(vpImage<unsigned char> &I);
225  void acquire(vpImage<unsigned char> &I, const vpRect &roi);
226  void acquire(vpImage<unsigned char> &I, struct timeval &timestamp, const vpRect &roi = vpRect());
227  void acquire(vpImage<vpRGBa> &I);
228  void acquire(vpImage<vpRGBa> &I, const vpRect &roi);
229  void acquire(vpImage<vpRGBa> &I, struct timeval &timestamp, const vpRect &roi = vpRect());
230  bool getField();
231  vpV4l2FramerateType getFramerate();
239  inline vpV4l2PixelFormatType getPixelFormat() { return (this->m_pixelformat); }
240 
241  vpV4l2Grabber &operator>>(vpImage<unsigned char> &I);
242  vpV4l2Grabber &operator>>(vpImage<vpRGBa> &I);
243 
248  void setVerboseMode(bool verbose) { this->m_verbose = verbose; };
249  void setFramerate(vpV4l2FramerateType framerate);
250 
251  void setInput(unsigned input = vpV4l2Grabber::DEFAULT_INPUT);
252 
257  inline void setWidth(unsigned w) { this->width = w; }
262  inline void setHeight(unsigned h) { this->height = h; }
263 
264  void setScale(unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
265 
277  inline void setNBuffers(unsigned nbuffers) { this->m_nbuffers = nbuffers; }
278 
285  inline void setDevice(const std::string &devname) { sprintf(device, "%s", devname.c_str()); }
294  inline void setPixelFormat(vpV4l2PixelFormatType pixelformat)
295  {
296  this->m_pixelformat = pixelformat;
297  if (this->m_pixelformat >= V4L2_MAX_FORMAT)
298  this->m_pixelformat = V4L2_RGB24_FORMAT;
299  }
300 
301  void close();
302 
303 private:
304  void setFormat();
312  inline void setFrameFormat(vpV4l2FrameFormatType frameformat) { this->m_frameformat = frameformat; }
313  void open();
314  void getCapabilities();
315  void startStreaming();
316  void stopStreaming();
317  unsigned char *waiton(__u32 &index, struct timeval &timestamp);
318  int queueBuffer();
319  void queueAll();
320  void printBufInfo(struct v4l2_buffer buf);
321 
322  int fd;
323  char device[FILENAME_MAX];
324  /* device descriptions */
325  struct v4l2_capability cap;
326  struct v4l2_streamparm streamparm;
327  struct v4l2_input *inp; //[vpV4l2Grabber::MAX_INPUTS];
328  struct v4l2_standard *std; //[vpV4l2Grabber::MAX_NORM];
329  struct v4l2_fmtdesc *fmt; //[vpV4l2Grabber::MAX_FORMAT];
330  struct v4l2_queryctrl *ctl; //[vpV4l2Grabber::MAX_CTRL*2];
331 
332  /* capture */
333  struct v4l2_format fmt_v4l2;
334  struct ng_video_fmt fmt_me;
335  struct v4l2_requestbuffers reqbufs;
336  struct v4l2_buffer *buf_v4l2; //[vpV4l2Grabber::MAX_BUFFERS];
337  struct ng_video_buf *buf_me; //[vpV4l2Grabber::MAX_BUFFERS];
338  unsigned int queue;
339  unsigned int waiton_cpt;
340  __u32 index_buffer;
341 
342  bool m_verbose;
343  unsigned m_nbuffers;
344  unsigned int field;
345  bool streaming;
346 
347  unsigned m_input;
348  vpV4l2FramerateType m_framerate;
349  vpV4l2FrameFormatType m_frameformat;
350  vpV4l2PixelFormatType m_pixelformat;
351 };
352 
353 #endif
354 #endif
static const unsigned int MAX_CTRL
50 frames per second
static const __u32 MAX_FORMAT
void setDevice(const std::string &devname)
vpV4l2PixelFormatType getPixelFormat()
void setVerboseMode(bool verbose)
static const unsigned int DEFAULT_INPUT
static const unsigned int FRAME_SIZE
void setHeight(unsigned h)
static const __u32 MAX_INPUTS
void setWidth(unsigned w)
static const __u32 MAX_NORM
Base class for all video devices. It is designed to provide a front end to video sources.
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
Defines a rectangle in the plane.
Definition: vpRect.h:78
void setNBuffers(unsigned nbuffers)
void setPixelFormat(vpV4l2PixelFormatType pixelformat)
static const unsigned int DEFAULT_SCALE
static const unsigned int MAX_BUFFERS