Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Video for linux two framegrabber.
32  *
33  * Authors:
34  * Fabien Spindler
35  *
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 <linux/types.h>
53 #include <linux/kernel.h>
54 #include <linux/videodev2.h> // Video For Linux Two interface
55 #include <libv4l2.h> // Video For Linux Two interface
56 
57 #include <visp3/core/vpImage.h>
58 #include <visp3/core/vpFrameGrabber.h>
59 #include <visp3/core/vpRGBa.h>
60 #include <visp3/core/vpRect.h>
61 
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
150  {
152  framerate_25fps
153  } vpV4l2FramerateType;
154 
158  typedef enum
159  {
161  V4L2_IMAGE_FORMAT
162  } vpV4l2FrameFormatType;
163 
167  typedef enum {
173  V4L2_MAX_FORMAT
174  } vpV4l2PixelFormatType;
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 
185  struct ng_video_buf {
186  struct ng_video_fmt fmt;
187  size_t size;
188  unsigned char *data;
189  int refcount;
190  };
191 #endif
192 
193 //private:
194 //#ifndef DOXYGEN_SHOULD_SKIP_THIS
195 // vpV4l2Grabber(const vpV4l2Grabber &)
196 // : fd(-1), device(), cap(), streamparm(), inp(NULL), std(NULL), fmt(NULL), ctl(NULL),
197 // fmt_v4l2(), fmt_me(), reqbufs(), buf_v4l2(NULL), buf_me(NULL), queue(0),
198 // waiton_cpt(0), index_buffer(0), m_verbose(false), m_nbuffers(3), field(0), streaming(false),
199 // m_input(vpV4l2Grabber::DEFAULT_INPUT),
200 // m_framerate(vpV4l2Grabber::framerate_25fps),
201 // m_frameformat(vpV4l2Grabber::V4L2_FRAME_FORMAT),
202 // m_pixelformat(vpV4l2Grabber::V4L2_YUYV_FORMAT)
203 // {
204 // throw vpException(vpException::functionNotImplementedError,"Not implemented!");
205 // }
206 // vpV4l2Grabber &operator=(const vpV4l2Grabber &){
207 // throw vpException(vpException::functionNotImplementedError,"Not implemented!");
208 // return *this;
209 // }
210 //#endif
211 
212 public:
213  vpV4l2Grabber();
214  vpV4l2Grabber(bool verbose);
215  vpV4l2Grabber(unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
217  unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
219  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 
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();
242  {
243  return (this->m_pixelformat);
244  }
245 
246  vpV4l2Grabber & operator>>(vpImage<unsigned char> &I);
247  vpV4l2Grabber & operator>>(vpImage<vpRGBa> &I);
248 
253  void setVerboseMode(bool verbose) {
254  this->m_verbose = verbose;
255  };
256  void setFramerate(vpV4l2FramerateType framerate);
257 
258  void setInput(unsigned input = vpV4l2Grabber::DEFAULT_INPUT) ;
259 
264  inline void setWidth(unsigned w)
265  {
266  this->width = w;
267  }
272  inline void setHeight(unsigned h)
273  {
274  this->height = h;
275  }
276 
277  void setScale(unsigned scale = vpV4l2Grabber::DEFAULT_SCALE) ;
278 
290  inline void setNBuffers(unsigned nbuffers)
291  {
292  this->m_nbuffers = nbuffers;
293  }
294 
301  inline void setDevice(const std::string &devname)
302  {
303  sprintf(device, "%s", devname.c_str());
304  }
313  inline void setPixelFormat(vpV4l2PixelFormatType pixelformat)
314  {
315  this->m_pixelformat = pixelformat;
316  if (this->m_pixelformat >= V4L2_MAX_FORMAT)
317  this->m_pixelformat = V4L2_RGB24_FORMAT;
318  }
319 
320  void close();
321 
322 private:
323 
324  void setFormat();
332  inline void setFrameFormat(vpV4l2FrameFormatType frameformat)
333  {
334  this->m_frameformat = frameformat;
335  }
336  void open();
337  void getCapabilities();
338  void startStreaming();
339  void stopStreaming();
340  unsigned char * waiton(__u32 &index, struct timeval &timestamp);
341  int queueBuffer();
342  void queueAll();
343  void printBufInfo(struct v4l2_buffer buf);
344 
345  int fd;
346  char device[FILENAME_MAX];
347  /* device descriptions */
348  struct v4l2_capability cap;
349  struct v4l2_streamparm streamparm;
350  struct v4l2_input *inp; //[vpV4l2Grabber::MAX_INPUTS];
351  struct v4l2_standard *std; //[vpV4l2Grabber::MAX_NORM];
352  struct v4l2_fmtdesc *fmt; //[vpV4l2Grabber::MAX_FORMAT];
353  struct v4l2_queryctrl *ctl; //[vpV4l2Grabber::MAX_CTRL*2];
354 
355  /* capture */
356  struct v4l2_format fmt_v4l2;
357  struct ng_video_fmt fmt_me;
358  struct v4l2_requestbuffers reqbufs;
359  struct v4l2_buffer *buf_v4l2; //[vpV4l2Grabber::MAX_BUFFERS];
360  struct ng_video_buf *buf_me; //[vpV4l2Grabber::MAX_BUFFERS];
361  unsigned int queue;
362  unsigned int waiton_cpt;
363  __u32 index_buffer;
364 
365  bool m_verbose;
366  unsigned m_nbuffers;
367  unsigned int field;
368  bool streaming;
369 
370  unsigned m_input;
371  vpV4l2FramerateType m_framerate;
372  vpV4l2FrameFormatType m_frameformat;
373  vpV4l2PixelFormatType m_pixelformat;
374 } ;
375 
376 #endif
377 #endif
378 
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
virtual void open(vpImage< unsigned char > &I)=0
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.
virtual void close()=0
Defines a rectangle in the plane.
Definition: vpRect.h:82
virtual void acquire(vpImage< unsigned char > &I)=0
void setNBuffers(unsigned nbuffers)
void setPixelFormat(vpV4l2PixelFormatType pixelformat)
static const unsigned int DEFAULT_SCALE
static const unsigned int MAX_BUFFERS