Visual Servoing Platform  version 3.1.0
vpV4l2Grabber.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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/kernel.h>
54 #include <linux/types.h>
55 #include <linux/videodev2.h> // Video For Linux Two interface
56 
57 #include <visp3/core/vpFrameGrabber.h>
58 #include <visp3/core/vpImage.h>
59 #include <visp3/core/vpRGBa.h>
60 #include <visp3/core/vpRect.h>
61 
136 class VISP_EXPORT vpV4l2Grabber : public vpFrameGrabber
137 {
138 public:
139  static const unsigned int DEFAULT_INPUT;
140  static const unsigned int DEFAULT_SCALE;
141  static const __u32 MAX_INPUTS;
142  static const __u32 MAX_NORM;
143  static const __u32 MAX_FORMAT;
144  static const unsigned int MAX_CTRL;
145  static const unsigned int MAX_BUFFERS;
146  static const unsigned int FRAME_SIZE;
147 
151  typedef enum {
153  framerate_25fps
155 
159  typedef enum {
161  V4L2_IMAGE_FORMAT
163 
167  typedef enum {
173  V4L2_MAX_FORMAT
175 
176 #ifndef DOXYGEN_SHOULD_SKIP_THIS
177  struct ng_video_fmt {
178  unsigned int pixelformat; /* VIDEO_* */
179  unsigned int width;
180  unsigned int height;
181  unsigned int bytesperline; /* zero for compressed formats */
182  };
183 
184  struct ng_video_buf {
185  struct ng_video_fmt fmt;
186  size_t size;
187  unsigned char *data;
188  int refcount;
189  };
190 #endif
191 
192  // private:
193  //#ifndef DOXYGEN_SHOULD_SKIP_THIS
194  // vpV4l2Grabber(const vpV4l2Grabber &)
195  // : fd(-1), device(), cap(), streamparm(), inp(NULL), std(NULL),
196  // fmt(NULL), ctl(NULL),
197  // fmt_v4l2(), fmt_me(), reqbufs(), buf_v4l2(NULL), buf_me(NULL),
198  // queue(0), waiton_cpt(0), index_buffer(0), m_verbose(false),
199  // m_nbuffers(3), field(0), streaming(false),
200  // m_input(vpV4l2Grabber::DEFAULT_INPUT),
201  // m_framerate(vpV4l2Grabber::framerate_25fps),
202  // m_frameformat(vpV4l2Grabber::V4L2_FRAME_FORMAT),
203  // m_pixelformat(vpV4l2Grabber::V4L2_YUYV_FORMAT)
204  // {
205  // throw vpException(vpException::functionNotImplementedError,"Not
206  // implemented!");
207  // }
208  // vpV4l2Grabber &operator=(const vpV4l2Grabber &){
209  // throw vpException(vpException::functionNotImplementedError,"Not
210  // implemented!"); return *this;
211  // }
212  //#endif
213 
214 public:
215  vpV4l2Grabber();
216  explicit vpV4l2Grabber(bool verbose);
217  vpV4l2Grabber(unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
218  vpV4l2Grabber(vpImage<unsigned char> &I, unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
219  vpV4l2Grabber(vpImage<vpRGBa> &I, unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
220  virtual ~vpV4l2Grabber();
221 
222 public:
223  void open(vpImage<unsigned char> &I);
224  void open(vpImage<vpRGBa> &I);
225 
226  void acquire(vpImage<unsigned char> &I);
227  void acquire(vpImage<unsigned char> &I, const vpRect &roi);
228  void acquire(vpImage<unsigned char> &I, struct timeval &timestamp, const vpRect &roi = vpRect());
229  void acquire(vpImage<vpRGBa> &I);
230  void acquire(vpImage<vpRGBa> &I, const vpRect &roi);
231  void acquire(vpImage<vpRGBa> &I, struct timeval &timestamp, const vpRect &roi = vpRect());
232  bool getField();
233  vpV4l2FramerateType getFramerate();
241  inline vpV4l2PixelFormatType getPixelFormat() { return (this->m_pixelformat); }
242 
243  vpV4l2Grabber &operator>>(vpImage<unsigned char> &I);
244  vpV4l2Grabber &operator>>(vpImage<vpRGBa> &I);
245 
250  void setVerboseMode(bool verbose) { this->m_verbose = verbose; };
251  void setFramerate(vpV4l2FramerateType framerate);
252 
253  void setInput(unsigned input = vpV4l2Grabber::DEFAULT_INPUT);
254 
259  inline void setWidth(unsigned w) { this->width = w; }
264  inline void setHeight(unsigned h) { this->height = h; }
265 
266  void setScale(unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
267 
279  inline void setNBuffers(unsigned nbuffers) { this->m_nbuffers = nbuffers; }
280 
287  inline void setDevice(const std::string &devname) { sprintf(device, "%s", devname.c_str()); }
296  inline void setPixelFormat(vpV4l2PixelFormatType pixelformat)
297  {
298  this->m_pixelformat = pixelformat;
299  if (this->m_pixelformat >= V4L2_MAX_FORMAT)
300  this->m_pixelformat = V4L2_RGB24_FORMAT;
301  }
302 
303  void close();
304 
305 private:
306  void setFormat();
314  inline void setFrameFormat(vpV4l2FrameFormatType frameformat) { this->m_frameformat = frameformat; }
315  void open();
316  void getCapabilities();
317  void startStreaming();
318  void stopStreaming();
319  unsigned char *waiton(__u32 &index, struct timeval &timestamp);
320  int queueBuffer();
321  void queueAll();
322  void printBufInfo(struct v4l2_buffer buf);
323 
324  int fd;
325  char device[FILENAME_MAX];
326  /* device descriptions */
327  struct v4l2_capability cap;
328  struct v4l2_streamparm streamparm;
329  struct v4l2_input *inp; //[vpV4l2Grabber::MAX_INPUTS];
330  struct v4l2_standard *std; //[vpV4l2Grabber::MAX_NORM];
331  struct v4l2_fmtdesc *fmt; //[vpV4l2Grabber::MAX_FORMAT];
332  struct v4l2_queryctrl *ctl; //[vpV4l2Grabber::MAX_CTRL*2];
333 
334  /* capture */
335  struct v4l2_format fmt_v4l2;
336  struct ng_video_fmt fmt_me;
337  struct v4l2_requestbuffers reqbufs;
338  struct v4l2_buffer *buf_v4l2; //[vpV4l2Grabber::MAX_BUFFERS];
339  struct ng_video_buf *buf_me; //[vpV4l2Grabber::MAX_BUFFERS];
340  unsigned int queue;
341  unsigned int waiton_cpt;
342  __u32 index_buffer;
343 
344  bool m_verbose;
345  unsigned m_nbuffers;
346  unsigned int field;
347  bool streaming;
348 
349  unsigned m_input;
350  vpV4l2FramerateType m_framerate;
351  vpV4l2FrameFormatType m_frameformat;
352  vpV4l2PixelFormatType m_pixelformat;
353 };
354 
355 #endif
356 #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