Visual Servoing Platform  version 3.0.0
vpV4l2Grabber.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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 
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
148  {
150  framerate_25fps
151  } vpV4l2FramerateType;
152 
156  typedef enum
157  {
159  V4L2_IMAGE_FORMAT
160  } vpV4l2FrameFormatType;
161 
165  typedef enum {
171  V4L2_MAX_FORMAT
172  } vpV4l2PixelFormatType;
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 
183  struct ng_video_buf {
184  struct ng_video_fmt fmt;
185  size_t size;
186  unsigned char *data;
187  int refcount;
188  };
189 #endif
190 
191 //private:
192 //#ifndef DOXYGEN_SHOULD_SKIP_THIS
193 // vpV4l2Grabber(const vpV4l2Grabber &)
194 // : fd(-1), device(), cap(), streamparm(), inp(NULL), std(NULL), fmt(NULL), ctl(NULL),
195 // fps(0), fmt_v4l2(), fmt_me(), reqbufs(), buf_v4l2(NULL), buf_me(NULL), queue(0),
196 // waiton_cpt(0), index_buffer(0), m_verbose(false), m_nbuffers(3), field(0), streaming(false),
197 // m_input(vpV4l2Grabber::DEFAULT_INPUT),
198 // m_framerate(vpV4l2Grabber::framerate_25fps),
199 // m_frameformat(vpV4l2Grabber::V4L2_FRAME_FORMAT),
200 // m_pixelformat(vpV4l2Grabber::V4L2_YUYV_FORMAT)
201 // {
202 // throw vpException(vpException::functionNotImplementedError,"Not implemented!");
203 // }
204 // vpV4l2Grabber &operator=(const vpV4l2Grabber &){
205 // throw vpException(vpException::functionNotImplementedError,"Not implemented!");
206 // return *this;
207 // }
208 //#endif
209 
210 public:
211  vpV4l2Grabber();
212  vpV4l2Grabber(bool verbose);
213  vpV4l2Grabber(unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
215  unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
217  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 
225  void acquire(vpImage<unsigned char> &I, struct timeval &timestamp) ;
226  void acquire(vpImage<vpRGBa> &I) ;
227  void acquire(vpImage<vpRGBa> &I, struct timeval &timestamp) ;
228  bool getField();
229  vpV4l2FramerateType getFramerate();
238  {
239  return (this->m_pixelformat);
240  }
245  void setVerboseMode(bool verbose) {
246  this->m_verbose = verbose;
247  };
248  void setFramerate(vpV4l2FramerateType framerate);
249 
250  void setInput(unsigned input = vpV4l2Grabber::DEFAULT_INPUT) ;
251 
256  inline void setWidth(unsigned w)
257  {
258  this->width = w;
259  }
264  inline void setHeight(unsigned h)
265  {
266  this->height = h;
267  }
268 
269  void setScale(unsigned scale = vpV4l2Grabber::DEFAULT_SCALE) ;
270 
282  inline void setNBuffers(unsigned nbuffers)
283  {
284  this->m_nbuffers = nbuffers;
285  }
286 
293  inline void setDevice(const std::string &devname)
294  {
295  sprintf(device, "%s", devname.c_str());
296  }
305  inline void setPixelFormat(vpV4l2PixelFormatType pixelformat)
306  {
307  this->m_pixelformat = pixelformat;
308  if (this->m_pixelformat >= V4L2_MAX_FORMAT)
309  this->m_pixelformat = V4L2_RGB24_FORMAT;
310  }
311 
312  void close();
313 
314 private:
315 
316  void setFormat();
324  inline void setFrameFormat(vpV4l2FrameFormatType frameformat)
325  {
326  this->m_frameformat = frameformat;
327  }
328  void open();
329  void getCapabilities();
330  void startStreaming();
331  void stopStreaming();
332  unsigned char * waiton(__u32 &index, struct timeval &timestamp);
333  int queueBuffer();
334  void queueAll();
335  void printBufInfo(struct v4l2_buffer buf);
336 
337 
338  int fd;
339  char device[FILENAME_MAX];
340  /* device descriptions */
341  struct v4l2_capability cap;
342  struct v4l2_streamparm streamparm;
343  struct v4l2_input *inp; //[vpV4l2Grabber::MAX_INPUTS];
344  struct v4l2_standard *std; //[vpV4l2Grabber::MAX_NORM];
345  struct v4l2_fmtdesc *fmt; //[vpV4l2Grabber::MAX_FORMAT];
346  struct v4l2_queryctrl *ctl; //[vpV4l2Grabber::MAX_CTRL*2];
347 
348  /* capture */
349  int fps;
350  struct v4l2_format fmt_v4l2;
351  struct ng_video_fmt fmt_me;
352  struct v4l2_requestbuffers reqbufs;
353  struct v4l2_buffer *buf_v4l2; //[vpV4l2Grabber::MAX_BUFFERS];
354  struct ng_video_buf *buf_me; //[vpV4l2Grabber::MAX_BUFFERS];
355  unsigned int queue;
356  unsigned int waiton_cpt;
357  __u32 index_buffer;
358 
359  bool m_verbose;
360  unsigned m_nbuffers;
361  unsigned int field;
362  bool streaming;
363 
364  unsigned m_input;
365  vpV4l2FramerateType m_framerate;
366  vpV4l2FrameFormatType m_frameformat;
367  vpV4l2PixelFormatType m_pixelformat;
368 } ;
369 
370 #endif
371 #endif
372 
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 for the Video4Linux2 video device.
virtual void close()=0
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